https://gcc.gnu.org/g:588dab8bc7504407031548a265eb68ba4cb62e9a
commit 588dab8bc7504407031548a265eb68ba4cb62e9a Author: Mikael Morin <[email protected]> Date: Thu Sep 17 14:43:02 2026 +0200 fortran: Use span array indexing for class pointers [PR127448] Mark the _data field of pointer class containers with the GFC_DECL_PTR_ARRAY_P flag, so that they use span indexing. Before this change, it was assumed that the size taken from the class container was the correct span value. It is not the case if the pointer target is a subobject of a bigger object, and yet is polymorphic. The change tries to keep the current behaviour if possible, that is use the size from the class container if the array is known to be contiguous. I doubt the information is propagated . The is_pointer_array hunk removes a condition that is redundant: the type of the operand 1 of a component ref is the type of the field, which is also the type of the component ref itself, which has already been checked before. I suspect the condition to be a typo and should check operand 0 of the component ref. If that was the case, this patch would remove it anyway. PR fortran/127448 gcc/fortran/ChangeLog: * trans-array.cc (is_pointer_array): Remove redundant condition. (span_addressed_array): For polymorphic arrays, return NULL if the array is known to be contiguous. * trans-types.cc (gfc_get_derived_type): Also set the GFC_DECL_PTR_ARRAY_P flag for _data fields of class containers corresponding to polymorphic array pointers. Diff: --- gcc/fortran/trans-array.cc | 28 +++++++++++++++++++++++++--- gcc/fortran/trans-types.cc | 5 +++-- 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/gcc/fortran/trans-array.cc b/gcc/fortran/trans-array.cc index 3e3e88012efd..d9b34a36a791 100644 --- a/gcc/fortran/trans-array.cc +++ b/gcc/fortran/trans-array.cc @@ -484,8 +484,7 @@ is_pointer_array (tree expr) /* The field declaration is marked as a pointer array. */ if (TREE_CODE (expr) == COMPONENT_REF - && GFC_DECL_PTR_ARRAY_P (TREE_OPERAND (expr, 1)) - && !GFC_CLASS_TYPE_P (TREE_TYPE (TREE_OPERAND (expr, 1)))) + && GFC_DECL_PTR_ARRAY_P (TREE_OPERAND (expr, 1))) return true; return false; @@ -501,7 +500,30 @@ static tree span_addressed_array (tree expr) { if (is_pointer_array (expr)) - return expr; + { + /* For classes, index arrays using the size from the virtual pointer if + the array is contiguous. Otherwise use the span. */ + if (TREE_CODE (expr) == COMPONENT_REF + && GFC_CLASS_TYPE_P (TREE_TYPE (TREE_OPERAND (expr, 0))) + && TYPE_LANG_SPECIFIC (TREE_TYPE (expr))) + { + switch (GFC_TYPE_ARRAY_AKIND (TREE_TYPE (expr))) + { + case GFC_ARRAY_ASSUMED_SHAPE_CONT: + case GFC_ARRAY_ASSUMED_RANK_CONT: + case GFC_ARRAY_ASSUMED_RANK_ALLOCATABLE: + case GFC_ARRAY_ASSUMED_RANK_POINTER_CONT: + case GFC_ARRAY_ALLOCATABLE: + case GFC_ARRAY_POINTER_CONT: + return NULL_TREE; + + default: + break; + } + } + + return expr; + } if (VAR_P (expr) && GFC_DECL_PTR_ARRAY_P (expr) diff --git a/gcc/fortran/trans-types.cc b/gcc/fortran/trans-types.cc index ef80cd8feabb..68661d165dab 100644 --- a/gcc/fortran/trans-types.cc +++ b/gcc/fortran/trans-types.cc @@ -3167,8 +3167,9 @@ gfc_get_derived_type (gfc_symbol * derived, int codimen) if (class_coarray_flag || !c->backend_decl || c->attr.caf_token) c->backend_decl = field; - if (c->attr.pointer && (c->attr.dimension || c->attr.codimension) - && !(c->ts.type == BT_DERIVED && strcmp (c->name, "_data") == 0)) + if ((c->attr.dimension || c->attr.codimension) + && ((c->ts.type == BT_CLASS && c->attr.class_pointer) + || (c->ts.type != BT_CLASS && c->attr.pointer))) GFC_DECL_PTR_ARRAY_P (c->backend_decl) = 1; }
