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