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