[gcc(refs/users/mikael/heads/refactor_descriptor_v206.01)] fortran: array descriptor: Move dtype constructor generation [PR122521]

Mikael Morin via Gcc-cvs <[email protected]>
Newsgroups gmane.comp.gcc.cvs
Message-ID <[email protected]>
https://gcc.gnu.org/g:a214f0776a0a7e5e518d0ed7bd960d5b706a9a88

commit a214f0776a0a7e5e518d0ed7bd960d5b706a9a88
Author: Mikael Morin <[email protected]>
Date:   Wed Nov 12 21:09:32 2025 +0100

    fortran: array descriptor: Move dtype constructor generation [PR122521]
    
    Fortran-tested on aarch64-unknown-linux-gnu.  OK for mainline?
    
    -- >8 --
    
    The final sequence of trans-types.cc's `gfc_get_dtype_rank_type' generates a
    constructor for an array descriptor dtype.  Move that sequence to a
    dedicated function in trans-descriptor.cc to avoid leaking the content and
    layout of the dtypes out of that file.
    
            PR fortran/122521
    
    gcc/fortran/ChangeLog:
    
            * trans-types.cc (gfc_get_dtype_rank_type): Move constructor
            generation ...
            * trans-descriptor.cc (gfc_build_dtype_constructor): ... here as a
            new function.
            * trans-descriptor.h (gfc_build_dtype_constructor): New declaration.

Diff:
---
 gcc/fortran/trans-descriptor.cc | 38 ++++++++++++++++++++++++++++++++++++++
 gcc/fortran/trans-descriptor.h  |  2 ++
 gcc/fortran/trans-types.cc      | 33 +--------------------------------
 3 files changed, 41 insertions(+), 32 deletions(-)

diff --git a/gcc/fortran/trans-descriptor.cc b/gcc/fortran/trans-descriptor.cc
index 6156f4e2db1e..e4ff96fff671 100644
--- a/gcc/fortran/trans-descriptor.cc
+++ b/gcc/fortran/trans-descriptor.cc
@@ -624,6 +624,44 @@ gfc_get_descriptor_offsets_for_info (const_tree desc_type, tree *data_off,
 /* Array descriptor higher level routines.
  ******************************************************************************/
 
+/* Return a constructor for a descriptor dtype with the caracteristics given by
+   the arguments.  */
+
+tree
+gfc_build_dtype_constructor (tree size, int type, int rank)
+{
+  tree field;
+  vec<constructor_elt, va_gc> *v = NULL;
+
+  gcc_assert (size);
+
+  STRIP_NOPS (size);
+  size = fold_convert (size_type_node, size);
+  tree dtype_type_node = get_dtype_type_node ();
+  field = gfc_advance_chain (TYPE_FIELDS (dtype_type_node),
+			     GFC_DTYPE_ELEM_LEN);
+  CONSTRUCTOR_APPEND_ELT (v, field,
+			  fold_convert (TREE_TYPE (field), size));
+  field = gfc_advance_chain (TYPE_FIELDS (dtype_type_node),
+			     GFC_DTYPE_VERSION);
+  CONSTRUCTOR_APPEND_ELT (v, field,
+			  build_zero_cst (TREE_TYPE (field)));
+
+  field = gfc_advance_chain (TYPE_FIELDS (dtype_type_node),
+			     GFC_DTYPE_RANK);
+  if (rank >= 0)
+    CONSTRUCTOR_APPEND_ELT (v, field,
+			    build_int_cst (TREE_TYPE (field), rank));
+
+  field = gfc_advance_chain (TYPE_FIELDS (dtype_type_node),
+			     GFC_DTYPE_TYPE);
+  CONSTRUCTOR_APPEND_ELT (v, field,
+			  build_int_cst (TREE_TYPE (field), type));
+
+  return build_constructor (dtype_type_node, v);
+}
+
+
 /* Build a null array descriptor constructor.  */
 
 tree
diff --git a/gcc/fortran/trans-descriptor.h b/gcc/fortran/trans-descriptor.h
index 081bb45ef074..7a206e2a4b21 100644
--- a/gcc/fortran/trans-descriptor.h
+++ b/gcc/fortran/trans-descriptor.h
@@ -53,6 +53,8 @@ void gfc_conv_descriptor_lbound_set (stmtblock_t *, tree, tree, tree);
 void gfc_conv_descriptor_ubound_set (stmtblock_t *, tree, tree, tree);
 void gfc_conv_descriptor_token_set (stmtblock_t *block, tree desc, tree value);
 
+tree gfc_build_dtype_constructor (tree, int, int);
+
 /* Build expressions for accessing components of an array descriptor.  */
 void gfc_get_descriptor_offsets_for_info (const_tree, tree *, tree *, tree *,
 					  tree *, tree *, tree *, tree *,
diff --git a/gcc/fortran/trans-types.cc b/gcc/fortran/trans-types.cc
index ea4395c67dd7..4cc63c4f64d4 100644
--- a/gcc/fortran/trans-types.cc
+++ b/gcc/fortran/trans-types.cc
@@ -1709,10 +1709,6 @@ gfc_get_dtype_rank_type (int rank, tree etype)
   tree ptype;
   tree size;
   int n;
-  tree tmp;
-  tree dtype;
-  tree field;
-  vec<constructor_elt, va_gc> *v = NULL;
 
   ptype = etype;
   while (TREE_CODE (etype) == POINTER_TYPE
@@ -1784,34 +1780,7 @@ gfc_get_dtype_rank_type (int rank, tree etype)
       break;
     }
 
-  gcc_assert (size);
-
-  STRIP_NOPS (size);
-  size = fold_convert (size_type_node, size);
-  tmp = get_dtype_type_node ();
-  field = gfc_advance_chain (TYPE_FIELDS (tmp),
-			     GFC_DTYPE_ELEM_LEN);
-  CONSTRUCTOR_APPEND_ELT (v, field,
-			  fold_convert (TREE_TYPE (field), size));
-  field = gfc_advance_chain (TYPE_FIELDS (dtype_type_node),
-			     GFC_DTYPE_VERSION);
-  CONSTRUCTOR_APPEND_ELT (v, field,
-			  build_zero_cst (TREE_TYPE (field)));
-
-  field = gfc_advance_chain (TYPE_FIELDS (dtype_type_node),
-			     GFC_DTYPE_RANK);
-  if (rank >= 0)
-    CONSTRUCTOR_APPEND_ELT (v, field,
-			    build_int_cst (TREE_TYPE (field), rank));
-
-  field = gfc_advance_chain (TYPE_FIELDS (dtype_type_node),
-			     GFC_DTYPE_TYPE);
-  CONSTRUCTOR_APPEND_ELT (v, field,
-			  build_int_cst (TREE_TYPE (field), n));
-
-  dtype = build_constructor (tmp, v);
-
-  return dtype;
+  return gfc_build_dtype_constructor (size, n, rank);
 }
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.