+2018-06-25 Fritz Reese <fritzoreese@gmail.com>
+
+ PR fortran/82972
+ PR fortran/83088
+ PR fortran/85851
+ * expr.c (component_initializer): Assign init expr to c->initializer.
+ (generate_isocbinding_initializer): New.
+ (gfc_generate_initializer): Call generate_isocbinding_initializer to
+ generate initializers for c_ptr and c_funptr with -finit-derived.
+
2018-06-23 Jerry DeLisle <jvdelisle@gcc.gnu.org>
PR fortran/85983
gfc_apply_init (&c->ts, &c->attr, init);
}
- return init;
+ return (c->initializer = init);
}
return gfc_generate_initializer (ts, false);
}
+/* Generate an initializer expression for an iso_c_binding type
+ such as c_[fun]ptr. The appropriate initializer is c_null_[fun]ptr. */
+
+static gfc_expr *
+generate_isocbinding_initializer (gfc_symbol *derived)
+{
+ /* The initializers have already been built into the c_null_[fun]ptr symbols
+ from gen_special_c_interop_ptr. */
+ gfc_symtree *npsym = NULL;
+ if (0 == strcmp (derived->name, "c_ptr"))
+ gfc_find_sym_tree ("c_null_ptr", gfc_current_ns, true, &npsym);
+ else if (0 == strcmp (derived->name, "c_funptr"))
+ gfc_find_sym_tree ("c_null_funptr", gfc_current_ns, true, &npsym);
+ else
+ gfc_internal_error ("generate_isocbinding_initializer(): bad iso_c_binding"
+ " type, expected %<c_ptr%> or %<c_funptr%>");
+ if (npsym)
+ {
+ gfc_expr *init = gfc_copy_expr (npsym->n.sym->value);
+ init->symtree = npsym;
+ init->ts.is_iso_c = true;
+ return init;
+ }
+
+ return NULL;
+}
/* Get or generate an expression for a default initializer of a derived type.
If -finit-derived is specified, generate default initialization expressions
{
gfc_expr *init, *tmp;
gfc_component *comp;
+
generate = flag_init_derived && generate;
+ if (ts->u.derived->ts.is_iso_c && generate)
+ return generate_isocbinding_initializer (ts->u.derived);
+
/* See if we have a default initializer in this, but not in nested
types (otherwise we could use gfc_has_default_initializer()).
We don't need to check if we are going to generate them. */
--- /dev/null
+! { dg-do compile }
+! { dg-options "-finit-derived -finit-local-zero -fdump-tree-original" }
+!
+! PR fortran/82972
+!
+! Make sure we do not ICE when generating initializers for c_ptr and c_funptr
+! components of derived types (and make sure they are properly initialized to
+! zero).
+!
+
+program init_flag_17
+ use iso_c_binding
+ implicit none
+
+ type :: ty
+ type(c_ptr) :: ptr ! = c_null_ptr
+ type(c_funptr) :: fptr ! = c_null_funptr
+ end type
+
+ type(ty) :: t
+
+ print *, t%ptr
+ print *, t%fptr
+
+end program
+
+! { dg-final { scan-tree-dump "\.ptr=0" "original" } }
+! { dg-final { scan-tree-dump "\.fptr=0" "original" } }