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)
85 for (; con; con = con->next)
87 if (mpz_cmp (offset, con->n.offset) == 0)
94 /* Find if there is a constructor which component is equal to COM. */
96 static gfc_constructor *
97 find_con_by_component (gfc_component *com, gfc_constructor *con)
99 for (; con; con = con->next)
101 if (com == con->n.component)
108 /* Create a character type intialization expression from RVALUE.
109 TS [and REF] describe [the substring of] the variable being initialized.
110 INIT is thh existing initializer, not NULL. Initialization is performed
111 according to normal assignment rules. */
114 create_character_intializer (gfc_expr * init, gfc_typespec * ts,
115 gfc_ref * ref, gfc_expr * rvalue)
122 gfc_extract_int (ts->cl->length, &len);
126 /* Create a new initializer. */
127 init = gfc_get_expr ();
128 init->expr_type = EXPR_CONSTANT;
131 dest = gfc_getmem (len);
132 init->value.character.length = len;
133 init->value.character.string = dest;
134 /* Blank the string if we're only setting a substring. */
136 memset (dest, ' ', len);
139 dest = init->value.character.string;
143 assert (ref->type == REF_SUBSTRING);
145 /* Only set a substring of the destination. Fortran substring bounds
146 are one-based [start, end], we want zero based [start, end). */
147 gfc_extract_int (ref->u.ss.start, &start);
149 gfc_extract_int (ref->u.ss.end, &end);
153 /* Set the whole string. */
158 /* Copy the initial value. */
159 len = rvalue->value.character.length;
160 if (len > end - start)
162 memcpy (&dest[start], rvalue->value.character.string, len);
164 /* Pad with spaces. Substrings will already be blanked. */
165 if (len < end - start && ref == NULL)
166 memset (&dest[start + len], ' ', end - (start + len));
171 /* Assign the initial value RVALUE to LVALUE's symbol->value. If the
172 LVALUE already has an initialization, we extend this, otherwise we
176 gfc_assign_data_value (gfc_expr * lvalue, gfc_expr * rvalue, mpz_t index)
181 gfc_constructor *con;
182 gfc_constructor *last_con;
184 gfc_typespec *last_ts;
187 symbol = lvalue->symtree->n.sym;
188 init = symbol->value;
189 last_ts = &symbol->ts;
191 mpz_init_set_si (offset, 0);
193 /* Find/create the parent expressions for subobject references. */
194 for (ref = lvalue->ref; ref; ref = ref->next)
196 /* Break out of the loop if we find a substring. */
197 if (ref->type == REF_SUBSTRING)
199 /* A substring should always br the last subobject reference. */
200 assert (ref->next == NULL);
204 /* Use the existing initializer expression if it exists. Otherwise
207 expr = gfc_get_expr ();
211 /* Find or create this element. */
217 /* The element typespec will be the same as the array
220 /* Setup the expression to hold the constructor. */
221 expr->expr_type = EXPR_ARRAY;
222 expr->rank = ref->u.ar.as->rank;
225 assert (expr->expr_type == EXPR_ARRAY);
227 if (ref->u.ar.type == AR_ELEMENT)
228 get_array_index (&ref->u.ar, &offset);
230 mpz_set (offset, index);
232 /* Find the same element in the existing constructor. */
233 con = expr->value.constructor;
234 con = find_con_by_offset (offset, con);
238 /* Create a new constructor. */
239 con = gfc_get_constructor();
240 mpz_set (con->n.offset, offset);
241 gfc_insert_constructor (expr, con);
248 /* Setup the expression to hold the constructor. */
249 expr->expr_type = EXPR_STRUCTURE;
250 expr->ts.type = BT_DERIVED;
251 expr->ts.derived = ref->u.c.sym;
254 assert (expr->expr_type == EXPR_STRUCTURE);
255 last_ts = &ref->u.c.component->ts;
257 /* Find the same element in the existing constructor. */
258 con = expr->value.constructor;
259 con = find_con_by_component (ref->u.c.component, con);
263 /* Create a new constructor. */
264 con = gfc_get_constructor ();
265 con->n.component = ref->u.c.component;
266 con->next = expr->value.constructor;
267 expr->value.constructor = con;
278 /* Point the container at the new expression. */
279 if (last_con == NULL)
280 symbol->value = expr;
282 last_con->expr = expr;
288 if (ref || last_ts->type == BT_CHARACTER)
289 expr = create_character_intializer (init, last_ts, ref, rvalue);
292 /* We should never be overwriting an existing initializer. */
295 expr = gfc_copy_expr (rvalue);
296 if (!gfc_compare_types (&lvalue->ts, &expr->ts))
297 gfc_convert_type (expr, &lvalue->ts, 0);
301 if (last_con == NULL)
302 symbol->value = expr;
304 last_con->expr = expr;
308 /* Modify the index of array section and re-calculate the array offset. */
311 gfc_advance_section (mpz_t *section_index, gfc_array_ref *ar,
320 for (i = 0; i < ar->dimen; i++)
322 if (ar->dimen_type[i] != DIMEN_RANGE)
327 mpz_add (section_index[i], section_index[i],
328 ar->stride[i]->value.integer);
329 if (mpz_cmp_si (ar->stride[i]->value.integer, 0) >= 0)
336 mpz_add_ui (section_index[i], section_index[i], 1);
341 cmp = mpz_cmp (section_index[i], ar->end[i]->value.integer);
343 cmp = mpz_cmp (section_index[i], ar->as->upper[i]->value.integer);
345 if ((cmp > 0 && forwards)
346 || (cmp < 0 && ! forwards))
348 /* Reset index to start, then loop to advance the next index. */
350 mpz_set (section_index[i], ar->start[i]->value.integer);
352 mpz_set (section_index[i], ar->as->lower[i]->value.integer);
358 mpz_set_si (*offset_ret, 0);
359 mpz_init_set_si (delta, 1);
361 for (i = 0; i < ar->dimen; i++)
363 mpz_sub (tmp, section_index[i], ar->as->lower[i]->value.integer);
364 mpz_mul (tmp, tmp, delta);
365 mpz_add (*offset_ret, tmp, *offset_ret);
367 mpz_sub (tmp, ar->as->upper[i]->value.integer,
368 ar->as->lower[i]->value.integer);
369 mpz_add_ui (tmp, tmp, 1);
370 mpz_mul (delta, tmp, delta);
377 /* Rearrange a structure constructor so the elements are in the specified
378 order. Also insert NULL entries if neccessary. */
381 formalize_structure_cons (gfc_expr * expr)
383 gfc_constructor *head;
384 gfc_constructor *tail;
385 gfc_constructor *cur;
386 gfc_constructor *last;
388 gfc_component *order;
390 c = expr->value.constructor;
392 /* Constructor is already fomalized. */
393 if (c->n.component == NULL)
397 for (order = expr->ts.derived->components; order; order = order->next)
399 /* Find the next component. */
402 while (cur != NULL && cur->n.component != order)
410 /* Create a new one. */
411 cur = gfc_get_constructor ();
415 /* Remove it from the chain. */
419 last->next = cur->next;
422 formalize_init_expr (cur->expr);
425 /* Add it to the new constructor. */
435 expr->value.constructor = head;
439 /* Make sure an initialization expression is in normalized form. Ie. all
440 elements of the constructors are in the correct order. */
443 formalize_init_expr (gfc_expr * expr)
451 type = expr->expr_type;
455 c = expr->value.constructor;
458 formalize_init_expr (c->expr);
464 formalize_structure_cons (expr);
473 /* Resolve symbol's initial value after all data statement. */
476 gfc_formalize_init_value (gfc_symbol *sym)
478 formalize_init_expr (sym->value);
482 /* Get the integer value into RET_AS and SECTION from AS and AR, and return
486 gfc_get_section_index (gfc_array_ref *ar, mpz_t *section_index, mpz_t *offset)
492 mpz_set_si (*offset, 0);
494 mpz_init_set_si (delta, 1);
495 for (i = 0; i < ar->dimen; i++)
497 mpz_init (section_index[i]);
498 switch (ar->dimen_type[i])
504 mpz_sub (tmp, ar->start[i]->value.integer,
505 ar->as->lower[i]->value.integer);
506 mpz_mul (tmp, tmp, delta);
507 mpz_add (*offset, tmp, *offset);
508 mpz_set (section_index[i], ar->start[i]->value.integer);
511 mpz_set (section_index[i], ar->as->lower[i]->value.integer);
515 gfc_internal_error ("TODO: Vector sections in data statements");
521 mpz_sub (tmp, ar->as->upper[i]->value.integer,
522 ar->as->lower[i]->value.integer);
523 mpz_add_ui (tmp, tmp, 1);
524 mpz_mul (delta, tmp, delta);