Daily bump.
[official-gcc.git] / gcc / fortran / array.c
blobf23d0bc7888cfb3fd0d82b16027adb1448bbd257
1 /* Array things
2 Copyright (C) 2000, 2001, 2002, 2004, 2005, 2006, 2007, 2008, 2009, 2010
3 Free Software Foundation, Inc.
4 Contributed by Andy Vaught
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "gfortran.h"
26 #include "match.h"
27 #include "constructor.h"
29 /**************** Array reference matching subroutines *****************/
31 /* Copy an array reference structure. */
33 gfc_array_ref *
34 gfc_copy_array_ref (gfc_array_ref *src)
36 gfc_array_ref *dest;
37 int i;
39 if (src == NULL)
40 return NULL;
42 dest = gfc_get_array_ref ();
44 *dest = *src;
46 for (i = 0; i < GFC_MAX_DIMENSIONS; i++)
48 dest->start[i] = gfc_copy_expr (src->start[i]);
49 dest->end[i] = gfc_copy_expr (src->end[i]);
50 dest->stride[i] = gfc_copy_expr (src->stride[i]);
53 return dest;
57 /* Match a single dimension of an array reference. This can be a
58 single element or an array section. Any modifications we've made
59 to the ar structure are cleaned up by the caller. If the init
60 is set, we require the subscript to be a valid initialization
61 expression. */
63 static match
64 match_subscript (gfc_array_ref *ar, int init, bool match_star)
66 match m = MATCH_ERROR;
67 bool star = false;
68 int i;
70 i = ar->dimen + ar->codimen;
72 gfc_gobble_whitespace ();
73 ar->c_where[i] = gfc_current_locus;
74 ar->start[i] = ar->end[i] = ar->stride[i] = NULL;
76 /* We can't be sure of the difference between DIMEN_ELEMENT and
77 DIMEN_VECTOR until we know the type of the element itself at
78 resolution time. */
80 ar->dimen_type[i] = DIMEN_UNKNOWN;
82 if (gfc_match_char (':') == MATCH_YES)
83 goto end_element;
85 /* Get start element. */
86 if (match_star && (m = gfc_match_char ('*')) == MATCH_YES)
87 star = true;
89 if (!star && init)
90 m = gfc_match_init_expr (&ar->start[i]);
91 else if (!star)
92 m = gfc_match_expr (&ar->start[i]);
94 if (m == MATCH_NO && gfc_match_char ('*') == MATCH_YES)
95 return MATCH_NO;
96 else if (m == MATCH_NO)
97 gfc_error ("Expected array subscript at %C");
98 if (m != MATCH_YES)
99 return MATCH_ERROR;
101 if (gfc_match_char (':') == MATCH_NO)
102 goto matched;
104 if (star)
106 gfc_error ("Unexpected '*' in coarray subscript at %C");
107 return MATCH_ERROR;
110 /* Get an optional end element. Because we've seen the colon, we
111 definitely have a range along this dimension. */
112 end_element:
113 ar->dimen_type[i] = DIMEN_RANGE;
115 if (match_star && (m = gfc_match_char ('*')) == MATCH_YES)
116 star = true;
117 else if (init)
118 m = gfc_match_init_expr (&ar->end[i]);
119 else
120 m = gfc_match_expr (&ar->end[i]);
122 if (m == MATCH_ERROR)
123 return MATCH_ERROR;
125 /* See if we have an optional stride. */
126 if (gfc_match_char (':') == MATCH_YES)
128 if (star)
130 gfc_error ("Strides not allowed in coarray subscript at %C");
131 return MATCH_ERROR;
134 m = init ? gfc_match_init_expr (&ar->stride[i])
135 : gfc_match_expr (&ar->stride[i]);
137 if (m == MATCH_NO)
138 gfc_error ("Expected array subscript stride at %C");
139 if (m != MATCH_YES)
140 return MATCH_ERROR;
143 matched:
144 if (star)
145 ar->dimen_type[i] = DIMEN_STAR;
147 return MATCH_YES;
151 /* Match an array reference, whether it is the whole array or a
152 particular elements or a section. If init is set, the reference has
153 to consist of init expressions. */
155 match
156 gfc_match_array_ref (gfc_array_ref *ar, gfc_array_spec *as, int init,
157 int corank)
159 match m;
160 bool matched_bracket = false;
162 memset (ar, '\0', sizeof (ar));
164 ar->where = gfc_current_locus;
165 ar->as = as;
166 ar->type = AR_UNKNOWN;
168 if (gfc_match_char ('[') == MATCH_YES)
170 matched_bracket = true;
171 goto coarray;
174 if (gfc_match_char ('(') != MATCH_YES)
176 ar->type = AR_FULL;
177 ar->dimen = 0;
178 return MATCH_YES;
181 for (ar->dimen = 0; ar->dimen < GFC_MAX_DIMENSIONS; ar->dimen++)
183 m = match_subscript (ar, init, false);
184 if (m == MATCH_ERROR)
185 return MATCH_ERROR;
187 if (gfc_match_char (')') == MATCH_YES)
189 ar->dimen++;
190 goto coarray;
193 if (gfc_match_char (',') != MATCH_YES)
195 gfc_error ("Invalid form of array reference at %C");
196 return MATCH_ERROR;
200 gfc_error ("Array reference at %C cannot have more than %d dimensions",
201 GFC_MAX_DIMENSIONS);
202 return MATCH_ERROR;
204 coarray:
205 if (!matched_bracket && gfc_match_char ('[') != MATCH_YES)
207 if (ar->dimen > 0)
208 return MATCH_YES;
209 else
210 return MATCH_ERROR;
213 if (gfc_option.coarray == GFC_FCOARRAY_NONE)
215 gfc_fatal_error ("Coarrays disabled at %C, use -fcoarray= to enable");
216 return MATCH_ERROR;
219 if (corank == 0)
221 gfc_error ("Unexpected coarray designator at %C");
222 return MATCH_ERROR;
225 for (ar->codimen = 0; ar->codimen + ar->dimen < GFC_MAX_DIMENSIONS; ar->codimen++)
227 m = match_subscript (ar, init, ar->codimen == (corank - 1));
228 if (m == MATCH_ERROR)
229 return MATCH_ERROR;
231 if (gfc_match_char (']') == MATCH_YES)
233 ar->codimen++;
234 if (ar->codimen < corank)
236 gfc_error ("Too few codimensions at %C, expected %d not %d",
237 corank, ar->codimen);
238 return MATCH_ERROR;
240 if (ar->codimen > corank)
242 gfc_error ("Too many codimensions at %C, expected %d not %d",
243 corank, ar->codimen);
244 return MATCH_ERROR;
246 return MATCH_YES;
249 if (gfc_match_char (',') != MATCH_YES)
251 if (gfc_match_char ('*') == MATCH_YES)
252 gfc_error ("Unexpected '*' for codimension %d of %d at %C",
253 ar->codimen + 1, corank);
254 else
255 gfc_error ("Invalid form of coarray reference at %C");
256 return MATCH_ERROR;
258 if (ar->codimen >= corank)
260 gfc_error ("Invalid codimension %d at %C, only %d codimensions exist",
261 ar->codimen + 1, corank);
262 return MATCH_ERROR;
266 gfc_error ("Array reference at %C cannot have more than %d dimensions",
267 GFC_MAX_DIMENSIONS);
268 return MATCH_ERROR;
273 /************** Array specification matching subroutines ***************/
275 /* Free all of the expressions associated with array bounds
276 specifications. */
278 void
279 gfc_free_array_spec (gfc_array_spec *as)
281 int i;
283 if (as == NULL)
284 return;
286 for (i = 0; i < as->rank + as->corank; i++)
288 gfc_free_expr (as->lower[i]);
289 gfc_free_expr (as->upper[i]);
292 free (as);
296 /* Take an array bound, resolves the expression, that make up the
297 shape and check associated constraints. */
299 static gfc_try
300 resolve_array_bound (gfc_expr *e, int check_constant)
302 if (e == NULL)
303 return SUCCESS;
305 if (gfc_resolve_expr (e) == FAILURE
306 || gfc_specification_expr (e) == FAILURE)
307 return FAILURE;
309 if (check_constant && !gfc_is_constant_expr (e))
311 if (e->expr_type == EXPR_VARIABLE)
312 gfc_error ("Variable '%s' at %L in this context must be constant",
313 e->symtree->n.sym->name, &e->where);
314 else
315 gfc_error ("Expression at %L in this context must be constant",
316 &e->where);
317 return FAILURE;
320 return SUCCESS;
324 /* Takes an array specification, resolves the expressions that make up
325 the shape and make sure everything is integral. */
327 gfc_try
328 gfc_resolve_array_spec (gfc_array_spec *as, int check_constant)
330 gfc_expr *e;
331 int i;
333 if (as == NULL)
334 return SUCCESS;
336 for (i = 0; i < as->rank + as->corank; i++)
338 e = as->lower[i];
339 if (resolve_array_bound (e, check_constant) == FAILURE)
340 return FAILURE;
342 e = as->upper[i];
343 if (resolve_array_bound (e, check_constant) == FAILURE)
344 return FAILURE;
346 if ((as->lower[i] == NULL) || (as->upper[i] == NULL))
347 continue;
349 /* If the size is negative in this dimension, set it to zero. */
350 if (as->lower[i]->expr_type == EXPR_CONSTANT
351 && as->upper[i]->expr_type == EXPR_CONSTANT
352 && mpz_cmp (as->upper[i]->value.integer,
353 as->lower[i]->value.integer) < 0)
355 gfc_free_expr (as->upper[i]);
356 as->upper[i] = gfc_copy_expr (as->lower[i]);
357 mpz_sub_ui (as->upper[i]->value.integer,
358 as->upper[i]->value.integer, 1);
362 return SUCCESS;
366 /* Match a single array element specification. The return values as
367 well as the upper and lower bounds of the array spec are filled
368 in according to what we see on the input. The caller makes sure
369 individual specifications make sense as a whole.
372 Parsed Lower Upper Returned
373 ------------------------------------
374 : NULL NULL AS_DEFERRED (*)
375 x 1 x AS_EXPLICIT
376 x: x NULL AS_ASSUMED_SHAPE
377 x:y x y AS_EXPLICIT
378 x:* x NULL AS_ASSUMED_SIZE
379 * 1 NULL AS_ASSUMED_SIZE
381 (*) For non-pointer dummy arrays this is AS_ASSUMED_SHAPE. This
382 is fixed during the resolution of formal interfaces.
384 Anything else AS_UNKNOWN. */
386 static array_type
387 match_array_element_spec (gfc_array_spec *as)
389 gfc_expr **upper, **lower;
390 match m;
391 int rank;
393 rank = as->rank == -1 ? 0 : as->rank;
394 lower = &as->lower[rank + as->corank - 1];
395 upper = &as->upper[rank + as->corank - 1];
397 if (gfc_match_char ('*') == MATCH_YES)
399 *lower = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
400 return AS_ASSUMED_SIZE;
403 if (gfc_match_char (':') == MATCH_YES)
404 return AS_DEFERRED;
406 m = gfc_match_expr (upper);
407 if (m == MATCH_NO)
408 gfc_error ("Expected expression in array specification at %C");
409 if (m != MATCH_YES)
410 return AS_UNKNOWN;
411 if (gfc_expr_check_typed (*upper, gfc_current_ns, false) == FAILURE)
412 return AS_UNKNOWN;
414 if (gfc_match_char (':') == MATCH_NO)
416 *lower = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
417 return AS_EXPLICIT;
420 *lower = *upper;
421 *upper = NULL;
423 if (gfc_match_char ('*') == MATCH_YES)
424 return AS_ASSUMED_SIZE;
426 m = gfc_match_expr (upper);
427 if (m == MATCH_ERROR)
428 return AS_UNKNOWN;
429 if (m == MATCH_NO)
430 return AS_ASSUMED_SHAPE;
431 if (gfc_expr_check_typed (*upper, gfc_current_ns, false) == FAILURE)
432 return AS_UNKNOWN;
434 return AS_EXPLICIT;
438 /* Matches an array specification, incidentally figuring out what sort
439 it is. Match either a normal array specification, or a coarray spec
440 or both. Optionally allow [:] for coarrays. */
442 match
443 gfc_match_array_spec (gfc_array_spec **asp, bool match_dim, bool match_codim)
445 array_type current_type;
446 gfc_array_spec *as;
447 int i;
449 as = gfc_get_array_spec ();
451 if (!match_dim)
452 goto coarray;
454 if (gfc_match_char ('(') != MATCH_YES)
456 if (!match_codim)
457 goto done;
458 goto coarray;
461 if (gfc_match (" .. )") == MATCH_YES)
463 as->type = AS_ASSUMED_RANK;
464 as->rank = -1;
466 if (gfc_notify_std (GFC_STD_F2008_TS, "Assumed-rank array at %C")
467 == FAILURE)
468 goto cleanup;
470 if (!match_codim)
471 goto done;
472 goto coarray;
475 for (;;)
477 as->rank++;
478 current_type = match_array_element_spec (as);
480 /* Note that current_type == AS_ASSUMED_SIZE for both assumed-size
481 and implied-shape specifications. If the rank is at least 2, we can
482 distinguish between them. But for rank 1, we currently return
483 ASSUMED_SIZE; this gets adjusted later when we know for sure
484 whether the symbol parsed is a PARAMETER or not. */
486 if (as->rank == 1)
488 if (current_type == AS_UNKNOWN)
489 goto cleanup;
490 as->type = current_type;
492 else
493 switch (as->type)
494 { /* See how current spec meshes with the existing. */
495 case AS_UNKNOWN:
496 goto cleanup;
498 case AS_IMPLIED_SHAPE:
499 if (current_type != AS_ASSUMED_SHAPE)
501 gfc_error ("Bad array specification for implied-shape"
502 " array at %C");
503 goto cleanup;
505 break;
507 case AS_EXPLICIT:
508 if (current_type == AS_ASSUMED_SIZE)
510 as->type = AS_ASSUMED_SIZE;
511 break;
514 if (current_type == AS_EXPLICIT)
515 break;
517 gfc_error ("Bad array specification for an explicitly shaped "
518 "array at %C");
520 goto cleanup;
522 case AS_ASSUMED_SHAPE:
523 if ((current_type == AS_ASSUMED_SHAPE)
524 || (current_type == AS_DEFERRED))
525 break;
527 gfc_error ("Bad array specification for assumed shape "
528 "array at %C");
529 goto cleanup;
531 case AS_DEFERRED:
532 if (current_type == AS_DEFERRED)
533 break;
535 if (current_type == AS_ASSUMED_SHAPE)
537 as->type = AS_ASSUMED_SHAPE;
538 break;
541 gfc_error ("Bad specification for deferred shape array at %C");
542 goto cleanup;
544 case AS_ASSUMED_SIZE:
545 if (as->rank == 2 && current_type == AS_ASSUMED_SIZE)
547 as->type = AS_IMPLIED_SHAPE;
548 break;
551 gfc_error ("Bad specification for assumed size array at %C");
552 goto cleanup;
554 case AS_ASSUMED_RANK:
555 gcc_unreachable ();
558 if (gfc_match_char (')') == MATCH_YES)
559 break;
561 if (gfc_match_char (',') != MATCH_YES)
563 gfc_error ("Expected another dimension in array declaration at %C");
564 goto cleanup;
567 if (as->rank + as->corank >= GFC_MAX_DIMENSIONS)
569 gfc_error ("Array specification at %C has more than %d dimensions",
570 GFC_MAX_DIMENSIONS);
571 goto cleanup;
574 if (as->corank + as->rank >= 7
575 && gfc_notify_std (GFC_STD_F2008, "Array "
576 "specification at %C with more than 7 dimensions")
577 == FAILURE)
578 goto cleanup;
581 if (!match_codim)
582 goto done;
584 coarray:
585 if (gfc_match_char ('[') != MATCH_YES)
586 goto done;
588 if (gfc_notify_std (GFC_STD_F2008, "Coarray declaration at %C")
589 == FAILURE)
590 goto cleanup;
592 if (gfc_option.coarray == GFC_FCOARRAY_NONE)
594 gfc_fatal_error ("Coarrays disabled at %C, use -fcoarray= to enable");
595 goto cleanup;
598 if (as->rank >= GFC_MAX_DIMENSIONS)
600 gfc_error ("Array specification at %C has more than %d "
601 "dimensions", GFC_MAX_DIMENSIONS);
602 goto cleanup;
605 for (;;)
607 as->corank++;
608 current_type = match_array_element_spec (as);
610 if (current_type == AS_UNKNOWN)
611 goto cleanup;
613 if (as->corank == 1)
614 as->cotype = current_type;
615 else
616 switch (as->cotype)
617 { /* See how current spec meshes with the existing. */
618 case AS_IMPLIED_SHAPE:
619 case AS_UNKNOWN:
620 goto cleanup;
622 case AS_EXPLICIT:
623 if (current_type == AS_ASSUMED_SIZE)
625 as->cotype = AS_ASSUMED_SIZE;
626 break;
629 if (current_type == AS_EXPLICIT)
630 break;
632 gfc_error ("Bad array specification for an explicitly "
633 "shaped array at %C");
635 goto cleanup;
637 case AS_ASSUMED_SHAPE:
638 if ((current_type == AS_ASSUMED_SHAPE)
639 || (current_type == AS_DEFERRED))
640 break;
642 gfc_error ("Bad array specification for assumed shape "
643 "array at %C");
644 goto cleanup;
646 case AS_DEFERRED:
647 if (current_type == AS_DEFERRED)
648 break;
650 if (current_type == AS_ASSUMED_SHAPE)
652 as->cotype = AS_ASSUMED_SHAPE;
653 break;
656 gfc_error ("Bad specification for deferred shape array at %C");
657 goto cleanup;
659 case AS_ASSUMED_SIZE:
660 gfc_error ("Bad specification for assumed size array at %C");
661 goto cleanup;
663 case AS_ASSUMED_RANK:
664 gcc_unreachable ();
667 if (gfc_match_char (']') == MATCH_YES)
668 break;
670 if (gfc_match_char (',') != MATCH_YES)
672 gfc_error ("Expected another dimension in array declaration at %C");
673 goto cleanup;
676 if (as->rank + as->corank >= GFC_MAX_DIMENSIONS)
678 gfc_error ("Array specification at %C has more than %d "
679 "dimensions", GFC_MAX_DIMENSIONS);
680 goto cleanup;
684 if (current_type == AS_EXPLICIT)
686 gfc_error ("Upper bound of last coarray dimension must be '*' at %C");
687 goto cleanup;
690 if (as->cotype == AS_ASSUMED_SIZE)
691 as->cotype = AS_EXPLICIT;
693 if (as->rank == 0)
694 as->type = as->cotype;
696 done:
697 if (as->rank == 0 && as->corank == 0)
699 *asp = NULL;
700 gfc_free_array_spec (as);
701 return MATCH_NO;
704 /* If a lower bounds of an assumed shape array is blank, put in one. */
705 if (as->type == AS_ASSUMED_SHAPE)
707 for (i = 0; i < as->rank + as->corank; i++)
709 if (as->lower[i] == NULL)
710 as->lower[i] = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
714 *asp = as;
716 return MATCH_YES;
718 cleanup:
719 /* Something went wrong. */
720 gfc_free_array_spec (as);
721 return MATCH_ERROR;
725 /* Given a symbol and an array specification, modify the symbol to
726 have that array specification. The error locus is needed in case
727 something goes wrong. On failure, the caller must free the spec. */
729 gfc_try
730 gfc_set_array_spec (gfc_symbol *sym, gfc_array_spec *as, locus *error_loc)
732 int i;
734 if (as == NULL)
735 return SUCCESS;
737 if (as->rank
738 && gfc_add_dimension (&sym->attr, sym->name, error_loc) == FAILURE)
739 return FAILURE;
741 if (as->corank
742 && gfc_add_codimension (&sym->attr, sym->name, error_loc) == FAILURE)
743 return FAILURE;
745 if (sym->as == NULL)
747 sym->as = as;
748 return SUCCESS;
751 if ((sym->as->type == AS_ASSUMED_RANK && as->corank)
752 || (as->type == AS_ASSUMED_RANK && sym->as->corank))
754 gfc_error ("The assumed-rank array '%s' at %L shall not have a "
755 "codimension", sym->name, error_loc);
756 return FAILURE;
759 if (as->corank)
761 /* The "sym" has no corank (checked via gfc_add_codimension). Thus
762 the codimension is simply added. */
763 gcc_assert (as->rank == 0 && sym->as->corank == 0);
765 sym->as->cotype = as->cotype;
766 sym->as->corank = as->corank;
767 for (i = 0; i < as->corank; i++)
769 sym->as->lower[sym->as->rank + i] = as->lower[i];
770 sym->as->upper[sym->as->rank + i] = as->upper[i];
773 else
775 /* The "sym" has no rank (checked via gfc_add_dimension). Thus
776 the dimension is added - but first the codimensions (if existing
777 need to be shifted to make space for the dimension. */
778 gcc_assert (as->corank == 0 && sym->as->rank == 0);
780 sym->as->rank = as->rank;
781 sym->as->type = as->type;
782 sym->as->cray_pointee = as->cray_pointee;
783 sym->as->cp_was_assumed = as->cp_was_assumed;
785 for (i = 0; i < sym->as->corank; i++)
787 sym->as->lower[as->rank + i] = sym->as->lower[i];
788 sym->as->upper[as->rank + i] = sym->as->upper[i];
790 for (i = 0; i < as->rank; i++)
792 sym->as->lower[i] = as->lower[i];
793 sym->as->upper[i] = as->upper[i];
797 free (as);
798 return SUCCESS;
802 /* Copy an array specification. */
804 gfc_array_spec *
805 gfc_copy_array_spec (gfc_array_spec *src)
807 gfc_array_spec *dest;
808 int i;
810 if (src == NULL)
811 return NULL;
813 dest = gfc_get_array_spec ();
815 *dest = *src;
817 for (i = 0; i < dest->rank + dest->corank; i++)
819 dest->lower[i] = gfc_copy_expr (dest->lower[i]);
820 dest->upper[i] = gfc_copy_expr (dest->upper[i]);
823 return dest;
827 /* Returns nonzero if the two expressions are equal. Only handles integer
828 constants. */
830 static int
831 compare_bounds (gfc_expr *bound1, gfc_expr *bound2)
833 if (bound1 == NULL || bound2 == NULL
834 || bound1->expr_type != EXPR_CONSTANT
835 || bound2->expr_type != EXPR_CONSTANT
836 || bound1->ts.type != BT_INTEGER
837 || bound2->ts.type != BT_INTEGER)
838 gfc_internal_error ("gfc_compare_array_spec(): Array spec clobbered");
840 if (mpz_cmp (bound1->value.integer, bound2->value.integer) == 0)
841 return 1;
842 else
843 return 0;
847 /* Compares two array specifications. They must be constant or deferred
848 shape. */
851 gfc_compare_array_spec (gfc_array_spec *as1, gfc_array_spec *as2)
853 int i;
855 if (as1 == NULL && as2 == NULL)
856 return 1;
858 if (as1 == NULL || as2 == NULL)
859 return 0;
861 if (as1->rank != as2->rank)
862 return 0;
864 if (as1->corank != as2->corank)
865 return 0;
867 if (as1->rank == 0)
868 return 1;
870 if (as1->type != as2->type)
871 return 0;
873 if (as1->type == AS_EXPLICIT)
874 for (i = 0; i < as1->rank + as1->corank; i++)
876 if (compare_bounds (as1->lower[i], as2->lower[i]) == 0)
877 return 0;
879 if (compare_bounds (as1->upper[i], as2->upper[i]) == 0)
880 return 0;
883 return 1;
887 /****************** Array constructor functions ******************/
890 /* Given an expression node that might be an array constructor and a
891 symbol, make sure that no iterators in this or child constructors
892 use the symbol as an implied-DO iterator. Returns nonzero if a
893 duplicate was found. */
895 static int
896 check_duplicate_iterator (gfc_constructor_base base, gfc_symbol *master)
898 gfc_constructor *c;
899 gfc_expr *e;
901 for (c = gfc_constructor_first (base); c; c = gfc_constructor_next (c))
903 e = c->expr;
905 if (e->expr_type == EXPR_ARRAY
906 && check_duplicate_iterator (e->value.constructor, master))
907 return 1;
909 if (c->iterator == NULL)
910 continue;
912 if (c->iterator->var->symtree->n.sym == master)
914 gfc_error ("DO-iterator '%s' at %L is inside iterator of the "
915 "same name", master->name, &c->where);
917 return 1;
921 return 0;
925 /* Forward declaration because these functions are mutually recursive. */
926 static match match_array_cons_element (gfc_constructor_base *);
928 /* Match a list of array elements. */
930 static match
931 match_array_list (gfc_constructor_base *result)
933 gfc_constructor_base head;
934 gfc_constructor *p;
935 gfc_iterator iter;
936 locus old_loc;
937 gfc_expr *e;
938 match m;
939 int n;
941 old_loc = gfc_current_locus;
943 if (gfc_match_char ('(') == MATCH_NO)
944 return MATCH_NO;
946 memset (&iter, '\0', sizeof (gfc_iterator));
947 head = NULL;
949 m = match_array_cons_element (&head);
950 if (m != MATCH_YES)
951 goto cleanup;
953 if (gfc_match_char (',') != MATCH_YES)
955 m = MATCH_NO;
956 goto cleanup;
959 for (n = 1;; n++)
961 m = gfc_match_iterator (&iter, 0);
962 if (m == MATCH_YES)
963 break;
964 if (m == MATCH_ERROR)
965 goto cleanup;
967 m = match_array_cons_element (&head);
968 if (m == MATCH_ERROR)
969 goto cleanup;
970 if (m == MATCH_NO)
972 if (n > 2)
973 goto syntax;
974 m = MATCH_NO;
975 goto cleanup; /* Could be a complex constant */
978 if (gfc_match_char (',') != MATCH_YES)
980 if (n > 2)
981 goto syntax;
982 m = MATCH_NO;
983 goto cleanup;
987 if (gfc_match_char (')') != MATCH_YES)
988 goto syntax;
990 if (check_duplicate_iterator (head, iter.var->symtree->n.sym))
992 m = MATCH_ERROR;
993 goto cleanup;
996 e = gfc_get_array_expr (BT_UNKNOWN, 0, &old_loc);
997 e->value.constructor = head;
999 p = gfc_constructor_append_expr (result, e, &gfc_current_locus);
1000 p->iterator = gfc_get_iterator ();
1001 *p->iterator = iter;
1003 return MATCH_YES;
1005 syntax:
1006 gfc_error ("Syntax error in array constructor at %C");
1007 m = MATCH_ERROR;
1009 cleanup:
1010 gfc_constructor_free (head);
1011 gfc_free_iterator (&iter, 0);
1012 gfc_current_locus = old_loc;
1013 return m;
1017 /* Match a single element of an array constructor, which can be a
1018 single expression or a list of elements. */
1020 static match
1021 match_array_cons_element (gfc_constructor_base *result)
1023 gfc_expr *expr;
1024 match m;
1026 m = match_array_list (result);
1027 if (m != MATCH_NO)
1028 return m;
1030 m = gfc_match_expr (&expr);
1031 if (m != MATCH_YES)
1032 return m;
1034 gfc_constructor_append_expr (result, expr, &gfc_current_locus);
1035 return MATCH_YES;
1039 /* Match an array constructor. */
1041 match
1042 gfc_match_array_constructor (gfc_expr **result)
1044 gfc_constructor_base head, new_cons;
1045 gfc_expr *expr;
1046 gfc_typespec ts;
1047 locus where;
1048 match m;
1049 const char *end_delim;
1050 bool seen_ts;
1052 if (gfc_match (" (/") == MATCH_NO)
1054 if (gfc_match (" [") == MATCH_NO)
1055 return MATCH_NO;
1056 else
1058 if (gfc_notify_std (GFC_STD_F2003, "[...] "
1059 "style array constructors at %C") == FAILURE)
1060 return MATCH_ERROR;
1061 end_delim = " ]";
1064 else
1065 end_delim = " /)";
1067 where = gfc_current_locus;
1068 head = new_cons = NULL;
1069 seen_ts = false;
1071 /* Try to match an optional "type-spec ::" */
1072 if (gfc_match_decl_type_spec (&ts, 0) == MATCH_YES)
1074 seen_ts = (gfc_match (" ::") == MATCH_YES);
1076 if (seen_ts)
1078 if (gfc_notify_std (GFC_STD_F2003, "Array constructor "
1079 "including type specification at %C") == FAILURE)
1080 goto cleanup;
1082 if (ts.deferred)
1084 gfc_error ("Type-spec at %L cannot contain a deferred "
1085 "type parameter", &where);
1086 goto cleanup;
1091 if (! seen_ts)
1092 gfc_current_locus = where;
1094 if (gfc_match (end_delim) == MATCH_YES)
1096 if (seen_ts)
1097 goto done;
1098 else
1100 gfc_error ("Empty array constructor at %C is not allowed");
1101 goto cleanup;
1105 for (;;)
1107 m = match_array_cons_element (&head);
1108 if (m == MATCH_ERROR)
1109 goto cleanup;
1110 if (m == MATCH_NO)
1111 goto syntax;
1113 if (gfc_match_char (',') == MATCH_NO)
1114 break;
1117 if (gfc_match (end_delim) == MATCH_NO)
1118 goto syntax;
1120 done:
1121 /* Size must be calculated at resolution time. */
1122 if (seen_ts)
1124 expr = gfc_get_array_expr (ts.type, ts.kind, &where);
1125 expr->ts = ts;
1127 else
1128 expr = gfc_get_array_expr (BT_UNKNOWN, 0, &where);
1130 expr->value.constructor = head;
1131 if (expr->ts.u.cl)
1132 expr->ts.u.cl->length_from_typespec = seen_ts;
1134 *result = expr;
1135 return MATCH_YES;
1137 syntax:
1138 gfc_error ("Syntax error in array constructor at %C");
1140 cleanup:
1141 gfc_constructor_free (head);
1142 return MATCH_ERROR;
1147 /************** Check array constructors for correctness **************/
1149 /* Given an expression, compare it's type with the type of the current
1150 constructor. Returns nonzero if an error was issued. The
1151 cons_state variable keeps track of whether the type of the
1152 constructor being read or resolved is known to be good, bad or just
1153 starting out. */
1155 static gfc_typespec constructor_ts;
1156 static enum
1157 { CONS_START, CONS_GOOD, CONS_BAD }
1158 cons_state;
1160 static int
1161 check_element_type (gfc_expr *expr, bool convert)
1163 if (cons_state == CONS_BAD)
1164 return 0; /* Suppress further errors */
1166 if (cons_state == CONS_START)
1168 if (expr->ts.type == BT_UNKNOWN)
1169 cons_state = CONS_BAD;
1170 else
1172 cons_state = CONS_GOOD;
1173 constructor_ts = expr->ts;
1176 return 0;
1179 if (gfc_compare_types (&constructor_ts, &expr->ts))
1180 return 0;
1182 if (convert)
1183 return gfc_convert_type (expr, &constructor_ts, 1) == SUCCESS ? 0 : 1;
1185 gfc_error ("Element in %s array constructor at %L is %s",
1186 gfc_typename (&constructor_ts), &expr->where,
1187 gfc_typename (&expr->ts));
1189 cons_state = CONS_BAD;
1190 return 1;
1194 /* Recursive work function for gfc_check_constructor_type(). */
1196 static gfc_try
1197 check_constructor_type (gfc_constructor_base base, bool convert)
1199 gfc_constructor *c;
1200 gfc_expr *e;
1202 for (c = gfc_constructor_first (base); c; c = gfc_constructor_next (c))
1204 e = c->expr;
1206 if (e->expr_type == EXPR_ARRAY)
1208 if (check_constructor_type (e->value.constructor, convert) == FAILURE)
1209 return FAILURE;
1211 continue;
1214 if (check_element_type (e, convert))
1215 return FAILURE;
1218 return SUCCESS;
1222 /* Check that all elements of an array constructor are the same type.
1223 On FAILURE, an error has been generated. */
1225 gfc_try
1226 gfc_check_constructor_type (gfc_expr *e)
1228 gfc_try t;
1230 if (e->ts.type != BT_UNKNOWN)
1232 cons_state = CONS_GOOD;
1233 constructor_ts = e->ts;
1235 else
1237 cons_state = CONS_START;
1238 gfc_clear_ts (&constructor_ts);
1241 /* If e->ts.type != BT_UNKNOWN, the array constructor included a
1242 typespec, and we will now convert the values on the fly. */
1243 t = check_constructor_type (e->value.constructor, e->ts.type != BT_UNKNOWN);
1244 if (t == SUCCESS && e->ts.type == BT_UNKNOWN)
1245 e->ts = constructor_ts;
1247 return t;
1252 typedef struct cons_stack
1254 gfc_iterator *iterator;
1255 struct cons_stack *previous;
1257 cons_stack;
1259 static cons_stack *base;
1261 static gfc_try check_constructor (gfc_constructor_base, gfc_try (*) (gfc_expr *));
1263 /* Check an EXPR_VARIABLE expression in a constructor to make sure
1264 that that variable is an iteration variables. */
1266 gfc_try
1267 gfc_check_iter_variable (gfc_expr *expr)
1269 gfc_symbol *sym;
1270 cons_stack *c;
1272 sym = expr->symtree->n.sym;
1274 for (c = base; c && c->iterator; c = c->previous)
1275 if (sym == c->iterator->var->symtree->n.sym)
1276 return SUCCESS;
1278 return FAILURE;
1282 /* Recursive work function for gfc_check_constructor(). This amounts
1283 to calling the check function for each expression in the
1284 constructor, giving variables with the names of iterators a pass. */
1286 static gfc_try
1287 check_constructor (gfc_constructor_base ctor, gfc_try (*check_function) (gfc_expr *))
1289 cons_stack element;
1290 gfc_expr *e;
1291 gfc_try t;
1292 gfc_constructor *c;
1294 for (c = gfc_constructor_first (ctor); c; c = gfc_constructor_next (c))
1296 e = c->expr;
1298 if (e->expr_type != EXPR_ARRAY)
1300 if ((*check_function) (e) == FAILURE)
1301 return FAILURE;
1302 continue;
1305 element.previous = base;
1306 element.iterator = c->iterator;
1308 base = &element;
1309 t = check_constructor (e->value.constructor, check_function);
1310 base = element.previous;
1312 if (t == FAILURE)
1313 return FAILURE;
1316 /* Nothing went wrong, so all OK. */
1317 return SUCCESS;
1321 /* Checks a constructor to see if it is a particular kind of
1322 expression -- specification, restricted, or initialization as
1323 determined by the check_function. */
1325 gfc_try
1326 gfc_check_constructor (gfc_expr *expr, gfc_try (*check_function) (gfc_expr *))
1328 cons_stack *base_save;
1329 gfc_try t;
1331 base_save = base;
1332 base = NULL;
1334 t = check_constructor (expr->value.constructor, check_function);
1335 base = base_save;
1337 return t;
1342 /**************** Simplification of array constructors ****************/
1344 iterator_stack *iter_stack;
1346 typedef struct
1348 gfc_constructor_base base;
1349 int extract_count, extract_n;
1350 gfc_expr *extracted;
1351 mpz_t *count;
1353 mpz_t *offset;
1354 gfc_component *component;
1355 mpz_t *repeat;
1357 gfc_try (*expand_work_function) (gfc_expr *);
1359 expand_info;
1361 static expand_info current_expand;
1363 static gfc_try expand_constructor (gfc_constructor_base);
1366 /* Work function that counts the number of elements present in a
1367 constructor. */
1369 static gfc_try
1370 count_elements (gfc_expr *e)
1372 mpz_t result;
1374 if (e->rank == 0)
1375 mpz_add_ui (*current_expand.count, *current_expand.count, 1);
1376 else
1378 if (gfc_array_size (e, &result) == FAILURE)
1380 gfc_free_expr (e);
1381 return FAILURE;
1384 mpz_add (*current_expand.count, *current_expand.count, result);
1385 mpz_clear (result);
1388 gfc_free_expr (e);
1389 return SUCCESS;
1393 /* Work function that extracts a particular element from an array
1394 constructor, freeing the rest. */
1396 static gfc_try
1397 extract_element (gfc_expr *e)
1399 if (e->rank != 0)
1400 { /* Something unextractable */
1401 gfc_free_expr (e);
1402 return FAILURE;
1405 if (current_expand.extract_count == current_expand.extract_n)
1406 current_expand.extracted = e;
1407 else
1408 gfc_free_expr (e);
1410 current_expand.extract_count++;
1412 return SUCCESS;
1416 /* Work function that constructs a new constructor out of the old one,
1417 stringing new elements together. */
1419 static gfc_try
1420 expand (gfc_expr *e)
1422 gfc_constructor *c = gfc_constructor_append_expr (&current_expand.base,
1423 e, &e->where);
1425 c->n.component = current_expand.component;
1426 return SUCCESS;
1430 /* Given an initialization expression that is a variable reference,
1431 substitute the current value of the iteration variable. */
1433 void
1434 gfc_simplify_iterator_var (gfc_expr *e)
1436 iterator_stack *p;
1438 for (p = iter_stack; p; p = p->prev)
1439 if (e->symtree == p->variable)
1440 break;
1442 if (p == NULL)
1443 return; /* Variable not found */
1445 gfc_replace_expr (e, gfc_get_int_expr (gfc_default_integer_kind, NULL, 0));
1447 mpz_set (e->value.integer, p->value);
1449 return;
1453 /* Expand an expression with that is inside of a constructor,
1454 recursing into other constructors if present. */
1456 static gfc_try
1457 expand_expr (gfc_expr *e)
1459 if (e->expr_type == EXPR_ARRAY)
1460 return expand_constructor (e->value.constructor);
1462 e = gfc_copy_expr (e);
1464 if (gfc_simplify_expr (e, 1) == FAILURE)
1466 gfc_free_expr (e);
1467 return FAILURE;
1470 return current_expand.expand_work_function (e);
1474 static gfc_try
1475 expand_iterator (gfc_constructor *c)
1477 gfc_expr *start, *end, *step;
1478 iterator_stack frame;
1479 mpz_t trip;
1480 gfc_try t;
1482 end = step = NULL;
1484 t = FAILURE;
1486 mpz_init (trip);
1487 mpz_init (frame.value);
1488 frame.prev = NULL;
1490 start = gfc_copy_expr (c->iterator->start);
1491 if (gfc_simplify_expr (start, 1) == FAILURE)
1492 goto cleanup;
1494 if (start->expr_type != EXPR_CONSTANT || start->ts.type != BT_INTEGER)
1495 goto cleanup;
1497 end = gfc_copy_expr (c->iterator->end);
1498 if (gfc_simplify_expr (end, 1) == FAILURE)
1499 goto cleanup;
1501 if (end->expr_type != EXPR_CONSTANT || end->ts.type != BT_INTEGER)
1502 goto cleanup;
1504 step = gfc_copy_expr (c->iterator->step);
1505 if (gfc_simplify_expr (step, 1) == FAILURE)
1506 goto cleanup;
1508 if (step->expr_type != EXPR_CONSTANT || step->ts.type != BT_INTEGER)
1509 goto cleanup;
1511 if (mpz_sgn (step->value.integer) == 0)
1513 gfc_error ("Iterator step at %L cannot be zero", &step->where);
1514 goto cleanup;
1517 /* Calculate the trip count of the loop. */
1518 mpz_sub (trip, end->value.integer, start->value.integer);
1519 mpz_add (trip, trip, step->value.integer);
1520 mpz_tdiv_q (trip, trip, step->value.integer);
1522 mpz_set (frame.value, start->value.integer);
1524 frame.prev = iter_stack;
1525 frame.variable = c->iterator->var->symtree;
1526 iter_stack = &frame;
1528 while (mpz_sgn (trip) > 0)
1530 if (expand_expr (c->expr) == FAILURE)
1531 goto cleanup;
1533 mpz_add (frame.value, frame.value, step->value.integer);
1534 mpz_sub_ui (trip, trip, 1);
1537 t = SUCCESS;
1539 cleanup:
1540 gfc_free_expr (start);
1541 gfc_free_expr (end);
1542 gfc_free_expr (step);
1544 mpz_clear (trip);
1545 mpz_clear (frame.value);
1547 iter_stack = frame.prev;
1549 return t;
1553 /* Expand a constructor into constant constructors without any
1554 iterators, calling the work function for each of the expanded
1555 expressions. The work function needs to either save or free the
1556 passed expression. */
1558 static gfc_try
1559 expand_constructor (gfc_constructor_base base)
1561 gfc_constructor *c;
1562 gfc_expr *e;
1564 for (c = gfc_constructor_first (base); c; c = gfc_constructor_next(c))
1566 if (c->iterator != NULL)
1568 if (expand_iterator (c) == FAILURE)
1569 return FAILURE;
1570 continue;
1573 e = c->expr;
1575 if (e->expr_type == EXPR_ARRAY)
1577 if (expand_constructor (e->value.constructor) == FAILURE)
1578 return FAILURE;
1580 continue;
1583 e = gfc_copy_expr (e);
1584 if (gfc_simplify_expr (e, 1) == FAILURE)
1586 gfc_free_expr (e);
1587 return FAILURE;
1589 current_expand.offset = &c->offset;
1590 current_expand.repeat = &c->repeat;
1591 current_expand.component = c->n.component;
1592 if (current_expand.expand_work_function (e) == FAILURE)
1593 return FAILURE;
1595 return SUCCESS;
1599 /* Given an array expression and an element number (starting at zero),
1600 return a pointer to the array element. NULL is returned if the
1601 size of the array has been exceeded. The expression node returned
1602 remains a part of the array and should not be freed. Access is not
1603 efficient at all, but this is another place where things do not
1604 have to be particularly fast. */
1606 static gfc_expr *
1607 gfc_get_array_element (gfc_expr *array, int element)
1609 expand_info expand_save;
1610 gfc_expr *e;
1611 gfc_try rc;
1613 expand_save = current_expand;
1614 current_expand.extract_n = element;
1615 current_expand.expand_work_function = extract_element;
1616 current_expand.extracted = NULL;
1617 current_expand.extract_count = 0;
1619 iter_stack = NULL;
1621 rc = expand_constructor (array->value.constructor);
1622 e = current_expand.extracted;
1623 current_expand = expand_save;
1625 if (rc == FAILURE)
1626 return NULL;
1628 return e;
1632 /* Top level subroutine for expanding constructors. We only expand
1633 constructor if they are small enough. */
1635 gfc_try
1636 gfc_expand_constructor (gfc_expr *e, bool fatal)
1638 expand_info expand_save;
1639 gfc_expr *f;
1640 gfc_try rc;
1642 /* If we can successfully get an array element at the max array size then
1643 the array is too big to expand, so we just return. */
1644 f = gfc_get_array_element (e, gfc_option.flag_max_array_constructor);
1645 if (f != NULL)
1647 gfc_free_expr (f);
1648 if (fatal)
1650 gfc_error ("The number of elements in the array constructor "
1651 "at %L requires an increase of the allowed %d "
1652 "upper limit. See -fmax-array-constructor "
1653 "option", &e->where,
1654 gfc_option.flag_max_array_constructor);
1655 return FAILURE;
1657 return SUCCESS;
1660 /* We now know the array is not too big so go ahead and try to expand it. */
1661 expand_save = current_expand;
1662 current_expand.base = NULL;
1664 iter_stack = NULL;
1666 current_expand.expand_work_function = expand;
1668 if (expand_constructor (e->value.constructor) == FAILURE)
1670 gfc_constructor_free (current_expand.base);
1671 rc = FAILURE;
1672 goto done;
1675 gfc_constructor_free (e->value.constructor);
1676 e->value.constructor = current_expand.base;
1678 rc = SUCCESS;
1680 done:
1681 current_expand = expand_save;
1683 return rc;
1687 /* Work function for checking that an element of a constructor is a
1688 constant, after removal of any iteration variables. We return
1689 FAILURE if not so. */
1691 static gfc_try
1692 is_constant_element (gfc_expr *e)
1694 int rv;
1696 rv = gfc_is_constant_expr (e);
1697 gfc_free_expr (e);
1699 return rv ? SUCCESS : FAILURE;
1703 /* Given an array constructor, determine if the constructor is
1704 constant or not by expanding it and making sure that all elements
1705 are constants. This is a bit of a hack since something like (/ (i,
1706 i=1,100000000) /) will take a while as* opposed to a more clever
1707 function that traverses the expression tree. FIXME. */
1710 gfc_constant_ac (gfc_expr *e)
1712 expand_info expand_save;
1713 gfc_try rc;
1715 iter_stack = NULL;
1716 expand_save = current_expand;
1717 current_expand.expand_work_function = is_constant_element;
1719 rc = expand_constructor (e->value.constructor);
1721 current_expand = expand_save;
1722 if (rc == FAILURE)
1723 return 0;
1725 return 1;
1729 /* Returns nonzero if an array constructor has been completely
1730 expanded (no iterators) and zero if iterators are present. */
1733 gfc_expanded_ac (gfc_expr *e)
1735 gfc_constructor *c;
1737 if (e->expr_type == EXPR_ARRAY)
1738 for (c = gfc_constructor_first (e->value.constructor);
1739 c; c = gfc_constructor_next (c))
1740 if (c->iterator != NULL || !gfc_expanded_ac (c->expr))
1741 return 0;
1743 return 1;
1747 /*************** Type resolution of array constructors ***************/
1750 /* The symbol expr_is_sought_symbol_ref will try to find. */
1751 static const gfc_symbol *sought_symbol = NULL;
1754 /* Tells whether the expression E is a variable reference to the symbol
1755 in the static variable SOUGHT_SYMBOL, and sets the locus pointer WHERE
1756 accordingly.
1757 To be used with gfc_expr_walker: if a reference is found we don't need
1758 to look further so we return 1 to skip any further walk. */
1760 static int
1761 expr_is_sought_symbol_ref (gfc_expr **e, int *walk_subtrees ATTRIBUTE_UNUSED,
1762 void *where)
1764 gfc_expr *expr = *e;
1765 locus *sym_loc = (locus *)where;
1767 if (expr->expr_type == EXPR_VARIABLE
1768 && expr->symtree->n.sym == sought_symbol)
1770 *sym_loc = expr->where;
1771 return 1;
1774 return 0;
1778 /* Tells whether the expression EXPR contains a reference to the symbol
1779 SYM and in that case sets the position SYM_LOC where the reference is. */
1781 static bool
1782 find_symbol_in_expr (gfc_symbol *sym, gfc_expr *expr, locus *sym_loc)
1784 int ret;
1786 sought_symbol = sym;
1787 ret = gfc_expr_walker (&expr, &expr_is_sought_symbol_ref, sym_loc);
1788 sought_symbol = NULL;
1789 return ret;
1793 /* Recursive array list resolution function. All of the elements must
1794 be of the same type. */
1796 static gfc_try
1797 resolve_array_list (gfc_constructor_base base)
1799 gfc_try t;
1800 gfc_constructor *c;
1801 gfc_iterator *iter;
1803 t = SUCCESS;
1805 for (c = gfc_constructor_first (base); c; c = gfc_constructor_next (c))
1807 iter = c->iterator;
1808 if (iter != NULL)
1810 gfc_symbol *iter_var;
1811 locus iter_var_loc;
1813 if (gfc_resolve_iterator (iter, false) == FAILURE)
1814 t = FAILURE;
1816 /* Check for bounds referencing the iterator variable. */
1817 gcc_assert (iter->var->expr_type == EXPR_VARIABLE);
1818 iter_var = iter->var->symtree->n.sym;
1819 if (find_symbol_in_expr (iter_var, iter->start, &iter_var_loc))
1821 if (gfc_notify_std (GFC_STD_LEGACY, "AC-IMPLIED-DO initial "
1822 "expression references control variable "
1823 "at %L", &iter_var_loc) == FAILURE)
1824 t = FAILURE;
1826 if (find_symbol_in_expr (iter_var, iter->end, &iter_var_loc))
1828 if (gfc_notify_std (GFC_STD_LEGACY, "AC-IMPLIED-DO final "
1829 "expression references control variable "
1830 "at %L", &iter_var_loc) == FAILURE)
1831 t = FAILURE;
1833 if (find_symbol_in_expr (iter_var, iter->step, &iter_var_loc))
1835 if (gfc_notify_std (GFC_STD_LEGACY, "AC-IMPLIED-DO step "
1836 "expression references control variable "
1837 "at %L", &iter_var_loc) == FAILURE)
1838 t = FAILURE;
1842 if (gfc_resolve_expr (c->expr) == FAILURE)
1843 t = FAILURE;
1846 return t;
1849 /* Resolve character array constructor. If it has a specified constant character
1850 length, pad/truncate the elements here; if the length is not specified and
1851 all elements are of compile-time known length, emit an error as this is
1852 invalid. */
1854 gfc_try
1855 gfc_resolve_character_array_constructor (gfc_expr *expr)
1857 gfc_constructor *p;
1858 int found_length;
1860 gcc_assert (expr->expr_type == EXPR_ARRAY);
1861 gcc_assert (expr->ts.type == BT_CHARACTER);
1863 if (expr->ts.u.cl == NULL)
1865 for (p = gfc_constructor_first (expr->value.constructor);
1866 p; p = gfc_constructor_next (p))
1867 if (p->expr->ts.u.cl != NULL)
1869 /* Ensure that if there is a char_len around that it is
1870 used; otherwise the middle-end confuses them! */
1871 expr->ts.u.cl = p->expr->ts.u.cl;
1872 goto got_charlen;
1875 expr->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
1878 got_charlen:
1880 found_length = -1;
1882 if (expr->ts.u.cl->length == NULL)
1884 /* Check that all constant string elements have the same length until
1885 we reach the end or find a variable-length one. */
1887 for (p = gfc_constructor_first (expr->value.constructor);
1888 p; p = gfc_constructor_next (p))
1890 int current_length = -1;
1891 gfc_ref *ref;
1892 for (ref = p->expr->ref; ref; ref = ref->next)
1893 if (ref->type == REF_SUBSTRING
1894 && ref->u.ss.start->expr_type == EXPR_CONSTANT
1895 && ref->u.ss.end->expr_type == EXPR_CONSTANT)
1896 break;
1898 if (p->expr->expr_type == EXPR_CONSTANT)
1899 current_length = p->expr->value.character.length;
1900 else if (ref)
1902 long j;
1903 j = mpz_get_ui (ref->u.ss.end->value.integer)
1904 - mpz_get_ui (ref->u.ss.start->value.integer) + 1;
1905 current_length = (int) j;
1907 else if (p->expr->ts.u.cl && p->expr->ts.u.cl->length
1908 && p->expr->ts.u.cl->length->expr_type == EXPR_CONSTANT)
1910 long j;
1911 j = mpz_get_si (p->expr->ts.u.cl->length->value.integer);
1912 current_length = (int) j;
1914 else
1915 return SUCCESS;
1917 gcc_assert (current_length != -1);
1919 if (found_length == -1)
1920 found_length = current_length;
1921 else if (found_length != current_length)
1923 gfc_error ("Different CHARACTER lengths (%d/%d) in array"
1924 " constructor at %L", found_length, current_length,
1925 &p->expr->where);
1926 return FAILURE;
1929 gcc_assert (found_length == current_length);
1932 gcc_assert (found_length != -1);
1934 /* Update the character length of the array constructor. */
1935 expr->ts.u.cl->length = gfc_get_int_expr (gfc_default_integer_kind,
1936 NULL, found_length);
1938 else
1940 /* We've got a character length specified. It should be an integer,
1941 otherwise an error is signalled elsewhere. */
1942 gcc_assert (expr->ts.u.cl->length);
1944 /* If we've got a constant character length, pad according to this.
1945 gfc_extract_int does check for BT_INTEGER and EXPR_CONSTANT and sets
1946 max_length only if they pass. */
1947 gfc_extract_int (expr->ts.u.cl->length, &found_length);
1949 /* Now pad/truncate the elements accordingly to the specified character
1950 length. This is ok inside this conditional, as in the case above
1951 (without typespec) all elements are verified to have the same length
1952 anyway. */
1953 if (found_length != -1)
1954 for (p = gfc_constructor_first (expr->value.constructor);
1955 p; p = gfc_constructor_next (p))
1956 if (p->expr->expr_type == EXPR_CONSTANT)
1958 gfc_expr *cl = NULL;
1959 int current_length = -1;
1960 bool has_ts;
1962 if (p->expr->ts.u.cl && p->expr->ts.u.cl->length)
1964 cl = p->expr->ts.u.cl->length;
1965 gfc_extract_int (cl, &current_length);
1968 /* If gfc_extract_int above set current_length, we implicitly
1969 know the type is BT_INTEGER and it's EXPR_CONSTANT. */
1971 has_ts = (expr->ts.u.cl && expr->ts.u.cl->length_from_typespec);
1973 if (! cl
1974 || (current_length != -1 && current_length != found_length))
1975 gfc_set_constant_character_len (found_length, p->expr,
1976 has_ts ? -1 : found_length);
1980 return SUCCESS;
1984 /* Resolve all of the expressions in an array list. */
1986 gfc_try
1987 gfc_resolve_array_constructor (gfc_expr *expr)
1989 gfc_try t;
1991 t = resolve_array_list (expr->value.constructor);
1992 if (t == SUCCESS)
1993 t = gfc_check_constructor_type (expr);
1995 /* gfc_resolve_character_array_constructor is called in gfc_resolve_expr after
1996 the call to this function, so we don't need to call it here; if it was
1997 called twice, an error message there would be duplicated. */
1999 return t;
2003 /* Copy an iterator structure. */
2005 gfc_iterator *
2006 gfc_copy_iterator (gfc_iterator *src)
2008 gfc_iterator *dest;
2010 if (src == NULL)
2011 return NULL;
2013 dest = gfc_get_iterator ();
2015 dest->var = gfc_copy_expr (src->var);
2016 dest->start = gfc_copy_expr (src->start);
2017 dest->end = gfc_copy_expr (src->end);
2018 dest->step = gfc_copy_expr (src->step);
2020 return dest;
2024 /********* Subroutines for determining the size of an array *********/
2026 /* These are needed just to accommodate RESHAPE(). There are no
2027 diagnostics here, we just return a negative number if something
2028 goes wrong. */
2031 /* Get the size of single dimension of an array specification. The
2032 array is guaranteed to be one dimensional. */
2034 gfc_try
2035 spec_dimen_size (gfc_array_spec *as, int dimen, mpz_t *result)
2037 if (as == NULL)
2038 return FAILURE;
2040 if (dimen < 0 || dimen > as->rank - 1)
2041 gfc_internal_error ("spec_dimen_size(): Bad dimension");
2043 if (as->type != AS_EXPLICIT
2044 || as->lower[dimen]->expr_type != EXPR_CONSTANT
2045 || as->upper[dimen]->expr_type != EXPR_CONSTANT
2046 || as->lower[dimen]->ts.type != BT_INTEGER
2047 || as->upper[dimen]->ts.type != BT_INTEGER)
2048 return FAILURE;
2050 mpz_init (*result);
2052 mpz_sub (*result, as->upper[dimen]->value.integer,
2053 as->lower[dimen]->value.integer);
2055 mpz_add_ui (*result, *result, 1);
2057 return SUCCESS;
2061 gfc_try
2062 spec_size (gfc_array_spec *as, mpz_t *result)
2064 mpz_t size;
2065 int d;
2067 if (as->type == AS_ASSUMED_RANK)
2068 return FAILURE;
2070 mpz_init_set_ui (*result, 1);
2072 for (d = 0; d < as->rank; d++)
2074 if (spec_dimen_size (as, d, &size) == FAILURE)
2076 mpz_clear (*result);
2077 return FAILURE;
2080 mpz_mul (*result, *result, size);
2081 mpz_clear (size);
2084 return SUCCESS;
2088 /* Get the number of elements in an array section. Optionally, also supply
2089 the end value. */
2091 gfc_try
2092 gfc_ref_dimen_size (gfc_array_ref *ar, int dimen, mpz_t *result, mpz_t *end)
2094 mpz_t upper, lower, stride;
2095 gfc_try t;
2097 if (dimen < 0 || ar == NULL || dimen > ar->dimen - 1)
2098 gfc_internal_error ("gfc_ref_dimen_size(): Bad dimension");
2100 switch (ar->dimen_type[dimen])
2102 case DIMEN_ELEMENT:
2103 mpz_init (*result);
2104 mpz_set_ui (*result, 1);
2105 t = SUCCESS;
2106 break;
2108 case DIMEN_VECTOR:
2109 t = gfc_array_size (ar->start[dimen], result); /* Recurse! */
2110 break;
2112 case DIMEN_RANGE:
2113 mpz_init (upper);
2114 mpz_init (lower);
2115 mpz_init (stride);
2116 t = FAILURE;
2118 if (ar->start[dimen] == NULL)
2120 if (ar->as->lower[dimen] == NULL
2121 || ar->as->lower[dimen]->expr_type != EXPR_CONSTANT)
2122 goto cleanup;
2123 mpz_set (lower, ar->as->lower[dimen]->value.integer);
2125 else
2127 if (ar->start[dimen]->expr_type != EXPR_CONSTANT)
2128 goto cleanup;
2129 mpz_set (lower, ar->start[dimen]->value.integer);
2132 if (ar->end[dimen] == NULL)
2134 if (ar->as->upper[dimen] == NULL
2135 || ar->as->upper[dimen]->expr_type != EXPR_CONSTANT)
2136 goto cleanup;
2137 mpz_set (upper, ar->as->upper[dimen]->value.integer);
2139 else
2141 if (ar->end[dimen]->expr_type != EXPR_CONSTANT)
2142 goto cleanup;
2143 mpz_set (upper, ar->end[dimen]->value.integer);
2146 if (ar->stride[dimen] == NULL)
2147 mpz_set_ui (stride, 1);
2148 else
2150 if (ar->stride[dimen]->expr_type != EXPR_CONSTANT)
2151 goto cleanup;
2152 mpz_set (stride, ar->stride[dimen]->value.integer);
2155 mpz_init (*result);
2156 mpz_sub (*result, upper, lower);
2157 mpz_add (*result, *result, stride);
2158 mpz_div (*result, *result, stride);
2160 /* Zero stride caught earlier. */
2161 if (mpz_cmp_ui (*result, 0) < 0)
2162 mpz_set_ui (*result, 0);
2163 t = SUCCESS;
2165 if (end)
2167 mpz_init (*end);
2169 mpz_sub_ui (*end, *result, 1UL);
2170 mpz_mul (*end, *end, stride);
2171 mpz_add (*end, *end, lower);
2174 cleanup:
2175 mpz_clear (upper);
2176 mpz_clear (lower);
2177 mpz_clear (stride);
2178 return t;
2180 default:
2181 gfc_internal_error ("gfc_ref_dimen_size(): Bad dimen_type");
2184 return t;
2188 static gfc_try
2189 ref_size (gfc_array_ref *ar, mpz_t *result)
2191 mpz_t size;
2192 int d;
2194 mpz_init_set_ui (*result, 1);
2196 for (d = 0; d < ar->dimen; d++)
2198 if (gfc_ref_dimen_size (ar, d, &size, NULL) == FAILURE)
2200 mpz_clear (*result);
2201 return FAILURE;
2204 mpz_mul (*result, *result, size);
2205 mpz_clear (size);
2208 return SUCCESS;
2212 /* Given an array expression and a dimension, figure out how many
2213 elements it has along that dimension. Returns SUCCESS if we were
2214 able to return a result in the 'result' variable, FAILURE
2215 otherwise. */
2217 gfc_try
2218 gfc_array_dimen_size (gfc_expr *array, int dimen, mpz_t *result)
2220 gfc_ref *ref;
2221 int i;
2223 if (array->ts.type == BT_CLASS)
2224 return FAILURE;
2226 if (array->rank == -1)
2227 return FAILURE;
2229 if (dimen < 0 || array == NULL || dimen > array->rank - 1)
2230 gfc_internal_error ("gfc_array_dimen_size(): Bad dimension");
2232 switch (array->expr_type)
2234 case EXPR_VARIABLE:
2235 case EXPR_FUNCTION:
2236 for (ref = array->ref; ref; ref = ref->next)
2238 if (ref->type != REF_ARRAY)
2239 continue;
2241 if (ref->u.ar.type == AR_FULL)
2242 return spec_dimen_size (ref->u.ar.as, dimen, result);
2244 if (ref->u.ar.type == AR_SECTION)
2246 for (i = 0; dimen >= 0; i++)
2247 if (ref->u.ar.dimen_type[i] != DIMEN_ELEMENT)
2248 dimen--;
2250 return gfc_ref_dimen_size (&ref->u.ar, i - 1, result, NULL);
2254 if (array->shape && array->shape[dimen])
2256 mpz_init_set (*result, array->shape[dimen]);
2257 return SUCCESS;
2260 if (array->symtree->n.sym->attr.generic
2261 && array->value.function.esym != NULL)
2263 if (spec_dimen_size (array->value.function.esym->as, dimen, result)
2264 == FAILURE)
2265 return FAILURE;
2267 else if (spec_dimen_size (array->symtree->n.sym->as, dimen, result)
2268 == FAILURE)
2269 return FAILURE;
2271 break;
2273 case EXPR_ARRAY:
2274 if (array->shape == NULL) {
2275 /* Expressions with rank > 1 should have "shape" properly set */
2276 if ( array->rank != 1 )
2277 gfc_internal_error ("gfc_array_dimen_size(): Bad EXPR_ARRAY expr");
2278 return gfc_array_size(array, result);
2281 /* Fall through */
2282 default:
2283 if (array->shape == NULL)
2284 return FAILURE;
2286 mpz_init_set (*result, array->shape[dimen]);
2288 break;
2291 return SUCCESS;
2295 /* Given an array expression, figure out how many elements are in the
2296 array. Returns SUCCESS if this is possible, and sets the 'result'
2297 variable. Otherwise returns FAILURE. */
2299 gfc_try
2300 gfc_array_size (gfc_expr *array, mpz_t *result)
2302 expand_info expand_save;
2303 gfc_ref *ref;
2304 int i;
2305 gfc_try t;
2307 if (array->ts.type == BT_CLASS)
2308 return FAILURE;
2310 switch (array->expr_type)
2312 case EXPR_ARRAY:
2313 gfc_push_suppress_errors ();
2315 expand_save = current_expand;
2317 current_expand.count = result;
2318 mpz_init_set_ui (*result, 0);
2320 current_expand.expand_work_function = count_elements;
2321 iter_stack = NULL;
2323 t = expand_constructor (array->value.constructor);
2325 gfc_pop_suppress_errors ();
2327 if (t == FAILURE)
2328 mpz_clear (*result);
2329 current_expand = expand_save;
2330 return t;
2332 case EXPR_VARIABLE:
2333 for (ref = array->ref; ref; ref = ref->next)
2335 if (ref->type != REF_ARRAY)
2336 continue;
2338 if (ref->u.ar.type == AR_FULL)
2339 return spec_size (ref->u.ar.as, result);
2341 if (ref->u.ar.type == AR_SECTION)
2342 return ref_size (&ref->u.ar, result);
2345 return spec_size (array->symtree->n.sym->as, result);
2348 default:
2349 if (array->rank == 0 || array->shape == NULL)
2350 return FAILURE;
2352 mpz_init_set_ui (*result, 1);
2354 for (i = 0; i < array->rank; i++)
2355 mpz_mul (*result, *result, array->shape[i]);
2357 break;
2360 return SUCCESS;
2364 /* Given an array reference, return the shape of the reference in an
2365 array of mpz_t integers. */
2367 gfc_try
2368 gfc_array_ref_shape (gfc_array_ref *ar, mpz_t *shape)
2370 int d;
2371 int i;
2373 d = 0;
2375 switch (ar->type)
2377 case AR_FULL:
2378 for (; d < ar->as->rank; d++)
2379 if (spec_dimen_size (ar->as, d, &shape[d]) == FAILURE)
2380 goto cleanup;
2382 return SUCCESS;
2384 case AR_SECTION:
2385 for (i = 0; i < ar->dimen; i++)
2387 if (ar->dimen_type[i] != DIMEN_ELEMENT)
2389 if (gfc_ref_dimen_size (ar, i, &shape[d], NULL) == FAILURE)
2390 goto cleanup;
2391 d++;
2395 return SUCCESS;
2397 default:
2398 break;
2401 cleanup:
2402 gfc_clear_shape (shape, d);
2403 return FAILURE;
2407 /* Given an array expression, find the array reference structure that
2408 characterizes the reference. */
2410 gfc_array_ref *
2411 gfc_find_array_ref (gfc_expr *e)
2413 gfc_ref *ref;
2415 for (ref = e->ref; ref; ref = ref->next)
2416 if (ref->type == REF_ARRAY
2417 && (ref->u.ar.type == AR_FULL || ref->u.ar.type == AR_SECTION))
2418 break;
2420 if (ref == NULL)
2421 gfc_internal_error ("gfc_find_array_ref(): No ref found");
2423 return &ref->u.ar;
2427 /* Find out if an array shape is known at compile time. */
2430 gfc_is_compile_time_shape (gfc_array_spec *as)
2432 int i;
2434 if (as->type != AS_EXPLICIT)
2435 return 0;
2437 for (i = 0; i < as->rank; i++)
2438 if (!gfc_is_constant_expr (as->lower[i])
2439 || !gfc_is_constant_expr (as->upper[i]))
2440 return 0;
2442 return 1;