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 : 168344 : gfc_copy_array_ref (gfc_array_ref *src)
36 : : {
37 : 168344 : gfc_array_ref *dest;
38 : 168344 : int i;
39 : :
40 : 168344 : if (src == NULL)
41 : : return NULL;
42 : :
43 : 168344 : dest = gfc_get_array_ref ();
44 : :
45 : 168344 : *dest = *src;
46 : :
47 : 2693504 : for (i = 0; i < GFC_MAX_DIMENSIONS; i++)
48 : : {
49 : 2525160 : dest->start[i] = gfc_copy_expr (src->start[i]);
50 : 2525160 : dest->end[i] = gfc_copy_expr (src->end[i]);
51 : 2525160 : dest->stride[i] = gfc_copy_expr (src->stride[i]);
52 : : }
53 : :
54 : 168344 : dest->stat = gfc_copy_expr (src->stat);
55 : 168344 : dest->team = gfc_copy_expr (src->team);
56 : :
57 : 168344 : 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 : 359513 : match_subscript (gfc_array_ref *ar, int init, bool match_star)
69 : : {
70 : 359513 : match m = MATCH_ERROR;
71 : 359513 : bool star = false;
72 : 359513 : int i;
73 : 359513 : bool saw_boz = false;
74 : :
75 : 359513 : i = ar->dimen + ar->codimen;
76 : :
77 : 359513 : gfc_gobble_whitespace ();
78 : 359513 : ar->c_where[i] = gfc_current_locus;
79 : 359513 : 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 : 359513 : ar->dimen_type[i] = DIMEN_UNKNOWN;
86 : :
87 : 359513 : if (gfc_match_char (':') == MATCH_YES)
88 : 43499 : goto end_element;
89 : :
90 : : /* Get start element. */
91 : 316014 : if (match_star && (m = gfc_match_char ('*')) == MATCH_YES)
92 : : star = true;
93 : :
94 : 316014 : if (!star && init)
95 : 1636 : m = gfc_match_init_expr (&ar->start[i]);
96 : 314378 : else if (!star)
97 : 313717 : m = gfc_match_expr (&ar->start[i]);
98 : :
99 : 316014 : 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 : 316014 : if (m == MATCH_NO)
106 : 4 : gfc_error ("Expected array subscript at %C");
107 : 316014 : if (m != MATCH_YES)
108 : : return MATCH_ERROR;
109 : :
110 : 316004 : if (gfc_match_char (':') == MATCH_NO)
111 : 281137 : goto matched;
112 : :
113 : 34867 : 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 : 34867 : end_element:
122 : 78366 : ar->dimen_type[i] = DIMEN_RANGE;
123 : :
124 : 78366 : if (match_star && (m = gfc_match_char ('*')) == MATCH_YES)
125 : : star = true;
126 : 78210 : else if (init)
127 : 371 : m = gfc_match_init_expr (&ar->end[i]);
128 : : else
129 : 77839 : m = gfc_match_expr (&ar->end[i]);
130 : :
131 : 78366 : 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 : 78366 : if (m == MATCH_ERROR)
138 : : return MATCH_ERROR;
139 : :
140 : 78366 : 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 : 78364 : if (gfc_match_char (':') == MATCH_YES)
149 : : {
150 : 16646 : if (star)
151 : : {
152 : 0 : gfc_error ("Strides not allowed in coarray subscript at %C");
153 : 0 : return MATCH_ERROR;
154 : : }
155 : :
156 : 16646 : m = init ? gfc_match_init_expr (&ar->stride[i])
157 : 16645 : : gfc_match_expr (&ar->stride[i]);
158 : :
159 : 16646 : 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 : 16646 : if (m == MATCH_NO)
166 : 0 : gfc_error ("Expected array subscript stride at %C");
167 : 16646 : if (m != MATCH_YES)
168 : : return MATCH_ERROR;
169 : : }
170 : :
171 : 61718 : matched:
172 : 359501 : if (star)
173 : 815 : ar->dimen_type[i] = DIMEN_STAR;
174 : :
175 : 359501 : return (saw_boz ? MATCH_ERROR : MATCH_YES);
176 : : }
177 : :
178 : : /** Match one of TEAM=, TEAM_NUMBER= or STAT=. */
179 : :
180 : : match
181 : 4860 : match_team_or_stat (gfc_array_ref *ar)
182 : : {
183 : 4860 : gfc_expr *tmp;
184 : 4860 : bool team_error = false;
185 : :
186 : 4860 : if (gfc_match (" team = %e", &tmp) == MATCH_YES)
187 : : {
188 : 50 : if (ar->team == NULL && ar->team_type == TEAM_UNSET)
189 : : {
190 : 46 : ar->team = tmp;
191 : 46 : 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 : 4810 : else if (gfc_match (" team_number = %e", &tmp) == MATCH_YES)
202 : : {
203 : 68 : if (!gfc_notify_std (GFC_STD_F2018, "TEAM_NUMBER= not supported at %C"))
204 : : return MATCH_ERROR;
205 : 66 : if (ar->team == NULL && ar->team_type == TEAM_UNSET)
206 : : {
207 : 64 : ar->team = tmp;
208 : 64 : 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 : 4742 : else if (gfc_match (" stat = %e", &tmp) == MATCH_YES)
219 : : {
220 : 79 : if (ar->stat == NULL)
221 : : {
222 : 75 : 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 : 73 : 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 : 185 : 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 : 719149 : gfc_match_array_ref (gfc_array_ref *ar, gfc_array_spec *as, int init,
255 : : int corank, bool coarray_only)
256 : : {
257 : 719149 : match m;
258 : 719149 : bool matched_bracket = false;
259 : :
260 : 719149 : memset (ar, '\0', sizeof (*ar));
261 : :
262 : 719149 : ar->where = gfc_current_locus;
263 : 719149 : ar->as = as;
264 : 719149 : ar->type = AR_UNKNOWN;
265 : :
266 : 719149 : if (gfc_match_char ('[') == MATCH_YES)
267 : : {
268 : 3017 : matched_bracket = true;
269 : 3017 : goto coarray;
270 : : }
271 : 716132 : else if (coarray_only && corank != 0)
272 : 1236 : goto coarray;
273 : :
274 : 714896 : if (gfc_match_char ('(') != MATCH_YES)
275 : : {
276 : 441770 : ar->type = AR_FULL;
277 : 441770 : ar->dimen = 0;
278 : 441770 : if (corank != 0)
279 : : {
280 : 150704 : for (int i = 0; i < GFC_MAX_DIMENSIONS; ++i)
281 : 141285 : ar->dimen_type[i] = DIMEN_THIS_IMAGE;
282 : 9419 : ar->codimen = corank;
283 : : }
284 : 441770 : return MATCH_YES;
285 : : }
286 : :
287 : 354848 : for (ar->dimen = 0; ar->dimen < GFC_MAX_DIMENSIONS; ar->dimen++)
288 : : {
289 : 354848 : m = match_subscript (ar, init, false);
290 : 354848 : if (m == MATCH_ERROR)
291 : : return MATCH_ERROR;
292 : :
293 : 354835 : if (gfc_match_char (')') == MATCH_YES)
294 : : {
295 : 273105 : ar->dimen++;
296 : 273105 : goto coarray;
297 : : }
298 : :
299 : 81730 : 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 : 277358 : coarray:
316 : 277358 : if (!matched_bracket && gfc_match_char ('[') != MATCH_YES)
317 : : {
318 : 273250 : int dim = coarray_only ? 0 : ar->dimen;
319 : 273250 : if (dim > 0 || coarray_only)
320 : : {
321 : 273250 : if (corank != 0)
322 : : {
323 : 70334 : for (int i = dim; i < GFC_MAX_DIMENSIONS; ++i)
324 : 65702 : ar->dimen_type[i] = DIMEN_THIS_IMAGE;
325 : 4632 : ar->codimen = corank;
326 : : }
327 : 273250 : return MATCH_YES;
328 : : }
329 : : else
330 : : return MATCH_ERROR;
331 : : }
332 : :
333 : 4108 : 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 : 4108 : if (corank == 0)
340 : : {
341 : 0 : gfc_error ("Unexpected coarray designator at %C");
342 : 0 : return MATCH_ERROR;
343 : : }
344 : :
345 : 4108 : ar->team_type = TEAM_UNSET;
346 : :
347 : 4665 : for (ar->codimen = 0; ar->codimen + ar->dimen < GFC_MAX_DIMENSIONS;
348 : : ar->codimen++)
349 : : {
350 : 4665 : m = match_subscript (ar, init, true);
351 : 4665 : if (m == MATCH_ERROR)
352 : : return MATCH_ERROR;
353 : :
354 : 4663 : if (gfc_match_char (',') != MATCH_YES)
355 : : {
356 : 3957 : 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 : 3957 : goto image_selector;
362 : : }
363 : 0 : return MATCH_ERROR;
364 : : }
365 : 706 : 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 : 704 : m = match_team_or_stat (ar);
373 : 704 : if (m == MATCH_ERROR)
374 : : return MATCH_ERROR;
375 : 700 : else if (m == MATCH_YES)
376 : 139 : goto image_selector;
377 : :
378 : 561 : if (gfc_match_char (']') == MATCH_YES)
379 : 0 : goto rank_check;
380 : :
381 : 561 : 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 : 4156 : image_selector:
394 : 4156 : for (;;)
395 : : {
396 : 4156 : m = match_team_or_stat (ar);
397 : 4156 : if (m == MATCH_ERROR)
398 : : return MATCH_ERROR;
399 : :
400 : 4146 : if (gfc_match_char (']') == MATCH_YES)
401 : 4074 : goto rank_check;
402 : :
403 : 72 : 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 : 4074 : rank_check:
413 : 4074 : ar->codimen++;
414 : 4074 : 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 : 4056 : 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 : 7026227 : gfc_free_array_spec (gfc_array_spec *as)
437 : : {
438 : 7026227 : int i;
439 : :
440 : 7026227 : if (as == NULL)
441 : : return;
442 : :
443 : 422407 : if (as->corank == 0)
444 : : {
445 : 635271 : for (i = 0; i < as->rank; i++)
446 : : {
447 : 215721 : gfc_free_expr (as->lower[i]);
448 : 215721 : gfc_free_expr (as->upper[i]);
449 : : }
450 : : }
451 : : else
452 : : {
453 : 2857 : int n = as->rank + as->corank - (as->cotype == AS_EXPLICIT ? 1 : 0);
454 : 6472 : for (i = 0; i < n; i++)
455 : : {
456 : 3615 : gfc_free_expr (as->lower[i]);
457 : 3615 : gfc_free_expr (as->upper[i]);
458 : : }
459 : : }
460 : :
461 : 422407 : 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 : 337447 : resolve_array_bound (gfc_expr *e, int check_constant)
470 : : {
471 : 337447 : if (e == NULL)
472 : : return true;
473 : :
474 : 194164 : if (!gfc_resolve_expr (e)
475 : 194164 : || !gfc_specification_expr (e))
476 : 36 : return false;
477 : :
478 : 194128 : 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 : 2791130 : gfc_resolve_array_spec (gfc_array_spec *as, int check_constant)
498 : : {
499 : 2791130 : gfc_expr *e;
500 : 2791130 : int i;
501 : :
502 : 2791130 : if (as == NULL)
503 : : return true;
504 : :
505 : 334268 : if (as->resolved)
506 : : return true;
507 : :
508 : 330178 : for (i = 0; i < as->rank + as->corank; i++)
509 : : {
510 : 168729 : if (i == GFC_MAX_DIMENSIONS)
511 : : return false;
512 : :
513 : 168726 : e = as->lower[i];
514 : 168726 : if (!resolve_array_bound (e, check_constant))
515 : : return false;
516 : :
517 : 168721 : e = as->upper[i];
518 : 168721 : if (!resolve_array_bound (e, check_constant))
519 : : return false;
520 : :
521 : 168690 : if ((as->lower[i] == NULL) || (as->upper[i] == NULL))
522 : 97307 : continue;
523 : :
524 : : /* If the size is negative in this dimension, set it to zero. */
525 : 71383 : if (as->lower[i]->expr_type == EXPR_CONSTANT
526 : 70669 : && as->upper[i]->expr_type == EXPR_CONSTANT
527 : 61930 : && mpz_cmp (as->upper[i]->value.integer,
528 : 61930 : 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 : 161449 : as->resolved = true;
538 : :
539 : 161449 : 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 : 106881 : match_array_element_spec (gfc_array_spec *as)
565 : : {
566 : 106881 : gfc_expr **upper, **lower;
567 : 106881 : match m;
568 : 106881 : int rank;
569 : 106881 : bool is_pdt_template;
570 : :
571 : 106881 : rank = as->rank == -1 ? 0 : as->rank;
572 : 106881 : lower = &as->lower[rank + as->corank - 1];
573 : 106881 : upper = &as->upper[rank + as->corank - 1];
574 : :
575 : 106881 : if (gfc_match_char ('*') == MATCH_YES)
576 : : {
577 : 7900 : *lower = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
578 : 7900 : return AS_ASSUMED_SIZE;
579 : : }
580 : :
581 : 98981 : if (gfc_match_char (':') == MATCH_YES)
582 : : {
583 : 38044 : locus old_loc = gfc_current_locus;
584 : 38044 : if (gfc_match_char ('*') == MATCH_YES)
585 : : {
586 : : /* F2018:R821: "assumed-implied-spec is [ lower-bound : ] *". */
587 : 3 : gfc_error ("A lower bound must precede colon in "
588 : : "assumed-size array specification at %L", &old_loc);
589 : 3 : return AS_UNKNOWN;
590 : : }
591 : : else
592 : : {
593 : : return AS_DEFERRED;
594 : : }
595 : : }
596 : :
597 : 60937 : m = gfc_match_expr (upper);
598 : 60937 : if (m == MATCH_NO)
599 : 1 : gfc_error ("Expected expression in array specification at %C");
600 : 60937 : if (m != MATCH_YES)
601 : : return AS_UNKNOWN;
602 : 60935 : if (!gfc_expr_check_typed (*upper, gfc_current_ns, false))
603 : : return AS_UNKNOWN;
604 : :
605 : 60928 : if (((*upper)->expr_type == EXPR_CONSTANT
606 : 60928 : && (*upper)->ts.type != BT_INTEGER) ||
607 : : ((*upper)->expr_type == EXPR_FUNCTION
608 : 679 : && (*upper)->ts.type == BT_UNKNOWN
609 : 678 : && (*upper)->symtree
610 : 678 : && strcmp ((*upper)->symtree->name, "null") == 0))
611 : : {
612 : 5 : gfc_error ("Expecting a scalar INTEGER expression at %C, found %s",
613 : : gfc_basic_typename ((*upper)->ts.type));
614 : 5 : return AS_UNKNOWN;
615 : : }
616 : :
617 : 121846 : is_pdt_template = gfc_current_block ()
618 : 54716 : && gfc_current_block ()->attr.pdt_template
619 : 61150 : && gfc_current_block ()->f2k_derived;
620 : :
621 : 60923 : if ((*upper)->expr_type != EXPR_CONSTANT && is_pdt_template)
622 : 210 : gfc_correct_parm_expr (gfc_current_block (), upper);
623 : :
624 : 60923 : if (gfc_match_char (':') == MATCH_NO)
625 : : {
626 : 53834 : *lower = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
627 : 53834 : return AS_EXPLICIT;
628 : : }
629 : :
630 : 7089 : *lower = *upper;
631 : 7089 : *upper = NULL;
632 : :
633 : 7089 : if (gfc_match_char ('*') == MATCH_YES)
634 : : return AS_ASSUMED_SIZE;
635 : :
636 : 6609 : m = gfc_match_expr (upper);
637 : 6609 : if (m == MATCH_ERROR)
638 : : return AS_UNKNOWN;
639 : 6609 : if (m == MATCH_NO)
640 : : return AS_ASSUMED_SHAPE;
641 : 5760 : if (!gfc_expr_check_typed (*upper, gfc_current_ns, false))
642 : : return AS_UNKNOWN;
643 : :
644 : 5760 : if (((*upper)->expr_type == EXPR_CONSTANT
645 : 5760 : && (*upper)->ts.type != BT_INTEGER) ||
646 : : ((*upper)->expr_type == EXPR_FUNCTION
647 : 76 : && (*upper)->ts.type == BT_UNKNOWN
648 : 76 : && (*upper)->symtree
649 : 76 : && strcmp ((*upper)->symtree->name, "null") == 0))
650 : : {
651 : 1 : gfc_error ("Expecting a scalar INTEGER expression at %C, found %s",
652 : : gfc_basic_typename ((*upper)->ts.type));
653 : 1 : return AS_UNKNOWN;
654 : : }
655 : :
656 : 5759 : if ((*upper)->expr_type != EXPR_CONSTANT && is_pdt_template)
657 : 6 : gfc_correct_parm_expr (gfc_current_block (), upper);
658 : :
659 : : return AS_EXPLICIT;
660 : : }
661 : :
662 : :
663 : : /* Matches an array specification, incidentally figuring out what sort
664 : : it is. Match either a normal array specification, or a coarray spec
665 : : or both. Optionally allow [:] for coarrays. */
666 : :
667 : : match
668 : 299859 : gfc_match_array_spec (gfc_array_spec **asp, bool match_dim, bool match_codim)
669 : : {
670 : 299859 : array_type current_type;
671 : 299859 : gfc_array_spec *as;
672 : 299859 : int i;
673 : :
674 : 299859 : as = gfc_get_array_spec ();
675 : :
676 : 299859 : if (!match_dim)
677 : 73 : goto coarray;
678 : :
679 : 299786 : if (gfc_match_char ('(') != MATCH_YES)
680 : : {
681 : 218019 : if (!match_codim)
682 : 1160 : goto done;
683 : 216859 : goto coarray;
684 : : }
685 : :
686 : 81767 : if (gfc_match (" .. )") == MATCH_YES)
687 : : {
688 : 4901 : as->type = AS_ASSUMED_RANK;
689 : 4901 : as->rank = -1;
690 : :
691 : 4901 : if (!gfc_notify_std (GFC_STD_F2018, "Assumed-rank array at %C"))
692 : 29 : goto cleanup;
693 : :
694 : 4872 : if (!match_codim)
695 : 2092 : goto done;
696 : 2780 : goto coarray;
697 : : }
698 : :
699 : 104883 : for (;;)
700 : : {
701 : 104883 : as->rank++;
702 : 104883 : current_type = match_array_element_spec (as);
703 : 104883 : if (current_type == AS_UNKNOWN)
704 : 17 : goto cleanup;
705 : :
706 : : /* Note that current_type == AS_ASSUMED_SIZE for both assumed-size
707 : : and implied-shape specifications. If the rank is at least 2, we can
708 : : distinguish between them. But for rank 1, we currently return
709 : : ASSUMED_SIZE; this gets adjusted later when we know for sure
710 : : whether the symbol parsed is a PARAMETER or not. */
711 : :
712 : 104866 : if (as->rank == 1)
713 : : {
714 : 76850 : as->type = current_type;
715 : : }
716 : : else
717 : 28016 : switch (as->type)
718 : : { /* See how current spec meshes with the existing. */
719 : 0 : case AS_UNKNOWN:
720 : 0 : goto cleanup;
721 : :
722 : 41 : case AS_IMPLIED_SHAPE:
723 : 41 : if (current_type != AS_ASSUMED_SIZE)
724 : : {
725 : 3 : gfc_error ("Bad array specification for implied-shape"
726 : : " array at %C");
727 : 3 : goto cleanup;
728 : : }
729 : : break;
730 : :
731 : 17498 : case AS_EXPLICIT:
732 : 17498 : if (current_type == AS_ASSUMED_SIZE)
733 : : {
734 : 623 : as->type = AS_ASSUMED_SIZE;
735 : 623 : break;
736 : : }
737 : :
738 : 16875 : if (current_type == AS_EXPLICIT)
739 : : break;
740 : :
741 : 0 : gfc_error ("Bad array specification for an explicitly shaped "
742 : : "array at %C");
743 : :
744 : 0 : goto cleanup;
745 : :
746 : 236 : case AS_ASSUMED_SHAPE:
747 : 236 : if ((current_type == AS_ASSUMED_SHAPE)
748 : 236 : || (current_type == AS_DEFERRED))
749 : : break;
750 : :
751 : 0 : gfc_error ("Bad array specification for assumed shape "
752 : : "array at %C");
753 : 0 : goto cleanup;
754 : :
755 : 10164 : case AS_DEFERRED:
756 : 10164 : if (current_type == AS_DEFERRED)
757 : : break;
758 : :
759 : 1 : if (current_type == AS_ASSUMED_SHAPE)
760 : : {
761 : 0 : as->type = AS_ASSUMED_SHAPE;
762 : 0 : break;
763 : : }
764 : :
765 : 1 : gfc_error ("Bad specification for deferred shape array at %C");
766 : 1 : goto cleanup;
767 : :
768 : 77 : case AS_ASSUMED_SIZE:
769 : 77 : if (as->rank == 2 && current_type == AS_ASSUMED_SIZE)
770 : : {
771 : 75 : as->type = AS_IMPLIED_SHAPE;
772 : 75 : break;
773 : : }
774 : :
775 : 2 : gfc_error ("Bad specification for assumed size array at %C");
776 : 2 : goto cleanup;
777 : :
778 : 0 : case AS_ASSUMED_RANK:
779 : 0 : gcc_unreachable ();
780 : : }
781 : :
782 : 104860 : if (gfc_match_char (')') == MATCH_YES)
783 : : break;
784 : :
785 : 28023 : if (gfc_match_char (',') != MATCH_YES)
786 : : {
787 : 2 : gfc_error ("Expected another dimension in array declaration at %C");
788 : 2 : goto cleanup;
789 : : }
790 : :
791 : 28021 : if (as->rank + as->corank >= GFC_MAX_DIMENSIONS)
792 : : {
793 : 2 : gfc_error ("Array specification at %C has more than %d dimensions",
794 : : GFC_MAX_DIMENSIONS);
795 : 2 : goto cleanup;
796 : : }
797 : :
798 : 28019 : if (as->corank + as->rank >= 7
799 : 28019 : && !gfc_notify_std (GFC_STD_F2008, "Array specification at %C "
800 : : "with more than 7 dimensions"))
801 : 2 : goto cleanup;
802 : : }
803 : :
804 : 76837 : if (!match_codim)
805 : 19058 : goto done;
806 : :
807 : 57779 : coarray:
808 : 277491 : if (gfc_match_char ('[') != MATCH_YES)
809 : 276039 : goto done;
810 : :
811 : 1452 : if (!gfc_notify_std (GFC_STD_F2008, "Coarray declaration at %C"))
812 : 3 : goto cleanup;
813 : :
814 : 1449 : if (flag_coarray == GFC_FCOARRAY_NONE)
815 : : {
816 : 1 : gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to enable");
817 : : goto cleanup;
818 : : }
819 : :
820 : 1448 : if (as->rank >= GFC_MAX_DIMENSIONS)
821 : : {
822 : 0 : gfc_error ("Array specification at %C has more than %d "
823 : : "dimensions", GFC_MAX_DIMENSIONS);
824 : 0 : goto cleanup;
825 : : }
826 : :
827 : 1998 : for (;;)
828 : : {
829 : 1998 : as->corank++;
830 : 1998 : current_type = match_array_element_spec (as);
831 : :
832 : 1998 : if (current_type == AS_UNKNOWN)
833 : 1 : goto cleanup;
834 : :
835 : 1997 : if (as->corank == 1)
836 : 1447 : as->cotype = current_type;
837 : : else
838 : 550 : switch (as->cotype)
839 : : { /* See how current spec meshes with the existing. */
840 : 0 : case AS_IMPLIED_SHAPE:
841 : 0 : case AS_UNKNOWN:
842 : 0 : goto cleanup;
843 : :
844 : 361 : case AS_EXPLICIT:
845 : 361 : if (current_type == AS_ASSUMED_SIZE)
846 : : {
847 : 186 : as->cotype = AS_ASSUMED_SIZE;
848 : 186 : break;
849 : : }
850 : :
851 : 175 : if (current_type == AS_EXPLICIT)
852 : : break;
853 : :
854 : 0 : gfc_error ("Bad array specification for an explicitly "
855 : : "shaped array at %C");
856 : :
857 : 0 : goto cleanup;
858 : :
859 : 0 : case AS_ASSUMED_SHAPE:
860 : 0 : if ((current_type == AS_ASSUMED_SHAPE)
861 : 0 : || (current_type == AS_DEFERRED))
862 : : break;
863 : :
864 : 0 : gfc_error ("Bad array specification for assumed shape "
865 : : "array at %C");
866 : 0 : goto cleanup;
867 : :
868 : 189 : case AS_DEFERRED:
869 : 189 : if (current_type == AS_DEFERRED)
870 : : break;
871 : :
872 : 0 : if (current_type == AS_ASSUMED_SHAPE)
873 : : {
874 : 0 : as->cotype = AS_ASSUMED_SHAPE;
875 : 0 : break;
876 : : }
877 : :
878 : 0 : gfc_error ("Bad specification for deferred shape array at %C");
879 : 0 : goto cleanup;
880 : :
881 : 0 : case AS_ASSUMED_SIZE:
882 : 0 : gfc_error ("Bad specification for assumed size array at %C");
883 : 0 : goto cleanup;
884 : :
885 : 0 : case AS_ASSUMED_RANK:
886 : 0 : gcc_unreachable ();
887 : : }
888 : :
889 : 1997 : if (gfc_match_char (']') == MATCH_YES)
890 : : break;
891 : :
892 : 552 : if (gfc_match_char (',') != MATCH_YES)
893 : : {
894 : 0 : gfc_error ("Expected another dimension in array declaration at %C");
895 : 0 : goto cleanup;
896 : : }
897 : :
898 : 552 : if (as->rank + as->corank >= GFC_MAX_DIMENSIONS)
899 : : {
900 : 2 : gfc_error ("Array specification at %C has more than %d "
901 : : "dimensions", GFC_MAX_DIMENSIONS);
902 : 2 : goto cleanup;
903 : : }
904 : : }
905 : :
906 : 1445 : if (current_type == AS_EXPLICIT)
907 : : {
908 : 1 : gfc_error ("Upper bound of last coarray dimension must be %<*%> at %C");
909 : 1 : goto cleanup;
910 : : }
911 : :
912 : 1444 : if (as->cotype == AS_ASSUMED_SIZE)
913 : 837 : as->cotype = AS_EXPLICIT;
914 : :
915 : 1444 : if (as->rank == 0)
916 : 824 : as->type = as->cotype;
917 : :
918 : 620 : done:
919 : 299793 : if (as->rank == 0 && as->corank == 0)
920 : : {
921 : 217261 : *asp = NULL;
922 : 217261 : gfc_free_array_spec (as);
923 : 217261 : return MATCH_NO;
924 : : }
925 : :
926 : : /* If a lower bounds of an assumed shape array is blank, put in one. */
927 : 82532 : if (as->type == AS_ASSUMED_SHAPE)
928 : : {
929 : 1466 : for (i = 0; i < as->rank + as->corank; i++)
930 : : {
931 : 853 : if (as->lower[i] == NULL)
932 : 1 : as->lower[i] = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
933 : : }
934 : : }
935 : :
936 : 82532 : *asp = as;
937 : :
938 : 82532 : return MATCH_YES;
939 : :
940 : 65 : cleanup:
941 : : /* Something went wrong. */
942 : 65 : gfc_free_array_spec (as);
943 : 65 : return MATCH_ERROR;
944 : : }
945 : :
946 : : /* Given a symbol and an array specification, modify the symbol to
947 : : have that array specification. The error locus is needed in case
948 : : something goes wrong. On failure, the caller must free the spec. */
949 : :
950 : : bool
951 : 265064 : gfc_set_array_spec (gfc_symbol *sym, gfc_array_spec *as, locus *error_loc)
952 : : {
953 : 265064 : int i;
954 : 265064 : symbol_attribute *attr;
955 : :
956 : 265064 : if (as == NULL)
957 : : return true;
958 : :
959 : : /* If the symbol corresponds to a submodule module procedure the array spec is
960 : : already set, so do not attempt to set it again here. */
961 : 79025 : attr = &sym->attr;
962 : 79025 : if (gfc_submodule_procedure(attr))
963 : : return true;
964 : :
965 : 79024 : if (as->rank
966 : 79024 : && !gfc_add_dimension (&sym->attr, sym->name, error_loc))
967 : : return false;
968 : :
969 : 79018 : if (as->corank
970 : 79018 : && !gfc_add_codimension (&sym->attr, sym->name, error_loc))
971 : : return false;
972 : :
973 : 79013 : if (sym->as == NULL)
974 : : {
975 : 78990 : sym->as = as;
976 : 78990 : return true;
977 : : }
978 : :
979 : 23 : if ((sym->as->type == AS_ASSUMED_RANK && as->corank)
980 : 21 : || (as->type == AS_ASSUMED_RANK && sym->as->corank))
981 : : {
982 : 4 : gfc_error ("The assumed-rank array %qs at %L shall not have a "
983 : : "codimension", sym->name, error_loc);
984 : 4 : return false;
985 : : }
986 : :
987 : : /* Check F2018:C822. */
988 : 19 : if (sym->as->rank + sym->as->corank > GFC_MAX_DIMENSIONS)
989 : 2 : goto too_many;
990 : :
991 : 17 : if (as->corank)
992 : : {
993 : 7 : sym->as->cotype = as->cotype;
994 : 7 : sym->as->corank = as->corank;
995 : : /* Check F2018:C822. */
996 : 7 : if (sym->as->rank + sym->as->corank > GFC_MAX_DIMENSIONS)
997 : 2 : goto too_many;
998 : :
999 : 10 : for (i = 0; i < as->corank; i++)
1000 : : {
1001 : 5 : sym->as->lower[sym->as->rank + i] = as->lower[i];
1002 : 5 : sym->as->upper[sym->as->rank + i] = as->upper[i];
1003 : : }
1004 : : }
1005 : : else
1006 : : {
1007 : : /* The "sym" has no rank (checked via gfc_add_dimension). Thus
1008 : : the dimension is added - but first the codimensions (if existing
1009 : : need to be shifted to make space for the dimension. */
1010 : 10 : gcc_assert (as->corank == 0 && sym->as->rank == 0);
1011 : :
1012 : 10 : sym->as->rank = as->rank;
1013 : 10 : sym->as->type = as->type;
1014 : 10 : sym->as->cray_pointee = as->cray_pointee;
1015 : 10 : sym->as->cp_was_assumed = as->cp_was_assumed;
1016 : :
1017 : : /* Check F2018:C822. */
1018 : 10 : if (sym->as->rank + sym->as->corank > GFC_MAX_DIMENSIONS)
1019 : 1 : goto too_many;
1020 : :
1021 : 42 : for (i = sym->as->corank - 1; i >= 0; i--)
1022 : : {
1023 : 33 : sym->as->lower[as->rank + i] = sym->as->lower[i];
1024 : 33 : sym->as->upper[as->rank + i] = sym->as->upper[i];
1025 : : }
1026 : 31 : for (i = 0; i < as->rank; i++)
1027 : : {
1028 : 22 : sym->as->lower[i] = as->lower[i];
1029 : 22 : sym->as->upper[i] = as->upper[i];
1030 : : }
1031 : : }
1032 : :
1033 : 14 : free (as);
1034 : 14 : return true;
1035 : :
1036 : 5 : too_many:
1037 : :
1038 : 5 : gfc_error ("rank + corank of %qs exceeds %d at %C", sym->name,
1039 : : GFC_MAX_DIMENSIONS);
1040 : 5 : return false;
1041 : : }
1042 : :
1043 : :
1044 : : /* Copy an array specification. */
1045 : :
1046 : : gfc_array_spec *
1047 : 288857 : gfc_copy_array_spec (gfc_array_spec *src)
1048 : : {
1049 : 288857 : gfc_array_spec *dest;
1050 : 288857 : int i;
1051 : :
1052 : 288857 : if (src == NULL)
1053 : : return NULL;
1054 : :
1055 : 45236 : dest = gfc_get_array_spec ();
1056 : :
1057 : 45236 : *dest = *src;
1058 : :
1059 : 89828 : for (i = 0; i < dest->rank + dest->corank; i++)
1060 : : {
1061 : 44592 : dest->lower[i] = gfc_copy_expr (dest->lower[i]);
1062 : 44592 : dest->upper[i] = gfc_copy_expr (dest->upper[i]);
1063 : : }
1064 : :
1065 : : return dest;
1066 : : }
1067 : :
1068 : :
1069 : : /* Returns nonzero if the two expressions are equal.
1070 : : We should not need to support more than constant values, as that's what is
1071 : : allowed in derived type component array spec. However, we may create types
1072 : : with non-constant array spec for dummy variable class container types, for
1073 : : which the _data component holds the array spec of the variable declaration.
1074 : : So we have to support non-constant bounds as well. */
1075 : :
1076 : : static bool
1077 : 390 : compare_bounds (gfc_expr *bound1, gfc_expr *bound2)
1078 : : {
1079 : 390 : if (bound1 == NULL || bound2 == NULL
1080 : 376 : || bound1->ts.type != BT_INTEGER
1081 : 376 : || bound2->ts.type != BT_INTEGER)
1082 : : return false;
1083 : :
1084 : : /* What qualifies as identical bounds? We could probably just check that the
1085 : : expressions are exact clones. We avoid rewriting a specific comparison
1086 : : function and re-use instead the rather involved gfc_dep_compare_expr which
1087 : : is just a bit more permissive, as it can also detect identical values for
1088 : : some mismatching expressions (extra parenthesis, swapped operands, unary
1089 : : plus, etc). It probably only makes a difference in corner cases. */
1090 : 375 : return gfc_dep_compare_expr (bound1, bound2) == 0;
1091 : : }
1092 : :
1093 : :
1094 : : /* Compares two array specifications. They must be constant or deferred
1095 : : shape. */
1096 : :
1097 : : bool
1098 : 698 : gfc_compare_array_spec (gfc_array_spec *as1, gfc_array_spec *as2)
1099 : : {
1100 : 698 : int i;
1101 : :
1102 : 698 : if (as1 == NULL && as2 == NULL)
1103 : : return 1;
1104 : :
1105 : 698 : if (as1 == NULL || as2 == NULL)
1106 : : return 0;
1107 : :
1108 : 698 : if (as1->rank != as2->rank)
1109 : : return 0;
1110 : :
1111 : 696 : if (as1->corank != as2->corank)
1112 : : return 0;
1113 : :
1114 : 696 : if (as1->rank == 0)
1115 : : return 1;
1116 : :
1117 : 683 : if (as1->type != as2->type)
1118 : : return 0;
1119 : :
1120 : 321 : if (as1->cotype != as2->cotype)
1121 : : return 0;
1122 : :
1123 : 321 : if (as1->type == AS_EXPLICIT)
1124 : 274 : for (i = 0; i < as1->rank + as1->corank; i++)
1125 : : {
1126 : 195 : if (!compare_bounds (as1->lower[i], as2->lower[i]))
1127 : : return 0;
1128 : :
1129 : 195 : if (!compare_bounds (as1->upper[i], as2->upper[i]))
1130 : : return 0;
1131 : : }
1132 : :
1133 : : return 1;
1134 : : }
1135 : :
1136 : :
1137 : : /****************** Array constructor functions ******************/
1138 : :
1139 : :
1140 : : /* Given an expression node that might be an array constructor and a
1141 : : symbol, make sure that no iterators in this or child constructors
1142 : : use the symbol as an implied-DO iterator. Returns nonzero if a
1143 : : duplicate was found. */
1144 : :
1145 : : static bool
1146 : 8408 : check_duplicate_iterator (gfc_constructor_base base, gfc_symbol *master)
1147 : : {
1148 : 8408 : gfc_constructor *c;
1149 : 8408 : gfc_expr *e;
1150 : :
1151 : 17123 : for (c = gfc_constructor_first (base); c; c = gfc_constructor_next (c))
1152 : : {
1153 : 8715 : e = c->expr;
1154 : :
1155 : 8715 : if (e->expr_type == EXPR_ARRAY
1156 : 8715 : && check_duplicate_iterator (e->value.constructor, master))
1157 : : return 1;
1158 : :
1159 : 8715 : if (c->iterator == NULL)
1160 : 8079 : continue;
1161 : :
1162 : 636 : if (c->iterator->var->symtree->n.sym == master)
1163 : : {
1164 : 0 : gfc_error ("DO-iterator %qs at %L is inside iterator of the "
1165 : : "same name", master->name, &c->where);
1166 : :
1167 : 0 : return 1;
1168 : : }
1169 : : }
1170 : :
1171 : : return 0;
1172 : : }
1173 : :
1174 : :
1175 : : /* Forward declaration because these functions are mutually recursive. */
1176 : : static match match_array_cons_element (gfc_constructor_base *);
1177 : :
1178 : : /* Match a list of array elements. */
1179 : :
1180 : : static match
1181 : 440081 : match_array_list (gfc_constructor_base *result)
1182 : : {
1183 : 440081 : gfc_constructor_base head;
1184 : 440081 : gfc_constructor *p;
1185 : 440081 : gfc_iterator iter;
1186 : 440081 : locus old_loc;
1187 : 440081 : gfc_expr *e;
1188 : 440081 : match m;
1189 : 440081 : int n;
1190 : :
1191 : 440081 : old_loc = gfc_current_locus;
1192 : :
1193 : 440081 : if (gfc_match_char ('(') == MATCH_NO)
1194 : : return MATCH_NO;
1195 : :
1196 : 9685 : memset (&iter, '\0', sizeof (gfc_iterator));
1197 : 9685 : head = NULL;
1198 : :
1199 : 9685 : m = match_array_cons_element (&head);
1200 : 9685 : if (m != MATCH_YES)
1201 : 147 : goto cleanup;
1202 : :
1203 : 9538 : if (gfc_match_char (',') != MATCH_YES)
1204 : : {
1205 : 329 : m = MATCH_NO;
1206 : 329 : goto cleanup;
1207 : : }
1208 : :
1209 : 58 : for (n = 1;; n++)
1210 : : {
1211 : 9267 : m = gfc_match_iterator (&iter, 0);
1212 : 9267 : if (m == MATCH_YES)
1213 : : break;
1214 : 1602 : if (m == MATCH_ERROR)
1215 : 1 : goto cleanup;
1216 : :
1217 : 1601 : m = match_array_cons_element (&head);
1218 : 1601 : if (m == MATCH_ERROR)
1219 : 0 : goto cleanup;
1220 : 1601 : if (m == MATCH_NO)
1221 : : {
1222 : 0 : if (n > 2)
1223 : 0 : goto syntax;
1224 : 0 : m = MATCH_NO;
1225 : 0 : goto cleanup; /* Could be a complex constant */
1226 : : }
1227 : :
1228 : 1601 : if (gfc_match_char (',') != MATCH_YES)
1229 : : {
1230 : 1543 : if (n > 2)
1231 : 0 : goto syntax;
1232 : 1543 : m = MATCH_NO;
1233 : 1543 : goto cleanup;
1234 : : }
1235 : : }
1236 : :
1237 : 7665 : if (gfc_match_char (')') != MATCH_YES)
1238 : 0 : goto syntax;
1239 : :
1240 : 7665 : if (check_duplicate_iterator (head, iter.var->symtree->n.sym))
1241 : : {
1242 : 0 : m = MATCH_ERROR;
1243 : 0 : goto cleanup;
1244 : : }
1245 : :
1246 : 7665 : e = gfc_get_array_expr (BT_UNKNOWN, 0, &old_loc);
1247 : 7665 : e->value.constructor = head;
1248 : :
1249 : 7665 : p = gfc_constructor_append_expr (result, e, &gfc_current_locus);
1250 : 7665 : p->iterator = gfc_get_iterator ();
1251 : 7665 : *p->iterator = iter;
1252 : :
1253 : 7665 : return MATCH_YES;
1254 : :
1255 : 0 : syntax:
1256 : 0 : gfc_error ("Syntax error in array constructor at %C");
1257 : 0 : m = MATCH_ERROR;
1258 : :
1259 : 2020 : cleanup:
1260 : 2020 : gfc_constructor_free (head);
1261 : 2020 : gfc_free_iterator (&iter, 0);
1262 : 2020 : gfc_current_locus = old_loc;
1263 : 2020 : return m;
1264 : : }
1265 : :
1266 : :
1267 : : /* Match a single element of an array constructor, which can be a
1268 : : single expression or a list of elements. */
1269 : :
1270 : : static match
1271 : 440081 : match_array_cons_element (gfc_constructor_base *result)
1272 : : {
1273 : 440081 : gfc_expr *expr;
1274 : 440081 : match m;
1275 : :
1276 : 440081 : m = match_array_list (result);
1277 : 440081 : if (m != MATCH_NO)
1278 : : return m;
1279 : :
1280 : 432414 : m = gfc_match_expr (&expr);
1281 : 432414 : if (m != MATCH_YES)
1282 : : return m;
1283 : :
1284 : 432259 : if (expr->ts.type == BT_BOZ)
1285 : : {
1286 : 4 : gfc_error ("BOZ literal constant at %L cannot appear in an "
1287 : : "array constructor", &expr->where);
1288 : 4 : goto done;
1289 : : }
1290 : :
1291 : 432255 : if (expr->expr_type == EXPR_FUNCTION
1292 : 13529 : && expr->ts.type == BT_UNKNOWN
1293 : 13529 : && strcmp(expr->symtree->name, "null") == 0)
1294 : : {
1295 : 3 : gfc_error ("NULL() at %C cannot appear in an array constructor");
1296 : 3 : goto done;
1297 : : }
1298 : :
1299 : 432252 : gfc_constructor_append_expr (result, expr, &gfc_current_locus);
1300 : 432252 : return MATCH_YES;
1301 : :
1302 : 7 : done:
1303 : 7 : gfc_free_expr (expr);
1304 : 7 : return MATCH_ERROR;
1305 : : }
1306 : :
1307 : :
1308 : : /* Convert components of an array constructor to the type in ts. */
1309 : :
1310 : : static match
1311 : 3486 : walk_array_constructor (gfc_typespec *ts, gfc_constructor_base head)
1312 : : {
1313 : 3486 : gfc_constructor *c;
1314 : 3486 : gfc_expr *e;
1315 : 3486 : match m;
1316 : :
1317 : 7576 : for (c = gfc_constructor_first (head); c; c = gfc_constructor_next (c))
1318 : : {
1319 : 4098 : e = c->expr;
1320 : 4098 : if (e->expr_type == EXPR_ARRAY && e->ts.type == BT_UNKNOWN
1321 : 48 : && !e->ref && e->value.constructor)
1322 : : {
1323 : 48 : m = walk_array_constructor (ts, e->value.constructor);
1324 : 48 : if (m == MATCH_ERROR)
1325 : : return m;
1326 : : }
1327 : 4050 : else if (!gfc_convert_type_warn (e, ts, 1, 1, true)
1328 : 4050 : && e->ts.type != BT_UNKNOWN)
1329 : : return MATCH_ERROR;
1330 : : }
1331 : : return MATCH_YES;
1332 : : }
1333 : :
1334 : : /* Match an array constructor. */
1335 : :
1336 : : match
1337 : 4282092 : gfc_match_array_constructor (gfc_expr **result)
1338 : : {
1339 : 4282092 : gfc_constructor *c;
1340 : 4282092 : gfc_constructor_base head;
1341 : 4282092 : gfc_expr *expr;
1342 : 4282092 : gfc_typespec ts;
1343 : 4282092 : locus where;
1344 : 4282092 : match m;
1345 : 4282092 : const char *end_delim;
1346 : 4282092 : bool seen_ts;
1347 : :
1348 : 4282092 : head = NULL;
1349 : 4282092 : seen_ts = false;
1350 : :
1351 : 4282092 : if (gfc_match (" (/") == MATCH_NO)
1352 : : {
1353 : 4219933 : if (gfc_match (" [") == MATCH_NO)
1354 : : return MATCH_NO;
1355 : : else
1356 : : {
1357 : 64554 : if (!gfc_notify_std (GFC_STD_F2003, "[...] "
1358 : : "style array constructors at %C"))
1359 : : return MATCH_ERROR;
1360 : : end_delim = " ]";
1361 : : }
1362 : : }
1363 : : else
1364 : : end_delim = " /)";
1365 : :
1366 : 126713 : where = gfc_current_locus;
1367 : :
1368 : : /* Try to match an optional "type-spec ::" */
1369 : 126713 : gfc_clear_ts (&ts);
1370 : 126713 : m = gfc_match_type_spec (&ts);
1371 : 126713 : if (m == MATCH_YES)
1372 : : {
1373 : 5917 : seen_ts = (gfc_match (" ::") == MATCH_YES);
1374 : :
1375 : 5917 : if (seen_ts)
1376 : : {
1377 : 4669 : if (!gfc_notify_std (GFC_STD_F2003, "Array constructor "
1378 : : "including type specification at %C"))
1379 : 1 : goto cleanup;
1380 : :
1381 : 4668 : if (ts.deferred)
1382 : : {
1383 : 1 : gfc_error ("Type-spec at %L cannot contain a deferred "
1384 : : "type parameter", &where);
1385 : 1 : goto cleanup;
1386 : : }
1387 : :
1388 : 4667 : if (ts.type == BT_CHARACTER
1389 : 650 : && ts.u.cl && !ts.u.cl->length && !ts.u.cl->length_from_typespec)
1390 : : {
1391 : 1 : gfc_error ("Type-spec at %L cannot contain an asterisk for a "
1392 : : "type parameter", &where);
1393 : 1 : goto cleanup;
1394 : : }
1395 : : }
1396 : : }
1397 : 120796 : else if (m == MATCH_ERROR)
1398 : 21 : goto cleanup;
1399 : :
1400 : 5914 : if (!seen_ts)
1401 : 122023 : gfc_current_locus = where;
1402 : :
1403 : 126689 : if (gfc_match (end_delim) == MATCH_YES)
1404 : : {
1405 : 1629 : if (seen_ts)
1406 : 1628 : goto done;
1407 : : else
1408 : : {
1409 : 1 : gfc_error ("Empty array constructor at %C is not allowed");
1410 : 1 : goto cleanup;
1411 : : }
1412 : : }
1413 : :
1414 : 428795 : for (;;)
1415 : : {
1416 : 428795 : m = match_array_cons_element (&head);
1417 : 428795 : if (m == MATCH_ERROR)
1418 : 17 : goto cleanup;
1419 : 428778 : if (m == MATCH_NO)
1420 : 0 : goto syntax;
1421 : :
1422 : 428778 : if (gfc_match_char (',') == MATCH_NO)
1423 : : break;
1424 : : }
1425 : :
1426 : 125043 : if (gfc_match (end_delim) == MATCH_NO)
1427 : 6 : goto syntax;
1428 : :
1429 : 126665 : done:
1430 : : /* Size must be calculated at resolution time. */
1431 : 126665 : if (seen_ts)
1432 : : {
1433 : 4661 : expr = gfc_get_array_expr (ts.type, ts.kind, &where);
1434 : 4661 : expr->ts = ts;
1435 : :
1436 : : /* If the typespec is CHARACTER, check that array elements can
1437 : : be converted. See PR fortran/67803. */
1438 : 4661 : if (ts.type == BT_CHARACTER)
1439 : : {
1440 : 649 : c = gfc_constructor_first (head);
1441 : 2412 : for (; c; c = gfc_constructor_next (c))
1442 : : {
1443 : 1119 : if (gfc_numeric_ts (&c->expr->ts)
1444 : 1119 : || c->expr->ts.type == BT_LOGICAL)
1445 : : {
1446 : 5 : gfc_error ("Incompatible typespec for array element at %L",
1447 : 5 : &c->expr->where);
1448 : 5 : return MATCH_ERROR;
1449 : : }
1450 : :
1451 : : /* Special case null(). */
1452 : 1114 : if (c->expr->expr_type == EXPR_FUNCTION
1453 : 54 : && c->expr->ts.type == BT_UNKNOWN
1454 : 54 : && strcmp (c->expr->symtree->name, "null") == 0)
1455 : : {
1456 : 0 : gfc_error ("Incompatible typespec for array element at %L",
1457 : : &c->expr->where);
1458 : 0 : return MATCH_ERROR;
1459 : : }
1460 : : }
1461 : : }
1462 : :
1463 : : /* Walk the constructor, and if possible, do type conversion for
1464 : : numeric types. */
1465 : 4656 : if (gfc_numeric_ts (&ts))
1466 : : {
1467 : 3438 : m = walk_array_constructor (&ts, head);
1468 : 3438 : if (m == MATCH_ERROR)
1469 : : return m;
1470 : : }
1471 : : }
1472 : : else
1473 : 122004 : expr = gfc_get_array_expr (BT_UNKNOWN, 0, &where);
1474 : :
1475 : 126653 : expr->value.constructor = head;
1476 : 126653 : if (expr->ts.type == BT_CHARACTER && expr->ts.u.cl)
1477 : 644 : expr->ts.u.cl->length_from_typespec = seen_ts;
1478 : :
1479 : 126653 : *result = expr;
1480 : :
1481 : 126653 : return MATCH_YES;
1482 : :
1483 : 6 : syntax:
1484 : 6 : gfc_error ("Syntax error in array constructor at %C");
1485 : :
1486 : 48 : cleanup:
1487 : 48 : gfc_constructor_free (head);
1488 : 48 : return MATCH_ERROR;
1489 : : }
1490 : :
1491 : :
1492 : :
1493 : : /************** Check array constructors for correctness **************/
1494 : :
1495 : : /* Given an expression, compare it's type with the type of the current
1496 : : constructor. Returns nonzero if an error was issued. The
1497 : : cons_state variable keeps track of whether the type of the
1498 : : constructor being read or resolved is known to be good, bad or just
1499 : : starting out. */
1500 : :
1501 : : static gfc_typespec constructor_ts;
1502 : : static enum
1503 : : { CONS_START, CONS_GOOD, CONS_BAD }
1504 : : cons_state;
1505 : :
1506 : : static int
1507 : 986447 : check_element_type (gfc_expr *expr, bool convert)
1508 : : {
1509 : 986447 : if (cons_state == CONS_BAD)
1510 : : return 0; /* Suppress further errors */
1511 : :
1512 : 986447 : if (cons_state == CONS_START)
1513 : : {
1514 : 54048 : if (expr->ts.type == BT_UNKNOWN)
1515 : 0 : cons_state = CONS_BAD;
1516 : : else
1517 : : {
1518 : 54048 : cons_state = CONS_GOOD;
1519 : 54048 : constructor_ts = expr->ts;
1520 : : }
1521 : :
1522 : 54048 : return 0;
1523 : : }
1524 : :
1525 : 932399 : if (gfc_compare_types (&constructor_ts, &expr->ts))
1526 : : return 0;
1527 : :
1528 : 63 : if (convert)
1529 : 63 : return gfc_convert_type_warn (expr, &constructor_ts, 1, 1, true) ? 0 : 1;
1530 : :
1531 : 0 : gfc_error ("Element in %s array constructor at %L is %s",
1532 : : gfc_typename (&constructor_ts), &expr->where,
1533 : : gfc_typename (expr));
1534 : :
1535 : 0 : cons_state = CONS_BAD;
1536 : 0 : return 1;
1537 : : }
1538 : :
1539 : :
1540 : : /* Recursive work function for gfc_check_constructor_type(). */
1541 : :
1542 : : static bool
1543 : 84090 : check_constructor_type (gfc_constructor_base base, bool convert)
1544 : : {
1545 : 84090 : gfc_constructor *c;
1546 : 84090 : gfc_expr *e;
1547 : :
1548 : 1076395 : for (c = gfc_constructor_first (base); c; c = gfc_constructor_next (c))
1549 : : {
1550 : 992360 : e = c->expr;
1551 : :
1552 : 992360 : if (e->expr_type == EXPR_ARRAY)
1553 : : {
1554 : 5913 : if (!check_constructor_type (e->value.constructor, convert))
1555 : : return false;
1556 : :
1557 : 5887 : continue;
1558 : : }
1559 : :
1560 : 986447 : if (check_element_type (e, convert))
1561 : : return false;
1562 : : }
1563 : :
1564 : : return true;
1565 : : }
1566 : :
1567 : :
1568 : : /* Check that all elements of an array constructor are the same type.
1569 : : On false, an error has been generated. */
1570 : :
1571 : : bool
1572 : 78177 : gfc_check_constructor_type (gfc_expr *e)
1573 : : {
1574 : 78177 : bool t;
1575 : :
1576 : 78177 : if (e->ts.type != BT_UNKNOWN)
1577 : : {
1578 : 24120 : cons_state = CONS_GOOD;
1579 : 24120 : constructor_ts = e->ts;
1580 : : }
1581 : : else
1582 : : {
1583 : 54057 : cons_state = CONS_START;
1584 : 54057 : gfc_clear_ts (&constructor_ts);
1585 : : }
1586 : :
1587 : : /* If e->ts.type != BT_UNKNOWN, the array constructor included a
1588 : : typespec, and we will now convert the values on the fly. */
1589 : 78177 : t = check_constructor_type (e->value.constructor, e->ts.type != BT_UNKNOWN);
1590 : 78177 : if (t && e->ts.type == BT_UNKNOWN)
1591 : 54057 : e->ts = constructor_ts;
1592 : :
1593 : 78177 : return t;
1594 : : }
1595 : :
1596 : :
1597 : :
1598 : : typedef struct cons_stack
1599 : : {
1600 : : gfc_iterator *iterator;
1601 : : struct cons_stack *previous;
1602 : : }
1603 : : cons_stack;
1604 : :
1605 : : static cons_stack *base;
1606 : :
1607 : : static bool check_constructor (gfc_constructor_base, bool (*) (gfc_expr *));
1608 : :
1609 : : /* Check an EXPR_VARIABLE expression in a constructor to make sure
1610 : : that that variable is an iteration variable. */
1611 : :
1612 : : bool
1613 : 6381 : gfc_check_iter_variable (gfc_expr *expr)
1614 : : {
1615 : 6381 : gfc_symbol *sym;
1616 : 6381 : cons_stack *c;
1617 : :
1618 : 6381 : sym = expr->symtree->n.sym;
1619 : :
1620 : 6391 : for (c = base; c && c->iterator; c = c->previous)
1621 : 26 : if (sym == c->iterator->var->symtree->n.sym)
1622 : : return true;
1623 : :
1624 : : return false;
1625 : : }
1626 : :
1627 : :
1628 : : /* Recursive work function for gfc_check_constructor(). This amounts
1629 : : to calling the check function for each expression in the
1630 : : constructor, giving variables with the names of iterators a pass. */
1631 : :
1632 : : static bool
1633 : 7182 : check_constructor (gfc_constructor_base ctor, bool (*check_function) (gfc_expr *))
1634 : : {
1635 : 7182 : cons_stack element;
1636 : 7182 : gfc_expr *e;
1637 : 7182 : bool t;
1638 : 7182 : gfc_constructor *c;
1639 : :
1640 : 356145 : for (c = gfc_constructor_first (ctor); c; c = gfc_constructor_next (c))
1641 : : {
1642 : 348989 : e = c->expr;
1643 : :
1644 : 348989 : if (!e)
1645 : 0 : continue;
1646 : :
1647 : : /* Allow procedures as potential target of a procedure pointer. */
1648 : 348989 : if (e->expr_type == EXPR_VARIABLE
1649 : 211 : && e->ts.type == BT_PROCEDURE
1650 : 91 : && e->symtree->n.sym->attr.flavor == FL_PROCEDURE)
1651 : 91 : continue;
1652 : :
1653 : 348898 : if (e->expr_type != EXPR_ARRAY)
1654 : : {
1655 : 348689 : if (!(*check_function)(e))
1656 : : return false;
1657 : 348669 : continue;
1658 : : }
1659 : :
1660 : 209 : element.previous = base;
1661 : 209 : element.iterator = c->iterator;
1662 : :
1663 : 209 : base = &element;
1664 : 209 : t = check_constructor (e->value.constructor, check_function);
1665 : 209 : base = element.previous;
1666 : :
1667 : 209 : if (!t)
1668 : : return false;
1669 : : }
1670 : :
1671 : : /* Nothing went wrong, so all OK. */
1672 : : return true;
1673 : : }
1674 : :
1675 : :
1676 : : /* Checks a constructor to see if it is a particular kind of
1677 : : expression -- specification, restricted, or initialization as
1678 : : determined by the check_function. */
1679 : :
1680 : : bool
1681 : 6973 : gfc_check_constructor (gfc_expr *expr, bool (*check_function) (gfc_expr *))
1682 : : {
1683 : 6973 : cons_stack *base_save;
1684 : 6973 : bool t;
1685 : :
1686 : 6973 : base_save = base;
1687 : 6973 : base = NULL;
1688 : :
1689 : 6973 : t = check_constructor (expr->value.constructor, check_function);
1690 : 6973 : base = base_save;
1691 : :
1692 : 6973 : return t;
1693 : : }
1694 : :
1695 : :
1696 : :
1697 : : /**************** Simplification of array constructors ****************/
1698 : :
1699 : : iterator_stack *iter_stack;
1700 : :
1701 : : typedef struct
1702 : : {
1703 : : gfc_constructor_base base;
1704 : : int extract_count, extract_n;
1705 : : gfc_expr *extracted;
1706 : : mpz_t *count;
1707 : :
1708 : : mpz_t *offset;
1709 : : gfc_component *component;
1710 : : mpz_t *repeat;
1711 : :
1712 : : bool (*expand_work_function) (gfc_expr *);
1713 : : }
1714 : : expand_info;
1715 : :
1716 : : static expand_info current_expand;
1717 : :
1718 : : static bool expand_constructor (gfc_constructor_base);
1719 : :
1720 : :
1721 : : /* Work function that counts the number of elements present in a
1722 : : constructor. */
1723 : :
1724 : : static bool
1725 : 4330097 : count_elements (gfc_expr *e)
1726 : : {
1727 : 4330097 : mpz_t result;
1728 : :
1729 : 4330097 : if (e->rank == 0)
1730 : 4327209 : mpz_add_ui (*current_expand.count, *current_expand.count, 1);
1731 : : else
1732 : : {
1733 : 2888 : if (!gfc_array_size (e, &result))
1734 : : {
1735 : 501 : gfc_free_expr (e);
1736 : 501 : return false;
1737 : : }
1738 : :
1739 : 2387 : mpz_add (*current_expand.count, *current_expand.count, result);
1740 : 2387 : mpz_clear (result);
1741 : : }
1742 : :
1743 : 4329596 : gfc_free_expr (e);
1744 : 4329596 : return true;
1745 : : }
1746 : :
1747 : :
1748 : : /* Work function that extracts a particular element from an array
1749 : : constructor, freeing the rest. */
1750 : :
1751 : : static bool
1752 : 4935163 : extract_element (gfc_expr *e)
1753 : : {
1754 : 4935163 : if (e->rank != 0)
1755 : : { /* Something unextractable */
1756 : 1187 : gfc_free_expr (e);
1757 : 1187 : return false;
1758 : : }
1759 : :
1760 : 4933976 : if (current_expand.extract_count == current_expand.extract_n)
1761 : 37 : current_expand.extracted = e;
1762 : : else
1763 : 4933939 : gfc_free_expr (e);
1764 : :
1765 : 4933976 : current_expand.extract_count++;
1766 : :
1767 : 4933976 : return true;
1768 : : }
1769 : :
1770 : :
1771 : : /* Work function that constructs a new constructor out of the old one,
1772 : : stringing new elements together. */
1773 : :
1774 : : static bool
1775 : 1280386 : expand (gfc_expr *e)
1776 : : {
1777 : 1280386 : gfc_constructor *c = gfc_constructor_append_expr (¤t_expand.base,
1778 : : e, &e->where);
1779 : :
1780 : 1280386 : c->n.component = current_expand.component;
1781 : 1280386 : return true;
1782 : : }
1783 : :
1784 : :
1785 : : /* Given an initialization expression that is a variable reference,
1786 : : substitute the current value of the iteration variable. */
1787 : :
1788 : : void
1789 : 13969022 : gfc_simplify_iterator_var (gfc_expr *e)
1790 : : {
1791 : 13969022 : iterator_stack *p;
1792 : :
1793 : 19231661 : for (p = iter_stack; p; p = p->prev)
1794 : 19113916 : if (e->symtree == p->variable)
1795 : : break;
1796 : :
1797 : 13969022 : if (p == NULL)
1798 : : return; /* Variable not found */
1799 : :
1800 : 13851277 : gfc_replace_expr (e, gfc_get_int_expr (gfc_default_integer_kind, NULL, 0));
1801 : :
1802 : 13851277 : mpz_set (e->value.integer, p->value);
1803 : :
1804 : 13851277 : return;
1805 : : }
1806 : :
1807 : :
1808 : : /* Expand an expression with that is inside of a constructor,
1809 : : recursing into other constructors if present. */
1810 : :
1811 : : static bool
1812 : 11258919 : expand_expr (gfc_expr *e)
1813 : : {
1814 : 11258919 : if (e->expr_type == EXPR_ARRAY)
1815 : 11258919 : return expand_constructor (e->value.constructor);
1816 : :
1817 : 0 : e = gfc_copy_expr (e);
1818 : :
1819 : 0 : if (!gfc_simplify_expr (e, 1))
1820 : : {
1821 : 0 : gfc_free_expr (e);
1822 : 0 : return false;
1823 : : }
1824 : :
1825 : 0 : return current_expand.expand_work_function (e);
1826 : : }
1827 : :
1828 : :
1829 : : static bool
1830 : 27796 : expand_iterator (gfc_constructor *c)
1831 : : {
1832 : 27796 : gfc_expr *start, *end, *step;
1833 : 27796 : iterator_stack frame;
1834 : 27796 : mpz_t trip;
1835 : 27796 : bool t;
1836 : :
1837 : 27796 : end = step = NULL;
1838 : :
1839 : 27796 : t = false;
1840 : :
1841 : 27796 : mpz_init (trip);
1842 : 27796 : mpz_init (frame.value);
1843 : 27796 : frame.prev = NULL;
1844 : :
1845 : 27796 : start = gfc_copy_expr (c->iterator->start);
1846 : 27796 : if (!gfc_simplify_expr (start, 1))
1847 : 0 : goto cleanup;
1848 : :
1849 : 27796 : if (start->expr_type != EXPR_CONSTANT || start->ts.type != BT_INTEGER)
1850 : 936 : goto cleanup;
1851 : :
1852 : 26860 : end = gfc_copy_expr (c->iterator->end);
1853 : 26860 : if (!gfc_simplify_expr (end, 1))
1854 : 0 : goto cleanup;
1855 : :
1856 : 26860 : if (end->expr_type != EXPR_CONSTANT || end->ts.type != BT_INTEGER)
1857 : 2902 : goto cleanup;
1858 : :
1859 : 23958 : step = gfc_copy_expr (c->iterator->step);
1860 : 23958 : if (!gfc_simplify_expr (step, 1))
1861 : 0 : goto cleanup;
1862 : :
1863 : 23958 : if (step->expr_type != EXPR_CONSTANT || step->ts.type != BT_INTEGER)
1864 : 0 : goto cleanup;
1865 : :
1866 : 23958 : if (mpz_sgn (step->value.integer) == 0)
1867 : : {
1868 : 0 : gfc_error ("Iterator step at %L cannot be zero", &step->where);
1869 : 0 : goto cleanup;
1870 : : }
1871 : :
1872 : : /* Calculate the trip count of the loop. */
1873 : 23958 : mpz_sub (trip, end->value.integer, start->value.integer);
1874 : 23958 : mpz_add (trip, trip, step->value.integer);
1875 : 23958 : mpz_tdiv_q (trip, trip, step->value.integer);
1876 : :
1877 : 23958 : mpz_set (frame.value, start->value.integer);
1878 : :
1879 : 23958 : frame.prev = iter_stack;
1880 : 23958 : frame.variable = c->iterator->var->symtree;
1881 : 23958 : iter_stack = &frame;
1882 : :
1883 : 11281712 : while (mpz_sgn (trip) > 0)
1884 : : {
1885 : 11258919 : if (!expand_expr (c->expr))
1886 : 1165 : goto cleanup;
1887 : :
1888 : 11257754 : mpz_add (frame.value, frame.value, step->value.integer);
1889 : 11257754 : mpz_sub_ui (trip, trip, 1);
1890 : : }
1891 : :
1892 : : t = true;
1893 : :
1894 : 27796 : cleanup:
1895 : 27796 : gfc_free_expr (start);
1896 : 27796 : gfc_free_expr (end);
1897 : 27796 : gfc_free_expr (step);
1898 : :
1899 : 27796 : mpz_clear (trip);
1900 : 27796 : mpz_clear (frame.value);
1901 : :
1902 : 27796 : iter_stack = frame.prev;
1903 : :
1904 : 27796 : return t;
1905 : : }
1906 : :
1907 : : /* Variables for noticing if all constructors are empty, and
1908 : : if any of them had a type. */
1909 : :
1910 : : static bool empty_constructor;
1911 : : static gfc_typespec empty_ts;
1912 : :
1913 : : /* Expand a constructor into constant constructors without any
1914 : : iterators, calling the work function for each of the expanded
1915 : : expressions. The work function needs to either save or free the
1916 : : passed expression. */
1917 : :
1918 : : static bool
1919 : 11542927 : expand_constructor (gfc_constructor_base base)
1920 : : {
1921 : 11542927 : gfc_constructor *c;
1922 : 11542927 : gfc_expr *e;
1923 : :
1924 : 29195047 : for (c = gfc_constructor_first (base); c; c = gfc_constructor_next(c))
1925 : : {
1926 : 17659978 : if (c->iterator != NULL)
1927 : : {
1928 : 27796 : if (!expand_iterator (c))
1929 : : return false;
1930 : 22793 : continue;
1931 : : }
1932 : :
1933 : 17632182 : e = c->expr;
1934 : :
1935 : 17632182 : if (e == NULL)
1936 : : return false;
1937 : :
1938 : 17632182 : if (empty_constructor)
1939 : 90542 : empty_ts = e->ts;
1940 : :
1941 : : /* Simplify constant array expression/section within constructor. */
1942 : 17632182 : if (e->expr_type == EXPR_VARIABLE && e->rank > 0 && e->ref
1943 : 4373 : && e->symtree && e->symtree->n.sym
1944 : 4373 : && e->symtree->n.sym->attr.flavor == FL_PARAMETER)
1945 : 715 : gfc_simplify_expr (e, 0);
1946 : :
1947 : 17632182 : if (e->expr_type == EXPR_ARRAY)
1948 : : {
1949 : 6772 : if (!expand_constructor (e->value.constructor))
1950 : : return false;
1951 : :
1952 : 6741 : continue;
1953 : : }
1954 : :
1955 : 17625410 : empty_constructor = false;
1956 : 17625410 : e = gfc_copy_expr (e);
1957 : 17625410 : if (!gfc_simplify_expr (e, 1))
1958 : : {
1959 : 200 : gfc_free_expr (e);
1960 : 200 : return false;
1961 : : }
1962 : 17625210 : e->from_constructor = 1;
1963 : 17625210 : current_expand.offset = &c->offset;
1964 : 17625210 : current_expand.repeat = &c->repeat;
1965 : 17625210 : current_expand.component = c->n.component;
1966 : 17625210 : if (!current_expand.expand_work_function(e))
1967 : : return false;
1968 : : }
1969 : : return true;
1970 : : }
1971 : :
1972 : :
1973 : : /* Given an array expression and an element number (starting at zero),
1974 : : return a pointer to the array element. NULL is returned if the
1975 : : size of the array has been exceeded. The expression node returned
1976 : : remains a part of the array and should not be freed. Access is not
1977 : : efficient at all, but this is another place where things do not
1978 : : have to be particularly fast. */
1979 : :
1980 : : static gfc_expr *
1981 : 89512 : gfc_get_array_element (gfc_expr *array, int element)
1982 : : {
1983 : 89512 : expand_info expand_save;
1984 : 89512 : gfc_expr *e;
1985 : 89512 : bool rc;
1986 : :
1987 : 89512 : expand_save = current_expand;
1988 : 89512 : current_expand.extract_n = element;
1989 : 89512 : current_expand.expand_work_function = extract_element;
1990 : 89512 : current_expand.extracted = NULL;
1991 : 89512 : current_expand.extract_count = 0;
1992 : :
1993 : 89512 : iter_stack = NULL;
1994 : :
1995 : 89512 : rc = expand_constructor (array->value.constructor);
1996 : 89512 : e = current_expand.extracted;
1997 : 89512 : current_expand = expand_save;
1998 : :
1999 : 89512 : if (!rc)
2000 : 1681 : return NULL;
2001 : :
2002 : : return e;
2003 : : }
2004 : :
2005 : :
2006 : : /* Top level subroutine for expanding constructors. We only expand
2007 : : constructor if they are small enough. */
2008 : :
2009 : : bool
2010 : 91642 : gfc_expand_constructor (gfc_expr *e, bool fatal)
2011 : : {
2012 : 91642 : expand_info expand_save;
2013 : 91642 : gfc_expr *f;
2014 : 91642 : bool rc;
2015 : :
2016 : 91642 : if (gfc_is_size_zero_array (e))
2017 : : return true;
2018 : :
2019 : : /* If we can successfully get an array element at the max array size then
2020 : : the array is too big to expand, so we just return. */
2021 : 89512 : f = gfc_get_array_element (e, flag_max_array_constructor);
2022 : 89512 : if (f != NULL)
2023 : : {
2024 : 37 : gfc_free_expr (f);
2025 : 37 : if (fatal)
2026 : : {
2027 : 8 : gfc_error ("The number of elements in the array constructor "
2028 : : "at %L requires an increase of the allowed %d "
2029 : : "upper limit. See %<-fmax-array-constructor%> "
2030 : : "option", &e->where, flag_max_array_constructor);
2031 : 8 : return false;
2032 : : }
2033 : : return true;
2034 : : }
2035 : :
2036 : : /* We now know the array is not too big so go ahead and try to expand it. */
2037 : 89475 : expand_save = current_expand;
2038 : 89475 : current_expand.base = NULL;
2039 : :
2040 : 89475 : iter_stack = NULL;
2041 : :
2042 : 89475 : empty_constructor = true;
2043 : 89475 : gfc_clear_ts (&empty_ts);
2044 : 89475 : current_expand.expand_work_function = expand;
2045 : :
2046 : 89475 : if (!expand_constructor (e->value.constructor))
2047 : : {
2048 : 494 : gfc_constructor_free (current_expand.base);
2049 : 494 : rc = false;
2050 : 494 : goto done;
2051 : : }
2052 : :
2053 : : /* If we don't have an explicit constructor type, and there
2054 : : were only empty constructors, then take the type from
2055 : : them. */
2056 : :
2057 : 88981 : if (constructor_ts.type == BT_UNKNOWN && empty_constructor)
2058 : 8 : e->ts = empty_ts;
2059 : :
2060 : 88981 : gfc_constructor_free (e->value.constructor);
2061 : 88981 : e->value.constructor = current_expand.base;
2062 : :
2063 : 88981 : rc = true;
2064 : :
2065 : 89475 : done:
2066 : 89475 : current_expand = expand_save;
2067 : :
2068 : 89475 : return rc;
2069 : : }
2070 : :
2071 : :
2072 : : /* Work function for checking that an element of a constructor is a
2073 : : constant, after removal of any iteration variables. We return
2074 : : false if not so. */
2075 : :
2076 : : static bool
2077 : 7079564 : is_constant_element (gfc_expr *e)
2078 : : {
2079 : 7079564 : int rv;
2080 : :
2081 : 7079564 : rv = gfc_is_constant_expr (e);
2082 : 7079564 : gfc_free_expr (e);
2083 : :
2084 : 7079564 : return rv ? true : false;
2085 : : }
2086 : :
2087 : :
2088 : : /* Given an array constructor, determine if the constructor is
2089 : : constant or not by expanding it and making sure that all elements
2090 : : are constants. This is a bit of a hack since something like (/ (i,
2091 : : i=1,100000000) /) will take a while as* opposed to a more clever
2092 : : function that traverses the expression tree. FIXME. */
2093 : :
2094 : : bool
2095 : 5513 : gfc_constant_ac (gfc_expr *e)
2096 : : {
2097 : 5513 : expand_info expand_save;
2098 : 5513 : bool rc;
2099 : :
2100 : 5513 : iter_stack = NULL;
2101 : 5513 : expand_save = current_expand;
2102 : 5513 : current_expand.expand_work_function = is_constant_element;
2103 : :
2104 : 5513 : rc = expand_constructor (e->value.constructor);
2105 : :
2106 : 5513 : current_expand = expand_save;
2107 : 5513 : if (!rc)
2108 : : return 0;
2109 : :
2110 : : return 1;
2111 : : }
2112 : :
2113 : :
2114 : : /* Returns nonzero if an array constructor has been completely
2115 : : expanded (no iterators) and zero if iterators are present. */
2116 : :
2117 : : bool
2118 : 27009 : gfc_expanded_ac (gfc_expr *e)
2119 : : {
2120 : 27009 : gfc_constructor *c;
2121 : :
2122 : 27009 : if (e->expr_type == EXPR_ARRAY)
2123 : 5682 : for (c = gfc_constructor_first (e->value.constructor);
2124 : 27425 : c; c = gfc_constructor_next (c))
2125 : 21743 : if (c->iterator != NULL || !gfc_expanded_ac (c->expr))
2126 : 0 : return 0;
2127 : :
2128 : : return 1;
2129 : : }
2130 : :
2131 : :
2132 : : /*************** Type resolution of array constructors ***************/
2133 : :
2134 : :
2135 : : /* The symbol expr_is_sought_symbol_ref will try to find. */
2136 : : static const gfc_symbol *sought_symbol = NULL;
2137 : :
2138 : :
2139 : : /* Tells whether the expression E is a variable reference to the symbol
2140 : : in the static variable SOUGHT_SYMBOL, and sets the locus pointer WHERE
2141 : : accordingly.
2142 : : To be used with gfc_expr_walker: if a reference is found we don't need
2143 : : to look further so we return 1 to skip any further walk. */
2144 : :
2145 : : static int
2146 : 14934 : expr_is_sought_symbol_ref (gfc_expr **e, int *walk_subtrees ATTRIBUTE_UNUSED,
2147 : : void *where)
2148 : : {
2149 : 14934 : gfc_expr *expr = *e;
2150 : 14934 : locus *sym_loc = (locus *)where;
2151 : :
2152 : 14934 : if (expr->expr_type == EXPR_VARIABLE
2153 : 1305 : && expr->symtree->n.sym == sought_symbol)
2154 : : {
2155 : 9 : *sym_loc = expr->where;
2156 : 9 : return 1;
2157 : : }
2158 : :
2159 : : return 0;
2160 : : }
2161 : :
2162 : :
2163 : : /* Tells whether the expression EXPR contains a reference to the symbol
2164 : : SYM and in that case sets the position SYM_LOC where the reference is. */
2165 : :
2166 : : static bool
2167 : 14352 : find_symbol_in_expr (gfc_symbol *sym, gfc_expr *expr, locus *sym_loc)
2168 : : {
2169 : 14352 : int ret;
2170 : :
2171 : 14352 : sought_symbol = sym;
2172 : 0 : ret = gfc_expr_walker (&expr, &expr_is_sought_symbol_ref, sym_loc);
2173 : 14352 : sought_symbol = NULL;
2174 : 14352 : return ret;
2175 : : }
2176 : :
2177 : :
2178 : : /* Recursive array list resolution function. All of the elements must
2179 : : be of the same type. */
2180 : :
2181 : : static bool
2182 : 68160 : resolve_array_list (gfc_constructor_base base)
2183 : : {
2184 : 68160 : bool t;
2185 : 68160 : gfc_constructor *c;
2186 : 68160 : gfc_iterator *iter;
2187 : :
2188 : 68160 : t = true;
2189 : :
2190 : 618021 : for (c = gfc_constructor_first (base); c; c = gfc_constructor_next (c))
2191 : : {
2192 : 549861 : iter = c->iterator;
2193 : 549861 : if (iter != NULL)
2194 : : {
2195 : 4784 : gfc_symbol *iter_var;
2196 : 4784 : locus iter_var_loc;
2197 : :
2198 : 4784 : if (!gfc_resolve_iterator (iter, false, true))
2199 : 1 : t = false;
2200 : :
2201 : : /* Check for bounds referencing the iterator variable. */
2202 : 4784 : gcc_assert (iter->var->expr_type == EXPR_VARIABLE);
2203 : 4784 : iter_var = iter->var->symtree->n.sym;
2204 : 4784 : if (find_symbol_in_expr (iter_var, iter->start, &iter_var_loc))
2205 : : {
2206 : 1 : if (!gfc_notify_std (GFC_STD_LEGACY, "AC-IMPLIED-DO initial "
2207 : : "expression references control variable "
2208 : : "at %L", &iter_var_loc))
2209 : 4784 : t = false;
2210 : : }
2211 : 4784 : if (find_symbol_in_expr (iter_var, iter->end, &iter_var_loc))
2212 : : {
2213 : 7 : if (!gfc_notify_std (GFC_STD_LEGACY, "AC-IMPLIED-DO final "
2214 : : "expression references control variable "
2215 : : "at %L", &iter_var_loc))
2216 : 4784 : t = false;
2217 : : }
2218 : 4784 : if (find_symbol_in_expr (iter_var, iter->step, &iter_var_loc))
2219 : : {
2220 : 1 : if (!gfc_notify_std (GFC_STD_LEGACY, "AC-IMPLIED-DO step "
2221 : : "expression references control variable "
2222 : : "at %L", &iter_var_loc))
2223 : 4784 : t = false;
2224 : : }
2225 : : }
2226 : :
2227 : 549861 : if (!gfc_resolve_expr (c->expr))
2228 : 59 : t = false;
2229 : :
2230 : 549861 : if (UNLIMITED_POLY (c->expr))
2231 : : {
2232 : 1 : gfc_error ("Array constructor value at %L shall not be unlimited "
2233 : : "polymorphic [F2008: C4106]", &c->expr->where);
2234 : 1 : t = false;
2235 : : }
2236 : :
2237 : : /* F2018:C7114 The declared type of an ac-value shall not be abstract. */
2238 : 549861 : if (c->expr->ts.type == BT_CLASS
2239 : 83 : && c->expr->ts.u.derived
2240 : 83 : && c->expr->ts.u.derived->attr.abstract
2241 : 5 : && CLASS_DATA (c->expr))
2242 : : {
2243 : 5 : gfc_error ("Array constructor value %qs at %L is of the ABSTRACT "
2244 : 5 : "type %qs", c->expr->symtree->name, &c->expr->where,
2245 : 5 : CLASS_DATA (c->expr)->ts.u.derived->name);
2246 : 5 : t = false;
2247 : : }
2248 : :
2249 : : }
2250 : :
2251 : 68160 : return t;
2252 : : }
2253 : :
2254 : : /* Resolve character array constructor. If it has a specified constant character
2255 : : length, pad/truncate the elements here; if the length is not specified and
2256 : : all elements are of compile-time known length, emit an error as this is
2257 : : invalid. */
2258 : :
2259 : : bool
2260 : 10109 : gfc_resolve_character_array_constructor (gfc_expr *expr)
2261 : : {
2262 : 10109 : gfc_constructor *p;
2263 : 10109 : HOST_WIDE_INT found_length;
2264 : :
2265 : 10109 : gcc_assert (expr->expr_type == EXPR_ARRAY);
2266 : 10109 : gcc_assert (expr->ts.type == BT_CHARACTER);
2267 : :
2268 : 10109 : if (expr->ts.u.cl == NULL)
2269 : : {
2270 : 156 : for (p = gfc_constructor_first (expr->value.constructor);
2271 : 251 : p; p = gfc_constructor_next (p))
2272 : 183 : if (p->expr->ts.u.cl != NULL)
2273 : : {
2274 : : /* Ensure that if there is a char_len around that it is
2275 : : used; otherwise the middle-end confuses them! */
2276 : 88 : expr->ts.u.cl = p->expr->ts.u.cl;
2277 : 88 : goto got_charlen;
2278 : : }
2279 : :
2280 : 68 : expr->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
2281 : : }
2282 : :
2283 : 9953 : got_charlen:
2284 : :
2285 : : /* Early exit for zero size arrays. */
2286 : 10109 : if (expr->shape)
2287 : : {
2288 : 9952 : mpz_t size;
2289 : 9952 : HOST_WIDE_INT arraysize;
2290 : :
2291 : 9952 : gfc_array_size (expr, &size);
2292 : 9952 : arraysize = mpz_get_ui (size);
2293 : 9952 : mpz_clear (size);
2294 : :
2295 : 9952 : if (arraysize == 0)
2296 : 344 : return true;
2297 : : }
2298 : :
2299 : 9765 : found_length = -1;
2300 : :
2301 : 9765 : if (expr->ts.u.cl->length == NULL)
2302 : : {
2303 : : /* Check that all constant string elements have the same length until
2304 : : we reach the end or find a variable-length one. */
2305 : :
2306 : 6807 : for (p = gfc_constructor_first (expr->value.constructor);
2307 : 31662 : p; p = gfc_constructor_next (p))
2308 : : {
2309 : 25207 : HOST_WIDE_INT current_length = -1;
2310 : 25207 : gfc_ref *ref;
2311 : 25443 : for (ref = p->expr->ref; ref; ref = ref->next)
2312 : 251 : if (ref->type == REF_SUBSTRING
2313 : 77 : && ref->u.ss.start
2314 : 77 : && ref->u.ss.start->expr_type == EXPR_CONSTANT
2315 : 52 : && ref->u.ss.end
2316 : 39 : && ref->u.ss.end->expr_type == EXPR_CONSTANT)
2317 : : break;
2318 : :
2319 : 25207 : if (p->expr->expr_type == EXPR_CONSTANT)
2320 : 24795 : current_length = p->expr->value.character.length;
2321 : 412 : else if (ref)
2322 : 15 : current_length = gfc_mpz_get_hwi (ref->u.ss.end->value.integer)
2323 : 15 : - gfc_mpz_get_hwi (ref->u.ss.start->value.integer) + 1;
2324 : 397 : else if (p->expr->ts.u.cl && p->expr->ts.u.cl->length
2325 : 51 : && p->expr->ts.u.cl->length->expr_type == EXPR_CONSTANT)
2326 : 51 : current_length = gfc_mpz_get_hwi (p->expr->ts.u.cl->length->value.integer);
2327 : : else
2328 : : return true;
2329 : :
2330 : 24861 : if (current_length < 0)
2331 : : current_length = 0;
2332 : :
2333 : 24861 : if (found_length == -1)
2334 : 6479 : found_length = current_length;
2335 : 18382 : else if (found_length != current_length)
2336 : : {
2337 : 6 : gfc_error ("Different CHARACTER lengths (%wd/%wd) in array"
2338 : : " constructor at %L", found_length,
2339 : 6 : current_length, &p->expr->where);
2340 : 6 : return false;
2341 : : }
2342 : :
2343 : 24855 : gcc_assert (found_length == current_length);
2344 : : }
2345 : :
2346 : 6455 : gcc_assert (found_length != -1);
2347 : :
2348 : : /* Update the character length of the array constructor. */
2349 : 6455 : expr->ts.u.cl->length = gfc_get_int_expr (gfc_charlen_int_kind,
2350 : : NULL, found_length);
2351 : : }
2352 : : else
2353 : : {
2354 : : /* We've got a character length specified. It should be an integer,
2355 : : otherwise an error is signalled elsewhere. */
2356 : 2958 : gcc_assert (expr->ts.u.cl->length);
2357 : :
2358 : : /* If we've got a constant character length, pad according to this.
2359 : : gfc_extract_int does check for BT_INTEGER and EXPR_CONSTANT and sets
2360 : : max_length only if they pass. */
2361 : 2958 : gfc_extract_hwi (expr->ts.u.cl->length, &found_length);
2362 : :
2363 : : /* Now pad/truncate the elements accordingly to the specified character
2364 : : length. This is ok inside this conditional, as in the case above
2365 : : (without typespec) all elements are verified to have the same length
2366 : : anyway. */
2367 : 2958 : if (found_length != -1)
2368 : 2891 : for (p = gfc_constructor_first (expr->value.constructor);
2369 : 13671 : p; p = gfc_constructor_next (p))
2370 : 10780 : if (p->expr->expr_type == EXPR_CONSTANT)
2371 : : {
2372 : 9244 : gfc_expr *cl = NULL;
2373 : 9244 : HOST_WIDE_INT current_length = -1;
2374 : 9244 : bool has_ts;
2375 : :
2376 : 9244 : if (p->expr->ts.u.cl && p->expr->ts.u.cl->length)
2377 : : {
2378 : 1859 : cl = p->expr->ts.u.cl->length;
2379 : 1859 : gfc_extract_hwi (cl, ¤t_length);
2380 : : }
2381 : :
2382 : : /* If gfc_extract_int above set current_length, we implicitly
2383 : : know the type is BT_INTEGER and it's EXPR_CONSTANT. */
2384 : :
2385 : 9244 : has_ts = expr->ts.u.cl->length_from_typespec;
2386 : :
2387 : 9244 : if (! cl
2388 : 1859 : || (current_length != -1 && current_length != found_length))
2389 : 7503 : gfc_set_constant_character_len (found_length, p->expr,
2390 : : has_ts ? -1 : found_length);
2391 : : }
2392 : : }
2393 : :
2394 : : return true;
2395 : : }
2396 : :
2397 : :
2398 : : /* Resolve all of the expressions in an array list. */
2399 : :
2400 : : bool
2401 : 68160 : gfc_resolve_array_constructor (gfc_expr *expr)
2402 : : {
2403 : 68160 : bool t;
2404 : :
2405 : 68160 : t = resolve_array_list (expr->value.constructor);
2406 : 68160 : if (t)
2407 : 68093 : t = gfc_check_constructor_type (expr);
2408 : :
2409 : : /* gfc_resolve_character_array_constructor is called in gfc_resolve_expr after
2410 : : the call to this function, so we don't need to call it here; if it was
2411 : : called twice, an error message there would be duplicated. */
2412 : :
2413 : 68160 : return t;
2414 : : }
2415 : :
2416 : :
2417 : : /* Copy an iterator structure. */
2418 : :
2419 : : gfc_iterator *
2420 : 851708 : gfc_copy_iterator (gfc_iterator *src)
2421 : : {
2422 : 851708 : gfc_iterator *dest;
2423 : :
2424 : 851708 : if (src == NULL)
2425 : : return NULL;
2426 : :
2427 : 499 : dest = gfc_get_iterator ();
2428 : :
2429 : 499 : dest->var = gfc_copy_expr (src->var);
2430 : 499 : dest->start = gfc_copy_expr (src->start);
2431 : 499 : dest->end = gfc_copy_expr (src->end);
2432 : 499 : dest->step = gfc_copy_expr (src->step);
2433 : 499 : dest->annot = src->annot;
2434 : :
2435 : 499 : return dest;
2436 : : }
2437 : :
2438 : :
2439 : : /********* Subroutines for determining the size of an array *********/
2440 : :
2441 : : /* These are needed just to accommodate RESHAPE(). There are no
2442 : : diagnostics here, we just return false if something goes wrong. */
2443 : :
2444 : :
2445 : : /* Get the size of single dimension of an array specification. The
2446 : : array is guaranteed to be one dimensional. */
2447 : :
2448 : : bool
2449 : 549310 : spec_dimen_size (gfc_array_spec *as, int dimen, mpz_t *result)
2450 : : {
2451 : 549310 : if (as == NULL)
2452 : : return false;
2453 : :
2454 : 535888 : if (dimen < 0 || dimen > as->rank - 1)
2455 : 0 : gfc_internal_error ("spec_dimen_size(): Bad dimension");
2456 : :
2457 : 535888 : if (as->type != AS_EXPLICIT
2458 : 341989 : || !as->lower[dimen]
2459 : 341989 : || !as->upper[dimen])
2460 : : return false;
2461 : :
2462 : 341989 : if (as->lower[dimen]->expr_type != EXPR_CONSTANT
2463 : 341210 : || as->upper[dimen]->expr_type != EXPR_CONSTANT
2464 : 323061 : || as->lower[dimen]->ts.type != BT_INTEGER
2465 : 323061 : || as->upper[dimen]->ts.type != BT_INTEGER)
2466 : : return false;
2467 : :
2468 : 323055 : mpz_init (*result);
2469 : :
2470 : 323055 : mpz_sub (*result, as->upper[dimen]->value.integer,
2471 : 323055 : as->lower[dimen]->value.integer);
2472 : :
2473 : 323055 : mpz_add_ui (*result, *result, 1);
2474 : :
2475 : 323055 : if (mpz_cmp_si (*result, 0) < 0)
2476 : 0 : mpz_set_si (*result, 0);
2477 : :
2478 : : return true;
2479 : : }
2480 : :
2481 : :
2482 : : bool
2483 : 30042 : spec_size (gfc_array_spec *as, mpz_t *result)
2484 : : {
2485 : 30042 : mpz_t size;
2486 : 30042 : int d;
2487 : :
2488 : 30042 : if (!as || as->type == AS_ASSUMED_RANK)
2489 : : return false;
2490 : :
2491 : 28592 : mpz_init_set_ui (*result, 1);
2492 : :
2493 : 77819 : for (d = 0; d < as->rank; d++)
2494 : : {
2495 : 34522 : if (!spec_dimen_size (as, d, &size))
2496 : : {
2497 : 13887 : mpz_clear (*result);
2498 : 13887 : return false;
2499 : : }
2500 : :
2501 : 20635 : mpz_mul (*result, *result, size);
2502 : 20635 : mpz_clear (size);
2503 : : }
2504 : :
2505 : : return true;
2506 : : }
2507 : :
2508 : :
2509 : : /* Get the number of elements in an array section. Optionally, also supply
2510 : : the end value. */
2511 : :
2512 : : bool
2513 : 74928 : gfc_ref_dimen_size (gfc_array_ref *ar, int dimen, mpz_t *result, mpz_t *end)
2514 : : {
2515 : 74928 : mpz_t upper, lower, stride;
2516 : 74928 : mpz_t diff;
2517 : 74928 : bool t;
2518 : 74928 : gfc_expr *stride_expr = NULL;
2519 : :
2520 : 74928 : if (dimen < 0 || ar == NULL)
2521 : 0 : gfc_internal_error ("gfc_ref_dimen_size(): Bad dimension");
2522 : :
2523 : 74928 : if (dimen > ar->dimen - 1)
2524 : : {
2525 : 1 : gfc_error ("Bad array dimension at %L", &ar->c_where[dimen]);
2526 : 1 : return false;
2527 : : }
2528 : :
2529 : 74927 : switch (ar->dimen_type[dimen])
2530 : : {
2531 : 164 : case DIMEN_ELEMENT:
2532 : 164 : mpz_init (*result);
2533 : 164 : mpz_set_ui (*result, 1);
2534 : 164 : t = true;
2535 : 164 : break;
2536 : :
2537 : 1381 : case DIMEN_VECTOR:
2538 : 1381 : t = gfc_array_size (ar->start[dimen], result); /* Recurse! */
2539 : 1381 : break;
2540 : :
2541 : 73382 : case DIMEN_RANGE:
2542 : :
2543 : 73382 : mpz_init (stride);
2544 : :
2545 : 73382 : if (ar->stride[dimen] == NULL)
2546 : 51394 : mpz_set_ui (stride, 1);
2547 : : else
2548 : : {
2549 : 21988 : stride_expr = gfc_copy_expr(ar->stride[dimen]);
2550 : :
2551 : 21988 : if (!gfc_simplify_expr (stride_expr, 1)
2552 : 21986 : || stride_expr->expr_type != EXPR_CONSTANT
2553 : 42265 : || mpz_cmp_ui (stride_expr->value.integer, 0) == 0)
2554 : : {
2555 : 1713 : gfc_free_expr (stride_expr);
2556 : 1713 : mpz_clear (stride);
2557 : 1713 : return false;
2558 : : }
2559 : 20275 : mpz_set (stride, stride_expr->value.integer);
2560 : 20275 : gfc_free_expr(stride_expr);
2561 : : }
2562 : :
2563 : : /* Calculate the number of elements via gfc_dep_difference, but only if
2564 : : start and end are both supplied in the reference or the array spec.
2565 : : This is to guard against strange but valid code like
2566 : :
2567 : : subroutine foo(a,n)
2568 : : real a(1:n)
2569 : : n = 3
2570 : : print *,size(a(n-1:))
2571 : :
2572 : : where the user changes the value of a variable. If we have to
2573 : : determine end as well, we cannot do this using gfc_dep_difference.
2574 : : Fall back to the constants-only code then. */
2575 : :
2576 : 71669 : if (end == NULL)
2577 : : {
2578 : 63613 : bool use_dep;
2579 : :
2580 : 63613 : use_dep = gfc_dep_difference (ar->end[dimen], ar->start[dimen],
2581 : : &diff);
2582 : 63613 : if (!use_dep && ar->end[dimen] == NULL && ar->start[dimen] == NULL)
2583 : 28066 : use_dep = gfc_dep_difference (ar->as->upper[dimen],
2584 : 28066 : ar->as->lower[dimen], &diff);
2585 : :
2586 : 42368 : if (use_dep)
2587 : : {
2588 : 32319 : mpz_init (*result);
2589 : 32319 : mpz_add (*result, diff, stride);
2590 : 32319 : mpz_div (*result, *result, stride);
2591 : 32319 : if (mpz_cmp_ui (*result, 0) < 0)
2592 : 112 : mpz_set_ui (*result, 0);
2593 : :
2594 : 32319 : mpz_clear (stride);
2595 : 32319 : mpz_clear (diff);
2596 : 32319 : return true;
2597 : : }
2598 : :
2599 : : }
2600 : :
2601 : : /* Constant-only code here, which covers more cases
2602 : : like a(:4) etc. */
2603 : 39350 : mpz_init (upper);
2604 : 39350 : mpz_init (lower);
2605 : 39350 : t = false;
2606 : :
2607 : 39350 : if (ar->start[dimen] == NULL)
2608 : : {
2609 : 29316 : if (ar->as->lower[dimen] == NULL
2610 : 12202 : || ar->as->lower[dimen]->expr_type != EXPR_CONSTANT
2611 : 12199 : || ar->as->lower[dimen]->ts.type != BT_INTEGER)
2612 : 17117 : goto cleanup;
2613 : 12199 : mpz_set (lower, ar->as->lower[dimen]->value.integer);
2614 : : }
2615 : : else
2616 : : {
2617 : 10034 : if (ar->start[dimen]->expr_type != EXPR_CONSTANT)
2618 : 2376 : goto cleanup;
2619 : 7658 : mpz_set (lower, ar->start[dimen]->value.integer);
2620 : : }
2621 : :
2622 : 19857 : if (ar->end[dimen] == NULL)
2623 : : {
2624 : 6367 : if (ar->as->upper[dimen] == NULL
2625 : 4954 : || ar->as->upper[dimen]->expr_type != EXPR_CONSTANT
2626 : 4424 : || ar->as->upper[dimen]->ts.type != BT_INTEGER)
2627 : 1944 : goto cleanup;
2628 : 4423 : mpz_set (upper, ar->as->upper[dimen]->value.integer);
2629 : : }
2630 : : else
2631 : : {
2632 : 13490 : if (ar->end[dimen]->expr_type != EXPR_CONSTANT)
2633 : 4388 : goto cleanup;
2634 : 9102 : mpz_set (upper, ar->end[dimen]->value.integer);
2635 : : }
2636 : :
2637 : 13525 : mpz_init (*result);
2638 : 13525 : mpz_sub (*result, upper, lower);
2639 : 13525 : mpz_add (*result, *result, stride);
2640 : 13525 : mpz_div (*result, *result, stride);
2641 : :
2642 : : /* Zero stride caught earlier. */
2643 : 13525 : if (mpz_cmp_ui (*result, 0) < 0)
2644 : 8 : mpz_set_ui (*result, 0);
2645 : 13525 : t = true;
2646 : :
2647 : 13525 : if (end)
2648 : : {
2649 : 6360 : mpz_init (*end);
2650 : :
2651 : 6360 : mpz_sub_ui (*end, *result, 1UL);
2652 : 6360 : mpz_mul (*end, *end, stride);
2653 : 6360 : mpz_add (*end, *end, lower);
2654 : : }
2655 : :
2656 : 7165 : cleanup:
2657 : 39350 : mpz_clear (upper);
2658 : 39350 : mpz_clear (lower);
2659 : 39350 : mpz_clear (stride);
2660 : 39350 : return t;
2661 : :
2662 : 0 : default:
2663 : 0 : gfc_internal_error ("gfc_ref_dimen_size(): Bad dimen_type");
2664 : : }
2665 : :
2666 : : return t;
2667 : : }
2668 : :
2669 : :
2670 : : static bool
2671 : 3426 : ref_size (gfc_array_ref *ar, mpz_t *result)
2672 : : {
2673 : 3426 : mpz_t size;
2674 : 3426 : int d;
2675 : :
2676 : 3426 : mpz_init_set_ui (*result, 1);
2677 : :
2678 : 10438 : for (d = 0; d < ar->dimen; d++)
2679 : : {
2680 : 3982 : if (!gfc_ref_dimen_size (ar, d, &size, NULL))
2681 : : {
2682 : 396 : mpz_clear (*result);
2683 : 396 : return false;
2684 : : }
2685 : :
2686 : 3586 : mpz_mul (*result, *result, size);
2687 : 3586 : mpz_clear (size);
2688 : : }
2689 : :
2690 : : return true;
2691 : : }
2692 : :
2693 : :
2694 : : /* Given an array expression and a dimension, figure out how many
2695 : : elements it has along that dimension. Returns true if we were
2696 : : able to return a result in the 'result' variable, false
2697 : : otherwise. */
2698 : :
2699 : : bool
2700 : 696065 : gfc_array_dimen_size (gfc_expr *array, int dimen, mpz_t *result)
2701 : : {
2702 : 696065 : gfc_ref *ref;
2703 : 696065 : int i;
2704 : :
2705 : 696065 : gcc_assert (array != NULL);
2706 : :
2707 : 696065 : if (array->ts.type == BT_CLASS)
2708 : : return false;
2709 : :
2710 : 684339 : if (array->rank == -1)
2711 : : return false;
2712 : :
2713 : 684339 : if (dimen < 0 || dimen > array->rank - 1)
2714 : 0 : gfc_internal_error ("gfc_array_dimen_size(): Bad dimension");
2715 : :
2716 : 684339 : switch (array->expr_type)
2717 : : {
2718 : 573557 : case EXPR_VARIABLE:
2719 : 573557 : case EXPR_FUNCTION:
2720 : 620274 : for (ref = array->ref; ref; ref = ref->next)
2721 : : {
2722 : : /* Ultimate component is a procedure pointer. */
2723 : 576425 : if (ref->type == REF_COMPONENT
2724 : 34808 : && !ref->next
2725 : 1053 : && ref->u.c.component->attr.function
2726 : 91 : && IS_PROC_POINTER (ref->u.c.component))
2727 : : return false;
2728 : :
2729 : 576334 : if (ref->type != REF_ARRAY)
2730 : 34717 : continue;
2731 : :
2732 : 541617 : if (ref->u.ar.type == AR_FULL)
2733 : 468020 : return spec_dimen_size (ref->u.ar.as, dimen, result);
2734 : :
2735 : 73597 : if (ref->u.ar.type == AR_SECTION)
2736 : : {
2737 : 144259 : for (i = 0; dimen >= 0; i++)
2738 : 82662 : if (ref->u.ar.dimen_type[i] != DIMEN_ELEMENT)
2739 : 77365 : dimen--;
2740 : :
2741 : 61597 : return gfc_ref_dimen_size (&ref->u.ar, i - 1, result, NULL);
2742 : : }
2743 : : }
2744 : :
2745 : 43849 : if (array->shape)
2746 : : {
2747 : 12698 : mpz_init_set (*result, array->shape[dimen]);
2748 : 12698 : return true;
2749 : : }
2750 : :
2751 : 31151 : if (array->symtree->n.sym->attr.generic
2752 : 26 : && array->value.function.esym != NULL)
2753 : : {
2754 : 25 : if (!spec_dimen_size (array->value.function.esym->as, dimen, result))
2755 : : return false;
2756 : : }
2757 : 31126 : else if (!spec_dimen_size (array->symtree->n.sym->as, dimen, result))
2758 : : return false;
2759 : :
2760 : : break;
2761 : :
2762 : 97420 : case EXPR_ARRAY:
2763 : 97420 : if (array->shape == NULL) {
2764 : : /* Expressions with rank > 1 should have "shape" properly set */
2765 : 58486 : if ( array->rank != 1 )
2766 : 0 : gfc_internal_error ("gfc_array_dimen_size(): Bad EXPR_ARRAY expr");
2767 : 58486 : return gfc_array_size(array, result);
2768 : : }
2769 : :
2770 : : /* Fall through */
2771 : 52296 : default:
2772 : 52296 : if (array->shape == NULL)
2773 : : return false;
2774 : :
2775 : 45067 : mpz_init_set (*result, array->shape[dimen]);
2776 : :
2777 : 45067 : break;
2778 : : }
2779 : :
2780 : : return true;
2781 : : }
2782 : :
2783 : :
2784 : : /* Given an array expression, figure out how many elements are in the
2785 : : array. Returns true if this is possible, and sets the 'result'
2786 : : variable. Otherwise returns false. */
2787 : :
2788 : : bool
2789 : 124442 : gfc_array_size (gfc_expr *array, mpz_t *result)
2790 : : {
2791 : 124442 : expand_info expand_save;
2792 : 124442 : gfc_ref *ref;
2793 : 124442 : int i;
2794 : 124442 : bool t;
2795 : :
2796 : 124442 : if (array->ts.type == BT_CLASS)
2797 : : return false;
2798 : :
2799 : 123409 : switch (array->expr_type)
2800 : : {
2801 : 92736 : case EXPR_ARRAY:
2802 : 92736 : gfc_push_suppress_errors ();
2803 : :
2804 : 92736 : expand_save = current_expand;
2805 : :
2806 : 92736 : current_expand.count = result;
2807 : 92736 : mpz_init_set_ui (*result, 0);
2808 : :
2809 : 92736 : current_expand.expand_work_function = count_elements;
2810 : 92736 : iter_stack = NULL;
2811 : :
2812 : 92736 : t = expand_constructor (array->value.constructor);
2813 : :
2814 : 92736 : gfc_pop_suppress_errors ();
2815 : :
2816 : 92736 : if (!t)
2817 : 2038 : mpz_clear (*result);
2818 : 92736 : current_expand = expand_save;
2819 : 92736 : return t;
2820 : :
2821 : 25816 : case EXPR_VARIABLE:
2822 : 28044 : for (ref = array->ref; ref; ref = ref->next)
2823 : : {
2824 : 28043 : if (ref->type != REF_ARRAY)
2825 : 1772 : continue;
2826 : :
2827 : 26271 : if (ref->u.ar.type == AR_FULL)
2828 : 22389 : return spec_size (ref->u.ar.as, result);
2829 : :
2830 : 3882 : if (ref->u.ar.type == AR_SECTION)
2831 : 3426 : return ref_size (&ref->u.ar, result);
2832 : : }
2833 : :
2834 : 1 : return spec_size (array->symtree->n.sym->as, result);
2835 : :
2836 : :
2837 : 4857 : default:
2838 : 4857 : if (array->rank == 0 || array->shape == NULL)
2839 : : return false;
2840 : :
2841 : 3437 : mpz_init_set_ui (*result, 1);
2842 : :
2843 : 10685 : for (i = 0; i < array->rank; i++)
2844 : 3811 : mpz_mul (*result, *result, array->shape[i]);
2845 : :
2846 : : break;
2847 : : }
2848 : :
2849 : : return true;
2850 : : }
2851 : :
2852 : :
2853 : : /* Given an array reference, return the shape of the reference in an
2854 : : array of mpz_t integers. */
2855 : :
2856 : : bool
2857 : 10933 : gfc_array_ref_shape (gfc_array_ref *ar, mpz_t *shape)
2858 : : {
2859 : 10933 : int d;
2860 : 10933 : int i;
2861 : :
2862 : 10933 : d = 0;
2863 : :
2864 : 10933 : switch (ar->type)
2865 : : {
2866 : : case AR_FULL:
2867 : 17292 : for (; d < ar->as->rank; d++)
2868 : 15021 : if (!spec_dimen_size (ar->as, d, &shape[d]))
2869 : 8583 : goto cleanup;
2870 : :
2871 : : return true;
2872 : :
2873 : : case AR_SECTION:
2874 : 199 : for (i = 0; i < ar->dimen; i++)
2875 : : {
2876 : 157 : if (ar->dimen_type[i] != DIMEN_ELEMENT)
2877 : : {
2878 : 129 : if (!gfc_ref_dimen_size (ar, i, &shape[d], NULL))
2879 : 37 : goto cleanup;
2880 : 92 : d++;
2881 : : }
2882 : : }
2883 : :
2884 : : return true;
2885 : :
2886 : : default:
2887 : : break;
2888 : : }
2889 : :
2890 : 8620 : cleanup:
2891 : 8620 : gfc_clear_shape (shape, d);
2892 : 8620 : return false;
2893 : : }
2894 : :
2895 : :
2896 : : /* Given an array expression, find the array reference structure that
2897 : : characterizes the reference. */
2898 : :
2899 : : gfc_array_ref *
2900 : 95894 : gfc_find_array_ref (gfc_expr *e, bool allow_null)
2901 : : {
2902 : 95894 : gfc_ref *ref;
2903 : :
2904 : 106781 : for (ref = e->ref; ref; ref = ref->next)
2905 : 99731 : if (ref->type == REF_ARRAY
2906 : 93717 : && (ref->u.ar.type == AR_FULL || ref->u.ar.type == AR_SECTION))
2907 : : break;
2908 : :
2909 : 95894 : if (ref == NULL)
2910 : : {
2911 : 7050 : if (allow_null)
2912 : : return NULL;
2913 : : else
2914 : 0 : gfc_internal_error ("gfc_find_array_ref(): No ref found");
2915 : : }
2916 : :
2917 : 88844 : return &ref->u.ar;
2918 : : }
2919 : :
2920 : :
2921 : : /* Find out if an array shape is known at compile time. */
2922 : :
2923 : : bool
2924 : 71 : gfc_is_compile_time_shape (gfc_array_spec *as)
2925 : : {
2926 : 71 : if (as->type != AS_EXPLICIT)
2927 : : return false;
2928 : :
2929 : 148 : for (int i = 0; i < as->rank; i++)
2930 : 88 : if (!gfc_is_constant_expr (as->lower[i])
2931 : 88 : || !gfc_is_constant_expr (as->upper[i]))
2932 : 9 : return false;
2933 : :
2934 : : return true;
2935 : : }
|