]>
Commit | Line | Data |
---|---|---|
6de9cd9a | 1 | /* Array things |
5624e564 | 2 | Copyright (C) 2000-2015 Free Software Foundation, Inc. |
6de9cd9a DN |
3 | Contributed by Andy Vaught |
4 | ||
9fc4d79b | 5 | This file is part of GCC. |
6de9cd9a | 6 | |
9fc4d79b TS |
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 | |
d234d788 | 9 | Software Foundation; either version 3, or (at your option) any later |
9fc4d79b | 10 | version. |
6de9cd9a | 11 | |
9fc4d79b TS |
12 | GCC is distributed in the hope that it will be useful, but WITHOUT ANY |
13 | WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
14 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
15 | for more details. | |
6de9cd9a DN |
16 | |
17 | You should have received a copy of the GNU General Public License | |
d234d788 NC |
18 | along with GCC; see the file COPYING3. If not see |
19 | <http://www.gnu.org/licenses/>. */ | |
6de9cd9a DN |
20 | |
21 | #include "config.h" | |
d22e4895 | 22 | #include "system.h" |
953bee7c | 23 | #include "coretypes.h" |
c61819ff | 24 | #include "flags.h" |
6de9cd9a DN |
25 | #include "gfortran.h" |
26 | #include "match.h" | |
b7e75771 | 27 | #include "constructor.h" |
6de9cd9a | 28 | |
6de9cd9a DN |
29 | /**************** Array reference matching subroutines *****************/ |
30 | ||
31 | /* Copy an array reference structure. */ | |
32 | ||
33 | gfc_array_ref * | |
65f8144a | 34 | gfc_copy_array_ref (gfc_array_ref *src) |
6de9cd9a DN |
35 | { |
36 | gfc_array_ref *dest; | |
37 | int i; | |
38 | ||
39 | if (src == NULL) | |
40 | return NULL; | |
41 | ||
42 | dest = gfc_get_array_ref (); | |
43 | ||
44 | *dest = *src; | |
45 | ||
46 | for (i = 0; i < GFC_MAX_DIMENSIONS; i++) | |
47 | { | |
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]); | |
51 | } | |
52 | ||
6de9cd9a DN |
53 | return dest; |
54 | } | |
55 | ||
56 | ||
57 | /* Match a single dimension of an array reference. This can be a | |
58 | single element or an array section. Any modifications we've made | |
59 | to the ar structure are cleaned up by the caller. If the init | |
60 | is set, we require the subscript to be a valid initialization | |
61 | expression. */ | |
62 | ||
63 | static match | |
d3a9eea2 | 64 | match_subscript (gfc_array_ref *ar, int init, bool match_star) |
6de9cd9a | 65 | { |
acaed831 | 66 | match m = MATCH_ERROR; |
d3a9eea2 | 67 | bool star = false; |
6de9cd9a DN |
68 | int i; |
69 | ||
d3a9eea2 | 70 | i = ar->dimen + ar->codimen; |
6de9cd9a | 71 | |
00b9bf8b | 72 | gfc_gobble_whitespace (); |
63645982 | 73 | ar->c_where[i] = gfc_current_locus; |
6de9cd9a DN |
74 | ar->start[i] = ar->end[i] = ar->stride[i] = NULL; |
75 | ||
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. */ | |
79 | ||
80 | ar->dimen_type[i] = DIMEN_UNKNOWN; | |
81 | ||
82 | if (gfc_match_char (':') == MATCH_YES) | |
83 | goto end_element; | |
84 | ||
85 | /* Get start element. */ | |
d3a9eea2 TB |
86 | if (match_star && (m = gfc_match_char ('*')) == MATCH_YES) |
87 | star = true; | |
88 | ||
89 | if (!star && init) | |
6de9cd9a | 90 | m = gfc_match_init_expr (&ar->start[i]); |
d3a9eea2 | 91 | else if (!star) |
6de9cd9a DN |
92 | m = gfc_match_expr (&ar->start[i]); |
93 | ||
6273c3c2 | 94 | if (m == MATCH_NO) |
6de9cd9a DN |
95 | gfc_error ("Expected array subscript at %C"); |
96 | if (m != MATCH_YES) | |
97 | return MATCH_ERROR; | |
98 | ||
99 | if (gfc_match_char (':') == MATCH_NO) | |
d3a9eea2 TB |
100 | goto matched; |
101 | ||
102 | if (star) | |
103 | { | |
c4100eae | 104 | gfc_error ("Unexpected %<*%> in coarray subscript at %C"); |
d3a9eea2 TB |
105 | return MATCH_ERROR; |
106 | } | |
6de9cd9a DN |
107 | |
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; | |
112 | ||
d3a9eea2 TB |
113 | if (match_star && (m = gfc_match_char ('*')) == MATCH_YES) |
114 | star = true; | |
115 | else if (init) | |
6de9cd9a DN |
116 | m = gfc_match_init_expr (&ar->end[i]); |
117 | else | |
118 | m = gfc_match_expr (&ar->end[i]); | |
119 | ||
120 | if (m == MATCH_ERROR) | |
121 | return MATCH_ERROR; | |
122 | ||
123 | /* See if we have an optional stride. */ | |
124 | if (gfc_match_char (':') == MATCH_YES) | |
125 | { | |
d3a9eea2 TB |
126 | if (star) |
127 | { | |
128 | gfc_error ("Strides not allowed in coarray subscript at %C"); | |
129 | return MATCH_ERROR; | |
130 | } | |
131 | ||
6de9cd9a | 132 | m = init ? gfc_match_init_expr (&ar->stride[i]) |
65f8144a | 133 | : gfc_match_expr (&ar->stride[i]); |
6de9cd9a DN |
134 | |
135 | if (m == MATCH_NO) | |
136 | gfc_error ("Expected array subscript stride at %C"); | |
137 | if (m != MATCH_YES) | |
138 | return MATCH_ERROR; | |
139 | } | |
140 | ||
d3a9eea2 TB |
141 | matched: |
142 | if (star) | |
143 | ar->dimen_type[i] = DIMEN_STAR; | |
144 | ||
6de9cd9a DN |
145 | return MATCH_YES; |
146 | } | |
147 | ||
148 | ||
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. */ | |
152 | ||
153 | match | |
d3a9eea2 TB |
154 | gfc_match_array_ref (gfc_array_ref *ar, gfc_array_spec *as, int init, |
155 | int corank) | |
6de9cd9a DN |
156 | { |
157 | match m; | |
d3a9eea2 | 158 | bool matched_bracket = false; |
6de9cd9a | 159 | |
1a4049e7 | 160 | memset (ar, '\0', sizeof (*ar)); |
6de9cd9a | 161 | |
63645982 | 162 | ar->where = gfc_current_locus; |
6de9cd9a | 163 | ar->as = as; |
d3a9eea2 TB |
164 | ar->type = AR_UNKNOWN; |
165 | ||
166 | if (gfc_match_char ('[') == MATCH_YES) | |
167 | { | |
168 | matched_bracket = true; | |
169 | goto coarray; | |
170 | } | |
6de9cd9a DN |
171 | |
172 | if (gfc_match_char ('(') != MATCH_YES) | |
173 | { | |
174 | ar->type = AR_FULL; | |
175 | ar->dimen = 0; | |
176 | return MATCH_YES; | |
177 | } | |
178 | ||
6de9cd9a DN |
179 | for (ar->dimen = 0; ar->dimen < GFC_MAX_DIMENSIONS; ar->dimen++) |
180 | { | |
d3a9eea2 | 181 | m = match_subscript (ar, init, false); |
6de9cd9a | 182 | if (m == MATCH_ERROR) |
d3a9eea2 | 183 | return MATCH_ERROR; |
6de9cd9a DN |
184 | |
185 | if (gfc_match_char (')') == MATCH_YES) | |
d3a9eea2 TB |
186 | { |
187 | ar->dimen++; | |
188 | goto coarray; | |
189 | } | |
6de9cd9a DN |
190 | |
191 | if (gfc_match_char (',') != MATCH_YES) | |
192 | { | |
193 | gfc_error ("Invalid form of array reference at %C"); | |
d3a9eea2 | 194 | return MATCH_ERROR; |
6de9cd9a DN |
195 | } |
196 | } | |
197 | ||
31043f6c FXC |
198 | gfc_error ("Array reference at %C cannot have more than %d dimensions", |
199 | GFC_MAX_DIMENSIONS); | |
6de9cd9a DN |
200 | return MATCH_ERROR; |
201 | ||
d3a9eea2 TB |
202 | coarray: |
203 | if (!matched_bracket && gfc_match_char ('[') != MATCH_YES) | |
204 | { | |
205 | if (ar->dimen > 0) | |
206 | return MATCH_YES; | |
207 | else | |
208 | return MATCH_ERROR; | |
209 | } | |
210 | ||
f19626cf | 211 | if (flag_coarray == GFC_FCOARRAY_NONE) |
d3a9eea2 | 212 | { |
ddc05d11 | 213 | gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to enable"); |
d3a9eea2 TB |
214 | return MATCH_ERROR; |
215 | } | |
216 | ||
217 | if (corank == 0) | |
218 | { | |
219 | gfc_error ("Unexpected coarray designator at %C"); | |
220 | return MATCH_ERROR; | |
221 | } | |
222 | ||
223 | for (ar->codimen = 0; ar->codimen + ar->dimen < GFC_MAX_DIMENSIONS; ar->codimen++) | |
224 | { | |
6273c3c2 | 225 | m = match_subscript (ar, init, true); |
d3a9eea2 TB |
226 | if (m == MATCH_ERROR) |
227 | return MATCH_ERROR; | |
228 | ||
229 | if (gfc_match_char (']') == MATCH_YES) | |
230 | { | |
231 | ar->codimen++; | |
7aa0849a TB |
232 | if (ar->codimen < corank) |
233 | { | |
234 | gfc_error ("Too few codimensions at %C, expected %d not %d", | |
235 | corank, ar->codimen); | |
236 | return MATCH_ERROR; | |
237 | } | |
e84b920c TB |
238 | if (ar->codimen > corank) |
239 | { | |
240 | gfc_error ("Too many codimensions at %C, expected %d not %d", | |
241 | corank, ar->codimen); | |
242 | return MATCH_ERROR; | |
243 | } | |
d3a9eea2 TB |
244 | return MATCH_YES; |
245 | } | |
246 | ||
247 | if (gfc_match_char (',') != MATCH_YES) | |
248 | { | |
7aa0849a | 249 | if (gfc_match_char ('*') == MATCH_YES) |
c4100eae | 250 | gfc_error ("Unexpected %<*%> for codimension %d of %d at %C", |
7aa0849a TB |
251 | ar->codimen + 1, corank); |
252 | else | |
253 | gfc_error ("Invalid form of coarray reference at %C"); | |
254 | return MATCH_ERROR; | |
255 | } | |
6273c3c2 TB |
256 | else if (ar->dimen_type[ar->codimen + ar->dimen] == DIMEN_STAR) |
257 | { | |
c4100eae | 258 | gfc_error ("Unexpected %<*%> for codimension %d of %d at %C", |
6273c3c2 TB |
259 | ar->codimen + 1, corank); |
260 | return MATCH_ERROR; | |
261 | } | |
262 | ||
7aa0849a TB |
263 | if (ar->codimen >= corank) |
264 | { | |
265 | gfc_error ("Invalid codimension %d at %C, only %d codimensions exist", | |
266 | ar->codimen + 1, corank); | |
d3a9eea2 TB |
267 | return MATCH_ERROR; |
268 | } | |
269 | } | |
270 | ||
271 | gfc_error ("Array reference at %C cannot have more than %d dimensions", | |
272 | GFC_MAX_DIMENSIONS); | |
273 | return MATCH_ERROR; | |
6de9cd9a | 274 | |
6de9cd9a DN |
275 | } |
276 | ||
277 | ||
278 | /************** Array specification matching subroutines ***************/ | |
279 | ||
280 | /* Free all of the expressions associated with array bounds | |
281 | specifications. */ | |
282 | ||
283 | void | |
65f8144a | 284 | gfc_free_array_spec (gfc_array_spec *as) |
6de9cd9a DN |
285 | { |
286 | int i; | |
287 | ||
288 | if (as == NULL) | |
289 | return; | |
290 | ||
be59db2d | 291 | for (i = 0; i < as->rank + as->corank; i++) |
6de9cd9a DN |
292 | { |
293 | gfc_free_expr (as->lower[i]); | |
294 | gfc_free_expr (as->upper[i]); | |
295 | } | |
296 | ||
cede9502 | 297 | free (as); |
6de9cd9a DN |
298 | } |
299 | ||
300 | ||
301 | /* Take an array bound, resolves the expression, that make up the | |
302 | shape and check associated constraints. */ | |
303 | ||
524af0d6 | 304 | static bool |
65f8144a | 305 | resolve_array_bound (gfc_expr *e, int check_constant) |
6de9cd9a | 306 | { |
6de9cd9a | 307 | if (e == NULL) |
524af0d6 | 308 | return true; |
6de9cd9a | 309 | |
524af0d6 JB |
310 | if (!gfc_resolve_expr (e) |
311 | || !gfc_specification_expr (e)) | |
312 | return false; | |
6de9cd9a | 313 | |
2b868bf5 | 314 | if (check_constant && !gfc_is_constant_expr (e)) |
6de9cd9a | 315 | { |
2b868bf5 | 316 | if (e->expr_type == EXPR_VARIABLE) |
c4100eae | 317 | gfc_error ("Variable %qs at %L in this context must be constant", |
2b868bf5 DK |
318 | e->symtree->n.sym->name, &e->where); |
319 | else | |
320 | gfc_error ("Expression at %L in this context must be constant", | |
321 | &e->where); | |
524af0d6 | 322 | return false; |
6de9cd9a DN |
323 | } |
324 | ||
524af0d6 | 325 | return true; |
6de9cd9a DN |
326 | } |
327 | ||
328 | ||
329 | /* Takes an array specification, resolves the expressions that make up | |
330 | the shape and make sure everything is integral. */ | |
331 | ||
524af0d6 | 332 | bool |
65f8144a | 333 | gfc_resolve_array_spec (gfc_array_spec *as, int check_constant) |
6de9cd9a DN |
334 | { |
335 | gfc_expr *e; | |
336 | int i; | |
337 | ||
338 | if (as == NULL) | |
524af0d6 | 339 | return true; |
6de9cd9a | 340 | |
be59db2d | 341 | for (i = 0; i < as->rank + as->corank; i++) |
6de9cd9a DN |
342 | { |
343 | e = as->lower[i]; | |
524af0d6 JB |
344 | if (!resolve_array_bound (e, check_constant)) |
345 | return false; | |
6de9cd9a DN |
346 | |
347 | e = as->upper[i]; | |
524af0d6 JB |
348 | if (!resolve_array_bound (e, check_constant)) |
349 | return false; | |
52f56431 PT |
350 | |
351 | if ((as->lower[i] == NULL) || (as->upper[i] == NULL)) | |
352 | continue; | |
353 | ||
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) | |
359 | { | |
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); | |
364 | } | |
6de9cd9a DN |
365 | } |
366 | ||
524af0d6 | 367 | return true; |
6de9cd9a DN |
368 | } |
369 | ||
370 | ||
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. | |
375 | ||
376 | ||
65f8144a SK |
377 | Parsed Lower Upper Returned |
378 | ------------------------------------ | |
b7fdd4ed SK |
379 | : NULL NULL AS_DEFERRED (*) |
380 | x 1 x AS_EXPLICIT | |
381 | x: x NULL AS_ASSUMED_SHAPE | |
382 | x:y x y AS_EXPLICIT | |
383 | x:* x NULL AS_ASSUMED_SIZE | |
384 | * 1 NULL AS_ASSUMED_SIZE | |
6de9cd9a DN |
385 | |
386 | (*) For non-pointer dummy arrays this is AS_ASSUMED_SHAPE. This | |
387 | is fixed during the resolution of formal interfaces. | |
388 | ||
389 | Anything else AS_UNKNOWN. */ | |
390 | ||
391 | static array_type | |
65f8144a | 392 | match_array_element_spec (gfc_array_spec *as) |
6de9cd9a DN |
393 | { |
394 | gfc_expr **upper, **lower; | |
395 | match m; | |
c62c6622 | 396 | int rank; |
6de9cd9a | 397 | |
c62c6622 TB |
398 | rank = as->rank == -1 ? 0 : as->rank; |
399 | lower = &as->lower[rank + as->corank - 1]; | |
400 | upper = &as->upper[rank + as->corank - 1]; | |
6de9cd9a DN |
401 | |
402 | if (gfc_match_char ('*') == MATCH_YES) | |
403 | { | |
b7e75771 | 404 | *lower = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1); |
6de9cd9a DN |
405 | return AS_ASSUMED_SIZE; |
406 | } | |
407 | ||
408 | if (gfc_match_char (':') == MATCH_YES) | |
409 | return AS_DEFERRED; | |
410 | ||
411 | m = gfc_match_expr (upper); | |
412 | if (m == MATCH_NO) | |
413 | gfc_error ("Expected expression in array specification at %C"); | |
414 | if (m != MATCH_YES) | |
415 | return AS_UNKNOWN; | |
524af0d6 | 416 | if (!gfc_expr_check_typed (*upper, gfc_current_ns, false)) |
f37e928c | 417 | return AS_UNKNOWN; |
6de9cd9a DN |
418 | |
419 | if (gfc_match_char (':') == MATCH_NO) | |
420 | { | |
b7e75771 | 421 | *lower = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1); |
6de9cd9a DN |
422 | return AS_EXPLICIT; |
423 | } | |
424 | ||
425 | *lower = *upper; | |
426 | *upper = NULL; | |
427 | ||
428 | if (gfc_match_char ('*') == MATCH_YES) | |
429 | return AS_ASSUMED_SIZE; | |
430 | ||
431 | m = gfc_match_expr (upper); | |
432 | if (m == MATCH_ERROR) | |
433 | return AS_UNKNOWN; | |
434 | if (m == MATCH_NO) | |
435 | return AS_ASSUMED_SHAPE; | |
524af0d6 | 436 | if (!gfc_expr_check_typed (*upper, gfc_current_ns, false)) |
f37e928c | 437 | return AS_UNKNOWN; |
6de9cd9a DN |
438 | |
439 | return AS_EXPLICIT; | |
440 | } | |
441 | ||
442 | ||
443 | /* Matches an array specification, incidentally figuring out what sort | |
be59db2d TB |
444 | it is. Match either a normal array specification, or a coarray spec |
445 | or both. Optionally allow [:] for coarrays. */ | |
6de9cd9a DN |
446 | |
447 | match | |
be59db2d | 448 | gfc_match_array_spec (gfc_array_spec **asp, bool match_dim, bool match_codim) |
6de9cd9a DN |
449 | { |
450 | array_type current_type; | |
451 | gfc_array_spec *as; | |
452 | int i; | |
6de9cd9a | 453 | |
40a33e3f | 454 | as = gfc_get_array_spec (); |
6de9cd9a | 455 | |
be59db2d TB |
456 | if (!match_dim) |
457 | goto coarray; | |
458 | ||
459 | if (gfc_match_char ('(') != MATCH_YES) | |
460 | { | |
461 | if (!match_codim) | |
462 | goto done; | |
463 | goto coarray; | |
464 | } | |
6de9cd9a | 465 | |
c62c6622 TB |
466 | if (gfc_match (" .. )") == MATCH_YES) |
467 | { | |
468 | as->type = AS_ASSUMED_RANK; | |
469 | as->rank = -1; | |
470 | ||
524af0d6 | 471 | if (!gfc_notify_std (GFC_STD_F2008_TS, "Assumed-rank array at %C")) |
c62c6622 TB |
472 | goto cleanup; |
473 | ||
474 | if (!match_codim) | |
475 | goto done; | |
476 | goto coarray; | |
477 | } | |
478 | ||
6de9cd9a DN |
479 | for (;;) |
480 | { | |
be59db2d | 481 | as->rank++; |
6de9cd9a DN |
482 | current_type = match_array_element_spec (as); |
483 | ||
f5ca06e6 DK |
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. */ | |
489 | ||
6de9cd9a DN |
490 | if (as->rank == 1) |
491 | { | |
492 | if (current_type == AS_UNKNOWN) | |
493 | goto cleanup; | |
494 | as->type = current_type; | |
495 | } | |
496 | else | |
497 | switch (as->type) | |
65f8144a | 498 | { /* See how current spec meshes with the existing. */ |
6de9cd9a DN |
499 | case AS_UNKNOWN: |
500 | goto cleanup; | |
501 | ||
f5ca06e6 DK |
502 | case AS_IMPLIED_SHAPE: |
503 | if (current_type != AS_ASSUMED_SHAPE) | |
504 | { | |
505 | gfc_error ("Bad array specification for implied-shape" | |
506 | " array at %C"); | |
507 | goto cleanup; | |
508 | } | |
509 | break; | |
510 | ||
6de9cd9a DN |
511 | case AS_EXPLICIT: |
512 | if (current_type == AS_ASSUMED_SIZE) | |
513 | { | |
514 | as->type = AS_ASSUMED_SIZE; | |
515 | break; | |
516 | } | |
517 | ||
518 | if (current_type == AS_EXPLICIT) | |
519 | break; | |
520 | ||
65f8144a SK |
521 | gfc_error ("Bad array specification for an explicitly shaped " |
522 | "array at %C"); | |
6de9cd9a DN |
523 | |
524 | goto cleanup; | |
525 | ||
526 | case AS_ASSUMED_SHAPE: | |
527 | if ((current_type == AS_ASSUMED_SHAPE) | |
528 | || (current_type == AS_DEFERRED)) | |
529 | break; | |
530 | ||
65f8144a SK |
531 | gfc_error ("Bad array specification for assumed shape " |
532 | "array at %C"); | |
6de9cd9a DN |
533 | goto cleanup; |
534 | ||
535 | case AS_DEFERRED: | |
536 | if (current_type == AS_DEFERRED) | |
537 | break; | |
538 | ||
539 | if (current_type == AS_ASSUMED_SHAPE) | |
540 | { | |
541 | as->type = AS_ASSUMED_SHAPE; | |
542 | break; | |
543 | } | |
544 | ||
545 | gfc_error ("Bad specification for deferred shape array at %C"); | |
546 | goto cleanup; | |
547 | ||
548 | case AS_ASSUMED_SIZE: | |
f5ca06e6 DK |
549 | if (as->rank == 2 && current_type == AS_ASSUMED_SIZE) |
550 | { | |
551 | as->type = AS_IMPLIED_SHAPE; | |
552 | break; | |
553 | } | |
554 | ||
6de9cd9a DN |
555 | gfc_error ("Bad specification for assumed size array at %C"); |
556 | goto cleanup; | |
c62c6622 TB |
557 | |
558 | case AS_ASSUMED_RANK: | |
8b704316 | 559 | gcc_unreachable (); |
6de9cd9a DN |
560 | } |
561 | ||
562 | if (gfc_match_char (')') == MATCH_YES) | |
563 | break; | |
564 | ||
565 | if (gfc_match_char (',') != MATCH_YES) | |
566 | { | |
567 | gfc_error ("Expected another dimension in array declaration at %C"); | |
568 | goto cleanup; | |
569 | } | |
570 | ||
be59db2d | 571 | if (as->rank + as->corank >= GFC_MAX_DIMENSIONS) |
6de9cd9a | 572 | { |
31043f6c FXC |
573 | gfc_error ("Array specification at %C has more than %d dimensions", |
574 | GFC_MAX_DIMENSIONS); | |
6de9cd9a DN |
575 | goto cleanup; |
576 | } | |
577 | ||
be59db2d | 578 | if (as->corank + as->rank >= 7 |
524af0d6 JB |
579 | && !gfc_notify_std (GFC_STD_F2008, "Array specification at %C " |
580 | "with more than 7 dimensions")) | |
a4cd1610 | 581 | goto cleanup; |
be59db2d | 582 | } |
a4cd1610 | 583 | |
be59db2d TB |
584 | if (!match_codim) |
585 | goto done; | |
586 | ||
587 | coarray: | |
588 | if (gfc_match_char ('[') != MATCH_YES) | |
589 | goto done; | |
590 | ||
524af0d6 | 591 | if (!gfc_notify_std (GFC_STD_F2008, "Coarray declaration at %C")) |
be59db2d TB |
592 | goto cleanup; |
593 | ||
f19626cf | 594 | if (flag_coarray == GFC_FCOARRAY_NONE) |
f4d1d50a | 595 | { |
ddc05d11 | 596 | gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to enable"); |
d3a9eea2 | 597 | goto cleanup; |
f4d1d50a TB |
598 | } |
599 | ||
e85df92e TB |
600 | if (as->rank >= GFC_MAX_DIMENSIONS) |
601 | { | |
602 | gfc_error ("Array specification at %C has more than %d " | |
603 | "dimensions", GFC_MAX_DIMENSIONS); | |
604 | goto cleanup; | |
605 | } | |
606 | ||
be59db2d TB |
607 | for (;;) |
608 | { | |
609 | as->corank++; | |
610 | current_type = match_array_element_spec (as); | |
611 | ||
612 | if (current_type == AS_UNKNOWN) | |
613 | goto cleanup; | |
614 | ||
be59db2d | 615 | if (as->corank == 1) |
178f9aa1 | 616 | as->cotype = current_type; |
be59db2d | 617 | else |
178f9aa1 | 618 | switch (as->cotype) |
be59db2d | 619 | { /* See how current spec meshes with the existing. */ |
f5ca06e6 | 620 | case AS_IMPLIED_SHAPE: |
be59db2d TB |
621 | case AS_UNKNOWN: |
622 | goto cleanup; | |
623 | ||
624 | case AS_EXPLICIT: | |
625 | if (current_type == AS_ASSUMED_SIZE) | |
626 | { | |
178f9aa1 | 627 | as->cotype = AS_ASSUMED_SIZE; |
be59db2d TB |
628 | break; |
629 | } | |
630 | ||
631 | if (current_type == AS_EXPLICIT) | |
632 | break; | |
633 | ||
634 | gfc_error ("Bad array specification for an explicitly " | |
635 | "shaped array at %C"); | |
636 | ||
637 | goto cleanup; | |
638 | ||
639 | case AS_ASSUMED_SHAPE: | |
640 | if ((current_type == AS_ASSUMED_SHAPE) | |
641 | || (current_type == AS_DEFERRED)) | |
642 | break; | |
643 | ||
644 | gfc_error ("Bad array specification for assumed shape " | |
645 | "array at %C"); | |
646 | goto cleanup; | |
647 | ||
648 | case AS_DEFERRED: | |
649 | if (current_type == AS_DEFERRED) | |
650 | break; | |
651 | ||
652 | if (current_type == AS_ASSUMED_SHAPE) | |
653 | { | |
178f9aa1 | 654 | as->cotype = AS_ASSUMED_SHAPE; |
be59db2d TB |
655 | break; |
656 | } | |
657 | ||
658 | gfc_error ("Bad specification for deferred shape array at %C"); | |
659 | goto cleanup; | |
660 | ||
661 | case AS_ASSUMED_SIZE: | |
662 | gfc_error ("Bad specification for assumed size array at %C"); | |
663 | goto cleanup; | |
c62c6622 TB |
664 | |
665 | case AS_ASSUMED_RANK: | |
8b704316 | 666 | gcc_unreachable (); |
be59db2d TB |
667 | } |
668 | ||
669 | if (gfc_match_char (']') == MATCH_YES) | |
670 | break; | |
671 | ||
672 | if (gfc_match_char (',') != MATCH_YES) | |
673 | { | |
674 | gfc_error ("Expected another dimension in array declaration at %C"); | |
675 | goto cleanup; | |
676 | } | |
677 | ||
e85df92e | 678 | if (as->rank + as->corank >= GFC_MAX_DIMENSIONS) |
be59db2d TB |
679 | { |
680 | gfc_error ("Array specification at %C has more than %d " | |
681 | "dimensions", GFC_MAX_DIMENSIONS); | |
682 | goto cleanup; | |
683 | } | |
684 | } | |
685 | ||
686 | if (current_type == AS_EXPLICIT) | |
687 | { | |
a4d9b221 | 688 | gfc_error ("Upper bound of last coarray dimension must be %<*%> at %C"); |
be59db2d TB |
689 | goto cleanup; |
690 | } | |
691 | ||
178f9aa1 TB |
692 | if (as->cotype == AS_ASSUMED_SIZE) |
693 | as->cotype = AS_EXPLICIT; | |
694 | ||
695 | if (as->rank == 0) | |
696 | as->type = as->cotype; | |
be59db2d TB |
697 | |
698 | done: | |
699 | if (as->rank == 0 && as->corank == 0) | |
700 | { | |
701 | *asp = NULL; | |
702 | gfc_free_array_spec (as); | |
703 | return MATCH_NO; | |
6de9cd9a DN |
704 | } |
705 | ||
706 | /* If a lower bounds of an assumed shape array is blank, put in one. */ | |
707 | if (as->type == AS_ASSUMED_SHAPE) | |
708 | { | |
be59db2d | 709 | for (i = 0; i < as->rank + as->corank; i++) |
6de9cd9a DN |
710 | { |
711 | if (as->lower[i] == NULL) | |
b7e75771 | 712 | as->lower[i] = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1); |
6de9cd9a DN |
713 | } |
714 | } | |
be59db2d | 715 | |
6de9cd9a | 716 | *asp = as; |
be59db2d | 717 | |
6de9cd9a DN |
718 | return MATCH_YES; |
719 | ||
720 | cleanup: | |
721 | /* Something went wrong. */ | |
722 | gfc_free_array_spec (as); | |
723 | return MATCH_ERROR; | |
724 | } | |
725 | ||
726 | ||
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. */ | |
730 | ||
524af0d6 | 731 | bool |
65f8144a | 732 | gfc_set_array_spec (gfc_symbol *sym, gfc_array_spec *as, locus *error_loc) |
6de9cd9a | 733 | { |
be59db2d TB |
734 | int i; |
735 | ||
6de9cd9a | 736 | if (as == NULL) |
524af0d6 | 737 | return true; |
6de9cd9a | 738 | |
be59db2d | 739 | if (as->rank |
524af0d6 JB |
740 | && !gfc_add_dimension (&sym->attr, sym->name, error_loc)) |
741 | return false; | |
be59db2d TB |
742 | |
743 | if (as->corank | |
524af0d6 JB |
744 | && !gfc_add_codimension (&sym->attr, sym->name, error_loc)) |
745 | return false; | |
6de9cd9a | 746 | |
be59db2d TB |
747 | if (sym->as == NULL) |
748 | { | |
749 | sym->as = as; | |
524af0d6 | 750 | return true; |
be59db2d | 751 | } |
6de9cd9a | 752 | |
63fbf586 TB |
753 | if ((sym->as->type == AS_ASSUMED_RANK && as->corank) |
754 | || (as->type == AS_ASSUMED_RANK && sym->as->corank)) | |
755 | { | |
c4100eae | 756 | gfc_error ("The assumed-rank array %qs at %L shall not have a " |
63fbf586 | 757 | "codimension", sym->name, error_loc); |
524af0d6 | 758 | return false; |
63fbf586 TB |
759 | } |
760 | ||
be59db2d TB |
761 | if (as->corank) |
762 | { | |
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); | |
766 | ||
178f9aa1 | 767 | sym->as->cotype = as->cotype; |
be59db2d TB |
768 | sym->as->corank = as->corank; |
769 | for (i = 0; i < as->corank; i++) | |
770 | { | |
771 | sym->as->lower[sym->as->rank + i] = as->lower[i]; | |
772 | sym->as->upper[sym->as->rank + i] = as->upper[i]; | |
773 | } | |
774 | } | |
775 | else | |
776 | { | |
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); | |
781 | ||
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; | |
786 | ||
787 | for (i = 0; i < sym->as->corank; i++) | |
788 | { | |
789 | sym->as->lower[as->rank + i] = sym->as->lower[i]; | |
790 | sym->as->upper[as->rank + i] = sym->as->upper[i]; | |
791 | } | |
792 | for (i = 0; i < as->rank; i++) | |
793 | { | |
794 | sym->as->lower[i] = as->lower[i]; | |
795 | sym->as->upper[i] = as->upper[i]; | |
796 | } | |
797 | } | |
798 | ||
cede9502 | 799 | free (as); |
524af0d6 | 800 | return true; |
6de9cd9a DN |
801 | } |
802 | ||
803 | ||
804 | /* Copy an array specification. */ | |
805 | ||
806 | gfc_array_spec * | |
65f8144a | 807 | gfc_copy_array_spec (gfc_array_spec *src) |
6de9cd9a DN |
808 | { |
809 | gfc_array_spec *dest; | |
810 | int i; | |
811 | ||
812 | if (src == NULL) | |
813 | return NULL; | |
814 | ||
815 | dest = gfc_get_array_spec (); | |
816 | ||
817 | *dest = *src; | |
818 | ||
be59db2d | 819 | for (i = 0; i < dest->rank + dest->corank; i++) |
6de9cd9a DN |
820 | { |
821 | dest->lower[i] = gfc_copy_expr (dest->lower[i]); | |
822 | dest->upper[i] = gfc_copy_expr (dest->upper[i]); | |
823 | } | |
824 | ||
825 | return dest; | |
826 | } | |
827 | ||
65f8144a | 828 | |
6de9cd9a DN |
829 | /* Returns nonzero if the two expressions are equal. Only handles integer |
830 | constants. */ | |
831 | ||
832 | static int | |
65f8144a | 833 | compare_bounds (gfc_expr *bound1, gfc_expr *bound2) |
6de9cd9a DN |
834 | { |
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"); | |
841 | ||
842 | if (mpz_cmp (bound1->value.integer, bound2->value.integer) == 0) | |
843 | return 1; | |
844 | else | |
845 | return 0; | |
846 | } | |
847 | ||
65f8144a | 848 | |
6de9cd9a DN |
849 | /* Compares two array specifications. They must be constant or deferred |
850 | shape. */ | |
851 | ||
852 | int | |
65f8144a | 853 | gfc_compare_array_spec (gfc_array_spec *as1, gfc_array_spec *as2) |
6de9cd9a DN |
854 | { |
855 | int i; | |
856 | ||
857 | if (as1 == NULL && as2 == NULL) | |
858 | return 1; | |
859 | ||
860 | if (as1 == NULL || as2 == NULL) | |
861 | return 0; | |
862 | ||
863 | if (as1->rank != as2->rank) | |
864 | return 0; | |
865 | ||
be59db2d TB |
866 | if (as1->corank != as2->corank) |
867 | return 0; | |
868 | ||
6de9cd9a DN |
869 | if (as1->rank == 0) |
870 | return 1; | |
871 | ||
872 | if (as1->type != as2->type) | |
873 | return 0; | |
874 | ||
875 | if (as1->type == AS_EXPLICIT) | |
be59db2d | 876 | for (i = 0; i < as1->rank + as1->corank; i++) |
6de9cd9a DN |
877 | { |
878 | if (compare_bounds (as1->lower[i], as2->lower[i]) == 0) | |
879 | return 0; | |
880 | ||
881 | if (compare_bounds (as1->upper[i], as2->upper[i]) == 0) | |
882 | return 0; | |
883 | } | |
884 | ||
885 | return 1; | |
886 | } | |
887 | ||
888 | ||
889 | /****************** Array constructor functions ******************/ | |
890 | ||
6de9cd9a DN |
891 | |
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. */ | |
896 | ||
897 | static int | |
b7e75771 | 898 | check_duplicate_iterator (gfc_constructor_base base, gfc_symbol *master) |
6de9cd9a | 899 | { |
b7e75771 | 900 | gfc_constructor *c; |
6de9cd9a DN |
901 | gfc_expr *e; |
902 | ||
b7e75771 | 903 | for (c = gfc_constructor_first (base); c; c = gfc_constructor_next (c)) |
6de9cd9a DN |
904 | { |
905 | e = c->expr; | |
906 | ||
907 | if (e->expr_type == EXPR_ARRAY | |
908 | && check_duplicate_iterator (e->value.constructor, master)) | |
909 | return 1; | |
910 | ||
911 | if (c->iterator == NULL) | |
912 | continue; | |
913 | ||
914 | if (c->iterator->var->symtree->n.sym == master) | |
915 | { | |
c4100eae | 916 | gfc_error ("DO-iterator %qs at %L is inside iterator of the " |
65f8144a | 917 | "same name", master->name, &c->where); |
6de9cd9a DN |
918 | |
919 | return 1; | |
920 | } | |
921 | } | |
922 | ||
923 | return 0; | |
924 | } | |
925 | ||
926 | ||
927 | /* Forward declaration because these functions are mutually recursive. */ | |
b7e75771 | 928 | static match match_array_cons_element (gfc_constructor_base *); |
6de9cd9a DN |
929 | |
930 | /* Match a list of array elements. */ | |
931 | ||
932 | static match | |
b7e75771 | 933 | match_array_list (gfc_constructor_base *result) |
6de9cd9a | 934 | { |
b7e75771 JD |
935 | gfc_constructor_base head; |
936 | gfc_constructor *p; | |
6de9cd9a DN |
937 | gfc_iterator iter; |
938 | locus old_loc; | |
939 | gfc_expr *e; | |
940 | match m; | |
941 | int n; | |
942 | ||
63645982 | 943 | old_loc = gfc_current_locus; |
6de9cd9a DN |
944 | |
945 | if (gfc_match_char ('(') == MATCH_NO) | |
946 | return MATCH_NO; | |
947 | ||
948 | memset (&iter, '\0', sizeof (gfc_iterator)); | |
949 | head = NULL; | |
950 | ||
951 | m = match_array_cons_element (&head); | |
952 | if (m != MATCH_YES) | |
953 | goto cleanup; | |
954 | ||
6de9cd9a DN |
955 | if (gfc_match_char (',') != MATCH_YES) |
956 | { | |
957 | m = MATCH_NO; | |
958 | goto cleanup; | |
959 | } | |
960 | ||
961 | for (n = 1;; n++) | |
962 | { | |
cdc6637d | 963 | m = gfc_match_iterator (&iter, 0); |
6de9cd9a DN |
964 | if (m == MATCH_YES) |
965 | break; | |
966 | if (m == MATCH_ERROR) | |
967 | goto cleanup; | |
968 | ||
b7e75771 | 969 | m = match_array_cons_element (&head); |
6de9cd9a DN |
970 | if (m == MATCH_ERROR) |
971 | goto cleanup; | |
972 | if (m == MATCH_NO) | |
973 | { | |
974 | if (n > 2) | |
975 | goto syntax; | |
976 | m = MATCH_NO; | |
977 | goto cleanup; /* Could be a complex constant */ | |
978 | } | |
979 | ||
6de9cd9a DN |
980 | if (gfc_match_char (',') != MATCH_YES) |
981 | { | |
982 | if (n > 2) | |
983 | goto syntax; | |
984 | m = MATCH_NO; | |
985 | goto cleanup; | |
986 | } | |
987 | } | |
988 | ||
989 | if (gfc_match_char (')') != MATCH_YES) | |
990 | goto syntax; | |
991 | ||
992 | if (check_duplicate_iterator (head, iter.var->symtree->n.sym)) | |
993 | { | |
994 | m = MATCH_ERROR; | |
995 | goto cleanup; | |
996 | } | |
997 | ||
b7e75771 | 998 | e = gfc_get_array_expr (BT_UNKNOWN, 0, &old_loc); |
6de9cd9a DN |
999 | e->value.constructor = head; |
1000 | ||
b7e75771 | 1001 | p = gfc_constructor_append_expr (result, e, &gfc_current_locus); |
6de9cd9a DN |
1002 | p->iterator = gfc_get_iterator (); |
1003 | *p->iterator = iter; | |
1004 | ||
6de9cd9a DN |
1005 | return MATCH_YES; |
1006 | ||
1007 | syntax: | |
1008 | gfc_error ("Syntax error in array constructor at %C"); | |
1009 | m = MATCH_ERROR; | |
1010 | ||
1011 | cleanup: | |
b7e75771 | 1012 | gfc_constructor_free (head); |
6de9cd9a | 1013 | gfc_free_iterator (&iter, 0); |
63645982 | 1014 | gfc_current_locus = old_loc; |
6de9cd9a DN |
1015 | return m; |
1016 | } | |
1017 | ||
1018 | ||
1019 | /* Match a single element of an array constructor, which can be a | |
1020 | single expression or a list of elements. */ | |
1021 | ||
1022 | static match | |
b7e75771 | 1023 | match_array_cons_element (gfc_constructor_base *result) |
6de9cd9a | 1024 | { |
6de9cd9a DN |
1025 | gfc_expr *expr; |
1026 | match m; | |
1027 | ||
1028 | m = match_array_list (result); | |
1029 | if (m != MATCH_NO) | |
1030 | return m; | |
1031 | ||
1032 | m = gfc_match_expr (&expr); | |
1033 | if (m != MATCH_YES) | |
1034 | return m; | |
1035 | ||
b7e75771 | 1036 | gfc_constructor_append_expr (result, expr, &gfc_current_locus); |
6de9cd9a DN |
1037 | return MATCH_YES; |
1038 | } | |
1039 | ||
1040 | ||
1041 | /* Match an array constructor. */ | |
1042 | ||
1043 | match | |
65f8144a | 1044 | gfc_match_array_constructor (gfc_expr **result) |
6de9cd9a | 1045 | { |
b7e75771 | 1046 | gfc_constructor_base head, new_cons; |
d6a5de16 | 1047 | gfc_undo_change_set changed_syms; |
6de9cd9a | 1048 | gfc_expr *expr; |
c03fc95d | 1049 | gfc_typespec ts; |
6de9cd9a DN |
1050 | locus where; |
1051 | match m; | |
acc75ae3 | 1052 | const char *end_delim; |
c03fc95d | 1053 | bool seen_ts; |
6de9cd9a DN |
1054 | |
1055 | if (gfc_match (" (/") == MATCH_NO) | |
acc75ae3 EE |
1056 | { |
1057 | if (gfc_match (" [") == MATCH_NO) | |
65f8144a | 1058 | return MATCH_NO; |
acc75ae3 | 1059 | else |
65f8144a | 1060 | { |
524af0d6 JB |
1061 | if (!gfc_notify_std (GFC_STD_F2003, "[...] " |
1062 | "style array constructors at %C")) | |
65f8144a SK |
1063 | return MATCH_ERROR; |
1064 | end_delim = " ]"; | |
1065 | } | |
acc75ae3 EE |
1066 | } |
1067 | else | |
1068 | end_delim = " /)"; | |
6de9cd9a | 1069 | |
63645982 | 1070 | where = gfc_current_locus; |
b7e75771 | 1071 | head = new_cons = NULL; |
c03fc95d DK |
1072 | seen_ts = false; |
1073 | ||
1074 | /* Try to match an optional "type-spec ::" */ | |
fc2655fb | 1075 | gfc_clear_ts (&ts); |
d6a5de16 | 1076 | gfc_new_undo_checkpoint (changed_syms); |
894460a7 | 1077 | if (gfc_match_type_spec (&ts) == MATCH_YES) |
c03fc95d DK |
1078 | { |
1079 | seen_ts = (gfc_match (" ::") == MATCH_YES); | |
1080 | ||
1081 | if (seen_ts) | |
1082 | { | |
524af0d6 JB |
1083 | if (!gfc_notify_std (GFC_STD_F2003, "Array constructor " |
1084 | "including type specification at %C")) | |
d6a5de16 MM |
1085 | { |
1086 | gfc_restore_last_undo_checkpoint (); | |
1087 | goto cleanup; | |
1088 | } | |
e69afb29 SK |
1089 | |
1090 | if (ts.deferred) | |
1091 | { | |
1092 | gfc_error ("Type-spec at %L cannot contain a deferred " | |
1093 | "type parameter", &where); | |
d6a5de16 | 1094 | gfc_restore_last_undo_checkpoint (); |
e69afb29 SK |
1095 | goto cleanup; |
1096 | } | |
c03fc95d DK |
1097 | } |
1098 | } | |
1099 | ||
d6a5de16 MM |
1100 | if (seen_ts) |
1101 | gfc_drop_last_undo_checkpoint (); | |
1102 | else | |
1103 | { | |
1104 | gfc_restore_last_undo_checkpoint (); | |
1105 | gfc_current_locus = where; | |
1106 | } | |
6de9cd9a | 1107 | |
acc75ae3 | 1108 | if (gfc_match (end_delim) == MATCH_YES) |
ab21e272 | 1109 | { |
c03fc95d DK |
1110 | if (seen_ts) |
1111 | goto done; | |
1112 | else | |
1113 | { | |
1114 | gfc_error ("Empty array constructor at %C is not allowed"); | |
1115 | goto cleanup; | |
1116 | } | |
ab21e272 | 1117 | } |
6de9cd9a DN |
1118 | |
1119 | for (;;) | |
1120 | { | |
b7e75771 | 1121 | m = match_array_cons_element (&head); |
6de9cd9a DN |
1122 | if (m == MATCH_ERROR) |
1123 | goto cleanup; | |
1124 | if (m == MATCH_NO) | |
1125 | goto syntax; | |
1126 | ||
6de9cd9a DN |
1127 | if (gfc_match_char (',') == MATCH_NO) |
1128 | break; | |
1129 | } | |
1130 | ||
acc75ae3 | 1131 | if (gfc_match (end_delim) == MATCH_NO) |
6de9cd9a DN |
1132 | goto syntax; |
1133 | ||
c03fc95d | 1134 | done: |
6de9cd9a | 1135 | /* Size must be calculated at resolution time. */ |
c03fc95d | 1136 | if (seen_ts) |
b7e75771 JD |
1137 | { |
1138 | expr = gfc_get_array_expr (ts.type, ts.kind, &where); | |
1139 | expr->ts = ts; | |
1140 | } | |
c03fc95d | 1141 | else |
b7e75771 JD |
1142 | expr = gfc_get_array_expr (BT_UNKNOWN, 0, &where); |
1143 | ||
1144 | expr->value.constructor = head; | |
bc21d315 JW |
1145 | if (expr->ts.u.cl) |
1146 | expr->ts.u.cl->length_from_typespec = seen_ts; | |
c03fc95d | 1147 | |
6de9cd9a DN |
1148 | *result = expr; |
1149 | return MATCH_YES; | |
1150 | ||
1151 | syntax: | |
1152 | gfc_error ("Syntax error in array constructor at %C"); | |
1153 | ||
1154 | cleanup: | |
b7e75771 | 1155 | gfc_constructor_free (head); |
6de9cd9a DN |
1156 | return MATCH_ERROR; |
1157 | } | |
1158 | ||
1159 | ||
1160 | ||
1161 | /************** Check array constructors for correctness **************/ | |
1162 | ||
1163 | /* Given an expression, compare it's type with the type of the current | |
1164 | constructor. Returns nonzero if an error was issued. The | |
1165 | cons_state variable keeps track of whether the type of the | |
1166 | constructor being read or resolved is known to be good, bad or just | |
1167 | starting out. */ | |
1168 | ||
1169 | static gfc_typespec constructor_ts; | |
1170 | static enum | |
1171 | { CONS_START, CONS_GOOD, CONS_BAD } | |
1172 | cons_state; | |
1173 | ||
1174 | static int | |
c03fc95d | 1175 | check_element_type (gfc_expr *expr, bool convert) |
6de9cd9a | 1176 | { |
6de9cd9a | 1177 | if (cons_state == CONS_BAD) |
1f2959f0 | 1178 | return 0; /* Suppress further errors */ |
6de9cd9a DN |
1179 | |
1180 | if (cons_state == CONS_START) | |
1181 | { | |
1182 | if (expr->ts.type == BT_UNKNOWN) | |
1183 | cons_state = CONS_BAD; | |
1184 | else | |
1185 | { | |
1186 | cons_state = CONS_GOOD; | |
1187 | constructor_ts = expr->ts; | |
1188 | } | |
1189 | ||
1190 | return 0; | |
1191 | } | |
1192 | ||
1193 | if (gfc_compare_types (&constructor_ts, &expr->ts)) | |
1194 | return 0; | |
1195 | ||
c03fc95d | 1196 | if (convert) |
524af0d6 | 1197 | return gfc_convert_type(expr, &constructor_ts, 1) ? 0 : 1; |
c03fc95d | 1198 | |
6de9cd9a DN |
1199 | gfc_error ("Element in %s array constructor at %L is %s", |
1200 | gfc_typename (&constructor_ts), &expr->where, | |
1201 | gfc_typename (&expr->ts)); | |
1202 | ||
1203 | cons_state = CONS_BAD; | |
1204 | return 1; | |
1205 | } | |
1206 | ||
1207 | ||
f7b529fa | 1208 | /* Recursive work function for gfc_check_constructor_type(). */ |
6de9cd9a | 1209 | |
524af0d6 | 1210 | static bool |
b7e75771 | 1211 | check_constructor_type (gfc_constructor_base base, bool convert) |
6de9cd9a | 1212 | { |
b7e75771 | 1213 | gfc_constructor *c; |
6de9cd9a DN |
1214 | gfc_expr *e; |
1215 | ||
b7e75771 | 1216 | for (c = gfc_constructor_first (base); c; c = gfc_constructor_next (c)) |
6de9cd9a DN |
1217 | { |
1218 | e = c->expr; | |
1219 | ||
1220 | if (e->expr_type == EXPR_ARRAY) | |
1221 | { | |
524af0d6 JB |
1222 | if (!check_constructor_type (e->value.constructor, convert)) |
1223 | return false; | |
6de9cd9a DN |
1224 | |
1225 | continue; | |
1226 | } | |
1227 | ||
c03fc95d | 1228 | if (check_element_type (e, convert)) |
524af0d6 | 1229 | return false; |
6de9cd9a DN |
1230 | } |
1231 | ||
524af0d6 | 1232 | return true; |
6de9cd9a DN |
1233 | } |
1234 | ||
1235 | ||
1236 | /* Check that all elements of an array constructor are the same type. | |
524af0d6 | 1237 | On false, an error has been generated. */ |
6de9cd9a | 1238 | |
524af0d6 | 1239 | bool |
65f8144a | 1240 | gfc_check_constructor_type (gfc_expr *e) |
6de9cd9a | 1241 | { |
524af0d6 | 1242 | bool t; |
6de9cd9a | 1243 | |
c03fc95d DK |
1244 | if (e->ts.type != BT_UNKNOWN) |
1245 | { | |
1246 | cons_state = CONS_GOOD; | |
1247 | constructor_ts = e->ts; | |
1248 | } | |
1249 | else | |
1250 | { | |
1251 | cons_state = CONS_START; | |
1252 | gfc_clear_ts (&constructor_ts); | |
1253 | } | |
6de9cd9a | 1254 | |
c03fc95d DK |
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); | |
524af0d6 | 1258 | if (t && e->ts.type == BT_UNKNOWN) |
6de9cd9a DN |
1259 | e->ts = constructor_ts; |
1260 | ||
1261 | return t; | |
1262 | } | |
1263 | ||
1264 | ||
1265 | ||
1266 | typedef struct cons_stack | |
1267 | { | |
1268 | gfc_iterator *iterator; | |
1269 | struct cons_stack *previous; | |
1270 | } | |
1271 | cons_stack; | |
1272 | ||
1273 | static cons_stack *base; | |
1274 | ||
524af0d6 | 1275 | static bool check_constructor (gfc_constructor_base, bool (*) (gfc_expr *)); |
6de9cd9a DN |
1276 | |
1277 | /* Check an EXPR_VARIABLE expression in a constructor to make sure | |
1278 | that that variable is an iteration variables. */ | |
1279 | ||
524af0d6 | 1280 | bool |
65f8144a | 1281 | gfc_check_iter_variable (gfc_expr *expr) |
6de9cd9a | 1282 | { |
6de9cd9a DN |
1283 | gfc_symbol *sym; |
1284 | cons_stack *c; | |
1285 | ||
1286 | sym = expr->symtree->n.sym; | |
1287 | ||
a48a9173 | 1288 | for (c = base; c && c->iterator; c = c->previous) |
6de9cd9a | 1289 | if (sym == c->iterator->var->symtree->n.sym) |
524af0d6 | 1290 | return true; |
6de9cd9a | 1291 | |
524af0d6 | 1292 | return false; |
6de9cd9a DN |
1293 | } |
1294 | ||
1295 | ||
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. */ | |
1299 | ||
524af0d6 JB |
1300 | static bool |
1301 | check_constructor (gfc_constructor_base ctor, bool (*check_function) (gfc_expr *)) | |
6de9cd9a DN |
1302 | { |
1303 | cons_stack element; | |
1304 | gfc_expr *e; | |
524af0d6 | 1305 | bool t; |
b7e75771 | 1306 | gfc_constructor *c; |
6de9cd9a | 1307 | |
b7e75771 | 1308 | for (c = gfc_constructor_first (ctor); c; c = gfc_constructor_next (c)) |
6de9cd9a DN |
1309 | { |
1310 | e = c->expr; | |
1311 | ||
7430df97 JW |
1312 | if (!e) |
1313 | continue; | |
1314 | ||
6de9cd9a DN |
1315 | if (e->expr_type != EXPR_ARRAY) |
1316 | { | |
524af0d6 JB |
1317 | if (!(*check_function)(e)) |
1318 | return false; | |
6de9cd9a DN |
1319 | continue; |
1320 | } | |
1321 | ||
1322 | element.previous = base; | |
1323 | element.iterator = c->iterator; | |
1324 | ||
1325 | base = &element; | |
1326 | t = check_constructor (e->value.constructor, check_function); | |
1327 | base = element.previous; | |
1328 | ||
524af0d6 JB |
1329 | if (!t) |
1330 | return false; | |
6de9cd9a DN |
1331 | } |
1332 | ||
1333 | /* Nothing went wrong, so all OK. */ | |
524af0d6 | 1334 | return true; |
6de9cd9a DN |
1335 | } |
1336 | ||
1337 | ||
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. */ | |
1341 | ||
524af0d6 JB |
1342 | bool |
1343 | gfc_check_constructor (gfc_expr *expr, bool (*check_function) (gfc_expr *)) | |
6de9cd9a DN |
1344 | { |
1345 | cons_stack *base_save; | |
524af0d6 | 1346 | bool t; |
6de9cd9a DN |
1347 | |
1348 | base_save = base; | |
1349 | base = NULL; | |
1350 | ||
1351 | t = check_constructor (expr->value.constructor, check_function); | |
1352 | base = base_save; | |
1353 | ||
1354 | return t; | |
1355 | } | |
1356 | ||
1357 | ||
1358 | ||
1359 | /**************** Simplification of array constructors ****************/ | |
1360 | ||
1361 | iterator_stack *iter_stack; | |
1362 | ||
1363 | typedef struct | |
1364 | { | |
b7e75771 | 1365 | gfc_constructor_base base; |
6de9cd9a DN |
1366 | int extract_count, extract_n; |
1367 | gfc_expr *extracted; | |
1368 | mpz_t *count; | |
1369 | ||
1370 | mpz_t *offset; | |
1371 | gfc_component *component; | |
21ea4922 | 1372 | mpz_t *repeat; |
6de9cd9a | 1373 | |
524af0d6 | 1374 | bool (*expand_work_function) (gfc_expr *); |
6de9cd9a DN |
1375 | } |
1376 | expand_info; | |
1377 | ||
1378 | static expand_info current_expand; | |
1379 | ||
524af0d6 | 1380 | static bool expand_constructor (gfc_constructor_base); |
6de9cd9a DN |
1381 | |
1382 | ||
1383 | /* Work function that counts the number of elements present in a | |
1384 | constructor. */ | |
1385 | ||
524af0d6 | 1386 | static bool |
65f8144a | 1387 | count_elements (gfc_expr *e) |
6de9cd9a DN |
1388 | { |
1389 | mpz_t result; | |
1390 | ||
1391 | if (e->rank == 0) | |
1392 | mpz_add_ui (*current_expand.count, *current_expand.count, 1); | |
1393 | else | |
1394 | { | |
524af0d6 | 1395 | if (!gfc_array_size (e, &result)) |
6de9cd9a DN |
1396 | { |
1397 | gfc_free_expr (e); | |
524af0d6 | 1398 | return false; |
6de9cd9a DN |
1399 | } |
1400 | ||
1401 | mpz_add (*current_expand.count, *current_expand.count, result); | |
1402 | mpz_clear (result); | |
1403 | } | |
1404 | ||
1405 | gfc_free_expr (e); | |
524af0d6 | 1406 | return true; |
6de9cd9a DN |
1407 | } |
1408 | ||
1409 | ||
1410 | /* Work function that extracts a particular element from an array | |
1411 | constructor, freeing the rest. */ | |
1412 | ||
524af0d6 | 1413 | static bool |
65f8144a | 1414 | extract_element (gfc_expr *e) |
6de9cd9a | 1415 | { |
6de9cd9a DN |
1416 | if (e->rank != 0) |
1417 | { /* Something unextractable */ | |
1418 | gfc_free_expr (e); | |
524af0d6 | 1419 | return false; |
6de9cd9a DN |
1420 | } |
1421 | ||
1422 | if (current_expand.extract_count == current_expand.extract_n) | |
1423 | current_expand.extracted = e; | |
1424 | else | |
1425 | gfc_free_expr (e); | |
1426 | ||
1427 | current_expand.extract_count++; | |
8b704316 | 1428 | |
524af0d6 | 1429 | return true; |
6de9cd9a DN |
1430 | } |
1431 | ||
1432 | ||
1433 | /* Work function that constructs a new constructor out of the old one, | |
1434 | stringing new elements together. */ | |
1435 | ||
524af0d6 | 1436 | static bool |
65f8144a | 1437 | expand (gfc_expr *e) |
6de9cd9a | 1438 | { |
b7e75771 JD |
1439 | gfc_constructor *c = gfc_constructor_append_expr (¤t_expand.base, |
1440 | e, &e->where); | |
6de9cd9a | 1441 | |
b7e75771 | 1442 | c->n.component = current_expand.component; |
524af0d6 | 1443 | return true; |
6de9cd9a DN |
1444 | } |
1445 | ||
1446 | ||
1447 | /* Given an initialization expression that is a variable reference, | |
1448 | substitute the current value of the iteration variable. */ | |
1449 | ||
1450 | void | |
65f8144a | 1451 | gfc_simplify_iterator_var (gfc_expr *e) |
6de9cd9a DN |
1452 | { |
1453 | iterator_stack *p; | |
1454 | ||
1455 | for (p = iter_stack; p; p = p->prev) | |
1456 | if (e->symtree == p->variable) | |
1457 | break; | |
1458 | ||
1459 | if (p == NULL) | |
1460 | return; /* Variable not found */ | |
1461 | ||
b7e75771 | 1462 | gfc_replace_expr (e, gfc_get_int_expr (gfc_default_integer_kind, NULL, 0)); |
6de9cd9a DN |
1463 | |
1464 | mpz_set (e->value.integer, p->value); | |
1465 | ||
1466 | return; | |
1467 | } | |
1468 | ||
1469 | ||
1470 | /* Expand an expression with that is inside of a constructor, | |
1471 | recursing into other constructors if present. */ | |
1472 | ||
524af0d6 | 1473 | static bool |
65f8144a | 1474 | expand_expr (gfc_expr *e) |
6de9cd9a | 1475 | { |
6de9cd9a DN |
1476 | if (e->expr_type == EXPR_ARRAY) |
1477 | return expand_constructor (e->value.constructor); | |
1478 | ||
1479 | e = gfc_copy_expr (e); | |
1480 | ||
524af0d6 | 1481 | if (!gfc_simplify_expr (e, 1)) |
6de9cd9a DN |
1482 | { |
1483 | gfc_free_expr (e); | |
524af0d6 | 1484 | return false; |
6de9cd9a DN |
1485 | } |
1486 | ||
1487 | return current_expand.expand_work_function (e); | |
1488 | } | |
1489 | ||
1490 | ||
524af0d6 | 1491 | static bool |
65f8144a | 1492 | expand_iterator (gfc_constructor *c) |
6de9cd9a DN |
1493 | { |
1494 | gfc_expr *start, *end, *step; | |
1495 | iterator_stack frame; | |
1496 | mpz_t trip; | |
524af0d6 | 1497 | bool t; |
6de9cd9a DN |
1498 | |
1499 | end = step = NULL; | |
1500 | ||
524af0d6 | 1501 | t = false; |
6de9cd9a DN |
1502 | |
1503 | mpz_init (trip); | |
1504 | mpz_init (frame.value); | |
66914102 | 1505 | frame.prev = NULL; |
6de9cd9a DN |
1506 | |
1507 | start = gfc_copy_expr (c->iterator->start); | |
524af0d6 | 1508 | if (!gfc_simplify_expr (start, 1)) |
6de9cd9a DN |
1509 | goto cleanup; |
1510 | ||
1511 | if (start->expr_type != EXPR_CONSTANT || start->ts.type != BT_INTEGER) | |
1512 | goto cleanup; | |
1513 | ||
1514 | end = gfc_copy_expr (c->iterator->end); | |
524af0d6 | 1515 | if (!gfc_simplify_expr (end, 1)) |
6de9cd9a DN |
1516 | goto cleanup; |
1517 | ||
1518 | if (end->expr_type != EXPR_CONSTANT || end->ts.type != BT_INTEGER) | |
1519 | goto cleanup; | |
1520 | ||
1521 | step = gfc_copy_expr (c->iterator->step); | |
524af0d6 | 1522 | if (!gfc_simplify_expr (step, 1)) |
6de9cd9a DN |
1523 | goto cleanup; |
1524 | ||
1525 | if (step->expr_type != EXPR_CONSTANT || step->ts.type != BT_INTEGER) | |
1526 | goto cleanup; | |
1527 | ||
1528 | if (mpz_sgn (step->value.integer) == 0) | |
1529 | { | |
1530 | gfc_error ("Iterator step at %L cannot be zero", &step->where); | |
1531 | goto cleanup; | |
1532 | } | |
1533 | ||
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); | |
1538 | ||
1539 | mpz_set (frame.value, start->value.integer); | |
1540 | ||
1541 | frame.prev = iter_stack; | |
1542 | frame.variable = c->iterator->var->symtree; | |
1543 | iter_stack = &frame; | |
1544 | ||
1545 | while (mpz_sgn (trip) > 0) | |
1546 | { | |
524af0d6 | 1547 | if (!expand_expr (c->expr)) |
6de9cd9a DN |
1548 | goto cleanup; |
1549 | ||
1550 | mpz_add (frame.value, frame.value, step->value.integer); | |
1551 | mpz_sub_ui (trip, trip, 1); | |
1552 | } | |
1553 | ||
524af0d6 | 1554 | t = true; |
6de9cd9a DN |
1555 | |
1556 | cleanup: | |
1557 | gfc_free_expr (start); | |
1558 | gfc_free_expr (end); | |
1559 | gfc_free_expr (step); | |
1560 | ||
1561 | mpz_clear (trip); | |
1562 | mpz_clear (frame.value); | |
1563 | ||
1564 | iter_stack = frame.prev; | |
1565 | ||
1566 | return t; | |
1567 | } | |
1568 | ||
1569 | ||
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. */ | |
1574 | ||
524af0d6 | 1575 | static bool |
b7e75771 | 1576 | expand_constructor (gfc_constructor_base base) |
6de9cd9a | 1577 | { |
b7e75771 | 1578 | gfc_constructor *c; |
6de9cd9a DN |
1579 | gfc_expr *e; |
1580 | ||
b7e75771 | 1581 | for (c = gfc_constructor_first (base); c; c = gfc_constructor_next(c)) |
6de9cd9a DN |
1582 | { |
1583 | if (c->iterator != NULL) | |
1584 | { | |
524af0d6 JB |
1585 | if (!expand_iterator (c)) |
1586 | return false; | |
6de9cd9a DN |
1587 | continue; |
1588 | } | |
1589 | ||
1590 | e = c->expr; | |
1591 | ||
1592 | if (e->expr_type == EXPR_ARRAY) | |
1593 | { | |
524af0d6 JB |
1594 | if (!expand_constructor (e->value.constructor)) |
1595 | return false; | |
6de9cd9a DN |
1596 | |
1597 | continue; | |
1598 | } | |
1599 | ||
1600 | e = gfc_copy_expr (e); | |
524af0d6 | 1601 | if (!gfc_simplify_expr (e, 1)) |
6de9cd9a DN |
1602 | { |
1603 | gfc_free_expr (e); | |
524af0d6 | 1604 | return false; |
6de9cd9a | 1605 | } |
b7e75771 | 1606 | current_expand.offset = &c->offset; |
21ea4922 | 1607 | current_expand.repeat = &c->repeat; |
b7e75771 | 1608 | current_expand.component = c->n.component; |
524af0d6 JB |
1609 | if (!current_expand.expand_work_function(e)) |
1610 | return false; | |
6de9cd9a | 1611 | } |
524af0d6 | 1612 | return true; |
6de9cd9a DN |
1613 | } |
1614 | ||
1615 | ||
b7e75771 JD |
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. */ | |
1622 | ||
1623 | static gfc_expr * | |
1624 | gfc_get_array_element (gfc_expr *array, int element) | |
1625 | { | |
1626 | expand_info expand_save; | |
1627 | gfc_expr *e; | |
524af0d6 | 1628 | bool rc; |
b7e75771 JD |
1629 | |
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; | |
1635 | ||
1636 | iter_stack = NULL; | |
1637 | ||
1638 | rc = expand_constructor (array->value.constructor); | |
1639 | e = current_expand.extracted; | |
1640 | current_expand = expand_save; | |
1641 | ||
524af0d6 | 1642 | if (!rc) |
b7e75771 JD |
1643 | return NULL; |
1644 | ||
1645 | return e; | |
1646 | } | |
1647 | ||
1648 | ||
6de9cd9a DN |
1649 | /* Top level subroutine for expanding constructors. We only expand |
1650 | constructor if they are small enough. */ | |
1651 | ||
524af0d6 | 1652 | bool |
928f0490 | 1653 | gfc_expand_constructor (gfc_expr *e, bool fatal) |
6de9cd9a DN |
1654 | { |
1655 | expand_info expand_save; | |
1656 | gfc_expr *f; | |
524af0d6 | 1657 | bool rc; |
6de9cd9a | 1658 | |
b7e75771 JD |
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. */ | |
c61819ff | 1661 | f = gfc_get_array_element (e, flag_max_array_constructor); |
6de9cd9a DN |
1662 | if (f != NULL) |
1663 | { | |
1664 | gfc_free_expr (f); | |
928f0490 TB |
1665 | if (fatal) |
1666 | { | |
1667 | gfc_error ("The number of elements in the array constructor " | |
1668 | "at %L requires an increase of the allowed %d " | |
c4100eae | 1669 | "upper limit. See %<-fmax-array-constructor%> " |
c61819ff | 1670 | "option", &e->where, flag_max_array_constructor); |
524af0d6 | 1671 | return false; |
928f0490 | 1672 | } |
524af0d6 | 1673 | return true; |
6de9cd9a DN |
1674 | } |
1675 | ||
b7e75771 | 1676 | /* We now know the array is not too big so go ahead and try to expand it. */ |
6de9cd9a | 1677 | expand_save = current_expand; |
b7e75771 | 1678 | current_expand.base = NULL; |
6de9cd9a DN |
1679 | |
1680 | iter_stack = NULL; | |
1681 | ||
1682 | current_expand.expand_work_function = expand; | |
1683 | ||
524af0d6 | 1684 | if (!expand_constructor (e->value.constructor)) |
6de9cd9a | 1685 | { |
b7e75771 | 1686 | gfc_constructor_free (current_expand.base); |
524af0d6 | 1687 | rc = false; |
6de9cd9a DN |
1688 | goto done; |
1689 | } | |
1690 | ||
b7e75771 JD |
1691 | gfc_constructor_free (e->value.constructor); |
1692 | e->value.constructor = current_expand.base; | |
6de9cd9a | 1693 | |
524af0d6 | 1694 | rc = true; |
6de9cd9a DN |
1695 | |
1696 | done: | |
1697 | current_expand = expand_save; | |
1698 | ||
1699 | return rc; | |
1700 | } | |
1701 | ||
1702 | ||
1703 | /* Work function for checking that an element of a constructor is a | |
1704 | constant, after removal of any iteration variables. We return | |
524af0d6 | 1705 | false if not so. */ |
6de9cd9a | 1706 | |
524af0d6 | 1707 | static bool |
f2ff577a | 1708 | is_constant_element (gfc_expr *e) |
6de9cd9a DN |
1709 | { |
1710 | int rv; | |
1711 | ||
1712 | rv = gfc_is_constant_expr (e); | |
1713 | gfc_free_expr (e); | |
1714 | ||
524af0d6 | 1715 | return rv ? true : false; |
6de9cd9a DN |
1716 | } |
1717 | ||
1718 | ||
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. */ | |
1724 | ||
1725 | int | |
65f8144a | 1726 | gfc_constant_ac (gfc_expr *e) |
6de9cd9a DN |
1727 | { |
1728 | expand_info expand_save; | |
524af0d6 | 1729 | bool rc; |
6de9cd9a | 1730 | |
b7e75771 JD |
1731 | iter_stack = NULL; |
1732 | expand_save = current_expand; | |
1733 | current_expand.expand_work_function = is_constant_element; | |
6de9cd9a | 1734 | |
b7e75771 | 1735 | rc = expand_constructor (e->value.constructor); |
6de9cd9a | 1736 | |
b7e75771 | 1737 | current_expand = expand_save; |
524af0d6 | 1738 | if (!rc) |
6de9cd9a DN |
1739 | return 0; |
1740 | ||
1741 | return 1; | |
1742 | } | |
1743 | ||
1744 | ||
1745 | /* Returns nonzero if an array constructor has been completely | |
1746 | expanded (no iterators) and zero if iterators are present. */ | |
1747 | ||
1748 | int | |
65f8144a | 1749 | gfc_expanded_ac (gfc_expr *e) |
6de9cd9a | 1750 | { |
b7e75771 | 1751 | gfc_constructor *c; |
6de9cd9a DN |
1752 | |
1753 | if (e->expr_type == EXPR_ARRAY) | |
b7e75771 JD |
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)) | |
6de9cd9a DN |
1757 | return 0; |
1758 | ||
1759 | return 1; | |
1760 | } | |
1761 | ||
1762 | ||
1763 | /*************** Type resolution of array constructors ***************/ | |
1764 | ||
ca27d5ae MM |
1765 | |
1766 | /* The symbol expr_is_sought_symbol_ref will try to find. */ | |
1767 | static const gfc_symbol *sought_symbol = NULL; | |
1768 | ||
1769 | ||
1770 | /* Tells whether the expression E is a variable reference to the symbol | |
1771 | in the static variable SOUGHT_SYMBOL, and sets the locus pointer WHERE | |
1772 | accordingly. | |
1773 | To be used with gfc_expr_walker: if a reference is found we don't need | |
1774 | to look further so we return 1 to skip any further walk. */ | |
1775 | ||
1776 | static int | |
1777 | expr_is_sought_symbol_ref (gfc_expr **e, int *walk_subtrees ATTRIBUTE_UNUSED, | |
1778 | void *where) | |
1779 | { | |
1780 | gfc_expr *expr = *e; | |
1781 | locus *sym_loc = (locus *)where; | |
1782 | ||
1783 | if (expr->expr_type == EXPR_VARIABLE | |
1784 | && expr->symtree->n.sym == sought_symbol) | |
1785 | { | |
1786 | *sym_loc = expr->where; | |
1787 | return 1; | |
1788 | } | |
1789 | ||
1790 | return 0; | |
1791 | } | |
1792 | ||
1793 | ||
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. */ | |
1796 | ||
1797 | static bool | |
1798 | find_symbol_in_expr (gfc_symbol *sym, gfc_expr *expr, locus *sym_loc) | |
1799 | { | |
1800 | int ret; | |
1801 | ||
1802 | sought_symbol = sym; | |
1803 | ret = gfc_expr_walker (&expr, &expr_is_sought_symbol_ref, sym_loc); | |
1804 | sought_symbol = NULL; | |
1805 | return ret; | |
1806 | } | |
1807 | ||
1808 | ||
6de9cd9a DN |
1809 | /* Recursive array list resolution function. All of the elements must |
1810 | be of the same type. */ | |
1811 | ||
524af0d6 | 1812 | static bool |
b7e75771 | 1813 | resolve_array_list (gfc_constructor_base base) |
6de9cd9a | 1814 | { |
524af0d6 | 1815 | bool t; |
b7e75771 | 1816 | gfc_constructor *c; |
ca27d5ae | 1817 | gfc_iterator *iter; |
6de9cd9a | 1818 | |
524af0d6 | 1819 | t = true; |
6de9cd9a | 1820 | |
b7e75771 | 1821 | for (c = gfc_constructor_first (base); c; c = gfc_constructor_next (c)) |
6de9cd9a | 1822 | { |
ca27d5ae MM |
1823 | iter = c->iterator; |
1824 | if (iter != NULL) | |
1825 | { | |
1826 | gfc_symbol *iter_var; | |
1827 | locus iter_var_loc; | |
8b704316 | 1828 | |
524af0d6 JB |
1829 | if (!gfc_resolve_iterator (iter, false, true)) |
1830 | t = false; | |
ca27d5ae MM |
1831 | |
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)) | |
1836 | { | |
524af0d6 JB |
1837 | if (!gfc_notify_std (GFC_STD_LEGACY, "AC-IMPLIED-DO initial " |
1838 | "expression references control variable " | |
1839 | "at %L", &iter_var_loc)) | |
1840 | t = false; | |
ca27d5ae MM |
1841 | } |
1842 | if (find_symbol_in_expr (iter_var, iter->end, &iter_var_loc)) | |
1843 | { | |
524af0d6 JB |
1844 | if (!gfc_notify_std (GFC_STD_LEGACY, "AC-IMPLIED-DO final " |
1845 | "expression references control variable " | |
1846 | "at %L", &iter_var_loc)) | |
1847 | t = false; | |
ca27d5ae MM |
1848 | } |
1849 | if (find_symbol_in_expr (iter_var, iter->step, &iter_var_loc)) | |
1850 | { | |
524af0d6 JB |
1851 | if (!gfc_notify_std (GFC_STD_LEGACY, "AC-IMPLIED-DO step " |
1852 | "expression references control variable " | |
1853 | "at %L", &iter_var_loc)) | |
1854 | t = false; | |
ca27d5ae MM |
1855 | } |
1856 | } | |
6de9cd9a | 1857 | |
524af0d6 JB |
1858 | if (!gfc_resolve_expr (c->expr)) |
1859 | t = false; | |
8b704316 PT |
1860 | |
1861 | if (UNLIMITED_POLY (c->expr)) | |
1862 | { | |
1863 | gfc_error ("Array constructor value at %L shall not be unlimited " | |
1864 | "polymorphic [F2008: C4106]", &c->expr->where); | |
524af0d6 | 1865 | t = false; |
8b704316 | 1866 | } |
6de9cd9a DN |
1867 | } |
1868 | ||
1869 | return t; | |
1870 | } | |
1871 | ||
88fec49f | 1872 | /* Resolve character array constructor. If it has a specified constant character |
df2fba9e | 1873 | length, pad/truncate the elements here; if the length is not specified and |
88fec49f DK |
1874 | all elements are of compile-time known length, emit an error as this is |
1875 | invalid. */ | |
6de9cd9a | 1876 | |
524af0d6 | 1877 | bool |
65f8144a | 1878 | gfc_resolve_character_array_constructor (gfc_expr *expr) |
df7cc9b5 | 1879 | { |
65f8144a | 1880 | gfc_constructor *p; |
88fec49f | 1881 | int found_length; |
df7cc9b5 FW |
1882 | |
1883 | gcc_assert (expr->expr_type == EXPR_ARRAY); | |
1884 | gcc_assert (expr->ts.type == BT_CHARACTER); | |
1885 | ||
bc21d315 | 1886 | if (expr->ts.u.cl == NULL) |
4a90ae54 | 1887 | { |
b7e75771 JD |
1888 | for (p = gfc_constructor_first (expr->value.constructor); |
1889 | p; p = gfc_constructor_next (p)) | |
bc21d315 | 1890 | if (p->expr->ts.u.cl != NULL) |
1855915a PT |
1891 | { |
1892 | /* Ensure that if there is a char_len around that it is | |
1893 | used; otherwise the middle-end confuses them! */ | |
bc21d315 | 1894 | expr->ts.u.cl = p->expr->ts.u.cl; |
1855915a PT |
1895 | goto got_charlen; |
1896 | } | |
1897 | ||
b76e28c6 | 1898 | expr->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL); |
4a90ae54 FW |
1899 | } |
1900 | ||
1855915a PT |
1901 | got_charlen: |
1902 | ||
88fec49f DK |
1903 | found_length = -1; |
1904 | ||
bc21d315 | 1905 | if (expr->ts.u.cl->length == NULL) |
df7cc9b5 | 1906 | { |
88fec49f DK |
1907 | /* Check that all constant string elements have the same length until |
1908 | we reach the end or find a variable-length one. */ | |
1855915a | 1909 | |
b7e75771 JD |
1910 | for (p = gfc_constructor_first (expr->value.constructor); |
1911 | p; p = gfc_constructor_next (p)) | |
1855915a | 1912 | { |
88fec49f | 1913 | int current_length = -1; |
1855915a PT |
1914 | gfc_ref *ref; |
1915 | for (ref = p->expr->ref; ref; ref = ref->next) | |
1916 | if (ref->type == REF_SUBSTRING | |
65f8144a SK |
1917 | && ref->u.ss.start->expr_type == EXPR_CONSTANT |
1918 | && ref->u.ss.end->expr_type == EXPR_CONSTANT) | |
1855915a PT |
1919 | break; |
1920 | ||
1921 | if (p->expr->expr_type == EXPR_CONSTANT) | |
88fec49f | 1922 | current_length = p->expr->value.character.length; |
1855915a | 1923 | else if (ref) |
65f8144a SK |
1924 | { |
1925 | long j; | |
1926 | j = mpz_get_ui (ref->u.ss.end->value.integer) | |
1927 | - mpz_get_ui (ref->u.ss.start->value.integer) + 1; | |
88fec49f | 1928 | current_length = (int) j; |
65f8144a | 1929 | } |
bc21d315 JW |
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) | |
65f8144a SK |
1932 | { |
1933 | long j; | |
bc21d315 | 1934 | j = mpz_get_si (p->expr->ts.u.cl->length->value.integer); |
88fec49f | 1935 | current_length = (int) j; |
65f8144a | 1936 | } |
1855915a | 1937 | else |
524af0d6 | 1938 | return true; |
df7cc9b5 | 1939 | |
88fec49f DK |
1940 | gcc_assert (current_length != -1); |
1941 | ||
1942 | if (found_length == -1) | |
1943 | found_length = current_length; | |
1944 | else if (found_length != current_length) | |
1945 | { | |
1946 | gfc_error ("Different CHARACTER lengths (%d/%d) in array" | |
1947 | " constructor at %L", found_length, current_length, | |
1948 | &p->expr->where); | |
524af0d6 | 1949 | return false; |
88fec49f DK |
1950 | } |
1951 | ||
1952 | gcc_assert (found_length == current_length); | |
df7cc9b5 | 1953 | } |
88fec49f DK |
1954 | |
1955 | gcc_assert (found_length != -1); | |
1956 | ||
1957 | /* Update the character length of the array constructor. */ | |
b7e75771 JD |
1958 | expr->ts.u.cl->length = gfc_get_int_expr (gfc_default_integer_kind, |
1959 | NULL, found_length); | |
df7cc9b5 | 1960 | } |
8b704316 | 1961 | else |
c03fc95d DK |
1962 | { |
1963 | /* We've got a character length specified. It should be an integer, | |
1964 | otherwise an error is signalled elsewhere. */ | |
bc21d315 | 1965 | gcc_assert (expr->ts.u.cl->length); |
c03fc95d DK |
1966 | |
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. */ | |
bc21d315 | 1970 | gfc_extract_int (expr->ts.u.cl->length, &found_length); |
88fec49f | 1971 | |
df2fba9e | 1972 | /* Now pad/truncate the elements accordingly to the specified character |
88fec49f DK |
1973 | length. This is ok inside this conditional, as in the case above |
1974 | (without typespec) all elements are verified to have the same length | |
1975 | anyway. */ | |
1976 | if (found_length != -1) | |
b7e75771 JD |
1977 | for (p = gfc_constructor_first (expr->value.constructor); |
1978 | p; p = gfc_constructor_next (p)) | |
d2848082 DK |
1979 | if (p->expr->expr_type == EXPR_CONSTANT) |
1980 | { | |
1981 | gfc_expr *cl = NULL; | |
1982 | int current_length = -1; | |
1983 | bool has_ts; | |
1984 | ||
bc21d315 | 1985 | if (p->expr->ts.u.cl && p->expr->ts.u.cl->length) |
d2848082 | 1986 | { |
bc21d315 | 1987 | cl = p->expr->ts.u.cl->length; |
d2848082 DK |
1988 | gfc_extract_int (cl, ¤t_length); |
1989 | } | |
1990 | ||
1991 | /* If gfc_extract_int above set current_length, we implicitly | |
1992 | know the type is BT_INTEGER and it's EXPR_CONSTANT. */ | |
1993 | ||
fc2655fb | 1994 | has_ts = expr->ts.u.cl->length_from_typespec; |
d2848082 DK |
1995 | |
1996 | if (! cl | |
a48a9173 | 1997 | || (current_length != -1 && current_length != found_length)) |
d2848082 DK |
1998 | gfc_set_constant_character_len (found_length, p->expr, |
1999 | has_ts ? -1 : found_length); | |
2000 | } | |
c03fc95d DK |
2001 | } |
2002 | ||
524af0d6 | 2003 | return true; |
df7cc9b5 FW |
2004 | } |
2005 | ||
65f8144a | 2006 | |
df7cc9b5 | 2007 | /* Resolve all of the expressions in an array list. */ |
6de9cd9a | 2008 | |
524af0d6 | 2009 | bool |
65f8144a | 2010 | gfc_resolve_array_constructor (gfc_expr *expr) |
6de9cd9a | 2011 | { |
524af0d6 | 2012 | bool t; |
6de9cd9a DN |
2013 | |
2014 | t = resolve_array_list (expr->value.constructor); | |
524af0d6 | 2015 | if (t) |
6de9cd9a | 2016 | t = gfc_check_constructor_type (expr); |
88fec49f DK |
2017 | |
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. */ | |
6de9cd9a DN |
2021 | |
2022 | return t; | |
2023 | } | |
2024 | ||
2025 | ||
2026 | /* Copy an iterator structure. */ | |
2027 | ||
b7e75771 JD |
2028 | gfc_iterator * |
2029 | gfc_copy_iterator (gfc_iterator *src) | |
6de9cd9a DN |
2030 | { |
2031 | gfc_iterator *dest; | |
2032 | ||
2033 | if (src == NULL) | |
2034 | return NULL; | |
2035 | ||
2036 | dest = gfc_get_iterator (); | |
2037 | ||
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); | |
2042 | ||
2043 | return dest; | |
2044 | } | |
2045 | ||
2046 | ||
6de9cd9a DN |
2047 | /********* Subroutines for determining the size of an array *********/ |
2048 | ||
1f2959f0 | 2049 | /* These are needed just to accommodate RESHAPE(). There are no |
6de9cd9a | 2050 | diagnostics here, we just return a negative number if something |
f7b529fa | 2051 | goes wrong. */ |
6de9cd9a DN |
2052 | |
2053 | ||
2054 | /* Get the size of single dimension of an array specification. The | |
2055 | array is guaranteed to be one dimensional. */ | |
2056 | ||
524af0d6 | 2057 | bool |
65f8144a | 2058 | spec_dimen_size (gfc_array_spec *as, int dimen, mpz_t *result) |
6de9cd9a | 2059 | { |
6de9cd9a | 2060 | if (as == NULL) |
524af0d6 | 2061 | return false; |
6de9cd9a DN |
2062 | |
2063 | if (dimen < 0 || dimen > as->rank - 1) | |
2064 | gfc_internal_error ("spec_dimen_size(): Bad dimension"); | |
2065 | ||
2066 | if (as->type != AS_EXPLICIT | |
2067 | || as->lower[dimen]->expr_type != EXPR_CONSTANT | |
1505f3b5 JD |
2068 | || as->upper[dimen]->expr_type != EXPR_CONSTANT |
2069 | || as->lower[dimen]->ts.type != BT_INTEGER | |
2070 | || as->upper[dimen]->ts.type != BT_INTEGER) | |
524af0d6 | 2071 | return false; |
6de9cd9a DN |
2072 | |
2073 | mpz_init (*result); | |
2074 | ||
2075 | mpz_sub (*result, as->upper[dimen]->value.integer, | |
2076 | as->lower[dimen]->value.integer); | |
2077 | ||
2078 | mpz_add_ui (*result, *result, 1); | |
2079 | ||
524af0d6 | 2080 | return true; |
6de9cd9a DN |
2081 | } |
2082 | ||
2083 | ||
524af0d6 | 2084 | bool |
65f8144a | 2085 | spec_size (gfc_array_spec *as, mpz_t *result) |
6de9cd9a DN |
2086 | { |
2087 | mpz_t size; | |
2088 | int d; | |
2089 | ||
81bcd96b | 2090 | if (!as || as->type == AS_ASSUMED_RANK) |
524af0d6 | 2091 | return false; |
c62c6622 | 2092 | |
6de9cd9a DN |
2093 | mpz_init_set_ui (*result, 1); |
2094 | ||
2095 | for (d = 0; d < as->rank; d++) | |
2096 | { | |
524af0d6 | 2097 | if (!spec_dimen_size (as, d, &size)) |
6de9cd9a DN |
2098 | { |
2099 | mpz_clear (*result); | |
524af0d6 | 2100 | return false; |
6de9cd9a DN |
2101 | } |
2102 | ||
2103 | mpz_mul (*result, *result, size); | |
2104 | mpz_clear (size); | |
2105 | } | |
2106 | ||
524af0d6 | 2107 | return true; |
6de9cd9a DN |
2108 | } |
2109 | ||
2110 | ||
ee247636 TK |
2111 | /* Get the number of elements in an array section. Optionally, also supply |
2112 | the end value. */ | |
6de9cd9a | 2113 | |
524af0d6 | 2114 | bool |
ee247636 | 2115 | gfc_ref_dimen_size (gfc_array_ref *ar, int dimen, mpz_t *result, mpz_t *end) |
6de9cd9a DN |
2116 | { |
2117 | mpz_t upper, lower, stride; | |
8cd61b3c | 2118 | mpz_t diff; |
524af0d6 | 2119 | bool t; |
6de9cd9a DN |
2120 | |
2121 | if (dimen < 0 || ar == NULL || dimen > ar->dimen - 1) | |
543af7ab | 2122 | gfc_internal_error ("gfc_ref_dimen_size(): Bad dimension"); |
6de9cd9a DN |
2123 | |
2124 | switch (ar->dimen_type[dimen]) | |
2125 | { | |
2126 | case DIMEN_ELEMENT: | |
2127 | mpz_init (*result); | |
2128 | mpz_set_ui (*result, 1); | |
524af0d6 | 2129 | t = true; |
6de9cd9a DN |
2130 | break; |
2131 | ||
2132 | case DIMEN_VECTOR: | |
2133 | t = gfc_array_size (ar->start[dimen], result); /* Recurse! */ | |
2134 | break; | |
2135 | ||
2136 | case DIMEN_RANGE: | |
8cd61b3c TK |
2137 | |
2138 | mpz_init (stride); | |
2139 | ||
2140 | if (ar->stride[dimen] == NULL) | |
2141 | mpz_set_ui (stride, 1); | |
2142 | else | |
2143 | { | |
2144 | if (ar->stride[dimen]->expr_type != EXPR_CONSTANT) | |
2145 | { | |
2146 | mpz_clear (stride); | |
2147 | return false; | |
2148 | } | |
2149 | mpz_set (stride, ar->stride[dimen]->value.integer); | |
2150 | } | |
2151 | ||
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 | |
2155 | ||
2156 | subroutine foo(a,n) | |
2157 | real a(1:n) | |
2158 | n = 3 | |
2159 | print *,size(a(n-1:)) | |
2160 | ||
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. */ | |
2164 | ||
2165 | if (end == NULL) | |
2166 | { | |
2167 | bool use_dep; | |
2168 | ||
2169 | use_dep = gfc_dep_difference (ar->end[dimen], ar->start[dimen], | |
2170 | &diff); | |
2171 | if (!use_dep && ar->end[dimen] == NULL && ar->start[dimen] == NULL) | |
2172 | use_dep = gfc_dep_difference (ar->as->upper[dimen], | |
2173 | ar->as->lower[dimen], &diff); | |
2174 | ||
2175 | if (use_dep) | |
2176 | { | |
2177 | mpz_init (*result); | |
2178 | mpz_add (*result, diff, stride); | |
2179 | mpz_div (*result, *result, stride); | |
2180 | if (mpz_cmp_ui (*result, 0) < 0) | |
2181 | mpz_set_ui (*result, 0); | |
2182 | ||
2183 | mpz_clear (stride); | |
2184 | mpz_clear (diff); | |
2185 | return true; | |
2186 | } | |
2187 | ||
2188 | } | |
2189 | ||
2190 | /* Constant-only code here, which covers more cases | |
2191 | like a(:4) etc. */ | |
6de9cd9a DN |
2192 | mpz_init (upper); |
2193 | mpz_init (lower); | |
524af0d6 | 2194 | t = false; |
6de9cd9a DN |
2195 | |
2196 | if (ar->start[dimen] == NULL) | |
2197 | { | |
2198 | if (ar->as->lower[dimen] == NULL | |
2199 | || ar->as->lower[dimen]->expr_type != EXPR_CONSTANT) | |
2200 | goto cleanup; | |
2201 | mpz_set (lower, ar->as->lower[dimen]->value.integer); | |
2202 | } | |
2203 | else | |
2204 | { | |
2205 | if (ar->start[dimen]->expr_type != EXPR_CONSTANT) | |
2206 | goto cleanup; | |
2207 | mpz_set (lower, ar->start[dimen]->value.integer); | |
2208 | } | |
2209 | ||
2210 | if (ar->end[dimen] == NULL) | |
2211 | { | |
2212 | if (ar->as->upper[dimen] == NULL | |
2213 | || ar->as->upper[dimen]->expr_type != EXPR_CONSTANT) | |
2214 | goto cleanup; | |
2215 | mpz_set (upper, ar->as->upper[dimen]->value.integer); | |
2216 | } | |
2217 | else | |
2218 | { | |
2219 | if (ar->end[dimen]->expr_type != EXPR_CONSTANT) | |
2220 | goto cleanup; | |
2221 | mpz_set (upper, ar->end[dimen]->value.integer); | |
2222 | } | |
2223 | ||
6de9cd9a DN |
2224 | mpz_init (*result); |
2225 | mpz_sub (*result, upper, lower); | |
2226 | mpz_add (*result, *result, stride); | |
2227 | mpz_div (*result, *result, stride); | |
2228 | ||
2229 | /* Zero stride caught earlier. */ | |
2230 | if (mpz_cmp_ui (*result, 0) < 0) | |
2231 | mpz_set_ui (*result, 0); | |
524af0d6 | 2232 | t = true; |
6de9cd9a | 2233 | |
ee247636 TK |
2234 | if (end) |
2235 | { | |
2236 | mpz_init (*end); | |
2237 | ||
2238 | mpz_sub_ui (*end, *result, 1UL); | |
2239 | mpz_mul (*end, *end, stride); | |
2240 | mpz_add (*end, *end, lower); | |
2241 | } | |
2242 | ||
6de9cd9a DN |
2243 | cleanup: |
2244 | mpz_clear (upper); | |
2245 | mpz_clear (lower); | |
2246 | mpz_clear (stride); | |
2247 | return t; | |
2248 | ||
2249 | default: | |
543af7ab | 2250 | gfc_internal_error ("gfc_ref_dimen_size(): Bad dimen_type"); |
6de9cd9a DN |
2251 | } |
2252 | ||
2253 | return t; | |
2254 | } | |
2255 | ||
2256 | ||
524af0d6 | 2257 | static bool |
65f8144a | 2258 | ref_size (gfc_array_ref *ar, mpz_t *result) |
6de9cd9a DN |
2259 | { |
2260 | mpz_t size; | |
2261 | int d; | |
2262 | ||
2263 | mpz_init_set_ui (*result, 1); | |
2264 | ||
2265 | for (d = 0; d < ar->dimen; d++) | |
2266 | { | |
524af0d6 | 2267 | if (!gfc_ref_dimen_size (ar, d, &size, NULL)) |
6de9cd9a DN |
2268 | { |
2269 | mpz_clear (*result); | |
524af0d6 | 2270 | return false; |
6de9cd9a DN |
2271 | } |
2272 | ||
2273 | mpz_mul (*result, *result, size); | |
2274 | mpz_clear (size); | |
2275 | } | |
2276 | ||
524af0d6 | 2277 | return true; |
6de9cd9a DN |
2278 | } |
2279 | ||
2280 | ||
2281 | /* Given an array expression and a dimension, figure out how many | |
524af0d6 JB |
2282 | elements it has along that dimension. Returns true if we were |
2283 | able to return a result in the 'result' variable, false | |
6de9cd9a DN |
2284 | otherwise. */ |
2285 | ||
524af0d6 | 2286 | bool |
65f8144a | 2287 | gfc_array_dimen_size (gfc_expr *array, int dimen, mpz_t *result) |
6de9cd9a DN |
2288 | { |
2289 | gfc_ref *ref; | |
2290 | int i; | |
2291 | ||
fc2655fb TB |
2292 | gcc_assert (array != NULL); |
2293 | ||
c49ea23d | 2294 | if (array->ts.type == BT_CLASS) |
524af0d6 | 2295 | return false; |
c49ea23d | 2296 | |
c62c6622 | 2297 | if (array->rank == -1) |
524af0d6 | 2298 | return false; |
c62c6622 | 2299 | |
fc2655fb | 2300 | if (dimen < 0 || dimen > array->rank - 1) |
6de9cd9a DN |
2301 | gfc_internal_error ("gfc_array_dimen_size(): Bad dimension"); |
2302 | ||
2303 | switch (array->expr_type) | |
2304 | { | |
2305 | case EXPR_VARIABLE: | |
2306 | case EXPR_FUNCTION: | |
2307 | for (ref = array->ref; ref; ref = ref->next) | |
2308 | { | |
2309 | if (ref->type != REF_ARRAY) | |
2310 | continue; | |
2311 | ||
2312 | if (ref->u.ar.type == AR_FULL) | |
2313 | return spec_dimen_size (ref->u.ar.as, dimen, result); | |
2314 | ||
2315 | if (ref->u.ar.type == AR_SECTION) | |
2316 | { | |
2317 | for (i = 0; dimen >= 0; i++) | |
2318 | if (ref->u.ar.dimen_type[i] != DIMEN_ELEMENT) | |
2319 | dimen--; | |
2320 | ||
ee247636 | 2321 | return gfc_ref_dimen_size (&ref->u.ar, i - 1, result, NULL); |
6de9cd9a DN |
2322 | } |
2323 | } | |
2324 | ||
e15e9be3 PT |
2325 | if (array->shape && array->shape[dimen]) |
2326 | { | |
2327 | mpz_init_set (*result, array->shape[dimen]); | |
524af0d6 | 2328 | return true; |
e15e9be3 PT |
2329 | } |
2330 | ||
23f6293e | 2331 | if (array->symtree->n.sym->attr.generic |
03d838ba | 2332 | && array->value.function.esym != NULL) |
23f6293e | 2333 | { |
524af0d6 JB |
2334 | if (!spec_dimen_size (array->value.function.esym->as, dimen, result)) |
2335 | return false; | |
23f6293e | 2336 | } |
524af0d6 JB |
2337 | else if (!spec_dimen_size (array->symtree->n.sym->as, dimen, result)) |
2338 | return false; | |
6de9cd9a DN |
2339 | |
2340 | break; | |
2341 | ||
2342 | case EXPR_ARRAY: | |
2343 | if (array->shape == NULL) { | |
2344 | /* Expressions with rank > 1 should have "shape" properly set */ | |
2345 | if ( array->rank != 1 ) | |
2346 | gfc_internal_error ("gfc_array_dimen_size(): Bad EXPR_ARRAY expr"); | |
2347 | return gfc_array_size(array, result); | |
2348 | } | |
2349 | ||
2350 | /* Fall through */ | |
2351 | default: | |
2352 | if (array->shape == NULL) | |
524af0d6 | 2353 | return false; |
6de9cd9a DN |
2354 | |
2355 | mpz_init_set (*result, array->shape[dimen]); | |
2356 | ||
2357 | break; | |
2358 | } | |
2359 | ||
524af0d6 | 2360 | return true; |
6de9cd9a DN |
2361 | } |
2362 | ||
2363 | ||
2364 | /* Given an array expression, figure out how many elements are in the | |
524af0d6 JB |
2365 | array. Returns true if this is possible, and sets the 'result' |
2366 | variable. Otherwise returns false. */ | |
6de9cd9a | 2367 | |
524af0d6 | 2368 | bool |
65f8144a | 2369 | gfc_array_size (gfc_expr *array, mpz_t *result) |
6de9cd9a DN |
2370 | { |
2371 | expand_info expand_save; | |
2372 | gfc_ref *ref; | |
a3d3c0f5 | 2373 | int i; |
524af0d6 | 2374 | bool t; |
6de9cd9a | 2375 | |
c49ea23d | 2376 | if (array->ts.type == BT_CLASS) |
524af0d6 | 2377 | return false; |
c49ea23d | 2378 | |
6de9cd9a DN |
2379 | switch (array->expr_type) |
2380 | { | |
2381 | case EXPR_ARRAY: | |
a3d3c0f5 | 2382 | gfc_push_suppress_errors (); |
6de9cd9a DN |
2383 | |
2384 | expand_save = current_expand; | |
2385 | ||
2386 | current_expand.count = result; | |
2387 | mpz_init_set_ui (*result, 0); | |
2388 | ||
2389 | current_expand.expand_work_function = count_elements; | |
2390 | iter_stack = NULL; | |
2391 | ||
2392 | t = expand_constructor (array->value.constructor); | |
a3d3c0f5 DK |
2393 | |
2394 | gfc_pop_suppress_errors (); | |
6de9cd9a | 2395 | |
524af0d6 | 2396 | if (!t) |
6de9cd9a DN |
2397 | mpz_clear (*result); |
2398 | current_expand = expand_save; | |
2399 | return t; | |
2400 | ||
2401 | case EXPR_VARIABLE: | |
2402 | for (ref = array->ref; ref; ref = ref->next) | |
2403 | { | |
2404 | if (ref->type != REF_ARRAY) | |
2405 | continue; | |
2406 | ||
2407 | if (ref->u.ar.type == AR_FULL) | |
2408 | return spec_size (ref->u.ar.as, result); | |
2409 | ||
2410 | if (ref->u.ar.type == AR_SECTION) | |
2411 | return ref_size (&ref->u.ar, result); | |
2412 | } | |
2413 | ||
2414 | return spec_size (array->symtree->n.sym->as, result); | |
2415 | ||
2416 | ||
2417 | default: | |
2418 | if (array->rank == 0 || array->shape == NULL) | |
524af0d6 | 2419 | return false; |
6de9cd9a DN |
2420 | |
2421 | mpz_init_set_ui (*result, 1); | |
2422 | ||
2423 | for (i = 0; i < array->rank; i++) | |
2424 | mpz_mul (*result, *result, array->shape[i]); | |
2425 | ||
2426 | break; | |
2427 | } | |
2428 | ||
524af0d6 | 2429 | return true; |
6de9cd9a DN |
2430 | } |
2431 | ||
2432 | ||
2433 | /* Given an array reference, return the shape of the reference in an | |
2434 | array of mpz_t integers. */ | |
2435 | ||
524af0d6 | 2436 | bool |
65f8144a | 2437 | gfc_array_ref_shape (gfc_array_ref *ar, mpz_t *shape) |
6de9cd9a DN |
2438 | { |
2439 | int d; | |
2440 | int i; | |
2441 | ||
2442 | d = 0; | |
2443 | ||
2444 | switch (ar->type) | |
2445 | { | |
2446 | case AR_FULL: | |
2447 | for (; d < ar->as->rank; d++) | |
524af0d6 | 2448 | if (!spec_dimen_size (ar->as, d, &shape[d])) |
6de9cd9a DN |
2449 | goto cleanup; |
2450 | ||
524af0d6 | 2451 | return true; |
6de9cd9a DN |
2452 | |
2453 | case AR_SECTION: | |
2454 | for (i = 0; i < ar->dimen; i++) | |
2455 | { | |
2456 | if (ar->dimen_type[i] != DIMEN_ELEMENT) | |
2457 | { | |
524af0d6 | 2458 | if (!gfc_ref_dimen_size (ar, i, &shape[d], NULL)) |
6de9cd9a DN |
2459 | goto cleanup; |
2460 | d++; | |
2461 | } | |
2462 | } | |
2463 | ||
524af0d6 | 2464 | return true; |
6de9cd9a DN |
2465 | |
2466 | default: | |
2467 | break; | |
2468 | } | |
2469 | ||
2470 | cleanup: | |
7d7212ec | 2471 | gfc_clear_shape (shape, d); |
524af0d6 | 2472 | return false; |
6de9cd9a DN |
2473 | } |
2474 | ||
2475 | ||
2476 | /* Given an array expression, find the array reference structure that | |
2477 | characterizes the reference. */ | |
2478 | ||
2479 | gfc_array_ref * | |
65f8144a | 2480 | gfc_find_array_ref (gfc_expr *e) |
6de9cd9a DN |
2481 | { |
2482 | gfc_ref *ref; | |
2483 | ||
2484 | for (ref = e->ref; ref; ref = ref->next) | |
2485 | if (ref->type == REF_ARRAY | |
db171167 | 2486 | && (ref->u.ar.type == AR_FULL || ref->u.ar.type == AR_SECTION)) |
6de9cd9a DN |
2487 | break; |
2488 | ||
2489 | if (ref == NULL) | |
2490 | gfc_internal_error ("gfc_find_array_ref(): No ref found"); | |
2491 | ||
2492 | return &ref->u.ar; | |
2493 | } | |
4077d207 TS |
2494 | |
2495 | ||
2496 | /* Find out if an array shape is known at compile time. */ | |
2497 | ||
2498 | int | |
2499 | gfc_is_compile_time_shape (gfc_array_spec *as) | |
2500 | { | |
2501 | int i; | |
2502 | ||
2503 | if (as->type != AS_EXPLICIT) | |
2504 | return 0; | |
2505 | ||
2506 | for (i = 0; i < as->rank; i++) | |
2507 | if (!gfc_is_constant_expr (as->lower[i]) | |
2508 | || !gfc_is_constant_expr (as->upper[i])) | |
2509 | return 0; | |
2510 | ||
2511 | return 1; | |
2512 | } |