Line data Source code
1 : /* Array things
2 : Copyright (C) 2000-2026 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 192102 : gfc_copy_array_ref (gfc_array_ref *src)
36 : {
37 192102 : gfc_array_ref *dest;
38 192102 : int i;
39 :
40 192102 : if (src == NULL)
41 : return NULL;
42 :
43 192102 : dest = gfc_get_array_ref ();
44 :
45 192102 : *dest = *src;
46 :
47 3073632 : for (i = 0; i < GFC_MAX_DIMENSIONS; i++)
48 : {
49 2881530 : dest->start[i] = gfc_copy_expr (src->start[i]);
50 2881530 : dest->end[i] = gfc_copy_expr (src->end[i]);
51 2881530 : dest->stride[i] = gfc_copy_expr (src->stride[i]);
52 : }
53 :
54 192102 : dest->stat = gfc_copy_expr (src->stat);
55 192102 : dest->team = gfc_copy_expr (src->team);
56 :
57 192102 : 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 381910 : match_subscript (gfc_array_ref *ar, int init, bool match_star)
69 : {
70 381910 : match m = MATCH_ERROR;
71 381910 : bool star = false;
72 381910 : int i;
73 381910 : bool saw_boz = false;
74 :
75 381910 : i = ar->dimen + ar->codimen;
76 :
77 381910 : gfc_gobble_whitespace ();
78 381910 : ar->c_where[i] = gfc_current_locus;
79 381910 : 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 381910 : ar->dimen_type[i] = DIMEN_UNKNOWN;
86 :
87 381910 : if (gfc_match_char (':') == MATCH_YES)
88 44813 : goto end_element;
89 :
90 : /* Get start element. */
91 337097 : if (match_star && (m = gfc_match_char ('*')) == MATCH_YES)
92 : star = true;
93 :
94 337097 : if (!star && init)
95 1636 : m = gfc_match_init_expr (&ar->start[i]);
96 335461 : else if (!star)
97 334490 : m = gfc_match_expr (&ar->start[i]);
98 :
99 337097 : 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 337097 : if (m == MATCH_NO)
106 4 : gfc_error ("Expected array subscript at %C");
107 337097 : if (m != MATCH_YES)
108 : return MATCH_ERROR;
109 :
110 337087 : if (gfc_match_char (':') == MATCH_NO)
111 300477 : goto matched;
112 :
113 36610 : 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 36610 : end_element:
122 81423 : ar->dimen_type[i] = DIMEN_RANGE;
123 :
124 81423 : if (match_star && (m = gfc_match_char ('*')) == MATCH_YES)
125 : star = true;
126 81213 : else if (init)
127 371 : m = gfc_match_init_expr (&ar->end[i]);
128 : else
129 80842 : m = gfc_match_expr (&ar->end[i]);
130 :
131 81423 : 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 81423 : if (m == MATCH_ERROR)
138 : return MATCH_ERROR;
139 :
140 81423 : 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 81421 : if (gfc_match_char (':') == MATCH_YES)
149 : {
150 17260 : if (star)
151 : {
152 0 : gfc_error ("Strides not allowed in coarray subscript at %C");
153 0 : return MATCH_ERROR;
154 : }
155 :
156 17260 : m = init ? gfc_match_init_expr (&ar->stride[i])
157 17259 : : gfc_match_expr (&ar->stride[i]);
158 :
159 17260 : 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 17260 : if (m == MATCH_NO)
166 0 : gfc_error ("Expected array subscript stride at %C");
167 17260 : if (m != MATCH_YES)
168 : return MATCH_ERROR;
169 : }
170 :
171 64161 : matched:
172 381898 : if (star)
173 1179 : ar->dimen_type[i] = DIMEN_STAR;
174 :
175 381898 : return (saw_boz ? MATCH_ERROR : MATCH_YES);
176 : }
177 :
178 : /** Match one of TEAM=, TEAM_NUMBER= or STAT=. */
179 :
180 : match
181 5994 : match_team_or_stat (gfc_array_ref *ar)
182 : {
183 5994 : gfc_expr *tmp;
184 5994 : bool team_error = false;
185 :
186 5994 : if (gfc_match (" team = %e", &tmp) == MATCH_YES)
187 : {
188 75 : if (ar->team == NULL && ar->team_type == TEAM_UNSET)
189 : {
190 69 : ar->team = tmp;
191 69 : ar->team_type = TEAM_TEAM;
192 : }
193 6 : else if (ar->team_type == TEAM_TEAM)
194 : {
195 3 : gfc_error ("Duplicate TEAM= attribute in %C");
196 3 : return MATCH_ERROR;
197 : }
198 : else
199 : team_error = true;
200 : }
201 5919 : else if (gfc_match (" team_number = %e", &tmp) == MATCH_YES)
202 : {
203 102 : if (!gfc_notify_std (GFC_STD_F2018, "TEAM_NUMBER= not supported at %C"))
204 : return MATCH_ERROR;
205 99 : if (ar->team == NULL && ar->team_type == TEAM_UNSET)
206 : {
207 96 : ar->team = tmp;
208 96 : ar->team_type = TEAM_NUMBER;
209 : }
210 3 : else if (ar->team_type == TEAM_NUMBER)
211 : {
212 3 : gfc_error ("Duplicate TEAM_NUMBER= attribute in %C");
213 3 : return MATCH_ERROR;
214 : }
215 : else
216 : team_error = true;
217 : }
218 5817 : else if (gfc_match (" stat = %e", &tmp) == MATCH_YES)
219 : {
220 113 : if (ar->stat == NULL)
221 : {
222 107 : if (gfc_is_coindexed (tmp))
223 : {
224 3 : gfc_error ("Expression in STAT= at %C must not be coindexed");
225 3 : gfc_free_expr (tmp);
226 3 : return MATCH_ERROR;
227 : }
228 104 : ar->stat = tmp;
229 : }
230 : else
231 : {
232 6 : gfc_error ("Duplicate STAT= attribute in %C");
233 6 : return MATCH_ERROR;
234 : }
235 : }
236 : else
237 : return MATCH_NO;
238 :
239 272 : if (ar->team && team_error)
240 : {
241 3 : gfc_error ("Only one of TEAM= or TEAM_NUMBER= may appear in a "
242 : "coarray reference at %C");
243 3 : 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 765748 : gfc_match_array_ref (gfc_array_ref *ar, gfc_array_spec *as, int init,
255 : int corank, bool coarray_only)
256 : {
257 765748 : match m;
258 765748 : bool matched_bracket = false;
259 :
260 765748 : memset (ar, '\0', sizeof (*ar));
261 :
262 765748 : ar->where = gfc_current_locus;
263 765748 : ar->as = as;
264 765748 : ar->type = AR_UNKNOWN;
265 :
266 765748 : if (gfc_match_char ('[') == MATCH_YES)
267 : {
268 3602 : matched_bracket = true;
269 3602 : goto coarray;
270 : }
271 762146 : else if (coarray_only && corank != 0)
272 1833 : goto coarray;
273 :
274 760313 : if (gfc_match_char ('(') != MATCH_YES)
275 : {
276 468835 : ar->type = AR_FULL;
277 468835 : ar->dimen = 0;
278 468835 : if (corank != 0)
279 : {
280 191792 : for (int i = 0; i < GFC_MAX_DIMENSIONS; ++i)
281 179805 : ar->dimen_type[i] = DIMEN_THIS_IMAGE;
282 11987 : ar->codimen = corank;
283 : }
284 : return MATCH_YES;
285 : }
286 :
287 376205 : for (ar->dimen = 0; ar->dimen < GFC_MAX_DIMENSIONS; ar->dimen++)
288 : {
289 376205 : m = match_subscript (ar, init, false);
290 376205 : if (m == MATCH_ERROR)
291 : return MATCH_ERROR;
292 :
293 376192 : if (gfc_match_char (')') == MATCH_YES)
294 : {
295 291453 : ar->dimen++;
296 291453 : goto coarray;
297 : }
298 :
299 84739 : if (gfc_match_char (',') != MATCH_YES)
300 : {
301 12 : gfc_error ("Invalid form of array reference at %C");
302 12 : 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 296888 : coarray:
316 296888 : if (!matched_bracket && gfc_match_char ('[') != MATCH_YES)
317 : {
318 291846 : int dim = coarray_only ? 0 : ar->dimen;
319 291846 : if (dim > 0 || coarray_only)
320 : {
321 291846 : if (corank != 0)
322 : {
323 103968 : for (int i = dim; i < GFC_MAX_DIMENSIONS; ++i)
324 97130 : ar->dimen_type[i] = DIMEN_THIS_IMAGE;
325 6838 : ar->codimen = corank;
326 : }
327 : return MATCH_YES;
328 : }
329 : else
330 : return MATCH_ERROR;
331 : }
332 :
333 5042 : 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 5042 : if (corank == 0)
340 : {
341 0 : gfc_error ("Unexpected coarray designator at %C");
342 0 : return MATCH_ERROR;
343 : }
344 :
345 5042 : ar->team_type = TEAM_UNSET;
346 :
347 5705 : for (ar->codimen = 0; ar->codimen + ar->dimen < GFC_MAX_DIMENSIONS;
348 : ar->codimen++)
349 : {
350 5705 : m = match_subscript (ar, init, true);
351 5705 : if (m == MATCH_ERROR)
352 : return MATCH_ERROR;
353 :
354 5703 : if (gfc_match_char (',') != MATCH_YES)
355 : {
356 4825 : 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 4825 : goto image_selector;
362 : }
363 0 : return MATCH_ERROR;
364 : }
365 878 : 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 876 : m = match_team_or_stat (ar);
373 876 : if (m == MATCH_ERROR)
374 : return MATCH_ERROR;
375 870 : else if (m == MATCH_YES)
376 203 : goto image_selector;
377 :
378 667 : if (gfc_match_char (']') == MATCH_YES)
379 0 : goto rank_check;
380 :
381 667 : 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 5028 : image_selector:
394 5118 : for (;;)
395 : {
396 5118 : m = match_team_or_stat (ar);
397 5118 : if (m == MATCH_ERROR)
398 : return MATCH_ERROR;
399 :
400 5103 : if (gfc_match_char (']') == MATCH_YES)
401 4995 : goto rank_check;
402 :
403 108 : if (gfc_match_char (',') != MATCH_YES)
404 : {
405 18 : gfc_error ("Invalid form of coarray reference at %C");
406 18 : return MATCH_ERROR;
407 : }
408 : }
409 :
410 : return MATCH_ERROR;
411 :
412 4995 : rank_check:
413 4995 : ar->codimen++;
414 4995 : 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 4977 : 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 7655808 : gfc_free_array_spec (gfc_array_spec *as)
437 : {
438 7655808 : int i;
439 :
440 7655808 : if (as == NULL)
441 : return;
442 :
443 456307 : if (as->corank == 0)
444 : {
445 683964 : for (i = 0; i < as->rank; i++)
446 : {
447 231430 : gfc_free_expr (as->lower[i]);
448 231430 : gfc_free_expr (as->upper[i]);
449 : }
450 : }
451 : else
452 : {
453 3773 : int n = as->rank + as->corank - (as->cotype == AS_EXPLICIT ? 1 : 0);
454 8448 : for (i = 0; i < n; i++)
455 : {
456 4675 : gfc_free_expr (as->lower[i]);
457 4675 : gfc_free_expr (as->upper[i]);
458 : }
459 : }
460 :
461 456307 : 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 368177 : resolve_array_bound (gfc_expr *e, int check_constant)
470 : {
471 368177 : if (e == NULL)
472 : return true;
473 :
474 210726 : if (e->ts.type == BT_DERIVED || e->ts.type == BT_CLASS)
475 : {
476 2 : gfc_error ("Derived type or class expression for array bound at %L",
477 : &e->where);
478 2 : return false;
479 : }
480 :
481 210724 : if (!gfc_resolve_expr (e)
482 210724 : || !gfc_specification_expr (e))
483 : return false;
484 :
485 210677 : if (check_constant && !gfc_is_constant_expr (e))
486 : {
487 0 : if (e->expr_type == EXPR_VARIABLE)
488 0 : gfc_error ("Variable %qs at %L in this context must be constant",
489 0 : e->symtree->n.sym->name, &e->where);
490 : else
491 0 : gfc_error ("Expression at %L in this context must be constant",
492 : &e->where);
493 : return false;
494 : }
495 :
496 : return true;
497 : }
498 :
499 :
500 : /* Takes an array specification, resolves the expressions that make up
501 : the shape and make sure everything is integral. */
502 :
503 : bool
504 3094428 : gfc_resolve_array_spec (gfc_array_spec *as, int check_constant)
505 : {
506 3094428 : gfc_expr *e;
507 3094428 : int i;
508 :
509 3094428 : if (as == NULL)
510 : return true;
511 :
512 369238 : if (as->resolved)
513 : return true;
514 :
515 361561 : for (i = 0; i < as->rank + as->corank; i++)
516 : {
517 184094 : if (i == GFC_MAX_DIMENSIONS)
518 : return false;
519 :
520 184091 : e = as->lower[i];
521 184091 : if (!resolve_array_bound (e, check_constant))
522 : return false;
523 :
524 184086 : e = as->upper[i];
525 184086 : if (!resolve_array_bound (e, check_constant))
526 : return false;
527 :
528 184042 : if ((as->lower[i] == NULL) || (as->upper[i] == NULL))
529 107039 : continue;
530 :
531 : /* If the size is negative in this dimension, set it to zero. */
532 77003 : if (as->lower[i]->expr_type == EXPR_CONSTANT
533 76267 : && as->upper[i]->expr_type == EXPR_CONSTANT
534 65012 : && mpz_cmp (as->upper[i]->value.integer,
535 65012 : as->lower[i]->value.integer) < 0)
536 : {
537 1248 : gfc_free_expr (as->upper[i]);
538 1248 : as->upper[i] = gfc_copy_expr (as->lower[i]);
539 1248 : mpz_sub_ui (as->upper[i]->value.integer,
540 1248 : as->upper[i]->value.integer, 1);
541 : }
542 : }
543 :
544 177467 : as->resolved = true;
545 :
546 177467 : return true;
547 : }
548 :
549 :
550 : /* Match a single array element specification. The return values as
551 : well as the upper and lower bounds of the array spec are filled
552 : in according to what we see on the input. The caller makes sure
553 : individual specifications make sense as a whole.
554 :
555 :
556 : Parsed Lower Upper Returned
557 : ------------------------------------
558 : : NULL NULL AS_DEFERRED (*)
559 : x 1 x AS_EXPLICIT
560 : x: x NULL AS_ASSUMED_SHAPE
561 : x:y x y AS_EXPLICIT
562 : x:* x NULL AS_ASSUMED_SIZE
563 : * 1 NULL AS_ASSUMED_SIZE
564 :
565 : (*) For non-pointer dummy arrays this is AS_ASSUMED_SHAPE. This
566 : is fixed during the resolution of formal interfaces.
567 :
568 : Anything else AS_UNKNOWN. */
569 :
570 : static array_type
571 113376 : match_array_element_spec (gfc_array_spec *as)
572 : {
573 113376 : gfc_expr **upper, **lower;
574 113376 : match m;
575 113376 : int rank;
576 113376 : bool is_pdt_template;
577 :
578 113376 : rank = as->rank == -1 ? 0 : as->rank;
579 113376 : lower = &as->lower[rank + as->corank - 1];
580 113376 : upper = &as->upper[rank + as->corank - 1];
581 :
582 113376 : if (gfc_match_char ('*') == MATCH_YES)
583 : {
584 8122 : *lower = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
585 8122 : return AS_ASSUMED_SIZE;
586 : }
587 :
588 105254 : if (gfc_match_char (':') == MATCH_YES)
589 : {
590 41449 : locus old_loc = gfc_current_locus;
591 41449 : if (gfc_match_char ('*') == MATCH_YES)
592 : {
593 : /* F2018:R821: "assumed-implied-spec is [ lower-bound : ] *". */
594 3 : gfc_error ("A lower bound must precede colon in "
595 : "assumed-size array specification at %L", &old_loc);
596 3 : return AS_UNKNOWN;
597 : }
598 : else
599 : {
600 : return AS_DEFERRED;
601 : }
602 : }
603 :
604 63805 : m = gfc_match_expr (upper);
605 63805 : if (m == MATCH_NO)
606 1 : gfc_error ("Expected expression in array specification at %C");
607 63805 : if (m != MATCH_YES)
608 : return AS_UNKNOWN;
609 63803 : if (!gfc_expr_check_typed (*upper, gfc_current_ns, false))
610 : return AS_UNKNOWN;
611 :
612 63796 : if (((*upper)->expr_type == EXPR_CONSTANT
613 63796 : && (*upper)->ts.type != BT_INTEGER) ||
614 : ((*upper)->expr_type == EXPR_FUNCTION
615 743 : && (*upper)->ts.type == BT_UNKNOWN
616 742 : && (*upper)->symtree
617 742 : && strcmp ((*upper)->symtree->name, "null") == 0))
618 : {
619 5 : gfc_error ("Expecting a scalar INTEGER expression at %C, found %s",
620 : gfc_basic_typename ((*upper)->ts.type));
621 5 : return AS_UNKNOWN;
622 : }
623 :
624 127582 : is_pdt_template = gfc_current_block ()
625 57485 : && gfc_current_block ()->attr.pdt_template
626 64361 : && gfc_current_block ()->f2k_derived;
627 :
628 63791 : if ((*upper)->expr_type != EXPR_CONSTANT && is_pdt_template)
629 505 : gfc_correct_parm_expr (gfc_current_block (), upper);
630 :
631 63791 : if (gfc_match_char (':') == MATCH_NO)
632 : {
633 56475 : *lower = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
634 56475 : return AS_EXPLICIT;
635 : }
636 :
637 7316 : *lower = *upper;
638 7316 : *upper = NULL;
639 :
640 7316 : if (gfc_match_char ('*') == MATCH_YES)
641 : return AS_ASSUMED_SIZE;
642 :
643 6801 : m = gfc_match_expr (upper);
644 6801 : if (m == MATCH_ERROR)
645 : return AS_UNKNOWN;
646 6801 : if (m == MATCH_NO)
647 : return AS_ASSUMED_SHAPE;
648 5951 : if (!gfc_expr_check_typed (*upper, gfc_current_ns, false))
649 : return AS_UNKNOWN;
650 :
651 5951 : if (((*upper)->expr_type == EXPR_CONSTANT
652 5951 : && (*upper)->ts.type != BT_INTEGER) ||
653 : ((*upper)->expr_type == EXPR_FUNCTION
654 76 : && (*upper)->ts.type == BT_UNKNOWN
655 76 : && (*upper)->symtree
656 76 : && strcmp ((*upper)->symtree->name, "null") == 0))
657 : {
658 1 : gfc_error ("Expecting a scalar INTEGER expression at %C, found %s",
659 : gfc_basic_typename ((*upper)->ts.type));
660 1 : return AS_UNKNOWN;
661 : }
662 :
663 5950 : if ((*upper)->expr_type != EXPR_CONSTANT && is_pdt_template)
664 12 : gfc_correct_parm_expr (gfc_current_block (), upper);
665 :
666 : return AS_EXPLICIT;
667 : }
668 :
669 :
670 : /* Matches an array specification, incidentally figuring out what sort
671 : it is. Match either a normal array specification, or a coarray spec
672 : or both. Optionally allow [:] for coarrays. */
673 :
674 : match
675 321503 : gfc_match_array_spec (gfc_array_spec **asp, bool match_dim, bool match_codim)
676 : {
677 321503 : array_type current_type;
678 321503 : gfc_array_spec *as;
679 321503 : int i;
680 :
681 321503 : as = gfc_get_array_spec ();
682 :
683 321503 : if (!match_dim)
684 112 : goto coarray;
685 :
686 321391 : if (gfc_match_char ('(') != MATCH_YES)
687 : {
688 234332 : if (!match_codim)
689 1188 : goto done;
690 233144 : goto coarray;
691 : }
692 :
693 87059 : if (gfc_match (" .. )") == MATCH_YES)
694 : {
695 5082 : as->type = AS_ASSUMED_RANK;
696 5082 : as->rank = -1;
697 :
698 5082 : if (!gfc_notify_std (GFC_STD_F2018, "Assumed-rank array at %C"))
699 30 : goto cleanup;
700 :
701 5052 : if (!match_codim)
702 2092 : goto done;
703 2960 : goto coarray;
704 : }
705 :
706 110931 : for (;;)
707 : {
708 110931 : as->rank++;
709 110931 : current_type = match_array_element_spec (as);
710 110931 : if (current_type == AS_UNKNOWN)
711 17 : goto cleanup;
712 :
713 : /* Note that current_type == AS_ASSUMED_SIZE for both assumed-size
714 : and implied-shape specifications. If the rank is at least 2, we can
715 : distinguish between them. But for rank 1, we currently return
716 : ASSUMED_SIZE; this gets adjusted later when we know for sure
717 : whether the symbol parsed is a PARAMETER or not. */
718 :
719 110914 : if (as->rank == 1)
720 : {
721 81961 : as->type = current_type;
722 : }
723 : else
724 28953 : switch (as->type)
725 : { /* See how current spec meshes with the existing. */
726 0 : case AS_UNKNOWN:
727 0 : goto cleanup;
728 :
729 41 : case AS_IMPLIED_SHAPE:
730 41 : if (current_type != AS_ASSUMED_SIZE)
731 : {
732 3 : gfc_error ("Bad array specification for implied-shape"
733 : " array at %C");
734 3 : goto cleanup;
735 : }
736 : break;
737 :
738 17908 : case AS_EXPLICIT:
739 17908 : if (current_type == AS_ASSUMED_SIZE)
740 : {
741 623 : as->type = AS_ASSUMED_SIZE;
742 623 : break;
743 : }
744 :
745 17285 : if (current_type == AS_EXPLICIT)
746 : break;
747 :
748 0 : gfc_error ("Bad array specification for an explicitly shaped "
749 : "array at %C");
750 :
751 0 : goto cleanup;
752 :
753 236 : case AS_ASSUMED_SHAPE:
754 236 : if ((current_type == AS_ASSUMED_SHAPE)
755 236 : || (current_type == AS_DEFERRED))
756 : break;
757 :
758 0 : gfc_error ("Bad array specification for assumed shape "
759 : "array at %C");
760 0 : goto cleanup;
761 :
762 10691 : case AS_DEFERRED:
763 10691 : if (current_type == AS_DEFERRED)
764 : break;
765 :
766 1 : if (current_type == AS_ASSUMED_SHAPE)
767 : {
768 0 : as->type = AS_ASSUMED_SHAPE;
769 0 : break;
770 : }
771 :
772 1 : gfc_error ("Bad specification for deferred shape array at %C");
773 1 : goto cleanup;
774 :
775 77 : case AS_ASSUMED_SIZE:
776 77 : if (as->rank == 2 && current_type == AS_ASSUMED_SIZE)
777 : {
778 75 : as->type = AS_IMPLIED_SHAPE;
779 75 : break;
780 : }
781 :
782 2 : gfc_error ("Bad specification for assumed size array at %C");
783 2 : goto cleanup;
784 :
785 0 : case AS_ASSUMED_RANK:
786 0 : gcc_unreachable ();
787 : }
788 :
789 110908 : if (gfc_match_char (')') == MATCH_YES)
790 : break;
791 :
792 28960 : if (gfc_match_char (',') != MATCH_YES)
793 : {
794 2 : gfc_error ("Expected another dimension in array declaration at %C");
795 2 : goto cleanup;
796 : }
797 :
798 28958 : if (as->rank + as->corank >= GFC_MAX_DIMENSIONS)
799 : {
800 2 : gfc_error ("Array specification at %C has more than %d dimensions",
801 : GFC_MAX_DIMENSIONS);
802 2 : goto cleanup;
803 : }
804 :
805 28956 : if (as->corank + as->rank >= 7
806 28956 : && !gfc_notify_std (GFC_STD_F2008, "Array specification at %C "
807 : "with more than 7 dimensions"))
808 2 : goto cleanup;
809 : }
810 :
811 81948 : if (!match_codim)
812 19413 : goto done;
813 :
814 62535 : coarray:
815 298751 : if (gfc_match_char ('[') != MATCH_YES)
816 296957 : goto done;
817 :
818 1794 : if (!gfc_notify_std (GFC_STD_F2008, "Coarray declaration at %C"))
819 3 : goto cleanup;
820 :
821 1791 : if (flag_coarray == GFC_FCOARRAY_NONE)
822 : {
823 1 : gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to enable");
824 : goto cleanup;
825 : }
826 :
827 1790 : if (as->rank >= GFC_MAX_DIMENSIONS)
828 : {
829 0 : gfc_error ("Array specification at %C has more than %d "
830 : "dimensions", GFC_MAX_DIMENSIONS);
831 0 : goto cleanup;
832 : }
833 :
834 2445 : for (;;)
835 : {
836 2445 : as->corank++;
837 2445 : current_type = match_array_element_spec (as);
838 :
839 2445 : if (current_type == AS_UNKNOWN)
840 1 : goto cleanup;
841 :
842 2444 : if (as->corank == 1)
843 1789 : as->cotype = current_type;
844 : else
845 655 : switch (as->cotype)
846 : { /* See how current spec meshes with the existing. */
847 0 : case AS_IMPLIED_SHAPE:
848 0 : case AS_UNKNOWN:
849 0 : goto cleanup;
850 :
851 429 : case AS_EXPLICIT:
852 429 : if (current_type == AS_ASSUMED_SIZE)
853 : {
854 224 : as->cotype = AS_ASSUMED_SIZE;
855 224 : break;
856 : }
857 :
858 205 : if (current_type == AS_EXPLICIT)
859 : break;
860 :
861 0 : gfc_error ("Bad array specification for an explicitly "
862 : "shaped array at %C");
863 :
864 0 : goto cleanup;
865 :
866 0 : case AS_ASSUMED_SHAPE:
867 0 : if ((current_type == AS_ASSUMED_SHAPE)
868 0 : || (current_type == AS_DEFERRED))
869 : break;
870 :
871 0 : gfc_error ("Bad array specification for assumed shape "
872 : "array at %C");
873 0 : goto cleanup;
874 :
875 226 : case AS_DEFERRED:
876 226 : if (current_type == AS_DEFERRED)
877 : break;
878 :
879 0 : if (current_type == AS_ASSUMED_SHAPE)
880 : {
881 0 : as->cotype = AS_ASSUMED_SHAPE;
882 0 : break;
883 : }
884 :
885 0 : gfc_error ("Bad specification for deferred shape array at %C");
886 0 : goto cleanup;
887 :
888 0 : case AS_ASSUMED_SIZE:
889 0 : gfc_error ("Bad specification for assumed size array at %C");
890 0 : goto cleanup;
891 :
892 0 : case AS_ASSUMED_RANK:
893 0 : gcc_unreachable ();
894 : }
895 :
896 2444 : if (gfc_match_char (']') == MATCH_YES)
897 : break;
898 :
899 657 : if (gfc_match_char (',') != MATCH_YES)
900 : {
901 0 : gfc_error ("Expected another dimension in array declaration at %C");
902 0 : goto cleanup;
903 : }
904 :
905 657 : if (as->rank + as->corank >= GFC_MAX_DIMENSIONS)
906 : {
907 2 : gfc_error ("Array specification at %C has more than %d "
908 : "dimensions", GFC_MAX_DIMENSIONS);
909 2 : goto cleanup;
910 : }
911 : }
912 :
913 1787 : if (current_type == AS_EXPLICIT)
914 : {
915 1 : gfc_error ("Upper bound of last coarray dimension must be %<*%> at %C");
916 1 : goto cleanup;
917 : }
918 :
919 1786 : if (as->cotype == AS_ASSUMED_SIZE)
920 1005 : as->cotype = AS_EXPLICIT;
921 :
922 1786 : if (as->rank == 0)
923 1053 : as->type = as->cotype;
924 :
925 733 : done:
926 321436 : if (as->rank == 0 && as->corank == 0)
927 : {
928 233384 : *asp = NULL;
929 233384 : gfc_free_array_spec (as);
930 233384 : return MATCH_NO;
931 : }
932 :
933 : /* If a lower bounds of an assumed shape array is blank, put in one. */
934 88052 : if (as->type == AS_ASSUMED_SHAPE)
935 : {
936 1470 : for (i = 0; i < as->rank + as->corank; i++)
937 : {
938 856 : if (as->lower[i] == NULL)
939 1 : as->lower[i] = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
940 : }
941 : }
942 :
943 88052 : *asp = as;
944 :
945 88052 : return MATCH_YES;
946 :
947 66 : cleanup:
948 : /* Something went wrong. */
949 66 : gfc_free_array_spec (as);
950 66 : return MATCH_ERROR;
951 : }
952 :
953 : /* Given a symbol and an array specification, modify the symbol to
954 : have that array specification. The error locus is needed in case
955 : something goes wrong. On failure, the caller must free the spec. */
956 :
957 : bool
958 282960 : gfc_set_array_spec (gfc_symbol *sym, gfc_array_spec *as, locus *error_loc)
959 : {
960 282960 : int i;
961 282960 : symbol_attribute *attr;
962 :
963 282960 : if (as == NULL)
964 : return true;
965 :
966 : /* If the symbol corresponds to a submodule module procedure the array spec is
967 : already set, so do not attempt to set it again here. */
968 83393 : attr = &sym->attr;
969 83393 : if (gfc_submodule_procedure(attr))
970 : return true;
971 :
972 83393 : if (as->rank
973 83393 : && !gfc_add_dimension (&sym->attr, sym->name, error_loc))
974 : return false;
975 :
976 83388 : if (as->corank
977 83388 : && !gfc_add_codimension (&sym->attr, sym->name, error_loc))
978 : return false;
979 :
980 83383 : if (sym->as == NULL)
981 : {
982 83357 : sym->as = as;
983 83357 : return true;
984 : }
985 :
986 26 : if ((sym->as->type == AS_ASSUMED_RANK && as->corank)
987 24 : || (as->type == AS_ASSUMED_RANK && sym->as->corank))
988 : {
989 4 : gfc_error ("The assumed-rank array %qs at %L shall not have a "
990 : "codimension", sym->name, error_loc);
991 4 : return false;
992 : }
993 :
994 : /* Check F2018:C822. */
995 22 : if (sym->as->rank + sym->as->corank > GFC_MAX_DIMENSIONS)
996 2 : goto too_many;
997 :
998 20 : if (as->corank)
999 : {
1000 7 : sym->as->cotype = as->cotype;
1001 7 : sym->as->corank = as->corank;
1002 : /* Check F2018:C822. */
1003 7 : if (sym->as->rank + sym->as->corank > GFC_MAX_DIMENSIONS)
1004 2 : goto too_many;
1005 :
1006 10 : for (i = 0; i < as->corank; i++)
1007 : {
1008 5 : sym->as->lower[sym->as->rank + i] = as->lower[i];
1009 5 : sym->as->upper[sym->as->rank + i] = as->upper[i];
1010 : }
1011 : }
1012 : else
1013 : {
1014 : /* The "sym" has no rank (checked via gfc_add_dimension). Thus
1015 : the dimension is added - but first the codimensions (if existing
1016 : need to be shifted to make space for the dimension. */
1017 13 : gcc_assert (as->corank == 0 && sym->as->rank == 0);
1018 :
1019 13 : sym->as->rank = as->rank;
1020 13 : sym->as->type = as->type;
1021 13 : sym->as->cray_pointee = as->cray_pointee;
1022 13 : sym->as->cp_was_assumed = as->cp_was_assumed;
1023 :
1024 : /* Check F2018:C822. */
1025 13 : if (sym->as->rank + sym->as->corank > GFC_MAX_DIMENSIONS)
1026 1 : goto too_many;
1027 :
1028 60 : for (i = sym->as->corank - 1; i >= 0; i--)
1029 : {
1030 48 : sym->as->lower[as->rank + i] = sym->as->lower[i];
1031 48 : sym->as->upper[as->rank + i] = sym->as->upper[i];
1032 : }
1033 43 : for (i = 0; i < as->rank; i++)
1034 : {
1035 31 : sym->as->lower[i] = as->lower[i];
1036 31 : sym->as->upper[i] = as->upper[i];
1037 : }
1038 : }
1039 :
1040 17 : free (as);
1041 17 : return true;
1042 :
1043 5 : too_many:
1044 :
1045 5 : gfc_error ("rank + corank of %qs exceeds %d at %C", sym->name,
1046 : GFC_MAX_DIMENSIONS);
1047 5 : return false;
1048 : }
1049 :
1050 :
1051 : /* Copy an array specification. */
1052 :
1053 : gfc_array_spec *
1054 310828 : gfc_copy_array_spec (gfc_array_spec *src)
1055 : {
1056 310828 : gfc_array_spec *dest;
1057 310828 : int i;
1058 :
1059 310828 : if (src == NULL)
1060 : return NULL;
1061 :
1062 48349 : dest = gfc_get_array_spec ();
1063 :
1064 48349 : *dest = *src;
1065 :
1066 96569 : for (i = 0; i < dest->rank + dest->corank; i++)
1067 : {
1068 48220 : dest->lower[i] = gfc_copy_expr (dest->lower[i]);
1069 48220 : dest->upper[i] = gfc_copy_expr (dest->upper[i]);
1070 : }
1071 :
1072 : return dest;
1073 : }
1074 :
1075 :
1076 : /* Returns nonzero if the two expressions are equal.
1077 : We should not need to support more than constant values, as that's what is
1078 : allowed in derived type component array spec. However, we may create types
1079 : with non-constant array spec for dummy variable class container types, for
1080 : which the _data component holds the array spec of the variable declaration.
1081 : So we have to support non-constant bounds as well. */
1082 :
1083 : static bool
1084 414 : compare_bounds (gfc_expr *bound1, gfc_expr *bound2)
1085 : {
1086 414 : if (bound1 == NULL || bound2 == NULL
1087 400 : || bound1->ts.type != BT_INTEGER
1088 400 : || bound2->ts.type != BT_INTEGER)
1089 : return false;
1090 :
1091 : /* What qualifies as identical bounds? We could probably just check that the
1092 : expressions are exact clones. We avoid rewriting a specific comparison
1093 : function and re-use instead the rather involved gfc_dep_compare_expr which
1094 : is just a bit more permissive, as it can also detect identical values for
1095 : some mismatching expressions (extra parenthesis, swapped operands, unary
1096 : plus, etc). It probably only makes a difference in corner cases. */
1097 399 : return gfc_dep_compare_expr (bound1, bound2) == 0;
1098 : }
1099 :
1100 :
1101 : /* Compares two array specifications. They must be constant or deferred
1102 : shape. */
1103 :
1104 : bool
1105 827 : gfc_compare_array_spec (gfc_array_spec *as1, gfc_array_spec *as2)
1106 : {
1107 827 : int i;
1108 :
1109 827 : if (as1 == NULL && as2 == NULL)
1110 : return 1;
1111 :
1112 827 : if (as1 == NULL || as2 == NULL)
1113 : return 0;
1114 :
1115 827 : if (as1->rank != as2->rank)
1116 : return 0;
1117 :
1118 825 : if (as1->corank != as2->corank)
1119 : return 0;
1120 :
1121 825 : if (as1->rank == 0)
1122 : return 1;
1123 :
1124 809 : if (as1->type != as2->type)
1125 : return 0;
1126 :
1127 393 : if (as1->cotype != as2->cotype)
1128 : return 0;
1129 :
1130 393 : if (as1->type == AS_EXPLICIT)
1131 298 : for (i = 0; i < as1->rank + as1->corank; i++)
1132 : {
1133 207 : if (!compare_bounds (as1->lower[i], as2->lower[i]))
1134 : return 0;
1135 :
1136 207 : if (!compare_bounds (as1->upper[i], as2->upper[i]))
1137 : return 0;
1138 : }
1139 :
1140 : return 1;
1141 : }
1142 :
1143 :
1144 : /****************** Array constructor functions ******************/
1145 :
1146 :
1147 : /* Given an expression node that might be an array constructor and a
1148 : symbol, make sure that no iterators in this or child constructors
1149 : use the symbol as an implied-DO iterator. Returns nonzero if a
1150 : duplicate was found. */
1151 :
1152 : static bool
1153 9790 : check_duplicate_iterator (gfc_constructor_base base, gfc_symbol *master)
1154 : {
1155 9790 : gfc_constructor *c;
1156 9790 : gfc_expr *e;
1157 :
1158 19887 : for (c = gfc_constructor_first (base); c; c = gfc_constructor_next (c))
1159 : {
1160 10097 : e = c->expr;
1161 :
1162 10097 : if (e->expr_type == EXPR_ARRAY
1163 10097 : && check_duplicate_iterator (e->value.constructor, master))
1164 : return 1;
1165 :
1166 10097 : if (c->iterator == NULL)
1167 9431 : continue;
1168 :
1169 666 : if (c->iterator->var->symtree->n.sym == master)
1170 : {
1171 0 : gfc_error ("DO-iterator %qs at %L is inside iterator of the "
1172 : "same name", master->name, &c->where);
1173 :
1174 0 : return 1;
1175 : }
1176 : }
1177 :
1178 : return 0;
1179 : }
1180 :
1181 :
1182 : /* Forward declaration because these functions are mutually recursive. */
1183 : static match match_array_cons_element (gfc_constructor_base *);
1184 :
1185 : /* Match a list of array elements. */
1186 :
1187 : static match
1188 475931 : match_array_list (gfc_constructor_base *result)
1189 : {
1190 475931 : gfc_constructor_base head;
1191 475931 : gfc_constructor *p;
1192 475931 : gfc_iterator iter;
1193 475931 : locus old_loc;
1194 475931 : gfc_expr *e;
1195 475931 : match m;
1196 475931 : int n;
1197 :
1198 475931 : old_loc = gfc_current_locus;
1199 :
1200 475931 : if (gfc_match_char ('(') == MATCH_NO)
1201 : return MATCH_NO;
1202 :
1203 11462 : memset (&iter, '\0', sizeof (gfc_iterator));
1204 11462 : head = NULL;
1205 :
1206 11462 : m = match_array_cons_element (&head);
1207 11462 : if (m != MATCH_YES)
1208 159 : goto cleanup;
1209 :
1210 11303 : if (gfc_match_char (',') != MATCH_YES)
1211 : {
1212 591 : m = MATCH_NO;
1213 591 : goto cleanup;
1214 : }
1215 :
1216 58 : for (n = 1;; n++)
1217 : {
1218 10770 : m = gfc_match_iterator (&iter, 0);
1219 10770 : if (m == MATCH_YES)
1220 : break;
1221 1765 : if (m == MATCH_ERROR)
1222 1 : goto cleanup;
1223 :
1224 1764 : m = match_array_cons_element (&head);
1225 1764 : if (m == MATCH_ERROR)
1226 0 : goto cleanup;
1227 1764 : if (m == MATCH_NO)
1228 : {
1229 0 : if (n > 2)
1230 0 : goto syntax;
1231 0 : m = MATCH_NO;
1232 0 : goto cleanup; /* Could be a complex constant */
1233 : }
1234 :
1235 1764 : if (gfc_match_char (',') != MATCH_YES)
1236 : {
1237 1706 : if (n > 2)
1238 0 : goto syntax;
1239 1706 : m = MATCH_NO;
1240 1706 : goto cleanup;
1241 : }
1242 : }
1243 :
1244 9005 : if (gfc_match_char (')') != MATCH_YES)
1245 0 : goto syntax;
1246 :
1247 9005 : if (check_duplicate_iterator (head, iter.var->symtree->n.sym))
1248 : {
1249 0 : m = MATCH_ERROR;
1250 0 : goto cleanup;
1251 : }
1252 :
1253 9005 : e = gfc_get_array_expr (BT_UNKNOWN, 0, &old_loc);
1254 9005 : e->value.constructor = head;
1255 :
1256 9005 : p = gfc_constructor_append_expr (result, e, &gfc_current_locus);
1257 9005 : p->iterator = gfc_get_iterator ();
1258 9005 : *p->iterator = iter;
1259 :
1260 9005 : return MATCH_YES;
1261 :
1262 0 : syntax:
1263 0 : gfc_error ("Syntax error in array constructor at %C");
1264 0 : m = MATCH_ERROR;
1265 :
1266 2457 : cleanup:
1267 2457 : gfc_constructor_free (head);
1268 2457 : gfc_free_iterator (&iter, 0);
1269 2457 : gfc_current_locus = old_loc;
1270 2457 : return m;
1271 : }
1272 :
1273 :
1274 : /* Match a single element of an array constructor, which can be a
1275 : single expression or a list of elements. */
1276 :
1277 : static match
1278 475931 : match_array_cons_element (gfc_constructor_base *result)
1279 : {
1280 475931 : gfc_expr *expr;
1281 475931 : match m;
1282 :
1283 475931 : m = match_array_list (result);
1284 475931 : if (m != MATCH_NO)
1285 : return m;
1286 :
1287 466924 : m = gfc_match_expr (&expr);
1288 466924 : if (m != MATCH_YES)
1289 : return m;
1290 :
1291 466756 : if (expr->ts.type == BT_BOZ)
1292 : {
1293 4 : gfc_error ("BOZ literal constant at %L cannot appear in an "
1294 : "array constructor", &expr->where);
1295 4 : goto done;
1296 : }
1297 :
1298 466752 : if (expr->expr_type == EXPR_FUNCTION
1299 14236 : && expr->ts.type == BT_UNKNOWN
1300 14236 : && strcmp(expr->symtree->name, "null") == 0)
1301 : {
1302 3 : gfc_error ("NULL() at %C cannot appear in an array constructor");
1303 3 : goto done;
1304 : }
1305 :
1306 466749 : gfc_constructor_append_expr (result, expr, &gfc_current_locus);
1307 466749 : return MATCH_YES;
1308 :
1309 7 : done:
1310 7 : gfc_free_expr (expr);
1311 7 : return MATCH_ERROR;
1312 : }
1313 :
1314 :
1315 : /* Convert components of an array constructor to the type in ts. */
1316 :
1317 : static match
1318 3672 : walk_array_constructor (gfc_typespec *ts, gfc_constructor_base head)
1319 : {
1320 3672 : gfc_constructor *c;
1321 3672 : gfc_expr *e;
1322 3672 : match m;
1323 :
1324 8020 : for (c = gfc_constructor_first (head); c; c = gfc_constructor_next (c))
1325 : {
1326 4356 : e = c->expr;
1327 4356 : if (e->expr_type == EXPR_ARRAY && e->ts.type == BT_UNKNOWN
1328 126 : && !e->ref && e->value.constructor)
1329 : {
1330 126 : m = walk_array_constructor (ts, e->value.constructor);
1331 126 : if (m == MATCH_ERROR)
1332 : return m;
1333 : }
1334 4230 : else if (!gfc_convert_type_warn (e, ts, 1, 1, true)
1335 4230 : && e->ts.type != BT_UNKNOWN)
1336 : return MATCH_ERROR;
1337 : }
1338 : return MATCH_YES;
1339 : }
1340 :
1341 : /* Match an array constructor. */
1342 :
1343 : match
1344 4524158 : gfc_match_array_constructor (gfc_expr **result)
1345 : {
1346 4524158 : gfc_constructor *c;
1347 4524158 : gfc_constructor_base head;
1348 4524158 : gfc_expr *expr;
1349 4524158 : gfc_typespec ts;
1350 4524158 : locus where;
1351 4524158 : match m;
1352 4524158 : const char *end_delim;
1353 4524158 : bool seen_ts;
1354 4524158 : gfc_namespace *old_ns = gfc_current_ns;
1355 :
1356 4524158 : head = NULL;
1357 4524158 : seen_ts = false;
1358 :
1359 4524158 : if (gfc_match (" (/") == MATCH_NO)
1360 : {
1361 4461860 : if (gfc_match (" [") == MATCH_NO)
1362 : return MATCH_NO;
1363 : else
1364 : {
1365 77411 : if (!gfc_notify_std (GFC_STD_F2003, "[...] "
1366 : "style array constructors at %C"))
1367 : return MATCH_ERROR;
1368 : end_delim = " ]";
1369 : }
1370 : }
1371 : else
1372 : end_delim = " /)";
1373 :
1374 139709 : where = gfc_current_locus;
1375 :
1376 : /* Try to match an optional "type-spec ::" */
1377 139709 : gfc_clear_ts (&ts);
1378 139709 : m = gfc_match_type_spec (&ts);
1379 139709 : gfc_current_ns = old_ns;
1380 :
1381 139709 : if (m == MATCH_YES)
1382 : {
1383 7253 : seen_ts = (gfc_match (" ::") == MATCH_YES);
1384 :
1385 7253 : if (seen_ts)
1386 : {
1387 5849 : if (!gfc_notify_std (GFC_STD_F2003, "Array constructor "
1388 : "including type specification at %C"))
1389 1 : goto cleanup;
1390 :
1391 5848 : if (ts.deferred)
1392 : {
1393 1 : gfc_error ("Type-spec at %L cannot contain a deferred "
1394 : "type parameter", &where);
1395 1 : goto cleanup;
1396 : }
1397 :
1398 5847 : if (ts.type == BT_CHARACTER
1399 1701 : && ts.u.cl && !ts.u.cl->length && !ts.u.cl->length_from_typespec)
1400 : {
1401 1 : gfc_error ("Type-spec at %L cannot contain an asterisk for a "
1402 : "type parameter", &where);
1403 1 : goto cleanup;
1404 : }
1405 : }
1406 : }
1407 132456 : else if (m == MATCH_ERROR)
1408 21 : goto cleanup;
1409 :
1410 7250 : if (!seen_ts)
1411 133839 : gfc_current_locus = where;
1412 :
1413 139685 : if (gfc_match (end_delim) == MATCH_YES)
1414 : {
1415 1979 : if (seen_ts)
1416 1978 : goto done;
1417 : else
1418 : {
1419 1 : gfc_error ("Empty array constructor at %C is not allowed");
1420 1 : goto cleanup;
1421 : }
1422 : }
1423 :
1424 462705 : for (;;)
1425 : {
1426 462705 : m = match_array_cons_element (&head);
1427 462705 : if (m == MATCH_ERROR)
1428 17 : goto cleanup;
1429 462688 : if (m == MATCH_NO)
1430 1 : goto syntax;
1431 :
1432 462687 : if (gfc_match_char (',') == MATCH_NO)
1433 : break;
1434 : }
1435 :
1436 137688 : if (gfc_match (end_delim) == MATCH_NO)
1437 6 : goto syntax;
1438 :
1439 139660 : done:
1440 : /* Size must be calculated at resolution time. */
1441 139660 : if (seen_ts)
1442 : {
1443 5841 : expr = gfc_get_array_expr (ts.type, ts.kind, &where);
1444 5841 : expr->ts = ts;
1445 :
1446 : /* If the typespec is CHARACTER, check that array elements can
1447 : be converted. See PR fortran/67803. */
1448 5841 : if (ts.type == BT_CHARACTER)
1449 : {
1450 1700 : c = gfc_constructor_first (head);
1451 5961 : for (; c; c = gfc_constructor_next (c))
1452 : {
1453 2566 : if (gfc_numeric_ts (&c->expr->ts)
1454 2566 : || c->expr->ts.type == BT_LOGICAL)
1455 : {
1456 5 : gfc_error ("Incompatible typespec for array element at %L",
1457 5 : &c->expr->where);
1458 5 : return MATCH_ERROR;
1459 : }
1460 :
1461 : /* Special case null(). */
1462 2561 : if (c->expr->expr_type == EXPR_FUNCTION
1463 54 : && c->expr->ts.type == BT_UNKNOWN
1464 54 : && strcmp (c->expr->symtree->name, "null") == 0)
1465 : {
1466 0 : gfc_error ("Incompatible typespec for array element at %L",
1467 : &c->expr->where);
1468 0 : return MATCH_ERROR;
1469 : }
1470 : }
1471 : }
1472 :
1473 : /* Walk the constructor, and if possible, do type conversion for
1474 : numeric types. */
1475 5836 : if (gfc_numeric_ts (&ts))
1476 : {
1477 3546 : m = walk_array_constructor (&ts, head);
1478 3546 : if (m == MATCH_ERROR)
1479 : return m;
1480 : }
1481 : }
1482 : else
1483 133819 : expr = gfc_get_array_expr (BT_UNKNOWN, 0, &where);
1484 :
1485 139648 : expr->value.constructor = head;
1486 139648 : if (expr->ts.type == BT_CHARACTER && expr->ts.u.cl)
1487 1695 : expr->ts.u.cl->length_from_typespec = seen_ts;
1488 :
1489 139648 : *result = expr;
1490 :
1491 139648 : return MATCH_YES;
1492 :
1493 7 : syntax:
1494 7 : gfc_error ("Syntax error in array constructor at %C");
1495 :
1496 49 : cleanup:
1497 49 : gfc_constructor_free (head);
1498 49 : return MATCH_ERROR;
1499 : }
1500 :
1501 :
1502 :
1503 : /************** Check array constructors for correctness **************/
1504 :
1505 : /* Given an expression, compare it's type with the type of the current
1506 : constructor. Returns nonzero if an error was issued. The
1507 : cons_state variable keeps track of whether the type of the
1508 : constructor being read or resolved is known to be good, bad or just
1509 : starting out. */
1510 :
1511 : static gfc_typespec constructor_ts;
1512 : static enum
1513 : { CONS_START, CONS_GOOD, CONS_BAD }
1514 : cons_state;
1515 :
1516 : static int
1517 1063434 : check_element_type (gfc_expr *expr, bool convert)
1518 : {
1519 1063434 : gfc_typespec ts;
1520 :
1521 1063434 : if (cons_state == CONS_BAD)
1522 : return 0; /* Suppress further errors */
1523 :
1524 : /* Without a type-spec, the values have the same declared type, which is
1525 : the type of the array constructor (F2023:C7120, 7.8 p2, p4). */
1526 1063434 : ts = expr->ts;
1527 1063434 : if (!convert && ts.type == BT_CLASS && ts.u.derived
1528 80 : && ts.u.derived->attr.is_class && ts.u.derived->components
1529 79 : && !UNLIMITED_POLY (expr))
1530 79 : ts = ts.u.derived->components->ts;
1531 :
1532 1063434 : if (cons_state == CONS_START)
1533 : {
1534 60230 : if (ts.type == BT_UNKNOWN)
1535 0 : cons_state = CONS_BAD;
1536 : else
1537 : {
1538 60230 : cons_state = CONS_GOOD;
1539 60230 : constructor_ts = ts;
1540 : }
1541 :
1542 : return 0;
1543 : }
1544 :
1545 1003204 : if (gfc_compare_types (&constructor_ts, &ts))
1546 : return 0;
1547 :
1548 351 : if (convert)
1549 348 : return gfc_convert_type_warn (expr, &constructor_ts, 1, 1, true) ? 0 : 1;
1550 :
1551 3 : gfc_error ("Element in %s array constructor at %L is %s",
1552 : gfc_typename (&constructor_ts), &expr->where,
1553 : gfc_typename (expr));
1554 :
1555 3 : cons_state = CONS_BAD;
1556 3 : return 1;
1557 : }
1558 :
1559 :
1560 : /* Recursive work function for gfc_check_constructor_type(). */
1561 :
1562 : static bool
1563 101183 : check_constructor_type (gfc_constructor_base base, bool convert)
1564 : {
1565 101183 : gfc_constructor *c;
1566 101183 : gfc_expr *e;
1567 :
1568 1173208 : for (c = gfc_constructor_first (base); c; c = gfc_constructor_next (c))
1569 : {
1570 1072174 : e = c->expr;
1571 :
1572 : /* Simplify non-constant expressions (like parenthesized arrays) so type
1573 : conversion can work on the simplified result. This handles cases like
1574 : [integer :: ([1.0])] where ([1.0]) is an EXPR_OP that needs to be
1575 : simplified to an EXPR_ARRAY before type conversion. */
1576 1072174 : if (convert && e->expr_type != EXPR_CONSTANT
1577 11662 : && e->expr_type != EXPR_ARRAY)
1578 10448 : gfc_simplify_expr (e, 0);
1579 :
1580 1072174 : if (e->expr_type == EXPR_ARRAY)
1581 : {
1582 : /* If the outer constructor has no type-spec (convert=false) and
1583 : the nested array has an explicit type-spec, process it separately
1584 : so its elements get converted according to its type-spec. This
1585 : handles cases like [[character(16) :: ['a','b']]] where the outer
1586 : constructor has no type-spec but the inner one does.
1587 : gfc_check_constructor_type will also update the global
1588 : constructor_ts and cons_state which propagates the type info
1589 : to the outer constructor.
1590 : For character types, length_from_typespec indicates an explicit
1591 : type-spec was provided. */
1592 8740 : if (!convert && e->ts.type == BT_CHARACTER
1593 541 : && e->ts.u.cl && e->ts.u.cl->length_from_typespec)
1594 : {
1595 102 : if (!gfc_check_constructor_type (e))
1596 : return false;
1597 : }
1598 : else
1599 : {
1600 8638 : if (!check_constructor_type (e->value.constructor, convert))
1601 : return false;
1602 : }
1603 :
1604 8683 : continue;
1605 : }
1606 :
1607 1063434 : if (check_element_type (e, convert))
1608 : return false;
1609 : }
1610 :
1611 : return true;
1612 : }
1613 :
1614 :
1615 : /* Check that all elements of an array constructor are the same type.
1616 : On false, an error has been generated. */
1617 :
1618 : bool
1619 92545 : gfc_check_constructor_type (gfc_expr *e)
1620 : {
1621 92545 : bool t;
1622 :
1623 92545 : if (e->ts.type != BT_UNKNOWN)
1624 : {
1625 32204 : cons_state = CONS_GOOD;
1626 32204 : constructor_ts = e->ts;
1627 : }
1628 : else
1629 : {
1630 60341 : cons_state = CONS_START;
1631 60341 : gfc_clear_ts (&constructor_ts);
1632 : }
1633 :
1634 : /* If e->ts.type != BT_UNKNOWN, the array constructor included a
1635 : typespec, and we will now convert the values on the fly. */
1636 92545 : t = check_constructor_type (e->value.constructor, e->ts.type != BT_UNKNOWN);
1637 92545 : if (t && e->ts.type == BT_UNKNOWN)
1638 60338 : e->ts = constructor_ts;
1639 :
1640 92545 : return t;
1641 : }
1642 :
1643 :
1644 :
1645 : typedef struct cons_stack
1646 : {
1647 : gfc_iterator *iterator;
1648 : struct cons_stack *previous;
1649 : }
1650 : cons_stack;
1651 :
1652 : static cons_stack *base;
1653 :
1654 : static bool check_constructor (gfc_constructor_base, bool (*) (gfc_expr *));
1655 :
1656 : /* Check an EXPR_VARIABLE expression in a constructor to make sure
1657 : that that variable is an iteration variable. */
1658 :
1659 : bool
1660 7065 : gfc_check_iter_variable (gfc_expr *expr)
1661 : {
1662 7065 : gfc_symbol *sym;
1663 7065 : cons_stack *c;
1664 :
1665 7065 : sym = expr->symtree->n.sym;
1666 :
1667 7075 : for (c = base; c && c->iterator; c = c->previous)
1668 26 : if (sym == c->iterator->var->symtree->n.sym)
1669 : return true;
1670 :
1671 : return false;
1672 : }
1673 :
1674 :
1675 : /* Recursive work function for gfc_check_constructor(). This amounts
1676 : to calling the check function for each expression in the
1677 : constructor, giving variables with the names of iterators a pass. */
1678 :
1679 : static bool
1680 7855 : check_constructor (gfc_constructor_base ctor, bool (*check_function) (gfc_expr *))
1681 : {
1682 7855 : cons_stack element;
1683 7855 : gfc_expr *e;
1684 7855 : bool t;
1685 7855 : gfc_constructor *c;
1686 :
1687 358280 : for (c = gfc_constructor_first (ctor); c; c = gfc_constructor_next (c))
1688 : {
1689 350451 : e = c->expr;
1690 :
1691 350451 : if (!e)
1692 0 : continue;
1693 :
1694 : /* Allow procedures as potential target of a procedure pointer. */
1695 350451 : if (e->expr_type == EXPR_VARIABLE
1696 220 : && e->ts.type == BT_PROCEDURE
1697 91 : && e->symtree->n.sym->attr.flavor == FL_PROCEDURE)
1698 91 : continue;
1699 :
1700 350360 : if (e->expr_type != EXPR_ARRAY)
1701 : {
1702 350064 : if (!(*check_function)(e))
1703 : return false;
1704 350044 : continue;
1705 : }
1706 :
1707 296 : element.previous = base;
1708 296 : element.iterator = c->iterator;
1709 :
1710 296 : base = &element;
1711 296 : t = check_constructor (e->value.constructor, check_function);
1712 296 : base = element.previous;
1713 :
1714 296 : if (!t)
1715 : return false;
1716 : }
1717 :
1718 : /* Nothing went wrong, so all OK. */
1719 : return true;
1720 : }
1721 :
1722 :
1723 : /* Checks a constructor to see if it is a particular kind of
1724 : expression -- specification, restricted, or initialization as
1725 : determined by the check_function. */
1726 :
1727 : bool
1728 7559 : gfc_check_constructor (gfc_expr *expr, bool (*check_function) (gfc_expr *))
1729 : {
1730 7559 : cons_stack *base_save;
1731 7559 : bool t;
1732 :
1733 7559 : base_save = base;
1734 7559 : base = NULL;
1735 :
1736 7559 : t = check_constructor (expr->value.constructor, check_function);
1737 7559 : base = base_save;
1738 :
1739 7559 : return t;
1740 : }
1741 :
1742 :
1743 :
1744 : /**************** Simplification of array constructors ****************/
1745 :
1746 : iterator_stack *iter_stack;
1747 :
1748 : typedef struct
1749 : {
1750 : gfc_constructor_base base;
1751 : int extract_count, extract_n;
1752 : gfc_expr *extracted;
1753 : mpz_t *count;
1754 :
1755 : mpz_t *offset;
1756 : gfc_component *component;
1757 : mpz_t *repeat;
1758 :
1759 : bool (*expand_work_function) (gfc_expr *);
1760 : }
1761 : expand_info;
1762 :
1763 : static expand_info current_expand;
1764 :
1765 : static bool expand_constructor (gfc_constructor_base);
1766 :
1767 :
1768 : /* Work function that counts the number of elements present in a
1769 : constructor. */
1770 :
1771 : static bool
1772 4402062 : count_elements (gfc_expr *e)
1773 : {
1774 4402062 : mpz_t result;
1775 :
1776 4402062 : if (e->rank == 0)
1777 4400344 : mpz_add_ui (*current_expand.count, *current_expand.count, 1);
1778 : else
1779 : {
1780 1718 : if (!gfc_array_size (e, &result))
1781 : {
1782 536 : gfc_free_expr (e);
1783 536 : return false;
1784 : }
1785 :
1786 1182 : mpz_add (*current_expand.count, *current_expand.count, result);
1787 1182 : mpz_clear (result);
1788 : }
1789 :
1790 4401526 : gfc_free_expr (e);
1791 4401526 : return true;
1792 : }
1793 :
1794 :
1795 : /* Work function that extracts a particular element from an array
1796 : constructor, freeing the rest. */
1797 :
1798 : static bool
1799 4997603 : extract_element (gfc_expr *e)
1800 : {
1801 4997603 : if (e->rank != 0)
1802 : { /* Something unextractable */
1803 1195 : gfc_free_expr (e);
1804 1195 : return false;
1805 : }
1806 :
1807 4996408 : if (current_expand.extract_count == current_expand.extract_n)
1808 37 : current_expand.extracted = e;
1809 : else
1810 4996371 : gfc_free_expr (e);
1811 :
1812 4996408 : current_expand.extract_count++;
1813 :
1814 4996408 : return true;
1815 : }
1816 :
1817 :
1818 : /* Work function that constructs a new constructor out of the old one,
1819 : stringing new elements together. */
1820 :
1821 : static bool
1822 1342845 : expand (gfc_expr *e)
1823 : {
1824 1342845 : gfc_constructor *c = gfc_constructor_append_expr (¤t_expand.base,
1825 : e, &e->where);
1826 :
1827 1342845 : c->n.component = current_expand.component;
1828 1342845 : return true;
1829 : }
1830 :
1831 :
1832 : /* Given an initialization expression that is a variable reference,
1833 : substitute the current value of the iteration variable. */
1834 :
1835 : void
1836 14086763 : gfc_simplify_iterator_var (gfc_expr *e)
1837 : {
1838 14086763 : iterator_stack *p;
1839 :
1840 19353312 : for (p = iter_stack; p; p = p->prev)
1841 19220061 : if (e->symtree == p->variable)
1842 : break;
1843 :
1844 14086763 : if (p == NULL)
1845 : return; /* Variable not found */
1846 :
1847 13953512 : gfc_replace_expr (e, gfc_get_int_expr (gfc_default_integer_kind, NULL, 0));
1848 :
1849 13953512 : mpz_set (e->value.integer, p->value);
1850 :
1851 13953512 : return;
1852 : }
1853 :
1854 :
1855 : /* Expand an expression with that is inside of a constructor,
1856 : recursing into other constructors if present. */
1857 :
1858 : static bool
1859 11352286 : expand_expr (gfc_expr *e)
1860 : {
1861 11352286 : if (e->expr_type == EXPR_ARRAY)
1862 11352286 : return expand_constructor (e->value.constructor);
1863 :
1864 0 : e = gfc_copy_expr (e);
1865 :
1866 0 : if (!gfc_simplify_expr (e, 1))
1867 : {
1868 0 : gfc_free_expr (e);
1869 0 : return false;
1870 : }
1871 :
1872 0 : return current_expand.expand_work_function (e);
1873 : }
1874 :
1875 :
1876 : static bool
1877 31230 : expand_iterator (gfc_constructor *c)
1878 : {
1879 31230 : gfc_expr *start, *end, *step;
1880 31230 : iterator_stack frame;
1881 31230 : mpz_t trip;
1882 31230 : bool t;
1883 :
1884 31230 : end = step = NULL;
1885 :
1886 31230 : t = false;
1887 :
1888 31230 : mpz_init (trip);
1889 31230 : mpz_init (frame.value);
1890 31230 : frame.prev = NULL;
1891 :
1892 31230 : start = gfc_copy_expr (c->iterator->start);
1893 31230 : if (!gfc_simplify_expr (start, 1))
1894 0 : goto cleanup;
1895 :
1896 31230 : if (start->expr_type != EXPR_CONSTANT || start->ts.type != BT_INTEGER)
1897 936 : goto cleanup;
1898 :
1899 30294 : end = gfc_copy_expr (c->iterator->end);
1900 30294 : if (!gfc_simplify_expr (end, 1))
1901 0 : goto cleanup;
1902 :
1903 30294 : if (end->expr_type != EXPR_CONSTANT || end->ts.type != BT_INTEGER)
1904 3621 : goto cleanup;
1905 :
1906 26673 : step = gfc_copy_expr (c->iterator->step);
1907 26673 : if (!gfc_simplify_expr (step, 1))
1908 0 : goto cleanup;
1909 :
1910 26673 : if (step->expr_type != EXPR_CONSTANT || step->ts.type != BT_INTEGER)
1911 0 : goto cleanup;
1912 :
1913 26673 : if (mpz_sgn (step->value.integer) == 0)
1914 : {
1915 0 : gfc_error ("Iterator step at %L cannot be zero", &step->where);
1916 0 : goto cleanup;
1917 : }
1918 :
1919 : /* Calculate the trip count of the loop. */
1920 26673 : mpz_sub (trip, end->value.integer, start->value.integer);
1921 26673 : mpz_add (trip, trip, step->value.integer);
1922 26673 : mpz_tdiv_q (trip, trip, step->value.integer);
1923 :
1924 26673 : mpz_set (frame.value, start->value.integer);
1925 :
1926 26673 : frame.prev = iter_stack;
1927 26673 : frame.variable = c->iterator->var->symtree;
1928 26673 : iter_stack = &frame;
1929 :
1930 11377565 : while (mpz_sgn (trip) > 0)
1931 : {
1932 11352286 : if (!expand_expr (c->expr))
1933 1394 : goto cleanup;
1934 :
1935 11350892 : mpz_add (frame.value, frame.value, step->value.integer);
1936 11350892 : mpz_sub_ui (trip, trip, 1);
1937 : }
1938 :
1939 : t = true;
1940 :
1941 31230 : cleanup:
1942 31230 : gfc_free_expr (start);
1943 31230 : gfc_free_expr (end);
1944 31230 : gfc_free_expr (step);
1945 :
1946 31230 : mpz_clear (trip);
1947 31230 : mpz_clear (frame.value);
1948 :
1949 31230 : iter_stack = frame.prev;
1950 :
1951 31230 : return t;
1952 : }
1953 :
1954 : /* Variables for noticing if all constructors are empty, and
1955 : if any of them had a type. */
1956 :
1957 : static bool empty_constructor;
1958 : static gfc_typespec empty_ts;
1959 :
1960 : /* Expand a constructor into constant constructors without any
1961 : iterators, calling the work function for each of the expanded
1962 : expressions. The work function needs to either save or free the
1963 : passed expression. */
1964 :
1965 : static bool
1966 11670892 : expand_constructor (gfc_constructor_base base)
1967 : {
1968 11670892 : gfc_constructor *c;
1969 11670892 : gfc_expr *e;
1970 :
1971 29549130 : for (c = gfc_constructor_first (base); c; c = gfc_constructor_next(c))
1972 : {
1973 17887315 : if (c->iterator != NULL)
1974 : {
1975 31230 : if (!expand_iterator (c))
1976 : return false;
1977 25279 : continue;
1978 : }
1979 :
1980 17856085 : e = c->expr;
1981 :
1982 17856085 : if (e == NULL)
1983 : return false;
1984 :
1985 17856085 : if (empty_constructor)
1986 101375 : empty_ts = e->ts;
1987 :
1988 : /* Simplify constant array expression/section within constructor. */
1989 17856085 : if (e->expr_type == EXPR_VARIABLE && e->rank > 0 && e->ref
1990 4330 : && e->symtree && e->symtree->n.sym
1991 4330 : && e->symtree->n.sym->attr.flavor == FL_PARAMETER)
1992 577 : gfc_simplify_expr (e, 0);
1993 :
1994 17856085 : if (e->expr_type == EXPR_ARRAY)
1995 : {
1996 10843 : if (!expand_constructor (e->value.constructor))
1997 : return false;
1998 :
1999 10788 : continue;
2000 : }
2001 :
2002 17845242 : empty_constructor = false;
2003 17845242 : e = gfc_copy_expr (e);
2004 17845242 : if (!gfc_simplify_expr (e, 1))
2005 : {
2006 200 : gfc_free_expr (e);
2007 200 : return false;
2008 : }
2009 17845042 : e->from_constructor = 1;
2010 17845042 : current_expand.offset = &c->offset;
2011 17845042 : current_expand.repeat = &c->repeat;
2012 17845042 : current_expand.component = c->n.component;
2013 17845042 : if (!current_expand.expand_work_function(e))
2014 : return false;
2015 : }
2016 : return true;
2017 : }
2018 :
2019 :
2020 : /* Given an array expression and an element number (starting at zero),
2021 : return a pointer to the array element. NULL is returned if the
2022 : size of the array has been exceeded. The expression node returned
2023 : remains a part of the array and should not be freed. Access is not
2024 : efficient at all, but this is another place where things do not
2025 : have to be particularly fast. */
2026 :
2027 : static gfc_expr *
2028 99174 : gfc_get_array_element (gfc_expr *array, int element)
2029 : {
2030 99174 : expand_info expand_save;
2031 99174 : gfc_expr *e;
2032 99174 : bool rc;
2033 :
2034 99174 : expand_save = current_expand;
2035 99174 : current_expand.extract_n = element;
2036 99174 : current_expand.expand_work_function = extract_element;
2037 99174 : current_expand.extracted = NULL;
2038 99174 : current_expand.extract_count = 0;
2039 :
2040 99174 : iter_stack = NULL;
2041 :
2042 99174 : rc = expand_constructor (array->value.constructor);
2043 99174 : e = current_expand.extracted;
2044 99174 : current_expand = expand_save;
2045 :
2046 99174 : if (!rc)
2047 1774 : return NULL;
2048 :
2049 : return e;
2050 : }
2051 :
2052 :
2053 : /* Top level subroutine for expanding constructors. We only expand
2054 : constructor if they are small enough. */
2055 :
2056 : bool
2057 101534 : gfc_expand_constructor (gfc_expr *e, bool fatal)
2058 : {
2059 101534 : expand_info expand_save;
2060 101534 : gfc_expr *f;
2061 101534 : bool rc;
2062 :
2063 101534 : if (gfc_is_size_zero_array (e))
2064 : return true;
2065 :
2066 : /* If we can successfully get an array element at the max array size then
2067 : the array is too big to expand, so we just return. */
2068 99174 : f = gfc_get_array_element (e, flag_max_array_constructor);
2069 99174 : if (f != NULL)
2070 : {
2071 37 : gfc_free_expr (f);
2072 37 : if (fatal)
2073 : {
2074 8 : gfc_error ("The number of elements in the array constructor "
2075 : "at %L requires an increase of the allowed %d "
2076 : "upper limit. See %<-fmax-array-constructor%> "
2077 : "option", &e->where, flag_max_array_constructor);
2078 8 : return false;
2079 : }
2080 : return true;
2081 : }
2082 :
2083 : /* We now know the array is not too big so go ahead and try to expand it. */
2084 99137 : expand_save = current_expand;
2085 99137 : current_expand.base = NULL;
2086 :
2087 99137 : iter_stack = NULL;
2088 :
2089 99137 : empty_constructor = true;
2090 99137 : gfc_clear_ts (&empty_ts);
2091 99137 : current_expand.expand_work_function = expand;
2092 :
2093 99137 : if (!expand_constructor (e->value.constructor))
2094 : {
2095 579 : gfc_constructor_free (current_expand.base);
2096 579 : rc = false;
2097 579 : goto done;
2098 : }
2099 :
2100 : /* If we don't have an explicit constructor type, and there
2101 : were only empty constructors, then take the type from
2102 : them. */
2103 :
2104 98558 : if (constructor_ts.type == BT_UNKNOWN && empty_constructor)
2105 8 : e->ts = empty_ts;
2106 :
2107 98558 : gfc_constructor_free (e->value.constructor);
2108 98558 : e->value.constructor = current_expand.base;
2109 :
2110 98558 : rc = true;
2111 :
2112 99137 : done:
2113 99137 : current_expand = expand_save;
2114 :
2115 99137 : return rc;
2116 : }
2117 :
2118 :
2119 : /* Work function for checking that an element of a constructor is a
2120 : constant, after removal of any iteration variables. We return
2121 : false if not so. */
2122 :
2123 : static bool
2124 7102532 : is_constant_element (gfc_expr *e)
2125 : {
2126 7102532 : int rv;
2127 :
2128 7102532 : rv = gfc_is_constant_expr (e);
2129 7102532 : gfc_free_expr (e);
2130 :
2131 7102532 : return rv;
2132 : }
2133 :
2134 :
2135 : /* Given an array constructor, determine if the constructor is
2136 : constant or not by expanding it and making sure that all elements
2137 : are constants. This is a bit of a hack since something like (/ (i,
2138 : i=1,100000000) /) will take a while as* opposed to a more clever
2139 : function that traverses the expression tree. FIXME. */
2140 :
2141 : bool
2142 6348 : gfc_constant_ac (gfc_expr *e)
2143 : {
2144 6348 : expand_info expand_save;
2145 6348 : bool rc;
2146 :
2147 6348 : iter_stack = NULL;
2148 6348 : expand_save = current_expand;
2149 6348 : current_expand.expand_work_function = is_constant_element;
2150 :
2151 6348 : rc = expand_constructor (e->value.constructor);
2152 :
2153 6348 : current_expand = expand_save;
2154 6348 : if (!rc)
2155 : return 0;
2156 :
2157 : return 1;
2158 : }
2159 :
2160 :
2161 : /* Returns nonzero if an array constructor has been completely
2162 : expanded (no iterators) and zero if iterators are present. */
2163 :
2164 : bool
2165 27804 : gfc_expanded_ac (gfc_expr *e)
2166 : {
2167 27804 : gfc_constructor *c;
2168 :
2169 27804 : if (e->expr_type == EXPR_ARRAY)
2170 5929 : for (c = gfc_constructor_first (e->value.constructor);
2171 28256 : c; c = gfc_constructor_next (c))
2172 22327 : if (c->iterator != NULL || !gfc_expanded_ac (c->expr))
2173 : return 0;
2174 :
2175 : return 1;
2176 : }
2177 :
2178 :
2179 : /*************** Type resolution of array constructors ***************/
2180 :
2181 :
2182 : /* The symbol expr_is_sought_symbol_ref will try to find. */
2183 : static const gfc_symbol *sought_symbol = NULL;
2184 :
2185 :
2186 : /* Tells whether the expression E is a variable reference to the symbol
2187 : in the static variable SOUGHT_SYMBOL, and sets the locus pointer WHERE
2188 : accordingly.
2189 : To be used with gfc_expr_walker: if a reference is found we don't need
2190 : to look further so we return 1 to skip any further walk. */
2191 :
2192 : static int
2193 17425 : expr_is_sought_symbol_ref (gfc_expr **e, int *walk_subtrees ATTRIBUTE_UNUSED,
2194 : void *where)
2195 : {
2196 17425 : gfc_expr *expr = *e;
2197 17425 : locus *sym_loc = (locus *)where;
2198 :
2199 17425 : if (expr->expr_type == EXPR_VARIABLE
2200 1504 : && expr->symtree->n.sym == sought_symbol)
2201 : {
2202 9 : *sym_loc = expr->where;
2203 9 : return 1;
2204 : }
2205 :
2206 : return 0;
2207 : }
2208 :
2209 :
2210 : /* Tells whether the expression EXPR contains a reference to the symbol
2211 : SYM and in that case sets the position SYM_LOC where the reference is. */
2212 :
2213 : static bool
2214 16749 : find_symbol_in_expr (gfc_symbol *sym, gfc_expr *expr, locus *sym_loc)
2215 : {
2216 16749 : int ret;
2217 :
2218 16749 : sought_symbol = sym;
2219 0 : ret = gfc_expr_walker (&expr, &expr_is_sought_symbol_ref, sym_loc);
2220 16749 : sought_symbol = NULL;
2221 16749 : return ret;
2222 : }
2223 :
2224 :
2225 : /* Recursive array list resolution function. All of the elements must
2226 : be of the same type. */
2227 :
2228 : static bool
2229 76493 : resolve_array_list (gfc_constructor_base base)
2230 : {
2231 76493 : bool t;
2232 76493 : gfc_constructor *c;
2233 76493 : gfc_iterator *iter;
2234 76493 : gfc_expr *expr1 = NULL;
2235 :
2236 76493 : t = true;
2237 :
2238 647207 : for (c = gfc_constructor_first (base); c; c = gfc_constructor_next (c))
2239 : {
2240 570714 : iter = c->iterator;
2241 570714 : if (iter != NULL)
2242 : {
2243 5583 : gfc_symbol *iter_var;
2244 5583 : locus iter_var_loc;
2245 :
2246 5583 : if (!gfc_resolve_iterator (iter, false, true))
2247 1 : t = false;
2248 :
2249 : /* Check for bounds referencing the iterator variable. */
2250 5583 : gcc_assert (iter->var->expr_type == EXPR_VARIABLE);
2251 5583 : iter_var = iter->var->symtree->n.sym;
2252 5583 : if (find_symbol_in_expr (iter_var, iter->start, &iter_var_loc))
2253 : {
2254 1 : if (!gfc_notify_std (GFC_STD_LEGACY, "AC-IMPLIED-DO initial "
2255 : "expression references control variable "
2256 : "at %L", &iter_var_loc))
2257 5583 : t = false;
2258 : }
2259 5583 : if (find_symbol_in_expr (iter_var, iter->end, &iter_var_loc))
2260 : {
2261 7 : if (!gfc_notify_std (GFC_STD_LEGACY, "AC-IMPLIED-DO final "
2262 : "expression references control variable "
2263 : "at %L", &iter_var_loc))
2264 5583 : t = false;
2265 : }
2266 5583 : if (find_symbol_in_expr (iter_var, iter->step, &iter_var_loc))
2267 : {
2268 1 : if (!gfc_notify_std (GFC_STD_LEGACY, "AC-IMPLIED-DO step "
2269 : "expression references control variable "
2270 : "at %L", &iter_var_loc))
2271 5583 : t = false;
2272 : }
2273 : }
2274 :
2275 570714 : if (!gfc_resolve_expr (c->expr))
2276 61 : t = false;
2277 :
2278 570714 : if (UNLIMITED_POLY (c->expr))
2279 : {
2280 1 : gfc_error ("Array constructor value at %L shall not be unlimited "
2281 : "polymorphic [F2008: C4106]", &c->expr->where);
2282 1 : t = false;
2283 : }
2284 :
2285 : /* F2018:C7114 The declared type of an ac-value shall not be abstract. */
2286 570714 : if (c->expr->ts.type == BT_CLASS
2287 86 : && c->expr->ts.u.derived
2288 86 : && c->expr->ts.u.derived->attr.abstract
2289 5 : && CLASS_DATA (c->expr))
2290 : {
2291 5 : gfc_error ("Array constructor value %qs at %L is of the ABSTRACT "
2292 5 : "type %qs", c->expr->symtree->name, &c->expr->where,
2293 5 : CLASS_DATA (c->expr)->ts.u.derived->name);
2294 5 : t = false;
2295 : }
2296 :
2297 : /* For valid expressions, check that the type specification parameters
2298 : are the same. */
2299 570714 : if (t && !c->iterator && c->expr && IS_PDT (c->expr))
2300 : {
2301 257 : if (expr1 == NULL)
2302 : expr1 = c->expr;
2303 : else
2304 121 : t = gfc_check_type_spec_parms (expr1, c->expr, "in array constructor");
2305 : }
2306 : }
2307 :
2308 76493 : return t;
2309 : }
2310 :
2311 : /* Resolve character array constructor. If it has a specified constant character
2312 : length, pad/truncate the elements here; if the length is not specified and
2313 : all elements are of compile-time known length, emit an error as this is
2314 : invalid. */
2315 :
2316 : bool
2317 11701 : gfc_resolve_character_array_constructor (gfc_expr *expr)
2318 : {
2319 11701 : gfc_constructor *p;
2320 11701 : HOST_WIDE_INT found_length;
2321 11701 : bool has_ts;
2322 :
2323 11701 : gcc_assert (expr->expr_type == EXPR_ARRAY);
2324 11701 : gcc_assert (expr->ts.type == BT_CHARACTER);
2325 :
2326 : /* Check if we have an explicit type-spec with length. */
2327 11701 : has_ts = expr->ts.u.cl && expr->ts.u.cl->length_from_typespec;
2328 :
2329 11701 : if (expr->ts.u.cl == NULL)
2330 : {
2331 300 : for (p = gfc_constructor_first (expr->value.constructor);
2332 419 : p; p = gfc_constructor_next (p))
2333 339 : if (p->expr->ts.u.cl != NULL)
2334 : {
2335 : /* Ensure that if there is a char_len around that it is
2336 : used; otherwise the middle-end confuses them! */
2337 220 : expr->ts.u.cl = p->expr->ts.u.cl;
2338 220 : goto got_charlen;
2339 : }
2340 :
2341 80 : expr->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
2342 : }
2343 :
2344 11401 : got_charlen:
2345 :
2346 : /* Early exit for zero size arrays. */
2347 11701 : if (expr->shape)
2348 : {
2349 11332 : mpz_t size;
2350 11332 : HOST_WIDE_INT arraysize;
2351 :
2352 11332 : gfc_array_size (expr, &size);
2353 11332 : arraysize = mpz_get_ui (size);
2354 11332 : mpz_clear (size);
2355 :
2356 11332 : if (arraysize == 0)
2357 470 : return true;
2358 : }
2359 :
2360 11231 : found_length = -1;
2361 :
2362 11231 : if (expr->ts.u.cl->length == NULL)
2363 : {
2364 : /* Check that all constant string elements have the same length until
2365 : we reach the end or find a variable-length one. */
2366 :
2367 7072 : for (p = gfc_constructor_first (expr->value.constructor);
2368 32797 : p; p = gfc_constructor_next (p))
2369 : {
2370 26009 : HOST_WIDE_INT current_length = -1;
2371 26009 : gfc_ref *ref;
2372 26196 : for (ref = p->expr->ref; ref; ref = ref->next)
2373 188 : if (ref->type == REF_SUBSTRING
2374 14 : && ref->u.ss.start
2375 14 : && ref->u.ss.start->expr_type == EXPR_CONSTANT
2376 14 : && ref->u.ss.end
2377 1 : && ref->u.ss.end->expr_type == EXPR_CONSTANT)
2378 : break;
2379 :
2380 26009 : if (p->expr->expr_type == EXPR_CONSTANT)
2381 25679 : current_length = p->expr->value.character.length;
2382 330 : else if (ref)
2383 1 : current_length = gfc_mpz_get_hwi (ref->u.ss.end->value.integer)
2384 1 : - gfc_mpz_get_hwi (ref->u.ss.start->value.integer) + 1;
2385 329 : else if (p->expr->ts.u.cl && p->expr->ts.u.cl->length
2386 51 : && p->expr->ts.u.cl->length->expr_type == EXPR_CONSTANT)
2387 51 : current_length = gfc_mpz_get_hwi (p->expr->ts.u.cl->length->value.integer);
2388 : else
2389 : return true;
2390 :
2391 25731 : if (current_length < 0)
2392 : current_length = 0;
2393 :
2394 25731 : if (found_length == -1)
2395 6806 : found_length = current_length;
2396 18925 : else if (found_length != current_length)
2397 : {
2398 6 : gfc_error ("Different CHARACTER lengths (%wd/%wd) in array"
2399 : " constructor at %L", found_length,
2400 6 : current_length, &p->expr->where);
2401 6 : return false;
2402 : }
2403 :
2404 25725 : gcc_assert (found_length == current_length);
2405 : }
2406 :
2407 6788 : gcc_assert (found_length != -1);
2408 :
2409 : /* Update the character length of the array constructor. */
2410 6788 : expr->ts.u.cl->length = gfc_get_int_expr (gfc_charlen_int_kind,
2411 : NULL, found_length);
2412 : }
2413 : else
2414 : {
2415 : /* We've got a character length specified. It should be an integer,
2416 : otherwise an error is signalled elsewhere. */
2417 4159 : gcc_assert (expr->ts.u.cl->length);
2418 :
2419 : /* If we've got a constant character length, pad according to this.
2420 : gfc_extract_int does check for BT_INTEGER and EXPR_CONSTANT and sets
2421 : max_length only if they pass. */
2422 4159 : gfc_extract_hwi (expr->ts.u.cl->length, &found_length);
2423 :
2424 : /* Now pad/truncate the elements accordingly to the specified character
2425 : length. This is ok inside this conditional, as in the case above
2426 : (without typespec) all elements are verified to have the same length
2427 : anyway. */
2428 4159 : if (found_length != -1)
2429 3986 : for (p = gfc_constructor_first (expr->value.constructor);
2430 17686 : p; p = gfc_constructor_next (p))
2431 : {
2432 : /* For non-constant expressions (like EXPR_OP from concatenation),
2433 : try to simplify them first so we can then pad/truncate. */
2434 13700 : if (p->expr->expr_type != EXPR_CONSTANT
2435 2460 : && p->expr->ts.type == BT_CHARACTER)
2436 2429 : gfc_simplify_expr (p->expr, 0);
2437 :
2438 13700 : if (p->expr->expr_type == EXPR_CONSTANT)
2439 : {
2440 11240 : gfc_expr *cl = NULL;
2441 11240 : HOST_WIDE_INT current_length = -1;
2442 :
2443 11240 : if (p->expr->ts.u.cl && p->expr->ts.u.cl->length)
2444 : {
2445 2306 : cl = p->expr->ts.u.cl->length;
2446 2306 : gfc_extract_hwi (cl, ¤t_length);
2447 : }
2448 :
2449 : /* If gfc_extract_int above set current_length, we implicitly
2450 : know the type is BT_INTEGER and it's EXPR_CONSTANT. */
2451 :
2452 2306 : if (! cl
2453 2306 : || (current_length != -1 && current_length != found_length))
2454 9174 : gfc_set_constant_character_len (found_length, p->expr,
2455 : has_ts ? -1 : found_length);
2456 : }
2457 2460 : else if (p->expr->expr_type == EXPR_ARRAY)
2458 : {
2459 : /* For nested array constructors, propagate the type-spec and
2460 : recursively resolve. This handles cases like
2461 : [character(16) :: ['a','b']] // "|". The inner constructor
2462 : may have BT_UNKNOWN type initially. */
2463 111 : if (p->expr->ts.type == BT_UNKNOWN
2464 80 : || p->expr->ts.type == BT_CHARACTER)
2465 : {
2466 111 : if (p->expr->ts.type == BT_CHARACTER
2467 80 : && p->expr->ts.u.cl
2468 80 : && p->expr->ts.u.cl->length_from_typespec)
2469 : {
2470 : /* If the inner array has an explicit type-spec, we must
2471 : honor it first (e.g. truncate/pad to its length),
2472 : before coercing it to the outer length. */
2473 48 : gfc_resolve_character_array_constructor (p->expr);
2474 : }
2475 :
2476 111 : p->expr->ts = expr->ts;
2477 111 : gfc_resolve_character_array_constructor (p->expr);
2478 : }
2479 : }
2480 : }
2481 : }
2482 :
2483 : return true;
2484 : }
2485 :
2486 :
2487 : /* Resolve all of the expressions in an array list. */
2488 :
2489 : bool
2490 76493 : gfc_resolve_array_constructor (gfc_expr *expr)
2491 : {
2492 76493 : bool t;
2493 :
2494 76493 : t = resolve_array_list (expr->value.constructor);
2495 76493 : if (t)
2496 76420 : t = gfc_check_constructor_type (expr);
2497 :
2498 : /* gfc_resolve_character_array_constructor is called in gfc_resolve_expr after
2499 : the call to this function, so we don't need to call it here; if it was
2500 : called twice, an error message there would be duplicated. */
2501 :
2502 76493 : return t;
2503 : }
2504 :
2505 :
2506 : /* Copy an iterator structure. */
2507 :
2508 : gfc_iterator *
2509 885263 : gfc_copy_iterator (gfc_iterator *src)
2510 : {
2511 885263 : gfc_iterator *dest;
2512 :
2513 885263 : if (src == NULL)
2514 : return NULL;
2515 :
2516 554 : dest = gfc_get_iterator ();
2517 :
2518 554 : dest->var = gfc_copy_expr (src->var);
2519 554 : dest->start = gfc_copy_expr (src->start);
2520 554 : dest->end = gfc_copy_expr (src->end);
2521 554 : dest->step = gfc_copy_expr (src->step);
2522 554 : dest->annot = src->annot;
2523 :
2524 554 : return dest;
2525 : }
2526 :
2527 :
2528 : /********* Subroutines for determining the size of an array *********/
2529 :
2530 : /* These are needed just to accommodate RESHAPE(). There are no
2531 : diagnostics here, we just return false if something goes wrong. */
2532 :
2533 :
2534 : /* Get the size of single dimension of an array specification. The
2535 : array is guaranteed to be one dimensional. */
2536 :
2537 : bool
2538 576862 : spec_dimen_size (gfc_array_spec *as, int dimen, mpz_t *result)
2539 : {
2540 576862 : if (as == NULL)
2541 : return false;
2542 :
2543 562745 : if (dimen < 0 || dimen > as->rank - 1)
2544 0 : gfc_internal_error ("spec_dimen_size(): Bad dimension");
2545 :
2546 562745 : if (as->type != AS_EXPLICIT
2547 355660 : || !as->lower[dimen]
2548 355660 : || !as->upper[dimen])
2549 : return false;
2550 :
2551 355660 : if (as->lower[dimen]->expr_type != EXPR_CONSTANT
2552 354861 : || as->upper[dimen]->expr_type != EXPR_CONSTANT
2553 334509 : || as->lower[dimen]->ts.type != BT_INTEGER
2554 334509 : || as->upper[dimen]->ts.type != BT_INTEGER)
2555 : return false;
2556 :
2557 334503 : mpz_init (*result);
2558 :
2559 334503 : mpz_sub (*result, as->upper[dimen]->value.integer,
2560 334503 : as->lower[dimen]->value.integer);
2561 :
2562 334503 : mpz_add_ui (*result, *result, 1);
2563 :
2564 334503 : if (mpz_cmp_si (*result, 0) < 0)
2565 0 : mpz_set_si (*result, 0);
2566 :
2567 : return true;
2568 : }
2569 :
2570 :
2571 : bool
2572 31605 : spec_size (gfc_array_spec *as, mpz_t *result)
2573 : {
2574 31605 : mpz_t size;
2575 31605 : int d;
2576 :
2577 31605 : if (!as || as->type == AS_ASSUMED_RANK)
2578 : return false;
2579 :
2580 30101 : mpz_init_set_ui (*result, 1);
2581 :
2582 81273 : for (d = 0; d < as->rank; d++)
2583 : {
2584 36045 : if (!spec_dimen_size (as, d, &size))
2585 : {
2586 14974 : mpz_clear (*result);
2587 14974 : return false;
2588 : }
2589 :
2590 21071 : mpz_mul (*result, *result, size);
2591 21071 : mpz_clear (size);
2592 : }
2593 :
2594 : return true;
2595 : }
2596 :
2597 :
2598 : /* Get the number of elements in an array section. Optionally, also supply
2599 : the end value. */
2600 :
2601 : bool
2602 78450 : gfc_ref_dimen_size (gfc_array_ref *ar, int dimen, mpz_t *result, mpz_t *end)
2603 : {
2604 78450 : mpz_t upper, lower, stride;
2605 78450 : mpz_t diff;
2606 78450 : bool t;
2607 78450 : gfc_expr *stride_expr = NULL;
2608 :
2609 78450 : if (dimen < 0 || ar == NULL)
2610 0 : gfc_internal_error ("gfc_ref_dimen_size(): Bad dimension");
2611 :
2612 78450 : if (dimen > ar->dimen - 1)
2613 : {
2614 1 : gfc_error ("Bad array dimension at %L", &ar->c_where[dimen]);
2615 1 : return false;
2616 : }
2617 :
2618 78449 : switch (ar->dimen_type[dimen])
2619 : {
2620 174 : case DIMEN_ELEMENT:
2621 174 : mpz_init (*result);
2622 174 : mpz_set_ui (*result, 1);
2623 174 : t = true;
2624 174 : break;
2625 :
2626 1851 : case DIMEN_VECTOR:
2627 1851 : t = gfc_array_size (ar->start[dimen], result); /* Recurse! */
2628 1851 : break;
2629 :
2630 76424 : case DIMEN_RANGE:
2631 :
2632 76424 : mpz_init (stride);
2633 :
2634 76424 : if (ar->stride[dimen] == NULL)
2635 53513 : mpz_set_ui (stride, 1);
2636 : else
2637 : {
2638 22911 : stride_expr = gfc_copy_expr(ar->stride[dimen]);
2639 :
2640 22911 : if (!gfc_simplify_expr (stride_expr, 1)
2641 22909 : || stride_expr->expr_type != EXPR_CONSTANT
2642 43990 : || mpz_cmp_ui (stride_expr->value.integer, 0) == 0)
2643 : {
2644 1834 : gfc_free_expr (stride_expr);
2645 1834 : mpz_clear (stride);
2646 1834 : return false;
2647 : }
2648 21077 : mpz_set (stride, stride_expr->value.integer);
2649 21077 : gfc_free_expr(stride_expr);
2650 : }
2651 :
2652 : /* Calculate the number of elements via gfc_dep_difference, but only if
2653 : start and end are both supplied in the reference or the array spec.
2654 : This is to guard against strange but valid code like
2655 :
2656 : subroutine foo(a,n)
2657 : real a(1:n)
2658 : n = 3
2659 : print *,size(a(n-1:))
2660 :
2661 : where the user changes the value of a variable. If we have to
2662 : determine end as well, we cannot do this using gfc_dep_difference.
2663 : Fall back to the constants-only code then. */
2664 :
2665 74590 : if (end == NULL)
2666 : {
2667 66159 : bool use_dep;
2668 :
2669 66159 : use_dep = gfc_dep_difference (ar->end[dimen], ar->start[dimen],
2670 : &diff);
2671 66159 : if (!use_dep && ar->end[dimen] == NULL && ar->start[dimen] == NULL)
2672 29279 : use_dep = gfc_dep_difference (ar->as->upper[dimen],
2673 29279 : ar->as->lower[dimen], &diff);
2674 :
2675 44059 : if (use_dep)
2676 : {
2677 33884 : mpz_init (*result);
2678 33884 : mpz_add (*result, diff, stride);
2679 33884 : mpz_div (*result, *result, stride);
2680 33884 : if (mpz_cmp_ui (*result, 0) < 0)
2681 112 : mpz_set_ui (*result, 0);
2682 :
2683 33884 : mpz_clear (stride);
2684 33884 : mpz_clear (diff);
2685 33884 : return true;
2686 : }
2687 :
2688 : }
2689 :
2690 : /* Constant-only code here, which covers more cases
2691 : like a(:4) etc. */
2692 40706 : mpz_init (upper);
2693 40706 : mpz_init (lower);
2694 40706 : t = false;
2695 :
2696 40706 : if (ar->start[dimen] == NULL)
2697 : {
2698 29984 : if (ar->as->lower[dimen] == NULL
2699 12435 : || ar->as->lower[dimen]->expr_type != EXPR_CONSTANT
2700 12432 : || ar->as->lower[dimen]->ts.type != BT_INTEGER)
2701 17552 : goto cleanup;
2702 12432 : mpz_set (lower, ar->as->lower[dimen]->value.integer);
2703 : }
2704 : else
2705 : {
2706 10722 : if (ar->start[dimen]->expr_type != EXPR_CONSTANT)
2707 2465 : goto cleanup;
2708 8257 : mpz_set (lower, ar->start[dimen]->value.integer);
2709 : }
2710 :
2711 20689 : if (ar->end[dimen] == NULL)
2712 : {
2713 6683 : if (ar->as->upper[dimen] == NULL
2714 5055 : || ar->as->upper[dimen]->expr_type != EXPR_CONSTANT
2715 4525 : || ar->as->upper[dimen]->ts.type != BT_INTEGER)
2716 2159 : goto cleanup;
2717 4524 : mpz_set (upper, ar->as->upper[dimen]->value.integer);
2718 : }
2719 : else
2720 : {
2721 14006 : if (ar->end[dimen]->expr_type != EXPR_CONSTANT)
2722 4584 : goto cleanup;
2723 9422 : mpz_set (upper, ar->end[dimen]->value.integer);
2724 : }
2725 :
2726 13946 : mpz_init (*result);
2727 13946 : mpz_sub (*result, upper, lower);
2728 13946 : mpz_add (*result, *result, stride);
2729 13946 : mpz_div (*result, *result, stride);
2730 :
2731 : /* Zero stride caught earlier. */
2732 13946 : if (mpz_cmp_ui (*result, 0) < 0)
2733 8 : mpz_set_ui (*result, 0);
2734 13946 : t = true;
2735 :
2736 13946 : if (end)
2737 : {
2738 6695 : mpz_init (*end);
2739 :
2740 6695 : mpz_sub_ui (*end, *result, 1UL);
2741 6695 : mpz_mul (*end, *end, stride);
2742 6695 : mpz_add (*end, *end, lower);
2743 : }
2744 :
2745 7251 : cleanup:
2746 40706 : mpz_clear (upper);
2747 40706 : mpz_clear (lower);
2748 40706 : mpz_clear (stride);
2749 40706 : return t;
2750 :
2751 0 : default:
2752 0 : gfc_internal_error ("gfc_ref_dimen_size(): Bad dimen_type");
2753 : }
2754 :
2755 : return t;
2756 : }
2757 :
2758 :
2759 : static bool
2760 3444 : ref_size (gfc_array_ref *ar, mpz_t *result)
2761 : {
2762 3444 : mpz_t size;
2763 3444 : int d;
2764 :
2765 3444 : mpz_init_set_ui (*result, 1);
2766 :
2767 10492 : for (d = 0; d < ar->dimen; d++)
2768 : {
2769 4004 : if (!gfc_ref_dimen_size (ar, d, &size, NULL))
2770 : {
2771 400 : mpz_clear (*result);
2772 400 : return false;
2773 : }
2774 :
2775 3604 : mpz_mul (*result, *result, size);
2776 3604 : mpz_clear (size);
2777 : }
2778 :
2779 : return true;
2780 : }
2781 :
2782 :
2783 : /* Given an array expression and a dimension, figure out how many
2784 : elements it has along that dimension. Returns true if we were
2785 : able to return a result in the 'result' variable, false
2786 : otherwise. */
2787 :
2788 : bool
2789 737480 : gfc_array_dimen_size (gfc_expr *array, int dimen, mpz_t *result)
2790 : {
2791 737480 : gfc_ref *ref;
2792 737480 : int i;
2793 :
2794 737480 : gcc_assert (array != NULL);
2795 :
2796 737480 : if (array->ts.type == BT_CLASS)
2797 : return false;
2798 :
2799 724006 : if (array->rank == -1)
2800 : return false;
2801 :
2802 724006 : if (dimen < 0 || dimen > array->rank - 1)
2803 0 : gfc_internal_error ("gfc_array_dimen_size(): Bad dimension");
2804 :
2805 724006 : switch (array->expr_type)
2806 : {
2807 601381 : case EXPR_VARIABLE:
2808 601381 : case EXPR_FUNCTION:
2809 663155 : for (ref = array->ref; ref; ref = ref->next)
2810 : {
2811 : /* Ultimate component is a procedure pointer. */
2812 618220 : if (ref->type == REF_COMPONENT
2813 45821 : && !ref->next
2814 1473 : && ref->u.c.component->attr.function
2815 97 : && IS_PROC_POINTER (ref->u.c.component))
2816 : return false;
2817 :
2818 618123 : if (ref->type != REF_ARRAY)
2819 45724 : continue;
2820 :
2821 572399 : if (ref->u.ar.type == AR_FULL)
2822 491838 : return spec_dimen_size (ref->u.ar.as, dimen, result);
2823 :
2824 80561 : if (ref->u.ar.type == AR_SECTION)
2825 : {
2826 150496 : for (i = 0; dimen >= 0; i++)
2827 85985 : if (ref->u.ar.dimen_type[i] != DIMEN_ELEMENT)
2828 80547 : dimen--;
2829 :
2830 64511 : return gfc_ref_dimen_size (&ref->u.ar, i - 1, result, NULL);
2831 : }
2832 : }
2833 :
2834 44935 : if (array->shape)
2835 : {
2836 12134 : mpz_init_set (*result, array->shape[dimen]);
2837 12134 : return true;
2838 : }
2839 :
2840 32801 : if (array->symtree->n.sym->attr.generic
2841 26 : && array->value.function.esym != NULL)
2842 : {
2843 25 : if (!spec_dimen_size (array->value.function.esym->as, dimen, result))
2844 : return false;
2845 : }
2846 32776 : else if (!spec_dimen_size (array->symtree->n.sym->as, dimen, result))
2847 : return false;
2848 :
2849 : break;
2850 :
2851 108490 : case EXPR_ARRAY:
2852 108490 : if (array->shape == NULL) {
2853 : /* Expressions with rank > 1 should have "shape" properly set */
2854 65760 : if ( array->rank != 1 )
2855 0 : gfc_internal_error ("gfc_array_dimen_size(): Bad EXPR_ARRAY expr");
2856 65760 : return gfc_array_size(array, result);
2857 : }
2858 :
2859 : /* Fall through */
2860 56865 : default:
2861 56865 : if (array->shape == NULL)
2862 : return false;
2863 :
2864 49024 : mpz_init_set (*result, array->shape[dimen]);
2865 :
2866 49024 : break;
2867 : }
2868 :
2869 : return true;
2870 : }
2871 :
2872 :
2873 : /* Given an array expression, figure out how many elements are in the
2874 : array. Returns true if this is possible, and sets the 'result'
2875 : variable. Otherwise returns false. */
2876 :
2877 : bool
2878 136497 : gfc_array_size (gfc_expr *array, mpz_t *result)
2879 : {
2880 136497 : expand_info expand_save;
2881 136497 : gfc_ref *ref;
2882 136497 : int i;
2883 136497 : bool t;
2884 :
2885 136497 : if (array->ts.type == BT_CLASS)
2886 : return false;
2887 :
2888 135292 : switch (array->expr_type)
2889 : {
2890 103104 : case EXPR_ARRAY:
2891 103104 : gfc_push_suppress_errors ();
2892 :
2893 103104 : expand_save = current_expand;
2894 :
2895 103104 : current_expand.count = result;
2896 103104 : mpz_init_set_ui (*result, 0);
2897 :
2898 103104 : current_expand.expand_work_function = count_elements;
2899 103104 : iter_stack = NULL;
2900 :
2901 103104 : t = expand_constructor (array->value.constructor);
2902 :
2903 103104 : gfc_pop_suppress_errors ();
2904 :
2905 103104 : if (!t)
2906 2426 : mpz_clear (*result);
2907 103104 : current_expand = expand_save;
2908 103104 : return t;
2909 :
2910 27215 : case EXPR_VARIABLE:
2911 29776 : for (ref = array->ref; ref; ref = ref->next)
2912 : {
2913 29775 : if (ref->type != REF_ARRAY)
2914 2063 : continue;
2915 :
2916 27712 : if (ref->u.ar.type == AR_FULL)
2917 23770 : return spec_size (ref->u.ar.as, result);
2918 :
2919 3942 : if (ref->u.ar.type == AR_SECTION)
2920 3444 : return ref_size (&ref->u.ar, result);
2921 : }
2922 :
2923 1 : return spec_size (array->symtree->n.sym->as, result);
2924 :
2925 :
2926 4973 : default:
2927 4973 : if (array->rank == 0 || array->shape == NULL)
2928 : return false;
2929 :
2930 3497 : mpz_init_set_ui (*result, 1);
2931 :
2932 10865 : for (i = 0; i < array->rank; i++)
2933 3871 : mpz_mul (*result, *result, array->shape[i]);
2934 :
2935 : break;
2936 : }
2937 :
2938 : return true;
2939 : }
2940 :
2941 :
2942 : /* Given an array reference, return the shape of the reference in an
2943 : array of mpz_t integers. */
2944 :
2945 : bool
2946 11209 : gfc_array_ref_shape (gfc_array_ref *ar, mpz_t *shape)
2947 : {
2948 11209 : int d;
2949 11209 : int i;
2950 :
2951 11209 : d = 0;
2952 :
2953 11209 : switch (ar->type)
2954 : {
2955 : case AR_FULL:
2956 17780 : for (; d < ar->as->rank; d++)
2957 15403 : if (!spec_dimen_size (ar->as, d, &shape[d]))
2958 8753 : goto cleanup;
2959 :
2960 : return true;
2961 :
2962 : case AR_SECTION:
2963 199 : for (i = 0; i < ar->dimen; i++)
2964 : {
2965 157 : if (ar->dimen_type[i] != DIMEN_ELEMENT)
2966 : {
2967 129 : if (!gfc_ref_dimen_size (ar, i, &shape[d], NULL))
2968 37 : goto cleanup;
2969 92 : d++;
2970 : }
2971 : }
2972 :
2973 : return true;
2974 :
2975 : default:
2976 : break;
2977 : }
2978 :
2979 8790 : cleanup:
2980 8790 : gfc_clear_shape (shape, d);
2981 8790 : return false;
2982 : }
2983 :
2984 :
2985 : /* Given an array expression, find the array reference structure that
2986 : characterizes the reference. */
2987 :
2988 : gfc_array_ref *
2989 101459 : gfc_find_array_ref (gfc_expr *e, bool allow_null)
2990 : {
2991 101459 : gfc_ref *ref;
2992 :
2993 113603 : for (ref = e->ref; ref; ref = ref->next)
2994 105887 : if (ref->type == REF_ARRAY
2995 99114 : && (ref->u.ar.type == AR_FULL || ref->u.ar.type == AR_SECTION))
2996 : break;
2997 :
2998 101459 : if (ref == NULL)
2999 : {
3000 7716 : if (allow_null)
3001 : return NULL;
3002 : else
3003 0 : gfc_internal_error ("gfc_find_array_ref(): No ref found");
3004 : }
3005 :
3006 93743 : return &ref->u.ar;
3007 : }
3008 :
3009 :
3010 : /* Find out if an array shape is known at compile time. */
3011 :
3012 : bool
3013 76 : gfc_is_compile_time_shape (gfc_array_spec *as)
3014 : {
3015 76 : if (as->type != AS_EXPLICIT)
3016 : return false;
3017 :
3018 156 : for (int i = 0; i < as->rank; i++)
3019 92 : if (!gfc_is_constant_expr (as->lower[i])
3020 92 : || !gfc_is_constant_expr (as->upper[i]))
3021 : return false;
3022 :
3023 : return true;
3024 : }
|