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