[gcc(refs/users/mikael/heads/refactor_descriptor_v205.01)] fortran: array descriptor: Move result default initialization [PR122521]

Mikael Morin via Gcc-cvs <[email protected]> Sun, 2 Aug 2026 09:41:10 +0000 (GMT)
Newsgroups gmane.comp.gcc.cvs
Message-ID <[email protected]>
https://gcc.gnu.org/g:ab637f9cd2a21c322f44818bd0ea72baa7d009e2

commit ab637f9cd2a21c322f44818bd0ea72baa7d009e2
Author: Mikael Morin <[email protected]>
Date:   Sun Aug 2 11:13:37 2026 +0200

    fortran: array descriptor: Move result default initialization [PR122521]
    
    An array descriptor representing an allocatable array function result has
    its data field initialized to null at the beginning of the function, to
    implement the initial unallocated status.  Move the code generating that
    initialization for polymorphic functions (both allocatable and pointer)
    to its own function in trans-descriptor.cc.  I think the initialization is
    unnecessary for pointers, but I'm not changing that here.
    
            PR fortran/122521
    
    gcc/fortran/ChangeLog:
    
            * trans-decl.cc (gfc_trans_deferred_vars): Move result array
            descriptor default initialization ...
            * trans-descriptor.cc (gfc_init_descriptor_result): ... here as
            a new function.
            * trans-descriptor.h (gfc_init_descriptor_result): New declaration.

Diff:
---
 gcc/fortran/trans-decl.cc       |  4 +---
 gcc/fortran/trans-descriptor.cc | 11 +++++++++++
 gcc/fortran/trans-descriptor.h  |  1 +
 3 files changed, 13 insertions(+), 3 deletions(-)

diff --git a/gcc/fortran/trans-decl.cc b/gcc/fortran/trans-decl.cc
index 47b28c1d0032..4eea168f3c52 100644
--- a/gcc/fortran/trans-decl.cc
+++ b/gcc/fortran/trans-decl.cc
@@ -4999,14 +4999,12 @@ gfc_trans_deferred_vars (gfc_symbol * proc_sym, gfc_wrapped_block * block)
   else if (proc_sym == proc_sym->result && IS_CLASS_ARRAY (proc_sym))
     {
       /* Nullify explicit return class arrays on entry.  */
-      tree type;
       tmp = get_proc_result (proc_sym);
       if (tmp && GFC_CLASS_TYPE_P (TREE_TYPE (tmp)))
 	{
 	  gfc_start_block (&init);
 	  tmp = gfc_class_data_get (tmp);
-	  type = TREE_TYPE (gfc_conv_descriptor_data_get (tmp));
-	  gfc_conv_descriptor_data_set (&init, tmp, build_int_cst (type, 0));
+	  gfc_init_result_descriptor (&init, tmp);
 	  gfc_add_init_cleanup (block, gfc_finish_block (&init), NULL_TREE);
 	}
     }
diff --git a/gcc/fortran/trans-descriptor.cc b/gcc/fortran/trans-descriptor.cc
index 53715b65e600..b2ef36070b67 100644
--- a/gcc/fortran/trans-descriptor.cc
+++ b/gcc/fortran/trans-descriptor.cc
@@ -725,6 +725,17 @@ gfc_nullify_descriptor (stmtblock_t *block, tree descr)
 }
 
 
+/* Add code to BLOCK default-initializing array function result descriptor
+   DESCR.  This is used for the initialization of polymorphic allocatable
+   function results.  */
+
+void
+gfc_init_result_descriptor (stmtblock_t *block, tree descr)
+{
+  gfc_conv_descriptor_data_set (block, descr, null_pointer_node);
+}
+
+
 /* For an array descriptor, get the total number of elements.  This is just
    the product of the extents along from_dim to to_dim.  */
 
diff --git a/gcc/fortran/trans-descriptor.h b/gcc/fortran/trans-descriptor.h
index cc15ac392a34..ca1943fdaea0 100644
--- a/gcc/fortran/trans-descriptor.h
+++ b/gcc/fortran/trans-descriptor.h
@@ -64,6 +64,7 @@ void gfc_get_descriptor_offsets_for_info (const_tree, tree *, tree *, tree *,
 tree gfc_build_null_descriptor (tree type);
 
 void gfc_nullify_descriptor (stmtblock_t *block, tree);
+void gfc_init_result_descriptor (stmtblock_t *block, tree descr);
 
 tree gfc_conv_descriptor_size (tree, int);
 tree gfc_conv_descriptor_cosize (tree, int, int);