[PATCH 2/6] fortran: array descriptor: Move result default initialization [PR122521]

Mikael Morin <[email protected]> Sun, 2 Aug 2026 22:59:35 +0200
Newsgroups gmane.comp.gcc.patches,gmane.comp.gcc.fortran
Message-ID <[email protected]>
From: Mikael Morin <[email protected]>

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.
---
 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 47b28c1d003..4eea168f3c5 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 53715b65e60..b2ef36070b6 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 cc15ac392a3..ca1943fdaea 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);
-- 
2.53.0