re PR fortran/67805 (ICE on array constructor with wrong character specification)
[official-gcc.git] / gcc / fortran / array.c
blob2355a980a6112ccfc08f05cdd445714e4ac436fb
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 "flags.h"
26 #include "gfortran.h"
27 #include "match.h"
28 #include "constructor.h"
30 /**************** Array reference matching subroutines *****************/
32 /* Copy an array reference structure. */
34 gfc_array_ref *
35 gfc_copy_array_ref (gfc_array_ref *src)
37 gfc_array_ref *dest;
38 int i;
40 if (src == NULL)
41 return NULL;
43 dest = gfc_get_array_ref ();
45 *dest = *src;
47 for (i = 0; i < GFC_MAX_DIMENSIONS; i++)
49 dest->start[i] = gfc_copy_expr (src->start[i]);
50 dest->end[i] = gfc_copy_expr (src->end[i]);
51 dest->stride[i] = gfc_copy_expr (src->stride[i]);
54 return dest;
58 /* Match a single dimension of an array reference. This can be a
59 single element or an array section. Any modifications we've made
60 to the ar structure are cleaned up by the caller. If the init
61 is set, we require the subscript to be a valid initialization
62 expression. */
64 static match
65 match_subscript (gfc_array_ref *ar, int init, bool match_star)
67 match m = MATCH_ERROR;
68 bool star = false;
69 int i;
71 i = ar->dimen + ar->codimen;
73 gfc_gobble_whitespace ();
74 ar->c_where[i] = gfc_current_locus;
75 ar->start[i] = ar->end[i] = ar->stride[i] = NULL;
77 /* We can't be sure of the difference between DIMEN_ELEMENT and
78 DIMEN_VECTOR until we know the type of the element itself at
79 resolution time. */
81 ar->dimen_type[i] = DIMEN_UNKNOWN;
83 if (gfc_match_char (':') == MATCH_YES)
84 goto end_element;
86 /* Get start element. */
87 if (match_star && (m = gfc_match_char ('*')) == MATCH_YES)
88 star = true;
90 if (!star && init)
91 m = gfc_match_init_expr (&ar->start[i]);
92 else if (!star)
93 m = gfc_match_expr (&ar->start[i]);
95 if (m == MATCH_NO)
96 gfc_error ("Expected array subscript at %C");
97 if (m != MATCH_YES)
98 return MATCH_ERROR;
100 if (gfc_match_char (':') == MATCH_NO)
101 goto matched;
103 if (star)
105 gfc_error ("Unexpected %<*%> in coarray subscript at %C");
106 return MATCH_ERROR;
109 /* Get an optional end element. Because we've seen the colon, we
110 definitely have a range along this dimension. */
111 end_element:
112 ar->dimen_type[i] = DIMEN_RANGE;
114 if (match_star && (m = gfc_match_char ('*')) == MATCH_YES)
115 star = true;
116 else if (init)
117 m = gfc_match_init_expr (&ar->end[i]);
118 else
119 m = gfc_match_expr (&ar->end[i]);
121 if (m == MATCH_ERROR)
122 return MATCH_ERROR;
124 /* See if we have an optional stride. */
125 if (gfc_match_char (':') == MATCH_YES)
127 if (star)
129 gfc_error ("Strides not allowed in coarray subscript at %C");
130 return MATCH_ERROR;
133 m = init ? gfc_match_init_expr (&ar->stride[i])
134 : gfc_match_expr (&ar->stride[i]);
136 if (m == MATCH_NO)
137 gfc_error ("Expected array subscript stride at %C");
138 if (m != MATCH_YES)
139 return MATCH_ERROR;
142 matched:
143 if (star)
144 ar->dimen_type[i] = DIMEN_STAR;
146 return MATCH_YES;
150 /* Match an array reference, whether it is the whole array or a
151 particular elements or a section. If init is set, the reference has
152 to consist of init expressions. */
154 match
155 gfc_match_array_ref (gfc_array_ref *ar, gfc_array_spec *as, int init,
156 int corank)
158 match m;
159 bool matched_bracket = false;
161 memset (ar, '\0', sizeof (*ar));
163 ar->where = gfc_current_locus;
164 ar->as = as;
165 ar->type = AR_UNKNOWN;
167 if (gfc_match_char ('[') == MATCH_YES)
169 matched_bracket = true;
170 goto coarray;
173 if (gfc_match_char ('(') != MATCH_YES)
175 ar->type = AR_FULL;
176 ar->dimen = 0;
177 return MATCH_YES;
180 for (ar->dimen = 0; ar->dimen < GFC_MAX_DIMENSIONS; ar->dimen++)
182 m = match_subscript (ar, init, false);
183 if (m == MATCH_ERROR)
184 return MATCH_ERROR;
186 if (gfc_match_char (')') == MATCH_YES)
188 ar->dimen++;
189 goto coarray;
192 if (gfc_match_char (',') != MATCH_YES)
194 gfc_error ("Invalid form of array reference at %C");
195 return MATCH_ERROR;
199 gfc_error ("Array reference at %C cannot have more than %d dimensions",
200 GFC_MAX_DIMENSIONS);
201 return MATCH_ERROR;
203 coarray:
204 if (!matched_bracket && gfc_match_char ('[') != MATCH_YES)
206 if (ar->dimen > 0)
207 return MATCH_YES;
208 else
209 return MATCH_ERROR;
212 if (flag_coarray == GFC_FCOARRAY_NONE)
214 gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to enable");
215 return MATCH_ERROR;
218 if (corank == 0)
220 gfc_error ("Unexpected coarray designator at %C");
221 return MATCH_ERROR;
224 for (ar->codimen = 0; ar->codimen + ar->dimen < GFC_MAX_DIMENSIONS; ar->codimen++)
226 m = match_subscript (ar, init, true);
227 if (m == MATCH_ERROR)
228 return MATCH_ERROR;
230 if (gfc_match_char (']') == MATCH_YES)
232 ar->codimen++;
233 if (ar->codimen < corank)
235 gfc_error ("Too few codimensions at %C, expected %d not %d",
236 corank, ar->codimen);
237 return MATCH_ERROR;
239 if (ar->codimen > corank)
241 gfc_error ("Too many codimensions at %C, expected %d not %d",
242 corank, ar->codimen);
243 return MATCH_ERROR;
245 return MATCH_YES;
248 if (gfc_match_char (',') != MATCH_YES)
250 if (gfc_match_char ('*') == MATCH_YES)
251 gfc_error ("Unexpected %<*%> for codimension %d of %d at %C",
252 ar->codimen + 1, corank);
253 else
254 gfc_error ("Invalid form of coarray reference at %C");
255 return MATCH_ERROR;
257 else if (ar->dimen_type[ar->codimen + ar->dimen] == DIMEN_STAR)
259 gfc_error ("Unexpected %<*%> for codimension %d of %d at %C",
260 ar->codimen + 1, corank);
261 return MATCH_ERROR;
264 if (ar->codimen >= corank)
266 gfc_error ("Invalid codimension %d at %C, only %d codimensions exist",
267 ar->codimen + 1, corank);
268 return MATCH_ERROR;
272 gfc_error ("Array reference at %C cannot have more than %d dimensions",
273 GFC_MAX_DIMENSIONS);
274 return MATCH_ERROR;
279 /************** Array specification matching subroutines ***************/
281 /* Free all of the expressions associated with array bounds
282 specifications. */
284 void
285 gfc_free_array_spec (gfc_array_spec *as)
287 int i;
289 if (as == NULL)
290 return;
292 for (i = 0; i < as->rank + as->corank; i++)
294 gfc_free_expr (as->lower[i]);
295 gfc_free_expr (as->upper[i]);
298 free (as);
302 /* Take an array bound, resolves the expression, that make up the
303 shape and check associated constraints. */
305 static bool
306 resolve_array_bound (gfc_expr *e, int check_constant)
308 if (e == NULL)
309 return true;
311 if (!gfc_resolve_expr (e)
312 || !gfc_specification_expr (e))
313 return false;
315 if (check_constant && !gfc_is_constant_expr (e))
317 if (e->expr_type == EXPR_VARIABLE)
318 gfc_error ("Variable %qs at %L in this context must be constant",
319 e->symtree->n.sym->name, &e->where);
320 else
321 gfc_error ("Expression at %L in this context must be constant",
322 &e->where);
323 return false;
326 return true;
330 /* Takes an array specification, resolves the expressions that make up
331 the shape and make sure everything is integral. */
333 bool
334 gfc_resolve_array_spec (gfc_array_spec *as, int check_constant)
336 gfc_expr *e;
337 int i;
339 if (as == NULL)
340 return true;
342 if (as->resolved)
343 return true;
345 for (i = 0; i < as->rank + as->corank; i++)
347 e = as->lower[i];
348 if (!resolve_array_bound (e, check_constant))
349 return false;
351 e = as->upper[i];
352 if (!resolve_array_bound (e, check_constant))
353 return false;
355 if ((as->lower[i] == NULL) || (as->upper[i] == NULL))
356 continue;
358 /* If the size is negative in this dimension, set it to zero. */
359 if (as->lower[i]->expr_type == EXPR_CONSTANT
360 && as->upper[i]->expr_type == EXPR_CONSTANT
361 && mpz_cmp (as->upper[i]->value.integer,
362 as->lower[i]->value.integer) < 0)
364 gfc_free_expr (as->upper[i]);
365 as->upper[i] = gfc_copy_expr (as->lower[i]);
366 mpz_sub_ui (as->upper[i]->value.integer,
367 as->upper[i]->value.integer, 1);
371 as->resolved = true;
373 return true;
377 /* Match a single array element specification. The return values as
378 well as the upper and lower bounds of the array spec are filled
379 in according to what we see on the input. The caller makes sure
380 individual specifications make sense as a whole.
383 Parsed Lower Upper Returned
384 ------------------------------------
385 : NULL NULL AS_DEFERRED (*)
386 x 1 x AS_EXPLICIT
387 x: x NULL AS_ASSUMED_SHAPE
388 x:y x y AS_EXPLICIT
389 x:* x NULL AS_ASSUMED_SIZE
390 * 1 NULL AS_ASSUMED_SIZE
392 (*) For non-pointer dummy arrays this is AS_ASSUMED_SHAPE. This
393 is fixed during the resolution of formal interfaces.
395 Anything else AS_UNKNOWN. */
397 static array_type
398 match_array_element_spec (gfc_array_spec *as)
400 gfc_expr **upper, **lower;
401 match m;
402 int rank;
404 rank = as->rank == -1 ? 0 : as->rank;
405 lower = &as->lower[rank + as->corank - 1];
406 upper = &as->upper[rank + as->corank - 1];
408 if (gfc_match_char ('*') == MATCH_YES)
410 *lower = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
411 return AS_ASSUMED_SIZE;
414 if (gfc_match_char (':') == MATCH_YES)
415 return AS_DEFERRED;
417 m = gfc_match_expr (upper);
418 if (m == MATCH_NO)
419 gfc_error ("Expected expression in array specification at %C");
420 if (m != MATCH_YES)
421 return AS_UNKNOWN;
422 if (!gfc_expr_check_typed (*upper, gfc_current_ns, false))
423 return AS_UNKNOWN;
425 if (gfc_match_char (':') == MATCH_NO)
427 *lower = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
428 return AS_EXPLICIT;
431 *lower = *upper;
432 *upper = NULL;
434 if (gfc_match_char ('*') == MATCH_YES)
435 return AS_ASSUMED_SIZE;
437 m = gfc_match_expr (upper);
438 if (m == MATCH_ERROR)
439 return AS_UNKNOWN;
440 if (m == MATCH_NO)
441 return AS_ASSUMED_SHAPE;
442 if (!gfc_expr_check_typed (*upper, gfc_current_ns, false))
443 return AS_UNKNOWN;
445 return AS_EXPLICIT;
449 /* Matches an array specification, incidentally figuring out what sort
450 it is. Match either a normal array specification, or a coarray spec
451 or both. Optionally allow [:] for coarrays. */
453 match
454 gfc_match_array_spec (gfc_array_spec **asp, bool match_dim, bool match_codim)
456 array_type current_type;
457 gfc_array_spec *as;
458 int i;
460 as = gfc_get_array_spec ();
462 if (!match_dim)
463 goto coarray;
465 if (gfc_match_char ('(') != MATCH_YES)
467 if (!match_codim)
468 goto done;
469 goto coarray;
472 if (gfc_match (" .. )") == MATCH_YES)
474 as->type = AS_ASSUMED_RANK;
475 as->rank = -1;
477 if (!gfc_notify_std (GFC_STD_F2008_TS, "Assumed-rank array at %C"))
478 goto cleanup;
480 if (!match_codim)
481 goto done;
482 goto coarray;
485 for (;;)
487 as->rank++;
488 current_type = match_array_element_spec (as);
490 /* Note that current_type == AS_ASSUMED_SIZE for both assumed-size
491 and implied-shape specifications. If the rank is at least 2, we can
492 distinguish between them. But for rank 1, we currently return
493 ASSUMED_SIZE; this gets adjusted later when we know for sure
494 whether the symbol parsed is a PARAMETER or not. */
496 if (as->rank == 1)
498 if (current_type == AS_UNKNOWN)
499 goto cleanup;
500 as->type = current_type;
502 else
503 switch (as->type)
504 { /* See how current spec meshes with the existing. */
505 case AS_UNKNOWN:
506 goto cleanup;
508 case AS_IMPLIED_SHAPE:
509 if (current_type != AS_ASSUMED_SHAPE)
511 gfc_error ("Bad array specification for implied-shape"
512 " array at %C");
513 goto cleanup;
515 break;
517 case AS_EXPLICIT:
518 if (current_type == AS_ASSUMED_SIZE)
520 as->type = AS_ASSUMED_SIZE;
521 break;
524 if (current_type == AS_EXPLICIT)
525 break;
527 gfc_error ("Bad array specification for an explicitly shaped "
528 "array at %C");
530 goto cleanup;
532 case AS_ASSUMED_SHAPE:
533 if ((current_type == AS_ASSUMED_SHAPE)
534 || (current_type == AS_DEFERRED))
535 break;
537 gfc_error ("Bad array specification for assumed shape "
538 "array at %C");
539 goto cleanup;
541 case AS_DEFERRED:
542 if (current_type == AS_DEFERRED)
543 break;
545 if (current_type == AS_ASSUMED_SHAPE)
547 as->type = AS_ASSUMED_SHAPE;
548 break;
551 gfc_error ("Bad specification for deferred shape array at %C");
552 goto cleanup;
554 case AS_ASSUMED_SIZE:
555 if (as->rank == 2 && current_type == AS_ASSUMED_SIZE)
557 as->type = AS_IMPLIED_SHAPE;
558 break;
561 gfc_error ("Bad specification for assumed size array at %C");
562 goto cleanup;
564 case AS_ASSUMED_RANK:
565 gcc_unreachable ();
568 if (gfc_match_char (')') == MATCH_YES)
569 break;
571 if (gfc_match_char (',') != MATCH_YES)
573 gfc_error ("Expected another dimension in array declaration at %C");
574 goto cleanup;
577 if (as->rank + as->corank >= GFC_MAX_DIMENSIONS)
579 gfc_error ("Array specification at %C has more than %d dimensions",
580 GFC_MAX_DIMENSIONS);
581 goto cleanup;
584 if (as->corank + as->rank >= 7
585 && !gfc_notify_std (GFC_STD_F2008, "Array specification at %C "
586 "with more than 7 dimensions"))
587 goto cleanup;
590 if (!match_codim)
591 goto done;
593 coarray:
594 if (gfc_match_char ('[') != MATCH_YES)
595 goto done;
597 if (!gfc_notify_std (GFC_STD_F2008, "Coarray declaration at %C"))
598 goto cleanup;
600 if (flag_coarray == GFC_FCOARRAY_NONE)
602 gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to enable");
603 goto cleanup;
606 if (as->rank >= GFC_MAX_DIMENSIONS)
608 gfc_error ("Array specification at %C has more than %d "
609 "dimensions", GFC_MAX_DIMENSIONS);
610 goto cleanup;
613 for (;;)
615 as->corank++;
616 current_type = match_array_element_spec (as);
618 if (current_type == AS_UNKNOWN)
619 goto cleanup;
621 if (as->corank == 1)
622 as->cotype = current_type;
623 else
624 switch (as->cotype)
625 { /* See how current spec meshes with the existing. */
626 case AS_IMPLIED_SHAPE:
627 case AS_UNKNOWN:
628 goto cleanup;
630 case AS_EXPLICIT:
631 if (current_type == AS_ASSUMED_SIZE)
633 as->cotype = AS_ASSUMED_SIZE;
634 break;
637 if (current_type == AS_EXPLICIT)
638 break;
640 gfc_error ("Bad array specification for an explicitly "
641 "shaped array at %C");
643 goto cleanup;
645 case AS_ASSUMED_SHAPE:
646 if ((current_type == AS_ASSUMED_SHAPE)
647 || (current_type == AS_DEFERRED))
648 break;
650 gfc_error ("Bad array specification for assumed shape "
651 "array at %C");
652 goto cleanup;
654 case AS_DEFERRED:
655 if (current_type == AS_DEFERRED)
656 break;
658 if (current_type == AS_ASSUMED_SHAPE)
660 as->cotype = AS_ASSUMED_SHAPE;
661 break;
664 gfc_error ("Bad specification for deferred shape array at %C");
665 goto cleanup;
667 case AS_ASSUMED_SIZE:
668 gfc_error ("Bad specification for assumed size array at %C");
669 goto cleanup;
671 case AS_ASSUMED_RANK:
672 gcc_unreachable ();
675 if (gfc_match_char (']') == MATCH_YES)
676 break;
678 if (gfc_match_char (',') != MATCH_YES)
680 gfc_error ("Expected another dimension in array declaration at %C");
681 goto cleanup;
684 if (as->rank + as->corank >= GFC_MAX_DIMENSIONS)
686 gfc_error ("Array specification at %C has more than %d "
687 "dimensions", GFC_MAX_DIMENSIONS);
688 goto cleanup;
692 if (current_type == AS_EXPLICIT)
694 gfc_error ("Upper bound of last coarray dimension must be %<*%> at %C");
695 goto cleanup;
698 if (as->cotype == AS_ASSUMED_SIZE)
699 as->cotype = AS_EXPLICIT;
701 if (as->rank == 0)
702 as->type = as->cotype;
704 done:
705 if (as->rank == 0 && as->corank == 0)
707 *asp = NULL;
708 gfc_free_array_spec (as);
709 return MATCH_NO;
712 /* If a lower bounds of an assumed shape array is blank, put in one. */
713 if (as->type == AS_ASSUMED_SHAPE)
715 for (i = 0; i < as->rank + as->corank; i++)
717 if (as->lower[i] == NULL)
718 as->lower[i] = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
722 *asp = as;
724 return MATCH_YES;
726 cleanup:
727 /* Something went wrong. */
728 gfc_free_array_spec (as);
729 return MATCH_ERROR;
733 /* Given a symbol and an array specification, modify the symbol to
734 have that array specification. The error locus is needed in case
735 something goes wrong. On failure, the caller must free the spec. */
737 bool
738 gfc_set_array_spec (gfc_symbol *sym, gfc_array_spec *as, locus *error_loc)
740 int i;
742 if (as == NULL)
743 return true;
745 if (as->rank
746 && !gfc_add_dimension (&sym->attr, sym->name, error_loc))
747 return false;
749 if (as->corank
750 && !gfc_add_codimension (&sym->attr, sym->name, error_loc))
751 return false;
753 if (sym->as == NULL)
755 sym->as = as;
756 return true;
759 if ((sym->as->type == AS_ASSUMED_RANK && as->corank)
760 || (as->type == AS_ASSUMED_RANK && sym->as->corank))
762 gfc_error ("The assumed-rank array %qs at %L shall not have a "
763 "codimension", sym->name, error_loc);
764 return false;
767 if (as->corank)
769 /* The "sym" has no corank (checked via gfc_add_codimension). Thus
770 the codimension is simply added. */
771 gcc_assert (as->rank == 0 && sym->as->corank == 0);
773 sym->as->cotype = as->cotype;
774 sym->as->corank = as->corank;
775 for (i = 0; i < as->corank; i++)
777 sym->as->lower[sym->as->rank + i] = as->lower[i];
778 sym->as->upper[sym->as->rank + i] = as->upper[i];
781 else
783 /* The "sym" has no rank (checked via gfc_add_dimension). Thus
784 the dimension is added - but first the codimensions (if existing
785 need to be shifted to make space for the dimension. */
786 gcc_assert (as->corank == 0 && sym->as->rank == 0);
788 sym->as->rank = as->rank;
789 sym->as->type = as->type;
790 sym->as->cray_pointee = as->cray_pointee;
791 sym->as->cp_was_assumed = as->cp_was_assumed;
793 for (i = 0; i < sym->as->corank; i++)
795 sym->as->lower[as->rank + i] = sym->as->lower[i];
796 sym->as->upper[as->rank + i] = sym->as->upper[i];
798 for (i = 0; i < as->rank; i++)
800 sym->as->lower[i] = as->lower[i];
801 sym->as->upper[i] = as->upper[i];
805 free (as);
806 return true;
810 /* Copy an array specification. */
812 gfc_array_spec *
813 gfc_copy_array_spec (gfc_array_spec *src)
815 gfc_array_spec *dest;
816 int i;
818 if (src == NULL)
819 return NULL;
821 dest = gfc_get_array_spec ();
823 *dest = *src;
825 for (i = 0; i < dest->rank + dest->corank; i++)
827 dest->lower[i] = gfc_copy_expr (dest->lower[i]);
828 dest->upper[i] = gfc_copy_expr (dest->upper[i]);
831 return dest;
835 /* Returns nonzero if the two expressions are equal. Only handles integer
836 constants. */
838 static int
839 compare_bounds (gfc_expr *bound1, gfc_expr *bound2)
841 if (bound1 == NULL || bound2 == NULL
842 || bound1->expr_type != EXPR_CONSTANT
843 || bound2->expr_type != EXPR_CONSTANT
844 || bound1->ts.type != BT_INTEGER
845 || bound2->ts.type != BT_INTEGER)
846 gfc_internal_error ("gfc_compare_array_spec(): Array spec clobbered");
848 if (mpz_cmp (bound1->value.integer, bound2->value.integer) == 0)
849 return 1;
850 else
851 return 0;
855 /* Compares two array specifications. They must be constant or deferred
856 shape. */
859 gfc_compare_array_spec (gfc_array_spec *as1, gfc_array_spec *as2)
861 int i;
863 if (as1 == NULL && as2 == NULL)
864 return 1;
866 if (as1 == NULL || as2 == NULL)
867 return 0;
869 if (as1->rank != as2->rank)
870 return 0;
872 if (as1->corank != as2->corank)
873 return 0;
875 if (as1->rank == 0)
876 return 1;
878 if (as1->type != as2->type)
879 return 0;
881 if (as1->type == AS_EXPLICIT)
882 for (i = 0; i < as1->rank + as1->corank; i++)
884 if (compare_bounds (as1->lower[i], as2->lower[i]) == 0)
885 return 0;
887 if (compare_bounds (as1->upper[i], as2->upper[i]) == 0)
888 return 0;
891 return 1;
895 /****************** Array constructor functions ******************/
898 /* Given an expression node that might be an array constructor and a
899 symbol, make sure that no iterators in this or child constructors
900 use the symbol as an implied-DO iterator. Returns nonzero if a
901 duplicate was found. */
903 static int
904 check_duplicate_iterator (gfc_constructor_base base, gfc_symbol *master)
906 gfc_constructor *c;
907 gfc_expr *e;
909 for (c = gfc_constructor_first (base); c; c = gfc_constructor_next (c))
911 e = c->expr;
913 if (e->expr_type == EXPR_ARRAY
914 && check_duplicate_iterator (e->value.constructor, master))
915 return 1;
917 if (c->iterator == NULL)
918 continue;
920 if (c->iterator->var->symtree->n.sym == master)
922 gfc_error ("DO-iterator %qs at %L is inside iterator of the "
923 "same name", master->name, &c->where);
925 return 1;
929 return 0;
933 /* Forward declaration because these functions are mutually recursive. */
934 static match match_array_cons_element (gfc_constructor_base *);
936 /* Match a list of array elements. */
938 static match
939 match_array_list (gfc_constructor_base *result)
941 gfc_constructor_base head;
942 gfc_constructor *p;
943 gfc_iterator iter;
944 locus old_loc;
945 gfc_expr *e;
946 match m;
947 int n;
949 old_loc = gfc_current_locus;
951 if (gfc_match_char ('(') == MATCH_NO)
952 return MATCH_NO;
954 memset (&iter, '\0', sizeof (gfc_iterator));
955 head = NULL;
957 m = match_array_cons_element (&head);
958 if (m != MATCH_YES)
959 goto cleanup;
961 if (gfc_match_char (',') != MATCH_YES)
963 m = MATCH_NO;
964 goto cleanup;
967 for (n = 1;; n++)
969 m = gfc_match_iterator (&iter, 0);
970 if (m == MATCH_YES)
971 break;
972 if (m == MATCH_ERROR)
973 goto cleanup;
975 m = match_array_cons_element (&head);
976 if (m == MATCH_ERROR)
977 goto cleanup;
978 if (m == MATCH_NO)
980 if (n > 2)
981 goto syntax;
982 m = MATCH_NO;
983 goto cleanup; /* Could be a complex constant */
986 if (gfc_match_char (',') != MATCH_YES)
988 if (n > 2)
989 goto syntax;
990 m = MATCH_NO;
991 goto cleanup;
995 if (gfc_match_char (')') != MATCH_YES)
996 goto syntax;
998 if (check_duplicate_iterator (head, iter.var->symtree->n.sym))
1000 m = MATCH_ERROR;
1001 goto cleanup;
1004 e = gfc_get_array_expr (BT_UNKNOWN, 0, &old_loc);
1005 e->value.constructor = head;
1007 p = gfc_constructor_append_expr (result, e, &gfc_current_locus);
1008 p->iterator = gfc_get_iterator ();
1009 *p->iterator = iter;
1011 return MATCH_YES;
1013 syntax:
1014 gfc_error ("Syntax error in array constructor at %C");
1015 m = MATCH_ERROR;
1017 cleanup:
1018 gfc_constructor_free (head);
1019 gfc_free_iterator (&iter, 0);
1020 gfc_current_locus = old_loc;
1021 return m;
1025 /* Match a single element of an array constructor, which can be a
1026 single expression or a list of elements. */
1028 static match
1029 match_array_cons_element (gfc_constructor_base *result)
1031 gfc_expr *expr;
1032 match m;
1034 m = match_array_list (result);
1035 if (m != MATCH_NO)
1036 return m;
1038 m = gfc_match_expr (&expr);
1039 if (m != MATCH_YES)
1040 return m;
1042 gfc_constructor_append_expr (result, expr, &gfc_current_locus);
1043 return MATCH_YES;
1047 /* Match an array constructor. */
1049 match
1050 gfc_match_array_constructor (gfc_expr **result)
1052 gfc_constructor_base head, new_cons;
1053 gfc_undo_change_set changed_syms;
1054 gfc_expr *expr;
1055 gfc_typespec ts;
1056 locus where;
1057 match m;
1058 const char *end_delim;
1059 bool seen_ts;
1061 if (gfc_match (" (/") == MATCH_NO)
1063 if (gfc_match (" [") == MATCH_NO)
1064 return MATCH_NO;
1065 else
1067 if (!gfc_notify_std (GFC_STD_F2003, "[...] "
1068 "style array constructors at %C"))
1069 return MATCH_ERROR;
1070 end_delim = " ]";
1073 else
1074 end_delim = " /)";
1076 where = gfc_current_locus;
1077 head = new_cons = NULL;
1078 seen_ts = false;
1080 /* Try to match an optional "type-spec ::" */
1081 gfc_clear_ts (&ts);
1082 gfc_new_undo_checkpoint (changed_syms);
1083 m = gfc_match_type_spec (&ts);
1084 if (m == MATCH_YES)
1086 seen_ts = (gfc_match (" ::") == MATCH_YES);
1088 if (seen_ts)
1090 if (!gfc_notify_std (GFC_STD_F2003, "Array constructor "
1091 "including type specification at %C"))
1093 gfc_restore_last_undo_checkpoint ();
1094 goto cleanup;
1097 if (ts.deferred)
1099 gfc_error ("Type-spec at %L cannot contain a deferred "
1100 "type parameter", &where);
1101 gfc_restore_last_undo_checkpoint ();
1102 goto cleanup;
1106 else if (m == MATCH_ERROR)
1108 gfc_restore_last_undo_checkpoint ();
1109 goto cleanup;
1112 if (seen_ts)
1113 gfc_drop_last_undo_checkpoint ();
1114 else
1116 gfc_restore_last_undo_checkpoint ();
1117 gfc_current_locus = where;
1120 if (gfc_match (end_delim) == MATCH_YES)
1122 if (seen_ts)
1123 goto done;
1124 else
1126 gfc_error ("Empty array constructor at %C is not allowed");
1127 goto cleanup;
1131 for (;;)
1133 m = match_array_cons_element (&head);
1134 if (m == MATCH_ERROR)
1135 goto cleanup;
1136 if (m == MATCH_NO)
1137 goto syntax;
1139 if (gfc_match_char (',') == MATCH_NO)
1140 break;
1143 if (gfc_match (end_delim) == MATCH_NO)
1144 goto syntax;
1146 done:
1147 /* Size must be calculated at resolution time. */
1148 if (seen_ts)
1150 expr = gfc_get_array_expr (ts.type, ts.kind, &where);
1151 expr->ts = ts;
1153 else
1154 expr = gfc_get_array_expr (BT_UNKNOWN, 0, &where);
1156 expr->value.constructor = head;
1157 if (expr->ts.u.cl)
1158 expr->ts.u.cl->length_from_typespec = seen_ts;
1160 *result = expr;
1161 return MATCH_YES;
1163 syntax:
1164 gfc_error ("Syntax error in array constructor at %C");
1166 cleanup:
1167 gfc_constructor_free (head);
1168 return MATCH_ERROR;
1173 /************** Check array constructors for correctness **************/
1175 /* Given an expression, compare it's type with the type of the current
1176 constructor. Returns nonzero if an error was issued. The
1177 cons_state variable keeps track of whether the type of the
1178 constructor being read or resolved is known to be good, bad or just
1179 starting out. */
1181 static gfc_typespec constructor_ts;
1182 static enum
1183 { CONS_START, CONS_GOOD, CONS_BAD }
1184 cons_state;
1186 static int
1187 check_element_type (gfc_expr *expr, bool convert)
1189 if (cons_state == CONS_BAD)
1190 return 0; /* Suppress further errors */
1192 if (cons_state == CONS_START)
1194 if (expr->ts.type == BT_UNKNOWN)
1195 cons_state = CONS_BAD;
1196 else
1198 cons_state = CONS_GOOD;
1199 constructor_ts = expr->ts;
1202 return 0;
1205 if (gfc_compare_types (&constructor_ts, &expr->ts))
1206 return 0;
1208 if (convert)
1209 return gfc_convert_type(expr, &constructor_ts, 1) ? 0 : 1;
1211 gfc_error ("Element in %s array constructor at %L is %s",
1212 gfc_typename (&constructor_ts), &expr->where,
1213 gfc_typename (&expr->ts));
1215 cons_state = CONS_BAD;
1216 return 1;
1220 /* Recursive work function for gfc_check_constructor_type(). */
1222 static bool
1223 check_constructor_type (gfc_constructor_base base, bool convert)
1225 gfc_constructor *c;
1226 gfc_expr *e;
1228 for (c = gfc_constructor_first (base); c; c = gfc_constructor_next (c))
1230 e = c->expr;
1232 if (e->expr_type == EXPR_ARRAY)
1234 if (!check_constructor_type (e->value.constructor, convert))
1235 return false;
1237 continue;
1240 if (check_element_type (e, convert))
1241 return false;
1244 return true;
1248 /* Check that all elements of an array constructor are the same type.
1249 On false, an error has been generated. */
1251 bool
1252 gfc_check_constructor_type (gfc_expr *e)
1254 bool t;
1256 if (e->ts.type != BT_UNKNOWN)
1258 cons_state = CONS_GOOD;
1259 constructor_ts = e->ts;
1261 else
1263 cons_state = CONS_START;
1264 gfc_clear_ts (&constructor_ts);
1267 /* If e->ts.type != BT_UNKNOWN, the array constructor included a
1268 typespec, and we will now convert the values on the fly. */
1269 t = check_constructor_type (e->value.constructor, e->ts.type != BT_UNKNOWN);
1270 if (t && e->ts.type == BT_UNKNOWN)
1271 e->ts = constructor_ts;
1273 return t;
1278 typedef struct cons_stack
1280 gfc_iterator *iterator;
1281 struct cons_stack *previous;
1283 cons_stack;
1285 static cons_stack *base;
1287 static bool check_constructor (gfc_constructor_base, bool (*) (gfc_expr *));
1289 /* Check an EXPR_VARIABLE expression in a constructor to make sure
1290 that that variable is an iteration variables. */
1292 bool
1293 gfc_check_iter_variable (gfc_expr *expr)
1295 gfc_symbol *sym;
1296 cons_stack *c;
1298 sym = expr->symtree->n.sym;
1300 for (c = base; c && c->iterator; c = c->previous)
1301 if (sym == c->iterator->var->symtree->n.sym)
1302 return true;
1304 return false;
1308 /* Recursive work function for gfc_check_constructor(). This amounts
1309 to calling the check function for each expression in the
1310 constructor, giving variables with the names of iterators a pass. */
1312 static bool
1313 check_constructor (gfc_constructor_base ctor, bool (*check_function) (gfc_expr *))
1315 cons_stack element;
1316 gfc_expr *e;
1317 bool t;
1318 gfc_constructor *c;
1320 for (c = gfc_constructor_first (ctor); c; c = gfc_constructor_next (c))
1322 e = c->expr;
1324 if (!e)
1325 continue;
1327 if (e->expr_type != EXPR_ARRAY)
1329 if (!(*check_function)(e))
1330 return false;
1331 continue;
1334 element.previous = base;
1335 element.iterator = c->iterator;
1337 base = &element;
1338 t = check_constructor (e->value.constructor, check_function);
1339 base = element.previous;
1341 if (!t)
1342 return false;
1345 /* Nothing went wrong, so all OK. */
1346 return true;
1350 /* Checks a constructor to see if it is a particular kind of
1351 expression -- specification, restricted, or initialization as
1352 determined by the check_function. */
1354 bool
1355 gfc_check_constructor (gfc_expr *expr, bool (*check_function) (gfc_expr *))
1357 cons_stack *base_save;
1358 bool t;
1360 base_save = base;
1361 base = NULL;
1363 t = check_constructor (expr->value.constructor, check_function);
1364 base = base_save;
1366 return t;
1371 /**************** Simplification of array constructors ****************/
1373 iterator_stack *iter_stack;
1375 typedef struct
1377 gfc_constructor_base base;
1378 int extract_count, extract_n;
1379 gfc_expr *extracted;
1380 mpz_t *count;
1382 mpz_t *offset;
1383 gfc_component *component;
1384 mpz_t *repeat;
1386 bool (*expand_work_function) (gfc_expr *);
1388 expand_info;
1390 static expand_info current_expand;
1392 static bool expand_constructor (gfc_constructor_base);
1395 /* Work function that counts the number of elements present in a
1396 constructor. */
1398 static bool
1399 count_elements (gfc_expr *e)
1401 mpz_t result;
1403 if (e->rank == 0)
1404 mpz_add_ui (*current_expand.count, *current_expand.count, 1);
1405 else
1407 if (!gfc_array_size (e, &result))
1409 gfc_free_expr (e);
1410 return false;
1413 mpz_add (*current_expand.count, *current_expand.count, result);
1414 mpz_clear (result);
1417 gfc_free_expr (e);
1418 return true;
1422 /* Work function that extracts a particular element from an array
1423 constructor, freeing the rest. */
1425 static bool
1426 extract_element (gfc_expr *e)
1428 if (e->rank != 0)
1429 { /* Something unextractable */
1430 gfc_free_expr (e);
1431 return false;
1434 if (current_expand.extract_count == current_expand.extract_n)
1435 current_expand.extracted = e;
1436 else
1437 gfc_free_expr (e);
1439 current_expand.extract_count++;
1441 return true;
1445 /* Work function that constructs a new constructor out of the old one,
1446 stringing new elements together. */
1448 static bool
1449 expand (gfc_expr *e)
1451 gfc_constructor *c = gfc_constructor_append_expr (&current_expand.base,
1452 e, &e->where);
1454 c->n.component = current_expand.component;
1455 return true;
1459 /* Given an initialization expression that is a variable reference,
1460 substitute the current value of the iteration variable. */
1462 void
1463 gfc_simplify_iterator_var (gfc_expr *e)
1465 iterator_stack *p;
1467 for (p = iter_stack; p; p = p->prev)
1468 if (e->symtree == p->variable)
1469 break;
1471 if (p == NULL)
1472 return; /* Variable not found */
1474 gfc_replace_expr (e, gfc_get_int_expr (gfc_default_integer_kind, NULL, 0));
1476 mpz_set (e->value.integer, p->value);
1478 return;
1482 /* Expand an expression with that is inside of a constructor,
1483 recursing into other constructors if present. */
1485 static bool
1486 expand_expr (gfc_expr *e)
1488 if (e->expr_type == EXPR_ARRAY)
1489 return expand_constructor (e->value.constructor);
1491 e = gfc_copy_expr (e);
1493 if (!gfc_simplify_expr (e, 1))
1495 gfc_free_expr (e);
1496 return false;
1499 return current_expand.expand_work_function (e);
1503 static bool
1504 expand_iterator (gfc_constructor *c)
1506 gfc_expr *start, *end, *step;
1507 iterator_stack frame;
1508 mpz_t trip;
1509 bool t;
1511 end = step = NULL;
1513 t = false;
1515 mpz_init (trip);
1516 mpz_init (frame.value);
1517 frame.prev = NULL;
1519 start = gfc_copy_expr (c->iterator->start);
1520 if (!gfc_simplify_expr (start, 1))
1521 goto cleanup;
1523 if (start->expr_type != EXPR_CONSTANT || start->ts.type != BT_INTEGER)
1524 goto cleanup;
1526 end = gfc_copy_expr (c->iterator->end);
1527 if (!gfc_simplify_expr (end, 1))
1528 goto cleanup;
1530 if (end->expr_type != EXPR_CONSTANT || end->ts.type != BT_INTEGER)
1531 goto cleanup;
1533 step = gfc_copy_expr (c->iterator->step);
1534 if (!gfc_simplify_expr (step, 1))
1535 goto cleanup;
1537 if (step->expr_type != EXPR_CONSTANT || step->ts.type != BT_INTEGER)
1538 goto cleanup;
1540 if (mpz_sgn (step->value.integer) == 0)
1542 gfc_error ("Iterator step at %L cannot be zero", &step->where);
1543 goto cleanup;
1546 /* Calculate the trip count of the loop. */
1547 mpz_sub (trip, end->value.integer, start->value.integer);
1548 mpz_add (trip, trip, step->value.integer);
1549 mpz_tdiv_q (trip, trip, step->value.integer);
1551 mpz_set (frame.value, start->value.integer);
1553 frame.prev = iter_stack;
1554 frame.variable = c->iterator->var->symtree;
1555 iter_stack = &frame;
1557 while (mpz_sgn (trip) > 0)
1559 if (!expand_expr (c->expr))
1560 goto cleanup;
1562 mpz_add (frame.value, frame.value, step->value.integer);
1563 mpz_sub_ui (trip, trip, 1);
1566 t = true;
1568 cleanup:
1569 gfc_free_expr (start);
1570 gfc_free_expr (end);
1571 gfc_free_expr (step);
1573 mpz_clear (trip);
1574 mpz_clear (frame.value);
1576 iter_stack = frame.prev;
1578 return t;
1582 /* Expand a constructor into constant constructors without any
1583 iterators, calling the work function for each of the expanded
1584 expressions. The work function needs to either save or free the
1585 passed expression. */
1587 static bool
1588 expand_constructor (gfc_constructor_base base)
1590 gfc_constructor *c;
1591 gfc_expr *e;
1593 for (c = gfc_constructor_first (base); c; c = gfc_constructor_next(c))
1595 if (c->iterator != NULL)
1597 if (!expand_iterator (c))
1598 return false;
1599 continue;
1602 e = c->expr;
1604 if (e->expr_type == EXPR_ARRAY)
1606 if (!expand_constructor (e->value.constructor))
1607 return false;
1609 continue;
1612 e = gfc_copy_expr (e);
1613 if (!gfc_simplify_expr (e, 1))
1615 gfc_free_expr (e);
1616 return false;
1618 current_expand.offset = &c->offset;
1619 current_expand.repeat = &c->repeat;
1620 current_expand.component = c->n.component;
1621 if (!current_expand.expand_work_function(e))
1622 return false;
1624 return true;
1628 /* Given an array expression and an element number (starting at zero),
1629 return a pointer to the array element. NULL is returned if the
1630 size of the array has been exceeded. The expression node returned
1631 remains a part of the array and should not be freed. Access is not
1632 efficient at all, but this is another place where things do not
1633 have to be particularly fast. */
1635 static gfc_expr *
1636 gfc_get_array_element (gfc_expr *array, int element)
1638 expand_info expand_save;
1639 gfc_expr *e;
1640 bool rc;
1642 expand_save = current_expand;
1643 current_expand.extract_n = element;
1644 current_expand.expand_work_function = extract_element;
1645 current_expand.extracted = NULL;
1646 current_expand.extract_count = 0;
1648 iter_stack = NULL;
1650 rc = expand_constructor (array->value.constructor);
1651 e = current_expand.extracted;
1652 current_expand = expand_save;
1654 if (!rc)
1655 return NULL;
1657 return e;
1661 /* Top level subroutine for expanding constructors. We only expand
1662 constructor if they are small enough. */
1664 bool
1665 gfc_expand_constructor (gfc_expr *e, bool fatal)
1667 expand_info expand_save;
1668 gfc_expr *f;
1669 bool rc;
1671 /* If we can successfully get an array element at the max array size then
1672 the array is too big to expand, so we just return. */
1673 f = gfc_get_array_element (e, flag_max_array_constructor);
1674 if (f != NULL)
1676 gfc_free_expr (f);
1677 if (fatal)
1679 gfc_error ("The number of elements in the array constructor "
1680 "at %L requires an increase of the allowed %d "
1681 "upper limit. See %<-fmax-array-constructor%> "
1682 "option", &e->where, flag_max_array_constructor);
1683 return false;
1685 return true;
1688 /* We now know the array is not too big so go ahead and try to expand it. */
1689 expand_save = current_expand;
1690 current_expand.base = NULL;
1692 iter_stack = NULL;
1694 current_expand.expand_work_function = expand;
1696 if (!expand_constructor (e->value.constructor))
1698 gfc_constructor_free (current_expand.base);
1699 rc = false;
1700 goto done;
1703 gfc_constructor_free (e->value.constructor);
1704 e->value.constructor = current_expand.base;
1706 rc = true;
1708 done:
1709 current_expand = expand_save;
1711 return rc;
1715 /* Work function for checking that an element of a constructor is a
1716 constant, after removal of any iteration variables. We return
1717 false if not so. */
1719 static bool
1720 is_constant_element (gfc_expr *e)
1722 int rv;
1724 rv = gfc_is_constant_expr (e);
1725 gfc_free_expr (e);
1727 return rv ? true : false;
1731 /* Given an array constructor, determine if the constructor is
1732 constant or not by expanding it and making sure that all elements
1733 are constants. This is a bit of a hack since something like (/ (i,
1734 i=1,100000000) /) will take a while as* opposed to a more clever
1735 function that traverses the expression tree. FIXME. */
1738 gfc_constant_ac (gfc_expr *e)
1740 expand_info expand_save;
1741 bool rc;
1743 iter_stack = NULL;
1744 expand_save = current_expand;
1745 current_expand.expand_work_function = is_constant_element;
1747 rc = expand_constructor (e->value.constructor);
1749 current_expand = expand_save;
1750 if (!rc)
1751 return 0;
1753 return 1;
1757 /* Returns nonzero if an array constructor has been completely
1758 expanded (no iterators) and zero if iterators are present. */
1761 gfc_expanded_ac (gfc_expr *e)
1763 gfc_constructor *c;
1765 if (e->expr_type == EXPR_ARRAY)
1766 for (c = gfc_constructor_first (e->value.constructor);
1767 c; c = gfc_constructor_next (c))
1768 if (c->iterator != NULL || !gfc_expanded_ac (c->expr))
1769 return 0;
1771 return 1;
1775 /*************** Type resolution of array constructors ***************/
1778 /* The symbol expr_is_sought_symbol_ref will try to find. */
1779 static const gfc_symbol *sought_symbol = NULL;
1782 /* Tells whether the expression E is a variable reference to the symbol
1783 in the static variable SOUGHT_SYMBOL, and sets the locus pointer WHERE
1784 accordingly.
1785 To be used with gfc_expr_walker: if a reference is found we don't need
1786 to look further so we return 1 to skip any further walk. */
1788 static int
1789 expr_is_sought_symbol_ref (gfc_expr **e, int *walk_subtrees ATTRIBUTE_UNUSED,
1790 void *where)
1792 gfc_expr *expr = *e;
1793 locus *sym_loc = (locus *)where;
1795 if (expr->expr_type == EXPR_VARIABLE
1796 && expr->symtree->n.sym == sought_symbol)
1798 *sym_loc = expr->where;
1799 return 1;
1802 return 0;
1806 /* Tells whether the expression EXPR contains a reference to the symbol
1807 SYM and in that case sets the position SYM_LOC where the reference is. */
1809 static bool
1810 find_symbol_in_expr (gfc_symbol *sym, gfc_expr *expr, locus *sym_loc)
1812 int ret;
1814 sought_symbol = sym;
1815 ret = gfc_expr_walker (&expr, &expr_is_sought_symbol_ref, sym_loc);
1816 sought_symbol = NULL;
1817 return ret;
1821 /* Recursive array list resolution function. All of the elements must
1822 be of the same type. */
1824 static bool
1825 resolve_array_list (gfc_constructor_base base)
1827 bool t;
1828 gfc_constructor *c;
1829 gfc_iterator *iter;
1831 t = true;
1833 for (c = gfc_constructor_first (base); c; c = gfc_constructor_next (c))
1835 iter = c->iterator;
1836 if (iter != NULL)
1838 gfc_symbol *iter_var;
1839 locus iter_var_loc;
1841 if (!gfc_resolve_iterator (iter, false, true))
1842 t = false;
1844 /* Check for bounds referencing the iterator variable. */
1845 gcc_assert (iter->var->expr_type == EXPR_VARIABLE);
1846 iter_var = iter->var->symtree->n.sym;
1847 if (find_symbol_in_expr (iter_var, iter->start, &iter_var_loc))
1849 if (!gfc_notify_std (GFC_STD_LEGACY, "AC-IMPLIED-DO initial "
1850 "expression references control variable "
1851 "at %L", &iter_var_loc))
1852 t = false;
1854 if (find_symbol_in_expr (iter_var, iter->end, &iter_var_loc))
1856 if (!gfc_notify_std (GFC_STD_LEGACY, "AC-IMPLIED-DO final "
1857 "expression references control variable "
1858 "at %L", &iter_var_loc))
1859 t = false;
1861 if (find_symbol_in_expr (iter_var, iter->step, &iter_var_loc))
1863 if (!gfc_notify_std (GFC_STD_LEGACY, "AC-IMPLIED-DO step "
1864 "expression references control variable "
1865 "at %L", &iter_var_loc))
1866 t = false;
1870 if (!gfc_resolve_expr (c->expr))
1871 t = false;
1873 if (UNLIMITED_POLY (c->expr))
1875 gfc_error ("Array constructor value at %L shall not be unlimited "
1876 "polymorphic [F2008: C4106]", &c->expr->where);
1877 t = false;
1881 return t;
1884 /* Resolve character array constructor. If it has a specified constant character
1885 length, pad/truncate the elements here; if the length is not specified and
1886 all elements are of compile-time known length, emit an error as this is
1887 invalid. */
1889 bool
1890 gfc_resolve_character_array_constructor (gfc_expr *expr)
1892 gfc_constructor *p;
1893 int found_length;
1895 gcc_assert (expr->expr_type == EXPR_ARRAY);
1896 gcc_assert (expr->ts.type == BT_CHARACTER);
1898 if (expr->ts.u.cl == NULL)
1900 for (p = gfc_constructor_first (expr->value.constructor);
1901 p; p = gfc_constructor_next (p))
1902 if (p->expr->ts.u.cl != NULL)
1904 /* Ensure that if there is a char_len around that it is
1905 used; otherwise the middle-end confuses them! */
1906 expr->ts.u.cl = p->expr->ts.u.cl;
1907 goto got_charlen;
1910 expr->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
1913 got_charlen:
1915 found_length = -1;
1917 if (expr->ts.u.cl->length == NULL)
1919 /* Check that all constant string elements have the same length until
1920 we reach the end or find a variable-length one. */
1922 for (p = gfc_constructor_first (expr->value.constructor);
1923 p; p = gfc_constructor_next (p))
1925 int current_length = -1;
1926 gfc_ref *ref;
1927 for (ref = p->expr->ref; ref; ref = ref->next)
1928 if (ref->type == REF_SUBSTRING
1929 && ref->u.ss.start->expr_type == EXPR_CONSTANT
1930 && ref->u.ss.end->expr_type == EXPR_CONSTANT)
1931 break;
1933 if (p->expr->expr_type == EXPR_CONSTANT)
1934 current_length = p->expr->value.character.length;
1935 else if (ref)
1937 long j;
1938 j = mpz_get_ui (ref->u.ss.end->value.integer)
1939 - mpz_get_ui (ref->u.ss.start->value.integer) + 1;
1940 current_length = (int) j;
1942 else if (p->expr->ts.u.cl && p->expr->ts.u.cl->length
1943 && p->expr->ts.u.cl->length->expr_type == EXPR_CONSTANT)
1945 long j;
1946 j = mpz_get_si (p->expr->ts.u.cl->length->value.integer);
1947 current_length = (int) j;
1949 else
1950 return true;
1952 gcc_assert (current_length != -1);
1954 if (found_length == -1)
1955 found_length = current_length;
1956 else if (found_length != current_length)
1958 gfc_error ("Different CHARACTER lengths (%d/%d) in array"
1959 " constructor at %L", found_length, current_length,
1960 &p->expr->where);
1961 return false;
1964 gcc_assert (found_length == current_length);
1967 gcc_assert (found_length != -1);
1969 /* Update the character length of the array constructor. */
1970 expr->ts.u.cl->length = gfc_get_int_expr (gfc_default_integer_kind,
1971 NULL, found_length);
1973 else
1975 /* We've got a character length specified. It should be an integer,
1976 otherwise an error is signalled elsewhere. */
1977 gcc_assert (expr->ts.u.cl->length);
1979 /* If we've got a constant character length, pad according to this.
1980 gfc_extract_int does check for BT_INTEGER and EXPR_CONSTANT and sets
1981 max_length only if they pass. */
1982 gfc_extract_int (expr->ts.u.cl->length, &found_length);
1984 /* Now pad/truncate the elements accordingly to the specified character
1985 length. This is ok inside this conditional, as in the case above
1986 (without typespec) all elements are verified to have the same length
1987 anyway. */
1988 if (found_length != -1)
1989 for (p = gfc_constructor_first (expr->value.constructor);
1990 p; p = gfc_constructor_next (p))
1991 if (p->expr->expr_type == EXPR_CONSTANT)
1993 gfc_expr *cl = NULL;
1994 int current_length = -1;
1995 bool has_ts;
1997 if (p->expr->ts.u.cl && p->expr->ts.u.cl->length)
1999 cl = p->expr->ts.u.cl->length;
2000 gfc_extract_int (cl, &current_length);
2003 /* If gfc_extract_int above set current_length, we implicitly
2004 know the type is BT_INTEGER and it's EXPR_CONSTANT. */
2006 has_ts = expr->ts.u.cl->length_from_typespec;
2008 if (! cl
2009 || (current_length != -1 && current_length != found_length))
2010 gfc_set_constant_character_len (found_length, p->expr,
2011 has_ts ? -1 : found_length);
2015 return true;
2019 /* Resolve all of the expressions in an array list. */
2021 bool
2022 gfc_resolve_array_constructor (gfc_expr *expr)
2024 bool t;
2026 t = resolve_array_list (expr->value.constructor);
2027 if (t)
2028 t = gfc_check_constructor_type (expr);
2030 /* gfc_resolve_character_array_constructor is called in gfc_resolve_expr after
2031 the call to this function, so we don't need to call it here; if it was
2032 called twice, an error message there would be duplicated. */
2034 return t;
2038 /* Copy an iterator structure. */
2040 gfc_iterator *
2041 gfc_copy_iterator (gfc_iterator *src)
2043 gfc_iterator *dest;
2045 if (src == NULL)
2046 return NULL;
2048 dest = gfc_get_iterator ();
2050 dest->var = gfc_copy_expr (src->var);
2051 dest->start = gfc_copy_expr (src->start);
2052 dest->end = gfc_copy_expr (src->end);
2053 dest->step = gfc_copy_expr (src->step);
2055 return dest;
2059 /********* Subroutines for determining the size of an array *********/
2061 /* These are needed just to accommodate RESHAPE(). There are no
2062 diagnostics here, we just return a negative number if something
2063 goes wrong. */
2066 /* Get the size of single dimension of an array specification. The
2067 array is guaranteed to be one dimensional. */
2069 bool
2070 spec_dimen_size (gfc_array_spec *as, int dimen, mpz_t *result)
2072 if (as == NULL)
2073 return false;
2075 if (dimen < 0 || dimen > as->rank - 1)
2076 gfc_internal_error ("spec_dimen_size(): Bad dimension");
2078 if (as->type != AS_EXPLICIT
2079 || as->lower[dimen]->expr_type != EXPR_CONSTANT
2080 || as->upper[dimen]->expr_type != EXPR_CONSTANT
2081 || as->lower[dimen]->ts.type != BT_INTEGER
2082 || as->upper[dimen]->ts.type != BT_INTEGER)
2083 return false;
2085 mpz_init (*result);
2087 mpz_sub (*result, as->upper[dimen]->value.integer,
2088 as->lower[dimen]->value.integer);
2090 mpz_add_ui (*result, *result, 1);
2092 return true;
2096 bool
2097 spec_size (gfc_array_spec *as, mpz_t *result)
2099 mpz_t size;
2100 int d;
2102 if (!as || as->type == AS_ASSUMED_RANK)
2103 return false;
2105 mpz_init_set_ui (*result, 1);
2107 for (d = 0; d < as->rank; d++)
2109 if (!spec_dimen_size (as, d, &size))
2111 mpz_clear (*result);
2112 return false;
2115 mpz_mul (*result, *result, size);
2116 mpz_clear (size);
2119 return true;
2123 /* Get the number of elements in an array section. Optionally, also supply
2124 the end value. */
2126 bool
2127 gfc_ref_dimen_size (gfc_array_ref *ar, int dimen, mpz_t *result, mpz_t *end)
2129 mpz_t upper, lower, stride;
2130 mpz_t diff;
2131 bool t;
2133 if (dimen < 0 || ar == NULL || dimen > ar->dimen - 1)
2134 gfc_internal_error ("gfc_ref_dimen_size(): Bad dimension");
2136 switch (ar->dimen_type[dimen])
2138 case DIMEN_ELEMENT:
2139 mpz_init (*result);
2140 mpz_set_ui (*result, 1);
2141 t = true;
2142 break;
2144 case DIMEN_VECTOR:
2145 t = gfc_array_size (ar->start[dimen], result); /* Recurse! */
2146 break;
2148 case DIMEN_RANGE:
2150 mpz_init (stride);
2152 if (ar->stride[dimen] == NULL)
2153 mpz_set_ui (stride, 1);
2154 else
2156 if (ar->stride[dimen]->expr_type != EXPR_CONSTANT)
2158 mpz_clear (stride);
2159 return false;
2161 mpz_set (stride, ar->stride[dimen]->value.integer);
2164 /* Calculate the number of elements via gfc_dep_differce, but only if
2165 start and end are both supplied in the reference or the array spec.
2166 This is to guard against strange but valid code like
2168 subroutine foo(a,n)
2169 real a(1:n)
2170 n = 3
2171 print *,size(a(n-1:))
2173 where the user changes the value of a variable. If we have to
2174 determine end as well, we cannot do this using gfc_dep_difference.
2175 Fall back to the constants-only code then. */
2177 if (end == NULL)
2179 bool use_dep;
2181 use_dep = gfc_dep_difference (ar->end[dimen], ar->start[dimen],
2182 &diff);
2183 if (!use_dep && ar->end[dimen] == NULL && ar->start[dimen] == NULL)
2184 use_dep = gfc_dep_difference (ar->as->upper[dimen],
2185 ar->as->lower[dimen], &diff);
2187 if (use_dep)
2189 mpz_init (*result);
2190 mpz_add (*result, diff, stride);
2191 mpz_div (*result, *result, stride);
2192 if (mpz_cmp_ui (*result, 0) < 0)
2193 mpz_set_ui (*result, 0);
2195 mpz_clear (stride);
2196 mpz_clear (diff);
2197 return true;
2202 /* Constant-only code here, which covers more cases
2203 like a(:4) etc. */
2204 mpz_init (upper);
2205 mpz_init (lower);
2206 t = false;
2208 if (ar->start[dimen] == NULL)
2210 if (ar->as->lower[dimen] == NULL
2211 || ar->as->lower[dimen]->expr_type != EXPR_CONSTANT)
2212 goto cleanup;
2213 mpz_set (lower, ar->as->lower[dimen]->value.integer);
2215 else
2217 if (ar->start[dimen]->expr_type != EXPR_CONSTANT)
2218 goto cleanup;
2219 mpz_set (lower, ar->start[dimen]->value.integer);
2222 if (ar->end[dimen] == NULL)
2224 if (ar->as->upper[dimen] == NULL
2225 || ar->as->upper[dimen]->expr_type != EXPR_CONSTANT)
2226 goto cleanup;
2227 mpz_set (upper, ar->as->upper[dimen]->value.integer);
2229 else
2231 if (ar->end[dimen]->expr_type != EXPR_CONSTANT)
2232 goto cleanup;
2233 mpz_set (upper, ar->end[dimen]->value.integer);
2236 mpz_init (*result);
2237 mpz_sub (*result, upper, lower);
2238 mpz_add (*result, *result, stride);
2239 mpz_div (*result, *result, stride);
2241 /* Zero stride caught earlier. */
2242 if (mpz_cmp_ui (*result, 0) < 0)
2243 mpz_set_ui (*result, 0);
2244 t = true;
2246 if (end)
2248 mpz_init (*end);
2250 mpz_sub_ui (*end, *result, 1UL);
2251 mpz_mul (*end, *end, stride);
2252 mpz_add (*end, *end, lower);
2255 cleanup:
2256 mpz_clear (upper);
2257 mpz_clear (lower);
2258 mpz_clear (stride);
2259 return t;
2261 default:
2262 gfc_internal_error ("gfc_ref_dimen_size(): Bad dimen_type");
2265 return t;
2269 static bool
2270 ref_size (gfc_array_ref *ar, mpz_t *result)
2272 mpz_t size;
2273 int d;
2275 mpz_init_set_ui (*result, 1);
2277 for (d = 0; d < ar->dimen; d++)
2279 if (!gfc_ref_dimen_size (ar, d, &size, NULL))
2281 mpz_clear (*result);
2282 return false;
2285 mpz_mul (*result, *result, size);
2286 mpz_clear (size);
2289 return true;
2293 /* Given an array expression and a dimension, figure out how many
2294 elements it has along that dimension. Returns true if we were
2295 able to return a result in the 'result' variable, false
2296 otherwise. */
2298 bool
2299 gfc_array_dimen_size (gfc_expr *array, int dimen, mpz_t *result)
2301 gfc_ref *ref;
2302 int i;
2304 gcc_assert (array != NULL);
2306 if (array->ts.type == BT_CLASS)
2307 return false;
2309 if (array->rank == -1)
2310 return false;
2312 if (dimen < 0 || dimen > array->rank - 1)
2313 gfc_internal_error ("gfc_array_dimen_size(): Bad dimension");
2315 switch (array->expr_type)
2317 case EXPR_VARIABLE:
2318 case EXPR_FUNCTION:
2319 for (ref = array->ref; ref; ref = ref->next)
2321 if (ref->type != REF_ARRAY)
2322 continue;
2324 if (ref->u.ar.type == AR_FULL)
2325 return spec_dimen_size (ref->u.ar.as, dimen, result);
2327 if (ref->u.ar.type == AR_SECTION)
2329 for (i = 0; dimen >= 0; i++)
2330 if (ref->u.ar.dimen_type[i] != DIMEN_ELEMENT)
2331 dimen--;
2333 return gfc_ref_dimen_size (&ref->u.ar, i - 1, result, NULL);
2337 if (array->shape && array->shape[dimen])
2339 mpz_init_set (*result, array->shape[dimen]);
2340 return true;
2343 if (array->symtree->n.sym->attr.generic
2344 && array->value.function.esym != NULL)
2346 if (!spec_dimen_size (array->value.function.esym->as, dimen, result))
2347 return false;
2349 else if (!spec_dimen_size (array->symtree->n.sym->as, dimen, result))
2350 return false;
2352 break;
2354 case EXPR_ARRAY:
2355 if (array->shape == NULL) {
2356 /* Expressions with rank > 1 should have "shape" properly set */
2357 if ( array->rank != 1 )
2358 gfc_internal_error ("gfc_array_dimen_size(): Bad EXPR_ARRAY expr");
2359 return gfc_array_size(array, result);
2362 /* Fall through */
2363 default:
2364 if (array->shape == NULL)
2365 return false;
2367 mpz_init_set (*result, array->shape[dimen]);
2369 break;
2372 return true;
2376 /* Given an array expression, figure out how many elements are in the
2377 array. Returns true if this is possible, and sets the 'result'
2378 variable. Otherwise returns false. */
2380 bool
2381 gfc_array_size (gfc_expr *array, mpz_t *result)
2383 expand_info expand_save;
2384 gfc_ref *ref;
2385 int i;
2386 bool t;
2388 if (array->ts.type == BT_CLASS)
2389 return false;
2391 switch (array->expr_type)
2393 case EXPR_ARRAY:
2394 gfc_push_suppress_errors ();
2396 expand_save = current_expand;
2398 current_expand.count = result;
2399 mpz_init_set_ui (*result, 0);
2401 current_expand.expand_work_function = count_elements;
2402 iter_stack = NULL;
2404 t = expand_constructor (array->value.constructor);
2406 gfc_pop_suppress_errors ();
2408 if (!t)
2409 mpz_clear (*result);
2410 current_expand = expand_save;
2411 return t;
2413 case EXPR_VARIABLE:
2414 for (ref = array->ref; ref; ref = ref->next)
2416 if (ref->type != REF_ARRAY)
2417 continue;
2419 if (ref->u.ar.type == AR_FULL)
2420 return spec_size (ref->u.ar.as, result);
2422 if (ref->u.ar.type == AR_SECTION)
2423 return ref_size (&ref->u.ar, result);
2426 return spec_size (array->symtree->n.sym->as, result);
2429 default:
2430 if (array->rank == 0 || array->shape == NULL)
2431 return false;
2433 mpz_init_set_ui (*result, 1);
2435 for (i = 0; i < array->rank; i++)
2436 mpz_mul (*result, *result, array->shape[i]);
2438 break;
2441 return true;
2445 /* Given an array reference, return the shape of the reference in an
2446 array of mpz_t integers. */
2448 bool
2449 gfc_array_ref_shape (gfc_array_ref *ar, mpz_t *shape)
2451 int d;
2452 int i;
2454 d = 0;
2456 switch (ar->type)
2458 case AR_FULL:
2459 for (; d < ar->as->rank; d++)
2460 if (!spec_dimen_size (ar->as, d, &shape[d]))
2461 goto cleanup;
2463 return true;
2465 case AR_SECTION:
2466 for (i = 0; i < ar->dimen; i++)
2468 if (ar->dimen_type[i] != DIMEN_ELEMENT)
2470 if (!gfc_ref_dimen_size (ar, i, &shape[d], NULL))
2471 goto cleanup;
2472 d++;
2476 return true;
2478 default:
2479 break;
2482 cleanup:
2483 gfc_clear_shape (shape, d);
2484 return false;
2488 /* Given an array expression, find the array reference structure that
2489 characterizes the reference. */
2491 gfc_array_ref *
2492 gfc_find_array_ref (gfc_expr *e)
2494 gfc_ref *ref;
2496 for (ref = e->ref; ref; ref = ref->next)
2497 if (ref->type == REF_ARRAY
2498 && (ref->u.ar.type == AR_FULL || ref->u.ar.type == AR_SECTION))
2499 break;
2501 if (ref == NULL)
2502 gfc_internal_error ("gfc_find_array_ref(): No ref found");
2504 return &ref->u.ar;
2508 /* Find out if an array shape is known at compile time. */
2511 gfc_is_compile_time_shape (gfc_array_spec *as)
2513 int i;
2515 if (as->type != AS_EXPLICIT)
2516 return 0;
2518 for (i = 0; i < as->rank; i++)
2519 if (!gfc_is_constant_expr (as->lower[i])
2520 || !gfc_is_constant_expr (as->upper[i]))
2521 return 0;
2523 return 1;