https://gcc.gnu.org/g:390ef6679cce82dc7636c031fa0e9bf6a9ff3c57

commit 390ef6679cce82dc7636c031fa0e9bf6a9ff3c57
Author: Mikael Morin <mik...@gcc.gnu.org>
Date:   Mon Apr 14 20:38:41 2025 +0200

    Corrections ISO_Fortran_binding_18.f90

Diff:
---
 gcc/fortran/trans-array.cc | 13 ++++++++-----
 gcc/fortran/trans.cc       | 17 +++++++++++------
 gcc/fortran/trans.h        |  6 ++++--
 3 files changed, 23 insertions(+), 13 deletions(-)

diff --git a/gcc/fortran/trans-array.cc b/gcc/fortran/trans-array.cc
index 8cd7de71fe4a..fe77af2e14e2 100644
--- a/gcc/fortran/trans-array.cc
+++ b/gcc/fortran/trans-array.cc
@@ -3487,7 +3487,8 @@ build_ptr_array_ref (tree data, tree offset)
 
 
 tree
-build_array_ref_dim (gfc_ss *ss, tree index, tree spacing, bool tmp_array = 
false)
+build_array_ref_dim (gfc_ss *ss, tree index, tree spacing,
+                    tree offset = NULL_TREE, bool tmp_array = false)
 {
   gfc_array_info *info = &ss->info->data.array;
 
@@ -3499,8 +3500,10 @@ build_array_ref_dim (gfc_ss *ss, tree index, tree 
spacing, bool tmp_array = fals
                             || ss_type == GFC_SS_INTRINSIC
                             || tmp_array
                             || non_negative_strides_array_p (info->descriptor);
-  return gfc_build_array_ref (base, index, non_negative_stride,
-                             NULL_TREE, spacing);
+  return gfc_build_array_ref (base, index, 
+                             non_negative_stride
+                             && (!offset || integer_zerop (offset)),
+                             NULL_TREE, spacing, offset);
 }
 
 
@@ -3520,8 +3523,8 @@ gfc_conv_scalarized_array_ref (gfc_se * se, gfc_array_ref 
* ar, bool tmp_array =
 
   tree index = conv_array_index (se, ss, ss->dim[n], n, ar);
 
-  se->expr = build_array_ref_dim (ss, index,
-                                 ss->info->data.array.spacing0,
+  gfc_array_info *info = &ss->info->data.array;
+  se->expr = build_array_ref_dim (ss, index, info->spacing0, info->offset,
                                  tmp_array);
 }
 
diff --git a/gcc/fortran/trans.cc b/gcc/fortran/trans.cc
index c59ebbdaf7bd..c2aae309b081 100644
--- a/gcc/fortran/trans.cc
+++ b/gcc/fortran/trans.cc
@@ -407,13 +407,13 @@ gfc_build_spanned_array_ref (tree base, tree offset, tree 
span)
 
 
 /* Build an ARRAY_REF with its natural type.
-   NON_NEGATIVE_OFFSET indicates if it’s true that OFFSET can’t be negative,
+   NON_NEGATIVE_SPACING indicates if it’s true that SPACING can’t be negative,
    and thus that an ARRAY_REF can safely be generated.  If it’s false, we
    have to play it safe and use pointer arithmetic.  */
 
 tree
 gfc_build_array_ref (tree type, tree base, tree index, bool 
non_negative_offset,
-                    tree min_val, tree spacing)
+                    tree min_idx, tree spacing, tree offset)
 {
   if (DECL_P (base))
     TREE_ADDRESSABLE (base) = 1;
@@ -423,12 +423,12 @@ gfc_build_array_ref (tree type, tree base, tree index, 
bool non_negative_offset,
 
   if (non_negative_offset)
     return build4_loc (input_location, ARRAY_REF, type, base, index,
-                      min_val, spacing);
+                      min_idx, spacing);
   /* Otherwise use pointer arithmetic.  */
   else
     {
       gcc_assert (TREE_CODE (TREE_TYPE (base)) == ARRAY_TYPE);
-      tree min = min_val;
+      tree min = min_idx;
       if (min == NULL_TREE
          && TYPE_DOMAIN (TREE_TYPE (base)))
        min = TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (base)));
@@ -449,6 +449,11 @@ gfc_build_array_ref (tree type, tree base, tree index, 
bool non_negative_offset,
       tree offset_bytes = fold_build2_loc (input_location, MULT_EXPR,
                                           gfc_array_index_type,
                                           zero_based_index, delta);
+      if (offset && !integer_zerop (offset))
+       offset_bytes = fold_build2_loc (input_location, PLUS_EXPR,
+                                       gfc_array_index_type,
+                                       offset_bytes, offset);
+
       offset_bytes = fold_convert_loc (input_location, sizetype,
                                       offset_bytes);
 
@@ -464,7 +469,7 @@ gfc_build_array_ref (tree type, tree base, tree index, bool 
non_negative_offset,
 
 tree
 gfc_build_array_ref (tree base, tree index, bool non_negative_offset,
-                    tree offset, tree spacing)
+                    tree min_idx, tree spacing, tree offset)
 {
   tree type = TREE_TYPE (base);
 
@@ -483,7 +488,7 @@ gfc_build_array_ref (tree base, tree index, bool 
non_negative_offset,
     }
 
   return gfc_build_array_ref (TREE_TYPE (type), base, index, 
non_negative_offset,
-                             offset, spacing);
+                             min_idx, spacing, offset);
 }
 
 
diff --git a/gcc/fortran/trans.h b/gcc/fortran/trans.h
index 72f9b82098e3..a232254fd63f 100644
--- a/gcc/fortran/trans.h
+++ b/gcc/fortran/trans.h
@@ -646,9 +646,11 @@ tree gfc_build_addr_expr (tree, tree);
 
 /* Build an ARRAY_REF.  */
 tree gfc_build_array_ref (tree, tree, bool non_negative_offset = false,
-                         tree offset = NULL_TREE, tree spacing = NULL_TREE);
+                         tree min_idx = NULL_TREE, tree spacing = NULL_TREE,
+                         tree offset = NULL_TREE);
 tree gfc_build_array_ref (tree, tree, tree,  bool non_negative_offset = false,
-                         tree offset = NULL_TREE, tree spacing = NULL_TREE);
+                         tree min_idx = NULL_TREE, tree spacing = NULL_TREE,
+                         tree offset = NULL_TREE);
 
 /* Build an array ref using pointer arithmetic.  */
 tree gfc_build_spanned_array_ref (tree base, tree offset, tree span);

Reply via email to