https://gcc.gnu.org/g:1def09a5fb0d1095e03fe7c72590a9b5b5082c01

commit 1def09a5fb0d1095e03fe7c72590a9b5b5082c01
Author: Mikael Morin <[email protected]>
Date:   Wed Aug 6 14:23:40 2025 +0200

    fortran: array descriptor: Factor component access
    
    All the array descriptor internal field accessors use a similar code to
    generate the component reference and check its type.  Introduce a common
    function to do it.
    
    gcc/fortran/ChangeLog:
    
            * trans-descriptor.cc (get_ref_comp): New function.
            (gfc_get_descriptor_field, conv_descriptor_rank,
            get_descriptor_version, conv_descriptor_elem_len,
            conv_descriptor_type, gfc_conv_descriptor_subfield): Use it.

Diff:
---
 gcc/fortran/trans-descriptor.cc | 57 +++++++++++++++--------------------------
 1 file changed, 20 insertions(+), 37 deletions(-)

diff --git a/gcc/fortran/trans-descriptor.cc b/gcc/fortran/trans-descriptor.cc
index 060fc1ebf87f..ad5b72c4e665 100644
--- a/gcc/fortran/trans-descriptor.cc
+++ b/gcc/fortran/trans-descriptor.cc
@@ -77,16 +77,24 @@ get_type_field (tree type, unsigned field_idx)
 /* Get FIELD_IDX'th field in array descriptor DESC.  */
 
 static tree
-gfc_get_descriptor_field (tree desc, unsigned field_idx)
+get_ref_comp (tree ref, unsigned field_idx, tree type = NULL_TREE)
 {
-  tree type = TREE_TYPE (desc);
-  gcc_assert (GFC_DESCRIPTOR_TYPE_P (type));
-
-  tree field = get_type_field (type, field_idx);
-  gcc_assert (field != NULL_TREE);
+  tree field = get_type_field (TREE_TYPE (ref), field_idx);
+  gcc_assert (field != NULL_TREE
+             && (type == NULL_TREE
+                 || TREE_TYPE (field) == type));
 
   return fold_build3_loc (input_location, COMPONENT_REF, TREE_TYPE (field),
-                         desc, field, NULL_TREE);
+                         ref, field, NULL_TREE);
+}
+
+
+static tree
+gfc_get_descriptor_field (tree desc, unsigned field_idx)
+{
+  gcc_assert (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (desc)));
+
+  return get_ref_comp (desc, field_idx);
 }
 
 
@@ -203,15 +211,10 @@ gfc_conv_descriptor_span_set (stmtblock_t *block, tree 
desc, tree value)
 static tree
 conv_descriptor_rank (tree desc)
 {
-  tree tmp;
   tree dtype;
 
   dtype = conv_descriptor_dtype (desc);
-  tmp = gfc_advance_chain (TYPE_FIELDS (TREE_TYPE (dtype)), GFC_DTYPE_RANK);
-  gcc_assert (tmp != NULL_TREE
-             && TREE_TYPE (tmp) == signed_char_type_node);
-  return fold_build3_loc (input_location, COMPONENT_REF, TREE_TYPE (tmp),
-                         dtype, tmp, NULL_TREE);
+  return get_ref_comp (dtype, GFC_DTYPE_RANK, signed_char_type_node);
 }
 
 /* Return the rank of the array descriptor DESC.  */
@@ -247,15 +250,10 @@ gfc_conv_descriptor_rank_set (stmtblock_t *block, tree 
desc, int value)
 static tree
 conv_descriptor_version (tree desc)
 {
-  tree tmp;
   tree dtype;
 
   dtype = conv_descriptor_dtype (desc);
-  tmp = gfc_advance_chain (TYPE_FIELDS (TREE_TYPE (dtype)), GFC_DTYPE_VERSION);
-  gcc_assert (tmp != NULL_TREE
-             && TREE_TYPE (tmp) == integer_type_node);
-  return fold_build3_loc (input_location, COMPONENT_REF, TREE_TYPE (tmp),
-                         dtype, tmp, NULL_TREE);
+  return get_ref_comp (dtype, GFC_DTYPE_VERSION, integer_type_node);
 }
 
 /* Return the value of descriptor DESC's format version.  */
@@ -283,16 +281,10 @@ gfc_conv_descriptor_version_set (stmtblock_t *block, tree 
desc, tree value)
 static tree
 conv_descriptor_elem_len (tree desc)
 {
-  tree tmp;
   tree dtype;
 
   dtype = conv_descriptor_dtype (desc);
-  tmp = gfc_advance_chain (TYPE_FIELDS (TREE_TYPE (dtype)),
-                          GFC_DTYPE_ELEM_LEN);
-  gcc_assert (tmp != NULL_TREE
-             && TREE_TYPE (tmp) == size_type_node);
-  return fold_build3_loc (input_location, COMPONENT_REF, TREE_TYPE (tmp),
-                         dtype, tmp, NULL_TREE);
+  return get_ref_comp (dtype, GFC_DTYPE_ELEM_LEN, size_type_node);
 }
 
 /* Return the descriptor DESC's array element size in bytes.  */
@@ -321,15 +313,10 @@ gfc_conv_descriptor_elem_len_set (stmtblock_t *block, 
tree desc, tree value)
 static tree
 conv_descriptor_type (tree desc)
 {
-  tree tmp;
   tree dtype;
 
   dtype = conv_descriptor_dtype (desc);
-  tmp = gfc_advance_chain (TYPE_FIELDS (TREE_TYPE (dtype)), GFC_DTYPE_TYPE);
-  gcc_assert (tmp!= NULL_TREE
-             && TREE_TYPE (tmp) == signed_char_type_node);
-  return fold_build3_loc (input_location, COMPONENT_REF, TREE_TYPE (tmp),
-                         dtype, tmp, NULL_TREE);
+  return get_ref_comp (dtype, GFC_DTYPE_TYPE, signed_char_type_node);
 }
 
 /* Return the value of the type discriminator of the array descriptor DESC.  */
@@ -491,11 +478,7 @@ static tree
 gfc_conv_descriptor_subfield (tree desc, tree dim, unsigned field_idx)
 {
   tree tmp = conv_descriptor_dimension (desc, dim);
-  tree field = gfc_advance_chain (TYPE_FIELDS (TREE_TYPE (tmp)), field_idx);
-  gcc_assert (field != NULL_TREE);
-
-  return fold_build3_loc (input_location, COMPONENT_REF, TREE_TYPE (field),
-                         tmp, field, NULL_TREE);
+  return get_ref_comp (tmp, field_idx);
 }

Reply via email to