[RTL-ifcvt] PR rtl-optimization/68506: Fix emitting order of insns in IF-THEN-JOIN...
[official-gcc.git] / gcc / fortran / array.c
blob1e3f0f2b0abcfd1e714a1e9a84fb5350ddd42cca
1 /* Array things
2 Copyright (C) 2000-2015 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;
160 memset (ar, '\0', sizeof (*ar));
162 ar->where = gfc_current_locus;
163 ar->as = as;
164 ar->type = AR_UNKNOWN;
166 if (gfc_match_char ('[') == MATCH_YES)
168 matched_bracket = true;
169 goto coarray;
172 if (gfc_match_char ('(') != MATCH_YES)
174 ar->type = AR_FULL;
175 ar->dimen = 0;
176 return MATCH_YES;
179 for (ar->dimen = 0; ar->dimen < GFC_MAX_DIMENSIONS; ar->dimen++)
181 m = match_subscript (ar, init, false);
182 if (m == MATCH_ERROR)
183 return MATCH_ERROR;
185 if (gfc_match_char (')') == MATCH_YES)
187 ar->dimen++;
188 goto coarray;
191 if (gfc_match_char (',') != MATCH_YES)
193 gfc_error ("Invalid form of array reference at %C");
194 return MATCH_ERROR;
198 gfc_error ("Array reference at %C cannot have more than %d dimensions",
199 GFC_MAX_DIMENSIONS);
200 return MATCH_ERROR;
202 coarray:
203 if (!matched_bracket && gfc_match_char ('[') != MATCH_YES)
205 if (ar->dimen > 0)
206 return MATCH_YES;
207 else
208 return MATCH_ERROR;
211 if (flag_coarray == GFC_FCOARRAY_NONE)
213 gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to enable");
214 return MATCH_ERROR;
217 if (corank == 0)
219 gfc_error ("Unexpected coarray designator at %C");
220 return MATCH_ERROR;
223 for (ar->codimen = 0; ar->codimen + ar->dimen < GFC_MAX_DIMENSIONS; ar->codimen++)
225 m = match_subscript (ar, init, true);
226 if (m == MATCH_ERROR)
227 return MATCH_ERROR;
229 if (gfc_match_char (']') == MATCH_YES)
231 ar->codimen++;
232 if (ar->codimen < corank)
234 gfc_error ("Too few codimensions at %C, expected %d not %d",
235 corank, ar->codimen);
236 return MATCH_ERROR;
238 if (ar->codimen > corank)
240 gfc_error ("Too many codimensions at %C, expected %d not %d",
241 corank, ar->codimen);
242 return MATCH_ERROR;
244 return MATCH_YES;
247 if (gfc_match_char (',') != MATCH_YES)
249 if (gfc_match_char ('*') == MATCH_YES)
250 gfc_error ("Unexpected %<*%> for codimension %d of %d at %C",
251 ar->codimen + 1, corank);
252 else
253 gfc_error ("Invalid form of coarray reference at %C");
254 return MATCH_ERROR;
256 else if (ar->dimen_type[ar->codimen + ar->dimen] == DIMEN_STAR)
258 gfc_error ("Unexpected %<*%> for codimension %d of %d at %C",
259 ar->codimen + 1, corank);
260 return MATCH_ERROR;
263 if (ar->codimen >= corank)
265 gfc_error ("Invalid codimension %d at %C, only %d codimensions exist",
266 ar->codimen + 1, corank);
267 return MATCH_ERROR;
271 gfc_error ("Array reference at %C cannot have more than %d dimensions",
272 GFC_MAX_DIMENSIONS);
273 return MATCH_ERROR;
278 /************** Array specification matching subroutines ***************/
280 /* Free all of the expressions associated with array bounds
281 specifications. */
283 void
284 gfc_free_array_spec (gfc_array_spec *as)
286 int i;
288 if (as == NULL)
289 return;
291 for (i = 0; i < as->rank + as->corank; i++)
293 gfc_free_expr (as->lower[i]);
294 gfc_free_expr (as->upper[i]);
297 free (as);
301 /* Take an array bound, resolves the expression, that make up the
302 shape and check associated constraints. */
304 static bool
305 resolve_array_bound (gfc_expr *e, int check_constant)
307 if (e == NULL)
308 return true;
310 if (!gfc_resolve_expr (e)
311 || !gfc_specification_expr (e))
312 return false;
314 if (check_constant && !gfc_is_constant_expr (e))
316 if (e->expr_type == EXPR_VARIABLE)
317 gfc_error ("Variable %qs at %L in this context must be constant",
318 e->symtree->n.sym->name, &e->where);
319 else
320 gfc_error ("Expression at %L in this context must be constant",
321 &e->where);
322 return false;
325 return true;
329 /* Takes an array specification, resolves the expressions that make up
330 the shape and make sure everything is integral. */
332 bool
333 gfc_resolve_array_spec (gfc_array_spec *as, int check_constant)
335 gfc_expr *e;
336 int i;
338 if (as == NULL)
339 return true;
341 if (as->resolved)
342 return true;
344 for (i = 0; i < as->rank + as->corank; i++)
346 e = as->lower[i];
347 if (!resolve_array_bound (e, check_constant))
348 return false;
350 e = as->upper[i];
351 if (!resolve_array_bound (e, check_constant))
352 return false;
354 if ((as->lower[i] == NULL) || (as->upper[i] == NULL))
355 continue;
357 /* If the size is negative in this dimension, set it to zero. */
358 if (as->lower[i]->expr_type == EXPR_CONSTANT
359 && as->upper[i]->expr_type == EXPR_CONSTANT
360 && mpz_cmp (as->upper[i]->value.integer,
361 as->lower[i]->value.integer) < 0)
363 gfc_free_expr (as->upper[i]);
364 as->upper[i] = gfc_copy_expr (as->lower[i]);
365 mpz_sub_ui (as->upper[i]->value.integer,
366 as->upper[i]->value.integer, 1);
370 as->resolved = true;
372 return true;
376 /* Match a single array element specification. The return values as
377 well as the upper and lower bounds of the array spec are filled
378 in according to what we see on the input. The caller makes sure
379 individual specifications make sense as a whole.
382 Parsed Lower Upper Returned
383 ------------------------------------
384 : NULL NULL AS_DEFERRED (*)
385 x 1 x AS_EXPLICIT
386 x: x NULL AS_ASSUMED_SHAPE
387 x:y x y AS_EXPLICIT
388 x:* x NULL AS_ASSUMED_SIZE
389 * 1 NULL AS_ASSUMED_SIZE
391 (*) For non-pointer dummy arrays this is AS_ASSUMED_SHAPE. This
392 is fixed during the resolution of formal interfaces.
394 Anything else AS_UNKNOWN. */
396 static array_type
397 match_array_element_spec (gfc_array_spec *as)
399 gfc_expr **upper, **lower;
400 match m;
401 int rank;
403 rank = as->rank == -1 ? 0 : as->rank;
404 lower = &as->lower[rank + as->corank - 1];
405 upper = &as->upper[rank + as->corank - 1];
407 if (gfc_match_char ('*') == MATCH_YES)
409 *lower = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
410 return AS_ASSUMED_SIZE;
413 if (gfc_match_char (':') == MATCH_YES)
414 return AS_DEFERRED;
416 m = gfc_match_expr (upper);
417 if (m == MATCH_NO)
418 gfc_error ("Expected expression in array specification at %C");
419 if (m != MATCH_YES)
420 return AS_UNKNOWN;
421 if (!gfc_expr_check_typed (*upper, gfc_current_ns, false))
422 return AS_UNKNOWN;
424 if ((*upper)->expr_type == EXPR_FUNCTION && (*upper)->ts.type == BT_UNKNOWN
425 && (*upper)->symtree && strcmp ((*upper)->symtree->name, "null") == 0)
427 gfc_error ("Expecting a scalar INTEGER expression at %C");
428 return AS_UNKNOWN;
431 if (gfc_match_char (':') == MATCH_NO)
433 *lower = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
434 return AS_EXPLICIT;
437 *lower = *upper;
438 *upper = NULL;
440 if (gfc_match_char ('*') == MATCH_YES)
441 return AS_ASSUMED_SIZE;
443 m = gfc_match_expr (upper);
444 if (m == MATCH_ERROR)
445 return AS_UNKNOWN;
446 if (m == MATCH_NO)
447 return AS_ASSUMED_SHAPE;
448 if (!gfc_expr_check_typed (*upper, gfc_current_ns, false))
449 return AS_UNKNOWN;
451 if ((*upper)->expr_type == EXPR_FUNCTION && (*upper)->ts.type == BT_UNKNOWN
452 && (*upper)->symtree && strcmp ((*upper)->symtree->name, "null") == 0)
454 gfc_error ("Expecting a scalar INTEGER expression at %C");
455 return AS_UNKNOWN;
458 return AS_EXPLICIT;
462 /* Matches an array specification, incidentally figuring out what sort
463 it is. Match either a normal array specification, or a coarray spec
464 or both. Optionally allow [:] for coarrays. */
466 match
467 gfc_match_array_spec (gfc_array_spec **asp, bool match_dim, bool match_codim)
469 array_type current_type;
470 gfc_array_spec *as;
471 int i;
473 as = gfc_get_array_spec ();
475 if (!match_dim)
476 goto coarray;
478 if (gfc_match_char ('(') != MATCH_YES)
480 if (!match_codim)
481 goto done;
482 goto coarray;
485 if (gfc_match (" .. )") == MATCH_YES)
487 as->type = AS_ASSUMED_RANK;
488 as->rank = -1;
490 if (!gfc_notify_std (GFC_STD_F2008_TS, "Assumed-rank array at %C"))
491 goto cleanup;
493 if (!match_codim)
494 goto done;
495 goto coarray;
498 for (;;)
500 as->rank++;
501 current_type = match_array_element_spec (as);
503 /* Note that current_type == AS_ASSUMED_SIZE for both assumed-size
504 and implied-shape specifications. If the rank is at least 2, we can
505 distinguish between them. But for rank 1, we currently return
506 ASSUMED_SIZE; this gets adjusted later when we know for sure
507 whether the symbol parsed is a PARAMETER or not. */
509 if (as->rank == 1)
511 if (current_type == AS_UNKNOWN)
512 goto cleanup;
513 as->type = current_type;
515 else
516 switch (as->type)
517 { /* See how current spec meshes with the existing. */
518 case AS_UNKNOWN:
519 goto cleanup;
521 case AS_IMPLIED_SHAPE:
522 if (current_type != AS_ASSUMED_SHAPE)
524 gfc_error ("Bad array specification for implied-shape"
525 " array at %C");
526 goto cleanup;
528 break;
530 case AS_EXPLICIT:
531 if (current_type == AS_ASSUMED_SIZE)
533 as->type = AS_ASSUMED_SIZE;
534 break;
537 if (current_type == AS_EXPLICIT)
538 break;
540 gfc_error ("Bad array specification for an explicitly shaped "
541 "array at %C");
543 goto cleanup;
545 case AS_ASSUMED_SHAPE:
546 if ((current_type == AS_ASSUMED_SHAPE)
547 || (current_type == AS_DEFERRED))
548 break;
550 gfc_error ("Bad array specification for assumed shape "
551 "array at %C");
552 goto cleanup;
554 case AS_DEFERRED:
555 if (current_type == AS_DEFERRED)
556 break;
558 if (current_type == AS_ASSUMED_SHAPE)
560 as->type = AS_ASSUMED_SHAPE;
561 break;
564 gfc_error ("Bad specification for deferred shape array at %C");
565 goto cleanup;
567 case AS_ASSUMED_SIZE:
568 if (as->rank == 2 && current_type == AS_ASSUMED_SIZE)
570 as->type = AS_IMPLIED_SHAPE;
571 break;
574 gfc_error ("Bad specification for assumed size array at %C");
575 goto cleanup;
577 case AS_ASSUMED_RANK:
578 gcc_unreachable ();
581 if (gfc_match_char (')') == MATCH_YES)
582 break;
584 if (gfc_match_char (',') != MATCH_YES)
586 gfc_error ("Expected another dimension in array declaration at %C");
587 goto cleanup;
590 if (as->rank + as->corank >= GFC_MAX_DIMENSIONS)
592 gfc_error ("Array specification at %C has more than %d dimensions",
593 GFC_MAX_DIMENSIONS);
594 goto cleanup;
597 if (as->corank + as->rank >= 7
598 && !gfc_notify_std (GFC_STD_F2008, "Array specification at %C "
599 "with more than 7 dimensions"))
600 goto cleanup;
603 if (!match_codim)
604 goto done;
606 coarray:
607 if (gfc_match_char ('[') != MATCH_YES)
608 goto done;
610 if (!gfc_notify_std (GFC_STD_F2008, "Coarray declaration at %C"))
611 goto cleanup;
613 if (flag_coarray == GFC_FCOARRAY_NONE)
615 gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to enable");
616 goto cleanup;
619 if (as->rank >= GFC_MAX_DIMENSIONS)
621 gfc_error ("Array specification at %C has more than %d "
622 "dimensions", GFC_MAX_DIMENSIONS);
623 goto cleanup;
626 for (;;)
628 as->corank++;
629 current_type = match_array_element_spec (as);
631 if (current_type == AS_UNKNOWN)
632 goto cleanup;
634 if (as->corank == 1)
635 as->cotype = current_type;
636 else
637 switch (as->cotype)
638 { /* See how current spec meshes with the existing. */
639 case AS_IMPLIED_SHAPE:
640 case AS_UNKNOWN:
641 goto cleanup;
643 case AS_EXPLICIT:
644 if (current_type == AS_ASSUMED_SIZE)
646 as->cotype = AS_ASSUMED_SIZE;
647 break;
650 if (current_type == AS_EXPLICIT)
651 break;
653 gfc_error ("Bad array specification for an explicitly "
654 "shaped array at %C");
656 goto cleanup;
658 case AS_ASSUMED_SHAPE:
659 if ((current_type == AS_ASSUMED_SHAPE)
660 || (current_type == AS_DEFERRED))
661 break;
663 gfc_error ("Bad array specification for assumed shape "
664 "array at %C");
665 goto cleanup;
667 case AS_DEFERRED:
668 if (current_type == AS_DEFERRED)
669 break;
671 if (current_type == AS_ASSUMED_SHAPE)
673 as->cotype = AS_ASSUMED_SHAPE;
674 break;
677 gfc_error ("Bad specification for deferred shape array at %C");
678 goto cleanup;
680 case AS_ASSUMED_SIZE:
681 gfc_error ("Bad specification for assumed size array at %C");
682 goto cleanup;
684 case AS_ASSUMED_RANK:
685 gcc_unreachable ();
688 if (gfc_match_char (']') == MATCH_YES)
689 break;
691 if (gfc_match_char (',') != MATCH_YES)
693 gfc_error ("Expected another dimension in array declaration at %C");
694 goto cleanup;
697 if (as->rank + as->corank >= GFC_MAX_DIMENSIONS)
699 gfc_error ("Array specification at %C has more than %d "
700 "dimensions", GFC_MAX_DIMENSIONS);
701 goto cleanup;
705 if (current_type == AS_EXPLICIT)
707 gfc_error ("Upper bound of last coarray dimension must be %<*%> at %C");
708 goto cleanup;
711 if (as->cotype == AS_ASSUMED_SIZE)
712 as->cotype = AS_EXPLICIT;
714 if (as->rank == 0)
715 as->type = as->cotype;
717 done:
718 if (as->rank == 0 && as->corank == 0)
720 *asp = NULL;
721 gfc_free_array_spec (as);
722 return MATCH_NO;
725 /* If a lower bounds of an assumed shape array is blank, put in one. */
726 if (as->type == AS_ASSUMED_SHAPE)
728 for (i = 0; i < as->rank + as->corank; i++)
730 if (as->lower[i] == NULL)
731 as->lower[i] = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
735 *asp = as;
737 return MATCH_YES;
739 cleanup:
740 /* Something went wrong. */
741 gfc_free_array_spec (as);
742 return MATCH_ERROR;
746 /* Given a symbol and an array specification, modify the symbol to
747 have that array specification. The error locus is needed in case
748 something goes wrong. On failure, the caller must free the spec. */
750 bool
751 gfc_set_array_spec (gfc_symbol *sym, gfc_array_spec *as, locus *error_loc)
753 int i;
755 if (as == NULL)
756 return true;
758 if (as->rank
759 && !gfc_add_dimension (&sym->attr, sym->name, error_loc))
760 return false;
762 if (as->corank
763 && !gfc_add_codimension (&sym->attr, sym->name, error_loc))
764 return false;
766 if (sym->as == NULL)
768 sym->as = as;
769 return true;
772 if ((sym->as->type == AS_ASSUMED_RANK && as->corank)
773 || (as->type == AS_ASSUMED_RANK && sym->as->corank))
775 gfc_error ("The assumed-rank array %qs at %L shall not have a "
776 "codimension", sym->name, error_loc);
777 return false;
780 if (as->corank)
782 /* The "sym" has no corank (checked via gfc_add_codimension). Thus
783 the codimension is simply added. */
784 gcc_assert (as->rank == 0 && sym->as->corank == 0);
786 sym->as->cotype = as->cotype;
787 sym->as->corank = as->corank;
788 for (i = 0; i < as->corank; i++)
790 sym->as->lower[sym->as->rank + i] = as->lower[i];
791 sym->as->upper[sym->as->rank + i] = as->upper[i];
794 else
796 /* The "sym" has no rank (checked via gfc_add_dimension). Thus
797 the dimension is added - but first the codimensions (if existing
798 need to be shifted to make space for the dimension. */
799 gcc_assert (as->corank == 0 && sym->as->rank == 0);
801 sym->as->rank = as->rank;
802 sym->as->type = as->type;
803 sym->as->cray_pointee = as->cray_pointee;
804 sym->as->cp_was_assumed = as->cp_was_assumed;
806 for (i = 0; i < sym->as->corank; i++)
808 sym->as->lower[as->rank + i] = sym->as->lower[i];
809 sym->as->upper[as->rank + i] = sym->as->upper[i];
811 for (i = 0; i < as->rank; i++)
813 sym->as->lower[i] = as->lower[i];
814 sym->as->upper[i] = as->upper[i];
818 free (as);
819 return true;
823 /* Copy an array specification. */
825 gfc_array_spec *
826 gfc_copy_array_spec (gfc_array_spec *src)
828 gfc_array_spec *dest;
829 int i;
831 if (src == NULL)
832 return NULL;
834 dest = gfc_get_array_spec ();
836 *dest = *src;
838 for (i = 0; i < dest->rank + dest->corank; i++)
840 dest->lower[i] = gfc_copy_expr (dest->lower[i]);
841 dest->upper[i] = gfc_copy_expr (dest->upper[i]);
844 return dest;
848 /* Returns nonzero if the two expressions are equal. Only handles integer
849 constants. */
851 static int
852 compare_bounds (gfc_expr *bound1, gfc_expr *bound2)
854 if (bound1 == NULL || bound2 == NULL
855 || bound1->expr_type != EXPR_CONSTANT
856 || bound2->expr_type != EXPR_CONSTANT
857 || bound1->ts.type != BT_INTEGER
858 || bound2->ts.type != BT_INTEGER)
859 gfc_internal_error ("gfc_compare_array_spec(): Array spec clobbered");
861 if (mpz_cmp (bound1->value.integer, bound2->value.integer) == 0)
862 return 1;
863 else
864 return 0;
868 /* Compares two array specifications. They must be constant or deferred
869 shape. */
872 gfc_compare_array_spec (gfc_array_spec *as1, gfc_array_spec *as2)
874 int i;
876 if (as1 == NULL && as2 == NULL)
877 return 1;
879 if (as1 == NULL || as2 == NULL)
880 return 0;
882 if (as1->rank != as2->rank)
883 return 0;
885 if (as1->corank != as2->corank)
886 return 0;
888 if (as1->rank == 0)
889 return 1;
891 if (as1->type != as2->type)
892 return 0;
894 if (as1->type == AS_EXPLICIT)
895 for (i = 0; i < as1->rank + as1->corank; i++)
897 if (compare_bounds (as1->lower[i], as2->lower[i]) == 0)
898 return 0;
900 if (compare_bounds (as1->upper[i], as2->upper[i]) == 0)
901 return 0;
904 return 1;
908 /****************** Array constructor functions ******************/
911 /* Given an expression node that might be an array constructor and a
912 symbol, make sure that no iterators in this or child constructors
913 use the symbol as an implied-DO iterator. Returns nonzero if a
914 duplicate was found. */
916 static int
917 check_duplicate_iterator (gfc_constructor_base base, gfc_symbol *master)
919 gfc_constructor *c;
920 gfc_expr *e;
922 for (c = gfc_constructor_first (base); c; c = gfc_constructor_next (c))
924 e = c->expr;
926 if (e->expr_type == EXPR_ARRAY
927 && check_duplicate_iterator (e->value.constructor, master))
928 return 1;
930 if (c->iterator == NULL)
931 continue;
933 if (c->iterator->var->symtree->n.sym == master)
935 gfc_error ("DO-iterator %qs at %L is inside iterator of the "
936 "same name", master->name, &c->where);
938 return 1;
942 return 0;
946 /* Forward declaration because these functions are mutually recursive. */
947 static match match_array_cons_element (gfc_constructor_base *);
949 /* Match a list of array elements. */
951 static match
952 match_array_list (gfc_constructor_base *result)
954 gfc_constructor_base head;
955 gfc_constructor *p;
956 gfc_iterator iter;
957 locus old_loc;
958 gfc_expr *e;
959 match m;
960 int n;
962 old_loc = gfc_current_locus;
964 if (gfc_match_char ('(') == MATCH_NO)
965 return MATCH_NO;
967 memset (&iter, '\0', sizeof (gfc_iterator));
968 head = NULL;
970 m = match_array_cons_element (&head);
971 if (m != MATCH_YES)
972 goto cleanup;
974 if (gfc_match_char (',') != MATCH_YES)
976 m = MATCH_NO;
977 goto cleanup;
980 for (n = 1;; n++)
982 m = gfc_match_iterator (&iter, 0);
983 if (m == MATCH_YES)
984 break;
985 if (m == MATCH_ERROR)
986 goto cleanup;
988 m = match_array_cons_element (&head);
989 if (m == MATCH_ERROR)
990 goto cleanup;
991 if (m == MATCH_NO)
993 if (n > 2)
994 goto syntax;
995 m = MATCH_NO;
996 goto cleanup; /* Could be a complex constant */
999 if (gfc_match_char (',') != MATCH_YES)
1001 if (n > 2)
1002 goto syntax;
1003 m = MATCH_NO;
1004 goto cleanup;
1008 if (gfc_match_char (')') != MATCH_YES)
1009 goto syntax;
1011 if (check_duplicate_iterator (head, iter.var->symtree->n.sym))
1013 m = MATCH_ERROR;
1014 goto cleanup;
1017 e = gfc_get_array_expr (BT_UNKNOWN, 0, &old_loc);
1018 e->value.constructor = head;
1020 p = gfc_constructor_append_expr (result, e, &gfc_current_locus);
1021 p->iterator = gfc_get_iterator ();
1022 *p->iterator = iter;
1024 return MATCH_YES;
1026 syntax:
1027 gfc_error ("Syntax error in array constructor at %C");
1028 m = MATCH_ERROR;
1030 cleanup:
1031 gfc_constructor_free (head);
1032 gfc_free_iterator (&iter, 0);
1033 gfc_current_locus = old_loc;
1034 return m;
1038 /* Match a single element of an array constructor, which can be a
1039 single expression or a list of elements. */
1041 static match
1042 match_array_cons_element (gfc_constructor_base *result)
1044 gfc_expr *expr;
1045 match m;
1047 m = match_array_list (result);
1048 if (m != MATCH_NO)
1049 return m;
1051 m = gfc_match_expr (&expr);
1052 if (m != MATCH_YES)
1053 return m;
1055 gfc_constructor_append_expr (result, expr, &gfc_current_locus);
1056 return MATCH_YES;
1060 /* Match an array constructor. */
1062 match
1063 gfc_match_array_constructor (gfc_expr **result)
1065 gfc_constructor_base head, new_cons;
1066 gfc_undo_change_set changed_syms;
1067 gfc_expr *expr;
1068 gfc_typespec ts;
1069 locus where;
1070 match m;
1071 const char *end_delim;
1072 bool seen_ts;
1074 if (gfc_match (" (/") == MATCH_NO)
1076 if (gfc_match (" [") == MATCH_NO)
1077 return MATCH_NO;
1078 else
1080 if (!gfc_notify_std (GFC_STD_F2003, "[...] "
1081 "style array constructors at %C"))
1082 return MATCH_ERROR;
1083 end_delim = " ]";
1086 else
1087 end_delim = " /)";
1089 where = gfc_current_locus;
1090 head = new_cons = NULL;
1091 seen_ts = false;
1093 /* Try to match an optional "type-spec ::" */
1094 gfc_clear_ts (&ts);
1095 gfc_new_undo_checkpoint (changed_syms);
1096 m = gfc_match_type_spec (&ts);
1097 if (m == MATCH_YES)
1099 seen_ts = (gfc_match (" ::") == MATCH_YES);
1101 if (seen_ts)
1103 if (!gfc_notify_std (GFC_STD_F2003, "Array constructor "
1104 "including type specification at %C"))
1106 gfc_restore_last_undo_checkpoint ();
1107 goto cleanup;
1110 if (ts.deferred)
1112 gfc_error ("Type-spec at %L cannot contain a deferred "
1113 "type parameter", &where);
1114 gfc_restore_last_undo_checkpoint ();
1115 goto cleanup;
1119 else if (m == MATCH_ERROR)
1121 gfc_restore_last_undo_checkpoint ();
1122 goto cleanup;
1125 if (seen_ts)
1126 gfc_drop_last_undo_checkpoint ();
1127 else
1129 gfc_restore_last_undo_checkpoint ();
1130 gfc_current_locus = where;
1133 if (gfc_match (end_delim) == MATCH_YES)
1135 if (seen_ts)
1136 goto done;
1137 else
1139 gfc_error ("Empty array constructor at %C is not allowed");
1140 goto cleanup;
1144 for (;;)
1146 m = match_array_cons_element (&head);
1147 if (m == MATCH_ERROR)
1148 goto cleanup;
1149 if (m == MATCH_NO)
1150 goto syntax;
1152 if (gfc_match_char (',') == MATCH_NO)
1153 break;
1156 if (gfc_match (end_delim) == MATCH_NO)
1157 goto syntax;
1159 done:
1160 /* Size must be calculated at resolution time. */
1161 if (seen_ts)
1163 expr = gfc_get_array_expr (ts.type, ts.kind, &where);
1164 expr->ts = ts;
1166 /* If the typespec is CHARACTER, check that array elements can
1167 be converted. See PR fortran/67803. */
1168 if (ts.type == BT_CHARACTER)
1170 gfc_constructor *c;
1172 c = gfc_constructor_first (head);
1173 for (; c; c = gfc_constructor_next (c))
1175 if (gfc_numeric_ts (&c->expr->ts)
1176 || c->expr->ts.type == BT_LOGICAL)
1178 gfc_error ("Incompatible typespec for array element at %L",
1179 &c->expr->where);
1180 return MATCH_ERROR;
1183 /* Special case null(). */
1184 if (c->expr->expr_type == EXPR_FUNCTION
1185 && c->expr->ts.type == BT_UNKNOWN
1186 && strcmp (c->expr->symtree->name, "null") == 0)
1188 gfc_error ("Incompatible typespec for array element at %L",
1189 &c->expr->where);
1190 return MATCH_ERROR;
1195 else
1196 expr = gfc_get_array_expr (BT_UNKNOWN, 0, &where);
1198 expr->value.constructor = head;
1199 if (expr->ts.u.cl)
1200 expr->ts.u.cl->length_from_typespec = seen_ts;
1202 *result = expr;
1204 return MATCH_YES;
1206 syntax:
1207 gfc_error ("Syntax error in array constructor at %C");
1209 cleanup:
1210 gfc_constructor_free (head);
1211 return MATCH_ERROR;
1216 /************** Check array constructors for correctness **************/
1218 /* Given an expression, compare it's type with the type of the current
1219 constructor. Returns nonzero if an error was issued. The
1220 cons_state variable keeps track of whether the type of the
1221 constructor being read or resolved is known to be good, bad or just
1222 starting out. */
1224 static gfc_typespec constructor_ts;
1225 static enum
1226 { CONS_START, CONS_GOOD, CONS_BAD }
1227 cons_state;
1229 static int
1230 check_element_type (gfc_expr *expr, bool convert)
1232 if (cons_state == CONS_BAD)
1233 return 0; /* Suppress further errors */
1235 if (cons_state == CONS_START)
1237 if (expr->ts.type == BT_UNKNOWN)
1238 cons_state = CONS_BAD;
1239 else
1241 cons_state = CONS_GOOD;
1242 constructor_ts = expr->ts;
1245 return 0;
1248 if (gfc_compare_types (&constructor_ts, &expr->ts))
1249 return 0;
1251 if (convert)
1252 return gfc_convert_type(expr, &constructor_ts, 1) ? 0 : 1;
1254 gfc_error ("Element in %s array constructor at %L is %s",
1255 gfc_typename (&constructor_ts), &expr->where,
1256 gfc_typename (&expr->ts));
1258 cons_state = CONS_BAD;
1259 return 1;
1263 /* Recursive work function for gfc_check_constructor_type(). */
1265 static bool
1266 check_constructor_type (gfc_constructor_base base, bool convert)
1268 gfc_constructor *c;
1269 gfc_expr *e;
1271 for (c = gfc_constructor_first (base); c; c = gfc_constructor_next (c))
1273 e = c->expr;
1275 if (e->expr_type == EXPR_ARRAY)
1277 if (!check_constructor_type (e->value.constructor, convert))
1278 return false;
1280 continue;
1283 if (check_element_type (e, convert))
1284 return false;
1287 return true;
1291 /* Check that all elements of an array constructor are the same type.
1292 On false, an error has been generated. */
1294 bool
1295 gfc_check_constructor_type (gfc_expr *e)
1297 bool t;
1299 if (e->ts.type != BT_UNKNOWN)
1301 cons_state = CONS_GOOD;
1302 constructor_ts = e->ts;
1304 else
1306 cons_state = CONS_START;
1307 gfc_clear_ts (&constructor_ts);
1310 /* If e->ts.type != BT_UNKNOWN, the array constructor included a
1311 typespec, and we will now convert the values on the fly. */
1312 t = check_constructor_type (e->value.constructor, e->ts.type != BT_UNKNOWN);
1313 if (t && e->ts.type == BT_UNKNOWN)
1314 e->ts = constructor_ts;
1316 return t;
1321 typedef struct cons_stack
1323 gfc_iterator *iterator;
1324 struct cons_stack *previous;
1326 cons_stack;
1328 static cons_stack *base;
1330 static bool check_constructor (gfc_constructor_base, bool (*) (gfc_expr *));
1332 /* Check an EXPR_VARIABLE expression in a constructor to make sure
1333 that that variable is an iteration variables. */
1335 bool
1336 gfc_check_iter_variable (gfc_expr *expr)
1338 gfc_symbol *sym;
1339 cons_stack *c;
1341 sym = expr->symtree->n.sym;
1343 for (c = base; c && c->iterator; c = c->previous)
1344 if (sym == c->iterator->var->symtree->n.sym)
1345 return true;
1347 return false;
1351 /* Recursive work function for gfc_check_constructor(). This amounts
1352 to calling the check function for each expression in the
1353 constructor, giving variables with the names of iterators a pass. */
1355 static bool
1356 check_constructor (gfc_constructor_base ctor, bool (*check_function) (gfc_expr *))
1358 cons_stack element;
1359 gfc_expr *e;
1360 bool t;
1361 gfc_constructor *c;
1363 for (c = gfc_constructor_first (ctor); c; c = gfc_constructor_next (c))
1365 e = c->expr;
1367 if (!e)
1368 continue;
1370 if (e->expr_type != EXPR_ARRAY)
1372 if (!(*check_function)(e))
1373 return false;
1374 continue;
1377 element.previous = base;
1378 element.iterator = c->iterator;
1380 base = &element;
1381 t = check_constructor (e->value.constructor, check_function);
1382 base = element.previous;
1384 if (!t)
1385 return false;
1388 /* Nothing went wrong, so all OK. */
1389 return true;
1393 /* Checks a constructor to see if it is a particular kind of
1394 expression -- specification, restricted, or initialization as
1395 determined by the check_function. */
1397 bool
1398 gfc_check_constructor (gfc_expr *expr, bool (*check_function) (gfc_expr *))
1400 cons_stack *base_save;
1401 bool t;
1403 base_save = base;
1404 base = NULL;
1406 t = check_constructor (expr->value.constructor, check_function);
1407 base = base_save;
1409 return t;
1414 /**************** Simplification of array constructors ****************/
1416 iterator_stack *iter_stack;
1418 typedef struct
1420 gfc_constructor_base base;
1421 int extract_count, extract_n;
1422 gfc_expr *extracted;
1423 mpz_t *count;
1425 mpz_t *offset;
1426 gfc_component *component;
1427 mpz_t *repeat;
1429 bool (*expand_work_function) (gfc_expr *);
1431 expand_info;
1433 static expand_info current_expand;
1435 static bool expand_constructor (gfc_constructor_base);
1438 /* Work function that counts the number of elements present in a
1439 constructor. */
1441 static bool
1442 count_elements (gfc_expr *e)
1444 mpz_t result;
1446 if (e->rank == 0)
1447 mpz_add_ui (*current_expand.count, *current_expand.count, 1);
1448 else
1450 if (!gfc_array_size (e, &result))
1452 gfc_free_expr (e);
1453 return false;
1456 mpz_add (*current_expand.count, *current_expand.count, result);
1457 mpz_clear (result);
1460 gfc_free_expr (e);
1461 return true;
1465 /* Work function that extracts a particular element from an array
1466 constructor, freeing the rest. */
1468 static bool
1469 extract_element (gfc_expr *e)
1471 if (e->rank != 0)
1472 { /* Something unextractable */
1473 gfc_free_expr (e);
1474 return false;
1477 if (current_expand.extract_count == current_expand.extract_n)
1478 current_expand.extracted = e;
1479 else
1480 gfc_free_expr (e);
1482 current_expand.extract_count++;
1484 return true;
1488 /* Work function that constructs a new constructor out of the old one,
1489 stringing new elements together. */
1491 static bool
1492 expand (gfc_expr *e)
1494 gfc_constructor *c = gfc_constructor_append_expr (&current_expand.base,
1495 e, &e->where);
1497 c->n.component = current_expand.component;
1498 return true;
1502 /* Given an initialization expression that is a variable reference,
1503 substitute the current value of the iteration variable. */
1505 void
1506 gfc_simplify_iterator_var (gfc_expr *e)
1508 iterator_stack *p;
1510 for (p = iter_stack; p; p = p->prev)
1511 if (e->symtree == p->variable)
1512 break;
1514 if (p == NULL)
1515 return; /* Variable not found */
1517 gfc_replace_expr (e, gfc_get_int_expr (gfc_default_integer_kind, NULL, 0));
1519 mpz_set (e->value.integer, p->value);
1521 return;
1525 /* Expand an expression with that is inside of a constructor,
1526 recursing into other constructors if present. */
1528 static bool
1529 expand_expr (gfc_expr *e)
1531 if (e->expr_type == EXPR_ARRAY)
1532 return expand_constructor (e->value.constructor);
1534 e = gfc_copy_expr (e);
1536 if (!gfc_simplify_expr (e, 1))
1538 gfc_free_expr (e);
1539 return false;
1542 return current_expand.expand_work_function (e);
1546 static bool
1547 expand_iterator (gfc_constructor *c)
1549 gfc_expr *start, *end, *step;
1550 iterator_stack frame;
1551 mpz_t trip;
1552 bool t;
1554 end = step = NULL;
1556 t = false;
1558 mpz_init (trip);
1559 mpz_init (frame.value);
1560 frame.prev = NULL;
1562 start = gfc_copy_expr (c->iterator->start);
1563 if (!gfc_simplify_expr (start, 1))
1564 goto cleanup;
1566 if (start->expr_type != EXPR_CONSTANT || start->ts.type != BT_INTEGER)
1567 goto cleanup;
1569 end = gfc_copy_expr (c->iterator->end);
1570 if (!gfc_simplify_expr (end, 1))
1571 goto cleanup;
1573 if (end->expr_type != EXPR_CONSTANT || end->ts.type != BT_INTEGER)
1574 goto cleanup;
1576 step = gfc_copy_expr (c->iterator->step);
1577 if (!gfc_simplify_expr (step, 1))
1578 goto cleanup;
1580 if (step->expr_type != EXPR_CONSTANT || step->ts.type != BT_INTEGER)
1581 goto cleanup;
1583 if (mpz_sgn (step->value.integer) == 0)
1585 gfc_error ("Iterator step at %L cannot be zero", &step->where);
1586 goto cleanup;
1589 /* Calculate the trip count of the loop. */
1590 mpz_sub (trip, end->value.integer, start->value.integer);
1591 mpz_add (trip, trip, step->value.integer);
1592 mpz_tdiv_q (trip, trip, step->value.integer);
1594 mpz_set (frame.value, start->value.integer);
1596 frame.prev = iter_stack;
1597 frame.variable = c->iterator->var->symtree;
1598 iter_stack = &frame;
1600 while (mpz_sgn (trip) > 0)
1602 if (!expand_expr (c->expr))
1603 goto cleanup;
1605 mpz_add (frame.value, frame.value, step->value.integer);
1606 mpz_sub_ui (trip, trip, 1);
1609 t = true;
1611 cleanup:
1612 gfc_free_expr (start);
1613 gfc_free_expr (end);
1614 gfc_free_expr (step);
1616 mpz_clear (trip);
1617 mpz_clear (frame.value);
1619 iter_stack = frame.prev;
1621 return t;
1625 /* Expand a constructor into constant constructors without any
1626 iterators, calling the work function for each of the expanded
1627 expressions. The work function needs to either save or free the
1628 passed expression. */
1630 static bool
1631 expand_constructor (gfc_constructor_base base)
1633 gfc_constructor *c;
1634 gfc_expr *e;
1636 for (c = gfc_constructor_first (base); c; c = gfc_constructor_next(c))
1638 if (c->iterator != NULL)
1640 if (!expand_iterator (c))
1641 return false;
1642 continue;
1645 e = c->expr;
1647 if (e->expr_type == EXPR_ARRAY)
1649 if (!expand_constructor (e->value.constructor))
1650 return false;
1652 continue;
1655 e = gfc_copy_expr (e);
1656 if (!gfc_simplify_expr (e, 1))
1658 gfc_free_expr (e);
1659 return false;
1661 current_expand.offset = &c->offset;
1662 current_expand.repeat = &c->repeat;
1663 current_expand.component = c->n.component;
1664 if (!current_expand.expand_work_function(e))
1665 return false;
1667 return true;
1671 /* Given an array expression and an element number (starting at zero),
1672 return a pointer to the array element. NULL is returned if the
1673 size of the array has been exceeded. The expression node returned
1674 remains a part of the array and should not be freed. Access is not
1675 efficient at all, but this is another place where things do not
1676 have to be particularly fast. */
1678 static gfc_expr *
1679 gfc_get_array_element (gfc_expr *array, int element)
1681 expand_info expand_save;
1682 gfc_expr *e;
1683 bool rc;
1685 expand_save = current_expand;
1686 current_expand.extract_n = element;
1687 current_expand.expand_work_function = extract_element;
1688 current_expand.extracted = NULL;
1689 current_expand.extract_count = 0;
1691 iter_stack = NULL;
1693 rc = expand_constructor (array->value.constructor);
1694 e = current_expand.extracted;
1695 current_expand = expand_save;
1697 if (!rc)
1698 return NULL;
1700 return e;
1704 /* Top level subroutine for expanding constructors. We only expand
1705 constructor if they are small enough. */
1707 bool
1708 gfc_expand_constructor (gfc_expr *e, bool fatal)
1710 expand_info expand_save;
1711 gfc_expr *f;
1712 bool rc;
1714 /* If we can successfully get an array element at the max array size then
1715 the array is too big to expand, so we just return. */
1716 f = gfc_get_array_element (e, flag_max_array_constructor);
1717 if (f != NULL)
1719 gfc_free_expr (f);
1720 if (fatal)
1722 gfc_error ("The number of elements in the array constructor "
1723 "at %L requires an increase of the allowed %d "
1724 "upper limit. See %<-fmax-array-constructor%> "
1725 "option", &e->where, flag_max_array_constructor);
1726 return false;
1728 return true;
1731 /* We now know the array is not too big so go ahead and try to expand it. */
1732 expand_save = current_expand;
1733 current_expand.base = NULL;
1735 iter_stack = NULL;
1737 current_expand.expand_work_function = expand;
1739 if (!expand_constructor (e->value.constructor))
1741 gfc_constructor_free (current_expand.base);
1742 rc = false;
1743 goto done;
1746 gfc_constructor_free (e->value.constructor);
1747 e->value.constructor = current_expand.base;
1749 rc = true;
1751 done:
1752 current_expand = expand_save;
1754 return rc;
1758 /* Work function for checking that an element of a constructor is a
1759 constant, after removal of any iteration variables. We return
1760 false if not so. */
1762 static bool
1763 is_constant_element (gfc_expr *e)
1765 int rv;
1767 rv = gfc_is_constant_expr (e);
1768 gfc_free_expr (e);
1770 return rv ? true : false;
1774 /* Given an array constructor, determine if the constructor is
1775 constant or not by expanding it and making sure that all elements
1776 are constants. This is a bit of a hack since something like (/ (i,
1777 i=1,100000000) /) will take a while as* opposed to a more clever
1778 function that traverses the expression tree. FIXME. */
1781 gfc_constant_ac (gfc_expr *e)
1783 expand_info expand_save;
1784 bool rc;
1786 iter_stack = NULL;
1787 expand_save = current_expand;
1788 current_expand.expand_work_function = is_constant_element;
1790 rc = expand_constructor (e->value.constructor);
1792 current_expand = expand_save;
1793 if (!rc)
1794 return 0;
1796 return 1;
1800 /* Returns nonzero if an array constructor has been completely
1801 expanded (no iterators) and zero if iterators are present. */
1804 gfc_expanded_ac (gfc_expr *e)
1806 gfc_constructor *c;
1808 if (e->expr_type == EXPR_ARRAY)
1809 for (c = gfc_constructor_first (e->value.constructor);
1810 c; c = gfc_constructor_next (c))
1811 if (c->iterator != NULL || !gfc_expanded_ac (c->expr))
1812 return 0;
1814 return 1;
1818 /*************** Type resolution of array constructors ***************/
1821 /* The symbol expr_is_sought_symbol_ref will try to find. */
1822 static const gfc_symbol *sought_symbol = NULL;
1825 /* Tells whether the expression E is a variable reference to the symbol
1826 in the static variable SOUGHT_SYMBOL, and sets the locus pointer WHERE
1827 accordingly.
1828 To be used with gfc_expr_walker: if a reference is found we don't need
1829 to look further so we return 1 to skip any further walk. */
1831 static int
1832 expr_is_sought_symbol_ref (gfc_expr **e, int *walk_subtrees ATTRIBUTE_UNUSED,
1833 void *where)
1835 gfc_expr *expr = *e;
1836 locus *sym_loc = (locus *)where;
1838 if (expr->expr_type == EXPR_VARIABLE
1839 && expr->symtree->n.sym == sought_symbol)
1841 *sym_loc = expr->where;
1842 return 1;
1845 return 0;
1849 /* Tells whether the expression EXPR contains a reference to the symbol
1850 SYM and in that case sets the position SYM_LOC where the reference is. */
1852 static bool
1853 find_symbol_in_expr (gfc_symbol *sym, gfc_expr *expr, locus *sym_loc)
1855 int ret;
1857 sought_symbol = sym;
1858 ret = gfc_expr_walker (&expr, &expr_is_sought_symbol_ref, sym_loc);
1859 sought_symbol = NULL;
1860 return ret;
1864 /* Recursive array list resolution function. All of the elements must
1865 be of the same type. */
1867 static bool
1868 resolve_array_list (gfc_constructor_base base)
1870 bool t;
1871 gfc_constructor *c;
1872 gfc_iterator *iter;
1874 t = true;
1876 for (c = gfc_constructor_first (base); c; c = gfc_constructor_next (c))
1878 iter = c->iterator;
1879 if (iter != NULL)
1881 gfc_symbol *iter_var;
1882 locus iter_var_loc;
1884 if (!gfc_resolve_iterator (iter, false, true))
1885 t = false;
1887 /* Check for bounds referencing the iterator variable. */
1888 gcc_assert (iter->var->expr_type == EXPR_VARIABLE);
1889 iter_var = iter->var->symtree->n.sym;
1890 if (find_symbol_in_expr (iter_var, iter->start, &iter_var_loc))
1892 if (!gfc_notify_std (GFC_STD_LEGACY, "AC-IMPLIED-DO initial "
1893 "expression references control variable "
1894 "at %L", &iter_var_loc))
1895 t = false;
1897 if (find_symbol_in_expr (iter_var, iter->end, &iter_var_loc))
1899 if (!gfc_notify_std (GFC_STD_LEGACY, "AC-IMPLIED-DO final "
1900 "expression references control variable "
1901 "at %L", &iter_var_loc))
1902 t = false;
1904 if (find_symbol_in_expr (iter_var, iter->step, &iter_var_loc))
1906 if (!gfc_notify_std (GFC_STD_LEGACY, "AC-IMPLIED-DO step "
1907 "expression references control variable "
1908 "at %L", &iter_var_loc))
1909 t = false;
1913 if (!gfc_resolve_expr (c->expr))
1914 t = false;
1916 if (UNLIMITED_POLY (c->expr))
1918 gfc_error ("Array constructor value at %L shall not be unlimited "
1919 "polymorphic [F2008: C4106]", &c->expr->where);
1920 t = false;
1924 return t;
1927 /* Resolve character array constructor. If it has a specified constant character
1928 length, pad/truncate the elements here; if the length is not specified and
1929 all elements are of compile-time known length, emit an error as this is
1930 invalid. */
1932 bool
1933 gfc_resolve_character_array_constructor (gfc_expr *expr)
1935 gfc_constructor *p;
1936 int found_length;
1938 gcc_assert (expr->expr_type == EXPR_ARRAY);
1939 gcc_assert (expr->ts.type == BT_CHARACTER);
1941 if (expr->ts.u.cl == NULL)
1943 for (p = gfc_constructor_first (expr->value.constructor);
1944 p; p = gfc_constructor_next (p))
1945 if (p->expr->ts.u.cl != NULL)
1947 /* Ensure that if there is a char_len around that it is
1948 used; otherwise the middle-end confuses them! */
1949 expr->ts.u.cl = p->expr->ts.u.cl;
1950 goto got_charlen;
1953 expr->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
1956 got_charlen:
1958 found_length = -1;
1960 if (expr->ts.u.cl->length == NULL)
1962 /* Check that all constant string elements have the same length until
1963 we reach the end or find a variable-length one. */
1965 for (p = gfc_constructor_first (expr->value.constructor);
1966 p; p = gfc_constructor_next (p))
1968 int current_length = -1;
1969 gfc_ref *ref;
1970 for (ref = p->expr->ref; ref; ref = ref->next)
1971 if (ref->type == REF_SUBSTRING
1972 && ref->u.ss.start->expr_type == EXPR_CONSTANT
1973 && ref->u.ss.end->expr_type == EXPR_CONSTANT)
1974 break;
1976 if (p->expr->expr_type == EXPR_CONSTANT)
1977 current_length = p->expr->value.character.length;
1978 else if (ref)
1980 long j;
1981 j = mpz_get_ui (ref->u.ss.end->value.integer)
1982 - mpz_get_ui (ref->u.ss.start->value.integer) + 1;
1983 current_length = (int) j;
1985 else if (p->expr->ts.u.cl && p->expr->ts.u.cl->length
1986 && p->expr->ts.u.cl->length->expr_type == EXPR_CONSTANT)
1988 long j;
1989 j = mpz_get_si (p->expr->ts.u.cl->length->value.integer);
1990 current_length = (int) j;
1992 else
1993 return true;
1995 gcc_assert (current_length != -1);
1997 if (found_length == -1)
1998 found_length = current_length;
1999 else if (found_length != current_length)
2001 gfc_error ("Different CHARACTER lengths (%d/%d) in array"
2002 " constructor at %L", found_length, current_length,
2003 &p->expr->where);
2004 return false;
2007 gcc_assert (found_length == current_length);
2010 gcc_assert (found_length != -1);
2012 /* Update the character length of the array constructor. */
2013 expr->ts.u.cl->length = gfc_get_int_expr (gfc_default_integer_kind,
2014 NULL, found_length);
2016 else
2018 /* We've got a character length specified. It should be an integer,
2019 otherwise an error is signalled elsewhere. */
2020 gcc_assert (expr->ts.u.cl->length);
2022 /* If we've got a constant character length, pad according to this.
2023 gfc_extract_int does check for BT_INTEGER and EXPR_CONSTANT and sets
2024 max_length only if they pass. */
2025 gfc_extract_int (expr->ts.u.cl->length, &found_length);
2027 /* Now pad/truncate the elements accordingly to the specified character
2028 length. This is ok inside this conditional, as in the case above
2029 (without typespec) all elements are verified to have the same length
2030 anyway. */
2031 if (found_length != -1)
2032 for (p = gfc_constructor_first (expr->value.constructor);
2033 p; p = gfc_constructor_next (p))
2034 if (p->expr->expr_type == EXPR_CONSTANT)
2036 gfc_expr *cl = NULL;
2037 int current_length = -1;
2038 bool has_ts;
2040 if (p->expr->ts.u.cl && p->expr->ts.u.cl->length)
2042 cl = p->expr->ts.u.cl->length;
2043 gfc_extract_int (cl, &current_length);
2046 /* If gfc_extract_int above set current_length, we implicitly
2047 know the type is BT_INTEGER and it's EXPR_CONSTANT. */
2049 has_ts = expr->ts.u.cl->length_from_typespec;
2051 if (! cl
2052 || (current_length != -1 && current_length != found_length))
2053 gfc_set_constant_character_len (found_length, p->expr,
2054 has_ts ? -1 : found_length);
2058 return true;
2062 /* Resolve all of the expressions in an array list. */
2064 bool
2065 gfc_resolve_array_constructor (gfc_expr *expr)
2067 bool t;
2069 t = resolve_array_list (expr->value.constructor);
2070 if (t)
2071 t = gfc_check_constructor_type (expr);
2073 /* gfc_resolve_character_array_constructor is called in gfc_resolve_expr after
2074 the call to this function, so we don't need to call it here; if it was
2075 called twice, an error message there would be duplicated. */
2077 return t;
2081 /* Copy an iterator structure. */
2083 gfc_iterator *
2084 gfc_copy_iterator (gfc_iterator *src)
2086 gfc_iterator *dest;
2088 if (src == NULL)
2089 return NULL;
2091 dest = gfc_get_iterator ();
2093 dest->var = gfc_copy_expr (src->var);
2094 dest->start = gfc_copy_expr (src->start);
2095 dest->end = gfc_copy_expr (src->end);
2096 dest->step = gfc_copy_expr (src->step);
2098 return dest;
2102 /********* Subroutines for determining the size of an array *********/
2104 /* These are needed just to accommodate RESHAPE(). There are no
2105 diagnostics here, we just return a negative number if something
2106 goes wrong. */
2109 /* Get the size of single dimension of an array specification. The
2110 array is guaranteed to be one dimensional. */
2112 bool
2113 spec_dimen_size (gfc_array_spec *as, int dimen, mpz_t *result)
2115 if (as == NULL)
2116 return false;
2118 if (dimen < 0 || dimen > as->rank - 1)
2119 gfc_internal_error ("spec_dimen_size(): Bad dimension");
2121 if (as->type != AS_EXPLICIT
2122 || as->lower[dimen]->expr_type != EXPR_CONSTANT
2123 || as->upper[dimen]->expr_type != EXPR_CONSTANT
2124 || as->lower[dimen]->ts.type != BT_INTEGER
2125 || as->upper[dimen]->ts.type != BT_INTEGER)
2126 return false;
2128 mpz_init (*result);
2130 mpz_sub (*result, as->upper[dimen]->value.integer,
2131 as->lower[dimen]->value.integer);
2133 mpz_add_ui (*result, *result, 1);
2135 return true;
2139 bool
2140 spec_size (gfc_array_spec *as, mpz_t *result)
2142 mpz_t size;
2143 int d;
2145 if (!as || as->type == AS_ASSUMED_RANK)
2146 return false;
2148 mpz_init_set_ui (*result, 1);
2150 for (d = 0; d < as->rank; d++)
2152 if (!spec_dimen_size (as, d, &size))
2154 mpz_clear (*result);
2155 return false;
2158 mpz_mul (*result, *result, size);
2159 mpz_clear (size);
2162 return true;
2166 /* Get the number of elements in an array section. Optionally, also supply
2167 the end value. */
2169 bool
2170 gfc_ref_dimen_size (gfc_array_ref *ar, int dimen, mpz_t *result, mpz_t *end)
2172 mpz_t upper, lower, stride;
2173 mpz_t diff;
2174 bool t;
2176 if (dimen < 0 || ar == NULL || dimen > ar->dimen - 1)
2177 gfc_internal_error ("gfc_ref_dimen_size(): Bad dimension");
2179 switch (ar->dimen_type[dimen])
2181 case DIMEN_ELEMENT:
2182 mpz_init (*result);
2183 mpz_set_ui (*result, 1);
2184 t = true;
2185 break;
2187 case DIMEN_VECTOR:
2188 t = gfc_array_size (ar->start[dimen], result); /* Recurse! */
2189 break;
2191 case DIMEN_RANGE:
2193 mpz_init (stride);
2195 if (ar->stride[dimen] == NULL)
2196 mpz_set_ui (stride, 1);
2197 else
2199 if (ar->stride[dimen]->expr_type != EXPR_CONSTANT)
2201 mpz_clear (stride);
2202 return false;
2204 mpz_set (stride, ar->stride[dimen]->value.integer);
2207 /* Calculate the number of elements via gfc_dep_differce, but only if
2208 start and end are both supplied in the reference or the array spec.
2209 This is to guard against strange but valid code like
2211 subroutine foo(a,n)
2212 real a(1:n)
2213 n = 3
2214 print *,size(a(n-1:))
2216 where the user changes the value of a variable. If we have to
2217 determine end as well, we cannot do this using gfc_dep_difference.
2218 Fall back to the constants-only code then. */
2220 if (end == NULL)
2222 bool use_dep;
2224 use_dep = gfc_dep_difference (ar->end[dimen], ar->start[dimen],
2225 &diff);
2226 if (!use_dep && ar->end[dimen] == NULL && ar->start[dimen] == NULL)
2227 use_dep = gfc_dep_difference (ar->as->upper[dimen],
2228 ar->as->lower[dimen], &diff);
2230 if (use_dep)
2232 mpz_init (*result);
2233 mpz_add (*result, diff, stride);
2234 mpz_div (*result, *result, stride);
2235 if (mpz_cmp_ui (*result, 0) < 0)
2236 mpz_set_ui (*result, 0);
2238 mpz_clear (stride);
2239 mpz_clear (diff);
2240 return true;
2245 /* Constant-only code here, which covers more cases
2246 like a(:4) etc. */
2247 mpz_init (upper);
2248 mpz_init (lower);
2249 t = false;
2251 if (ar->start[dimen] == NULL)
2253 if (ar->as->lower[dimen] == NULL
2254 || ar->as->lower[dimen]->expr_type != EXPR_CONSTANT
2255 || ar->as->lower[dimen]->ts.type != BT_INTEGER)
2256 goto cleanup;
2257 mpz_set (lower, ar->as->lower[dimen]->value.integer);
2259 else
2261 if (ar->start[dimen]->expr_type != EXPR_CONSTANT)
2262 goto cleanup;
2263 mpz_set (lower, ar->start[dimen]->value.integer);
2266 if (ar->end[dimen] == NULL)
2268 if (ar->as->upper[dimen] == NULL
2269 || ar->as->upper[dimen]->expr_type != EXPR_CONSTANT
2270 || ar->as->upper[dimen]->ts.type != BT_INTEGER)
2271 goto cleanup;
2272 mpz_set (upper, ar->as->upper[dimen]->value.integer);
2274 else
2276 if (ar->end[dimen]->expr_type != EXPR_CONSTANT)
2277 goto cleanup;
2278 mpz_set (upper, ar->end[dimen]->value.integer);
2281 mpz_init (*result);
2282 mpz_sub (*result, upper, lower);
2283 mpz_add (*result, *result, stride);
2284 mpz_div (*result, *result, stride);
2286 /* Zero stride caught earlier. */
2287 if (mpz_cmp_ui (*result, 0) < 0)
2288 mpz_set_ui (*result, 0);
2289 t = true;
2291 if (end)
2293 mpz_init (*end);
2295 mpz_sub_ui (*end, *result, 1UL);
2296 mpz_mul (*end, *end, stride);
2297 mpz_add (*end, *end, lower);
2300 cleanup:
2301 mpz_clear (upper);
2302 mpz_clear (lower);
2303 mpz_clear (stride);
2304 return t;
2306 default:
2307 gfc_internal_error ("gfc_ref_dimen_size(): Bad dimen_type");
2310 return t;
2314 static bool
2315 ref_size (gfc_array_ref *ar, mpz_t *result)
2317 mpz_t size;
2318 int d;
2320 mpz_init_set_ui (*result, 1);
2322 for (d = 0; d < ar->dimen; d++)
2324 if (!gfc_ref_dimen_size (ar, d, &size, NULL))
2326 mpz_clear (*result);
2327 return false;
2330 mpz_mul (*result, *result, size);
2331 mpz_clear (size);
2334 return true;
2338 /* Given an array expression and a dimension, figure out how many
2339 elements it has along that dimension. Returns true if we were
2340 able to return a result in the 'result' variable, false
2341 otherwise. */
2343 bool
2344 gfc_array_dimen_size (gfc_expr *array, int dimen, mpz_t *result)
2346 gfc_ref *ref;
2347 int i;
2349 gcc_assert (array != NULL);
2351 if (array->ts.type == BT_CLASS)
2352 return false;
2354 if (array->rank == -1)
2355 return false;
2357 if (dimen < 0 || dimen > array->rank - 1)
2358 gfc_internal_error ("gfc_array_dimen_size(): Bad dimension");
2360 switch (array->expr_type)
2362 case EXPR_VARIABLE:
2363 case EXPR_FUNCTION:
2364 for (ref = array->ref; ref; ref = ref->next)
2366 if (ref->type != REF_ARRAY)
2367 continue;
2369 if (ref->u.ar.type == AR_FULL)
2370 return spec_dimen_size (ref->u.ar.as, dimen, result);
2372 if (ref->u.ar.type == AR_SECTION)
2374 for (i = 0; dimen >= 0; i++)
2375 if (ref->u.ar.dimen_type[i] != DIMEN_ELEMENT)
2376 dimen--;
2378 return gfc_ref_dimen_size (&ref->u.ar, i - 1, result, NULL);
2382 if (array->shape && array->shape[dimen])
2384 mpz_init_set (*result, array->shape[dimen]);
2385 return true;
2388 if (array->symtree->n.sym->attr.generic
2389 && array->value.function.esym != NULL)
2391 if (!spec_dimen_size (array->value.function.esym->as, dimen, result))
2392 return false;
2394 else if (!spec_dimen_size (array->symtree->n.sym->as, dimen, result))
2395 return false;
2397 break;
2399 case EXPR_ARRAY:
2400 if (array->shape == NULL) {
2401 /* Expressions with rank > 1 should have "shape" properly set */
2402 if ( array->rank != 1 )
2403 gfc_internal_error ("gfc_array_dimen_size(): Bad EXPR_ARRAY expr");
2404 return gfc_array_size(array, result);
2407 /* Fall through */
2408 default:
2409 if (array->shape == NULL)
2410 return false;
2412 mpz_init_set (*result, array->shape[dimen]);
2414 break;
2417 return true;
2421 /* Given an array expression, figure out how many elements are in the
2422 array. Returns true if this is possible, and sets the 'result'
2423 variable. Otherwise returns false. */
2425 bool
2426 gfc_array_size (gfc_expr *array, mpz_t *result)
2428 expand_info expand_save;
2429 gfc_ref *ref;
2430 int i;
2431 bool t;
2433 if (array->ts.type == BT_CLASS)
2434 return false;
2436 switch (array->expr_type)
2438 case EXPR_ARRAY:
2439 gfc_push_suppress_errors ();
2441 expand_save = current_expand;
2443 current_expand.count = result;
2444 mpz_init_set_ui (*result, 0);
2446 current_expand.expand_work_function = count_elements;
2447 iter_stack = NULL;
2449 t = expand_constructor (array->value.constructor);
2451 gfc_pop_suppress_errors ();
2453 if (!t)
2454 mpz_clear (*result);
2455 current_expand = expand_save;
2456 return t;
2458 case EXPR_VARIABLE:
2459 for (ref = array->ref; ref; ref = ref->next)
2461 if (ref->type != REF_ARRAY)
2462 continue;
2464 if (ref->u.ar.type == AR_FULL)
2465 return spec_size (ref->u.ar.as, result);
2467 if (ref->u.ar.type == AR_SECTION)
2468 return ref_size (&ref->u.ar, result);
2471 return spec_size (array->symtree->n.sym->as, result);
2474 default:
2475 if (array->rank == 0 || array->shape == NULL)
2476 return false;
2478 mpz_init_set_ui (*result, 1);
2480 for (i = 0; i < array->rank; i++)
2481 mpz_mul (*result, *result, array->shape[i]);
2483 break;
2486 return true;
2490 /* Given an array reference, return the shape of the reference in an
2491 array of mpz_t integers. */
2493 bool
2494 gfc_array_ref_shape (gfc_array_ref *ar, mpz_t *shape)
2496 int d;
2497 int i;
2499 d = 0;
2501 switch (ar->type)
2503 case AR_FULL:
2504 for (; d < ar->as->rank; d++)
2505 if (!spec_dimen_size (ar->as, d, &shape[d]))
2506 goto cleanup;
2508 return true;
2510 case AR_SECTION:
2511 for (i = 0; i < ar->dimen; i++)
2513 if (ar->dimen_type[i] != DIMEN_ELEMENT)
2515 if (!gfc_ref_dimen_size (ar, i, &shape[d], NULL))
2516 goto cleanup;
2517 d++;
2521 return true;
2523 default:
2524 break;
2527 cleanup:
2528 gfc_clear_shape (shape, d);
2529 return false;
2533 /* Given an array expression, find the array reference structure that
2534 characterizes the reference. */
2536 gfc_array_ref *
2537 gfc_find_array_ref (gfc_expr *e)
2539 gfc_ref *ref;
2541 for (ref = e->ref; ref; ref = ref->next)
2542 if (ref->type == REF_ARRAY
2543 && (ref->u.ar.type == AR_FULL || ref->u.ar.type == AR_SECTION))
2544 break;
2546 if (ref == NULL)
2547 gfc_internal_error ("gfc_find_array_ref(): No ref found");
2549 return &ref->u.ar;
2553 /* Find out if an array shape is known at compile time. */
2556 gfc_is_compile_time_shape (gfc_array_spec *as)
2558 int i;
2560 if (as->type != AS_EXPLICIT)
2561 return 0;
2563 for (i = 0; i < as->rank; i++)
2564 if (!gfc_is_constant_expr (as->lower[i])
2565 || !gfc_is_constant_expr (as->upper[i]))
2566 return 0;
2568 return 1;