2018-06-25 Richard Biener <rguenther@suse.de>
[official-gcc.git] / gcc / fortran / array.c
blobe4926d751471d58937eb6f762b9cbd800924adbb
1 /* Array things
2 Copyright (C) 2000-2018 Free Software Foundation, Inc.
3 Contributed by Andy Vaught
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "options.h"
25 #include "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 particular
150 elements or a section. If init is set, the reference has to consist
151 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;
159 gfc_expr *tmp;
160 bool stat_just_seen = false;
161 bool team_just_seen = false;
163 memset (ar, '\0', sizeof (*ar));
165 ar->where = gfc_current_locus;
166 ar->as = as;
167 ar->type = AR_UNKNOWN;
169 if (gfc_match_char ('[') == MATCH_YES)
171 matched_bracket = true;
172 goto coarray;
175 if (gfc_match_char ('(') != MATCH_YES)
177 ar->type = AR_FULL;
178 ar->dimen = 0;
179 return MATCH_YES;
182 for (ar->dimen = 0; ar->dimen < GFC_MAX_DIMENSIONS; ar->dimen++)
184 m = match_subscript (ar, init, false);
185 if (m == MATCH_ERROR)
186 return MATCH_ERROR;
188 if (gfc_match_char (')') == MATCH_YES)
190 ar->dimen++;
191 goto coarray;
194 if (gfc_match_char (',') != MATCH_YES)
196 gfc_error ("Invalid form of array reference at %C");
197 return MATCH_ERROR;
201 if (ar->dimen >= 7
202 && !gfc_notify_std (GFC_STD_F2008,
203 "Array reference at %C has more than 7 dimensions"))
204 return MATCH_ERROR;
206 gfc_error ("Array reference at %C cannot have more than %d dimensions",
207 GFC_MAX_DIMENSIONS);
208 return MATCH_ERROR;
210 coarray:
211 if (!matched_bracket && gfc_match_char ('[') != MATCH_YES)
213 if (ar->dimen > 0)
214 return MATCH_YES;
215 else
216 return MATCH_ERROR;
219 if (flag_coarray == GFC_FCOARRAY_NONE)
221 gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to enable");
222 return MATCH_ERROR;
225 if (corank == 0)
227 gfc_error ("Unexpected coarray designator at %C");
228 return MATCH_ERROR;
231 ar->stat = NULL;
233 for (ar->codimen = 0; ar->codimen + ar->dimen < GFC_MAX_DIMENSIONS; ar->codimen++)
235 m = match_subscript (ar, init, true);
236 if (m == MATCH_ERROR)
237 return MATCH_ERROR;
239 team_just_seen = false;
240 stat_just_seen = false;
241 if (gfc_match (" , team = %e", &tmp) == MATCH_YES && ar->team == NULL)
243 ar->team = tmp;
244 team_just_seen = true;
247 if (ar->team && !team_just_seen)
249 gfc_error ("TEAM= attribute in %C misplaced");
250 return MATCH_ERROR;
253 if (gfc_match (" , stat = %e",&tmp) == MATCH_YES && ar->stat == NULL)
255 ar->stat = tmp;
256 stat_just_seen = true;
259 if (ar->stat && !stat_just_seen)
261 gfc_error ("STAT= attribute in %C misplaced");
262 return MATCH_ERROR;
265 if (gfc_match_char (']') == MATCH_YES)
267 ar->codimen++;
268 if (ar->codimen < corank)
270 gfc_error ("Too few codimensions at %C, expected %d not %d",
271 corank, ar->codimen);
272 return MATCH_ERROR;
274 if (ar->codimen > corank)
276 gfc_error ("Too many codimensions at %C, expected %d not %d",
277 corank, ar->codimen);
278 return MATCH_ERROR;
280 return MATCH_YES;
283 if (gfc_match_char (',') != MATCH_YES)
285 if (gfc_match_char ('*') == MATCH_YES)
286 gfc_error ("Unexpected %<*%> for codimension %d of %d at %C",
287 ar->codimen + 1, corank);
288 else
289 gfc_error ("Invalid form of coarray reference at %C");
290 return MATCH_ERROR;
292 else if (ar->dimen_type[ar->codimen + ar->dimen] == DIMEN_STAR)
294 gfc_error ("Unexpected %<*%> for codimension %d of %d at %C",
295 ar->codimen + 1, corank);
296 return MATCH_ERROR;
299 if (ar->codimen >= corank)
301 gfc_error ("Invalid codimension %d at %C, only %d codimensions exist",
302 ar->codimen + 1, corank);
303 return MATCH_ERROR;
307 gfc_error ("Array reference at %C cannot have more than %d dimensions",
308 GFC_MAX_DIMENSIONS);
309 return MATCH_ERROR;
314 /************** Array specification matching subroutines ***************/
316 /* Free all of the expressions associated with array bounds
317 specifications. */
319 void
320 gfc_free_array_spec (gfc_array_spec *as)
322 int i;
324 if (as == NULL)
325 return;
327 for (i = 0; i < as->rank + as->corank; i++)
329 gfc_free_expr (as->lower[i]);
330 gfc_free_expr (as->upper[i]);
333 free (as);
337 /* Take an array bound, resolves the expression, that make up the
338 shape and check associated constraints. */
340 static bool
341 resolve_array_bound (gfc_expr *e, int check_constant)
343 if (e == NULL)
344 return true;
346 if (!gfc_resolve_expr (e)
347 || !gfc_specification_expr (e))
348 return false;
350 if (check_constant && !gfc_is_constant_expr (e))
352 if (e->expr_type == EXPR_VARIABLE)
353 gfc_error ("Variable %qs at %L in this context must be constant",
354 e->symtree->n.sym->name, &e->where);
355 else
356 gfc_error ("Expression at %L in this context must be constant",
357 &e->where);
358 return false;
361 return true;
365 /* Takes an array specification, resolves the expressions that make up
366 the shape and make sure everything is integral. */
368 bool
369 gfc_resolve_array_spec (gfc_array_spec *as, int check_constant)
371 gfc_expr *e;
372 int i;
374 if (as == NULL)
375 return true;
377 if (as->resolved)
378 return true;
380 for (i = 0; i < as->rank + as->corank; i++)
382 e = as->lower[i];
383 if (!resolve_array_bound (e, check_constant))
384 return false;
386 e = as->upper[i];
387 if (!resolve_array_bound (e, check_constant))
388 return false;
390 if ((as->lower[i] == NULL) || (as->upper[i] == NULL))
391 continue;
393 /* If the size is negative in this dimension, set it to zero. */
394 if (as->lower[i]->expr_type == EXPR_CONSTANT
395 && as->upper[i]->expr_type == EXPR_CONSTANT
396 && mpz_cmp (as->upper[i]->value.integer,
397 as->lower[i]->value.integer) < 0)
399 gfc_free_expr (as->upper[i]);
400 as->upper[i] = gfc_copy_expr (as->lower[i]);
401 mpz_sub_ui (as->upper[i]->value.integer,
402 as->upper[i]->value.integer, 1);
406 as->resolved = true;
408 return true;
412 /* Match a single array element specification. The return values as
413 well as the upper and lower bounds of the array spec are filled
414 in according to what we see on the input. The caller makes sure
415 individual specifications make sense as a whole.
418 Parsed Lower Upper Returned
419 ------------------------------------
420 : NULL NULL AS_DEFERRED (*)
421 x 1 x AS_EXPLICIT
422 x: x NULL AS_ASSUMED_SHAPE
423 x:y x y AS_EXPLICIT
424 x:* x NULL AS_ASSUMED_SIZE
425 * 1 NULL AS_ASSUMED_SIZE
427 (*) For non-pointer dummy arrays this is AS_ASSUMED_SHAPE. This
428 is fixed during the resolution of formal interfaces.
430 Anything else AS_UNKNOWN. */
432 static array_type
433 match_array_element_spec (gfc_array_spec *as)
435 gfc_expr **upper, **lower;
436 match m;
437 int rank;
439 rank = as->rank == -1 ? 0 : as->rank;
440 lower = &as->lower[rank + as->corank - 1];
441 upper = &as->upper[rank + as->corank - 1];
443 if (gfc_match_char ('*') == MATCH_YES)
445 *lower = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
446 return AS_ASSUMED_SIZE;
449 if (gfc_match_char (':') == MATCH_YES)
450 return AS_DEFERRED;
452 m = gfc_match_expr (upper);
453 if (m == MATCH_NO)
454 gfc_error ("Expected expression in array specification at %C");
455 if (m != MATCH_YES)
456 return AS_UNKNOWN;
457 if (!gfc_expr_check_typed (*upper, gfc_current_ns, false))
458 return AS_UNKNOWN;
460 if (((*upper)->expr_type == EXPR_CONSTANT
461 && (*upper)->ts.type != BT_INTEGER) ||
462 ((*upper)->expr_type == EXPR_FUNCTION
463 && (*upper)->ts.type == BT_UNKNOWN
464 && (*upper)->symtree
465 && strcmp ((*upper)->symtree->name, "null") == 0))
467 gfc_error ("Expecting a scalar INTEGER expression at %C, found %s",
468 gfc_basic_typename ((*upper)->ts.type));
469 return AS_UNKNOWN;
472 if (gfc_match_char (':') == MATCH_NO)
474 *lower = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
475 return AS_EXPLICIT;
478 *lower = *upper;
479 *upper = NULL;
481 if (gfc_match_char ('*') == MATCH_YES)
482 return AS_ASSUMED_SIZE;
484 m = gfc_match_expr (upper);
485 if (m == MATCH_ERROR)
486 return AS_UNKNOWN;
487 if (m == MATCH_NO)
488 return AS_ASSUMED_SHAPE;
489 if (!gfc_expr_check_typed (*upper, gfc_current_ns, false))
490 return AS_UNKNOWN;
492 if (((*upper)->expr_type == EXPR_CONSTANT
493 && (*upper)->ts.type != BT_INTEGER) ||
494 ((*upper)->expr_type == EXPR_FUNCTION
495 && (*upper)->ts.type == BT_UNKNOWN
496 && (*upper)->symtree
497 && strcmp ((*upper)->symtree->name, "null") == 0))
499 gfc_error ("Expecting a scalar INTEGER expression at %C, found %s",
500 gfc_basic_typename ((*upper)->ts.type));
501 return AS_UNKNOWN;
504 return AS_EXPLICIT;
508 /* Matches an array specification, incidentally figuring out what sort
509 it is. Match either a normal array specification, or a coarray spec
510 or both. Optionally allow [:] for coarrays. */
512 match
513 gfc_match_array_spec (gfc_array_spec **asp, bool match_dim, bool match_codim)
515 array_type current_type;
516 gfc_array_spec *as;
517 int i;
519 as = gfc_get_array_spec ();
521 if (!match_dim)
522 goto coarray;
524 if (gfc_match_char ('(') != MATCH_YES)
526 if (!match_codim)
527 goto done;
528 goto coarray;
531 if (gfc_match (" .. )") == MATCH_YES)
533 as->type = AS_ASSUMED_RANK;
534 as->rank = -1;
536 if (!gfc_notify_std (GFC_STD_F2018, "Assumed-rank array at %C"))
537 goto cleanup;
539 if (!match_codim)
540 goto done;
541 goto coarray;
544 for (;;)
546 as->rank++;
547 current_type = match_array_element_spec (as);
549 /* Note that current_type == AS_ASSUMED_SIZE for both assumed-size
550 and implied-shape specifications. If the rank is at least 2, we can
551 distinguish between them. But for rank 1, we currently return
552 ASSUMED_SIZE; this gets adjusted later when we know for sure
553 whether the symbol parsed is a PARAMETER or not. */
555 if (as->rank == 1)
557 if (current_type == AS_UNKNOWN)
558 goto cleanup;
559 as->type = current_type;
561 else
562 switch (as->type)
563 { /* See how current spec meshes with the existing. */
564 case AS_UNKNOWN:
565 goto cleanup;
567 case AS_IMPLIED_SHAPE:
568 if (current_type != AS_ASSUMED_SHAPE)
570 gfc_error ("Bad array specification for implied-shape"
571 " array at %C");
572 goto cleanup;
574 break;
576 case AS_EXPLICIT:
577 if (current_type == AS_ASSUMED_SIZE)
579 as->type = AS_ASSUMED_SIZE;
580 break;
583 if (current_type == AS_EXPLICIT)
584 break;
586 gfc_error ("Bad array specification for an explicitly shaped "
587 "array at %C");
589 goto cleanup;
591 case AS_ASSUMED_SHAPE:
592 if ((current_type == AS_ASSUMED_SHAPE)
593 || (current_type == AS_DEFERRED))
594 break;
596 gfc_error ("Bad array specification for assumed shape "
597 "array at %C");
598 goto cleanup;
600 case AS_DEFERRED:
601 if (current_type == AS_DEFERRED)
602 break;
604 if (current_type == AS_ASSUMED_SHAPE)
606 as->type = AS_ASSUMED_SHAPE;
607 break;
610 gfc_error ("Bad specification for deferred shape array at %C");
611 goto cleanup;
613 case AS_ASSUMED_SIZE:
614 if (as->rank == 2 && current_type == AS_ASSUMED_SIZE)
616 as->type = AS_IMPLIED_SHAPE;
617 break;
620 gfc_error ("Bad specification for assumed size array at %C");
621 goto cleanup;
623 case AS_ASSUMED_RANK:
624 gcc_unreachable ();
627 if (gfc_match_char (')') == MATCH_YES)
628 break;
630 if (gfc_match_char (',') != MATCH_YES)
632 gfc_error ("Expected another dimension in array declaration at %C");
633 goto cleanup;
636 if (as->rank + as->corank >= GFC_MAX_DIMENSIONS)
638 gfc_error ("Array specification at %C has more than %d dimensions",
639 GFC_MAX_DIMENSIONS);
640 goto cleanup;
643 if (as->corank + as->rank >= 7
644 && !gfc_notify_std (GFC_STD_F2008, "Array specification at %C "
645 "with more than 7 dimensions"))
646 goto cleanup;
649 if (!match_codim)
650 goto done;
652 coarray:
653 if (gfc_match_char ('[') != MATCH_YES)
654 goto done;
656 if (!gfc_notify_std (GFC_STD_F2008, "Coarray declaration at %C"))
657 goto cleanup;
659 if (flag_coarray == GFC_FCOARRAY_NONE)
661 gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to enable");
662 goto cleanup;
665 if (as->rank >= GFC_MAX_DIMENSIONS)
667 gfc_error ("Array specification at %C has more than %d "
668 "dimensions", GFC_MAX_DIMENSIONS);
669 goto cleanup;
672 for (;;)
674 as->corank++;
675 current_type = match_array_element_spec (as);
677 if (current_type == AS_UNKNOWN)
678 goto cleanup;
680 if (as->corank == 1)
681 as->cotype = current_type;
682 else
683 switch (as->cotype)
684 { /* See how current spec meshes with the existing. */
685 case AS_IMPLIED_SHAPE:
686 case AS_UNKNOWN:
687 goto cleanup;
689 case AS_EXPLICIT:
690 if (current_type == AS_ASSUMED_SIZE)
692 as->cotype = AS_ASSUMED_SIZE;
693 break;
696 if (current_type == AS_EXPLICIT)
697 break;
699 gfc_error ("Bad array specification for an explicitly "
700 "shaped array at %C");
702 goto cleanup;
704 case AS_ASSUMED_SHAPE:
705 if ((current_type == AS_ASSUMED_SHAPE)
706 || (current_type == AS_DEFERRED))
707 break;
709 gfc_error ("Bad array specification for assumed shape "
710 "array at %C");
711 goto cleanup;
713 case AS_DEFERRED:
714 if (current_type == AS_DEFERRED)
715 break;
717 if (current_type == AS_ASSUMED_SHAPE)
719 as->cotype = AS_ASSUMED_SHAPE;
720 break;
723 gfc_error ("Bad specification for deferred shape array at %C");
724 goto cleanup;
726 case AS_ASSUMED_SIZE:
727 gfc_error ("Bad specification for assumed size array at %C");
728 goto cleanup;
730 case AS_ASSUMED_RANK:
731 gcc_unreachable ();
734 if (gfc_match_char (']') == MATCH_YES)
735 break;
737 if (gfc_match_char (',') != MATCH_YES)
739 gfc_error ("Expected another dimension in array declaration at %C");
740 goto cleanup;
743 if (as->rank + as->corank >= GFC_MAX_DIMENSIONS)
745 gfc_error ("Array specification at %C has more than %d "
746 "dimensions", GFC_MAX_DIMENSIONS);
747 goto cleanup;
751 if (current_type == AS_EXPLICIT)
753 gfc_error ("Upper bound of last coarray dimension must be %<*%> at %C");
754 goto cleanup;
757 if (as->cotype == AS_ASSUMED_SIZE)
758 as->cotype = AS_EXPLICIT;
760 if (as->rank == 0)
761 as->type = as->cotype;
763 done:
764 if (as->rank == 0 && as->corank == 0)
766 *asp = NULL;
767 gfc_free_array_spec (as);
768 return MATCH_NO;
771 /* If a lower bounds of an assumed shape array is blank, put in one. */
772 if (as->type == AS_ASSUMED_SHAPE)
774 for (i = 0; i < as->rank + as->corank; i++)
776 if (as->lower[i] == NULL)
777 as->lower[i] = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
781 *asp = as;
783 return MATCH_YES;
785 cleanup:
786 /* Something went wrong. */
787 gfc_free_array_spec (as);
788 return MATCH_ERROR;
792 /* Given a symbol and an array specification, modify the symbol to
793 have that array specification. The error locus is needed in case
794 something goes wrong. On failure, the caller must free the spec. */
796 bool
797 gfc_set_array_spec (gfc_symbol *sym, gfc_array_spec *as, locus *error_loc)
799 int i;
801 if (as == NULL)
802 return true;
804 if (as->rank
805 && !gfc_add_dimension (&sym->attr, sym->name, error_loc))
806 return false;
808 if (as->corank
809 && !gfc_add_codimension (&sym->attr, sym->name, error_loc))
810 return false;
812 if (sym->as == NULL)
814 sym->as = as;
815 return true;
818 if ((sym->as->type == AS_ASSUMED_RANK && as->corank)
819 || (as->type == AS_ASSUMED_RANK && sym->as->corank))
821 gfc_error ("The assumed-rank array %qs at %L shall not have a "
822 "codimension", sym->name, error_loc);
823 return false;
826 if (as->corank)
828 /* The "sym" has no corank (checked via gfc_add_codimension). Thus
829 the codimension is simply added. */
830 gcc_assert (as->rank == 0 && sym->as->corank == 0);
832 sym->as->cotype = as->cotype;
833 sym->as->corank = as->corank;
834 for (i = 0; i < as->corank; i++)
836 sym->as->lower[sym->as->rank + i] = as->lower[i];
837 sym->as->upper[sym->as->rank + i] = as->upper[i];
840 else
842 /* The "sym" has no rank (checked via gfc_add_dimension). Thus
843 the dimension is added - but first the codimensions (if existing
844 need to be shifted to make space for the dimension. */
845 gcc_assert (as->corank == 0 && sym->as->rank == 0);
847 sym->as->rank = as->rank;
848 sym->as->type = as->type;
849 sym->as->cray_pointee = as->cray_pointee;
850 sym->as->cp_was_assumed = as->cp_was_assumed;
852 for (i = 0; i < sym->as->corank; i++)
854 sym->as->lower[as->rank + i] = sym->as->lower[i];
855 sym->as->upper[as->rank + i] = sym->as->upper[i];
857 for (i = 0; i < as->rank; i++)
859 sym->as->lower[i] = as->lower[i];
860 sym->as->upper[i] = as->upper[i];
864 free (as);
865 return true;
869 /* Copy an array specification. */
871 gfc_array_spec *
872 gfc_copy_array_spec (gfc_array_spec *src)
874 gfc_array_spec *dest;
875 int i;
877 if (src == NULL)
878 return NULL;
880 dest = gfc_get_array_spec ();
882 *dest = *src;
884 for (i = 0; i < dest->rank + dest->corank; i++)
886 dest->lower[i] = gfc_copy_expr (dest->lower[i]);
887 dest->upper[i] = gfc_copy_expr (dest->upper[i]);
890 return dest;
894 /* Returns nonzero if the two expressions are equal. Only handles integer
895 constants. */
897 static int
898 compare_bounds (gfc_expr *bound1, gfc_expr *bound2)
900 if (bound1 == NULL || bound2 == NULL
901 || bound1->expr_type != EXPR_CONSTANT
902 || bound2->expr_type != EXPR_CONSTANT
903 || bound1->ts.type != BT_INTEGER
904 || bound2->ts.type != BT_INTEGER)
905 gfc_internal_error ("gfc_compare_array_spec(): Array spec clobbered");
907 if (mpz_cmp (bound1->value.integer, bound2->value.integer) == 0)
908 return 1;
909 else
910 return 0;
914 /* Compares two array specifications. They must be constant or deferred
915 shape. */
918 gfc_compare_array_spec (gfc_array_spec *as1, gfc_array_spec *as2)
920 int i;
922 if (as1 == NULL && as2 == NULL)
923 return 1;
925 if (as1 == NULL || as2 == NULL)
926 return 0;
928 if (as1->rank != as2->rank)
929 return 0;
931 if (as1->corank != as2->corank)
932 return 0;
934 if (as1->rank == 0)
935 return 1;
937 if (as1->type != as2->type)
938 return 0;
940 if (as1->type == AS_EXPLICIT)
941 for (i = 0; i < as1->rank + as1->corank; i++)
943 if (compare_bounds (as1->lower[i], as2->lower[i]) == 0)
944 return 0;
946 if (compare_bounds (as1->upper[i], as2->upper[i]) == 0)
947 return 0;
950 return 1;
954 /****************** Array constructor functions ******************/
957 /* Given an expression node that might be an array constructor and a
958 symbol, make sure that no iterators in this or child constructors
959 use the symbol as an implied-DO iterator. Returns nonzero if a
960 duplicate was found. */
962 static int
963 check_duplicate_iterator (gfc_constructor_base base, gfc_symbol *master)
965 gfc_constructor *c;
966 gfc_expr *e;
968 for (c = gfc_constructor_first (base); c; c = gfc_constructor_next (c))
970 e = c->expr;
972 if (e->expr_type == EXPR_ARRAY
973 && check_duplicate_iterator (e->value.constructor, master))
974 return 1;
976 if (c->iterator == NULL)
977 continue;
979 if (c->iterator->var->symtree->n.sym == master)
981 gfc_error ("DO-iterator %qs at %L is inside iterator of the "
982 "same name", master->name, &c->where);
984 return 1;
988 return 0;
992 /* Forward declaration because these functions are mutually recursive. */
993 static match match_array_cons_element (gfc_constructor_base *);
995 /* Match a list of array elements. */
997 static match
998 match_array_list (gfc_constructor_base *result)
1000 gfc_constructor_base head;
1001 gfc_constructor *p;
1002 gfc_iterator iter;
1003 locus old_loc;
1004 gfc_expr *e;
1005 match m;
1006 int n;
1008 old_loc = gfc_current_locus;
1010 if (gfc_match_char ('(') == MATCH_NO)
1011 return MATCH_NO;
1013 memset (&iter, '\0', sizeof (gfc_iterator));
1014 head = NULL;
1016 m = match_array_cons_element (&head);
1017 if (m != MATCH_YES)
1018 goto cleanup;
1020 if (gfc_match_char (',') != MATCH_YES)
1022 m = MATCH_NO;
1023 goto cleanup;
1026 for (n = 1;; n++)
1028 m = gfc_match_iterator (&iter, 0);
1029 if (m == MATCH_YES)
1030 break;
1031 if (m == MATCH_ERROR)
1032 goto cleanup;
1034 m = match_array_cons_element (&head);
1035 if (m == MATCH_ERROR)
1036 goto cleanup;
1037 if (m == MATCH_NO)
1039 if (n > 2)
1040 goto syntax;
1041 m = MATCH_NO;
1042 goto cleanup; /* Could be a complex constant */
1045 if (gfc_match_char (',') != MATCH_YES)
1047 if (n > 2)
1048 goto syntax;
1049 m = MATCH_NO;
1050 goto cleanup;
1054 if (gfc_match_char (')') != MATCH_YES)
1055 goto syntax;
1057 if (check_duplicate_iterator (head, iter.var->symtree->n.sym))
1059 m = MATCH_ERROR;
1060 goto cleanup;
1063 e = gfc_get_array_expr (BT_UNKNOWN, 0, &old_loc);
1064 e->value.constructor = head;
1066 p = gfc_constructor_append_expr (result, e, &gfc_current_locus);
1067 p->iterator = gfc_get_iterator ();
1068 *p->iterator = iter;
1070 return MATCH_YES;
1072 syntax:
1073 gfc_error ("Syntax error in array constructor at %C");
1074 m = MATCH_ERROR;
1076 cleanup:
1077 gfc_constructor_free (head);
1078 gfc_free_iterator (&iter, 0);
1079 gfc_current_locus = old_loc;
1080 return m;
1084 /* Match a single element of an array constructor, which can be a
1085 single expression or a list of elements. */
1087 static match
1088 match_array_cons_element (gfc_constructor_base *result)
1090 gfc_expr *expr;
1091 match m;
1093 m = match_array_list (result);
1094 if (m != MATCH_NO)
1095 return m;
1097 m = gfc_match_expr (&expr);
1098 if (m != MATCH_YES)
1099 return m;
1101 if (expr->expr_type == EXPR_FUNCTION
1102 && expr->ts.type == BT_UNKNOWN
1103 && strcmp(expr->symtree->name, "null") == 0)
1105 gfc_error ("NULL() at %C cannot appear in an array constructor");
1106 gfc_free_expr (expr);
1107 return MATCH_ERROR;
1110 gfc_constructor_append_expr (result, expr, &gfc_current_locus);
1111 return MATCH_YES;
1115 /* Match an array constructor. */
1117 match
1118 gfc_match_array_constructor (gfc_expr **result)
1120 gfc_constructor *c;
1121 gfc_constructor_base head;
1122 gfc_expr *expr;
1123 gfc_typespec ts;
1124 locus where;
1125 match m;
1126 const char *end_delim;
1127 bool seen_ts;
1129 head = NULL;
1130 seen_ts = false;
1132 if (gfc_match (" (/") == MATCH_NO)
1134 if (gfc_match (" [") == MATCH_NO)
1135 return MATCH_NO;
1136 else
1138 if (!gfc_notify_std (GFC_STD_F2003, "[...] "
1139 "style array constructors at %C"))
1140 return MATCH_ERROR;
1141 end_delim = " ]";
1144 else
1145 end_delim = " /)";
1147 where = gfc_current_locus;
1149 /* Try to match an optional "type-spec ::" */
1150 gfc_clear_ts (&ts);
1151 m = gfc_match_type_spec (&ts);
1152 if (m == MATCH_YES)
1154 seen_ts = (gfc_match (" ::") == MATCH_YES);
1156 if (seen_ts)
1158 if (!gfc_notify_std (GFC_STD_F2003, "Array constructor "
1159 "including type specification at %C"))
1160 goto cleanup;
1162 if (ts.deferred)
1164 gfc_error ("Type-spec at %L cannot contain a deferred "
1165 "type parameter", &where);
1166 goto cleanup;
1169 if (ts.type == BT_CHARACTER
1170 && ts.u.cl && !ts.u.cl->length && !ts.u.cl->length_from_typespec)
1172 gfc_error ("Type-spec at %L cannot contain an asterisk for a "
1173 "type parameter", &where);
1174 goto cleanup;
1178 else if (m == MATCH_ERROR)
1179 goto cleanup;
1181 if (!seen_ts)
1182 gfc_current_locus = where;
1184 if (gfc_match (end_delim) == MATCH_YES)
1186 if (seen_ts)
1187 goto done;
1188 else
1190 gfc_error ("Empty array constructor at %C is not allowed");
1191 goto cleanup;
1195 for (;;)
1197 m = match_array_cons_element (&head);
1198 if (m == MATCH_ERROR)
1199 goto cleanup;
1200 if (m == MATCH_NO)
1201 goto syntax;
1203 if (gfc_match_char (',') == MATCH_NO)
1204 break;
1207 if (gfc_match (end_delim) == MATCH_NO)
1208 goto syntax;
1210 done:
1211 /* Size must be calculated at resolution time. */
1212 if (seen_ts)
1214 expr = gfc_get_array_expr (ts.type, ts.kind, &where);
1215 expr->ts = ts;
1217 /* If the typespec is CHARACTER, check that array elements can
1218 be converted. See PR fortran/67803. */
1219 if (ts.type == BT_CHARACTER)
1221 c = gfc_constructor_first (head);
1222 for (; c; c = gfc_constructor_next (c))
1224 if (gfc_numeric_ts (&c->expr->ts)
1225 || c->expr->ts.type == BT_LOGICAL)
1227 gfc_error ("Incompatible typespec for array element at %L",
1228 &c->expr->where);
1229 return MATCH_ERROR;
1232 /* Special case null(). */
1233 if (c->expr->expr_type == EXPR_FUNCTION
1234 && c->expr->ts.type == BT_UNKNOWN
1235 && strcmp (c->expr->symtree->name, "null") == 0)
1237 gfc_error ("Incompatible typespec for array element at %L",
1238 &c->expr->where);
1239 return MATCH_ERROR;
1244 /* Walk the constructor and ensure type conversion for numeric types. */
1245 if (gfc_numeric_ts (&ts))
1247 c = gfc_constructor_first (head);
1248 for (; c; c = gfc_constructor_next (c))
1249 gfc_convert_type (c->expr, &ts, 1);
1252 else
1253 expr = gfc_get_array_expr (BT_UNKNOWN, 0, &where);
1255 expr->value.constructor = head;
1256 if (expr->ts.u.cl)
1257 expr->ts.u.cl->length_from_typespec = seen_ts;
1259 *result = expr;
1261 return MATCH_YES;
1263 syntax:
1264 gfc_error ("Syntax error in array constructor at %C");
1266 cleanup:
1267 gfc_constructor_free (head);
1268 return MATCH_ERROR;
1273 /************** Check array constructors for correctness **************/
1275 /* Given an expression, compare it's type with the type of the current
1276 constructor. Returns nonzero if an error was issued. The
1277 cons_state variable keeps track of whether the type of the
1278 constructor being read or resolved is known to be good, bad or just
1279 starting out. */
1281 static gfc_typespec constructor_ts;
1282 static enum
1283 { CONS_START, CONS_GOOD, CONS_BAD }
1284 cons_state;
1286 static int
1287 check_element_type (gfc_expr *expr, bool convert)
1289 if (cons_state == CONS_BAD)
1290 return 0; /* Suppress further errors */
1292 if (cons_state == CONS_START)
1294 if (expr->ts.type == BT_UNKNOWN)
1295 cons_state = CONS_BAD;
1296 else
1298 cons_state = CONS_GOOD;
1299 constructor_ts = expr->ts;
1302 return 0;
1305 if (gfc_compare_types (&constructor_ts, &expr->ts))
1306 return 0;
1308 if (convert)
1309 return gfc_convert_type(expr, &constructor_ts, 1) ? 0 : 1;
1311 gfc_error ("Element in %s array constructor at %L is %s",
1312 gfc_typename (&constructor_ts), &expr->where,
1313 gfc_typename (&expr->ts));
1315 cons_state = CONS_BAD;
1316 return 1;
1320 /* Recursive work function for gfc_check_constructor_type(). */
1322 static bool
1323 check_constructor_type (gfc_constructor_base base, bool convert)
1325 gfc_constructor *c;
1326 gfc_expr *e;
1328 for (c = gfc_constructor_first (base); c; c = gfc_constructor_next (c))
1330 e = c->expr;
1332 if (e->expr_type == EXPR_ARRAY)
1334 if (!check_constructor_type (e->value.constructor, convert))
1335 return false;
1337 continue;
1340 if (check_element_type (e, convert))
1341 return false;
1344 return true;
1348 /* Check that all elements of an array constructor are the same type.
1349 On false, an error has been generated. */
1351 bool
1352 gfc_check_constructor_type (gfc_expr *e)
1354 bool t;
1356 if (e->ts.type != BT_UNKNOWN)
1358 cons_state = CONS_GOOD;
1359 constructor_ts = e->ts;
1361 else
1363 cons_state = CONS_START;
1364 gfc_clear_ts (&constructor_ts);
1367 /* If e->ts.type != BT_UNKNOWN, the array constructor included a
1368 typespec, and we will now convert the values on the fly. */
1369 t = check_constructor_type (e->value.constructor, e->ts.type != BT_UNKNOWN);
1370 if (t && e->ts.type == BT_UNKNOWN)
1371 e->ts = constructor_ts;
1373 return t;
1378 typedef struct cons_stack
1380 gfc_iterator *iterator;
1381 struct cons_stack *previous;
1383 cons_stack;
1385 static cons_stack *base;
1387 static bool check_constructor (gfc_constructor_base, bool (*) (gfc_expr *));
1389 /* Check an EXPR_VARIABLE expression in a constructor to make sure
1390 that that variable is an iteration variables. */
1392 bool
1393 gfc_check_iter_variable (gfc_expr *expr)
1395 gfc_symbol *sym;
1396 cons_stack *c;
1398 sym = expr->symtree->n.sym;
1400 for (c = base; c && c->iterator; c = c->previous)
1401 if (sym == c->iterator->var->symtree->n.sym)
1402 return true;
1404 return false;
1408 /* Recursive work function for gfc_check_constructor(). This amounts
1409 to calling the check function for each expression in the
1410 constructor, giving variables with the names of iterators a pass. */
1412 static bool
1413 check_constructor (gfc_constructor_base ctor, bool (*check_function) (gfc_expr *))
1415 cons_stack element;
1416 gfc_expr *e;
1417 bool t;
1418 gfc_constructor *c;
1420 for (c = gfc_constructor_first (ctor); c; c = gfc_constructor_next (c))
1422 e = c->expr;
1424 if (!e)
1425 continue;
1427 if (e->expr_type != EXPR_ARRAY)
1429 if (!(*check_function)(e))
1430 return false;
1431 continue;
1434 element.previous = base;
1435 element.iterator = c->iterator;
1437 base = &element;
1438 t = check_constructor (e->value.constructor, check_function);
1439 base = element.previous;
1441 if (!t)
1442 return false;
1445 /* Nothing went wrong, so all OK. */
1446 return true;
1450 /* Checks a constructor to see if it is a particular kind of
1451 expression -- specification, restricted, or initialization as
1452 determined by the check_function. */
1454 bool
1455 gfc_check_constructor (gfc_expr *expr, bool (*check_function) (gfc_expr *))
1457 cons_stack *base_save;
1458 bool t;
1460 base_save = base;
1461 base = NULL;
1463 t = check_constructor (expr->value.constructor, check_function);
1464 base = base_save;
1466 return t;
1471 /**************** Simplification of array constructors ****************/
1473 iterator_stack *iter_stack;
1475 typedef struct
1477 gfc_constructor_base base;
1478 int extract_count, extract_n;
1479 gfc_expr *extracted;
1480 mpz_t *count;
1482 mpz_t *offset;
1483 gfc_component *component;
1484 mpz_t *repeat;
1486 bool (*expand_work_function) (gfc_expr *);
1488 expand_info;
1490 static expand_info current_expand;
1492 static bool expand_constructor (gfc_constructor_base);
1495 /* Work function that counts the number of elements present in a
1496 constructor. */
1498 static bool
1499 count_elements (gfc_expr *e)
1501 mpz_t result;
1503 if (e->rank == 0)
1504 mpz_add_ui (*current_expand.count, *current_expand.count, 1);
1505 else
1507 if (!gfc_array_size (e, &result))
1509 gfc_free_expr (e);
1510 return false;
1513 mpz_add (*current_expand.count, *current_expand.count, result);
1514 mpz_clear (result);
1517 gfc_free_expr (e);
1518 return true;
1522 /* Work function that extracts a particular element from an array
1523 constructor, freeing the rest. */
1525 static bool
1526 extract_element (gfc_expr *e)
1528 if (e->rank != 0)
1529 { /* Something unextractable */
1530 gfc_free_expr (e);
1531 return false;
1534 if (current_expand.extract_count == current_expand.extract_n)
1535 current_expand.extracted = e;
1536 else
1537 gfc_free_expr (e);
1539 current_expand.extract_count++;
1541 return true;
1545 /* Work function that constructs a new constructor out of the old one,
1546 stringing new elements together. */
1548 static bool
1549 expand (gfc_expr *e)
1551 gfc_constructor *c = gfc_constructor_append_expr (&current_expand.base,
1552 e, &e->where);
1554 c->n.component = current_expand.component;
1555 return true;
1559 /* Given an initialization expression that is a variable reference,
1560 substitute the current value of the iteration variable. */
1562 void
1563 gfc_simplify_iterator_var (gfc_expr *e)
1565 iterator_stack *p;
1567 for (p = iter_stack; p; p = p->prev)
1568 if (e->symtree == p->variable)
1569 break;
1571 if (p == NULL)
1572 return; /* Variable not found */
1574 gfc_replace_expr (e, gfc_get_int_expr (gfc_default_integer_kind, NULL, 0));
1576 mpz_set (e->value.integer, p->value);
1578 return;
1582 /* Expand an expression with that is inside of a constructor,
1583 recursing into other constructors if present. */
1585 static bool
1586 expand_expr (gfc_expr *e)
1588 if (e->expr_type == EXPR_ARRAY)
1589 return expand_constructor (e->value.constructor);
1591 e = gfc_copy_expr (e);
1593 if (!gfc_simplify_expr (e, 1))
1595 gfc_free_expr (e);
1596 return false;
1599 return current_expand.expand_work_function (e);
1603 static bool
1604 expand_iterator (gfc_constructor *c)
1606 gfc_expr *start, *end, *step;
1607 iterator_stack frame;
1608 mpz_t trip;
1609 bool t;
1611 end = step = NULL;
1613 t = false;
1615 mpz_init (trip);
1616 mpz_init (frame.value);
1617 frame.prev = NULL;
1619 start = gfc_copy_expr (c->iterator->start);
1620 if (!gfc_simplify_expr (start, 1))
1621 goto cleanup;
1623 if (start->expr_type != EXPR_CONSTANT || start->ts.type != BT_INTEGER)
1624 goto cleanup;
1626 end = gfc_copy_expr (c->iterator->end);
1627 if (!gfc_simplify_expr (end, 1))
1628 goto cleanup;
1630 if (end->expr_type != EXPR_CONSTANT || end->ts.type != BT_INTEGER)
1631 goto cleanup;
1633 step = gfc_copy_expr (c->iterator->step);
1634 if (!gfc_simplify_expr (step, 1))
1635 goto cleanup;
1637 if (step->expr_type != EXPR_CONSTANT || step->ts.type != BT_INTEGER)
1638 goto cleanup;
1640 if (mpz_sgn (step->value.integer) == 0)
1642 gfc_error ("Iterator step at %L cannot be zero", &step->where);
1643 goto cleanup;
1646 /* Calculate the trip count of the loop. */
1647 mpz_sub (trip, end->value.integer, start->value.integer);
1648 mpz_add (trip, trip, step->value.integer);
1649 mpz_tdiv_q (trip, trip, step->value.integer);
1651 mpz_set (frame.value, start->value.integer);
1653 frame.prev = iter_stack;
1654 frame.variable = c->iterator->var->symtree;
1655 iter_stack = &frame;
1657 while (mpz_sgn (trip) > 0)
1659 if (!expand_expr (c->expr))
1660 goto cleanup;
1662 mpz_add (frame.value, frame.value, step->value.integer);
1663 mpz_sub_ui (trip, trip, 1);
1666 t = true;
1668 cleanup:
1669 gfc_free_expr (start);
1670 gfc_free_expr (end);
1671 gfc_free_expr (step);
1673 mpz_clear (trip);
1674 mpz_clear (frame.value);
1676 iter_stack = frame.prev;
1678 return t;
1682 /* Expand a constructor into constant constructors without any
1683 iterators, calling the work function for each of the expanded
1684 expressions. The work function needs to either save or free the
1685 passed expression. */
1687 static bool
1688 expand_constructor (gfc_constructor_base base)
1690 gfc_constructor *c;
1691 gfc_expr *e;
1693 for (c = gfc_constructor_first (base); c; c = gfc_constructor_next(c))
1695 if (c->iterator != NULL)
1697 if (!expand_iterator (c))
1698 return false;
1699 continue;
1702 e = c->expr;
1704 if (e->expr_type == EXPR_ARRAY)
1706 if (!expand_constructor (e->value.constructor))
1707 return false;
1709 continue;
1712 e = gfc_copy_expr (e);
1713 if (!gfc_simplify_expr (e, 1))
1715 gfc_free_expr (e);
1716 return false;
1718 current_expand.offset = &c->offset;
1719 current_expand.repeat = &c->repeat;
1720 current_expand.component = c->n.component;
1721 if (!current_expand.expand_work_function(e))
1722 return false;
1724 return true;
1728 /* Given an array expression and an element number (starting at zero),
1729 return a pointer to the array element. NULL is returned if the
1730 size of the array has been exceeded. The expression node returned
1731 remains a part of the array and should not be freed. Access is not
1732 efficient at all, but this is another place where things do not
1733 have to be particularly fast. */
1735 static gfc_expr *
1736 gfc_get_array_element (gfc_expr *array, int element)
1738 expand_info expand_save;
1739 gfc_expr *e;
1740 bool rc;
1742 expand_save = current_expand;
1743 current_expand.extract_n = element;
1744 current_expand.expand_work_function = extract_element;
1745 current_expand.extracted = NULL;
1746 current_expand.extract_count = 0;
1748 iter_stack = NULL;
1750 rc = expand_constructor (array->value.constructor);
1751 e = current_expand.extracted;
1752 current_expand = expand_save;
1754 if (!rc)
1755 return NULL;
1757 return e;
1761 /* Top level subroutine for expanding constructors. We only expand
1762 constructor if they are small enough. */
1764 bool
1765 gfc_expand_constructor (gfc_expr *e, bool fatal)
1767 expand_info expand_save;
1768 gfc_expr *f;
1769 bool rc;
1771 /* If we can successfully get an array element at the max array size then
1772 the array is too big to expand, so we just return. */
1773 f = gfc_get_array_element (e, flag_max_array_constructor);
1774 if (f != NULL)
1776 gfc_free_expr (f);
1777 if (fatal)
1779 gfc_error ("The number of elements in the array constructor "
1780 "at %L requires an increase of the allowed %d "
1781 "upper limit. See %<-fmax-array-constructor%> "
1782 "option", &e->where, flag_max_array_constructor);
1783 return false;
1785 return true;
1788 /* We now know the array is not too big so go ahead and try to expand it. */
1789 expand_save = current_expand;
1790 current_expand.base = NULL;
1792 iter_stack = NULL;
1794 current_expand.expand_work_function = expand;
1796 if (!expand_constructor (e->value.constructor))
1798 gfc_constructor_free (current_expand.base);
1799 rc = false;
1800 goto done;
1803 gfc_constructor_free (e->value.constructor);
1804 e->value.constructor = current_expand.base;
1806 rc = true;
1808 done:
1809 current_expand = expand_save;
1811 return rc;
1815 /* Work function for checking that an element of a constructor is a
1816 constant, after removal of any iteration variables. We return
1817 false if not so. */
1819 static bool
1820 is_constant_element (gfc_expr *e)
1822 int rv;
1824 rv = gfc_is_constant_expr (e);
1825 gfc_free_expr (e);
1827 return rv ? true : false;
1831 /* Given an array constructor, determine if the constructor is
1832 constant or not by expanding it and making sure that all elements
1833 are constants. This is a bit of a hack since something like (/ (i,
1834 i=1,100000000) /) will take a while as* opposed to a more clever
1835 function that traverses the expression tree. FIXME. */
1838 gfc_constant_ac (gfc_expr *e)
1840 expand_info expand_save;
1841 bool rc;
1843 iter_stack = NULL;
1844 expand_save = current_expand;
1845 current_expand.expand_work_function = is_constant_element;
1847 rc = expand_constructor (e->value.constructor);
1849 current_expand = expand_save;
1850 if (!rc)
1851 return 0;
1853 return 1;
1857 /* Returns nonzero if an array constructor has been completely
1858 expanded (no iterators) and zero if iterators are present. */
1861 gfc_expanded_ac (gfc_expr *e)
1863 gfc_constructor *c;
1865 if (e->expr_type == EXPR_ARRAY)
1866 for (c = gfc_constructor_first (e->value.constructor);
1867 c; c = gfc_constructor_next (c))
1868 if (c->iterator != NULL || !gfc_expanded_ac (c->expr))
1869 return 0;
1871 return 1;
1875 /*************** Type resolution of array constructors ***************/
1878 /* The symbol expr_is_sought_symbol_ref will try to find. */
1879 static const gfc_symbol *sought_symbol = NULL;
1882 /* Tells whether the expression E is a variable reference to the symbol
1883 in the static variable SOUGHT_SYMBOL, and sets the locus pointer WHERE
1884 accordingly.
1885 To be used with gfc_expr_walker: if a reference is found we don't need
1886 to look further so we return 1 to skip any further walk. */
1888 static int
1889 expr_is_sought_symbol_ref (gfc_expr **e, int *walk_subtrees ATTRIBUTE_UNUSED,
1890 void *where)
1892 gfc_expr *expr = *e;
1893 locus *sym_loc = (locus *)where;
1895 if (expr->expr_type == EXPR_VARIABLE
1896 && expr->symtree->n.sym == sought_symbol)
1898 *sym_loc = expr->where;
1899 return 1;
1902 return 0;
1906 /* Tells whether the expression EXPR contains a reference to the symbol
1907 SYM and in that case sets the position SYM_LOC where the reference is. */
1909 static bool
1910 find_symbol_in_expr (gfc_symbol *sym, gfc_expr *expr, locus *sym_loc)
1912 int ret;
1914 sought_symbol = sym;
1915 ret = gfc_expr_walker (&expr, &expr_is_sought_symbol_ref, sym_loc);
1916 sought_symbol = NULL;
1917 return ret;
1921 /* Recursive array list resolution function. All of the elements must
1922 be of the same type. */
1924 static bool
1925 resolve_array_list (gfc_constructor_base base)
1927 bool t;
1928 gfc_constructor *c;
1929 gfc_iterator *iter;
1931 t = true;
1933 for (c = gfc_constructor_first (base); c; c = gfc_constructor_next (c))
1935 iter = c->iterator;
1936 if (iter != NULL)
1938 gfc_symbol *iter_var;
1939 locus iter_var_loc;
1941 if (!gfc_resolve_iterator (iter, false, true))
1942 t = false;
1944 /* Check for bounds referencing the iterator variable. */
1945 gcc_assert (iter->var->expr_type == EXPR_VARIABLE);
1946 iter_var = iter->var->symtree->n.sym;
1947 if (find_symbol_in_expr (iter_var, iter->start, &iter_var_loc))
1949 if (!gfc_notify_std (GFC_STD_LEGACY, "AC-IMPLIED-DO initial "
1950 "expression references control variable "
1951 "at %L", &iter_var_loc))
1952 t = false;
1954 if (find_symbol_in_expr (iter_var, iter->end, &iter_var_loc))
1956 if (!gfc_notify_std (GFC_STD_LEGACY, "AC-IMPLIED-DO final "
1957 "expression references control variable "
1958 "at %L", &iter_var_loc))
1959 t = false;
1961 if (find_symbol_in_expr (iter_var, iter->step, &iter_var_loc))
1963 if (!gfc_notify_std (GFC_STD_LEGACY, "AC-IMPLIED-DO step "
1964 "expression references control variable "
1965 "at %L", &iter_var_loc))
1966 t = false;
1970 if (!gfc_resolve_expr (c->expr))
1971 t = false;
1973 if (UNLIMITED_POLY (c->expr))
1975 gfc_error ("Array constructor value at %L shall not be unlimited "
1976 "polymorphic [F2008: C4106]", &c->expr->where);
1977 t = false;
1981 return t;
1984 /* Resolve character array constructor. If it has a specified constant character
1985 length, pad/truncate the elements here; if the length is not specified and
1986 all elements are of compile-time known length, emit an error as this is
1987 invalid. */
1989 bool
1990 gfc_resolve_character_array_constructor (gfc_expr *expr)
1992 gfc_constructor *p;
1993 HOST_WIDE_INT found_length;
1995 gcc_assert (expr->expr_type == EXPR_ARRAY);
1996 gcc_assert (expr->ts.type == BT_CHARACTER);
1998 if (expr->ts.u.cl == NULL)
2000 for (p = gfc_constructor_first (expr->value.constructor);
2001 p; p = gfc_constructor_next (p))
2002 if (p->expr->ts.u.cl != NULL)
2004 /* Ensure that if there is a char_len around that it is
2005 used; otherwise the middle-end confuses them! */
2006 expr->ts.u.cl = p->expr->ts.u.cl;
2007 goto got_charlen;
2010 expr->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
2013 got_charlen:
2015 /* Early exit for zero size arrays. */
2016 if (expr->shape)
2018 mpz_t size;
2019 HOST_WIDE_INT arraysize;
2021 gfc_array_size (expr, &size);
2022 arraysize = mpz_get_ui (size);
2023 mpz_clear (size);
2025 if (arraysize == 0)
2026 return true;
2029 found_length = -1;
2031 if (expr->ts.u.cl->length == NULL)
2033 /* Check that all constant string elements have the same length until
2034 we reach the end or find a variable-length one. */
2036 for (p = gfc_constructor_first (expr->value.constructor);
2037 p; p = gfc_constructor_next (p))
2039 HOST_WIDE_INT current_length = -1;
2040 gfc_ref *ref;
2041 for (ref = p->expr->ref; ref; ref = ref->next)
2042 if (ref->type == REF_SUBSTRING
2043 && ref->u.ss.start
2044 && ref->u.ss.start->expr_type == EXPR_CONSTANT
2045 && ref->u.ss.end
2046 && ref->u.ss.end->expr_type == EXPR_CONSTANT)
2047 break;
2049 if (p->expr->expr_type == EXPR_CONSTANT)
2050 current_length = p->expr->value.character.length;
2051 else if (ref)
2052 current_length = gfc_mpz_get_hwi (ref->u.ss.end->value.integer)
2053 - gfc_mpz_get_hwi (ref->u.ss.start->value.integer) + 1;
2054 else if (p->expr->ts.u.cl && p->expr->ts.u.cl->length
2055 && p->expr->ts.u.cl->length->expr_type == EXPR_CONSTANT)
2056 current_length = gfc_mpz_get_hwi (p->expr->ts.u.cl->length->value.integer);
2057 else
2058 return true;
2060 if (current_length < 0)
2061 current_length = 0;
2063 if (found_length == -1)
2064 found_length = current_length;
2065 else if (found_length != current_length)
2067 gfc_error ("Different CHARACTER lengths (%ld/%ld) in array"
2068 " constructor at %L", (long) found_length,
2069 (long) current_length, &p->expr->where);
2070 return false;
2073 gcc_assert (found_length == current_length);
2076 gcc_assert (found_length != -1);
2078 /* Update the character length of the array constructor. */
2079 expr->ts.u.cl->length = gfc_get_int_expr (gfc_charlen_int_kind,
2080 NULL, found_length);
2082 else
2084 /* We've got a character length specified. It should be an integer,
2085 otherwise an error is signalled elsewhere. */
2086 gcc_assert (expr->ts.u.cl->length);
2088 /* If we've got a constant character length, pad according to this.
2089 gfc_extract_int does check for BT_INTEGER and EXPR_CONSTANT and sets
2090 max_length only if they pass. */
2091 gfc_extract_hwi (expr->ts.u.cl->length, &found_length);
2093 /* Now pad/truncate the elements accordingly to the specified character
2094 length. This is ok inside this conditional, as in the case above
2095 (without typespec) all elements are verified to have the same length
2096 anyway. */
2097 if (found_length != -1)
2098 for (p = gfc_constructor_first (expr->value.constructor);
2099 p; p = gfc_constructor_next (p))
2100 if (p->expr->expr_type == EXPR_CONSTANT)
2102 gfc_expr *cl = NULL;
2103 HOST_WIDE_INT current_length = -1;
2104 bool has_ts;
2106 if (p->expr->ts.u.cl && p->expr->ts.u.cl->length)
2108 cl = p->expr->ts.u.cl->length;
2109 gfc_extract_hwi (cl, &current_length);
2112 /* If gfc_extract_int above set current_length, we implicitly
2113 know the type is BT_INTEGER and it's EXPR_CONSTANT. */
2115 has_ts = expr->ts.u.cl->length_from_typespec;
2117 if (! cl
2118 || (current_length != -1 && current_length != found_length))
2119 gfc_set_constant_character_len (found_length, p->expr,
2120 has_ts ? -1 : found_length);
2124 return true;
2128 /* Resolve all of the expressions in an array list. */
2130 bool
2131 gfc_resolve_array_constructor (gfc_expr *expr)
2133 bool t;
2135 t = resolve_array_list (expr->value.constructor);
2136 if (t)
2137 t = gfc_check_constructor_type (expr);
2139 /* gfc_resolve_character_array_constructor is called in gfc_resolve_expr after
2140 the call to this function, so we don't need to call it here; if it was
2141 called twice, an error message there would be duplicated. */
2143 return t;
2147 /* Copy an iterator structure. */
2149 gfc_iterator *
2150 gfc_copy_iterator (gfc_iterator *src)
2152 gfc_iterator *dest;
2154 if (src == NULL)
2155 return NULL;
2157 dest = gfc_get_iterator ();
2159 dest->var = gfc_copy_expr (src->var);
2160 dest->start = gfc_copy_expr (src->start);
2161 dest->end = gfc_copy_expr (src->end);
2162 dest->step = gfc_copy_expr (src->step);
2163 dest->unroll = src->unroll;
2165 return dest;
2169 /********* Subroutines for determining the size of an array *********/
2171 /* These are needed just to accommodate RESHAPE(). There are no
2172 diagnostics here, we just return a negative number if something
2173 goes wrong. */
2176 /* Get the size of single dimension of an array specification. The
2177 array is guaranteed to be one dimensional. */
2179 bool
2180 spec_dimen_size (gfc_array_spec *as, int dimen, mpz_t *result)
2182 if (as == NULL)
2183 return false;
2185 if (dimen < 0 || dimen > as->rank - 1)
2186 gfc_internal_error ("spec_dimen_size(): Bad dimension");
2188 if (as->type != AS_EXPLICIT
2189 || as->lower[dimen]->expr_type != EXPR_CONSTANT
2190 || as->upper[dimen]->expr_type != EXPR_CONSTANT
2191 || as->lower[dimen]->ts.type != BT_INTEGER
2192 || as->upper[dimen]->ts.type != BT_INTEGER)
2193 return false;
2195 mpz_init (*result);
2197 mpz_sub (*result, as->upper[dimen]->value.integer,
2198 as->lower[dimen]->value.integer);
2200 mpz_add_ui (*result, *result, 1);
2202 return true;
2206 bool
2207 spec_size (gfc_array_spec *as, mpz_t *result)
2209 mpz_t size;
2210 int d;
2212 if (!as || as->type == AS_ASSUMED_RANK)
2213 return false;
2215 mpz_init_set_ui (*result, 1);
2217 for (d = 0; d < as->rank; d++)
2219 if (!spec_dimen_size (as, d, &size))
2221 mpz_clear (*result);
2222 return false;
2225 mpz_mul (*result, *result, size);
2226 mpz_clear (size);
2229 return true;
2233 /* Get the number of elements in an array section. Optionally, also supply
2234 the end value. */
2236 bool
2237 gfc_ref_dimen_size (gfc_array_ref *ar, int dimen, mpz_t *result, mpz_t *end)
2239 mpz_t upper, lower, stride;
2240 mpz_t diff;
2241 bool t;
2242 gfc_expr *stride_expr = NULL;
2244 if (dimen < 0 || ar == NULL)
2245 gfc_internal_error ("gfc_ref_dimen_size(): Bad dimension");
2247 if (dimen > ar->dimen - 1)
2249 gfc_error ("Bad array dimension at %L", &ar->c_where[dimen]);
2250 return false;
2253 switch (ar->dimen_type[dimen])
2255 case DIMEN_ELEMENT:
2256 mpz_init (*result);
2257 mpz_set_ui (*result, 1);
2258 t = true;
2259 break;
2261 case DIMEN_VECTOR:
2262 t = gfc_array_size (ar->start[dimen], result); /* Recurse! */
2263 break;
2265 case DIMEN_RANGE:
2267 mpz_init (stride);
2269 if (ar->stride[dimen] == NULL)
2270 mpz_set_ui (stride, 1);
2271 else
2273 stride_expr = gfc_copy_expr(ar->stride[dimen]);
2275 if(!gfc_simplify_expr(stride_expr, 1))
2276 gfc_internal_error("Simplification error");
2278 if (stride_expr->expr_type != EXPR_CONSTANT
2279 || mpz_cmp_ui (stride_expr->value.integer, 0) == 0)
2281 mpz_clear (stride);
2282 return false;
2284 mpz_set (stride, stride_expr->value.integer);
2285 gfc_free_expr(stride_expr);
2288 /* Calculate the number of elements via gfc_dep_differce, but only if
2289 start and end are both supplied in the reference or the array spec.
2290 This is to guard against strange but valid code like
2292 subroutine foo(a,n)
2293 real a(1:n)
2294 n = 3
2295 print *,size(a(n-1:))
2297 where the user changes the value of a variable. If we have to
2298 determine end as well, we cannot do this using gfc_dep_difference.
2299 Fall back to the constants-only code then. */
2301 if (end == NULL)
2303 bool use_dep;
2305 use_dep = gfc_dep_difference (ar->end[dimen], ar->start[dimen],
2306 &diff);
2307 if (!use_dep && ar->end[dimen] == NULL && ar->start[dimen] == NULL)
2308 use_dep = gfc_dep_difference (ar->as->upper[dimen],
2309 ar->as->lower[dimen], &diff);
2311 if (use_dep)
2313 mpz_init (*result);
2314 mpz_add (*result, diff, stride);
2315 mpz_div (*result, *result, stride);
2316 if (mpz_cmp_ui (*result, 0) < 0)
2317 mpz_set_ui (*result, 0);
2319 mpz_clear (stride);
2320 mpz_clear (diff);
2321 return true;
2326 /* Constant-only code here, which covers more cases
2327 like a(:4) etc. */
2328 mpz_init (upper);
2329 mpz_init (lower);
2330 t = false;
2332 if (ar->start[dimen] == NULL)
2334 if (ar->as->lower[dimen] == NULL
2335 || ar->as->lower[dimen]->expr_type != EXPR_CONSTANT
2336 || ar->as->lower[dimen]->ts.type != BT_INTEGER)
2337 goto cleanup;
2338 mpz_set (lower, ar->as->lower[dimen]->value.integer);
2340 else
2342 if (ar->start[dimen]->expr_type != EXPR_CONSTANT)
2343 goto cleanup;
2344 mpz_set (lower, ar->start[dimen]->value.integer);
2347 if (ar->end[dimen] == NULL)
2349 if (ar->as->upper[dimen] == NULL
2350 || ar->as->upper[dimen]->expr_type != EXPR_CONSTANT
2351 || ar->as->upper[dimen]->ts.type != BT_INTEGER)
2352 goto cleanup;
2353 mpz_set (upper, ar->as->upper[dimen]->value.integer);
2355 else
2357 if (ar->end[dimen]->expr_type != EXPR_CONSTANT)
2358 goto cleanup;
2359 mpz_set (upper, ar->end[dimen]->value.integer);
2362 mpz_init (*result);
2363 mpz_sub (*result, upper, lower);
2364 mpz_add (*result, *result, stride);
2365 mpz_div (*result, *result, stride);
2367 /* Zero stride caught earlier. */
2368 if (mpz_cmp_ui (*result, 0) < 0)
2369 mpz_set_ui (*result, 0);
2370 t = true;
2372 if (end)
2374 mpz_init (*end);
2376 mpz_sub_ui (*end, *result, 1UL);
2377 mpz_mul (*end, *end, stride);
2378 mpz_add (*end, *end, lower);
2381 cleanup:
2382 mpz_clear (upper);
2383 mpz_clear (lower);
2384 mpz_clear (stride);
2385 return t;
2387 default:
2388 gfc_internal_error ("gfc_ref_dimen_size(): Bad dimen_type");
2391 return t;
2395 static bool
2396 ref_size (gfc_array_ref *ar, mpz_t *result)
2398 mpz_t size;
2399 int d;
2401 mpz_init_set_ui (*result, 1);
2403 for (d = 0; d < ar->dimen; d++)
2405 if (!gfc_ref_dimen_size (ar, d, &size, NULL))
2407 mpz_clear (*result);
2408 return false;
2411 mpz_mul (*result, *result, size);
2412 mpz_clear (size);
2415 return true;
2419 /* Given an array expression and a dimension, figure out how many
2420 elements it has along that dimension. Returns true if we were
2421 able to return a result in the 'result' variable, false
2422 otherwise. */
2424 bool
2425 gfc_array_dimen_size (gfc_expr *array, int dimen, mpz_t *result)
2427 gfc_ref *ref;
2428 int i;
2430 gcc_assert (array != NULL);
2432 if (array->ts.type == BT_CLASS)
2433 return false;
2435 if (array->rank == -1)
2436 return false;
2438 if (dimen < 0 || dimen > array->rank - 1)
2439 gfc_internal_error ("gfc_array_dimen_size(): Bad dimension");
2441 switch (array->expr_type)
2443 case EXPR_VARIABLE:
2444 case EXPR_FUNCTION:
2445 for (ref = array->ref; ref; ref = ref->next)
2447 if (ref->type != REF_ARRAY)
2448 continue;
2450 if (ref->u.ar.type == AR_FULL)
2451 return spec_dimen_size (ref->u.ar.as, dimen, result);
2453 if (ref->u.ar.type == AR_SECTION)
2455 for (i = 0; dimen >= 0; i++)
2456 if (ref->u.ar.dimen_type[i] != DIMEN_ELEMENT)
2457 dimen--;
2459 return gfc_ref_dimen_size (&ref->u.ar, i - 1, result, NULL);
2463 if (array->shape && array->shape[dimen])
2465 mpz_init_set (*result, array->shape[dimen]);
2466 return true;
2469 if (array->symtree->n.sym->attr.generic
2470 && array->value.function.esym != NULL)
2472 if (!spec_dimen_size (array->value.function.esym->as, dimen, result))
2473 return false;
2475 else if (!spec_dimen_size (array->symtree->n.sym->as, dimen, result))
2476 return false;
2478 break;
2480 case EXPR_ARRAY:
2481 if (array->shape == NULL) {
2482 /* Expressions with rank > 1 should have "shape" properly set */
2483 if ( array->rank != 1 )
2484 gfc_internal_error ("gfc_array_dimen_size(): Bad EXPR_ARRAY expr");
2485 return gfc_array_size(array, result);
2488 /* Fall through */
2489 default:
2490 if (array->shape == NULL)
2491 return false;
2493 mpz_init_set (*result, array->shape[dimen]);
2495 break;
2498 return true;
2502 /* Given an array expression, figure out how many elements are in the
2503 array. Returns true if this is possible, and sets the 'result'
2504 variable. Otherwise returns false. */
2506 bool
2507 gfc_array_size (gfc_expr *array, mpz_t *result)
2509 expand_info expand_save;
2510 gfc_ref *ref;
2511 int i;
2512 bool t;
2514 if (array->ts.type == BT_CLASS)
2515 return false;
2517 switch (array->expr_type)
2519 case EXPR_ARRAY:
2520 gfc_push_suppress_errors ();
2522 expand_save = current_expand;
2524 current_expand.count = result;
2525 mpz_init_set_ui (*result, 0);
2527 current_expand.expand_work_function = count_elements;
2528 iter_stack = NULL;
2530 t = expand_constructor (array->value.constructor);
2532 gfc_pop_suppress_errors ();
2534 if (!t)
2535 mpz_clear (*result);
2536 current_expand = expand_save;
2537 return t;
2539 case EXPR_VARIABLE:
2540 for (ref = array->ref; ref; ref = ref->next)
2542 if (ref->type != REF_ARRAY)
2543 continue;
2545 if (ref->u.ar.type == AR_FULL)
2546 return spec_size (ref->u.ar.as, result);
2548 if (ref->u.ar.type == AR_SECTION)
2549 return ref_size (&ref->u.ar, result);
2552 return spec_size (array->symtree->n.sym->as, result);
2555 default:
2556 if (array->rank == 0 || array->shape == NULL)
2557 return false;
2559 mpz_init_set_ui (*result, 1);
2561 for (i = 0; i < array->rank; i++)
2562 mpz_mul (*result, *result, array->shape[i]);
2564 break;
2567 return true;
2571 /* Given an array reference, return the shape of the reference in an
2572 array of mpz_t integers. */
2574 bool
2575 gfc_array_ref_shape (gfc_array_ref *ar, mpz_t *shape)
2577 int d;
2578 int i;
2580 d = 0;
2582 switch (ar->type)
2584 case AR_FULL:
2585 for (; d < ar->as->rank; d++)
2586 if (!spec_dimen_size (ar->as, d, &shape[d]))
2587 goto cleanup;
2589 return true;
2591 case AR_SECTION:
2592 for (i = 0; i < ar->dimen; i++)
2594 if (ar->dimen_type[i] != DIMEN_ELEMENT)
2596 if (!gfc_ref_dimen_size (ar, i, &shape[d], NULL))
2597 goto cleanup;
2598 d++;
2602 return true;
2604 default:
2605 break;
2608 cleanup:
2609 gfc_clear_shape (shape, d);
2610 return false;
2614 /* Given an array expression, find the array reference structure that
2615 characterizes the reference. */
2617 gfc_array_ref *
2618 gfc_find_array_ref (gfc_expr *e, bool allow_null)
2620 gfc_ref *ref;
2622 for (ref = e->ref; ref; ref = ref->next)
2623 if (ref->type == REF_ARRAY
2624 && (ref->u.ar.type == AR_FULL || ref->u.ar.type == AR_SECTION))
2625 break;
2627 if (ref == NULL)
2629 if (allow_null)
2630 return NULL;
2631 else
2632 gfc_internal_error ("gfc_find_array_ref(): No ref found");
2635 return &ref->u.ar;
2639 /* Find out if an array shape is known at compile time. */
2641 bool
2642 gfc_is_compile_time_shape (gfc_array_spec *as)
2644 if (as->type != AS_EXPLICIT)
2645 return false;
2647 for (int i = 0; i < as->rank; i++)
2648 if (!gfc_is_constant_expr (as->lower[i])
2649 || !gfc_is_constant_expr (as->upper[i]))
2650 return false;
2652 return true;