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

commit c2611c2cb576f9fbeb98bb4e0e079238a8209e43
Author: Mikael Morin <[email protected]>
Date:   Thu Nov 12 18:55:21 2026 +0100

    fortran: array descriptor: Extract class container with its value [PR122521]
    
    Fortran-tested on aarch64-unknown-linux-gnu.
    
    -- >8 --
    
    In gfc_set_descriptor_from_scalar_class, use a single walk over the input
    reference to extract both the class container and the pointer to its value.
    
    This is a follow-up to the previous change.  That change introduced the walk
    of the input reference to extract the class container, but it was ignoring
    the change before, that independently checked whether the input reference
    was the class container, to produce a reference to its _data.  This
    integrates the latter into the walk of the input reference.
    
            PR fortran/122521
    
    gcc/fortran/ChangeLog:
    
            * trans-descriptor.cc (is_polymorphic_ref): Add argument valptr_ref.
            Set it to the value pointer reference found during the walk, if such
            a reference was found.  Otherwise set it to a new reference to the
            _data field of the class container reference.
            (gfc_set_descriptor_from_scalar_class): Add a valptr_ref variable
            and pass it to is_polymorphic_ref.  Don't check the input reference
            type and don't create a new reference to the _data from the input.
            Directly use the reference returned by is_polymorphic_ref instead.

Diff:
---
 gcc/fortran/trans-descriptor.cc | 59 +++++++++++++++++++++++++++++------------
 1 file changed, 42 insertions(+), 17 deletions(-)

diff --git a/gcc/fortran/trans-descriptor.cc b/gcc/fortran/trans-descriptor.cc
index 4302f751ec73..b4c1fde63a47 100644
--- a/gcc/fortran/trans-descriptor.cc
+++ b/gcc/fortran/trans-descriptor.cc
@@ -908,11 +908,17 @@ gfc_set_descriptor_from_scalar (stmtblock_t *block, tree 
descr, tree scalar)
 
 /* Return true if EXPR is a polymorphic reference; otherwise return false.  If
    CLASS_REF isn't nullptr, set its target to a reference to the class 
container
-   in the true case, and leave it unmodified in the false case.  */
+   in the true case, and leave it unmodified in the false case.  If VALPTR_REF
+   isn't nullptr, set its target to a reference to the _data component of the
+   class container similarly.  */
 
 static bool
-is_polymorphic_ref (tree expr, tree *class_ref = nullptr)
+is_polymorphic_ref (tree expr, tree *class_ref = nullptr,
+                   tree *valptr_ref = nullptr)
 {
+  tree valptr_candidate = NULL_TREE;
+  tree class_container = NULL_TREE;
+
   tree e = expr;
   while (true)
     {
@@ -923,25 +929,33 @@ is_polymorphic_ref (tree expr, tree *class_ref = nullptr)
               && GFC_CLASS_TYPE_P (TREE_TYPE (TREE_TYPE (e))))
        {
          if (class_ref != nullptr)
-           *class_ref = build_fold_indirect_ref_loc (input_location, e);
-         return true;
+           class_container = build_fold_indirect_ref_loc (input_location, e);
+         break;
        }
       else if (GFC_CLASS_TYPE_P (TREE_TYPE (e)))
        {
          if (class_ref != nullptr)
-           *class_ref = e;
-         return true;
+           class_container = e;
+         break;
        }
       else if (TREE_CODE (e) == COMPONENT_REF)
        {
          tree base_obj = TREE_OPERAND (e, 0);
          tree field_decl = TREE_OPERAND (e, 1);
          tree field_name = DECL_NAME (field_decl);
-         if (!((GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (base_obj))
-                && strcmp (IDENTIFIER_POINTER (field_name), "data") == 0)
-               || (GFC_CLASS_TYPE_P (TREE_TYPE (base_obj))
-                   && strcmp (IDENTIFIER_POINTER (field_name), "_data") == 0)))
+         bool array_data = GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (base_obj))
+                           && strcmp (IDENTIFIER_POINTER (field_name), "data")
+                              == 0;
+         bool class_data = GFC_CLASS_TYPE_P (TREE_TYPE (base_obj))
+                           && strcmp (IDENTIFIER_POINTER (field_name), "_data")
+                              == 0;
+         if (!(array_data || class_data))
            return false;
+
+         if (array_data || (class_data && !valptr_candidate))
+           valptr_candidate = e;
+         else
+           valptr_candidate = nullptr;
        }
       else if (!(TREE_CODE (e) == INDIRECT_REF
                 || TREE_CODE (e) == ARRAY_REF
@@ -950,6 +964,20 @@ is_polymorphic_ref (tree expr, tree *class_ref = nullptr)
 
       e = TREE_OPERAND (e, 0);
     }
+
+  gcc_assert (class_container != nullptr);
+  if (class_ref != nullptr)
+    *class_ref = class_container;
+
+  if (valptr_ref != nullptr)
+    {
+      if (valptr_candidate != nullptr)
+       *valptr_ref = valptr_candidate;
+      else
+       *valptr_ref = gfc_class_data_get (class_container);
+    }
+
+  return true;
 }
 
 
@@ -963,17 +991,14 @@ void
 gfc_set_descriptor_from_scalar_class (stmtblock_t *block, tree descr,
                                      tree scalar)
 {
-  tree class_ref = nullptr;;
-  bool is_class = is_polymorphic_ref (scalar, &class_ref);
+  tree class_ref = nullptr;
+  tree valptr_ref = nullptr;
+  bool is_class = is_polymorphic_ref (scalar, &class_ref, &valptr_ref);
   gcc_checking_assert (is_class);
   tree dtype_val = gfc_get_dtype_rank_type (0, TREE_TYPE (class_ref));
   gfc_conv_descriptor_dtype_set (block, descr, dtype_val);
 
-  tree tmp = scalar;
-  if (GFC_CLASS_TYPE_P (TREE_TYPE (tmp))
-      || (POINTER_TYPE_P (TREE_TYPE (tmp))
-         && GFC_CLASS_TYPE_P (TREE_TYPE (TREE_TYPE (tmp)))))
-    tmp = gfc_class_data_get (tmp);
+  tree tmp = valptr_ref;
   if (!POINTER_TYPE_P (TREE_TYPE (tmp)))
     tmp = gfc_build_addr_expr (NULL_TREE, tmp);

Reply via email to