]>
Commit | Line | Data |
---|---|---|
6de9cd9a | 1 | /* Array things |
835aac92 | 2 | Copyright (C) 2000, 2001, 2002, 2004, 2005, 2006, 2007, 2008 |
65f8144a | 3 | Free Software Foundation, Inc. |
6de9cd9a DN |
4 | Contributed by Andy Vaught |
5 | ||
9fc4d79b | 6 | This file is part of GCC. |
6de9cd9a | 7 | |
9fc4d79b TS |
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 | |
d234d788 | 10 | Software Foundation; either version 3, or (at your option) any later |
9fc4d79b | 11 | version. |
6de9cd9a | 12 | |
9fc4d79b TS |
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. | |
6de9cd9a DN |
17 | |
18 | You should have received a copy of the GNU General Public License | |
d234d788 NC |
19 | along with GCC; see the file COPYING3. If not see |
20 | <http://www.gnu.org/licenses/>. */ | |
6de9cd9a DN |
21 | |
22 | #include "config.h" | |
d22e4895 | 23 | #include "system.h" |
6de9cd9a DN |
24 | #include "gfortran.h" |
25 | #include "match.h" | |
26 | ||
6de9cd9a DN |
27 | /* This parameter is the size of the largest array constructor that we |
28 | will expand to an array constructor without iterators. | |
29 | Constructors larger than this will remain in the iterator form. */ | |
30 | ||
beb6ae88 | 31 | #define GFC_MAX_AC_EXPAND 65535 |
6de9cd9a DN |
32 | |
33 | ||
34 | /**************** Array reference matching subroutines *****************/ | |
35 | ||
36 | /* Copy an array reference structure. */ | |
37 | ||
38 | gfc_array_ref * | |
65f8144a | 39 | gfc_copy_array_ref (gfc_array_ref *src) |
6de9cd9a DN |
40 | { |
41 | gfc_array_ref *dest; | |
42 | int i; | |
43 | ||
44 | if (src == NULL) | |
45 | return NULL; | |
46 | ||
47 | dest = gfc_get_array_ref (); | |
48 | ||
49 | *dest = *src; | |
50 | ||
51 | for (i = 0; i < GFC_MAX_DIMENSIONS; i++) | |
52 | { | |
53 | dest->start[i] = gfc_copy_expr (src->start[i]); | |
54 | dest->end[i] = gfc_copy_expr (src->end[i]); | |
55 | dest->stride[i] = gfc_copy_expr (src->stride[i]); | |
56 | } | |
57 | ||
58 | dest->offset = gfc_copy_expr (src->offset); | |
59 | ||
60 | return dest; | |
61 | } | |
62 | ||
63 | ||
64 | /* Match a single dimension of an array reference. This can be a | |
65 | single element or an array section. Any modifications we've made | |
66 | to the ar structure are cleaned up by the caller. If the init | |
67 | is set, we require the subscript to be a valid initialization | |
68 | expression. */ | |
69 | ||
70 | static match | |
65f8144a | 71 | match_subscript (gfc_array_ref *ar, int init) |
6de9cd9a DN |
72 | { |
73 | match m; | |
74 | int i; | |
75 | ||
76 | i = ar->dimen; | |
77 | ||
63645982 | 78 | ar->c_where[i] = gfc_current_locus; |
6de9cd9a DN |
79 | ar->start[i] = ar->end[i] = ar->stride[i] = NULL; |
80 | ||
81 | /* We can't be sure of the difference between DIMEN_ELEMENT and | |
82 | DIMEN_VECTOR until we know the type of the element itself at | |
83 | resolution time. */ | |
84 | ||
85 | ar->dimen_type[i] = DIMEN_UNKNOWN; | |
86 | ||
87 | if (gfc_match_char (':') == MATCH_YES) | |
88 | goto end_element; | |
89 | ||
90 | /* Get start element. */ | |
91 | if (init) | |
92 | m = gfc_match_init_expr (&ar->start[i]); | |
93 | else | |
94 | m = gfc_match_expr (&ar->start[i]); | |
95 | ||
96 | if (m == MATCH_NO) | |
97 | gfc_error ("Expected array subscript at %C"); | |
98 | if (m != MATCH_YES) | |
99 | return MATCH_ERROR; | |
100 | ||
101 | if (gfc_match_char (':') == MATCH_NO) | |
102 | return MATCH_YES; | |
103 | ||
104 | /* Get an optional end element. Because we've seen the colon, we | |
105 | definitely have a range along this dimension. */ | |
106 | end_element: | |
107 | ar->dimen_type[i] = DIMEN_RANGE; | |
108 | ||
109 | if (init) | |
110 | m = gfc_match_init_expr (&ar->end[i]); | |
111 | else | |
112 | m = gfc_match_expr (&ar->end[i]); | |
113 | ||
114 | if (m == MATCH_ERROR) | |
115 | return MATCH_ERROR; | |
116 | ||
117 | /* See if we have an optional stride. */ | |
118 | if (gfc_match_char (':') == MATCH_YES) | |
119 | { | |
120 | m = init ? gfc_match_init_expr (&ar->stride[i]) | |
65f8144a | 121 | : gfc_match_expr (&ar->stride[i]); |
6de9cd9a DN |
122 | |
123 | if (m == MATCH_NO) | |
124 | gfc_error ("Expected array subscript stride at %C"); | |
125 | if (m != MATCH_YES) | |
126 | return MATCH_ERROR; | |
127 | } | |
128 | ||
129 | return MATCH_YES; | |
130 | } | |
131 | ||
132 | ||
133 | /* Match an array reference, whether it is the whole array or a | |
134 | particular elements or a section. If init is set, the reference has | |
135 | to consist of init expressions. */ | |
136 | ||
137 | match | |
65f8144a | 138 | gfc_match_array_ref (gfc_array_ref *ar, gfc_array_spec *as, int init) |
6de9cd9a DN |
139 | { |
140 | match m; | |
141 | ||
142 | memset (ar, '\0', sizeof (ar)); | |
143 | ||
63645982 | 144 | ar->where = gfc_current_locus; |
6de9cd9a DN |
145 | ar->as = as; |
146 | ||
147 | if (gfc_match_char ('(') != MATCH_YES) | |
148 | { | |
149 | ar->type = AR_FULL; | |
150 | ar->dimen = 0; | |
151 | return MATCH_YES; | |
152 | } | |
153 | ||
154 | ar->type = AR_UNKNOWN; | |
155 | ||
156 | for (ar->dimen = 0; ar->dimen < GFC_MAX_DIMENSIONS; ar->dimen++) | |
157 | { | |
158 | m = match_subscript (ar, init); | |
159 | if (m == MATCH_ERROR) | |
160 | goto error; | |
161 | ||
162 | if (gfc_match_char (')') == MATCH_YES) | |
163 | goto matched; | |
164 | ||
165 | if (gfc_match_char (',') != MATCH_YES) | |
166 | { | |
167 | gfc_error ("Invalid form of array reference at %C"); | |
168 | goto error; | |
169 | } | |
170 | } | |
171 | ||
31043f6c FXC |
172 | gfc_error ("Array reference at %C cannot have more than %d dimensions", |
173 | GFC_MAX_DIMENSIONS); | |
6de9cd9a DN |
174 | |
175 | error: | |
176 | return MATCH_ERROR; | |
177 | ||
178 | matched: | |
179 | ar->dimen++; | |
180 | ||
181 | return MATCH_YES; | |
182 | } | |
183 | ||
184 | ||
185 | /************** Array specification matching subroutines ***************/ | |
186 | ||
187 | /* Free all of the expressions associated with array bounds | |
188 | specifications. */ | |
189 | ||
190 | void | |
65f8144a | 191 | gfc_free_array_spec (gfc_array_spec *as) |
6de9cd9a DN |
192 | { |
193 | int i; | |
194 | ||
195 | if (as == NULL) | |
196 | return; | |
197 | ||
198 | for (i = 0; i < as->rank; i++) | |
199 | { | |
200 | gfc_free_expr (as->lower[i]); | |
201 | gfc_free_expr (as->upper[i]); | |
202 | } | |
203 | ||
204 | gfc_free (as); | |
205 | } | |
206 | ||
207 | ||
208 | /* Take an array bound, resolves the expression, that make up the | |
209 | shape and check associated constraints. */ | |
210 | ||
211 | static try | |
65f8144a | 212 | resolve_array_bound (gfc_expr *e, int check_constant) |
6de9cd9a | 213 | { |
6de9cd9a DN |
214 | if (e == NULL) |
215 | return SUCCESS; | |
216 | ||
217 | if (gfc_resolve_expr (e) == FAILURE | |
218 | || gfc_specification_expr (e) == FAILURE) | |
219 | return FAILURE; | |
220 | ||
221 | if (check_constant && gfc_is_constant_expr (e) == 0) | |
222 | { | |
223 | gfc_error ("Variable '%s' at %L in this context must be constant", | |
224 | e->symtree->n.sym->name, &e->where); | |
225 | return FAILURE; | |
226 | } | |
227 | ||
228 | return SUCCESS; | |
229 | } | |
230 | ||
231 | ||
232 | /* Takes an array specification, resolves the expressions that make up | |
233 | the shape and make sure everything is integral. */ | |
234 | ||
235 | try | |
65f8144a | 236 | gfc_resolve_array_spec (gfc_array_spec *as, int check_constant) |
6de9cd9a DN |
237 | { |
238 | gfc_expr *e; | |
239 | int i; | |
240 | ||
241 | if (as == NULL) | |
242 | return SUCCESS; | |
243 | ||
244 | for (i = 0; i < as->rank; i++) | |
245 | { | |
246 | e = as->lower[i]; | |
247 | if (resolve_array_bound (e, check_constant) == FAILURE) | |
248 | return FAILURE; | |
249 | ||
250 | e = as->upper[i]; | |
251 | if (resolve_array_bound (e, check_constant) == FAILURE) | |
252 | return FAILURE; | |
52f56431 PT |
253 | |
254 | if ((as->lower[i] == NULL) || (as->upper[i] == NULL)) | |
255 | continue; | |
256 | ||
257 | /* If the size is negative in this dimension, set it to zero. */ | |
258 | if (as->lower[i]->expr_type == EXPR_CONSTANT | |
259 | && as->upper[i]->expr_type == EXPR_CONSTANT | |
260 | && mpz_cmp (as->upper[i]->value.integer, | |
261 | as->lower[i]->value.integer) < 0) | |
262 | { | |
263 | gfc_free_expr (as->upper[i]); | |
264 | as->upper[i] = gfc_copy_expr (as->lower[i]); | |
265 | mpz_sub_ui (as->upper[i]->value.integer, | |
266 | as->upper[i]->value.integer, 1); | |
267 | } | |
6de9cd9a DN |
268 | } |
269 | ||
270 | return SUCCESS; | |
271 | } | |
272 | ||
273 | ||
274 | /* Match a single array element specification. The return values as | |
275 | well as the upper and lower bounds of the array spec are filled | |
276 | in according to what we see on the input. The caller makes sure | |
277 | individual specifications make sense as a whole. | |
278 | ||
279 | ||
65f8144a SK |
280 | Parsed Lower Upper Returned |
281 | ------------------------------------ | |
b7fdd4ed SK |
282 | : NULL NULL AS_DEFERRED (*) |
283 | x 1 x AS_EXPLICIT | |
284 | x: x NULL AS_ASSUMED_SHAPE | |
285 | x:y x y AS_EXPLICIT | |
286 | x:* x NULL AS_ASSUMED_SIZE | |
287 | * 1 NULL AS_ASSUMED_SIZE | |
6de9cd9a DN |
288 | |
289 | (*) For non-pointer dummy arrays this is AS_ASSUMED_SHAPE. This | |
290 | is fixed during the resolution of formal interfaces. | |
291 | ||
292 | Anything else AS_UNKNOWN. */ | |
293 | ||
294 | static array_type | |
65f8144a | 295 | match_array_element_spec (gfc_array_spec *as) |
6de9cd9a DN |
296 | { |
297 | gfc_expr **upper, **lower; | |
298 | match m; | |
299 | ||
300 | lower = &as->lower[as->rank - 1]; | |
301 | upper = &as->upper[as->rank - 1]; | |
302 | ||
303 | if (gfc_match_char ('*') == MATCH_YES) | |
304 | { | |
305 | *lower = gfc_int_expr (1); | |
306 | return AS_ASSUMED_SIZE; | |
307 | } | |
308 | ||
309 | if (gfc_match_char (':') == MATCH_YES) | |
310 | return AS_DEFERRED; | |
311 | ||
312 | m = gfc_match_expr (upper); | |
313 | if (m == MATCH_NO) | |
314 | gfc_error ("Expected expression in array specification at %C"); | |
315 | if (m != MATCH_YES) | |
316 | return AS_UNKNOWN; | |
317 | ||
318 | if (gfc_match_char (':') == MATCH_NO) | |
319 | { | |
320 | *lower = gfc_int_expr (1); | |
321 | return AS_EXPLICIT; | |
322 | } | |
323 | ||
324 | *lower = *upper; | |
325 | *upper = NULL; | |
326 | ||
327 | if (gfc_match_char ('*') == MATCH_YES) | |
328 | return AS_ASSUMED_SIZE; | |
329 | ||
330 | m = gfc_match_expr (upper); | |
331 | if (m == MATCH_ERROR) | |
332 | return AS_UNKNOWN; | |
333 | if (m == MATCH_NO) | |
334 | return AS_ASSUMED_SHAPE; | |
335 | ||
336 | return AS_EXPLICIT; | |
337 | } | |
338 | ||
339 | ||
340 | /* Matches an array specification, incidentally figuring out what sort | |
341 | it is. */ | |
342 | ||
343 | match | |
65f8144a | 344 | gfc_match_array_spec (gfc_array_spec **asp) |
6de9cd9a DN |
345 | { |
346 | array_type current_type; | |
347 | gfc_array_spec *as; | |
348 | int i; | |
349 | ||
350 | if (gfc_match_char ('(') != MATCH_YES) | |
351 | { | |
352 | *asp = NULL; | |
353 | return MATCH_NO; | |
354 | } | |
355 | ||
356 | as = gfc_get_array_spec (); | |
357 | ||
358 | for (i = 0; i < GFC_MAX_DIMENSIONS; i++) | |
359 | { | |
360 | as->lower[i] = NULL; | |
361 | as->upper[i] = NULL; | |
362 | } | |
363 | ||
364 | as->rank = 1; | |
365 | ||
366 | for (;;) | |
367 | { | |
368 | current_type = match_array_element_spec (as); | |
369 | ||
370 | if (as->rank == 1) | |
371 | { | |
372 | if (current_type == AS_UNKNOWN) | |
373 | goto cleanup; | |
374 | as->type = current_type; | |
375 | } | |
376 | else | |
377 | switch (as->type) | |
65f8144a | 378 | { /* See how current spec meshes with the existing. */ |
6de9cd9a DN |
379 | case AS_UNKNOWN: |
380 | goto cleanup; | |
381 | ||
382 | case AS_EXPLICIT: | |
383 | if (current_type == AS_ASSUMED_SIZE) | |
384 | { | |
385 | as->type = AS_ASSUMED_SIZE; | |
386 | break; | |
387 | } | |
388 | ||
389 | if (current_type == AS_EXPLICIT) | |
390 | break; | |
391 | ||
65f8144a SK |
392 | gfc_error ("Bad array specification for an explicitly shaped " |
393 | "array at %C"); | |
6de9cd9a DN |
394 | |
395 | goto cleanup; | |
396 | ||
397 | case AS_ASSUMED_SHAPE: | |
398 | if ((current_type == AS_ASSUMED_SHAPE) | |
399 | || (current_type == AS_DEFERRED)) | |
400 | break; | |
401 | ||
65f8144a SK |
402 | gfc_error ("Bad array specification for assumed shape " |
403 | "array at %C"); | |
6de9cd9a DN |
404 | goto cleanup; |
405 | ||
406 | case AS_DEFERRED: | |
407 | if (current_type == AS_DEFERRED) | |
408 | break; | |
409 | ||
410 | if (current_type == AS_ASSUMED_SHAPE) | |
411 | { | |
412 | as->type = AS_ASSUMED_SHAPE; | |
413 | break; | |
414 | } | |
415 | ||
416 | gfc_error ("Bad specification for deferred shape array at %C"); | |
417 | goto cleanup; | |
418 | ||
419 | case AS_ASSUMED_SIZE: | |
420 | gfc_error ("Bad specification for assumed size array at %C"); | |
421 | goto cleanup; | |
422 | } | |
423 | ||
424 | if (gfc_match_char (')') == MATCH_YES) | |
425 | break; | |
426 | ||
427 | if (gfc_match_char (',') != MATCH_YES) | |
428 | { | |
429 | gfc_error ("Expected another dimension in array declaration at %C"); | |
430 | goto cleanup; | |
431 | } | |
432 | ||
433 | if (as->rank >= GFC_MAX_DIMENSIONS) | |
434 | { | |
31043f6c FXC |
435 | gfc_error ("Array specification at %C has more than %d dimensions", |
436 | GFC_MAX_DIMENSIONS); | |
6de9cd9a DN |
437 | goto cleanup; |
438 | } | |
439 | ||
a4cd1610 TB |
440 | if (as->rank > 7 |
441 | && gfc_notify_std (GFC_STD_F2008, "Fortran 2008: Array " | |
442 | "specification at %C with more than 7 dimensions") | |
443 | == FAILURE) | |
444 | goto cleanup; | |
445 | ||
6de9cd9a DN |
446 | as->rank++; |
447 | } | |
448 | ||
449 | /* If a lower bounds of an assumed shape array is blank, put in one. */ | |
450 | if (as->type == AS_ASSUMED_SHAPE) | |
451 | { | |
452 | for (i = 0; i < as->rank; i++) | |
453 | { | |
454 | if (as->lower[i] == NULL) | |
455 | as->lower[i] = gfc_int_expr (1); | |
456 | } | |
457 | } | |
458 | *asp = as; | |
459 | return MATCH_YES; | |
460 | ||
461 | cleanup: | |
462 | /* Something went wrong. */ | |
463 | gfc_free_array_spec (as); | |
464 | return MATCH_ERROR; | |
465 | } | |
466 | ||
467 | ||
468 | /* Given a symbol and an array specification, modify the symbol to | |
469 | have that array specification. The error locus is needed in case | |
470 | something goes wrong. On failure, the caller must free the spec. */ | |
471 | ||
472 | try | |
65f8144a | 473 | gfc_set_array_spec (gfc_symbol *sym, gfc_array_spec *as, locus *error_loc) |
6de9cd9a | 474 | { |
6de9cd9a DN |
475 | if (as == NULL) |
476 | return SUCCESS; | |
477 | ||
231b2fcc | 478 | if (gfc_add_dimension (&sym->attr, sym->name, error_loc) == FAILURE) |
6de9cd9a DN |
479 | return FAILURE; |
480 | ||
481 | sym->as = as; | |
482 | ||
483 | return SUCCESS; | |
484 | } | |
485 | ||
486 | ||
487 | /* Copy an array specification. */ | |
488 | ||
489 | gfc_array_spec * | |
65f8144a | 490 | gfc_copy_array_spec (gfc_array_spec *src) |
6de9cd9a DN |
491 | { |
492 | gfc_array_spec *dest; | |
493 | int i; | |
494 | ||
495 | if (src == NULL) | |
496 | return NULL; | |
497 | ||
498 | dest = gfc_get_array_spec (); | |
499 | ||
500 | *dest = *src; | |
501 | ||
502 | for (i = 0; i < dest->rank; i++) | |
503 | { | |
504 | dest->lower[i] = gfc_copy_expr (dest->lower[i]); | |
505 | dest->upper[i] = gfc_copy_expr (dest->upper[i]); | |
506 | } | |
507 | ||
508 | return dest; | |
509 | } | |
510 | ||
65f8144a | 511 | |
6de9cd9a DN |
512 | /* Returns nonzero if the two expressions are equal. Only handles integer |
513 | constants. */ | |
514 | ||
515 | static int | |
65f8144a | 516 | compare_bounds (gfc_expr *bound1, gfc_expr *bound2) |
6de9cd9a DN |
517 | { |
518 | if (bound1 == NULL || bound2 == NULL | |
519 | || bound1->expr_type != EXPR_CONSTANT | |
520 | || bound2->expr_type != EXPR_CONSTANT | |
521 | || bound1->ts.type != BT_INTEGER | |
522 | || bound2->ts.type != BT_INTEGER) | |
523 | gfc_internal_error ("gfc_compare_array_spec(): Array spec clobbered"); | |
524 | ||
525 | if (mpz_cmp (bound1->value.integer, bound2->value.integer) == 0) | |
526 | return 1; | |
527 | else | |
528 | return 0; | |
529 | } | |
530 | ||
65f8144a | 531 | |
6de9cd9a DN |
532 | /* Compares two array specifications. They must be constant or deferred |
533 | shape. */ | |
534 | ||
535 | int | |
65f8144a | 536 | gfc_compare_array_spec (gfc_array_spec *as1, gfc_array_spec *as2) |
6de9cd9a DN |
537 | { |
538 | int i; | |
539 | ||
540 | if (as1 == NULL && as2 == NULL) | |
541 | return 1; | |
542 | ||
543 | if (as1 == NULL || as2 == NULL) | |
544 | return 0; | |
545 | ||
546 | if (as1->rank != as2->rank) | |
547 | return 0; | |
548 | ||
549 | if (as1->rank == 0) | |
550 | return 1; | |
551 | ||
552 | if (as1->type != as2->type) | |
553 | return 0; | |
554 | ||
555 | if (as1->type == AS_EXPLICIT) | |
556 | for (i = 0; i < as1->rank; i++) | |
557 | { | |
558 | if (compare_bounds (as1->lower[i], as2->lower[i]) == 0) | |
559 | return 0; | |
560 | ||
561 | if (compare_bounds (as1->upper[i], as2->upper[i]) == 0) | |
562 | return 0; | |
563 | } | |
564 | ||
565 | return 1; | |
566 | } | |
567 | ||
568 | ||
569 | /****************** Array constructor functions ******************/ | |
570 | ||
571 | /* Start an array constructor. The constructor starts with zero | |
572 | elements and should be appended to by gfc_append_constructor(). */ | |
573 | ||
574 | gfc_expr * | |
65f8144a | 575 | gfc_start_constructor (bt type, int kind, locus *where) |
6de9cd9a DN |
576 | { |
577 | gfc_expr *result; | |
578 | ||
579 | result = gfc_get_expr (); | |
580 | ||
581 | result->expr_type = EXPR_ARRAY; | |
582 | result->rank = 1; | |
583 | ||
584 | result->ts.type = type; | |
585 | result->ts.kind = kind; | |
586 | result->where = *where; | |
587 | return result; | |
588 | } | |
589 | ||
590 | ||
591 | /* Given an array constructor expression, append the new expression | |
592 | node onto the constructor. */ | |
593 | ||
594 | void | |
65f8144a | 595 | gfc_append_constructor (gfc_expr *base, gfc_expr *new) |
6de9cd9a DN |
596 | { |
597 | gfc_constructor *c; | |
598 | ||
599 | if (base->value.constructor == NULL) | |
600 | base->value.constructor = c = gfc_get_constructor (); | |
601 | else | |
602 | { | |
603 | c = base->value.constructor; | |
604 | while (c->next) | |
605 | c = c->next; | |
606 | ||
607 | c->next = gfc_get_constructor (); | |
608 | c = c->next; | |
609 | } | |
610 | ||
611 | c->expr = new; | |
612 | ||
613 | if (new->ts.type != base->ts.type || new->ts.kind != base->ts.kind) | |
614 | gfc_internal_error ("gfc_append_constructor(): New node has wrong kind"); | |
615 | } | |
616 | ||
617 | ||
618 | /* Given an array constructor expression, insert the new expression's | |
619 | constructor onto the base's one according to the offset. */ | |
620 | ||
621 | void | |
65f8144a | 622 | gfc_insert_constructor (gfc_expr *base, gfc_constructor *c1) |
6de9cd9a DN |
623 | { |
624 | gfc_constructor *c, *pre; | |
625 | expr_t type; | |
da4f9e3b | 626 | int t; |
6de9cd9a DN |
627 | |
628 | type = base->expr_type; | |
629 | ||
630 | if (base->value.constructor == NULL) | |
631 | base->value.constructor = c1; | |
632 | else | |
633 | { | |
634 | c = pre = base->value.constructor; | |
635 | while (c) | |
65f8144a SK |
636 | { |
637 | if (type == EXPR_ARRAY) | |
638 | { | |
da4f9e3b | 639 | t = mpz_cmp (c->n.offset, c1->n.offset); |
65f8144a SK |
640 | if (t < 0) |
641 | { | |
642 | pre = c; | |
643 | c = c->next; | |
644 | } | |
645 | else if (t == 0) | |
646 | { | |
647 | gfc_error ("duplicated initializer"); | |
648 | break; | |
649 | } | |
650 | else | |
651 | break; | |
652 | } | |
653 | else | |
654 | { | |
655 | pre = c; | |
656 | c = c->next; | |
657 | } | |
658 | } | |
6de9cd9a DN |
659 | |
660 | if (pre != c) | |
65f8144a SK |
661 | { |
662 | pre->next = c1; | |
663 | c1->next = c; | |
664 | } | |
6de9cd9a | 665 | else |
65f8144a SK |
666 | { |
667 | c1->next = c; | |
668 | base->value.constructor = c1; | |
669 | } | |
6de9cd9a DN |
670 | } |
671 | } | |
672 | ||
673 | ||
674 | /* Get a new constructor. */ | |
675 | ||
676 | gfc_constructor * | |
677 | gfc_get_constructor (void) | |
678 | { | |
679 | gfc_constructor *c; | |
680 | ||
681 | c = gfc_getmem (sizeof(gfc_constructor)); | |
682 | c->expr = NULL; | |
683 | c->iterator = NULL; | |
684 | c->next = NULL; | |
685 | mpz_init_set_si (c->n.offset, 0); | |
686 | mpz_init_set_si (c->repeat, 0); | |
687 | return c; | |
688 | } | |
689 | ||
690 | ||
691 | /* Free chains of gfc_constructor structures. */ | |
692 | ||
693 | void | |
65f8144a | 694 | gfc_free_constructor (gfc_constructor *p) |
6de9cd9a DN |
695 | { |
696 | gfc_constructor *next; | |
697 | ||
698 | if (p == NULL) | |
699 | return; | |
700 | ||
701 | for (; p; p = next) | |
702 | { | |
703 | next = p->next; | |
704 | ||
705 | if (p->expr) | |
65f8144a | 706 | gfc_free_expr (p->expr); |
6de9cd9a DN |
707 | if (p->iterator != NULL) |
708 | gfc_free_iterator (p->iterator, 1); | |
709 | mpz_clear (p->n.offset); | |
710 | mpz_clear (p->repeat); | |
711 | gfc_free (p); | |
712 | } | |
713 | } | |
714 | ||
715 | ||
716 | /* Given an expression node that might be an array constructor and a | |
717 | symbol, make sure that no iterators in this or child constructors | |
718 | use the symbol as an implied-DO iterator. Returns nonzero if a | |
719 | duplicate was found. */ | |
720 | ||
721 | static int | |
65f8144a | 722 | check_duplicate_iterator (gfc_constructor *c, gfc_symbol *master) |
6de9cd9a DN |
723 | { |
724 | gfc_expr *e; | |
725 | ||
726 | for (; c; c = c->next) | |
727 | { | |
728 | e = c->expr; | |
729 | ||
730 | if (e->expr_type == EXPR_ARRAY | |
731 | && check_duplicate_iterator (e->value.constructor, master)) | |
732 | return 1; | |
733 | ||
734 | if (c->iterator == NULL) | |
735 | continue; | |
736 | ||
737 | if (c->iterator->var->symtree->n.sym == master) | |
738 | { | |
65f8144a SK |
739 | gfc_error ("DO-iterator '%s' at %L is inside iterator of the " |
740 | "same name", master->name, &c->where); | |
6de9cd9a DN |
741 | |
742 | return 1; | |
743 | } | |
744 | } | |
745 | ||
746 | return 0; | |
747 | } | |
748 | ||
749 | ||
750 | /* Forward declaration because these functions are mutually recursive. */ | |
751 | static match match_array_cons_element (gfc_constructor **); | |
752 | ||
753 | /* Match a list of array elements. */ | |
754 | ||
755 | static match | |
65f8144a | 756 | match_array_list (gfc_constructor **result) |
6de9cd9a DN |
757 | { |
758 | gfc_constructor *p, *head, *tail, *new; | |
759 | gfc_iterator iter; | |
760 | locus old_loc; | |
761 | gfc_expr *e; | |
762 | match m; | |
763 | int n; | |
764 | ||
63645982 | 765 | old_loc = gfc_current_locus; |
6de9cd9a DN |
766 | |
767 | if (gfc_match_char ('(') == MATCH_NO) | |
768 | return MATCH_NO; | |
769 | ||
770 | memset (&iter, '\0', sizeof (gfc_iterator)); | |
771 | head = NULL; | |
772 | ||
773 | m = match_array_cons_element (&head); | |
774 | if (m != MATCH_YES) | |
775 | goto cleanup; | |
776 | ||
777 | tail = head; | |
778 | ||
779 | if (gfc_match_char (',') != MATCH_YES) | |
780 | { | |
781 | m = MATCH_NO; | |
782 | goto cleanup; | |
783 | } | |
784 | ||
785 | for (n = 1;; n++) | |
786 | { | |
787 | m = gfc_match_iterator (&iter, 0); | |
788 | if (m == MATCH_YES) | |
789 | break; | |
790 | if (m == MATCH_ERROR) | |
791 | goto cleanup; | |
792 | ||
793 | m = match_array_cons_element (&new); | |
794 | if (m == MATCH_ERROR) | |
795 | goto cleanup; | |
796 | if (m == MATCH_NO) | |
797 | { | |
798 | if (n > 2) | |
799 | goto syntax; | |
800 | m = MATCH_NO; | |
801 | goto cleanup; /* Could be a complex constant */ | |
802 | } | |
803 | ||
804 | tail->next = new; | |
805 | tail = new; | |
806 | ||
807 | if (gfc_match_char (',') != MATCH_YES) | |
808 | { | |
809 | if (n > 2) | |
810 | goto syntax; | |
811 | m = MATCH_NO; | |
812 | goto cleanup; | |
813 | } | |
814 | } | |
815 | ||
816 | if (gfc_match_char (')') != MATCH_YES) | |
817 | goto syntax; | |
818 | ||
819 | if (check_duplicate_iterator (head, iter.var->symtree->n.sym)) | |
820 | { | |
821 | m = MATCH_ERROR; | |
822 | goto cleanup; | |
823 | } | |
824 | ||
825 | e = gfc_get_expr (); | |
826 | e->expr_type = EXPR_ARRAY; | |
827 | e->where = old_loc; | |
828 | e->value.constructor = head; | |
829 | ||
830 | p = gfc_get_constructor (); | |
63645982 | 831 | p->where = gfc_current_locus; |
6de9cd9a DN |
832 | p->iterator = gfc_get_iterator (); |
833 | *p->iterator = iter; | |
834 | ||
835 | p->expr = e; | |
836 | *result = p; | |
837 | ||
838 | return MATCH_YES; | |
839 | ||
840 | syntax: | |
841 | gfc_error ("Syntax error in array constructor at %C"); | |
842 | m = MATCH_ERROR; | |
843 | ||
844 | cleanup: | |
845 | gfc_free_constructor (head); | |
846 | gfc_free_iterator (&iter, 0); | |
63645982 | 847 | gfc_current_locus = old_loc; |
6de9cd9a DN |
848 | return m; |
849 | } | |
850 | ||
851 | ||
852 | /* Match a single element of an array constructor, which can be a | |
853 | single expression or a list of elements. */ | |
854 | ||
855 | static match | |
65f8144a | 856 | match_array_cons_element (gfc_constructor **result) |
6de9cd9a DN |
857 | { |
858 | gfc_constructor *p; | |
859 | gfc_expr *expr; | |
860 | match m; | |
861 | ||
862 | m = match_array_list (result); | |
863 | if (m != MATCH_NO) | |
864 | return m; | |
865 | ||
866 | m = gfc_match_expr (&expr); | |
867 | if (m != MATCH_YES) | |
868 | return m; | |
869 | ||
870 | p = gfc_get_constructor (); | |
63645982 | 871 | p->where = gfc_current_locus; |
6de9cd9a DN |
872 | p->expr = expr; |
873 | ||
874 | *result = p; | |
875 | return MATCH_YES; | |
876 | } | |
877 | ||
878 | ||
879 | /* Match an array constructor. */ | |
880 | ||
881 | match | |
65f8144a | 882 | gfc_match_array_constructor (gfc_expr **result) |
6de9cd9a DN |
883 | { |
884 | gfc_constructor *head, *tail, *new; | |
885 | gfc_expr *expr; | |
c03fc95d | 886 | gfc_typespec ts; |
6de9cd9a DN |
887 | locus where; |
888 | match m; | |
acc75ae3 | 889 | const char *end_delim; |
c03fc95d | 890 | bool seen_ts; |
6de9cd9a DN |
891 | |
892 | if (gfc_match (" (/") == MATCH_NO) | |
acc75ae3 EE |
893 | { |
894 | if (gfc_match (" [") == MATCH_NO) | |
65f8144a | 895 | return MATCH_NO; |
acc75ae3 | 896 | else |
65f8144a SK |
897 | { |
898 | if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: [...] " | |
899 | "style array constructors at %C") == FAILURE) | |
900 | return MATCH_ERROR; | |
901 | end_delim = " ]"; | |
902 | } | |
acc75ae3 EE |
903 | } |
904 | else | |
905 | end_delim = " /)"; | |
6de9cd9a | 906 | |
63645982 | 907 | where = gfc_current_locus; |
6de9cd9a | 908 | head = tail = NULL; |
c03fc95d DK |
909 | seen_ts = false; |
910 | ||
911 | /* Try to match an optional "type-spec ::" */ | |
912 | if (gfc_match_type_spec (&ts, 0) == MATCH_YES) | |
913 | { | |
914 | seen_ts = (gfc_match (" ::") == MATCH_YES); | |
915 | ||
916 | if (seen_ts) | |
917 | { | |
918 | if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: Array constructor " | |
919 | "including type specification at %C") == FAILURE) | |
920 | goto cleanup; | |
921 | } | |
922 | } | |
923 | ||
924 | if (! seen_ts) | |
925 | gfc_current_locus = where; | |
6de9cd9a | 926 | |
acc75ae3 | 927 | if (gfc_match (end_delim) == MATCH_YES) |
ab21e272 | 928 | { |
c03fc95d DK |
929 | if (seen_ts) |
930 | goto done; | |
931 | else | |
932 | { | |
933 | gfc_error ("Empty array constructor at %C is not allowed"); | |
934 | goto cleanup; | |
935 | } | |
ab21e272 | 936 | } |
6de9cd9a DN |
937 | |
938 | for (;;) | |
939 | { | |
940 | m = match_array_cons_element (&new); | |
941 | if (m == MATCH_ERROR) | |
942 | goto cleanup; | |
943 | if (m == MATCH_NO) | |
944 | goto syntax; | |
945 | ||
946 | if (head == NULL) | |
947 | head = new; | |
948 | else | |
949 | tail->next = new; | |
950 | ||
951 | tail = new; | |
952 | ||
953 | if (gfc_match_char (',') == MATCH_NO) | |
954 | break; | |
955 | } | |
956 | ||
acc75ae3 | 957 | if (gfc_match (end_delim) == MATCH_NO) |
6de9cd9a DN |
958 | goto syntax; |
959 | ||
c03fc95d | 960 | done: |
6de9cd9a DN |
961 | expr = gfc_get_expr (); |
962 | ||
963 | expr->expr_type = EXPR_ARRAY; | |
964 | ||
965 | expr->value.constructor = head; | |
966 | /* Size must be calculated at resolution time. */ | |
967 | ||
c03fc95d DK |
968 | if (seen_ts) |
969 | expr->ts = ts; | |
970 | else | |
971 | expr->ts.type = BT_UNKNOWN; | |
972 | ||
973 | if (expr->ts.cl) | |
974 | expr->ts.cl->length_from_typespec = seen_ts; | |
975 | ||
6de9cd9a DN |
976 | expr->where = where; |
977 | expr->rank = 1; | |
978 | ||
979 | *result = expr; | |
980 | return MATCH_YES; | |
981 | ||
982 | syntax: | |
983 | gfc_error ("Syntax error in array constructor at %C"); | |
984 | ||
985 | cleanup: | |
986 | gfc_free_constructor (head); | |
987 | return MATCH_ERROR; | |
988 | } | |
989 | ||
990 | ||
991 | ||
992 | /************** Check array constructors for correctness **************/ | |
993 | ||
994 | /* Given an expression, compare it's type with the type of the current | |
995 | constructor. Returns nonzero if an error was issued. The | |
996 | cons_state variable keeps track of whether the type of the | |
997 | constructor being read or resolved is known to be good, bad or just | |
998 | starting out. */ | |
999 | ||
1000 | static gfc_typespec constructor_ts; | |
1001 | static enum | |
1002 | { CONS_START, CONS_GOOD, CONS_BAD } | |
1003 | cons_state; | |
1004 | ||
1005 | static int | |
c03fc95d | 1006 | check_element_type (gfc_expr *expr, bool convert) |
6de9cd9a | 1007 | { |
6de9cd9a | 1008 | if (cons_state == CONS_BAD) |
1f2959f0 | 1009 | return 0; /* Suppress further errors */ |
6de9cd9a DN |
1010 | |
1011 | if (cons_state == CONS_START) | |
1012 | { | |
1013 | if (expr->ts.type == BT_UNKNOWN) | |
1014 | cons_state = CONS_BAD; | |
1015 | else | |
1016 | { | |
1017 | cons_state = CONS_GOOD; | |
1018 | constructor_ts = expr->ts; | |
1019 | } | |
1020 | ||
1021 | return 0; | |
1022 | } | |
1023 | ||
1024 | if (gfc_compare_types (&constructor_ts, &expr->ts)) | |
1025 | return 0; | |
1026 | ||
c03fc95d DK |
1027 | if (convert) |
1028 | return gfc_convert_type (expr, &constructor_ts, 1) == SUCCESS ? 0 : 1; | |
1029 | ||
6de9cd9a DN |
1030 | gfc_error ("Element in %s array constructor at %L is %s", |
1031 | gfc_typename (&constructor_ts), &expr->where, | |
1032 | gfc_typename (&expr->ts)); | |
1033 | ||
1034 | cons_state = CONS_BAD; | |
1035 | return 1; | |
1036 | } | |
1037 | ||
1038 | ||
f7b529fa | 1039 | /* Recursive work function for gfc_check_constructor_type(). */ |
6de9cd9a DN |
1040 | |
1041 | static try | |
c03fc95d | 1042 | check_constructor_type (gfc_constructor *c, bool convert) |
6de9cd9a DN |
1043 | { |
1044 | gfc_expr *e; | |
1045 | ||
1046 | for (; c; c = c->next) | |
1047 | { | |
1048 | e = c->expr; | |
1049 | ||
1050 | if (e->expr_type == EXPR_ARRAY) | |
1051 | { | |
c03fc95d | 1052 | if (check_constructor_type (e->value.constructor, convert) == FAILURE) |
6de9cd9a DN |
1053 | return FAILURE; |
1054 | ||
1055 | continue; | |
1056 | } | |
1057 | ||
c03fc95d | 1058 | if (check_element_type (e, convert)) |
6de9cd9a DN |
1059 | return FAILURE; |
1060 | } | |
1061 | ||
1062 | return SUCCESS; | |
1063 | } | |
1064 | ||
1065 | ||
1066 | /* Check that all elements of an array constructor are the same type. | |
1067 | On FAILURE, an error has been generated. */ | |
1068 | ||
1069 | try | |
65f8144a | 1070 | gfc_check_constructor_type (gfc_expr *e) |
6de9cd9a DN |
1071 | { |
1072 | try t; | |
1073 | ||
c03fc95d DK |
1074 | if (e->ts.type != BT_UNKNOWN) |
1075 | { | |
1076 | cons_state = CONS_GOOD; | |
1077 | constructor_ts = e->ts; | |
1078 | } | |
1079 | else | |
1080 | { | |
1081 | cons_state = CONS_START; | |
1082 | gfc_clear_ts (&constructor_ts); | |
1083 | } | |
6de9cd9a | 1084 | |
c03fc95d DK |
1085 | /* If e->ts.type != BT_UNKNOWN, the array constructor included a |
1086 | typespec, and we will now convert the values on the fly. */ | |
1087 | t = check_constructor_type (e->value.constructor, e->ts.type != BT_UNKNOWN); | |
6de9cd9a DN |
1088 | if (t == SUCCESS && e->ts.type == BT_UNKNOWN) |
1089 | e->ts = constructor_ts; | |
1090 | ||
1091 | return t; | |
1092 | } | |
1093 | ||
1094 | ||
1095 | ||
1096 | typedef struct cons_stack | |
1097 | { | |
1098 | gfc_iterator *iterator; | |
1099 | struct cons_stack *previous; | |
1100 | } | |
1101 | cons_stack; | |
1102 | ||
1103 | static cons_stack *base; | |
1104 | ||
65f8144a | 1105 | static try check_constructor (gfc_constructor *, try (*) (gfc_expr *)); |
6de9cd9a DN |
1106 | |
1107 | /* Check an EXPR_VARIABLE expression in a constructor to make sure | |
1108 | that that variable is an iteration variables. */ | |
1109 | ||
1110 | try | |
65f8144a | 1111 | gfc_check_iter_variable (gfc_expr *expr) |
6de9cd9a | 1112 | { |
6de9cd9a DN |
1113 | gfc_symbol *sym; |
1114 | cons_stack *c; | |
1115 | ||
1116 | sym = expr->symtree->n.sym; | |
1117 | ||
1118 | for (c = base; c; c = c->previous) | |
1119 | if (sym == c->iterator->var->symtree->n.sym) | |
1120 | return SUCCESS; | |
1121 | ||
1122 | return FAILURE; | |
1123 | } | |
1124 | ||
1125 | ||
1126 | /* Recursive work function for gfc_check_constructor(). This amounts | |
1127 | to calling the check function for each expression in the | |
1128 | constructor, giving variables with the names of iterators a pass. */ | |
1129 | ||
1130 | static try | |
65f8144a | 1131 | check_constructor (gfc_constructor *c, try (*check_function) (gfc_expr *)) |
6de9cd9a DN |
1132 | { |
1133 | cons_stack element; | |
1134 | gfc_expr *e; | |
1135 | try t; | |
1136 | ||
1137 | for (; c; c = c->next) | |
1138 | { | |
1139 | e = c->expr; | |
1140 | ||
1141 | if (e->expr_type != EXPR_ARRAY) | |
1142 | { | |
1143 | if ((*check_function) (e) == FAILURE) | |
1144 | return FAILURE; | |
1145 | continue; | |
1146 | } | |
1147 | ||
1148 | element.previous = base; | |
1149 | element.iterator = c->iterator; | |
1150 | ||
1151 | base = &element; | |
1152 | t = check_constructor (e->value.constructor, check_function); | |
1153 | base = element.previous; | |
1154 | ||
1155 | if (t == FAILURE) | |
1156 | return FAILURE; | |
1157 | } | |
1158 | ||
1159 | /* Nothing went wrong, so all OK. */ | |
1160 | return SUCCESS; | |
1161 | } | |
1162 | ||
1163 | ||
1164 | /* Checks a constructor to see if it is a particular kind of | |
1165 | expression -- specification, restricted, or initialization as | |
1166 | determined by the check_function. */ | |
1167 | ||
1168 | try | |
65f8144a | 1169 | gfc_check_constructor (gfc_expr *expr, try (*check_function) (gfc_expr *)) |
6de9cd9a DN |
1170 | { |
1171 | cons_stack *base_save; | |
1172 | try t; | |
1173 | ||
1174 | base_save = base; | |
1175 | base = NULL; | |
1176 | ||
1177 | t = check_constructor (expr->value.constructor, check_function); | |
1178 | base = base_save; | |
1179 | ||
1180 | return t; | |
1181 | } | |
1182 | ||
1183 | ||
1184 | ||
1185 | /**************** Simplification of array constructors ****************/ | |
1186 | ||
1187 | iterator_stack *iter_stack; | |
1188 | ||
1189 | typedef struct | |
1190 | { | |
1191 | gfc_constructor *new_head, *new_tail; | |
1192 | int extract_count, extract_n; | |
1193 | gfc_expr *extracted; | |
1194 | mpz_t *count; | |
1195 | ||
1196 | mpz_t *offset; | |
1197 | gfc_component *component; | |
1198 | mpz_t *repeat; | |
1199 | ||
1200 | try (*expand_work_function) (gfc_expr *); | |
1201 | } | |
1202 | expand_info; | |
1203 | ||
1204 | static expand_info current_expand; | |
1205 | ||
1206 | static try expand_constructor (gfc_constructor *); | |
1207 | ||
1208 | ||
1209 | /* Work function that counts the number of elements present in a | |
1210 | constructor. */ | |
1211 | ||
1212 | static try | |
65f8144a | 1213 | count_elements (gfc_expr *e) |
6de9cd9a DN |
1214 | { |
1215 | mpz_t result; | |
1216 | ||
1217 | if (e->rank == 0) | |
1218 | mpz_add_ui (*current_expand.count, *current_expand.count, 1); | |
1219 | else | |
1220 | { | |
1221 | if (gfc_array_size (e, &result) == FAILURE) | |
1222 | { | |
1223 | gfc_free_expr (e); | |
1224 | return FAILURE; | |
1225 | } | |
1226 | ||
1227 | mpz_add (*current_expand.count, *current_expand.count, result); | |
1228 | mpz_clear (result); | |
1229 | } | |
1230 | ||
1231 | gfc_free_expr (e); | |
1232 | return SUCCESS; | |
1233 | } | |
1234 | ||
1235 | ||
1236 | /* Work function that extracts a particular element from an array | |
1237 | constructor, freeing the rest. */ | |
1238 | ||
1239 | static try | |
65f8144a | 1240 | extract_element (gfc_expr *e) |
6de9cd9a DN |
1241 | { |
1242 | ||
1243 | if (e->rank != 0) | |
1244 | { /* Something unextractable */ | |
1245 | gfc_free_expr (e); | |
1246 | return FAILURE; | |
1247 | } | |
1248 | ||
1249 | if (current_expand.extract_count == current_expand.extract_n) | |
1250 | current_expand.extracted = e; | |
1251 | else | |
1252 | gfc_free_expr (e); | |
1253 | ||
1254 | current_expand.extract_count++; | |
1255 | return SUCCESS; | |
1256 | } | |
1257 | ||
1258 | ||
1259 | /* Work function that constructs a new constructor out of the old one, | |
1260 | stringing new elements together. */ | |
1261 | ||
1262 | static try | |
65f8144a | 1263 | expand (gfc_expr *e) |
6de9cd9a | 1264 | { |
6de9cd9a DN |
1265 | if (current_expand.new_head == NULL) |
1266 | current_expand.new_head = current_expand.new_tail = | |
1267 | gfc_get_constructor (); | |
1268 | else | |
1269 | { | |
1270 | current_expand.new_tail->next = gfc_get_constructor (); | |
1271 | current_expand.new_tail = current_expand.new_tail->next; | |
1272 | } | |
1273 | ||
1274 | current_expand.new_tail->where = e->where; | |
1275 | current_expand.new_tail->expr = e; | |
1276 | ||
1277 | mpz_set (current_expand.new_tail->n.offset, *current_expand.offset); | |
1278 | current_expand.new_tail->n.component = current_expand.component; | |
1279 | mpz_set (current_expand.new_tail->repeat, *current_expand.repeat); | |
1280 | return SUCCESS; | |
1281 | } | |
1282 | ||
1283 | ||
1284 | /* Given an initialization expression that is a variable reference, | |
1285 | substitute the current value of the iteration variable. */ | |
1286 | ||
1287 | void | |
65f8144a | 1288 | gfc_simplify_iterator_var (gfc_expr *e) |
6de9cd9a DN |
1289 | { |
1290 | iterator_stack *p; | |
1291 | ||
1292 | for (p = iter_stack; p; p = p->prev) | |
1293 | if (e->symtree == p->variable) | |
1294 | break; | |
1295 | ||
1296 | if (p == NULL) | |
1297 | return; /* Variable not found */ | |
1298 | ||
1299 | gfc_replace_expr (e, gfc_int_expr (0)); | |
1300 | ||
1301 | mpz_set (e->value.integer, p->value); | |
1302 | ||
1303 | return; | |
1304 | } | |
1305 | ||
1306 | ||
1307 | /* Expand an expression with that is inside of a constructor, | |
1308 | recursing into other constructors if present. */ | |
1309 | ||
1310 | static try | |
65f8144a | 1311 | expand_expr (gfc_expr *e) |
6de9cd9a | 1312 | { |
6de9cd9a DN |
1313 | if (e->expr_type == EXPR_ARRAY) |
1314 | return expand_constructor (e->value.constructor); | |
1315 | ||
1316 | e = gfc_copy_expr (e); | |
1317 | ||
1318 | if (gfc_simplify_expr (e, 1) == FAILURE) | |
1319 | { | |
1320 | gfc_free_expr (e); | |
1321 | return FAILURE; | |
1322 | } | |
1323 | ||
1324 | return current_expand.expand_work_function (e); | |
1325 | } | |
1326 | ||
1327 | ||
1328 | static try | |
65f8144a | 1329 | expand_iterator (gfc_constructor *c) |
6de9cd9a DN |
1330 | { |
1331 | gfc_expr *start, *end, *step; | |
1332 | iterator_stack frame; | |
1333 | mpz_t trip; | |
1334 | try t; | |
1335 | ||
1336 | end = step = NULL; | |
1337 | ||
1338 | t = FAILURE; | |
1339 | ||
1340 | mpz_init (trip); | |
1341 | mpz_init (frame.value); | |
66914102 | 1342 | frame.prev = NULL; |
6de9cd9a DN |
1343 | |
1344 | start = gfc_copy_expr (c->iterator->start); | |
1345 | if (gfc_simplify_expr (start, 1) == FAILURE) | |
1346 | goto cleanup; | |
1347 | ||
1348 | if (start->expr_type != EXPR_CONSTANT || start->ts.type != BT_INTEGER) | |
1349 | goto cleanup; | |
1350 | ||
1351 | end = gfc_copy_expr (c->iterator->end); | |
1352 | if (gfc_simplify_expr (end, 1) == FAILURE) | |
1353 | goto cleanup; | |
1354 | ||
1355 | if (end->expr_type != EXPR_CONSTANT || end->ts.type != BT_INTEGER) | |
1356 | goto cleanup; | |
1357 | ||
1358 | step = gfc_copy_expr (c->iterator->step); | |
1359 | if (gfc_simplify_expr (step, 1) == FAILURE) | |
1360 | goto cleanup; | |
1361 | ||
1362 | if (step->expr_type != EXPR_CONSTANT || step->ts.type != BT_INTEGER) | |
1363 | goto cleanup; | |
1364 | ||
1365 | if (mpz_sgn (step->value.integer) == 0) | |
1366 | { | |
1367 | gfc_error ("Iterator step at %L cannot be zero", &step->where); | |
1368 | goto cleanup; | |
1369 | } | |
1370 | ||
1371 | /* Calculate the trip count of the loop. */ | |
1372 | mpz_sub (trip, end->value.integer, start->value.integer); | |
1373 | mpz_add (trip, trip, step->value.integer); | |
1374 | mpz_tdiv_q (trip, trip, step->value.integer); | |
1375 | ||
1376 | mpz_set (frame.value, start->value.integer); | |
1377 | ||
1378 | frame.prev = iter_stack; | |
1379 | frame.variable = c->iterator->var->symtree; | |
1380 | iter_stack = &frame; | |
1381 | ||
1382 | while (mpz_sgn (trip) > 0) | |
1383 | { | |
1384 | if (expand_expr (c->expr) == FAILURE) | |
1385 | goto cleanup; | |
1386 | ||
1387 | mpz_add (frame.value, frame.value, step->value.integer); | |
1388 | mpz_sub_ui (trip, trip, 1); | |
1389 | } | |
1390 | ||
1391 | t = SUCCESS; | |
1392 | ||
1393 | cleanup: | |
1394 | gfc_free_expr (start); | |
1395 | gfc_free_expr (end); | |
1396 | gfc_free_expr (step); | |
1397 | ||
1398 | mpz_clear (trip); | |
1399 | mpz_clear (frame.value); | |
1400 | ||
1401 | iter_stack = frame.prev; | |
1402 | ||
1403 | return t; | |
1404 | } | |
1405 | ||
1406 | ||
1407 | /* Expand a constructor into constant constructors without any | |
1408 | iterators, calling the work function for each of the expanded | |
1409 | expressions. The work function needs to either save or free the | |
1410 | passed expression. */ | |
1411 | ||
1412 | static try | |
65f8144a | 1413 | expand_constructor (gfc_constructor *c) |
6de9cd9a DN |
1414 | { |
1415 | gfc_expr *e; | |
1416 | ||
1417 | for (; c; c = c->next) | |
1418 | { | |
1419 | if (c->iterator != NULL) | |
1420 | { | |
1421 | if (expand_iterator (c) == FAILURE) | |
1422 | return FAILURE; | |
1423 | continue; | |
1424 | } | |
1425 | ||
1426 | e = c->expr; | |
1427 | ||
1428 | if (e->expr_type == EXPR_ARRAY) | |
1429 | { | |
1430 | if (expand_constructor (e->value.constructor) == FAILURE) | |
1431 | return FAILURE; | |
1432 | ||
1433 | continue; | |
1434 | } | |
1435 | ||
1436 | e = gfc_copy_expr (e); | |
1437 | if (gfc_simplify_expr (e, 1) == FAILURE) | |
1438 | { | |
1439 | gfc_free_expr (e); | |
1440 | return FAILURE; | |
1441 | } | |
1442 | current_expand.offset = &c->n.offset; | |
1443 | current_expand.component = c->n.component; | |
1444 | current_expand.repeat = &c->repeat; | |
1445 | if (current_expand.expand_work_function (e) == FAILURE) | |
1446 | return FAILURE; | |
1447 | } | |
1448 | return SUCCESS; | |
1449 | } | |
1450 | ||
1451 | ||
1452 | /* Top level subroutine for expanding constructors. We only expand | |
1453 | constructor if they are small enough. */ | |
1454 | ||
1455 | try | |
65f8144a | 1456 | gfc_expand_constructor (gfc_expr *e) |
6de9cd9a DN |
1457 | { |
1458 | expand_info expand_save; | |
1459 | gfc_expr *f; | |
1460 | try rc; | |
1461 | ||
1462 | f = gfc_get_array_element (e, GFC_MAX_AC_EXPAND); | |
1463 | if (f != NULL) | |
1464 | { | |
1465 | gfc_free_expr (f); | |
1466 | return SUCCESS; | |
1467 | } | |
1468 | ||
1469 | expand_save = current_expand; | |
1470 | current_expand.new_head = current_expand.new_tail = NULL; | |
1471 | ||
1472 | iter_stack = NULL; | |
1473 | ||
1474 | current_expand.expand_work_function = expand; | |
1475 | ||
1476 | if (expand_constructor (e->value.constructor) == FAILURE) | |
1477 | { | |
1478 | gfc_free_constructor (current_expand.new_head); | |
1479 | rc = FAILURE; | |
1480 | goto done; | |
1481 | } | |
1482 | ||
1483 | gfc_free_constructor (e->value.constructor); | |
1484 | e->value.constructor = current_expand.new_head; | |
1485 | ||
1486 | rc = SUCCESS; | |
1487 | ||
1488 | done: | |
1489 | current_expand = expand_save; | |
1490 | ||
1491 | return rc; | |
1492 | } | |
1493 | ||
1494 | ||
1495 | /* Work function for checking that an element of a constructor is a | |
1496 | constant, after removal of any iteration variables. We return | |
1497 | FAILURE if not so. */ | |
1498 | ||
1499 | static try | |
65f8144a | 1500 | constant_element (gfc_expr *e) |
6de9cd9a DN |
1501 | { |
1502 | int rv; | |
1503 | ||
1504 | rv = gfc_is_constant_expr (e); | |
1505 | gfc_free_expr (e); | |
1506 | ||
1507 | return rv ? SUCCESS : FAILURE; | |
1508 | } | |
1509 | ||
1510 | ||
1511 | /* Given an array constructor, determine if the constructor is | |
1512 | constant or not by expanding it and making sure that all elements | |
1513 | are constants. This is a bit of a hack since something like (/ (i, | |
1514 | i=1,100000000) /) will take a while as* opposed to a more clever | |
1515 | function that traverses the expression tree. FIXME. */ | |
1516 | ||
1517 | int | |
65f8144a | 1518 | gfc_constant_ac (gfc_expr *e) |
6de9cd9a DN |
1519 | { |
1520 | expand_info expand_save; | |
1521 | try rc; | |
1522 | ||
1523 | iter_stack = NULL; | |
1524 | expand_save = current_expand; | |
1525 | current_expand.expand_work_function = constant_element; | |
1526 | ||
1527 | rc = expand_constructor (e->value.constructor); | |
1528 | ||
1529 | current_expand = expand_save; | |
1530 | if (rc == FAILURE) | |
1531 | return 0; | |
1532 | ||
1533 | return 1; | |
1534 | } | |
1535 | ||
1536 | ||
1537 | /* Returns nonzero if an array constructor has been completely | |
1538 | expanded (no iterators) and zero if iterators are present. */ | |
1539 | ||
1540 | int | |
65f8144a | 1541 | gfc_expanded_ac (gfc_expr *e) |
6de9cd9a DN |
1542 | { |
1543 | gfc_constructor *p; | |
1544 | ||
1545 | if (e->expr_type == EXPR_ARRAY) | |
1546 | for (p = e->value.constructor; p; p = p->next) | |
1547 | if (p->iterator != NULL || !gfc_expanded_ac (p->expr)) | |
1548 | return 0; | |
1549 | ||
1550 | return 1; | |
1551 | } | |
1552 | ||
1553 | ||
1554 | /*************** Type resolution of array constructors ***************/ | |
1555 | ||
1556 | /* Recursive array list resolution function. All of the elements must | |
1557 | be of the same type. */ | |
1558 | ||
1559 | static try | |
65f8144a | 1560 | resolve_array_list (gfc_constructor *p) |
6de9cd9a DN |
1561 | { |
1562 | try t; | |
1563 | ||
1564 | t = SUCCESS; | |
1565 | ||
1566 | for (; p; p = p->next) | |
1567 | { | |
1568 | if (p->iterator != NULL | |
8d5cfa27 | 1569 | && gfc_resolve_iterator (p->iterator, false) == FAILURE) |
6de9cd9a DN |
1570 | t = FAILURE; |
1571 | ||
1572 | if (gfc_resolve_expr (p->expr) == FAILURE) | |
1573 | t = FAILURE; | |
1574 | } | |
1575 | ||
1576 | return t; | |
1577 | } | |
1578 | ||
88fec49f DK |
1579 | /* Resolve character array constructor. If it has a specified constant character |
1580 | length, pad/trunkate the elements here; if the length is not specified and | |
1581 | all elements are of compile-time known length, emit an error as this is | |
1582 | invalid. */ | |
6de9cd9a | 1583 | |
88fec49f | 1584 | try |
65f8144a | 1585 | gfc_resolve_character_array_constructor (gfc_expr *expr) |
df7cc9b5 | 1586 | { |
65f8144a | 1587 | gfc_constructor *p; |
88fec49f | 1588 | int found_length; |
df7cc9b5 FW |
1589 | |
1590 | gcc_assert (expr->expr_type == EXPR_ARRAY); | |
1591 | gcc_assert (expr->ts.type == BT_CHARACTER); | |
1592 | ||
4a90ae54 FW |
1593 | if (expr->ts.cl == NULL) |
1594 | { | |
1855915a PT |
1595 | for (p = expr->value.constructor; p; p = p->next) |
1596 | if (p->expr->ts.cl != NULL) | |
1597 | { | |
1598 | /* Ensure that if there is a char_len around that it is | |
1599 | used; otherwise the middle-end confuses them! */ | |
1600 | expr->ts.cl = p->expr->ts.cl; | |
1601 | goto got_charlen; | |
1602 | } | |
1603 | ||
4a90ae54 FW |
1604 | expr->ts.cl = gfc_get_charlen (); |
1605 | expr->ts.cl->next = gfc_current_ns->cl_list; | |
1606 | gfc_current_ns->cl_list = expr->ts.cl; | |
1607 | } | |
1608 | ||
1855915a PT |
1609 | got_charlen: |
1610 | ||
88fec49f DK |
1611 | found_length = -1; |
1612 | ||
4a90ae54 | 1613 | if (expr->ts.cl->length == NULL) |
df7cc9b5 | 1614 | { |
88fec49f DK |
1615 | /* Check that all constant string elements have the same length until |
1616 | we reach the end or find a variable-length one. */ | |
1855915a | 1617 | |
df7cc9b5 | 1618 | for (p = expr->value.constructor; p; p = p->next) |
1855915a | 1619 | { |
88fec49f | 1620 | int current_length = -1; |
1855915a PT |
1621 | gfc_ref *ref; |
1622 | for (ref = p->expr->ref; ref; ref = ref->next) | |
1623 | if (ref->type == REF_SUBSTRING | |
65f8144a SK |
1624 | && ref->u.ss.start->expr_type == EXPR_CONSTANT |
1625 | && ref->u.ss.end->expr_type == EXPR_CONSTANT) | |
1855915a PT |
1626 | break; |
1627 | ||
1628 | if (p->expr->expr_type == EXPR_CONSTANT) | |
88fec49f | 1629 | current_length = p->expr->value.character.length; |
1855915a | 1630 | else if (ref) |
65f8144a SK |
1631 | { |
1632 | long j; | |
1633 | j = mpz_get_ui (ref->u.ss.end->value.integer) | |
1634 | - mpz_get_ui (ref->u.ss.start->value.integer) + 1; | |
88fec49f | 1635 | current_length = (int) j; |
65f8144a | 1636 | } |
1855915a | 1637 | else if (p->expr->ts.cl && p->expr->ts.cl->length |
65f8144a SK |
1638 | && p->expr->ts.cl->length->expr_type == EXPR_CONSTANT) |
1639 | { | |
1640 | long j; | |
1641 | j = mpz_get_si (p->expr->ts.cl->length->value.integer); | |
88fec49f | 1642 | current_length = (int) j; |
65f8144a | 1643 | } |
1855915a | 1644 | else |
88fec49f | 1645 | return SUCCESS; |
df7cc9b5 | 1646 | |
88fec49f DK |
1647 | gcc_assert (current_length != -1); |
1648 | ||
1649 | if (found_length == -1) | |
1650 | found_length = current_length; | |
1651 | else if (found_length != current_length) | |
1652 | { | |
1653 | gfc_error ("Different CHARACTER lengths (%d/%d) in array" | |
1654 | " constructor at %L", found_length, current_length, | |
1655 | &p->expr->where); | |
1656 | return FAILURE; | |
1657 | } | |
1658 | ||
1659 | gcc_assert (found_length == current_length); | |
df7cc9b5 | 1660 | } |
88fec49f DK |
1661 | |
1662 | gcc_assert (found_length != -1); | |
1663 | ||
1664 | /* Update the character length of the array constructor. */ | |
1665 | expr->ts.cl->length = gfc_int_expr (found_length); | |
df7cc9b5 | 1666 | } |
c03fc95d DK |
1667 | else |
1668 | { | |
1669 | /* We've got a character length specified. It should be an integer, | |
1670 | otherwise an error is signalled elsewhere. */ | |
1671 | gcc_assert (expr->ts.cl->length); | |
1672 | ||
1673 | /* If we've got a constant character length, pad according to this. | |
1674 | gfc_extract_int does check for BT_INTEGER and EXPR_CONSTANT and sets | |
1675 | max_length only if they pass. */ | |
88fec49f DK |
1676 | gfc_extract_int (expr->ts.cl->length, &found_length); |
1677 | ||
1678 | /* Now pad/trunkate the elements accordingly to the specified character | |
1679 | length. This is ok inside this conditional, as in the case above | |
1680 | (without typespec) all elements are verified to have the same length | |
1681 | anyway. */ | |
1682 | if (found_length != -1) | |
1683 | for (p = expr->value.constructor; p; p = p->next) | |
1684 | if (p->expr->expr_type == EXPR_CONSTANT) | |
1685 | { | |
1686 | gfc_expr *cl = NULL; | |
1687 | int current_length = -1; | |
1688 | ||
1689 | if (p->expr->ts.cl && p->expr->ts.cl->length) | |
1690 | { | |
1691 | cl = p->expr->ts.cl->length; | |
1692 | gfc_extract_int (cl, ¤t_length); | |
1693 | } | |
1694 | ||
1695 | /* If gfc_extract_int above set current_length, we implicitly | |
1696 | know the type is BT_INTEGER and it's EXPR_CONSTANT. */ | |
1697 | ||
1698 | if (! cl | |
1699 | || (current_length != -1 && current_length < found_length)) | |
1700 | gfc_set_constant_character_len (found_length, p->expr, true); | |
1701 | } | |
c03fc95d DK |
1702 | } |
1703 | ||
88fec49f | 1704 | return SUCCESS; |
df7cc9b5 FW |
1705 | } |
1706 | ||
65f8144a | 1707 | |
df7cc9b5 | 1708 | /* Resolve all of the expressions in an array list. */ |
6de9cd9a DN |
1709 | |
1710 | try | |
65f8144a | 1711 | gfc_resolve_array_constructor (gfc_expr *expr) |
6de9cd9a DN |
1712 | { |
1713 | try t; | |
1714 | ||
1715 | t = resolve_array_list (expr->value.constructor); | |
1716 | if (t == SUCCESS) | |
1717 | t = gfc_check_constructor_type (expr); | |
88fec49f DK |
1718 | |
1719 | /* gfc_resolve_character_array_constructor is called in gfc_resolve_expr after | |
1720 | the call to this function, so we don't need to call it here; if it was | |
1721 | called twice, an error message there would be duplicated. */ | |
6de9cd9a DN |
1722 | |
1723 | return t; | |
1724 | } | |
1725 | ||
1726 | ||
1727 | /* Copy an iterator structure. */ | |
1728 | ||
1729 | static gfc_iterator * | |
65f8144a | 1730 | copy_iterator (gfc_iterator *src) |
6de9cd9a DN |
1731 | { |
1732 | gfc_iterator *dest; | |
1733 | ||
1734 | if (src == NULL) | |
1735 | return NULL; | |
1736 | ||
1737 | dest = gfc_get_iterator (); | |
1738 | ||
1739 | dest->var = gfc_copy_expr (src->var); | |
1740 | dest->start = gfc_copy_expr (src->start); | |
1741 | dest->end = gfc_copy_expr (src->end); | |
1742 | dest->step = gfc_copy_expr (src->step); | |
1743 | ||
1744 | return dest; | |
1745 | } | |
1746 | ||
1747 | ||
1748 | /* Copy a constructor structure. */ | |
1749 | ||
1750 | gfc_constructor * | |
65f8144a | 1751 | gfc_copy_constructor (gfc_constructor *src) |
6de9cd9a DN |
1752 | { |
1753 | gfc_constructor *dest; | |
1754 | gfc_constructor *tail; | |
1755 | ||
1756 | if (src == NULL) | |
1757 | return NULL; | |
1758 | ||
1759 | dest = tail = NULL; | |
1760 | while (src) | |
1761 | { | |
1762 | if (dest == NULL) | |
1763 | dest = tail = gfc_get_constructor (); | |
1764 | else | |
1765 | { | |
1766 | tail->next = gfc_get_constructor (); | |
1767 | tail = tail->next; | |
1768 | } | |
1769 | tail->where = src->where; | |
1770 | tail->expr = gfc_copy_expr (src->expr); | |
1771 | tail->iterator = copy_iterator (src->iterator); | |
1772 | mpz_set (tail->n.offset, src->n.offset); | |
1773 | tail->n.component = src->n.component; | |
1774 | mpz_set (tail->repeat, src->repeat); | |
1775 | src = src->next; | |
1776 | } | |
1777 | ||
1778 | return dest; | |
1779 | } | |
1780 | ||
1781 | ||
1782 | /* Given an array expression and an element number (starting at zero), | |
1783 | return a pointer to the array element. NULL is returned if the | |
1784 | size of the array has been exceeded. The expression node returned | |
1785 | remains a part of the array and should not be freed. Access is not | |
1786 | efficient at all, but this is another place where things do not | |
1787 | have to be particularly fast. */ | |
1788 | ||
1789 | gfc_expr * | |
65f8144a | 1790 | gfc_get_array_element (gfc_expr *array, int element) |
6de9cd9a DN |
1791 | { |
1792 | expand_info expand_save; | |
1793 | gfc_expr *e; | |
1794 | try rc; | |
1795 | ||
1796 | expand_save = current_expand; | |
1797 | current_expand.extract_n = element; | |
1798 | current_expand.expand_work_function = extract_element; | |
1799 | current_expand.extracted = NULL; | |
1800 | current_expand.extract_count = 0; | |
1801 | ||
1802 | iter_stack = NULL; | |
1803 | ||
1804 | rc = expand_constructor (array->value.constructor); | |
1805 | e = current_expand.extracted; | |
1806 | current_expand = expand_save; | |
1807 | ||
1808 | if (rc == FAILURE) | |
1809 | return NULL; | |
1810 | ||
1811 | return e; | |
1812 | } | |
1813 | ||
1814 | ||
1815 | /********* Subroutines for determining the size of an array *********/ | |
1816 | ||
1f2959f0 | 1817 | /* These are needed just to accommodate RESHAPE(). There are no |
6de9cd9a | 1818 | diagnostics here, we just return a negative number if something |
f7b529fa | 1819 | goes wrong. */ |
6de9cd9a DN |
1820 | |
1821 | ||
1822 | /* Get the size of single dimension of an array specification. The | |
1823 | array is guaranteed to be one dimensional. */ | |
1824 | ||
a9b43781 | 1825 | try |
65f8144a | 1826 | spec_dimen_size (gfc_array_spec *as, int dimen, mpz_t *result) |
6de9cd9a | 1827 | { |
6de9cd9a DN |
1828 | if (as == NULL) |
1829 | return FAILURE; | |
1830 | ||
1831 | if (dimen < 0 || dimen > as->rank - 1) | |
1832 | gfc_internal_error ("spec_dimen_size(): Bad dimension"); | |
1833 | ||
1834 | if (as->type != AS_EXPLICIT | |
1835 | || as->lower[dimen]->expr_type != EXPR_CONSTANT | |
1505f3b5 JD |
1836 | || as->upper[dimen]->expr_type != EXPR_CONSTANT |
1837 | || as->lower[dimen]->ts.type != BT_INTEGER | |
1838 | || as->upper[dimen]->ts.type != BT_INTEGER) | |
6de9cd9a DN |
1839 | return FAILURE; |
1840 | ||
1841 | mpz_init (*result); | |
1842 | ||
1843 | mpz_sub (*result, as->upper[dimen]->value.integer, | |
1844 | as->lower[dimen]->value.integer); | |
1845 | ||
1846 | mpz_add_ui (*result, *result, 1); | |
1847 | ||
1848 | return SUCCESS; | |
1849 | } | |
1850 | ||
1851 | ||
1852 | try | |
65f8144a | 1853 | spec_size (gfc_array_spec *as, mpz_t *result) |
6de9cd9a DN |
1854 | { |
1855 | mpz_t size; | |
1856 | int d; | |
1857 | ||
1858 | mpz_init_set_ui (*result, 1); | |
1859 | ||
1860 | for (d = 0; d < as->rank; d++) | |
1861 | { | |
1862 | if (spec_dimen_size (as, d, &size) == FAILURE) | |
1863 | { | |
1864 | mpz_clear (*result); | |
1865 | return FAILURE; | |
1866 | } | |
1867 | ||
1868 | mpz_mul (*result, *result, size); | |
1869 | mpz_clear (size); | |
1870 | } | |
1871 | ||
1872 | return SUCCESS; | |
1873 | } | |
1874 | ||
1875 | ||
1876 | /* Get the number of elements in an array section. */ | |
1877 | ||
1878 | static try | |
65f8144a | 1879 | ref_dimen_size (gfc_array_ref *ar, int dimen, mpz_t *result) |
6de9cd9a DN |
1880 | { |
1881 | mpz_t upper, lower, stride; | |
1882 | try t; | |
1883 | ||
1884 | if (dimen < 0 || ar == NULL || dimen > ar->dimen - 1) | |
1885 | gfc_internal_error ("ref_dimen_size(): Bad dimension"); | |
1886 | ||
1887 | switch (ar->dimen_type[dimen]) | |
1888 | { | |
1889 | case DIMEN_ELEMENT: | |
1890 | mpz_init (*result); | |
1891 | mpz_set_ui (*result, 1); | |
1892 | t = SUCCESS; | |
1893 | break; | |
1894 | ||
1895 | case DIMEN_VECTOR: | |
1896 | t = gfc_array_size (ar->start[dimen], result); /* Recurse! */ | |
1897 | break; | |
1898 | ||
1899 | case DIMEN_RANGE: | |
1900 | mpz_init (upper); | |
1901 | mpz_init (lower); | |
1902 | mpz_init (stride); | |
1903 | t = FAILURE; | |
1904 | ||
1905 | if (ar->start[dimen] == NULL) | |
1906 | { | |
1907 | if (ar->as->lower[dimen] == NULL | |
1908 | || ar->as->lower[dimen]->expr_type != EXPR_CONSTANT) | |
1909 | goto cleanup; | |
1910 | mpz_set (lower, ar->as->lower[dimen]->value.integer); | |
1911 | } | |
1912 | else | |
1913 | { | |
1914 | if (ar->start[dimen]->expr_type != EXPR_CONSTANT) | |
1915 | goto cleanup; | |
1916 | mpz_set (lower, ar->start[dimen]->value.integer); | |
1917 | } | |
1918 | ||
1919 | if (ar->end[dimen] == NULL) | |
1920 | { | |
1921 | if (ar->as->upper[dimen] == NULL | |
1922 | || ar->as->upper[dimen]->expr_type != EXPR_CONSTANT) | |
1923 | goto cleanup; | |
1924 | mpz_set (upper, ar->as->upper[dimen]->value.integer); | |
1925 | } | |
1926 | else | |
1927 | { | |
1928 | if (ar->end[dimen]->expr_type != EXPR_CONSTANT) | |
1929 | goto cleanup; | |
1930 | mpz_set (upper, ar->end[dimen]->value.integer); | |
1931 | } | |
1932 | ||
1933 | if (ar->stride[dimen] == NULL) | |
1934 | mpz_set_ui (stride, 1); | |
1935 | else | |
1936 | { | |
1937 | if (ar->stride[dimen]->expr_type != EXPR_CONSTANT) | |
1938 | goto cleanup; | |
1939 | mpz_set (stride, ar->stride[dimen]->value.integer); | |
1940 | } | |
1941 | ||
1942 | mpz_init (*result); | |
1943 | mpz_sub (*result, upper, lower); | |
1944 | mpz_add (*result, *result, stride); | |
1945 | mpz_div (*result, *result, stride); | |
1946 | ||
1947 | /* Zero stride caught earlier. */ | |
1948 | if (mpz_cmp_ui (*result, 0) < 0) | |
1949 | mpz_set_ui (*result, 0); | |
1950 | t = SUCCESS; | |
1951 | ||
1952 | cleanup: | |
1953 | mpz_clear (upper); | |
1954 | mpz_clear (lower); | |
1955 | mpz_clear (stride); | |
1956 | return t; | |
1957 | ||
1958 | default: | |
1959 | gfc_internal_error ("ref_dimen_size(): Bad dimen_type"); | |
1960 | } | |
1961 | ||
1962 | return t; | |
1963 | } | |
1964 | ||
1965 | ||
1966 | static try | |
65f8144a | 1967 | ref_size (gfc_array_ref *ar, mpz_t *result) |
6de9cd9a DN |
1968 | { |
1969 | mpz_t size; | |
1970 | int d; | |
1971 | ||
1972 | mpz_init_set_ui (*result, 1); | |
1973 | ||
1974 | for (d = 0; d < ar->dimen; d++) | |
1975 | { | |
1976 | if (ref_dimen_size (ar, d, &size) == FAILURE) | |
1977 | { | |
1978 | mpz_clear (*result); | |
1979 | return FAILURE; | |
1980 | } | |
1981 | ||
1982 | mpz_mul (*result, *result, size); | |
1983 | mpz_clear (size); | |
1984 | } | |
1985 | ||
1986 | return SUCCESS; | |
1987 | } | |
1988 | ||
1989 | ||
1990 | /* Given an array expression and a dimension, figure out how many | |
1991 | elements it has along that dimension. Returns SUCCESS if we were | |
1992 | able to return a result in the 'result' variable, FAILURE | |
1993 | otherwise. */ | |
1994 | ||
1995 | try | |
65f8144a | 1996 | gfc_array_dimen_size (gfc_expr *array, int dimen, mpz_t *result) |
6de9cd9a DN |
1997 | { |
1998 | gfc_ref *ref; | |
1999 | int i; | |
2000 | ||
2001 | if (dimen < 0 || array == NULL || dimen > array->rank - 1) | |
2002 | gfc_internal_error ("gfc_array_dimen_size(): Bad dimension"); | |
2003 | ||
2004 | switch (array->expr_type) | |
2005 | { | |
2006 | case EXPR_VARIABLE: | |
2007 | case EXPR_FUNCTION: | |
2008 | for (ref = array->ref; ref; ref = ref->next) | |
2009 | { | |
2010 | if (ref->type != REF_ARRAY) | |
2011 | continue; | |
2012 | ||
2013 | if (ref->u.ar.type == AR_FULL) | |
2014 | return spec_dimen_size (ref->u.ar.as, dimen, result); | |
2015 | ||
2016 | if (ref->u.ar.type == AR_SECTION) | |
2017 | { | |
2018 | for (i = 0; dimen >= 0; i++) | |
2019 | if (ref->u.ar.dimen_type[i] != DIMEN_ELEMENT) | |
2020 | dimen--; | |
2021 | ||
2022 | return ref_dimen_size (&ref->u.ar, i - 1, result); | |
2023 | } | |
2024 | } | |
2025 | ||
e15e9be3 PT |
2026 | if (array->shape && array->shape[dimen]) |
2027 | { | |
2028 | mpz_init_set (*result, array->shape[dimen]); | |
2029 | return SUCCESS; | |
2030 | } | |
2031 | ||
6de9cd9a DN |
2032 | if (spec_dimen_size (array->symtree->n.sym->as, dimen, result) == FAILURE) |
2033 | return FAILURE; | |
2034 | ||
2035 | break; | |
2036 | ||
2037 | case EXPR_ARRAY: | |
2038 | if (array->shape == NULL) { | |
2039 | /* Expressions with rank > 1 should have "shape" properly set */ | |
2040 | if ( array->rank != 1 ) | |
2041 | gfc_internal_error ("gfc_array_dimen_size(): Bad EXPR_ARRAY expr"); | |
2042 | return gfc_array_size(array, result); | |
2043 | } | |
2044 | ||
2045 | /* Fall through */ | |
2046 | default: | |
2047 | if (array->shape == NULL) | |
2048 | return FAILURE; | |
2049 | ||
2050 | mpz_init_set (*result, array->shape[dimen]); | |
2051 | ||
2052 | break; | |
2053 | } | |
2054 | ||
2055 | return SUCCESS; | |
2056 | } | |
2057 | ||
2058 | ||
2059 | /* Given an array expression, figure out how many elements are in the | |
2060 | array. Returns SUCCESS if this is possible, and sets the 'result' | |
2061 | variable. Otherwise returns FAILURE. */ | |
2062 | ||
2063 | try | |
65f8144a | 2064 | gfc_array_size (gfc_expr *array, mpz_t *result) |
6de9cd9a DN |
2065 | { |
2066 | expand_info expand_save; | |
2067 | gfc_ref *ref; | |
2068 | int i, flag; | |
2069 | try t; | |
2070 | ||
2071 | switch (array->expr_type) | |
2072 | { | |
2073 | case EXPR_ARRAY: | |
2074 | flag = gfc_suppress_error; | |
2075 | gfc_suppress_error = 1; | |
2076 | ||
2077 | expand_save = current_expand; | |
2078 | ||
2079 | current_expand.count = result; | |
2080 | mpz_init_set_ui (*result, 0); | |
2081 | ||
2082 | current_expand.expand_work_function = count_elements; | |
2083 | iter_stack = NULL; | |
2084 | ||
2085 | t = expand_constructor (array->value.constructor); | |
2086 | gfc_suppress_error = flag; | |
2087 | ||
2088 | if (t == FAILURE) | |
2089 | mpz_clear (*result); | |
2090 | current_expand = expand_save; | |
2091 | return t; | |
2092 | ||
2093 | case EXPR_VARIABLE: | |
2094 | for (ref = array->ref; ref; ref = ref->next) | |
2095 | { | |
2096 | if (ref->type != REF_ARRAY) | |
2097 | continue; | |
2098 | ||
2099 | if (ref->u.ar.type == AR_FULL) | |
2100 | return spec_size (ref->u.ar.as, result); | |
2101 | ||
2102 | if (ref->u.ar.type == AR_SECTION) | |
2103 | return ref_size (&ref->u.ar, result); | |
2104 | } | |
2105 | ||
2106 | return spec_size (array->symtree->n.sym->as, result); | |
2107 | ||
2108 | ||
2109 | default: | |
2110 | if (array->rank == 0 || array->shape == NULL) | |
2111 | return FAILURE; | |
2112 | ||
2113 | mpz_init_set_ui (*result, 1); | |
2114 | ||
2115 | for (i = 0; i < array->rank; i++) | |
2116 | mpz_mul (*result, *result, array->shape[i]); | |
2117 | ||
2118 | break; | |
2119 | } | |
2120 | ||
2121 | return SUCCESS; | |
2122 | } | |
2123 | ||
2124 | ||
2125 | /* Given an array reference, return the shape of the reference in an | |
2126 | array of mpz_t integers. */ | |
2127 | ||
2128 | try | |
65f8144a | 2129 | gfc_array_ref_shape (gfc_array_ref *ar, mpz_t *shape) |
6de9cd9a DN |
2130 | { |
2131 | int d; | |
2132 | int i; | |
2133 | ||
2134 | d = 0; | |
2135 | ||
2136 | switch (ar->type) | |
2137 | { | |
2138 | case AR_FULL: | |
2139 | for (; d < ar->as->rank; d++) | |
2140 | if (spec_dimen_size (ar->as, d, &shape[d]) == FAILURE) | |
2141 | goto cleanup; | |
2142 | ||
2143 | return SUCCESS; | |
2144 | ||
2145 | case AR_SECTION: | |
2146 | for (i = 0; i < ar->dimen; i++) | |
2147 | { | |
2148 | if (ar->dimen_type[i] != DIMEN_ELEMENT) | |
2149 | { | |
2150 | if (ref_dimen_size (ar, i, &shape[d]) == FAILURE) | |
2151 | goto cleanup; | |
2152 | d++; | |
2153 | } | |
2154 | } | |
2155 | ||
2156 | return SUCCESS; | |
2157 | ||
2158 | default: | |
2159 | break; | |
2160 | } | |
2161 | ||
2162 | cleanup: | |
2163 | for (d--; d >= 0; d--) | |
2164 | mpz_clear (shape[d]); | |
2165 | ||
2166 | return FAILURE; | |
2167 | } | |
2168 | ||
2169 | ||
2170 | /* Given an array expression, find the array reference structure that | |
2171 | characterizes the reference. */ | |
2172 | ||
2173 | gfc_array_ref * | |
65f8144a | 2174 | gfc_find_array_ref (gfc_expr *e) |
6de9cd9a DN |
2175 | { |
2176 | gfc_ref *ref; | |
2177 | ||
2178 | for (ref = e->ref; ref; ref = ref->next) | |
2179 | if (ref->type == REF_ARRAY | |
65f8144a | 2180 | && (ref->u.ar.type == AR_FULL || ref->u.ar.type == AR_SECTION)) |
6de9cd9a DN |
2181 | break; |
2182 | ||
2183 | if (ref == NULL) | |
2184 | gfc_internal_error ("gfc_find_array_ref(): No ref found"); | |
2185 | ||
2186 | return &ref->u.ar; | |
2187 | } | |
4077d207 TS |
2188 | |
2189 | ||
2190 | /* Find out if an array shape is known at compile time. */ | |
2191 | ||
2192 | int | |
2193 | gfc_is_compile_time_shape (gfc_array_spec *as) | |
2194 | { | |
2195 | int i; | |
2196 | ||
2197 | if (as->type != AS_EXPLICIT) | |
2198 | return 0; | |
2199 | ||
2200 | for (i = 0; i < as->rank; i++) | |
2201 | if (!gfc_is_constant_expr (as->lower[i]) | |
2202 | || !gfc_is_constant_expr (as->upper[i])) | |
2203 | return 0; | |
2204 | ||
2205 | return 1; | |
2206 | } |