[AArch64] Avoid GET_MODE_NUNITS in v8.4 support
[official-gcc.git] / gcc / fortran / array.c
blob882fe577b76611570f4a87b640e5c4f972bf1d14
1 /* Array things
2 Copyright (C) 2000-2018 Free Software Foundation, Inc.
3 Contributed by Andy Vaught
5 This file is part of GCC.
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.
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.
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/>. */
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"
29 /**************** Array reference matching subroutines *****************/
31 /* Copy an array reference structure. */
33 gfc_array_ref *
34 gfc_copy_array_ref (gfc_array_ref *src)
36 gfc_array_ref *dest;
37 int i;
39 if (src == NULL)
40 return NULL;
42 dest = gfc_get_array_ref ();
44 *dest = *src;
46 for (i = 0; i < GFC_MAX_DIMENSIONS; i++)
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]);
53 return dest;
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. */
63 static match
64 match_subscript (gfc_array_ref *ar, int init, bool match_star)
66 match m = MATCH_ERROR;
67 bool star = false;
68 int i;
70 i = ar->dimen + ar->codimen;
72 gfc_gobble_whitespace ();
73 ar->c_where[i] = gfc_current_locus;
74 ar->start[i] = ar->end[i] = ar->stride[i] = NULL;
76 /* We can't be sure of the difference between DIMEN_ELEMENT and
77 DIMEN_VECTOR until we know the type of the element itself at
78 resolution time. */
80 ar->dimen_type[i] = DIMEN_UNKNOWN;
82 if (gfc_match_char (':') == MATCH_YES)
83 goto end_element;
85 /* Get start element. */
86 if (match_star && (m = gfc_match_char ('*')) == MATCH_YES)
87 star = true;
89 if (!star && init)
90 m = gfc_match_init_expr (&ar->start[i]);
91 else if (!star)
92 m = gfc_match_expr (&ar->start[i]);
94 if (m == MATCH_NO)
95 gfc_error ("Expected array subscript at %C");
96 if (m != MATCH_YES)
97 return MATCH_ERROR;
99 if (gfc_match_char (':') == MATCH_NO)
100 goto matched;
102 if (star)
104 gfc_error ("Unexpected %<*%> in coarray subscript at %C");
105 return MATCH_ERROR;
108 /* Get an optional end element. Because we've seen the colon, we
109 definitely have a range along this dimension. */
110 end_element:
111 ar->dimen_type[i] = DIMEN_RANGE;
113 if (match_star && (m = gfc_match_char ('*')) == MATCH_YES)
114 star = true;
115 else if (init)
116 m = gfc_match_init_expr (&ar->end[i]);
117 else
118 m = gfc_match_expr (&ar->end[i]);
120 if (m == MATCH_ERROR)
121 return MATCH_ERROR;
123 /* See if we have an optional stride. */
124 if (gfc_match_char (':') == MATCH_YES)
126 if (star)
128 gfc_error ("Strides not allowed in coarray subscript at %C");
129 return MATCH_ERROR;
132 m = init ? gfc_match_init_expr (&ar->stride[i])
133 : gfc_match_expr (&ar->stride[i]);
135 if (m == MATCH_NO)
136 gfc_error ("Expected array subscript stride at %C");
137 if (m != MATCH_YES)
138 return MATCH_ERROR;
141 matched:
142 if (star)
143 ar->dimen_type[i] = DIMEN_STAR;
145 return MATCH_YES;
149 /* Match an array reference, whether it is the whole array or particular
150 elements or a section. If init is set, the reference has to consist
151 of init expressions. */
153 match
154 gfc_match_array_ref (gfc_array_ref *ar, gfc_array_spec *as, int init,
155 int corank)
157 match m;
158 bool matched_bracket = false;
159 gfc_expr *tmp;
160 bool stat_just_seen = false;
162 memset (ar, '\0', sizeof (*ar));
164 ar->where = gfc_current_locus;
165 ar->as = as;
166 ar->type = AR_UNKNOWN;
168 if (gfc_match_char ('[') == MATCH_YES)
170 matched_bracket = true;
171 goto coarray;
174 if (gfc_match_char ('(') != MATCH_YES)
176 ar->type = AR_FULL;
177 ar->dimen = 0;
178 return MATCH_YES;
181 for (ar->dimen = 0; ar->dimen < GFC_MAX_DIMENSIONS; ar->dimen++)
183 m = match_subscript (ar, init, false);
184 if (m == MATCH_ERROR)
185 return MATCH_ERROR;
187 if (gfc_match_char (')') == MATCH_YES)
189 ar->dimen++;
190 goto coarray;
193 if (gfc_match_char (',') != MATCH_YES)
195 gfc_error ("Invalid form of array reference at %C");
196 return MATCH_ERROR;
200 gfc_error ("Array reference at %C cannot have more than %d dimensions",
201 GFC_MAX_DIMENSIONS);
202 return MATCH_ERROR;
204 coarray:
205 if (!matched_bracket && gfc_match_char ('[') != MATCH_YES)
207 if (ar->dimen > 0)
208 return MATCH_YES;
209 else
210 return MATCH_ERROR;
213 if (flag_coarray == GFC_FCOARRAY_NONE)
215 gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to enable");
216 return MATCH_ERROR;
219 if (corank == 0)
221 gfc_error ("Unexpected coarray designator at %C");
222 return MATCH_ERROR;
225 ar->stat = NULL;
227 for (ar->codimen = 0; ar->codimen + ar->dimen < GFC_MAX_DIMENSIONS; ar->codimen++)
229 m = match_subscript (ar, init, true);
230 if (m == MATCH_ERROR)
231 return MATCH_ERROR;
233 stat_just_seen = false;
234 if (gfc_match(" , stat = %e",&tmp) == MATCH_YES && ar->stat == NULL)
236 ar->stat = tmp;
237 stat_just_seen = true;
240 if (ar->stat && !stat_just_seen)
242 gfc_error ("STAT= attribute in %C misplaced");
243 return MATCH_ERROR;
246 if (gfc_match_char (']') == MATCH_YES)
248 ar->codimen++;
249 if (ar->codimen < corank)
251 gfc_error ("Too few codimensions at %C, expected %d not %d",
252 corank, ar->codimen);
253 return MATCH_ERROR;
255 if (ar->codimen > corank)
257 gfc_error ("Too many codimensions at %C, expected %d not %d",
258 corank, ar->codimen);
259 return MATCH_ERROR;
261 return MATCH_YES;
264 if (gfc_match_char (',') != MATCH_YES)
266 if (gfc_match_char ('*') == MATCH_YES)
267 gfc_error ("Unexpected %<*%> for codimension %d of %d at %C",
268 ar->codimen + 1, corank);
269 else
270 gfc_error ("Invalid form of coarray reference at %C");
271 return MATCH_ERROR;
273 else if (ar->dimen_type[ar->codimen + ar->dimen] == DIMEN_STAR)
275 gfc_error ("Unexpected %<*%> for codimension %d of %d at %C",
276 ar->codimen + 1, corank);
277 return MATCH_ERROR;
280 if (ar->codimen >= corank)
282 gfc_error ("Invalid codimension %d at %C, only %d codimensions exist",
283 ar->codimen + 1, corank);
284 return MATCH_ERROR;
288 gfc_error ("Array reference at %C cannot have more than %d dimensions",
289 GFC_MAX_DIMENSIONS);
290 return MATCH_ERROR;
295 /************** Array specification matching subroutines ***************/
297 /* Free all of the expressions associated with array bounds
298 specifications. */
300 void
301 gfc_free_array_spec (gfc_array_spec *as)
303 int i;
305 if (as == NULL)
306 return;
308 for (i = 0; i < as->rank + as->corank; i++)
310 gfc_free_expr (as->lower[i]);
311 gfc_free_expr (as->upper[i]);
314 free (as);
318 /* Take an array bound, resolves the expression, that make up the
319 shape and check associated constraints. */
321 static bool
322 resolve_array_bound (gfc_expr *e, int check_constant)
324 if (e == NULL)
325 return true;
327 if (!gfc_resolve_expr (e)
328 || !gfc_specification_expr (e))
329 return false;
331 if (check_constant && !gfc_is_constant_expr (e))
333 if (e->expr_type == EXPR_VARIABLE)
334 gfc_error ("Variable %qs at %L in this context must be constant",
335 e->symtree->n.sym->name, &e->where);
336 else
337 gfc_error ("Expression at %L in this context must be constant",
338 &e->where);
339 return false;
342 return true;
346 /* Takes an array specification, resolves the expressions that make up
347 the shape and make sure everything is integral. */
349 bool
350 gfc_resolve_array_spec (gfc_array_spec *as, int check_constant)
352 gfc_expr *e;
353 int i;
355 if (as == NULL)
356 return true;
358 if (as->resolved)
359 return true;
361 for (i = 0; i < as->rank + as->corank; i++)
363 e = as->lower[i];
364 if (!resolve_array_bound (e, check_constant))
365 return false;
367 e = as->upper[i];
368 if (!resolve_array_bound (e, check_constant))
369 return false;
371 if ((as->lower[i] == NULL) || (as->upper[i] == NULL))
372 continue;
374 /* If the size is negative in this dimension, set it to zero. */
375 if (as->lower[i]->expr_type == EXPR_CONSTANT
376 && as->upper[i]->expr_type == EXPR_CONSTANT
377 && mpz_cmp (as->upper[i]->value.integer,
378 as->lower[i]->value.integer) < 0)
380 gfc_free_expr (as->upper[i]);
381 as->upper[i] = gfc_copy_expr (as->lower[i]);
382 mpz_sub_ui (as->upper[i]->value.integer,
383 as->upper[i]->value.integer, 1);
387 as->resolved = true;
389 return true;
393 /* Match a single array element specification. The return values as
394 well as the upper and lower bounds of the array spec are filled
395 in according to what we see on the input. The caller makes sure
396 individual specifications make sense as a whole.
399 Parsed Lower Upper Returned
400 ------------------------------------
401 : NULL NULL AS_DEFERRED (*)
402 x 1 x AS_EXPLICIT
403 x: x NULL AS_ASSUMED_SHAPE
404 x:y x y AS_EXPLICIT
405 x:* x NULL AS_ASSUMED_SIZE
406 * 1 NULL AS_ASSUMED_SIZE
408 (*) For non-pointer dummy arrays this is AS_ASSUMED_SHAPE. This
409 is fixed during the resolution of formal interfaces.
411 Anything else AS_UNKNOWN. */
413 static array_type
414 match_array_element_spec (gfc_array_spec *as)
416 gfc_expr **upper, **lower;
417 match m;
418 int rank;
420 rank = as->rank == -1 ? 0 : as->rank;
421 lower = &as->lower[rank + as->corank - 1];
422 upper = &as->upper[rank + as->corank - 1];
424 if (gfc_match_char ('*') == MATCH_YES)
426 *lower = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
427 return AS_ASSUMED_SIZE;
430 if (gfc_match_char (':') == MATCH_YES)
431 return AS_DEFERRED;
433 m = gfc_match_expr (upper);
434 if (m == MATCH_NO)
435 gfc_error ("Expected expression in array specification at %C");
436 if (m != MATCH_YES)
437 return AS_UNKNOWN;
438 if (!gfc_expr_check_typed (*upper, gfc_current_ns, false))
439 return AS_UNKNOWN;
441 if (((*upper)->expr_type == EXPR_CONSTANT
442 && (*upper)->ts.type != BT_INTEGER) ||
443 ((*upper)->expr_type == EXPR_FUNCTION
444 && (*upper)->ts.type == BT_UNKNOWN
445 && (*upper)->symtree
446 && strcmp ((*upper)->symtree->name, "null") == 0))
448 gfc_error ("Expecting a scalar INTEGER expression at %C, found %s",
449 gfc_basic_typename ((*upper)->ts.type));
450 return AS_UNKNOWN;
453 if (gfc_match_char (':') == MATCH_NO)
455 *lower = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
456 return AS_EXPLICIT;
459 *lower = *upper;
460 *upper = NULL;
462 if (gfc_match_char ('*') == MATCH_YES)
463 return AS_ASSUMED_SIZE;
465 m = gfc_match_expr (upper);
466 if (m == MATCH_ERROR)
467 return AS_UNKNOWN;
468 if (m == MATCH_NO)
469 return AS_ASSUMED_SHAPE;
470 if (!gfc_expr_check_typed (*upper, gfc_current_ns, false))
471 return AS_UNKNOWN;
473 if (((*upper)->expr_type == EXPR_CONSTANT
474 && (*upper)->ts.type != BT_INTEGER) ||
475 ((*upper)->expr_type == EXPR_FUNCTION
476 && (*upper)->ts.type == BT_UNKNOWN
477 && (*upper)->symtree
478 && strcmp ((*upper)->symtree->name, "null") == 0))
480 gfc_error ("Expecting a scalar INTEGER expression at %C, found %s",
481 gfc_basic_typename ((*upper)->ts.type));
482 return AS_UNKNOWN;
485 return AS_EXPLICIT;
489 /* Matches an array specification, incidentally figuring out what sort
490 it is. Match either a normal array specification, or a coarray spec
491 or both. Optionally allow [:] for coarrays. */
493 match
494 gfc_match_array_spec (gfc_array_spec **asp, bool match_dim, bool match_codim)
496 array_type current_type;
497 gfc_array_spec *as;
498 int i;
500 as = gfc_get_array_spec ();
502 if (!match_dim)
503 goto coarray;
505 if (gfc_match_char ('(') != MATCH_YES)
507 if (!match_codim)
508 goto done;
509 goto coarray;
512 if (gfc_match (" .. )") == MATCH_YES)
514 as->type = AS_ASSUMED_RANK;
515 as->rank = -1;
517 if (!gfc_notify_std (GFC_STD_F2008_TS, "Assumed-rank array at %C"))
518 goto cleanup;
520 if (!match_codim)
521 goto done;
522 goto coarray;
525 for (;;)
527 as->rank++;
528 current_type = match_array_element_spec (as);
530 /* Note that current_type == AS_ASSUMED_SIZE for both assumed-size
531 and implied-shape specifications. If the rank is at least 2, we can
532 distinguish between them. But for rank 1, we currently return
533 ASSUMED_SIZE; this gets adjusted later when we know for sure
534 whether the symbol parsed is a PARAMETER or not. */
536 if (as->rank == 1)
538 if (current_type == AS_UNKNOWN)
539 goto cleanup;
540 as->type = current_type;
542 else
543 switch (as->type)
544 { /* See how current spec meshes with the existing. */
545 case AS_UNKNOWN:
546 goto cleanup;
548 case AS_IMPLIED_SHAPE:
549 if (current_type != AS_ASSUMED_SHAPE)
551 gfc_error ("Bad array specification for implied-shape"
552 " array at %C");
553 goto cleanup;
555 break;
557 case AS_EXPLICIT:
558 if (current_type == AS_ASSUMED_SIZE)
560 as->type = AS_ASSUMED_SIZE;
561 break;
564 if (current_type == AS_EXPLICIT)
565 break;
567 gfc_error ("Bad array specification for an explicitly shaped "
568 "array at %C");
570 goto cleanup;
572 case AS_ASSUMED_SHAPE:
573 if ((current_type == AS_ASSUMED_SHAPE)
574 || (current_type == AS_DEFERRED))
575 break;
577 gfc_error ("Bad array specification for assumed shape "
578 "array at %C");
579 goto cleanup;
581 case AS_DEFERRED:
582 if (current_type == AS_DEFERRED)
583 break;
585 if (current_type == AS_ASSUMED_SHAPE)
587 as->type = AS_ASSUMED_SHAPE;
588 break;
591 gfc_error ("Bad specification for deferred shape array at %C");
592 goto cleanup;
594 case AS_ASSUMED_SIZE:
595 if (as->rank == 2 && current_type == AS_ASSUMED_SIZE)
597 as->type = AS_IMPLIED_SHAPE;
598 break;
601 gfc_error ("Bad specification for assumed size array at %C");
602 goto cleanup;
604 case AS_ASSUMED_RANK:
605 gcc_unreachable ();
608 if (gfc_match_char (')') == MATCH_YES)
609 break;
611 if (gfc_match_char (',') != MATCH_YES)
613 gfc_error ("Expected another dimension in array declaration at %C");
614 goto cleanup;
617 if (as->rank + as->corank >= GFC_MAX_DIMENSIONS)
619 gfc_error ("Array specification at %C has more than %d dimensions",
620 GFC_MAX_DIMENSIONS);
621 goto cleanup;
624 if (as->corank + as->rank >= 7
625 && !gfc_notify_std (GFC_STD_F2008, "Array specification at %C "
626 "with more than 7 dimensions"))
627 goto cleanup;
630 if (!match_codim)
631 goto done;
633 coarray:
634 if (gfc_match_char ('[') != MATCH_YES)
635 goto done;
637 if (!gfc_notify_std (GFC_STD_F2008, "Coarray declaration at %C"))
638 goto cleanup;
640 if (flag_coarray == GFC_FCOARRAY_NONE)
642 gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to enable");
643 goto cleanup;
646 if (as->rank >= GFC_MAX_DIMENSIONS)
648 gfc_error ("Array specification at %C has more than %d "
649 "dimensions", GFC_MAX_DIMENSIONS);
650 goto cleanup;
653 for (;;)
655 as->corank++;
656 current_type = match_array_element_spec (as);
658 if (current_type == AS_UNKNOWN)
659 goto cleanup;
661 if (as->corank == 1)
662 as->cotype = current_type;
663 else
664 switch (as->cotype)
665 { /* See how current spec meshes with the existing. */
666 case AS_IMPLIED_SHAPE:
667 case AS_UNKNOWN:
668 goto cleanup;
670 case AS_EXPLICIT:
671 if (current_type == AS_ASSUMED_SIZE)
673 as->cotype = AS_ASSUMED_SIZE;
674 break;
677 if (current_type == AS_EXPLICIT)
678 break;
680 gfc_error ("Bad array specification for an explicitly "
681 "shaped array at %C");
683 goto cleanup;
685 case AS_ASSUMED_SHAPE:
686 if ((current_type == AS_ASSUMED_SHAPE)
687 || (current_type == AS_DEFERRED))
688 break;
690 gfc_error ("Bad array specification for assumed shape "
691 "array at %C");
692 goto cleanup;
694 case AS_DEFERRED:
695 if (current_type == AS_DEFERRED)
696 break;
698 if (current_type == AS_ASSUMED_SHAPE)
700 as->cotype = AS_ASSUMED_SHAPE;
701 break;
704 gfc_error ("Bad specification for deferred shape array at %C");
705 goto cleanup;
707 case AS_ASSUMED_SIZE:
708 gfc_error ("Bad specification for assumed size array at %C");
709 goto cleanup;
711 case AS_ASSUMED_RANK:
712 gcc_unreachable ();
715 if (gfc_match_char (']') == MATCH_YES)
716 break;
718 if (gfc_match_char (',') != MATCH_YES)
720 gfc_error ("Expected another dimension in array declaration at %C");
721 goto cleanup;
724 if (as->rank + as->corank >= GFC_MAX_DIMENSIONS)
726 gfc_error ("Array specification at %C has more than %d "
727 "dimensions", GFC_MAX_DIMENSIONS);
728 goto cleanup;
732 if (current_type == AS_EXPLICIT)
734 gfc_error ("Upper bound of last coarray dimension must be %<*%> at %C");
735 goto cleanup;
738 if (as->cotype == AS_ASSUMED_SIZE)
739 as->cotype = AS_EXPLICIT;
741 if (as->rank == 0)
742 as->type = as->cotype;
744 done:
745 if (as->rank == 0 && as->corank == 0)
747 *asp = NULL;
748 gfc_free_array_spec (as);
749 return MATCH_NO;
752 /* If a lower bounds of an assumed shape array is blank, put in one. */
753 if (as->type == AS_ASSUMED_SHAPE)
755 for (i = 0; i < as->rank + as->corank; i++)
757 if (as->lower[i] == NULL)
758 as->lower[i] = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
762 *asp = as;
764 return MATCH_YES;
766 cleanup:
767 /* Something went wrong. */
768 gfc_free_array_spec (as);
769 return MATCH_ERROR;
773 /* Given a symbol and an array specification, modify the symbol to
774 have that array specification. The error locus is needed in case
775 something goes wrong. On failure, the caller must free the spec. */
777 bool
778 gfc_set_array_spec (gfc_symbol *sym, gfc_array_spec *as, locus *error_loc)
780 int i;
782 if (as == NULL)
783 return true;
785 if (as->rank
786 && !gfc_add_dimension (&sym->attr, sym->name, error_loc))
787 return false;
789 if (as->corank
790 && !gfc_add_codimension (&sym->attr, sym->name, error_loc))
791 return false;
793 if (sym->as == NULL)
795 sym->as = as;
796 return true;
799 if ((sym->as->type == AS_ASSUMED_RANK && as->corank)
800 || (as->type == AS_ASSUMED_RANK && sym->as->corank))
802 gfc_error ("The assumed-rank array %qs at %L shall not have a "
803 "codimension", sym->name, error_loc);
804 return false;
807 if (as->corank)
809 /* The "sym" has no corank (checked via gfc_add_codimension). Thus
810 the codimension is simply added. */
811 gcc_assert (as->rank == 0 && sym->as->corank == 0);
813 sym->as->cotype = as->cotype;
814 sym->as->corank = as->corank;
815 for (i = 0; i < as->corank; i++)
817 sym->as->lower[sym->as->rank + i] = as->lower[i];
818 sym->as->upper[sym->as->rank + i] = as->upper[i];
821 else
823 /* The "sym" has no rank (checked via gfc_add_dimension). Thus
824 the dimension is added - but first the codimensions (if existing
825 need to be shifted to make space for the dimension. */
826 gcc_assert (as->corank == 0 && sym->as->rank == 0);
828 sym->as->rank = as->rank;
829 sym->as->type = as->type;
830 sym->as->cray_pointee = as->cray_pointee;
831 sym->as->cp_was_assumed = as->cp_was_assumed;
833 for (i = 0; i < sym->as->corank; i++)
835 sym->as->lower[as->rank + i] = sym->as->lower[i];
836 sym->as->upper[as->rank + i] = sym->as->upper[i];
838 for (i = 0; i < as->rank; i++)
840 sym->as->lower[i] = as->lower[i];
841 sym->as->upper[i] = as->upper[i];
845 free (as);
846 return true;
850 /* Copy an array specification. */
852 gfc_array_spec *
853 gfc_copy_array_spec (gfc_array_spec *src)
855 gfc_array_spec *dest;
856 int i;
858 if (src == NULL)
859 return NULL;
861 dest = gfc_get_array_spec ();
863 *dest = *src;
865 for (i = 0; i < dest->rank + dest->corank; i++)
867 dest->lower[i] = gfc_copy_expr (dest->lower[i]);
868 dest->upper[i] = gfc_copy_expr (dest->upper[i]);
871 return dest;
875 /* Returns nonzero if the two expressions are equal. Only handles integer
876 constants. */
878 static int
879 compare_bounds (gfc_expr *bound1, gfc_expr *bound2)
881 if (bound1 == NULL || bound2 == NULL
882 || bound1->expr_type != EXPR_CONSTANT
883 || bound2->expr_type != EXPR_CONSTANT
884 || bound1->ts.type != BT_INTEGER
885 || bound2->ts.type != BT_INTEGER)
886 gfc_internal_error ("gfc_compare_array_spec(): Array spec clobbered");
888 if (mpz_cmp (bound1->value.integer, bound2->value.integer) == 0)
889 return 1;
890 else
891 return 0;
895 /* Compares two array specifications. They must be constant or deferred
896 shape. */
899 gfc_compare_array_spec (gfc_array_spec *as1, gfc_array_spec *as2)
901 int i;
903 if (as1 == NULL && as2 == NULL)
904 return 1;
906 if (as1 == NULL || as2 == NULL)
907 return 0;
909 if (as1->rank != as2->rank)
910 return 0;
912 if (as1->corank != as2->corank)
913 return 0;
915 if (as1->rank == 0)
916 return 1;
918 if (as1->type != as2->type)
919 return 0;
921 if (as1->type == AS_EXPLICIT)
922 for (i = 0; i < as1->rank + as1->corank; i++)
924 if (compare_bounds (as1->lower[i], as2->lower[i]) == 0)
925 return 0;
927 if (compare_bounds (as1->upper[i], as2->upper[i]) == 0)
928 return 0;
931 return 1;
935 /****************** Array constructor functions ******************/
938 /* Given an expression node that might be an array constructor and a
939 symbol, make sure that no iterators in this or child constructors
940 use the symbol as an implied-DO iterator. Returns nonzero if a
941 duplicate was found. */
943 static int
944 check_duplicate_iterator (gfc_constructor_base base, gfc_symbol *master)
946 gfc_constructor *c;
947 gfc_expr *e;
949 for (c = gfc_constructor_first (base); c; c = gfc_constructor_next (c))
951 e = c->expr;
953 if (e->expr_type == EXPR_ARRAY
954 && check_duplicate_iterator (e->value.constructor, master))
955 return 1;
957 if (c->iterator == NULL)
958 continue;
960 if (c->iterator->var->symtree->n.sym == master)
962 gfc_error ("DO-iterator %qs at %L is inside iterator of the "
963 "same name", master->name, &c->where);
965 return 1;
969 return 0;
973 /* Forward declaration because these functions are mutually recursive. */
974 static match match_array_cons_element (gfc_constructor_base *);
976 /* Match a list of array elements. */
978 static match
979 match_array_list (gfc_constructor_base *result)
981 gfc_constructor_base head;
982 gfc_constructor *p;
983 gfc_iterator iter;
984 locus old_loc;
985 gfc_expr *e;
986 match m;
987 int n;
989 old_loc = gfc_current_locus;
991 if (gfc_match_char ('(') == MATCH_NO)
992 return MATCH_NO;
994 memset (&iter, '\0', sizeof (gfc_iterator));
995 head = NULL;
997 m = match_array_cons_element (&head);
998 if (m != MATCH_YES)
999 goto cleanup;
1001 if (gfc_match_char (',') != MATCH_YES)
1003 m = MATCH_NO;
1004 goto cleanup;
1007 for (n = 1;; n++)
1009 m = gfc_match_iterator (&iter, 0);
1010 if (m == MATCH_YES)
1011 break;
1012 if (m == MATCH_ERROR)
1013 goto cleanup;
1015 m = match_array_cons_element (&head);
1016 if (m == MATCH_ERROR)
1017 goto cleanup;
1018 if (m == MATCH_NO)
1020 if (n > 2)
1021 goto syntax;
1022 m = MATCH_NO;
1023 goto cleanup; /* Could be a complex constant */
1026 if (gfc_match_char (',') != MATCH_YES)
1028 if (n > 2)
1029 goto syntax;
1030 m = MATCH_NO;
1031 goto cleanup;
1035 if (gfc_match_char (')') != MATCH_YES)
1036 goto syntax;
1038 if (check_duplicate_iterator (head, iter.var->symtree->n.sym))
1040 m = MATCH_ERROR;
1041 goto cleanup;
1044 e = gfc_get_array_expr (BT_UNKNOWN, 0, &old_loc);
1045 e->value.constructor = head;
1047 p = gfc_constructor_append_expr (result, e, &gfc_current_locus);
1048 p->iterator = gfc_get_iterator ();
1049 *p->iterator = iter;
1051 return MATCH_YES;
1053 syntax:
1054 gfc_error ("Syntax error in array constructor at %C");
1055 m = MATCH_ERROR;
1057 cleanup:
1058 gfc_constructor_free (head);
1059 gfc_free_iterator (&iter, 0);
1060 gfc_current_locus = old_loc;
1061 return m;
1065 /* Match a single element of an array constructor, which can be a
1066 single expression or a list of elements. */
1068 static match
1069 match_array_cons_element (gfc_constructor_base *result)
1071 gfc_expr *expr;
1072 match m;
1074 m = match_array_list (result);
1075 if (m != MATCH_NO)
1076 return m;
1078 m = gfc_match_expr (&expr);
1079 if (m != MATCH_YES)
1080 return m;
1082 gfc_constructor_append_expr (result, expr, &gfc_current_locus);
1083 return MATCH_YES;
1087 /* Match an array constructor. */
1089 match
1090 gfc_match_array_constructor (gfc_expr **result)
1092 gfc_constructor *c;
1093 gfc_constructor_base head;
1094 gfc_expr *expr;
1095 gfc_typespec ts;
1096 locus where;
1097 match m;
1098 const char *end_delim;
1099 bool seen_ts;
1101 head = NULL;
1102 seen_ts = false;
1104 if (gfc_match (" (/") == MATCH_NO)
1106 if (gfc_match (" [") == MATCH_NO)
1107 return MATCH_NO;
1108 else
1110 if (!gfc_notify_std (GFC_STD_F2003, "[...] "
1111 "style array constructors at %C"))
1112 return MATCH_ERROR;
1113 end_delim = " ]";
1116 else
1117 end_delim = " /)";
1119 where = gfc_current_locus;
1121 /* Try to match an optional "type-spec ::" */
1122 gfc_clear_ts (&ts);
1123 m = gfc_match_type_spec (&ts);
1124 if (m == MATCH_YES)
1126 seen_ts = (gfc_match (" ::") == MATCH_YES);
1128 if (seen_ts)
1130 if (!gfc_notify_std (GFC_STD_F2003, "Array constructor "
1131 "including type specification at %C"))
1132 goto cleanup;
1134 if (ts.deferred)
1136 gfc_error ("Type-spec at %L cannot contain a deferred "
1137 "type parameter", &where);
1138 goto cleanup;
1141 if (ts.type == BT_CHARACTER
1142 && ts.u.cl && !ts.u.cl->length && !ts.u.cl->length_from_typespec)
1144 gfc_error ("Type-spec at %L cannot contain an asterisk for a "
1145 "type parameter", &where);
1146 goto cleanup;
1150 else if (m == MATCH_ERROR)
1151 goto cleanup;
1153 if (!seen_ts)
1154 gfc_current_locus = where;
1156 if (gfc_match (end_delim) == MATCH_YES)
1158 if (seen_ts)
1159 goto done;
1160 else
1162 gfc_error ("Empty array constructor at %C is not allowed");
1163 goto cleanup;
1167 for (;;)
1169 m = match_array_cons_element (&head);
1170 if (m == MATCH_ERROR)
1171 goto cleanup;
1172 if (m == MATCH_NO)
1173 goto syntax;
1175 if (gfc_match_char (',') == MATCH_NO)
1176 break;
1179 if (gfc_match (end_delim) == MATCH_NO)
1180 goto syntax;
1182 done:
1183 /* Size must be calculated at resolution time. */
1184 if (seen_ts)
1186 expr = gfc_get_array_expr (ts.type, ts.kind, &where);
1187 expr->ts = ts;
1189 /* If the typespec is CHARACTER, check that array elements can
1190 be converted. See PR fortran/67803. */
1191 if (ts.type == BT_CHARACTER)
1193 c = gfc_constructor_first (head);
1194 for (; c; c = gfc_constructor_next (c))
1196 if (gfc_numeric_ts (&c->expr->ts)
1197 || c->expr->ts.type == BT_LOGICAL)
1199 gfc_error ("Incompatible typespec for array element at %L",
1200 &c->expr->where);
1201 return MATCH_ERROR;
1204 /* Special case null(). */
1205 if (c->expr->expr_type == EXPR_FUNCTION
1206 && c->expr->ts.type == BT_UNKNOWN
1207 && strcmp (c->expr->symtree->name, "null") == 0)
1209 gfc_error ("Incompatible typespec for array element at %L",
1210 &c->expr->where);
1211 return MATCH_ERROR;
1216 /* Walk the constructor and ensure type conversion for numeric types. */
1217 if (gfc_numeric_ts (&ts))
1219 c = gfc_constructor_first (head);
1220 for (; c; c = gfc_constructor_next (c))
1221 gfc_convert_type (c->expr, &ts, 1);
1224 else
1225 expr = gfc_get_array_expr (BT_UNKNOWN, 0, &where);
1227 expr->value.constructor = head;
1228 if (expr->ts.u.cl)
1229 expr->ts.u.cl->length_from_typespec = seen_ts;
1231 *result = expr;
1233 return MATCH_YES;
1235 syntax:
1236 gfc_error ("Syntax error in array constructor at %C");
1238 cleanup:
1239 gfc_constructor_free (head);
1240 return MATCH_ERROR;
1245 /************** Check array constructors for correctness **************/
1247 /* Given an expression, compare it's type with the type of the current
1248 constructor. Returns nonzero if an error was issued. The
1249 cons_state variable keeps track of whether the type of the
1250 constructor being read or resolved is known to be good, bad or just
1251 starting out. */
1253 static gfc_typespec constructor_ts;
1254 static enum
1255 { CONS_START, CONS_GOOD, CONS_BAD }
1256 cons_state;
1258 static int
1259 check_element_type (gfc_expr *expr, bool convert)
1261 if (cons_state == CONS_BAD)
1262 return 0; /* Suppress further errors */
1264 if (cons_state == CONS_START)
1266 if (expr->ts.type == BT_UNKNOWN)
1267 cons_state = CONS_BAD;
1268 else
1270 cons_state = CONS_GOOD;
1271 constructor_ts = expr->ts;
1274 return 0;
1277 if (gfc_compare_types (&constructor_ts, &expr->ts))
1278 return 0;
1280 if (convert)
1281 return gfc_convert_type(expr, &constructor_ts, 1) ? 0 : 1;
1283 gfc_error ("Element in %s array constructor at %L is %s",
1284 gfc_typename (&constructor_ts), &expr->where,
1285 gfc_typename (&expr->ts));
1287 cons_state = CONS_BAD;
1288 return 1;
1292 /* Recursive work function for gfc_check_constructor_type(). */
1294 static bool
1295 check_constructor_type (gfc_constructor_base base, bool convert)
1297 gfc_constructor *c;
1298 gfc_expr *e;
1300 for (c = gfc_constructor_first (base); c; c = gfc_constructor_next (c))
1302 e = c->expr;
1304 if (e->expr_type == EXPR_ARRAY)
1306 if (!check_constructor_type (e->value.constructor, convert))
1307 return false;
1309 continue;
1312 if (check_element_type (e, convert))
1313 return false;
1316 return true;
1320 /* Check that all elements of an array constructor are the same type.
1321 On false, an error has been generated. */
1323 bool
1324 gfc_check_constructor_type (gfc_expr *e)
1326 bool t;
1328 if (e->ts.type != BT_UNKNOWN)
1330 cons_state = CONS_GOOD;
1331 constructor_ts = e->ts;
1333 else
1335 cons_state = CONS_START;
1336 gfc_clear_ts (&constructor_ts);
1339 /* If e->ts.type != BT_UNKNOWN, the array constructor included a
1340 typespec, and we will now convert the values on the fly. */
1341 t = check_constructor_type (e->value.constructor, e->ts.type != BT_UNKNOWN);
1342 if (t && e->ts.type == BT_UNKNOWN)
1343 e->ts = constructor_ts;
1345 return t;
1350 typedef struct cons_stack
1352 gfc_iterator *iterator;
1353 struct cons_stack *previous;
1355 cons_stack;
1357 static cons_stack *base;
1359 static bool check_constructor (gfc_constructor_base, bool (*) (gfc_expr *));
1361 /* Check an EXPR_VARIABLE expression in a constructor to make sure
1362 that that variable is an iteration variables. */
1364 bool
1365 gfc_check_iter_variable (gfc_expr *expr)
1367 gfc_symbol *sym;
1368 cons_stack *c;
1370 sym = expr->symtree->n.sym;
1372 for (c = base; c && c->iterator; c = c->previous)
1373 if (sym == c->iterator->var->symtree->n.sym)
1374 return true;
1376 return false;
1380 /* Recursive work function for gfc_check_constructor(). This amounts
1381 to calling the check function for each expression in the
1382 constructor, giving variables with the names of iterators a pass. */
1384 static bool
1385 check_constructor (gfc_constructor_base ctor, bool (*check_function) (gfc_expr *))
1387 cons_stack element;
1388 gfc_expr *e;
1389 bool t;
1390 gfc_constructor *c;
1392 for (c = gfc_constructor_first (ctor); c; c = gfc_constructor_next (c))
1394 e = c->expr;
1396 if (!e)
1397 continue;
1399 if (e->expr_type != EXPR_ARRAY)
1401 if (!(*check_function)(e))
1402 return false;
1403 continue;
1406 element.previous = base;
1407 element.iterator = c->iterator;
1409 base = &element;
1410 t = check_constructor (e->value.constructor, check_function);
1411 base = element.previous;
1413 if (!t)
1414 return false;
1417 /* Nothing went wrong, so all OK. */
1418 return true;
1422 /* Checks a constructor to see if it is a particular kind of
1423 expression -- specification, restricted, or initialization as
1424 determined by the check_function. */
1426 bool
1427 gfc_check_constructor (gfc_expr *expr, bool (*check_function) (gfc_expr *))
1429 cons_stack *base_save;
1430 bool t;
1432 base_save = base;
1433 base = NULL;
1435 t = check_constructor (expr->value.constructor, check_function);
1436 base = base_save;
1438 return t;
1443 /**************** Simplification of array constructors ****************/
1445 iterator_stack *iter_stack;
1447 typedef struct
1449 gfc_constructor_base base;
1450 int extract_count, extract_n;
1451 gfc_expr *extracted;
1452 mpz_t *count;
1454 mpz_t *offset;
1455 gfc_component *component;
1456 mpz_t *repeat;
1458 bool (*expand_work_function) (gfc_expr *);
1460 expand_info;
1462 static expand_info current_expand;
1464 static bool expand_constructor (gfc_constructor_base);
1467 /* Work function that counts the number of elements present in a
1468 constructor. */
1470 static bool
1471 count_elements (gfc_expr *e)
1473 mpz_t result;
1475 if (e->rank == 0)
1476 mpz_add_ui (*current_expand.count, *current_expand.count, 1);
1477 else
1479 if (!gfc_array_size (e, &result))
1481 gfc_free_expr (e);
1482 return false;
1485 mpz_add (*current_expand.count, *current_expand.count, result);
1486 mpz_clear (result);
1489 gfc_free_expr (e);
1490 return true;
1494 /* Work function that extracts a particular element from an array
1495 constructor, freeing the rest. */
1497 static bool
1498 extract_element (gfc_expr *e)
1500 if (e->rank != 0)
1501 { /* Something unextractable */
1502 gfc_free_expr (e);
1503 return false;
1506 if (current_expand.extract_count == current_expand.extract_n)
1507 current_expand.extracted = e;
1508 else
1509 gfc_free_expr (e);
1511 current_expand.extract_count++;
1513 return true;
1517 /* Work function that constructs a new constructor out of the old one,
1518 stringing new elements together. */
1520 static bool
1521 expand (gfc_expr *e)
1523 gfc_constructor *c = gfc_constructor_append_expr (&current_expand.base,
1524 e, &e->where);
1526 c->n.component = current_expand.component;
1527 return true;
1531 /* Given an initialization expression that is a variable reference,
1532 substitute the current value of the iteration variable. */
1534 void
1535 gfc_simplify_iterator_var (gfc_expr *e)
1537 iterator_stack *p;
1539 for (p = iter_stack; p; p = p->prev)
1540 if (e->symtree == p->variable)
1541 break;
1543 if (p == NULL)
1544 return; /* Variable not found */
1546 gfc_replace_expr (e, gfc_get_int_expr (gfc_default_integer_kind, NULL, 0));
1548 mpz_set (e->value.integer, p->value);
1550 return;
1554 /* Expand an expression with that is inside of a constructor,
1555 recursing into other constructors if present. */
1557 static bool
1558 expand_expr (gfc_expr *e)
1560 if (e->expr_type == EXPR_ARRAY)
1561 return expand_constructor (e->value.constructor);
1563 e = gfc_copy_expr (e);
1565 if (!gfc_simplify_expr (e, 1))
1567 gfc_free_expr (e);
1568 return false;
1571 return current_expand.expand_work_function (e);
1575 static bool
1576 expand_iterator (gfc_constructor *c)
1578 gfc_expr *start, *end, *step;
1579 iterator_stack frame;
1580 mpz_t trip;
1581 bool t;
1583 end = step = NULL;
1585 t = false;
1587 mpz_init (trip);
1588 mpz_init (frame.value);
1589 frame.prev = NULL;
1591 start = gfc_copy_expr (c->iterator->start);
1592 if (!gfc_simplify_expr (start, 1))
1593 goto cleanup;
1595 if (start->expr_type != EXPR_CONSTANT || start->ts.type != BT_INTEGER)
1596 goto cleanup;
1598 end = gfc_copy_expr (c->iterator->end);
1599 if (!gfc_simplify_expr (end, 1))
1600 goto cleanup;
1602 if (end->expr_type != EXPR_CONSTANT || end->ts.type != BT_INTEGER)
1603 goto cleanup;
1605 step = gfc_copy_expr (c->iterator->step);
1606 if (!gfc_simplify_expr (step, 1))
1607 goto cleanup;
1609 if (step->expr_type != EXPR_CONSTANT || step->ts.type != BT_INTEGER)
1610 goto cleanup;
1612 if (mpz_sgn (step->value.integer) == 0)
1614 gfc_error ("Iterator step at %L cannot be zero", &step->where);
1615 goto cleanup;
1618 /* Calculate the trip count of the loop. */
1619 mpz_sub (trip, end->value.integer, start->value.integer);
1620 mpz_add (trip, trip, step->value.integer);
1621 mpz_tdiv_q (trip, trip, step->value.integer);
1623 mpz_set (frame.value, start->value.integer);
1625 frame.prev = iter_stack;
1626 frame.variable = c->iterator->var->symtree;
1627 iter_stack = &frame;
1629 while (mpz_sgn (trip) > 0)
1631 if (!expand_expr (c->expr))
1632 goto cleanup;
1634 mpz_add (frame.value, frame.value, step->value.integer);
1635 mpz_sub_ui (trip, trip, 1);
1638 t = true;
1640 cleanup:
1641 gfc_free_expr (start);
1642 gfc_free_expr (end);
1643 gfc_free_expr (step);
1645 mpz_clear (trip);
1646 mpz_clear (frame.value);
1648 iter_stack = frame.prev;
1650 return t;
1654 /* Expand a constructor into constant constructors without any
1655 iterators, calling the work function for each of the expanded
1656 expressions. The work function needs to either save or free the
1657 passed expression. */
1659 static bool
1660 expand_constructor (gfc_constructor_base base)
1662 gfc_constructor *c;
1663 gfc_expr *e;
1665 for (c = gfc_constructor_first (base); c; c = gfc_constructor_next(c))
1667 if (c->iterator != NULL)
1669 if (!expand_iterator (c))
1670 return false;
1671 continue;
1674 e = c->expr;
1676 if (e->expr_type == EXPR_ARRAY)
1678 if (!expand_constructor (e->value.constructor))
1679 return false;
1681 continue;
1684 e = gfc_copy_expr (e);
1685 if (!gfc_simplify_expr (e, 1))
1687 gfc_free_expr (e);
1688 return false;
1690 current_expand.offset = &c->offset;
1691 current_expand.repeat = &c->repeat;
1692 current_expand.component = c->n.component;
1693 if (!current_expand.expand_work_function(e))
1694 return false;
1696 return true;
1700 /* Given an array expression and an element number (starting at zero),
1701 return a pointer to the array element. NULL is returned if the
1702 size of the array has been exceeded. The expression node returned
1703 remains a part of the array and should not be freed. Access is not
1704 efficient at all, but this is another place where things do not
1705 have to be particularly fast. */
1707 static gfc_expr *
1708 gfc_get_array_element (gfc_expr *array, int element)
1710 expand_info expand_save;
1711 gfc_expr *e;
1712 bool rc;
1714 expand_save = current_expand;
1715 current_expand.extract_n = element;
1716 current_expand.expand_work_function = extract_element;
1717 current_expand.extracted = NULL;
1718 current_expand.extract_count = 0;
1720 iter_stack = NULL;
1722 rc = expand_constructor (array->value.constructor);
1723 e = current_expand.extracted;
1724 current_expand = expand_save;
1726 if (!rc)
1727 return NULL;
1729 return e;
1733 /* Top level subroutine for expanding constructors. We only expand
1734 constructor if they are small enough. */
1736 bool
1737 gfc_expand_constructor (gfc_expr *e, bool fatal)
1739 expand_info expand_save;
1740 gfc_expr *f;
1741 bool rc;
1743 /* If we can successfully get an array element at the max array size then
1744 the array is too big to expand, so we just return. */
1745 f = gfc_get_array_element (e, flag_max_array_constructor);
1746 if (f != NULL)
1748 gfc_free_expr (f);
1749 if (fatal)
1751 gfc_error ("The number of elements in the array constructor "
1752 "at %L requires an increase of the allowed %d "
1753 "upper limit. See %<-fmax-array-constructor%> "
1754 "option", &e->where, flag_max_array_constructor);
1755 return false;
1757 return true;
1760 /* We now know the array is not too big so go ahead and try to expand it. */
1761 expand_save = current_expand;
1762 current_expand.base = NULL;
1764 iter_stack = NULL;
1766 current_expand.expand_work_function = expand;
1768 if (!expand_constructor (e->value.constructor))
1770 gfc_constructor_free (current_expand.base);
1771 rc = false;
1772 goto done;
1775 gfc_constructor_free (e->value.constructor);
1776 e->value.constructor = current_expand.base;
1778 rc = true;
1780 done:
1781 current_expand = expand_save;
1783 return rc;
1787 /* Work function for checking that an element of a constructor is a
1788 constant, after removal of any iteration variables. We return
1789 false if not so. */
1791 static bool
1792 is_constant_element (gfc_expr *e)
1794 int rv;
1796 rv = gfc_is_constant_expr (e);
1797 gfc_free_expr (e);
1799 return rv ? true : false;
1803 /* Given an array constructor, determine if the constructor is
1804 constant or not by expanding it and making sure that all elements
1805 are constants. This is a bit of a hack since something like (/ (i,
1806 i=1,100000000) /) will take a while as* opposed to a more clever
1807 function that traverses the expression tree. FIXME. */
1810 gfc_constant_ac (gfc_expr *e)
1812 expand_info expand_save;
1813 bool rc;
1815 iter_stack = NULL;
1816 expand_save = current_expand;
1817 current_expand.expand_work_function = is_constant_element;
1819 rc = expand_constructor (e->value.constructor);
1821 current_expand = expand_save;
1822 if (!rc)
1823 return 0;
1825 return 1;
1829 /* Returns nonzero if an array constructor has been completely
1830 expanded (no iterators) and zero if iterators are present. */
1833 gfc_expanded_ac (gfc_expr *e)
1835 gfc_constructor *c;
1837 if (e->expr_type == EXPR_ARRAY)
1838 for (c = gfc_constructor_first (e->value.constructor);
1839 c; c = gfc_constructor_next (c))
1840 if (c->iterator != NULL || !gfc_expanded_ac (c->expr))
1841 return 0;
1843 return 1;
1847 /*************** Type resolution of array constructors ***************/
1850 /* The symbol expr_is_sought_symbol_ref will try to find. */
1851 static const gfc_symbol *sought_symbol = NULL;
1854 /* Tells whether the expression E is a variable reference to the symbol
1855 in the static variable SOUGHT_SYMBOL, and sets the locus pointer WHERE
1856 accordingly.
1857 To be used with gfc_expr_walker: if a reference is found we don't need
1858 to look further so we return 1 to skip any further walk. */
1860 static int
1861 expr_is_sought_symbol_ref (gfc_expr **e, int *walk_subtrees ATTRIBUTE_UNUSED,
1862 void *where)
1864 gfc_expr *expr = *e;
1865 locus *sym_loc = (locus *)where;
1867 if (expr->expr_type == EXPR_VARIABLE
1868 && expr->symtree->n.sym == sought_symbol)
1870 *sym_loc = expr->where;
1871 return 1;
1874 return 0;
1878 /* Tells whether the expression EXPR contains a reference to the symbol
1879 SYM and in that case sets the position SYM_LOC where the reference is. */
1881 static bool
1882 find_symbol_in_expr (gfc_symbol *sym, gfc_expr *expr, locus *sym_loc)
1884 int ret;
1886 sought_symbol = sym;
1887 ret = gfc_expr_walker (&expr, &expr_is_sought_symbol_ref, sym_loc);
1888 sought_symbol = NULL;
1889 return ret;
1893 /* Recursive array list resolution function. All of the elements must
1894 be of the same type. */
1896 static bool
1897 resolve_array_list (gfc_constructor_base base)
1899 bool t;
1900 gfc_constructor *c;
1901 gfc_iterator *iter;
1903 t = true;
1905 for (c = gfc_constructor_first (base); c; c = gfc_constructor_next (c))
1907 iter = c->iterator;
1908 if (iter != NULL)
1910 gfc_symbol *iter_var;
1911 locus iter_var_loc;
1913 if (!gfc_resolve_iterator (iter, false, true))
1914 t = false;
1916 /* Check for bounds referencing the iterator variable. */
1917 gcc_assert (iter->var->expr_type == EXPR_VARIABLE);
1918 iter_var = iter->var->symtree->n.sym;
1919 if (find_symbol_in_expr (iter_var, iter->start, &iter_var_loc))
1921 if (!gfc_notify_std (GFC_STD_LEGACY, "AC-IMPLIED-DO initial "
1922 "expression references control variable "
1923 "at %L", &iter_var_loc))
1924 t = false;
1926 if (find_symbol_in_expr (iter_var, iter->end, &iter_var_loc))
1928 if (!gfc_notify_std (GFC_STD_LEGACY, "AC-IMPLIED-DO final "
1929 "expression references control variable "
1930 "at %L", &iter_var_loc))
1931 t = false;
1933 if (find_symbol_in_expr (iter_var, iter->step, &iter_var_loc))
1935 if (!gfc_notify_std (GFC_STD_LEGACY, "AC-IMPLIED-DO step "
1936 "expression references control variable "
1937 "at %L", &iter_var_loc))
1938 t = false;
1942 if (!gfc_resolve_expr (c->expr))
1943 t = false;
1945 if (UNLIMITED_POLY (c->expr))
1947 gfc_error ("Array constructor value at %L shall not be unlimited "
1948 "polymorphic [F2008: C4106]", &c->expr->where);
1949 t = false;
1953 return t;
1956 /* Resolve character array constructor. If it has a specified constant character
1957 length, pad/truncate the elements here; if the length is not specified and
1958 all elements are of compile-time known length, emit an error as this is
1959 invalid. */
1961 bool
1962 gfc_resolve_character_array_constructor (gfc_expr *expr)
1964 gfc_constructor *p;
1965 int found_length;
1967 gcc_assert (expr->expr_type == EXPR_ARRAY);
1968 gcc_assert (expr->ts.type == BT_CHARACTER);
1970 if (expr->ts.u.cl == NULL)
1972 for (p = gfc_constructor_first (expr->value.constructor);
1973 p; p = gfc_constructor_next (p))
1974 if (p->expr->ts.u.cl != NULL)
1976 /* Ensure that if there is a char_len around that it is
1977 used; otherwise the middle-end confuses them! */
1978 expr->ts.u.cl = p->expr->ts.u.cl;
1979 goto got_charlen;
1982 expr->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
1985 got_charlen:
1987 found_length = -1;
1989 if (expr->ts.u.cl->length == NULL)
1991 /* Check that all constant string elements have the same length until
1992 we reach the end or find a variable-length one. */
1994 for (p = gfc_constructor_first (expr->value.constructor);
1995 p; p = gfc_constructor_next (p))
1997 int current_length = -1;
1998 gfc_ref *ref;
1999 for (ref = p->expr->ref; ref; ref = ref->next)
2000 if (ref->type == REF_SUBSTRING
2001 && ref->u.ss.start->expr_type == EXPR_CONSTANT
2002 && ref->u.ss.end->expr_type == EXPR_CONSTANT)
2003 break;
2005 if (p->expr->expr_type == EXPR_CONSTANT)
2006 current_length = p->expr->value.character.length;
2007 else if (ref)
2009 long j;
2010 j = mpz_get_ui (ref->u.ss.end->value.integer)
2011 - mpz_get_ui (ref->u.ss.start->value.integer) + 1;
2012 current_length = (int) j;
2014 else if (p->expr->ts.u.cl && p->expr->ts.u.cl->length
2015 && p->expr->ts.u.cl->length->expr_type == EXPR_CONSTANT)
2017 long j;
2018 j = mpz_get_si (p->expr->ts.u.cl->length->value.integer);
2019 current_length = (int) j;
2021 else
2022 return true;
2024 gcc_assert (current_length != -1);
2026 if (found_length == -1)
2027 found_length = current_length;
2028 else if (found_length != current_length)
2030 gfc_error ("Different CHARACTER lengths (%d/%d) in array"
2031 " constructor at %L", found_length, current_length,
2032 &p->expr->where);
2033 return false;
2036 gcc_assert (found_length == current_length);
2039 gcc_assert (found_length != -1);
2041 /* Update the character length of the array constructor. */
2042 expr->ts.u.cl->length = gfc_get_int_expr (gfc_charlen_int_kind,
2043 NULL, found_length);
2045 else
2047 /* We've got a character length specified. It should be an integer,
2048 otherwise an error is signalled elsewhere. */
2049 gcc_assert (expr->ts.u.cl->length);
2051 /* If we've got a constant character length, pad according to this.
2052 gfc_extract_int does check for BT_INTEGER and EXPR_CONSTANT and sets
2053 max_length only if they pass. */
2054 gfc_extract_int (expr->ts.u.cl->length, &found_length);
2056 /* Now pad/truncate the elements accordingly to the specified character
2057 length. This is ok inside this conditional, as in the case above
2058 (without typespec) all elements are verified to have the same length
2059 anyway. */
2060 if (found_length != -1)
2061 for (p = gfc_constructor_first (expr->value.constructor);
2062 p; p = gfc_constructor_next (p))
2063 if (p->expr->expr_type == EXPR_CONSTANT)
2065 gfc_expr *cl = NULL;
2066 int current_length = -1;
2067 bool has_ts;
2069 if (p->expr->ts.u.cl && p->expr->ts.u.cl->length)
2071 cl = p->expr->ts.u.cl->length;
2072 gfc_extract_int (cl, &current_length);
2075 /* If gfc_extract_int above set current_length, we implicitly
2076 know the type is BT_INTEGER and it's EXPR_CONSTANT. */
2078 has_ts = expr->ts.u.cl->length_from_typespec;
2080 if (! cl
2081 || (current_length != -1 && current_length != found_length))
2082 gfc_set_constant_character_len (found_length, p->expr,
2083 has_ts ? -1 : found_length);
2087 return true;
2091 /* Resolve all of the expressions in an array list. */
2093 bool
2094 gfc_resolve_array_constructor (gfc_expr *expr)
2096 bool t;
2098 t = resolve_array_list (expr->value.constructor);
2099 if (t)
2100 t = gfc_check_constructor_type (expr);
2102 /* gfc_resolve_character_array_constructor is called in gfc_resolve_expr after
2103 the call to this function, so we don't need to call it here; if it was
2104 called twice, an error message there would be duplicated. */
2106 return t;
2110 /* Copy an iterator structure. */
2112 gfc_iterator *
2113 gfc_copy_iterator (gfc_iterator *src)
2115 gfc_iterator *dest;
2117 if (src == NULL)
2118 return NULL;
2120 dest = gfc_get_iterator ();
2122 dest->var = gfc_copy_expr (src->var);
2123 dest->start = gfc_copy_expr (src->start);
2124 dest->end = gfc_copy_expr (src->end);
2125 dest->step = gfc_copy_expr (src->step);
2126 dest->unroll = src->unroll;
2128 return dest;
2132 /********* Subroutines for determining the size of an array *********/
2134 /* These are needed just to accommodate RESHAPE(). There are no
2135 diagnostics here, we just return a negative number if something
2136 goes wrong. */
2139 /* Get the size of single dimension of an array specification. The
2140 array is guaranteed to be one dimensional. */
2142 bool
2143 spec_dimen_size (gfc_array_spec *as, int dimen, mpz_t *result)
2145 if (as == NULL)
2146 return false;
2148 if (dimen < 0 || dimen > as->rank - 1)
2149 gfc_internal_error ("spec_dimen_size(): Bad dimension");
2151 if (as->type != AS_EXPLICIT
2152 || as->lower[dimen]->expr_type != EXPR_CONSTANT
2153 || as->upper[dimen]->expr_type != EXPR_CONSTANT
2154 || as->lower[dimen]->ts.type != BT_INTEGER
2155 || as->upper[dimen]->ts.type != BT_INTEGER)
2156 return false;
2158 mpz_init (*result);
2160 mpz_sub (*result, as->upper[dimen]->value.integer,
2161 as->lower[dimen]->value.integer);
2163 mpz_add_ui (*result, *result, 1);
2165 return true;
2169 bool
2170 spec_size (gfc_array_spec *as, mpz_t *result)
2172 mpz_t size;
2173 int d;
2175 if (!as || as->type == AS_ASSUMED_RANK)
2176 return false;
2178 mpz_init_set_ui (*result, 1);
2180 for (d = 0; d < as->rank; d++)
2182 if (!spec_dimen_size (as, d, &size))
2184 mpz_clear (*result);
2185 return false;
2188 mpz_mul (*result, *result, size);
2189 mpz_clear (size);
2192 return true;
2196 /* Get the number of elements in an array section. Optionally, also supply
2197 the end value. */
2199 bool
2200 gfc_ref_dimen_size (gfc_array_ref *ar, int dimen, mpz_t *result, mpz_t *end)
2202 mpz_t upper, lower, stride;
2203 mpz_t diff;
2204 bool t;
2205 gfc_expr *stride_expr = NULL;
2207 if (dimen < 0 || ar == NULL)
2208 gfc_internal_error ("gfc_ref_dimen_size(): Bad dimension");
2210 if (dimen > ar->dimen - 1)
2212 gfc_error ("Bad array dimension at %L", &ar->c_where[dimen]);
2213 return false;
2216 switch (ar->dimen_type[dimen])
2218 case DIMEN_ELEMENT:
2219 mpz_init (*result);
2220 mpz_set_ui (*result, 1);
2221 t = true;
2222 break;
2224 case DIMEN_VECTOR:
2225 t = gfc_array_size (ar->start[dimen], result); /* Recurse! */
2226 break;
2228 case DIMEN_RANGE:
2230 mpz_init (stride);
2232 if (ar->stride[dimen] == NULL)
2233 mpz_set_ui (stride, 1);
2234 else
2236 stride_expr = gfc_copy_expr(ar->stride[dimen]);
2237 if(!gfc_simplify_expr(stride_expr, 1))
2238 gfc_internal_error("Simplification error");
2239 if (stride_expr->expr_type != EXPR_CONSTANT)
2241 mpz_clear (stride);
2242 return false;
2244 mpz_set (stride, stride_expr->value.integer);
2245 gfc_free_expr(stride_expr);
2248 /* Calculate the number of elements via gfc_dep_differce, but only if
2249 start and end are both supplied in the reference or the array spec.
2250 This is to guard against strange but valid code like
2252 subroutine foo(a,n)
2253 real a(1:n)
2254 n = 3
2255 print *,size(a(n-1:))
2257 where the user changes the value of a variable. If we have to
2258 determine end as well, we cannot do this using gfc_dep_difference.
2259 Fall back to the constants-only code then. */
2261 if (end == NULL)
2263 bool use_dep;
2265 use_dep = gfc_dep_difference (ar->end[dimen], ar->start[dimen],
2266 &diff);
2267 if (!use_dep && ar->end[dimen] == NULL && ar->start[dimen] == NULL)
2268 use_dep = gfc_dep_difference (ar->as->upper[dimen],
2269 ar->as->lower[dimen], &diff);
2271 if (use_dep)
2273 mpz_init (*result);
2274 mpz_add (*result, diff, stride);
2275 mpz_div (*result, *result, stride);
2276 if (mpz_cmp_ui (*result, 0) < 0)
2277 mpz_set_ui (*result, 0);
2279 mpz_clear (stride);
2280 mpz_clear (diff);
2281 return true;
2286 /* Constant-only code here, which covers more cases
2287 like a(:4) etc. */
2288 mpz_init (upper);
2289 mpz_init (lower);
2290 t = false;
2292 if (ar->start[dimen] == NULL)
2294 if (ar->as->lower[dimen] == NULL
2295 || ar->as->lower[dimen]->expr_type != EXPR_CONSTANT
2296 || ar->as->lower[dimen]->ts.type != BT_INTEGER)
2297 goto cleanup;
2298 mpz_set (lower, ar->as->lower[dimen]->value.integer);
2300 else
2302 if (ar->start[dimen]->expr_type != EXPR_CONSTANT)
2303 goto cleanup;
2304 mpz_set (lower, ar->start[dimen]->value.integer);
2307 if (ar->end[dimen] == NULL)
2309 if (ar->as->upper[dimen] == NULL
2310 || ar->as->upper[dimen]->expr_type != EXPR_CONSTANT
2311 || ar->as->upper[dimen]->ts.type != BT_INTEGER)
2312 goto cleanup;
2313 mpz_set (upper, ar->as->upper[dimen]->value.integer);
2315 else
2317 if (ar->end[dimen]->expr_type != EXPR_CONSTANT)
2318 goto cleanup;
2319 mpz_set (upper, ar->end[dimen]->value.integer);
2322 mpz_init (*result);
2323 mpz_sub (*result, upper, lower);
2324 mpz_add (*result, *result, stride);
2325 mpz_div (*result, *result, stride);
2327 /* Zero stride caught earlier. */
2328 if (mpz_cmp_ui (*result, 0) < 0)
2329 mpz_set_ui (*result, 0);
2330 t = true;
2332 if (end)
2334 mpz_init (*end);
2336 mpz_sub_ui (*end, *result, 1UL);
2337 mpz_mul (*end, *end, stride);
2338 mpz_add (*end, *end, lower);
2341 cleanup:
2342 mpz_clear (upper);
2343 mpz_clear (lower);
2344 mpz_clear (stride);
2345 return t;
2347 default:
2348 gfc_internal_error ("gfc_ref_dimen_size(): Bad dimen_type");
2351 return t;
2355 static bool
2356 ref_size (gfc_array_ref *ar, mpz_t *result)
2358 mpz_t size;
2359 int d;
2361 mpz_init_set_ui (*result, 1);
2363 for (d = 0; d < ar->dimen; d++)
2365 if (!gfc_ref_dimen_size (ar, d, &size, NULL))
2367 mpz_clear (*result);
2368 return false;
2371 mpz_mul (*result, *result, size);
2372 mpz_clear (size);
2375 return true;
2379 /* Given an array expression and a dimension, figure out how many
2380 elements it has along that dimension. Returns true if we were
2381 able to return a result in the 'result' variable, false
2382 otherwise. */
2384 bool
2385 gfc_array_dimen_size (gfc_expr *array, int dimen, mpz_t *result)
2387 gfc_ref *ref;
2388 int i;
2390 gcc_assert (array != NULL);
2392 if (array->ts.type == BT_CLASS)
2393 return false;
2395 if (array->rank == -1)
2396 return false;
2398 if (dimen < 0 || dimen > array->rank - 1)
2399 gfc_internal_error ("gfc_array_dimen_size(): Bad dimension");
2401 switch (array->expr_type)
2403 case EXPR_VARIABLE:
2404 case EXPR_FUNCTION:
2405 for (ref = array->ref; ref; ref = ref->next)
2407 if (ref->type != REF_ARRAY)
2408 continue;
2410 if (ref->u.ar.type == AR_FULL)
2411 return spec_dimen_size (ref->u.ar.as, dimen, result);
2413 if (ref->u.ar.type == AR_SECTION)
2415 for (i = 0; dimen >= 0; i++)
2416 if (ref->u.ar.dimen_type[i] != DIMEN_ELEMENT)
2417 dimen--;
2419 return gfc_ref_dimen_size (&ref->u.ar, i - 1, result, NULL);
2423 if (array->shape && array->shape[dimen])
2425 mpz_init_set (*result, array->shape[dimen]);
2426 return true;
2429 if (array->symtree->n.sym->attr.generic
2430 && array->value.function.esym != NULL)
2432 if (!spec_dimen_size (array->value.function.esym->as, dimen, result))
2433 return false;
2435 else if (!spec_dimen_size (array->symtree->n.sym->as, dimen, result))
2436 return false;
2438 break;
2440 case EXPR_ARRAY:
2441 if (array->shape == NULL) {
2442 /* Expressions with rank > 1 should have "shape" properly set */
2443 if ( array->rank != 1 )
2444 gfc_internal_error ("gfc_array_dimen_size(): Bad EXPR_ARRAY expr");
2445 return gfc_array_size(array, result);
2448 /* Fall through */
2449 default:
2450 if (array->shape == NULL)
2451 return false;
2453 mpz_init_set (*result, array->shape[dimen]);
2455 break;
2458 return true;
2462 /* Given an array expression, figure out how many elements are in the
2463 array. Returns true if this is possible, and sets the 'result'
2464 variable. Otherwise returns false. */
2466 bool
2467 gfc_array_size (gfc_expr *array, mpz_t *result)
2469 expand_info expand_save;
2470 gfc_ref *ref;
2471 int i;
2472 bool t;
2474 if (array->ts.type == BT_CLASS)
2475 return false;
2477 switch (array->expr_type)
2479 case EXPR_ARRAY:
2480 gfc_push_suppress_errors ();
2482 expand_save = current_expand;
2484 current_expand.count = result;
2485 mpz_init_set_ui (*result, 0);
2487 current_expand.expand_work_function = count_elements;
2488 iter_stack = NULL;
2490 t = expand_constructor (array->value.constructor);
2492 gfc_pop_suppress_errors ();
2494 if (!t)
2495 mpz_clear (*result);
2496 current_expand = expand_save;
2497 return t;
2499 case EXPR_VARIABLE:
2500 for (ref = array->ref; ref; ref = ref->next)
2502 if (ref->type != REF_ARRAY)
2503 continue;
2505 if (ref->u.ar.type == AR_FULL)
2506 return spec_size (ref->u.ar.as, result);
2508 if (ref->u.ar.type == AR_SECTION)
2509 return ref_size (&ref->u.ar, result);
2512 return spec_size (array->symtree->n.sym->as, result);
2515 default:
2516 if (array->rank == 0 || array->shape == NULL)
2517 return false;
2519 mpz_init_set_ui (*result, 1);
2521 for (i = 0; i < array->rank; i++)
2522 mpz_mul (*result, *result, array->shape[i]);
2524 break;
2527 return true;
2531 /* Given an array reference, return the shape of the reference in an
2532 array of mpz_t integers. */
2534 bool
2535 gfc_array_ref_shape (gfc_array_ref *ar, mpz_t *shape)
2537 int d;
2538 int i;
2540 d = 0;
2542 switch (ar->type)
2544 case AR_FULL:
2545 for (; d < ar->as->rank; d++)
2546 if (!spec_dimen_size (ar->as, d, &shape[d]))
2547 goto cleanup;
2549 return true;
2551 case AR_SECTION:
2552 for (i = 0; i < ar->dimen; i++)
2554 if (ar->dimen_type[i] != DIMEN_ELEMENT)
2556 if (!gfc_ref_dimen_size (ar, i, &shape[d], NULL))
2557 goto cleanup;
2558 d++;
2562 return true;
2564 default:
2565 break;
2568 cleanup:
2569 gfc_clear_shape (shape, d);
2570 return false;
2574 /* Given an array expression, find the array reference structure that
2575 characterizes the reference. */
2577 gfc_array_ref *
2578 gfc_find_array_ref (gfc_expr *e, bool allow_null)
2580 gfc_ref *ref;
2582 for (ref = e->ref; ref; ref = ref->next)
2583 if (ref->type == REF_ARRAY
2584 && (ref->u.ar.type == AR_FULL || ref->u.ar.type == AR_SECTION))
2585 break;
2587 if (ref == NULL)
2589 if (allow_null)
2590 return NULL;
2591 else
2592 gfc_internal_error ("gfc_find_array_ref(): No ref found");
2595 return &ref->u.ar;
2599 /* Find out if an array shape is known at compile time. */
2601 bool
2602 gfc_is_compile_time_shape (gfc_array_spec *as)
2604 if (as->type != AS_EXPLICIT)
2605 return false;
2607 for (int i = 0; i < as->rank; i++)
2608 if (!gfc_is_constant_expr (as->lower[i])
2609 || !gfc_is_constant_expr (as->upper[i]))
2610 return false;
2612 return true;