2 Copyright (C) 2000-2015 Free Software Foundation, Inc.
3 Contributed by Andy Vaught
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
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
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/>. */
23 #include "coretypes.h"
27 #include "constructor.h"
29 /**************** Array reference matching subroutines *****************/
31 /* Copy an array reference structure. */
34 gfc_copy_array_ref (gfc_array_ref
*src
)
42 dest
= gfc_get_array_ref ();
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
]);
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
64 match_subscript (gfc_array_ref
*ar
, int init
, bool match_star
)
66 match m
= MATCH_ERROR
;
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
80 ar
->dimen_type
[i
] = DIMEN_UNKNOWN
;
82 if (gfc_match_char (':') == MATCH_YES
)
85 /* Get start element. */
86 if (match_star
&& (m
= gfc_match_char ('*')) == MATCH_YES
)
90 m
= gfc_match_init_expr (&ar
->start
[i
]);
92 m
= gfc_match_expr (&ar
->start
[i
]);
95 gfc_error ("Expected array subscript at %C");
99 if (gfc_match_char (':') == MATCH_NO
)
104 gfc_error ("Unexpected %<*%> in coarray subscript at %C");
108 /* Get an optional end element. Because we've seen the colon, we
109 definitely have a range along this dimension. */
111 ar
->dimen_type
[i
] = DIMEN_RANGE
;
113 if (match_star
&& (m
= gfc_match_char ('*')) == MATCH_YES
)
116 m
= gfc_match_init_expr (&ar
->end
[i
]);
118 m
= gfc_match_expr (&ar
->end
[i
]);
120 if (m
== MATCH_ERROR
)
123 /* See if we have an optional stride. */
124 if (gfc_match_char (':') == MATCH_YES
)
128 gfc_error ("Strides not allowed in coarray subscript at %C");
132 m
= init
? gfc_match_init_expr (&ar
->stride
[i
])
133 : gfc_match_expr (&ar
->stride
[i
]);
136 gfc_error ("Expected array subscript stride at %C");
143 ar
->dimen_type
[i
] = DIMEN_STAR
;
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. */
154 gfc_match_array_ref (gfc_array_ref
*ar
, gfc_array_spec
*as
, int init
,
158 bool matched_bracket
= false;
160 memset (ar
, '\0', sizeof (*ar
));
162 ar
->where
= gfc_current_locus
;
164 ar
->type
= AR_UNKNOWN
;
166 if (gfc_match_char ('[') == MATCH_YES
)
168 matched_bracket
= true;
172 if (gfc_match_char ('(') != 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
)
185 if (gfc_match_char (')') == MATCH_YES
)
191 if (gfc_match_char (',') != MATCH_YES
)
193 gfc_error ("Invalid form of array reference at %C");
198 gfc_error ("Array reference at %C cannot have more than %d dimensions",
203 if (!matched_bracket
&& gfc_match_char ('[') != MATCH_YES
)
211 if (flag_coarray
== GFC_FCOARRAY_NONE
)
213 gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to enable");
219 gfc_error ("Unexpected coarray designator at %C");
223 for (ar
->codimen
= 0; ar
->codimen
+ ar
->dimen
< GFC_MAX_DIMENSIONS
; ar
->codimen
++)
225 m
= match_subscript (ar
, init
, true);
226 if (m
== MATCH_ERROR
)
229 if (gfc_match_char (']') == MATCH_YES
)
232 if (ar
->codimen
< corank
)
234 gfc_error ("Too few codimensions at %C, expected %d not %d",
235 corank
, ar
->codimen
);
238 if (ar
->codimen
> corank
)
240 gfc_error ("Too many codimensions at %C, expected %d not %d",
241 corank
, ar
->codimen
);
247 if (gfc_match_char (',') != MATCH_YES
)
249 if (gfc_match_char ('*') == MATCH_YES
)
250 gfc_error ("Unexpected %<*%> for codimension %d of %d at %C",
251 ar
->codimen
+ 1, corank
);
253 gfc_error ("Invalid form of coarray reference at %C");
256 else if (ar
->dimen_type
[ar
->codimen
+ ar
->dimen
] == DIMEN_STAR
)
258 gfc_error ("Unexpected %<*%> for codimension %d of %d at %C",
259 ar
->codimen
+ 1, corank
);
263 if (ar
->codimen
>= corank
)
265 gfc_error ("Invalid codimension %d at %C, only %d codimensions exist",
266 ar
->codimen
+ 1, corank
);
271 gfc_error ("Array reference at %C cannot have more than %d dimensions",
278 /************** Array specification matching subroutines ***************/
280 /* Free all of the expressions associated with array bounds
284 gfc_free_array_spec (gfc_array_spec
*as
)
291 for (i
= 0; i
< as
->rank
+ as
->corank
; i
++)
293 gfc_free_expr (as
->lower
[i
]);
294 gfc_free_expr (as
->upper
[i
]);
301 /* Take an array bound, resolves the expression, that make up the
302 shape and check associated constraints. */
305 resolve_array_bound (gfc_expr
*e
, int check_constant
)
310 if (!gfc_resolve_expr (e
)
311 || !gfc_specification_expr (e
))
314 if (check_constant
&& !gfc_is_constant_expr (e
))
316 if (e
->expr_type
== EXPR_VARIABLE
)
317 gfc_error ("Variable %qs at %L in this context must be constant",
318 e
->symtree
->n
.sym
->name
, &e
->where
);
320 gfc_error ("Expression at %L in this context must be constant",
329 /* Takes an array specification, resolves the expressions that make up
330 the shape and make sure everything is integral. */
333 gfc_resolve_array_spec (gfc_array_spec
*as
, int check_constant
)
341 for (i
= 0; i
< as
->rank
+ as
->corank
; i
++)
344 if (!resolve_array_bound (e
, check_constant
))
348 if (!resolve_array_bound (e
, check_constant
))
351 if ((as
->lower
[i
] == NULL
) || (as
->upper
[i
] == NULL
))
354 /* If the size is negative in this dimension, set it to zero. */
355 if (as
->lower
[i
]->expr_type
== EXPR_CONSTANT
356 && as
->upper
[i
]->expr_type
== EXPR_CONSTANT
357 && mpz_cmp (as
->upper
[i
]->value
.integer
,
358 as
->lower
[i
]->value
.integer
) < 0)
360 gfc_free_expr (as
->upper
[i
]);
361 as
->upper
[i
] = gfc_copy_expr (as
->lower
[i
]);
362 mpz_sub_ui (as
->upper
[i
]->value
.integer
,
363 as
->upper
[i
]->value
.integer
, 1);
371 /* Match a single array element specification. The return values as
372 well as the upper and lower bounds of the array spec are filled
373 in according to what we see on the input. The caller makes sure
374 individual specifications make sense as a whole.
377 Parsed Lower Upper Returned
378 ------------------------------------
379 : NULL NULL AS_DEFERRED (*)
381 x: x NULL AS_ASSUMED_SHAPE
383 x:* x NULL AS_ASSUMED_SIZE
384 * 1 NULL AS_ASSUMED_SIZE
386 (*) For non-pointer dummy arrays this is AS_ASSUMED_SHAPE. This
387 is fixed during the resolution of formal interfaces.
389 Anything else AS_UNKNOWN. */
392 match_array_element_spec (gfc_array_spec
*as
)
394 gfc_expr
**upper
, **lower
;
398 rank
= as
->rank
== -1 ? 0 : as
->rank
;
399 lower
= &as
->lower
[rank
+ as
->corank
- 1];
400 upper
= &as
->upper
[rank
+ as
->corank
- 1];
402 if (gfc_match_char ('*') == MATCH_YES
)
404 *lower
= gfc_get_int_expr (gfc_default_integer_kind
, NULL
, 1);
405 return AS_ASSUMED_SIZE
;
408 if (gfc_match_char (':') == MATCH_YES
)
411 m
= gfc_match_expr (upper
);
413 gfc_error ("Expected expression in array specification at %C");
416 if (!gfc_expr_check_typed (*upper
, gfc_current_ns
, false))
419 if (gfc_match_char (':') == MATCH_NO
)
421 *lower
= gfc_get_int_expr (gfc_default_integer_kind
, NULL
, 1);
428 if (gfc_match_char ('*') == MATCH_YES
)
429 return AS_ASSUMED_SIZE
;
431 m
= gfc_match_expr (upper
);
432 if (m
== MATCH_ERROR
)
435 return AS_ASSUMED_SHAPE
;
436 if (!gfc_expr_check_typed (*upper
, gfc_current_ns
, false))
443 /* Matches an array specification, incidentally figuring out what sort
444 it is. Match either a normal array specification, or a coarray spec
445 or both. Optionally allow [:] for coarrays. */
448 gfc_match_array_spec (gfc_array_spec
**asp
, bool match_dim
, bool match_codim
)
450 array_type current_type
;
454 as
= gfc_get_array_spec ();
459 if (gfc_match_char ('(') != MATCH_YES
)
466 if (gfc_match (" .. )") == MATCH_YES
)
468 as
->type
= AS_ASSUMED_RANK
;
471 if (!gfc_notify_std (GFC_STD_F2008_TS
, "Assumed-rank array at %C"))
482 current_type
= match_array_element_spec (as
);
484 /* Note that current_type == AS_ASSUMED_SIZE for both assumed-size
485 and implied-shape specifications. If the rank is at least 2, we can
486 distinguish between them. But for rank 1, we currently return
487 ASSUMED_SIZE; this gets adjusted later when we know for sure
488 whether the symbol parsed is a PARAMETER or not. */
492 if (current_type
== AS_UNKNOWN
)
494 as
->type
= current_type
;
498 { /* See how current spec meshes with the existing. */
502 case AS_IMPLIED_SHAPE
:
503 if (current_type
!= AS_ASSUMED_SHAPE
)
505 gfc_error ("Bad array specification for implied-shape"
512 if (current_type
== AS_ASSUMED_SIZE
)
514 as
->type
= AS_ASSUMED_SIZE
;
518 if (current_type
== AS_EXPLICIT
)
521 gfc_error ("Bad array specification for an explicitly shaped "
526 case AS_ASSUMED_SHAPE
:
527 if ((current_type
== AS_ASSUMED_SHAPE
)
528 || (current_type
== AS_DEFERRED
))
531 gfc_error ("Bad array specification for assumed shape "
536 if (current_type
== AS_DEFERRED
)
539 if (current_type
== AS_ASSUMED_SHAPE
)
541 as
->type
= AS_ASSUMED_SHAPE
;
545 gfc_error ("Bad specification for deferred shape array at %C");
548 case AS_ASSUMED_SIZE
:
549 if (as
->rank
== 2 && current_type
== AS_ASSUMED_SIZE
)
551 as
->type
= AS_IMPLIED_SHAPE
;
555 gfc_error ("Bad specification for assumed size array at %C");
558 case AS_ASSUMED_RANK
:
562 if (gfc_match_char (')') == MATCH_YES
)
565 if (gfc_match_char (',') != MATCH_YES
)
567 gfc_error ("Expected another dimension in array declaration at %C");
571 if (as
->rank
+ as
->corank
>= GFC_MAX_DIMENSIONS
)
573 gfc_error ("Array specification at %C has more than %d dimensions",
578 if (as
->corank
+ as
->rank
>= 7
579 && !gfc_notify_std (GFC_STD_F2008
, "Array specification at %C "
580 "with more than 7 dimensions"))
588 if (gfc_match_char ('[') != MATCH_YES
)
591 if (!gfc_notify_std (GFC_STD_F2008
, "Coarray declaration at %C"))
594 if (flag_coarray
== GFC_FCOARRAY_NONE
)
596 gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to enable");
600 if (as
->rank
>= GFC_MAX_DIMENSIONS
)
602 gfc_error ("Array specification at %C has more than %d "
603 "dimensions", GFC_MAX_DIMENSIONS
);
610 current_type
= match_array_element_spec (as
);
612 if (current_type
== AS_UNKNOWN
)
616 as
->cotype
= current_type
;
619 { /* See how current spec meshes with the existing. */
620 case AS_IMPLIED_SHAPE
:
625 if (current_type
== AS_ASSUMED_SIZE
)
627 as
->cotype
= AS_ASSUMED_SIZE
;
631 if (current_type
== AS_EXPLICIT
)
634 gfc_error ("Bad array specification for an explicitly "
635 "shaped array at %C");
639 case AS_ASSUMED_SHAPE
:
640 if ((current_type
== AS_ASSUMED_SHAPE
)
641 || (current_type
== AS_DEFERRED
))
644 gfc_error ("Bad array specification for assumed shape "
649 if (current_type
== AS_DEFERRED
)
652 if (current_type
== AS_ASSUMED_SHAPE
)
654 as
->cotype
= AS_ASSUMED_SHAPE
;
658 gfc_error ("Bad specification for deferred shape array at %C");
661 case AS_ASSUMED_SIZE
:
662 gfc_error ("Bad specification for assumed size array at %C");
665 case AS_ASSUMED_RANK
:
669 if (gfc_match_char (']') == MATCH_YES
)
672 if (gfc_match_char (',') != MATCH_YES
)
674 gfc_error ("Expected another dimension in array declaration at %C");
678 if (as
->rank
+ as
->corank
>= GFC_MAX_DIMENSIONS
)
680 gfc_error ("Array specification at %C has more than %d "
681 "dimensions", GFC_MAX_DIMENSIONS
);
686 if (current_type
== AS_EXPLICIT
)
688 gfc_error ("Upper bound of last coarray dimension must be %<*%> at %C");
692 if (as
->cotype
== AS_ASSUMED_SIZE
)
693 as
->cotype
= AS_EXPLICIT
;
696 as
->type
= as
->cotype
;
699 if (as
->rank
== 0 && as
->corank
== 0)
702 gfc_free_array_spec (as
);
706 /* If a lower bounds of an assumed shape array is blank, put in one. */
707 if (as
->type
== AS_ASSUMED_SHAPE
)
709 for (i
= 0; i
< as
->rank
+ as
->corank
; i
++)
711 if (as
->lower
[i
] == NULL
)
712 as
->lower
[i
] = gfc_get_int_expr (gfc_default_integer_kind
, NULL
, 1);
721 /* Something went wrong. */
722 gfc_free_array_spec (as
);
727 /* Given a symbol and an array specification, modify the symbol to
728 have that array specification. The error locus is needed in case
729 something goes wrong. On failure, the caller must free the spec. */
732 gfc_set_array_spec (gfc_symbol
*sym
, gfc_array_spec
*as
, locus
*error_loc
)
740 && !gfc_add_dimension (&sym
->attr
, sym
->name
, error_loc
))
744 && !gfc_add_codimension (&sym
->attr
, sym
->name
, error_loc
))
753 if ((sym
->as
->type
== AS_ASSUMED_RANK
&& as
->corank
)
754 || (as
->type
== AS_ASSUMED_RANK
&& sym
->as
->corank
))
756 gfc_error ("The assumed-rank array %qs at %L shall not have a "
757 "codimension", sym
->name
, error_loc
);
763 /* The "sym" has no corank (checked via gfc_add_codimension). Thus
764 the codimension is simply added. */
765 gcc_assert (as
->rank
== 0 && sym
->as
->corank
== 0);
767 sym
->as
->cotype
= as
->cotype
;
768 sym
->as
->corank
= as
->corank
;
769 for (i
= 0; i
< as
->corank
; i
++)
771 sym
->as
->lower
[sym
->as
->rank
+ i
] = as
->lower
[i
];
772 sym
->as
->upper
[sym
->as
->rank
+ i
] = as
->upper
[i
];
777 /* The "sym" has no rank (checked via gfc_add_dimension). Thus
778 the dimension is added - but first the codimensions (if existing
779 need to be shifted to make space for the dimension. */
780 gcc_assert (as
->corank
== 0 && sym
->as
->rank
== 0);
782 sym
->as
->rank
= as
->rank
;
783 sym
->as
->type
= as
->type
;
784 sym
->as
->cray_pointee
= as
->cray_pointee
;
785 sym
->as
->cp_was_assumed
= as
->cp_was_assumed
;
787 for (i
= 0; i
< sym
->as
->corank
; i
++)
789 sym
->as
->lower
[as
->rank
+ i
] = sym
->as
->lower
[i
];
790 sym
->as
->upper
[as
->rank
+ i
] = sym
->as
->upper
[i
];
792 for (i
= 0; i
< as
->rank
; i
++)
794 sym
->as
->lower
[i
] = as
->lower
[i
];
795 sym
->as
->upper
[i
] = as
->upper
[i
];
804 /* Copy an array specification. */
807 gfc_copy_array_spec (gfc_array_spec
*src
)
809 gfc_array_spec
*dest
;
815 dest
= gfc_get_array_spec ();
819 for (i
= 0; i
< dest
->rank
+ dest
->corank
; i
++)
821 dest
->lower
[i
] = gfc_copy_expr (dest
->lower
[i
]);
822 dest
->upper
[i
] = gfc_copy_expr (dest
->upper
[i
]);
829 /* Returns nonzero if the two expressions are equal. Only handles integer
833 compare_bounds (gfc_expr
*bound1
, gfc_expr
*bound2
)
835 if (bound1
== NULL
|| bound2
== NULL
836 || bound1
->expr_type
!= EXPR_CONSTANT
837 || bound2
->expr_type
!= EXPR_CONSTANT
838 || bound1
->ts
.type
!= BT_INTEGER
839 || bound2
->ts
.type
!= BT_INTEGER
)
840 gfc_internal_error ("gfc_compare_array_spec(): Array spec clobbered");
842 if (mpz_cmp (bound1
->value
.integer
, bound2
->value
.integer
) == 0)
849 /* Compares two array specifications. They must be constant or deferred
853 gfc_compare_array_spec (gfc_array_spec
*as1
, gfc_array_spec
*as2
)
857 if (as1
== NULL
&& as2
== NULL
)
860 if (as1
== NULL
|| as2
== NULL
)
863 if (as1
->rank
!= as2
->rank
)
866 if (as1
->corank
!= as2
->corank
)
872 if (as1
->type
!= as2
->type
)
875 if (as1
->type
== AS_EXPLICIT
)
876 for (i
= 0; i
< as1
->rank
+ as1
->corank
; i
++)
878 if (compare_bounds (as1
->lower
[i
], as2
->lower
[i
]) == 0)
881 if (compare_bounds (as1
->upper
[i
], as2
->upper
[i
]) == 0)
889 /****************** Array constructor functions ******************/
892 /* Given an expression node that might be an array constructor and a
893 symbol, make sure that no iterators in this or child constructors
894 use the symbol as an implied-DO iterator. Returns nonzero if a
895 duplicate was found. */
898 check_duplicate_iterator (gfc_constructor_base base
, gfc_symbol
*master
)
903 for (c
= gfc_constructor_first (base
); c
; c
= gfc_constructor_next (c
))
907 if (e
->expr_type
== EXPR_ARRAY
908 && check_duplicate_iterator (e
->value
.constructor
, master
))
911 if (c
->iterator
== NULL
)
914 if (c
->iterator
->var
->symtree
->n
.sym
== master
)
916 gfc_error ("DO-iterator %qs at %L is inside iterator of the "
917 "same name", master
->name
, &c
->where
);
927 /* Forward declaration because these functions are mutually recursive. */
928 static match
match_array_cons_element (gfc_constructor_base
*);
930 /* Match a list of array elements. */
933 match_array_list (gfc_constructor_base
*result
)
935 gfc_constructor_base head
;
943 old_loc
= gfc_current_locus
;
945 if (gfc_match_char ('(') == MATCH_NO
)
948 memset (&iter
, '\0', sizeof (gfc_iterator
));
951 m
= match_array_cons_element (&head
);
955 if (gfc_match_char (',') != MATCH_YES
)
963 m
= gfc_match_iterator (&iter
, 0);
966 if (m
== MATCH_ERROR
)
969 m
= match_array_cons_element (&head
);
970 if (m
== MATCH_ERROR
)
977 goto cleanup
; /* Could be a complex constant */
980 if (gfc_match_char (',') != MATCH_YES
)
989 if (gfc_match_char (')') != MATCH_YES
)
992 if (check_duplicate_iterator (head
, iter
.var
->symtree
->n
.sym
))
998 e
= gfc_get_array_expr (BT_UNKNOWN
, 0, &old_loc
);
999 e
->value
.constructor
= head
;
1001 p
= gfc_constructor_append_expr (result
, e
, &gfc_current_locus
);
1002 p
->iterator
= gfc_get_iterator ();
1003 *p
->iterator
= iter
;
1008 gfc_error ("Syntax error in array constructor at %C");
1012 gfc_constructor_free (head
);
1013 gfc_free_iterator (&iter
, 0);
1014 gfc_current_locus
= old_loc
;
1019 /* Match a single element of an array constructor, which can be a
1020 single expression or a list of elements. */
1023 match_array_cons_element (gfc_constructor_base
*result
)
1028 m
= match_array_list (result
);
1032 m
= gfc_match_expr (&expr
);
1036 gfc_constructor_append_expr (result
, expr
, &gfc_current_locus
);
1041 /* Match an array constructor. */
1044 gfc_match_array_constructor (gfc_expr
**result
)
1046 gfc_constructor_base head
, new_cons
;
1047 gfc_undo_change_set changed_syms
;
1052 const char *end_delim
;
1055 if (gfc_match (" (/") == MATCH_NO
)
1057 if (gfc_match (" [") == MATCH_NO
)
1061 if (!gfc_notify_std (GFC_STD_F2003
, "[...] "
1062 "style array constructors at %C"))
1070 where
= gfc_current_locus
;
1071 head
= new_cons
= NULL
;
1074 /* Try to match an optional "type-spec ::" */
1076 gfc_new_undo_checkpoint (changed_syms
);
1077 if (gfc_match_type_spec (&ts
) == MATCH_YES
)
1079 seen_ts
= (gfc_match (" ::") == MATCH_YES
);
1083 if (!gfc_notify_std (GFC_STD_F2003
, "Array constructor "
1084 "including type specification at %C"))
1086 gfc_restore_last_undo_checkpoint ();
1092 gfc_error ("Type-spec at %L cannot contain a deferred "
1093 "type parameter", &where
);
1094 gfc_restore_last_undo_checkpoint ();
1101 gfc_drop_last_undo_checkpoint ();
1104 gfc_restore_last_undo_checkpoint ();
1105 gfc_current_locus
= where
;
1108 if (gfc_match (end_delim
) == MATCH_YES
)
1114 gfc_error ("Empty array constructor at %C is not allowed");
1121 m
= match_array_cons_element (&head
);
1122 if (m
== MATCH_ERROR
)
1127 if (gfc_match_char (',') == MATCH_NO
)
1131 if (gfc_match (end_delim
) == MATCH_NO
)
1135 /* Size must be calculated at resolution time. */
1138 expr
= gfc_get_array_expr (ts
.type
, ts
.kind
, &where
);
1142 expr
= gfc_get_array_expr (BT_UNKNOWN
, 0, &where
);
1144 expr
->value
.constructor
= head
;
1146 expr
->ts
.u
.cl
->length_from_typespec
= seen_ts
;
1152 gfc_error ("Syntax error in array constructor at %C");
1155 gfc_constructor_free (head
);
1161 /************** Check array constructors for correctness **************/
1163 /* Given an expression, compare it's type with the type of the current
1164 constructor. Returns nonzero if an error was issued. The
1165 cons_state variable keeps track of whether the type of the
1166 constructor being read or resolved is known to be good, bad or just
1169 static gfc_typespec constructor_ts
;
1171 { CONS_START
, CONS_GOOD
, CONS_BAD
}
1175 check_element_type (gfc_expr
*expr
, bool convert
)
1177 if (cons_state
== CONS_BAD
)
1178 return 0; /* Suppress further errors */
1180 if (cons_state
== CONS_START
)
1182 if (expr
->ts
.type
== BT_UNKNOWN
)
1183 cons_state
= CONS_BAD
;
1186 cons_state
= CONS_GOOD
;
1187 constructor_ts
= expr
->ts
;
1193 if (gfc_compare_types (&constructor_ts
, &expr
->ts
))
1197 return gfc_convert_type(expr
, &constructor_ts
, 1) ? 0 : 1;
1199 gfc_error ("Element in %s array constructor at %L is %s",
1200 gfc_typename (&constructor_ts
), &expr
->where
,
1201 gfc_typename (&expr
->ts
));
1203 cons_state
= CONS_BAD
;
1208 /* Recursive work function for gfc_check_constructor_type(). */
1211 check_constructor_type (gfc_constructor_base base
, bool convert
)
1216 for (c
= gfc_constructor_first (base
); c
; c
= gfc_constructor_next (c
))
1220 if (e
->expr_type
== EXPR_ARRAY
)
1222 if (!check_constructor_type (e
->value
.constructor
, convert
))
1228 if (check_element_type (e
, convert
))
1236 /* Check that all elements of an array constructor are the same type.
1237 On false, an error has been generated. */
1240 gfc_check_constructor_type (gfc_expr
*e
)
1244 if (e
->ts
.type
!= BT_UNKNOWN
)
1246 cons_state
= CONS_GOOD
;
1247 constructor_ts
= e
->ts
;
1251 cons_state
= CONS_START
;
1252 gfc_clear_ts (&constructor_ts
);
1255 /* If e->ts.type != BT_UNKNOWN, the array constructor included a
1256 typespec, and we will now convert the values on the fly. */
1257 t
= check_constructor_type (e
->value
.constructor
, e
->ts
.type
!= BT_UNKNOWN
);
1258 if (t
&& e
->ts
.type
== BT_UNKNOWN
)
1259 e
->ts
= constructor_ts
;
1266 typedef struct cons_stack
1268 gfc_iterator
*iterator
;
1269 struct cons_stack
*previous
;
1273 static cons_stack
*base
;
1275 static bool check_constructor (gfc_constructor_base
, bool (*) (gfc_expr
*));
1277 /* Check an EXPR_VARIABLE expression in a constructor to make sure
1278 that that variable is an iteration variables. */
1281 gfc_check_iter_variable (gfc_expr
*expr
)
1286 sym
= expr
->symtree
->n
.sym
;
1288 for (c
= base
; c
&& c
->iterator
; c
= c
->previous
)
1289 if (sym
== c
->iterator
->var
->symtree
->n
.sym
)
1296 /* Recursive work function for gfc_check_constructor(). This amounts
1297 to calling the check function for each expression in the
1298 constructor, giving variables with the names of iterators a pass. */
1301 check_constructor (gfc_constructor_base ctor
, bool (*check_function
) (gfc_expr
*))
1308 for (c
= gfc_constructor_first (ctor
); c
; c
= gfc_constructor_next (c
))
1315 if (e
->expr_type
!= EXPR_ARRAY
)
1317 if (!(*check_function
)(e
))
1322 element
.previous
= base
;
1323 element
.iterator
= c
->iterator
;
1326 t
= check_constructor (e
->value
.constructor
, check_function
);
1327 base
= element
.previous
;
1333 /* Nothing went wrong, so all OK. */
1338 /* Checks a constructor to see if it is a particular kind of
1339 expression -- specification, restricted, or initialization as
1340 determined by the check_function. */
1343 gfc_check_constructor (gfc_expr
*expr
, bool (*check_function
) (gfc_expr
*))
1345 cons_stack
*base_save
;
1351 t
= check_constructor (expr
->value
.constructor
, check_function
);
1359 /**************** Simplification of array constructors ****************/
1361 iterator_stack
*iter_stack
;
1365 gfc_constructor_base base
;
1366 int extract_count
, extract_n
;
1367 gfc_expr
*extracted
;
1371 gfc_component
*component
;
1374 bool (*expand_work_function
) (gfc_expr
*);
1378 static expand_info current_expand
;
1380 static bool expand_constructor (gfc_constructor_base
);
1383 /* Work function that counts the number of elements present in a
1387 count_elements (gfc_expr
*e
)
1392 mpz_add_ui (*current_expand
.count
, *current_expand
.count
, 1);
1395 if (!gfc_array_size (e
, &result
))
1401 mpz_add (*current_expand
.count
, *current_expand
.count
, result
);
1410 /* Work function that extracts a particular element from an array
1411 constructor, freeing the rest. */
1414 extract_element (gfc_expr
*e
)
1417 { /* Something unextractable */
1422 if (current_expand
.extract_count
== current_expand
.extract_n
)
1423 current_expand
.extracted
= e
;
1427 current_expand
.extract_count
++;
1433 /* Work function that constructs a new constructor out of the old one,
1434 stringing new elements together. */
1437 expand (gfc_expr
*e
)
1439 gfc_constructor
*c
= gfc_constructor_append_expr (¤t_expand
.base
,
1442 c
->n
.component
= current_expand
.component
;
1447 /* Given an initialization expression that is a variable reference,
1448 substitute the current value of the iteration variable. */
1451 gfc_simplify_iterator_var (gfc_expr
*e
)
1455 for (p
= iter_stack
; p
; p
= p
->prev
)
1456 if (e
->symtree
== p
->variable
)
1460 return; /* Variable not found */
1462 gfc_replace_expr (e
, gfc_get_int_expr (gfc_default_integer_kind
, NULL
, 0));
1464 mpz_set (e
->value
.integer
, p
->value
);
1470 /* Expand an expression with that is inside of a constructor,
1471 recursing into other constructors if present. */
1474 expand_expr (gfc_expr
*e
)
1476 if (e
->expr_type
== EXPR_ARRAY
)
1477 return expand_constructor (e
->value
.constructor
);
1479 e
= gfc_copy_expr (e
);
1481 if (!gfc_simplify_expr (e
, 1))
1487 return current_expand
.expand_work_function (e
);
1492 expand_iterator (gfc_constructor
*c
)
1494 gfc_expr
*start
, *end
, *step
;
1495 iterator_stack frame
;
1504 mpz_init (frame
.value
);
1507 start
= gfc_copy_expr (c
->iterator
->start
);
1508 if (!gfc_simplify_expr (start
, 1))
1511 if (start
->expr_type
!= EXPR_CONSTANT
|| start
->ts
.type
!= BT_INTEGER
)
1514 end
= gfc_copy_expr (c
->iterator
->end
);
1515 if (!gfc_simplify_expr (end
, 1))
1518 if (end
->expr_type
!= EXPR_CONSTANT
|| end
->ts
.type
!= BT_INTEGER
)
1521 step
= gfc_copy_expr (c
->iterator
->step
);
1522 if (!gfc_simplify_expr (step
, 1))
1525 if (step
->expr_type
!= EXPR_CONSTANT
|| step
->ts
.type
!= BT_INTEGER
)
1528 if (mpz_sgn (step
->value
.integer
) == 0)
1530 gfc_error ("Iterator step at %L cannot be zero", &step
->where
);
1534 /* Calculate the trip count of the loop. */
1535 mpz_sub (trip
, end
->value
.integer
, start
->value
.integer
);
1536 mpz_add (trip
, trip
, step
->value
.integer
);
1537 mpz_tdiv_q (trip
, trip
, step
->value
.integer
);
1539 mpz_set (frame
.value
, start
->value
.integer
);
1541 frame
.prev
= iter_stack
;
1542 frame
.variable
= c
->iterator
->var
->symtree
;
1543 iter_stack
= &frame
;
1545 while (mpz_sgn (trip
) > 0)
1547 if (!expand_expr (c
->expr
))
1550 mpz_add (frame
.value
, frame
.value
, step
->value
.integer
);
1551 mpz_sub_ui (trip
, trip
, 1);
1557 gfc_free_expr (start
);
1558 gfc_free_expr (end
);
1559 gfc_free_expr (step
);
1562 mpz_clear (frame
.value
);
1564 iter_stack
= frame
.prev
;
1570 /* Expand a constructor into constant constructors without any
1571 iterators, calling the work function for each of the expanded
1572 expressions. The work function needs to either save or free the
1573 passed expression. */
1576 expand_constructor (gfc_constructor_base base
)
1581 for (c
= gfc_constructor_first (base
); c
; c
= gfc_constructor_next(c
))
1583 if (c
->iterator
!= NULL
)
1585 if (!expand_iterator (c
))
1592 if (e
->expr_type
== EXPR_ARRAY
)
1594 if (!expand_constructor (e
->value
.constructor
))
1600 e
= gfc_copy_expr (e
);
1601 if (!gfc_simplify_expr (e
, 1))
1606 current_expand
.offset
= &c
->offset
;
1607 current_expand
.repeat
= &c
->repeat
;
1608 current_expand
.component
= c
->n
.component
;
1609 if (!current_expand
.expand_work_function(e
))
1616 /* Given an array expression and an element number (starting at zero),
1617 return a pointer to the array element. NULL is returned if the
1618 size of the array has been exceeded. The expression node returned
1619 remains a part of the array and should not be freed. Access is not
1620 efficient at all, but this is another place where things do not
1621 have to be particularly fast. */
1624 gfc_get_array_element (gfc_expr
*array
, int element
)
1626 expand_info expand_save
;
1630 expand_save
= current_expand
;
1631 current_expand
.extract_n
= element
;
1632 current_expand
.expand_work_function
= extract_element
;
1633 current_expand
.extracted
= NULL
;
1634 current_expand
.extract_count
= 0;
1638 rc
= expand_constructor (array
->value
.constructor
);
1639 e
= current_expand
.extracted
;
1640 current_expand
= expand_save
;
1649 /* Top level subroutine for expanding constructors. We only expand
1650 constructor if they are small enough. */
1653 gfc_expand_constructor (gfc_expr
*e
, bool fatal
)
1655 expand_info expand_save
;
1659 /* If we can successfully get an array element at the max array size then
1660 the array is too big to expand, so we just return. */
1661 f
= gfc_get_array_element (e
, flag_max_array_constructor
);
1667 gfc_error ("The number of elements in the array constructor "
1668 "at %L requires an increase of the allowed %d "
1669 "upper limit. See %<-fmax-array-constructor%> "
1670 "option", &e
->where
, flag_max_array_constructor
);
1676 /* We now know the array is not too big so go ahead and try to expand it. */
1677 expand_save
= current_expand
;
1678 current_expand
.base
= NULL
;
1682 current_expand
.expand_work_function
= expand
;
1684 if (!expand_constructor (e
->value
.constructor
))
1686 gfc_constructor_free (current_expand
.base
);
1691 gfc_constructor_free (e
->value
.constructor
);
1692 e
->value
.constructor
= current_expand
.base
;
1697 current_expand
= expand_save
;
1703 /* Work function for checking that an element of a constructor is a
1704 constant, after removal of any iteration variables. We return
1708 is_constant_element (gfc_expr
*e
)
1712 rv
= gfc_is_constant_expr (e
);
1715 return rv
? true : false;
1719 /* Given an array constructor, determine if the constructor is
1720 constant or not by expanding it and making sure that all elements
1721 are constants. This is a bit of a hack since something like (/ (i,
1722 i=1,100000000) /) will take a while as* opposed to a more clever
1723 function that traverses the expression tree. FIXME. */
1726 gfc_constant_ac (gfc_expr
*e
)
1728 expand_info expand_save
;
1732 expand_save
= current_expand
;
1733 current_expand
.expand_work_function
= is_constant_element
;
1735 rc
= expand_constructor (e
->value
.constructor
);
1737 current_expand
= expand_save
;
1745 /* Returns nonzero if an array constructor has been completely
1746 expanded (no iterators) and zero if iterators are present. */
1749 gfc_expanded_ac (gfc_expr
*e
)
1753 if (e
->expr_type
== EXPR_ARRAY
)
1754 for (c
= gfc_constructor_first (e
->value
.constructor
);
1755 c
; c
= gfc_constructor_next (c
))
1756 if (c
->iterator
!= NULL
|| !gfc_expanded_ac (c
->expr
))
1763 /*************** Type resolution of array constructors ***************/
1766 /* The symbol expr_is_sought_symbol_ref will try to find. */
1767 static const gfc_symbol
*sought_symbol
= NULL
;
1770 /* Tells whether the expression E is a variable reference to the symbol
1771 in the static variable SOUGHT_SYMBOL, and sets the locus pointer WHERE
1773 To be used with gfc_expr_walker: if a reference is found we don't need
1774 to look further so we return 1 to skip any further walk. */
1777 expr_is_sought_symbol_ref (gfc_expr
**e
, int *walk_subtrees ATTRIBUTE_UNUSED
,
1780 gfc_expr
*expr
= *e
;
1781 locus
*sym_loc
= (locus
*)where
;
1783 if (expr
->expr_type
== EXPR_VARIABLE
1784 && expr
->symtree
->n
.sym
== sought_symbol
)
1786 *sym_loc
= expr
->where
;
1794 /* Tells whether the expression EXPR contains a reference to the symbol
1795 SYM and in that case sets the position SYM_LOC where the reference is. */
1798 find_symbol_in_expr (gfc_symbol
*sym
, gfc_expr
*expr
, locus
*sym_loc
)
1802 sought_symbol
= sym
;
1803 ret
= gfc_expr_walker (&expr
, &expr_is_sought_symbol_ref
, sym_loc
);
1804 sought_symbol
= NULL
;
1809 /* Recursive array list resolution function. All of the elements must
1810 be of the same type. */
1813 resolve_array_list (gfc_constructor_base base
)
1821 for (c
= gfc_constructor_first (base
); c
; c
= gfc_constructor_next (c
))
1826 gfc_symbol
*iter_var
;
1829 if (!gfc_resolve_iterator (iter
, false, true))
1832 /* Check for bounds referencing the iterator variable. */
1833 gcc_assert (iter
->var
->expr_type
== EXPR_VARIABLE
);
1834 iter_var
= iter
->var
->symtree
->n
.sym
;
1835 if (find_symbol_in_expr (iter_var
, iter
->start
, &iter_var_loc
))
1837 if (!gfc_notify_std (GFC_STD_LEGACY
, "AC-IMPLIED-DO initial "
1838 "expression references control variable "
1839 "at %L", &iter_var_loc
))
1842 if (find_symbol_in_expr (iter_var
, iter
->end
, &iter_var_loc
))
1844 if (!gfc_notify_std (GFC_STD_LEGACY
, "AC-IMPLIED-DO final "
1845 "expression references control variable "
1846 "at %L", &iter_var_loc
))
1849 if (find_symbol_in_expr (iter_var
, iter
->step
, &iter_var_loc
))
1851 if (!gfc_notify_std (GFC_STD_LEGACY
, "AC-IMPLIED-DO step "
1852 "expression references control variable "
1853 "at %L", &iter_var_loc
))
1858 if (!gfc_resolve_expr (c
->expr
))
1861 if (UNLIMITED_POLY (c
->expr
))
1863 gfc_error ("Array constructor value at %L shall not be unlimited "
1864 "polymorphic [F2008: C4106]", &c
->expr
->where
);
1872 /* Resolve character array constructor. If it has a specified constant character
1873 length, pad/truncate the elements here; if the length is not specified and
1874 all elements are of compile-time known length, emit an error as this is
1878 gfc_resolve_character_array_constructor (gfc_expr
*expr
)
1883 gcc_assert (expr
->expr_type
== EXPR_ARRAY
);
1884 gcc_assert (expr
->ts
.type
== BT_CHARACTER
);
1886 if (expr
->ts
.u
.cl
== NULL
)
1888 for (p
= gfc_constructor_first (expr
->value
.constructor
);
1889 p
; p
= gfc_constructor_next (p
))
1890 if (p
->expr
->ts
.u
.cl
!= NULL
)
1892 /* Ensure that if there is a char_len around that it is
1893 used; otherwise the middle-end confuses them! */
1894 expr
->ts
.u
.cl
= p
->expr
->ts
.u
.cl
;
1898 expr
->ts
.u
.cl
= gfc_new_charlen (gfc_current_ns
, NULL
);
1905 if (expr
->ts
.u
.cl
->length
== NULL
)
1907 /* Check that all constant string elements have the same length until
1908 we reach the end or find a variable-length one. */
1910 for (p
= gfc_constructor_first (expr
->value
.constructor
);
1911 p
; p
= gfc_constructor_next (p
))
1913 int current_length
= -1;
1915 for (ref
= p
->expr
->ref
; ref
; ref
= ref
->next
)
1916 if (ref
->type
== REF_SUBSTRING
1917 && ref
->u
.ss
.start
->expr_type
== EXPR_CONSTANT
1918 && ref
->u
.ss
.end
->expr_type
== EXPR_CONSTANT
)
1921 if (p
->expr
->expr_type
== EXPR_CONSTANT
)
1922 current_length
= p
->expr
->value
.character
.length
;
1926 j
= mpz_get_ui (ref
->u
.ss
.end
->value
.integer
)
1927 - mpz_get_ui (ref
->u
.ss
.start
->value
.integer
) + 1;
1928 current_length
= (int) j
;
1930 else if (p
->expr
->ts
.u
.cl
&& p
->expr
->ts
.u
.cl
->length
1931 && p
->expr
->ts
.u
.cl
->length
->expr_type
== EXPR_CONSTANT
)
1934 j
= mpz_get_si (p
->expr
->ts
.u
.cl
->length
->value
.integer
);
1935 current_length
= (int) j
;
1940 gcc_assert (current_length
!= -1);
1942 if (found_length
== -1)
1943 found_length
= current_length
;
1944 else if (found_length
!= current_length
)
1946 gfc_error ("Different CHARACTER lengths (%d/%d) in array"
1947 " constructor at %L", found_length
, current_length
,
1952 gcc_assert (found_length
== current_length
);
1955 gcc_assert (found_length
!= -1);
1957 /* Update the character length of the array constructor. */
1958 expr
->ts
.u
.cl
->length
= gfc_get_int_expr (gfc_default_integer_kind
,
1959 NULL
, found_length
);
1963 /* We've got a character length specified. It should be an integer,
1964 otherwise an error is signalled elsewhere. */
1965 gcc_assert (expr
->ts
.u
.cl
->length
);
1967 /* If we've got a constant character length, pad according to this.
1968 gfc_extract_int does check for BT_INTEGER and EXPR_CONSTANT and sets
1969 max_length only if they pass. */
1970 gfc_extract_int (expr
->ts
.u
.cl
->length
, &found_length
);
1972 /* Now pad/truncate the elements accordingly to the specified character
1973 length. This is ok inside this conditional, as in the case above
1974 (without typespec) all elements are verified to have the same length
1976 if (found_length
!= -1)
1977 for (p
= gfc_constructor_first (expr
->value
.constructor
);
1978 p
; p
= gfc_constructor_next (p
))
1979 if (p
->expr
->expr_type
== EXPR_CONSTANT
)
1981 gfc_expr
*cl
= NULL
;
1982 int current_length
= -1;
1985 if (p
->expr
->ts
.u
.cl
&& p
->expr
->ts
.u
.cl
->length
)
1987 cl
= p
->expr
->ts
.u
.cl
->length
;
1988 gfc_extract_int (cl
, ¤t_length
);
1991 /* If gfc_extract_int above set current_length, we implicitly
1992 know the type is BT_INTEGER and it's EXPR_CONSTANT. */
1994 has_ts
= expr
->ts
.u
.cl
->length_from_typespec
;
1997 || (current_length
!= -1 && current_length
!= found_length
))
1998 gfc_set_constant_character_len (found_length
, p
->expr
,
1999 has_ts
? -1 : found_length
);
2007 /* Resolve all of the expressions in an array list. */
2010 gfc_resolve_array_constructor (gfc_expr
*expr
)
2014 t
= resolve_array_list (expr
->value
.constructor
);
2016 t
= gfc_check_constructor_type (expr
);
2018 /* gfc_resolve_character_array_constructor is called in gfc_resolve_expr after
2019 the call to this function, so we don't need to call it here; if it was
2020 called twice, an error message there would be duplicated. */
2026 /* Copy an iterator structure. */
2029 gfc_copy_iterator (gfc_iterator
*src
)
2036 dest
= gfc_get_iterator ();
2038 dest
->var
= gfc_copy_expr (src
->var
);
2039 dest
->start
= gfc_copy_expr (src
->start
);
2040 dest
->end
= gfc_copy_expr (src
->end
);
2041 dest
->step
= gfc_copy_expr (src
->step
);
2047 /********* Subroutines for determining the size of an array *********/
2049 /* These are needed just to accommodate RESHAPE(). There are no
2050 diagnostics here, we just return a negative number if something
2054 /* Get the size of single dimension of an array specification. The
2055 array is guaranteed to be one dimensional. */
2058 spec_dimen_size (gfc_array_spec
*as
, int dimen
, mpz_t
*result
)
2063 if (dimen
< 0 || dimen
> as
->rank
- 1)
2064 gfc_internal_error ("spec_dimen_size(): Bad dimension");
2066 if (as
->type
!= AS_EXPLICIT
2067 || as
->lower
[dimen
]->expr_type
!= EXPR_CONSTANT
2068 || as
->upper
[dimen
]->expr_type
!= EXPR_CONSTANT
2069 || as
->lower
[dimen
]->ts
.type
!= BT_INTEGER
2070 || as
->upper
[dimen
]->ts
.type
!= BT_INTEGER
)
2075 mpz_sub (*result
, as
->upper
[dimen
]->value
.integer
,
2076 as
->lower
[dimen
]->value
.integer
);
2078 mpz_add_ui (*result
, *result
, 1);
2085 spec_size (gfc_array_spec
*as
, mpz_t
*result
)
2090 if (!as
|| as
->type
== AS_ASSUMED_RANK
)
2093 mpz_init_set_ui (*result
, 1);
2095 for (d
= 0; d
< as
->rank
; d
++)
2097 if (!spec_dimen_size (as
, d
, &size
))
2099 mpz_clear (*result
);
2103 mpz_mul (*result
, *result
, size
);
2111 /* Get the number of elements in an array section. Optionally, also supply
2115 gfc_ref_dimen_size (gfc_array_ref
*ar
, int dimen
, mpz_t
*result
, mpz_t
*end
)
2117 mpz_t upper
, lower
, stride
;
2121 if (dimen
< 0 || ar
== NULL
|| dimen
> ar
->dimen
- 1)
2122 gfc_internal_error ("gfc_ref_dimen_size(): Bad dimension");
2124 switch (ar
->dimen_type
[dimen
])
2128 mpz_set_ui (*result
, 1);
2133 t
= gfc_array_size (ar
->start
[dimen
], result
); /* Recurse! */
2140 if (ar
->stride
[dimen
] == NULL
)
2141 mpz_set_ui (stride
, 1);
2144 if (ar
->stride
[dimen
]->expr_type
!= EXPR_CONSTANT
)
2149 mpz_set (stride
, ar
->stride
[dimen
]->value
.integer
);
2152 /* Calculate the number of elements via gfc_dep_differce, but only if
2153 start and end are both supplied in the reference or the array spec.
2154 This is to guard against strange but valid code like
2159 print *,size(a(n-1:))
2161 where the user changes the value of a variable. If we have to
2162 determine end as well, we cannot do this using gfc_dep_difference.
2163 Fall back to the constants-only code then. */
2169 use_dep
= gfc_dep_difference (ar
->end
[dimen
], ar
->start
[dimen
],
2171 if (!use_dep
&& ar
->end
[dimen
] == NULL
&& ar
->start
[dimen
] == NULL
)
2172 use_dep
= gfc_dep_difference (ar
->as
->upper
[dimen
],
2173 ar
->as
->lower
[dimen
], &diff
);
2178 mpz_add (*result
, diff
, stride
);
2179 mpz_div (*result
, *result
, stride
);
2180 if (mpz_cmp_ui (*result
, 0) < 0)
2181 mpz_set_ui (*result
, 0);
2190 /* Constant-only code here, which covers more cases
2196 if (ar
->start
[dimen
] == NULL
)
2198 if (ar
->as
->lower
[dimen
] == NULL
2199 || ar
->as
->lower
[dimen
]->expr_type
!= EXPR_CONSTANT
)
2201 mpz_set (lower
, ar
->as
->lower
[dimen
]->value
.integer
);
2205 if (ar
->start
[dimen
]->expr_type
!= EXPR_CONSTANT
)
2207 mpz_set (lower
, ar
->start
[dimen
]->value
.integer
);
2210 if (ar
->end
[dimen
] == NULL
)
2212 if (ar
->as
->upper
[dimen
] == NULL
2213 || ar
->as
->upper
[dimen
]->expr_type
!= EXPR_CONSTANT
)
2215 mpz_set (upper
, ar
->as
->upper
[dimen
]->value
.integer
);
2219 if (ar
->end
[dimen
]->expr_type
!= EXPR_CONSTANT
)
2221 mpz_set (upper
, ar
->end
[dimen
]->value
.integer
);
2225 mpz_sub (*result
, upper
, lower
);
2226 mpz_add (*result
, *result
, stride
);
2227 mpz_div (*result
, *result
, stride
);
2229 /* Zero stride caught earlier. */
2230 if (mpz_cmp_ui (*result
, 0) < 0)
2231 mpz_set_ui (*result
, 0);
2238 mpz_sub_ui (*end
, *result
, 1UL);
2239 mpz_mul (*end
, *end
, stride
);
2240 mpz_add (*end
, *end
, lower
);
2250 gfc_internal_error ("gfc_ref_dimen_size(): Bad dimen_type");
2258 ref_size (gfc_array_ref
*ar
, mpz_t
*result
)
2263 mpz_init_set_ui (*result
, 1);
2265 for (d
= 0; d
< ar
->dimen
; d
++)
2267 if (!gfc_ref_dimen_size (ar
, d
, &size
, NULL
))
2269 mpz_clear (*result
);
2273 mpz_mul (*result
, *result
, size
);
2281 /* Given an array expression and a dimension, figure out how many
2282 elements it has along that dimension. Returns true if we were
2283 able to return a result in the 'result' variable, false
2287 gfc_array_dimen_size (gfc_expr
*array
, int dimen
, mpz_t
*result
)
2292 gcc_assert (array
!= NULL
);
2294 if (array
->ts
.type
== BT_CLASS
)
2297 if (array
->rank
== -1)
2300 if (dimen
< 0 || dimen
> array
->rank
- 1)
2301 gfc_internal_error ("gfc_array_dimen_size(): Bad dimension");
2303 switch (array
->expr_type
)
2307 for (ref
= array
->ref
; ref
; ref
= ref
->next
)
2309 if (ref
->type
!= REF_ARRAY
)
2312 if (ref
->u
.ar
.type
== AR_FULL
)
2313 return spec_dimen_size (ref
->u
.ar
.as
, dimen
, result
);
2315 if (ref
->u
.ar
.type
== AR_SECTION
)
2317 for (i
= 0; dimen
>= 0; i
++)
2318 if (ref
->u
.ar
.dimen_type
[i
] != DIMEN_ELEMENT
)
2321 return gfc_ref_dimen_size (&ref
->u
.ar
, i
- 1, result
, NULL
);
2325 if (array
->shape
&& array
->shape
[dimen
])
2327 mpz_init_set (*result
, array
->shape
[dimen
]);
2331 if (array
->symtree
->n
.sym
->attr
.generic
2332 && array
->value
.function
.esym
!= NULL
)
2334 if (!spec_dimen_size (array
->value
.function
.esym
->as
, dimen
, result
))
2337 else if (!spec_dimen_size (array
->symtree
->n
.sym
->as
, dimen
, result
))
2343 if (array
->shape
== NULL
) {
2344 /* Expressions with rank > 1 should have "shape" properly set */
2345 if ( array
->rank
!= 1 )
2346 gfc_internal_error ("gfc_array_dimen_size(): Bad EXPR_ARRAY expr");
2347 return gfc_array_size(array
, result
);
2352 if (array
->shape
== NULL
)
2355 mpz_init_set (*result
, array
->shape
[dimen
]);
2364 /* Given an array expression, figure out how many elements are in the
2365 array. Returns true if this is possible, and sets the 'result'
2366 variable. Otherwise returns false. */
2369 gfc_array_size (gfc_expr
*array
, mpz_t
*result
)
2371 expand_info expand_save
;
2376 if (array
->ts
.type
== BT_CLASS
)
2379 switch (array
->expr_type
)
2382 gfc_push_suppress_errors ();
2384 expand_save
= current_expand
;
2386 current_expand
.count
= result
;
2387 mpz_init_set_ui (*result
, 0);
2389 current_expand
.expand_work_function
= count_elements
;
2392 t
= expand_constructor (array
->value
.constructor
);
2394 gfc_pop_suppress_errors ();
2397 mpz_clear (*result
);
2398 current_expand
= expand_save
;
2402 for (ref
= array
->ref
; ref
; ref
= ref
->next
)
2404 if (ref
->type
!= REF_ARRAY
)
2407 if (ref
->u
.ar
.type
== AR_FULL
)
2408 return spec_size (ref
->u
.ar
.as
, result
);
2410 if (ref
->u
.ar
.type
== AR_SECTION
)
2411 return ref_size (&ref
->u
.ar
, result
);
2414 return spec_size (array
->symtree
->n
.sym
->as
, result
);
2418 if (array
->rank
== 0 || array
->shape
== NULL
)
2421 mpz_init_set_ui (*result
, 1);
2423 for (i
= 0; i
< array
->rank
; i
++)
2424 mpz_mul (*result
, *result
, array
->shape
[i
]);
2433 /* Given an array reference, return the shape of the reference in an
2434 array of mpz_t integers. */
2437 gfc_array_ref_shape (gfc_array_ref
*ar
, mpz_t
*shape
)
2447 for (; d
< ar
->as
->rank
; d
++)
2448 if (!spec_dimen_size (ar
->as
, d
, &shape
[d
]))
2454 for (i
= 0; i
< ar
->dimen
; i
++)
2456 if (ar
->dimen_type
[i
] != DIMEN_ELEMENT
)
2458 if (!gfc_ref_dimen_size (ar
, i
, &shape
[d
], NULL
))
2471 gfc_clear_shape (shape
, d
);
2476 /* Given an array expression, find the array reference structure that
2477 characterizes the reference. */
2480 gfc_find_array_ref (gfc_expr
*e
)
2484 for (ref
= e
->ref
; ref
; ref
= ref
->next
)
2485 if (ref
->type
== REF_ARRAY
2486 && (ref
->u
.ar
.type
== AR_FULL
|| ref
->u
.ar
.type
== AR_SECTION
))
2490 gfc_internal_error ("gfc_find_array_ref(): No ref found");
2496 /* Find out if an array shape is known at compile time. */
2499 gfc_is_compile_time_shape (gfc_array_spec
*as
)
2503 if (as
->type
!= AS_EXPLICIT
)
2506 for (i
= 0; i
< as
->rank
; i
++)
2507 if (!gfc_is_constant_expr (as
->lower
[i
])
2508 || !gfc_is_constant_expr (as
->upper
[i
]))