------- Comment #14 from janus at gcc dot gnu dot org 2010-01-29 19:01 ------- Ok, I think the best solution is to move the code setting up the initializer back to resolve.c. Here is a patch which does so (without any testsuite regressions):
Index: gcc/fortran/trans-stmt.c =================================================================== --- gcc/fortran/trans-stmt.c (revision 156357) +++ gcc/fortran/trans-stmt.c (working copy) @@ -4176,14 +4176,6 @@ gfc_trans_allocate (gfc_code * code) tmp = gfc_build_memcpy_call (dst.expr, src.expr, memsz); gfc_add_expr_to_block (&block, tmp); } - /* Add default initializer for those derived types that need them. */ - else if (expr->ts.type == BT_DERIVED - && (init_e = gfc_default_initializer (&expr->ts))) - { - tmp = gfc_trans_assignment (gfc_expr_to_initialize (expr), - init_e, true); - gfc_add_expr_to_block (&block, tmp); - } /* Allocation of CLASS entities. */ gfc_free_expr (expr); Index: gcc/fortran/resolve.c =================================================================== --- gcc/fortran/resolve.c (revision 156357) +++ gcc/fortran/resolve.c (working copy) @@ -6099,6 +6099,7 @@ resolve_allocate_expr (gfc_expr *e, gfc_code *code gfc_symbol *sym; gfc_alloc *a; gfc_component *c; + gfc_expr *init_e; /* Check INTENT(IN), unless the object is a sub-component of a pointer. */ check_intent_in = 1; @@ -6223,6 +6224,34 @@ resolve_allocate_expr (gfc_expr *e, gfc_code *code sym->name, &e->where); return FAILURE; } + + if (!code->expr3) + { + /* Add default initializer for those derived types that need them. */ + if (e->ts.type == BT_DERIVED + && (init_e = gfc_default_initializer (&e->ts))) + { + gfc_code *init_st = gfc_get_code (); + init_st->loc = code->loc; + init_st->op = EXEC_INIT_ASSIGN; + init_st->expr1 = gfc_expr_to_initialize (e); + init_st->expr2 = init_e; + init_st->next = code->next; + code->next = init_st; + } + else if (e->ts.type == BT_CLASS && code->ext.alloc.ts.type == BT_UNKNOWN + && (init_e = gfc_default_initializer (&e->ts.u.derived->components->ts))) + { + gfc_code *init_st = gfc_get_code (); + init_st->loc = code->loc; + init_st->op = EXEC_INIT_ASSIGN; + init_st->expr1 = gfc_expr_to_initialize (e); + gfc_add_component_ref (init_st->expr1, "$data"); + init_st->expr2 = init_e; + init_st->next = code->next; + code->next = init_st; + } + } if (pointer || dimension == 0) return SUCCESS; Note: There is another call to 'gfc_default_initializer' in 'gfc_trans_allocate', which should be moved to resolve.c too, I guess. -- http://gcc.gnu.org/bugzilla/show_bug.cgi?id=42888