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