diff mbox

[fortran] gfc_add_field_to_struct refactoring

Message ID 4C2F0964.3070801@sfr.fr
State New
Headers show

Commit Message

Mikael Morin July 3, 2010, 9:56 a.m. UTC
This is a followup to the patch by Nathan Froyd :
http://gcc.gnu.org/ml/gcc-patches/2010-07/msg00213.html
where some questionable iso_c code was forcing to have one extra 
argument to the gfc_add_field_to_struct function to set TYPE_FIELDS from 
outside of it.

That code was creating a FIELD_DECL and adding it, as backend_decl for 
C_PTR's hidden C_ADDRESS field, and as TYPE_FIELDS for the containing 
type (which is a void pointer type).
As the field is hidden, it should not be accessible from the front-end, 
and as the containing type is a void pointer type, the middle-end should 
not try to access its TYPE_FIELDS.

Thus, I'm removing the code and proceeding with refactoring.

The testsuite run is more than halfway now without new failures
OK for trunk, if it passes?

Mikael
2010-07-03  Mikael Morin  <mikael@gcc.gnu.org>

	* trans-io.c (gfc_build_st_parameter): Update calls to
	gfc_add_field_to_struct.
	* trans-stmt.c (ADD_FIELD): Ditto.
	* trans-types.c
	(gfc_get_derived_type): Ditto. Don't create backend_decl for C_PTR's
	C_ADDRESS field. 
	(gfc_add_field_to_struct_1): Set TYPE_FIELDS(context) instead of
	fieldlist, remove fieldlist from argument list.
	(gfc_add_field_to_struct): Update call to gfc_add_field_to_struct_1
	and remove fieldlist from argument list. 
	(gfc_get_desc_dim_type, gfc_get_array_descriptor_base,
	gfc_get_mixed_entry_union): Move setting
	TYPE_FIELDS to gfc_add_field_to_struct_1 and update calls to it.
	* trans-types.h (gfc_add_field_to_struct): Update prototype.

Comments

Tobias Burnus July 8, 2010, 8:07 p.m. UTC | #1
Mikael Morin wrote:
> That code was creating a FIELD_DECL and adding it, as backend_decl for
> C_PTR's hidden C_ADDRESS field, and as TYPE_FIELDS for the containing
> type (which is a void pointer type).
> As the field is hidden, it should not be accessible from the
> front-end, and as the containing type is a void pointer type, the
> middle-end should not try to access its TYPE_FIELDS.
>
> The testsuite run is more than halfway now without new failures
> OK for trunk, if it passes?

OK. Thanks for the patch!

Tobias
Mikael Morin July 10, 2010, 6:17 p.m. UTC | #2
Le 08.07.2010 22:07, Tobias Burnus a écrit :
>
> Mikael Morin wrote:
>> That code was creating a FIELD_DECL and adding it, as backend_decl for
>> C_PTR's hidden C_ADDRESS field, and as TYPE_FIELDS for the containing
>> type (which is a void pointer type).
>> As the field is hidden, it should not be accessible from the
>> front-end, and as the containing type is a void pointer type, the
>> middle-end should not try to access its TYPE_FIELDS.
>>
>> The testsuite run is more than halfway now without new failures
>> OK for trunk, if it passes?
>
> OK. Thanks for the patch!
>
> Tobias
>
Thanks for looking.
It is in at revision 162042.

Mikael
diff mbox

Patch

diff --git a/trans-io.c b/trans-io.c
index 9926d2f..e602c1f 100644
--- a/trans-io.c
+++ b/trans-io.c
@@ -176,13 +176,11 @@  gfc_build_st_parameter (enum ioparam_type ptype, tree *types)
 	case IOPARM_type_parray:
 	case IOPARM_type_pchar:
 	case IOPARM_type_pad:
-	  p->field = gfc_add_field_to_struct (&TYPE_FIELDS (t), t,
-					      get_identifier (p->name),
+	  p->field = gfc_add_field_to_struct (t, get_identifier (p->name),
 					      types[p->type], &chain);
 	  break;
 	case IOPARM_type_char1:
-	  p->field = gfc_add_field_to_struct (&TYPE_FIELDS (t), t,
-					      get_identifier (p->name),
+	  p->field = gfc_add_field_to_struct (t, get_identifier (p->name),
 					      pchar_type_node, &chain);
 	  /* FALLTHROUGH */
 	case IOPARM_type_char2:
@@ -190,18 +188,16 @@  gfc_build_st_parameter (enum ioparam_type ptype, tree *types)
 	  gcc_assert (len <= sizeof (name) - sizeof ("_len"));
 	  memcpy (name, p->name, len);
 	  memcpy (name + len, "_len", sizeof ("_len"));
-	  p->field_len = gfc_add_field_to_struct (&TYPE_FIELDS (t), t,
-						  get_identifier (name),
+	  p->field_len = gfc_add_field_to_struct (t, get_identifier (name),
 						  gfc_charlen_type_node,
 						  &chain);
 	  if (p->type == IOPARM_type_char2)
-	    p->field = gfc_add_field_to_struct (&TYPE_FIELDS (t), t,
-						get_identifier (p->name),
+	    p->field = gfc_add_field_to_struct (t, get_identifier (p->name),
 						pchar_type_node, &chain);
 	  break;
 	case IOPARM_type_common:
 	  p->field
-	    = gfc_add_field_to_struct (&TYPE_FIELDS (t), t,
+	    = gfc_add_field_to_struct (t,
 				       get_identifier (p->name),
 				       st_parameter[IOPARM_ptype_common].type,
 				       &chain);
diff --git a/trans-stmt.c b/trans-stmt.c
index 15f2acb..0c1e8f6 100644
--- a/trans-stmt.c
+++ b/trans-stmt.c
@@ -1644,10 +1644,11 @@  gfc_trans_character_select (gfc_code *code)
 	gcc_unreachable ();
 
 #undef ADD_FIELD
-#define ADD_FIELD(NAME, TYPE)					\
-  ss_##NAME[k] = gfc_add_field_to_struct				\
-     (&(TYPE_FIELDS (select_struct[k])), select_struct[k],	\
-      get_identifier (stringize(NAME)), TYPE, &chain)
+#define ADD_FIELD(NAME, TYPE)						    \
+  ss_##NAME[k] = gfc_add_field_to_struct (select_struct[k],		    \
+					  get_identifier (stringize(NAME)), \
+					  TYPE,				    \
+					  &chain)
 
       ADD_FIELD (string1, pchartype);
       ADD_FIELD (string1_len, gfc_charlen_type_node);
diff --git a/trans-types.c b/trans-types.c
index f4e78c2..aff42d1 100644
--- a/trans-types.c
+++ b/trans-types.c
@@ -86,7 +86,7 @@  gfc_character_info gfc_character_kinds[MAX_CHARACTER_KINDS + 1];
 static GTY(()) tree gfc_character_types[MAX_CHARACTER_KINDS + 1];
 static GTY(()) tree gfc_pcharacter_types[MAX_CHARACTER_KINDS + 1];
 
-static tree gfc_add_field_to_struct_1 (tree *, tree, tree, tree, tree **);
+static tree gfc_add_field_to_struct_1 (tree, tree, tree, tree **);
 
 /* The integer kind to use for array indices.  This will be set to the
    proper value based on target information from the backend.  */
@@ -1233,7 +1233,7 @@  static tree
 gfc_get_desc_dim_type (void)
 {
   tree type;
-  tree fieldlist = NULL_TREE, decl, *chain = NULL;
+  tree decl, *chain = NULL;
 
   if (gfc_desc_dim_type)
     return gfc_desc_dim_type;
@@ -1245,24 +1245,22 @@  gfc_get_desc_dim_type (void)
   TYPE_PACKED (type) = 1;
 
   /* Consists of the stride, lbound and ubound members.  */
-  decl = gfc_add_field_to_struct_1 (&fieldlist, type,
+  decl = gfc_add_field_to_struct_1 (type,
 				    get_identifier ("stride"),
 				    gfc_array_index_type, &chain);
   TREE_NO_WARNING (decl) = 1;
 
-  decl = gfc_add_field_to_struct_1 (&fieldlist, type,
+  decl = gfc_add_field_to_struct_1 (type,
 				    get_identifier ("lbound"),
 				    gfc_array_index_type, &chain);
   TREE_NO_WARNING (decl) = 1;
 
-  decl = gfc_add_field_to_struct_1 (&fieldlist, type,
+  decl = gfc_add_field_to_struct_1 (type,
 				    get_identifier ("ubound"),
 				    gfc_array_index_type, &chain);
   TREE_NO_WARNING (decl) = 1;
 
   /* Finish off the type.  */
-  TYPE_FIELDS (type) = fieldlist;
-
   gfc_finish_type (type);
   TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type)) = 1;
 
@@ -1534,7 +1532,7 @@  gfc_get_nodesc_array_type (tree etype, gfc_array_spec * as, gfc_packed packed,
 static tree
 gfc_get_array_descriptor_base (int dimen, int codimen, bool restricted)
 {
-  tree fat_type, fieldlist = NULL_TREE, decl, arraytype, *chain = NULL;
+  tree fat_type, decl, arraytype, *chain = NULL;
   char name[16 + 2*GFC_RANK_DIGITS + 1 + 1];
   int idx = 2 * (codimen + dimen - 1) + restricted;
 
@@ -1549,20 +1547,20 @@  gfc_get_array_descriptor_base (int dimen, int codimen, bool restricted)
   TYPE_NAME (fat_type) = get_identifier (name);
 
   /* Add the data member as the first element of the descriptor.  */
-  decl = gfc_add_field_to_struct_1 (&fieldlist, fat_type,
+  decl = gfc_add_field_to_struct_1 (fat_type,
 				    get_identifier ("data"),
 				    (restricted
 				     ? prvoid_type_node
 				     : ptr_type_node), &chain);
 
   /* Add the base component.  */
-  decl = gfc_add_field_to_struct_1 (&fieldlist, fat_type,
+  decl = gfc_add_field_to_struct_1 (fat_type,
 				    get_identifier ("offset"),
 				    gfc_array_index_type, &chain);
   TREE_NO_WARNING (decl) = 1;
 
   /* Add the dtype component.  */
-  decl = gfc_add_field_to_struct_1 (&fieldlist, fat_type,
+  decl = gfc_add_field_to_struct_1 (fat_type,
 				    get_identifier ("dtype"),
 				    gfc_array_index_type, &chain);
   TREE_NO_WARNING (decl) = 1;
@@ -1574,14 +1572,12 @@  gfc_get_array_descriptor_base (int dimen, int codimen, bool restricted)
 					gfc_index_zero_node,
 					gfc_rank_cst[codimen + dimen - 1]));
 
-  decl = gfc_add_field_to_struct_1 (&fieldlist, fat_type,
+  decl = gfc_add_field_to_struct_1 (fat_type,
 				    get_identifier ("dim"),
 				    arraytype, &chain);
   TREE_NO_WARNING (decl) = 1;
 
   /* Finish off the type.  */
-  TYPE_FIELDS (fat_type) = fieldlist;
-
   gfc_finish_type (fat_type);
   TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (fat_type)) = 1;
 
@@ -1842,20 +1838,19 @@  gfc_finish_type (tree type)
 
 /* Add a field of given NAME and TYPE to the context of a UNION_TYPE
    or RECORD_TYPE pointed to by CONTEXT.  The new field is chained
-   to the fieldlist pointed to by FIELDLIST through *CHAIN.
+   to the end of the field list pointed to by *CHAIN.
 
    Returns a pointer to the new field.  */
 
 static tree
-gfc_add_field_to_struct_1 (tree *fieldlist, tree context,
-				 tree name, tree type, tree **chain)
+gfc_add_field_to_struct_1 (tree context, tree name, tree type, tree **chain)
 {
   tree decl = build_decl (input_location, FIELD_DECL, name, type);
 
   DECL_CONTEXT (decl) = context;
   TREE_CHAIN (decl) = NULL_TREE;
-  if (*fieldlist == NULL_TREE)
-    *fieldlist = decl;
+  if (TYPE_FIELDS (context) == NULL_TREE)
+    TYPE_FIELDS (context) = decl;
   if (chain != NULL)
     {
       if (*chain != NULL)
@@ -1870,11 +1865,9 @@  gfc_add_field_to_struct_1 (tree *fieldlist, tree context,
    information.  */
 
 tree
-gfc_add_field_to_struct (tree *fieldlist, tree context,
-			 tree name, tree type, tree **chain)
+gfc_add_field_to_struct (tree context, tree name, tree type, tree **chain)
 {
-  tree decl = gfc_add_field_to_struct_1 (fieldlist, context,
-					 name, type, chain);
+  tree decl = gfc_add_field_to_struct_1 (context, name, type, chain);
 
   DECL_INITIAL (decl) = 0;
   DECL_ALIGN (decl) = 0;
@@ -1954,7 +1947,7 @@  gfc_get_ppc_type (gfc_component* c)
 tree
 gfc_get_derived_type (gfc_symbol * derived)
 {
-  tree typenode = NULL, field = NULL, field_type = NULL, fieldlist = NULL;
+  tree typenode = NULL, field = NULL, field_type = NULL;
   tree canonical = NULL_TREE;
   tree *chain = NULL;
   bool got_canonical = false;
@@ -1976,14 +1969,6 @@  gfc_get_derived_type (gfc_symbol * derived)
       else
 	derived->backend_decl = pfunc_type_node;
 
-      /* Create a backend_decl for the __c_ptr_c_address field.  */
-      derived->components->backend_decl =
-	gfc_add_field_to_struct (&(derived->backend_decl->type.values),
-				 derived->backend_decl,
-				 get_identifier (derived->components->name),
-				 gfc_typenode_for_spec (
-				 &(derived->components->ts)), NULL);
-
       derived->ts.kind = gfc_index_integer_kind;
       derived->ts.type = BT_INTEGER;
       /* Set the f90_type to BT_VOID as a way to recognize something of type
@@ -2105,7 +2090,6 @@  gfc_get_derived_type (gfc_symbol * derived)
 
   /* Build the type member list. Install the newly created RECORD_TYPE
      node as DECL_CONTEXT of each FIELD_DECL.  */
-  fieldlist = NULL_TREE;
   for (c = derived->components; c; c = c->next)
     {
       if (c->attr.proc_pointer)
@@ -2152,7 +2136,7 @@  gfc_get_derived_type (gfc_symbol * derived)
 	       && !c->attr.proc_pointer)
 	field_type = build_pointer_type (field_type);
 
-      field = gfc_add_field_to_struct (&fieldlist, typenode,
+      field = gfc_add_field_to_struct (typenode,
 				       get_identifier (c->name),
 				       field_type, &chain);
       if (c->loc.lb)
@@ -2167,9 +2151,7 @@  gfc_get_derived_type (gfc_symbol * derived)
 	c->backend_decl = field;
     }
 
-  /* Now we have the final fieldlist.  Record it, then lay out the
-     derived type, including the fields.  */
-  TYPE_FIELDS (typenode) = fieldlist;
+  /* Now lay out the derived type, including the fields.  */
   if (canonical)
     TYPE_CANONICAL (typenode) = canonical;
 
@@ -2232,7 +2214,6 @@  static tree
 gfc_get_mixed_entry_union (gfc_namespace *ns)
 {
   tree type;
-  tree fieldlist;
   tree *chain = NULL;
   char name[GFC_MAX_SYMBOL_LEN + 1];
   gfc_entry_list *el, *el2;
@@ -2246,7 +2227,6 @@  gfc_get_mixed_entry_union (gfc_namespace *ns)
   type = make_node (UNION_TYPE);
 
   TYPE_NAME (type) = get_identifier (name);
-  fieldlist = NULL;
 
   for (el = ns->entries; el; el = el->next)
     {
@@ -2256,14 +2236,12 @@  gfc_get_mixed_entry_union (gfc_namespace *ns)
 	  break;
 
       if (el == el2)
-	gfc_add_field_to_struct_1 (&fieldlist, type,
+	gfc_add_field_to_struct_1 (type,
 				   get_identifier (el->sym->result->name),
 				   gfc_sym_type (el->sym->result), &chain);
     }
 
   /* Finish off the type.  */
-  TYPE_FIELDS (type) = fieldlist;
-
   gfc_finish_type (type);
   TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type)) = 1;
   return type;
diff --git a/trans-types.h b/trans-types.h
index 0949b77..7e79480 100644
--- a/trans-types.h
+++ b/trans-types.h
@@ -77,7 +77,7 @@  tree gfc_get_array_type_bounds (tree, int, int, tree *, tree *, int,
 tree gfc_get_nodesc_array_type (tree, gfc_array_spec *, gfc_packed, bool);
 
 /* Add a field of given name and type to a UNION_TYPE or RECORD_TYPE.  */
-tree gfc_add_field_to_struct (tree *, tree, tree, tree, tree **);
+tree gfc_add_field_to_struct (tree, tree, tree, tree **);
 
 /* Layout and output debugging info for a type.  */
 void gfc_finish_type (tree);