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