#include "gfortran.h"
#include "intrinsic.h"
#include "constructor.h"
+#include "arith.h"
/* Given printf-like arguments, return a stable version of the result string.
void
gfc_resolve_repeat (gfc_expr *f, gfc_expr *string,
- gfc_expr *ncopies ATTRIBUTE_UNUSED)
+ gfc_expr *ncopies)
{
+ int len;
+ gfc_expr *tmp;
f->ts.type = BT_CHARACTER;
f->ts.kind = string->ts.kind;
f->value.function.name = gfc_get_string ("__repeat_%d", string->ts.kind);
+
+ /* If possible, generate a character length. */
+ if (f->ts.u.cl == NULL)
+ f->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
+
+ tmp = NULL;
+ if (string->expr_type == EXPR_CONSTANT)
+ {
+ len = string->value.character.length;
+ tmp = gfc_get_int_expr (gfc_default_integer_kind, NULL , len);
+ }
+ else if (string->ts.u.cl && string->ts.u.cl->length)
+ {
+ tmp = gfc_copy_expr (string->ts.u.cl->length);
+ }
+
+ if (tmp)
+ f->ts.u.cl->length = gfc_multiply (tmp, gfc_copy_expr (ncopies));
}
gfc_conv_expr (&se_sz, code->expr3);
memsz = se_sz.string_length;
}
- else
+ else if (code->expr3->ts.u.cl
+ && code->expr3->ts.u.cl->length)
+ {
+ gfc_conv_expr (&se_sz, code->expr3->ts.u.cl->length);
+ gfc_add_block_to_block (&se.pre, &se_sz.pre);
+ se_sz.expr = gfc_evaluate_now (se_sz.expr, &se.pre);
+ gfc_add_block_to_block (&se.pre, &se_sz.post);
+ memsz = se_sz.expr;
+ }
+ else if (code->ext.alloc.ts.u.cl
+ && code->ext.alloc.ts.u.cl->length)
{
gfc_conv_expr (&se_sz, code->ext.alloc.ts.u.cl->length);
memsz = se_sz.expr;
}
- if (TREE_CODE (se.string_length) == VAR_DECL)
- gfc_add_modify (&block, se.string_length,
- fold_convert (TREE_TYPE (se.string_length),
- memsz));
+ else
+ {
+ /* This is likely to be inefficient. */
+ gfc_conv_expr (&se_sz, code->expr3);
+ gfc_add_block_to_block (&se.pre, &se_sz.pre);
+ se_sz.expr = gfc_evaluate_now (se_sz.expr, &se.pre);
+ gfc_add_block_to_block (&se.pre, &se_sz.post);
+ memsz = se_sz.string_length;
+ }
}
else
/* Otherwise use the stored string length. */
/* Store the string length. */
if (tmp && TREE_CODE (tmp) == VAR_DECL)
- gfc_add_modify (&block, tmp, fold_convert (TREE_TYPE (tmp),
+ gfc_add_modify (&se.pre, tmp, fold_convert (TREE_TYPE (tmp),
memsz));
/* Convert to size in bytes, using the character KIND. */
if (expr->ts.type == BT_CHARACTER && memsz == NULL_TREE)
{
- if (expr->ts.deferred)
- {
- gfc_se se_sz;
- gfc_init_se (&se_sz, NULL);
- gfc_conv_expr (&se_sz, code->ext.alloc.ts.u.cl->length);
- memsz = se_sz.expr;
- gfc_add_modify (&block, se.string_length,
- fold_convert (TREE_TYPE (se.string_length),
- memsz));
- }
- else
- memsz = se.string_length;
+ memsz = se.string_length;
+
/* Convert to size in bytes, using the character KIND. */
tmp = TREE_TYPE (gfc_typenode_for_spec (&code->ext.alloc.ts));
tmp = TYPE_SIZE_UNIT (tmp);
tmp = gfc_finish_block (&call.pre);
}
else
- tmp = gfc_trans_assignment (gfc_expr_to_initialize (expr),
- rhs, false, false);
+ {
+ /* Switch off automatic reallocation since we have just done
+ the ALLOCATE. */
+ int realloc_lhs = gfc_option.flag_realloc_lhs;
+ gfc_option.flag_realloc_lhs = 0;
+ tmp = gfc_trans_assignment (gfc_expr_to_initialize (expr),
+ rhs, false, false);
+ gfc_option.flag_realloc_lhs = realloc_lhs;
+ }
gfc_free_expr (rhs);
gfc_add_expr_to_block (&block, tmp);
}
--- /dev/null
+! { dg-do run }
+! Test the fix for PR47519, in which the character length was not
+! calculated for the SOURCE expressions below and an ICE resulted.
+!
+! Contributed by Tobias Burnus <burnus@gcc.gnu.org>
+!
+program note7_35
+ implicit none
+ character(:), allocatable :: name
+ character(:), allocatable :: src
+ integer n
+ n = 10
+ allocate(name, SOURCE=repeat('x',n))
+ if (name .ne. 'xxxxxxxxxx') call abort
+ if (len (name) .ne. 10 ) call abort
+ deallocate(name)
+ src = 'xyxy'
+ allocate(name, SOURCE=repeat(src,n))
+ if (name(37:40) .ne. 'xyxy') call abort
+ if (len (name) .ne. 40 ) call abort
+end program note7_35