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