PR rtl-optimization/43520
[official-gcc.git] / gcc / fortran / array.c
blob5487be7aa4fd589efaac32cc7a07591e9683724c
1 /* Array things
2 Copyright (C) 2000, 2001, 2002, 2004, 2005, 2006, 2007, 2008, 2009, 2010
3 Free Software Foundation, Inc.
4 Contributed by Andy Vaught
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 #include "config.h"
23 #include "system.h"
24 #include "gfortran.h"
25 #include "match.h"
26 #include "constructor.h"
28 /**************** Array reference matching subroutines *****************/
30 /* Copy an array reference structure. */
32 gfc_array_ref *
33 gfc_copy_array_ref (gfc_array_ref *src)
35 gfc_array_ref *dest;
36 int i;
38 if (src == NULL)
39 return NULL;
41 dest = gfc_get_array_ref ();
43 *dest = *src;
45 for (i = 0; i < GFC_MAX_DIMENSIONS; i++)
47 dest->start[i] = gfc_copy_expr (src->start[i]);
48 dest->end[i] = gfc_copy_expr (src->end[i]);
49 dest->stride[i] = gfc_copy_expr (src->stride[i]);
52 dest->offset = gfc_copy_expr (src->offset);
54 return dest;
58 /* Match a single dimension of an array reference. This can be a
59 single element or an array section. Any modifications we've made
60 to the ar structure are cleaned up by the caller. If the init
61 is set, we require the subscript to be a valid initialization
62 expression. */
64 static match
65 match_subscript (gfc_array_ref *ar, int init, bool match_star)
67 match m;
68 bool star = false;
69 int i;
71 i = ar->dimen + ar->codimen;
73 ar->c_where[i] = gfc_current_locus;
74 ar->start[i] = ar->end[i] = ar->stride[i] = NULL;
76 /* We can't be sure of the difference between DIMEN_ELEMENT and
77 DIMEN_VECTOR until we know the type of the element itself at
78 resolution time. */
80 ar->dimen_type[i] = DIMEN_UNKNOWN;
82 if (gfc_match_char (':') == MATCH_YES)
83 goto end_element;
85 /* Get start element. */
86 if (match_star && (m = gfc_match_char ('*')) == MATCH_YES)
87 star = true;
89 if (!star && init)
90 m = gfc_match_init_expr (&ar->start[i]);
91 else if (!star)
92 m = gfc_match_expr (&ar->start[i]);
94 if (m == MATCH_NO)
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 (gfc_option.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, ar->codimen == (corank - 1));
226 if (m == MATCH_ERROR)
227 return MATCH_ERROR;
229 if (gfc_match_char (']') == MATCH_YES)
231 ar->codimen++;
232 return MATCH_YES;
235 if (gfc_match_char (',') != MATCH_YES)
237 gfc_error ("Invalid form of coarray reference at %C");
238 return MATCH_ERROR;
242 gfc_error ("Array reference at %C cannot have more than %d dimensions",
243 GFC_MAX_DIMENSIONS);
244 return MATCH_ERROR;
249 /************** Array specification matching subroutines ***************/
251 /* Free all of the expressions associated with array bounds
252 specifications. */
254 void
255 gfc_free_array_spec (gfc_array_spec *as)
257 int i;
259 if (as == NULL)
260 return;
262 for (i = 0; i < as->rank + as->corank; i++)
264 gfc_free_expr (as->lower[i]);
265 gfc_free_expr (as->upper[i]);
268 gfc_free (as);
272 /* Take an array bound, resolves the expression, that make up the
273 shape and check associated constraints. */
275 static gfc_try
276 resolve_array_bound (gfc_expr *e, int check_constant)
278 if (e == NULL)
279 return SUCCESS;
281 if (gfc_resolve_expr (e) == FAILURE
282 || gfc_specification_expr (e) == FAILURE)
283 return FAILURE;
285 if (check_constant && gfc_is_constant_expr (e) == 0)
287 gfc_error ("Variable '%s' at %L in this context must be constant",
288 e->symtree->n.sym->name, &e->where);
289 return FAILURE;
292 return SUCCESS;
296 /* Takes an array specification, resolves the expressions that make up
297 the shape and make sure everything is integral. */
299 gfc_try
300 gfc_resolve_array_spec (gfc_array_spec *as, int check_constant)
302 gfc_expr *e;
303 int i;
305 if (as == NULL)
306 return SUCCESS;
308 for (i = 0; i < as->rank + as->corank; i++)
310 e = as->lower[i];
311 if (resolve_array_bound (e, check_constant) == FAILURE)
312 return FAILURE;
314 e = as->upper[i];
315 if (resolve_array_bound (e, check_constant) == FAILURE)
316 return FAILURE;
318 if ((as->lower[i] == NULL) || (as->upper[i] == NULL))
319 continue;
321 /* If the size is negative in this dimension, set it to zero. */
322 if (as->lower[i]->expr_type == EXPR_CONSTANT
323 && as->upper[i]->expr_type == EXPR_CONSTANT
324 && mpz_cmp (as->upper[i]->value.integer,
325 as->lower[i]->value.integer) < 0)
327 gfc_free_expr (as->upper[i]);
328 as->upper[i] = gfc_copy_expr (as->lower[i]);
329 mpz_sub_ui (as->upper[i]->value.integer,
330 as->upper[i]->value.integer, 1);
334 return SUCCESS;
338 /* Match a single array element specification. The return values as
339 well as the upper and lower bounds of the array spec are filled
340 in according to what we see on the input. The caller makes sure
341 individual specifications make sense as a whole.
344 Parsed Lower Upper Returned
345 ------------------------------------
346 : NULL NULL AS_DEFERRED (*)
347 x 1 x AS_EXPLICIT
348 x: x NULL AS_ASSUMED_SHAPE
349 x:y x y AS_EXPLICIT
350 x:* x NULL AS_ASSUMED_SIZE
351 * 1 NULL AS_ASSUMED_SIZE
353 (*) For non-pointer dummy arrays this is AS_ASSUMED_SHAPE. This
354 is fixed during the resolution of formal interfaces.
356 Anything else AS_UNKNOWN. */
358 static array_type
359 match_array_element_spec (gfc_array_spec *as)
361 gfc_expr **upper, **lower;
362 match m;
364 lower = &as->lower[as->rank + as->corank - 1];
365 upper = &as->upper[as->rank + as->corank - 1];
367 if (gfc_match_char ('*') == MATCH_YES)
369 *lower = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
370 return AS_ASSUMED_SIZE;
373 if (gfc_match_char (':') == MATCH_YES)
374 return AS_DEFERRED;
376 m = gfc_match_expr (upper);
377 if (m == MATCH_NO)
378 gfc_error ("Expected expression in array specification at %C");
379 if (m != MATCH_YES)
380 return AS_UNKNOWN;
381 if (gfc_expr_check_typed (*upper, gfc_current_ns, false) == FAILURE)
382 return AS_UNKNOWN;
384 if (gfc_match_char (':') == MATCH_NO)
386 *lower = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
387 return AS_EXPLICIT;
390 *lower = *upper;
391 *upper = NULL;
393 if (gfc_match_char ('*') == MATCH_YES)
394 return AS_ASSUMED_SIZE;
396 m = gfc_match_expr (upper);
397 if (m == MATCH_ERROR)
398 return AS_UNKNOWN;
399 if (m == MATCH_NO)
400 return AS_ASSUMED_SHAPE;
401 if (gfc_expr_check_typed (*upper, gfc_current_ns, false) == FAILURE)
402 return AS_UNKNOWN;
404 return AS_EXPLICIT;
408 /* Matches an array specification, incidentally figuring out what sort
409 it is. Match either a normal array specification, or a coarray spec
410 or both. Optionally allow [:] for coarrays. */
412 match
413 gfc_match_array_spec (gfc_array_spec **asp, bool match_dim, bool match_codim)
415 array_type current_type;
416 gfc_array_spec *as;
417 int i;
419 as = gfc_get_array_spec ();
420 as->corank = 0;
421 as->rank = 0;
423 for (i = 0; i < GFC_MAX_DIMENSIONS; i++)
425 as->lower[i] = NULL;
426 as->upper[i] = NULL;
429 if (!match_dim)
430 goto coarray;
432 if (gfc_match_char ('(') != MATCH_YES)
434 if (!match_codim)
435 goto done;
436 goto coarray;
439 for (;;)
441 as->rank++;
442 current_type = match_array_element_spec (as);
444 if (as->rank == 1)
446 if (current_type == AS_UNKNOWN)
447 goto cleanup;
448 as->type = current_type;
450 else
451 switch (as->type)
452 { /* See how current spec meshes with the existing. */
453 case AS_UNKNOWN:
454 goto cleanup;
456 case AS_EXPLICIT:
457 if (current_type == AS_ASSUMED_SIZE)
459 as->type = AS_ASSUMED_SIZE;
460 break;
463 if (current_type == AS_EXPLICIT)
464 break;
466 gfc_error ("Bad array specification for an explicitly shaped "
467 "array at %C");
469 goto cleanup;
471 case AS_ASSUMED_SHAPE:
472 if ((current_type == AS_ASSUMED_SHAPE)
473 || (current_type == AS_DEFERRED))
474 break;
476 gfc_error ("Bad array specification for assumed shape "
477 "array at %C");
478 goto cleanup;
480 case AS_DEFERRED:
481 if (current_type == AS_DEFERRED)
482 break;
484 if (current_type == AS_ASSUMED_SHAPE)
486 as->type = AS_ASSUMED_SHAPE;
487 break;
490 gfc_error ("Bad specification for deferred shape array at %C");
491 goto cleanup;
493 case AS_ASSUMED_SIZE:
494 gfc_error ("Bad specification for assumed size array at %C");
495 goto cleanup;
498 if (gfc_match_char (')') == MATCH_YES)
499 break;
501 if (gfc_match_char (',') != MATCH_YES)
503 gfc_error ("Expected another dimension in array declaration at %C");
504 goto cleanup;
507 if (as->rank + as->corank >= GFC_MAX_DIMENSIONS)
509 gfc_error ("Array specification at %C has more than %d dimensions",
510 GFC_MAX_DIMENSIONS);
511 goto cleanup;
514 if (as->corank + as->rank >= 7
515 && gfc_notify_std (GFC_STD_F2008, "Fortran 2008: Array "
516 "specification at %C with more than 7 dimensions")
517 == FAILURE)
518 goto cleanup;
521 if (!match_codim)
522 goto done;
524 coarray:
525 if (gfc_match_char ('[') != MATCH_YES)
526 goto done;
528 if (gfc_notify_std (GFC_STD_F2008, "Fortran 2008: Coarray declaration at %C")
529 == FAILURE)
530 goto cleanup;
532 if (gfc_option.coarray == GFC_FCOARRAY_NONE)
534 gfc_fatal_error ("Coarrays disabled at %C, use -fcoarray= to enable");
535 goto cleanup;
538 for (;;)
540 as->corank++;
541 current_type = match_array_element_spec (as);
543 if (current_type == AS_UNKNOWN)
544 goto cleanup;
546 if (as->corank == 1)
547 as->cotype = current_type;
548 else
549 switch (as->cotype)
550 { /* See how current spec meshes with the existing. */
551 case AS_UNKNOWN:
552 goto cleanup;
554 case AS_EXPLICIT:
555 if (current_type == AS_ASSUMED_SIZE)
557 as->cotype = AS_ASSUMED_SIZE;
558 break;
561 if (current_type == AS_EXPLICIT)
562 break;
564 gfc_error ("Bad array specification for an explicitly "
565 "shaped array at %C");
567 goto cleanup;
569 case AS_ASSUMED_SHAPE:
570 if ((current_type == AS_ASSUMED_SHAPE)
571 || (current_type == AS_DEFERRED))
572 break;
574 gfc_error ("Bad array specification for assumed shape "
575 "array at %C");
576 goto cleanup;
578 case AS_DEFERRED:
579 if (current_type == AS_DEFERRED)
580 break;
582 if (current_type == AS_ASSUMED_SHAPE)
584 as->cotype = AS_ASSUMED_SHAPE;
585 break;
588 gfc_error ("Bad specification for deferred shape array at %C");
589 goto cleanup;
591 case AS_ASSUMED_SIZE:
592 gfc_error ("Bad specification for assumed size array at %C");
593 goto cleanup;
596 if (gfc_match_char (']') == MATCH_YES)
597 break;
599 if (gfc_match_char (',') != MATCH_YES)
601 gfc_error ("Expected another dimension in array declaration at %C");
602 goto cleanup;
605 if (as->corank >= GFC_MAX_DIMENSIONS)
607 gfc_error ("Array specification at %C has more than %d "
608 "dimensions", GFC_MAX_DIMENSIONS);
609 goto cleanup;
613 if (current_type == AS_EXPLICIT)
615 gfc_error ("Upper bound of last coarray dimension must be '*' at %C");
616 goto cleanup;
619 if (as->cotype == AS_ASSUMED_SIZE)
620 as->cotype = AS_EXPLICIT;
622 if (as->rank == 0)
623 as->type = as->cotype;
625 done:
626 if (as->rank == 0 && as->corank == 0)
628 *asp = NULL;
629 gfc_free_array_spec (as);
630 return MATCH_NO;
633 /* If a lower bounds of an assumed shape array is blank, put in one. */
634 if (as->type == AS_ASSUMED_SHAPE)
636 for (i = 0; i < as->rank + as->corank; i++)
638 if (as->lower[i] == NULL)
639 as->lower[i] = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
643 *asp = as;
645 return MATCH_YES;
647 cleanup:
648 /* Something went wrong. */
649 gfc_free_array_spec (as);
650 return MATCH_ERROR;
654 /* Given a symbol and an array specification, modify the symbol to
655 have that array specification. The error locus is needed in case
656 something goes wrong. On failure, the caller must free the spec. */
658 gfc_try
659 gfc_set_array_spec (gfc_symbol *sym, gfc_array_spec *as, locus *error_loc)
661 int i;
663 if (as == NULL)
664 return SUCCESS;
666 if (as->rank
667 && gfc_add_dimension (&sym->attr, sym->name, error_loc) == FAILURE)
668 return FAILURE;
670 if (as->corank
671 && gfc_add_codimension (&sym->attr, sym->name, error_loc) == FAILURE)
672 return FAILURE;
674 if (sym->as == NULL)
676 sym->as = as;
677 return SUCCESS;
680 if (as->corank)
682 /* The "sym" has no corank (checked via gfc_add_codimension). Thus
683 the codimension is simply added. */
684 gcc_assert (as->rank == 0 && sym->as->corank == 0);
686 sym->as->cotype = as->cotype;
687 sym->as->corank = as->corank;
688 for (i = 0; i < as->corank; i++)
690 sym->as->lower[sym->as->rank + i] = as->lower[i];
691 sym->as->upper[sym->as->rank + i] = as->upper[i];
694 else
696 /* The "sym" has no rank (checked via gfc_add_dimension). Thus
697 the dimension is added - but first the codimensions (if existing
698 need to be shifted to make space for the dimension. */
699 gcc_assert (as->corank == 0 && sym->as->rank == 0);
701 sym->as->rank = as->rank;
702 sym->as->type = as->type;
703 sym->as->cray_pointee = as->cray_pointee;
704 sym->as->cp_was_assumed = as->cp_was_assumed;
706 for (i = 0; i < sym->as->corank; i++)
708 sym->as->lower[as->rank + i] = sym->as->lower[i];
709 sym->as->upper[as->rank + i] = sym->as->upper[i];
711 for (i = 0; i < as->rank; i++)
713 sym->as->lower[i] = as->lower[i];
714 sym->as->upper[i] = as->upper[i];
718 gfc_free (as);
719 return SUCCESS;
723 /* Copy an array specification. */
725 gfc_array_spec *
726 gfc_copy_array_spec (gfc_array_spec *src)
728 gfc_array_spec *dest;
729 int i;
731 if (src == NULL)
732 return NULL;
734 dest = gfc_get_array_spec ();
736 *dest = *src;
738 for (i = 0; i < dest->rank + dest->corank; i++)
740 dest->lower[i] = gfc_copy_expr (dest->lower[i]);
741 dest->upper[i] = gfc_copy_expr (dest->upper[i]);
744 return dest;
748 /* Returns nonzero if the two expressions are equal. Only handles integer
749 constants. */
751 static int
752 compare_bounds (gfc_expr *bound1, gfc_expr *bound2)
754 if (bound1 == NULL || bound2 == NULL
755 || bound1->expr_type != EXPR_CONSTANT
756 || bound2->expr_type != EXPR_CONSTANT
757 || bound1->ts.type != BT_INTEGER
758 || bound2->ts.type != BT_INTEGER)
759 gfc_internal_error ("gfc_compare_array_spec(): Array spec clobbered");
761 if (mpz_cmp (bound1->value.integer, bound2->value.integer) == 0)
762 return 1;
763 else
764 return 0;
768 /* Compares two array specifications. They must be constant or deferred
769 shape. */
772 gfc_compare_array_spec (gfc_array_spec *as1, gfc_array_spec *as2)
774 int i;
776 if (as1 == NULL && as2 == NULL)
777 return 1;
779 if (as1 == NULL || as2 == NULL)
780 return 0;
782 if (as1->rank != as2->rank)
783 return 0;
785 if (as1->corank != as2->corank)
786 return 0;
788 if (as1->rank == 0)
789 return 1;
791 if (as1->type != as2->type)
792 return 0;
794 if (as1->type == AS_EXPLICIT)
795 for (i = 0; i < as1->rank + as1->corank; i++)
797 if (compare_bounds (as1->lower[i], as2->lower[i]) == 0)
798 return 0;
800 if (compare_bounds (as1->upper[i], as2->upper[i]) == 0)
801 return 0;
804 return 1;
808 /****************** Array constructor functions ******************/
811 /* Given an expression node that might be an array constructor and a
812 symbol, make sure that no iterators in this or child constructors
813 use the symbol as an implied-DO iterator. Returns nonzero if a
814 duplicate was found. */
816 static int
817 check_duplicate_iterator (gfc_constructor_base base, gfc_symbol *master)
819 gfc_constructor *c;
820 gfc_expr *e;
822 for (c = gfc_constructor_first (base); c; c = gfc_constructor_next (c))
824 e = c->expr;
826 if (e->expr_type == EXPR_ARRAY
827 && check_duplicate_iterator (e->value.constructor, master))
828 return 1;
830 if (c->iterator == NULL)
831 continue;
833 if (c->iterator->var->symtree->n.sym == master)
835 gfc_error ("DO-iterator '%s' at %L is inside iterator of the "
836 "same name", master->name, &c->where);
838 return 1;
842 return 0;
846 /* Forward declaration because these functions are mutually recursive. */
847 static match match_array_cons_element (gfc_constructor_base *);
849 /* Match a list of array elements. */
851 static match
852 match_array_list (gfc_constructor_base *result)
854 gfc_constructor_base head;
855 gfc_constructor *p;
856 gfc_iterator iter;
857 locus old_loc;
858 gfc_expr *e;
859 match m;
860 int n;
862 old_loc = gfc_current_locus;
864 if (gfc_match_char ('(') == MATCH_NO)
865 return MATCH_NO;
867 memset (&iter, '\0', sizeof (gfc_iterator));
868 head = NULL;
870 m = match_array_cons_element (&head);
871 if (m != MATCH_YES)
872 goto cleanup;
874 if (gfc_match_char (',') != MATCH_YES)
876 m = MATCH_NO;
877 goto cleanup;
880 for (n = 1;; n++)
882 m = gfc_match_iterator (&iter, 0);
883 if (m == MATCH_YES)
884 break;
885 if (m == MATCH_ERROR)
886 goto cleanup;
888 m = match_array_cons_element (&head);
889 if (m == MATCH_ERROR)
890 goto cleanup;
891 if (m == MATCH_NO)
893 if (n > 2)
894 goto syntax;
895 m = MATCH_NO;
896 goto cleanup; /* Could be a complex constant */
899 if (gfc_match_char (',') != MATCH_YES)
901 if (n > 2)
902 goto syntax;
903 m = MATCH_NO;
904 goto cleanup;
908 if (gfc_match_char (')') != MATCH_YES)
909 goto syntax;
911 if (check_duplicate_iterator (head, iter.var->symtree->n.sym))
913 m = MATCH_ERROR;
914 goto cleanup;
917 e = gfc_get_array_expr (BT_UNKNOWN, 0, &old_loc);
918 e->value.constructor = head;
920 p = gfc_constructor_append_expr (result, e, &gfc_current_locus);
921 p->iterator = gfc_get_iterator ();
922 *p->iterator = iter;
924 return MATCH_YES;
926 syntax:
927 gfc_error ("Syntax error in array constructor at %C");
928 m = MATCH_ERROR;
930 cleanup:
931 gfc_constructor_free (head);
932 gfc_free_iterator (&iter, 0);
933 gfc_current_locus = old_loc;
934 return m;
938 /* Match a single element of an array constructor, which can be a
939 single expression or a list of elements. */
941 static match
942 match_array_cons_element (gfc_constructor_base *result)
944 gfc_expr *expr;
945 match m;
947 m = match_array_list (result);
948 if (m != MATCH_NO)
949 return m;
951 m = gfc_match_expr (&expr);
952 if (m != MATCH_YES)
953 return m;
955 gfc_constructor_append_expr (result, expr, &gfc_current_locus);
956 return MATCH_YES;
960 /* Match an array constructor. */
962 match
963 gfc_match_array_constructor (gfc_expr **result)
965 gfc_constructor_base head, new_cons;
966 gfc_expr *expr;
967 gfc_typespec ts;
968 locus where;
969 match m;
970 const char *end_delim;
971 bool seen_ts;
973 if (gfc_match (" (/") == MATCH_NO)
975 if (gfc_match (" [") == MATCH_NO)
976 return MATCH_NO;
977 else
979 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: [...] "
980 "style array constructors at %C") == FAILURE)
981 return MATCH_ERROR;
982 end_delim = " ]";
985 else
986 end_delim = " /)";
988 where = gfc_current_locus;
989 head = new_cons = NULL;
990 seen_ts = false;
992 /* Try to match an optional "type-spec ::" */
993 if (gfc_match_decl_type_spec (&ts, 0) == MATCH_YES)
995 seen_ts = (gfc_match (" ::") == MATCH_YES);
997 if (seen_ts)
999 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: Array constructor "
1000 "including type specification at %C") == FAILURE)
1001 goto cleanup;
1005 if (! seen_ts)
1006 gfc_current_locus = where;
1008 if (gfc_match (end_delim) == MATCH_YES)
1010 if (seen_ts)
1011 goto done;
1012 else
1014 gfc_error ("Empty array constructor at %C is not allowed");
1015 goto cleanup;
1019 for (;;)
1021 m = match_array_cons_element (&head);
1022 if (m == MATCH_ERROR)
1023 goto cleanup;
1024 if (m == MATCH_NO)
1025 goto syntax;
1027 if (gfc_match_char (',') == MATCH_NO)
1028 break;
1031 if (gfc_match (end_delim) == MATCH_NO)
1032 goto syntax;
1034 done:
1035 /* Size must be calculated at resolution time. */
1036 if (seen_ts)
1038 expr = gfc_get_array_expr (ts.type, ts.kind, &where);
1039 expr->ts = ts;
1041 else
1042 expr = gfc_get_array_expr (BT_UNKNOWN, 0, &where);
1044 expr->value.constructor = head;
1045 if (expr->ts.u.cl)
1046 expr->ts.u.cl->length_from_typespec = seen_ts;
1048 *result = expr;
1049 return MATCH_YES;
1051 syntax:
1052 gfc_error ("Syntax error in array constructor at %C");
1054 cleanup:
1055 gfc_constructor_free (head);
1056 return MATCH_ERROR;
1061 /************** Check array constructors for correctness **************/
1063 /* Given an expression, compare it's type with the type of the current
1064 constructor. Returns nonzero if an error was issued. The
1065 cons_state variable keeps track of whether the type of the
1066 constructor being read or resolved is known to be good, bad or just
1067 starting out. */
1069 static gfc_typespec constructor_ts;
1070 static enum
1071 { CONS_START, CONS_GOOD, CONS_BAD }
1072 cons_state;
1074 static int
1075 check_element_type (gfc_expr *expr, bool convert)
1077 if (cons_state == CONS_BAD)
1078 return 0; /* Suppress further errors */
1080 if (cons_state == CONS_START)
1082 if (expr->ts.type == BT_UNKNOWN)
1083 cons_state = CONS_BAD;
1084 else
1086 cons_state = CONS_GOOD;
1087 constructor_ts = expr->ts;
1090 return 0;
1093 if (gfc_compare_types (&constructor_ts, &expr->ts))
1094 return 0;
1096 if (convert)
1097 return gfc_convert_type (expr, &constructor_ts, 1) == SUCCESS ? 0 : 1;
1099 gfc_error ("Element in %s array constructor at %L is %s",
1100 gfc_typename (&constructor_ts), &expr->where,
1101 gfc_typename (&expr->ts));
1103 cons_state = CONS_BAD;
1104 return 1;
1108 /* Recursive work function for gfc_check_constructor_type(). */
1110 static gfc_try
1111 check_constructor_type (gfc_constructor_base base, bool convert)
1113 gfc_constructor *c;
1114 gfc_expr *e;
1116 for (c = gfc_constructor_first (base); c; c = gfc_constructor_next (c))
1118 e = c->expr;
1120 if (e->expr_type == EXPR_ARRAY)
1122 if (check_constructor_type (e->value.constructor, convert) == FAILURE)
1123 return FAILURE;
1125 continue;
1128 if (check_element_type (e, convert))
1129 return FAILURE;
1132 return SUCCESS;
1136 /* Check that all elements of an array constructor are the same type.
1137 On FAILURE, an error has been generated. */
1139 gfc_try
1140 gfc_check_constructor_type (gfc_expr *e)
1142 gfc_try t;
1144 if (e->ts.type != BT_UNKNOWN)
1146 cons_state = CONS_GOOD;
1147 constructor_ts = e->ts;
1149 else
1151 cons_state = CONS_START;
1152 gfc_clear_ts (&constructor_ts);
1155 /* If e->ts.type != BT_UNKNOWN, the array constructor included a
1156 typespec, and we will now convert the values on the fly. */
1157 t = check_constructor_type (e->value.constructor, e->ts.type != BT_UNKNOWN);
1158 if (t == SUCCESS && e->ts.type == BT_UNKNOWN)
1159 e->ts = constructor_ts;
1161 return t;
1166 typedef struct cons_stack
1168 gfc_iterator *iterator;
1169 struct cons_stack *previous;
1171 cons_stack;
1173 static cons_stack *base;
1175 static gfc_try check_constructor (gfc_constructor_base, gfc_try (*) (gfc_expr *));
1177 /* Check an EXPR_VARIABLE expression in a constructor to make sure
1178 that that variable is an iteration variables. */
1180 gfc_try
1181 gfc_check_iter_variable (gfc_expr *expr)
1183 gfc_symbol *sym;
1184 cons_stack *c;
1186 sym = expr->symtree->n.sym;
1188 for (c = base; c; c = c->previous)
1189 if (sym == c->iterator->var->symtree->n.sym)
1190 return SUCCESS;
1192 return FAILURE;
1196 /* Recursive work function for gfc_check_constructor(). This amounts
1197 to calling the check function for each expression in the
1198 constructor, giving variables with the names of iterators a pass. */
1200 static gfc_try
1201 check_constructor (gfc_constructor_base ctor, gfc_try (*check_function) (gfc_expr *))
1203 cons_stack element;
1204 gfc_expr *e;
1205 gfc_try t;
1206 gfc_constructor *c;
1208 for (c = gfc_constructor_first (ctor); c; c = gfc_constructor_next (c))
1210 e = c->expr;
1212 if (e->expr_type != EXPR_ARRAY)
1214 if ((*check_function) (e) == FAILURE)
1215 return FAILURE;
1216 continue;
1219 element.previous = base;
1220 element.iterator = c->iterator;
1222 base = &element;
1223 t = check_constructor (e->value.constructor, check_function);
1224 base = element.previous;
1226 if (t == FAILURE)
1227 return FAILURE;
1230 /* Nothing went wrong, so all OK. */
1231 return SUCCESS;
1235 /* Checks a constructor to see if it is a particular kind of
1236 expression -- specification, restricted, or initialization as
1237 determined by the check_function. */
1239 gfc_try
1240 gfc_check_constructor (gfc_expr *expr, gfc_try (*check_function) (gfc_expr *))
1242 cons_stack *base_save;
1243 gfc_try t;
1245 base_save = base;
1246 base = NULL;
1248 t = check_constructor (expr->value.constructor, check_function);
1249 base = base_save;
1251 return t;
1256 /**************** Simplification of array constructors ****************/
1258 iterator_stack *iter_stack;
1260 typedef struct
1262 gfc_constructor_base base;
1263 int extract_count, extract_n;
1264 gfc_expr *extracted;
1265 mpz_t *count;
1267 mpz_t *offset;
1268 gfc_component *component;
1269 mpz_t *repeat;
1271 gfc_try (*expand_work_function) (gfc_expr *);
1273 expand_info;
1275 static expand_info current_expand;
1277 static gfc_try expand_constructor (gfc_constructor_base);
1280 /* Work function that counts the number of elements present in a
1281 constructor. */
1283 static gfc_try
1284 count_elements (gfc_expr *e)
1286 mpz_t result;
1288 if (e->rank == 0)
1289 mpz_add_ui (*current_expand.count, *current_expand.count, 1);
1290 else
1292 if (gfc_array_size (e, &result) == FAILURE)
1294 gfc_free_expr (e);
1295 return FAILURE;
1298 mpz_add (*current_expand.count, *current_expand.count, result);
1299 mpz_clear (result);
1302 gfc_free_expr (e);
1303 return SUCCESS;
1307 /* Work function that extracts a particular element from an array
1308 constructor, freeing the rest. */
1310 static gfc_try
1311 extract_element (gfc_expr *e)
1313 if (e->rank != 0)
1314 { /* Something unextractable */
1315 gfc_free_expr (e);
1316 return FAILURE;
1319 if (current_expand.extract_count == current_expand.extract_n)
1320 current_expand.extracted = e;
1321 else
1322 gfc_free_expr (e);
1324 current_expand.extract_count++;
1326 return SUCCESS;
1330 /* Work function that constructs a new constructor out of the old one,
1331 stringing new elements together. */
1333 static gfc_try
1334 expand (gfc_expr *e)
1336 gfc_constructor *c = gfc_constructor_append_expr (&current_expand.base,
1337 e, &e->where);
1339 c->n.component = current_expand.component;
1340 return SUCCESS;
1344 /* Given an initialization expression that is a variable reference,
1345 substitute the current value of the iteration variable. */
1347 void
1348 gfc_simplify_iterator_var (gfc_expr *e)
1350 iterator_stack *p;
1352 for (p = iter_stack; p; p = p->prev)
1353 if (e->symtree == p->variable)
1354 break;
1356 if (p == NULL)
1357 return; /* Variable not found */
1359 gfc_replace_expr (e, gfc_get_int_expr (gfc_default_integer_kind, NULL, 0));
1361 mpz_set (e->value.integer, p->value);
1363 return;
1367 /* Expand an expression with that is inside of a constructor,
1368 recursing into other constructors if present. */
1370 static gfc_try
1371 expand_expr (gfc_expr *e)
1373 if (e->expr_type == EXPR_ARRAY)
1374 return expand_constructor (e->value.constructor);
1376 e = gfc_copy_expr (e);
1378 if (gfc_simplify_expr (e, 1) == FAILURE)
1380 gfc_free_expr (e);
1381 return FAILURE;
1384 return current_expand.expand_work_function (e);
1388 static gfc_try
1389 expand_iterator (gfc_constructor *c)
1391 gfc_expr *start, *end, *step;
1392 iterator_stack frame;
1393 mpz_t trip;
1394 gfc_try t;
1396 end = step = NULL;
1398 t = FAILURE;
1400 mpz_init (trip);
1401 mpz_init (frame.value);
1402 frame.prev = NULL;
1404 start = gfc_copy_expr (c->iterator->start);
1405 if (gfc_simplify_expr (start, 1) == FAILURE)
1406 goto cleanup;
1408 if (start->expr_type != EXPR_CONSTANT || start->ts.type != BT_INTEGER)
1409 goto cleanup;
1411 end = gfc_copy_expr (c->iterator->end);
1412 if (gfc_simplify_expr (end, 1) == FAILURE)
1413 goto cleanup;
1415 if (end->expr_type != EXPR_CONSTANT || end->ts.type != BT_INTEGER)
1416 goto cleanup;
1418 step = gfc_copy_expr (c->iterator->step);
1419 if (gfc_simplify_expr (step, 1) == FAILURE)
1420 goto cleanup;
1422 if (step->expr_type != EXPR_CONSTANT || step->ts.type != BT_INTEGER)
1423 goto cleanup;
1425 if (mpz_sgn (step->value.integer) == 0)
1427 gfc_error ("Iterator step at %L cannot be zero", &step->where);
1428 goto cleanup;
1431 /* Calculate the trip count of the loop. */
1432 mpz_sub (trip, end->value.integer, start->value.integer);
1433 mpz_add (trip, trip, step->value.integer);
1434 mpz_tdiv_q (trip, trip, step->value.integer);
1436 mpz_set (frame.value, start->value.integer);
1438 frame.prev = iter_stack;
1439 frame.variable = c->iterator->var->symtree;
1440 iter_stack = &frame;
1442 while (mpz_sgn (trip) > 0)
1444 if (expand_expr (c->expr) == FAILURE)
1445 goto cleanup;
1447 mpz_add (frame.value, frame.value, step->value.integer);
1448 mpz_sub_ui (trip, trip, 1);
1451 t = SUCCESS;
1453 cleanup:
1454 gfc_free_expr (start);
1455 gfc_free_expr (end);
1456 gfc_free_expr (step);
1458 mpz_clear (trip);
1459 mpz_clear (frame.value);
1461 iter_stack = frame.prev;
1463 return t;
1467 /* Expand a constructor into constant constructors without any
1468 iterators, calling the work function for each of the expanded
1469 expressions. The work function needs to either save or free the
1470 passed expression. */
1472 static gfc_try
1473 expand_constructor (gfc_constructor_base base)
1475 gfc_constructor *c;
1476 gfc_expr *e;
1478 for (c = gfc_constructor_first (base); c; c = gfc_constructor_next(c))
1480 if (c->iterator != NULL)
1482 if (expand_iterator (c) == FAILURE)
1483 return FAILURE;
1484 continue;
1487 e = c->expr;
1489 if (e->expr_type == EXPR_ARRAY)
1491 if (expand_constructor (e->value.constructor) == FAILURE)
1492 return FAILURE;
1494 continue;
1497 e = gfc_copy_expr (e);
1498 if (gfc_simplify_expr (e, 1) == FAILURE)
1500 gfc_free_expr (e);
1501 return FAILURE;
1503 current_expand.offset = &c->offset;
1504 current_expand.repeat = &c->repeat;
1505 current_expand.component = c->n.component;
1506 if (current_expand.expand_work_function (e) == FAILURE)
1507 return FAILURE;
1509 return SUCCESS;
1513 /* Given an array expression and an element number (starting at zero),
1514 return a pointer to the array element. NULL is returned if the
1515 size of the array has been exceeded. The expression node returned
1516 remains a part of the array and should not be freed. Access is not
1517 efficient at all, but this is another place where things do not
1518 have to be particularly fast. */
1520 static gfc_expr *
1521 gfc_get_array_element (gfc_expr *array, int element)
1523 expand_info expand_save;
1524 gfc_expr *e;
1525 gfc_try rc;
1527 expand_save = current_expand;
1528 current_expand.extract_n = element;
1529 current_expand.expand_work_function = extract_element;
1530 current_expand.extracted = NULL;
1531 current_expand.extract_count = 0;
1533 iter_stack = NULL;
1535 rc = expand_constructor (array->value.constructor);
1536 e = current_expand.extracted;
1537 current_expand = expand_save;
1539 if (rc == FAILURE)
1540 return NULL;
1542 return e;
1546 /* Top level subroutine for expanding constructors. We only expand
1547 constructor if they are small enough. */
1549 gfc_try
1550 gfc_expand_constructor (gfc_expr *e)
1552 expand_info expand_save;
1553 gfc_expr *f;
1554 gfc_try rc;
1556 /* If we can successfully get an array element at the max array size then
1557 the array is too big to expand, so we just return. */
1558 f = gfc_get_array_element (e, gfc_option.flag_max_array_constructor);
1559 if (f != NULL)
1561 gfc_free_expr (f);
1562 return SUCCESS;
1565 /* We now know the array is not too big so go ahead and try to expand it. */
1566 expand_save = current_expand;
1567 current_expand.base = NULL;
1569 iter_stack = NULL;
1571 current_expand.expand_work_function = expand;
1573 if (expand_constructor (e->value.constructor) == FAILURE)
1575 gfc_constructor_free (current_expand.base);
1576 rc = FAILURE;
1577 goto done;
1580 gfc_constructor_free (e->value.constructor);
1581 e->value.constructor = current_expand.base;
1583 rc = SUCCESS;
1585 done:
1586 current_expand = expand_save;
1588 return rc;
1592 /* Work function for checking that an element of a constructor is a
1593 constant, after removal of any iteration variables. We return
1594 FAILURE if not so. */
1596 static gfc_try
1597 is_constant_element (gfc_expr *e)
1599 int rv;
1601 rv = gfc_is_constant_expr (e);
1602 gfc_free_expr (e);
1604 return rv ? SUCCESS : FAILURE;
1608 /* Given an array constructor, determine if the constructor is
1609 constant or not by expanding it and making sure that all elements
1610 are constants. This is a bit of a hack since something like (/ (i,
1611 i=1,100000000) /) will take a while as* opposed to a more clever
1612 function that traverses the expression tree. FIXME. */
1615 gfc_constant_ac (gfc_expr *e)
1617 expand_info expand_save;
1618 gfc_try rc;
1620 iter_stack = NULL;
1621 expand_save = current_expand;
1622 current_expand.expand_work_function = is_constant_element;
1624 rc = expand_constructor (e->value.constructor);
1626 current_expand = expand_save;
1627 if (rc == FAILURE)
1628 return 0;
1630 return 1;
1634 /* Returns nonzero if an array constructor has been completely
1635 expanded (no iterators) and zero if iterators are present. */
1638 gfc_expanded_ac (gfc_expr *e)
1640 gfc_constructor *c;
1642 if (e->expr_type == EXPR_ARRAY)
1643 for (c = gfc_constructor_first (e->value.constructor);
1644 c; c = gfc_constructor_next (c))
1645 if (c->iterator != NULL || !gfc_expanded_ac (c->expr))
1646 return 0;
1648 return 1;
1652 /*************** Type resolution of array constructors ***************/
1654 /* Recursive array list resolution function. All of the elements must
1655 be of the same type. */
1657 static gfc_try
1658 resolve_array_list (gfc_constructor_base base)
1660 gfc_try t;
1661 gfc_constructor *c;
1663 t = SUCCESS;
1665 for (c = gfc_constructor_first (base); c; c = gfc_constructor_next (c))
1667 if (c->iterator != NULL
1668 && gfc_resolve_iterator (c->iterator, false) == FAILURE)
1669 t = FAILURE;
1671 if (gfc_resolve_expr (c->expr) == FAILURE)
1672 t = FAILURE;
1675 return t;
1678 /* Resolve character array constructor. If it has a specified constant character
1679 length, pad/truncate the elements here; if the length is not specified and
1680 all elements are of compile-time known length, emit an error as this is
1681 invalid. */
1683 gfc_try
1684 gfc_resolve_character_array_constructor (gfc_expr *expr)
1686 gfc_constructor *p;
1687 int found_length;
1689 gcc_assert (expr->expr_type == EXPR_ARRAY);
1690 gcc_assert (expr->ts.type == BT_CHARACTER);
1692 if (expr->ts.u.cl == NULL)
1694 for (p = gfc_constructor_first (expr->value.constructor);
1695 p; p = gfc_constructor_next (p))
1696 if (p->expr->ts.u.cl != NULL)
1698 /* Ensure that if there is a char_len around that it is
1699 used; otherwise the middle-end confuses them! */
1700 expr->ts.u.cl = p->expr->ts.u.cl;
1701 goto got_charlen;
1704 expr->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
1707 got_charlen:
1709 found_length = -1;
1711 if (expr->ts.u.cl->length == NULL)
1713 /* Check that all constant string elements have the same length until
1714 we reach the end or find a variable-length one. */
1716 for (p = gfc_constructor_first (expr->value.constructor);
1717 p; p = gfc_constructor_next (p))
1719 int current_length = -1;
1720 gfc_ref *ref;
1721 for (ref = p->expr->ref; ref; ref = ref->next)
1722 if (ref->type == REF_SUBSTRING
1723 && ref->u.ss.start->expr_type == EXPR_CONSTANT
1724 && ref->u.ss.end->expr_type == EXPR_CONSTANT)
1725 break;
1727 if (p->expr->expr_type == EXPR_CONSTANT)
1728 current_length = p->expr->value.character.length;
1729 else if (ref)
1731 long j;
1732 j = mpz_get_ui (ref->u.ss.end->value.integer)
1733 - mpz_get_ui (ref->u.ss.start->value.integer) + 1;
1734 current_length = (int) j;
1736 else if (p->expr->ts.u.cl && p->expr->ts.u.cl->length
1737 && p->expr->ts.u.cl->length->expr_type == EXPR_CONSTANT)
1739 long j;
1740 j = mpz_get_si (p->expr->ts.u.cl->length->value.integer);
1741 current_length = (int) j;
1743 else
1744 return SUCCESS;
1746 gcc_assert (current_length != -1);
1748 if (found_length == -1)
1749 found_length = current_length;
1750 else if (found_length != current_length)
1752 gfc_error ("Different CHARACTER lengths (%d/%d) in array"
1753 " constructor at %L", found_length, current_length,
1754 &p->expr->where);
1755 return FAILURE;
1758 gcc_assert (found_length == current_length);
1761 gcc_assert (found_length != -1);
1763 /* Update the character length of the array constructor. */
1764 expr->ts.u.cl->length = gfc_get_int_expr (gfc_default_integer_kind,
1765 NULL, found_length);
1767 else
1769 /* We've got a character length specified. It should be an integer,
1770 otherwise an error is signalled elsewhere. */
1771 gcc_assert (expr->ts.u.cl->length);
1773 /* If we've got a constant character length, pad according to this.
1774 gfc_extract_int does check for BT_INTEGER and EXPR_CONSTANT and sets
1775 max_length only if they pass. */
1776 gfc_extract_int (expr->ts.u.cl->length, &found_length);
1778 /* Now pad/truncate the elements accordingly to the specified character
1779 length. This is ok inside this conditional, as in the case above
1780 (without typespec) all elements are verified to have the same length
1781 anyway. */
1782 if (found_length != -1)
1783 for (p = gfc_constructor_first (expr->value.constructor);
1784 p; p = gfc_constructor_next (p))
1785 if (p->expr->expr_type == EXPR_CONSTANT)
1787 gfc_expr *cl = NULL;
1788 int current_length = -1;
1789 bool has_ts;
1791 if (p->expr->ts.u.cl && p->expr->ts.u.cl->length)
1793 cl = p->expr->ts.u.cl->length;
1794 gfc_extract_int (cl, &current_length);
1797 /* If gfc_extract_int above set current_length, we implicitly
1798 know the type is BT_INTEGER and it's EXPR_CONSTANT. */
1800 has_ts = (expr->ts.u.cl && expr->ts.u.cl->length_from_typespec);
1802 if (! cl
1803 || (current_length != -1 && current_length < found_length))
1804 gfc_set_constant_character_len (found_length, p->expr,
1805 has_ts ? -1 : found_length);
1809 return SUCCESS;
1813 /* Resolve all of the expressions in an array list. */
1815 gfc_try
1816 gfc_resolve_array_constructor (gfc_expr *expr)
1818 gfc_try t;
1820 t = resolve_array_list (expr->value.constructor);
1821 if (t == SUCCESS)
1822 t = gfc_check_constructor_type (expr);
1824 /* gfc_resolve_character_array_constructor is called in gfc_resolve_expr after
1825 the call to this function, so we don't need to call it here; if it was
1826 called twice, an error message there would be duplicated. */
1828 return t;
1832 /* Copy an iterator structure. */
1834 gfc_iterator *
1835 gfc_copy_iterator (gfc_iterator *src)
1837 gfc_iterator *dest;
1839 if (src == NULL)
1840 return NULL;
1842 dest = gfc_get_iterator ();
1844 dest->var = gfc_copy_expr (src->var);
1845 dest->start = gfc_copy_expr (src->start);
1846 dest->end = gfc_copy_expr (src->end);
1847 dest->step = gfc_copy_expr (src->step);
1849 return dest;
1853 /********* Subroutines for determining the size of an array *********/
1855 /* These are needed just to accommodate RESHAPE(). There are no
1856 diagnostics here, we just return a negative number if something
1857 goes wrong. */
1860 /* Get the size of single dimension of an array specification. The
1861 array is guaranteed to be one dimensional. */
1863 gfc_try
1864 spec_dimen_size (gfc_array_spec *as, int dimen, mpz_t *result)
1866 if (as == NULL)
1867 return FAILURE;
1869 if (dimen < 0 || dimen > as->rank - 1)
1870 gfc_internal_error ("spec_dimen_size(): Bad dimension");
1872 if (as->type != AS_EXPLICIT
1873 || as->lower[dimen]->expr_type != EXPR_CONSTANT
1874 || as->upper[dimen]->expr_type != EXPR_CONSTANT
1875 || as->lower[dimen]->ts.type != BT_INTEGER
1876 || as->upper[dimen]->ts.type != BT_INTEGER)
1877 return FAILURE;
1879 mpz_init (*result);
1881 mpz_sub (*result, as->upper[dimen]->value.integer,
1882 as->lower[dimen]->value.integer);
1884 mpz_add_ui (*result, *result, 1);
1886 return SUCCESS;
1890 gfc_try
1891 spec_size (gfc_array_spec *as, mpz_t *result)
1893 mpz_t size;
1894 int d;
1896 mpz_init_set_ui (*result, 1);
1898 for (d = 0; d < as->rank; d++)
1900 if (spec_dimen_size (as, d, &size) == FAILURE)
1902 mpz_clear (*result);
1903 return FAILURE;
1906 mpz_mul (*result, *result, size);
1907 mpz_clear (size);
1910 return SUCCESS;
1914 /* Get the number of elements in an array section. */
1916 gfc_try
1917 gfc_ref_dimen_size (gfc_array_ref *ar, int dimen, mpz_t *result)
1919 mpz_t upper, lower, stride;
1920 gfc_try t;
1922 if (dimen < 0 || ar == NULL || dimen > ar->dimen - 1)
1923 gfc_internal_error ("gfc_ref_dimen_size(): Bad dimension");
1925 switch (ar->dimen_type[dimen])
1927 case DIMEN_ELEMENT:
1928 mpz_init (*result);
1929 mpz_set_ui (*result, 1);
1930 t = SUCCESS;
1931 break;
1933 case DIMEN_VECTOR:
1934 t = gfc_array_size (ar->start[dimen], result); /* Recurse! */
1935 break;
1937 case DIMEN_RANGE:
1938 mpz_init (upper);
1939 mpz_init (lower);
1940 mpz_init (stride);
1941 t = FAILURE;
1943 if (ar->start[dimen] == NULL)
1945 if (ar->as->lower[dimen] == NULL
1946 || ar->as->lower[dimen]->expr_type != EXPR_CONSTANT)
1947 goto cleanup;
1948 mpz_set (lower, ar->as->lower[dimen]->value.integer);
1950 else
1952 if (ar->start[dimen]->expr_type != EXPR_CONSTANT)
1953 goto cleanup;
1954 mpz_set (lower, ar->start[dimen]->value.integer);
1957 if (ar->end[dimen] == NULL)
1959 if (ar->as->upper[dimen] == NULL
1960 || ar->as->upper[dimen]->expr_type != EXPR_CONSTANT)
1961 goto cleanup;
1962 mpz_set (upper, ar->as->upper[dimen]->value.integer);
1964 else
1966 if (ar->end[dimen]->expr_type != EXPR_CONSTANT)
1967 goto cleanup;
1968 mpz_set (upper, ar->end[dimen]->value.integer);
1971 if (ar->stride[dimen] == NULL)
1972 mpz_set_ui (stride, 1);
1973 else
1975 if (ar->stride[dimen]->expr_type != EXPR_CONSTANT)
1976 goto cleanup;
1977 mpz_set (stride, ar->stride[dimen]->value.integer);
1980 mpz_init (*result);
1981 mpz_sub (*result, upper, lower);
1982 mpz_add (*result, *result, stride);
1983 mpz_div (*result, *result, stride);
1985 /* Zero stride caught earlier. */
1986 if (mpz_cmp_ui (*result, 0) < 0)
1987 mpz_set_ui (*result, 0);
1988 t = SUCCESS;
1990 cleanup:
1991 mpz_clear (upper);
1992 mpz_clear (lower);
1993 mpz_clear (stride);
1994 return t;
1996 default:
1997 gfc_internal_error ("gfc_ref_dimen_size(): Bad dimen_type");
2000 return t;
2004 static gfc_try
2005 ref_size (gfc_array_ref *ar, mpz_t *result)
2007 mpz_t size;
2008 int d;
2010 mpz_init_set_ui (*result, 1);
2012 for (d = 0; d < ar->dimen; d++)
2014 if (gfc_ref_dimen_size (ar, d, &size) == FAILURE)
2016 mpz_clear (*result);
2017 return FAILURE;
2020 mpz_mul (*result, *result, size);
2021 mpz_clear (size);
2024 return SUCCESS;
2028 /* Given an array expression and a dimension, figure out how many
2029 elements it has along that dimension. Returns SUCCESS if we were
2030 able to return a result in the 'result' variable, FAILURE
2031 otherwise. */
2033 gfc_try
2034 gfc_array_dimen_size (gfc_expr *array, int dimen, mpz_t *result)
2036 gfc_ref *ref;
2037 int i;
2039 if (dimen < 0 || array == NULL || dimen > array->rank - 1)
2040 gfc_internal_error ("gfc_array_dimen_size(): Bad dimension");
2042 switch (array->expr_type)
2044 case EXPR_VARIABLE:
2045 case EXPR_FUNCTION:
2046 for (ref = array->ref; ref; ref = ref->next)
2048 if (ref->type != REF_ARRAY)
2049 continue;
2051 if (ref->u.ar.type == AR_FULL)
2052 return spec_dimen_size (ref->u.ar.as, dimen, result);
2054 if (ref->u.ar.type == AR_SECTION)
2056 for (i = 0; dimen >= 0; i++)
2057 if (ref->u.ar.dimen_type[i] != DIMEN_ELEMENT)
2058 dimen--;
2060 return gfc_ref_dimen_size (&ref->u.ar, i - 1, result);
2064 if (array->shape && array->shape[dimen])
2066 mpz_init_set (*result, array->shape[dimen]);
2067 return SUCCESS;
2070 if (array->symtree->n.sym->attr.generic
2071 && array->value.function.esym != NULL)
2073 if (spec_dimen_size (array->value.function.esym->as, dimen, result)
2074 == FAILURE)
2075 return FAILURE;
2077 else if (spec_dimen_size (array->symtree->n.sym->as, dimen, result)
2078 == FAILURE)
2079 return FAILURE;
2081 break;
2083 case EXPR_ARRAY:
2084 if (array->shape == NULL) {
2085 /* Expressions with rank > 1 should have "shape" properly set */
2086 if ( array->rank != 1 )
2087 gfc_internal_error ("gfc_array_dimen_size(): Bad EXPR_ARRAY expr");
2088 return gfc_array_size(array, result);
2091 /* Fall through */
2092 default:
2093 if (array->shape == NULL)
2094 return FAILURE;
2096 mpz_init_set (*result, array->shape[dimen]);
2098 break;
2101 return SUCCESS;
2105 /* Given an array expression, figure out how many elements are in the
2106 array. Returns SUCCESS if this is possible, and sets the 'result'
2107 variable. Otherwise returns FAILURE. */
2109 gfc_try
2110 gfc_array_size (gfc_expr *array, mpz_t *result)
2112 expand_info expand_save;
2113 gfc_ref *ref;
2114 int i;
2115 gfc_try t;
2117 switch (array->expr_type)
2119 case EXPR_ARRAY:
2120 gfc_push_suppress_errors ();
2122 expand_save = current_expand;
2124 current_expand.count = result;
2125 mpz_init_set_ui (*result, 0);
2127 current_expand.expand_work_function = count_elements;
2128 iter_stack = NULL;
2130 t = expand_constructor (array->value.constructor);
2132 gfc_pop_suppress_errors ();
2134 if (t == FAILURE)
2135 mpz_clear (*result);
2136 current_expand = expand_save;
2137 return t;
2139 case EXPR_VARIABLE:
2140 for (ref = array->ref; ref; ref = ref->next)
2142 if (ref->type != REF_ARRAY)
2143 continue;
2145 if (ref->u.ar.type == AR_FULL)
2146 return spec_size (ref->u.ar.as, result);
2148 if (ref->u.ar.type == AR_SECTION)
2149 return ref_size (&ref->u.ar, result);
2152 return spec_size (array->symtree->n.sym->as, result);
2155 default:
2156 if (array->rank == 0 || array->shape == NULL)
2157 return FAILURE;
2159 mpz_init_set_ui (*result, 1);
2161 for (i = 0; i < array->rank; i++)
2162 mpz_mul (*result, *result, array->shape[i]);
2164 break;
2167 return SUCCESS;
2171 /* Given an array reference, return the shape of the reference in an
2172 array of mpz_t integers. */
2174 gfc_try
2175 gfc_array_ref_shape (gfc_array_ref *ar, mpz_t *shape)
2177 int d;
2178 int i;
2180 d = 0;
2182 switch (ar->type)
2184 case AR_FULL:
2185 for (; d < ar->as->rank; d++)
2186 if (spec_dimen_size (ar->as, d, &shape[d]) == FAILURE)
2187 goto cleanup;
2189 return SUCCESS;
2191 case AR_SECTION:
2192 for (i = 0; i < ar->dimen; i++)
2194 if (ar->dimen_type[i] != DIMEN_ELEMENT)
2196 if (gfc_ref_dimen_size (ar, i, &shape[d]) == FAILURE)
2197 goto cleanup;
2198 d++;
2202 return SUCCESS;
2204 default:
2205 break;
2208 cleanup:
2209 for (d--; d >= 0; d--)
2210 mpz_clear (shape[d]);
2212 return FAILURE;
2216 /* Given an array expression, find the array reference structure that
2217 characterizes the reference. */
2219 gfc_array_ref *
2220 gfc_find_array_ref (gfc_expr *e)
2222 gfc_ref *ref;
2224 for (ref = e->ref; ref; ref = ref->next)
2225 if (ref->type == REF_ARRAY
2226 && (ref->u.ar.type == AR_FULL || ref->u.ar.type == AR_SECTION
2227 || (ref->u.ar.type == AR_ELEMENT && ref->u.ar.dimen == 0)))
2228 break;
2230 if (ref == NULL)
2231 gfc_internal_error ("gfc_find_array_ref(): No ref found");
2233 return &ref->u.ar;
2237 /* Find out if an array shape is known at compile time. */
2240 gfc_is_compile_time_shape (gfc_array_spec *as)
2242 int i;
2244 if (as->type != AS_EXPLICIT)
2245 return 0;
2247 for (i = 0; i < as->rank; i++)
2248 if (!gfc_is_constant_expr (as->lower[i])
2249 || !gfc_is_constant_expr (as->upper[i]))
2250 return 0;
2252 return 1;