Daily bump.
[official-gcc.git] / gcc / fortran / array.c
blobc2ac1ece1e652010ff9675242784f5d07e898537
1 /* Array things
2 Copyright (C) 2000-2013 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 "gfortran.h"
25 #include "match.h"
26 #include "constructor.h"
28 /**************** Array reference matching subroutines *****************/
30 /* Copy an array reference structure. */
32 gfc_array_ref *
33 gfc_copy_array_ref (gfc_array_ref *src)
35 gfc_array_ref *dest;
36 int i;
38 if (src == NULL)
39 return NULL;
41 dest = gfc_get_array_ref ();
43 *dest = *src;
45 for (i = 0; i < GFC_MAX_DIMENSIONS; i++)
47 dest->start[i] = gfc_copy_expr (src->start[i]);
48 dest->end[i] = gfc_copy_expr (src->end[i]);
49 dest->stride[i] = gfc_copy_expr (src->stride[i]);
52 return dest;
56 /* Match a single dimension of an array reference. This can be a
57 single element or an array section. Any modifications we've made
58 to the ar structure are cleaned up by the caller. If the init
59 is set, we require the subscript to be a valid initialization
60 expression. */
62 static match
63 match_subscript (gfc_array_ref *ar, int init, bool match_star)
65 match m = MATCH_ERROR;
66 bool star = false;
67 int i;
69 i = ar->dimen + ar->codimen;
71 gfc_gobble_whitespace ();
72 ar->c_where[i] = gfc_current_locus;
73 ar->start[i] = ar->end[i] = ar->stride[i] = NULL;
75 /* We can't be sure of the difference between DIMEN_ELEMENT and
76 DIMEN_VECTOR until we know the type of the element itself at
77 resolution time. */
79 ar->dimen_type[i] = DIMEN_UNKNOWN;
81 if (gfc_match_char (':') == MATCH_YES)
82 goto end_element;
84 /* Get start element. */
85 if (match_star && (m = gfc_match_char ('*')) == MATCH_YES)
86 star = true;
88 if (!star && init)
89 m = gfc_match_init_expr (&ar->start[i]);
90 else if (!star)
91 m = gfc_match_expr (&ar->start[i]);
93 if (m == MATCH_NO)
94 gfc_error ("Expected array subscript at %C");
95 if (m != MATCH_YES)
96 return MATCH_ERROR;
98 if (gfc_match_char (':') == MATCH_NO)
99 goto matched;
101 if (star)
103 gfc_error ("Unexpected '*' in coarray subscript at %C");
104 return MATCH_ERROR;
107 /* Get an optional end element. Because we've seen the colon, we
108 definitely have a range along this dimension. */
109 end_element:
110 ar->dimen_type[i] = DIMEN_RANGE;
112 if (match_star && (m = gfc_match_char ('*')) == MATCH_YES)
113 star = true;
114 else if (init)
115 m = gfc_match_init_expr (&ar->end[i]);
116 else
117 m = gfc_match_expr (&ar->end[i]);
119 if (m == MATCH_ERROR)
120 return MATCH_ERROR;
122 /* See if we have an optional stride. */
123 if (gfc_match_char (':') == MATCH_YES)
125 if (star)
127 gfc_error ("Strides not allowed in coarray subscript at %C");
128 return MATCH_ERROR;
131 m = init ? gfc_match_init_expr (&ar->stride[i])
132 : gfc_match_expr (&ar->stride[i]);
134 if (m == MATCH_NO)
135 gfc_error ("Expected array subscript stride at %C");
136 if (m != MATCH_YES)
137 return MATCH_ERROR;
140 matched:
141 if (star)
142 ar->dimen_type[i] = DIMEN_STAR;
144 return MATCH_YES;
148 /* Match an array reference, whether it is the whole array or a
149 particular elements or a section. If init is set, the reference has
150 to consist of init expressions. */
152 match
153 gfc_match_array_ref (gfc_array_ref *ar, gfc_array_spec *as, int init,
154 int corank)
156 match m;
157 bool matched_bracket = false;
159 memset (ar, '\0', sizeof (*ar));
161 ar->where = gfc_current_locus;
162 ar->as = as;
163 ar->type = AR_UNKNOWN;
165 if (gfc_match_char ('[') == MATCH_YES)
167 matched_bracket = true;
168 goto coarray;
171 if (gfc_match_char ('(') != MATCH_YES)
173 ar->type = AR_FULL;
174 ar->dimen = 0;
175 return MATCH_YES;
178 for (ar->dimen = 0; ar->dimen < GFC_MAX_DIMENSIONS; ar->dimen++)
180 m = match_subscript (ar, init, false);
181 if (m == MATCH_ERROR)
182 return MATCH_ERROR;
184 if (gfc_match_char (')') == MATCH_YES)
186 ar->dimen++;
187 goto coarray;
190 if (gfc_match_char (',') != MATCH_YES)
192 gfc_error ("Invalid form of array reference at %C");
193 return MATCH_ERROR;
197 gfc_error ("Array reference at %C cannot have more than %d dimensions",
198 GFC_MAX_DIMENSIONS);
199 return MATCH_ERROR;
201 coarray:
202 if (!matched_bracket && gfc_match_char ('[') != MATCH_YES)
204 if (ar->dimen > 0)
205 return MATCH_YES;
206 else
207 return MATCH_ERROR;
210 if (gfc_option.coarray == GFC_FCOARRAY_NONE)
212 gfc_fatal_error ("Coarrays disabled at %C, use -fcoarray= to enable");
213 return MATCH_ERROR;
216 if (corank == 0)
218 gfc_error ("Unexpected coarray designator at %C");
219 return MATCH_ERROR;
222 for (ar->codimen = 0; ar->codimen + ar->dimen < GFC_MAX_DIMENSIONS; ar->codimen++)
224 m = match_subscript (ar, init, true);
225 if (m == MATCH_ERROR)
226 return MATCH_ERROR;
228 if (gfc_match_char (']') == MATCH_YES)
230 ar->codimen++;
231 if (ar->codimen < corank)
233 gfc_error ("Too few codimensions at %C, expected %d not %d",
234 corank, ar->codimen);
235 return MATCH_ERROR;
237 if (ar->codimen > corank)
239 gfc_error ("Too many codimensions at %C, expected %d not %d",
240 corank, ar->codimen);
241 return MATCH_ERROR;
243 return MATCH_YES;
246 if (gfc_match_char (',') != MATCH_YES)
248 if (gfc_match_char ('*') == MATCH_YES)
249 gfc_error ("Unexpected '*' for codimension %d of %d at %C",
250 ar->codimen + 1, corank);
251 else
252 gfc_error ("Invalid form of coarray reference at %C");
253 return MATCH_ERROR;
255 else if (ar->dimen_type[ar->codimen + ar->dimen] == DIMEN_STAR)
257 gfc_error ("Unexpected '*' for codimension %d of %d at %C",
258 ar->codimen + 1, corank);
259 return MATCH_ERROR;
262 if (ar->codimen >= corank)
264 gfc_error ("Invalid codimension %d at %C, only %d codimensions exist",
265 ar->codimen + 1, corank);
266 return MATCH_ERROR;
270 gfc_error ("Array reference at %C cannot have more than %d dimensions",
271 GFC_MAX_DIMENSIONS);
272 return MATCH_ERROR;
277 /************** Array specification matching subroutines ***************/
279 /* Free all of the expressions associated with array bounds
280 specifications. */
282 void
283 gfc_free_array_spec (gfc_array_spec *as)
285 int i;
287 if (as == NULL)
288 return;
290 for (i = 0; i < as->rank + as->corank; i++)
292 gfc_free_expr (as->lower[i]);
293 gfc_free_expr (as->upper[i]);
296 free (as);
300 /* Take an array bound, resolves the expression, that make up the
301 shape and check associated constraints. */
303 static bool
304 resolve_array_bound (gfc_expr *e, int check_constant)
306 if (e == NULL)
307 return true;
309 if (!gfc_resolve_expr (e)
310 || !gfc_specification_expr (e))
311 return false;
313 if (check_constant && !gfc_is_constant_expr (e))
315 if (e->expr_type == EXPR_VARIABLE)
316 gfc_error ("Variable '%s' at %L in this context must be constant",
317 e->symtree->n.sym->name, &e->where);
318 else
319 gfc_error ("Expression at %L in this context must be constant",
320 &e->where);
321 return false;
324 return true;
328 /* Takes an array specification, resolves the expressions that make up
329 the shape and make sure everything is integral. */
331 bool
332 gfc_resolve_array_spec (gfc_array_spec *as, int check_constant)
334 gfc_expr *e;
335 int i;
337 if (as == NULL)
338 return true;
340 for (i = 0; i < as->rank + as->corank; i++)
342 e = as->lower[i];
343 if (!resolve_array_bound (e, check_constant))
344 return false;
346 e = as->upper[i];
347 if (!resolve_array_bound (e, check_constant))
348 return false;
350 if ((as->lower[i] == NULL) || (as->upper[i] == NULL))
351 continue;
353 /* If the size is negative in this dimension, set it to zero. */
354 if (as->lower[i]->expr_type == EXPR_CONSTANT
355 && as->upper[i]->expr_type == EXPR_CONSTANT
356 && mpz_cmp (as->upper[i]->value.integer,
357 as->lower[i]->value.integer) < 0)
359 gfc_free_expr (as->upper[i]);
360 as->upper[i] = gfc_copy_expr (as->lower[i]);
361 mpz_sub_ui (as->upper[i]->value.integer,
362 as->upper[i]->value.integer, 1);
366 return true;
370 /* Match a single array element specification. The return values as
371 well as the upper and lower bounds of the array spec are filled
372 in according to what we see on the input. The caller makes sure
373 individual specifications make sense as a whole.
376 Parsed Lower Upper Returned
377 ------------------------------------
378 : NULL NULL AS_DEFERRED (*)
379 x 1 x AS_EXPLICIT
380 x: x NULL AS_ASSUMED_SHAPE
381 x:y x y AS_EXPLICIT
382 x:* x NULL AS_ASSUMED_SIZE
383 * 1 NULL AS_ASSUMED_SIZE
385 (*) For non-pointer dummy arrays this is AS_ASSUMED_SHAPE. This
386 is fixed during the resolution of formal interfaces.
388 Anything else AS_UNKNOWN. */
390 static array_type
391 match_array_element_spec (gfc_array_spec *as)
393 gfc_expr **upper, **lower;
394 match m;
395 int rank;
397 rank = as->rank == -1 ? 0 : as->rank;
398 lower = &as->lower[rank + as->corank - 1];
399 upper = &as->upper[rank + as->corank - 1];
401 if (gfc_match_char ('*') == MATCH_YES)
403 *lower = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
404 return AS_ASSUMED_SIZE;
407 if (gfc_match_char (':') == MATCH_YES)
408 return AS_DEFERRED;
410 m = gfc_match_expr (upper);
411 if (m == MATCH_NO)
412 gfc_error ("Expected expression in array specification at %C");
413 if (m != MATCH_YES)
414 return AS_UNKNOWN;
415 if (!gfc_expr_check_typed (*upper, gfc_current_ns, false))
416 return AS_UNKNOWN;
418 if (gfc_match_char (':') == MATCH_NO)
420 *lower = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
421 return AS_EXPLICIT;
424 *lower = *upper;
425 *upper = NULL;
427 if (gfc_match_char ('*') == MATCH_YES)
428 return AS_ASSUMED_SIZE;
430 m = gfc_match_expr (upper);
431 if (m == MATCH_ERROR)
432 return AS_UNKNOWN;
433 if (m == MATCH_NO)
434 return AS_ASSUMED_SHAPE;
435 if (!gfc_expr_check_typed (*upper, gfc_current_ns, false))
436 return AS_UNKNOWN;
438 return AS_EXPLICIT;
442 /* Matches an array specification, incidentally figuring out what sort
443 it is. Match either a normal array specification, or a coarray spec
444 or both. Optionally allow [:] for coarrays. */
446 match
447 gfc_match_array_spec (gfc_array_spec **asp, bool match_dim, bool match_codim)
449 array_type current_type;
450 gfc_array_spec *as;
451 int i;
453 as = gfc_get_array_spec ();
455 if (!match_dim)
456 goto coarray;
458 if (gfc_match_char ('(') != MATCH_YES)
460 if (!match_codim)
461 goto done;
462 goto coarray;
465 if (gfc_match (" .. )") == MATCH_YES)
467 as->type = AS_ASSUMED_RANK;
468 as->rank = -1;
470 if (!gfc_notify_std (GFC_STD_F2008_TS, "Assumed-rank array at %C"))
471 goto cleanup;
473 if (!match_codim)
474 goto done;
475 goto coarray;
478 for (;;)
480 as->rank++;
481 current_type = match_array_element_spec (as);
483 /* Note that current_type == AS_ASSUMED_SIZE for both assumed-size
484 and implied-shape specifications. If the rank is at least 2, we can
485 distinguish between them. But for rank 1, we currently return
486 ASSUMED_SIZE; this gets adjusted later when we know for sure
487 whether the symbol parsed is a PARAMETER or not. */
489 if (as->rank == 1)
491 if (current_type == AS_UNKNOWN)
492 goto cleanup;
493 as->type = current_type;
495 else
496 switch (as->type)
497 { /* See how current spec meshes with the existing. */
498 case AS_UNKNOWN:
499 goto cleanup;
501 case AS_IMPLIED_SHAPE:
502 if (current_type != AS_ASSUMED_SHAPE)
504 gfc_error ("Bad array specification for implied-shape"
505 " array at %C");
506 goto cleanup;
508 break;
510 case AS_EXPLICIT:
511 if (current_type == AS_ASSUMED_SIZE)
513 as->type = AS_ASSUMED_SIZE;
514 break;
517 if (current_type == AS_EXPLICIT)
518 break;
520 gfc_error ("Bad array specification for an explicitly shaped "
521 "array at %C");
523 goto cleanup;
525 case AS_ASSUMED_SHAPE:
526 if ((current_type == AS_ASSUMED_SHAPE)
527 || (current_type == AS_DEFERRED))
528 break;
530 gfc_error ("Bad array specification for assumed shape "
531 "array at %C");
532 goto cleanup;
534 case AS_DEFERRED:
535 if (current_type == AS_DEFERRED)
536 break;
538 if (current_type == AS_ASSUMED_SHAPE)
540 as->type = AS_ASSUMED_SHAPE;
541 break;
544 gfc_error ("Bad specification for deferred shape array at %C");
545 goto cleanup;
547 case AS_ASSUMED_SIZE:
548 if (as->rank == 2 && current_type == AS_ASSUMED_SIZE)
550 as->type = AS_IMPLIED_SHAPE;
551 break;
554 gfc_error ("Bad specification for assumed size array at %C");
555 goto cleanup;
557 case AS_ASSUMED_RANK:
558 gcc_unreachable ();
561 if (gfc_match_char (')') == MATCH_YES)
562 break;
564 if (gfc_match_char (',') != MATCH_YES)
566 gfc_error ("Expected another dimension in array declaration at %C");
567 goto cleanup;
570 if (as->rank + as->corank >= GFC_MAX_DIMENSIONS)
572 gfc_error ("Array specification at %C has more than %d dimensions",
573 GFC_MAX_DIMENSIONS);
574 goto cleanup;
577 if (as->corank + as->rank >= 7
578 && !gfc_notify_std (GFC_STD_F2008, "Array specification at %C "
579 "with more than 7 dimensions"))
580 goto cleanup;
583 if (!match_codim)
584 goto done;
586 coarray:
587 if (gfc_match_char ('[') != MATCH_YES)
588 goto done;
590 if (!gfc_notify_std (GFC_STD_F2008, "Coarray declaration at %C"))
591 goto cleanup;
593 if (gfc_option.coarray == GFC_FCOARRAY_NONE)
595 gfc_fatal_error ("Coarrays disabled at %C, use -fcoarray= to enable");
596 goto cleanup;
599 if (as->rank >= GFC_MAX_DIMENSIONS)
601 gfc_error ("Array specification at %C has more than %d "
602 "dimensions", GFC_MAX_DIMENSIONS);
603 goto cleanup;
606 for (;;)
608 as->corank++;
609 current_type = match_array_element_spec (as);
611 if (current_type == AS_UNKNOWN)
612 goto cleanup;
614 if (as->corank == 1)
615 as->cotype = current_type;
616 else
617 switch (as->cotype)
618 { /* See how current spec meshes with the existing. */
619 case AS_IMPLIED_SHAPE:
620 case AS_UNKNOWN:
621 goto cleanup;
623 case AS_EXPLICIT:
624 if (current_type == AS_ASSUMED_SIZE)
626 as->cotype = AS_ASSUMED_SIZE;
627 break;
630 if (current_type == AS_EXPLICIT)
631 break;
633 gfc_error ("Bad array specification for an explicitly "
634 "shaped array at %C");
636 goto cleanup;
638 case AS_ASSUMED_SHAPE:
639 if ((current_type == AS_ASSUMED_SHAPE)
640 || (current_type == AS_DEFERRED))
641 break;
643 gfc_error ("Bad array specification for assumed shape "
644 "array at %C");
645 goto cleanup;
647 case AS_DEFERRED:
648 if (current_type == AS_DEFERRED)
649 break;
651 if (current_type == AS_ASSUMED_SHAPE)
653 as->cotype = AS_ASSUMED_SHAPE;
654 break;
657 gfc_error ("Bad specification for deferred shape array at %C");
658 goto cleanup;
660 case AS_ASSUMED_SIZE:
661 gfc_error ("Bad specification for assumed size array at %C");
662 goto cleanup;
664 case AS_ASSUMED_RANK:
665 gcc_unreachable ();
668 if (gfc_match_char (']') == MATCH_YES)
669 break;
671 if (gfc_match_char (',') != MATCH_YES)
673 gfc_error ("Expected another dimension in array declaration at %C");
674 goto cleanup;
677 if (as->rank + as->corank >= GFC_MAX_DIMENSIONS)
679 gfc_error ("Array specification at %C has more than %d "
680 "dimensions", GFC_MAX_DIMENSIONS);
681 goto cleanup;
685 if (current_type == AS_EXPLICIT)
687 gfc_error ("Upper bound of last coarray dimension must be '*' at %C");
688 goto cleanup;
691 if (as->cotype == AS_ASSUMED_SIZE)
692 as->cotype = AS_EXPLICIT;
694 if (as->rank == 0)
695 as->type = as->cotype;
697 done:
698 if (as->rank == 0 && as->corank == 0)
700 *asp = NULL;
701 gfc_free_array_spec (as);
702 return MATCH_NO;
705 /* If a lower bounds of an assumed shape array is blank, put in one. */
706 if (as->type == AS_ASSUMED_SHAPE)
708 for (i = 0; i < as->rank + as->corank; i++)
710 if (as->lower[i] == NULL)
711 as->lower[i] = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
715 *asp = as;
717 return MATCH_YES;
719 cleanup:
720 /* Something went wrong. */
721 gfc_free_array_spec (as);
722 return MATCH_ERROR;
726 /* Given a symbol and an array specification, modify the symbol to
727 have that array specification. The error locus is needed in case
728 something goes wrong. On failure, the caller must free the spec. */
730 bool
731 gfc_set_array_spec (gfc_symbol *sym, gfc_array_spec *as, locus *error_loc)
733 int i;
735 if (as == NULL)
736 return true;
738 if (as->rank
739 && !gfc_add_dimension (&sym->attr, sym->name, error_loc))
740 return false;
742 if (as->corank
743 && !gfc_add_codimension (&sym->attr, sym->name, error_loc))
744 return false;
746 if (sym->as == NULL)
748 sym->as = as;
749 return true;
752 if ((sym->as->type == AS_ASSUMED_RANK && as->corank)
753 || (as->type == AS_ASSUMED_RANK && sym->as->corank))
755 gfc_error ("The assumed-rank array '%s' at %L shall not have a "
756 "codimension", sym->name, error_loc);
757 return false;
760 if (as->corank)
762 /* The "sym" has no corank (checked via gfc_add_codimension). Thus
763 the codimension is simply added. */
764 gcc_assert (as->rank == 0 && sym->as->corank == 0);
766 sym->as->cotype = as->cotype;
767 sym->as->corank = as->corank;
768 for (i = 0; i < as->corank; i++)
770 sym->as->lower[sym->as->rank + i] = as->lower[i];
771 sym->as->upper[sym->as->rank + i] = as->upper[i];
774 else
776 /* The "sym" has no rank (checked via gfc_add_dimension). Thus
777 the dimension is added - but first the codimensions (if existing
778 need to be shifted to make space for the dimension. */
779 gcc_assert (as->corank == 0 && sym->as->rank == 0);
781 sym->as->rank = as->rank;
782 sym->as->type = as->type;
783 sym->as->cray_pointee = as->cray_pointee;
784 sym->as->cp_was_assumed = as->cp_was_assumed;
786 for (i = 0; i < sym->as->corank; i++)
788 sym->as->lower[as->rank + i] = sym->as->lower[i];
789 sym->as->upper[as->rank + i] = sym->as->upper[i];
791 for (i = 0; i < as->rank; i++)
793 sym->as->lower[i] = as->lower[i];
794 sym->as->upper[i] = as->upper[i];
798 free (as);
799 return true;
803 /* Copy an array specification. */
805 gfc_array_spec *
806 gfc_copy_array_spec (gfc_array_spec *src)
808 gfc_array_spec *dest;
809 int i;
811 if (src == NULL)
812 return NULL;
814 dest = gfc_get_array_spec ();
816 *dest = *src;
818 for (i = 0; i < dest->rank + dest->corank; i++)
820 dest->lower[i] = gfc_copy_expr (dest->lower[i]);
821 dest->upper[i] = gfc_copy_expr (dest->upper[i]);
824 return dest;
828 /* Returns nonzero if the two expressions are equal. Only handles integer
829 constants. */
831 static int
832 compare_bounds (gfc_expr *bound1, gfc_expr *bound2)
834 if (bound1 == NULL || bound2 == NULL
835 || bound1->expr_type != EXPR_CONSTANT
836 || bound2->expr_type != EXPR_CONSTANT
837 || bound1->ts.type != BT_INTEGER
838 || bound2->ts.type != BT_INTEGER)
839 gfc_internal_error ("gfc_compare_array_spec(): Array spec clobbered");
841 if (mpz_cmp (bound1->value.integer, bound2->value.integer) == 0)
842 return 1;
843 else
844 return 0;
848 /* Compares two array specifications. They must be constant or deferred
849 shape. */
852 gfc_compare_array_spec (gfc_array_spec *as1, gfc_array_spec *as2)
854 int i;
856 if (as1 == NULL && as2 == NULL)
857 return 1;
859 if (as1 == NULL || as2 == NULL)
860 return 0;
862 if (as1->rank != as2->rank)
863 return 0;
865 if (as1->corank != as2->corank)
866 return 0;
868 if (as1->rank == 0)
869 return 1;
871 if (as1->type != as2->type)
872 return 0;
874 if (as1->type == AS_EXPLICIT)
875 for (i = 0; i < as1->rank + as1->corank; i++)
877 if (compare_bounds (as1->lower[i], as2->lower[i]) == 0)
878 return 0;
880 if (compare_bounds (as1->upper[i], as2->upper[i]) == 0)
881 return 0;
884 return 1;
888 /****************** Array constructor functions ******************/
891 /* Given an expression node that might be an array constructor and a
892 symbol, make sure that no iterators in this or child constructors
893 use the symbol as an implied-DO iterator. Returns nonzero if a
894 duplicate was found. */
896 static int
897 check_duplicate_iterator (gfc_constructor_base base, gfc_symbol *master)
899 gfc_constructor *c;
900 gfc_expr *e;
902 for (c = gfc_constructor_first (base); c; c = gfc_constructor_next (c))
904 e = c->expr;
906 if (e->expr_type == EXPR_ARRAY
907 && check_duplicate_iterator (e->value.constructor, master))
908 return 1;
910 if (c->iterator == NULL)
911 continue;
913 if (c->iterator->var->symtree->n.sym == master)
915 gfc_error ("DO-iterator '%s' at %L is inside iterator of the "
916 "same name", master->name, &c->where);
918 return 1;
922 return 0;
926 /* Forward declaration because these functions are mutually recursive. */
927 static match match_array_cons_element (gfc_constructor_base *);
929 /* Match a list of array elements. */
931 static match
932 match_array_list (gfc_constructor_base *result)
934 gfc_constructor_base head;
935 gfc_constructor *p;
936 gfc_iterator iter;
937 locus old_loc;
938 gfc_expr *e;
939 match m;
940 int n;
942 old_loc = gfc_current_locus;
944 if (gfc_match_char ('(') == MATCH_NO)
945 return MATCH_NO;
947 memset (&iter, '\0', sizeof (gfc_iterator));
948 head = NULL;
950 m = match_array_cons_element (&head);
951 if (m != MATCH_YES)
952 goto cleanup;
954 if (gfc_match_char (',') != MATCH_YES)
956 m = MATCH_NO;
957 goto cleanup;
960 for (n = 1;; n++)
962 m = gfc_match_iterator (&iter, 0);
963 if (m == MATCH_YES)
964 break;
965 if (m == MATCH_ERROR)
966 goto cleanup;
968 m = match_array_cons_element (&head);
969 if (m == MATCH_ERROR)
970 goto cleanup;
971 if (m == MATCH_NO)
973 if (n > 2)
974 goto syntax;
975 m = MATCH_NO;
976 goto cleanup; /* Could be a complex constant */
979 if (gfc_match_char (',') != MATCH_YES)
981 if (n > 2)
982 goto syntax;
983 m = MATCH_NO;
984 goto cleanup;
988 if (gfc_match_char (')') != MATCH_YES)
989 goto syntax;
991 if (check_duplicate_iterator (head, iter.var->symtree->n.sym))
993 m = MATCH_ERROR;
994 goto cleanup;
997 e = gfc_get_array_expr (BT_UNKNOWN, 0, &old_loc);
998 e->value.constructor = head;
1000 p = gfc_constructor_append_expr (result, e, &gfc_current_locus);
1001 p->iterator = gfc_get_iterator ();
1002 *p->iterator = iter;
1004 return MATCH_YES;
1006 syntax:
1007 gfc_error ("Syntax error in array constructor at %C");
1008 m = MATCH_ERROR;
1010 cleanup:
1011 gfc_constructor_free (head);
1012 gfc_free_iterator (&iter, 0);
1013 gfc_current_locus = old_loc;
1014 return m;
1018 /* Match a single element of an array constructor, which can be a
1019 single expression or a list of elements. */
1021 static match
1022 match_array_cons_element (gfc_constructor_base *result)
1024 gfc_expr *expr;
1025 match m;
1027 m = match_array_list (result);
1028 if (m != MATCH_NO)
1029 return m;
1031 m = gfc_match_expr (&expr);
1032 if (m != MATCH_YES)
1033 return m;
1035 gfc_constructor_append_expr (result, expr, &gfc_current_locus);
1036 return MATCH_YES;
1040 /* Match an array constructor. */
1042 match
1043 gfc_match_array_constructor (gfc_expr **result)
1045 gfc_constructor_base head, new_cons;
1046 gfc_undo_change_set changed_syms;
1047 gfc_expr *expr;
1048 gfc_typespec ts;
1049 locus where;
1050 match m;
1051 const char *end_delim;
1052 bool seen_ts;
1054 if (gfc_match (" (/") == MATCH_NO)
1056 if (gfc_match (" [") == MATCH_NO)
1057 return MATCH_NO;
1058 else
1060 if (!gfc_notify_std (GFC_STD_F2003, "[...] "
1061 "style array constructors at %C"))
1062 return MATCH_ERROR;
1063 end_delim = " ]";
1066 else
1067 end_delim = " /)";
1069 where = gfc_current_locus;
1070 head = new_cons = NULL;
1071 seen_ts = false;
1073 /* Try to match an optional "type-spec ::" */
1074 gfc_clear_ts (&ts);
1075 gfc_new_undo_checkpoint (changed_syms);
1076 if (gfc_match_decl_type_spec (&ts, 0) == MATCH_YES)
1078 seen_ts = (gfc_match (" ::") == MATCH_YES);
1080 if (seen_ts)
1082 if (!gfc_notify_std (GFC_STD_F2003, "Array constructor "
1083 "including type specification at %C"))
1085 gfc_restore_last_undo_checkpoint ();
1086 goto cleanup;
1089 if (ts.deferred)
1091 gfc_error ("Type-spec at %L cannot contain a deferred "
1092 "type parameter", &where);
1093 gfc_restore_last_undo_checkpoint ();
1094 goto cleanup;
1099 if (seen_ts)
1100 gfc_drop_last_undo_checkpoint ();
1101 else
1103 gfc_restore_last_undo_checkpoint ();
1104 gfc_current_locus = where;
1107 if (gfc_match (end_delim) == MATCH_YES)
1109 if (seen_ts)
1110 goto done;
1111 else
1113 gfc_error ("Empty array constructor at %C is not allowed");
1114 goto cleanup;
1118 for (;;)
1120 m = match_array_cons_element (&head);
1121 if (m == MATCH_ERROR)
1122 goto cleanup;
1123 if (m == MATCH_NO)
1124 goto syntax;
1126 if (gfc_match_char (',') == MATCH_NO)
1127 break;
1130 if (gfc_match (end_delim) == MATCH_NO)
1131 goto syntax;
1133 done:
1134 /* Size must be calculated at resolution time. */
1135 if (seen_ts)
1137 expr = gfc_get_array_expr (ts.type, ts.kind, &where);
1138 expr->ts = ts;
1140 else
1141 expr = gfc_get_array_expr (BT_UNKNOWN, 0, &where);
1143 expr->value.constructor = head;
1144 if (expr->ts.u.cl)
1145 expr->ts.u.cl->length_from_typespec = seen_ts;
1147 *result = expr;
1148 return MATCH_YES;
1150 syntax:
1151 gfc_error ("Syntax error in array constructor at %C");
1153 cleanup:
1154 gfc_constructor_free (head);
1155 return MATCH_ERROR;
1160 /************** Check array constructors for correctness **************/
1162 /* Given an expression, compare it's type with the type of the current
1163 constructor. Returns nonzero if an error was issued. The
1164 cons_state variable keeps track of whether the type of the
1165 constructor being read or resolved is known to be good, bad or just
1166 starting out. */
1168 static gfc_typespec constructor_ts;
1169 static enum
1170 { CONS_START, CONS_GOOD, CONS_BAD }
1171 cons_state;
1173 static int
1174 check_element_type (gfc_expr *expr, bool convert)
1176 if (cons_state == CONS_BAD)
1177 return 0; /* Suppress further errors */
1179 if (cons_state == CONS_START)
1181 if (expr->ts.type == BT_UNKNOWN)
1182 cons_state = CONS_BAD;
1183 else
1185 cons_state = CONS_GOOD;
1186 constructor_ts = expr->ts;
1189 return 0;
1192 if (gfc_compare_types (&constructor_ts, &expr->ts))
1193 return 0;
1195 if (convert)
1196 return gfc_convert_type(expr, &constructor_ts, 1) ? 0 : 1;
1198 gfc_error ("Element in %s array constructor at %L is %s",
1199 gfc_typename (&constructor_ts), &expr->where,
1200 gfc_typename (&expr->ts));
1202 cons_state = CONS_BAD;
1203 return 1;
1207 /* Recursive work function for gfc_check_constructor_type(). */
1209 static bool
1210 check_constructor_type (gfc_constructor_base base, bool convert)
1212 gfc_constructor *c;
1213 gfc_expr *e;
1215 for (c = gfc_constructor_first (base); c; c = gfc_constructor_next (c))
1217 e = c->expr;
1219 if (e->expr_type == EXPR_ARRAY)
1221 if (!check_constructor_type (e->value.constructor, convert))
1222 return false;
1224 continue;
1227 if (check_element_type (e, convert))
1228 return false;
1231 return true;
1235 /* Check that all elements of an array constructor are the same type.
1236 On false, an error has been generated. */
1238 bool
1239 gfc_check_constructor_type (gfc_expr *e)
1241 bool t;
1243 if (e->ts.type != BT_UNKNOWN)
1245 cons_state = CONS_GOOD;
1246 constructor_ts = e->ts;
1248 else
1250 cons_state = CONS_START;
1251 gfc_clear_ts (&constructor_ts);
1254 /* If e->ts.type != BT_UNKNOWN, the array constructor included a
1255 typespec, and we will now convert the values on the fly. */
1256 t = check_constructor_type (e->value.constructor, e->ts.type != BT_UNKNOWN);
1257 if (t && e->ts.type == BT_UNKNOWN)
1258 e->ts = constructor_ts;
1260 return t;
1265 typedef struct cons_stack
1267 gfc_iterator *iterator;
1268 struct cons_stack *previous;
1270 cons_stack;
1272 static cons_stack *base;
1274 static bool check_constructor (gfc_constructor_base, bool (*) (gfc_expr *));
1276 /* Check an EXPR_VARIABLE expression in a constructor to make sure
1277 that that variable is an iteration variables. */
1279 bool
1280 gfc_check_iter_variable (gfc_expr *expr)
1282 gfc_symbol *sym;
1283 cons_stack *c;
1285 sym = expr->symtree->n.sym;
1287 for (c = base; c && c->iterator; c = c->previous)
1288 if (sym == c->iterator->var->symtree->n.sym)
1289 return true;
1291 return false;
1295 /* Recursive work function for gfc_check_constructor(). This amounts
1296 to calling the check function for each expression in the
1297 constructor, giving variables with the names of iterators a pass. */
1299 static bool
1300 check_constructor (gfc_constructor_base ctor, bool (*check_function) (gfc_expr *))
1302 cons_stack element;
1303 gfc_expr *e;
1304 bool t;
1305 gfc_constructor *c;
1307 for (c = gfc_constructor_first (ctor); c; c = gfc_constructor_next (c))
1309 e = c->expr;
1311 if (e->expr_type != EXPR_ARRAY)
1313 if (!(*check_function)(e))
1314 return false;
1315 continue;
1318 element.previous = base;
1319 element.iterator = c->iterator;
1321 base = &element;
1322 t = check_constructor (e->value.constructor, check_function);
1323 base = element.previous;
1325 if (!t)
1326 return false;
1329 /* Nothing went wrong, so all OK. */
1330 return true;
1334 /* Checks a constructor to see if it is a particular kind of
1335 expression -- specification, restricted, or initialization as
1336 determined by the check_function. */
1338 bool
1339 gfc_check_constructor (gfc_expr *expr, bool (*check_function) (gfc_expr *))
1341 cons_stack *base_save;
1342 bool t;
1344 base_save = base;
1345 base = NULL;
1347 t = check_constructor (expr->value.constructor, check_function);
1348 base = base_save;
1350 return t;
1355 /**************** Simplification of array constructors ****************/
1357 iterator_stack *iter_stack;
1359 typedef struct
1361 gfc_constructor_base base;
1362 int extract_count, extract_n;
1363 gfc_expr *extracted;
1364 mpz_t *count;
1366 mpz_t *offset;
1367 gfc_component *component;
1368 mpz_t *repeat;
1370 bool (*expand_work_function) (gfc_expr *);
1372 expand_info;
1374 static expand_info current_expand;
1376 static bool expand_constructor (gfc_constructor_base);
1379 /* Work function that counts the number of elements present in a
1380 constructor. */
1382 static bool
1383 count_elements (gfc_expr *e)
1385 mpz_t result;
1387 if (e->rank == 0)
1388 mpz_add_ui (*current_expand.count, *current_expand.count, 1);
1389 else
1391 if (!gfc_array_size (e, &result))
1393 gfc_free_expr (e);
1394 return false;
1397 mpz_add (*current_expand.count, *current_expand.count, result);
1398 mpz_clear (result);
1401 gfc_free_expr (e);
1402 return true;
1406 /* Work function that extracts a particular element from an array
1407 constructor, freeing the rest. */
1409 static bool
1410 extract_element (gfc_expr *e)
1412 if (e->rank != 0)
1413 { /* Something unextractable */
1414 gfc_free_expr (e);
1415 return false;
1418 if (current_expand.extract_count == current_expand.extract_n)
1419 current_expand.extracted = e;
1420 else
1421 gfc_free_expr (e);
1423 current_expand.extract_count++;
1425 return true;
1429 /* Work function that constructs a new constructor out of the old one,
1430 stringing new elements together. */
1432 static bool
1433 expand (gfc_expr *e)
1435 gfc_constructor *c = gfc_constructor_append_expr (&current_expand.base,
1436 e, &e->where);
1438 c->n.component = current_expand.component;
1439 return true;
1443 /* Given an initialization expression that is a variable reference,
1444 substitute the current value of the iteration variable. */
1446 void
1447 gfc_simplify_iterator_var (gfc_expr *e)
1449 iterator_stack *p;
1451 for (p = iter_stack; p; p = p->prev)
1452 if (e->symtree == p->variable)
1453 break;
1455 if (p == NULL)
1456 return; /* Variable not found */
1458 gfc_replace_expr (e, gfc_get_int_expr (gfc_default_integer_kind, NULL, 0));
1460 mpz_set (e->value.integer, p->value);
1462 return;
1466 /* Expand an expression with that is inside of a constructor,
1467 recursing into other constructors if present. */
1469 static bool
1470 expand_expr (gfc_expr *e)
1472 if (e->expr_type == EXPR_ARRAY)
1473 return expand_constructor (e->value.constructor);
1475 e = gfc_copy_expr (e);
1477 if (!gfc_simplify_expr (e, 1))
1479 gfc_free_expr (e);
1480 return false;
1483 return current_expand.expand_work_function (e);
1487 static bool
1488 expand_iterator (gfc_constructor *c)
1490 gfc_expr *start, *end, *step;
1491 iterator_stack frame;
1492 mpz_t trip;
1493 bool t;
1495 end = step = NULL;
1497 t = false;
1499 mpz_init (trip);
1500 mpz_init (frame.value);
1501 frame.prev = NULL;
1503 start = gfc_copy_expr (c->iterator->start);
1504 if (!gfc_simplify_expr (start, 1))
1505 goto cleanup;
1507 if (start->expr_type != EXPR_CONSTANT || start->ts.type != BT_INTEGER)
1508 goto cleanup;
1510 end = gfc_copy_expr (c->iterator->end);
1511 if (!gfc_simplify_expr (end, 1))
1512 goto cleanup;
1514 if (end->expr_type != EXPR_CONSTANT || end->ts.type != BT_INTEGER)
1515 goto cleanup;
1517 step = gfc_copy_expr (c->iterator->step);
1518 if (!gfc_simplify_expr (step, 1))
1519 goto cleanup;
1521 if (step->expr_type != EXPR_CONSTANT || step->ts.type != BT_INTEGER)
1522 goto cleanup;
1524 if (mpz_sgn (step->value.integer) == 0)
1526 gfc_error ("Iterator step at %L cannot be zero", &step->where);
1527 goto cleanup;
1530 /* Calculate the trip count of the loop. */
1531 mpz_sub (trip, end->value.integer, start->value.integer);
1532 mpz_add (trip, trip, step->value.integer);
1533 mpz_tdiv_q (trip, trip, step->value.integer);
1535 mpz_set (frame.value, start->value.integer);
1537 frame.prev = iter_stack;
1538 frame.variable = c->iterator->var->symtree;
1539 iter_stack = &frame;
1541 while (mpz_sgn (trip) > 0)
1543 if (!expand_expr (c->expr))
1544 goto cleanup;
1546 mpz_add (frame.value, frame.value, step->value.integer);
1547 mpz_sub_ui (trip, trip, 1);
1550 t = true;
1552 cleanup:
1553 gfc_free_expr (start);
1554 gfc_free_expr (end);
1555 gfc_free_expr (step);
1557 mpz_clear (trip);
1558 mpz_clear (frame.value);
1560 iter_stack = frame.prev;
1562 return t;
1566 /* Expand a constructor into constant constructors without any
1567 iterators, calling the work function for each of the expanded
1568 expressions. The work function needs to either save or free the
1569 passed expression. */
1571 static bool
1572 expand_constructor (gfc_constructor_base base)
1574 gfc_constructor *c;
1575 gfc_expr *e;
1577 for (c = gfc_constructor_first (base); c; c = gfc_constructor_next(c))
1579 if (c->iterator != NULL)
1581 if (!expand_iterator (c))
1582 return false;
1583 continue;
1586 e = c->expr;
1588 if (e->expr_type == EXPR_ARRAY)
1590 if (!expand_constructor (e->value.constructor))
1591 return false;
1593 continue;
1596 e = gfc_copy_expr (e);
1597 if (!gfc_simplify_expr (e, 1))
1599 gfc_free_expr (e);
1600 return false;
1602 current_expand.offset = &c->offset;
1603 current_expand.repeat = &c->repeat;
1604 current_expand.component = c->n.component;
1605 if (!current_expand.expand_work_function(e))
1606 return false;
1608 return true;
1612 /* Given an array expression and an element number (starting at zero),
1613 return a pointer to the array element. NULL is returned if the
1614 size of the array has been exceeded. The expression node returned
1615 remains a part of the array and should not be freed. Access is not
1616 efficient at all, but this is another place where things do not
1617 have to be particularly fast. */
1619 static gfc_expr *
1620 gfc_get_array_element (gfc_expr *array, int element)
1622 expand_info expand_save;
1623 gfc_expr *e;
1624 bool rc;
1626 expand_save = current_expand;
1627 current_expand.extract_n = element;
1628 current_expand.expand_work_function = extract_element;
1629 current_expand.extracted = NULL;
1630 current_expand.extract_count = 0;
1632 iter_stack = NULL;
1634 rc = expand_constructor (array->value.constructor);
1635 e = current_expand.extracted;
1636 current_expand = expand_save;
1638 if (!rc)
1639 return NULL;
1641 return e;
1645 /* Top level subroutine for expanding constructors. We only expand
1646 constructor if they are small enough. */
1648 bool
1649 gfc_expand_constructor (gfc_expr *e, bool fatal)
1651 expand_info expand_save;
1652 gfc_expr *f;
1653 bool rc;
1655 /* If we can successfully get an array element at the max array size then
1656 the array is too big to expand, so we just return. */
1657 f = gfc_get_array_element (e, gfc_option.flag_max_array_constructor);
1658 if (f != NULL)
1660 gfc_free_expr (f);
1661 if (fatal)
1663 gfc_error ("The number of elements in the array constructor "
1664 "at %L requires an increase of the allowed %d "
1665 "upper limit. See -fmax-array-constructor "
1666 "option", &e->where,
1667 gfc_option.flag_max_array_constructor);
1668 return false;
1670 return true;
1673 /* We now know the array is not too big so go ahead and try to expand it. */
1674 expand_save = current_expand;
1675 current_expand.base = NULL;
1677 iter_stack = NULL;
1679 current_expand.expand_work_function = expand;
1681 if (!expand_constructor (e->value.constructor))
1683 gfc_constructor_free (current_expand.base);
1684 rc = false;
1685 goto done;
1688 gfc_constructor_free (e->value.constructor);
1689 e->value.constructor = current_expand.base;
1691 rc = true;
1693 done:
1694 current_expand = expand_save;
1696 return rc;
1700 /* Work function for checking that an element of a constructor is a
1701 constant, after removal of any iteration variables. We return
1702 false if not so. */
1704 static bool
1705 is_constant_element (gfc_expr *e)
1707 int rv;
1709 rv = gfc_is_constant_expr (e);
1710 gfc_free_expr (e);
1712 return rv ? true : false;
1716 /* Given an array constructor, determine if the constructor is
1717 constant or not by expanding it and making sure that all elements
1718 are constants. This is a bit of a hack since something like (/ (i,
1719 i=1,100000000) /) will take a while as* opposed to a more clever
1720 function that traverses the expression tree. FIXME. */
1723 gfc_constant_ac (gfc_expr *e)
1725 expand_info expand_save;
1726 bool rc;
1728 iter_stack = NULL;
1729 expand_save = current_expand;
1730 current_expand.expand_work_function = is_constant_element;
1732 rc = expand_constructor (e->value.constructor);
1734 current_expand = expand_save;
1735 if (!rc)
1736 return 0;
1738 return 1;
1742 /* Returns nonzero if an array constructor has been completely
1743 expanded (no iterators) and zero if iterators are present. */
1746 gfc_expanded_ac (gfc_expr *e)
1748 gfc_constructor *c;
1750 if (e->expr_type == EXPR_ARRAY)
1751 for (c = gfc_constructor_first (e->value.constructor);
1752 c; c = gfc_constructor_next (c))
1753 if (c->iterator != NULL || !gfc_expanded_ac (c->expr))
1754 return 0;
1756 return 1;
1760 /*************** Type resolution of array constructors ***************/
1763 /* The symbol expr_is_sought_symbol_ref will try to find. */
1764 static const gfc_symbol *sought_symbol = NULL;
1767 /* Tells whether the expression E is a variable reference to the symbol
1768 in the static variable SOUGHT_SYMBOL, and sets the locus pointer WHERE
1769 accordingly.
1770 To be used with gfc_expr_walker: if a reference is found we don't need
1771 to look further so we return 1 to skip any further walk. */
1773 static int
1774 expr_is_sought_symbol_ref (gfc_expr **e, int *walk_subtrees ATTRIBUTE_UNUSED,
1775 void *where)
1777 gfc_expr *expr = *e;
1778 locus *sym_loc = (locus *)where;
1780 if (expr->expr_type == EXPR_VARIABLE
1781 && expr->symtree->n.sym == sought_symbol)
1783 *sym_loc = expr->where;
1784 return 1;
1787 return 0;
1791 /* Tells whether the expression EXPR contains a reference to the symbol
1792 SYM and in that case sets the position SYM_LOC where the reference is. */
1794 static bool
1795 find_symbol_in_expr (gfc_symbol *sym, gfc_expr *expr, locus *sym_loc)
1797 int ret;
1799 sought_symbol = sym;
1800 ret = gfc_expr_walker (&expr, &expr_is_sought_symbol_ref, sym_loc);
1801 sought_symbol = NULL;
1802 return ret;
1806 /* Recursive array list resolution function. All of the elements must
1807 be of the same type. */
1809 static bool
1810 resolve_array_list (gfc_constructor_base base)
1812 bool t;
1813 gfc_constructor *c;
1814 gfc_iterator *iter;
1816 t = true;
1818 for (c = gfc_constructor_first (base); c; c = gfc_constructor_next (c))
1820 iter = c->iterator;
1821 if (iter != NULL)
1823 gfc_symbol *iter_var;
1824 locus iter_var_loc;
1826 if (!gfc_resolve_iterator (iter, false, true))
1827 t = false;
1829 /* Check for bounds referencing the iterator variable. */
1830 gcc_assert (iter->var->expr_type == EXPR_VARIABLE);
1831 iter_var = iter->var->symtree->n.sym;
1832 if (find_symbol_in_expr (iter_var, iter->start, &iter_var_loc))
1834 if (!gfc_notify_std (GFC_STD_LEGACY, "AC-IMPLIED-DO initial "
1835 "expression references control variable "
1836 "at %L", &iter_var_loc))
1837 t = false;
1839 if (find_symbol_in_expr (iter_var, iter->end, &iter_var_loc))
1841 if (!gfc_notify_std (GFC_STD_LEGACY, "AC-IMPLIED-DO final "
1842 "expression references control variable "
1843 "at %L", &iter_var_loc))
1844 t = false;
1846 if (find_symbol_in_expr (iter_var, iter->step, &iter_var_loc))
1848 if (!gfc_notify_std (GFC_STD_LEGACY, "AC-IMPLIED-DO step "
1849 "expression references control variable "
1850 "at %L", &iter_var_loc))
1851 t = false;
1855 if (!gfc_resolve_expr (c->expr))
1856 t = false;
1858 if (UNLIMITED_POLY (c->expr))
1860 gfc_error ("Array constructor value at %L shall not be unlimited "
1861 "polymorphic [F2008: C4106]", &c->expr->where);
1862 t = false;
1866 return t;
1869 /* Resolve character array constructor. If it has a specified constant character
1870 length, pad/truncate the elements here; if the length is not specified and
1871 all elements are of compile-time known length, emit an error as this is
1872 invalid. */
1874 bool
1875 gfc_resolve_character_array_constructor (gfc_expr *expr)
1877 gfc_constructor *p;
1878 int found_length;
1880 gcc_assert (expr->expr_type == EXPR_ARRAY);
1881 gcc_assert (expr->ts.type == BT_CHARACTER);
1883 if (expr->ts.u.cl == NULL)
1885 for (p = gfc_constructor_first (expr->value.constructor);
1886 p; p = gfc_constructor_next (p))
1887 if (p->expr->ts.u.cl != NULL)
1889 /* Ensure that if there is a char_len around that it is
1890 used; otherwise the middle-end confuses them! */
1891 expr->ts.u.cl = p->expr->ts.u.cl;
1892 goto got_charlen;
1895 expr->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
1898 got_charlen:
1900 found_length = -1;
1902 if (expr->ts.u.cl->length == NULL)
1904 /* Check that all constant string elements have the same length until
1905 we reach the end or find a variable-length one. */
1907 for (p = gfc_constructor_first (expr->value.constructor);
1908 p; p = gfc_constructor_next (p))
1910 int current_length = -1;
1911 gfc_ref *ref;
1912 for (ref = p->expr->ref; ref; ref = ref->next)
1913 if (ref->type == REF_SUBSTRING
1914 && ref->u.ss.start->expr_type == EXPR_CONSTANT
1915 && ref->u.ss.end->expr_type == EXPR_CONSTANT)
1916 break;
1918 if (p->expr->expr_type == EXPR_CONSTANT)
1919 current_length = p->expr->value.character.length;
1920 else if (ref)
1922 long j;
1923 j = mpz_get_ui (ref->u.ss.end->value.integer)
1924 - mpz_get_ui (ref->u.ss.start->value.integer) + 1;
1925 current_length = (int) j;
1927 else if (p->expr->ts.u.cl && p->expr->ts.u.cl->length
1928 && p->expr->ts.u.cl->length->expr_type == EXPR_CONSTANT)
1930 long j;
1931 j = mpz_get_si (p->expr->ts.u.cl->length->value.integer);
1932 current_length = (int) j;
1934 else
1935 return true;
1937 gcc_assert (current_length != -1);
1939 if (found_length == -1)
1940 found_length = current_length;
1941 else if (found_length != current_length)
1943 gfc_error ("Different CHARACTER lengths (%d/%d) in array"
1944 " constructor at %L", found_length, current_length,
1945 &p->expr->where);
1946 return false;
1949 gcc_assert (found_length == current_length);
1952 gcc_assert (found_length != -1);
1954 /* Update the character length of the array constructor. */
1955 expr->ts.u.cl->length = gfc_get_int_expr (gfc_default_integer_kind,
1956 NULL, found_length);
1958 else
1960 /* We've got a character length specified. It should be an integer,
1961 otherwise an error is signalled elsewhere. */
1962 gcc_assert (expr->ts.u.cl->length);
1964 /* If we've got a constant character length, pad according to this.
1965 gfc_extract_int does check for BT_INTEGER and EXPR_CONSTANT and sets
1966 max_length only if they pass. */
1967 gfc_extract_int (expr->ts.u.cl->length, &found_length);
1969 /* Now pad/truncate the elements accordingly to the specified character
1970 length. This is ok inside this conditional, as in the case above
1971 (without typespec) all elements are verified to have the same length
1972 anyway. */
1973 if (found_length != -1)
1974 for (p = gfc_constructor_first (expr->value.constructor);
1975 p; p = gfc_constructor_next (p))
1976 if (p->expr->expr_type == EXPR_CONSTANT)
1978 gfc_expr *cl = NULL;
1979 int current_length = -1;
1980 bool has_ts;
1982 if (p->expr->ts.u.cl && p->expr->ts.u.cl->length)
1984 cl = p->expr->ts.u.cl->length;
1985 gfc_extract_int (cl, &current_length);
1988 /* If gfc_extract_int above set current_length, we implicitly
1989 know the type is BT_INTEGER and it's EXPR_CONSTANT. */
1991 has_ts = expr->ts.u.cl->length_from_typespec;
1993 if (! cl
1994 || (current_length != -1 && current_length != found_length))
1995 gfc_set_constant_character_len (found_length, p->expr,
1996 has_ts ? -1 : found_length);
2000 return true;
2004 /* Resolve all of the expressions in an array list. */
2006 bool
2007 gfc_resolve_array_constructor (gfc_expr *expr)
2009 bool t;
2011 t = resolve_array_list (expr->value.constructor);
2012 if (t)
2013 t = gfc_check_constructor_type (expr);
2015 /* gfc_resolve_character_array_constructor is called in gfc_resolve_expr after
2016 the call to this function, so we don't need to call it here; if it was
2017 called twice, an error message there would be duplicated. */
2019 return t;
2023 /* Copy an iterator structure. */
2025 gfc_iterator *
2026 gfc_copy_iterator (gfc_iterator *src)
2028 gfc_iterator *dest;
2030 if (src == NULL)
2031 return NULL;
2033 dest = gfc_get_iterator ();
2035 dest->var = gfc_copy_expr (src->var);
2036 dest->start = gfc_copy_expr (src->start);
2037 dest->end = gfc_copy_expr (src->end);
2038 dest->step = gfc_copy_expr (src->step);
2040 return dest;
2044 /********* Subroutines for determining the size of an array *********/
2046 /* These are needed just to accommodate RESHAPE(). There are no
2047 diagnostics here, we just return a negative number if something
2048 goes wrong. */
2051 /* Get the size of single dimension of an array specification. The
2052 array is guaranteed to be one dimensional. */
2054 bool
2055 spec_dimen_size (gfc_array_spec *as, int dimen, mpz_t *result)
2057 if (as == NULL)
2058 return false;
2060 if (dimen < 0 || dimen > as->rank - 1)
2061 gfc_internal_error ("spec_dimen_size(): Bad dimension");
2063 if (as->type != AS_EXPLICIT
2064 || as->lower[dimen]->expr_type != EXPR_CONSTANT
2065 || as->upper[dimen]->expr_type != EXPR_CONSTANT
2066 || as->lower[dimen]->ts.type != BT_INTEGER
2067 || as->upper[dimen]->ts.type != BT_INTEGER)
2068 return false;
2070 mpz_init (*result);
2072 mpz_sub (*result, as->upper[dimen]->value.integer,
2073 as->lower[dimen]->value.integer);
2075 mpz_add_ui (*result, *result, 1);
2077 return true;
2081 bool
2082 spec_size (gfc_array_spec *as, mpz_t *result)
2084 mpz_t size;
2085 int d;
2087 if (as->type == AS_ASSUMED_RANK)
2088 return false;
2090 mpz_init_set_ui (*result, 1);
2092 for (d = 0; d < as->rank; d++)
2094 if (!spec_dimen_size (as, d, &size))
2096 mpz_clear (*result);
2097 return false;
2100 mpz_mul (*result, *result, size);
2101 mpz_clear (size);
2104 return true;
2108 /* Get the number of elements in an array section. Optionally, also supply
2109 the end value. */
2111 bool
2112 gfc_ref_dimen_size (gfc_array_ref *ar, int dimen, mpz_t *result, mpz_t *end)
2114 mpz_t upper, lower, stride;
2115 bool t;
2117 if (dimen < 0 || ar == NULL || dimen > ar->dimen - 1)
2118 gfc_internal_error ("gfc_ref_dimen_size(): Bad dimension");
2120 switch (ar->dimen_type[dimen])
2122 case DIMEN_ELEMENT:
2123 mpz_init (*result);
2124 mpz_set_ui (*result, 1);
2125 t = true;
2126 break;
2128 case DIMEN_VECTOR:
2129 t = gfc_array_size (ar->start[dimen], result); /* Recurse! */
2130 break;
2132 case DIMEN_RANGE:
2133 mpz_init (upper);
2134 mpz_init (lower);
2135 mpz_init (stride);
2136 t = false;
2138 if (ar->start[dimen] == NULL)
2140 if (ar->as->lower[dimen] == NULL
2141 || ar->as->lower[dimen]->expr_type != EXPR_CONSTANT)
2142 goto cleanup;
2143 mpz_set (lower, ar->as->lower[dimen]->value.integer);
2145 else
2147 if (ar->start[dimen]->expr_type != EXPR_CONSTANT)
2148 goto cleanup;
2149 mpz_set (lower, ar->start[dimen]->value.integer);
2152 if (ar->end[dimen] == NULL)
2154 if (ar->as->upper[dimen] == NULL
2155 || ar->as->upper[dimen]->expr_type != EXPR_CONSTANT)
2156 goto cleanup;
2157 mpz_set (upper, ar->as->upper[dimen]->value.integer);
2159 else
2161 if (ar->end[dimen]->expr_type != EXPR_CONSTANT)
2162 goto cleanup;
2163 mpz_set (upper, ar->end[dimen]->value.integer);
2166 if (ar->stride[dimen] == NULL)
2167 mpz_set_ui (stride, 1);
2168 else
2170 if (ar->stride[dimen]->expr_type != EXPR_CONSTANT)
2171 goto cleanup;
2172 mpz_set (stride, ar->stride[dimen]->value.integer);
2175 mpz_init (*result);
2176 mpz_sub (*result, upper, lower);
2177 mpz_add (*result, *result, stride);
2178 mpz_div (*result, *result, stride);
2180 /* Zero stride caught earlier. */
2181 if (mpz_cmp_ui (*result, 0) < 0)
2182 mpz_set_ui (*result, 0);
2183 t = true;
2185 if (end)
2187 mpz_init (*end);
2189 mpz_sub_ui (*end, *result, 1UL);
2190 mpz_mul (*end, *end, stride);
2191 mpz_add (*end, *end, lower);
2194 cleanup:
2195 mpz_clear (upper);
2196 mpz_clear (lower);
2197 mpz_clear (stride);
2198 return t;
2200 default:
2201 gfc_internal_error ("gfc_ref_dimen_size(): Bad dimen_type");
2204 return t;
2208 static bool
2209 ref_size (gfc_array_ref *ar, mpz_t *result)
2211 mpz_t size;
2212 int d;
2214 mpz_init_set_ui (*result, 1);
2216 for (d = 0; d < ar->dimen; d++)
2218 if (!gfc_ref_dimen_size (ar, d, &size, NULL))
2220 mpz_clear (*result);
2221 return false;
2224 mpz_mul (*result, *result, size);
2225 mpz_clear (size);
2228 return true;
2232 /* Given an array expression and a dimension, figure out how many
2233 elements it has along that dimension. Returns true if we were
2234 able to return a result in the 'result' variable, false
2235 otherwise. */
2237 bool
2238 gfc_array_dimen_size (gfc_expr *array, int dimen, mpz_t *result)
2240 gfc_ref *ref;
2241 int i;
2243 gcc_assert (array != NULL);
2245 if (array->ts.type == BT_CLASS)
2246 return false;
2248 if (array->rank == -1)
2249 return false;
2251 if (dimen < 0 || dimen > array->rank - 1)
2252 gfc_internal_error ("gfc_array_dimen_size(): Bad dimension");
2254 switch (array->expr_type)
2256 case EXPR_VARIABLE:
2257 case EXPR_FUNCTION:
2258 for (ref = array->ref; ref; ref = ref->next)
2260 if (ref->type != REF_ARRAY)
2261 continue;
2263 if (ref->u.ar.type == AR_FULL)
2264 return spec_dimen_size (ref->u.ar.as, dimen, result);
2266 if (ref->u.ar.type == AR_SECTION)
2268 for (i = 0; dimen >= 0; i++)
2269 if (ref->u.ar.dimen_type[i] != DIMEN_ELEMENT)
2270 dimen--;
2272 return gfc_ref_dimen_size (&ref->u.ar, i - 1, result, NULL);
2276 if (array->shape && array->shape[dimen])
2278 mpz_init_set (*result, array->shape[dimen]);
2279 return true;
2282 if (array->symtree->n.sym->attr.generic
2283 && array->value.function.esym != NULL)
2285 if (!spec_dimen_size (array->value.function.esym->as, dimen, result))
2286 return false;
2288 else if (!spec_dimen_size (array->symtree->n.sym->as, dimen, result))
2289 return false;
2291 break;
2293 case EXPR_ARRAY:
2294 if (array->shape == NULL) {
2295 /* Expressions with rank > 1 should have "shape" properly set */
2296 if ( array->rank != 1 )
2297 gfc_internal_error ("gfc_array_dimen_size(): Bad EXPR_ARRAY expr");
2298 return gfc_array_size(array, result);
2301 /* Fall through */
2302 default:
2303 if (array->shape == NULL)
2304 return false;
2306 mpz_init_set (*result, array->shape[dimen]);
2308 break;
2311 return true;
2315 /* Given an array expression, figure out how many elements are in the
2316 array. Returns true if this is possible, and sets the 'result'
2317 variable. Otherwise returns false. */
2319 bool
2320 gfc_array_size (gfc_expr *array, mpz_t *result)
2322 expand_info expand_save;
2323 gfc_ref *ref;
2324 int i;
2325 bool t;
2327 if (array->ts.type == BT_CLASS)
2328 return false;
2330 switch (array->expr_type)
2332 case EXPR_ARRAY:
2333 gfc_push_suppress_errors ();
2335 expand_save = current_expand;
2337 current_expand.count = result;
2338 mpz_init_set_ui (*result, 0);
2340 current_expand.expand_work_function = count_elements;
2341 iter_stack = NULL;
2343 t = expand_constructor (array->value.constructor);
2345 gfc_pop_suppress_errors ();
2347 if (!t)
2348 mpz_clear (*result);
2349 current_expand = expand_save;
2350 return t;
2352 case EXPR_VARIABLE:
2353 for (ref = array->ref; ref; ref = ref->next)
2355 if (ref->type != REF_ARRAY)
2356 continue;
2358 if (ref->u.ar.type == AR_FULL)
2359 return spec_size (ref->u.ar.as, result);
2361 if (ref->u.ar.type == AR_SECTION)
2362 return ref_size (&ref->u.ar, result);
2365 return spec_size (array->symtree->n.sym->as, result);
2368 default:
2369 if (array->rank == 0 || array->shape == NULL)
2370 return false;
2372 mpz_init_set_ui (*result, 1);
2374 for (i = 0; i < array->rank; i++)
2375 mpz_mul (*result, *result, array->shape[i]);
2377 break;
2380 return true;
2384 /* Given an array reference, return the shape of the reference in an
2385 array of mpz_t integers. */
2387 bool
2388 gfc_array_ref_shape (gfc_array_ref *ar, mpz_t *shape)
2390 int d;
2391 int i;
2393 d = 0;
2395 switch (ar->type)
2397 case AR_FULL:
2398 for (; d < ar->as->rank; d++)
2399 if (!spec_dimen_size (ar->as, d, &shape[d]))
2400 goto cleanup;
2402 return true;
2404 case AR_SECTION:
2405 for (i = 0; i < ar->dimen; i++)
2407 if (ar->dimen_type[i] != DIMEN_ELEMENT)
2409 if (!gfc_ref_dimen_size (ar, i, &shape[d], NULL))
2410 goto cleanup;
2411 d++;
2415 return true;
2417 default:
2418 break;
2421 cleanup:
2422 gfc_clear_shape (shape, d);
2423 return false;
2427 /* Given an array expression, find the array reference structure that
2428 characterizes the reference. */
2430 gfc_array_ref *
2431 gfc_find_array_ref (gfc_expr *e)
2433 gfc_ref *ref;
2435 for (ref = e->ref; ref; ref = ref->next)
2436 if (ref->type == REF_ARRAY
2437 && (ref->u.ar.type == AR_FULL || ref->u.ar.type == AR_SECTION))
2438 break;
2440 if (ref == NULL)
2441 gfc_internal_error ("gfc_find_array_ref(): No ref found");
2443 return &ref->u.ar;
2447 /* Find out if an array shape is known at compile time. */
2450 gfc_is_compile_time_shape (gfc_array_spec *as)
2452 int i;
2454 if (as->type != AS_EXPLICIT)
2455 return 0;
2457 for (i = 0; i < as->rank; i++)
2458 if (!gfc_is_constant_expr (as->lower[i])
2459 || !gfc_is_constant_expr (as->upper[i]))
2460 return 0;
2462 return 1;