https://gcc.gnu.org/g:413bb01ee4412fc63541cfa7c06e0f17a2cecf88

commit 413bb01ee4412fc63541cfa7c06e0f17a2cecf88
Author: Mikael Morin <[email protected]>
Date:   Sat Nov 7 21:39:46 2026 +0100

    Sauvegarde compil' OK

Diff:
---
 gcc/fortran/class.cc | 2194 +++++++++++++++++++++++++++-----------------------
 1 file changed, 1165 insertions(+), 1029 deletions(-)

diff --git a/gcc/fortran/class.cc b/gcc/fortran/class.cc
index faca0b877d0c..9844ce5807e4 100644
--- a/gcc/fortran/class.cc
+++ b/gcc/fortran/class.cc
@@ -1025,1156 +1025,1303 @@ comp_is_finalizable (gfc_component *comp)
 }
 
 
-/* Call DEALLOCATE for the passed component if it is allocatable, if it is
-   neither allocatable nor a pointer but has a finalizer, call it. If it
-   is a nonpointer component with allocatable components or has finalizers, 
walk
-   them. Either of them is required; other nonallocatables and pointers aren't
-   handled gracefully.
-   Note: If the component is allocatable, the DEALLOCATE handling takes care
-   of calling the appropriate finalizers, coarray deregistering, and
-   deallocation of allocatable subcomponents.  */
-
 static bool
-finalize_component (gfc_expr *expr, gfc_symbol *derived, gfc_component *comp,
-                   gfc_symbol *stat, gfc_symbol *fini_coarray, gfc_code **code,
-                   gfc_namespace *sub_ns)
+array_with_comp_finalizable (gfc_component *comp)
 {
-  gfc_expr *e;
-  gfc_ref *ref;
-  gfc_was_finalized *f;
-
-  if (!comp_is_finalizable (comp))
+  /* False for simple types.  */
+  if (!(comp->ts.type == BT_DERIVED
+       || comp->ts.type == BT_CLASS))
     return false;
 
-  /* If this expression with this component has been finalized
-     already in this namespace, there is nothing to do.  */
-  for (f = sub_ns->was_finalized; f; f = f->next)
-    {
-      if (f->e == expr && f->c == comp)
-       return false;
-    }
+  /* False for non-arrays.  */
+  if (!((comp->ts.type == BT_CLASS
+        && comp->ts.u.derived->attr.is_class
+        && CLASS_DATA (comp)->as
+        && CLASS_DATA (comp)->as->rank != 0)
+       || (!(comp->ts.type == BT_CLASS
+             && comp->ts.u.derived->attr.is_class)
+           && comp->as
+           && comp->as->rank != 0)))
+    return false;
 
-  e = gfc_copy_expr (expr);
-  if (!e->ref)
-    e->ref = ref = gfc_get_ref ();
-  else
-    {
-      for (ref = e->ref; ref->next; ref = ref->next)
-       ;
-      ref->next = gfc_get_ref ();
-      ref = ref->next;
-    }
-  ref->type = REF_COMPONENT;
-  ref->u.c.sym = derived;
-  ref->u.c.component = comp;
-  e->ts = comp->ts;
+  for (gfc_component *c = comp->ts.u.derived->components; c; c = c->next)
+    if (comp_is_finalizable (c))
+      return true;
 
-  if (comp->attr.dimension || comp->attr.codimension
-      || (comp->ts.type == BT_CLASS && CLASS_DATA (comp)
-         && (CLASS_DATA (comp)->attr.dimension
-             || CLASS_DATA (comp)->attr.codimension)))
-    {
-      ref->next = gfc_get_ref ();
-      ref->next->type = REF_ARRAY;
-      ref->next->u.ar.dimen = 0;
-      ref->next->u.ar.as = comp->ts.type == BT_CLASS ? CLASS_DATA (comp)->as
-                                                       : comp->as;
-      e->rank = ref->next->u.ar.as->rank;
-      e->corank = ref->next->u.ar.as->corank;
-      ref->next->u.ar.type = e->rank ? AR_FULL : AR_ELEMENT;
-    }
+  return false;
+}
 
-  /* Call DEALLOCATE (comp, stat=ignore).  */
-  if (comp->attr.allocatable
-      || (comp->ts.type == BT_CLASS && CLASS_DATA (comp)
-         && CLASS_DATA (comp)->attr.allocatable))
-    {
-      gfc_code *dealloc, *block = NULL;
 
-      /* Add IF (fini_coarray).  */
-      if (comp->attr.codimension
-         || (comp->ts.type == BT_CLASS && CLASS_DATA (comp)
-             && CLASS_DATA (comp)->attr.codimension))
-       {
-         block = gfc_get_code (EXEC_IF);
-         if (*code)
-           {
-             (*code)->next = block;
-             (*code) = (*code)->next;
-           }
-         else
-             (*code) = block;
+struct scalarizable_array
+{
+  gfc_namespace *ns;
+  gfc_expr *array;
+  gfc_expr *rank;
+  gfc_symbol *idx;
+  gfc_symbol *idx2;
+  gfc_symbol *offset;
+  gfc_symbol *is_contiguous;
+  gfc_symbol *sizes;
+  gfc_symbol *strides;
+  gfc_symbol *nelem;
+  gfc_symbol *ptr;
+};
+
+
+static scalarizable_array
+prepare_scalarization (gfc_expr *array, gfc_namespace *sub_ns,
+                      gfc_code **insertion_point)
+{
+  gfc_symbol *sizes, *strides, *idx, *idx2;
+  gfc_symbol *ptr, *is_contiguous, *offset, *nelem;
+  gfc_code *block = nullptr, *last_code = nullptr;
 
-         block->block = gfc_get_code (EXEC_IF);
-         block = block->block;
-         block->expr1 = gfc_lval_expr_from_sym (fini_coarray);
-       }
+  gcc_assert (array->ts.type == BT_DERIVED);
 
-      dealloc = gfc_get_code (EXEC_DEALLOCATE);
+  scalarizable_array sa;
 
-      dealloc->ext.alloc.list = gfc_get_alloc ();
-      dealloc->ext.alloc.list->expr = e;
-      dealloc->expr1 = gfc_lval_expr_from_sym (stat);
+  sa.ns = sub_ns;
 
-      gfc_code *cond = gfc_get_code (EXEC_IF);
-      cond->block = gfc_get_code (EXEC_IF);
-      cond->block->expr1 = gfc_get_expr ();
-      cond->block->expr1->expr_type = EXPR_FUNCTION;
-      cond->block->expr1->where = gfc_current_locus;
-      gfc_get_sym_tree ("associated", sub_ns, &cond->block->expr1->symtree, 
false);
-      cond->block->expr1->symtree->n.sym->attr.flavor = FL_PROCEDURE;
-      cond->block->expr1->symtree->n.sym->attr.intrinsic = 1;
-      cond->block->expr1->symtree->n.sym->result = 
cond->block->expr1->symtree->n.sym;
-      gfc_commit_symbol (cond->block->expr1->symtree->n.sym);
-      cond->block->expr1->ts.type = BT_LOGICAL;
-      cond->block->expr1->ts.kind = gfc_default_logical_kind;
-      cond->block->expr1->value.function.isym = gfc_intrinsic_function_by_id 
(GFC_ISYM_ASSOCIATED);
-      cond->block->expr1->value.function.actual = gfc_get_actual_arglist ();
-      /* EXPR only adds nonpointer, nonallocatable components to its base
-        pointer, so testing the base suffices.  */
-      cond->block->expr1->value.function.actual->expr
-       = gfc_lval_expr_from_sym (expr->symtree->n.sym);
-      cond->block->expr1->value.function.actual->next = gfc_get_actual_arglist 
();
-      cond->block->next = dealloc;
+  /* Local variables.  */
 
-      if (block)
-       block->next = cond;
-      else if (*code)
-       {
-         (*code)->next = cond;
-         (*code) = (*code)->next;
-       }
-      else
-       (*code) = cond;
+  gfc_get_symbol ("idx", sub_ns, &idx);
+  idx->ts.type = BT_INTEGER;
+  idx->ts.kind = gfc_index_integer_kind;
+  idx->attr.flavor = FL_VARIABLE;
+  idx->attr.artificial = 1;
+  gfc_set_sym_referenced (idx);
+  gfc_commit_symbol (idx);
+  sa.idx = idx;
 
-    }
-  else if (comp->ts.type == BT_DERIVED
-           && comp->ts.u.derived->f2k_derived
-           && comp->ts.u.derived->f2k_derived->finalizers)
-    {
-      /* Call FINAL_WRAPPER (comp);  */
-      gfc_code *final_wrap;
-      gfc_symbol *vtab, *byte_stride;
-      gfc_expr *scalar, *size_expr, *fini_coarray_expr;
-      gfc_component *c;
+  gfc_get_symbol ("idx2", sub_ns, &idx2);
+  idx2->ts.type = BT_INTEGER;
+  idx2->ts.kind = gfc_index_integer_kind;
+  idx2->attr.flavor = FL_VARIABLE;
+  idx2->attr.artificial = 1;
+  gfc_set_sym_referenced (idx2);
+  gfc_commit_symbol (idx2);
+  sa.idx2 = idx2;
 
-      vtab = gfc_find_derived_vtab (comp->ts.u.derived);
-      for (c = vtab->ts.u.derived->components; c; c = c->next)
-       if (strcmp (c->name, "_final") == 0)
-         break;
+  gfc_get_symbol ("offset", sub_ns, &offset);
+  offset->ts.type = BT_INTEGER;
+  offset->ts.kind = gfc_index_integer_kind;
+  offset->attr.flavor = FL_VARIABLE;
+  offset->attr.artificial = 1;
+  gfc_set_sym_referenced (offset);
+  gfc_commit_symbol (offset);
+  sa.offset = offset;
 
-      gcc_assert (c);
+  /* Create RANK expression.  */
+  gfc_expr *rank = gfc_build_intrinsic_call (sub_ns, GFC_ISYM_RANK, "rank",
+                                            gfc_current_locus, 1,
+                                            gfc_copy_expr (array));
+  sa.rank = rank;
+  if (rank->ts.kind != idx->ts.kind)
+    gfc_convert_type_warn (rank, &idx->ts, 2, 0);
 
-      /* Set scalar argument for storage_size. A leading underscore in
-        the name prevents an unwanted finalization.  */
-      gfc_get_symbol ("_comp_byte_stride", sub_ns, &byte_stride);
-      byte_stride->ts = e->ts;
-      byte_stride->attr.flavor = FL_VARIABLE;
-      byte_stride->attr.value = 1;
-      byte_stride->attr.artificial = 1;
-      gfc_set_sym_referenced (byte_stride);
-      gfc_commit_symbol (byte_stride);
-      scalar = gfc_lval_expr_from_sym (byte_stride);
+  /* Create is_contiguous variable.  */
+  gfc_get_symbol ("is_contiguous", sub_ns, &is_contiguous);
+  is_contiguous->ts.type = BT_LOGICAL;
+  is_contiguous->ts.kind = gfc_default_logical_kind;
+  is_contiguous->attr.flavor = FL_VARIABLE;
+  is_contiguous->attr.artificial = 1;
+  gfc_set_sym_referenced (is_contiguous);
+  gfc_commit_symbol (is_contiguous);
+  sa.is_contiguous = is_contiguous;
 
-      final_wrap = gfc_get_code (EXEC_CALL);
-      final_wrap->symtree = c->initializer->symtree;
-      final_wrap->resolved_sym = c->initializer->symtree->n.sym;
-      final_wrap->ext.actual = gfc_get_actual_arglist ();
-      final_wrap->ext.actual->expr = e;
+  /* Create "sizes(0..rank)" variable, which contains the multiplied
+     up extent of the dimensions, i.e. sizes(0) = 1, sizes(1) = extent(dim=1),
+     sizes(2) = sizes(1) * extent(dim=2) etc.  */
+  gfc_get_symbol ("sizes", sub_ns, &sizes);
+  sizes->ts.type = BT_INTEGER;
+  sizes->ts.kind = gfc_index_integer_kind;
+  sizes->attr.flavor = FL_VARIABLE;
+  sizes->attr.dimension = 1;
+  sizes->attr.artificial = 1;
+  sizes->as = gfc_get_array_spec();
+  sizes->attr.intent = INTENT_INOUT;
+  sizes->as->type = AS_EXPLICIT;
+  sizes->as->rank = 1;
+  sizes->as->lower[0] = gfc_get_int_expr (gfc_index_integer_kind, NULL, 0);
+  sizes->as->upper[0] = gfc_copy_expr (rank);
+  gfc_set_sym_referenced (sizes);
+  gfc_commit_symbol (sizes);
+  sa.sizes = sizes;
 
-      /* size_expr = STORAGE_SIZE (...) / NUMERIC_STORAGE_SIZE.  */
-      size_expr = gfc_get_expr ();
-      size_expr->where = gfc_current_locus;
-      size_expr->expr_type = EXPR_OP;
-      size_expr->value.op.op = INTRINSIC_DIVIDE;
+  /* Create "strides(1..rank)" variable, which contains the strides per
+     dimension.  */
+  gfc_get_symbol ("strides", sub_ns, &strides);
+  strides->ts.type = BT_INTEGER;
+  strides->ts.kind = gfc_index_integer_kind;
+  strides->attr.flavor = FL_VARIABLE;
+  strides->attr.dimension = 1;
+  strides->attr.artificial = 1;
+  strides->as = gfc_get_array_spec();
+  strides->attr.intent = INTENT_INOUT;
+  strides->as->type = AS_EXPLICIT;
+  strides->as->rank = 1;
+  strides->as->lower[0] = gfc_get_int_expr (gfc_index_integer_kind, NULL, 1);
+  strides->as->upper[0] = gfc_copy_expr (rank);
+  gfc_set_sym_referenced (strides);
+  gfc_commit_symbol (strides);
+  sa.strides = strides;
 
-      /* STORAGE_SIZE (array,kind=c_intptr_t).  */
-      size_expr->value.op.op1
-       = gfc_build_intrinsic_call (sub_ns, GFC_ISYM_STORAGE_SIZE,
-                                   "storage_size", gfc_current_locus, 2,
-                                   scalar,
-                                   gfc_get_int_expr (gfc_index_integer_kind,
-                                                     NULL, 0));
+  /* Set:  is_contiguous = .true.  */
+  last_code = gfc_get_code (EXEC_ASSIGN);
+  *insertion_point = last_code;
+  last_code->expr1 = gfc_lval_expr_from_sym (is_contiguous);
+  last_code->expr2 = gfc_get_logical_expr (gfc_default_logical_kind,
+                                          &gfc_current_locus, true);
 
-      /* NUMERIC_STORAGE_SIZE.  */
-      size_expr->value.op.op2 = gfc_get_int_expr (gfc_index_integer_kind, NULL,
-                                                 gfc_character_storage_size);
-      size_expr->value.op.op1->ts = size_expr->value.op.op2->ts;
-      size_expr->ts = size_expr->value.op.op1->ts;
+  /* Set:  sizes(0) = 1.  */
+  last_code->next = gfc_get_code (EXEC_ASSIGN);
+  last_code = last_code->next;
+  last_code->expr1 = gfc_lval_expr_from_sym (sizes);
+  last_code->expr1->ref = gfc_get_ref ();
+  last_code->expr1->ref->type = REF_ARRAY;
+  last_code->expr1->ref->u.ar.type = AR_ELEMENT;
+  last_code->expr1->ref->u.ar.dimen = 1;
+  last_code->expr1->ref->u.ar.dimen_type[0] = DIMEN_ELEMENT;
+  last_code->expr1->ref->u.ar.start[0]
+               = gfc_get_int_expr (gfc_index_integer_kind, NULL, 0);
+  last_code->expr1->ref->u.ar.as = sizes->as;
+  last_code->expr2 = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
 
-      /* Which provides the argument 'byte_stride'.....  */
-      final_wrap->ext.actual->next = gfc_get_actual_arglist ();
-      final_wrap->ext.actual->next->expr = size_expr;
+  /* Create:
+     DO idx = 1, rank
+       strides(idx) = _F._stride (array, dim=idx)
+       sizes(idx) = sizes(i-1) * size(array, dim=idx, kind=index_kind)
+       if (strides (idx) /= sizes(i-1)) is_contiguous = .false.
+     END DO.  */
 
-      /* ...and last of all the 'fini_coarray' argument.  */
-      fini_coarray_expr = gfc_lval_expr_from_sym (fini_coarray);
-      final_wrap->ext.actual->next->next = gfc_get_actual_arglist ();
-      final_wrap->ext.actual->next->next->expr = fini_coarray_expr;
+  /* Create loop.  */
+  gfc_iterator *iter = gfc_get_iterator ();
+  iter->var = gfc_lval_expr_from_sym (idx);
+  iter->start = gfc_get_int_expr (gfc_index_integer_kind, NULL, 1);
+  iter->end = gfc_copy_expr (rank);
+  iter->step = gfc_get_int_expr (gfc_index_integer_kind, NULL, 1);
+  last_code->next = gfc_get_code (EXEC_DO);
+  last_code = last_code->next;
+  last_code->ext.iterator = iter;
+  last_code->block = gfc_get_code (EXEC_DO);
 
-      if (*code)
-       {
-         (*code)->next = final_wrap;
-         (*code) = (*code)->next;
-       }
-      else
-       (*code) = final_wrap;
-    }
-  else
-    {
-      gfc_component *c;
-      bool ret = false;
+  /* strides(idx) = _F._stride(array,dim=idx).  */
+  last_code->block->next = gfc_get_code (EXEC_ASSIGN);
+  block = last_code->block->next;
 
-      for (c = comp->ts.u.derived->components; c; c = c->next)
-       ret |= finalize_component (e, comp->ts.u.derived, c, stat, fini_coarray,
-                                  code, sub_ns);
-      /* Only free the expression, if it has never been used.  */
-      if (!ret)
-       gfc_free_expr (e);
-    }
+  block->expr1 = gfc_lval_expr_from_sym (strides);
+  block->expr1->ref = gfc_get_ref ();
+  block->expr1->ref->type = REF_ARRAY;
+  block->expr1->ref->u.ar.type = AR_ELEMENT;
+  block->expr1->ref->u.ar.dimen = 1;
+  block->expr1->ref->u.ar.dimen_type[0] = DIMEN_ELEMENT;
+  block->expr1->ref->u.ar.start[0] = gfc_lval_expr_from_sym (idx);
+  block->expr1->ref->u.ar.as = strides->as;
 
-  /* Record that this was finalized already in this namespace.  */
-  f = sub_ns->was_finalized;
-  sub_ns->was_finalized = XCNEW (gfc_was_finalized);
-  sub_ns->was_finalized->e = expr;
-  sub_ns->was_finalized->c = comp;
-  sub_ns->was_finalized->next = f;
-  return true;
-}
+  block->expr2 = gfc_build_intrinsic_call (sub_ns, GFC_ISYM_STRIDE, "stride",
+                                          gfc_current_locus, 2,
+                                          gfc_copy_expr (array),
+                                          gfc_lval_expr_from_sym (idx));
 
+  /* sizes(idx) = sizes(idx-1) * size(array,dim=idx, kind=index_kind).  */
+  block->next = gfc_get_code (EXEC_ASSIGN);
+  block = block->next;
 
-/* Generate code equivalent to
-   CALL C_F_POINTER (TRANSFER (TRANSFER (C_LOC (array, cptr), c_intptr)
-                    + offset, c_ptr), ptr).  */
+  /* sizes(idx) = ...  */
+  block->expr1 = gfc_lval_expr_from_sym (sizes);
+  block->expr1->ref = gfc_get_ref ();
+  block->expr1->ref->type = REF_ARRAY;
+  block->expr1->ref->u.ar.type = AR_ELEMENT;
+  block->expr1->ref->u.ar.dimen = 1;
+  block->expr1->ref->u.ar.dimen_type[0] = DIMEN_ELEMENT;
+  block->expr1->ref->u.ar.start[0] = gfc_lval_expr_from_sym (idx);
+  block->expr1->ref->u.ar.as = sizes->as;
 
-static gfc_code *
-finalization_scalarizer (gfc_symbol *array, gfc_symbol *ptr,
-                        gfc_expr *offset, gfc_namespace *sub_ns)
-{
-  gfc_code *block;
-  gfc_expr *expr, *expr2;
+  block->expr2 = gfc_get_expr ();
+  block->expr2->expr_type = EXPR_OP;
+  block->expr2->value.op.op = INTRINSIC_TIMES;
+  block->expr2->where = gfc_current_locus;
 
-  /* C_F_POINTER().  */
-  block = gfc_get_code (EXEC_CALL);
-  gfc_get_sym_tree ("c_f_pointer", sub_ns, &block->symtree, true);
-  block->resolved_sym = block->symtree->n.sym;
-  block->resolved_sym->attr.flavor = FL_PROCEDURE;
-  block->resolved_sym->attr.intrinsic = 1;
-  block->resolved_sym->attr.subroutine = 1;
-  block->resolved_sym->from_intmod = INTMOD_ISO_C_BINDING;
-  block->resolved_sym->intmod_sym_id = ISOCBINDING_F_POINTER;
-  block->resolved_isym = gfc_intrinsic_subroutine_by_id (GFC_ISYM_C_F_POINTER);
-  gfc_commit_symbol (block->resolved_sym);
+  /* sizes(idx-1).  */
+  block->expr2->value.op.op1 = gfc_lval_expr_from_sym (sizes);
+  block->expr2->value.op.op1->ref = gfc_get_ref ();
+  block->expr2->value.op.op1->ref->type = REF_ARRAY;
+  block->expr2->value.op.op1->ref->u.ar.as = sizes->as;
+  block->expr2->value.op.op1->ref->u.ar.type = AR_ELEMENT;
+  block->expr2->value.op.op1->ref->u.ar.dimen = 1;
+  block->expr2->value.op.op1->ref->u.ar.dimen_type[0] = DIMEN_ELEMENT;
+  block->expr2->value.op.op1->ref->u.ar.start[0] = gfc_get_expr ();
+  block->expr2->value.op.op1->ref->u.ar.start[0]->expr_type = EXPR_OP;
+  block->expr2->value.op.op1->ref->u.ar.start[0]->where = gfc_current_locus;
+  block->expr2->value.op.op1->ref->u.ar.start[0]->value.op.op = 
INTRINSIC_MINUS;
+  block->expr2->value.op.op1->ref->u.ar.start[0]->value.op.op1
+       = gfc_lval_expr_from_sym (idx);
+  block->expr2->value.op.op1->ref->u.ar.start[0]->value.op.op2
+       = gfc_get_int_expr (gfc_index_integer_kind, NULL, 1);
+  block->expr2->value.op.op1->ref->u.ar.start[0]->ts
+       = block->expr2->value.op.op1->ref->u.ar.start[0]->value.op.op1->ts;
 
-  /* C_F_POINTER's first argument: TRANSFER ( <addr>, c_intptr_t).  */
-  block->ext.actual = gfc_get_actual_arglist ();
-  block->ext.actual->next = gfc_get_actual_arglist ();
-  block->ext.actual->next->expr = gfc_get_int_expr (gfc_index_integer_kind,
-                                                   NULL, 0);
-  block->ext.actual->next->next = gfc_get_actual_arglist (); /* SIZE.  */
+  /* size(array, dim=idx, kind=index_kind).  */
+  block->expr2->value.op.op2
+       = gfc_build_intrinsic_call (sub_ns, GFC_ISYM_SIZE, "size",
+                                   gfc_current_locus, 3,
+                                   gfc_copy_expr (array),
+                                   gfc_lval_expr_from_sym (idx),
+                                   gfc_get_int_expr (gfc_index_integer_kind,
+                                                     NULL,
+                                                     gfc_index_integer_kind));
+  block->expr2->value.op.op2->ts.kind = gfc_index_integer_kind;
+  block->expr2->ts = idx->ts;
 
-  /* The <addr> part: TRANSFER (C_LOC (array), c_intptr_t).  */
+  /* if (strides (idx) /= sizes(idx-1)) is_contiguous = .false.  */
+  block->next = gfc_get_code (EXEC_IF);
+  block = block->next;
 
-  /* TRANSFER's first argument: C_LOC (array).  */
-  expr = gfc_get_expr ();
-  expr->expr_type = EXPR_FUNCTION;
-  gfc_get_sym_tree ("c_loc", sub_ns, &expr->symtree, false);
-  expr->symtree->n.sym->attr.flavor = FL_PROCEDURE;
-  expr->symtree->n.sym->intmod_sym_id = ISOCBINDING_LOC;
-  expr->symtree->n.sym->attr.intrinsic = 1;
-  expr->symtree->n.sym->from_intmod = INTMOD_ISO_C_BINDING;
-  expr->value.function.isym = gfc_intrinsic_function_by_id (GFC_ISYM_C_LOC);
-  expr->value.function.actual = gfc_get_actual_arglist ();
-  expr->value.function.actual->expr
-           = gfc_lval_expr_from_sym (array);
-  expr->symtree->n.sym->result = expr->symtree->n.sym;
-  gfc_commit_symbol (expr->symtree->n.sym);
-  expr->ts.type = BT_INTEGER;
-  expr->ts.kind = gfc_index_integer_kind;
-  expr->where = gfc_current_locus;
+  block->block = gfc_get_code (EXEC_IF);
+  block = block->block;
 
-  /* TRANSFER.  */
-  expr2 = gfc_build_intrinsic_call (sub_ns, GFC_ISYM_TRANSFER, "transfer",
-                                   gfc_current_locus, 3, expr,
-                                   gfc_get_int_expr (gfc_index_integer_kind,
-                                                     NULL, 0), NULL);
-  expr2->ts.type = BT_INTEGER;
-  expr2->ts.kind = gfc_index_integer_kind;
+  /* if condition: strides(idx) /= sizes(idx-1).  */
+  block->expr1 = gfc_get_expr ();
+  block->expr1->ts.type = BT_LOGICAL;
+  block->expr1->ts.kind = gfc_default_logical_kind;
+  block->expr1->expr_type = EXPR_OP;
+  block->expr1->where = gfc_current_locus;
+  block->expr1->value.op.op = INTRINSIC_NE;
 
-  /* <array addr> + <offset>.  */
-  block->ext.actual->expr = gfc_get_expr ();
-  block->ext.actual->expr->expr_type = EXPR_OP;
-  block->ext.actual->expr->value.op.op = INTRINSIC_PLUS;
-  block->ext.actual->expr->value.op.op1 = expr2;
-  block->ext.actual->expr->value.op.op2 = offset;
-  block->ext.actual->expr->ts = expr->ts;
-  block->ext.actual->expr->where = gfc_current_locus;
+  block->expr1->value.op.op1 = gfc_lval_expr_from_sym (strides);
+  block->expr1->value.op.op1->ref = gfc_get_ref ();
+  block->expr1->value.op.op1->ref->type = REF_ARRAY;
+  block->expr1->value.op.op1->ref->u.ar.type = AR_ELEMENT;
+  block->expr1->value.op.op1->ref->u.ar.dimen = 1;
+  block->expr1->value.op.op1->ref->u.ar.dimen_type[0] = DIMEN_ELEMENT;
+  block->expr1->value.op.op1->ref->u.ar.start[0] = gfc_lval_expr_from_sym 
(idx);
+  block->expr1->value.op.op1->ref->u.ar.as = strides->as;
 
-  /* C_F_POINTER's 2nd arg: ptr -- and its absent shape=.  */
-  block->ext.actual->next = gfc_get_actual_arglist ();
-  block->ext.actual->next->expr = gfc_lval_expr_from_sym (ptr);
-  block->ext.actual->next->next = gfc_get_actual_arglist ();
+  block->expr1->value.op.op2 = gfc_lval_expr_from_sym (sizes);
+  block->expr1->value.op.op2->ref = gfc_get_ref ();
+  block->expr1->value.op.op2->ref->type = REF_ARRAY;
+  block->expr1->value.op.op2->ref->u.ar.as = sizes->as;
+  block->expr1->value.op.op2->ref->u.ar.type = AR_ELEMENT;
+  block->expr1->value.op.op2->ref->u.ar.dimen = 1;
+  block->expr1->value.op.op2->ref->u.ar.dimen_type[0] = DIMEN_ELEMENT;
+  block->expr1->value.op.op2->ref->u.ar.start[0] = gfc_get_expr ();
+  block->expr1->value.op.op2->ref->u.ar.start[0]->expr_type = EXPR_OP;
+  block->expr1->value.op.op2->ref->u.ar.start[0]->where = gfc_current_locus;
+  block->expr1->value.op.op2->ref->u.ar.start[0]->value.op.op = 
INTRINSIC_MINUS;
+  block->expr1->value.op.op2->ref->u.ar.start[0]->value.op.op1
+       = gfc_lval_expr_from_sym (idx);
+  block->expr1->value.op.op2->ref->u.ar.start[0]->value.op.op2
+       = gfc_get_int_expr (gfc_index_integer_kind, NULL, 1);
+  block->expr1->value.op.op2->ref->u.ar.start[0]->ts
+       = block->expr1->value.op.op2->ref->u.ar.start[0]->value.op.op1->ts;
 
-  return block;
-}
+  /* if body: is_contiguous = .false.  */
+  block->next = gfc_get_code (EXEC_ASSIGN);
+  block = block->next;
+  block->expr1 = gfc_lval_expr_from_sym (is_contiguous);
+  block->expr2 = gfc_get_logical_expr (gfc_default_logical_kind,
+                                      &gfc_current_locus, false);
 
+  /* Obtain the size (number of elements) of "array" MINUS ONE,
+     which is used in the scalarization.  */
+  gfc_get_symbol ("nelem", sub_ns, &nelem);
+  nelem->ts.type = BT_INTEGER;
+  nelem->ts.kind = gfc_index_integer_kind;
+  nelem->attr.flavor = FL_VARIABLE;
+  nelem->attr.artificial = 1;
+  gfc_set_sym_referenced (nelem);
+  gfc_commit_symbol (nelem);
+  sa.nelem = nelem;
 
-/* Calculates the offset to the (idx+1)th element of an array, taking the
-   stride into account. It generates the code:
-     offset = 0
-     do idx2 = 1, rank
-       offset = offset + mod (idx, sizes(idx2)) / sizes(idx2-1) * strides(idx2)
-     end do
-     offset = offset * byte_stride.  */
+  /* nelem = sizes (rank) - 1.  */
+  last_code->next = gfc_get_code (EXEC_ASSIGN);
+  last_code = last_code->next;
 
-static gfc_code*
-finalization_get_offset (gfc_symbol *idx, gfc_symbol *idx2, gfc_symbol *offset,
-                        gfc_symbol *strides, gfc_symbol *sizes,
-                        gfc_symbol *byte_stride, gfc_expr *rank,
-                        gfc_code *block, gfc_namespace *sub_ns)
+  last_code->expr1 = gfc_lval_expr_from_sym (nelem);
+
+  last_code->expr2 = gfc_get_expr ();
+  last_code->expr2->expr_type = EXPR_OP;
+  last_code->expr2->value.op.op = INTRINSIC_MINUS;
+  last_code->expr2->value.op.op2
+       = gfc_get_int_expr (gfc_index_integer_kind, NULL, 1);
+  last_code->expr2->ts = last_code->expr2->value.op.op2->ts;
+  last_code->expr2->where = gfc_current_locus;
+
+  last_code->expr2->value.op.op1 = gfc_lval_expr_from_sym (sizes);
+  last_code->expr2->value.op.op1->ref = gfc_get_ref ();
+  last_code->expr2->value.op.op1->ref->type = REF_ARRAY;
+  last_code->expr2->value.op.op1->ref->u.ar.type = AR_ELEMENT;
+  last_code->expr2->value.op.op1->ref->u.ar.dimen = 1;
+  last_code->expr2->value.op.op1->ref->u.ar.dimen_type[0] = DIMEN_ELEMENT;
+  last_code->expr2->value.op.op1->ref->u.ar.start[0] = gfc_copy_expr (rank);
+  last_code->expr2->value.op.op1->ref->u.ar.as = sizes->as;
+
+  gfc_get_symbol ("ptr", sub_ns, &ptr);
+  ptr->ts.type = BT_DERIVED;
+  ptr->ts.u.derived = array->ts.u.derived;
+  ptr->attr.flavor = FL_VARIABLE;
+  ptr->attr.pointer = 1;
+  ptr->attr.artificial = 1;
+  gfc_set_sym_referenced (ptr);
+  gfc_commit_symbol (ptr);
+  sa.ptr = ptr;
+
+  return sa;
+}
+
+
+static gfc_expr *
+get_byte_stride (gfc_expr *e, gfc_namespace *sub_ns)
 {
-  gfc_iterator *iter;
-  gfc_expr *expr, *expr2;
+  /* Set scalar argument for storage_size. A leading underscore in
+     the name prevents an unwanted finalization.  */
+  gfc_symbol *byte_stride;
+  gfc_get_symbol ("_comp_byte_stride", sub_ns, &byte_stride);
+  byte_stride->ts = e->ts;
+  byte_stride->attr.flavor = FL_VARIABLE;
+  byte_stride->attr.value = 1;
+  byte_stride->attr.artificial = 1;
+  gfc_set_sym_referenced (byte_stride);
+  gfc_commit_symbol (byte_stride);
+  gfc_expr *scalar = gfc_lval_expr_from_sym (byte_stride);
 
-  /* offset = 0.  */
-  block->next = gfc_get_code (EXEC_ASSIGN);
-  block = block->next;
-  block->expr1 = gfc_lval_expr_from_sym (offset);
-  block->expr2 = gfc_get_int_expr (gfc_index_integer_kind, NULL, 0);
+  /* size_expr = STORAGE_SIZE (...) / NUMERIC_STORAGE_SIZE.  */
+  gfc_expr *size_expr = gfc_get_expr ();
+  size_expr->where = gfc_current_locus;
+  size_expr->expr_type = EXPR_OP;
+  size_expr->value.op.op = INTRINSIC_DIVIDE;
 
-  /* Create loop.  */
-  iter = gfc_get_iterator ();
-  iter->var = gfc_lval_expr_from_sym (idx2);
-  iter->start = gfc_get_int_expr (gfc_index_integer_kind, NULL, 1);
-  iter->end = gfc_copy_expr (rank);
-  iter->step = gfc_get_int_expr (gfc_index_integer_kind, NULL, 1);
-  block->next = gfc_get_code (EXEC_DO);
-  block = block->next;
-  block->ext.iterator = iter;
-  block->block = gfc_get_code (EXEC_DO);
+  /* STORAGE_SIZE (array,kind=c_intptr_t).  */
+  size_expr->value.op.op1
+    = gfc_build_intrinsic_call (sub_ns, GFC_ISYM_STORAGE_SIZE,
+                               "storage_size", gfc_current_locus, 2,
+                               scalar,
+                               gfc_get_int_expr (gfc_index_integer_kind,
+                                                 NULL, 0));
 
-  /* Loop body: offset = offset + mod (idx, sizes(idx2)) / sizes(idx2-1)
-                                 * strides(idx2).  */
+  /* NUMERIC_STORAGE_SIZE.  */
+  size_expr->value.op.op2 = gfc_get_int_expr (gfc_index_integer_kind, NULL,
+                                             gfc_character_storage_size);
+  size_expr->value.op.op1->ts = size_expr->value.op.op2->ts;
+  size_expr->ts = size_expr->value.op.op1->ts;
 
-  /* mod (idx, sizes(idx2)).  */
-  expr = gfc_lval_expr_from_sym (sizes);
-  expr->ref = gfc_get_ref ();
-  expr->ref->type = REF_ARRAY;
-  expr->ref->u.ar.as = sizes->as;
-  expr->ref->u.ar.type = AR_ELEMENT;
-  expr->ref->u.ar.dimen = 1;
-  expr->ref->u.ar.dimen_type[0] = DIMEN_ELEMENT;
-  expr->ref->u.ar.start[0] = gfc_lval_expr_from_sym (idx2);
-  expr->where = sizes->declared_at;
+  return size_expr;
+}
 
-  expr = gfc_build_intrinsic_call (sub_ns, GFC_ISYM_MOD, "mod",
-                                  gfc_current_locus, 2,
-                                  gfc_lval_expr_from_sym (idx), expr);
-  expr->ts = idx->ts;
 
-  /* (...) / sizes(idx2-1).  */
-  expr2 = gfc_get_expr ();
-  expr2->expr_type = EXPR_OP;
-  expr2->value.op.op = INTRINSIC_DIVIDE;
-  expr2->value.op.op1 = expr;
-  expr2->value.op.op2 = gfc_lval_expr_from_sym (sizes);
-  expr2->value.op.op2->ref = gfc_get_ref ();
-  expr2->value.op.op2->ref->type = REF_ARRAY;
-  expr2->value.op.op2->ref->u.ar.as = sizes->as;
-  expr2->value.op.op2->ref->u.ar.type = AR_ELEMENT;
-  expr2->value.op.op2->ref->u.ar.dimen = 1;
-  expr2->value.op.op2->ref->u.ar.dimen_type[0] = DIMEN_ELEMENT;
-  expr2->value.op.op2->ref->u.ar.start[0] = gfc_get_expr ();
-  expr2->value.op.op2->ref->u.ar.start[0]->expr_type = EXPR_OP;
-  expr2->value.op.op2->ref->u.ar.start[0]->where = gfc_current_locus;
-  expr2->value.op.op2->ref->u.ar.start[0]->value.op.op = INTRINSIC_MINUS;
-  expr2->value.op.op2->ref->u.ar.start[0]->value.op.op1
-       = gfc_lval_expr_from_sym (idx2);
-  expr2->value.op.op2->ref->u.ar.start[0]->value.op.op2
-       = gfc_get_int_expr (gfc_index_integer_kind, NULL, 1);
-  expr2->value.op.op2->ref->u.ar.start[0]->ts
-       = expr2->value.op.op2->ref->u.ar.start[0]->value.op.op1->ts;
-  expr2->ts = idx->ts;
-  expr2->where = gfc_current_locus;
+static gfc_code *
+finalization_scalarize (gfc_expr * array, gfc_namespace *host_ns,
+                       scalarizable_array * scalarization_data, 
+                       gfc_code *** scalarized_insertion_point);
 
-  /* ... * strides(idx2).  */
-  expr = gfc_get_expr ();
-  expr->expr_type = EXPR_OP;
-  expr->value.op.op = INTRINSIC_TIMES;
-  expr->value.op.op1 = expr2;
-  expr->value.op.op2 = gfc_lval_expr_from_sym (strides);
-  expr->value.op.op2->ref = gfc_get_ref ();
-  expr->value.op.op2->ref->type = REF_ARRAY;
-  expr->value.op.op2->ref->u.ar.type = AR_ELEMENT;
-  expr->value.op.op2->ref->u.ar.dimen = 1;
-  expr->value.op.op2->ref->u.ar.dimen_type[0] = DIMEN_ELEMENT;
-  expr->value.op.op2->ref->u.ar.start[0] = gfc_lval_expr_from_sym (idx2);
-  expr->value.op.op2->ref->u.ar.as = strides->as;
-  expr->ts = idx->ts;
-  expr->where = gfc_current_locus;
 
-  /* offset = offset + ...  */
-  block->block->next = gfc_get_code (EXEC_ASSIGN);
-  block->block->next->expr1 = gfc_lval_expr_from_sym (offset);
-  block->block->next->expr2 = gfc_get_expr ();
-  block->block->next->expr2->expr_type = EXPR_OP;
-  block->block->next->expr2->value.op.op = INTRINSIC_PLUS;
-  block->block->next->expr2->value.op.op1 = gfc_lval_expr_from_sym (offset);
-  block->block->next->expr2->value.op.op2 = expr;
-  block->block->next->expr2->ts = idx->ts;
-  block->block->next->expr2->where = gfc_current_locus;
+/* Call DEALLOCATE for the passed component if it is allocatable, if it is
+   neither allocatable nor a pointer but has a finalizer, call it. If it
+   is a nonpointer component with allocatable components or has finalizers, 
walk
+   them. Either of them is required; other nonallocatables and pointers aren't
+   handled gracefully.
+   Note: If the component is allocatable, the DEALLOCATE handling takes care
+   of calling the appropriate finalizers, coarray deregistering, and
+   deallocation of allocatable subcomponents.  */
 
-  /* After the loop:  offset = offset * byte_stride.  */
-  block->next = gfc_get_code (EXEC_ASSIGN);
-  block = block->next;
-  block->expr1 = gfc_lval_expr_from_sym (offset);
-  block->expr2 = gfc_get_expr ();
-  block->expr2->expr_type = EXPR_OP;
-  block->expr2->value.op.op = INTRINSIC_TIMES;
-  block->expr2->value.op.op1 = gfc_lval_expr_from_sym (offset);
-  block->expr2->value.op.op2 = gfc_lval_expr_from_sym (byte_stride);
-  block->expr2->ts = block->expr2->value.op.op1->ts;
-  block->expr2->where = gfc_current_locus;
-  return block;
-}
+static bool
+finalize_component (gfc_expr *expr, gfc_symbol *derived, gfc_component *comp,
+                   gfc_symbol *stat, gfc_symbol *fini_coarray, gfc_code **code,
+                   gfc_namespace *sub_ns)
+{
+  gfc_expr *e;
+  gfc_ref *ref;
+  gfc_was_finalized *f;
 
+  if (!comp_is_finalizable (comp))
+    return false;
 
-/* Insert code of the following form:
+  /* If this expression with this component has been finalized
+     already in this namespace, there is nothing to do.  */
+  for (f = sub_ns->was_finalized; f; f = f->next)
+    {
+      if (f->e == expr && f->c == comp)
+       return false;
+    }
 
-   block
-     integer(c_intptr_t) :: i
+  e = gfc_copy_expr (expr);
+  if (!e->ref)
+    e->ref = ref = gfc_get_ref ();
+  else
+    {
+      for (ref = e->ref; ref->next; ref = ref->next)
+       ;
+      ref->next = gfc_get_ref ();
+      ref = ref->next;
+    }
+  ref->type = REF_COMPONENT;
+  ref->u.c.sym = derived;
+  ref->u.c.component = comp;
+  e->ts = comp->ts;
 
-     if ((byte_stride == STORAGE_SIZE (array)/NUMERIC_STORAGE_SIZE
-         && (is_contiguous || !final_rank3->attr.contiguous
-             || final_rank3->as->type != AS_ASSUMED_SHAPE))
-         || 0 == STORAGE_SIZE (array)) then
-       call final_rank3 (array)
-     else
-       block
-         integer(c_intptr_t) :: offset, j
-         type(t) :: tmp(shape (array))
+  if (comp->attr.dimension || comp->attr.codimension
+      || (comp->ts.type == BT_CLASS && CLASS_DATA (comp)
+         && (CLASS_DATA (comp)->attr.dimension
+             || CLASS_DATA (comp)->attr.codimension)))
+    {
+      ref->next = gfc_get_ref ();
+      ref->next->type = REF_ARRAY;
+      ref->next->u.ar.dimen = 0;
+      ref->next->u.ar.as = comp->ts.type == BT_CLASS ? CLASS_DATA (comp)->as
+                                                       : comp->as;
+      e->rank = ref->next->u.ar.as->rank;
+      e->corank = ref->next->u.ar.as->corank;
+      ref->next->u.ar.type = e->rank ? AR_FULL : AR_ELEMENT;
+    }
 
-         do i = 0, size (array)-1
-          offset = obtain_offset(i, strides, sizes, byte_stride)
-          addr = transfer (c_loc (array), addr) + offset
-          call c_f_pointer (transfer (addr, cptr), ptr)
+  /* Call DEALLOCATE (comp, stat=ignore).  */
+  if (comp->attr.allocatable
+      || (comp->ts.type == BT_CLASS && CLASS_DATA (comp)
+         && CLASS_DATA (comp)->attr.allocatable))
+    {
+      gfc_code *dealloc, *block = NULL;
 
-          addr = transfer (c_loc (tmp), addr)
-                           + i * STORAGE_SIZE (array)/NUMERIC_STORAGE_SIZE
-          call c_f_pointer (transfer (addr, cptr), ptr2)
-          ptr2 = ptr
-         end do
-         call final_rank3 (tmp)
-       end block
-     end if
-   block  */
+      /* Add IF (fini_coarray).  */
+      if (comp->attr.codimension
+         || (comp->ts.type == BT_CLASS && CLASS_DATA (comp)
+             && CLASS_DATA (comp)->attr.codimension))
+       {
+         block = gfc_get_code (EXEC_IF);
+         if (*code)
+           {
+             (*code)->next = block;
+             (*code) = (*code)->next;
+           }
+         else
+             (*code) = block;
 
-static void
-finalizer_insert_packed_call (gfc_code *block, gfc_finalizer *fini,
-                             gfc_symbol *array, gfc_symbol *byte_stride,
-                             gfc_symbol *idx, gfc_symbol *ptr,
-                             gfc_symbol *nelem,
-                             gfc_symbol *strides, gfc_symbol *sizes,
-                             gfc_symbol *idx2, gfc_symbol *offset,
-                             gfc_symbol *is_contiguous, gfc_expr *rank,
-                             gfc_namespace *sub_ns)
-{
-  gfc_symbol *tmp_array, *ptr2;
-  gfc_expr *size_expr, *offset2, *expr;
-  gfc_namespace *ns;
-  gfc_iterator *iter;
-  gfc_code *block2;
-  int i;
+         block->block = gfc_get_code (EXEC_IF);
+         block = block->block;
+         block->expr1 = gfc_lval_expr_from_sym (fini_coarray);
+       }
 
-  block->next = gfc_get_code (EXEC_IF);
-  block = block->next;
+      dealloc = gfc_get_code (EXEC_DEALLOCATE);
 
-  block->block = gfc_get_code (EXEC_IF);
-  block = block->block;
+      dealloc->ext.alloc.list = gfc_get_alloc ();
+      dealloc->ext.alloc.list->expr = e;
+      dealloc->expr1 = gfc_lval_expr_from_sym (stat);
 
-  /* size_expr = STORAGE_SIZE (...) / NUMERIC_STORAGE_SIZE.  */
-  size_expr = gfc_get_expr ();
-  size_expr->where = gfc_current_locus;
-  size_expr->expr_type = EXPR_OP;
-  size_expr->value.op.op = INTRINSIC_DIVIDE;
+      gfc_code *cond = gfc_get_code (EXEC_IF);
+      cond->block = gfc_get_code (EXEC_IF);
+      cond->block->expr1 = gfc_get_expr ();
+      cond->block->expr1->expr_type = EXPR_FUNCTION;
+      cond->block->expr1->where = gfc_current_locus;
+      gfc_get_sym_tree ("associated", sub_ns, &cond->block->expr1->symtree, 
false);
+      cond->block->expr1->symtree->n.sym->attr.flavor = FL_PROCEDURE;
+      cond->block->expr1->symtree->n.sym->attr.intrinsic = 1;
+      cond->block->expr1->symtree->n.sym->result = 
cond->block->expr1->symtree->n.sym;
+      gfc_commit_symbol (cond->block->expr1->symtree->n.sym);
+      cond->block->expr1->ts.type = BT_LOGICAL;
+      cond->block->expr1->ts.kind = gfc_default_logical_kind;
+      cond->block->expr1->value.function.isym = gfc_intrinsic_function_by_id 
(GFC_ISYM_ASSOCIATED);
+      cond->block->expr1->value.function.actual = gfc_get_actual_arglist ();
+      /* EXPR only adds nonpointer, nonallocatable components to its base
+        pointer, so testing the base suffices.  */
+      cond->block->expr1->value.function.actual->expr
+       = gfc_lval_expr_from_sym (expr->symtree->n.sym);
+      cond->block->expr1->value.function.actual->next = gfc_get_actual_arglist 
();
+      cond->block->next = dealloc;
 
-  /* STORAGE_SIZE (array,kind=c_intptr_t).  */
-  size_expr->value.op.op1
-       = gfc_build_intrinsic_call (sub_ns, GFC_ISYM_STORAGE_SIZE,
-                                   "storage_size", gfc_current_locus, 2,
-                                   gfc_lval_expr_from_sym (array),
-                                   gfc_get_int_expr (gfc_index_integer_kind,
-                                                     NULL, 0));
+      if (block)
+       block->next = cond;
+      else if (*code)
+       {
+         (*code)->next = cond;
+         (*code) = (*code)->next;
+       }
+      else
+       (*code) = cond;
 
-  /* NUMERIC_STORAGE_SIZE.  */
-  size_expr->value.op.op2 = gfc_get_int_expr (gfc_index_integer_kind, NULL,
-                                             gfc_character_storage_size);
-  size_expr->value.op.op1->ts = size_expr->value.op.op2->ts;
-  size_expr->ts = size_expr->value.op.op1->ts;
+    }
+  else if (comp->ts.type == BT_DERIVED
+           && comp->ts.u.derived->f2k_derived
+           && comp->ts.u.derived->f2k_derived->finalizers)
+    {
+      /* Call FINAL_WRAPPER (comp);  */
+      gfc_code *final_wrap;
+      gfc_symbol *vtab;
+      gfc_expr *fini_coarray_expr;
+      gfc_component *c;
 
-  /* IF condition: (stride == size_expr
-                   && ((fini's as->ASSUMED_SIZE && !fini's attr.contiguous)
-                       || is_contiguous)
-                  || 0 == size_expr.  */
-  block->expr1 = gfc_get_expr ();
-  block->expr1->ts.type = BT_LOGICAL;
-  block->expr1->ts.kind = gfc_default_logical_kind;
-  block->expr1->expr_type = EXPR_OP;
-  block->expr1->where = gfc_current_locus;
+      vtab = gfc_find_derived_vtab (comp->ts.u.derived);
+      for (c = vtab->ts.u.derived->components; c; c = c->next)
+       if (strcmp (c->name, "_final") == 0)
+         break;
 
-  block->expr1->value.op.op = INTRINSIC_OR;
+      gcc_assert (c);
 
-  /* byte_stride == size_expr */
-  expr = gfc_get_expr ();
-  expr->ts.type = BT_LOGICAL;
-  expr->ts.kind = gfc_default_logical_kind;
-  expr->expr_type = EXPR_OP;
-  expr->where = gfc_current_locus;
-  expr->value.op.op = INTRINSIC_EQ;
-  expr->value.op.op1
-       = gfc_lval_expr_from_sym (byte_stride);
-  expr->value.op.op2 = size_expr;
+      final_wrap = gfc_get_code (EXEC_CALL);
+      final_wrap->symtree = c->initializer->symtree;
+      final_wrap->resolved_sym = c->initializer->symtree->n.sym;
+      final_wrap->ext.actual = gfc_get_actual_arglist ();
+      final_wrap->ext.actual->expr = e;
 
-  /* If strides aren't allowed (not assumed shape or CONTIGUOUS),
-     add is_contiguous check.  */
+      /* byte_stride = STORAGE_SIZE (...) / NUMERIC_STORAGE_SIZE.  */
+      final_wrap->ext.actual->next = gfc_get_actual_arglist ();
+      final_wrap->ext.actual->next->expr = get_byte_stride (e, sub_ns);
 
-  if (fini->proc_tree->n.sym->formal->sym->as->type != AS_ASSUMED_SHAPE
-      || fini->proc_tree->n.sym->formal->sym->attr.contiguous)
-    {
-      gfc_expr *expr2;
-      expr2 = gfc_get_expr ();
-      expr2->ts.type = BT_LOGICAL;
-      expr2->ts.kind = gfc_default_logical_kind;
-      expr2->expr_type = EXPR_OP;
-      expr2->where = gfc_current_locus;
-      expr2->value.op.op = INTRINSIC_AND;
-      expr2->value.op.op1 = expr;
-      expr2->value.op.op2 = gfc_lval_expr_from_sym (is_contiguous);
-      expr = expr2;
+      /* ...and last of all the 'fini_coarray' argument.  */
+      fini_coarray_expr = gfc_lval_expr_from_sym (fini_coarray);
+      final_wrap->ext.actual->next->next = gfc_get_actual_arglist ();
+      final_wrap->ext.actual->next->next->expr = fini_coarray_expr;
+
+      if (*code)
+       {
+         (*code)->next = final_wrap;
+         (*code) = (*code)->next;
+       }
+      else
+       (*code) = final_wrap;
     }
+  else
+    {
+      gfc_expr *p;
+      gfc_code **insertion_point;
+      gfc_namespace *ns;
 
-  block->expr1->value.op.op1 = expr;
+      if (array_with_comp_finalizable (comp))
+       {
+         scalarizable_array sa;
+         *code = finalization_scalarize (e, sub_ns, &sa, &insertion_point);
+         p = gfc_lval_expr_from_sym (sa.ptr);
+         ns = sa.ns;
+       }
+      else
+       {
+         p = e;
+         insertion_point = code;
+         ns = sub_ns;
+       }
 
-  /* 0 == size_expr */
-  block->expr1->value.op.op2 = gfc_get_expr ();
-  block->expr1->value.op.op2->ts.type = BT_LOGICAL;
-  block->expr1->value.op.op2->ts.kind = gfc_default_logical_kind;
-  block->expr1->value.op.op2->expr_type = EXPR_OP;
-  block->expr1->value.op.op2->where = gfc_current_locus;
-  block->expr1->value.op.op2->value.op.op = INTRINSIC_EQ;
-  block->expr1->value.op.op2->value.op.op1 =
-                       gfc_get_int_expr (gfc_index_integer_kind, NULL, 0);
-  block->expr1->value.op.op2->value.op.op2 = gfc_copy_expr (size_expr);
+      gfc_component *c;
+      bool ret = false;
 
-  /* IF body: call final subroutine.  */
-  block->next = gfc_get_code (EXEC_CALL);
-  block->next->symtree = fini->proc_tree;
-  block->next->resolved_sym = fini->proc_tree->n.sym;
-  block->next->ext.actual = gfc_get_actual_arglist ();
-  block->next->ext.actual->expr = gfc_lval_expr_from_sym (array);
+      for (c = comp->ts.u.derived->components; c; c = c->next)
+       ret |= finalize_component (p, comp->ts.u.derived, c, stat, fini_coarray,
+                                  insertion_point, ns);
 
-  /* ELSE.  */
+      /* Only free the expression, if it has never been used.  */
+      if (!ret)
+       gfc_free_expr (e);
+    }
 
-  block->block = gfc_get_code (EXEC_IF);
-  block = block->block;
+  /* Record that this was finalized already in this namespace.  */
+  f = sub_ns->was_finalized;
+  sub_ns->was_finalized = XCNEW (gfc_was_finalized);
+  sub_ns->was_finalized->e = expr;
+  sub_ns->was_finalized->c = comp;
+  sub_ns->was_finalized->next = f;
+  return true;
+}
 
-  /* BLOCK ... END BLOCK.  */
-  block->next = gfc_get_code (EXEC_BLOCK);
-  block = block->next;
 
-  ns = gfc_build_block_ns (sub_ns);
-  block->ext.block.ns = ns;
-  block->ext.block.assoc = NULL;
+/* Generate code equivalent to
+   CALL C_F_POINTER (TRANSFER (TRANSFER (C_LOC (array, cptr), c_intptr)
+                    + offset, c_ptr), ptr).  */
 
-  gfc_get_symbol ("ptr2", ns, &ptr2);
-  ptr2->ts.type = BT_DERIVED;
-  ptr2->ts.u.derived = array->ts.u.derived;
-  ptr2->attr.flavor = FL_VARIABLE;
-  ptr2->attr.pointer = 1;
-  ptr2->attr.artificial = 1;
-  gfc_set_sym_referenced (ptr2);
-  gfc_commit_symbol (ptr2);
+static gfc_code *
+finalization_scalarizer (gfc_expr *array, gfc_symbol *ptr,
+                        gfc_expr *offset, gfc_namespace *sub_ns)
+{
+  gfc_code *block;
+  gfc_expr *expr, *expr2;
 
-  gfc_get_symbol ("tmp_array", ns, &tmp_array);
-  tmp_array->ts.type = BT_DERIVED;
-  tmp_array->ts.u.derived = array->ts.u.derived;
-  tmp_array->attr.flavor = FL_VARIABLE;
-  tmp_array->attr.dimension = 1;
-  tmp_array->attr.artificial = 1;
-  tmp_array->as = gfc_get_array_spec();
-  tmp_array->attr.intent = INTENT_INOUT;
-  tmp_array->as->type = AS_EXPLICIT;
-  tmp_array->as->rank = fini->proc_tree->n.sym->formal->sym->as->rank;
+  /* C_F_POINTER().  */
+  block = gfc_get_code (EXEC_CALL);
+  gfc_get_sym_tree ("c_f_pointer", sub_ns, &block->symtree, true);
+  block->resolved_sym = block->symtree->n.sym;
+  block->resolved_sym->attr.flavor = FL_PROCEDURE;
+  block->resolved_sym->attr.intrinsic = 1;
+  block->resolved_sym->attr.subroutine = 1;
+  block->resolved_sym->from_intmod = INTMOD_ISO_C_BINDING;
+  block->resolved_sym->intmod_sym_id = ISOCBINDING_F_POINTER;
+  block->resolved_isym = gfc_intrinsic_subroutine_by_id (GFC_ISYM_C_F_POINTER);
+  gfc_commit_symbol (block->resolved_sym);
 
-  for (i = 0; i < tmp_array->as->rank; i++)
-    {
-      gfc_expr *shape_expr;
-      tmp_array->as->lower[i] = gfc_get_int_expr (gfc_default_integer_kind,
-                                                 NULL, 1);
-      /* SIZE (array, dim=i+1, kind=gfc_index_integer_kind).  */
-      shape_expr
-       = gfc_build_intrinsic_call (sub_ns, GFC_ISYM_SIZE, "size",
-                                   gfc_current_locus, 3,
-                                   gfc_lval_expr_from_sym (array),
-                                   gfc_get_int_expr (gfc_default_integer_kind,
-                                                     NULL, i+1),
-                                   gfc_get_int_expr (gfc_default_integer_kind,
-                                                     NULL,
-                                                     gfc_index_integer_kind));
-      shape_expr->ts.kind = gfc_index_integer_kind;
-      tmp_array->as->upper[i] = shape_expr;
-    }
-  gfc_set_sym_referenced (tmp_array);
-  gfc_commit_symbol (tmp_array);
+  /* C_F_POINTER's first argument: TRANSFER ( <addr>, c_intptr_t).  */
+  block->ext.actual = gfc_get_actual_arglist ();
+  block->ext.actual->next = gfc_get_actual_arglist ();
+  block->ext.actual->next->expr = gfc_get_int_expr (gfc_index_integer_kind,
+                                                   NULL, 0);
+  block->ext.actual->next->next = gfc_get_actual_arglist (); /* SIZE.  */
 
-  /* Create loop.  */
-  iter = gfc_get_iterator ();
-  iter->var = gfc_lval_expr_from_sym (idx);
-  iter->start = gfc_get_int_expr (gfc_index_integer_kind, NULL, 0);
-  iter->end = gfc_lval_expr_from_sym (nelem);
-  iter->step = gfc_get_int_expr (gfc_index_integer_kind, NULL, 1);
+  /* The <addr> part: TRANSFER (C_LOC (array), c_intptr_t).  */
 
-  block = gfc_get_code (EXEC_DO);
-  ns->code = block;
-  block->ext.iterator = iter;
-  block->block = gfc_get_code (EXEC_DO);
+  /* TRANSFER's first argument: C_LOC (array).  */
+  expr = gfc_get_expr ();
+  expr->expr_type = EXPR_FUNCTION;
+  gfc_get_sym_tree ("c_loc", sub_ns, &expr->symtree, false);
+  expr->symtree->n.sym->attr.flavor = FL_PROCEDURE;
+  expr->symtree->n.sym->intmod_sym_id = ISOCBINDING_LOC;
+  expr->symtree->n.sym->attr.intrinsic = 1;
+  expr->symtree->n.sym->from_intmod = INTMOD_ISO_C_BINDING;
+  expr->value.function.isym = gfc_intrinsic_function_by_id (GFC_ISYM_C_LOC);
+  expr->value.function.actual = gfc_get_actual_arglist ();
+  expr->value.function.actual->expr = gfc_copy_expr (array);
+  expr->symtree->n.sym->result = expr->symtree->n.sym;
+  gfc_commit_symbol (expr->symtree->n.sym);
+  expr->ts.type = BT_INTEGER;
+  expr->ts.kind = gfc_index_integer_kind;
+  expr->where = gfc_current_locus;
 
-  /* Offset calculation for the new array: idx * size of type (in bytes).  */
-  offset2 = gfc_get_expr ();
-  offset2->expr_type = EXPR_OP;
-  offset2->where = gfc_current_locus;
-  offset2->value.op.op = INTRINSIC_TIMES;
-  offset2->value.op.op1 = gfc_lval_expr_from_sym (idx);
-  offset2->value.op.op2 = gfc_copy_expr (size_expr);
-  offset2->ts = byte_stride->ts;
+  /* TRANSFER.  */
+  expr2 = gfc_build_intrinsic_call (sub_ns, GFC_ISYM_TRANSFER, "transfer",
+                                   gfc_current_locus, 3, expr,
+                                   gfc_get_int_expr (gfc_index_integer_kind,
+                                                     NULL, 0), NULL);
+  expr2->ts.type = BT_INTEGER;
+  expr2->ts.kind = gfc_index_integer_kind;
 
-  /* Offset calculation of "array".  */
-  block2 = finalization_get_offset (idx, idx2, offset, strides, sizes,
-                                   byte_stride, rank, block->block, sub_ns);
+  /* <array addr> + <offset>.  */
+  block->ext.actual->expr = gfc_get_expr ();
+  block->ext.actual->expr->expr_type = EXPR_OP;
+  block->ext.actual->expr->value.op.op = INTRINSIC_PLUS;
+  block->ext.actual->expr->value.op.op1 = expr2;
+  block->ext.actual->expr->value.op.op2 = offset;
+  block->ext.actual->expr->ts = expr->ts;
+  block->ext.actual->expr->where = gfc_current_locus;
 
-  /* Create code for
-     CALL C_F_POINTER (TRANSFER (TRANSFER (C_LOC (array, cptr), c_intptr)
-                      + idx * stride, c_ptr), ptr).  */
-  block2->next = finalization_scalarizer (array, ptr,
-                                         gfc_lval_expr_from_sym (offset),
-                                         sub_ns);
-  block2 = block2->next;
-  block2->next = finalization_scalarizer (tmp_array, ptr2, offset2, sub_ns);
-  block2 = block2->next;
+  /* C_F_POINTER's 2nd arg: ptr -- and its absent shape=.  */
+  block->ext.actual->next = gfc_get_actual_arglist ();
+  block->ext.actual->next->expr = gfc_lval_expr_from_sym (ptr);
+  block->ext.actual->next->next = gfc_get_actual_arglist ();
 
-  /* ptr2 = ptr.  */
-  block2->next = gfc_get_code (EXEC_ASSIGN);
-  block2 = block2->next;
-  block2->expr1 = gfc_lval_expr_from_sym (ptr2);
-  block2->expr2 = gfc_lval_expr_from_sym (ptr);
+  return block;
+}
 
-  /* Call now the user's final subroutine.  */
-  block->next  = gfc_get_code (EXEC_CALL);
-  block = block->next;
-  block->symtree = fini->proc_tree;
-  block->resolved_sym = fini->proc_tree->n.sym;
-  block->ext.actual = gfc_get_actual_arglist ();
-  block->ext.actual->expr = gfc_lval_expr_from_sym (tmp_array);
 
-  if (fini->proc_tree->n.sym->formal->sym->attr.intent == INTENT_IN)
-    return;
+/* Calculates the offset to the (idx+1)th element of an array, taking the
+   stride into account. It generates the code:
+     offset = 0
+     do idx2 = 1, rank
+       offset = offset + mod (idx, sizes(idx2)) / sizes(idx2-1) * strides(idx2)
+     end do
+     offset = offset * byte_stride.  */
 
-  /* Copy back.  */
+static gfc_code*
+finalization_get_offset (const scalarizable_array & array, gfc_expr 
*byte_stride,
+                        gfc_code *block)
+{
+  gfc_iterator *iter;
+  gfc_expr *expr, *expr2;
 
-  /* Loop.  */
+  /* offset = 0.  */
+  block->next = gfc_get_code (EXEC_ASSIGN);
+  block = block->next;
+  block->expr1 = gfc_lval_expr_from_sym (array.offset);
+  block->expr2 = gfc_get_int_expr (gfc_index_integer_kind, NULL, 0);
+
+  /* Create loop.  */
   iter = gfc_get_iterator ();
-  iter->var = gfc_lval_expr_from_sym (idx);
-  iter->start = gfc_get_int_expr (gfc_index_integer_kind, NULL, 0);
-  iter->end = gfc_lval_expr_from_sym (nelem);
+  iter->var = gfc_lval_expr_from_sym (array.idx2);
+  iter->start = gfc_get_int_expr (gfc_index_integer_kind, NULL, 1);
+  iter->end = gfc_copy_expr (array.rank);
   iter->step = gfc_get_int_expr (gfc_index_integer_kind, NULL, 1);
-
   block->next = gfc_get_code (EXEC_DO);
   block = block->next;
   block->ext.iterator = iter;
   block->block = gfc_get_code (EXEC_DO);
 
-  /* Offset calculation of "array".  */
-  block2 = finalization_get_offset (idx, idx2, offset, strides, sizes,
-                                   byte_stride, rank, block->block, sub_ns);
-
-  /* Create code for
-     CALL C_F_POINTER (TRANSFER (TRANSFER (C_LOC (array, cptr), c_intptr)
-                      + offset, c_ptr), ptr).  */
-  block2->next = finalization_scalarizer (array, ptr,
-                                         gfc_lval_expr_from_sym (offset),
-                                         sub_ns);
-  block2 = block2->next;
-  block2->next = finalization_scalarizer (tmp_array, ptr2,
-                                         gfc_copy_expr (offset2), sub_ns);
-  block2 = block2->next;
-
-  /* ptr = ptr2.  */
-  block2->next = gfc_get_code (EXEC_ASSIGN);
-  block2->next->expr1 = gfc_lval_expr_from_sym (ptr);
-  block2->next->expr2 = gfc_lval_expr_from_sym (ptr2);
-}
+  /* Loop body: offset = offset + mod (idx, sizes(idx2)) / sizes(idx2-1)
+                                 * strides(idx2).  */
 
+  /* mod (idx, sizes(idx2)).  */
+  expr = gfc_lval_expr_from_sym (array.sizes);
+  expr->ref = gfc_get_ref ();
+  expr->ref->type = REF_ARRAY;
+  expr->ref->u.ar.as = array.sizes->as;
+  expr->ref->u.ar.type = AR_ELEMENT;
+  expr->ref->u.ar.dimen = 1;
+  expr->ref->u.ar.dimen_type[0] = DIMEN_ELEMENT;
+  expr->ref->u.ar.start[0] = gfc_lval_expr_from_sym (array.idx2);
+  expr->where = array.sizes->declared_at;
 
-/* Generate the finalization/polymorphic freeing wrapper subroutine for the
-   derived type "derived". The function first calls the appropriate FINAL
-   subroutine, then it DEALLOCATEs (finalizes/frees) the allocatable
-   components (but not the inherited ones). Last, it calls the wrapper
-   subroutine of the parent. The generated wrapper procedure takes as argument
-   an assumed-rank array.
-   If neither allocatable components nor FINAL subroutines exists, the vtab
-   will contain a NULL pointer.
-   The generated function has the form
-     _final(assumed-rank array, stride, skip_corarray)
-   where the array has to be contiguous (except of the lowest dimension). The
-   stride (in bytes) is used to allow different sizes for ancestor types by
-   skipping over the additionally added components in the scalarizer. If
-   "fini_coarray" is false, coarray components are not finalized to allow for
-   the correct semantic with intrinsic assignment.  */
+  expr = gfc_build_intrinsic_call (array.ns, GFC_ISYM_MOD, "mod",
+                                  gfc_current_locus, 2,
+                                  gfc_lval_expr_from_sym (array.idx), expr);
+  expr->ts = array.idx->ts;
 
-static void
-generate_finalization_wrapper (gfc_symbol *derived, gfc_namespace *ns,
-                              const char *tname, gfc_component *vtab_final)
+  /* (...) / sizes(idx2-1).  */
+  expr2 = gfc_get_expr ();
+  expr2->expr_type = EXPR_OP;
+  expr2->value.op.op = INTRINSIC_DIVIDE;
+  expr2->value.op.op1 = expr;
+  expr2->value.op.op2 = gfc_lval_expr_from_sym (array.sizes);
+  expr2->value.op.op2->ref = gfc_get_ref ();
+  expr2->value.op.op2->ref->type = REF_ARRAY;
+  expr2->value.op.op2->ref->u.ar.as = array.sizes->as;
+  expr2->value.op.op2->ref->u.ar.type = AR_ELEMENT;
+  expr2->value.op.op2->ref->u.ar.dimen = 1;
+  expr2->value.op.op2->ref->u.ar.dimen_type[0] = DIMEN_ELEMENT;
+  expr2->value.op.op2->ref->u.ar.start[0] = gfc_get_expr ();
+  expr2->value.op.op2->ref->u.ar.start[0]->expr_type = EXPR_OP;
+  expr2->value.op.op2->ref->u.ar.start[0]->where = gfc_current_locus;
+  expr2->value.op.op2->ref->u.ar.start[0]->value.op.op = INTRINSIC_MINUS;
+  expr2->value.op.op2->ref->u.ar.start[0]->value.op.op1
+       = gfc_lval_expr_from_sym (array.idx2);
+  expr2->value.op.op2->ref->u.ar.start[0]->value.op.op2
+       = gfc_get_int_expr (gfc_index_integer_kind, NULL, 1);
+  expr2->value.op.op2->ref->u.ar.start[0]->ts
+       = expr2->value.op.op2->ref->u.ar.start[0]->value.op.op1->ts;
+  expr2->ts = array.idx->ts;
+  expr2->where = gfc_current_locus;
+
+  /* ... * strides(idx2).  */
+  expr = gfc_get_expr ();
+  expr->expr_type = EXPR_OP;
+  expr->value.op.op = INTRINSIC_TIMES;
+  expr->value.op.op1 = expr2;
+  expr->value.op.op2 = gfc_lval_expr_from_sym (array.strides);
+  expr->value.op.op2->ref = gfc_get_ref ();
+  expr->value.op.op2->ref->type = REF_ARRAY;
+  expr->value.op.op2->ref->u.ar.type = AR_ELEMENT;
+  expr->value.op.op2->ref->u.ar.dimen = 1;
+  expr->value.op.op2->ref->u.ar.dimen_type[0] = DIMEN_ELEMENT;
+  expr->value.op.op2->ref->u.ar.start[0] = gfc_lval_expr_from_sym (array.idx2);
+  expr->value.op.op2->ref->u.ar.as = array.strides->as;
+  expr->ts = array.idx->ts;
+  expr->where = gfc_current_locus;
+
+  /* offset = offset + ...  */
+  block->block->next = gfc_get_code (EXEC_ASSIGN);
+  block->block->next->expr1 = gfc_lval_expr_from_sym (array.offset);
+  block->block->next->expr2 = gfc_get_expr ();
+  block->block->next->expr2->expr_type = EXPR_OP;
+  block->block->next->expr2->value.op.op = INTRINSIC_PLUS;
+  block->block->next->expr2->value.op.op1 = gfc_lval_expr_from_sym 
(array.offset);
+  block->block->next->expr2->value.op.op2 = expr;
+  block->block->next->expr2->ts = array.idx->ts;
+  block->block->next->expr2->where = gfc_current_locus;
+
+  /* After the loop:  offset = offset * byte_stride.  */
+  block->next = gfc_get_code (EXEC_ASSIGN);
+  block = block->next;
+  block->expr1 = gfc_lval_expr_from_sym (array.offset);
+  block->expr2 = gfc_get_expr ();
+  block->expr2->expr_type = EXPR_OP;
+  block->expr2->value.op.op = INTRINSIC_TIMES;
+  block->expr2->value.op.op1 = gfc_lval_expr_from_sym (array.offset);
+  block->expr2->value.op.op2 = gfc_copy_expr (byte_stride);
+  block->expr2->ts = block->expr2->value.op.op1->ts;
+  block->expr2->where = gfc_current_locus;
+  return block;
+}
+
+
+static gfc_code *
+finalization_scalarize (gfc_expr * array, gfc_namespace *host_ns,
+                       scalarizable_array * scalarization_data, 
+                       gfc_code *** scalarized_insertion_point)
 {
-  gfc_symbol *final, *array, *fini_coarray, *byte_stride, *sizes, *strides;
-  gfc_symbol *ptr = NULL, *idx, *idx2, *is_contiguous, *offset, *nelem;
-  gfc_symbol *result = NULL;
-  gfc_component *comp;
-  gfc_namespace *sub_ns;
-  gfc_code *last_code, *block;
-  char *name;
-  char *result_name;
-  bool finalizable_comp = false;
-  gfc_expr *ancestor_wrapper = NULL, *rank;
-  gfc_iterator *iter;
+  gfc_code *block = gfc_get_code (EXEC_BLOCK);
 
-  if (derived->attr.unlimited_polymorphic || derived->error)
-    {
-      vtab_final->initializer = gfc_get_null_expr (NULL);
-      return;
-    }
+  gfc_namespace *ns = gfc_build_block_ns (host_ns);
+  block->ext.block.ns = ns;
+  block->ext.block.assoc = NULL;
 
-  /* Search for the ancestor's finalizers.  */
-  if (derived->attr.extension && derived->components
-      && (!derived->components->ts.u.derived->attr.abstract
-         || has_finalizer_component (derived)))
-    {
-      gfc_symbol *vtab;
-      gfc_component *comp;
+  scalarizable_array sa;
+  sa = prepare_scalarization (gfc_copy_expr (array), ns, &ns->code);
 
-      vtab = gfc_find_derived_vtab (derived->components->ts.u.derived);
-      for (comp = vtab->ts.u.derived->components; comp; comp = comp->next)
-       if (comp->name[0] == '_' && comp->name[1] == 'f')
-         {
-           ancestor_wrapper = comp->initializer;
-           break;
-         }
-    }
+  /* Create loop.  */
+  gfc_iterator *iter = gfc_get_iterator ();
+  iter->var = gfc_lval_expr_from_sym (sa.idx);
+  iter->start = gfc_get_int_expr (gfc_index_integer_kind, NULL, 0);
+  iter->end = gfc_lval_expr_from_sym (sa.nelem);
+  iter->step = gfc_get_int_expr (gfc_index_integer_kind, NULL, 1);
+  gfc_code *last_code = gfc_get_code (EXEC_DO);
+  ns->code = last_code;
+  last_code->ext.iterator = iter;
+  last_code->block = gfc_get_code (EXEC_DO);
 
-  /* No wrapper of the ancestor and no own FINAL subroutines and allocatable
-     components: Return a NULL() expression; we defer this a bit to have
-     an interface declaration.  */
-  if ((!ancestor_wrapper || ancestor_wrapper->expr_type == EXPR_NULL)
-      && !derived->attr.alloc_comp
-      && (!derived->f2k_derived || !derived->f2k_derived->finalizers)
-      && !has_finalizer_component (derived))
-    {
-      vtab_final->initializer = gfc_get_null_expr (NULL);
-      gcc_assert (vtab_final->ts.interface == NULL);
-      return;
-    }
-  else
-    /* Check whether there are new allocatable components.  */
-    for (comp = derived->components; comp; comp = comp->next)
-      {
-       if (comp == derived->components && derived->attr.extension
-           && ancestor_wrapper && ancestor_wrapper->expr_type != EXPR_NULL)
-       continue;
+  /* Offset calculation.  */
+  block = finalization_get_offset (sa, get_byte_stride (array, ns),
+                                  last_code->block);
 
-       finalizable_comp |= comp_is_finalizable (comp);
-      }
+  /* Create code for
+     CALL C_F_POINTER (TRANSFER (TRANSFER (C_LOC (array, cptr), c_intptr)
+                      + idx * stride, c_ptr), ptr).  */
+  block->next = finalization_scalarizer (sa.array, sa.ptr,
+                                        gfc_lval_expr_from_sym(sa.offset),
+                                        ns);
 
-  /* If there is no new finalizer and no new allocatable, return with
-     an expr to the ancestor's one.  */
-  if (!finalizable_comp
-      && (!derived->f2k_derived || !derived->f2k_derived->finalizers))
-    {
-      gcc_assert (ancestor_wrapper && ancestor_wrapper->ref == NULL
-                 && ancestor_wrapper->expr_type == EXPR_VARIABLE);
-      vtab_final->initializer = gfc_copy_expr (ancestor_wrapper);
-      vtab_final->ts.interface = vtab_final->initializer->symtree->n.sym;
-      return;
-    }
+  *scalarization_data = sa;
+  *scalarized_insertion_point = &block->next;
 
-  /* We now create a wrapper, which does the following:
-     1. Call the suitable finalization subroutine for this type
-     2. Loop over all noninherited allocatable components and noninherited
-       components with allocatable components and DEALLOCATE those; this will
-       take care of finalizers, coarray deregistering and allocatable
-       nested components.
-     3. Call the ancestor's finalizer.  */
+  return block;
+}
 
-  /* Declare the wrapper function; it takes an assumed-rank array
-     and a VALUE logical as arguments.  */
 
-  /* Set up the namespace.  */
-  sub_ns = gfc_get_namespace (ns, 0);
-  sub_ns->sibling = ns->contained;
-  ns->contained = sub_ns;
-  sub_ns->resolved = 1;
+/* Insert code of the following form:
 
-  /* Set up the procedure symbol.  */
-  name = xasprintf ("__final_%s", tname);
-  gfc_get_symbol (name, sub_ns, &final);
-  sub_ns->proc_name = final;
-  final->attr.flavor = FL_PROCEDURE;
-  final->attr.function = 1;
-  final->attr.pure = 0;
-  final->attr.recursive = 1;
-  final->ts.type = BT_INTEGER;
-  final->ts.kind = 4;
-  final->attr.artificial = 1;
-  final->attr.always_explicit = 1;
-  final->attr.if_source = IFSRC_DECL;
-  if (ns->proc_name->attr.flavor == FL_MODULE)
-    final->module = ns->proc_name->name;
+   block
+     integer(c_intptr_t) :: i
 
-  /* Create a separate result symbol instead of using final->result = final.
-     Self-referencing result symbols (final->result = final) create a cycle
-     in the symbol structure that causes an ICE in gimplify_call_expr when
-     the finalizer wrapper is used as a procedure pointer initializer.  */
-  result_name = xasprintf ("__result_%s", tname);
-  if (gfc_get_symbol (result_name, sub_ns, &result) != 0)
-    gfc_internal_error ("Failed to create finalizer result symbol");
-  free (result_name);
+     if ((byte_stride == STORAGE_SIZE (array)/NUMERIC_STORAGE_SIZE
+         && (is_contiguous || !final_rank3->attr.contiguous
+             || final_rank3->as->type != AS_ASSUMED_SHAPE))
+         || 0 == STORAGE_SIZE (array)) then
+       call final_rank3 (array)
+     else
+       block
+         integer(c_intptr_t) :: offset, j
+         type(t) :: tmp(shape (array))
 
-  if (!gfc_add_flavor (&result->attr, FL_VARIABLE, result->name,
-                      &gfc_current_locus)
-      || !gfc_add_result (&result->attr, result->name, &gfc_current_locus))
-    gfc_internal_error ("Failed to set finalizer result attributes");
+         do i = 0, size (array)-1
+          offset = obtain_offset(i, strides, sizes, byte_stride)
+          addr = transfer (c_loc (array), addr) + offset
+          call c_f_pointer (transfer (addr, cptr), ptr)
 
-  result->ts = final->ts;
-  result->attr.artificial = 1;
-  gfc_set_sym_referenced (result);
-  gfc_commit_symbol (result);
-  final->result = result;
-  gfc_set_sym_referenced (final);
-  gfc_commit_symbol (final);
+          addr = transfer (c_loc (tmp), addr)
+                           + i * STORAGE_SIZE (array)/NUMERIC_STORAGE_SIZE
+          call c_f_pointer (transfer (addr, cptr), ptr2)
+          ptr2 = ptr
+         end do
+         call final_rank3 (tmp)
+       end block
+     end if
+   block  */
 
-  /* Set up formal argument.  */
-  gfc_get_symbol ("array", sub_ns, &array);
-  array->ts.type = BT_DERIVED;
-  array->ts.u.derived = derived;
-  array->attr.flavor = FL_VARIABLE;
-  array->attr.dummy = 1;
-  array->attr.contiguous = 1;
-  array->attr.dimension = 1;
-  array->attr.artificial = 1;
-  array->as = gfc_get_array_spec();
-  array->as->type = AS_ASSUMED_RANK;
-  array->as->rank = -1;
-  array->attr.intent = INTENT_INOUT;
-  gfc_set_sym_referenced (array);
-  final->formal = gfc_get_formal_arglist ();
-  final->formal->sym = array;
-  gfc_commit_symbol (array);
+static void
+finalizer_insert_packed_call (gfc_code *block, gfc_finalizer *fini,
+                             const scalarizable_array & sa,
+                             gfc_expr *byte_stride)
+{
+  gfc_symbol *tmp_array, *ptr2;
+  gfc_expr *size_expr, *offset2, *expr;
+  gfc_namespace *ns;
+  gfc_iterator *iter;
+  gfc_code *block2;
+  int i;
 
-  /* Set up formal argument.  */
-  gfc_get_symbol ("byte_stride", sub_ns, &byte_stride);
-  byte_stride->ts.type = BT_INTEGER;
-  byte_stride->ts.kind = gfc_index_integer_kind;
-  byte_stride->attr.flavor = FL_VARIABLE;
-  byte_stride->attr.dummy = 1;
-  byte_stride->attr.value = 1;
-  byte_stride->attr.artificial = 1;
-  gfc_set_sym_referenced (byte_stride);
-  final->formal->next = gfc_get_formal_arglist ();
-  final->formal->next->sym = byte_stride;
-  gfc_commit_symbol (byte_stride);
+  block->next = gfc_get_code (EXEC_IF);
+  block = block->next;
 
-  /* Set up formal argument.  */
-  gfc_get_symbol ("fini_coarray", sub_ns, &fini_coarray);
-  fini_coarray->ts.type = BT_LOGICAL;
-  fini_coarray->ts.kind = 1;
-  fini_coarray->attr.flavor = FL_VARIABLE;
-  fini_coarray->attr.dummy = 1;
-  fini_coarray->attr.value = 1;
-  fini_coarray->attr.artificial = 1;
-  gfc_set_sym_referenced (fini_coarray);
-  final->formal->next->next = gfc_get_formal_arglist ();
-  final->formal->next->next->sym = fini_coarray;
-  gfc_commit_symbol (fini_coarray);
+  block->block = gfc_get_code (EXEC_IF);
+  block = block->block;
+
+  /* size_expr = STORAGE_SIZE (...) / NUMERIC_STORAGE_SIZE.  */
+  size_expr = gfc_get_expr ();
+  size_expr->where = gfc_current_locus;
+  size_expr->expr_type = EXPR_OP;
+  size_expr->value.op.op = INTRINSIC_DIVIDE;
+
+  /* STORAGE_SIZE (array,kind=c_intptr_t).  */
+  size_expr->value.op.op1
+       = gfc_build_intrinsic_call (sa.ns, GFC_ISYM_STORAGE_SIZE,
+                                   "storage_size", gfc_current_locus, 2,
+                                   gfc_copy_expr (sa.array),
+                                   gfc_get_int_expr (gfc_index_integer_kind,
+                                                     NULL, 0));
+
+  /* NUMERIC_STORAGE_SIZE.  */
+  size_expr->value.op.op2 = gfc_get_int_expr (gfc_index_integer_kind, NULL,
+                                             gfc_character_storage_size);
+  size_expr->value.op.op1->ts = size_expr->value.op.op2->ts;
+  size_expr->ts = size_expr->value.op.op1->ts;
+
+  /* IF condition: (stride == size_expr
+                   && ((fini's as->ASSUMED_SIZE && !fini's attr.contiguous)
+                       || is_contiguous)
+                  || 0 == size_expr.  */
+  block->expr1 = gfc_get_expr ();
+  block->expr1->ts.type = BT_LOGICAL;
+  block->expr1->ts.kind = gfc_default_logical_kind;
+  block->expr1->expr_type = EXPR_OP;
+  block->expr1->where = gfc_current_locus;
+
+  block->expr1->value.op.op = INTRINSIC_OR;
+
+  /* byte_stride == size_expr */
+  expr = gfc_get_expr ();
+  expr->ts.type = BT_LOGICAL;
+  expr->ts.kind = gfc_default_logical_kind;
+  expr->expr_type = EXPR_OP;
+  expr->where = gfc_current_locus;
+  expr->value.op.op = INTRINSIC_EQ;
+  expr->value.op.op1 = gfc_copy_expr (byte_stride);
+  expr->value.op.op2 = size_expr;
+
+  /* If strides aren't allowed (not assumed shape or CONTIGUOUS),
+     add is_contiguous check.  */
+
+  if (fini->proc_tree->n.sym->formal->sym->as->type != AS_ASSUMED_SHAPE
+      || fini->proc_tree->n.sym->formal->sym->attr.contiguous)
+    {
+      gfc_expr *expr2;
+      expr2 = gfc_get_expr ();
+      expr2->ts.type = BT_LOGICAL;
+      expr2->ts.kind = gfc_default_logical_kind;
+      expr2->expr_type = EXPR_OP;
+      expr2->where = gfc_current_locus;
+      expr2->value.op.op = INTRINSIC_AND;
+      expr2->value.op.op1 = expr;
+      expr2->value.op.op2 = gfc_lval_expr_from_sym (sa.is_contiguous);
+      expr = expr2;
+    }
+
+  block->expr1->value.op.op1 = expr;
+
+  /* 0 == size_expr */
+  block->expr1->value.op.op2 = gfc_get_expr ();
+  block->expr1->value.op.op2->ts.type = BT_LOGICAL;
+  block->expr1->value.op.op2->ts.kind = gfc_default_logical_kind;
+  block->expr1->value.op.op2->expr_type = EXPR_OP;
+  block->expr1->value.op.op2->where = gfc_current_locus;
+  block->expr1->value.op.op2->value.op.op = INTRINSIC_EQ;
+  block->expr1->value.op.op2->value.op.op1 =
+                       gfc_get_int_expr (gfc_index_integer_kind, NULL, 0);
+  block->expr1->value.op.op2->value.op.op2 = gfc_copy_expr (size_expr);
+
+  /* IF body: call final subroutine.  */
+  block->next = gfc_get_code (EXEC_CALL);
+  block->next->symtree = fini->proc_tree;
+  block->next->resolved_sym = fini->proc_tree->n.sym;
+  block->next->ext.actual = gfc_get_actual_arglist ();
+  block->next->ext.actual->expr = gfc_copy_expr (sa.array);
 
-  /* Local variables.  */
+  /* ELSE.  */
 
-  gfc_get_symbol ("idx", sub_ns, &idx);
-  idx->ts.type = BT_INTEGER;
-  idx->ts.kind = gfc_index_integer_kind;
-  idx->attr.flavor = FL_VARIABLE;
-  idx->attr.artificial = 1;
-  gfc_set_sym_referenced (idx);
-  gfc_commit_symbol (idx);
+  block->block = gfc_get_code (EXEC_IF);
+  block = block->block;
 
-  gfc_get_symbol ("idx2", sub_ns, &idx2);
-  idx2->ts.type = BT_INTEGER;
-  idx2->ts.kind = gfc_index_integer_kind;
-  idx2->attr.flavor = FL_VARIABLE;
-  idx2->attr.artificial = 1;
-  gfc_set_sym_referenced (idx2);
-  gfc_commit_symbol (idx2);
+  /* BLOCK ... END BLOCK.  */
+  block->next = gfc_get_code (EXEC_BLOCK);
+  block = block->next;
 
-  gfc_get_symbol ("offset", sub_ns, &offset);
-  offset->ts.type = BT_INTEGER;
-  offset->ts.kind = gfc_index_integer_kind;
-  offset->attr.flavor = FL_VARIABLE;
-  offset->attr.artificial = 1;
-  gfc_set_sym_referenced (offset);
-  gfc_commit_symbol (offset);
+  ns = gfc_build_block_ns (sa.ns);
+  block->ext.block.ns = ns;
+  block->ext.block.assoc = NULL;
 
-  /* Create RANK expression.  */
-  rank = gfc_build_intrinsic_call (sub_ns, GFC_ISYM_RANK, "rank",
-                                  gfc_current_locus, 1,
-                                  gfc_lval_expr_from_sym (array));
-  if (rank->ts.kind != idx->ts.kind)
-    gfc_convert_type_warn (rank, &idx->ts, 2, 0);
+  gfc_get_symbol ("ptr2", ns, &ptr2);
+  ptr2->ts.type = BT_DERIVED;
+  ptr2->ts.u.derived = sa.array->ts.u.derived;
+  ptr2->attr.flavor = FL_VARIABLE;
+  ptr2->attr.pointer = 1;
+  ptr2->attr.artificial = 1;
+  gfc_set_sym_referenced (ptr2);
+  gfc_commit_symbol (ptr2);
 
-  /* Create is_contiguous variable.  */
-  gfc_get_symbol ("is_contiguous", sub_ns, &is_contiguous);
-  is_contiguous->ts.type = BT_LOGICAL;
-  is_contiguous->ts.kind = gfc_default_logical_kind;
-  is_contiguous->attr.flavor = FL_VARIABLE;
-  is_contiguous->attr.artificial = 1;
-  gfc_set_sym_referenced (is_contiguous);
-  gfc_commit_symbol (is_contiguous);
+  gfc_get_symbol ("tmp_array", ns, &tmp_array);
+  tmp_array->ts.type = BT_DERIVED;
+  tmp_array->ts.u.derived = sa.array->ts.u.derived;
+  tmp_array->attr.flavor = FL_VARIABLE;
+  tmp_array->attr.dimension = 1;
+  tmp_array->attr.artificial = 1;
+  tmp_array->as = gfc_get_array_spec();
+  tmp_array->attr.intent = INTENT_INOUT;
+  tmp_array->as->type = AS_EXPLICIT;
+  tmp_array->as->rank = fini->proc_tree->n.sym->formal->sym->as->rank;
 
-  /* Create "sizes(0..rank)" variable, which contains the multiplied
-     up extent of the dimensions, i.e. sizes(0) = 1, sizes(1) = extent(dim=1),
-     sizes(2) = sizes(1) * extent(dim=2) etc.  */
-  gfc_get_symbol ("sizes", sub_ns, &sizes);
-  sizes->ts.type = BT_INTEGER;
-  sizes->ts.kind = gfc_index_integer_kind;
-  sizes->attr.flavor = FL_VARIABLE;
-  sizes->attr.dimension = 1;
-  sizes->attr.artificial = 1;
-  sizes->as = gfc_get_array_spec();
-  sizes->attr.intent = INTENT_INOUT;
-  sizes->as->type = AS_EXPLICIT;
-  sizes->as->rank = 1;
-  sizes->as->lower[0] = gfc_get_int_expr (gfc_index_integer_kind, NULL, 0);
-  sizes->as->upper[0] = gfc_copy_expr (rank);
-  gfc_set_sym_referenced (sizes);
-  gfc_commit_symbol (sizes);
+  for (i = 0; i < tmp_array->as->rank; i++)
+    {
+      gfc_expr *shape_expr;
+      tmp_array->as->lower[i] = gfc_get_int_expr (gfc_default_integer_kind,
+                                                 NULL, 1);
+      /* SIZE (array, dim=i+1, kind=gfc_index_integer_kind).  */
+      shape_expr
+       = gfc_build_intrinsic_call (sa.ns, GFC_ISYM_SIZE, "size",
+                                   gfc_current_locus, 3,
+                                   gfc_copy_expr (sa.array),
+                                   gfc_get_int_expr (gfc_default_integer_kind,
+                                                     NULL, i+1),
+                                   gfc_get_int_expr (gfc_default_integer_kind,
+                                                     NULL,
+                                                     gfc_index_integer_kind));
+      shape_expr->ts.kind = gfc_index_integer_kind;
+      tmp_array->as->upper[i] = shape_expr;
+    }
+  gfc_set_sym_referenced (tmp_array);
+  gfc_commit_symbol (tmp_array);
 
-  /* Create "strides(1..rank)" variable, which contains the strides per
-     dimension.  */
-  gfc_get_symbol ("strides", sub_ns, &strides);
-  strides->ts.type = BT_INTEGER;
-  strides->ts.kind = gfc_index_integer_kind;
-  strides->attr.flavor = FL_VARIABLE;
-  strides->attr.dimension = 1;
-  strides->attr.artificial = 1;
-  strides->as = gfc_get_array_spec();
-  strides->attr.intent = INTENT_INOUT;
-  strides->as->type = AS_EXPLICIT;
-  strides->as->rank = 1;
-  strides->as->lower[0] = gfc_get_int_expr (gfc_index_integer_kind, NULL, 1);
-  strides->as->upper[0] = gfc_copy_expr (rank);
-  gfc_set_sym_referenced (strides);
-  gfc_commit_symbol (strides);
+  /* Create loop.  */
+  iter = gfc_get_iterator ();
+  iter->var = gfc_lval_expr_from_sym (sa.idx);
+  iter->start = gfc_get_int_expr (gfc_index_integer_kind, NULL, 0);
+  iter->end = gfc_lval_expr_from_sym (sa.nelem);
+  iter->step = gfc_get_int_expr (gfc_index_integer_kind, NULL, 1);
 
+  block = gfc_get_code (EXEC_DO);
+  ns->code = block;
+  block->ext.iterator = iter;
+  block->block = gfc_get_code (EXEC_DO);
 
-  /* Set return value to 0.  */
-  last_code = gfc_get_code (EXEC_ASSIGN);
-  last_code->expr1 = gfc_lval_expr_from_sym (result);
-  last_code->expr2 = gfc_get_int_expr (4, NULL, 0);
-  sub_ns->code = last_code;
+  /* Offset calculation for the new array: idx * size of type (in bytes).  */
+  offset2 = gfc_get_expr ();
+  offset2->expr_type = EXPR_OP;
+  offset2->where = gfc_current_locus;
+  offset2->value.op.op = INTRINSIC_TIMES;
+  offset2->value.op.op1 = gfc_lval_expr_from_sym (sa.idx);
+  offset2->value.op.op2 = gfc_copy_expr (size_expr);
+  offset2->ts = byte_stride->ts;
 
-  /* Set:  is_contiguous = .true.  */
-  last_code->next = gfc_get_code (EXEC_ASSIGN);
-  last_code = last_code->next;
-  last_code->expr1 = gfc_lval_expr_from_sym (is_contiguous);
-  last_code->expr2 = gfc_get_logical_expr (gfc_default_logical_kind,
-                                          &gfc_current_locus, true);
+  /* Offset calculation of "array".  */
+  block2 = finalization_get_offset (sa, byte_stride, block->block);
 
-  /* Set:  sizes(0) = 1.  */
-  last_code->next = gfc_get_code (EXEC_ASSIGN);
-  last_code = last_code->next;
-  last_code->expr1 = gfc_lval_expr_from_sym (sizes);
-  last_code->expr1->ref = gfc_get_ref ();
-  last_code->expr1->ref->type = REF_ARRAY;
-  last_code->expr1->ref->u.ar.type = AR_ELEMENT;
-  last_code->expr1->ref->u.ar.dimen = 1;
-  last_code->expr1->ref->u.ar.dimen_type[0] = DIMEN_ELEMENT;
-  last_code->expr1->ref->u.ar.start[0]
-               = gfc_get_int_expr (gfc_index_integer_kind, NULL, 0);
-  last_code->expr1->ref->u.ar.as = sizes->as;
-  last_code->expr2 = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
+  /* Create code for
+     CALL C_F_POINTER (TRANSFER (TRANSFER (C_LOC (array, cptr), c_intptr)
+                      + idx * stride, c_ptr), ptr).  */
+  block2->next = finalization_scalarizer (sa.array, sa.ptr,
+                                         gfc_lval_expr_from_sym (sa.offset),
+                                         sa.ns);
+  block2 = block2->next;
+  block2->next = finalization_scalarizer (gfc_lval_expr_from_sym (tmp_array),
+                                         ptr2, offset2, sa.ns);
+  block2 = block2->next;
 
-  /* Create:
-     DO idx = 1, rank
-       strides(idx) = _F._stride (array, dim=idx)
-       sizes(idx) = sizes(i-1) * size(array, dim=idx, kind=index_kind)
-       if (strides (idx) /= sizes(i-1)) is_contiguous = .false.
-     END DO.  */
+  /* ptr2 = ptr.  */
+  block2->next = gfc_get_code (EXEC_ASSIGN);
+  block2 = block2->next;
+  block2->expr1 = gfc_lval_expr_from_sym (ptr2);
+  block2->expr2 = gfc_lval_expr_from_sym (sa.ptr);
 
-  /* Create loop.  */
+  /* Call now the user's final subroutine.  */
+  block->next  = gfc_get_code (EXEC_CALL);
+  block = block->next;
+  block->symtree = fini->proc_tree;
+  block->resolved_sym = fini->proc_tree->n.sym;
+  block->ext.actual = gfc_get_actual_arglist ();
+  block->ext.actual->expr = gfc_lval_expr_from_sym (tmp_array);
+
+  if (fini->proc_tree->n.sym->formal->sym->attr.intent == INTENT_IN)
+    return;
+
+  /* Copy back.  */
+
+  /* Loop.  */
   iter = gfc_get_iterator ();
-  iter->var = gfc_lval_expr_from_sym (idx);
-  iter->start = gfc_get_int_expr (gfc_index_integer_kind, NULL, 1);
-  iter->end = gfc_copy_expr (rank);
+  iter->var = gfc_lval_expr_from_sym (sa.idx);
+  iter->start = gfc_get_int_expr (gfc_index_integer_kind, NULL, 0);
+  iter->end = gfc_lval_expr_from_sym (sa.nelem);
   iter->step = gfc_get_int_expr (gfc_index_integer_kind, NULL, 1);
-  last_code->next = gfc_get_code (EXEC_DO);
-  last_code = last_code->next;
-  last_code->ext.iterator = iter;
-  last_code->block = gfc_get_code (EXEC_DO);
 
-  /* strides(idx) = _F._stride(array,dim=idx).  */
-  last_code->block->next = gfc_get_code (EXEC_ASSIGN);
-  block = last_code->block->next;
+  block->next = gfc_get_code (EXEC_DO);
+  block = block->next;
+  block->ext.iterator = iter;
+  block->block = gfc_get_code (EXEC_DO);
 
-  block->expr1 = gfc_lval_expr_from_sym (strides);
-  block->expr1->ref = gfc_get_ref ();
-  block->expr1->ref->type = REF_ARRAY;
-  block->expr1->ref->u.ar.type = AR_ELEMENT;
-  block->expr1->ref->u.ar.dimen = 1;
-  block->expr1->ref->u.ar.dimen_type[0] = DIMEN_ELEMENT;
-  block->expr1->ref->u.ar.start[0] = gfc_lval_expr_from_sym (idx);
-  block->expr1->ref->u.ar.as = strides->as;
+  /* Offset calculation of "array".  */
+  block2 = finalization_get_offset (sa, byte_stride, block->block);
 
-  block->expr2 = gfc_build_intrinsic_call (sub_ns, GFC_ISYM_STRIDE, "stride",
-                                          gfc_current_locus, 2,
-                                          gfc_lval_expr_from_sym (array),
-                                          gfc_lval_expr_from_sym (idx));
+  /* Create code for
+     CALL C_F_POINTER (TRANSFER (TRANSFER (C_LOC (array, cptr), c_intptr)
+                      + offset, c_ptr), ptr).  */
+  block2->next = finalization_scalarizer (sa.array, sa.ptr,
+                                         gfc_lval_expr_from_sym (sa.offset),
+                                         sa.ns);
+  block2 = block2->next;
+  block2->next = finalization_scalarizer (gfc_lval_expr_from_sym (tmp_array),
+                                         ptr2, gfc_copy_expr (offset2), sa.ns);
+  block2 = block2->next;
+
+  /* ptr = ptr2.  */
+  block2->next = gfc_get_code (EXEC_ASSIGN);
+  block2->next->expr1 = gfc_lval_expr_from_sym (sa.ptr);
+  block2->next->expr2 = gfc_lval_expr_from_sym (ptr2);
+}
+
+
+/* Generate the finalization/polymorphic freeing wrapper subroutine for the
+   derived type "derived". The function first calls the appropriate FINAL
+   subroutine, then it DEALLOCATEs (finalizes/frees) the allocatable
+   components (but not the inherited ones). Last, it calls the wrapper
+   subroutine of the parent. The generated wrapper procedure takes as argument
+   an assumed-rank array.
+   If neither allocatable components nor FINAL subroutines exists, the vtab
+   will contain a NULL pointer.
+   The generated function has the form
+     _final(assumed-rank array, stride, skip_corarray)
+   where the array has to be contiguous (except of the lowest dimension). The
+   stride (in bytes) is used to allow different sizes for ancestor types by
+   skipping over the additionally added components in the scalarizer. If
+   "fini_coarray" is false, coarray components are not finalized to allow for
+   the correct semantic with intrinsic assignment.  */
+
+static void
+generate_finalization_wrapper (gfc_symbol *derived, gfc_namespace *ns,
+                              const char *tname, gfc_component *vtab_final)
+{
+  gfc_symbol *final, *array, *fini_coarray, *byte_stride;
+  gfc_symbol *ptr = NULL;
+  gfc_symbol *result = NULL;
+  gfc_component *comp;
+  gfc_namespace *sub_ns;
+  gfc_code *last_code, *block;
+  char *name;
+  char *result_name;
+  bool finalizable_comp = false;
+  gfc_expr *ancestor_wrapper = NULL;
+  gfc_iterator *iter;
+
+  if (derived->attr.unlimited_polymorphic || derived->error)
+    {
+      vtab_final->initializer = gfc_get_null_expr (NULL);
+      return;
+    }
+
+  /* Search for the ancestor's finalizers.  */
+  if (derived->attr.extension && derived->components
+      && (!derived->components->ts.u.derived->attr.abstract
+         || has_finalizer_component (derived)))
+    {
+      gfc_symbol *vtab;
+      gfc_component *comp;
 
-  /* sizes(idx) = sizes(idx-1) * size(array,dim=idx, kind=index_kind).  */
-  block->next = gfc_get_code (EXEC_ASSIGN);
-  block = block->next;
+      vtab = gfc_find_derived_vtab (derived->components->ts.u.derived);
+      for (comp = vtab->ts.u.derived->components; comp; comp = comp->next)
+       if (comp->name[0] == '_' && comp->name[1] == 'f')
+         {
+           ancestor_wrapper = comp->initializer;
+           break;
+         }
+    }
 
-  /* sizes(idx) = ...  */
-  block->expr1 = gfc_lval_expr_from_sym (sizes);
-  block->expr1->ref = gfc_get_ref ();
-  block->expr1->ref->type = REF_ARRAY;
-  block->expr1->ref->u.ar.type = AR_ELEMENT;
-  block->expr1->ref->u.ar.dimen = 1;
-  block->expr1->ref->u.ar.dimen_type[0] = DIMEN_ELEMENT;
-  block->expr1->ref->u.ar.start[0] = gfc_lval_expr_from_sym (idx);
-  block->expr1->ref->u.ar.as = sizes->as;
+  /* No wrapper of the ancestor and no own FINAL subroutines and allocatable
+     components: Return a NULL() expression; we defer this a bit to have
+     an interface declaration.  */
+  if ((!ancestor_wrapper || ancestor_wrapper->expr_type == EXPR_NULL)
+      && !derived->attr.alloc_comp
+      && (!derived->f2k_derived || !derived->f2k_derived->finalizers)
+      && !has_finalizer_component (derived))
+    {
+      vtab_final->initializer = gfc_get_null_expr (NULL);
+      gcc_assert (vtab_final->ts.interface == NULL);
+      return;
+    }
+  else
+    /* Check whether there are new allocatable components.  */
+    for (comp = derived->components; comp; comp = comp->next)
+      {
+       if (comp == derived->components && derived->attr.extension
+           && ancestor_wrapper && ancestor_wrapper->expr_type != EXPR_NULL)
+       continue;
 
-  block->expr2 = gfc_get_expr ();
-  block->expr2->expr_type = EXPR_OP;
-  block->expr2->value.op.op = INTRINSIC_TIMES;
-  block->expr2->where = gfc_current_locus;
+       finalizable_comp |= comp_is_finalizable (comp);
+      }
 
-  /* sizes(idx-1).  */
-  block->expr2->value.op.op1 = gfc_lval_expr_from_sym (sizes);
-  block->expr2->value.op.op1->ref = gfc_get_ref ();
-  block->expr2->value.op.op1->ref->type = REF_ARRAY;
-  block->expr2->value.op.op1->ref->u.ar.as = sizes->as;
-  block->expr2->value.op.op1->ref->u.ar.type = AR_ELEMENT;
-  block->expr2->value.op.op1->ref->u.ar.dimen = 1;
-  block->expr2->value.op.op1->ref->u.ar.dimen_type[0] = DIMEN_ELEMENT;
-  block->expr2->value.op.op1->ref->u.ar.start[0] = gfc_get_expr ();
-  block->expr2->value.op.op1->ref->u.ar.start[0]->expr_type = EXPR_OP;
-  block->expr2->value.op.op1->ref->u.ar.start[0]->where = gfc_current_locus;
-  block->expr2->value.op.op1->ref->u.ar.start[0]->value.op.op = 
INTRINSIC_MINUS;
-  block->expr2->value.op.op1->ref->u.ar.start[0]->value.op.op1
-       = gfc_lval_expr_from_sym (idx);
-  block->expr2->value.op.op1->ref->u.ar.start[0]->value.op.op2
-       = gfc_get_int_expr (gfc_index_integer_kind, NULL, 1);
-  block->expr2->value.op.op1->ref->u.ar.start[0]->ts
-       = block->expr2->value.op.op1->ref->u.ar.start[0]->value.op.op1->ts;
+  /* If there is no new finalizer and no new allocatable, return with
+     an expr to the ancestor's one.  */
+  if (!finalizable_comp
+      && (!derived->f2k_derived || !derived->f2k_derived->finalizers))
+    {
+      gcc_assert (ancestor_wrapper && ancestor_wrapper->ref == NULL
+                 && ancestor_wrapper->expr_type == EXPR_VARIABLE);
+      vtab_final->initializer = gfc_copy_expr (ancestor_wrapper);
+      vtab_final->ts.interface = vtab_final->initializer->symtree->n.sym;
+      return;
+    }
 
-  /* size(array, dim=idx, kind=index_kind).  */
-  block->expr2->value.op.op2
-       = gfc_build_intrinsic_call (sub_ns, GFC_ISYM_SIZE, "size",
-                                   gfc_current_locus, 3,
-                                   gfc_lval_expr_from_sym (array),
-                                   gfc_lval_expr_from_sym (idx),
-                                   gfc_get_int_expr (gfc_index_integer_kind,
-                                                     NULL,
-                                                     gfc_index_integer_kind));
-  block->expr2->value.op.op2->ts.kind = gfc_index_integer_kind;
-  block->expr2->ts = idx->ts;
+  /* We now create a wrapper, which does the following:
+     1. Call the suitable finalization subroutine for this type
+     2. Loop over all noninherited allocatable components and noninherited
+       components with allocatable components and DEALLOCATE those; this will
+       take care of finalizers, coarray deregistering and allocatable
+       nested components.
+     3. Call the ancestor's finalizer.  */
 
-  /* if (strides (idx) /= sizes(idx-1)) is_contiguous = .false.  */
-  block->next = gfc_get_code (EXEC_IF);
-  block = block->next;
+  /* Declare the wrapper function; it takes an assumed-rank array
+     and a VALUE logical as arguments.  */
 
-  block->block = gfc_get_code (EXEC_IF);
-  block = block->block;
+  /* Set up the namespace.  */
+  sub_ns = gfc_get_namespace (ns, 0);
+  sub_ns->sibling = ns->contained;
+  ns->contained = sub_ns;
+  sub_ns->resolved = 1;
 
-  /* if condition: strides(idx) /= sizes(idx-1).  */
-  block->expr1 = gfc_get_expr ();
-  block->expr1->ts.type = BT_LOGICAL;
-  block->expr1->ts.kind = gfc_default_logical_kind;
-  block->expr1->expr_type = EXPR_OP;
-  block->expr1->where = gfc_current_locus;
-  block->expr1->value.op.op = INTRINSIC_NE;
+  /* Set up the procedure symbol.  */
+  name = xasprintf ("__final_%s", tname);
+  gfc_get_symbol (name, sub_ns, &final);
+  sub_ns->proc_name = final;
+  final->attr.flavor = FL_PROCEDURE;
+  final->attr.function = 1;
+  final->attr.pure = 0;
+  final->attr.recursive = 1;
+  final->ts.type = BT_INTEGER;
+  final->ts.kind = 4;
+  final->attr.artificial = 1;
+  final->attr.always_explicit = 1;
+  final->attr.if_source = IFSRC_DECL;
+  if (ns->proc_name->attr.flavor == FL_MODULE)
+    final->module = ns->proc_name->name;
 
-  block->expr1->value.op.op1 = gfc_lval_expr_from_sym (strides);
-  block->expr1->value.op.op1->ref = gfc_get_ref ();
-  block->expr1->value.op.op1->ref->type = REF_ARRAY;
-  block->expr1->value.op.op1->ref->u.ar.type = AR_ELEMENT;
-  block->expr1->value.op.op1->ref->u.ar.dimen = 1;
-  block->expr1->value.op.op1->ref->u.ar.dimen_type[0] = DIMEN_ELEMENT;
-  block->expr1->value.op.op1->ref->u.ar.start[0] = gfc_lval_expr_from_sym 
(idx);
-  block->expr1->value.op.op1->ref->u.ar.as = strides->as;
+  /* Create a separate result symbol instead of using final->result = final.
+     Self-referencing result symbols (final->result = final) create a cycle
+     in the symbol structure that causes an ICE in gimplify_call_expr when
+     the finalizer wrapper is used as a procedure pointer initializer.  */
+  result_name = xasprintf ("__result_%s", tname);
+  if (gfc_get_symbol (result_name, sub_ns, &result) != 0)
+    gfc_internal_error ("Failed to create finalizer result symbol");
+  free (result_name);
 
-  block->expr1->value.op.op2 = gfc_lval_expr_from_sym (sizes);
-  block->expr1->value.op.op2->ref = gfc_get_ref ();
-  block->expr1->value.op.op2->ref->type = REF_ARRAY;
-  block->expr1->value.op.op2->ref->u.ar.as = sizes->as;
-  block->expr1->value.op.op2->ref->u.ar.type = AR_ELEMENT;
-  block->expr1->value.op.op2->ref->u.ar.dimen = 1;
-  block->expr1->value.op.op2->ref->u.ar.dimen_type[0] = DIMEN_ELEMENT;
-  block->expr1->value.op.op2->ref->u.ar.start[0] = gfc_get_expr ();
-  block->expr1->value.op.op2->ref->u.ar.start[0]->expr_type = EXPR_OP;
-  block->expr1->value.op.op2->ref->u.ar.start[0]->where = gfc_current_locus;
-  block->expr1->value.op.op2->ref->u.ar.start[0]->value.op.op = 
INTRINSIC_MINUS;
-  block->expr1->value.op.op2->ref->u.ar.start[0]->value.op.op1
-       = gfc_lval_expr_from_sym (idx);
-  block->expr1->value.op.op2->ref->u.ar.start[0]->value.op.op2
-       = gfc_get_int_expr (gfc_index_integer_kind, NULL, 1);
-  block->expr1->value.op.op2->ref->u.ar.start[0]->ts
-       = block->expr1->value.op.op2->ref->u.ar.start[0]->value.op.op1->ts;
+  if (!gfc_add_flavor (&result->attr, FL_VARIABLE, result->name,
+                      &gfc_current_locus)
+      || !gfc_add_result (&result->attr, result->name, &gfc_current_locus))
+    gfc_internal_error ("Failed to set finalizer result attributes");
 
-  /* if body: is_contiguous = .false.  */
-  block->next = gfc_get_code (EXEC_ASSIGN);
-  block = block->next;
-  block->expr1 = gfc_lval_expr_from_sym (is_contiguous);
-  block->expr2 = gfc_get_logical_expr (gfc_default_logical_kind,
-                                      &gfc_current_locus, false);
+  result->ts = final->ts;
+  result->attr.artificial = 1;
+  gfc_set_sym_referenced (result);
+  gfc_commit_symbol (result);
+  final->result = result;
+  gfc_set_sym_referenced (final);
+  gfc_commit_symbol (final);
 
-  /* Obtain the size (number of elements) of "array" MINUS ONE,
-     which is used in the scalarization.  */
-  gfc_get_symbol ("nelem", sub_ns, &nelem);
-  nelem->ts.type = BT_INTEGER;
-  nelem->ts.kind = gfc_index_integer_kind;
-  nelem->attr.flavor = FL_VARIABLE;
-  nelem->attr.artificial = 1;
-  gfc_set_sym_referenced (nelem);
-  gfc_commit_symbol (nelem);
+  /* Set up formal argument.  */
+  gfc_get_symbol ("array", sub_ns, &array);
+  array->ts.type = BT_DERIVED;
+  array->ts.u.derived = derived;
+  array->attr.flavor = FL_VARIABLE;
+  array->attr.dummy = 1;
+  array->attr.contiguous = 1;
+  array->attr.dimension = 1;
+  array->attr.artificial = 1;
+  array->as = gfc_get_array_spec();
+  array->as->type = AS_ASSUMED_RANK;
+  array->as->rank = -1;
+  array->attr.intent = INTENT_INOUT;
+  gfc_set_sym_referenced (array);
+  final->formal = gfc_get_formal_arglist ();
+  final->formal->sym = array;
+  gfc_commit_symbol (array);
 
-  /* nelem = sizes (rank) - 1.  */
-  last_code->next = gfc_get_code (EXEC_ASSIGN);
-  last_code = last_code->next;
+  /* Set up formal argument.  */
+  gfc_get_symbol ("byte_stride", sub_ns, &byte_stride);
+  byte_stride->ts.type = BT_INTEGER;
+  byte_stride->ts.kind = gfc_index_integer_kind;
+  byte_stride->attr.flavor = FL_VARIABLE;
+  byte_stride->attr.dummy = 1;
+  byte_stride->attr.value = 1;
+  byte_stride->attr.artificial = 1;
+  gfc_set_sym_referenced (byte_stride);
+  final->formal->next = gfc_get_formal_arglist ();
+  final->formal->next->sym = byte_stride;
+  gfc_commit_symbol (byte_stride);
 
-  last_code->expr1 = gfc_lval_expr_from_sym (nelem);
+  /* Set up formal argument.  */
+  gfc_get_symbol ("fini_coarray", sub_ns, &fini_coarray);
+  fini_coarray->ts.type = BT_LOGICAL;
+  fini_coarray->ts.kind = 1;
+  fini_coarray->attr.flavor = FL_VARIABLE;
+  fini_coarray->attr.dummy = 1;
+  fini_coarray->attr.value = 1;
+  fini_coarray->attr.artificial = 1;
+  gfc_set_sym_referenced (fini_coarray);
+  final->formal->next->next = gfc_get_formal_arglist ();
+  final->formal->next->next->sym = fini_coarray;
+  gfc_commit_symbol (fini_coarray);
 
-  last_code->expr2 = gfc_get_expr ();
-  last_code->expr2->expr_type = EXPR_OP;
-  last_code->expr2->value.op.op = INTRINSIC_MINUS;
-  last_code->expr2->value.op.op2
-       = gfc_get_int_expr (gfc_index_integer_kind, NULL, 1);
-  last_code->expr2->ts = last_code->expr2->value.op.op2->ts;
-  last_code->expr2->where = gfc_current_locus;
+  scalarizable_array sa = prepare_scalarization (gfc_lval_expr_from_sym 
(array),
+                                                sub_ns, &sub_ns->code);
 
-  last_code->expr2->value.op.op1 = gfc_lval_expr_from_sym (sizes);
-  last_code->expr2->value.op.op1->ref = gfc_get_ref ();
-  last_code->expr2->value.op.op1->ref->type = REF_ARRAY;
-  last_code->expr2->value.op.op1->ref->u.ar.type = AR_ELEMENT;
-  last_code->expr2->value.op.op1->ref->u.ar.dimen = 1;
-  last_code->expr2->value.op.op1->ref->u.ar.dimen_type[0] = DIMEN_ELEMENT;
-  last_code->expr2->value.op.op1->ref->u.ar.start[0] = gfc_copy_expr (rank);
-  last_code->expr2->value.op.op1->ref->u.ar.as = sizes->as;
+  /* Set return value to 0.  */
+  last_code = gfc_get_code (EXEC_ASSIGN);
+  last_code->expr1 = gfc_lval_expr_from_sym (result);
+  last_code->expr2 = gfc_get_int_expr (4, NULL, 0);
+  sub_ns->code = last_code;
 
   /* Call final subroutines. We now generate code like:
      use iso_c_binding
@@ -2198,15 +2345,6 @@ generate_finalization_wrapper (gfc_symbol *derived, 
gfc_namespace *ns,
     {
       gfc_finalizer *fini, *fini_elem = NULL;
 
-      gfc_get_symbol ("ptr1", sub_ns, &ptr);
-      ptr->ts.type = BT_DERIVED;
-      ptr->ts.u.derived = derived;
-      ptr->attr.flavor = FL_VARIABLE;
-      ptr->attr.pointer = 1;
-      ptr->attr.artificial = 1;
-      gfc_set_sym_referenced (ptr);
-      gfc_commit_symbol (ptr);
-
       fini = derived->f2k_derived->finalizers;
 
       /* Assumed rank finalizers can be called directly. The call takes care
@@ -2228,7 +2366,7 @@ generate_finalization_wrapper (gfc_symbol *derived, 
gfc_namespace *ns,
       /* SELECT CASE (RANK (array)).  */
       last_code->next = gfc_get_code (EXEC_SELECT);
       last_code = last_code->next;
-      last_code->expr1 = gfc_copy_expr (rank);
+      last_code->expr1 = gfc_copy_expr (sa.rank);
       block = NULL;
 
 
@@ -2266,10 +2404,8 @@ generate_finalization_wrapper (gfc_symbol *derived, 
gfc_namespace *ns,
 
          /* CALL fini_rank (array) - possibly with packing.  */
           if (fini->proc_tree->n.sym->formal->sym->attr.dimension)
-           finalizer_insert_packed_call (block, fini, array, byte_stride,
-                                         idx, ptr, nelem, strides,
-                                         sizes, idx2, offset, is_contiguous,
-                                         rank, sub_ns);
+           finalizer_insert_packed_call (block, fini, sa,
+                                         gfc_lval_expr_from_sym (byte_stride));
          else
            {
              block->next = gfc_get_code (EXEC_CALL);
@@ -2298,9 +2434,9 @@ generate_finalization_wrapper (gfc_symbol *derived, 
gfc_namespace *ns,
 
          /* Create loop.  */
          iter = gfc_get_iterator ();
-         iter->var = gfc_lval_expr_from_sym (idx);
+         iter->var = gfc_lval_expr_from_sym (sa.idx);
          iter->start = gfc_get_int_expr (gfc_index_integer_kind, NULL, 0);
-         iter->end = gfc_lval_expr_from_sym (nelem);
+         iter->end = gfc_lval_expr_from_sym (sa.nelem);
          iter->step = gfc_get_int_expr (gfc_index_integer_kind, NULL, 1);
          block->next = gfc_get_code (EXEC_DO);
          block = block->next;
@@ -2308,16 +2444,16 @@ generate_finalization_wrapper (gfc_symbol *derived, 
gfc_namespace *ns,
          block->block = gfc_get_code (EXEC_DO);
 
          /* Offset calculation.  */
-         block = finalization_get_offset (idx, idx2, offset, strides, sizes,
-                                          byte_stride, rank, block->block,
-                                          sub_ns);
+         block = finalization_get_offset (sa,
+                                          gfc_lval_expr_from_sym (byte_stride),
+                                          block->block);
 
          /* Create code for
             CALL C_F_POINTER (TRANSFER (TRANSFER (C_LOC (array, cptr), 
c_intptr)
                               + offset, c_ptr), ptr).  */
          block->next
-               = finalization_scalarizer (array, ptr,
-                                          gfc_lval_expr_from_sym (offset),
+               = finalization_scalarizer (gfc_lval_expr_from_sym (array), ptr,
+                                          gfc_lval_expr_from_sym (sa.offset),
                                           sub_ns);
          block = block->next;
 
@@ -2364,9 +2500,9 @@ finish_assumed_rank:
 
       /* Create loop.  */
       iter = gfc_get_iterator ();
-      iter->var = gfc_lval_expr_from_sym (idx);
+      iter->var = gfc_lval_expr_from_sym (sa.idx);
       iter->start = gfc_get_int_expr (gfc_index_integer_kind, NULL, 0);
-      iter->end = gfc_lval_expr_from_sym (nelem);
+      iter->end = gfc_lval_expr_from_sym (sa.nelem);
       iter->step = gfc_get_int_expr (gfc_index_integer_kind, NULL, 1);
       last_code->next = gfc_get_code (EXEC_DO);
       last_code = last_code->next;
@@ -2374,15 +2510,14 @@ finish_assumed_rank:
       last_code->block = gfc_get_code (EXEC_DO);
 
       /* Offset calculation.  */
-      block = finalization_get_offset (idx, idx2, offset, strides, sizes,
-                                      byte_stride, rank, last_code->block,
-                                      sub_ns);
+      block = finalization_get_offset (sa, gfc_lval_expr_from_sym 
(byte_stride),
+                                      last_code->block);
 
       /* Create code for
         CALL C_F_POINTER (TRANSFER (TRANSFER (C_LOC (array, cptr), c_intptr)
                           + idx * stride, c_ptr), ptr).  */
-      block->next = finalization_scalarizer (array, ptr,
-                                            gfc_lval_expr_from_sym(offset),
+      block->next = finalization_scalarizer (gfc_lval_expr_from_sym (array), 
ptr,
+                                            gfc_lval_expr_from_sym(sa.offset),
                                             sub_ns);
       block = block->next;
 
@@ -2418,7 +2553,8 @@ finish_assumed_rank:
                        = gfc_lval_expr_from_sym (fini_coarray);
     }
 
-  gfc_free_expr (rank);
+  //TODO:
+  //gfc_free_expr (rank);
   vtab_final->initializer = gfc_lval_expr_from_sym (final);
   vtab_final->ts.interface = final;
   free (name);

Reply via email to