2011-06-14 Zdenek Dvorak <ook@ucw.cz>
[official-gcc.git] / gcc / fortran / array.c
blob1394e17baf0c870fdbb970d28898c4bd09f4aac5
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 "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 dest->offset = gfc_copy_expr (src->offset);
54 return dest;
58 /* Match a single dimension of an array reference. This can be a
59 single element or an array section. Any modifications we've made
60 to the ar structure are cleaned up by the caller. If the init
61 is set, we require the subscript to be a valid initialization
62 expression. */
64 static match
65 match_subscript (gfc_array_ref *ar, int init, bool match_star)
67 match m = MATCH_ERROR;
68 bool star = false;
69 int i;
71 i = ar->dimen + ar->codimen;
73 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;
392 lower = &as->lower[as->rank + as->corank - 1];
393 upper = &as->upper[as->rank + as->corank - 1];
395 if (gfc_match_char ('*') == MATCH_YES)
397 *lower = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
398 return AS_ASSUMED_SIZE;
401 if (gfc_match_char (':') == MATCH_YES)
402 return AS_DEFERRED;
404 m = gfc_match_expr (upper);
405 if (m == MATCH_NO)
406 gfc_error ("Expected expression in array specification at %C");
407 if (m != MATCH_YES)
408 return AS_UNKNOWN;
409 if (gfc_expr_check_typed (*upper, gfc_current_ns, false) == FAILURE)
410 return AS_UNKNOWN;
412 if (gfc_match_char (':') == MATCH_NO)
414 *lower = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
415 return AS_EXPLICIT;
418 *lower = *upper;
419 *upper = NULL;
421 if (gfc_match_char ('*') == MATCH_YES)
422 return AS_ASSUMED_SIZE;
424 m = gfc_match_expr (upper);
425 if (m == MATCH_ERROR)
426 return AS_UNKNOWN;
427 if (m == MATCH_NO)
428 return AS_ASSUMED_SHAPE;
429 if (gfc_expr_check_typed (*upper, gfc_current_ns, false) == FAILURE)
430 return AS_UNKNOWN;
432 return AS_EXPLICIT;
436 /* Matches an array specification, incidentally figuring out what sort
437 it is. Match either a normal array specification, or a coarray spec
438 or both. Optionally allow [:] for coarrays. */
440 match
441 gfc_match_array_spec (gfc_array_spec **asp, bool match_dim, bool match_codim)
443 array_type current_type;
444 gfc_array_spec *as;
445 int i;
447 as = gfc_get_array_spec ();
449 if (!match_dim)
450 goto coarray;
452 if (gfc_match_char ('(') != MATCH_YES)
454 if (!match_codim)
455 goto done;
456 goto coarray;
459 for (;;)
461 as->rank++;
462 current_type = match_array_element_spec (as);
464 /* Note that current_type == AS_ASSUMED_SIZE for both assumed-size
465 and implied-shape specifications. If the rank is at least 2, we can
466 distinguish between them. But for rank 1, we currently return
467 ASSUMED_SIZE; this gets adjusted later when we know for sure
468 whether the symbol parsed is a PARAMETER or not. */
470 if (as->rank == 1)
472 if (current_type == AS_UNKNOWN)
473 goto cleanup;
474 as->type = current_type;
476 else
477 switch (as->type)
478 { /* See how current spec meshes with the existing. */
479 case AS_UNKNOWN:
480 goto cleanup;
482 case AS_IMPLIED_SHAPE:
483 if (current_type != AS_ASSUMED_SHAPE)
485 gfc_error ("Bad array specification for implied-shape"
486 " array at %C");
487 goto cleanup;
489 break;
491 case AS_EXPLICIT:
492 if (current_type == AS_ASSUMED_SIZE)
494 as->type = AS_ASSUMED_SIZE;
495 break;
498 if (current_type == AS_EXPLICIT)
499 break;
501 gfc_error ("Bad array specification for an explicitly shaped "
502 "array at %C");
504 goto cleanup;
506 case AS_ASSUMED_SHAPE:
507 if ((current_type == AS_ASSUMED_SHAPE)
508 || (current_type == AS_DEFERRED))
509 break;
511 gfc_error ("Bad array specification for assumed shape "
512 "array at %C");
513 goto cleanup;
515 case AS_DEFERRED:
516 if (current_type == AS_DEFERRED)
517 break;
519 if (current_type == AS_ASSUMED_SHAPE)
521 as->type = AS_ASSUMED_SHAPE;
522 break;
525 gfc_error ("Bad specification for deferred shape array at %C");
526 goto cleanup;
528 case AS_ASSUMED_SIZE:
529 if (as->rank == 2 && current_type == AS_ASSUMED_SIZE)
531 as->type = AS_IMPLIED_SHAPE;
532 break;
535 gfc_error ("Bad specification for assumed size array at %C");
536 goto cleanup;
539 if (gfc_match_char (')') == MATCH_YES)
540 break;
542 if (gfc_match_char (',') != MATCH_YES)
544 gfc_error ("Expected another dimension in array declaration at %C");
545 goto cleanup;
548 if (as->rank + as->corank >= GFC_MAX_DIMENSIONS)
550 gfc_error ("Array specification at %C has more than %d dimensions",
551 GFC_MAX_DIMENSIONS);
552 goto cleanup;
555 if (as->corank + as->rank >= 7
556 && gfc_notify_std (GFC_STD_F2008, "Fortran 2008: Array "
557 "specification at %C with more than 7 dimensions")
558 == FAILURE)
559 goto cleanup;
562 if (!match_codim)
563 goto done;
565 coarray:
566 if (gfc_match_char ('[') != MATCH_YES)
567 goto done;
569 if (gfc_notify_std (GFC_STD_F2008, "Fortran 2008: Coarray declaration at %C")
570 == FAILURE)
571 goto cleanup;
573 if (gfc_option.coarray == GFC_FCOARRAY_NONE)
575 gfc_fatal_error ("Coarrays disabled at %C, use -fcoarray= to enable");
576 goto cleanup;
579 if (as->rank >= GFC_MAX_DIMENSIONS)
581 gfc_error ("Array specification at %C has more than %d "
582 "dimensions", GFC_MAX_DIMENSIONS);
583 goto cleanup;
586 for (;;)
588 as->corank++;
589 current_type = match_array_element_spec (as);
591 if (current_type == AS_UNKNOWN)
592 goto cleanup;
594 if (as->corank == 1)
595 as->cotype = current_type;
596 else
597 switch (as->cotype)
598 { /* See how current spec meshes with the existing. */
599 case AS_IMPLIED_SHAPE:
600 case AS_UNKNOWN:
601 goto cleanup;
603 case AS_EXPLICIT:
604 if (current_type == AS_ASSUMED_SIZE)
606 as->cotype = AS_ASSUMED_SIZE;
607 break;
610 if (current_type == AS_EXPLICIT)
611 break;
613 gfc_error ("Bad array specification for an explicitly "
614 "shaped array at %C");
616 goto cleanup;
618 case AS_ASSUMED_SHAPE:
619 if ((current_type == AS_ASSUMED_SHAPE)
620 || (current_type == AS_DEFERRED))
621 break;
623 gfc_error ("Bad array specification for assumed shape "
624 "array at %C");
625 goto cleanup;
627 case AS_DEFERRED:
628 if (current_type == AS_DEFERRED)
629 break;
631 if (current_type == AS_ASSUMED_SHAPE)
633 as->cotype = AS_ASSUMED_SHAPE;
634 break;
637 gfc_error ("Bad specification for deferred shape array at %C");
638 goto cleanup;
640 case AS_ASSUMED_SIZE:
641 gfc_error ("Bad specification for assumed size array at %C");
642 goto cleanup;
645 if (gfc_match_char (']') == MATCH_YES)
646 break;
648 if (gfc_match_char (',') != MATCH_YES)
650 gfc_error ("Expected another dimension in array declaration at %C");
651 goto cleanup;
654 if (as->rank + as->corank >= GFC_MAX_DIMENSIONS)
656 gfc_error ("Array specification at %C has more than %d "
657 "dimensions", GFC_MAX_DIMENSIONS);
658 goto cleanup;
662 if (current_type == AS_EXPLICIT)
664 gfc_error ("Upper bound of last coarray dimension must be '*' at %C");
665 goto cleanup;
668 if (as->cotype == AS_ASSUMED_SIZE)
669 as->cotype = AS_EXPLICIT;
671 if (as->rank == 0)
672 as->type = as->cotype;
674 done:
675 if (as->rank == 0 && as->corank == 0)
677 *asp = NULL;
678 gfc_free_array_spec (as);
679 return MATCH_NO;
682 /* If a lower bounds of an assumed shape array is blank, put in one. */
683 if (as->type == AS_ASSUMED_SHAPE)
685 for (i = 0; i < as->rank + as->corank; i++)
687 if (as->lower[i] == NULL)
688 as->lower[i] = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
692 *asp = as;
694 return MATCH_YES;
696 cleanup:
697 /* Something went wrong. */
698 gfc_free_array_spec (as);
699 return MATCH_ERROR;
703 /* Given a symbol and an array specification, modify the symbol to
704 have that array specification. The error locus is needed in case
705 something goes wrong. On failure, the caller must free the spec. */
707 gfc_try
708 gfc_set_array_spec (gfc_symbol *sym, gfc_array_spec *as, locus *error_loc)
710 int i;
712 if (as == NULL)
713 return SUCCESS;
715 if (as->rank
716 && gfc_add_dimension (&sym->attr, sym->name, error_loc) == FAILURE)
717 return FAILURE;
719 if (as->corank
720 && gfc_add_codimension (&sym->attr, sym->name, error_loc) == FAILURE)
721 return FAILURE;
723 if (sym->as == NULL)
725 sym->as = as;
726 return SUCCESS;
729 if (as->corank)
731 /* The "sym" has no corank (checked via gfc_add_codimension). Thus
732 the codimension is simply added. */
733 gcc_assert (as->rank == 0 && sym->as->corank == 0);
735 sym->as->cotype = as->cotype;
736 sym->as->corank = as->corank;
737 for (i = 0; i < as->corank; i++)
739 sym->as->lower[sym->as->rank + i] = as->lower[i];
740 sym->as->upper[sym->as->rank + i] = as->upper[i];
743 else
745 /* The "sym" has no rank (checked via gfc_add_dimension). Thus
746 the dimension is added - but first the codimensions (if existing
747 need to be shifted to make space for the dimension. */
748 gcc_assert (as->corank == 0 && sym->as->rank == 0);
750 sym->as->rank = as->rank;
751 sym->as->type = as->type;
752 sym->as->cray_pointee = as->cray_pointee;
753 sym->as->cp_was_assumed = as->cp_was_assumed;
755 for (i = 0; i < sym->as->corank; i++)
757 sym->as->lower[as->rank + i] = sym->as->lower[i];
758 sym->as->upper[as->rank + i] = sym->as->upper[i];
760 for (i = 0; i < as->rank; i++)
762 sym->as->lower[i] = as->lower[i];
763 sym->as->upper[i] = as->upper[i];
767 free (as);
768 return SUCCESS;
772 /* Copy an array specification. */
774 gfc_array_spec *
775 gfc_copy_array_spec (gfc_array_spec *src)
777 gfc_array_spec *dest;
778 int i;
780 if (src == NULL)
781 return NULL;
783 dest = gfc_get_array_spec ();
785 *dest = *src;
787 for (i = 0; i < dest->rank + dest->corank; i++)
789 dest->lower[i] = gfc_copy_expr (dest->lower[i]);
790 dest->upper[i] = gfc_copy_expr (dest->upper[i]);
793 return dest;
797 /* Returns nonzero if the two expressions are equal. Only handles integer
798 constants. */
800 static int
801 compare_bounds (gfc_expr *bound1, gfc_expr *bound2)
803 if (bound1 == NULL || bound2 == NULL
804 || bound1->expr_type != EXPR_CONSTANT
805 || bound2->expr_type != EXPR_CONSTANT
806 || bound1->ts.type != BT_INTEGER
807 || bound2->ts.type != BT_INTEGER)
808 gfc_internal_error ("gfc_compare_array_spec(): Array spec clobbered");
810 if (mpz_cmp (bound1->value.integer, bound2->value.integer) == 0)
811 return 1;
812 else
813 return 0;
817 /* Compares two array specifications. They must be constant or deferred
818 shape. */
821 gfc_compare_array_spec (gfc_array_spec *as1, gfc_array_spec *as2)
823 int i;
825 if (as1 == NULL && as2 == NULL)
826 return 1;
828 if (as1 == NULL || as2 == NULL)
829 return 0;
831 if (as1->rank != as2->rank)
832 return 0;
834 if (as1->corank != as2->corank)
835 return 0;
837 if (as1->rank == 0)
838 return 1;
840 if (as1->type != as2->type)
841 return 0;
843 if (as1->type == AS_EXPLICIT)
844 for (i = 0; i < as1->rank + as1->corank; i++)
846 if (compare_bounds (as1->lower[i], as2->lower[i]) == 0)
847 return 0;
849 if (compare_bounds (as1->upper[i], as2->upper[i]) == 0)
850 return 0;
853 return 1;
857 /****************** Array constructor functions ******************/
860 /* Given an expression node that might be an array constructor and a
861 symbol, make sure that no iterators in this or child constructors
862 use the symbol as an implied-DO iterator. Returns nonzero if a
863 duplicate was found. */
865 static int
866 check_duplicate_iterator (gfc_constructor_base base, gfc_symbol *master)
868 gfc_constructor *c;
869 gfc_expr *e;
871 for (c = gfc_constructor_first (base); c; c = gfc_constructor_next (c))
873 e = c->expr;
875 if (e->expr_type == EXPR_ARRAY
876 && check_duplicate_iterator (e->value.constructor, master))
877 return 1;
879 if (c->iterator == NULL)
880 continue;
882 if (c->iterator->var->symtree->n.sym == master)
884 gfc_error ("DO-iterator '%s' at %L is inside iterator of the "
885 "same name", master->name, &c->where);
887 return 1;
891 return 0;
895 /* Forward declaration because these functions are mutually recursive. */
896 static match match_array_cons_element (gfc_constructor_base *);
898 /* Match a list of array elements. */
900 static match
901 match_array_list (gfc_constructor_base *result)
903 gfc_constructor_base head;
904 gfc_constructor *p;
905 gfc_iterator iter;
906 locus old_loc;
907 gfc_expr *e;
908 match m;
909 int n;
911 old_loc = gfc_current_locus;
913 if (gfc_match_char ('(') == MATCH_NO)
914 return MATCH_NO;
916 memset (&iter, '\0', sizeof (gfc_iterator));
917 head = NULL;
919 m = match_array_cons_element (&head);
920 if (m != MATCH_YES)
921 goto cleanup;
923 if (gfc_match_char (',') != MATCH_YES)
925 m = MATCH_NO;
926 goto cleanup;
929 for (n = 1;; n++)
931 m = gfc_match_iterator (&iter, 0);
932 if (m == MATCH_YES)
933 break;
934 if (m == MATCH_ERROR)
935 goto cleanup;
937 m = match_array_cons_element (&head);
938 if (m == MATCH_ERROR)
939 goto cleanup;
940 if (m == MATCH_NO)
942 if (n > 2)
943 goto syntax;
944 m = MATCH_NO;
945 goto cleanup; /* Could be a complex constant */
948 if (gfc_match_char (',') != MATCH_YES)
950 if (n > 2)
951 goto syntax;
952 m = MATCH_NO;
953 goto cleanup;
957 if (gfc_match_char (')') != MATCH_YES)
958 goto syntax;
960 if (check_duplicate_iterator (head, iter.var->symtree->n.sym))
962 m = MATCH_ERROR;
963 goto cleanup;
966 e = gfc_get_array_expr (BT_UNKNOWN, 0, &old_loc);
967 e->value.constructor = head;
969 p = gfc_constructor_append_expr (result, e, &gfc_current_locus);
970 p->iterator = gfc_get_iterator ();
971 *p->iterator = iter;
973 return MATCH_YES;
975 syntax:
976 gfc_error ("Syntax error in array constructor at %C");
977 m = MATCH_ERROR;
979 cleanup:
980 gfc_constructor_free (head);
981 gfc_free_iterator (&iter, 0);
982 gfc_current_locus = old_loc;
983 return m;
987 /* Match a single element of an array constructor, which can be a
988 single expression or a list of elements. */
990 static match
991 match_array_cons_element (gfc_constructor_base *result)
993 gfc_expr *expr;
994 match m;
996 m = match_array_list (result);
997 if (m != MATCH_NO)
998 return m;
1000 m = gfc_match_expr (&expr);
1001 if (m != MATCH_YES)
1002 return m;
1004 gfc_constructor_append_expr (result, expr, &gfc_current_locus);
1005 return MATCH_YES;
1009 /* Match an array constructor. */
1011 match
1012 gfc_match_array_constructor (gfc_expr **result)
1014 gfc_constructor_base head, new_cons;
1015 gfc_expr *expr;
1016 gfc_typespec ts;
1017 locus where;
1018 match m;
1019 const char *end_delim;
1020 bool seen_ts;
1022 if (gfc_match (" (/") == MATCH_NO)
1024 if (gfc_match (" [") == MATCH_NO)
1025 return MATCH_NO;
1026 else
1028 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: [...] "
1029 "style array constructors at %C") == FAILURE)
1030 return MATCH_ERROR;
1031 end_delim = " ]";
1034 else
1035 end_delim = " /)";
1037 where = gfc_current_locus;
1038 head = new_cons = NULL;
1039 seen_ts = false;
1041 /* Try to match an optional "type-spec ::" */
1042 if (gfc_match_decl_type_spec (&ts, 0) == MATCH_YES)
1044 seen_ts = (gfc_match (" ::") == MATCH_YES);
1046 if (seen_ts)
1048 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: Array constructor "
1049 "including type specification at %C") == FAILURE)
1050 goto cleanup;
1052 if (ts.deferred)
1054 gfc_error ("Type-spec at %L cannot contain a deferred "
1055 "type parameter", &where);
1056 goto cleanup;
1061 if (! seen_ts)
1062 gfc_current_locus = where;
1064 if (gfc_match (end_delim) == MATCH_YES)
1066 if (seen_ts)
1067 goto done;
1068 else
1070 gfc_error ("Empty array constructor at %C is not allowed");
1071 goto cleanup;
1075 for (;;)
1077 m = match_array_cons_element (&head);
1078 if (m == MATCH_ERROR)
1079 goto cleanup;
1080 if (m == MATCH_NO)
1081 goto syntax;
1083 if (gfc_match_char (',') == MATCH_NO)
1084 break;
1087 if (gfc_match (end_delim) == MATCH_NO)
1088 goto syntax;
1090 done:
1091 /* Size must be calculated at resolution time. */
1092 if (seen_ts)
1094 expr = gfc_get_array_expr (ts.type, ts.kind, &where);
1095 expr->ts = ts;
1097 else
1098 expr = gfc_get_array_expr (BT_UNKNOWN, 0, &where);
1100 expr->value.constructor = head;
1101 if (expr->ts.u.cl)
1102 expr->ts.u.cl->length_from_typespec = seen_ts;
1104 *result = expr;
1105 return MATCH_YES;
1107 syntax:
1108 gfc_error ("Syntax error in array constructor at %C");
1110 cleanup:
1111 gfc_constructor_free (head);
1112 return MATCH_ERROR;
1117 /************** Check array constructors for correctness **************/
1119 /* Given an expression, compare it's type with the type of the current
1120 constructor. Returns nonzero if an error was issued. The
1121 cons_state variable keeps track of whether the type of the
1122 constructor being read or resolved is known to be good, bad or just
1123 starting out. */
1125 static gfc_typespec constructor_ts;
1126 static enum
1127 { CONS_START, CONS_GOOD, CONS_BAD }
1128 cons_state;
1130 static int
1131 check_element_type (gfc_expr *expr, bool convert)
1133 if (cons_state == CONS_BAD)
1134 return 0; /* Suppress further errors */
1136 if (cons_state == CONS_START)
1138 if (expr->ts.type == BT_UNKNOWN)
1139 cons_state = CONS_BAD;
1140 else
1142 cons_state = CONS_GOOD;
1143 constructor_ts = expr->ts;
1146 return 0;
1149 if (gfc_compare_types (&constructor_ts, &expr->ts))
1150 return 0;
1152 if (convert)
1153 return gfc_convert_type (expr, &constructor_ts, 1) == SUCCESS ? 0 : 1;
1155 gfc_error ("Element in %s array constructor at %L is %s",
1156 gfc_typename (&constructor_ts), &expr->where,
1157 gfc_typename (&expr->ts));
1159 cons_state = CONS_BAD;
1160 return 1;
1164 /* Recursive work function for gfc_check_constructor_type(). */
1166 static gfc_try
1167 check_constructor_type (gfc_constructor_base base, bool convert)
1169 gfc_constructor *c;
1170 gfc_expr *e;
1172 for (c = gfc_constructor_first (base); c; c = gfc_constructor_next (c))
1174 e = c->expr;
1176 if (e->expr_type == EXPR_ARRAY)
1178 if (check_constructor_type (e->value.constructor, convert) == FAILURE)
1179 return FAILURE;
1181 continue;
1184 if (check_element_type (e, convert))
1185 return FAILURE;
1188 return SUCCESS;
1192 /* Check that all elements of an array constructor are the same type.
1193 On FAILURE, an error has been generated. */
1195 gfc_try
1196 gfc_check_constructor_type (gfc_expr *e)
1198 gfc_try t;
1200 if (e->ts.type != BT_UNKNOWN)
1202 cons_state = CONS_GOOD;
1203 constructor_ts = e->ts;
1205 else
1207 cons_state = CONS_START;
1208 gfc_clear_ts (&constructor_ts);
1211 /* If e->ts.type != BT_UNKNOWN, the array constructor included a
1212 typespec, and we will now convert the values on the fly. */
1213 t = check_constructor_type (e->value.constructor, e->ts.type != BT_UNKNOWN);
1214 if (t == SUCCESS && e->ts.type == BT_UNKNOWN)
1215 e->ts = constructor_ts;
1217 return t;
1222 typedef struct cons_stack
1224 gfc_iterator *iterator;
1225 struct cons_stack *previous;
1227 cons_stack;
1229 static cons_stack *base;
1231 static gfc_try check_constructor (gfc_constructor_base, gfc_try (*) (gfc_expr *));
1233 /* Check an EXPR_VARIABLE expression in a constructor to make sure
1234 that that variable is an iteration variables. */
1236 gfc_try
1237 gfc_check_iter_variable (gfc_expr *expr)
1239 gfc_symbol *sym;
1240 cons_stack *c;
1242 sym = expr->symtree->n.sym;
1244 for (c = base; c && c->iterator; c = c->previous)
1245 if (sym == c->iterator->var->symtree->n.sym)
1246 return SUCCESS;
1248 return FAILURE;
1252 /* Recursive work function for gfc_check_constructor(). This amounts
1253 to calling the check function for each expression in the
1254 constructor, giving variables with the names of iterators a pass. */
1256 static gfc_try
1257 check_constructor (gfc_constructor_base ctor, gfc_try (*check_function) (gfc_expr *))
1259 cons_stack element;
1260 gfc_expr *e;
1261 gfc_try t;
1262 gfc_constructor *c;
1264 for (c = gfc_constructor_first (ctor); c; c = gfc_constructor_next (c))
1266 e = c->expr;
1268 if (e->expr_type != EXPR_ARRAY)
1270 if ((*check_function) (e) == FAILURE)
1271 return FAILURE;
1272 continue;
1275 element.previous = base;
1276 element.iterator = c->iterator;
1278 base = &element;
1279 t = check_constructor (e->value.constructor, check_function);
1280 base = element.previous;
1282 if (t == FAILURE)
1283 return FAILURE;
1286 /* Nothing went wrong, so all OK. */
1287 return SUCCESS;
1291 /* Checks a constructor to see if it is a particular kind of
1292 expression -- specification, restricted, or initialization as
1293 determined by the check_function. */
1295 gfc_try
1296 gfc_check_constructor (gfc_expr *expr, gfc_try (*check_function) (gfc_expr *))
1298 cons_stack *base_save;
1299 gfc_try t;
1301 base_save = base;
1302 base = NULL;
1304 t = check_constructor (expr->value.constructor, check_function);
1305 base = base_save;
1307 return t;
1312 /**************** Simplification of array constructors ****************/
1314 iterator_stack *iter_stack;
1316 typedef struct
1318 gfc_constructor_base base;
1319 int extract_count, extract_n;
1320 gfc_expr *extracted;
1321 mpz_t *count;
1323 mpz_t *offset;
1324 gfc_component *component;
1326 gfc_try (*expand_work_function) (gfc_expr *);
1328 expand_info;
1330 static expand_info current_expand;
1332 static gfc_try expand_constructor (gfc_constructor_base);
1335 /* Work function that counts the number of elements present in a
1336 constructor. */
1338 static gfc_try
1339 count_elements (gfc_expr *e)
1341 mpz_t result;
1343 if (e->rank == 0)
1344 mpz_add_ui (*current_expand.count, *current_expand.count, 1);
1345 else
1347 if (gfc_array_size (e, &result) == FAILURE)
1349 gfc_free_expr (e);
1350 return FAILURE;
1353 mpz_add (*current_expand.count, *current_expand.count, result);
1354 mpz_clear (result);
1357 gfc_free_expr (e);
1358 return SUCCESS;
1362 /* Work function that extracts a particular element from an array
1363 constructor, freeing the rest. */
1365 static gfc_try
1366 extract_element (gfc_expr *e)
1368 if (e->rank != 0)
1369 { /* Something unextractable */
1370 gfc_free_expr (e);
1371 return FAILURE;
1374 if (current_expand.extract_count == current_expand.extract_n)
1375 current_expand.extracted = e;
1376 else
1377 gfc_free_expr (e);
1379 current_expand.extract_count++;
1381 return SUCCESS;
1385 /* Work function that constructs a new constructor out of the old one,
1386 stringing new elements together. */
1388 static gfc_try
1389 expand (gfc_expr *e)
1391 gfc_constructor *c = gfc_constructor_append_expr (&current_expand.base,
1392 e, &e->where);
1394 c->n.component = current_expand.component;
1395 return SUCCESS;
1399 /* Given an initialization expression that is a variable reference,
1400 substitute the current value of the iteration variable. */
1402 void
1403 gfc_simplify_iterator_var (gfc_expr *e)
1405 iterator_stack *p;
1407 for (p = iter_stack; p; p = p->prev)
1408 if (e->symtree == p->variable)
1409 break;
1411 if (p == NULL)
1412 return; /* Variable not found */
1414 gfc_replace_expr (e, gfc_get_int_expr (gfc_default_integer_kind, NULL, 0));
1416 mpz_set (e->value.integer, p->value);
1418 return;
1422 /* Expand an expression with that is inside of a constructor,
1423 recursing into other constructors if present. */
1425 static gfc_try
1426 expand_expr (gfc_expr *e)
1428 if (e->expr_type == EXPR_ARRAY)
1429 return expand_constructor (e->value.constructor);
1431 e = gfc_copy_expr (e);
1433 if (gfc_simplify_expr (e, 1) == FAILURE)
1435 gfc_free_expr (e);
1436 return FAILURE;
1439 return current_expand.expand_work_function (e);
1443 static gfc_try
1444 expand_iterator (gfc_constructor *c)
1446 gfc_expr *start, *end, *step;
1447 iterator_stack frame;
1448 mpz_t trip;
1449 gfc_try t;
1451 end = step = NULL;
1453 t = FAILURE;
1455 mpz_init (trip);
1456 mpz_init (frame.value);
1457 frame.prev = NULL;
1459 start = gfc_copy_expr (c->iterator->start);
1460 if (gfc_simplify_expr (start, 1) == FAILURE)
1461 goto cleanup;
1463 if (start->expr_type != EXPR_CONSTANT || start->ts.type != BT_INTEGER)
1464 goto cleanup;
1466 end = gfc_copy_expr (c->iterator->end);
1467 if (gfc_simplify_expr (end, 1) == FAILURE)
1468 goto cleanup;
1470 if (end->expr_type != EXPR_CONSTANT || end->ts.type != BT_INTEGER)
1471 goto cleanup;
1473 step = gfc_copy_expr (c->iterator->step);
1474 if (gfc_simplify_expr (step, 1) == FAILURE)
1475 goto cleanup;
1477 if (step->expr_type != EXPR_CONSTANT || step->ts.type != BT_INTEGER)
1478 goto cleanup;
1480 if (mpz_sgn (step->value.integer) == 0)
1482 gfc_error ("Iterator step at %L cannot be zero", &step->where);
1483 goto cleanup;
1486 /* Calculate the trip count of the loop. */
1487 mpz_sub (trip, end->value.integer, start->value.integer);
1488 mpz_add (trip, trip, step->value.integer);
1489 mpz_tdiv_q (trip, trip, step->value.integer);
1491 mpz_set (frame.value, start->value.integer);
1493 frame.prev = iter_stack;
1494 frame.variable = c->iterator->var->symtree;
1495 iter_stack = &frame;
1497 while (mpz_sgn (trip) > 0)
1499 if (expand_expr (c->expr) == FAILURE)
1500 goto cleanup;
1502 mpz_add (frame.value, frame.value, step->value.integer);
1503 mpz_sub_ui (trip, trip, 1);
1506 t = SUCCESS;
1508 cleanup:
1509 gfc_free_expr (start);
1510 gfc_free_expr (end);
1511 gfc_free_expr (step);
1513 mpz_clear (trip);
1514 mpz_clear (frame.value);
1516 iter_stack = frame.prev;
1518 return t;
1522 /* Expand a constructor into constant constructors without any
1523 iterators, calling the work function for each of the expanded
1524 expressions. The work function needs to either save or free the
1525 passed expression. */
1527 static gfc_try
1528 expand_constructor (gfc_constructor_base base)
1530 gfc_constructor *c;
1531 gfc_expr *e;
1533 for (c = gfc_constructor_first (base); c; c = gfc_constructor_next(c))
1535 if (c->iterator != NULL)
1537 if (expand_iterator (c) == FAILURE)
1538 return FAILURE;
1539 continue;
1542 e = c->expr;
1544 if (e->expr_type == EXPR_ARRAY)
1546 if (expand_constructor (e->value.constructor) == FAILURE)
1547 return FAILURE;
1549 continue;
1552 e = gfc_copy_expr (e);
1553 if (gfc_simplify_expr (e, 1) == FAILURE)
1555 gfc_free_expr (e);
1556 return FAILURE;
1558 current_expand.offset = &c->offset;
1559 current_expand.component = c->n.component;
1560 if (current_expand.expand_work_function (e) == FAILURE)
1561 return FAILURE;
1563 return SUCCESS;
1567 /* Given an array expression and an element number (starting at zero),
1568 return a pointer to the array element. NULL is returned if the
1569 size of the array has been exceeded. The expression node returned
1570 remains a part of the array and should not be freed. Access is not
1571 efficient at all, but this is another place where things do not
1572 have to be particularly fast. */
1574 static gfc_expr *
1575 gfc_get_array_element (gfc_expr *array, int element)
1577 expand_info expand_save;
1578 gfc_expr *e;
1579 gfc_try rc;
1581 expand_save = current_expand;
1582 current_expand.extract_n = element;
1583 current_expand.expand_work_function = extract_element;
1584 current_expand.extracted = NULL;
1585 current_expand.extract_count = 0;
1587 iter_stack = NULL;
1589 rc = expand_constructor (array->value.constructor);
1590 e = current_expand.extracted;
1591 current_expand = expand_save;
1593 if (rc == FAILURE)
1594 return NULL;
1596 return e;
1600 /* Top level subroutine for expanding constructors. We only expand
1601 constructor if they are small enough. */
1603 gfc_try
1604 gfc_expand_constructor (gfc_expr *e, bool fatal)
1606 expand_info expand_save;
1607 gfc_expr *f;
1608 gfc_try rc;
1610 /* If we can successfully get an array element at the max array size then
1611 the array is too big to expand, so we just return. */
1612 f = gfc_get_array_element (e, gfc_option.flag_max_array_constructor);
1613 if (f != NULL)
1615 gfc_free_expr (f);
1616 if (fatal)
1618 gfc_error ("The number of elements in the array constructor "
1619 "at %L requires an increase of the allowed %d "
1620 "upper limit. See -fmax-array-constructor "
1621 "option", &e->where,
1622 gfc_option.flag_max_array_constructor);
1623 return FAILURE;
1625 return SUCCESS;
1628 /* We now know the array is not too big so go ahead and try to expand it. */
1629 expand_save = current_expand;
1630 current_expand.base = NULL;
1632 iter_stack = NULL;
1634 current_expand.expand_work_function = expand;
1636 if (expand_constructor (e->value.constructor) == FAILURE)
1638 gfc_constructor_free (current_expand.base);
1639 rc = FAILURE;
1640 goto done;
1643 gfc_constructor_free (e->value.constructor);
1644 e->value.constructor = current_expand.base;
1646 rc = SUCCESS;
1648 done:
1649 current_expand = expand_save;
1651 return rc;
1655 /* Work function for checking that an element of a constructor is a
1656 constant, after removal of any iteration variables. We return
1657 FAILURE if not so. */
1659 static gfc_try
1660 is_constant_element (gfc_expr *e)
1662 int rv;
1664 rv = gfc_is_constant_expr (e);
1665 gfc_free_expr (e);
1667 return rv ? SUCCESS : FAILURE;
1671 /* Given an array constructor, determine if the constructor is
1672 constant or not by expanding it and making sure that all elements
1673 are constants. This is a bit of a hack since something like (/ (i,
1674 i=1,100000000) /) will take a while as* opposed to a more clever
1675 function that traverses the expression tree. FIXME. */
1678 gfc_constant_ac (gfc_expr *e)
1680 expand_info expand_save;
1681 gfc_try rc;
1683 iter_stack = NULL;
1684 expand_save = current_expand;
1685 current_expand.expand_work_function = is_constant_element;
1687 rc = expand_constructor (e->value.constructor);
1689 current_expand = expand_save;
1690 if (rc == FAILURE)
1691 return 0;
1693 return 1;
1697 /* Returns nonzero if an array constructor has been completely
1698 expanded (no iterators) and zero if iterators are present. */
1701 gfc_expanded_ac (gfc_expr *e)
1703 gfc_constructor *c;
1705 if (e->expr_type == EXPR_ARRAY)
1706 for (c = gfc_constructor_first (e->value.constructor);
1707 c; c = gfc_constructor_next (c))
1708 if (c->iterator != NULL || !gfc_expanded_ac (c->expr))
1709 return 0;
1711 return 1;
1715 /*************** Type resolution of array constructors ***************/
1717 /* Recursive array list resolution function. All of the elements must
1718 be of the same type. */
1720 static gfc_try
1721 resolve_array_list (gfc_constructor_base base)
1723 gfc_try t;
1724 gfc_constructor *c;
1726 t = SUCCESS;
1728 for (c = gfc_constructor_first (base); c; c = gfc_constructor_next (c))
1730 if (c->iterator != NULL
1731 && gfc_resolve_iterator (c->iterator, false) == FAILURE)
1732 t = FAILURE;
1734 if (gfc_resolve_expr (c->expr) == FAILURE)
1735 t = FAILURE;
1738 return t;
1741 /* Resolve character array constructor. If it has a specified constant character
1742 length, pad/truncate the elements here; if the length is not specified and
1743 all elements are of compile-time known length, emit an error as this is
1744 invalid. */
1746 gfc_try
1747 gfc_resolve_character_array_constructor (gfc_expr *expr)
1749 gfc_constructor *p;
1750 int found_length;
1752 gcc_assert (expr->expr_type == EXPR_ARRAY);
1753 gcc_assert (expr->ts.type == BT_CHARACTER);
1755 if (expr->ts.u.cl == NULL)
1757 for (p = gfc_constructor_first (expr->value.constructor);
1758 p; p = gfc_constructor_next (p))
1759 if (p->expr->ts.u.cl != NULL)
1761 /* Ensure that if there is a char_len around that it is
1762 used; otherwise the middle-end confuses them! */
1763 expr->ts.u.cl = p->expr->ts.u.cl;
1764 goto got_charlen;
1767 expr->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
1770 got_charlen:
1772 found_length = -1;
1774 if (expr->ts.u.cl->length == NULL)
1776 /* Check that all constant string elements have the same length until
1777 we reach the end or find a variable-length one. */
1779 for (p = gfc_constructor_first (expr->value.constructor);
1780 p; p = gfc_constructor_next (p))
1782 int current_length = -1;
1783 gfc_ref *ref;
1784 for (ref = p->expr->ref; ref; ref = ref->next)
1785 if (ref->type == REF_SUBSTRING
1786 && ref->u.ss.start->expr_type == EXPR_CONSTANT
1787 && ref->u.ss.end->expr_type == EXPR_CONSTANT)
1788 break;
1790 if (p->expr->expr_type == EXPR_CONSTANT)
1791 current_length = p->expr->value.character.length;
1792 else if (ref)
1794 long j;
1795 j = mpz_get_ui (ref->u.ss.end->value.integer)
1796 - mpz_get_ui (ref->u.ss.start->value.integer) + 1;
1797 current_length = (int) j;
1799 else if (p->expr->ts.u.cl && p->expr->ts.u.cl->length
1800 && p->expr->ts.u.cl->length->expr_type == EXPR_CONSTANT)
1802 long j;
1803 j = mpz_get_si (p->expr->ts.u.cl->length->value.integer);
1804 current_length = (int) j;
1806 else
1807 return SUCCESS;
1809 gcc_assert (current_length != -1);
1811 if (found_length == -1)
1812 found_length = current_length;
1813 else if (found_length != current_length)
1815 gfc_error ("Different CHARACTER lengths (%d/%d) in array"
1816 " constructor at %L", found_length, current_length,
1817 &p->expr->where);
1818 return FAILURE;
1821 gcc_assert (found_length == current_length);
1824 gcc_assert (found_length != -1);
1826 /* Update the character length of the array constructor. */
1827 expr->ts.u.cl->length = gfc_get_int_expr (gfc_default_integer_kind,
1828 NULL, found_length);
1830 else
1832 /* We've got a character length specified. It should be an integer,
1833 otherwise an error is signalled elsewhere. */
1834 gcc_assert (expr->ts.u.cl->length);
1836 /* If we've got a constant character length, pad according to this.
1837 gfc_extract_int does check for BT_INTEGER and EXPR_CONSTANT and sets
1838 max_length only if they pass. */
1839 gfc_extract_int (expr->ts.u.cl->length, &found_length);
1841 /* Now pad/truncate the elements accordingly to the specified character
1842 length. This is ok inside this conditional, as in the case above
1843 (without typespec) all elements are verified to have the same length
1844 anyway. */
1845 if (found_length != -1)
1846 for (p = gfc_constructor_first (expr->value.constructor);
1847 p; p = gfc_constructor_next (p))
1848 if (p->expr->expr_type == EXPR_CONSTANT)
1850 gfc_expr *cl = NULL;
1851 int current_length = -1;
1852 bool has_ts;
1854 if (p->expr->ts.u.cl && p->expr->ts.u.cl->length)
1856 cl = p->expr->ts.u.cl->length;
1857 gfc_extract_int (cl, &current_length);
1860 /* If gfc_extract_int above set current_length, we implicitly
1861 know the type is BT_INTEGER and it's EXPR_CONSTANT. */
1863 has_ts = (expr->ts.u.cl && expr->ts.u.cl->length_from_typespec);
1865 if (! cl
1866 || (current_length != -1 && current_length != found_length))
1867 gfc_set_constant_character_len (found_length, p->expr,
1868 has_ts ? -1 : found_length);
1872 return SUCCESS;
1876 /* Resolve all of the expressions in an array list. */
1878 gfc_try
1879 gfc_resolve_array_constructor (gfc_expr *expr)
1881 gfc_try t;
1883 t = resolve_array_list (expr->value.constructor);
1884 if (t == SUCCESS)
1885 t = gfc_check_constructor_type (expr);
1887 /* gfc_resolve_character_array_constructor is called in gfc_resolve_expr after
1888 the call to this function, so we don't need to call it here; if it was
1889 called twice, an error message there would be duplicated. */
1891 return t;
1895 /* Copy an iterator structure. */
1897 gfc_iterator *
1898 gfc_copy_iterator (gfc_iterator *src)
1900 gfc_iterator *dest;
1902 if (src == NULL)
1903 return NULL;
1905 dest = gfc_get_iterator ();
1907 dest->var = gfc_copy_expr (src->var);
1908 dest->start = gfc_copy_expr (src->start);
1909 dest->end = gfc_copy_expr (src->end);
1910 dest->step = gfc_copy_expr (src->step);
1912 return dest;
1916 /********* Subroutines for determining the size of an array *********/
1918 /* These are needed just to accommodate RESHAPE(). There are no
1919 diagnostics here, we just return a negative number if something
1920 goes wrong. */
1923 /* Get the size of single dimension of an array specification. The
1924 array is guaranteed to be one dimensional. */
1926 gfc_try
1927 spec_dimen_size (gfc_array_spec *as, int dimen, mpz_t *result)
1929 if (as == NULL)
1930 return FAILURE;
1932 if (dimen < 0 || dimen > as->rank - 1)
1933 gfc_internal_error ("spec_dimen_size(): Bad dimension");
1935 if (as->type != AS_EXPLICIT
1936 || as->lower[dimen]->expr_type != EXPR_CONSTANT
1937 || as->upper[dimen]->expr_type != EXPR_CONSTANT
1938 || as->lower[dimen]->ts.type != BT_INTEGER
1939 || as->upper[dimen]->ts.type != BT_INTEGER)
1940 return FAILURE;
1942 mpz_init (*result);
1944 mpz_sub (*result, as->upper[dimen]->value.integer,
1945 as->lower[dimen]->value.integer);
1947 mpz_add_ui (*result, *result, 1);
1949 return SUCCESS;
1953 gfc_try
1954 spec_size (gfc_array_spec *as, mpz_t *result)
1956 mpz_t size;
1957 int d;
1959 mpz_init_set_ui (*result, 1);
1961 for (d = 0; d < as->rank; d++)
1963 if (spec_dimen_size (as, d, &size) == FAILURE)
1965 mpz_clear (*result);
1966 return FAILURE;
1969 mpz_mul (*result, *result, size);
1970 mpz_clear (size);
1973 return SUCCESS;
1977 /* Get the number of elements in an array section. Optionally, also supply
1978 the end value. */
1980 gfc_try
1981 gfc_ref_dimen_size (gfc_array_ref *ar, int dimen, mpz_t *result, mpz_t *end)
1983 mpz_t upper, lower, stride;
1984 gfc_try t;
1986 if (dimen < 0 || ar == NULL || dimen > ar->dimen - 1)
1987 gfc_internal_error ("gfc_ref_dimen_size(): Bad dimension");
1989 switch (ar->dimen_type[dimen])
1991 case DIMEN_ELEMENT:
1992 mpz_init (*result);
1993 mpz_set_ui (*result, 1);
1994 t = SUCCESS;
1995 break;
1997 case DIMEN_VECTOR:
1998 t = gfc_array_size (ar->start[dimen], result); /* Recurse! */
1999 break;
2001 case DIMEN_RANGE:
2002 mpz_init (upper);
2003 mpz_init (lower);
2004 mpz_init (stride);
2005 t = FAILURE;
2007 if (ar->start[dimen] == NULL)
2009 if (ar->as->lower[dimen] == NULL
2010 || ar->as->lower[dimen]->expr_type != EXPR_CONSTANT)
2011 goto cleanup;
2012 mpz_set (lower, ar->as->lower[dimen]->value.integer);
2014 else
2016 if (ar->start[dimen]->expr_type != EXPR_CONSTANT)
2017 goto cleanup;
2018 mpz_set (lower, ar->start[dimen]->value.integer);
2021 if (ar->end[dimen] == NULL)
2023 if (ar->as->upper[dimen] == NULL
2024 || ar->as->upper[dimen]->expr_type != EXPR_CONSTANT)
2025 goto cleanup;
2026 mpz_set (upper, ar->as->upper[dimen]->value.integer);
2028 else
2030 if (ar->end[dimen]->expr_type != EXPR_CONSTANT)
2031 goto cleanup;
2032 mpz_set (upper, ar->end[dimen]->value.integer);
2035 if (ar->stride[dimen] == NULL)
2036 mpz_set_ui (stride, 1);
2037 else
2039 if (ar->stride[dimen]->expr_type != EXPR_CONSTANT)
2040 goto cleanup;
2041 mpz_set (stride, ar->stride[dimen]->value.integer);
2044 mpz_init (*result);
2045 mpz_sub (*result, upper, lower);
2046 mpz_add (*result, *result, stride);
2047 mpz_div (*result, *result, stride);
2049 /* Zero stride caught earlier. */
2050 if (mpz_cmp_ui (*result, 0) < 0)
2051 mpz_set_ui (*result, 0);
2052 t = SUCCESS;
2054 if (end)
2056 mpz_init (*end);
2058 mpz_sub_ui (*end, *result, 1UL);
2059 mpz_mul (*end, *end, stride);
2060 mpz_add (*end, *end, lower);
2063 cleanup:
2064 mpz_clear (upper);
2065 mpz_clear (lower);
2066 mpz_clear (stride);
2067 return t;
2069 default:
2070 gfc_internal_error ("gfc_ref_dimen_size(): Bad dimen_type");
2073 return t;
2077 static gfc_try
2078 ref_size (gfc_array_ref *ar, mpz_t *result)
2080 mpz_t size;
2081 int d;
2083 mpz_init_set_ui (*result, 1);
2085 for (d = 0; d < ar->dimen; d++)
2087 if (gfc_ref_dimen_size (ar, d, &size, NULL) == FAILURE)
2089 mpz_clear (*result);
2090 return FAILURE;
2093 mpz_mul (*result, *result, size);
2094 mpz_clear (size);
2097 return SUCCESS;
2101 /* Given an array expression and a dimension, figure out how many
2102 elements it has along that dimension. Returns SUCCESS if we were
2103 able to return a result in the 'result' variable, FAILURE
2104 otherwise. */
2106 gfc_try
2107 gfc_array_dimen_size (gfc_expr *array, int dimen, mpz_t *result)
2109 gfc_ref *ref;
2110 int i;
2112 if (dimen < 0 || array == NULL || dimen > array->rank - 1)
2113 gfc_internal_error ("gfc_array_dimen_size(): Bad dimension");
2115 switch (array->expr_type)
2117 case EXPR_VARIABLE:
2118 case EXPR_FUNCTION:
2119 for (ref = array->ref; ref; ref = ref->next)
2121 if (ref->type != REF_ARRAY)
2122 continue;
2124 if (ref->u.ar.type == AR_FULL)
2125 return spec_dimen_size (ref->u.ar.as, dimen, result);
2127 if (ref->u.ar.type == AR_SECTION)
2129 for (i = 0; dimen >= 0; i++)
2130 if (ref->u.ar.dimen_type[i] != DIMEN_ELEMENT)
2131 dimen--;
2133 return gfc_ref_dimen_size (&ref->u.ar, i - 1, result, NULL);
2137 if (array->shape && array->shape[dimen])
2139 mpz_init_set (*result, array->shape[dimen]);
2140 return SUCCESS;
2143 if (array->symtree->n.sym->attr.generic
2144 && array->value.function.esym != NULL)
2146 if (spec_dimen_size (array->value.function.esym->as, dimen, result)
2147 == FAILURE)
2148 return FAILURE;
2150 else if (spec_dimen_size (array->symtree->n.sym->as, dimen, result)
2151 == FAILURE)
2152 return FAILURE;
2154 break;
2156 case EXPR_ARRAY:
2157 if (array->shape == NULL) {
2158 /* Expressions with rank > 1 should have "shape" properly set */
2159 if ( array->rank != 1 )
2160 gfc_internal_error ("gfc_array_dimen_size(): Bad EXPR_ARRAY expr");
2161 return gfc_array_size(array, result);
2164 /* Fall through */
2165 default:
2166 if (array->shape == NULL)
2167 return FAILURE;
2169 mpz_init_set (*result, array->shape[dimen]);
2171 break;
2174 return SUCCESS;
2178 /* Given an array expression, figure out how many elements are in the
2179 array. Returns SUCCESS if this is possible, and sets the 'result'
2180 variable. Otherwise returns FAILURE. */
2182 gfc_try
2183 gfc_array_size (gfc_expr *array, mpz_t *result)
2185 expand_info expand_save;
2186 gfc_ref *ref;
2187 int i;
2188 gfc_try t;
2190 switch (array->expr_type)
2192 case EXPR_ARRAY:
2193 gfc_push_suppress_errors ();
2195 expand_save = current_expand;
2197 current_expand.count = result;
2198 mpz_init_set_ui (*result, 0);
2200 current_expand.expand_work_function = count_elements;
2201 iter_stack = NULL;
2203 t = expand_constructor (array->value.constructor);
2205 gfc_pop_suppress_errors ();
2207 if (t == FAILURE)
2208 mpz_clear (*result);
2209 current_expand = expand_save;
2210 return t;
2212 case EXPR_VARIABLE:
2213 for (ref = array->ref; ref; ref = ref->next)
2215 if (ref->type != REF_ARRAY)
2216 continue;
2218 if (ref->u.ar.type == AR_FULL)
2219 return spec_size (ref->u.ar.as, result);
2221 if (ref->u.ar.type == AR_SECTION)
2222 return ref_size (&ref->u.ar, result);
2225 return spec_size (array->symtree->n.sym->as, result);
2228 default:
2229 if (array->rank == 0 || array->shape == NULL)
2230 return FAILURE;
2232 mpz_init_set_ui (*result, 1);
2234 for (i = 0; i < array->rank; i++)
2235 mpz_mul (*result, *result, array->shape[i]);
2237 break;
2240 return SUCCESS;
2244 /* Given an array reference, return the shape of the reference in an
2245 array of mpz_t integers. */
2247 gfc_try
2248 gfc_array_ref_shape (gfc_array_ref *ar, mpz_t *shape)
2250 int d;
2251 int i;
2253 d = 0;
2255 switch (ar->type)
2257 case AR_FULL:
2258 for (; d < ar->as->rank; d++)
2259 if (spec_dimen_size (ar->as, d, &shape[d]) == FAILURE)
2260 goto cleanup;
2262 return SUCCESS;
2264 case AR_SECTION:
2265 for (i = 0; i < ar->dimen; i++)
2267 if (ar->dimen_type[i] != DIMEN_ELEMENT)
2269 if (gfc_ref_dimen_size (ar, i, &shape[d], NULL) == FAILURE)
2270 goto cleanup;
2271 d++;
2275 return SUCCESS;
2277 default:
2278 break;
2281 cleanup:
2282 for (d--; d >= 0; d--)
2283 mpz_clear (shape[d]);
2285 return FAILURE;
2289 /* Given an array expression, find the array reference structure that
2290 characterizes the reference. */
2292 gfc_array_ref *
2293 gfc_find_array_ref (gfc_expr *e)
2295 gfc_ref *ref;
2297 for (ref = e->ref; ref; ref = ref->next)
2298 if (ref->type == REF_ARRAY
2299 && (ref->u.ar.type == AR_FULL || ref->u.ar.type == AR_SECTION
2300 || (ref->u.ar.type == AR_ELEMENT && ref->u.ar.dimen == 0)))
2301 break;
2303 if (ref == NULL)
2304 gfc_internal_error ("gfc_find_array_ref(): No ref found");
2306 return &ref->u.ar;
2310 /* Find out if an array shape is known at compile time. */
2313 gfc_is_compile_time_shape (gfc_array_spec *as)
2315 int i;
2317 if (as->type != AS_EXPLICIT)
2318 return 0;
2320 for (i = 0; i < as->rank; i++)
2321 if (!gfc_is_constant_expr (as->lower[i])
2322 || !gfc_is_constant_expr (as->upper[i]))
2323 return 0;
2325 return 1;