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