2015-05-05 Yvan Roux <yvan.roux@linaro.org>
[official-gcc.git] / gcc / fortran / array.c
blob64d0abf8fa448e23e7986a8720a4c4cef419a9d8
1 /* Array things
2 Copyright (C) 2000-2015 Free Software Foundation, Inc.
3 Contributed by Andy Vaught
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "flags.h"
25 #include "gfortran.h"
26 #include "match.h"
27 #include "constructor.h"
29 /**************** Array reference matching subroutines *****************/
31 /* Copy an array reference structure. */
33 gfc_array_ref *
34 gfc_copy_array_ref (gfc_array_ref *src)
36 gfc_array_ref *dest;
37 int i;
39 if (src == NULL)
40 return NULL;
42 dest = gfc_get_array_ref ();
44 *dest = *src;
46 for (i = 0; i < GFC_MAX_DIMENSIONS; i++)
48 dest->start[i] = gfc_copy_expr (src->start[i]);
49 dest->end[i] = gfc_copy_expr (src->end[i]);
50 dest->stride[i] = gfc_copy_expr (src->stride[i]);
53 return dest;
57 /* Match a single dimension of an array reference. This can be a
58 single element or an array section. Any modifications we've made
59 to the ar structure are cleaned up by the caller. If the init
60 is set, we require the subscript to be a valid initialization
61 expression. */
63 static match
64 match_subscript (gfc_array_ref *ar, int init, bool match_star)
66 match m = MATCH_ERROR;
67 bool star = false;
68 int i;
70 i = ar->dimen + ar->codimen;
72 gfc_gobble_whitespace ();
73 ar->c_where[i] = gfc_current_locus;
74 ar->start[i] = ar->end[i] = ar->stride[i] = NULL;
76 /* We can't be sure of the difference between DIMEN_ELEMENT and
77 DIMEN_VECTOR until we know the type of the element itself at
78 resolution time. */
80 ar->dimen_type[i] = DIMEN_UNKNOWN;
82 if (gfc_match_char (':') == MATCH_YES)
83 goto end_element;
85 /* Get start element. */
86 if (match_star && (m = gfc_match_char ('*')) == MATCH_YES)
87 star = true;
89 if (!star && init)
90 m = gfc_match_init_expr (&ar->start[i]);
91 else if (!star)
92 m = gfc_match_expr (&ar->start[i]);
94 if (m == MATCH_NO)
95 gfc_error ("Expected array subscript at %C");
96 if (m != MATCH_YES)
97 return MATCH_ERROR;
99 if (gfc_match_char (':') == MATCH_NO)
100 goto matched;
102 if (star)
104 gfc_error ("Unexpected %<*%> in coarray subscript at %C");
105 return MATCH_ERROR;
108 /* Get an optional end element. Because we've seen the colon, we
109 definitely have a range along this dimension. */
110 end_element:
111 ar->dimen_type[i] = DIMEN_RANGE;
113 if (match_star && (m = gfc_match_char ('*')) == MATCH_YES)
114 star = true;
115 else if (init)
116 m = gfc_match_init_expr (&ar->end[i]);
117 else
118 m = gfc_match_expr (&ar->end[i]);
120 if (m == MATCH_ERROR)
121 return MATCH_ERROR;
123 /* See if we have an optional stride. */
124 if (gfc_match_char (':') == MATCH_YES)
126 if (star)
128 gfc_error ("Strides not allowed in coarray subscript at %C");
129 return MATCH_ERROR;
132 m = init ? gfc_match_init_expr (&ar->stride[i])
133 : gfc_match_expr (&ar->stride[i]);
135 if (m == MATCH_NO)
136 gfc_error ("Expected array subscript stride at %C");
137 if (m != MATCH_YES)
138 return MATCH_ERROR;
141 matched:
142 if (star)
143 ar->dimen_type[i] = DIMEN_STAR;
145 return MATCH_YES;
149 /* Match an array reference, whether it is the whole array or a
150 particular elements or a section. If init is set, the reference has
151 to consist of init expressions. */
153 match
154 gfc_match_array_ref (gfc_array_ref *ar, gfc_array_spec *as, int init,
155 int corank)
157 match m;
158 bool matched_bracket = false;
160 memset (ar, '\0', sizeof (*ar));
162 ar->where = gfc_current_locus;
163 ar->as = as;
164 ar->type = AR_UNKNOWN;
166 if (gfc_match_char ('[') == MATCH_YES)
168 matched_bracket = true;
169 goto coarray;
172 if (gfc_match_char ('(') != MATCH_YES)
174 ar->type = AR_FULL;
175 ar->dimen = 0;
176 return MATCH_YES;
179 for (ar->dimen = 0; ar->dimen < GFC_MAX_DIMENSIONS; ar->dimen++)
181 m = match_subscript (ar, init, false);
182 if (m == MATCH_ERROR)
183 return MATCH_ERROR;
185 if (gfc_match_char (')') == MATCH_YES)
187 ar->dimen++;
188 goto coarray;
191 if (gfc_match_char (',') != MATCH_YES)
193 gfc_error ("Invalid form of array reference at %C");
194 return MATCH_ERROR;
198 gfc_error ("Array reference at %C cannot have more than %d dimensions",
199 GFC_MAX_DIMENSIONS);
200 return MATCH_ERROR;
202 coarray:
203 if (!matched_bracket && gfc_match_char ('[') != MATCH_YES)
205 if (ar->dimen > 0)
206 return MATCH_YES;
207 else
208 return MATCH_ERROR;
211 if (flag_coarray == GFC_FCOARRAY_NONE)
213 gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to enable");
214 return MATCH_ERROR;
217 if (corank == 0)
219 gfc_error ("Unexpected coarray designator at %C");
220 return MATCH_ERROR;
223 for (ar->codimen = 0; ar->codimen + ar->dimen < GFC_MAX_DIMENSIONS; ar->codimen++)
225 m = match_subscript (ar, init, true);
226 if (m == MATCH_ERROR)
227 return MATCH_ERROR;
229 if (gfc_match_char (']') == MATCH_YES)
231 ar->codimen++;
232 if (ar->codimen < corank)
234 gfc_error ("Too few codimensions at %C, expected %d not %d",
235 corank, ar->codimen);
236 return MATCH_ERROR;
238 if (ar->codimen > corank)
240 gfc_error ("Too many codimensions at %C, expected %d not %d",
241 corank, ar->codimen);
242 return MATCH_ERROR;
244 return MATCH_YES;
247 if (gfc_match_char (',') != MATCH_YES)
249 if (gfc_match_char ('*') == MATCH_YES)
250 gfc_error ("Unexpected %<*%> for codimension %d of %d at %C",
251 ar->codimen + 1, corank);
252 else
253 gfc_error ("Invalid form of coarray reference at %C");
254 return MATCH_ERROR;
256 else if (ar->dimen_type[ar->codimen + ar->dimen] == DIMEN_STAR)
258 gfc_error ("Unexpected %<*%> for codimension %d of %d at %C",
259 ar->codimen + 1, corank);
260 return MATCH_ERROR;
263 if (ar->codimen >= corank)
265 gfc_error ("Invalid codimension %d at %C, only %d codimensions exist",
266 ar->codimen + 1, corank);
267 return MATCH_ERROR;
271 gfc_error ("Array reference at %C cannot have more than %d dimensions",
272 GFC_MAX_DIMENSIONS);
273 return MATCH_ERROR;
278 /************** Array specification matching subroutines ***************/
280 /* Free all of the expressions associated with array bounds
281 specifications. */
283 void
284 gfc_free_array_spec (gfc_array_spec *as)
286 int i;
288 if (as == NULL)
289 return;
291 for (i = 0; i < as->rank + as->corank; i++)
293 gfc_free_expr (as->lower[i]);
294 gfc_free_expr (as->upper[i]);
297 free (as);
301 /* Take an array bound, resolves the expression, that make up the
302 shape and check associated constraints. */
304 static bool
305 resolve_array_bound (gfc_expr *e, int check_constant)
307 if (e == NULL)
308 return true;
310 if (!gfc_resolve_expr (e)
311 || !gfc_specification_expr (e))
312 return false;
314 if (check_constant && !gfc_is_constant_expr (e))
316 if (e->expr_type == EXPR_VARIABLE)
317 gfc_error ("Variable %qs at %L in this context must be constant",
318 e->symtree->n.sym->name, &e->where);
319 else
320 gfc_error ("Expression at %L in this context must be constant",
321 &e->where);
322 return false;
325 return true;
329 /* Takes an array specification, resolves the expressions that make up
330 the shape and make sure everything is integral. */
332 bool
333 gfc_resolve_array_spec (gfc_array_spec *as, int check_constant)
335 gfc_expr *e;
336 int i;
338 if (as == NULL)
339 return true;
341 for (i = 0; i < as->rank + as->corank; i++)
343 e = as->lower[i];
344 if (!resolve_array_bound (e, check_constant))
345 return false;
347 e = as->upper[i];
348 if (!resolve_array_bound (e, check_constant))
349 return false;
351 if ((as->lower[i] == NULL) || (as->upper[i] == NULL))
352 continue;
354 /* If the size is negative in this dimension, set it to zero. */
355 if (as->lower[i]->expr_type == EXPR_CONSTANT
356 && as->upper[i]->expr_type == EXPR_CONSTANT
357 && mpz_cmp (as->upper[i]->value.integer,
358 as->lower[i]->value.integer) < 0)
360 gfc_free_expr (as->upper[i]);
361 as->upper[i] = gfc_copy_expr (as->lower[i]);
362 mpz_sub_ui (as->upper[i]->value.integer,
363 as->upper[i]->value.integer, 1);
367 return true;
371 /* Match a single array element specification. The return values as
372 well as the upper and lower bounds of the array spec are filled
373 in according to what we see on the input. The caller makes sure
374 individual specifications make sense as a whole.
377 Parsed Lower Upper Returned
378 ------------------------------------
379 : NULL NULL AS_DEFERRED (*)
380 x 1 x AS_EXPLICIT
381 x: x NULL AS_ASSUMED_SHAPE
382 x:y x y AS_EXPLICIT
383 x:* x NULL AS_ASSUMED_SIZE
384 * 1 NULL AS_ASSUMED_SIZE
386 (*) For non-pointer dummy arrays this is AS_ASSUMED_SHAPE. This
387 is fixed during the resolution of formal interfaces.
389 Anything else AS_UNKNOWN. */
391 static array_type
392 match_array_element_spec (gfc_array_spec *as)
394 gfc_expr **upper, **lower;
395 match m;
396 int rank;
398 rank = as->rank == -1 ? 0 : as->rank;
399 lower = &as->lower[rank + as->corank - 1];
400 upper = &as->upper[rank + as->corank - 1];
402 if (gfc_match_char ('*') == MATCH_YES)
404 *lower = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
405 return AS_ASSUMED_SIZE;
408 if (gfc_match_char (':') == MATCH_YES)
409 return AS_DEFERRED;
411 m = gfc_match_expr (upper);
412 if (m == MATCH_NO)
413 gfc_error ("Expected expression in array specification at %C");
414 if (m != MATCH_YES)
415 return AS_UNKNOWN;
416 if (!gfc_expr_check_typed (*upper, gfc_current_ns, false))
417 return AS_UNKNOWN;
419 if (gfc_match_char (':') == MATCH_NO)
421 *lower = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
422 return AS_EXPLICIT;
425 *lower = *upper;
426 *upper = NULL;
428 if (gfc_match_char ('*') == MATCH_YES)
429 return AS_ASSUMED_SIZE;
431 m = gfc_match_expr (upper);
432 if (m == MATCH_ERROR)
433 return AS_UNKNOWN;
434 if (m == MATCH_NO)
435 return AS_ASSUMED_SHAPE;
436 if (!gfc_expr_check_typed (*upper, gfc_current_ns, false))
437 return AS_UNKNOWN;
439 return AS_EXPLICIT;
443 /* Matches an array specification, incidentally figuring out what sort
444 it is. Match either a normal array specification, or a coarray spec
445 or both. Optionally allow [:] for coarrays. */
447 match
448 gfc_match_array_spec (gfc_array_spec **asp, bool match_dim, bool match_codim)
450 array_type current_type;
451 gfc_array_spec *as;
452 int i;
454 as = gfc_get_array_spec ();
456 if (!match_dim)
457 goto coarray;
459 if (gfc_match_char ('(') != MATCH_YES)
461 if (!match_codim)
462 goto done;
463 goto coarray;
466 if (gfc_match (" .. )") == MATCH_YES)
468 as->type = AS_ASSUMED_RANK;
469 as->rank = -1;
471 if (!gfc_notify_std (GFC_STD_F2008_TS, "Assumed-rank array at %C"))
472 goto cleanup;
474 if (!match_codim)
475 goto done;
476 goto coarray;
479 for (;;)
481 as->rank++;
482 current_type = match_array_element_spec (as);
484 /* Note that current_type == AS_ASSUMED_SIZE for both assumed-size
485 and implied-shape specifications. If the rank is at least 2, we can
486 distinguish between them. But for rank 1, we currently return
487 ASSUMED_SIZE; this gets adjusted later when we know for sure
488 whether the symbol parsed is a PARAMETER or not. */
490 if (as->rank == 1)
492 if (current_type == AS_UNKNOWN)
493 goto cleanup;
494 as->type = current_type;
496 else
497 switch (as->type)
498 { /* See how current spec meshes with the existing. */
499 case AS_UNKNOWN:
500 goto cleanup;
502 case AS_IMPLIED_SHAPE:
503 if (current_type != AS_ASSUMED_SHAPE)
505 gfc_error ("Bad array specification for implied-shape"
506 " array at %C");
507 goto cleanup;
509 break;
511 case AS_EXPLICIT:
512 if (current_type == AS_ASSUMED_SIZE)
514 as->type = AS_ASSUMED_SIZE;
515 break;
518 if (current_type == AS_EXPLICIT)
519 break;
521 gfc_error ("Bad array specification for an explicitly shaped "
522 "array at %C");
524 goto cleanup;
526 case AS_ASSUMED_SHAPE:
527 if ((current_type == AS_ASSUMED_SHAPE)
528 || (current_type == AS_DEFERRED))
529 break;
531 gfc_error ("Bad array specification for assumed shape "
532 "array at %C");
533 goto cleanup;
535 case AS_DEFERRED:
536 if (current_type == AS_DEFERRED)
537 break;
539 if (current_type == AS_ASSUMED_SHAPE)
541 as->type = AS_ASSUMED_SHAPE;
542 break;
545 gfc_error ("Bad specification for deferred shape array at %C");
546 goto cleanup;
548 case AS_ASSUMED_SIZE:
549 if (as->rank == 2 && current_type == AS_ASSUMED_SIZE)
551 as->type = AS_IMPLIED_SHAPE;
552 break;
555 gfc_error ("Bad specification for assumed size array at %C");
556 goto cleanup;
558 case AS_ASSUMED_RANK:
559 gcc_unreachable ();
562 if (gfc_match_char (')') == MATCH_YES)
563 break;
565 if (gfc_match_char (',') != MATCH_YES)
567 gfc_error ("Expected another dimension in array declaration at %C");
568 goto cleanup;
571 if (as->rank + as->corank >= GFC_MAX_DIMENSIONS)
573 gfc_error ("Array specification at %C has more than %d dimensions",
574 GFC_MAX_DIMENSIONS);
575 goto cleanup;
578 if (as->corank + as->rank >= 7
579 && !gfc_notify_std (GFC_STD_F2008, "Array specification at %C "
580 "with more than 7 dimensions"))
581 goto cleanup;
584 if (!match_codim)
585 goto done;
587 coarray:
588 if (gfc_match_char ('[') != MATCH_YES)
589 goto done;
591 if (!gfc_notify_std (GFC_STD_F2008, "Coarray declaration at %C"))
592 goto cleanup;
594 if (flag_coarray == GFC_FCOARRAY_NONE)
596 gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to enable");
597 goto cleanup;
600 if (as->rank >= GFC_MAX_DIMENSIONS)
602 gfc_error ("Array specification at %C has more than %d "
603 "dimensions", GFC_MAX_DIMENSIONS);
604 goto cleanup;
607 for (;;)
609 as->corank++;
610 current_type = match_array_element_spec (as);
612 if (current_type == AS_UNKNOWN)
613 goto cleanup;
615 if (as->corank == 1)
616 as->cotype = current_type;
617 else
618 switch (as->cotype)
619 { /* See how current spec meshes with the existing. */
620 case AS_IMPLIED_SHAPE:
621 case AS_UNKNOWN:
622 goto cleanup;
624 case AS_EXPLICIT:
625 if (current_type == AS_ASSUMED_SIZE)
627 as->cotype = AS_ASSUMED_SIZE;
628 break;
631 if (current_type == AS_EXPLICIT)
632 break;
634 gfc_error ("Bad array specification for an explicitly "
635 "shaped array at %C");
637 goto cleanup;
639 case AS_ASSUMED_SHAPE:
640 if ((current_type == AS_ASSUMED_SHAPE)
641 || (current_type == AS_DEFERRED))
642 break;
644 gfc_error ("Bad array specification for assumed shape "
645 "array at %C");
646 goto cleanup;
648 case AS_DEFERRED:
649 if (current_type == AS_DEFERRED)
650 break;
652 if (current_type == AS_ASSUMED_SHAPE)
654 as->cotype = AS_ASSUMED_SHAPE;
655 break;
658 gfc_error ("Bad specification for deferred shape array at %C");
659 goto cleanup;
661 case AS_ASSUMED_SIZE:
662 gfc_error ("Bad specification for assumed size array at %C");
663 goto cleanup;
665 case AS_ASSUMED_RANK:
666 gcc_unreachable ();
669 if (gfc_match_char (']') == MATCH_YES)
670 break;
672 if (gfc_match_char (',') != MATCH_YES)
674 gfc_error ("Expected another dimension in array declaration at %C");
675 goto cleanup;
678 if (as->rank + as->corank >= GFC_MAX_DIMENSIONS)
680 gfc_error ("Array specification at %C has more than %d "
681 "dimensions", GFC_MAX_DIMENSIONS);
682 goto cleanup;
686 if (current_type == AS_EXPLICIT)
688 gfc_error ("Upper bound of last coarray dimension must be %<*%> at %C");
689 goto cleanup;
692 if (as->cotype == AS_ASSUMED_SIZE)
693 as->cotype = AS_EXPLICIT;
695 if (as->rank == 0)
696 as->type = as->cotype;
698 done:
699 if (as->rank == 0 && as->corank == 0)
701 *asp = NULL;
702 gfc_free_array_spec (as);
703 return MATCH_NO;
706 /* If a lower bounds of an assumed shape array is blank, put in one. */
707 if (as->type == AS_ASSUMED_SHAPE)
709 for (i = 0; i < as->rank + as->corank; i++)
711 if (as->lower[i] == NULL)
712 as->lower[i] = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
716 *asp = as;
718 return MATCH_YES;
720 cleanup:
721 /* Something went wrong. */
722 gfc_free_array_spec (as);
723 return MATCH_ERROR;
727 /* Given a symbol and an array specification, modify the symbol to
728 have that array specification. The error locus is needed in case
729 something goes wrong. On failure, the caller must free the spec. */
731 bool
732 gfc_set_array_spec (gfc_symbol *sym, gfc_array_spec *as, locus *error_loc)
734 int i;
736 if (as == NULL)
737 return true;
739 if (as->rank
740 && !gfc_add_dimension (&sym->attr, sym->name, error_loc))
741 return false;
743 if (as->corank
744 && !gfc_add_codimension (&sym->attr, sym->name, error_loc))
745 return false;
747 if (sym->as == NULL)
749 sym->as = as;
750 return true;
753 if ((sym->as->type == AS_ASSUMED_RANK && as->corank)
754 || (as->type == AS_ASSUMED_RANK && sym->as->corank))
756 gfc_error ("The assumed-rank array %qs at %L shall not have a "
757 "codimension", sym->name, error_loc);
758 return false;
761 if (as->corank)
763 /* The "sym" has no corank (checked via gfc_add_codimension). Thus
764 the codimension is simply added. */
765 gcc_assert (as->rank == 0 && sym->as->corank == 0);
767 sym->as->cotype = as->cotype;
768 sym->as->corank = as->corank;
769 for (i = 0; i < as->corank; i++)
771 sym->as->lower[sym->as->rank + i] = as->lower[i];
772 sym->as->upper[sym->as->rank + i] = as->upper[i];
775 else
777 /* The "sym" has no rank (checked via gfc_add_dimension). Thus
778 the dimension is added - but first the codimensions (if existing
779 need to be shifted to make space for the dimension. */
780 gcc_assert (as->corank == 0 && sym->as->rank == 0);
782 sym->as->rank = as->rank;
783 sym->as->type = as->type;
784 sym->as->cray_pointee = as->cray_pointee;
785 sym->as->cp_was_assumed = as->cp_was_assumed;
787 for (i = 0; i < sym->as->corank; i++)
789 sym->as->lower[as->rank + i] = sym->as->lower[i];
790 sym->as->upper[as->rank + i] = sym->as->upper[i];
792 for (i = 0; i < as->rank; i++)
794 sym->as->lower[i] = as->lower[i];
795 sym->as->upper[i] = as->upper[i];
799 free (as);
800 return true;
804 /* Copy an array specification. */
806 gfc_array_spec *
807 gfc_copy_array_spec (gfc_array_spec *src)
809 gfc_array_spec *dest;
810 int i;
812 if (src == NULL)
813 return NULL;
815 dest = gfc_get_array_spec ();
817 *dest = *src;
819 for (i = 0; i < dest->rank + dest->corank; i++)
821 dest->lower[i] = gfc_copy_expr (dest->lower[i]);
822 dest->upper[i] = gfc_copy_expr (dest->upper[i]);
825 return dest;
829 /* Returns nonzero if the two expressions are equal. Only handles integer
830 constants. */
832 static int
833 compare_bounds (gfc_expr *bound1, gfc_expr *bound2)
835 if (bound1 == NULL || bound2 == NULL
836 || bound1->expr_type != EXPR_CONSTANT
837 || bound2->expr_type != EXPR_CONSTANT
838 || bound1->ts.type != BT_INTEGER
839 || bound2->ts.type != BT_INTEGER)
840 gfc_internal_error ("gfc_compare_array_spec(): Array spec clobbered");
842 if (mpz_cmp (bound1->value.integer, bound2->value.integer) == 0)
843 return 1;
844 else
845 return 0;
849 /* Compares two array specifications. They must be constant or deferred
850 shape. */
853 gfc_compare_array_spec (gfc_array_spec *as1, gfc_array_spec *as2)
855 int i;
857 if (as1 == NULL && as2 == NULL)
858 return 1;
860 if (as1 == NULL || as2 == NULL)
861 return 0;
863 if (as1->rank != as2->rank)
864 return 0;
866 if (as1->corank != as2->corank)
867 return 0;
869 if (as1->rank == 0)
870 return 1;
872 if (as1->type != as2->type)
873 return 0;
875 if (as1->type == AS_EXPLICIT)
876 for (i = 0; i < as1->rank + as1->corank; i++)
878 if (compare_bounds (as1->lower[i], as2->lower[i]) == 0)
879 return 0;
881 if (compare_bounds (as1->upper[i], as2->upper[i]) == 0)
882 return 0;
885 return 1;
889 /****************** Array constructor functions ******************/
892 /* Given an expression node that might be an array constructor and a
893 symbol, make sure that no iterators in this or child constructors
894 use the symbol as an implied-DO iterator. Returns nonzero if a
895 duplicate was found. */
897 static int
898 check_duplicate_iterator (gfc_constructor_base base, gfc_symbol *master)
900 gfc_constructor *c;
901 gfc_expr *e;
903 for (c = gfc_constructor_first (base); c; c = gfc_constructor_next (c))
905 e = c->expr;
907 if (e->expr_type == EXPR_ARRAY
908 && check_duplicate_iterator (e->value.constructor, master))
909 return 1;
911 if (c->iterator == NULL)
912 continue;
914 if (c->iterator->var->symtree->n.sym == master)
916 gfc_error ("DO-iterator %qs at %L is inside iterator of the "
917 "same name", master->name, &c->where);
919 return 1;
923 return 0;
927 /* Forward declaration because these functions are mutually recursive. */
928 static match match_array_cons_element (gfc_constructor_base *);
930 /* Match a list of array elements. */
932 static match
933 match_array_list (gfc_constructor_base *result)
935 gfc_constructor_base head;
936 gfc_constructor *p;
937 gfc_iterator iter;
938 locus old_loc;
939 gfc_expr *e;
940 match m;
941 int n;
943 old_loc = gfc_current_locus;
945 if (gfc_match_char ('(') == MATCH_NO)
946 return MATCH_NO;
948 memset (&iter, '\0', sizeof (gfc_iterator));
949 head = NULL;
951 m = match_array_cons_element (&head);
952 if (m != MATCH_YES)
953 goto cleanup;
955 if (gfc_match_char (',') != MATCH_YES)
957 m = MATCH_NO;
958 goto cleanup;
961 for (n = 1;; n++)
963 m = gfc_match_iterator (&iter, 0);
964 if (m == MATCH_YES)
965 break;
966 if (m == MATCH_ERROR)
967 goto cleanup;
969 m = match_array_cons_element (&head);
970 if (m == MATCH_ERROR)
971 goto cleanup;
972 if (m == MATCH_NO)
974 if (n > 2)
975 goto syntax;
976 m = MATCH_NO;
977 goto cleanup; /* Could be a complex constant */
980 if (gfc_match_char (',') != MATCH_YES)
982 if (n > 2)
983 goto syntax;
984 m = MATCH_NO;
985 goto cleanup;
989 if (gfc_match_char (')') != MATCH_YES)
990 goto syntax;
992 if (check_duplicate_iterator (head, iter.var->symtree->n.sym))
994 m = MATCH_ERROR;
995 goto cleanup;
998 e = gfc_get_array_expr (BT_UNKNOWN, 0, &old_loc);
999 e->value.constructor = head;
1001 p = gfc_constructor_append_expr (result, e, &gfc_current_locus);
1002 p->iterator = gfc_get_iterator ();
1003 *p->iterator = iter;
1005 return MATCH_YES;
1007 syntax:
1008 gfc_error ("Syntax error in array constructor at %C");
1009 m = MATCH_ERROR;
1011 cleanup:
1012 gfc_constructor_free (head);
1013 gfc_free_iterator (&iter, 0);
1014 gfc_current_locus = old_loc;
1015 return m;
1019 /* Match a single element of an array constructor, which can be a
1020 single expression or a list of elements. */
1022 static match
1023 match_array_cons_element (gfc_constructor_base *result)
1025 gfc_expr *expr;
1026 match m;
1028 m = match_array_list (result);
1029 if (m != MATCH_NO)
1030 return m;
1032 m = gfc_match_expr (&expr);
1033 if (m != MATCH_YES)
1034 return m;
1036 gfc_constructor_append_expr (result, expr, &gfc_current_locus);
1037 return MATCH_YES;
1041 /* Match an array constructor. */
1043 match
1044 gfc_match_array_constructor (gfc_expr **result)
1046 gfc_constructor_base head, new_cons;
1047 gfc_undo_change_set changed_syms;
1048 gfc_expr *expr;
1049 gfc_typespec ts;
1050 locus where;
1051 match m;
1052 const char *end_delim;
1053 bool seen_ts;
1055 if (gfc_match (" (/") == MATCH_NO)
1057 if (gfc_match (" [") == MATCH_NO)
1058 return MATCH_NO;
1059 else
1061 if (!gfc_notify_std (GFC_STD_F2003, "[...] "
1062 "style array constructors at %C"))
1063 return MATCH_ERROR;
1064 end_delim = " ]";
1067 else
1068 end_delim = " /)";
1070 where = gfc_current_locus;
1071 head = new_cons = NULL;
1072 seen_ts = false;
1074 /* Try to match an optional "type-spec ::" */
1075 gfc_clear_ts (&ts);
1076 gfc_new_undo_checkpoint (changed_syms);
1077 if (gfc_match_type_spec (&ts) == MATCH_YES)
1079 seen_ts = (gfc_match (" ::") == MATCH_YES);
1081 if (seen_ts)
1083 if (!gfc_notify_std (GFC_STD_F2003, "Array constructor "
1084 "including type specification at %C"))
1086 gfc_restore_last_undo_checkpoint ();
1087 goto cleanup;
1090 if (ts.deferred)
1092 gfc_error ("Type-spec at %L cannot contain a deferred "
1093 "type parameter", &where);
1094 gfc_restore_last_undo_checkpoint ();
1095 goto cleanup;
1100 if (seen_ts)
1101 gfc_drop_last_undo_checkpoint ();
1102 else
1104 gfc_restore_last_undo_checkpoint ();
1105 gfc_current_locus = where;
1108 if (gfc_match (end_delim) == MATCH_YES)
1110 if (seen_ts)
1111 goto done;
1112 else
1114 gfc_error ("Empty array constructor at %C is not allowed");
1115 goto cleanup;
1119 for (;;)
1121 m = match_array_cons_element (&head);
1122 if (m == MATCH_ERROR)
1123 goto cleanup;
1124 if (m == MATCH_NO)
1125 goto syntax;
1127 if (gfc_match_char (',') == MATCH_NO)
1128 break;
1131 if (gfc_match (end_delim) == MATCH_NO)
1132 goto syntax;
1134 done:
1135 /* Size must be calculated at resolution time. */
1136 if (seen_ts)
1138 expr = gfc_get_array_expr (ts.type, ts.kind, &where);
1139 expr->ts = ts;
1141 else
1142 expr = gfc_get_array_expr (BT_UNKNOWN, 0, &where);
1144 expr->value.constructor = head;
1145 if (expr->ts.u.cl)
1146 expr->ts.u.cl->length_from_typespec = seen_ts;
1148 *result = expr;
1149 return MATCH_YES;
1151 syntax:
1152 gfc_error ("Syntax error in array constructor at %C");
1154 cleanup:
1155 gfc_constructor_free (head);
1156 return MATCH_ERROR;
1161 /************** Check array constructors for correctness **************/
1163 /* Given an expression, compare it's type with the type of the current
1164 constructor. Returns nonzero if an error was issued. The
1165 cons_state variable keeps track of whether the type of the
1166 constructor being read or resolved is known to be good, bad or just
1167 starting out. */
1169 static gfc_typespec constructor_ts;
1170 static enum
1171 { CONS_START, CONS_GOOD, CONS_BAD }
1172 cons_state;
1174 static int
1175 check_element_type (gfc_expr *expr, bool convert)
1177 if (cons_state == CONS_BAD)
1178 return 0; /* Suppress further errors */
1180 if (cons_state == CONS_START)
1182 if (expr->ts.type == BT_UNKNOWN)
1183 cons_state = CONS_BAD;
1184 else
1186 cons_state = CONS_GOOD;
1187 constructor_ts = expr->ts;
1190 return 0;
1193 if (gfc_compare_types (&constructor_ts, &expr->ts))
1194 return 0;
1196 if (convert)
1197 return gfc_convert_type(expr, &constructor_ts, 1) ? 0 : 1;
1199 gfc_error ("Element in %s array constructor at %L is %s",
1200 gfc_typename (&constructor_ts), &expr->where,
1201 gfc_typename (&expr->ts));
1203 cons_state = CONS_BAD;
1204 return 1;
1208 /* Recursive work function for gfc_check_constructor_type(). */
1210 static bool
1211 check_constructor_type (gfc_constructor_base base, bool convert)
1213 gfc_constructor *c;
1214 gfc_expr *e;
1216 for (c = gfc_constructor_first (base); c; c = gfc_constructor_next (c))
1218 e = c->expr;
1220 if (e->expr_type == EXPR_ARRAY)
1222 if (!check_constructor_type (e->value.constructor, convert))
1223 return false;
1225 continue;
1228 if (check_element_type (e, convert))
1229 return false;
1232 return true;
1236 /* Check that all elements of an array constructor are the same type.
1237 On false, an error has been generated. */
1239 bool
1240 gfc_check_constructor_type (gfc_expr *e)
1242 bool t;
1244 if (e->ts.type != BT_UNKNOWN)
1246 cons_state = CONS_GOOD;
1247 constructor_ts = e->ts;
1249 else
1251 cons_state = CONS_START;
1252 gfc_clear_ts (&constructor_ts);
1255 /* If e->ts.type != BT_UNKNOWN, the array constructor included a
1256 typespec, and we will now convert the values on the fly. */
1257 t = check_constructor_type (e->value.constructor, e->ts.type != BT_UNKNOWN);
1258 if (t && e->ts.type == BT_UNKNOWN)
1259 e->ts = constructor_ts;
1261 return t;
1266 typedef struct cons_stack
1268 gfc_iterator *iterator;
1269 struct cons_stack *previous;
1271 cons_stack;
1273 static cons_stack *base;
1275 static bool check_constructor (gfc_constructor_base, bool (*) (gfc_expr *));
1277 /* Check an EXPR_VARIABLE expression in a constructor to make sure
1278 that that variable is an iteration variables. */
1280 bool
1281 gfc_check_iter_variable (gfc_expr *expr)
1283 gfc_symbol *sym;
1284 cons_stack *c;
1286 sym = expr->symtree->n.sym;
1288 for (c = base; c && c->iterator; c = c->previous)
1289 if (sym == c->iterator->var->symtree->n.sym)
1290 return true;
1292 return false;
1296 /* Recursive work function for gfc_check_constructor(). This amounts
1297 to calling the check function for each expression in the
1298 constructor, giving variables with the names of iterators a pass. */
1300 static bool
1301 check_constructor (gfc_constructor_base ctor, bool (*check_function) (gfc_expr *))
1303 cons_stack element;
1304 gfc_expr *e;
1305 bool t;
1306 gfc_constructor *c;
1308 for (c = gfc_constructor_first (ctor); c; c = gfc_constructor_next (c))
1310 e = c->expr;
1312 if (!e)
1313 continue;
1315 if (e->expr_type != EXPR_ARRAY)
1317 if (!(*check_function)(e))
1318 return false;
1319 continue;
1322 element.previous = base;
1323 element.iterator = c->iterator;
1325 base = &element;
1326 t = check_constructor (e->value.constructor, check_function);
1327 base = element.previous;
1329 if (!t)
1330 return false;
1333 /* Nothing went wrong, so all OK. */
1334 return true;
1338 /* Checks a constructor to see if it is a particular kind of
1339 expression -- specification, restricted, or initialization as
1340 determined by the check_function. */
1342 bool
1343 gfc_check_constructor (gfc_expr *expr, bool (*check_function) (gfc_expr *))
1345 cons_stack *base_save;
1346 bool t;
1348 base_save = base;
1349 base = NULL;
1351 t = check_constructor (expr->value.constructor, check_function);
1352 base = base_save;
1354 return t;
1359 /**************** Simplification of array constructors ****************/
1361 iterator_stack *iter_stack;
1363 typedef struct
1365 gfc_constructor_base base;
1366 int extract_count, extract_n;
1367 gfc_expr *extracted;
1368 mpz_t *count;
1370 mpz_t *offset;
1371 gfc_component *component;
1372 mpz_t *repeat;
1374 bool (*expand_work_function) (gfc_expr *);
1376 expand_info;
1378 static expand_info current_expand;
1380 static bool expand_constructor (gfc_constructor_base);
1383 /* Work function that counts the number of elements present in a
1384 constructor. */
1386 static bool
1387 count_elements (gfc_expr *e)
1389 mpz_t result;
1391 if (e->rank == 0)
1392 mpz_add_ui (*current_expand.count, *current_expand.count, 1);
1393 else
1395 if (!gfc_array_size (e, &result))
1397 gfc_free_expr (e);
1398 return false;
1401 mpz_add (*current_expand.count, *current_expand.count, result);
1402 mpz_clear (result);
1405 gfc_free_expr (e);
1406 return true;
1410 /* Work function that extracts a particular element from an array
1411 constructor, freeing the rest. */
1413 static bool
1414 extract_element (gfc_expr *e)
1416 if (e->rank != 0)
1417 { /* Something unextractable */
1418 gfc_free_expr (e);
1419 return false;
1422 if (current_expand.extract_count == current_expand.extract_n)
1423 current_expand.extracted = e;
1424 else
1425 gfc_free_expr (e);
1427 current_expand.extract_count++;
1429 return true;
1433 /* Work function that constructs a new constructor out of the old one,
1434 stringing new elements together. */
1436 static bool
1437 expand (gfc_expr *e)
1439 gfc_constructor *c = gfc_constructor_append_expr (&current_expand.base,
1440 e, &e->where);
1442 c->n.component = current_expand.component;
1443 return true;
1447 /* Given an initialization expression that is a variable reference,
1448 substitute the current value of the iteration variable. */
1450 void
1451 gfc_simplify_iterator_var (gfc_expr *e)
1453 iterator_stack *p;
1455 for (p = iter_stack; p; p = p->prev)
1456 if (e->symtree == p->variable)
1457 break;
1459 if (p == NULL)
1460 return; /* Variable not found */
1462 gfc_replace_expr (e, gfc_get_int_expr (gfc_default_integer_kind, NULL, 0));
1464 mpz_set (e->value.integer, p->value);
1466 return;
1470 /* Expand an expression with that is inside of a constructor,
1471 recursing into other constructors if present. */
1473 static bool
1474 expand_expr (gfc_expr *e)
1476 if (e->expr_type == EXPR_ARRAY)
1477 return expand_constructor (e->value.constructor);
1479 e = gfc_copy_expr (e);
1481 if (!gfc_simplify_expr (e, 1))
1483 gfc_free_expr (e);
1484 return false;
1487 return current_expand.expand_work_function (e);
1491 static bool
1492 expand_iterator (gfc_constructor *c)
1494 gfc_expr *start, *end, *step;
1495 iterator_stack frame;
1496 mpz_t trip;
1497 bool t;
1499 end = step = NULL;
1501 t = false;
1503 mpz_init (trip);
1504 mpz_init (frame.value);
1505 frame.prev = NULL;
1507 start = gfc_copy_expr (c->iterator->start);
1508 if (!gfc_simplify_expr (start, 1))
1509 goto cleanup;
1511 if (start->expr_type != EXPR_CONSTANT || start->ts.type != BT_INTEGER)
1512 goto cleanup;
1514 end = gfc_copy_expr (c->iterator->end);
1515 if (!gfc_simplify_expr (end, 1))
1516 goto cleanup;
1518 if (end->expr_type != EXPR_CONSTANT || end->ts.type != BT_INTEGER)
1519 goto cleanup;
1521 step = gfc_copy_expr (c->iterator->step);
1522 if (!gfc_simplify_expr (step, 1))
1523 goto cleanup;
1525 if (step->expr_type != EXPR_CONSTANT || step->ts.type != BT_INTEGER)
1526 goto cleanup;
1528 if (mpz_sgn (step->value.integer) == 0)
1530 gfc_error ("Iterator step at %L cannot be zero", &step->where);
1531 goto cleanup;
1534 /* Calculate the trip count of the loop. */
1535 mpz_sub (trip, end->value.integer, start->value.integer);
1536 mpz_add (trip, trip, step->value.integer);
1537 mpz_tdiv_q (trip, trip, step->value.integer);
1539 mpz_set (frame.value, start->value.integer);
1541 frame.prev = iter_stack;
1542 frame.variable = c->iterator->var->symtree;
1543 iter_stack = &frame;
1545 while (mpz_sgn (trip) > 0)
1547 if (!expand_expr (c->expr))
1548 goto cleanup;
1550 mpz_add (frame.value, frame.value, step->value.integer);
1551 mpz_sub_ui (trip, trip, 1);
1554 t = true;
1556 cleanup:
1557 gfc_free_expr (start);
1558 gfc_free_expr (end);
1559 gfc_free_expr (step);
1561 mpz_clear (trip);
1562 mpz_clear (frame.value);
1564 iter_stack = frame.prev;
1566 return t;
1570 /* Expand a constructor into constant constructors without any
1571 iterators, calling the work function for each of the expanded
1572 expressions. The work function needs to either save or free the
1573 passed expression. */
1575 static bool
1576 expand_constructor (gfc_constructor_base base)
1578 gfc_constructor *c;
1579 gfc_expr *e;
1581 for (c = gfc_constructor_first (base); c; c = gfc_constructor_next(c))
1583 if (c->iterator != NULL)
1585 if (!expand_iterator (c))
1586 return false;
1587 continue;
1590 e = c->expr;
1592 if (e->expr_type == EXPR_ARRAY)
1594 if (!expand_constructor (e->value.constructor))
1595 return false;
1597 continue;
1600 e = gfc_copy_expr (e);
1601 if (!gfc_simplify_expr (e, 1))
1603 gfc_free_expr (e);
1604 return false;
1606 current_expand.offset = &c->offset;
1607 current_expand.repeat = &c->repeat;
1608 current_expand.component = c->n.component;
1609 if (!current_expand.expand_work_function(e))
1610 return false;
1612 return true;
1616 /* Given an array expression and an element number (starting at zero),
1617 return a pointer to the array element. NULL is returned if the
1618 size of the array has been exceeded. The expression node returned
1619 remains a part of the array and should not be freed. Access is not
1620 efficient at all, but this is another place where things do not
1621 have to be particularly fast. */
1623 static gfc_expr *
1624 gfc_get_array_element (gfc_expr *array, int element)
1626 expand_info expand_save;
1627 gfc_expr *e;
1628 bool rc;
1630 expand_save = current_expand;
1631 current_expand.extract_n = element;
1632 current_expand.expand_work_function = extract_element;
1633 current_expand.extracted = NULL;
1634 current_expand.extract_count = 0;
1636 iter_stack = NULL;
1638 rc = expand_constructor (array->value.constructor);
1639 e = current_expand.extracted;
1640 current_expand = expand_save;
1642 if (!rc)
1643 return NULL;
1645 return e;
1649 /* Top level subroutine for expanding constructors. We only expand
1650 constructor if they are small enough. */
1652 bool
1653 gfc_expand_constructor (gfc_expr *e, bool fatal)
1655 expand_info expand_save;
1656 gfc_expr *f;
1657 bool rc;
1659 /* If we can successfully get an array element at the max array size then
1660 the array is too big to expand, so we just return. */
1661 f = gfc_get_array_element (e, flag_max_array_constructor);
1662 if (f != NULL)
1664 gfc_free_expr (f);
1665 if (fatal)
1667 gfc_error ("The number of elements in the array constructor "
1668 "at %L requires an increase of the allowed %d "
1669 "upper limit. See %<-fmax-array-constructor%> "
1670 "option", &e->where, flag_max_array_constructor);
1671 return false;
1673 return true;
1676 /* We now know the array is not too big so go ahead and try to expand it. */
1677 expand_save = current_expand;
1678 current_expand.base = NULL;
1680 iter_stack = NULL;
1682 current_expand.expand_work_function = expand;
1684 if (!expand_constructor (e->value.constructor))
1686 gfc_constructor_free (current_expand.base);
1687 rc = false;
1688 goto done;
1691 gfc_constructor_free (e->value.constructor);
1692 e->value.constructor = current_expand.base;
1694 rc = true;
1696 done:
1697 current_expand = expand_save;
1699 return rc;
1703 /* Work function for checking that an element of a constructor is a
1704 constant, after removal of any iteration variables. We return
1705 false if not so. */
1707 static bool
1708 is_constant_element (gfc_expr *e)
1710 int rv;
1712 rv = gfc_is_constant_expr (e);
1713 gfc_free_expr (e);
1715 return rv ? true : false;
1719 /* Given an array constructor, determine if the constructor is
1720 constant or not by expanding it and making sure that all elements
1721 are constants. This is a bit of a hack since something like (/ (i,
1722 i=1,100000000) /) will take a while as* opposed to a more clever
1723 function that traverses the expression tree. FIXME. */
1726 gfc_constant_ac (gfc_expr *e)
1728 expand_info expand_save;
1729 bool rc;
1731 iter_stack = NULL;
1732 expand_save = current_expand;
1733 current_expand.expand_work_function = is_constant_element;
1735 rc = expand_constructor (e->value.constructor);
1737 current_expand = expand_save;
1738 if (!rc)
1739 return 0;
1741 return 1;
1745 /* Returns nonzero if an array constructor has been completely
1746 expanded (no iterators) and zero if iterators are present. */
1749 gfc_expanded_ac (gfc_expr *e)
1751 gfc_constructor *c;
1753 if (e->expr_type == EXPR_ARRAY)
1754 for (c = gfc_constructor_first (e->value.constructor);
1755 c; c = gfc_constructor_next (c))
1756 if (c->iterator != NULL || !gfc_expanded_ac (c->expr))
1757 return 0;
1759 return 1;
1763 /*************** Type resolution of array constructors ***************/
1766 /* The symbol expr_is_sought_symbol_ref will try to find. */
1767 static const gfc_symbol *sought_symbol = NULL;
1770 /* Tells whether the expression E is a variable reference to the symbol
1771 in the static variable SOUGHT_SYMBOL, and sets the locus pointer WHERE
1772 accordingly.
1773 To be used with gfc_expr_walker: if a reference is found we don't need
1774 to look further so we return 1 to skip any further walk. */
1776 static int
1777 expr_is_sought_symbol_ref (gfc_expr **e, int *walk_subtrees ATTRIBUTE_UNUSED,
1778 void *where)
1780 gfc_expr *expr = *e;
1781 locus *sym_loc = (locus *)where;
1783 if (expr->expr_type == EXPR_VARIABLE
1784 && expr->symtree->n.sym == sought_symbol)
1786 *sym_loc = expr->where;
1787 return 1;
1790 return 0;
1794 /* Tells whether the expression EXPR contains a reference to the symbol
1795 SYM and in that case sets the position SYM_LOC where the reference is. */
1797 static bool
1798 find_symbol_in_expr (gfc_symbol *sym, gfc_expr *expr, locus *sym_loc)
1800 int ret;
1802 sought_symbol = sym;
1803 ret = gfc_expr_walker (&expr, &expr_is_sought_symbol_ref, sym_loc);
1804 sought_symbol = NULL;
1805 return ret;
1809 /* Recursive array list resolution function. All of the elements must
1810 be of the same type. */
1812 static bool
1813 resolve_array_list (gfc_constructor_base base)
1815 bool t;
1816 gfc_constructor *c;
1817 gfc_iterator *iter;
1819 t = true;
1821 for (c = gfc_constructor_first (base); c; c = gfc_constructor_next (c))
1823 iter = c->iterator;
1824 if (iter != NULL)
1826 gfc_symbol *iter_var;
1827 locus iter_var_loc;
1829 if (!gfc_resolve_iterator (iter, false, true))
1830 t = false;
1832 /* Check for bounds referencing the iterator variable. */
1833 gcc_assert (iter->var->expr_type == EXPR_VARIABLE);
1834 iter_var = iter->var->symtree->n.sym;
1835 if (find_symbol_in_expr (iter_var, iter->start, &iter_var_loc))
1837 if (!gfc_notify_std (GFC_STD_LEGACY, "AC-IMPLIED-DO initial "
1838 "expression references control variable "
1839 "at %L", &iter_var_loc))
1840 t = false;
1842 if (find_symbol_in_expr (iter_var, iter->end, &iter_var_loc))
1844 if (!gfc_notify_std (GFC_STD_LEGACY, "AC-IMPLIED-DO final "
1845 "expression references control variable "
1846 "at %L", &iter_var_loc))
1847 t = false;
1849 if (find_symbol_in_expr (iter_var, iter->step, &iter_var_loc))
1851 if (!gfc_notify_std (GFC_STD_LEGACY, "AC-IMPLIED-DO step "
1852 "expression references control variable "
1853 "at %L", &iter_var_loc))
1854 t = false;
1858 if (!gfc_resolve_expr (c->expr))
1859 t = false;
1861 if (UNLIMITED_POLY (c->expr))
1863 gfc_error ("Array constructor value at %L shall not be unlimited "
1864 "polymorphic [F2008: C4106]", &c->expr->where);
1865 t = false;
1869 return t;
1872 /* Resolve character array constructor. If it has a specified constant character
1873 length, pad/truncate the elements here; if the length is not specified and
1874 all elements are of compile-time known length, emit an error as this is
1875 invalid. */
1877 bool
1878 gfc_resolve_character_array_constructor (gfc_expr *expr)
1880 gfc_constructor *p;
1881 int found_length;
1883 gcc_assert (expr->expr_type == EXPR_ARRAY);
1884 gcc_assert (expr->ts.type == BT_CHARACTER);
1886 if (expr->ts.u.cl == NULL)
1888 for (p = gfc_constructor_first (expr->value.constructor);
1889 p; p = gfc_constructor_next (p))
1890 if (p->expr->ts.u.cl != NULL)
1892 /* Ensure that if there is a char_len around that it is
1893 used; otherwise the middle-end confuses them! */
1894 expr->ts.u.cl = p->expr->ts.u.cl;
1895 goto got_charlen;
1898 expr->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
1901 got_charlen:
1903 found_length = -1;
1905 if (expr->ts.u.cl->length == NULL)
1907 /* Check that all constant string elements have the same length until
1908 we reach the end or find a variable-length one. */
1910 for (p = gfc_constructor_first (expr->value.constructor);
1911 p; p = gfc_constructor_next (p))
1913 int current_length = -1;
1914 gfc_ref *ref;
1915 for (ref = p->expr->ref; ref; ref = ref->next)
1916 if (ref->type == REF_SUBSTRING
1917 && ref->u.ss.start->expr_type == EXPR_CONSTANT
1918 && ref->u.ss.end->expr_type == EXPR_CONSTANT)
1919 break;
1921 if (p->expr->expr_type == EXPR_CONSTANT)
1922 current_length = p->expr->value.character.length;
1923 else if (ref)
1925 long j;
1926 j = mpz_get_ui (ref->u.ss.end->value.integer)
1927 - mpz_get_ui (ref->u.ss.start->value.integer) + 1;
1928 current_length = (int) j;
1930 else if (p->expr->ts.u.cl && p->expr->ts.u.cl->length
1931 && p->expr->ts.u.cl->length->expr_type == EXPR_CONSTANT)
1933 long j;
1934 j = mpz_get_si (p->expr->ts.u.cl->length->value.integer);
1935 current_length = (int) j;
1937 else
1938 return true;
1940 gcc_assert (current_length != -1);
1942 if (found_length == -1)
1943 found_length = current_length;
1944 else if (found_length != current_length)
1946 gfc_error ("Different CHARACTER lengths (%d/%d) in array"
1947 " constructor at %L", found_length, current_length,
1948 &p->expr->where);
1949 return false;
1952 gcc_assert (found_length == current_length);
1955 gcc_assert (found_length != -1);
1957 /* Update the character length of the array constructor. */
1958 expr->ts.u.cl->length = gfc_get_int_expr (gfc_default_integer_kind,
1959 NULL, found_length);
1961 else
1963 /* We've got a character length specified. It should be an integer,
1964 otherwise an error is signalled elsewhere. */
1965 gcc_assert (expr->ts.u.cl->length);
1967 /* If we've got a constant character length, pad according to this.
1968 gfc_extract_int does check for BT_INTEGER and EXPR_CONSTANT and sets
1969 max_length only if they pass. */
1970 gfc_extract_int (expr->ts.u.cl->length, &found_length);
1972 /* Now pad/truncate the elements accordingly to the specified character
1973 length. This is ok inside this conditional, as in the case above
1974 (without typespec) all elements are verified to have the same length
1975 anyway. */
1976 if (found_length != -1)
1977 for (p = gfc_constructor_first (expr->value.constructor);
1978 p; p = gfc_constructor_next (p))
1979 if (p->expr->expr_type == EXPR_CONSTANT)
1981 gfc_expr *cl = NULL;
1982 int current_length = -1;
1983 bool has_ts;
1985 if (p->expr->ts.u.cl && p->expr->ts.u.cl->length)
1987 cl = p->expr->ts.u.cl->length;
1988 gfc_extract_int (cl, &current_length);
1991 /* If gfc_extract_int above set current_length, we implicitly
1992 know the type is BT_INTEGER and it's EXPR_CONSTANT. */
1994 has_ts = expr->ts.u.cl->length_from_typespec;
1996 if (! cl
1997 || (current_length != -1 && current_length != found_length))
1998 gfc_set_constant_character_len (found_length, p->expr,
1999 has_ts ? -1 : found_length);
2003 return true;
2007 /* Resolve all of the expressions in an array list. */
2009 bool
2010 gfc_resolve_array_constructor (gfc_expr *expr)
2012 bool t;
2014 t = resolve_array_list (expr->value.constructor);
2015 if (t)
2016 t = gfc_check_constructor_type (expr);
2018 /* gfc_resolve_character_array_constructor is called in gfc_resolve_expr after
2019 the call to this function, so we don't need to call it here; if it was
2020 called twice, an error message there would be duplicated. */
2022 return t;
2026 /* Copy an iterator structure. */
2028 gfc_iterator *
2029 gfc_copy_iterator (gfc_iterator *src)
2031 gfc_iterator *dest;
2033 if (src == NULL)
2034 return NULL;
2036 dest = gfc_get_iterator ();
2038 dest->var = gfc_copy_expr (src->var);
2039 dest->start = gfc_copy_expr (src->start);
2040 dest->end = gfc_copy_expr (src->end);
2041 dest->step = gfc_copy_expr (src->step);
2043 return dest;
2047 /********* Subroutines for determining the size of an array *********/
2049 /* These are needed just to accommodate RESHAPE(). There are no
2050 diagnostics here, we just return a negative number if something
2051 goes wrong. */
2054 /* Get the size of single dimension of an array specification. The
2055 array is guaranteed to be one dimensional. */
2057 bool
2058 spec_dimen_size (gfc_array_spec *as, int dimen, mpz_t *result)
2060 if (as == NULL)
2061 return false;
2063 if (dimen < 0 || dimen > as->rank - 1)
2064 gfc_internal_error ("spec_dimen_size(): Bad dimension");
2066 if (as->type != AS_EXPLICIT
2067 || as->lower[dimen]->expr_type != EXPR_CONSTANT
2068 || as->upper[dimen]->expr_type != EXPR_CONSTANT
2069 || as->lower[dimen]->ts.type != BT_INTEGER
2070 || as->upper[dimen]->ts.type != BT_INTEGER)
2071 return false;
2073 mpz_init (*result);
2075 mpz_sub (*result, as->upper[dimen]->value.integer,
2076 as->lower[dimen]->value.integer);
2078 mpz_add_ui (*result, *result, 1);
2080 return true;
2084 bool
2085 spec_size (gfc_array_spec *as, mpz_t *result)
2087 mpz_t size;
2088 int d;
2090 if (!as || as->type == AS_ASSUMED_RANK)
2091 return false;
2093 mpz_init_set_ui (*result, 1);
2095 for (d = 0; d < as->rank; d++)
2097 if (!spec_dimen_size (as, d, &size))
2099 mpz_clear (*result);
2100 return false;
2103 mpz_mul (*result, *result, size);
2104 mpz_clear (size);
2107 return true;
2111 /* Get the number of elements in an array section. Optionally, also supply
2112 the end value. */
2114 bool
2115 gfc_ref_dimen_size (gfc_array_ref *ar, int dimen, mpz_t *result, mpz_t *end)
2117 mpz_t upper, lower, stride;
2118 mpz_t diff;
2119 bool t;
2121 if (dimen < 0 || ar == NULL || dimen > ar->dimen - 1)
2122 gfc_internal_error ("gfc_ref_dimen_size(): Bad dimension");
2124 switch (ar->dimen_type[dimen])
2126 case DIMEN_ELEMENT:
2127 mpz_init (*result);
2128 mpz_set_ui (*result, 1);
2129 t = true;
2130 break;
2132 case DIMEN_VECTOR:
2133 t = gfc_array_size (ar->start[dimen], result); /* Recurse! */
2134 break;
2136 case DIMEN_RANGE:
2138 mpz_init (stride);
2140 if (ar->stride[dimen] == NULL)
2141 mpz_set_ui (stride, 1);
2142 else
2144 if (ar->stride[dimen]->expr_type != EXPR_CONSTANT)
2146 mpz_clear (stride);
2147 return false;
2149 mpz_set (stride, ar->stride[dimen]->value.integer);
2152 /* Calculate the number of elements via gfc_dep_differce, but only if
2153 start and end are both supplied in the reference or the array spec.
2154 This is to guard against strange but valid code like
2156 subroutine foo(a,n)
2157 real a(1:n)
2158 n = 3
2159 print *,size(a(n-1:))
2161 where the user changes the value of a variable. If we have to
2162 determine end as well, we cannot do this using gfc_dep_difference.
2163 Fall back to the constants-only code then. */
2165 if (end == NULL)
2167 bool use_dep;
2169 use_dep = gfc_dep_difference (ar->end[dimen], ar->start[dimen],
2170 &diff);
2171 if (!use_dep && ar->end[dimen] == NULL && ar->start[dimen] == NULL)
2172 use_dep = gfc_dep_difference (ar->as->upper[dimen],
2173 ar->as->lower[dimen], &diff);
2175 if (use_dep)
2177 mpz_init (*result);
2178 mpz_add (*result, diff, stride);
2179 mpz_div (*result, *result, stride);
2180 if (mpz_cmp_ui (*result, 0) < 0)
2181 mpz_set_ui (*result, 0);
2183 mpz_clear (stride);
2184 mpz_clear (diff);
2185 return true;
2190 /* Constant-only code here, which covers more cases
2191 like a(:4) etc. */
2192 mpz_init (upper);
2193 mpz_init (lower);
2194 t = false;
2196 if (ar->start[dimen] == NULL)
2198 if (ar->as->lower[dimen] == NULL
2199 || ar->as->lower[dimen]->expr_type != EXPR_CONSTANT)
2200 goto cleanup;
2201 mpz_set (lower, ar->as->lower[dimen]->value.integer);
2203 else
2205 if (ar->start[dimen]->expr_type != EXPR_CONSTANT)
2206 goto cleanup;
2207 mpz_set (lower, ar->start[dimen]->value.integer);
2210 if (ar->end[dimen] == NULL)
2212 if (ar->as->upper[dimen] == NULL
2213 || ar->as->upper[dimen]->expr_type != EXPR_CONSTANT)
2214 goto cleanup;
2215 mpz_set (upper, ar->as->upper[dimen]->value.integer);
2217 else
2219 if (ar->end[dimen]->expr_type != EXPR_CONSTANT)
2220 goto cleanup;
2221 mpz_set (upper, ar->end[dimen]->value.integer);
2224 mpz_init (*result);
2225 mpz_sub (*result, upper, lower);
2226 mpz_add (*result, *result, stride);
2227 mpz_div (*result, *result, stride);
2229 /* Zero stride caught earlier. */
2230 if (mpz_cmp_ui (*result, 0) < 0)
2231 mpz_set_ui (*result, 0);
2232 t = true;
2234 if (end)
2236 mpz_init (*end);
2238 mpz_sub_ui (*end, *result, 1UL);
2239 mpz_mul (*end, *end, stride);
2240 mpz_add (*end, *end, lower);
2243 cleanup:
2244 mpz_clear (upper);
2245 mpz_clear (lower);
2246 mpz_clear (stride);
2247 return t;
2249 default:
2250 gfc_internal_error ("gfc_ref_dimen_size(): Bad dimen_type");
2253 return t;
2257 static bool
2258 ref_size (gfc_array_ref *ar, mpz_t *result)
2260 mpz_t size;
2261 int d;
2263 mpz_init_set_ui (*result, 1);
2265 for (d = 0; d < ar->dimen; d++)
2267 if (!gfc_ref_dimen_size (ar, d, &size, NULL))
2269 mpz_clear (*result);
2270 return false;
2273 mpz_mul (*result, *result, size);
2274 mpz_clear (size);
2277 return true;
2281 /* Given an array expression and a dimension, figure out how many
2282 elements it has along that dimension. Returns true if we were
2283 able to return a result in the 'result' variable, false
2284 otherwise. */
2286 bool
2287 gfc_array_dimen_size (gfc_expr *array, int dimen, mpz_t *result)
2289 gfc_ref *ref;
2290 int i;
2292 gcc_assert (array != NULL);
2294 if (array->ts.type == BT_CLASS)
2295 return false;
2297 if (array->rank == -1)
2298 return false;
2300 if (dimen < 0 || dimen > array->rank - 1)
2301 gfc_internal_error ("gfc_array_dimen_size(): Bad dimension");
2303 switch (array->expr_type)
2305 case EXPR_VARIABLE:
2306 case EXPR_FUNCTION:
2307 for (ref = array->ref; ref; ref = ref->next)
2309 if (ref->type != REF_ARRAY)
2310 continue;
2312 if (ref->u.ar.type == AR_FULL)
2313 return spec_dimen_size (ref->u.ar.as, dimen, result);
2315 if (ref->u.ar.type == AR_SECTION)
2317 for (i = 0; dimen >= 0; i++)
2318 if (ref->u.ar.dimen_type[i] != DIMEN_ELEMENT)
2319 dimen--;
2321 return gfc_ref_dimen_size (&ref->u.ar, i - 1, result, NULL);
2325 if (array->shape && array->shape[dimen])
2327 mpz_init_set (*result, array->shape[dimen]);
2328 return true;
2331 if (array->symtree->n.sym->attr.generic
2332 && array->value.function.esym != NULL)
2334 if (!spec_dimen_size (array->value.function.esym->as, dimen, result))
2335 return false;
2337 else if (!spec_dimen_size (array->symtree->n.sym->as, dimen, result))
2338 return false;
2340 break;
2342 case EXPR_ARRAY:
2343 if (array->shape == NULL) {
2344 /* Expressions with rank > 1 should have "shape" properly set */
2345 if ( array->rank != 1 )
2346 gfc_internal_error ("gfc_array_dimen_size(): Bad EXPR_ARRAY expr");
2347 return gfc_array_size(array, result);
2350 /* Fall through */
2351 default:
2352 if (array->shape == NULL)
2353 return false;
2355 mpz_init_set (*result, array->shape[dimen]);
2357 break;
2360 return true;
2364 /* Given an array expression, figure out how many elements are in the
2365 array. Returns true if this is possible, and sets the 'result'
2366 variable. Otherwise returns false. */
2368 bool
2369 gfc_array_size (gfc_expr *array, mpz_t *result)
2371 expand_info expand_save;
2372 gfc_ref *ref;
2373 int i;
2374 bool t;
2376 if (array->ts.type == BT_CLASS)
2377 return false;
2379 switch (array->expr_type)
2381 case EXPR_ARRAY:
2382 gfc_push_suppress_errors ();
2384 expand_save = current_expand;
2386 current_expand.count = result;
2387 mpz_init_set_ui (*result, 0);
2389 current_expand.expand_work_function = count_elements;
2390 iter_stack = NULL;
2392 t = expand_constructor (array->value.constructor);
2394 gfc_pop_suppress_errors ();
2396 if (!t)
2397 mpz_clear (*result);
2398 current_expand = expand_save;
2399 return t;
2401 case EXPR_VARIABLE:
2402 for (ref = array->ref; ref; ref = ref->next)
2404 if (ref->type != REF_ARRAY)
2405 continue;
2407 if (ref->u.ar.type == AR_FULL)
2408 return spec_size (ref->u.ar.as, result);
2410 if (ref->u.ar.type == AR_SECTION)
2411 return ref_size (&ref->u.ar, result);
2414 return spec_size (array->symtree->n.sym->as, result);
2417 default:
2418 if (array->rank == 0 || array->shape == NULL)
2419 return false;
2421 mpz_init_set_ui (*result, 1);
2423 for (i = 0; i < array->rank; i++)
2424 mpz_mul (*result, *result, array->shape[i]);
2426 break;
2429 return true;
2433 /* Given an array reference, return the shape of the reference in an
2434 array of mpz_t integers. */
2436 bool
2437 gfc_array_ref_shape (gfc_array_ref *ar, mpz_t *shape)
2439 int d;
2440 int i;
2442 d = 0;
2444 switch (ar->type)
2446 case AR_FULL:
2447 for (; d < ar->as->rank; d++)
2448 if (!spec_dimen_size (ar->as, d, &shape[d]))
2449 goto cleanup;
2451 return true;
2453 case AR_SECTION:
2454 for (i = 0; i < ar->dimen; i++)
2456 if (ar->dimen_type[i] != DIMEN_ELEMENT)
2458 if (!gfc_ref_dimen_size (ar, i, &shape[d], NULL))
2459 goto cleanup;
2460 d++;
2464 return true;
2466 default:
2467 break;
2470 cleanup:
2471 gfc_clear_shape (shape, d);
2472 return false;
2476 /* Given an array expression, find the array reference structure that
2477 characterizes the reference. */
2479 gfc_array_ref *
2480 gfc_find_array_ref (gfc_expr *e)
2482 gfc_ref *ref;
2484 for (ref = e->ref; ref; ref = ref->next)
2485 if (ref->type == REF_ARRAY
2486 && (ref->u.ar.type == AR_FULL || ref->u.ar.type == AR_SECTION))
2487 break;
2489 if (ref == NULL)
2490 gfc_internal_error ("gfc_find_array_ref(): No ref found");
2492 return &ref->u.ar;
2496 /* Find out if an array shape is known at compile time. */
2499 gfc_is_compile_time_shape (gfc_array_spec *as)
2501 int i;
2503 if (as->type != AS_EXPLICIT)
2504 return 0;
2506 for (i = 0; i < as->rank; i++)
2507 if (!gfc_is_constant_expr (as->lower[i])
2508 || !gfc_is_constant_expr (as->upper[i]))
2509 return 0;
2511 return 1;