https://gcc.gnu.org/g:c6b658f0aa28343cd787cfcfcd0075b8870568d7

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

    fortran: array descriptor: Move result default initialization [PR122521]
    
    Fortran-tested on aarch64-unknown-linux-gnu.  OK for mainline?
    
    -- >8 --
    
    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 initially 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);

Reply via email to