1 /* Supporting functions for resolving DATA statement.
2 Copyright (C) 2002, 2003, 2004 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
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
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, 59 Temple Place - Suite 330,Boston, MA
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 handleing 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)
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 intialization 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);
160 init->value.character.length = len;
161 init->value.character.string = dest;
162 /* Blank the string if we're only setting a substring. */
164 memset (dest, ' ', len);
167 dest = init->value.character.string;
171 assert (ref->type == REF_SUBSTRING);
173 /* Only set a substring of the destination. Fortran substring bounds
174 are one-based [start, end], we want zero based [start, end). */
175 gfc_extract_int (ref->u.ss.start, &start);
177 gfc_extract_int (ref->u.ss.end, &end);
181 /* Set the whole string. */
186 /* Copy the initial value. */
187 len = rvalue->value.character.length;
188 if (len > end - start)
190 memcpy (&dest[start], rvalue->value.character.string, len);
192 /* Pad with spaces. Substrings will already be blanked. */
193 if (len < end - start && ref == NULL)
194 memset (&dest[start + len], ' ', end - (start + len));
199 /* Assign the initial value RVALUE to LVALUE's symbol->value. If the
200 LVALUE already has an initialization, we extend this, otherwise we
204 gfc_assign_data_value (gfc_expr * lvalue, gfc_expr * rvalue, mpz_t index)
209 gfc_constructor *con;
210 gfc_constructor *last_con;
212 gfc_typespec *last_ts;
215 symbol = lvalue->symtree->n.sym;
216 init = symbol->value;
217 last_ts = &symbol->ts;
219 mpz_init_set_si (offset, 0);
221 /* Find/create the parent expressions for subobject references. */
222 for (ref = lvalue->ref; ref; ref = ref->next)
224 /* Break out of the loop if we find a substring. */
225 if (ref->type == REF_SUBSTRING)
227 /* A substring should always br the last subobject reference. */
228 assert (ref->next == NULL);
232 /* Use the existing initializer expression if it exists. Otherwise
235 expr = gfc_get_expr ();
239 /* Find or create this element. */
245 /* The element typespec will be the same as the array
248 /* Setup the expression to hold the constructor. */
249 expr->expr_type = EXPR_ARRAY;
250 expr->rank = ref->u.ar.as->rank;
253 assert (expr->expr_type == EXPR_ARRAY);
255 if (ref->u.ar.type == AR_ELEMENT)
256 get_array_index (&ref->u.ar, &offset);
258 mpz_set (offset, index);
260 /* Find the same element in the existing constructor. */
261 con = expr->value.constructor;
262 con = find_con_by_offset (offset, con);
266 /* Create a new constructor. */
267 con = gfc_get_constructor ();
268 mpz_set (con->n.offset, offset);
269 gfc_insert_constructor (expr, con);
276 /* Setup the expression to hold the constructor. */
277 expr->expr_type = EXPR_STRUCTURE;
278 expr->ts.type = BT_DERIVED;
279 expr->ts.derived = ref->u.c.sym;
282 assert (expr->expr_type == EXPR_STRUCTURE);
283 last_ts = &ref->u.c.component->ts;
285 /* Find the same element in the existing constructor. */
286 con = expr->value.constructor;
287 con = find_con_by_component (ref->u.c.component, con);
291 /* Create a new constructor. */
292 con = gfc_get_constructor ();
293 con->n.component = ref->u.c.component;
294 con->next = expr->value.constructor;
295 expr->value.constructor = con;
305 /* Point the container at the new expression. */
306 if (last_con == NULL)
307 symbol->value = expr;
309 last_con->expr = expr;
315 if (ref || last_ts->type == BT_CHARACTER)
316 expr = create_character_intializer (init, last_ts, ref, rvalue);
319 /* We should never be overwriting an existing initializer. */
322 expr = gfc_copy_expr (rvalue);
323 if (!gfc_compare_types (&lvalue->ts, &expr->ts))
324 gfc_convert_type (expr, &lvalue->ts, 0);
327 if (last_con == NULL)
328 symbol->value = expr;
330 last_con->expr = expr;
333 /* Similarly, but initialize REPEAT consectutive values in LVALUE the same
334 value in RVALUE. For the nonce, LVALUE must refer to a full array, not
338 gfc_assign_data_value_range (gfc_expr * lvalue, gfc_expr * rvalue,
339 mpz_t index, mpz_t repeat)
342 gfc_expr *init, *expr;
343 gfc_constructor *con, *last_con;
345 gfc_typespec *last_ts;
348 symbol = lvalue->symtree->n.sym;
349 init = symbol->value;
350 last_ts = &symbol->ts;
352 mpz_init_set_si (offset, 0);
354 /* Find/create the parent expressions for subobject references. */
355 for (ref = lvalue->ref; ref; ref = ref->next)
357 /* Use the existing initializer expression if it exists.
358 Otherwise create a new one. */
360 expr = gfc_get_expr ();
364 /* Find or create this element. */
370 /* The element typespec will be the same as the array
373 /* Setup the expression to hold the constructor. */
374 expr->expr_type = EXPR_ARRAY;
375 expr->rank = ref->u.ar.as->rank;
378 assert (expr->expr_type == EXPR_ARRAY);
380 if (ref->u.ar.type == AR_ELEMENT)
382 get_array_index (&ref->u.ar, &offset);
384 /* This had better not be the bottom of the reference.
385 We can still get to a full array via a component. */
386 assert (ref->next != NULL);
390 mpz_set (offset, index);
392 /* We're at a full array or an array section. This means
393 that we've better have found a full array, and that we're
394 at the bottom of the reference. */
395 assert (ref->u.ar.type == AR_FULL);
396 assert (ref->next == NULL);
399 /* Find the same element in the existing constructor. */
400 con = expr->value.constructor;
401 con = find_con_by_offset (offset, con);
403 /* Create a new constructor. */
406 con = gfc_get_constructor ();
407 mpz_set (con->n.offset, offset);
408 if (ref->next == NULL)
409 mpz_set (con->repeat, repeat);
410 gfc_insert_constructor (expr, con);
413 assert (ref->next != NULL);
419 /* Setup the expression to hold the constructor. */
420 expr->expr_type = EXPR_STRUCTURE;
421 expr->ts.type = BT_DERIVED;
422 expr->ts.derived = ref->u.c.sym;
425 assert (expr->expr_type == EXPR_STRUCTURE);
426 last_ts = &ref->u.c.component->ts;
428 /* Find the same element in the existing constructor. */
429 con = expr->value.constructor;
430 con = find_con_by_component (ref->u.c.component, con);
434 /* Create a new constructor. */
435 con = gfc_get_constructor ();
436 con->n.component = ref->u.c.component;
437 con->next = expr->value.constructor;
438 expr->value.constructor = con;
441 /* Since we're only intending to initialize arrays here,
442 there better be an inner reference. */
443 assert (ref->next != NULL);
453 /* Point the container at the new expression. */
454 if (last_con == NULL)
455 symbol->value = expr;
457 last_con->expr = expr;
463 /* We should never be overwriting an existing initializer. */
466 expr = gfc_copy_expr (rvalue);
467 if (!gfc_compare_types (&lvalue->ts, &expr->ts))
468 gfc_convert_type (expr, &lvalue->ts, 0);
470 if (last_con == NULL)
471 symbol->value = expr;
473 last_con->expr = expr;
476 /* Modify the index of array section and re-calculate the array offset. */
479 gfc_advance_section (mpz_t *section_index, gfc_array_ref *ar,
488 for (i = 0; i < ar->dimen; i++)
490 if (ar->dimen_type[i] != DIMEN_RANGE)
495 mpz_add (section_index[i], section_index[i],
496 ar->stride[i]->value.integer);
497 if (mpz_cmp_si (ar->stride[i]->value.integer, 0) >= 0)
504 mpz_add_ui (section_index[i], section_index[i], 1);
509 cmp = mpz_cmp (section_index[i], ar->end[i]->value.integer);
511 cmp = mpz_cmp (section_index[i], ar->as->upper[i]->value.integer);
513 if ((cmp > 0 && forwards)
514 || (cmp < 0 && ! forwards))
516 /* Reset index to start, then loop to advance the next index. */
518 mpz_set (section_index[i], ar->start[i]->value.integer);
520 mpz_set (section_index[i], ar->as->lower[i]->value.integer);
526 mpz_set_si (*offset_ret, 0);
527 mpz_init_set_si (delta, 1);
529 for (i = 0; i < ar->dimen; i++)
531 mpz_sub (tmp, section_index[i], ar->as->lower[i]->value.integer);
532 mpz_mul (tmp, tmp, delta);
533 mpz_add (*offset_ret, tmp, *offset_ret);
535 mpz_sub (tmp, ar->as->upper[i]->value.integer,
536 ar->as->lower[i]->value.integer);
537 mpz_add_ui (tmp, tmp, 1);
538 mpz_mul (delta, tmp, delta);
545 /* Rearrange a structure constructor so the elements are in the specified
546 order. Also insert NULL entries if neccessary. */
549 formalize_structure_cons (gfc_expr * expr)
551 gfc_constructor *head;
552 gfc_constructor *tail;
553 gfc_constructor *cur;
554 gfc_constructor *last;
556 gfc_component *order;
558 c = expr->value.constructor;
560 /* Constructor is already fomalized. */
561 if (c->n.component == NULL)
565 for (order = expr->ts.derived->components; order; order = order->next)
567 /* Find the next component. */
570 while (cur != NULL && cur->n.component != order)
578 /* Create a new one. */
579 cur = gfc_get_constructor ();
583 /* Remove it from the chain. */
587 last->next = cur->next;
590 formalize_init_expr (cur->expr);
593 /* Add it to the new constructor. */
603 expr->value.constructor = head;
607 /* Make sure an initialization expression is in normalized form. Ie. all
608 elements of the constructors are in the correct order. */
611 formalize_init_expr (gfc_expr * expr)
619 type = expr->expr_type;
623 c = expr->value.constructor;
626 formalize_init_expr (c->expr);
632 formalize_structure_cons (expr);
641 /* Resolve symbol's initial value after all data statement. */
644 gfc_formalize_init_value (gfc_symbol *sym)
646 formalize_init_expr (sym->value);
650 /* Get the integer value into RET_AS and SECTION from AS and AR, and return
654 gfc_get_section_index (gfc_array_ref *ar, mpz_t *section_index, mpz_t *offset)
660 mpz_set_si (*offset, 0);
662 mpz_init_set_si (delta, 1);
663 for (i = 0; i < ar->dimen; i++)
665 mpz_init (section_index[i]);
666 switch (ar->dimen_type[i])
672 mpz_sub (tmp, ar->start[i]->value.integer,
673 ar->as->lower[i]->value.integer);
674 mpz_mul (tmp, tmp, delta);
675 mpz_add (*offset, tmp, *offset);
676 mpz_set (section_index[i], ar->start[i]->value.integer);
679 mpz_set (section_index[i], ar->as->lower[i]->value.integer);
683 gfc_internal_error ("TODO: Vector sections in data statements");
689 mpz_sub (tmp, ar->as->upper[i]->value.integer,
690 ar->as->lower[i]->value.integer);
691 mpz_add_ui (tmp, tmp, 1);
692 mpz_mul (delta, tmp, delta);