pr 33870
[official-gcc.git] / gcc / fortran / data.c
blob59ac5e9d8e6fa8cfcf3fa2b8650e4f05fabc0713
1 /* Supporting functions for resolving DATA statement.
2 Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007
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
11 version.
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
16 for more details.
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 resolveing 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
34 trans-array.c. */
36 #include "config.h"
37 #include "gfortran.h"
38 #include "data.h"
40 static void formalize_init_expr (gfc_expr *);
42 /* Calculate the array element offset. */
44 static void
45 get_array_index (gfc_array_ref *ar, mpz_t *offset)
47 gfc_expr *e;
48 int i;
49 try re;
50 mpz_t delta;
51 mpz_t tmp;
53 mpz_init (tmp);
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);
66 mpz_set (tmp, e->value.integer);
67 mpz_sub (tmp, tmp, ar->as->lower[i]->value.integer);
68 mpz_mul (tmp, tmp, delta);
69 mpz_add (*offset, tmp, *offset);
71 mpz_sub (tmp, ar->as->upper[i]->value.integer,
72 ar->as->lower[i]->value.integer);
73 mpz_add_ui (tmp, tmp, 1);
74 mpz_mul (delta, tmp, delta);
76 mpz_clear (delta);
77 mpz_clear (tmp);
81 /* Find if there is a constructor which offset is equal to OFFSET. */
83 static gfc_constructor *
84 find_con_by_offset (splay_tree spt, mpz_t offset)
86 mpz_t tmp;
87 gfc_constructor *ret = NULL;
88 gfc_constructor *con;
89 splay_tree_node sptn;
91 /* The complexity is due to needing quick access to the linked list of
92 constructors. Both a linked list and a splay tree are used, and both
93 are kept up to date if they are array elements (which is the only time
94 that a specific constructor has to be found). */
96 gcc_assert (spt != NULL);
97 mpz_init (tmp);
99 sptn = splay_tree_lookup (spt, (splay_tree_key) mpz_get_si (offset));
101 if (sptn)
102 ret = (gfc_constructor*) sptn->value;
103 else
105 /* Need to check and see if we match a range, so we will pull
106 the next lowest index and see if the range matches. */
107 sptn = splay_tree_predecessor (spt,
108 (splay_tree_key) mpz_get_si (offset));
109 if (sptn)
111 con = (gfc_constructor*) sptn->value;
112 if (mpz_cmp_ui (con->repeat, 1) > 0)
114 mpz_init (tmp);
115 mpz_add (tmp, con->n.offset, con->repeat);
116 if (mpz_cmp (offset, tmp) < 0)
117 ret = con;
118 mpz_clear (tmp);
120 else
121 ret = NULL; /* The range did not match. */
123 else
124 ret = NULL; /* No pred, so no match. */
127 return ret;
131 /* Find if there is a constructor which component is equal to COM. */
133 static gfc_constructor *
134 find_con_by_component (gfc_component *com, gfc_constructor *con)
136 for (; con; con = con->next)
138 if (com == con->n.component)
139 return con;
141 return NULL;
145 /* Create a character type initialization expression from RVALUE.
146 TS [and REF] describe [the substring of] the variable being initialized.
147 INIT is thh existing initializer, not NULL. Initialization is performed
148 according to normal assignment rules. */
150 static gfc_expr *
151 create_character_intializer (gfc_expr *init, gfc_typespec *ts,
152 gfc_ref *ref, gfc_expr *rvalue)
154 int len;
155 int start;
156 int end;
157 char *dest, *rvalue_string;
159 gfc_extract_int (ts->cl->length, &len);
161 if (init == NULL)
163 /* Create a new initializer. */
164 init = gfc_get_expr ();
165 init->expr_type = EXPR_CONSTANT;
166 init->ts = *ts;
168 dest = gfc_getmem (len + 1);
169 dest[len] = '\0';
170 init->value.character.length = len;
171 init->value.character.string = dest;
172 /* Blank the string if we're only setting a substring. */
173 if (ref != NULL)
174 memset (dest, ' ', len);
176 else
177 dest = init->value.character.string;
179 if (ref)
181 gfc_expr *start_expr, *end_expr;
183 gcc_assert (ref->type == REF_SUBSTRING);
185 /* Only set a substring of the destination. Fortran substring bounds
186 are one-based [start, end], we want zero based [start, end). */
187 start_expr = gfc_copy_expr (ref->u.ss.start);
188 end_expr = gfc_copy_expr (ref->u.ss.end);
190 if ((gfc_simplify_expr (start_expr, 1) == FAILURE)
191 || (gfc_simplify_expr (end_expr, 1)) == FAILURE)
193 gfc_error ("failure to simplify substring reference in DATA "
194 "statement at %L", &ref->u.ss.start->where);
195 return NULL;
198 gfc_extract_int (start_expr, &start);
199 start--;
200 gfc_extract_int (end_expr, &end);
202 else
204 /* Set the whole string. */
205 start = 0;
206 end = len;
209 /* Copy the initial value. */
210 if (rvalue->ts.type == BT_HOLLERITH)
212 len = rvalue->representation.length;
213 rvalue_string = rvalue->representation.string;
215 else
217 len = rvalue->value.character.length;
218 rvalue_string = rvalue->value.character.string;
221 if (len > end - start)
223 len = end - start;
224 gfc_warning_now ("initialization string truncated to match variable "
225 "at %L", &rvalue->where);
228 memcpy (&dest[start], rvalue_string, len);
230 /* Pad with spaces. Substrings will already be blanked. */
231 if (len < end - start && ref == NULL)
232 memset (&dest[start + len], ' ', end - (start + len));
234 if (rvalue->ts.type == BT_HOLLERITH)
236 init->representation.length = init->value.character.length;
237 init->representation.string = init->value.character.string;
240 return init;
244 /* Assign the initial value RVALUE to LVALUE's symbol->value. If the
245 LVALUE already has an initialization, we extend this, otherwise we
246 create a new one. */
249 gfc_assign_data_value (gfc_expr *lvalue, gfc_expr *rvalue, mpz_t index)
251 gfc_ref *ref;
252 gfc_expr *init;
253 gfc_expr *expr;
254 gfc_constructor *con;
255 gfc_constructor *last_con;
256 gfc_constructor *pred;
257 gfc_symbol *symbol;
258 gfc_typespec *last_ts;
259 mpz_t offset;
260 splay_tree spt;
261 splay_tree_node sptn;
263 symbol = lvalue->symtree->n.sym;
264 init = symbol->value;
265 last_ts = &symbol->ts;
266 last_con = NULL;
267 mpz_init_set_si (offset, 0);
269 /* Find/create the parent expressions for subobject references. */
270 for (ref = lvalue->ref; ref; ref = ref->next)
272 /* Break out of the loop if we find a substring. */
273 if (ref->type == REF_SUBSTRING)
275 /* A substring should always be the last subobject reference. */
276 gcc_assert (ref->next == NULL);
277 break;
280 /* Use the existing initializer expression if it exists. Otherwise
281 create a new one. */
282 if (init == NULL)
283 expr = gfc_get_expr ();
284 else
285 expr = init;
287 /* Find or create this element. */
288 switch (ref->type)
290 case REF_ARRAY:
291 if (init && expr->expr_type != EXPR_ARRAY)
293 gfc_error ("'%s' at %L already is initialized at %L",
294 lvalue->symtree->n.sym->name, &lvalue->where,
295 &init->where);
296 return FAILURE;
299 if (init == NULL)
301 /* The element typespec will be the same as the array
302 typespec. */
303 expr->ts = *last_ts;
304 /* Setup the expression to hold the constructor. */
305 expr->expr_type = EXPR_ARRAY;
306 expr->rank = ref->u.ar.as->rank;
309 if (ref->u.ar.type == AR_ELEMENT)
310 get_array_index (&ref->u.ar, &offset);
311 else
312 mpz_set (offset, index);
314 /* Splay tree containing offset and gfc_constructor. */
315 spt = expr->con_by_offset;
317 if (spt == NULL)
319 spt = splay_tree_new (splay_tree_compare_ints, NULL, NULL);
320 expr->con_by_offset = spt;
321 con = NULL;
323 else
324 con = find_con_by_offset (spt, offset);
326 if (con == NULL)
328 splay_tree_key j;
330 /* Create a new constructor. */
331 con = gfc_get_constructor ();
332 mpz_set (con->n.offset, offset);
333 j = (splay_tree_key) mpz_get_si (offset);
334 sptn = splay_tree_insert (spt, j, (splay_tree_value) con);
335 /* Fix up the linked list. */
336 sptn = splay_tree_predecessor (spt, j);
337 if (sptn == NULL)
338 { /* Insert at the head. */
339 con->next = expr->value.constructor;
340 expr->value.constructor = con;
342 else
343 { /* Insert in the chain. */
344 pred = (gfc_constructor*) sptn->value;
345 con->next = pred->next;
346 pred->next = con;
349 break;
351 case REF_COMPONENT:
352 if (init == NULL)
354 /* Setup the expression to hold the constructor. */
355 expr->expr_type = EXPR_STRUCTURE;
356 expr->ts.type = BT_DERIVED;
357 expr->ts.derived = ref->u.c.sym;
359 else
360 gcc_assert (expr->expr_type == EXPR_STRUCTURE);
361 last_ts = &ref->u.c.component->ts;
363 /* Find the same element in the existing constructor. */
364 con = expr->value.constructor;
365 con = find_con_by_component (ref->u.c.component, con);
367 if (con == NULL)
369 /* Create a new constructor. */
370 con = gfc_get_constructor ();
371 con->n.component = ref->u.c.component;
372 con->next = expr->value.constructor;
373 expr->value.constructor = con;
375 break;
377 default:
378 gcc_unreachable ();
381 if (init == NULL)
383 /* Point the container at the new expression. */
384 if (last_con == NULL)
385 symbol->value = expr;
386 else
387 last_con->expr = expr;
389 init = con->expr;
390 last_con = con;
393 if (ref || last_ts->type == BT_CHARACTER)
394 expr = create_character_intializer (init, last_ts, ref, rvalue);
395 else
397 /* Overwriting an existing initializer is non-standard but usually only
398 provokes a warning from other compilers. */
399 if (init != NULL)
401 /* Order in which the expressions arrive here depends on whether
402 they are from data statements or F95 style declarations.
403 Therefore, check which is the most recent. */
404 #ifdef USE_MAPPED_LOCATION
405 expr = (LOCATION_LINE (init->where.lb->location)
406 > LOCATION_LINE (rvalue->where.lb->location))
407 ? init : rvalue;
408 #else
409 expr = (init->where.lb->linenum > rvalue->where.lb->linenum)
410 ? init : rvalue;
411 #endif
412 gfc_notify_std (GFC_STD_GNU, "Extension: re-initialization "
413 "of '%s' at %L", symbol->name, &expr->where);
416 expr = gfc_copy_expr (rvalue);
417 if (!gfc_compare_types (&lvalue->ts, &expr->ts))
418 gfc_convert_type (expr, &lvalue->ts, 0);
421 if (last_con == NULL)
422 symbol->value = expr;
423 else
424 last_con->expr = expr;
426 return SUCCESS;
430 /* Similarly, but initialize REPEAT consecutive values in LVALUE the same
431 value in RVALUE. For the nonce, LVALUE must refer to a full array, not
432 an array section. */
434 void
435 gfc_assign_data_value_range (gfc_expr *lvalue, gfc_expr *rvalue,
436 mpz_t index, mpz_t repeat)
438 gfc_ref *ref;
439 gfc_expr *init, *expr;
440 gfc_constructor *con, *last_con;
441 gfc_constructor *pred;
442 gfc_symbol *symbol;
443 gfc_typespec *last_ts;
444 mpz_t offset;
445 splay_tree spt;
446 splay_tree_node sptn;
448 symbol = lvalue->symtree->n.sym;
449 init = symbol->value;
450 last_ts = &symbol->ts;
451 last_con = NULL;
452 mpz_init_set_si (offset, 0);
454 /* Find/create the parent expressions for subobject references. */
455 for (ref = lvalue->ref; ref; ref = ref->next)
457 /* Use the existing initializer expression if it exists.
458 Otherwise create a new one. */
459 if (init == NULL)
460 expr = gfc_get_expr ();
461 else
462 expr = init;
464 /* Find or create this element. */
465 switch (ref->type)
467 case REF_ARRAY:
468 if (init == NULL)
470 /* The element typespec will be the same as the array
471 typespec. */
472 expr->ts = *last_ts;
473 /* Setup the expression to hold the constructor. */
474 expr->expr_type = EXPR_ARRAY;
475 expr->rank = ref->u.ar.as->rank;
477 else
478 gcc_assert (expr->expr_type == EXPR_ARRAY);
480 if (ref->u.ar.type == AR_ELEMENT)
482 get_array_index (&ref->u.ar, &offset);
484 /* This had better not be the bottom of the reference.
485 We can still get to a full array via a component. */
486 gcc_assert (ref->next != NULL);
488 else
490 mpz_set (offset, index);
492 /* We're at a full array or an array section. This means
493 that we've better have found a full array, and that we're
494 at the bottom of the reference. */
495 gcc_assert (ref->u.ar.type == AR_FULL);
496 gcc_assert (ref->next == NULL);
499 /* Find the same element in the existing constructor. */
501 /* Splay tree containing offset and gfc_constructor. */
502 spt = expr->con_by_offset;
504 if (spt == NULL)
506 spt = splay_tree_new (splay_tree_compare_ints, NULL, NULL);
507 expr->con_by_offset = spt;
508 con = NULL;
510 else
511 con = find_con_by_offset (spt, offset);
513 if (con == NULL)
515 splay_tree_key j;
516 /* Create a new constructor. */
517 con = gfc_get_constructor ();
518 mpz_set (con->n.offset, offset);
519 j = (splay_tree_key) mpz_get_si (offset);
521 if (ref->next == NULL)
522 mpz_set (con->repeat, repeat);
523 sptn = splay_tree_insert (spt, j, (splay_tree_value) con);
524 /* Fix up the linked list. */
525 sptn = splay_tree_predecessor (spt, j);
526 if (sptn == NULL)
527 { /* Insert at the head. */
528 con->next = expr->value.constructor;
529 expr->value.constructor = con;
531 else
532 { /* Insert in the chain. */
533 pred = (gfc_constructor*) sptn->value;
534 con->next = pred->next;
535 pred->next = con;
538 else
539 gcc_assert (ref->next != NULL);
540 break;
542 case REF_COMPONENT:
543 if (init == NULL)
545 /* Setup the expression to hold the constructor. */
546 expr->expr_type = EXPR_STRUCTURE;
547 expr->ts.type = BT_DERIVED;
548 expr->ts.derived = ref->u.c.sym;
550 else
551 gcc_assert (expr->expr_type == EXPR_STRUCTURE);
552 last_ts = &ref->u.c.component->ts;
554 /* Find the same element in the existing constructor. */
555 con = expr->value.constructor;
556 con = find_con_by_component (ref->u.c.component, con);
558 if (con == NULL)
560 /* Create a new constructor. */
561 con = gfc_get_constructor ();
562 con->n.component = ref->u.c.component;
563 con->next = expr->value.constructor;
564 expr->value.constructor = con;
567 /* Since we're only intending to initialize arrays here,
568 there better be an inner reference. */
569 gcc_assert (ref->next != NULL);
570 break;
572 case REF_SUBSTRING:
573 default:
574 gcc_unreachable ();
577 if (init == NULL)
579 /* Point the container at the new expression. */
580 if (last_con == NULL)
581 symbol->value = expr;
582 else
583 last_con->expr = expr;
585 init = con->expr;
586 last_con = con;
589 if (last_ts->type == BT_CHARACTER)
590 expr = create_character_intializer (init, last_ts, NULL, rvalue);
591 else
593 /* We should never be overwriting an existing initializer. */
594 gcc_assert (!init);
596 expr = gfc_copy_expr (rvalue);
597 if (!gfc_compare_types (&lvalue->ts, &expr->ts))
598 gfc_convert_type (expr, &lvalue->ts, 0);
601 if (last_con == NULL)
602 symbol->value = expr;
603 else
604 last_con->expr = expr;
607 /* Modify the index of array section and re-calculate the array offset. */
609 void
610 gfc_advance_section (mpz_t *section_index, gfc_array_ref *ar,
611 mpz_t *offset_ret)
613 int i;
614 mpz_t delta;
615 mpz_t tmp;
616 bool forwards;
617 int cmp;
619 for (i = 0; i < ar->dimen; i++)
621 if (ar->dimen_type[i] != DIMEN_RANGE)
622 continue;
624 if (ar->stride[i])
626 mpz_add (section_index[i], section_index[i],
627 ar->stride[i]->value.integer);
628 if (mpz_cmp_si (ar->stride[i]->value.integer, 0) >= 0)
629 forwards = true;
630 else
631 forwards = false;
633 else
635 mpz_add_ui (section_index[i], section_index[i], 1);
636 forwards = true;
639 if (ar->end[i])
640 cmp = mpz_cmp (section_index[i], ar->end[i]->value.integer);
641 else
642 cmp = mpz_cmp (section_index[i], ar->as->upper[i]->value.integer);
644 if ((cmp > 0 && forwards) || (cmp < 0 && !forwards))
646 /* Reset index to start, then loop to advance the next index. */
647 if (ar->start[i])
648 mpz_set (section_index[i], ar->start[i]->value.integer);
649 else
650 mpz_set (section_index[i], ar->as->lower[i]->value.integer);
652 else
653 break;
656 mpz_set_si (*offset_ret, 0);
657 mpz_init_set_si (delta, 1);
658 mpz_init (tmp);
659 for (i = 0; i < ar->dimen; i++)
661 mpz_sub (tmp, section_index[i], ar->as->lower[i]->value.integer);
662 mpz_mul (tmp, tmp, delta);
663 mpz_add (*offset_ret, tmp, *offset_ret);
665 mpz_sub (tmp, ar->as->upper[i]->value.integer,
666 ar->as->lower[i]->value.integer);
667 mpz_add_ui (tmp, tmp, 1);
668 mpz_mul (delta, tmp, delta);
670 mpz_clear (tmp);
671 mpz_clear (delta);
675 /* Rearrange a structure constructor so the elements are in the specified
676 order. Also insert NULL entries if necessary. */
678 static void
679 formalize_structure_cons (gfc_expr *expr)
681 gfc_constructor *head;
682 gfc_constructor *tail;
683 gfc_constructor *cur;
684 gfc_constructor *last;
685 gfc_constructor *c;
686 gfc_component *order;
688 c = expr->value.constructor;
690 /* Constructor is already formalized. */
691 if (c->n.component == NULL)
692 return;
694 head = tail = NULL;
695 for (order = expr->ts.derived->components; order; order = order->next)
697 /* Find the next component. */
698 last = NULL;
699 cur = c;
700 while (cur != NULL && cur->n.component != order)
702 last = cur;
703 cur = cur->next;
706 if (cur == NULL)
708 /* Create a new one. */
709 cur = gfc_get_constructor ();
711 else
713 /* Remove it from the chain. */
714 if (last == NULL)
715 c = cur->next;
716 else
717 last->next = cur->next;
718 cur->next = NULL;
720 formalize_init_expr (cur->expr);
723 /* Add it to the new constructor. */
724 if (head == NULL)
725 head = tail = cur;
726 else
728 tail->next = cur;
729 tail = tail->next;
732 gcc_assert (c == NULL);
733 expr->value.constructor = head;
737 /* Make sure an initialization expression is in normalized form. Ie. all
738 elements of the constructors are in the correct order. */
740 static void
741 formalize_init_expr (gfc_expr *expr)
743 expr_t type;
744 gfc_constructor *c;
746 if (expr == NULL)
747 return;
749 type = expr->expr_type;
750 switch (type)
752 case EXPR_ARRAY:
753 c = expr->value.constructor;
754 while (c)
756 formalize_init_expr (c->expr);
757 c = c->next;
759 break;
761 case EXPR_STRUCTURE:
762 formalize_structure_cons (expr);
763 break;
765 default:
766 break;
771 /* Resolve symbol's initial value after all data statement. */
773 void
774 gfc_formalize_init_value (gfc_symbol *sym)
776 formalize_init_expr (sym->value);
780 /* Get the integer value into RET_AS and SECTION from AS and AR, and return
781 offset. */
783 void
784 gfc_get_section_index (gfc_array_ref *ar, mpz_t *section_index, mpz_t *offset)
786 int i;
787 mpz_t delta;
788 mpz_t tmp;
790 mpz_set_si (*offset, 0);
791 mpz_init (tmp);
792 mpz_init_set_si (delta, 1);
793 for (i = 0; i < ar->dimen; i++)
795 mpz_init (section_index[i]);
796 switch (ar->dimen_type[i])
798 case DIMEN_ELEMENT:
799 case DIMEN_RANGE:
800 if (ar->start[i])
802 mpz_sub (tmp, ar->start[i]->value.integer,
803 ar->as->lower[i]->value.integer);
804 mpz_mul (tmp, tmp, delta);
805 mpz_add (*offset, tmp, *offset);
806 mpz_set (section_index[i], ar->start[i]->value.integer);
808 else
809 mpz_set (section_index[i], ar->as->lower[i]->value.integer);
810 break;
812 case DIMEN_VECTOR:
813 gfc_internal_error ("TODO: Vector sections in data statements");
815 default:
816 gcc_unreachable ();
819 mpz_sub (tmp, ar->as->upper[i]->value.integer,
820 ar->as->lower[i]->value.integer);
821 mpz_add_ui (tmp, tmp, 1);
822 mpz_mul (delta, tmp, delta);
825 mpz_clear (tmp);
826 mpz_clear (delta);