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

commit eb598269ef7b34ec84bc60b2e8c616d9b028995e
Author: Mikael Morin <[email protected]>
Date:   Mon Nov 23 18:00:04 2026 +0100

    Correction pr87992.f90

Diff:
---
 gcc/fortran/class.cc | 743 +++++++++++++++++++++++++++++----------------------
 1 file changed, 430 insertions(+), 313 deletions(-)

diff --git a/gcc/fortran/class.cc b/gcc/fortran/class.cc
index e95d818fe507..89f854cc5c35 100644
--- a/gcc/fortran/class.cc
+++ b/gcc/fortran/class.cc
@@ -632,6 +632,9 @@ gfc_get_len_component (gfc_expr *e, int k)
 }
 
 
+static gfc_symbol * find_derived_vtype (gfc_symbol *derived);
+
+
 /* Build a polymorphic CLASS entity, using the symbol that comes from
    build_sym. A CLASS entity is represented by an encapsulating type,
    which contains the declared type as '_data' component, plus a pointer
@@ -648,7 +651,6 @@ gfc_build_class_symbol (gfc_typespec *ts, symbol_attribute 
*attr,
   char *name;
   gfc_typespec *orig_ts = ts;
   gfc_symbol *fclass;
-  gfc_symbol *vtab;
   gfc_component *c;
   gfc_namespace *ns;
   int rank;
@@ -783,9 +785,9 @@ gfc_build_class_symbol (gfc_typespec *ts, symbol_attribute 
*attr,
 
       if (ts->u.derived->attr.unlimited_polymorphic)
        {
-         vtab = gfc_find_derived_vtab (ts->u.derived);
-         gcc_assert (vtab);
-         c->ts.u.derived = vtab->ts.u.derived;
+         gfc_symbol * vtype = find_derived_vtype (ts->u.derived);
+         gcc_assert (vtype);
+         c->ts.u.derived = vtype;
 
          /* Add component '_len'.  Only unlimited polymorphic pointers may
              have a string assigned to them, i.e., only those need the _len
@@ -2492,20 +2494,13 @@ add_procs_to_declared_vtab (gfc_symbol *derived, 
gfc_symbol *vtype)
 }
 
 
-/* Find or generate the symbol for a derived type's vtab.  */
-
-gfc_symbol *
-gfc_find_derived_vtab (gfc_symbol *derived)
+static gfc_symbol *
+find_toplevel_symbol (gfc_symbol * derived, const char * name,
+                     gfc_namespace ** pns)
 {
-  gfc_namespace *ns;
-  gfc_symbol *vtab = NULL, *vtype = NULL, *found_sym = NULL, *def_init = NULL;
-  gfc_symbol *copy = NULL, *src = NULL, *dst = NULL;
-  gfc_gsymbol *gsym = NULL;
-  gfc_symbol *dealloc = NULL, *arg = NULL;
-
-  if (derived->attr.pdt_template
-      || derived->attr.unlimited_polymorphic)
-    return NULL;
+  gfc_namespace *ns = nullptr;
+  gfc_symbol *sym = nullptr;
+  gfc_gsymbol *gsym = nullptr;
 
   /* Find the top-level namespace, stopping at module/submodule boundaries.
      A submodule's namespace may have its parent pointer set to the ancestor
@@ -2517,16 +2512,6 @@ gfc_find_derived_vtab (gfc_symbol *derived)
        || (ns->proc_name && ns->proc_name->attr.flavor == FL_MODULE))
       break;
 
-  /* If the type is a class container, use the underlying derived type.  */
-  if (derived->attr.is_class)
-    derived = gfc_get_derived_super_type (derived);
-
-  if (!derived)
-    return NULL;
-
-  if (!derived->name)
-    return NULL;
-
   /* Find the gsymbol for the module of use associated derived types.  */
   if ((derived->attr.use_assoc || derived->attr.used_in_submodule)
        && !derived->attr.vtype && !derived->attr.is_class)
@@ -2542,311 +2527,443 @@ gfc_find_derived_vtab (gfc_symbol *derived)
       && ns->proc_name->attr.flavor == FL_MODULE)
     ns = gsym->ns;
 
-  if (ns)
+  if (!ns)
     {
-      char tname[GFC_MAX_SYMBOL_LEN+1];
-      char *name;
+      *pns = nullptr;
+      return nullptr;
+    }
 
-      get_unique_hashed_string (tname, derived);
-      name = xasprintf ("__vtab_%s", tname);
+  /* Look for the vtab symbol in various namespaces.  */
+  if (gsym && gsym->ns)
+    {
+      gfc_find_symbol (name, gsym->ns, 0, &sym);
+      if (sym != nullptr)
+       return sym;
+    }
 
-      /* Look for the vtab symbol in various namespaces.  */
-      if (gsym && gsym->ns)
-       {
-         gfc_find_symbol (name, gsym->ns, 0, &vtab);
-         if (vtab)
-           ns = gsym->ns;
-       }
-      if (vtab == NULL)
-       gfc_find_symbol (name, gfc_current_ns, 0, &vtab);
-      if (vtab == NULL)
-       gfc_find_symbol (name, ns, 0, &vtab);
-      if (vtab == NULL)
-       gfc_find_symbol (name, derived->ns, 0, &vtab);
-      /* If all else fails, look in the module/submodule namespace so that
-        the module variable and procedure body translations find the same
-        frontend symbol and backend decl.  */
-      if (vtab == NULL && module_ns != ns)
-       gfc_find_symbol (name, module_ns, 0, &vtab);
+  gfc_find_symbol (name, gfc_current_ns, 0, &sym);
+  if (sym != nullptr)
+    return sym;
 
-      /* Fix up a stale non-use-associated duplicate vtab.  */
-      if (vtab
-         && (derived->attr.use_assoc || derived->attr.used_in_submodule)
-         && !vtab->attr.use_assoc && !vtab->module)
-       {
-         vtab->attr.use_assoc = 1;
-         vtab->module = derived->module;
-       }
+  gfc_find_symbol (name, ns, 0, &sym);
+  if (sym != nullptr)
+    return sym;
 
-      if (vtab == NULL)
-       {
-         gfc_get_symbol (name, ns, &vtab);
-         vtab->ts.type = BT_DERIVED;
-         if (!gfc_add_flavor (&vtab->attr, FL_VARIABLE, NULL,
-                              &gfc_current_locus))
-           goto cleanup;
-         vtab->attr.target = 1;
-         vtab->attr.save = SAVE_IMPLICIT;
-         vtab->attr.vtab = 1;
-         vtab->attr.access = ACCESS_PUBLIC;
-         vtab->attr.artificial = 1;
-         gfc_set_sym_referenced (vtab);
-         free (name);
-         name = xasprintf ("__vtype_%s", tname);
+  gfc_find_symbol (name, derived->ns, 0, &sym);
+  if (sym != nullptr)
+    return sym;
 
-         gfc_find_symbol (name, ns, 0, &vtype);
-         if (vtype == NULL)
-           {
-             gfc_component *c;
-             gfc_symbol *parent = NULL, *parent_vtab = NULL;
+  if (module_ns != ns)
+    {
+      /* If all else fails, look in the module/submodule namespace so that the
+         module variable and procedure body translations find the same frontend
+        symbol and backend decl.  */
+      gfc_find_symbol (name, module_ns, 0, &sym);
+      if (sym != nullptr)
+       return sym;
+    }
 
-             gfc_get_symbol (name, ns, &vtype);
-             if (!gfc_add_flavor (&vtype->attr, FL_DERIVED, NULL,
-                                  &gfc_current_locus))
-               goto cleanup;
-             vtype->attr.access = ACCESS_PUBLIC;
-             vtype->attr.vtype = 1;
-             gfc_set_sym_referenced (vtype);
+  *pns = ns;
+  return nullptr;
+}
 
-             /* Add component '_hash'.  */
-             if (!gfc_add_component (vtype, "_hash", &c))
-               goto cleanup;
-             c->ts.type = BT_INTEGER;
-             c->ts.kind = 4;
-             c->attr.access = ACCESS_PRIVATE;
-             c->initializer = gfc_get_int_expr (gfc_default_integer_kind,
-                                                NULL, derived->hash_value);
 
-             /* Add component '_size'.  */
-             if (!gfc_add_component (vtype, "_size", &c))
-               goto cleanup;
-             c->ts.type = BT_INTEGER;
-             c->ts.kind = gfc_size_kind;
-             c->attr.access = ACCESS_PRIVATE;
-             /* Remember the derived type in ts.u.derived,
-                so that the correct initializer can be set later on
-                (in gfc_conv_structure).  */
-             c->ts.u.derived = derived;
-             c->initializer = gfc_get_int_expr (gfc_size_kind,
-                                                NULL, 0);
+static gfc_symbol *
+get_toplevel_vtype (gfc_symbol * derived)
+{
+  gfc_component *c;
+  gfc_symbol *vtype = nullptr;
+  gfc_symbol *parent = nullptr;
+  gfc_symbol *parent_vtab = nullptr;
+  gfc_symbol *def_init = nullptr;
+  gfc_symbol *copy = nullptr;
+  gfc_symbol *src = nullptr;
+  gfc_symbol *dst = nullptr;
+  gfc_symbol *dealloc = nullptr;
+  gfc_symbol *arg = nullptr;
+  gfc_namespace *ns = nullptr;
 
-             /* Add component _extends.  */
-             if (!gfc_add_component (vtype, "_extends", &c))
-               goto cleanup;
-             c->attr.pointer = 1;
-             c->attr.access = ACCESS_PRIVATE;
-             if (!derived->attr.unlimited_polymorphic)
-               parent = gfc_get_derived_super_type (derived);
-             else
-               parent = NULL;
+  char tname[GFC_MAX_SYMBOL_LEN+1];
+  char *name = nullptr;
 
-             if (parent)
-               {
-                 parent_vtab = gfc_find_derived_vtab (parent);
-                 c->ts.type = BT_DERIVED;
-                 c->ts.u.derived = parent_vtab->ts.u.derived;
-                 c->initializer = gfc_get_expr ();
-                 c->initializer->expr_type = EXPR_VARIABLE;
-                 gfc_find_sym_tree (parent_vtab->name, parent_vtab->ns,
-                                    0, &c->initializer->symtree);
-               }
-             else
-               {
-                 c->ts.type = BT_DERIVED;
-                 c->ts.u.derived = vtype;
-                 c->initializer = gfc_get_null_expr (NULL);
-               }
+  get_unique_hashed_string (tname, derived);
+  name = xasprintf ("__vtype_%s", tname);
+  vtype = find_toplevel_symbol (derived, name, &ns);
+  if (vtype || !ns)
+    {
+      free (name);
+      return vtype;
+    }
 
-             if (!derived->attr.unlimited_polymorphic
-                 && derived->components == NULL
-                 && !derived->attr.zero_comp)
-               {
-                 /* At this point an error must have occurred.
-                    Prevent further errors on the vtype components.  */
-                 found_sym = vtab;
-                 goto have_vtype;
-               }
+  gfc_get_symbol (name, ns, &vtype);
+  if (!gfc_add_flavor (&vtype->attr, FL_DERIVED, NULL,
+                      &gfc_current_locus))
+    goto cleanup;
+  vtype->attr.access = ACCESS_PUBLIC;
+  vtype->attr.vtype = 1;
+  gfc_set_sym_referenced (vtype);
+
+  /* Add component '_hash'.  */
+  if (!gfc_add_component (vtype, "_hash", &c))
+    goto cleanup;
+  c->ts.type = BT_INTEGER;
+  c->ts.kind = 4;
+  c->attr.access = ACCESS_PRIVATE;
+  c->initializer = gfc_get_int_expr (gfc_default_integer_kind,
+                                    NULL, derived->hash_value);
+
+  /* Add component '_size'.  */
+  if (!gfc_add_component (vtype, "_size", &c))
+    goto cleanup;
+  c->ts.type = BT_INTEGER;
+  c->ts.kind = gfc_size_kind;
+  c->attr.access = ACCESS_PRIVATE;
+  /* Remember the derived type in ts.u.derived,
+     so that the correct initializer can be set later on
+     (in gfc_conv_structure).  */
+  c->ts.u.derived = derived;
+  c->initializer = gfc_get_int_expr (gfc_size_kind,
+                                    NULL, 0);
+
+  /* Add component _extends.  */
+  if (!gfc_add_component (vtype, "_extends", &c))
+    goto cleanup;
+  c->attr.pointer = 1;
+  c->attr.access = ACCESS_PRIVATE;
+  if (!derived->attr.unlimited_polymorphic)
+    parent = gfc_get_derived_super_type (derived);
+  else
+    parent = NULL;
 
-             /* Add component _def_init.  */
-             if (!gfc_add_component (vtype, "_def_init", &c))
-               goto cleanup;
-             c->attr.pointer = 1;
-             c->attr.artificial = 1;
-             c->attr.access = ACCESS_PRIVATE;
-             c->ts.type = BT_DERIVED;
-             c->ts.u.derived = derived;
-             if (derived->attr.unlimited_polymorphic
-                 || derived->attr.abstract)
-               c->initializer = gfc_get_null_expr (NULL);
-             else
-               {
-                 /* Construct default initialization variable.  */
-                 free (name);
-                 name = xasprintf ("__def_init_%s", tname);
-                 gfc_get_symbol (name, ns, &def_init);
-                 def_init->attr.target = 1;
-                 def_init->attr.artificial = 1;
-                 def_init->attr.save = SAVE_IMPLICIT;
-                 def_init->attr.access = ACCESS_PUBLIC;
-                 def_init->attr.flavor = FL_VARIABLE;
-                 gfc_set_sym_referenced (def_init);
-                 def_init->ts.type = BT_DERIVED;
-                 def_init->ts.u.derived = derived;
-                 def_init->value = gfc_default_initializer (&def_init->ts);
-
-                 c->initializer = gfc_lval_expr_from_sym (def_init);
-               }
+  if (parent)
+    {
+      parent_vtab = gfc_find_derived_vtab (parent);
+      c->ts.type = BT_DERIVED;
+      c->ts.u.derived = parent_vtab->ts.u.derived;
+      c->initializer = gfc_get_expr ();
+      c->initializer->expr_type = EXPR_VARIABLE;
+      gfc_find_sym_tree (parent_vtab->name, parent_vtab->ns,
+                        0, &c->initializer->symtree);
+    }
+  else
+    {
+      c->ts.type = BT_DERIVED;
+      c->ts.u.derived = vtype;
+      c->initializer = gfc_get_null_expr (NULL);
+    }
 
-             /* Add component _copy.  */
-             if (!gfc_add_component (vtype, "_copy", &c))
-               goto cleanup;
-             c->attr.proc_pointer = 1;
-             c->attr.access = ACCESS_PRIVATE;
-             c->attr.artificial = 1;
-             c->tb = XCNEW (gfc_typebound_proc);
-             c->tb->ppc = 1;
-             if (derived->attr.unlimited_polymorphic
-                 || derived->attr.abstract)
-               c->initializer = gfc_get_null_expr (NULL);
-             else
-               {
-                 /* Set up namespace.  */
-                 gfc_namespace *sub_ns = gfc_get_namespace (ns, 0);
-                 sub_ns->sibling = ns->contained;
-                 ns->contained = sub_ns;
-                 sub_ns->resolved = 1;
-                 /* Set up procedure symbol.  */
-                 free (name);
-                 name = xasprintf ("__copy_%s", tname);
-                 gfc_get_symbol (name, sub_ns, &copy);
-                 sub_ns->proc_name = copy;
-                 copy->attr.flavor = FL_PROCEDURE;
-                 copy->attr.subroutine = 1;
-                 copy->attr.pure = 1;
-                 copy->attr.artificial = 1;
-                 copy->attr.if_source = IFSRC_DECL;
-                 /* This is elemental so that arrays are automatically
-                    treated correctly by the scalarizer.  */
-                 copy->attr.elemental = 1;
-                 if (ns->proc_name->attr.flavor == FL_MODULE)
-                   copy->module = ns->proc_name->name;
-                 gfc_set_sym_referenced (copy);
-                 /* Set up formal arguments.  */
-                 gfc_get_symbol ("src", sub_ns, &src);
-                 src->ts.type = BT_DERIVED;
-                 src->ts.u.derived = derived;
-                 src->attr.flavor = FL_VARIABLE;
-                 src->attr.dummy = 1;
-                 src->attr.artificial = 1;
-                 src->attr.intent = INTENT_IN;
-                 gfc_set_sym_referenced (src);
-                 copy->formal = gfc_get_formal_arglist ();
-                 copy->formal->sym = src;
-                 gfc_get_symbol ("dst", sub_ns, &dst);
-                 dst->ts.type = BT_DERIVED;
-                 dst->ts.u.derived = derived;
-                 dst->attr.flavor = FL_VARIABLE;
-                 dst->attr.dummy = 1;
-                 dst->attr.artificial = 1;
-                 dst->attr.intent = INTENT_INOUT;
-                 gfc_set_sym_referenced (dst);
-                 copy->formal->next = gfc_get_formal_arglist ();
-                 copy->formal->next->sym = dst;
-                 /* Set up code.  */
-                 sub_ns->code = gfc_get_code (EXEC_INIT_ASSIGN);
-                 sub_ns->code->expr1 = gfc_lval_expr_from_sym (dst);
-                 sub_ns->code->expr2 = gfc_lval_expr_from_sym (src);
-                 /* Set initializer.  */
-                 c->initializer = gfc_lval_expr_from_sym (copy);
-                 c->ts.interface = copy;
-               }
+  if (!derived->attr.unlimited_polymorphic
+      && derived->components == NULL
+      && !derived->attr.zero_comp)
+    {
+      /* At this point an error must have occurred.
+        Prevent further errors on the vtype components.  */
+      goto cleanup;
+    }
 
-             /* Add component _final, which contains a procedure pointer to
-                a wrapper which handles both the freeing of allocatable
-                components and the calls to finalization subroutines.
-                Note: The actual wrapper function can only be generated
-                at resolution time.  */
-             if (!gfc_add_component (vtype, "_final", &c))
-               goto cleanup;
-             c->attr.proc_pointer = 1;
-             c->attr.access = ACCESS_PRIVATE;
-             c->attr.artificial = 1;
-             c->tb = XCNEW (gfc_typebound_proc);
-             c->tb->ppc = 1;
-             generate_finalization_wrapper (derived, ns, tname, c);
+  /* Add component _def_init.  */
+  if (!gfc_add_component (vtype, "_def_init", &c))
+    goto cleanup;
+  c->attr.pointer = 1;
+  c->attr.artificial = 1;
+  c->attr.access = ACCESS_PRIVATE;
+  c->ts.type = BT_DERIVED;
+  c->ts.u.derived = derived;
+  if (derived->attr.unlimited_polymorphic
+      || derived->attr.abstract)
+    c->initializer = gfc_get_null_expr (NULL);
+  else
+    {
+      /* Construct default initialization variable.  */
+      char * xname = xasprintf ("__def_init_%s", tname);
+      gfc_get_symbol (xname, ns, &def_init);
+      free (xname);
+      def_init->attr.target = 1;
+      def_init->attr.artificial = 1;
+      def_init->attr.save = SAVE_IMPLICIT;
+      def_init->attr.access = ACCESS_PUBLIC;
+      def_init->attr.flavor = FL_VARIABLE;
+      gfc_set_sym_referenced (def_init);
+      def_init->ts.type = BT_DERIVED;
+      def_init->ts.u.derived = derived;
+      def_init->value = gfc_default_initializer (&def_init->ts);
+
+      c->initializer = gfc_lval_expr_from_sym (def_init);
+    }
 
-             /* Add component _deallocate.  */
-             if (!gfc_add_component (vtype, "_deallocate", &c))
-               goto cleanup;
-             c->attr.proc_pointer = 1;
-             c->attr.access = ACCESS_PRIVATE;
-             c->attr.artificial = 1;
-             c->tb = XCNEW (gfc_typebound_proc);
-             c->tb->ppc = 1;
-             if (derived->attr.unlimited_polymorphic || derived->attr.abstract
-                 || !derived->attr.recursive)
-               c->initializer = gfc_get_null_expr (NULL);
-             else
-               {
-                 /* Set up namespace.  */
-                 gfc_namespace *sub_ns = gfc_get_namespace (ns, 0);
-
-                 sub_ns->sibling = ns->contained;
-                 ns->contained = sub_ns;
-                 sub_ns->resolved = 1;
-                 /* Set up procedure symbol.  */
-                 free (name);
-                 name = xasprintf ("__deallocate_%s", tname);
-                 gfc_get_symbol (name, sub_ns, &dealloc);
-                 sub_ns->proc_name = dealloc;
-                 dealloc->attr.flavor = FL_PROCEDURE;
-                 dealloc->attr.subroutine = 1;
-                 dealloc->attr.pure = 1;
-                 dealloc->attr.artificial = 1;
-                 dealloc->attr.if_source = IFSRC_DECL;
-
-                 if (ns->proc_name->attr.flavor == FL_MODULE)
-                   dealloc->module = ns->proc_name->name;
-                 gfc_set_sym_referenced (dealloc);
-                 /* Set up formal argument.  */
-                 gfc_get_symbol ("arg", sub_ns, &arg);
-                 arg->ts.type = BT_DERIVED;
-                 arg->ts.u.derived = derived;
-                 arg->attr.flavor = FL_VARIABLE;
-                 arg->attr.dummy = 1;
-                 arg->attr.artificial = 1;
-                 arg->attr.intent = INTENT_INOUT;
-                 arg->attr.dimension = 1;
-                 arg->attr.allocatable = 1;
-                 arg->as = gfc_get_array_spec();
-                 arg->as->type = AS_ASSUMED_SHAPE;
-                 arg->as->rank = 1;
-                 arg->as->lower[0] = gfc_get_int_expr 
(gfc_default_integer_kind,
-                                                       NULL, 1);
-                 gfc_set_sym_referenced (arg);
-                 dealloc->formal = gfc_get_formal_arglist ();
-                 dealloc->formal->sym = arg;
-                 /* Set up code.  */
-                 sub_ns->code = gfc_get_code (EXEC_DEALLOCATE);
-                 sub_ns->code->ext.alloc.list = gfc_get_alloc ();
-                 sub_ns->code->ext.alloc.list->expr
-                               = gfc_lval_expr_from_sym (arg);
-                 /* Set initializer.  */
-                 c->initializer = gfc_lval_expr_from_sym (dealloc);
-                 c->ts.interface = dealloc;
-               }
+  /* Add component _copy.  */
+  if (!gfc_add_component (vtype, "_copy", &c))
+    goto cleanup;
+  c->attr.proc_pointer = 1;
+  c->attr.access = ACCESS_PRIVATE;
+  c->attr.artificial = 1;
+  c->tb = XCNEW (gfc_typebound_proc);
+  c->tb->ppc = 1;
+  if (derived->attr.unlimited_polymorphic
+      || derived->attr.abstract)
+    c->initializer = gfc_get_null_expr (NULL);
+  else
+    {
+      /* Set up namespace.  */
+      gfc_namespace *sub_ns = gfc_get_namespace (ns, 0);
+      sub_ns->sibling = ns->contained;
+      ns->contained = sub_ns;
+      sub_ns->resolved = 1;
+      /* Set up procedure symbol.  */
+      char * xname = xasprintf ("__copy_%s", tname);
+      gfc_get_symbol (xname, sub_ns, &copy);
+      free (xname);
+      sub_ns->proc_name = copy;
+      copy->attr.flavor = FL_PROCEDURE;
+      copy->attr.subroutine = 1;
+      copy->attr.pure = 1;
+      copy->attr.artificial = 1;
+      copy->attr.if_source = IFSRC_DECL;
+      /* This is elemental so that arrays are automatically
+        treated correctly by the scalarizer.  */
+      copy->attr.elemental = 1;
+      if (ns->proc_name->attr.flavor == FL_MODULE)
+       copy->module = ns->proc_name->name;
+      gfc_set_sym_referenced (copy);
+      /* Set up formal arguments.  */
+      gfc_get_symbol ("src", sub_ns, &src);
+      src->ts.type = BT_DERIVED;
+      src->ts.u.derived = derived;
+      src->attr.flavor = FL_VARIABLE;
+      src->attr.dummy = 1;
+      src->attr.artificial = 1;
+      src->attr.intent = INTENT_IN;
+      gfc_set_sym_referenced (src);
+      copy->formal = gfc_get_formal_arglist ();
+      copy->formal->sym = src;
+      gfc_get_symbol ("dst", sub_ns, &dst);
+      dst->ts.type = BT_DERIVED;
+      dst->ts.u.derived = derived;
+      dst->attr.flavor = FL_VARIABLE;
+      dst->attr.dummy = 1;
+      dst->attr.artificial = 1;
+      dst->attr.intent = INTENT_INOUT;
+      gfc_set_sym_referenced (dst);
+      copy->formal->next = gfc_get_formal_arglist ();
+      copy->formal->next->sym = dst;
+      /* Set up code.  */
+      sub_ns->code = gfc_get_code (EXEC_INIT_ASSIGN);
+      sub_ns->code->expr1 = gfc_lval_expr_from_sym (dst);
+      sub_ns->code->expr2 = gfc_lval_expr_from_sym (src);
+      /* Set initializer.  */
+      c->initializer = gfc_lval_expr_from_sym (copy);
+      c->ts.interface = copy;
+    }
 
-             /* Add procedure pointers for type-bound procedures.  */
-             if (!derived->attr.unlimited_polymorphic)
-               add_procs_to_declared_vtab (derived, vtype);
-         }
+  /* Add component _final, which contains a procedure pointer to
+     a wrapper which handles both the freeing of allocatable
+     components and the calls to finalization subroutines.
+     Note: The actual wrapper function can only be generated
+     at resolution time.  */
+  if (!gfc_add_component (vtype, "_final", &c))
+    goto cleanup;
+  c->attr.proc_pointer = 1;
+  c->attr.access = ACCESS_PRIVATE;
+  c->attr.artificial = 1;
+  c->tb = XCNEW (gfc_typebound_proc);
+  c->tb->ppc = 1;
+  generate_finalization_wrapper (derived, ns, name, c);
+
+  /* Add component _deallocate.  */
+  if (!gfc_add_component (vtype, "_deallocate", &c))
+    goto cleanup;
+  c->attr.proc_pointer = 1;
+  c->attr.access = ACCESS_PRIVATE;
+  c->attr.artificial = 1;
+  c->tb = XCNEW (gfc_typebound_proc);
+  c->tb->ppc = 1;
+  if (derived->attr.unlimited_polymorphic || derived->attr.abstract
+      || !derived->attr.recursive)
+    c->initializer = gfc_get_null_expr (NULL);
+  else
+    {
+      /* Set up namespace.  */
+      gfc_namespace *sub_ns = gfc_get_namespace (ns, 0);
+
+      sub_ns->sibling = ns->contained;
+      ns->contained = sub_ns;
+      sub_ns->resolved = 1;
+      /* Set up procedure symbol.  */
+      char * tname = xasprintf ("__deallocate_%s", name);
+      gfc_get_symbol (tname, sub_ns, &dealloc);
+      free (tname);
+      sub_ns->proc_name = dealloc;
+      dealloc->attr.flavor = FL_PROCEDURE;
+      dealloc->attr.subroutine = 1;
+      dealloc->attr.pure = 1;
+      dealloc->attr.artificial = 1;
+      dealloc->attr.if_source = IFSRC_DECL;
+
+      if (ns->proc_name->attr.flavor == FL_MODULE)
+       dealloc->module = ns->proc_name->name;
+      gfc_set_sym_referenced (dealloc);
+      /* Set up formal argument.  */
+      gfc_get_symbol ("arg", sub_ns, &arg);
+      arg->ts.type = BT_DERIVED;
+      arg->ts.u.derived = derived;
+      arg->attr.flavor = FL_VARIABLE;
+      arg->attr.dummy = 1;
+      arg->attr.artificial = 1;
+      arg->attr.intent = INTENT_INOUT;
+      arg->attr.dimension = 1;
+      arg->attr.allocatable = 1;
+      arg->as = gfc_get_array_spec();
+      arg->as->type = AS_ASSUMED_SHAPE;
+      arg->as->rank = 1;
+      arg->as->lower[0] = gfc_get_int_expr (gfc_default_integer_kind,
+                                           NULL, 1);
+      gfc_set_sym_referenced (arg);
+      dealloc->formal = gfc_get_formal_arglist ();
+      dealloc->formal->sym = arg;
+      /* Set up code.  */
+      sub_ns->code = gfc_get_code (EXEC_DEALLOCATE);
+      sub_ns->code->ext.alloc.list = gfc_get_alloc ();
+      sub_ns->code->ext.alloc.list->expr
+                   = gfc_lval_expr_from_sym (arg);
+      /* Set initializer.  */
+      c->initializer = gfc_lval_expr_from_sym (dealloc);
+      c->ts.interface = dealloc;
+    }
 
-have_vtype:
-         vtab->ts.u.derived = vtype;
-         vtab->value = gfc_default_initializer (&vtab->ts);
-       }
-      free (name);
+  /* Add procedure pointers for type-bound procedures.  */
+  if (!derived->attr.unlimited_polymorphic)
+    add_procs_to_declared_vtab (derived, vtype);
+
+cleanup:
+  /* It is unexpected to have some symbols added at resolution or code
+     generation time. We commit the changes in order to keep a clean state.  */
+  if (vtype)
+    gfc_commit_symbol (vtype);
+  if (def_init)
+    gfc_commit_symbol (def_init);
+  if (copy)
+    gfc_commit_symbol (copy);
+  if (src)
+    gfc_commit_symbol (src);
+  if (dst)
+    gfc_commit_symbol (dst);
+  if (dealloc)
+    gfc_commit_symbol (dealloc);
+  if (arg)
+    gfc_commit_symbol (arg);
+
+  free (name);
+  return vtype;
+}
+
+
+static gfc_symbol *
+find_derived_vtype (gfc_symbol *derived)
+{
+  gfc_symbol *vtype = NULL, *found_sym = NULL, *def_init = NULL;
+  gfc_symbol *copy = NULL, *src = NULL, *dst = NULL;
+  gfc_symbol *dealloc = NULL, *arg = NULL;
+
+  if (derived->attr.pdt_template)
+    return NULL;
+
+  /* If the type is a class container, use the underlying derived type.  */
+  if (derived->attr.is_class)
+    derived = gfc_get_derived_super_type (derived);
+
+  if (!(derived && derived->name))
+    return NULL;
+
+  vtype = get_toplevel_vtype (derived);
+  found_sym = vtype;
+
+  /* It is unexpected to have some symbols added at resolution or code
+     generation time. We commit the changes in order to keep a clean state.  */
+  if (found_sym)
+    {
+      if (vtype)
+       gfc_commit_symbol (vtype);
+      if (def_init)
+       gfc_commit_symbol (def_init);
+      if (copy)
+       gfc_commit_symbol (copy);
+      if (src)
+       gfc_commit_symbol (src);
+      if (dst)
+       gfc_commit_symbol (dst);
+      if (dealloc)
+       gfc_commit_symbol (dealloc);
+      if (arg)
+       gfc_commit_symbol (arg);
     }
+  else
+    gfc_undo_symbols ();
+
+  return found_sym;
+}
+
+
+/* Find or generate the symbol for a derived type's vtab.  */
+
+gfc_symbol *
+gfc_find_derived_vtab (gfc_symbol *derived)
+{
+  gfc_namespace *ns;
+  gfc_symbol *vtab = NULL, *vtype = NULL, *found_sym = NULL, *def_init = NULL;
+  gfc_symbol *copy = NULL, *src = NULL, *dst = NULL;
+  gfc_symbol *dealloc = NULL, *arg = NULL;
+
+  if (derived->attr.pdt_template
+      || derived->attr.unlimited_polymorphic)
+    return NULL;
+
+  /* If the type is a class container, use the underlying derived type.  */
+  if (derived->attr.is_class)
+    derived = gfc_get_derived_super_type (derived);
+
+  if (!(derived && derived->name))
+    return NULL;
+
+  {
+    char tname[GFC_MAX_SYMBOL_LEN+1];
+    char *name;
+
+    get_unique_hashed_string (tname, derived);
+    name = xasprintf ("__vtab_%s", tname);
+    vtab = find_toplevel_symbol (derived, name, &ns);
+
+    /* Fix up a stale non-use-associated duplicate vtab.  */
+    if (vtab
+       && (derived->attr.use_assoc || derived->attr.used_in_submodule)
+       && !vtab->attr.use_assoc && !vtab->module)
+      {
+       vtab->attr.use_assoc = 1;
+       vtab->module = derived->module;
+      }
+
+    if (!vtab && ns)
+      {
+       gfc_get_symbol (name, ns, &vtab);
+       free (name);
+       vtab->ts.type = BT_DERIVED;
+       if (!gfc_add_flavor (&vtab->attr, FL_VARIABLE, NULL,
+                            &gfc_current_locus))
+         goto cleanup;
+       vtab->attr.target = 1;
+       vtab->attr.save = SAVE_IMPLICIT;
+       vtab->attr.vtab = 1;
+       vtab->attr.access = ACCESS_PUBLIC;
+       vtab->attr.artificial = 1;
+       gfc_set_sym_referenced (vtab);
+
+       vtype = get_toplevel_vtype (derived);
+
+       vtab->ts.u.derived = vtype;
+       vtab->value = gfc_default_initializer (&vtab->ts);
+      }
+    else
+      free (name);
+  }
 
   found_sym = vtab;

Reply via email to