1 /* Supporting functions for resolving DATA statement.
2 Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
3 Free Software Foundation, Inc.
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 3, 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 COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
23 /* Notes for DATA statement implementation:
25 We first assign initial value to each symbol by gfc_assign_data_value
26 during resolving DATA statement. Refer to check_data_variable and
27 traverse_data_list in resolve.c.
29 The complexity exists in the handling of array section, implied do
30 and array of struct appeared in DATA statement.
32 We call gfc_conv_structure, gfc_con_array_array_initializer,
33 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)
53 mpz_set_si (*offset, 0);
54 mpz_init_set_si (delta, 1);
55 for (i = 0; i < ar->dimen; i++)
57 e = gfc_copy_expr (ar->start[i]);
58 gfc_simplify_expr (e, 1);
60 if ((gfc_is_constant_expr (ar->as->lower[i]) == 0)
61 || (gfc_is_constant_expr (ar->as->upper[i]) == 0)
62 || (gfc_is_constant_expr (e) == 0))
63 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 (splay_tree spt, mpz_t offset)
86 gfc_constructor *ret = NULL;
90 /* The complexity is due to needing quick access to the linked list of
91 constructors. Both a linked list and a splay tree are used, and both
92 are kept up to date if they are array elements (which is the only time
93 that a specific constructor has to be found). */
95 gcc_assert (spt != NULL);
98 sptn = splay_tree_lookup (spt, (splay_tree_key) mpz_get_si (offset));
101 ret = (gfc_constructor*) sptn->value;
104 /* Need to check and see if we match a range, so we will pull
105 the next lowest index and see if the range matches. */
106 sptn = splay_tree_predecessor (spt,
107 (splay_tree_key) mpz_get_si (offset));
110 con = (gfc_constructor*) sptn->value;
111 if (mpz_cmp_ui (con->repeat, 1) > 0)
114 mpz_add (tmp, con->n.offset, con->repeat);
115 if (mpz_cmp (offset, tmp) < 0)
120 ret = NULL; /* The range did not match. */
123 ret = NULL; /* No pred, so no match. */
130 /* Find if there is a constructor which component is equal to COM. */
132 static gfc_constructor *
133 find_con_by_component (gfc_component *com, gfc_constructor *con)
135 for (; con; con = con->next)
137 if (com == con->n.component)
144 /* Create a character type initialization expression from RVALUE.
145 TS [and REF] describe [the substring of] the variable being initialized.
146 INIT is the existing initializer, not NULL. Initialization is performed
147 according to normal assignment rules. */
150 create_character_intializer (gfc_expr *init, gfc_typespec *ts,
151 gfc_ref *ref, gfc_expr *rvalue)
156 gfc_extract_int (ts->u.cl->length, &len);
160 /* Create a new initializer. */
161 init = gfc_get_expr ();
162 init->expr_type = EXPR_CONSTANT;
165 dest = gfc_get_wide_string (len + 1);
167 init->value.character.length = len;
168 init->value.character.string = dest;
169 /* Blank the string if we're only setting a substring. */
171 gfc_wide_memset (dest, ' ', len);
174 dest = init->value.character.string;
178 gfc_expr *start_expr, *end_expr;
180 gcc_assert (ref->type == REF_SUBSTRING);
182 /* Only set a substring of the destination. Fortran substring bounds
183 are one-based [start, end], we want zero based [start, end). */
184 start_expr = gfc_copy_expr (ref->u.ss.start);
185 end_expr = gfc_copy_expr (ref->u.ss.end);
187 if ((gfc_simplify_expr (start_expr, 1) == FAILURE)
188 || (gfc_simplify_expr (end_expr, 1)) == FAILURE)
190 gfc_error ("failure to simplify substring reference in DATA "
191 "statement at %L", &ref->u.ss.start->where);
195 gfc_extract_int (start_expr, &start);
197 gfc_extract_int (end_expr, &end);
201 /* Set the whole string. */
206 /* Copy the initial value. */
207 if (rvalue->ts.type == BT_HOLLERITH)
208 len = rvalue->representation.length;
210 len = rvalue->value.character.length;
212 if (len > end - start)
215 gfc_warning_now ("initialization string truncated to match variable "
216 "at %L", &rvalue->where);
219 if (rvalue->ts.type == BT_HOLLERITH)
222 for (i = 0; i < len; i++)
223 dest[start+i] = rvalue->representation.string[i];
226 memcpy (&dest[start], rvalue->value.character.string,
227 len * sizeof (gfc_char_t));
229 /* Pad with spaces. Substrings will already be blanked. */
230 if (len < end - start && ref == NULL)
231 gfc_wide_memset (&dest[start + len], ' ', end - (start + len));
233 if (rvalue->ts.type == BT_HOLLERITH)
235 init->representation.length = init->value.character.length;
236 init->representation.string
237 = gfc_widechar_to_char (init->value.character.string,
238 init->value.character.length);
245 /* Assign the initial value RVALUE to LVALUE's symbol->value. If the
246 LVALUE already has an initialization, we extend this, otherwise we
250 gfc_assign_data_value (gfc_expr *lvalue, gfc_expr *rvalue, mpz_t index)
255 gfc_constructor *con;
256 gfc_constructor *last_con;
257 gfc_constructor *pred;
259 gfc_typespec *last_ts;
262 splay_tree_node sptn;
264 symbol = lvalue->symtree->n.sym;
265 init = symbol->value;
266 last_ts = &symbol->ts;
268 mpz_init_set_si (offset, 0);
270 /* Find/create the parent expressions for subobject references. */
271 for (ref = lvalue->ref; ref; ref = ref->next)
273 /* Break out of the loop if we find a substring. */
274 if (ref->type == REF_SUBSTRING)
276 /* A substring should always be the last subobject reference. */
277 gcc_assert (ref->next == NULL);
281 /* Use the existing initializer expression if it exists. Otherwise
284 expr = gfc_get_expr ();
288 /* Find or create this element. */
292 if (ref->u.ar.as->rank == 0)
294 gcc_assert (ref->u.ar.as->corank > 0);
300 if (init && expr->expr_type != EXPR_ARRAY)
302 gfc_error ("'%s' at %L already is initialized at %L",
303 lvalue->symtree->n.sym->name, &lvalue->where,
310 /* The element typespec will be the same as the array
313 /* Setup the expression to hold the constructor. */
314 expr->expr_type = EXPR_ARRAY;
315 expr->rank = ref->u.ar.as->rank;
318 if (ref->u.ar.type == AR_ELEMENT)
319 get_array_index (&ref->u.ar, &offset);
321 mpz_set (offset, index);
323 /* Check the bounds. */
324 if (mpz_cmp_si (offset, 0) < 0)
326 gfc_error ("Data element below array lower bound at %L",
333 if (spec_size (ref->u.ar.as, &size) == SUCCESS)
335 if (mpz_cmp (offset, size) >= 0)
338 gfc_error ("Data element above array upper bound at %L",
346 /* Splay tree containing offset and gfc_constructor. */
347 spt = expr->con_by_offset;
351 spt = splay_tree_new (splay_tree_compare_ints, NULL, NULL);
352 expr->con_by_offset = spt;
356 con = find_con_by_offset (spt, offset);
362 /* Create a new constructor. */
363 con = gfc_get_constructor ();
364 mpz_set (con->n.offset, offset);
365 j = (splay_tree_key) mpz_get_si (offset);
366 sptn = splay_tree_insert (spt, j, (splay_tree_value) con);
367 /* Fix up the linked list. */
368 sptn = splay_tree_predecessor (spt, j);
370 { /* Insert at the head. */
371 con->next = expr->value.constructor;
372 expr->value.constructor = con;
375 { /* Insert in the chain. */
376 pred = (gfc_constructor*) sptn->value;
377 con->next = pred->next;
386 /* Setup the expression to hold the constructor. */
387 expr->expr_type = EXPR_STRUCTURE;
388 expr->ts.type = BT_DERIVED;
389 expr->ts.u.derived = ref->u.c.sym;
392 gcc_assert (expr->expr_type == EXPR_STRUCTURE);
393 last_ts = &ref->u.c.component->ts;
395 /* Find the same element in the existing constructor. */
396 con = expr->value.constructor;
397 con = find_con_by_component (ref->u.c.component, con);
401 /* Create a new constructor. */
402 con = gfc_get_constructor ();
403 con->n.component = ref->u.c.component;
404 con->next = expr->value.constructor;
405 expr->value.constructor = con;
415 /* Point the container at the new expression. */
416 if (last_con == NULL)
417 symbol->value = expr;
419 last_con->expr = expr;
425 if (ref || last_ts->type == BT_CHARACTER)
427 if (lvalue->ts.u.cl->length == NULL && !(ref && ref->u.ss.length != NULL))
429 expr = create_character_intializer (init, last_ts, ref, rvalue);
433 /* Overwriting an existing initializer is non-standard but usually only
434 provokes a warning from other compilers. */
437 /* Order in which the expressions arrive here depends on whether
438 they are from data statements or F95 style declarations.
439 Therefore, check which is the most recent. */
440 expr = (LOCATION_LINE (init->where.lb->location)
441 > LOCATION_LINE (rvalue->where.lb->location))
443 gfc_notify_std (GFC_STD_GNU, "Extension: re-initialization "
444 "of '%s' at %L", symbol->name, &expr->where);
447 expr = gfc_copy_expr (rvalue);
448 if (!gfc_compare_types (&lvalue->ts, &expr->ts))
449 gfc_convert_type (expr, &lvalue->ts, 0);
452 if (last_con == NULL)
453 symbol->value = expr;
455 last_con->expr = expr;
461 /* Similarly, but initialize REPEAT consecutive values in LVALUE the same
462 value in RVALUE. For the nonce, LVALUE must refer to a full array, not
466 gfc_assign_data_value_range (gfc_expr *lvalue, gfc_expr *rvalue,
467 mpz_t index, mpz_t repeat)
470 gfc_expr *init, *expr;
471 gfc_constructor *con, *last_con;
472 gfc_constructor *pred;
474 gfc_typespec *last_ts;
477 splay_tree_node sptn;
479 symbol = lvalue->symtree->n.sym;
480 init = symbol->value;
481 last_ts = &symbol->ts;
483 mpz_init_set_si (offset, 0);
485 /* Find/create the parent expressions for subobject references. */
486 for (ref = lvalue->ref; ref; ref = ref->next)
488 /* Use the existing initializer expression if it exists.
489 Otherwise create a new one. */
491 expr = gfc_get_expr ();
495 /* Find or create this element. */
501 /* The element typespec will be the same as the array
504 /* Setup the expression to hold the constructor. */
505 expr->expr_type = EXPR_ARRAY;
506 expr->rank = ref->u.ar.as->rank;
509 gcc_assert (expr->expr_type == EXPR_ARRAY);
511 if (ref->u.ar.type == AR_ELEMENT)
513 get_array_index (&ref->u.ar, &offset);
515 /* This had better not be the bottom of the reference.
516 We can still get to a full array via a component. */
517 gcc_assert (ref->next != NULL);
521 mpz_set (offset, index);
523 /* We're at a full array or an array section. This means
524 that we've better have found a full array, and that we're
525 at the bottom of the reference. */
526 gcc_assert (ref->u.ar.type == AR_FULL);
527 gcc_assert (ref->next == NULL);
530 /* Find the same element in the existing constructor. */
532 /* Splay tree containing offset and gfc_constructor. */
533 spt = expr->con_by_offset;
537 spt = splay_tree_new (splay_tree_compare_ints, NULL, NULL);
538 expr->con_by_offset = spt;
542 con = find_con_by_offset (spt, offset);
547 /* Create a new constructor. */
548 con = gfc_get_constructor ();
549 mpz_set (con->n.offset, offset);
550 j = (splay_tree_key) mpz_get_si (offset);
552 if (ref->next == NULL)
553 mpz_set (con->repeat, repeat);
554 sptn = splay_tree_insert (spt, j, (splay_tree_value) con);
555 /* Fix up the linked list. */
556 sptn = splay_tree_predecessor (spt, j);
558 { /* Insert at the head. */
559 con->next = expr->value.constructor;
560 expr->value.constructor = con;
563 { /* Insert in the chain. */
564 pred = (gfc_constructor*) sptn->value;
565 con->next = pred->next;
570 gcc_assert (ref->next != NULL);
576 /* Setup the expression to hold the constructor. */
577 expr->expr_type = EXPR_STRUCTURE;
578 expr->ts.type = BT_DERIVED;
579 expr->ts.u.derived = ref->u.c.sym;
582 gcc_assert (expr->expr_type == EXPR_STRUCTURE);
583 last_ts = &ref->u.c.component->ts;
585 /* Find the same element in the existing constructor. */
586 con = expr->value.constructor;
587 con = find_con_by_component (ref->u.c.component, con);
591 /* Create a new constructor. */
592 con = gfc_get_constructor ();
593 con->n.component = ref->u.c.component;
594 con->next = expr->value.constructor;
595 expr->value.constructor = con;
598 /* Since we're only intending to initialize arrays here,
599 there better be an inner reference. */
600 gcc_assert (ref->next != NULL);
610 /* Point the container at the new expression. */
611 if (last_con == NULL)
612 symbol->value = expr;
614 last_con->expr = expr;
620 if (last_ts->type == BT_CHARACTER)
621 expr = create_character_intializer (init, last_ts, NULL, rvalue);
624 /* We should never be overwriting an existing initializer. */
627 expr = gfc_copy_expr (rvalue);
628 if (!gfc_compare_types (&lvalue->ts, &expr->ts))
629 gfc_convert_type (expr, &lvalue->ts, 0);
632 if (last_con == NULL)
633 symbol->value = expr;
635 last_con->expr = expr;
638 /* Modify the index of array section and re-calculate the array offset. */
641 gfc_advance_section (mpz_t *section_index, gfc_array_ref *ar,
650 for (i = 0; i < ar->dimen; i++)
652 if (ar->dimen_type[i] != DIMEN_RANGE)
657 mpz_add (section_index[i], section_index[i],
658 ar->stride[i]->value.integer);
659 if (mpz_cmp_si (ar->stride[i]->value.integer, 0) >= 0)
666 mpz_add_ui (section_index[i], section_index[i], 1);
671 cmp = mpz_cmp (section_index[i], ar->end[i]->value.integer);
673 cmp = mpz_cmp (section_index[i], ar->as->upper[i]->value.integer);
675 if ((cmp > 0 && forwards) || (cmp < 0 && !forwards))
677 /* Reset index to start, then loop to advance the next index. */
679 mpz_set (section_index[i], ar->start[i]->value.integer);
681 mpz_set (section_index[i], ar->as->lower[i]->value.integer);
687 mpz_set_si (*offset_ret, 0);
688 mpz_init_set_si (delta, 1);
690 for (i = 0; i < ar->dimen; i++)
692 mpz_sub (tmp, section_index[i], ar->as->lower[i]->value.integer);
693 mpz_mul (tmp, tmp, delta);
694 mpz_add (*offset_ret, tmp, *offset_ret);
696 mpz_sub (tmp, ar->as->upper[i]->value.integer,
697 ar->as->lower[i]->value.integer);
698 mpz_add_ui (tmp, tmp, 1);
699 mpz_mul (delta, tmp, delta);
706 /* Rearrange a structure constructor so the elements are in the specified
707 order. Also insert NULL entries if necessary. */
710 formalize_structure_cons (gfc_expr *expr)
712 gfc_constructor *head;
713 gfc_constructor *tail;
714 gfc_constructor *cur;
715 gfc_constructor *last;
717 gfc_component *order;
719 c = expr->value.constructor;
721 /* Constructor is already formalized. */
722 if (!c || c->n.component == NULL)
726 for (order = expr->ts.u.derived->components; order; order = order->next)
728 /* Find the next component. */
731 while (cur != NULL && cur->n.component != order)
739 /* Create a new one. */
740 cur = gfc_get_constructor ();
744 /* Remove it from the chain. */
748 last->next = cur->next;
751 formalize_init_expr (cur->expr);
754 /* Add it to the new constructor. */
763 gcc_assert (c == NULL);
764 expr->value.constructor = head;
768 /* Make sure an initialization expression is in normalized form, i.e., all
769 elements of the constructors are in the correct order. */
772 formalize_init_expr (gfc_expr *expr)
780 type = expr->expr_type;
784 c = expr->value.constructor;
787 formalize_init_expr (c->expr);
793 formalize_structure_cons (expr);
802 /* Resolve symbol's initial value after all data statement. */
805 gfc_formalize_init_value (gfc_symbol *sym)
807 formalize_init_expr (sym->value);
811 /* Get the integer value into RET_AS and SECTION from AS and AR, and return
815 gfc_get_section_index (gfc_array_ref *ar, mpz_t *section_index, mpz_t *offset)
821 mpz_set_si (*offset, 0);
823 mpz_init_set_si (delta, 1);
824 for (i = 0; i < ar->dimen; i++)
826 mpz_init (section_index[i]);
827 switch (ar->dimen_type[i])
833 mpz_sub (tmp, ar->start[i]->value.integer,
834 ar->as->lower[i]->value.integer);
835 mpz_mul (tmp, tmp, delta);
836 mpz_add (*offset, tmp, *offset);
837 mpz_set (section_index[i], ar->start[i]->value.integer);
840 mpz_set (section_index[i], ar->as->lower[i]->value.integer);
844 gfc_internal_error ("TODO: Vector sections in data statements");
850 mpz_sub (tmp, ar->as->upper[i]->value.integer,
851 ar->as->lower[i]->value.integer);
852 mpz_add_ui (tmp, tmp, 1);
853 mpz_mul (delta, tmp, delta);