intrinsic.texi: Minor cleanup, reflowing overlong paragraphs, and correcting whitespace.
[official-gcc.git] / gcc / fortran / data.c
bloba7a04c662f024055e240a0a326daac00498e0ad0
1 /* Supporting functions for resolving DATA statement.
2 Copyright (C) 2002, 2003, 2004, 2005, 2006 Free Software
3 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 2, 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 COPYING. If not, write to the Free
20 Software Foundation, 51 Franklin Street, Fifth Floor,Boston, MA
21 02110-1301, USA. */
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
35 trans-array.c. */
37 #include "config.h"
38 #include "gfortran.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);
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);
75 mpz_clear (delta);
76 mpz_clear (tmp);
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)
85 mpz_t tmp;
86 gfc_constructor *ret = NULL;
87 gfc_constructor *con;
88 splay_tree_node sptn;
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 are
92 kept up to date if they are array elements (which is the only time that
93 a specific constructor has to be found). */
95 gcc_assert (spt != NULL);
96 mpz_init (tmp);
98 sptn = splay_tree_lookup (spt, (splay_tree_key) mpz_get_si(offset));
100 if (sptn)
101 ret = (gfc_constructor*) sptn->value;
102 else
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, (splay_tree_key) mpz_get_si(offset));
107 if (sptn)
109 con = (gfc_constructor*) sptn->value;
110 if (mpz_cmp_ui (con->repeat, 1) > 0)
112 mpz_init (tmp);
113 mpz_add (tmp, con->n.offset, con->repeat);
114 if (mpz_cmp (offset, tmp) < 0)
115 ret = con;
116 mpz_clear (tmp);
118 else
119 ret = NULL; /* The range did not match. */
121 else
122 ret = NULL; /* No pred, so no match. */
125 return ret;
129 /* Find if there is a constructor which component is equal to COM. */
131 static gfc_constructor *
132 find_con_by_component (gfc_component *com, gfc_constructor *con)
134 for (; con; con = con->next)
136 if (com == con->n.component)
137 return con;
139 return NULL;
143 /* Create a character type initialization expression from RVALUE.
144 TS [and REF] describe [the substring of] the variable being initialized.
145 INIT is thh existing initializer, not NULL. Initialization is performed
146 according to normal assignment rules. */
148 static gfc_expr *
149 create_character_intializer (gfc_expr * init, gfc_typespec * ts,
150 gfc_ref * ref, gfc_expr * rvalue)
152 int len;
153 int start;
154 int end;
155 char *dest;
157 gfc_extract_int (ts->cl->length, &len);
159 if (init == NULL)
161 /* Create a new initializer. */
162 init = gfc_get_expr ();
163 init->expr_type = EXPR_CONSTANT;
164 init->ts = *ts;
166 dest = gfc_getmem (len + 1);
167 dest[len] = '\0';
168 init->value.character.length = len;
169 init->value.character.string = dest;
170 /* Blank the string if we're only setting a substring. */
171 if (ref != NULL)
172 memset (dest, ' ', len);
174 else
175 dest = init->value.character.string;
177 if (ref)
179 gfc_expr *start_expr, *end_expr;
181 gcc_assert (ref->type == REF_SUBSTRING);
183 /* Only set a substring of the destination. Fortran substring bounds
184 are one-based [start, end], we want zero based [start, end). */
185 start_expr = gfc_copy_expr (ref->u.ss.start);
186 end_expr = gfc_copy_expr (ref->u.ss.end);
188 if ((gfc_simplify_expr (start_expr, 1) == FAILURE)
189 || (gfc_simplify_expr (end_expr, 1)) == FAILURE)
191 gfc_error ("failure to simplify substring reference in DATA"
192 "statement at %L", &ref->u.ss.start->where);
193 return NULL;
196 gfc_extract_int (start_expr, &start);
197 start--;
198 gfc_extract_int (end_expr, &end);
200 else
202 /* Set the whole string. */
203 start = 0;
204 end = len;
207 /* Copy the initial value. */
208 len = rvalue->value.character.length;
209 if (len > end - start)
211 len = end - start;
212 gfc_warning_now ("initialization string truncated to match variable "
213 "at %L", &rvalue->where);
216 memcpy (&dest[start], rvalue->value.character.string, len);
218 /* Pad with spaces. Substrings will already be blanked. */
219 if (len < end - start && ref == NULL)
220 memset (&dest[start + len], ' ', end - (start + len));
222 if (rvalue->ts.type == BT_HOLLERITH)
223 init->from_H = 1;
225 return init;
228 /* Assign the initial value RVALUE to LVALUE's symbol->value. If the
229 LVALUE already has an initialization, we extend this, otherwise we
230 create a new one. */
232 void
233 gfc_assign_data_value (gfc_expr * lvalue, gfc_expr * rvalue, mpz_t index)
235 gfc_ref *ref;
236 gfc_expr *init;
237 gfc_expr *expr;
238 gfc_constructor *con;
239 gfc_constructor *last_con;
240 gfc_constructor *pred;
241 gfc_symbol *symbol;
242 gfc_typespec *last_ts;
243 mpz_t offset;
244 splay_tree spt;
245 splay_tree_node sptn;
247 symbol = lvalue->symtree->n.sym;
248 init = symbol->value;
249 last_ts = &symbol->ts;
250 last_con = NULL;
251 mpz_init_set_si (offset, 0);
253 /* Find/create the parent expressions for subobject references. */
254 for (ref = lvalue->ref; ref; ref = ref->next)
256 /* Break out of the loop if we find a substring. */
257 if (ref->type == REF_SUBSTRING)
259 /* A substring should always be the last subobject reference. */
260 gcc_assert (ref->next == NULL);
261 break;
264 /* Use the existing initializer expression if it exists. Otherwise
265 create a new one. */
266 if (init == NULL)
267 expr = gfc_get_expr ();
268 else
269 expr = init;
271 /* Find or create this element. */
272 switch (ref->type)
274 case REF_ARRAY:
275 if (init == NULL)
277 /* The element typespec will be the same as the array
278 typespec. */
279 expr->ts = *last_ts;
280 /* Setup the expression to hold the constructor. */
281 expr->expr_type = EXPR_ARRAY;
282 expr->rank = ref->u.ar.as->rank;
284 else
285 gcc_assert (expr->expr_type == EXPR_ARRAY);
287 if (ref->u.ar.type == AR_ELEMENT)
288 get_array_index (&ref->u.ar, &offset);
289 else
290 mpz_set (offset, index);
292 /* Splay tree containing offset and gfc_constructor. */
293 spt = expr->con_by_offset;
295 if (spt == NULL)
297 spt = splay_tree_new (splay_tree_compare_ints,NULL,NULL);
298 expr->con_by_offset = spt;
299 con = NULL;
301 else
302 con = find_con_by_offset (spt, offset);
304 if (con == NULL)
306 /* Create a new constructor. */
307 con = gfc_get_constructor ();
308 mpz_set (con->n.offset, offset);
309 sptn = splay_tree_insert (spt, (splay_tree_key) mpz_get_si(offset),
310 (splay_tree_value) con);
311 /* Fix up the linked list. */
312 sptn = splay_tree_predecessor (spt, (splay_tree_key) mpz_get_si(offset));
313 if (sptn == NULL)
314 { /* Insert at the head. */
315 con->next = expr->value.constructor;
316 expr->value.constructor = con;
318 else
319 { /* Insert in the chain. */
320 pred = (gfc_constructor*) sptn->value;
321 con->next = pred->next;
322 pred->next = con;
325 break;
327 case REF_COMPONENT:
328 if (init == NULL)
330 /* Setup the expression to hold the constructor. */
331 expr->expr_type = EXPR_STRUCTURE;
332 expr->ts.type = BT_DERIVED;
333 expr->ts.derived = ref->u.c.sym;
335 else
336 gcc_assert (expr->expr_type == EXPR_STRUCTURE);
337 last_ts = &ref->u.c.component->ts;
339 /* Find the same element in the existing constructor. */
340 con = expr->value.constructor;
341 con = find_con_by_component (ref->u.c.component, con);
343 if (con == NULL)
345 /* Create a new constructor. */
346 con = gfc_get_constructor ();
347 con->n.component = ref->u.c.component;
348 con->next = expr->value.constructor;
349 expr->value.constructor = con;
351 break;
353 default:
354 gcc_unreachable ();
357 if (init == NULL)
359 /* Point the container at the new expression. */
360 if (last_con == NULL)
361 symbol->value = expr;
362 else
363 last_con->expr = expr;
365 init = con->expr;
366 last_con = con;
369 if (ref || last_ts->type == BT_CHARACTER)
370 expr = create_character_intializer (init, last_ts, ref, rvalue);
371 else
373 /* Overwriting an existing initializer is non-standard but usually only
374 provokes a warning from other compilers. */
375 if (init != NULL)
377 /* Order in which the expressions arrive here depends on whether they
378 are from data statements or F95 style declarations. Therefore,
379 check which is the most recent. */
380 #ifdef USE_MAPPED_LOCATION
381 expr = (LOCATION_LINE (init->where.lb->location)
382 > LOCATION_LINE (rvalue->where.lb->location))
383 ? init : rvalue;
384 #else
385 expr = (init->where.lb->linenum > rvalue->where.lb->linenum) ?
386 init : rvalue;
387 #endif
388 gfc_notify_std (GFC_STD_GNU, "Extension: re-initialization "
389 "of '%s' at %L", symbol->name, &expr->where);
392 expr = gfc_copy_expr (rvalue);
393 if (!gfc_compare_types (&lvalue->ts, &expr->ts))
394 gfc_convert_type (expr, &lvalue->ts, 0);
397 if (last_con == NULL)
398 symbol->value = expr;
399 else
400 last_con->expr = expr;
403 /* Similarly, but initialize REPEAT consecutive values in LVALUE the same
404 value in RVALUE. For the nonce, LVALUE must refer to a full array, not
405 an array section. */
407 void
408 gfc_assign_data_value_range (gfc_expr * lvalue, gfc_expr * rvalue,
409 mpz_t index, mpz_t repeat)
411 gfc_ref *ref;
412 gfc_expr *init, *expr;
413 gfc_constructor *con, *last_con;
414 gfc_constructor *pred;
415 gfc_symbol *symbol;
416 gfc_typespec *last_ts;
417 mpz_t offset;
418 splay_tree spt;
419 splay_tree_node sptn;
421 symbol = lvalue->symtree->n.sym;
422 init = symbol->value;
423 last_ts = &symbol->ts;
424 last_con = NULL;
425 mpz_init_set_si (offset, 0);
427 /* Find/create the parent expressions for subobject references. */
428 for (ref = lvalue->ref; ref; ref = ref->next)
430 /* Use the existing initializer expression if it exists.
431 Otherwise create a new one. */
432 if (init == NULL)
433 expr = gfc_get_expr ();
434 else
435 expr = init;
437 /* Find or create this element. */
438 switch (ref->type)
440 case REF_ARRAY:
441 if (init == NULL)
443 /* The element typespec will be the same as the array
444 typespec. */
445 expr->ts = *last_ts;
446 /* Setup the expression to hold the constructor. */
447 expr->expr_type = EXPR_ARRAY;
448 expr->rank = ref->u.ar.as->rank;
450 else
451 gcc_assert (expr->expr_type == EXPR_ARRAY);
453 if (ref->u.ar.type == AR_ELEMENT)
455 get_array_index (&ref->u.ar, &offset);
457 /* This had better not be the bottom of the reference.
458 We can still get to a full array via a component. */
459 gcc_assert (ref->next != NULL);
461 else
463 mpz_set (offset, index);
465 /* We're at a full array or an array section. This means
466 that we've better have found a full array, and that we're
467 at the bottom of the reference. */
468 gcc_assert (ref->u.ar.type == AR_FULL);
469 gcc_assert (ref->next == NULL);
472 /* Find the same element in the existing constructor. */
474 /* Splay tree containing offset and gfc_constructor. */
475 spt = expr->con_by_offset;
477 if (spt == NULL)
479 spt = splay_tree_new (splay_tree_compare_ints,NULL,NULL);
480 expr->con_by_offset = spt;
481 con = NULL;
483 else
484 con = find_con_by_offset (spt, offset);
486 if (con == NULL)
488 /* Create a new constructor. */
489 con = gfc_get_constructor ();
490 mpz_set (con->n.offset, offset);
491 if (ref->next == NULL)
492 mpz_set (con->repeat, repeat);
493 sptn = splay_tree_insert (spt, (splay_tree_key) mpz_get_si(offset),
494 (splay_tree_value) con);
495 /* Fix up the linked list. */
496 sptn = splay_tree_predecessor (spt, (splay_tree_key) mpz_get_si(offset));
497 if (sptn == NULL)
498 { /* Insert at the head. */
499 con->next = expr->value.constructor;
500 expr->value.constructor = con;
502 else
503 { /* Insert in the chain. */
504 pred = (gfc_constructor*) sptn->value;
505 con->next = pred->next;
506 pred->next = con;
509 else
510 gcc_assert (ref->next != NULL);
511 break;
513 case REF_COMPONENT:
514 if (init == NULL)
516 /* Setup the expression to hold the constructor. */
517 expr->expr_type = EXPR_STRUCTURE;
518 expr->ts.type = BT_DERIVED;
519 expr->ts.derived = ref->u.c.sym;
521 else
522 gcc_assert (expr->expr_type == EXPR_STRUCTURE);
523 last_ts = &ref->u.c.component->ts;
525 /* Find the same element in the existing constructor. */
526 con = expr->value.constructor;
527 con = find_con_by_component (ref->u.c.component, con);
529 if (con == NULL)
531 /* Create a new constructor. */
532 con = gfc_get_constructor ();
533 con->n.component = ref->u.c.component;
534 con->next = expr->value.constructor;
535 expr->value.constructor = con;
538 /* Since we're only intending to initialize arrays here,
539 there better be an inner reference. */
540 gcc_assert (ref->next != NULL);
541 break;
543 case REF_SUBSTRING:
544 default:
545 gcc_unreachable ();
548 if (init == NULL)
550 /* Point the container at the new expression. */
551 if (last_con == NULL)
552 symbol->value = expr;
553 else
554 last_con->expr = expr;
556 init = con->expr;
557 last_con = con;
560 if (last_ts->type == BT_CHARACTER)
561 expr = create_character_intializer (init, last_ts, NULL, rvalue);
562 else
564 /* We should never be overwriting an existing initializer. */
565 gcc_assert (!init);
567 expr = gfc_copy_expr (rvalue);
568 if (!gfc_compare_types (&lvalue->ts, &expr->ts))
569 gfc_convert_type (expr, &lvalue->ts, 0);
572 if (last_con == NULL)
573 symbol->value = expr;
574 else
575 last_con->expr = expr;
578 /* Modify the index of array section and re-calculate the array offset. */
580 void
581 gfc_advance_section (mpz_t *section_index, gfc_array_ref *ar,
582 mpz_t *offset_ret)
584 int i;
585 mpz_t delta;
586 mpz_t tmp;
587 bool forwards;
588 int cmp;
590 for (i = 0; i < ar->dimen; i++)
592 if (ar->dimen_type[i] != DIMEN_RANGE)
593 continue;
595 if (ar->stride[i])
597 mpz_add (section_index[i], section_index[i],
598 ar->stride[i]->value.integer);
599 if (mpz_cmp_si (ar->stride[i]->value.integer, 0) >= 0)
600 forwards = true;
601 else
602 forwards = false;
604 else
606 mpz_add_ui (section_index[i], section_index[i], 1);
607 forwards = true;
610 if (ar->end[i])
611 cmp = mpz_cmp (section_index[i], ar->end[i]->value.integer);
612 else
613 cmp = mpz_cmp (section_index[i], ar->as->upper[i]->value.integer);
615 if ((cmp > 0 && forwards)
616 || (cmp < 0 && ! forwards))
618 /* Reset index to start, then loop to advance the next index. */
619 if (ar->start[i])
620 mpz_set (section_index[i], ar->start[i]->value.integer);
621 else
622 mpz_set (section_index[i], ar->as->lower[i]->value.integer);
624 else
625 break;
628 mpz_set_si (*offset_ret, 0);
629 mpz_init_set_si (delta, 1);
630 mpz_init (tmp);
631 for (i = 0; i < ar->dimen; i++)
633 mpz_sub (tmp, section_index[i], ar->as->lower[i]->value.integer);
634 mpz_mul (tmp, tmp, delta);
635 mpz_add (*offset_ret, tmp, *offset_ret);
637 mpz_sub (tmp, ar->as->upper[i]->value.integer,
638 ar->as->lower[i]->value.integer);
639 mpz_add_ui (tmp, tmp, 1);
640 mpz_mul (delta, tmp, delta);
642 mpz_clear (tmp);
643 mpz_clear (delta);
647 /* Rearrange a structure constructor so the elements are in the specified
648 order. Also insert NULL entries if necessary. */
650 static void
651 formalize_structure_cons (gfc_expr * expr)
653 gfc_constructor *head;
654 gfc_constructor *tail;
655 gfc_constructor *cur;
656 gfc_constructor *last;
657 gfc_constructor *c;
658 gfc_component *order;
660 c = expr->value.constructor;
662 /* Constructor is already formalized. */
663 if (c->n.component == NULL)
664 return;
666 head = tail = NULL;
667 for (order = expr->ts.derived->components; order; order = order->next)
669 /* Find the next component. */
670 last = NULL;
671 cur = c;
672 while (cur != NULL && cur->n.component != order)
674 last = cur;
675 cur = cur->next;
678 if (cur == NULL)
680 /* Create a new one. */
681 cur = gfc_get_constructor ();
683 else
685 /* Remove it from the chain. */
686 if (last == NULL)
687 c = cur->next;
688 else
689 last->next = cur->next;
690 cur->next = NULL;
692 formalize_init_expr (cur->expr);
695 /* Add it to the new constructor. */
696 if (head == NULL)
697 head = tail = cur;
698 else
700 tail->next = cur;
701 tail = tail->next;
704 gcc_assert (c == NULL);
705 expr->value.constructor = head;
709 /* Make sure an initialization expression is in normalized form. Ie. all
710 elements of the constructors are in the correct order. */
712 static void
713 formalize_init_expr (gfc_expr * expr)
715 expr_t type;
716 gfc_constructor *c;
718 if (expr == NULL)
719 return;
721 type = expr->expr_type;
722 switch (type)
724 case EXPR_ARRAY:
725 c = expr->value.constructor;
726 while (c)
728 formalize_init_expr (c->expr);
729 c = c->next;
731 break;
733 case EXPR_STRUCTURE:
734 formalize_structure_cons (expr);
735 break;
737 default:
738 break;
743 /* Resolve symbol's initial value after all data statement. */
745 void
746 gfc_formalize_init_value (gfc_symbol *sym)
748 formalize_init_expr (sym->value);
752 /* Get the integer value into RET_AS and SECTION from AS and AR, and return
753 offset. */
755 void
756 gfc_get_section_index (gfc_array_ref *ar, mpz_t *section_index, mpz_t *offset)
758 int i;
759 mpz_t delta;
760 mpz_t tmp;
762 mpz_set_si (*offset, 0);
763 mpz_init (tmp);
764 mpz_init_set_si (delta, 1);
765 for (i = 0; i < ar->dimen; i++)
767 mpz_init (section_index[i]);
768 switch (ar->dimen_type[i])
770 case DIMEN_ELEMENT:
771 case DIMEN_RANGE:
772 if (ar->start[i])
774 mpz_sub (tmp, ar->start[i]->value.integer,
775 ar->as->lower[i]->value.integer);
776 mpz_mul (tmp, tmp, delta);
777 mpz_add (*offset, tmp, *offset);
778 mpz_set (section_index[i], ar->start[i]->value.integer);
780 else
781 mpz_set (section_index[i], ar->as->lower[i]->value.integer);
782 break;
784 case DIMEN_VECTOR:
785 gfc_internal_error ("TODO: Vector sections in data statements");
787 default:
788 gcc_unreachable ();
791 mpz_sub (tmp, ar->as->upper[i]->value.integer,
792 ar->as->lower[i]->value.integer);
793 mpz_add_ui (tmp, tmp, 1);
794 mpz_mul (delta, tmp, delta);
797 mpz_clear (tmp);
798 mpz_clear (delta);