* c-common.h: Remove the prototype for yyparse.
[official-gcc.git] / gcc / fortran / data.c
blob8fb8b5d08297b1f44116b82f12290060e9663bc9
1 /* Supporting functions for resolving DATA statement.
2 Copyright (C) 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
3 Contributed by Lifang Zeng <zlf605@hotmail.com>
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 51 Franklin Street, Fifth Floor,Boston, MA
20 02110-1301, USA. */
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"
39 static void formalize_init_expr (gfc_expr *);
41 /* Calculate the array element offset. */
43 static void
44 get_array_index (gfc_array_ref * ar, mpz_t * offset)
46 gfc_expr *e;
47 int i;
48 try re;
49 mpz_t delta;
50 mpz_t tmp;
52 mpz_init (tmp);
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 re = 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);
64 mpz_set (tmp, e->value.integer);
65 mpz_sub (tmp, tmp, ar->as->lower[i]->value.integer);
66 mpz_mul (tmp, tmp, delta);
67 mpz_add (*offset, tmp, *offset);
69 mpz_sub (tmp, ar->as->upper[i]->value.integer,
70 ar->as->lower[i]->value.integer);
71 mpz_add_ui (tmp, tmp, 1);
72 mpz_mul (delta, tmp, delta);
74 mpz_clear (delta);
75 mpz_clear (tmp);
79 /* Find if there is a constructor which offset is equal to OFFSET. */
81 static gfc_constructor *
82 find_con_by_offset (mpz_t offset, gfc_constructor *con)
84 mpz_t tmp;
85 gfc_constructor *ret = NULL;
87 mpz_init (tmp);
89 for (; con; con = con->next)
91 int cmp = mpz_cmp (offset, con->n.offset);
93 /* We retain a sorted list, so if we're too large, we're done. */
94 if (cmp < 0)
95 break;
97 /* Yaye for exact matches. */
98 if (cmp == 0)
100 ret = con;
101 break;
104 /* If the constructor element is a range, match any element. */
105 if (mpz_cmp_ui (con->repeat, 1) > 0)
107 mpz_add (tmp, con->n.offset, con->repeat);
108 if (mpz_cmp (offset, tmp) < 0)
110 ret = con;
111 break;
116 mpz_clear (tmp);
117 return ret;
121 /* Find if there is a constructor which component is equal to COM. */
123 static gfc_constructor *
124 find_con_by_component (gfc_component *com, gfc_constructor *con)
126 for (; con; con = con->next)
128 if (com == con->n.component)
129 return con;
131 return NULL;
135 /* Create a character type initialization expression from RVALUE.
136 TS [and REF] describe [the substring of] the variable being initialized.
137 INIT is thh existing initializer, not NULL. Initialization is performed
138 according to normal assignment rules. */
140 static gfc_expr *
141 create_character_intializer (gfc_expr * init, gfc_typespec * ts,
142 gfc_ref * ref, gfc_expr * rvalue)
144 int len;
145 int start;
146 int end;
147 char *dest;
149 gfc_extract_int (ts->cl->length, &len);
151 if (init == NULL)
153 /* Create a new initializer. */
154 init = gfc_get_expr ();
155 init->expr_type = EXPR_CONSTANT;
156 init->ts = *ts;
158 dest = gfc_getmem (len);
159 init->value.character.length = len;
160 init->value.character.string = dest;
161 /* Blank the string if we're only setting a substring. */
162 if (ref != NULL)
163 memset (dest, ' ', len);
165 else
166 dest = init->value.character.string;
168 if (ref)
170 gcc_assert (ref->type == REF_SUBSTRING);
172 /* Only set a substring of the destination. Fortran substring bounds
173 are one-based [start, end], we want zero based [start, end). */
174 gfc_extract_int (ref->u.ss.start, &start);
175 start--;
176 gfc_extract_int (ref->u.ss.end, &end);
178 else
180 /* Set the whole string. */
181 start = 0;
182 end = len;
185 /* Copy the initial value. */
186 len = rvalue->value.character.length;
187 if (len > end - start)
188 len = end - start;
189 memcpy (&dest[start], rvalue->value.character.string, len);
191 /* Pad with spaces. Substrings will already be blanked. */
192 if (len < end - start && ref == NULL)
193 memset (&dest[start + len], ' ', end - (start + len));
195 if (rvalue->ts.type == BT_HOLLERITH)
196 init->from_H = 1;
198 return init;
201 /* Assign the initial value RVALUE to LVALUE's symbol->value. If the
202 LVALUE already has an initialization, we extend this, otherwise we
203 create a new one. */
205 void
206 gfc_assign_data_value (gfc_expr * lvalue, gfc_expr * rvalue, mpz_t index)
208 gfc_ref *ref;
209 gfc_expr *init;
210 gfc_expr *expr;
211 gfc_constructor *con;
212 gfc_constructor *last_con;
213 gfc_symbol *symbol;
214 gfc_typespec *last_ts;
215 mpz_t offset;
217 symbol = lvalue->symtree->n.sym;
218 init = symbol->value;
219 last_ts = &symbol->ts;
220 last_con = NULL;
221 mpz_init_set_si (offset, 0);
223 /* Find/create the parent expressions for subobject references. */
224 for (ref = lvalue->ref; ref; ref = ref->next)
226 /* Break out of the loop if we find a substring. */
227 if (ref->type == REF_SUBSTRING)
229 /* A substring should always br the last subobject reference. */
230 gcc_assert (ref->next == NULL);
231 break;
234 /* Use the existing initializer expression if it exists. Otherwise
235 create a new one. */
236 if (init == NULL)
237 expr = gfc_get_expr ();
238 else
239 expr = init;
241 /* Find or create this element. */
242 switch (ref->type)
244 case REF_ARRAY:
245 if (init == NULL)
247 /* The element typespec will be the same as the array
248 typespec. */
249 expr->ts = *last_ts;
250 /* Setup the expression to hold the constructor. */
251 expr->expr_type = EXPR_ARRAY;
252 expr->rank = ref->u.ar.as->rank;
254 else
255 gcc_assert (expr->expr_type == EXPR_ARRAY);
257 if (ref->u.ar.type == AR_ELEMENT)
258 get_array_index (&ref->u.ar, &offset);
259 else
260 mpz_set (offset, index);
262 /* Find the same element in the existing constructor. */
263 con = expr->value.constructor;
264 con = find_con_by_offset (offset, con);
266 if (con == NULL)
268 /* Create a new constructor. */
269 con = gfc_get_constructor ();
270 mpz_set (con->n.offset, offset);
271 gfc_insert_constructor (expr, con);
273 break;
275 case REF_COMPONENT:
276 if (init == NULL)
278 /* Setup the expression to hold the constructor. */
279 expr->expr_type = EXPR_STRUCTURE;
280 expr->ts.type = BT_DERIVED;
281 expr->ts.derived = ref->u.c.sym;
283 else
284 gcc_assert (expr->expr_type == EXPR_STRUCTURE);
285 last_ts = &ref->u.c.component->ts;
287 /* Find the same element in the existing constructor. */
288 con = expr->value.constructor;
289 con = find_con_by_component (ref->u.c.component, con);
291 if (con == NULL)
293 /* Create a new constructor. */
294 con = gfc_get_constructor ();
295 con->n.component = ref->u.c.component;
296 con->next = expr->value.constructor;
297 expr->value.constructor = con;
299 break;
301 default:
302 gcc_unreachable ();
305 if (init == NULL)
307 /* Point the container at the new expression. */
308 if (last_con == NULL)
309 symbol->value = expr;
310 else
311 last_con->expr = expr;
313 init = con->expr;
314 last_con = con;
317 if (ref || last_ts->type == BT_CHARACTER)
318 expr = create_character_intializer (init, last_ts, ref, rvalue);
319 else
321 /* Overwriting an existing initializer is non-standard but usually only
322 provokes a warning from other compilers. */
323 if (init != NULL)
325 /* Order in which the expressions arrive here depends on whether they
326 are from data statements or F95 style declarations. Therefore,
327 check which is the most recent. */
328 expr = (init->where.lb->linenum > rvalue->where.lb->linenum) ?
329 init : rvalue;
330 gfc_notify_std (GFC_STD_GNU, "Extension: re-initialization "
331 "of '%s' at %L", symbol->name, &expr->where);
332 return;
335 expr = gfc_copy_expr (rvalue);
336 if (!gfc_compare_types (&lvalue->ts, &expr->ts))
337 gfc_convert_type (expr, &lvalue->ts, 0);
340 if (last_con == NULL)
341 symbol->value = expr;
342 else
343 last_con->expr = expr;
346 /* Similarly, but initialize REPEAT consecutive values in LVALUE the same
347 value in RVALUE. For the nonce, LVALUE must refer to a full array, not
348 an array section. */
350 void
351 gfc_assign_data_value_range (gfc_expr * lvalue, gfc_expr * rvalue,
352 mpz_t index, mpz_t repeat)
354 gfc_ref *ref;
355 gfc_expr *init, *expr;
356 gfc_constructor *con, *last_con;
357 gfc_symbol *symbol;
358 gfc_typespec *last_ts;
359 mpz_t offset;
361 symbol = lvalue->symtree->n.sym;
362 init = symbol->value;
363 last_ts = &symbol->ts;
364 last_con = NULL;
365 mpz_init_set_si (offset, 0);
367 /* Find/create the parent expressions for subobject references. */
368 for (ref = lvalue->ref; ref; ref = ref->next)
370 /* Use the existing initializer expression if it exists.
371 Otherwise create a new one. */
372 if (init == NULL)
373 expr = gfc_get_expr ();
374 else
375 expr = init;
377 /* Find or create this element. */
378 switch (ref->type)
380 case REF_ARRAY:
381 if (init == NULL)
383 /* The element typespec will be the same as the array
384 typespec. */
385 expr->ts = *last_ts;
386 /* Setup the expression to hold the constructor. */
387 expr->expr_type = EXPR_ARRAY;
388 expr->rank = ref->u.ar.as->rank;
390 else
391 gcc_assert (expr->expr_type == EXPR_ARRAY);
393 if (ref->u.ar.type == AR_ELEMENT)
395 get_array_index (&ref->u.ar, &offset);
397 /* This had better not be the bottom of the reference.
398 We can still get to a full array via a component. */
399 gcc_assert (ref->next != NULL);
401 else
403 mpz_set (offset, index);
405 /* We're at a full array or an array section. This means
406 that we've better have found a full array, and that we're
407 at the bottom of the reference. */
408 gcc_assert (ref->u.ar.type == AR_FULL);
409 gcc_assert (ref->next == NULL);
412 /* Find the same element in the existing constructor. */
413 con = expr->value.constructor;
414 con = find_con_by_offset (offset, con);
416 /* Create a new constructor. */
417 if (con == NULL)
419 con = gfc_get_constructor ();
420 mpz_set (con->n.offset, offset);
421 if (ref->next == NULL)
422 mpz_set (con->repeat, repeat);
423 gfc_insert_constructor (expr, con);
425 else
426 gcc_assert (ref->next != NULL);
427 break;
429 case REF_COMPONENT:
430 if (init == NULL)
432 /* Setup the expression to hold the constructor. */
433 expr->expr_type = EXPR_STRUCTURE;
434 expr->ts.type = BT_DERIVED;
435 expr->ts.derived = ref->u.c.sym;
437 else
438 gcc_assert (expr->expr_type == EXPR_STRUCTURE);
439 last_ts = &ref->u.c.component->ts;
441 /* Find the same element in the existing constructor. */
442 con = expr->value.constructor;
443 con = find_con_by_component (ref->u.c.component, con);
445 if (con == NULL)
447 /* Create a new constructor. */
448 con = gfc_get_constructor ();
449 con->n.component = ref->u.c.component;
450 con->next = expr->value.constructor;
451 expr->value.constructor = con;
454 /* Since we're only intending to initialize arrays here,
455 there better be an inner reference. */
456 gcc_assert (ref->next != NULL);
457 break;
459 case REF_SUBSTRING:
460 default:
461 gcc_unreachable ();
464 if (init == NULL)
466 /* Point the container at the new expression. */
467 if (last_con == NULL)
468 symbol->value = expr;
469 else
470 last_con->expr = expr;
472 init = con->expr;
473 last_con = con;
476 if (last_ts->type == BT_CHARACTER)
477 expr = create_character_intializer (init, last_ts, NULL, rvalue);
478 else
480 /* We should never be overwriting an existing initializer. */
481 gcc_assert (!init);
483 expr = gfc_copy_expr (rvalue);
484 if (!gfc_compare_types (&lvalue->ts, &expr->ts))
485 gfc_convert_type (expr, &lvalue->ts, 0);
488 if (last_con == NULL)
489 symbol->value = expr;
490 else
491 last_con->expr = expr;
494 /* Modify the index of array section and re-calculate the array offset. */
496 void
497 gfc_advance_section (mpz_t *section_index, gfc_array_ref *ar,
498 mpz_t *offset_ret)
500 int i;
501 mpz_t delta;
502 mpz_t tmp;
503 bool forwards;
504 int cmp;
506 for (i = 0; i < ar->dimen; i++)
508 if (ar->dimen_type[i] != DIMEN_RANGE)
509 continue;
511 if (ar->stride[i])
513 mpz_add (section_index[i], section_index[i],
514 ar->stride[i]->value.integer);
515 if (mpz_cmp_si (ar->stride[i]->value.integer, 0) >= 0)
516 forwards = true;
517 else
518 forwards = false;
520 else
522 mpz_add_ui (section_index[i], section_index[i], 1);
523 forwards = true;
526 if (ar->end[i])
527 cmp = mpz_cmp (section_index[i], ar->end[i]->value.integer);
528 else
529 cmp = mpz_cmp (section_index[i], ar->as->upper[i]->value.integer);
531 if ((cmp > 0 && forwards)
532 || (cmp < 0 && ! forwards))
534 /* Reset index to start, then loop to advance the next index. */
535 if (ar->start[i])
536 mpz_set (section_index[i], ar->start[i]->value.integer);
537 else
538 mpz_set (section_index[i], ar->as->lower[i]->value.integer);
540 else
541 break;
544 mpz_set_si (*offset_ret, 0);
545 mpz_init_set_si (delta, 1);
546 mpz_init (tmp);
547 for (i = 0; i < ar->dimen; i++)
549 mpz_sub (tmp, section_index[i], ar->as->lower[i]->value.integer);
550 mpz_mul (tmp, tmp, delta);
551 mpz_add (*offset_ret, tmp, *offset_ret);
553 mpz_sub (tmp, ar->as->upper[i]->value.integer,
554 ar->as->lower[i]->value.integer);
555 mpz_add_ui (tmp, tmp, 1);
556 mpz_mul (delta, tmp, delta);
558 mpz_clear (tmp);
559 mpz_clear (delta);
563 /* Rearrange a structure constructor so the elements are in the specified
564 order. Also insert NULL entries if necessary. */
566 static void
567 formalize_structure_cons (gfc_expr * expr)
569 gfc_constructor *head;
570 gfc_constructor *tail;
571 gfc_constructor *cur;
572 gfc_constructor *last;
573 gfc_constructor *c;
574 gfc_component *order;
576 c = expr->value.constructor;
578 /* Constructor is already formalized. */
579 if (c->n.component == NULL)
580 return;
582 head = tail = NULL;
583 for (order = expr->ts.derived->components; order; order = order->next)
585 /* Find the next component. */
586 last = NULL;
587 cur = c;
588 while (cur != NULL && cur->n.component != order)
590 last = cur;
591 cur = cur->next;
594 if (cur == NULL)
596 /* Create a new one. */
597 cur = gfc_get_constructor ();
599 else
601 /* Remove it from the chain. */
602 if (last == NULL)
603 c = cur->next;
604 else
605 last->next = cur->next;
606 cur->next = NULL;
608 formalize_init_expr (cur->expr);
611 /* Add it to the new constructor. */
612 if (head == NULL)
613 head = tail = cur;
614 else
616 tail->next = cur;
617 tail = tail->next;
620 gcc_assert (c == NULL);
621 expr->value.constructor = head;
625 /* Make sure an initialization expression is in normalized form. Ie. all
626 elements of the constructors are in the correct order. */
628 static void
629 formalize_init_expr (gfc_expr * expr)
631 expr_t type;
632 gfc_constructor *c;
634 if (expr == NULL)
635 return;
637 type = expr->expr_type;
638 switch (type)
640 case EXPR_ARRAY:
641 c = expr->value.constructor;
642 while (c)
644 formalize_init_expr (c->expr);
645 c = c->next;
647 break;
649 case EXPR_STRUCTURE:
650 formalize_structure_cons (expr);
651 break;
653 default:
654 break;
659 /* Resolve symbol's initial value after all data statement. */
661 void
662 gfc_formalize_init_value (gfc_symbol *sym)
664 formalize_init_expr (sym->value);
668 /* Get the integer value into RET_AS and SECTION from AS and AR, and return
669 offset. */
671 void
672 gfc_get_section_index (gfc_array_ref *ar, mpz_t *section_index, mpz_t *offset)
674 int i;
675 mpz_t delta;
676 mpz_t tmp;
678 mpz_set_si (*offset, 0);
679 mpz_init (tmp);
680 mpz_init_set_si (delta, 1);
681 for (i = 0; i < ar->dimen; i++)
683 mpz_init (section_index[i]);
684 switch (ar->dimen_type[i])
686 case DIMEN_ELEMENT:
687 case DIMEN_RANGE:
688 if (ar->start[i])
690 mpz_sub (tmp, ar->start[i]->value.integer,
691 ar->as->lower[i]->value.integer);
692 mpz_mul (tmp, tmp, delta);
693 mpz_add (*offset, tmp, *offset);
694 mpz_set (section_index[i], ar->start[i]->value.integer);
696 else
697 mpz_set (section_index[i], ar->as->lower[i]->value.integer);
698 break;
700 case DIMEN_VECTOR:
701 gfc_internal_error ("TODO: Vector sections in data statements");
703 default:
704 gcc_unreachable ();
707 mpz_sub (tmp, ar->as->upper[i]->value.integer,
708 ar->as->lower[i]->value.integer);
709 mpz_add_ui (tmp, tmp, 1);
710 mpz_mul (delta, tmp, delta);
713 mpz_clear (tmp);
714 mpz_clear (delta);