1 /* Supporting functions for resolving DATA statement.
2 Copyright (C) 2002, 2003, 2004, 2005, 2006 Free Software
4 Contributed by Lifang Zeng <zlf605@hotmail.com>
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 2, or (at your option) any later
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING. If not, write to the Free
20 Software Foundation, 51 Franklin Street, Fifth Floor,Boston, MA
24 /* Notes for DATA statement implementation:
26 We first assign initial value to each symbol by gfc_assign_data_value
27 during resolveing DATA statement. Refer to check_data_variable and
28 traverse_data_list in resolve.c.
30 The complexity exists in the handling of array section, implied do
31 and array of struct appeared in DATA statement.
33 We call gfc_conv_structure, gfc_con_array_array_initializer,
34 etc., to convert the initial value. Refer to trans-expr.c and
40 static void formalize_init_expr (gfc_expr *);
42 /* Calculate the array element offset. */
45 get_array_index (gfc_array_ref * ar, mpz_t * offset)
54 mpz_set_si (*offset, 0);
55 mpz_init_set_si (delta, 1);
56 for (i = 0; i < ar->dimen; i++)
58 e = gfc_copy_expr (ar->start[i]);
59 re = gfc_simplify_expr (e, 1);
61 if ((gfc_is_constant_expr (ar->as->lower[i]) == 0)
62 || (gfc_is_constant_expr (ar->as->upper[i]) == 0)
63 || (gfc_is_constant_expr (e) == 0))
64 gfc_error ("non-constant array in DATA statement %L.", &ar->where);
65 mpz_set (tmp, e->value.integer);
66 mpz_sub (tmp, tmp, ar->as->lower[i]->value.integer);
67 mpz_mul (tmp, tmp, delta);
68 mpz_add (*offset, tmp, *offset);
70 mpz_sub (tmp, ar->as->upper[i]->value.integer,
71 ar->as->lower[i]->value.integer);
72 mpz_add_ui (tmp, tmp, 1);
73 mpz_mul (delta, tmp, delta);
80 /* Find if there is a constructor which offset is equal to OFFSET. */
82 static gfc_constructor *
83 find_con_by_offset (mpz_t offset, gfc_constructor *con)
86 gfc_constructor *ret = NULL;
90 for (; con; con = con->next)
92 int cmp = mpz_cmp (offset, con->n.offset);
94 /* We retain a sorted list, so if we're too large, we're done. */
98 /* Yaye for exact matches. */
105 /* If the constructor element is a range, match any element. */
106 if (mpz_cmp_ui (con->repeat, 1) > 0)
108 mpz_add (tmp, con->n.offset, con->repeat);
109 if (mpz_cmp (offset, tmp) < 0)
122 /* Find if there is a constructor which component is equal to COM. */
124 static gfc_constructor *
125 find_con_by_component (gfc_component *com, gfc_constructor *con)
127 for (; con; con = con->next)
129 if (com == con->n.component)
136 /* Create a character type initialization expression from RVALUE.
137 TS [and REF] describe [the substring of] the variable being initialized.
138 INIT is thh existing initializer, not NULL. Initialization is performed
139 according to normal assignment rules. */
142 create_character_intializer (gfc_expr * init, gfc_typespec * ts,
143 gfc_ref * ref, gfc_expr * rvalue)
150 gfc_extract_int (ts->cl->length, &len);
154 /* Create a new initializer. */
155 init = gfc_get_expr ();
156 init->expr_type = EXPR_CONSTANT;
159 dest = gfc_getmem (len + 1);
161 init->value.character.length = len;
162 init->value.character.string = dest;
163 /* Blank the string if we're only setting a substring. */
165 memset (dest, ' ', len);
168 dest = init->value.character.string;
172 gfc_expr *start_expr, *end_expr;
174 gcc_assert (ref->type == REF_SUBSTRING);
176 /* Only set a substring of the destination. Fortran substring bounds
177 are one-based [start, end], we want zero based [start, end). */
178 start_expr = gfc_copy_expr (ref->u.ss.start);
179 end_expr = gfc_copy_expr (ref->u.ss.end);
181 if ((gfc_simplify_expr (start_expr, 1) == FAILURE)
182 || (gfc_simplify_expr (end_expr, 1)) == FAILURE)
184 gfc_error ("failure to simplify substring reference in DATA"
185 "statement at %L", &ref->u.ss.start->where);
189 gfc_extract_int (start_expr, &start);
191 gfc_extract_int (end_expr, &end);
195 /* Set the whole string. */
200 /* Copy the initial value. */
201 len = rvalue->value.character.length;
202 if (len > end - start)
205 gfc_warning_now ("initialization string truncated to match variable "
206 "at %L", &rvalue->where);
209 memcpy (&dest[start], rvalue->value.character.string, len);
211 /* Pad with spaces. Substrings will already be blanked. */
212 if (len < end - start && ref == NULL)
213 memset (&dest[start + len], ' ', end - (start + len));
215 if (rvalue->ts.type == BT_HOLLERITH)
221 /* Assign the initial value RVALUE to LVALUE's symbol->value. If the
222 LVALUE already has an initialization, we extend this, otherwise we
226 gfc_assign_data_value (gfc_expr * lvalue, gfc_expr * rvalue, mpz_t index)
231 gfc_constructor *con;
232 gfc_constructor *last_con;
234 gfc_typespec *last_ts;
237 symbol = lvalue->symtree->n.sym;
238 init = symbol->value;
239 last_ts = &symbol->ts;
241 mpz_init_set_si (offset, 0);
243 /* Find/create the parent expressions for subobject references. */
244 for (ref = lvalue->ref; ref; ref = ref->next)
246 /* Break out of the loop if we find a substring. */
247 if (ref->type == REF_SUBSTRING)
249 /* A substring should always be the last subobject reference. */
250 gcc_assert (ref->next == NULL);
254 /* Use the existing initializer expression if it exists. Otherwise
257 expr = gfc_get_expr ();
261 /* Find or create this element. */
267 /* The element typespec will be the same as the array
270 /* Setup the expression to hold the constructor. */
271 expr->expr_type = EXPR_ARRAY;
272 expr->rank = ref->u.ar.as->rank;
275 gcc_assert (expr->expr_type == EXPR_ARRAY);
277 if (ref->u.ar.type == AR_ELEMENT)
278 get_array_index (&ref->u.ar, &offset);
280 mpz_set (offset, index);
282 /* Find the same element in the existing constructor. */
283 con = expr->value.constructor;
284 con = find_con_by_offset (offset, con);
288 /* Create a new constructor. */
289 con = gfc_get_constructor ();
290 mpz_set (con->n.offset, offset);
291 gfc_insert_constructor (expr, con);
298 /* Setup the expression to hold the constructor. */
299 expr->expr_type = EXPR_STRUCTURE;
300 expr->ts.type = BT_DERIVED;
301 expr->ts.derived = ref->u.c.sym;
304 gcc_assert (expr->expr_type == EXPR_STRUCTURE);
305 last_ts = &ref->u.c.component->ts;
307 /* Find the same element in the existing constructor. */
308 con = expr->value.constructor;
309 con = find_con_by_component (ref->u.c.component, con);
313 /* Create a new constructor. */
314 con = gfc_get_constructor ();
315 con->n.component = ref->u.c.component;
316 con->next = expr->value.constructor;
317 expr->value.constructor = con;
327 /* Point the container at the new expression. */
328 if (last_con == NULL)
329 symbol->value = expr;
331 last_con->expr = expr;
337 if (ref || last_ts->type == BT_CHARACTER)
338 expr = create_character_intializer (init, last_ts, ref, rvalue);
341 /* Overwriting an existing initializer is non-standard but usually only
342 provokes a warning from other compilers. */
345 /* Order in which the expressions arrive here depends on whether they
346 are from data statements or F95 style declarations. Therefore,
347 check which is the most recent. */
348 #ifdef USE_MAPPED_LOCATION
349 expr = (LOCATION_LINE (init->where.lb->location)
350 > LOCATION_LINE (rvalue->where.lb->location))
353 expr = (init->where.lb->linenum > rvalue->where.lb->linenum) ?
356 gfc_notify_std (GFC_STD_GNU, "Extension: re-initialization "
357 "of '%s' at %L", symbol->name, &expr->where);
360 expr = gfc_copy_expr (rvalue);
361 if (!gfc_compare_types (&lvalue->ts, &expr->ts))
362 gfc_convert_type (expr, &lvalue->ts, 0);
365 if (last_con == NULL)
366 symbol->value = expr;
368 last_con->expr = expr;
371 /* Similarly, but initialize REPEAT consecutive values in LVALUE the same
372 value in RVALUE. For the nonce, LVALUE must refer to a full array, not
376 gfc_assign_data_value_range (gfc_expr * lvalue, gfc_expr * rvalue,
377 mpz_t index, mpz_t repeat)
380 gfc_expr *init, *expr;
381 gfc_constructor *con, *last_con;
383 gfc_typespec *last_ts;
386 symbol = lvalue->symtree->n.sym;
387 init = symbol->value;
388 last_ts = &symbol->ts;
390 mpz_init_set_si (offset, 0);
392 /* Find/create the parent expressions for subobject references. */
393 for (ref = lvalue->ref; ref; ref = ref->next)
395 /* Use the existing initializer expression if it exists.
396 Otherwise create a new one. */
398 expr = gfc_get_expr ();
402 /* Find or create this element. */
408 /* The element typespec will be the same as the array
411 /* Setup the expression to hold the constructor. */
412 expr->expr_type = EXPR_ARRAY;
413 expr->rank = ref->u.ar.as->rank;
416 gcc_assert (expr->expr_type == EXPR_ARRAY);
418 if (ref->u.ar.type == AR_ELEMENT)
420 get_array_index (&ref->u.ar, &offset);
422 /* This had better not be the bottom of the reference.
423 We can still get to a full array via a component. */
424 gcc_assert (ref->next != NULL);
428 mpz_set (offset, index);
430 /* We're at a full array or an array section. This means
431 that we've better have found a full array, and that we're
432 at the bottom of the reference. */
433 gcc_assert (ref->u.ar.type == AR_FULL);
434 gcc_assert (ref->next == NULL);
437 /* Find the same element in the existing constructor. */
438 con = expr->value.constructor;
439 con = find_con_by_offset (offset, con);
441 /* Create a new constructor. */
444 con = gfc_get_constructor ();
445 mpz_set (con->n.offset, offset);
446 if (ref->next == NULL)
447 mpz_set (con->repeat, repeat);
448 gfc_insert_constructor (expr, con);
451 gcc_assert (ref->next != NULL);
457 /* Setup the expression to hold the constructor. */
458 expr->expr_type = EXPR_STRUCTURE;
459 expr->ts.type = BT_DERIVED;
460 expr->ts.derived = ref->u.c.sym;
463 gcc_assert (expr->expr_type == EXPR_STRUCTURE);
464 last_ts = &ref->u.c.component->ts;
466 /* Find the same element in the existing constructor. */
467 con = expr->value.constructor;
468 con = find_con_by_component (ref->u.c.component, con);
472 /* Create a new constructor. */
473 con = gfc_get_constructor ();
474 con->n.component = ref->u.c.component;
475 con->next = expr->value.constructor;
476 expr->value.constructor = con;
479 /* Since we're only intending to initialize arrays here,
480 there better be an inner reference. */
481 gcc_assert (ref->next != NULL);
491 /* Point the container at the new expression. */
492 if (last_con == NULL)
493 symbol->value = expr;
495 last_con->expr = expr;
501 if (last_ts->type == BT_CHARACTER)
502 expr = create_character_intializer (init, last_ts, NULL, rvalue);
505 /* We should never be overwriting an existing initializer. */
508 expr = gfc_copy_expr (rvalue);
509 if (!gfc_compare_types (&lvalue->ts, &expr->ts))
510 gfc_convert_type (expr, &lvalue->ts, 0);
513 if (last_con == NULL)
514 symbol->value = expr;
516 last_con->expr = expr;
519 /* Modify the index of array section and re-calculate the array offset. */
522 gfc_advance_section (mpz_t *section_index, gfc_array_ref *ar,
531 for (i = 0; i < ar->dimen; i++)
533 if (ar->dimen_type[i] != DIMEN_RANGE)
538 mpz_add (section_index[i], section_index[i],
539 ar->stride[i]->value.integer);
540 if (mpz_cmp_si (ar->stride[i]->value.integer, 0) >= 0)
547 mpz_add_ui (section_index[i], section_index[i], 1);
552 cmp = mpz_cmp (section_index[i], ar->end[i]->value.integer);
554 cmp = mpz_cmp (section_index[i], ar->as->upper[i]->value.integer);
556 if ((cmp > 0 && forwards)
557 || (cmp < 0 && ! forwards))
559 /* Reset index to start, then loop to advance the next index. */
561 mpz_set (section_index[i], ar->start[i]->value.integer);
563 mpz_set (section_index[i], ar->as->lower[i]->value.integer);
569 mpz_set_si (*offset_ret, 0);
570 mpz_init_set_si (delta, 1);
572 for (i = 0; i < ar->dimen; i++)
574 mpz_sub (tmp, section_index[i], ar->as->lower[i]->value.integer);
575 mpz_mul (tmp, tmp, delta);
576 mpz_add (*offset_ret, tmp, *offset_ret);
578 mpz_sub (tmp, ar->as->upper[i]->value.integer,
579 ar->as->lower[i]->value.integer);
580 mpz_add_ui (tmp, tmp, 1);
581 mpz_mul (delta, tmp, delta);
588 /* Rearrange a structure constructor so the elements are in the specified
589 order. Also insert NULL entries if necessary. */
592 formalize_structure_cons (gfc_expr * expr)
594 gfc_constructor *head;
595 gfc_constructor *tail;
596 gfc_constructor *cur;
597 gfc_constructor *last;
599 gfc_component *order;
601 c = expr->value.constructor;
603 /* Constructor is already formalized. */
604 if (c->n.component == NULL)
608 for (order = expr->ts.derived->components; order; order = order->next)
610 /* Find the next component. */
613 while (cur != NULL && cur->n.component != order)
621 /* Create a new one. */
622 cur = gfc_get_constructor ();
626 /* Remove it from the chain. */
630 last->next = cur->next;
633 formalize_init_expr (cur->expr);
636 /* Add it to the new constructor. */
645 gcc_assert (c == NULL);
646 expr->value.constructor = head;
650 /* Make sure an initialization expression is in normalized form. Ie. all
651 elements of the constructors are in the correct order. */
654 formalize_init_expr (gfc_expr * expr)
662 type = expr->expr_type;
666 c = expr->value.constructor;
669 formalize_init_expr (c->expr);
675 formalize_structure_cons (expr);
684 /* Resolve symbol's initial value after all data statement. */
687 gfc_formalize_init_value (gfc_symbol *sym)
689 formalize_init_expr (sym->value);
693 /* Get the integer value into RET_AS and SECTION from AS and AR, and return
697 gfc_get_section_index (gfc_array_ref *ar, mpz_t *section_index, mpz_t *offset)
703 mpz_set_si (*offset, 0);
705 mpz_init_set_si (delta, 1);
706 for (i = 0; i < ar->dimen; i++)
708 mpz_init (section_index[i]);
709 switch (ar->dimen_type[i])
715 mpz_sub (tmp, ar->start[i]->value.integer,
716 ar->as->lower[i]->value.integer);
717 mpz_mul (tmp, tmp, delta);
718 mpz_add (*offset, tmp, *offset);
719 mpz_set (section_index[i], ar->start[i]->value.integer);
722 mpz_set (section_index[i], ar->as->lower[i]->value.integer);
726 gfc_internal_error ("TODO: Vector sections in data statements");
732 mpz_sub (tmp, ar->as->upper[i]->value.integer,
733 ar->as->lower[i]->value.integer);
734 mpz_add_ui (tmp, tmp, 1);
735 mpz_mul (delta, tmp, delta);