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