Line data Source code
1 : /* Routines for manipulation of expression nodes.
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 "arith.h"
27 : #include "match.h"
28 : #include "target-memory.h" /* for gfc_convert_boz */
29 : #include "constructor.h"
30 : #include "tree.h"
31 :
32 :
33 : /* The following set of functions provide access to gfc_expr* of
34 : various types - actual all but EXPR_FUNCTION and EXPR_VARIABLE.
35 :
36 : There are two functions available elsewhere that provide
37 : slightly different flavours of variables. Namely:
38 : expr.cc (gfc_get_variable_expr)
39 : symbol.cc (gfc_lval_expr_from_sym)
40 : TODO: Merge these functions, if possible. */
41 :
42 : /* Get a new expression node. */
43 :
44 : gfc_expr *
45 89676366 : gfc_get_expr (void)
46 : {
47 89676366 : gfc_expr *e;
48 :
49 89676366 : e = XCNEW (gfc_expr);
50 89676366 : gfc_clear_ts (&e->ts);
51 89676366 : e->shape = NULL;
52 89676366 : e->ref = NULL;
53 89676366 : e->symtree = NULL;
54 89676366 : return e;
55 : }
56 :
57 :
58 : /* Get a new expression node that is an array constructor
59 : of given type and kind. */
60 :
61 : gfc_expr *
62 179027 : gfc_get_array_expr (bt type, int kind, locus *where)
63 : {
64 179027 : gfc_expr *e;
65 :
66 179027 : e = gfc_get_expr ();
67 179027 : e->expr_type = EXPR_ARRAY;
68 179027 : e->value.constructor = NULL;
69 179027 : e->rank = 1;
70 179027 : e->shape = NULL;
71 :
72 179027 : e->ts.type = type;
73 179027 : e->ts.kind = kind;
74 179027 : if (where)
75 177707 : e->where = *where;
76 :
77 179027 : return e;
78 : }
79 :
80 :
81 : /* Get a new expression node that is the NULL expression. */
82 :
83 : gfc_expr *
84 54763 : gfc_get_null_expr (locus *where)
85 : {
86 54763 : gfc_expr *e;
87 :
88 54763 : e = gfc_get_expr ();
89 54763 : e->expr_type = EXPR_NULL;
90 54763 : e->ts.type = BT_UNKNOWN;
91 :
92 54763 : if (where)
93 15717 : e->where = *where;
94 :
95 54763 : return e;
96 : }
97 :
98 :
99 : /* Get a new expression node that is an operator expression node. */
100 :
101 : gfc_expr *
102 1628346 : gfc_get_operator_expr (locus *where, gfc_intrinsic_op op,
103 : gfc_expr *op1, gfc_expr *op2)
104 : {
105 1628346 : gfc_expr *e;
106 :
107 1628346 : e = gfc_get_expr ();
108 1628346 : e->expr_type = EXPR_OP;
109 1628346 : e->value.op.op = op;
110 1628346 : e->value.op.op1 = op1;
111 1628346 : e->value.op.op2 = op2;
112 :
113 1628346 : if (where)
114 1628346 : e->where = *where;
115 :
116 1628346 : return e;
117 : }
118 :
119 : /* Get a new expression node that is an conditional expression node. */
120 :
121 : gfc_expr *
122 242 : gfc_get_conditional_expr (locus *where, gfc_expr *condition,
123 : gfc_expr *true_expr, gfc_expr *false_expr)
124 : {
125 242 : gfc_expr *e;
126 :
127 242 : e = gfc_get_expr ();
128 242 : e->expr_type = EXPR_CONDITIONAL;
129 242 : e->value.conditional.condition = condition;
130 242 : e->value.conditional.true_expr = true_expr;
131 242 : e->value.conditional.false_expr = false_expr;
132 :
133 242 : if (where)
134 242 : e->where = *where;
135 :
136 242 : return e;
137 : }
138 :
139 : /* Get a new expression node that is an structure constructor
140 : of given type and kind. */
141 :
142 : gfc_expr *
143 37275 : gfc_get_structure_constructor_expr (bt type, int kind, locus *where)
144 : {
145 37275 : gfc_expr *e;
146 :
147 37275 : e = gfc_get_expr ();
148 37275 : e->expr_type = EXPR_STRUCTURE;
149 37275 : e->value.constructor = NULL;
150 :
151 37275 : e->ts.type = type;
152 37275 : e->ts.kind = kind;
153 37275 : if (where)
154 37275 : e->where = *where;
155 :
156 37275 : return e;
157 : }
158 :
159 :
160 : /* Get a new expression node that is an constant of given type and kind. */
161 :
162 : gfc_expr *
163 31783411 : gfc_get_constant_expr (bt type, int kind, locus *where)
164 : {
165 31783411 : gfc_expr *e;
166 :
167 31783411 : if (!where)
168 0 : gfc_internal_error ("gfc_get_constant_expr(): locus %<where%> cannot be "
169 : "NULL");
170 :
171 31783411 : e = gfc_get_expr ();
172 :
173 31783411 : e->expr_type = EXPR_CONSTANT;
174 31783411 : e->ts.type = type;
175 31783411 : e->ts.kind = kind;
176 31783411 : e->where = *where;
177 :
178 31783411 : switch (type)
179 : {
180 30816842 : case BT_INTEGER:
181 30816842 : case BT_UNSIGNED:
182 30816842 : mpz_init (e->value.integer);
183 30816842 : break;
184 :
185 416081 : case BT_REAL:
186 416081 : gfc_set_model_kind (kind);
187 416081 : mpfr_init (e->value.real);
188 416081 : break;
189 :
190 19831 : case BT_COMPLEX:
191 19831 : gfc_set_model_kind (kind);
192 19831 : mpc_init2 (e->value.complex, mpfr_get_default_prec());
193 19831 : break;
194 :
195 : default:
196 : break;
197 : }
198 :
199 31783411 : return e;
200 : }
201 :
202 :
203 : /* Get a new expression node that is an string constant.
204 : If no string is passed, a string of len is allocated,
205 : blanked and null-terminated. */
206 :
207 : gfc_expr *
208 357702 : gfc_get_character_expr (int kind, locus *where, const char *src, gfc_charlen_t len)
209 : {
210 357702 : gfc_expr *e;
211 357702 : gfc_char_t *dest;
212 :
213 357702 : if (!src)
214 : {
215 355985 : dest = gfc_get_wide_string (len + 1);
216 355985 : gfc_wide_memset (dest, ' ', len);
217 355985 : dest[len] = '\0';
218 : }
219 : else
220 1717 : dest = gfc_char_to_widechar (src);
221 :
222 359505 : e = gfc_get_constant_expr (BT_CHARACTER, kind,
223 : where ? where : &gfc_current_locus);
224 357702 : e->value.character.string = dest;
225 357702 : e->value.character.length = len;
226 :
227 357702 : return e;
228 : }
229 :
230 :
231 : /* Get a new expression node that is an integer constant. */
232 :
233 : gfc_expr *
234 14516517 : gfc_get_int_expr (int kind, locus *where, HOST_WIDE_INT value)
235 : {
236 14516517 : gfc_expr *p;
237 28990954 : p = gfc_get_constant_expr (BT_INTEGER, kind,
238 : where ? where : &gfc_current_locus);
239 :
240 14516517 : const wide_int w = wi::shwi (value, kind * BITS_PER_UNIT);
241 14516517 : wi::to_mpz (w, p->value.integer, SIGNED);
242 :
243 14516517 : return p;
244 14516517 : }
245 :
246 : /* Get a new expression node that is an unsigned constant. */
247 :
248 : gfc_expr *
249 66 : gfc_get_unsigned_expr (int kind, locus *where, HOST_WIDE_INT value)
250 : {
251 66 : gfc_expr *p;
252 132 : p = gfc_get_constant_expr (BT_UNSIGNED, kind,
253 : where ? where : &gfc_current_locus);
254 66 : const wide_int w = wi::shwi (value, kind * BITS_PER_UNIT);
255 66 : wi::to_mpz (w, p->value.integer, UNSIGNED);
256 :
257 66 : return p;
258 66 : }
259 :
260 : /* Get a new expression node that is a logical constant. */
261 :
262 : gfc_expr *
263 77048 : gfc_get_logical_expr (int kind, locus *where, bool value)
264 : {
265 77048 : gfc_expr *p;
266 88434 : p = gfc_get_constant_expr (BT_LOGICAL, kind,
267 : where ? where : &gfc_current_locus);
268 :
269 77048 : p->value.logical = value;
270 :
271 77048 : return p;
272 : }
273 :
274 :
275 : gfc_expr *
276 34006 : gfc_get_iokind_expr (locus *where, io_kind k)
277 : {
278 34006 : gfc_expr *e;
279 :
280 : /* Set the types to something compatible with iokind. This is needed to
281 : get through gfc_free_expr later since iokind really has no Basic Type,
282 : BT, of its own. */
283 :
284 34006 : e = gfc_get_expr ();
285 34006 : e->expr_type = EXPR_CONSTANT;
286 34006 : e->ts.type = BT_LOGICAL;
287 34006 : e->value.iokind = k;
288 34006 : e->where = *where;
289 :
290 34006 : return e;
291 : }
292 :
293 :
294 : /* Given an expression pointer, return a copy of the expression. This
295 : subroutine is recursive. */
296 :
297 : gfc_expr *
298 57402237 : gfc_copy_expr (gfc_expr *p)
299 : {
300 57402237 : gfc_expr *q;
301 57402237 : gfc_char_t *s;
302 57402237 : char *c;
303 :
304 57402237 : if (p == NULL)
305 : return NULL;
306 :
307 48556887 : q = gfc_get_expr ();
308 48556887 : *q = *p;
309 :
310 48556887 : switch (q->expr_type)
311 : {
312 980 : case EXPR_SUBSTRING:
313 980 : s = gfc_get_wide_string (p->value.character.length + 1);
314 980 : q->value.character.string = s;
315 980 : memcpy (s, p->value.character.string,
316 980 : (p->value.character.length + 1) * sizeof (gfc_char_t));
317 980 : break;
318 :
319 17043340 : case EXPR_CONSTANT:
320 : /* Copy target representation, if it exists. */
321 17043340 : if (p->representation.string)
322 : {
323 5046 : c = XCNEWVEC (char, p->representation.length + 1);
324 5046 : q->representation.string = c;
325 5046 : memcpy (c, p->representation.string, (p->representation.length + 1));
326 : }
327 :
328 : /* Copy the values of any pointer components of p->value. */
329 17043340 : switch (q->ts.type)
330 : {
331 15252557 : case BT_INTEGER:
332 15252557 : case BT_UNSIGNED:
333 15252557 : mpz_init_set (q->value.integer, p->value.integer);
334 15252557 : break;
335 :
336 350430 : case BT_REAL:
337 350430 : gfc_set_model_kind (q->ts.kind);
338 350430 : mpfr_init (q->value.real);
339 350430 : mpfr_set (q->value.real, p->value.real, GFC_RND_MODE);
340 350430 : break;
341 :
342 27830 : case BT_COMPLEX:
343 27830 : gfc_set_model_kind (q->ts.kind);
344 27830 : mpc_init2 (q->value.complex, mpfr_get_default_prec());
345 27830 : mpc_set (q->value.complex, p->value.complex, GFC_MPC_RND_MODE);
346 27830 : break;
347 :
348 310141 : case BT_CHARACTER:
349 310141 : if (p->representation.string
350 2091 : && p->ts.kind == gfc_default_character_kind)
351 2085 : q->value.character.string
352 2085 : = gfc_char_to_widechar (q->representation.string,
353 : p->representation.length);
354 : else
355 : {
356 308056 : s = gfc_get_wide_string (p->value.character.length + 1);
357 308056 : q->value.character.string = s;
358 :
359 : /* This is the case for the C_NULL_CHAR named constant. */
360 308056 : if (p->value.character.length == 0
361 2397 : && (p->ts.is_c_interop || p->ts.is_iso_c))
362 : {
363 0 : *s = '\0';
364 : /* Need to set the length to 1 to make sure the NUL
365 : terminator is copied. */
366 0 : q->value.character.length = 1;
367 : }
368 : else
369 308056 : memcpy (s, p->value.character.string,
370 308056 : (p->value.character.length + 1) * sizeof (gfc_char_t));
371 : }
372 : break;
373 :
374 : case BT_HOLLERITH:
375 : case BT_LOGICAL:
376 : case_bt_struct:
377 : case BT_CLASS:
378 : case BT_ASSUMED:
379 : break; /* Already done. */
380 :
381 3 : case BT_BOZ:
382 3 : q->boz.len = p->boz.len;
383 3 : q->boz.rdx = p->boz.rdx;
384 3 : q->boz.str = XCNEWVEC (char, q->boz.len + 1);
385 3 : strncpy (q->boz.str, p->boz.str, p->boz.len);
386 3 : break;
387 :
388 0 : case BT_PROCEDURE:
389 0 : case BT_VOID:
390 : /* Should never be reached. */
391 0 : case BT_UNKNOWN:
392 0 : gfc_internal_error ("gfc_copy_expr(): Bad expr node");
393 : /* Not reached. */
394 : }
395 :
396 : break;
397 :
398 16472243 : case EXPR_OP:
399 16472243 : switch (q->value.op.op)
400 : {
401 5270543 : case INTRINSIC_NOT:
402 5270543 : case INTRINSIC_PARENTHESES:
403 5270543 : case INTRINSIC_UPLUS:
404 5270543 : case INTRINSIC_UMINUS:
405 5270543 : q->value.op.op1 = gfc_copy_expr (p->value.op.op1);
406 5270543 : break;
407 :
408 11201700 : default: /* Binary operators. */
409 11201700 : q->value.op.op1 = gfc_copy_expr (p->value.op.op1);
410 11201700 : q->value.op.op2 = gfc_copy_expr (p->value.op.op2);
411 11201700 : break;
412 : }
413 :
414 : break;
415 :
416 2 : case EXPR_CONDITIONAL:
417 2 : q->value.conditional.condition
418 2 : = gfc_copy_expr (p->value.conditional.condition);
419 2 : q->value.conditional.true_expr
420 2 : = gfc_copy_expr (p->value.conditional.true_expr);
421 2 : q->value.conditional.false_expr
422 2 : = gfc_copy_expr (p->value.conditional.false_expr);
423 2 : break;
424 :
425 419675 : case EXPR_FUNCTION:
426 839350 : q->value.function.actual =
427 419675 : gfc_copy_actual_arglist (p->value.function.actual);
428 419675 : break;
429 :
430 90 : case EXPR_COMPCALL:
431 90 : case EXPR_PPC:
432 180 : q->value.compcall.actual =
433 90 : gfc_copy_actual_arglist (p->value.compcall.actual);
434 90 : q->value.compcall.tbp = p->value.compcall.tbp;
435 90 : break;
436 :
437 124934 : case EXPR_STRUCTURE:
438 124934 : case EXPR_ARRAY:
439 124934 : q->value.constructor = gfc_constructor_copy (p->value.constructor);
440 124934 : break;
441 :
442 : case EXPR_VARIABLE:
443 : case EXPR_NULL:
444 : break;
445 :
446 0 : case EXPR_UNKNOWN:
447 0 : gcc_unreachable ();
448 : }
449 :
450 48556887 : q->shape = gfc_copy_shape (p->shape, p->rank);
451 :
452 48556887 : q->ref = gfc_copy_ref (p->ref);
453 :
454 48556887 : if (p->param_list)
455 1983 : q->param_list = gfc_copy_actual_arglist (p->param_list);
456 :
457 : return q;
458 : }
459 :
460 :
461 : void
462 459944 : gfc_clear_shape (mpz_t *shape, int rank)
463 : {
464 459944 : int i;
465 :
466 1051693 : for (i = 0; i < rank; i++)
467 591749 : mpz_clear (shape[i]);
468 459944 : }
469 :
470 :
471 : void
472 89429134 : gfc_free_shape (mpz_t **shape, int rank)
473 : {
474 89429134 : if (*shape == NULL)
475 : return;
476 :
477 445782 : gfc_clear_shape (*shape, rank);
478 445782 : free (*shape);
479 445782 : *shape = NULL;
480 : }
481 :
482 :
483 : /* Workhorse function for gfc_free_expr() that frees everything
484 : beneath an expression node, but not the node itself. This is
485 : useful when we want to simplify a node and replace it with
486 : something else or the expression node belongs to another structure. */
487 :
488 : static void
489 89407157 : free_expr0 (gfc_expr *e)
490 : {
491 89407157 : switch (e->expr_type)
492 : {
493 49099338 : case EXPR_CONSTANT:
494 : /* Free any parts of the value that need freeing. */
495 49099338 : switch (e->ts.type)
496 : {
497 46304878 : case BT_INTEGER:
498 46304878 : case BT_UNSIGNED:
499 46304878 : mpz_clear (e->value.integer);
500 46304878 : break;
501 :
502 766779 : case BT_REAL:
503 766779 : mpfr_clear (e->value.real);
504 766779 : break;
505 :
506 685834 : case BT_CHARACTER:
507 685834 : free (e->value.character.string);
508 685834 : break;
509 :
510 47611 : case BT_COMPLEX:
511 47611 : mpc_clear (e->value.complex);
512 47611 : break;
513 :
514 1683 : case BT_BOZ:
515 1683 : free (e->boz.str);
516 1683 : break;
517 :
518 : default:
519 : break;
520 : }
521 :
522 : /* Free the representation. */
523 49099338 : free (e->representation.string);
524 :
525 49099338 : break;
526 :
527 18133119 : case EXPR_OP:
528 18133119 : if (e->value.op.op1 != NULL)
529 1695890 : gfc_free_expr (e->value.op.op1);
530 18133119 : if (e->value.op.op2 != NULL)
531 1538885 : gfc_free_expr (e->value.op.op2);
532 : break;
533 :
534 244 : case EXPR_CONDITIONAL:
535 244 : gfc_free_expr (e->value.conditional.condition);
536 244 : gfc_free_expr (e->value.conditional.true_expr);
537 244 : gfc_free_expr (e->value.conditional.false_expr);
538 244 : break;
539 :
540 1969313 : case EXPR_FUNCTION:
541 1969313 : gfc_free_actual_arglist (e->value.function.actual);
542 1969313 : break;
543 :
544 3715 : case EXPR_COMPCALL:
545 3715 : case EXPR_PPC:
546 3715 : gfc_free_actual_arglist (e->value.compcall.actual);
547 3715 : break;
548 :
549 : case EXPR_VARIABLE:
550 : break;
551 :
552 367575 : case EXPR_ARRAY:
553 367575 : case EXPR_STRUCTURE:
554 367575 : gfc_constructor_free (e->value.constructor);
555 367575 : break;
556 :
557 1199 : case EXPR_SUBSTRING:
558 1199 : free (e->value.character.string);
559 1199 : break;
560 :
561 : case EXPR_NULL:
562 : break;
563 :
564 0 : default:
565 0 : gfc_internal_error ("free_expr0(): Bad expr type");
566 : }
567 :
568 : /* Free a shape array. */
569 89407157 : gfc_free_shape (&e->shape, e->rank);
570 :
571 89407157 : gfc_free_ref_list (e->ref);
572 :
573 89407157 : gfc_free_actual_arglist (e->param_list);
574 :
575 89407157 : memset (e, '\0', sizeof (gfc_expr));
576 89407157 : }
577 :
578 :
579 : /* Free an expression node and everything beneath it. */
580 :
581 : void
582 125656648 : gfc_free_expr (gfc_expr *e)
583 : {
584 125656648 : if (e == NULL)
585 : return;
586 58398329 : free_expr0 (e);
587 58398329 : free (e);
588 : }
589 :
590 :
591 : /* Free an argument list and everything below it. */
592 :
593 : void
594 91528728 : gfc_free_actual_arglist (gfc_actual_arglist *a1)
595 : {
596 91528728 : gfc_actual_arglist *a2;
597 :
598 94833670 : while (a1)
599 : {
600 3304942 : a2 = a1->next;
601 3304942 : if (a1->expr)
602 3004782 : gfc_free_expr (a1->expr);
603 3304942 : free (a1->associated_dummy);
604 3304942 : free (a1);
605 3304942 : a1 = a2;
606 : }
607 91528728 : }
608 :
609 :
610 : /* Copy an arglist structure and all of the arguments. */
611 :
612 : gfc_actual_arglist *
613 428579 : gfc_copy_actual_arglist (gfc_actual_arglist *p)
614 : {
615 428579 : gfc_actual_arglist *head, *tail, *new_arg;
616 :
617 428579 : head = tail = NULL;
618 :
619 1226900 : for (; p; p = p->next)
620 : {
621 798321 : new_arg = gfc_get_actual_arglist ();
622 798321 : *new_arg = *p;
623 :
624 798321 : if (p->associated_dummy != NULL)
625 : {
626 695163 : new_arg->associated_dummy = gfc_get_dummy_arg ();
627 695163 : *new_arg->associated_dummy = *p->associated_dummy;
628 : }
629 :
630 798321 : new_arg->expr = gfc_copy_expr (p->expr);
631 798321 : new_arg->next = NULL;
632 :
633 798321 : if (head == NULL)
634 : head = new_arg;
635 : else
636 373506 : tail->next = new_arg;
637 :
638 798321 : tail = new_arg;
639 : }
640 :
641 428579 : return head;
642 : }
643 :
644 :
645 : /* Free a list of reference structures. */
646 :
647 : void
648 89516966 : gfc_free_ref_list (gfc_ref *p)
649 : {
650 89516966 : gfc_ref *q;
651 89516966 : int i;
652 :
653 90903883 : for (; p; p = q)
654 : {
655 1386917 : q = p->next;
656 :
657 1386917 : switch (p->type)
658 : {
659 : case REF_ARRAY:
660 16545872 : for (i = 0; i < GFC_MAX_DIMENSIONS; i++)
661 : {
662 15511755 : gfc_free_expr (p->u.ar.start[i]);
663 15511755 : gfc_free_expr (p->u.ar.end[i]);
664 15511755 : gfc_free_expr (p->u.ar.stride[i]);
665 : }
666 :
667 1034117 : gfc_free_expr (p->u.ar.stat);
668 1034117 : gfc_free_expr (p->u.ar.team);
669 1034117 : break;
670 :
671 22925 : case REF_SUBSTRING:
672 22925 : gfc_free_expr (p->u.ss.start);
673 22925 : gfc_free_expr (p->u.ss.end);
674 22925 : break;
675 :
676 : case REF_COMPONENT:
677 : case REF_INQUIRY:
678 : break;
679 : }
680 :
681 1386917 : free (p);
682 : }
683 89516966 : }
684 :
685 :
686 : /* Graft the *src expression onto the *dest subexpression. */
687 :
688 : void
689 31008394 : gfc_replace_expr (gfc_expr *dest, gfc_expr *src)
690 : {
691 31008394 : free_expr0 (dest);
692 31008394 : *dest = *src;
693 31008394 : free (src);
694 31008394 : }
695 :
696 :
697 : /* Try to extract an integer constant from the passed expression node.
698 : Return true if some error occurred, false on success. If REPORT_ERROR
699 : is non-zero, emit error, for positive REPORT_ERROR using gfc_error,
700 : for negative using gfc_error_now. */
701 :
702 : bool
703 480660 : gfc_extract_int (gfc_expr *expr, int *result, int report_error)
704 : {
705 480660 : gfc_ref *ref;
706 :
707 : /* A KIND component is a parameter too. The expression for it
708 : is stored in the initializer and should be consistent with
709 : the tests below. */
710 480660 : if (gfc_expr_attr(expr).pdt_kind)
711 : {
712 16 : for (ref = expr->ref; ref; ref = ref->next)
713 : {
714 8 : if (ref->u.c.component->attr.pdt_kind)
715 8 : expr = ref->u.c.component->initializer;
716 : }
717 : }
718 :
719 480660 : if (expr->expr_type != EXPR_CONSTANT)
720 : {
721 1033 : if (report_error > 0)
722 1018 : gfc_error ("Constant expression required at %C");
723 15 : else if (report_error < 0)
724 4 : gfc_error_now ("Constant expression required at %C");
725 : return true;
726 : }
727 :
728 479627 : if (expr->ts.type != BT_INTEGER)
729 : {
730 492 : if (report_error > 0)
731 492 : gfc_error ("Integer expression required at %C");
732 0 : else if (report_error < 0)
733 0 : gfc_error_now ("Integer expression required at %C");
734 : return true;
735 : }
736 :
737 479135 : if ((mpz_cmp_si (expr->value.integer, INT_MAX) > 0)
738 479135 : || (mpz_cmp_si (expr->value.integer, INT_MIN) < 0))
739 : {
740 0 : if (report_error > 0)
741 0 : gfc_error ("Integer value too large in expression at %C");
742 0 : else if (report_error < 0)
743 0 : gfc_error_now ("Integer value too large in expression at %C");
744 : return true;
745 : }
746 :
747 479135 : *result = (int) mpz_get_si (expr->value.integer);
748 :
749 479135 : return false;
750 : }
751 :
752 : /* Same as gfc_extract_int, but use a HWI. */
753 :
754 : bool
755 10944 : gfc_extract_hwi (gfc_expr *expr, HOST_WIDE_INT *result, int report_error)
756 : {
757 10944 : gfc_ref *ref;
758 :
759 : /* A KIND component is a parameter too. The expression for it is
760 : stored in the initializer and should be consistent with the tests
761 : below. */
762 10944 : if (gfc_expr_attr(expr).pdt_kind)
763 : {
764 3 : for (ref = expr->ref; ref; ref = ref->next)
765 : {
766 0 : if (ref->u.c.component->attr.pdt_kind)
767 0 : expr = ref->u.c.component->initializer;
768 : }
769 : }
770 :
771 10944 : if (expr->expr_type != EXPR_CONSTANT)
772 : {
773 176 : if (report_error > 0)
774 0 : gfc_error ("Constant expression required at %C");
775 176 : else if (report_error < 0)
776 0 : gfc_error_now ("Constant expression required at %C");
777 : return true;
778 : }
779 :
780 10768 : if (expr->ts.type != BT_INTEGER)
781 : {
782 0 : if (report_error > 0)
783 0 : gfc_error ("Integer expression required at %C");
784 0 : else if (report_error < 0)
785 0 : gfc_error_now ("Integer expression required at %C");
786 : return true;
787 : }
788 :
789 : /* Use long_long_integer_type_node to determine when to saturate. */
790 10768 : const wide_int val = wi::from_mpz (long_long_integer_type_node,
791 10768 : expr->value.integer, false);
792 :
793 10768 : if (!wi::fits_shwi_p (val))
794 : {
795 0 : if (report_error > 0)
796 0 : gfc_error ("Integer value too large in expression at %C");
797 0 : else if (report_error < 0)
798 0 : gfc_error_now ("Integer value too large in expression at %C");
799 : return true;
800 : }
801 :
802 10768 : *result = val.to_shwi ();
803 :
804 10768 : return false;
805 10768 : }
806 :
807 :
808 : /* Recursively copy a list of reference structures. */
809 :
810 : gfc_ref *
811 48842834 : gfc_copy_ref (gfc_ref *src)
812 : {
813 48842834 : gfc_array_ref *ar;
814 48842834 : gfc_ref *dest;
815 :
816 48842834 : if (src == NULL)
817 : return NULL;
818 :
819 258354 : dest = gfc_get_ref ();
820 258354 : dest->type = src->type;
821 :
822 258354 : switch (src->type)
823 : {
824 188080 : case REF_ARRAY:
825 188080 : ar = gfc_copy_array_ref (&src->u.ar);
826 188080 : dest->u.ar = *ar;
827 188080 : free (ar);
828 188080 : break;
829 :
830 61492 : case REF_COMPONENT:
831 61492 : dest->u.c = src->u.c;
832 61492 : break;
833 :
834 2821 : case REF_INQUIRY:
835 2821 : dest->u.i = src->u.i;
836 2821 : break;
837 :
838 5961 : case REF_SUBSTRING:
839 5961 : dest->u.ss = src->u.ss;
840 5961 : dest->u.ss.start = gfc_copy_expr (src->u.ss.start);
841 5961 : dest->u.ss.end = gfc_copy_expr (src->u.ss.end);
842 5961 : break;
843 : }
844 :
845 258354 : dest->next = gfc_copy_ref (src->next);
846 :
847 258354 : return dest;
848 : }
849 :
850 :
851 : /* Detect whether an expression has any vector index array references. */
852 :
853 : bool
854 39267 : gfc_has_vector_index (gfc_expr *e)
855 : {
856 39267 : gfc_ref *ref;
857 39267 : int i;
858 47088 : for (ref = e->ref; ref; ref = ref->next)
859 7831 : if (ref->type == REF_ARRAY)
860 12985 : for (i = 0; i < ref->u.ar.dimen; i++)
861 7007 : if (ref->u.ar.dimen_type[i] == DIMEN_VECTOR)
862 : return 1;
863 : return 0;
864 : }
865 :
866 :
867 : bool
868 2681 : gfc_is_ptr_fcn (gfc_expr *e)
869 : {
870 2681 : return e != NULL && e->expr_type == EXPR_FUNCTION
871 3386 : && gfc_expr_attr (e).pointer;
872 : }
873 :
874 :
875 : /* Copy a shape array. */
876 :
877 : mpz_t *
878 48828379 : gfc_copy_shape (mpz_t *shape, int rank)
879 : {
880 48828379 : mpz_t *new_shape;
881 48828379 : int n;
882 :
883 48828379 : if (shape == NULL)
884 : return NULL;
885 :
886 157590 : new_shape = gfc_get_shape (rank);
887 :
888 529139 : for (n = 0; n < rank; n++)
889 213959 : mpz_init_set (new_shape[n], shape[n]);
890 :
891 : return new_shape;
892 : }
893 :
894 :
895 : /* Copy a shape array excluding dimension N, where N is an integer
896 : constant expression. Dimensions are numbered in Fortran style --
897 : starting with ONE.
898 :
899 : So, if the original shape array contains R elements
900 : { s1 ... sN-1 sN sN+1 ... sR-1 sR}
901 : the result contains R-1 elements:
902 : { s1 ... sN-1 sN+1 ... sR-1}
903 :
904 : If anything goes wrong -- N is not a constant, its value is out
905 : of range -- or anything else, just returns NULL. */
906 :
907 : mpz_t *
908 2990 : gfc_copy_shape_excluding (mpz_t *shape, int rank, gfc_expr *dim)
909 : {
910 2990 : mpz_t *new_shape, *s;
911 2990 : int i, n;
912 :
913 2990 : if (shape == NULL
914 2990 : || rank <= 1
915 2424 : || dim == NULL
916 2424 : || dim->expr_type != EXPR_CONSTANT
917 2151 : || dim->ts.type != BT_INTEGER)
918 : return NULL;
919 :
920 2151 : n = mpz_get_si (dim->value.integer);
921 2151 : n--; /* Convert to zero based index. */
922 2151 : if (n < 0 || n >= rank)
923 : return NULL;
924 :
925 2151 : s = new_shape = gfc_get_shape (rank - 1);
926 :
927 9177 : for (i = 0; i < rank; i++)
928 : {
929 4875 : if (i == n)
930 2151 : continue;
931 2724 : mpz_init_set (*s, shape[i]);
932 2724 : s++;
933 : }
934 :
935 : return new_shape;
936 : }
937 :
938 :
939 : /* Return the maximum kind of two expressions. In general, higher
940 : kind numbers mean more precision for numeric types. */
941 :
942 : int
943 97089 : gfc_kind_max (gfc_expr *e1, gfc_expr *e2)
944 : {
945 97089 : return (e1->ts.kind > e2->ts.kind) ? e1->ts.kind : e2->ts.kind;
946 : }
947 :
948 :
949 : /* Returns nonzero if the type is numeric, zero otherwise. */
950 :
951 : static bool
952 25700282 : numeric_type (bt type)
953 : {
954 0 : return type == BT_COMPLEX || type == BT_REAL || type == BT_INTEGER
955 0 : || type == BT_UNSIGNED;
956 : }
957 :
958 :
959 : /* Returns nonzero if the typespec is a numeric type, zero otherwise. */
960 :
961 : bool
962 25695423 : gfc_numeric_ts (gfc_typespec *ts)
963 : {
964 25695423 : return numeric_type (ts->type);
965 : }
966 :
967 :
968 : /* Return an expression node with an optional argument list attached.
969 : A variable number of gfc_expr pointers are strung together in an
970 : argument list with a NULL pointer terminating the list. */
971 :
972 : gfc_expr *
973 136973 : gfc_build_conversion (gfc_expr *e)
974 : {
975 136973 : gfc_expr *p;
976 :
977 136973 : p = gfc_get_expr ();
978 136973 : p->expr_type = EXPR_FUNCTION;
979 136973 : p->symtree = NULL;
980 136973 : p->value.function.actual = gfc_get_actual_arglist ();
981 136973 : p->value.function.actual->expr = e;
982 :
983 136973 : return p;
984 : }
985 :
986 :
987 : /* Given an expression node with some sort of numeric binary
988 : expression, insert type conversions required to make the operands
989 : have the same type. Conversion warnings are disabled if wconversion
990 : is set to 0.
991 :
992 : The exception is that the operands of an exponential don't have to
993 : have the same type. If possible, the base is promoted to the type
994 : of the exponent. For example, 1**2.3 becomes 1.0**2.3, but
995 : 1.0**2 stays as it is. */
996 :
997 : void
998 12269691 : gfc_type_convert_binary (gfc_expr *e, int wconversion)
999 : {
1000 12269691 : gfc_expr *op1, *op2;
1001 :
1002 12269691 : op1 = e->value.op.op1;
1003 12269691 : op2 = e->value.op.op2;
1004 :
1005 12269691 : if (op1->ts.type == BT_UNKNOWN || op2->ts.type == BT_UNKNOWN)
1006 : {
1007 0 : gfc_clear_ts (&e->ts);
1008 0 : return;
1009 : }
1010 :
1011 : /* Kind conversions of same type. */
1012 12269691 : if (op1->ts.type == op2->ts.type)
1013 : {
1014 12247253 : if (op1->ts.kind == op2->ts.kind)
1015 : {
1016 : /* No type conversions. */
1017 12133367 : e->ts = op1->ts;
1018 12133367 : goto done;
1019 : }
1020 :
1021 : /* Unsigned exponentiation is special, we need the type of the first
1022 : argument here because of modulo arithmetic. */
1023 113886 : if (op1->ts.type == BT_UNSIGNED && e->value.op.op == INTRINSIC_POWER)
1024 : {
1025 84378 : e->ts = op1->ts;
1026 84378 : goto done;
1027 : }
1028 :
1029 29508 : if (op1->ts.kind > op2->ts.kind)
1030 21991 : gfc_convert_type_warn (op2, &op1->ts, 2, wconversion);
1031 : else
1032 7517 : gfc_convert_type_warn (op1, &op2->ts, 2, wconversion);
1033 :
1034 29508 : e->ts = op1->ts;
1035 29508 : goto done;
1036 : }
1037 :
1038 : /* Integer combined with real or complex. */
1039 22438 : if (op2->ts.type == BT_INTEGER)
1040 : {
1041 17064 : e->ts = op1->ts;
1042 :
1043 : /* Special case for ** operator. */
1044 17064 : if (e->value.op.op == INTRINSIC_POWER)
1045 4791 : goto done;
1046 :
1047 12273 : gfc_convert_type_warn (e->value.op.op2, &e->ts, 2, wconversion);
1048 12273 : goto done;
1049 : }
1050 :
1051 5374 : if (op1->ts.type == BT_INTEGER)
1052 : {
1053 4776 : e->ts = op2->ts;
1054 4776 : gfc_convert_type_warn (e->value.op.op1, &e->ts, 2, wconversion);
1055 4776 : goto done;
1056 : }
1057 :
1058 : /* Real combined with complex. */
1059 598 : e->ts.type = BT_COMPLEX;
1060 598 : if (op1->ts.kind > op2->ts.kind)
1061 : e->ts.kind = op1->ts.kind;
1062 : else
1063 : e->ts.kind = op2->ts.kind;
1064 598 : if (op1->ts.type != BT_COMPLEX || op1->ts.kind != e->ts.kind)
1065 116 : gfc_convert_type_warn (e->value.op.op1, &e->ts, 2, wconversion);
1066 598 : if (op2->ts.type != BT_COMPLEX || op2->ts.kind != e->ts.kind)
1067 494 : gfc_convert_type_warn (e->value.op.op2, &e->ts, 2, wconversion);
1068 :
1069 104 : done:
1070 : return;
1071 : }
1072 :
1073 :
1074 : /* Standard intrinsics listed under F2018:10.1.12 (6), which are excluded in
1075 : constant expressions, except TRANSFER (c.f. item (8)), which would need
1076 : separate treatment. */
1077 :
1078 : static bool
1079 302725 : is_non_constant_intrinsic (gfc_expr *e)
1080 : {
1081 302725 : if (e->expr_type == EXPR_FUNCTION
1082 302725 : && e->value.function.isym)
1083 : {
1084 302725 : switch (e->value.function.isym->id)
1085 : {
1086 : case GFC_ISYM_COMMAND_ARGUMENT_COUNT:
1087 : case GFC_ISYM_GET_TEAM:
1088 : case GFC_ISYM_NULL:
1089 : case GFC_ISYM_NUM_IMAGES:
1090 : case GFC_ISYM_TEAM_NUMBER:
1091 : case GFC_ISYM_THIS_IMAGE:
1092 : return true;
1093 :
1094 298674 : default:
1095 298674 : return false;
1096 : }
1097 : }
1098 : return false;
1099 : }
1100 :
1101 :
1102 : /* Determine if an expression is constant in the sense of F08:7.1.12.
1103 : * This function expects that the expression has already been simplified. */
1104 :
1105 : bool
1106 45543331 : gfc_is_constant_expr (gfc_expr *e)
1107 : {
1108 45543331 : gfc_constructor *c;
1109 45543331 : gfc_actual_arglist *arg;
1110 :
1111 45543331 : if (e == NULL)
1112 : return true;
1113 :
1114 45523232 : switch (e->expr_type)
1115 : {
1116 1137068 : case EXPR_OP:
1117 1137068 : return (gfc_is_constant_expr (e->value.op.op1)
1118 1137068 : && (e->value.op.op2 == NULL
1119 105318 : || gfc_is_constant_expr (e->value.op.op2)));
1120 :
1121 3 : case EXPR_CONDITIONAL:
1122 3 : return gfc_is_constant_expr (e->value.conditional.condition)
1123 0 : && gfc_is_constant_expr (e->value.conditional.true_expr)
1124 3 : && gfc_is_constant_expr (e->value.conditional.false_expr);
1125 :
1126 1522923 : case EXPR_VARIABLE:
1127 : /* The only context in which this can occur is in a parameterized
1128 : derived type declaration, so returning true is OK. */
1129 1522923 : if (e->symtree->n.sym->attr.pdt_len
1130 1519544 : || e->symtree->n.sym->attr.pdt_kind)
1131 : return true;
1132 : return false;
1133 :
1134 369004 : case EXPR_FUNCTION:
1135 369004 : case EXPR_PPC:
1136 369004 : case EXPR_COMPCALL:
1137 369004 : gcc_assert (e->symtree || e->value.function.esym
1138 : || e->value.function.isym);
1139 :
1140 : /* Check for intrinsics excluded in constant expressions. */
1141 369004 : if (e->value.function.isym && is_non_constant_intrinsic (e))
1142 : return false;
1143 :
1144 : /* Call to intrinsic with at least one argument. */
1145 364953 : if (e->value.function.isym && e->value.function.actual)
1146 : {
1147 306368 : for (arg = e->value.function.actual; arg; arg = arg->next)
1148 303054 : if (!gfc_is_constant_expr (arg->expr))
1149 : return false;
1150 : }
1151 :
1152 69753 : if (e->value.function.isym
1153 3474 : && (e->value.function.isym->elemental
1154 3379 : || e->value.function.isym->pure
1155 3170 : || e->value.function.isym->inquiry
1156 3170 : || e->value.function.isym->transformational))
1157 : return true;
1158 :
1159 : return false;
1160 :
1161 : case EXPR_CONSTANT:
1162 : case EXPR_NULL:
1163 : return true;
1164 :
1165 2025 : case EXPR_SUBSTRING:
1166 2025 : return e->ref == NULL || (gfc_is_constant_expr (e->ref->u.ss.start)
1167 820 : && gfc_is_constant_expr (e->ref->u.ss.end));
1168 :
1169 164515 : case EXPR_ARRAY:
1170 164515 : case EXPR_STRUCTURE:
1171 164515 : c = gfc_constructor_first (e->value.constructor);
1172 164515 : if ((e->expr_type == EXPR_ARRAY) && c && c->iterator)
1173 6227 : return gfc_constant_ac (e);
1174 :
1175 1969865 : for (; c; c = gfc_constructor_next (c))
1176 1823527 : if (!gfc_is_constant_expr (c->expr))
1177 : return false;
1178 :
1179 : return true;
1180 :
1181 :
1182 0 : default:
1183 0 : gfc_internal_error ("gfc_is_constant_expr(): Unknown expression type");
1184 : return false;
1185 : }
1186 : }
1187 :
1188 :
1189 : /* Is true if the expression or symbol is a passed CFI descriptor. */
1190 : bool
1191 751634 : is_CFI_desc (gfc_symbol *sym, gfc_expr *e)
1192 : {
1193 751634 : if (sym == NULL
1194 751634 : && e && e->expr_type == EXPR_VARIABLE)
1195 186089 : sym = e->symtree->n.sym;
1196 :
1197 751634 : if (sym && sym->attr.dummy && sym->ns && sym->ns->proc_name
1198 315254 : && sym->ns->proc_name->attr.is_bind_c
1199 77972 : && (sym->attr.pointer
1200 73586 : || sym->attr.allocatable
1201 70323 : || (sym->attr.dimension
1202 42487 : && (sym->as->type == AS_ASSUMED_SHAPE
1203 26207 : || sym->as->type == AS_ASSUMED_RANK))
1204 43178 : || (sym->ts.type == BT_CHARACTER
1205 14718 : && (!sym->ts.u.cl || !sym->ts.u.cl->length))))
1206 47244 : return true;
1207 :
1208 : return false;
1209 : }
1210 :
1211 :
1212 : /* Is true if an array reference is followed by a component or substring
1213 : reference. */
1214 : bool
1215 276717 : is_subref_array (gfc_expr * e)
1216 : {
1217 276717 : gfc_ref * ref;
1218 276717 : bool seen_array;
1219 276717 : gfc_symbol *sym;
1220 :
1221 276717 : if (e->expr_type != EXPR_VARIABLE)
1222 : return false;
1223 :
1224 275474 : sym = e->symtree->n.sym;
1225 :
1226 275474 : if (sym->attr.subref_array_pointer
1227 275474 : || gfc_is_span_addressed_dummy (sym))
1228 : return true;
1229 :
1230 269818 : seen_array = false;
1231 :
1232 566080 : for (ref = e->ref; ref; ref = ref->next)
1233 : {
1234 : /* If we haven't seen the array reference and this is an intrinsic,
1235 : what follows cannot be a subreference array, unless there is a
1236 : substring reference. */
1237 299593 : if (!seen_array && ref->type == REF_COMPONENT
1238 32191 : && ref->next == NULL
1239 4720 : && ref->u.c.component->ts.type != BT_CHARACTER
1240 4699 : && ref->u.c.component->ts.type != BT_CLASS
1241 4291 : && !gfc_bt_struct (ref->u.c.component->ts.type))
1242 : return false;
1243 :
1244 299478 : if (ref->type == REF_ARRAY
1245 263853 : && ref->u.ar.type != AR_ELEMENT)
1246 : seen_array = true;
1247 :
1248 39251 : if (seen_array
1249 263443 : && ref->type != REF_ARRAY)
1250 : return seen_array;
1251 : }
1252 :
1253 266487 : if (sym->ts.type == BT_CLASS
1254 21744 : && sym->attr.dummy
1255 6329 : && CLASS_DATA (sym)->attr.dimension
1256 3945 : && CLASS_DATA (sym)->attr.class_pointer)
1257 658 : return true;
1258 :
1259 : return false;
1260 : }
1261 :
1262 :
1263 : /* Try to collapse intrinsic expressions. */
1264 :
1265 : static bool
1266 17353228 : simplify_intrinsic_op (gfc_expr *p, int type)
1267 : {
1268 17353228 : gfc_intrinsic_op op;
1269 17353228 : gfc_expr *op1, *op2, *result;
1270 :
1271 17353228 : if (p->value.op.op == INTRINSIC_USER)
1272 : return true;
1273 :
1274 17353225 : op1 = p->value.op.op1;
1275 17353225 : op2 = p->value.op.op2;
1276 17353225 : op = p->value.op.op;
1277 :
1278 17353225 : if (!gfc_simplify_expr (op1, type))
1279 : return false;
1280 17352831 : if (!gfc_simplify_expr (op2, type))
1281 : return false;
1282 :
1283 17352783 : if (!gfc_is_constant_expr (op1)
1284 17352783 : || (op2 != NULL && !gfc_is_constant_expr (op2)))
1285 : return true;
1286 :
1287 : /* Rip p apart. */
1288 16437251 : p->value.op.op1 = NULL;
1289 16437251 : p->value.op.op2 = NULL;
1290 :
1291 16437251 : switch (op)
1292 : {
1293 5258742 : case INTRINSIC_PARENTHESES:
1294 5258742 : result = gfc_parentheses (op1);
1295 5258742 : break;
1296 :
1297 31 : case INTRINSIC_UPLUS:
1298 31 : result = gfc_uplus (op1);
1299 31 : break;
1300 :
1301 13504 : case INTRINSIC_UMINUS:
1302 13504 : result = gfc_uminus (op1);
1303 13504 : break;
1304 :
1305 10282520 : case INTRINSIC_PLUS:
1306 10282520 : result = gfc_add (op1, op2);
1307 10282520 : break;
1308 :
1309 503963 : case INTRINSIC_MINUS:
1310 503963 : result = gfc_subtract (op1, op2);
1311 503963 : break;
1312 :
1313 337388 : case INTRINSIC_TIMES:
1314 337388 : result = gfc_multiply (op1, op2);
1315 337388 : break;
1316 :
1317 5815 : case INTRINSIC_DIVIDE:
1318 5815 : result = gfc_divide (op1, op2);
1319 5815 : break;
1320 :
1321 6025 : case INTRINSIC_POWER:
1322 6025 : result = gfc_power (op1, op2);
1323 6025 : break;
1324 :
1325 2475 : case INTRINSIC_CONCAT:
1326 2475 : result = gfc_concat (op1, op2);
1327 2475 : break;
1328 :
1329 1258 : case INTRINSIC_EQ:
1330 1258 : case INTRINSIC_EQ_OS:
1331 1258 : result = gfc_eq (op1, op2, op);
1332 1258 : break;
1333 :
1334 20984 : case INTRINSIC_NE:
1335 20984 : case INTRINSIC_NE_OS:
1336 20984 : result = gfc_ne (op1, op2, op);
1337 20984 : break;
1338 :
1339 611 : case INTRINSIC_GT:
1340 611 : case INTRINSIC_GT_OS:
1341 611 : result = gfc_gt (op1, op2, op);
1342 611 : break;
1343 :
1344 71 : case INTRINSIC_GE:
1345 71 : case INTRINSIC_GE_OS:
1346 71 : result = gfc_ge (op1, op2, op);
1347 71 : break;
1348 :
1349 147 : case INTRINSIC_LT:
1350 147 : case INTRINSIC_LT_OS:
1351 147 : result = gfc_lt (op1, op2, op);
1352 147 : break;
1353 :
1354 414 : case INTRINSIC_LE:
1355 414 : case INTRINSIC_LE_OS:
1356 414 : result = gfc_le (op1, op2, op);
1357 414 : break;
1358 :
1359 490 : case INTRINSIC_NOT:
1360 490 : result = gfc_not (op1);
1361 490 : break;
1362 :
1363 1021 : case INTRINSIC_AND:
1364 1021 : result = gfc_and (op1, op2);
1365 1021 : break;
1366 :
1367 447 : case INTRINSIC_OR:
1368 447 : result = gfc_or (op1, op2);
1369 447 : break;
1370 :
1371 12 : case INTRINSIC_EQV:
1372 12 : result = gfc_eqv (op1, op2);
1373 12 : break;
1374 :
1375 1333 : case INTRINSIC_NEQV:
1376 1333 : result = gfc_neqv (op1, op2);
1377 1333 : break;
1378 :
1379 0 : default:
1380 0 : gfc_internal_error ("simplify_intrinsic_op(): Bad operator");
1381 : }
1382 :
1383 16437251 : if (result == NULL)
1384 : {
1385 55 : gfc_free_expr (op1);
1386 55 : gfc_free_expr (op2);
1387 55 : return false;
1388 : }
1389 :
1390 16437196 : result->rank = p->rank;
1391 16437196 : result->corank = p->corank;
1392 16437196 : result->where = p->where;
1393 16437196 : gfc_replace_expr (p, result);
1394 :
1395 16437196 : return true;
1396 : }
1397 :
1398 : /* Try to collapse conditional expressions. */
1399 :
1400 : static bool
1401 27 : simplify_conditional (gfc_expr *p, int type)
1402 : {
1403 27 : gfc_expr *condition, *true_expr, *false_expr;
1404 :
1405 27 : condition = p->value.conditional.condition;
1406 27 : true_expr = p->value.conditional.true_expr;
1407 27 : false_expr = p->value.conditional.false_expr;
1408 :
1409 27 : if (!gfc_simplify_expr (condition, type)
1410 27 : || !gfc_simplify_expr (true_expr, type)
1411 54 : || !gfc_simplify_expr (false_expr, type))
1412 : return false;
1413 :
1414 27 : if (!gfc_is_constant_expr (condition))
1415 : return true;
1416 :
1417 0 : p->value.conditional.condition = NULL;
1418 0 : p->value.conditional.true_expr = NULL;
1419 0 : p->value.conditional.false_expr = NULL;
1420 :
1421 0 : if (condition->value.logical)
1422 : {
1423 0 : gfc_replace_expr (p, true_expr);
1424 0 : gfc_free_expr (false_expr);
1425 : }
1426 : else
1427 : {
1428 0 : gfc_replace_expr (p, false_expr);
1429 0 : gfc_free_expr (true_expr);
1430 : }
1431 0 : gfc_free_expr (condition);
1432 :
1433 0 : return true;
1434 : }
1435 :
1436 : /* Subroutine to simplify constructor expressions. Mutually recursive
1437 : with gfc_simplify_expr(). */
1438 :
1439 : static bool
1440 141235 : simplify_constructor (gfc_constructor_base base, int type)
1441 : {
1442 141235 : gfc_constructor *c;
1443 141235 : gfc_expr *p;
1444 :
1445 849372 : for (c = gfc_constructor_first (base); c; c = gfc_constructor_next (c))
1446 : {
1447 708137 : if (c->iterator
1448 708137 : && (!gfc_simplify_expr(c->iterator->start, type)
1449 821 : || !gfc_simplify_expr (c->iterator->end, type)
1450 821 : || !gfc_simplify_expr (c->iterator->step, type)))
1451 : return false;
1452 :
1453 708137 : if (c->expr && c->expr->expr_type != EXPR_CONSTANT)
1454 : {
1455 : /* Try and simplify a copy. Replace the original if successful
1456 : but keep going through the constructor at all costs. Not
1457 : doing so can make a dog's dinner of complicated things. */
1458 48069 : p = gfc_copy_expr (c->expr);
1459 :
1460 48069 : if (!gfc_simplify_expr (p, type))
1461 : {
1462 10 : gfc_free_expr (p);
1463 10 : continue;
1464 : }
1465 :
1466 48059 : gfc_replace_expr (c->expr, p);
1467 : }
1468 : }
1469 :
1470 : return true;
1471 : }
1472 :
1473 :
1474 : /* Pull a single array element out of an array constructor. */
1475 :
1476 : static bool
1477 4830 : find_array_element (gfc_constructor_base base, gfc_array_ref *ar,
1478 : gfc_constructor **rval)
1479 : {
1480 4830 : unsigned long nelemen;
1481 4830 : int i;
1482 4830 : mpz_t delta;
1483 4830 : mpz_t offset;
1484 4830 : mpz_t span;
1485 4830 : mpz_t tmp;
1486 4830 : gfc_constructor *cons;
1487 4830 : gfc_expr *e;
1488 4830 : bool t;
1489 :
1490 4830 : t = true;
1491 4830 : e = NULL;
1492 :
1493 4830 : mpz_init_set_ui (offset, 0);
1494 4830 : mpz_init (delta);
1495 4830 : mpz_init (tmp);
1496 4830 : mpz_init_set_ui (span, 1);
1497 12531 : for (i = 0; i < ar->dimen; i++)
1498 : {
1499 4897 : if (!gfc_reduce_init_expr (ar->as->lower[i])
1500 4892 : || !gfc_reduce_init_expr (ar->as->upper[i])
1501 4892 : || ar->as->upper[i]->expr_type != EXPR_CONSTANT
1502 9785 : || ar->as->lower[i]->expr_type != EXPR_CONSTANT)
1503 : {
1504 9 : t = false;
1505 9 : cons = NULL;
1506 9 : goto depart;
1507 : }
1508 :
1509 4888 : e = ar->start[i];
1510 4888 : if (e->expr_type != EXPR_CONSTANT)
1511 : {
1512 2008 : cons = NULL;
1513 2008 : goto depart;
1514 : }
1515 :
1516 : /* Check the bounds. */
1517 2880 : if ((ar->as->upper[i]
1518 2880 : && mpz_cmp (e->value.integer,
1519 2880 : ar->as->upper[i]->value.integer) > 0)
1520 2871 : || (mpz_cmp (e->value.integer,
1521 2871 : ar->as->lower[i]->value.integer) < 0))
1522 : {
1523 9 : gfc_error ("Index in dimension %d is out of bounds "
1524 : "at %L", i + 1, &ar->c_where[i]);
1525 9 : cons = NULL;
1526 9 : t = false;
1527 9 : goto depart;
1528 : }
1529 :
1530 2871 : mpz_sub (delta, e->value.integer, ar->as->lower[i]->value.integer);
1531 2871 : mpz_mul (delta, delta, span);
1532 2871 : mpz_add (offset, offset, delta);
1533 :
1534 2871 : mpz_set_ui (tmp, 1);
1535 2871 : mpz_add (tmp, tmp, ar->as->upper[i]->value.integer);
1536 2871 : mpz_sub (tmp, tmp, ar->as->lower[i]->value.integer);
1537 2871 : mpz_mul (span, span, tmp);
1538 : }
1539 :
1540 3609 : for (cons = gfc_constructor_first (base), nelemen = mpz_get_ui (offset);
1541 12049 : cons && nelemen > 0; cons = gfc_constructor_next (cons), nelemen--)
1542 : {
1543 9245 : if (cons->iterator)
1544 : {
1545 0 : cons = NULL;
1546 0 : goto depart;
1547 : }
1548 : }
1549 :
1550 2804 : depart:
1551 4830 : mpz_clear (delta);
1552 4830 : mpz_clear (offset);
1553 4830 : mpz_clear (span);
1554 4830 : mpz_clear (tmp);
1555 4830 : *rval = cons;
1556 4830 : return t;
1557 : }
1558 :
1559 :
1560 : /* Find a component of a structure constructor. */
1561 :
1562 : static gfc_constructor *
1563 1799 : find_component_ref (gfc_constructor_base base, gfc_ref *ref)
1564 : {
1565 1799 : gfc_component *pick = ref->u.c.component;
1566 1799 : gfc_constructor *c = gfc_constructor_first (base);
1567 :
1568 1799 : gfc_symbol *dt = ref->u.c.sym;
1569 1799 : int ext = dt->attr.extension;
1570 :
1571 : /* For extended types, check if the desired component is in one of the
1572 : * parent types. */
1573 1889 : while (ext > 0 && gfc_find_component (dt->components->ts.u.derived,
1574 : pick->name, true, true, NULL))
1575 : {
1576 90 : dt = dt->components->ts.u.derived;
1577 90 : c = gfc_constructor_first (c->expr->value.constructor);
1578 90 : ext--;
1579 : }
1580 :
1581 1799 : gfc_component *comp = dt->components;
1582 1942 : while (comp != pick)
1583 : {
1584 143 : comp = comp->next;
1585 143 : c = gfc_constructor_next (c);
1586 : }
1587 :
1588 1799 : return c;
1589 : }
1590 :
1591 :
1592 : /* Replace an expression with the contents of a constructor, removing
1593 : the subobject reference in the process. */
1594 :
1595 : static void
1596 4639 : remove_subobject_ref (gfc_expr *p, gfc_constructor *cons)
1597 : {
1598 4639 : gfc_expr *e;
1599 :
1600 4639 : if (cons)
1601 : {
1602 4603 : e = cons->expr;
1603 4603 : cons->expr = NULL;
1604 : }
1605 : else
1606 36 : e = gfc_copy_expr (p);
1607 4639 : e->ref = p->ref->next;
1608 4639 : p->ref->next = NULL;
1609 4639 : gfc_replace_expr (p, e);
1610 4639 : }
1611 :
1612 :
1613 : /* Pull an array section out of an array constructor. */
1614 :
1615 : static bool
1616 1322 : find_array_section (gfc_expr *expr, gfc_ref *ref)
1617 : {
1618 1322 : int idx;
1619 1322 : int rank;
1620 1322 : int d;
1621 1322 : int shape_i;
1622 1322 : int limit;
1623 1322 : long unsigned one = 1;
1624 1322 : bool incr_ctr;
1625 1322 : mpz_t start[GFC_MAX_DIMENSIONS];
1626 1322 : mpz_t end[GFC_MAX_DIMENSIONS];
1627 1322 : mpz_t stride[GFC_MAX_DIMENSIONS];
1628 1322 : mpz_t delta[GFC_MAX_DIMENSIONS];
1629 1322 : mpz_t ctr[GFC_MAX_DIMENSIONS];
1630 1322 : mpz_t delta_mpz;
1631 1322 : mpz_t tmp_mpz;
1632 1322 : mpz_t nelts;
1633 1322 : mpz_t ptr;
1634 1322 : gfc_constructor_base base;
1635 1322 : gfc_constructor *cons, *vecsub[GFC_MAX_DIMENSIONS];
1636 1322 : gfc_expr *begin;
1637 1322 : gfc_expr *finish;
1638 1322 : gfc_expr *step;
1639 1322 : gfc_expr *upper;
1640 1322 : gfc_expr *lower;
1641 1322 : bool t;
1642 :
1643 1322 : t = true;
1644 :
1645 1322 : base = expr->value.constructor;
1646 1322 : expr->value.constructor = NULL;
1647 :
1648 1322 : rank = ref->u.ar.as->rank;
1649 :
1650 1322 : if (expr->shape == NULL)
1651 243 : expr->shape = gfc_get_shape (rank);
1652 :
1653 1322 : mpz_init_set_ui (delta_mpz, one);
1654 1322 : mpz_init_set_ui (nelts, one);
1655 1322 : mpz_init (tmp_mpz);
1656 1322 : mpz_init (ptr);
1657 :
1658 : /* Do the initialization now, so that we can cleanup without
1659 : keeping track of where we were. */
1660 4493 : for (d = 0; d < rank; d++)
1661 : {
1662 1849 : mpz_init (delta[d]);
1663 1849 : mpz_init (start[d]);
1664 1849 : mpz_init (end[d]);
1665 1849 : mpz_init (ctr[d]);
1666 1849 : mpz_init (stride[d]);
1667 1849 : vecsub[d] = NULL;
1668 : }
1669 :
1670 : /* Build the counters to clock through the array reference. */
1671 : shape_i = 0;
1672 2481 : for (d = 0; d < rank; d++)
1673 : {
1674 : /* Make this stretch of code easier on the eye! */
1675 1602 : begin = ref->u.ar.start[d];
1676 1602 : finish = ref->u.ar.end[d];
1677 1602 : step = ref->u.ar.stride[d];
1678 1602 : lower = ref->u.ar.as->lower[d];
1679 1602 : upper = ref->u.ar.as->upper[d];
1680 :
1681 1602 : if (!lower || !upper
1682 1592 : || lower->expr_type != EXPR_CONSTANT
1683 1592 : || upper->expr_type != EXPR_CONSTANT
1684 1592 : || lower->ts.type != BT_INTEGER
1685 1592 : || upper->ts.type != BT_INTEGER)
1686 : {
1687 11 : t = false;
1688 11 : goto cleanup;
1689 : }
1690 :
1691 1591 : if (ref->u.ar.dimen_type[d] == DIMEN_VECTOR) /* Vector subscript. */
1692 : {
1693 70 : gfc_constructor *ci;
1694 70 : gcc_assert (begin);
1695 :
1696 70 : if (begin->expr_type != EXPR_ARRAY || !gfc_is_constant_expr (begin))
1697 : {
1698 6 : t = false;
1699 6 : goto cleanup;
1700 : }
1701 :
1702 64 : gcc_assert (begin->rank == 1);
1703 : /* Zero-sized arrays have no shape and no elements, stop early. */
1704 64 : if (!begin->shape)
1705 : {
1706 0 : mpz_set_ui (nelts, 0);
1707 0 : break;
1708 : }
1709 :
1710 64 : vecsub[d] = gfc_constructor_first (begin->value.constructor);
1711 64 : mpz_set (ctr[d], vecsub[d]->expr->value.integer);
1712 64 : mpz_mul (nelts, nelts, begin->shape[0]);
1713 64 : mpz_set (expr->shape[shape_i++], begin->shape[0]);
1714 :
1715 : /* Check bounds. */
1716 296 : for (ci = vecsub[d]; ci; ci = gfc_constructor_next (ci))
1717 : {
1718 170 : if (mpz_cmp (ci->expr->value.integer, upper->value.integer) > 0
1719 168 : || mpz_cmp (ci->expr->value.integer,
1720 168 : lower->value.integer) < 0)
1721 : {
1722 2 : gfc_error ("index in dimension %d is out of bounds "
1723 : "at %L", d + 1, &ref->u.ar.c_where[d]);
1724 2 : t = false;
1725 2 : goto cleanup;
1726 : }
1727 : }
1728 : }
1729 : else
1730 : {
1731 1521 : if ((begin && begin->expr_type != EXPR_CONSTANT)
1732 1161 : || (finish && finish->expr_type != EXPR_CONSTANT)
1733 1131 : || (step && step->expr_type != EXPR_CONSTANT))
1734 : {
1735 390 : t = false;
1736 390 : goto cleanup;
1737 : }
1738 :
1739 : /* Obtain the stride. */
1740 1131 : if (step)
1741 118 : mpz_set (stride[d], step->value.integer);
1742 : else
1743 1013 : mpz_set_ui (stride[d], one);
1744 :
1745 1131 : if (mpz_cmp_ui (stride[d], 0) == 0)
1746 0 : mpz_set_ui (stride[d], one);
1747 :
1748 : /* Obtain the start value for the index. */
1749 1131 : if (begin)
1750 855 : mpz_set (start[d], begin->value.integer);
1751 : else
1752 276 : mpz_set (start[d], lower->value.integer);
1753 :
1754 1131 : mpz_set (ctr[d], start[d]);
1755 :
1756 : /* Obtain the end value for the index. */
1757 1131 : if (finish)
1758 626 : mpz_set (end[d], finish->value.integer);
1759 : else
1760 505 : mpz_set (end[d], upper->value.integer);
1761 :
1762 : /* Separate 'if' because elements sometimes arrive with
1763 : non-null end. */
1764 1131 : if (ref->u.ar.dimen_type[d] == DIMEN_ELEMENT)
1765 248 : mpz_set (end [d], begin->value.integer);
1766 :
1767 : /* Check the bounds. */
1768 1131 : if (mpz_cmp (ctr[d], upper->value.integer) > 0
1769 1112 : || mpz_cmp (end[d], upper->value.integer) > 0
1770 1112 : || mpz_cmp (ctr[d], lower->value.integer) < 0
1771 1097 : || mpz_cmp (end[d], lower->value.integer) < 0)
1772 : {
1773 34 : gfc_error ("index in dimension %d is out of bounds "
1774 : "at %L", d + 1, &ref->u.ar.c_where[d]);
1775 34 : t = false;
1776 34 : goto cleanup;
1777 : }
1778 :
1779 : /* Calculate the number of elements and the shape. */
1780 1097 : mpz_set (tmp_mpz, stride[d]);
1781 1097 : mpz_add (tmp_mpz, end[d], tmp_mpz);
1782 1097 : mpz_sub (tmp_mpz, tmp_mpz, ctr[d]);
1783 1097 : mpz_div (tmp_mpz, tmp_mpz, stride[d]);
1784 1097 : mpz_mul (nelts, nelts, tmp_mpz);
1785 :
1786 : /* An element reference reduces the rank of the expression; don't
1787 : add anything to the shape array. */
1788 1097 : if (ref->u.ar.dimen_type[d] != DIMEN_ELEMENT)
1789 849 : mpz_set (expr->shape[shape_i++], tmp_mpz);
1790 : }
1791 :
1792 : /* Calculate the 'stride' (=delta) for conversion of the
1793 : counter values into the index along the constructor. */
1794 1159 : mpz_set (delta[d], delta_mpz);
1795 1159 : mpz_sub (tmp_mpz, upper->value.integer, lower->value.integer);
1796 1159 : mpz_add_ui (tmp_mpz, tmp_mpz, one);
1797 1159 : mpz_mul (delta_mpz, delta_mpz, tmp_mpz);
1798 : }
1799 :
1800 879 : cons = gfc_constructor_first (base);
1801 :
1802 : /* Now clock through the array reference, calculating the index in
1803 : the source constructor and transferring the elements to the new
1804 : constructor. */
1805 14254 : for (idx = 0; idx < (int) mpz_get_si (nelts); idx++)
1806 : {
1807 12497 : mpz_set_ui (ptr, 0);
1808 :
1809 12497 : incr_ctr = true;
1810 38436 : for (d = 0; d < rank; d++)
1811 : {
1812 13442 : mpz_set (tmp_mpz, ctr[d]);
1813 13442 : mpz_sub (tmp_mpz, tmp_mpz, ref->u.ar.as->lower[d]->value.integer);
1814 13442 : mpz_mul (tmp_mpz, tmp_mpz, delta[d]);
1815 13442 : mpz_add (ptr, ptr, tmp_mpz);
1816 :
1817 13442 : if (!incr_ctr) continue;
1818 :
1819 13077 : if (ref->u.ar.dimen_type[d] == DIMEN_VECTOR) /* Vector subscript. */
1820 : {
1821 203 : gcc_assert(vecsub[d]);
1822 :
1823 203 : if (!gfc_constructor_next (vecsub[d]))
1824 74 : vecsub[d] = gfc_constructor_first (ref->u.ar.start[d]->value.constructor);
1825 : else
1826 : {
1827 129 : vecsub[d] = gfc_constructor_next (vecsub[d]);
1828 129 : incr_ctr = false;
1829 : }
1830 203 : mpz_set (ctr[d], vecsub[d]->expr->value.integer);
1831 : }
1832 : else
1833 : {
1834 12874 : mpz_add (ctr[d], ctr[d], stride[d]);
1835 :
1836 25748 : if (mpz_cmp_ui (stride[d], 0) > 0
1837 12523 : ? mpz_cmp (ctr[d], end[d]) > 0
1838 351 : : mpz_cmp (ctr[d], end[d]) < 0)
1839 1384 : mpz_set (ctr[d], start[d]);
1840 : else
1841 : incr_ctr = false;
1842 : }
1843 : }
1844 :
1845 12497 : limit = mpz_get_ui (ptr);
1846 12497 : if (limit >= flag_max_array_constructor)
1847 : {
1848 0 : gfc_error ("The number of elements in the array constructor "
1849 : "at %L requires an increase of the allowed %d "
1850 : "upper limit. See %<-fmax-array-constructor%> "
1851 : "option", &expr->where, flag_max_array_constructor);
1852 0 : t = false;
1853 0 : goto cleanup;
1854 : }
1855 :
1856 12497 : cons = gfc_constructor_lookup (base, limit);
1857 12497 : if (cons == NULL)
1858 : {
1859 1 : gfc_error ("Error in array constructor referenced at %L",
1860 : &ref->u.ar.where);
1861 1 : t = false;
1862 1 : goto cleanup;
1863 : }
1864 12496 : gfc_constructor_append_expr (&expr->value.constructor,
1865 : gfc_copy_expr (cons->expr), NULL);
1866 : }
1867 :
1868 878 : cleanup:
1869 :
1870 1322 : mpz_clear (delta_mpz);
1871 1322 : mpz_clear (tmp_mpz);
1872 1322 : mpz_clear (nelts);
1873 4493 : for (d = 0; d < rank; d++)
1874 : {
1875 1849 : mpz_clear (delta[d]);
1876 1849 : mpz_clear (start[d]);
1877 1849 : mpz_clear (end[d]);
1878 1849 : mpz_clear (ctr[d]);
1879 1849 : mpz_clear (stride[d]);
1880 : }
1881 1322 : mpz_clear (ptr);
1882 1322 : gfc_constructor_free (base);
1883 1322 : return t;
1884 : }
1885 :
1886 : /* Pull a substring out of an expression. */
1887 :
1888 : static bool
1889 1258 : find_substring_ref (gfc_expr *p, gfc_expr **newp)
1890 : {
1891 1258 : gfc_charlen_t end;
1892 1258 : gfc_charlen_t start;
1893 1258 : gfc_charlen_t length;
1894 1258 : gfc_char_t *chr;
1895 :
1896 1258 : if (p->ref->u.ss.start->expr_type != EXPR_CONSTANT
1897 1258 : || p->ref->u.ss.end->expr_type != EXPR_CONSTANT)
1898 : return false;
1899 :
1900 1258 : *newp = gfc_copy_expr (p);
1901 1258 : free ((*newp)->value.character.string);
1902 :
1903 1258 : end = (gfc_charlen_t) mpz_get_si (p->ref->u.ss.end->value.integer);
1904 1258 : start = (gfc_charlen_t) mpz_get_si (p->ref->u.ss.start->value.integer);
1905 1258 : if (end >= start)
1906 1237 : length = end - start + 1;
1907 : else
1908 : length = 0;
1909 :
1910 1258 : chr = (*newp)->value.character.string = gfc_get_wide_string (length + 1);
1911 1258 : (*newp)->value.character.length = length;
1912 1258 : memcpy (chr, &p->value.character.string[start - 1],
1913 1258 : length * sizeof (gfc_char_t));
1914 1258 : chr[length] = '\0';
1915 1258 : return true;
1916 : }
1917 :
1918 :
1919 : /* Simplify inquiry references (%re/%im) of constant complex arrays.
1920 : Used by find_inquiry_ref. */
1921 :
1922 : static gfc_expr *
1923 60 : simplify_complex_array_inquiry_ref (gfc_expr *p, inquiry_type inquiry)
1924 : {
1925 60 : gfc_expr *e, *r, *result;
1926 60 : gfc_constructor_base base;
1927 60 : gfc_constructor *c;
1928 :
1929 60 : if ((inquiry != INQUIRY_RE && inquiry != INQUIRY_IM)
1930 60 : || p->expr_type != EXPR_ARRAY
1931 60 : || p->ts.type != BT_COMPLEX
1932 60 : || p->rank <= 0
1933 60 : || p->value.constructor == NULL
1934 120 : || !gfc_is_constant_array_expr (p))
1935 : return NULL;
1936 :
1937 : /* Simplify array sections. */
1938 60 : gfc_simplify_expr (p, 0);
1939 :
1940 60 : result = gfc_get_array_expr (BT_REAL, p->ts.kind, &p->where);
1941 60 : result->rank = p->rank;
1942 60 : result->shape = gfc_copy_shape (p->shape, p->rank);
1943 :
1944 60 : base = p->value.constructor;
1945 312 : for (c = gfc_constructor_first (base); c; c = gfc_constructor_next (c))
1946 : {
1947 252 : e = c->expr;
1948 252 : if (e->expr_type != EXPR_CONSTANT)
1949 0 : goto fail;
1950 :
1951 252 : r = gfc_get_constant_expr (BT_REAL, e->ts.kind, &e->where);
1952 252 : if (inquiry == INQUIRY_RE)
1953 126 : mpfr_set (r->value.real, mpc_realref (e->value.complex), GFC_RND_MODE);
1954 : else
1955 126 : mpfr_set (r->value.real, mpc_imagref (e->value.complex), GFC_RND_MODE);
1956 :
1957 252 : gfc_constructor_append_expr (&result->value.constructor, r, &e->where);
1958 : }
1959 :
1960 : return result;
1961 :
1962 0 : fail:
1963 0 : gfc_free_expr (result);
1964 0 : return NULL;
1965 : }
1966 :
1967 :
1968 : /* Pull an inquiry result out of an expression. */
1969 :
1970 : static bool
1971 2625 : find_inquiry_ref (gfc_expr *p, gfc_expr **newp)
1972 : {
1973 2625 : gfc_ref *ref;
1974 2625 : gfc_ref *inquiry = NULL;
1975 2625 : gfc_ref *inquiry_head;
1976 2625 : gfc_ref *ref_ss = NULL;
1977 2625 : gfc_expr *tmp;
1978 2625 : bool nofail = false;
1979 :
1980 2625 : tmp = gfc_copy_expr (p);
1981 :
1982 2625 : if (tmp->ref && tmp->ref->type == REF_INQUIRY)
1983 : {
1984 974 : inquiry = tmp->ref;
1985 974 : tmp->ref = NULL;
1986 : }
1987 : else
1988 : {
1989 1864 : for (ref = tmp->ref; ref; ref = ref->next)
1990 1864 : if (ref->next && ref->next->type == REF_INQUIRY)
1991 : {
1992 1651 : inquiry = ref->next;
1993 1651 : ref->next = NULL;
1994 1651 : if (ref->type == REF_SUBSTRING)
1995 38 : ref_ss = ref;
1996 : break;
1997 : }
1998 : }
1999 :
2000 2625 : if (!inquiry)
2001 : {
2002 0 : gfc_free_expr (tmp);
2003 0 : return false;
2004 : }
2005 :
2006 2625 : inquiry_head = inquiry;
2007 2625 : gfc_resolve_expr (tmp);
2008 :
2009 : /* Leave these to the backend since the type and kind is not confirmed until
2010 : resolution. */
2011 2625 : if (IS_INFERRED_TYPE (tmp))
2012 456 : goto cleanup;
2013 :
2014 : /* In principle there can be more than one inquiry reference. */
2015 2774 : for (; inquiry; inquiry = inquiry->next)
2016 : {
2017 2169 : switch (inquiry->u.i)
2018 : {
2019 458 : case INQUIRY_LEN:
2020 458 : if (tmp->ts.type != BT_CHARACTER)
2021 24 : goto cleanup;
2022 :
2023 434 : if (!gfc_notify_std (GFC_STD_F2003, "LEN part_ref at %C"))
2024 0 : goto cleanup;
2025 :
2026 : /* Inquire length of substring? */
2027 434 : if (ref_ss)
2028 : {
2029 32 : if (ref_ss->u.ss.start->expr_type == EXPR_CONSTANT
2030 32 : && ref_ss->u.ss.end->expr_type == EXPR_CONSTANT)
2031 : {
2032 32 : HOST_WIDE_INT istart, iend, length;
2033 32 : istart = gfc_mpz_get_hwi (ref_ss->u.ss.start->value.integer);
2034 32 : iend = gfc_mpz_get_hwi (ref_ss->u.ss.end->value.integer);
2035 :
2036 32 : if (istart <= iend)
2037 32 : length = iend - istart + 1;
2038 : else
2039 : length = 0;
2040 32 : *newp = gfc_get_int_expr (gfc_default_integer_kind,
2041 : NULL, length);
2042 32 : break;
2043 : }
2044 : else
2045 0 : goto cleanup;
2046 : }
2047 :
2048 402 : if (tmp->ts.u.cl->length
2049 105 : && tmp->ts.u.cl->length->expr_type == EXPR_CONSTANT)
2050 69 : *newp = gfc_copy_expr (tmp->ts.u.cl->length);
2051 333 : else if (tmp->expr_type == EXPR_CONSTANT)
2052 12 : *newp = gfc_get_int_expr (gfc_default_integer_kind,
2053 12 : NULL, tmp->value.character.length);
2054 : else
2055 : {
2056 321 : gfc_expr *kind_expr;
2057 642 : kind_expr = gfc_get_int_expr (gfc_default_integer_kind, NULL,
2058 321 : tmp->ts.kind);
2059 321 : *newp = gfc_simplify_len (tmp, kind_expr);
2060 321 : if (*newp)
2061 : break;
2062 285 : goto cleanup;
2063 : }
2064 :
2065 : break;
2066 :
2067 312 : case INQUIRY_KIND:
2068 312 : if (tmp->ts.type == BT_DERIVED || tmp->ts.type == BT_CLASS)
2069 0 : goto cleanup;
2070 :
2071 312 : if (!gfc_notify_std (GFC_STD_F2003, "KIND part_ref at %C"))
2072 0 : goto cleanup;
2073 :
2074 624 : *newp = gfc_get_int_expr (gfc_default_integer_kind,
2075 312 : NULL, tmp->ts.kind);
2076 312 : break;
2077 :
2078 828 : case INQUIRY_RE:
2079 828 : if (tmp->ts.type != BT_COMPLEX)
2080 77 : goto cleanup;
2081 :
2082 751 : if (!gfc_notify_std (GFC_STD_F2008, "RE part_ref at %C"))
2083 0 : goto cleanup;
2084 :
2085 751 : if (tmp->expr_type == EXPR_ARRAY)
2086 : {
2087 30 : *newp = simplify_complex_array_inquiry_ref (tmp, INQUIRY_RE);
2088 30 : if (*newp != NULL)
2089 : {
2090 : nofail = true;
2091 : break;
2092 : }
2093 : }
2094 :
2095 721 : if (tmp->expr_type != EXPR_CONSTANT)
2096 667 : goto cleanup;
2097 :
2098 54 : *newp = gfc_get_constant_expr (BT_REAL, tmp->ts.kind, &tmp->where);
2099 54 : mpfr_set ((*newp)->value.real,
2100 : mpc_realref (tmp->value.complex), GFC_RND_MODE);
2101 54 : break;
2102 :
2103 571 : case INQUIRY_IM:
2104 571 : if (tmp->ts.type != BT_COMPLEX)
2105 74 : goto cleanup;
2106 :
2107 497 : if (!gfc_notify_std (GFC_STD_F2008, "IM part_ref at %C"))
2108 0 : goto cleanup;
2109 :
2110 497 : if (tmp->expr_type == EXPR_ARRAY)
2111 : {
2112 30 : *newp = simplify_complex_array_inquiry_ref (tmp, INQUIRY_IM);
2113 30 : if (*newp != NULL)
2114 : {
2115 : nofail = true;
2116 : break;
2117 : }
2118 : }
2119 :
2120 467 : if (tmp->expr_type != EXPR_CONSTANT)
2121 437 : goto cleanup;
2122 :
2123 30 : *newp = gfc_get_constant_expr (BT_REAL, tmp->ts.kind, &tmp->where);
2124 30 : mpfr_set ((*newp)->value.real,
2125 : mpc_imagref (tmp->value.complex), GFC_RND_MODE);
2126 30 : break;
2127 : }
2128 :
2129 605 : if (inquiry->next)
2130 0 : gfc_replace_expr (tmp, *newp);
2131 : }
2132 :
2133 605 : if (!(*newp))
2134 0 : goto cleanup;
2135 605 : else if ((*newp)->expr_type != EXPR_CONSTANT && !nofail)
2136 : {
2137 30 : gfc_free_expr (*newp);
2138 30 : goto cleanup;
2139 : }
2140 :
2141 575 : gfc_free_expr (tmp);
2142 575 : gfc_free_ref_list (inquiry_head);
2143 575 : return true;
2144 :
2145 2050 : cleanup:
2146 2050 : gfc_free_expr (tmp);
2147 2050 : gfc_free_ref_list (inquiry_head);
2148 2050 : return false;
2149 : }
2150 :
2151 :
2152 :
2153 : /* Simplify a subobject reference of a constructor. This occurs when
2154 : parameter variable values are substituted. */
2155 :
2156 : static bool
2157 143849 : simplify_const_ref (gfc_expr *p)
2158 : {
2159 143849 : gfc_constructor *cons, *c;
2160 143849 : gfc_expr *newp = NULL;
2161 143849 : gfc_ref *last_ref;
2162 :
2163 302632 : while (p->ref)
2164 : {
2165 17413 : switch (p->ref->type)
2166 : {
2167 14356 : case REF_ARRAY:
2168 : /* <type/kind spec>, parameter :: x(<int>) = scalar_expr
2169 : will generate this. */
2170 14356 : if (p->expr_type != EXPR_ARRAY)
2171 : {
2172 45 : if (p->ref->u.ar.type == AR_ELEMENT)
2173 : {
2174 : int dim;
2175 45 : for (dim = 0; dim < p->ref->u.ar.dimen; dim++)
2176 27 : if (!p->ref->u.ar.start[dim]
2177 27 : || p->ref->u.ar.start[dim]->expr_type != EXPR_CONSTANT)
2178 : return true;
2179 : }
2180 :
2181 36 : remove_subobject_ref (p, NULL);
2182 36 : break;
2183 : }
2184 :
2185 14311 : switch (p->ref->u.ar.type)
2186 : {
2187 4830 : case AR_ELEMENT:
2188 4830 : if (!find_array_element (p->value.constructor, &p->ref->u.ar, &cons))
2189 : return false;
2190 :
2191 4812 : if (!cons)
2192 : return true;
2193 :
2194 2804 : remove_subobject_ref (p, cons);
2195 2804 : break;
2196 :
2197 1322 : case AR_SECTION:
2198 1322 : if (!find_array_section (p, p->ref))
2199 : return false;
2200 878 : p->ref->u.ar.type = AR_FULL;
2201 :
2202 : /* Fall through. */
2203 :
2204 9037 : case AR_FULL:
2205 9037 : if (p->ref->next != NULL
2206 336 : && (p->ts.type == BT_CHARACTER || gfc_bt_struct (p->ts.type)))
2207 : {
2208 336 : for (c = gfc_constructor_first (p->value.constructor);
2209 2950 : c; c = gfc_constructor_next (c))
2210 : {
2211 2614 : c->expr->ref = gfc_copy_ref (p->ref->next);
2212 2614 : if (!simplify_const_ref (c->expr))
2213 : return false;
2214 : }
2215 :
2216 75 : if (gfc_bt_struct (p->ts.type)
2217 261 : && p->ref->next
2218 597 : && (c = gfc_constructor_first (p->value.constructor)))
2219 : {
2220 : /* There may have been component references. */
2221 261 : p->ts = c->expr->ts;
2222 : }
2223 :
2224 336 : last_ref = p->ref;
2225 690 : for (; last_ref->next; last_ref = last_ref->next) {};
2226 :
2227 336 : if (p->ts.type == BT_CHARACTER
2228 97 : && last_ref->type == REF_SUBSTRING)
2229 : {
2230 : /* If this is a CHARACTER array and we possibly took
2231 : a substring out of it, update the type-spec's
2232 : character length according to the first element
2233 : (as all should have the same length). */
2234 75 : gfc_charlen_t string_len;
2235 75 : if ((c = gfc_constructor_first (p->value.constructor)))
2236 : {
2237 75 : const gfc_expr* first = c->expr;
2238 75 : gcc_assert (first->expr_type == EXPR_CONSTANT);
2239 75 : gcc_assert (first->ts.type == BT_CHARACTER);
2240 75 : string_len = first->value.character.length;
2241 : }
2242 : else
2243 : string_len = 0;
2244 :
2245 75 : if (!p->ts.u.cl)
2246 : {
2247 0 : if (p->symtree)
2248 0 : p->ts.u.cl = gfc_new_charlen (p->symtree->n.sym->ns,
2249 : NULL);
2250 : else
2251 0 : p->ts.u.cl = gfc_new_charlen (gfc_current_ns,
2252 : NULL);
2253 : }
2254 : else
2255 75 : gfc_free_expr (p->ts.u.cl->length);
2256 :
2257 75 : p->ts.u.cl->length
2258 75 : = gfc_get_int_expr (gfc_charlen_int_kind,
2259 : NULL, string_len);
2260 : }
2261 : }
2262 9037 : gfc_free_ref_list (p->ref);
2263 9037 : p->ref = NULL;
2264 9037 : break;
2265 :
2266 : default:
2267 : return true;
2268 : }
2269 :
2270 : break;
2271 :
2272 1799 : case REF_COMPONENT:
2273 1799 : cons = find_component_ref (p->value.constructor, p->ref);
2274 1799 : remove_subobject_ref (p, cons);
2275 1799 : break;
2276 :
2277 0 : case REF_INQUIRY:
2278 0 : if (!find_inquiry_ref (p, &newp))
2279 : return false;
2280 :
2281 0 : gfc_replace_expr (p, newp);
2282 0 : gfc_free_ref_list (p->ref);
2283 0 : p->ref = NULL;
2284 0 : break;
2285 :
2286 1258 : case REF_SUBSTRING:
2287 1258 : if (!find_substring_ref (p, &newp))
2288 : return false;
2289 :
2290 1258 : gfc_replace_expr (p, newp);
2291 1258 : gfc_free_ref_list (p->ref);
2292 1258 : p->ref = NULL;
2293 1258 : break;
2294 : }
2295 : }
2296 :
2297 : return true;
2298 : }
2299 :
2300 :
2301 : /* Simplify a chain of references. */
2302 :
2303 : static bool
2304 15238200 : simplify_ref_chain (gfc_ref *ref, int type, gfc_expr **p)
2305 : {
2306 15238200 : int n;
2307 15238200 : gfc_expr *newp = NULL;
2308 :
2309 15589994 : for (; ref; ref = ref->next)
2310 : {
2311 354420 : switch (ref->type)
2312 : {
2313 : case REF_ARRAY:
2314 608267 : for (n = 0; n < ref->u.ar.dimen; n++)
2315 : {
2316 334358 : if (!gfc_simplify_expr (ref->u.ar.start[n], type))
2317 : return false;
2318 334358 : if (!gfc_simplify_expr (ref->u.ar.end[n], type))
2319 : return false;
2320 334358 : if (!gfc_simplify_expr (ref->u.ar.stride[n], type))
2321 : return false;
2322 : }
2323 : break;
2324 :
2325 10276 : case REF_SUBSTRING:
2326 10276 : if (!gfc_simplify_expr (ref->u.ss.start, type))
2327 : return false;
2328 10276 : if (!gfc_simplify_expr (ref->u.ss.end, type))
2329 : return false;
2330 : break;
2331 :
2332 2625 : case REF_INQUIRY:
2333 2625 : if (!find_inquiry_ref (*p, &newp))
2334 : return false;
2335 :
2336 575 : gfc_replace_expr (*p, newp);
2337 575 : gfc_free_ref_list ((*p)->ref);
2338 575 : (*p)->ref = NULL;
2339 575 : return true;
2340 :
2341 : default:
2342 : break;
2343 : }
2344 : }
2345 : return true;
2346 : }
2347 :
2348 :
2349 : /* Try to substitute the value of a parameter variable. */
2350 :
2351 : static bool
2352 15005 : simplify_parameter_variable (gfc_expr *p, int type)
2353 : {
2354 15005 : gfc_expr *e;
2355 15005 : bool t;
2356 :
2357 : /* Set rank and check array ref; as resolve_variable calls
2358 : gfc_simplify_expr, call gfc_resolve_ref + gfc_expression_rank instead. */
2359 15005 : if (!gfc_resolve_ref (p))
2360 : {
2361 1 : gfc_error_check ();
2362 1 : return false;
2363 : }
2364 15004 : gfc_expression_rank (p);
2365 :
2366 : /* Is this an inquiry? */
2367 15004 : bool inquiry = false;
2368 15004 : gfc_ref* ref = p->ref;
2369 30865 : while (ref)
2370 : {
2371 15989 : if (ref->type == REF_INQUIRY)
2372 : break;
2373 15861 : ref = ref->next;
2374 : }
2375 15004 : if (ref && ref->type == REF_INQUIRY)
2376 128 : inquiry = ref->u.i == INQUIRY_LEN || ref->u.i == INQUIRY_KIND;
2377 :
2378 15004 : if (gfc_is_size_zero_array (p))
2379 : {
2380 690 : if (p->expr_type == EXPR_ARRAY)
2381 : return true;
2382 :
2383 690 : e = gfc_get_expr ();
2384 690 : e->expr_type = EXPR_ARRAY;
2385 690 : e->ts = p->ts;
2386 690 : e->rank = p->rank;
2387 690 : e->corank = p->corank;
2388 690 : e->value.constructor = NULL;
2389 690 : e->shape = gfc_copy_shape (p->shape, p->rank);
2390 690 : e->where = p->where;
2391 : /* If %kind and %len are not used then we're done, otherwise
2392 : drop through for simplification. */
2393 690 : if (!inquiry)
2394 : {
2395 620 : gfc_replace_expr (p, e);
2396 620 : return true;
2397 : }
2398 : }
2399 : else
2400 : {
2401 14314 : e = gfc_copy_expr (p->symtree->n.sym->value);
2402 14314 : if (e == NULL)
2403 : return false;
2404 :
2405 14309 : gfc_free_shape (&e->shape, e->rank);
2406 14309 : e->shape = gfc_copy_shape (p->shape, p->rank);
2407 14309 : e->rank = p->rank;
2408 14309 : e->corank = p->corank;
2409 :
2410 14309 : if (e->ts.type == BT_CHARACTER && p->ts.u.cl)
2411 3510 : e->ts = p->ts;
2412 : }
2413 :
2414 14379 : if (e->ts.type == BT_CHARACTER && e->ts.u.cl == NULL)
2415 0 : e->ts.u.cl = gfc_new_charlen (gfc_current_ns, p->ts.u.cl);
2416 :
2417 : /* Do not copy subobject refs for constant. */
2418 14379 : if (e->expr_type != EXPR_CONSTANT && p->ref != NULL)
2419 14372 : e->ref = gfc_copy_ref (p->ref);
2420 14379 : t = gfc_simplify_expr (e, type);
2421 14379 : e->where = p->where;
2422 :
2423 : /* Only use the simplification if it eliminated all subobject references. */
2424 14379 : if (t && !e->ref)
2425 11897 : gfc_replace_expr (p, e);
2426 : else
2427 2482 : gfc_free_expr (e);
2428 :
2429 : return t;
2430 : }
2431 :
2432 :
2433 : static bool
2434 : scalarize_intrinsic_call (gfc_expr *, bool init_flag);
2435 :
2436 : /* Given an expression, simplify it by collapsing constant
2437 : expressions. Most simplification takes place when the expression
2438 : tree is being constructed. If an intrinsic function is simplified
2439 : at some point, we get called again to collapse the result against
2440 : other constants.
2441 :
2442 : We work by recursively simplifying expression nodes, simplifying
2443 : intrinsic functions where possible, which can lead to further
2444 : constant collapsing. If an operator has constant operand(s), we
2445 : rip the expression apart, and rebuild it, hoping that it becomes
2446 : something simpler.
2447 :
2448 : The expression type is defined for:
2449 : 0 Basic expression parsing
2450 : 1 Simplifying array constructors -- will substitute
2451 : iterator values.
2452 : Returns false on error, true otherwise.
2453 : NOTE: Will return true even if the expression cannot be simplified. */
2454 :
2455 : bool
2456 56708246 : gfc_simplify_expr (gfc_expr *p, int type)
2457 : {
2458 56708246 : gfc_actual_arglist *ap;
2459 56708246 : gfc_intrinsic_sym* isym = NULL;
2460 :
2461 :
2462 56708246 : if (p == NULL)
2463 : return true;
2464 :
2465 50346067 : switch (p->expr_type)
2466 : {
2467 17137149 : case EXPR_CONSTANT:
2468 17137149 : if (p->ref && p->ref->type == REF_INQUIRY)
2469 40 : simplify_ref_chain (p->ref, type, &p);
2470 : break;
2471 : case EXPR_NULL:
2472 : break;
2473 :
2474 600501 : case EXPR_FUNCTION:
2475 : // For array-bound functions, we don't need to optimize
2476 : // the 'array' argument. In particular, if the argument
2477 : // is a PARAMETER, simplifying might convert an EXPR_VARIABLE
2478 : // into an EXPR_ARRAY; the latter has lbound = 1, the former
2479 : // can have any lbound.
2480 600501 : ap = p->value.function.actual;
2481 600501 : if (p->value.function.isym &&
2482 562969 : (p->value.function.isym->id == GFC_ISYM_LBOUND
2483 549743 : || p->value.function.isym->id == GFC_ISYM_UBOUND
2484 541750 : || p->value.function.isym->id == GFC_ISYM_LCOBOUND
2485 541476 : || p->value.function.isym->id == GFC_ISYM_UCOBOUND
2486 541194 : || p->value.function.isym->id == GFC_ISYM_SHAPE))
2487 26624 : ap = ap->next;
2488 :
2489 1723899 : for ( ; ap; ap = ap->next)
2490 1123612 : if (!gfc_simplify_expr (ap->expr, type))
2491 : return false;
2492 :
2493 600287 : if (p->value.function.isym != NULL
2494 600287 : && gfc_intrinsic_func_interface (p, 1) == MATCH_ERROR)
2495 : return false;
2496 :
2497 600228 : if (p->symtree && (p->value.function.isym || p->ts.type == BT_UNKNOWN))
2498 : {
2499 243057 : isym = gfc_find_function (p->symtree->n.sym->name);
2500 243057 : if (isym && isym->elemental)
2501 123061 : scalarize_intrinsic_call (p, false);
2502 : }
2503 :
2504 : break;
2505 :
2506 1447 : case EXPR_SUBSTRING:
2507 1447 : if (!simplify_ref_chain (p->ref, type, &p))
2508 : return false;
2509 :
2510 1447 : if (gfc_is_constant_expr (p))
2511 : {
2512 775 : gfc_char_t *s;
2513 775 : HOST_WIDE_INT start, end;
2514 :
2515 775 : start = 0;
2516 775 : if (p->ref && p->ref->u.ss.start)
2517 : {
2518 750 : gfc_extract_hwi (p->ref->u.ss.start, &start);
2519 750 : start--; /* Convert from one-based to zero-based. */
2520 : }
2521 :
2522 775 : if (start < 0)
2523 3 : return false;
2524 :
2525 772 : end = p->value.character.length;
2526 772 : if (p->ref && p->ref->u.ss.end)
2527 747 : gfc_extract_hwi (p->ref->u.ss.end, &end);
2528 :
2529 772 : if (end < start)
2530 7 : end = start;
2531 :
2532 772 : s = gfc_get_wide_string (end - start + 2);
2533 772 : memcpy (s, p->value.character.string + start,
2534 772 : (end - start) * sizeof (gfc_char_t));
2535 772 : s[end - start + 1] = '\0'; /* TODO: C-style string. */
2536 772 : free (p->value.character.string);
2537 772 : p->value.character.string = s;
2538 772 : p->value.character.length = end - start;
2539 772 : p->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
2540 1544 : p->ts.u.cl->length = gfc_get_int_expr (gfc_charlen_int_kind,
2541 : NULL,
2542 772 : p->value.character.length);
2543 772 : gfc_free_ref_list (p->ref);
2544 772 : p->ref = NULL;
2545 772 : p->expr_type = EXPR_CONSTANT;
2546 : }
2547 : break;
2548 :
2549 17353228 : case EXPR_OP:
2550 17353228 : if (!simplify_intrinsic_op (p, type))
2551 : return false;
2552 : break;
2553 :
2554 27 : case EXPR_CONDITIONAL:
2555 27 : if (!simplify_conditional (p, type))
2556 : return false;
2557 : break;
2558 :
2559 15108996 : case EXPR_VARIABLE:
2560 : /* Only substitute array parameter variables if we are in an
2561 : initialization expression, or we want a subsection. */
2562 15108996 : if (p->symtree->n.sym->attr.flavor == FL_PARAMETER
2563 14597 : && (gfc_init_expr_flag || p->ref
2564 2 : || (p->symtree->n.sym->value
2565 1 : && p->symtree->n.sym->value->expr_type != EXPR_ARRAY)))
2566 : {
2567 14596 : if (!simplify_parameter_variable (p, type))
2568 : return false;
2569 14138 : if (!iter_stack)
2570 : break;
2571 : }
2572 :
2573 15095294 : if (type == 1)
2574 : {
2575 14073893 : gfc_simplify_iterator_var (p);
2576 : }
2577 :
2578 : /* Simplify subcomponent references. */
2579 15095294 : if (!simplify_ref_chain (p->ref, type, &p))
2580 : return false;
2581 :
2582 : break;
2583 :
2584 141419 : case EXPR_STRUCTURE:
2585 141419 : case EXPR_ARRAY:
2586 141419 : if (!simplify_ref_chain (p->ref, type, &p))
2587 : return false;
2588 :
2589 : /* If the following conditions hold, we found something like kind type
2590 : inquiry of the form a(2)%kind while simplify the ref chain. */
2591 141418 : if (p->expr_type == EXPR_CONSTANT && !p->ref && !p->rank && !p->shape)
2592 : return true;
2593 :
2594 141235 : if (!simplify_constructor (p->value.constructor, type))
2595 : return false;
2596 :
2597 141235 : if (p->expr_type == EXPR_ARRAY && p->ref && p->ref->type == REF_ARRAY
2598 14275 : && p->ref->u.ar.type == AR_FULL)
2599 8148 : gfc_expand_constructor (p, false);
2600 :
2601 141235 : if (!simplify_const_ref (p))
2602 : return false;
2603 :
2604 : break;
2605 :
2606 : case EXPR_COMPCALL:
2607 : case EXPR_PPC:
2608 : break;
2609 :
2610 0 : case EXPR_UNKNOWN:
2611 0 : gcc_unreachable ();
2612 : }
2613 :
2614 : return true;
2615 : }
2616 :
2617 :
2618 : /* Try simplification of an expression via gfc_simplify_expr.
2619 : When an error occurs (arithmetic or otherwise), roll back. */
2620 :
2621 : bool
2622 0 : gfc_try_simplify_expr (gfc_expr *e, int type)
2623 : {
2624 0 : gfc_expr *n;
2625 0 : bool t, saved_div0;
2626 :
2627 0 : if (e == NULL || e->expr_type == EXPR_CONSTANT)
2628 : return true;
2629 :
2630 0 : saved_div0 = gfc_seen_div0;
2631 0 : gfc_seen_div0 = false;
2632 0 : n = gfc_copy_expr (e);
2633 0 : t = gfc_simplify_expr (n, type) && !gfc_seen_div0;
2634 0 : if (t)
2635 0 : gfc_replace_expr (e, n);
2636 : else
2637 0 : gfc_free_expr (n);
2638 0 : gfc_seen_div0 = saved_div0;
2639 0 : return t;
2640 : }
2641 :
2642 :
2643 : /* Returns the type of an expression with the exception that iterator
2644 : variables are automatically integers no matter what else they may
2645 : be declared as. */
2646 :
2647 : static bt
2648 5076 : et0 (gfc_expr *e)
2649 : {
2650 5076 : if (e->expr_type == EXPR_VARIABLE && gfc_check_iter_variable (e))
2651 : return BT_INTEGER;
2652 :
2653 5076 : return e->ts.type;
2654 : }
2655 :
2656 :
2657 : /* Scalarize an expression for an elemental intrinsic call. */
2658 :
2659 : static bool
2660 123301 : scalarize_intrinsic_call (gfc_expr *e, bool init_flag)
2661 : {
2662 123301 : gfc_actual_arglist *a, *b;
2663 123301 : gfc_constructor_base ctor;
2664 123301 : gfc_constructor *args[5] = {}; /* Avoid uninitialized warnings. */
2665 123301 : gfc_constructor *ci, *new_ctor;
2666 123301 : gfc_expr *expr, *old, *p;
2667 123301 : int n, i, rank[5], array_arg;
2668 :
2669 123301 : if (e == NULL)
2670 : return false;
2671 :
2672 123301 : a = e->value.function.actual;
2673 131237 : for (; a; a = a->next)
2674 130471 : if (a->expr && !gfc_is_constant_expr (a->expr))
2675 : return false;
2676 :
2677 : /* Find which, if any, arguments are arrays. Assume that the old
2678 : expression carries the type information and that the first arg
2679 : that is an array expression carries all the shape information.*/
2680 766 : n = array_arg = 0;
2681 766 : a = e->value.function.actual;
2682 1544 : for (; a; a = a->next)
2683 : {
2684 1212 : n++;
2685 1212 : if (!a->expr || a->expr->expr_type != EXPR_ARRAY)
2686 778 : continue;
2687 434 : array_arg = n;
2688 434 : expr = gfc_copy_expr (a->expr);
2689 434 : break;
2690 : }
2691 :
2692 766 : if (!array_arg)
2693 : return false;
2694 :
2695 434 : old = gfc_copy_expr (e);
2696 :
2697 434 : gfc_constructor_free (expr->value.constructor);
2698 434 : expr->value.constructor = NULL;
2699 434 : expr->ts = old->ts;
2700 434 : expr->where = old->where;
2701 434 : expr->expr_type = EXPR_ARRAY;
2702 :
2703 : /* Copy the array argument constructors into an array, with nulls
2704 : for the scalars. */
2705 434 : n = 0;
2706 434 : a = old->value.function.actual;
2707 1360 : for (; a; a = a->next)
2708 : {
2709 : /* Check that this is OK for an initialization expression. */
2710 926 : if (a->expr && init_flag && !gfc_check_init_expr (a->expr))
2711 0 : goto cleanup;
2712 :
2713 926 : rank[n] = 0;
2714 926 : if (a->expr && a->expr->rank && a->expr->expr_type == EXPR_VARIABLE)
2715 : {
2716 0 : rank[n] = a->expr->rank;
2717 0 : ctor = a->expr->symtree->n.sym->value->value.constructor;
2718 0 : args[n] = gfc_constructor_first (ctor);
2719 : }
2720 926 : else if (a->expr && a->expr->expr_type == EXPR_ARRAY)
2721 : {
2722 475 : if (a->expr->rank)
2723 : rank[n] = a->expr->rank;
2724 : else
2725 0 : rank[n] = 1;
2726 475 : ctor = a->expr->value.constructor;
2727 475 : args[n] = gfc_constructor_first (ctor);
2728 : }
2729 : else
2730 451 : args[n] = NULL;
2731 :
2732 926 : n++;
2733 : }
2734 :
2735 : /* Using the array argument as the master, step through the array
2736 : calling the function for each element and advancing the array
2737 : constructors together. */
2738 3502 : for (ci = args[array_arg - 1]; ci; ci = gfc_constructor_next (ci))
2739 : {
2740 3068 : new_ctor = gfc_constructor_append_expr (&expr->value.constructor,
2741 : gfc_copy_expr (old), NULL);
2742 :
2743 3068 : gfc_free_actual_arglist (new_ctor->expr->value.function.actual);
2744 3068 : a = NULL;
2745 3068 : b = old->value.function.actual;
2746 9277 : for (i = 0; i < n; i++)
2747 : {
2748 6209 : if (a == NULL)
2749 6136 : new_ctor->expr->value.function.actual
2750 3068 : = a = gfc_get_actual_arglist ();
2751 : else
2752 : {
2753 3141 : a->next = gfc_get_actual_arglist ();
2754 3141 : a = a->next;
2755 : }
2756 :
2757 6209 : if (args[i])
2758 4069 : a->expr = gfc_copy_expr (args[i]->expr);
2759 : else
2760 2140 : a->expr = gfc_copy_expr (b->expr);
2761 :
2762 6209 : b = b->next;
2763 : }
2764 :
2765 : /* Simplify the function calls. If the simplification fails, the
2766 : error will be flagged up down-stream or the library will deal
2767 : with it. */
2768 3068 : p = gfc_copy_expr (new_ctor->expr);
2769 :
2770 3068 : if (!gfc_simplify_expr (p, init_flag))
2771 13 : gfc_free_expr (p);
2772 : else
2773 3055 : gfc_replace_expr (new_ctor->expr, p);
2774 :
2775 9277 : for (i = 0; i < n; i++)
2776 6209 : if (args[i])
2777 4069 : args[i] = gfc_constructor_next (args[i]);
2778 :
2779 6209 : for (i = 1; i < n; i++)
2780 3141 : if (rank[i] && ((args[i] != NULL && args[array_arg - 1] == NULL)
2781 1133 : || (args[i] == NULL && args[array_arg - 1] != NULL)))
2782 0 : goto compliance;
2783 : }
2784 :
2785 434 : free_expr0 (e);
2786 434 : *e = *expr;
2787 : /* Free "expr" but not the pointers it contains. */
2788 434 : free (expr);
2789 434 : gfc_free_expr (old);
2790 434 : return true;
2791 :
2792 0 : compliance:
2793 0 : gfc_error_now ("elemental function arguments at %C are not compliant");
2794 :
2795 0 : cleanup:
2796 0 : gfc_free_expr (expr);
2797 0 : gfc_free_expr (old);
2798 0 : return false;
2799 : }
2800 :
2801 :
2802 : static bool
2803 3772 : check_intrinsic_op (gfc_expr *e, bool (*check_function) (gfc_expr *))
2804 : {
2805 3772 : gfc_expr *op1 = e->value.op.op1;
2806 3772 : gfc_expr *op2 = e->value.op.op2;
2807 :
2808 3772 : if (!(*check_function)(op1))
2809 : return false;
2810 :
2811 2964 : switch (e->value.op.op)
2812 : {
2813 535 : case INTRINSIC_UPLUS:
2814 535 : case INTRINSIC_UMINUS:
2815 535 : if (!numeric_type (et0 (op1)))
2816 0 : goto not_numeric;
2817 : break;
2818 :
2819 145 : case INTRINSIC_EQ:
2820 145 : case INTRINSIC_EQ_OS:
2821 145 : case INTRINSIC_NE:
2822 145 : case INTRINSIC_NE_OS:
2823 145 : case INTRINSIC_GT:
2824 145 : case INTRINSIC_GT_OS:
2825 145 : case INTRINSIC_GE:
2826 145 : case INTRINSIC_GE_OS:
2827 145 : case INTRINSIC_LT:
2828 145 : case INTRINSIC_LT_OS:
2829 145 : case INTRINSIC_LE:
2830 145 : case INTRINSIC_LE_OS:
2831 145 : if (!(*check_function)(op2))
2832 : return false;
2833 :
2834 217 : if (!(et0 (op1) == BT_CHARACTER && et0 (op2) == BT_CHARACTER)
2835 145 : && !(numeric_type (et0 (op1)) && numeric_type (et0 (op2))))
2836 : {
2837 0 : gfc_error ("Numeric or CHARACTER operands are required in "
2838 : "expression at %L", &e->where);
2839 0 : return false;
2840 : }
2841 : break;
2842 :
2843 2233 : case INTRINSIC_PLUS:
2844 2233 : case INTRINSIC_MINUS:
2845 2233 : case INTRINSIC_TIMES:
2846 2233 : case INTRINSIC_DIVIDE:
2847 2233 : case INTRINSIC_POWER:
2848 2233 : if (!(*check_function)(op2))
2849 : return false;
2850 :
2851 2089 : if (!numeric_type (et0 (op1)) || !numeric_type (et0 (op2)))
2852 0 : goto not_numeric;
2853 :
2854 : break;
2855 :
2856 0 : case INTRINSIC_CONCAT:
2857 0 : if (!(*check_function)(op2))
2858 : return false;
2859 :
2860 0 : if (et0 (op1) != BT_CHARACTER || et0 (op2) != BT_CHARACTER)
2861 : {
2862 0 : gfc_error ("Concatenation operator in expression at %L "
2863 : "must have two CHARACTER operands", &op1->where);
2864 0 : return false;
2865 : }
2866 :
2867 0 : if (op1->ts.kind != op2->ts.kind)
2868 : {
2869 0 : gfc_error ("Concat operator at %L must concatenate strings of the "
2870 : "same kind", &e->where);
2871 0 : return false;
2872 : }
2873 :
2874 : break;
2875 :
2876 0 : case INTRINSIC_NOT:
2877 0 : if (et0 (op1) != BT_LOGICAL)
2878 : {
2879 0 : gfc_error (".NOT. operator in expression at %L must have a LOGICAL "
2880 : "operand", &op1->where);
2881 0 : return false;
2882 : }
2883 :
2884 : break;
2885 :
2886 0 : case INTRINSIC_AND:
2887 0 : case INTRINSIC_OR:
2888 0 : case INTRINSIC_EQV:
2889 0 : case INTRINSIC_NEQV:
2890 0 : if (!(*check_function)(op2))
2891 : return false;
2892 :
2893 0 : if (et0 (op1) != BT_LOGICAL || et0 (op2) != BT_LOGICAL)
2894 : {
2895 0 : gfc_error ("LOGICAL operands are required in expression at %L",
2896 : &e->where);
2897 0 : return false;
2898 : }
2899 :
2900 : break;
2901 :
2902 : case INTRINSIC_PARENTHESES:
2903 : break;
2904 :
2905 0 : default:
2906 0 : gfc_error ("Only intrinsic operators can be used in expression at %L",
2907 : &e->where);
2908 0 : return false;
2909 : }
2910 :
2911 : return true;
2912 :
2913 0 : not_numeric:
2914 0 : gfc_error ("Numeric operands are required in expression at %L", &e->where);
2915 :
2916 0 : return false;
2917 : }
2918 :
2919 : /* F2003, 7.1.7 (3): In init expression, allocatable components
2920 : must not be data-initialized. */
2921 : static bool
2922 2258 : check_alloc_comp_init (gfc_expr *e)
2923 : {
2924 2258 : gfc_component *comp;
2925 2258 : gfc_constructor *ctor;
2926 :
2927 2258 : gcc_assert (e->expr_type == EXPR_STRUCTURE);
2928 2258 : gcc_assert (e->ts.type == BT_DERIVED || e->ts.type == BT_CLASS);
2929 :
2930 2258 : for (comp = e->ts.u.derived->components,
2931 2258 : ctor = gfc_constructor_first (e->value.constructor);
2932 5211 : comp; comp = comp->next, ctor = gfc_constructor_next (ctor))
2933 : {
2934 2954 : if (comp->attr.allocatable && ctor->expr
2935 31 : && ctor->expr->expr_type != EXPR_NULL)
2936 : {
2937 1 : gfc_error ("Invalid initialization expression for ALLOCATABLE "
2938 : "component %qs in structure constructor at %L",
2939 : comp->name, &ctor->expr->where);
2940 1 : return false;
2941 : }
2942 : }
2943 :
2944 : return true;
2945 : }
2946 :
2947 : static match
2948 593 : check_init_expr_arguments (gfc_expr *e)
2949 : {
2950 593 : gfc_actual_arglist *ap;
2951 :
2952 1556 : for (ap = e->value.function.actual; ap; ap = ap->next)
2953 1276 : if (!gfc_check_init_expr (ap->expr))
2954 : return MATCH_ERROR;
2955 :
2956 : return MATCH_YES;
2957 : }
2958 :
2959 : static bool check_restricted (gfc_expr *);
2960 :
2961 : /* F95, 7.1.6.1, Initialization expressions, (7)
2962 : F2003, 7.1.7 Initialization expression, (8)
2963 : F2008, 7.1.12 Constant expression, (4) */
2964 :
2965 : static match
2966 4204 : check_inquiry (gfc_expr *e, int not_restricted)
2967 : {
2968 4204 : const char *name;
2969 4204 : const char *const *functions;
2970 :
2971 4204 : static const char *const inquiry_func_f95[] = {
2972 : "lbound", "shape", "size", "ubound",
2973 : "bit_size", "len", "kind",
2974 : "digits", "epsilon", "huge", "maxexponent", "minexponent",
2975 : "precision", "radix", "range", "tiny",
2976 : NULL
2977 : };
2978 :
2979 4204 : static const char *const inquiry_func_f2003[] = {
2980 : "lbound", "shape", "size", "ubound",
2981 : "bit_size", "len", "kind",
2982 : "digits", "epsilon", "huge", "maxexponent", "minexponent",
2983 : "precision", "radix", "range", "tiny",
2984 : "new_line", NULL
2985 : };
2986 :
2987 : /* std=f2008+ or -std=gnu */
2988 4204 : static const char *const inquiry_func_gnu[] = {
2989 : "lbound", "shape", "size", "ubound",
2990 : "bit_size", "len", "kind",
2991 : "digits", "epsilon", "huge", "maxexponent", "minexponent",
2992 : "precision", "radix", "range", "tiny",
2993 : "new_line", "storage_size", NULL
2994 : };
2995 :
2996 4204 : int i = 0;
2997 4204 : gfc_actual_arglist *ap;
2998 4204 : gfc_symbol *sym;
2999 4204 : gfc_symbol *asym;
3000 :
3001 4204 : if (!e->value.function.isym
3002 4098 : || !e->value.function.isym->inquiry)
3003 : return MATCH_NO;
3004 :
3005 : /* An undeclared parameter will get us here (PR25018). */
3006 2853 : if (e->symtree == NULL)
3007 : return MATCH_NO;
3008 :
3009 2851 : sym = e->symtree->n.sym;
3010 :
3011 2851 : if (sym->from_intmod)
3012 : {
3013 2 : if (sym->from_intmod == INTMOD_ISO_FORTRAN_ENV
3014 0 : && sym->intmod_sym_id != ISOFORTRAN_COMPILER_OPTIONS
3015 0 : && sym->intmod_sym_id != ISOFORTRAN_COMPILER_VERSION)
3016 : return MATCH_NO;
3017 :
3018 2 : if (sym->from_intmod == INTMOD_ISO_C_BINDING
3019 2 : && sym->intmod_sym_id != ISOCBINDING_C_SIZEOF)
3020 : return MATCH_NO;
3021 : }
3022 : else
3023 : {
3024 2849 : name = sym->name;
3025 :
3026 2849 : functions = inquiry_func_gnu;
3027 2849 : if (gfc_option.warn_std & GFC_STD_F2003)
3028 0 : functions = inquiry_func_f2003;
3029 2849 : if (gfc_option.warn_std & GFC_STD_F95)
3030 0 : functions = inquiry_func_f95;
3031 :
3032 11918 : for (i = 0; functions[i]; i++)
3033 11913 : if (strcmp (functions[i], name) == 0)
3034 : break;
3035 :
3036 2849 : if (functions[i] == NULL)
3037 : return MATCH_ERROR;
3038 : }
3039 :
3040 : /* At this point we have an inquiry function with a variable argument. The
3041 : type of the variable might be undefined, but we need it now, because the
3042 : arguments of these functions are not allowed to be undefined. */
3043 :
3044 9143 : for (ap = e->value.function.actual; ap; ap = ap->next)
3045 : {
3046 6806 : if (!ap->expr)
3047 3339 : continue;
3048 :
3049 3467 : asym = ap->expr->symtree ? ap->expr->symtree->n.sym : NULL;
3050 :
3051 3467 : if (ap->expr->ts.type == BT_UNKNOWN)
3052 : {
3053 0 : if (asym && asym->ts.type == BT_UNKNOWN
3054 0 : && !gfc_set_default_type (asym, 0, gfc_current_ns))
3055 : return MATCH_NO;
3056 :
3057 0 : ap->expr->ts = asym->ts;
3058 : }
3059 :
3060 3467 : if (asym && asym->assoc && asym->assoc->target
3061 12 : && asym->assoc->target->expr_type == EXPR_CONSTANT)
3062 : {
3063 12 : gfc_free_expr (ap->expr);
3064 12 : ap->expr = gfc_copy_expr (asym->assoc->target);
3065 : }
3066 :
3067 : /* Assumed character length will not reduce to a constant expression
3068 : with LEN, as required by the standard. */
3069 3467 : if (i == 5 && not_restricted && asym
3070 411 : && asym->ts.type == BT_CHARACTER
3071 411 : && ((asym->ts.u.cl && asym->ts.u.cl->length == NULL)
3072 49 : || asym->ts.deferred))
3073 : {
3074 362 : gfc_error ("Assumed or deferred character length variable %qs "
3075 : "in constant expression at %L",
3076 362 : asym->name, &ap->expr->where);
3077 362 : return MATCH_ERROR;
3078 : }
3079 3105 : else if (not_restricted && !gfc_check_init_expr (ap->expr))
3080 : return MATCH_ERROR;
3081 :
3082 2963 : if (not_restricted == 0
3083 2943 : && ap->expr->expr_type != EXPR_VARIABLE
3084 3642 : && !check_restricted (ap->expr))
3085 : return MATCH_ERROR;
3086 :
3087 2961 : if (not_restricted == 0
3088 2941 : && ap->expr->expr_type == EXPR_VARIABLE
3089 2264 : && asym->attr.dummy && asym->attr.optional)
3090 : return MATCH_NO;
3091 : }
3092 :
3093 : return MATCH_YES;
3094 : }
3095 :
3096 :
3097 : /* F95, 7.1.6.1, Initialization expressions, (5)
3098 : F2003, 7.1.7 Initialization expression, (5) */
3099 :
3100 : static match
3101 594 : check_transformational (gfc_expr *e)
3102 : {
3103 594 : static const char * const trans_func_f95[] = {
3104 : "repeat", "reshape", "selected_int_kind",
3105 : "selected_real_kind", "transfer", "trim", NULL
3106 : };
3107 :
3108 594 : static const char * const trans_func_f2003[] = {
3109 : "all", "any", "count", "dot_product", "matmul", "null", "pack",
3110 : "product", "repeat", "reshape", "selected_char_kind", "selected_int_kind",
3111 : "selected_real_kind", "spread", "sum", "transfer", "transpose",
3112 : "trim", "unpack", NULL
3113 : };
3114 :
3115 594 : static const char * const trans_func_f2008[] = {
3116 : "all", "any", "count", "dot_product", "matmul", "null", "pack",
3117 : "product", "repeat", "reshape", "selected_char_kind", "selected_int_kind",
3118 : "selected_real_kind", "spread", "sum", "transfer", "transpose",
3119 : "trim", "unpack", "findloc", NULL
3120 : };
3121 :
3122 594 : static const char * const trans_func_f2023[] = {
3123 : "all", "any", "count", "dot_product", "matmul", "null", "pack",
3124 : "product", "repeat", "reshape", "selected_char_kind", "selected_int_kind",
3125 : "selected_logical_kind", "selected_real_kind", "spread", "sum", "transfer",
3126 : "transpose", "trim", "unpack", "findloc", NULL
3127 : };
3128 :
3129 594 : int i;
3130 594 : const char *name;
3131 594 : const char *const *functions;
3132 :
3133 594 : if (!e->value.function.isym
3134 594 : || !e->value.function.isym->transformational)
3135 : return MATCH_NO;
3136 :
3137 109 : name = e->symtree->n.sym->name;
3138 :
3139 109 : if (gfc_option.allow_std & GFC_STD_F2023)
3140 : functions = trans_func_f2023;
3141 0 : else if (gfc_option.allow_std & GFC_STD_F2008)
3142 : functions = trans_func_f2008;
3143 0 : else if (gfc_option.allow_std & GFC_STD_F2003)
3144 : functions = trans_func_f2003;
3145 : else
3146 0 : functions = trans_func_f95;
3147 :
3148 : /* NULL() is dealt with below. */
3149 109 : if (strcmp ("null", name) == 0)
3150 : return MATCH_NO;
3151 :
3152 1740 : for (i = 0; functions[i]; i++)
3153 1739 : if (strcmp (functions[i], name) == 0)
3154 : break;
3155 :
3156 109 : if (functions[i] == NULL)
3157 : {
3158 1 : gfc_error ("transformational intrinsic %qs at %L is not permitted "
3159 : "in an initialization expression", name, &e->where);
3160 1 : return MATCH_ERROR;
3161 : }
3162 :
3163 108 : return check_init_expr_arguments (e);
3164 : }
3165 :
3166 :
3167 : /* F95, 7.1.6.1, Initialization expressions, (6)
3168 : F2003, 7.1.7 Initialization expression, (6) */
3169 :
3170 : static match
3171 594 : check_null (gfc_expr *e)
3172 : {
3173 594 : if (strcmp ("null", e->symtree->n.sym->name) != 0)
3174 : return MATCH_NO;
3175 :
3176 0 : return check_init_expr_arguments (e);
3177 : }
3178 :
3179 :
3180 : static match
3181 485 : check_elemental (gfc_expr *e)
3182 : {
3183 485 : if (!e->value.function.isym
3184 485 : || !e->value.function.isym->elemental)
3185 : return MATCH_NO;
3186 :
3187 482 : if (e->ts.type != BT_INTEGER
3188 2 : && e->ts.type != BT_CHARACTER
3189 484 : && !gfc_notify_std (GFC_STD_F2003, "Evaluation of nonstandard "
3190 : "initialization expression at %L", &e->where))
3191 : return MATCH_ERROR;
3192 :
3193 482 : return check_init_expr_arguments (e);
3194 : }
3195 :
3196 :
3197 : static match
3198 1118 : check_conversion (gfc_expr *e)
3199 : {
3200 1118 : if (!e->value.function.isym
3201 1118 : || !e->value.function.isym->conversion)
3202 : return MATCH_NO;
3203 :
3204 3 : return check_init_expr_arguments (e);
3205 : }
3206 :
3207 :
3208 : /* Verify that an expression is an initialization expression. A side
3209 : effect is that the expression tree is reduced to a single constant
3210 : node if all goes well. This would normally happen when the
3211 : expression is constructed but function references are assumed to be
3212 : intrinsics in the context of initialization expressions. If
3213 : false is returned an error message has been generated. */
3214 :
3215 : bool
3216 475237 : gfc_check_init_expr (gfc_expr *e)
3217 : {
3218 475237 : match m;
3219 475237 : bool t;
3220 :
3221 475237 : if (e == NULL)
3222 : return true;
3223 :
3224 475196 : switch (e->expr_type)
3225 : {
3226 1005 : case EXPR_OP:
3227 1005 : t = check_intrinsic_op (e, gfc_check_init_expr);
3228 1005 : if (t)
3229 55 : t = gfc_simplify_expr (e, 0);
3230 :
3231 : break;
3232 :
3233 1 : case EXPR_CONDITIONAL:
3234 1 : t = gfc_check_init_expr (e->value.conditional.condition);
3235 1 : if (!t)
3236 : break;
3237 0 : t = gfc_check_init_expr (e->value.conditional.true_expr);
3238 0 : if (!t)
3239 : break;
3240 0 : t = gfc_check_init_expr (e->value.conditional.false_expr);
3241 0 : if (t)
3242 0 : t = gfc_simplify_expr (e, 0);
3243 : else
3244 : t = false;
3245 : break;
3246 :
3247 1676 : case EXPR_FUNCTION:
3248 1676 : t = false;
3249 :
3250 1676 : {
3251 1676 : bool conversion;
3252 1676 : gfc_intrinsic_sym* isym = NULL;
3253 1676 : gfc_symbol* sym = e->symtree->n.sym;
3254 :
3255 : /* Simplify here the intrinsics from the IEEE_ARITHMETIC and
3256 : IEEE_EXCEPTIONS modules. */
3257 1676 : int mod = sym->from_intmod;
3258 1676 : if (mod == INTMOD_NONE && sym->generic)
3259 192 : mod = sym->generic->sym->from_intmod;
3260 1676 : if (mod == INTMOD_IEEE_ARITHMETIC || mod == INTMOD_IEEE_EXCEPTIONS)
3261 : {
3262 453 : gfc_expr *new_expr = gfc_simplify_ieee_functions (e);
3263 453 : if (new_expr)
3264 : {
3265 327 : gfc_replace_expr (e, new_expr);
3266 327 : t = true;
3267 327 : break;
3268 : }
3269 : }
3270 :
3271 : /* If a conversion function, e.g., __convert_i8_i4, was inserted
3272 : into an array constructor, we need to skip the error check here.
3273 : Conversion errors are caught below in scalarize_intrinsic_call. */
3274 3813 : conversion = e->value.function.isym
3275 1349 : && (e->value.function.isym->conversion == 1);
3276 :
3277 1346 : if (!conversion && (!gfc_is_intrinsic (sym, 0, e->where)
3278 1131 : || (m = gfc_intrinsic_func_interface (e, 0)) == MATCH_NO))
3279 : {
3280 231 : gfc_error ("Function %qs in initialization expression at %L "
3281 : "must be an intrinsic function",
3282 231 : e->symtree->n.sym->name, &e->where);
3283 231 : break;
3284 : }
3285 :
3286 1118 : if ((m = check_conversion (e)) == MATCH_NO
3287 1115 : && (m = check_inquiry (e, 1)) == MATCH_NO
3288 594 : && (m = check_null (e)) == MATCH_NO
3289 594 : && (m = check_transformational (e)) == MATCH_NO
3290 1603 : && (m = check_elemental (e)) == MATCH_NO)
3291 : {
3292 3 : gfc_error ("Intrinsic function %qs at %L is not permitted "
3293 : "in an initialization expression",
3294 3 : e->symtree->n.sym->name, &e->where);
3295 3 : m = MATCH_ERROR;
3296 : }
3297 :
3298 1118 : if (m == MATCH_ERROR)
3299 : return false;
3300 :
3301 : /* Try to scalarize an elemental intrinsic function that has an
3302 : array argument. */
3303 296 : isym = gfc_find_function (e->symtree->n.sym->name);
3304 296 : if (isym && isym->elemental
3305 536 : && (t = scalarize_intrinsic_call (e, true)))
3306 : break;
3307 : }
3308 :
3309 296 : if (m == MATCH_YES)
3310 296 : t = gfc_simplify_expr (e, 0);
3311 :
3312 : break;
3313 :
3314 4790 : case EXPR_VARIABLE:
3315 4790 : t = true;
3316 :
3317 : /* This occurs when parsing pdt templates. */
3318 4790 : if (gfc_expr_attr (e).pdt_kind)
3319 : break;
3320 :
3321 4740 : if (gfc_check_iter_variable (e))
3322 : break;
3323 :
3324 4724 : if (e->symtree->n.sym->attr.flavor == FL_PARAMETER)
3325 : {
3326 : /* A PARAMETER shall not be used to define itself, i.e.
3327 : REAL, PARAMETER :: x = transfer(0, x)
3328 : is invalid. */
3329 418 : if (!e->symtree->n.sym->value)
3330 : {
3331 9 : gfc_error ("PARAMETER %qs is used at %L before its definition "
3332 : "is complete", e->symtree->n.sym->name, &e->where);
3333 9 : t = false;
3334 : }
3335 : else
3336 409 : t = simplify_parameter_variable (e, 0);
3337 :
3338 : break;
3339 : }
3340 :
3341 4306 : if (gfc_in_match_data ())
3342 : break;
3343 :
3344 4289 : t = false;
3345 :
3346 4289 : if (e->symtree->n.sym->as)
3347 : {
3348 154 : switch (e->symtree->n.sym->as->type)
3349 : {
3350 1 : case AS_ASSUMED_SIZE:
3351 1 : gfc_error ("Assumed size array %qs at %L is not permitted "
3352 : "in an initialization expression",
3353 : e->symtree->n.sym->name, &e->where);
3354 1 : break;
3355 :
3356 18 : case AS_ASSUMED_SHAPE:
3357 18 : gfc_error ("Assumed shape array %qs at %L is not permitted "
3358 : "in an initialization expression",
3359 : e->symtree->n.sym->name, &e->where);
3360 18 : break;
3361 :
3362 109 : case AS_DEFERRED:
3363 109 : if (!e->symtree->n.sym->attr.allocatable
3364 89 : && !e->symtree->n.sym->attr.pointer
3365 65 : && e->symtree->n.sym->attr.dummy)
3366 65 : gfc_error ("Assumed-shape array %qs at %L is not permitted "
3367 : "in an initialization expression",
3368 : e->symtree->n.sym->name, &e->where);
3369 : else
3370 44 : gfc_error ("Deferred array %qs at %L is not permitted "
3371 : "in an initialization expression",
3372 : e->symtree->n.sym->name, &e->where);
3373 : break;
3374 :
3375 20 : case AS_EXPLICIT:
3376 20 : gfc_error ("Array %qs at %L is a variable, which does "
3377 : "not reduce to a constant expression",
3378 : e->symtree->n.sym->name, &e->where);
3379 20 : break;
3380 :
3381 6 : case AS_ASSUMED_RANK:
3382 6 : gfc_error ("Assumed-rank array %qs at %L is not permitted "
3383 : "in an initialization expression",
3384 : e->symtree->n.sym->name, &e->where);
3385 6 : break;
3386 :
3387 0 : default:
3388 0 : gcc_unreachable();
3389 : }
3390 : }
3391 : else
3392 4135 : gfc_error ("Parameter %qs at %L has not been declared or is "
3393 : "a variable, which does not reduce to a constant "
3394 : "expression", e->symtree->name, &e->where);
3395 :
3396 : break;
3397 :
3398 : case EXPR_CONSTANT:
3399 : case EXPR_NULL:
3400 : t = true;
3401 : break;
3402 :
3403 11 : case EXPR_SUBSTRING:
3404 11 : if (e->ref)
3405 : {
3406 7 : t = gfc_check_init_expr (e->ref->u.ss.start);
3407 7 : if (!t)
3408 : break;
3409 :
3410 7 : t = gfc_check_init_expr (e->ref->u.ss.end);
3411 7 : if (t)
3412 7 : t = gfc_simplify_expr (e, 0);
3413 : }
3414 : else
3415 : t = false;
3416 : break;
3417 :
3418 2372 : case EXPR_STRUCTURE:
3419 2372 : t = e->ts.is_iso_c;
3420 2372 : if (t)
3421 : break;
3422 :
3423 2258 : t = check_alloc_comp_init (e);
3424 2258 : if (!t)
3425 : break;
3426 :
3427 2257 : t = gfc_check_constructor (e, gfc_check_init_expr);
3428 2257 : if (!t)
3429 : break;
3430 :
3431 2257 : break;
3432 :
3433 5109 : case EXPR_ARRAY:
3434 5109 : t = gfc_check_constructor (e, gfc_check_init_expr);
3435 5109 : if (!t)
3436 : break;
3437 :
3438 5090 : t = gfc_expand_constructor (e, true);
3439 5090 : if (!t)
3440 : break;
3441 :
3442 5069 : t = gfc_check_constructor_type (e);
3443 5069 : break;
3444 :
3445 0 : default:
3446 0 : gfc_internal_error ("check_init_expr(): Unknown expression type");
3447 : }
3448 :
3449 : return t;
3450 : }
3451 :
3452 : /* Reduces a general expression to an initialization expression (a constant).
3453 : This used to be part of gfc_match_init_expr.
3454 : Note that this function doesn't free the given expression on false. */
3455 :
3456 : bool
3457 114196 : gfc_reduce_init_expr (gfc_expr *expr)
3458 : {
3459 114196 : bool t;
3460 :
3461 : /* It is far too early to resolve a class compcall. Punt to resolution. */
3462 114196 : if (expr && expr->expr_type == EXPR_COMPCALL
3463 25 : && expr->symtree->n.sym->ts.type == BT_CLASS)
3464 : return false;
3465 :
3466 114171 : gfc_init_expr_flag = true;
3467 114171 : t = gfc_resolve_expr (expr);
3468 114171 : if (t)
3469 114030 : t = gfc_check_init_expr (expr);
3470 114171 : gfc_init_expr_flag = false;
3471 :
3472 114171 : if (!t || !expr)
3473 : return false;
3474 :
3475 109090 : if (expr->expr_type == EXPR_ARRAY)
3476 : {
3477 5322 : if (!gfc_check_constructor_type (expr))
3478 : return false;
3479 5322 : if (!gfc_expand_constructor (expr, true))
3480 : return false;
3481 : }
3482 :
3483 : return true;
3484 : }
3485 :
3486 :
3487 : /* Match an initialization expression. We work by first matching an
3488 : expression, then reducing it to a constant. */
3489 :
3490 : match
3491 98976 : gfc_match_init_expr (gfc_expr **result)
3492 : {
3493 98976 : gfc_expr *expr;
3494 98976 : match m;
3495 98976 : bool t;
3496 :
3497 98976 : expr = NULL;
3498 :
3499 98976 : gfc_init_expr_flag = true;
3500 :
3501 98976 : m = gfc_match_expr (&expr);
3502 98976 : if (m != MATCH_YES)
3503 : {
3504 115 : gfc_init_expr_flag = false;
3505 115 : return m;
3506 : }
3507 :
3508 98861 : if (expr->expr_type != EXPR_FUNCTION && gfc_derived_parameter_expr (expr))
3509 : {
3510 384 : *result = expr;
3511 384 : gfc_init_expr_flag = false;
3512 384 : return m;
3513 : }
3514 :
3515 98477 : t = gfc_reduce_init_expr (expr);
3516 98477 : if (!t)
3517 : {
3518 671 : gfc_free_expr (expr);
3519 671 : gfc_init_expr_flag = false;
3520 671 : return MATCH_ERROR;
3521 : }
3522 :
3523 97806 : *result = expr;
3524 97806 : gfc_init_expr_flag = false;
3525 :
3526 97806 : return MATCH_YES;
3527 : }
3528 :
3529 :
3530 : /* Given an actual argument list, test to see that each argument is a
3531 : restricted expression and optionally if the expression type is
3532 : integer or character. */
3533 :
3534 : static bool
3535 1345 : restricted_args (gfc_actual_arglist *a)
3536 : {
3537 3428 : for (; a; a = a->next)
3538 : {
3539 2084 : if (!check_restricted (a->expr))
3540 : return false;
3541 : }
3542 :
3543 : return true;
3544 : }
3545 :
3546 :
3547 : /************* Restricted/specification expressions *************/
3548 :
3549 :
3550 : /* Make sure a non-intrinsic function is a specification function,
3551 : * see F08:7.1.11.5. */
3552 :
3553 : static bool
3554 579 : external_spec_function (gfc_expr *e)
3555 : {
3556 579 : gfc_symbol *f;
3557 :
3558 579 : f = e->value.function.esym;
3559 :
3560 : /* IEEE functions allowed are "a reference to a transformational function
3561 : from the intrinsic module IEEE_ARITHMETIC or IEEE_EXCEPTIONS", and
3562 : "inquiry function from the intrinsic modules IEEE_ARITHMETIC and
3563 : IEEE_EXCEPTIONS". */
3564 579 : if (f->from_intmod == INTMOD_IEEE_ARITHMETIC
3565 579 : || f->from_intmod == INTMOD_IEEE_EXCEPTIONS)
3566 : {
3567 234 : if (!strcmp (f->name, "ieee_selected_real_kind")
3568 216 : || !strcmp (f->name, "ieee_support_rounding")
3569 216 : || !strcmp (f->name, "ieee_support_flag")
3570 216 : || !strcmp (f->name, "ieee_support_halting")
3571 216 : || !strcmp (f->name, "ieee_support_datatype")
3572 216 : || !strcmp (f->name, "ieee_support_denormal")
3573 216 : || !strcmp (f->name, "ieee_support_subnormal")
3574 216 : || !strcmp (f->name, "ieee_support_divide")
3575 216 : || !strcmp (f->name, "ieee_support_inf")
3576 216 : || !strcmp (f->name, "ieee_support_io")
3577 216 : || !strcmp (f->name, "ieee_support_nan")
3578 216 : || !strcmp (f->name, "ieee_support_sqrt")
3579 216 : || !strcmp (f->name, "ieee_support_standard")
3580 216 : || !strcmp (f->name, "ieee_support_underflow_control"))
3581 18 : goto function_allowed;
3582 : }
3583 :
3584 561 : if (f->attr.proc == PROC_ST_FUNCTION)
3585 : {
3586 0 : gfc_error ("Specification function %qs at %L cannot be a statement "
3587 : "function", f->name, &e->where);
3588 0 : return false;
3589 : }
3590 :
3591 561 : if (f->attr.proc == PROC_INTERNAL)
3592 : {
3593 0 : gfc_error ("Specification function %qs at %L cannot be an internal "
3594 : "function", f->name, &e->where);
3595 0 : return false;
3596 : }
3597 :
3598 561 : if (!f->attr.pure && !f->attr.elemental)
3599 : {
3600 2 : gfc_error ("Specification function %qs at %L must be PURE", f->name,
3601 : &e->where);
3602 2 : return false;
3603 : }
3604 :
3605 : /* F08:7.1.11.6. */
3606 559 : if (f->attr.recursive
3607 559 : && !gfc_notify_std (GFC_STD_F2003,
3608 : "Specification function %qs "
3609 : "at %L cannot be RECURSIVE", f->name, &e->where))
3610 : return false;
3611 :
3612 577 : function_allowed:
3613 577 : return restricted_args (e->value.function.actual);
3614 : }
3615 :
3616 :
3617 : /* Check to see that a function reference to an intrinsic is a
3618 : restricted expression. */
3619 :
3620 : static bool
3621 3089 : restricted_intrinsic (gfc_expr *e)
3622 : {
3623 : /* TODO: Check constraints on inquiry functions. 7.1.6.2 (7). */
3624 3089 : if (check_inquiry (e, 0) == MATCH_YES)
3625 : return true;
3626 :
3627 768 : return restricted_args (e->value.function.actual);
3628 : }
3629 :
3630 :
3631 : /* Check the expressions of an actual arglist. Used by check_restricted. */
3632 :
3633 : static bool
3634 1346 : check_arglist (gfc_actual_arglist* arg, bool (*checker) (gfc_expr*))
3635 : {
3636 3412 : for (; arg; arg = arg->next)
3637 2074 : if (!checker (arg->expr))
3638 : return false;
3639 :
3640 : return true;
3641 : }
3642 :
3643 :
3644 : /* Check the subscription expressions of a reference chain with a checking
3645 : function; used by check_restricted. */
3646 :
3647 : static bool
3648 17707 : check_references (gfc_ref* ref, bool (*checker) (gfc_expr*))
3649 : {
3650 18583 : int dim;
3651 :
3652 18583 : if (!ref)
3653 : return true;
3654 :
3655 879 : switch (ref->type)
3656 : {
3657 : case REF_ARRAY:
3658 1412 : for (dim = 0; dim < ref->u.ar.dimen; ++dim)
3659 : {
3660 713 : if (!checker (ref->u.ar.start[dim]))
3661 : return false;
3662 711 : if (!checker (ref->u.ar.end[dim]))
3663 : return false;
3664 711 : if (!checker (ref->u.ar.stride[dim]))
3665 : return false;
3666 : }
3667 : break;
3668 :
3669 : case REF_COMPONENT:
3670 : /* Nothing needed, just proceed to next reference. */
3671 : break;
3672 :
3673 13 : case REF_SUBSTRING:
3674 13 : if (!checker (ref->u.ss.start))
3675 : return false;
3676 12 : if (!checker (ref->u.ss.end))
3677 : return false;
3678 : break;
3679 :
3680 0 : default:
3681 0 : gcc_unreachable ();
3682 876 : break;
3683 : }
3684 :
3685 876 : return check_references (ref->next, checker);
3686 : }
3687 :
3688 : /* Return true if ns is a parent of the current ns. */
3689 :
3690 : static bool
3691 548 : is_parent_of_current_ns (gfc_namespace *ns)
3692 : {
3693 548 : gfc_namespace *p;
3694 576 : for (p = gfc_current_ns->parent; p; p = p->parent)
3695 561 : if (ns == p)
3696 : return true;
3697 :
3698 : return false;
3699 : }
3700 :
3701 : /* Verify that an expression is a restricted expression. Like its
3702 : cousin check_init_expr(), an error message is generated if we
3703 : return false. */
3704 :
3705 : static bool
3706 460680 : check_restricted (gfc_expr *e)
3707 : {
3708 460680 : gfc_symbol* sym;
3709 460680 : bool t;
3710 :
3711 460680 : if (e == NULL)
3712 : return true;
3713 :
3714 458113 : switch (e->expr_type)
3715 : {
3716 2767 : case EXPR_OP:
3717 2767 : t = check_intrinsic_op (e, check_restricted);
3718 2767 : if (t)
3719 2765 : t = gfc_simplify_expr (e, 0);
3720 :
3721 : break;
3722 :
3723 1 : case EXPR_CONDITIONAL:
3724 1 : t = check_restricted (e->value.conditional.condition);
3725 1 : if (!t)
3726 : break;
3727 1 : t = check_restricted (e->value.conditional.true_expr);
3728 1 : if (!t)
3729 : break;
3730 1 : t = check_restricted (e->value.conditional.false_expr);
3731 1 : if (t)
3732 1 : t = gfc_simplify_expr (e, 0);
3733 : else
3734 : t = false;
3735 : break;
3736 :
3737 3676 : case EXPR_FUNCTION:
3738 3676 : if (e->value.function.esym)
3739 : {
3740 579 : t = check_arglist (e->value.function.actual, &check_restricted);
3741 579 : if (t)
3742 579 : t = external_spec_function (e);
3743 : }
3744 : else
3745 : {
3746 3097 : if (e->value.function.isym && e->value.function.isym->inquiry)
3747 : t = true;
3748 : else
3749 767 : t = check_arglist (e->value.function.actual, &check_restricted);
3750 :
3751 767 : if (t)
3752 3089 : t = restricted_intrinsic (e);
3753 : }
3754 : break;
3755 :
3756 17713 : case EXPR_VARIABLE:
3757 17713 : sym = e->symtree->n.sym;
3758 17713 : t = false;
3759 :
3760 : /* If a dummy argument appears in a context that is valid for a
3761 : restricted expression in an elemental procedure, it will have
3762 : already been simplified away once we get here. Therefore we
3763 : don't need to jump through hoops to distinguish valid from
3764 : invalid cases. Allowed in F2008 and F2018. */
3765 17713 : if (gfc_notification_std (GFC_STD_F2008)
3766 49 : && sym->attr.dummy && sym->ns == gfc_current_ns
3767 17762 : && sym->ns->proc_name && sym->ns->proc_name->attr.elemental)
3768 : {
3769 4 : gfc_error_now ("Dummy argument %qs not "
3770 : "allowed in expression at %L",
3771 : sym->name, &e->where);
3772 4 : break;
3773 : }
3774 :
3775 17709 : if (sym->attr.optional)
3776 : {
3777 2 : gfc_error ("Dummy argument %qs at %L cannot be OPTIONAL",
3778 : sym->name, &e->where);
3779 2 : break;
3780 : }
3781 :
3782 17707 : if (sym->attr.intent == INTENT_OUT)
3783 : {
3784 0 : gfc_error ("Dummy argument %qs at %L cannot be INTENT(OUT)",
3785 : sym->name, &e->where);
3786 0 : break;
3787 : }
3788 :
3789 : /* Check reference chain if any. */
3790 17707 : if (!check_references (e->ref, &check_restricted))
3791 : break;
3792 :
3793 17704 : if (e->error
3794 17684 : || sym->attr.in_common
3795 17489 : || sym->attr.use_assoc
3796 12305 : || sym->attr.used_in_submodule
3797 12304 : || sym->attr.dummy
3798 606 : || sym->attr.implied_index
3799 606 : || sym->attr.flavor == FL_PARAMETER
3800 18800 : || is_parent_of_current_ns (gfc_get_spec_ns (sym)))
3801 : {
3802 : t = true;
3803 : break;
3804 : }
3805 :
3806 15 : gfc_error ("Variable %qs cannot appear in the expression at %L",
3807 : sym->name, &e->where);
3808 : /* Prevent a repetition of the error. */
3809 15 : e->error = 1;
3810 15 : break;
3811 :
3812 : case EXPR_NULL:
3813 : case EXPR_CONSTANT:
3814 : t = true;
3815 : break;
3816 :
3817 7 : case EXPR_SUBSTRING:
3818 7 : t = gfc_specification_expr (e->ref->u.ss.start);
3819 7 : if (!t)
3820 : break;
3821 :
3822 6 : t = gfc_specification_expr (e->ref->u.ss.end);
3823 6 : if (t)
3824 6 : t = gfc_simplify_expr (e, 0);
3825 :
3826 : break;
3827 :
3828 6 : case EXPR_STRUCTURE:
3829 6 : t = gfc_check_constructor (e, check_restricted);
3830 6 : break;
3831 :
3832 58 : case EXPR_ARRAY:
3833 58 : t = gfc_check_constructor (e, check_restricted);
3834 58 : break;
3835 :
3836 0 : default:
3837 0 : gfc_internal_error ("check_restricted(): Unknown expression type");
3838 : }
3839 :
3840 : return t;
3841 : }
3842 :
3843 :
3844 : /* Check to see that an expression is a specification expression. If
3845 : we return false, an error has been generated. */
3846 :
3847 : bool
3848 483612 : gfc_specification_expr (gfc_expr *e)
3849 : {
3850 483612 : gfc_component *comp;
3851 :
3852 483612 : if (e == NULL)
3853 : return true;
3854 :
3855 448417 : if (e->ts.type != BT_INTEGER)
3856 : {
3857 26 : gfc_error ("Expression at %L must be of INTEGER type, found %s",
3858 : &e->where, gfc_basic_typename (e->ts.type));
3859 26 : return false;
3860 : }
3861 :
3862 448391 : comp = gfc_get_proc_ptr_comp (e);
3863 448391 : if (e->expr_type == EXPR_FUNCTION
3864 2402 : && !e->value.function.isym
3865 392 : && !e->value.function.esym
3866 109 : && !gfc_pure (e->symtree->n.sym)
3867 448493 : && (!comp || !comp->attr.pure))
3868 : {
3869 3 : gfc_error ("Function %qs at %L must be PURE",
3870 3 : e->symtree->n.sym->name, &e->where);
3871 : /* Prevent repeat error messages. */
3872 3 : e->symtree->n.sym->attr.pure = 1;
3873 3 : return false;
3874 : }
3875 :
3876 448388 : if (e->rank != 0)
3877 : {
3878 3 : gfc_error ("Expression at %L must be scalar", &e->where);
3879 3 : return false;
3880 : }
3881 :
3882 448385 : if (!gfc_simplify_expr (e, 0))
3883 : return false;
3884 :
3885 448379 : return check_restricted (e);
3886 : }
3887 :
3888 :
3889 : /************** Expression conformance checks. *************/
3890 :
3891 : /* Given two expressions, make sure that the arrays are conformable. */
3892 :
3893 : bool
3894 198868 : gfc_check_conformance (gfc_expr *op1, gfc_expr *op2, const char *optype_msgid, ...)
3895 : {
3896 198868 : int op1_flag, op2_flag, d;
3897 198868 : mpz_t op1_size, op2_size;
3898 198868 : bool t;
3899 :
3900 198868 : va_list argp;
3901 198868 : char buffer[240];
3902 :
3903 198868 : if (op1->rank == 0 || op2->rank == 0)
3904 : return true;
3905 :
3906 72448 : va_start (argp, optype_msgid);
3907 72448 : d = vsnprintf (buffer, sizeof (buffer), optype_msgid, argp);
3908 72448 : va_end (argp);
3909 72448 : if (d < 1 || d >= (int) sizeof (buffer)) /* Reject truncation. */
3910 0 : gfc_internal_error ("optype_msgid overflow: %d", d);
3911 :
3912 72448 : if (op1->rank != op2->rank)
3913 : {
3914 34 : gfc_error ("Incompatible ranks in %s (%d and %d) at %L", _(buffer),
3915 : op1->rank, op2->rank, &op1->where);
3916 34 : return false;
3917 : }
3918 :
3919 : t = true;
3920 :
3921 174210 : for (d = 0; d < op1->rank; d++)
3922 : {
3923 101864 : op1_flag = gfc_array_dimen_size(op1, d, &op1_size);
3924 101864 : op2_flag = gfc_array_dimen_size(op2, d, &op2_size);
3925 :
3926 101864 : if (op1_flag && op2_flag && mpz_cmp (op1_size, op2_size) != 0)
3927 : {
3928 68 : gfc_error ("Different shape for %s at %L on dimension %d "
3929 : "(%d and %d)", _(buffer), &op1->where, d + 1,
3930 68 : (int) mpz_get_si (op1_size),
3931 68 : (int) mpz_get_si (op2_size));
3932 :
3933 68 : t = false;
3934 : }
3935 :
3936 101864 : if (op1_flag)
3937 67138 : mpz_clear (op1_size);
3938 101864 : if (op2_flag)
3939 77204 : mpz_clear (op2_size);
3940 :
3941 101864 : if (!t)
3942 : return false;
3943 : }
3944 :
3945 : return true;
3946 : }
3947 :
3948 :
3949 : /* Functions to check constant valued type specification parameters. */
3950 :
3951 : static gfc_actual_arglist *
3952 4358 : get_parm_list_from_expr (gfc_expr *expr)
3953 : {
3954 4358 : gfc_actual_arglist *a = NULL;
3955 4358 : gfc_constructor *c;
3956 :
3957 4358 : if (expr->expr_type == EXPR_STRUCTURE)
3958 1908 : a = expr->param_list;
3959 2450 : else if (expr->expr_type == EXPR_ARRAY)
3960 : {
3961 : /* Take the first constant expression, if there is one. */
3962 34 : c = gfc_constructor_first (expr->value.constructor);
3963 70 : for (; c; c = gfc_constructor_next (c))
3964 35 : if (!c->iterator && c->expr && c->expr->param_list)
3965 : {
3966 : a = c->expr->param_list;
3967 : break;
3968 : }
3969 : }
3970 2416 : else if (expr->expr_type == EXPR_VARIABLE)
3971 2183 : a = expr->symtree->n.sym->param_list;
3972 :
3973 4358 : return a;
3974 : }
3975 :
3976 : bool
3977 2179 : gfc_check_type_spec_parms (gfc_expr *expr1, gfc_expr *expr2,
3978 : const char *context)
3979 : {
3980 2179 : bool t = true;
3981 2179 : gfc_actual_arglist *a1, *a2;
3982 :
3983 2179 : gcc_assert (expr1->ts.type == BT_DERIVED
3984 : && expr1->ts.u.derived->attr.pdt_type);
3985 :
3986 2179 : a1 = get_parm_list_from_expr (expr1);
3987 2179 : a2 = get_parm_list_from_expr (expr2);
3988 :
3989 4795 : for (; a1 && a2; a1 = a1->next, a2 = a2->next)
3990 : {
3991 437 : if (a1->expr && a1->expr->expr_type == EXPR_CONSTANT
3992 413 : && a2->expr && a2->expr->expr_type == EXPR_CONSTANT
3993 400 : && !strcmp (a1->name, a2->name)
3994 376 : && mpz_cmp (a1->expr->value.integer, a2->expr->value.integer))
3995 : {
3996 30 : gfc_error ("Mismatched type parameters %qs(%d/%d) %s at %L/%L",
3997 : a2->name,
3998 20 : (int)mpz_get_ui (a1->expr->value.integer),
3999 12 : (int)mpz_get_ui (a2->expr->value.integer),
4000 : context,
4001 : &expr1->where, &expr2->where);
4002 10 : t = false;
4003 : }
4004 : }
4005 :
4006 2179 : return t;
4007 : }
4008 :
4009 :
4010 : /* Used by the backend to decide when and where a hidden PDT should be
4011 : passed to a PDT dummy. */
4012 :
4013 512022 : bool gfc_pdt_requires_hidden_arg (gfc_symbol *arg)
4014 : {
4015 512022 : if (IS_PDT (arg)
4016 3260 : && (arg->attr.allocatable || arg->attr.pointer))
4017 : return true;
4018 :
4019 479598 : if (IS_CLASS_PDT (arg)
4020 594 : && (CLASS_DATA (arg)->attr.allocatable
4021 462 : || CLASS_DATA (arg)->attr.class_pointer))
4022 132 : return true;
4023 :
4024 : return false;
4025 : }
4026 :
4027 :
4028 : /* Given an assignable expression and an arbitrary expression, make
4029 : sure that the assignment can take place. Only add a call to the intrinsic
4030 : conversion routines, when allow_convert is set. When this assign is a
4031 : coarray call, then the convert is done by the coarray routine implicitly and
4032 : adding the intrinsic conversion would do harm in most cases. */
4033 :
4034 : bool
4035 826595 : gfc_check_assign (gfc_expr *lvalue, gfc_expr *rvalue, int conform,
4036 : bool allow_convert)
4037 : {
4038 826595 : gfc_symbol *sym;
4039 826595 : gfc_ref *ref;
4040 826595 : int has_pointer;
4041 :
4042 826595 : sym = lvalue->symtree->n.sym;
4043 :
4044 : /* See if this is the component or subcomponent of a pointer and guard
4045 : against assignment to LEN or KIND part-refs. */
4046 826595 : has_pointer = sym->attr.pointer;
4047 968652 : for (ref = lvalue->ref; ref; ref = ref->next)
4048 : {
4049 142057 : if (!has_pointer && ref->type == REF_COMPONENT
4050 46889 : && ref->u.c.component->attr.pointer)
4051 : has_pointer = 1;
4052 141002 : else if (ref->type == REF_INQUIRY
4053 92 : && (ref->u.i == INQUIRY_LEN || ref->u.i == INQUIRY_KIND))
4054 : {
4055 0 : gfc_error ("Assignment to a LEN or KIND part_ref at %L is not "
4056 : "allowed", &lvalue->where);
4057 0 : return false;
4058 : }
4059 : }
4060 :
4061 : /* 12.5.2.2, Note 12.26: The result variable is very similar to any other
4062 : variable local to a function subprogram. Its existence begins when
4063 : execution of the function is initiated and ends when execution of the
4064 : function is terminated...
4065 : Therefore, the left hand side is no longer a variable, when it is: */
4066 826595 : if (sym->attr.flavor == FL_PROCEDURE && sym->attr.proc != PROC_ST_FUNCTION
4067 8769 : && !sym->attr.external)
4068 : {
4069 8759 : bool bad_proc;
4070 8759 : bad_proc = false;
4071 :
4072 : /* (i) Use associated; */
4073 8759 : if (sym->attr.use_assoc)
4074 0 : bad_proc = true;
4075 :
4076 : /* (ii) The assignment is in the main program; or */
4077 8759 : if (gfc_current_ns->proc_name
4078 8758 : && gfc_current_ns->proc_name->attr.is_main_program)
4079 8759 : bad_proc = true;
4080 :
4081 : /* (iii) A module or internal procedure... */
4082 8759 : if (gfc_current_ns->proc_name
4083 8758 : && (gfc_current_ns->proc_name->attr.proc == PROC_INTERNAL
4084 5024 : || gfc_current_ns->proc_name->attr.proc == PROC_MODULE)
4085 6216 : && gfc_current_ns->parent
4086 5673 : && (!(gfc_current_ns->parent->proc_name->attr.function
4087 5520 : || gfc_current_ns->parent->proc_name->attr.subroutine)
4088 2948 : || gfc_current_ns->parent->proc_name->attr.is_main_program))
4089 : {
4090 : /* ... that is not a function... */
4091 5203 : if (gfc_current_ns->proc_name
4092 5203 : && !gfc_current_ns->proc_name->attr.function)
4093 0 : bad_proc = true;
4094 :
4095 : /* ... or is not an entry and has a different name. */
4096 5203 : if (!sym->attr.entry && sym->name != gfc_current_ns->proc_name->name)
4097 8759 : bad_proc = true;
4098 : }
4099 :
4100 : /* (iv) Host associated and not the function symbol or the
4101 : parent result. This picks up sibling references, which
4102 : cannot be entries. */
4103 8759 : if (!sym->attr.entry
4104 7957 : && sym->ns == gfc_current_ns->parent
4105 5428 : && sym != gfc_current_ns->proc_name
4106 72 : && sym != gfc_current_ns->parent->proc_name->result)
4107 : bad_proc = true;
4108 :
4109 8758 : if (bad_proc)
4110 : {
4111 1 : gfc_error ("%qs at %L is not a VALUE", sym->name, &lvalue->where);
4112 1 : return false;
4113 : }
4114 : }
4115 : else
4116 : {
4117 : /* Reject assigning to an external symbol. For initializers, this
4118 : was already done before, in resolve_fl_procedure. */
4119 817836 : if (sym->attr.flavor == FL_PROCEDURE && sym->attr.external
4120 10 : && sym->attr.proc != PROC_MODULE && !rvalue->error)
4121 : {
4122 2 : gfc_error ("Illegal assignment to external procedure at %L",
4123 : &lvalue->where);
4124 2 : return false;
4125 : }
4126 : }
4127 :
4128 826592 : if (rvalue->rank != 0 && lvalue->rank != rvalue->rank)
4129 : {
4130 25 : gfc_error ("Incompatible ranks %d and %d in assignment at %L",
4131 : lvalue->rank, rvalue->rank, &lvalue->where);
4132 25 : return false;
4133 : }
4134 :
4135 826567 : if (lvalue->ts.type == BT_UNKNOWN)
4136 : {
4137 0 : gfc_error ("Variable type is UNKNOWN in assignment at %L",
4138 : &lvalue->where);
4139 0 : return false;
4140 : }
4141 :
4142 826567 : if (rvalue->expr_type == EXPR_NULL)
4143 : {
4144 22 : if (has_pointer && (ref == NULL || ref->next == NULL)
4145 8 : && lvalue->symtree->n.sym->attr.data)
4146 : return true;
4147 : /* Prevent the following error message for caf-single mode, because there
4148 : are no teams in single mode and the simplify returns a null then. */
4149 15 : else if (!(flag_coarray == GFC_FCOARRAY_SINGLE
4150 12 : && rvalue->ts.type == BT_DERIVED
4151 12 : && rvalue->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
4152 12 : && rvalue->ts.u.derived->intmod_sym_id
4153 : == ISOFORTRAN_TEAM_TYPE))
4154 : {
4155 3 : gfc_error ("NULL appears on right-hand side in assignment at %L",
4156 : &rvalue->where);
4157 3 : return false;
4158 : }
4159 : }
4160 :
4161 : /* This is possibly a typo: x = f() instead of x => f(). */
4162 826557 : if (warn_surprising
4163 826557 : && rvalue->expr_type == EXPR_FUNCTION && gfc_expr_attr (rvalue).pointer)
4164 6 : gfc_warning (OPT_Wsurprising,
4165 : "POINTER-valued function appears on right-hand side of "
4166 : "assignment at %L", &rvalue->where);
4167 :
4168 : /* Check size of array assignments. */
4169 81116 : if (lvalue->rank != 0 && rvalue->rank != 0
4170 879480 : && !gfc_check_conformance (lvalue, rvalue, _("array assignment")))
4171 : return false;
4172 :
4173 : /* Handle the case of a BOZ literal on the RHS. */
4174 826525 : if (rvalue->ts.type == BT_BOZ)
4175 : {
4176 241 : if (lvalue->symtree->n.sym->attr.data)
4177 : {
4178 93 : if (lvalue->ts.type == BT_INTEGER
4179 93 : && gfc_boz2int (rvalue, lvalue->ts.kind))
4180 : return true;
4181 :
4182 2 : if (lvalue->ts.type == BT_REAL
4183 2 : && gfc_boz2real (rvalue, lvalue->ts.kind))
4184 : {
4185 2 : if (gfc_invalid_boz ("BOZ literal constant near %L cannot "
4186 : "be assigned to a REAL variable",
4187 : &rvalue->where))
4188 : return false;
4189 1 : return true;
4190 : }
4191 : }
4192 :
4193 148 : if (!lvalue->symtree->n.sym->attr.data
4194 148 : && gfc_invalid_boz ("BOZ literal constant at %L is neither a "
4195 : "data-stmt-constant nor an actual argument to "
4196 : "INT, REAL, DBLE, or CMPLX intrinsic function",
4197 : &rvalue->where))
4198 : return false;
4199 :
4200 148 : if (lvalue->ts.type == BT_INTEGER
4201 148 : && gfc_boz2int (rvalue, lvalue->ts.kind))
4202 : return true;
4203 :
4204 1 : if (lvalue->ts.type == BT_REAL
4205 1 : && gfc_boz2real (rvalue, lvalue->ts.kind))
4206 : return true;
4207 :
4208 0 : gfc_error ("BOZ literal constant near %L cannot be assigned to a "
4209 : "%qs variable", &rvalue->where, gfc_typename (lvalue));
4210 0 : return false;
4211 : }
4212 :
4213 826284 : if (gfc_expr_attr (lvalue).pdt_kind || gfc_expr_attr (lvalue).pdt_len)
4214 : {
4215 3 : gfc_error ("The assignment to a KIND or LEN component of a "
4216 : "parameterized type at %L is not allowed",
4217 : &lvalue->where);
4218 3 : return false;
4219 : }
4220 :
4221 :
4222 : /* Check that the type spec. parameters are the same on both sides. */
4223 59212 : if (lvalue->ts.type == BT_DERIVED && lvalue->ts.u.derived->attr.pdt_type
4224 828339 : && !gfc_check_type_spec_parms (lvalue, rvalue, "in assignment"))
4225 : return false;
4226 :
4227 826278 : if (gfc_compare_types (&lvalue->ts, &rvalue->ts))
4228 : return true;
4229 :
4230 : /* Only DATA Statements come here. */
4231 19602 : if (!conform)
4232 : {
4233 1524 : locus *where;
4234 :
4235 : /* Numeric can be converted to any other numeric. And Hollerith can be
4236 : converted to any other type. */
4237 2817 : if ((gfc_numeric_ts (&lvalue->ts) && gfc_numeric_ts (&rvalue->ts))
4238 2127 : || rvalue->ts.type == BT_HOLLERITH)
4239 : return true;
4240 :
4241 364 : if (flag_dec_char_conversions && (gfc_numeric_ts (&lvalue->ts)
4242 91 : || lvalue->ts.type == BT_LOGICAL)
4243 364 : && rvalue->ts.type == BT_CHARACTER
4244 743 : && rvalue->ts.kind == gfc_default_character_kind)
4245 : return true;
4246 :
4247 19 : if (lvalue->ts.type == BT_LOGICAL && rvalue->ts.type == BT_LOGICAL)
4248 : return true;
4249 :
4250 18 : where = (GFC_LOCUS_IS_SET (lvalue->where)
4251 18 : ? &lvalue->where : &rvalue->where);
4252 18 : gfc_error ("Incompatible types in DATA statement at %L; attempted "
4253 : "conversion of %s to %s", where,
4254 : gfc_typename (rvalue), gfc_typename (lvalue));
4255 :
4256 18 : return false;
4257 : }
4258 :
4259 : /* Assignment is the only case where character variables of different
4260 : kind values can be converted into one another. */
4261 18078 : if (lvalue->ts.type == BT_CHARACTER && rvalue->ts.type == BT_CHARACTER)
4262 : {
4263 394 : if (lvalue->ts.kind != rvalue->ts.kind && allow_convert)
4264 394 : return gfc_convert_chartype (rvalue, &lvalue->ts);
4265 : else
4266 : return true;
4267 : }
4268 :
4269 17684 : if (!allow_convert)
4270 : return true;
4271 :
4272 17684 : return gfc_convert_type (rvalue, &lvalue->ts, 1);
4273 : }
4274 :
4275 :
4276 : /* Check that a pointer assignment is OK. We first check lvalue, and
4277 : we only check rvalue if it's not an assignment to NULL() or a
4278 : NULLIFY statement. */
4279 :
4280 : bool
4281 17006 : gfc_check_pointer_assign (gfc_expr *lvalue, gfc_expr *rvalue,
4282 : bool suppress_type_test, bool is_init_expr)
4283 : {
4284 17006 : symbol_attribute attr, lhs_attr;
4285 17006 : gfc_ref *ref;
4286 17006 : bool is_pure, is_implicit_pure, rank_remap;
4287 17006 : int proc_pointer;
4288 17006 : bool same_rank;
4289 :
4290 17006 : if (!lvalue->symtree)
4291 : return false;
4292 :
4293 17005 : lhs_attr = gfc_expr_attr (lvalue);
4294 17005 : if (lvalue->ts.type == BT_UNKNOWN && !lhs_attr.proc_pointer)
4295 : {
4296 0 : gfc_error ("Pointer assignment target is not a POINTER at %L",
4297 : &lvalue->where);
4298 0 : return false;
4299 : }
4300 :
4301 17005 : if (lhs_attr.flavor == FL_PROCEDURE && lhs_attr.use_assoc
4302 36 : && !lhs_attr.proc_pointer)
4303 : {
4304 0 : gfc_error ("%qs in the pointer assignment at %L cannot be an "
4305 : "l-value since it is a procedure",
4306 0 : lvalue->symtree->n.sym->name, &lvalue->where);
4307 0 : return false;
4308 : }
4309 :
4310 17005 : proc_pointer = lvalue->symtree->n.sym->attr.proc_pointer;
4311 :
4312 17005 : rank_remap = false;
4313 17005 : same_rank = lvalue->rank == rvalue->rank;
4314 24523 : for (ref = lvalue->ref; ref; ref = ref->next)
4315 : {
4316 11808 : if (ref->type == REF_COMPONENT)
4317 6673 : proc_pointer = ref->u.c.component->attr.proc_pointer;
4318 :
4319 11808 : if (ref->type == REF_ARRAY && ref->next == NULL)
4320 : {
4321 4663 : int dim;
4322 :
4323 4663 : if (ref->u.ar.type == AR_FULL)
4324 : break;
4325 :
4326 384 : if (ref->u.ar.type != AR_SECTION)
4327 : {
4328 2 : gfc_error ("Expected bounds specification for %qs at %L",
4329 2 : lvalue->symtree->n.sym->name, &lvalue->where);
4330 2 : return false;
4331 : }
4332 :
4333 382 : if (!gfc_notify_std (GFC_STD_F2003, "Bounds specification "
4334 : "for %qs in pointer assignment at %L",
4335 382 : lvalue->symtree->n.sym->name, &lvalue->where))
4336 : return false;
4337 :
4338 : /* Fortran standard (e.g. F2018, 10.2.2 Pointer assignment):
4339 : *
4340 : * (C1017) If bounds-spec-list is specified, the number of
4341 : * bounds-specs shall equal the rank of data-pointer-object.
4342 : *
4343 : * If bounds-spec-list appears, it specifies the lower bounds.
4344 : *
4345 : * (C1018) If bounds-remapping-list is specified, the number of
4346 : * bounds-remappings shall equal the rank of data-pointer-object.
4347 : *
4348 : * If bounds-remapping-list appears, it specifies the upper and
4349 : * lower bounds of each dimension of the pointer; the pointer target
4350 : * shall be simply contiguous or of rank one.
4351 : *
4352 : * (C1019) If bounds-remapping-list is not specified, the ranks of
4353 : * data-pointer-object and data-target shall be the same.
4354 : *
4355 : * Thus when bounds are given, all lbounds are necessary and either
4356 : * all or none of the upper bounds; no strides are allowed. If the
4357 : * upper bounds are present, we may do rank remapping. */
4358 980 : for (dim = 0; dim < ref->u.ar.dimen; ++dim)
4359 : {
4360 607 : if (ref->u.ar.stride[dim])
4361 : {
4362 1 : gfc_error ("Stride must not be present at %L",
4363 : &lvalue->where);
4364 1 : return false;
4365 : }
4366 606 : if (!same_rank && (!ref->u.ar.start[dim] ||!ref->u.ar.end[dim]))
4367 : {
4368 3 : gfc_error ("Rank remapping requires a "
4369 : "list of %<lower-bound : upper-bound%> "
4370 : "specifications at %L", &lvalue->where);
4371 3 : return false;
4372 : }
4373 603 : if (!ref->u.ar.start[dim]
4374 602 : || ref->u.ar.dimen_type[dim] != DIMEN_RANGE)
4375 : {
4376 2 : gfc_error ("Expected list of %<lower-bound :%> or "
4377 : "list of %<lower-bound : upper-bound%> "
4378 : "specifications at %L", &lvalue->where);
4379 2 : return false;
4380 : }
4381 :
4382 601 : if (dim == 0)
4383 374 : rank_remap = (ref->u.ar.end[dim] != NULL);
4384 : else
4385 : {
4386 227 : if ((rank_remap && !ref->u.ar.end[dim]))
4387 : {
4388 0 : gfc_error ("Rank remapping requires a "
4389 : "list of %<lower-bound : upper-bound%> "
4390 : "specifications at %L", &lvalue->where);
4391 0 : return false;
4392 : }
4393 102 : if (!rank_remap && ref->u.ar.end[dim])
4394 : {
4395 0 : gfc_error ("Expected list of %<lower-bound :%> or "
4396 : "list of %<lower-bound : upper-bound%> "
4397 : "specifications at %L", &lvalue->where);
4398 0 : return false;
4399 : }
4400 : }
4401 : }
4402 : }
4403 : }
4404 :
4405 16994 : is_pure = gfc_pure (NULL);
4406 16994 : is_implicit_pure = gfc_implicit_pure (NULL);
4407 :
4408 : /* If rvalue is a NULL() or NULLIFY, we're done. Otherwise the type,
4409 : kind, etc for lvalue and rvalue must match, and rvalue must be a
4410 : pure variable if we're in a pure function. */
4411 16994 : if (rvalue->expr_type == EXPR_NULL && rvalue->ts.type == BT_UNKNOWN)
4412 : return true;
4413 :
4414 : /* F2008, C723 (pointer) and C726 (proc-pointer); for PURE also C1283. */
4415 9386 : if (lvalue->expr_type == EXPR_VARIABLE
4416 9386 : && gfc_is_coindexed (lvalue))
4417 : {
4418 5 : gfc_ref *ref;
4419 6 : for (ref = lvalue->ref; ref; ref = ref->next)
4420 6 : if (ref->type == REF_ARRAY && ref->u.ar.codimen)
4421 : {
4422 5 : gfc_error ("Pointer object at %L shall not have a coindex",
4423 : &lvalue->where);
4424 5 : return false;
4425 : }
4426 : }
4427 :
4428 : /* Checks on rvalue for procedure pointer assignments. */
4429 9381 : if (proc_pointer)
4430 : {
4431 1312 : char err[200];
4432 1312 : gfc_symbol *s1,*s2;
4433 1312 : gfc_component *comp1, *comp2;
4434 1312 : const char *name;
4435 :
4436 1312 : attr = gfc_expr_attr (rvalue);
4437 :
4438 1312 : if (rvalue->expr_type == EXPR_VARIABLE && !attr.proc_pointer)
4439 : {
4440 : /* Check for intrinsics. */
4441 1056 : gfc_symbol *sym = rvalue->symtree->n.sym;
4442 1056 : if (!sym->attr.intrinsic
4443 1056 : && (gfc_is_intrinsic (sym, 0, sym->declared_at)
4444 915 : || gfc_is_intrinsic (sym, 1, sym->declared_at)))
4445 : {
4446 37 : sym->attr.intrinsic = 1;
4447 37 : gfc_resolve_intrinsic (sym, &rvalue->where);
4448 37 : attr = gfc_expr_attr (rvalue);
4449 : }
4450 : /* Check for result of embracing function. */
4451 1056 : if (sym->attr.function && sym->result == sym)
4452 : {
4453 392 : gfc_namespace *ns;
4454 :
4455 863 : for (ns = gfc_current_ns; ns; ns = ns->parent)
4456 475 : if (sym == ns->proc_name)
4457 : {
4458 4 : gfc_error ("Function result %qs is invalid as proc-target "
4459 : "in procedure pointer assignment at %L",
4460 : sym->name, &rvalue->where);
4461 4 : return false;
4462 : }
4463 : }
4464 : }
4465 :
4466 2362 : if (!((rvalue->expr_type == EXPR_NULL)
4467 1289 : || (rvalue->expr_type == EXPR_FUNCTION && attr.proc_pointer)
4468 1168 : || (rvalue->expr_type == EXPR_VARIABLE && attr.proc_pointer)
4469 : || (rvalue->expr_type == EXPR_VARIABLE
4470 1052 : && attr.flavor == FL_PROCEDURE)))
4471 : {
4472 6 : gfc_error ("Invalid procedure pointer assignment at %L",
4473 : &rvalue->where);
4474 6 : return false;
4475 : }
4476 :
4477 1302 : if (attr.abstract)
4478 : {
4479 1 : gfc_error ("Abstract interface %qs is invalid "
4480 : "in procedure pointer assignment at %L",
4481 1 : rvalue->symtree->name, &rvalue->where);
4482 1 : return false;
4483 : }
4484 : /* Check for F08:C729. */
4485 1301 : if (attr.flavor == FL_PROCEDURE)
4486 : {
4487 1282 : if (attr.proc == PROC_ST_FUNCTION)
4488 : {
4489 1 : gfc_error ("Statement function %qs is invalid "
4490 : "in procedure pointer assignment at %L",
4491 1 : rvalue->symtree->name, &rvalue->where);
4492 1 : return false;
4493 : }
4494 1631 : if (attr.proc == PROC_INTERNAL &&
4495 350 : !gfc_notify_std(GFC_STD_F2008, "Internal procedure %qs "
4496 : "is invalid in procedure pointer assignment "
4497 350 : "at %L", rvalue->symtree->name, &rvalue->where))
4498 : return false;
4499 1421 : if (attr.intrinsic && gfc_intrinsic_actual_ok (rvalue->symtree->name,
4500 141 : attr.subroutine) == 0)
4501 : {
4502 1 : gfc_error ("Intrinsic %qs at %L is invalid in procedure pointer "
4503 1 : "assignment", rvalue->symtree->name, &rvalue->where);
4504 1 : return false;
4505 : }
4506 : }
4507 : /* Check for F08:C730. */
4508 1298 : if (attr.elemental && !attr.intrinsic)
4509 : {
4510 1 : gfc_error ("Nonintrinsic elemental procedure %qs is invalid "
4511 : "in procedure pointer assignment at %L",
4512 1 : rvalue->symtree->name, &rvalue->where);
4513 1 : return false;
4514 : }
4515 :
4516 : /* Ensure that the calling convention is the same. As other attributes
4517 : such as DLLEXPORT may differ, one explicitly only tests for the
4518 : calling conventions. */
4519 1297 : if (rvalue->expr_type == EXPR_VARIABLE
4520 1157 : && lvalue->symtree->n.sym->attr.ext_attr
4521 1157 : != rvalue->symtree->n.sym->attr.ext_attr)
4522 : {
4523 10 : symbol_attribute calls;
4524 :
4525 10 : calls.ext_attr = 0;
4526 10 : gfc_add_ext_attribute (&calls, EXT_ATTR_CDECL, NULL);
4527 10 : gfc_add_ext_attribute (&calls, EXT_ATTR_STDCALL, NULL);
4528 10 : gfc_add_ext_attribute (&calls, EXT_ATTR_FASTCALL, NULL);
4529 :
4530 10 : if ((calls.ext_attr & lvalue->symtree->n.sym->attr.ext_attr)
4531 10 : != (calls.ext_attr & rvalue->symtree->n.sym->attr.ext_attr))
4532 : {
4533 10 : gfc_error ("Mismatch in the procedure pointer assignment "
4534 : "at %L: mismatch in the calling convention",
4535 : &rvalue->where);
4536 10 : return false;
4537 : }
4538 : }
4539 :
4540 1287 : comp1 = gfc_get_proc_ptr_comp (lvalue);
4541 1287 : if (comp1)
4542 427 : s1 = comp1->ts.interface;
4543 : else
4544 : {
4545 860 : s1 = lvalue->symtree->n.sym;
4546 860 : if (s1->ts.interface)
4547 655 : s1 = s1->ts.interface;
4548 : }
4549 :
4550 1287 : comp2 = gfc_get_proc_ptr_comp (rvalue);
4551 1287 : if (comp2)
4552 : {
4553 67 : if (rvalue->expr_type == EXPR_FUNCTION)
4554 : {
4555 6 : s2 = comp2->ts.interface->result;
4556 6 : name = s2->name;
4557 : }
4558 : else
4559 : {
4560 61 : s2 = comp2->ts.interface;
4561 61 : name = comp2->name;
4562 : }
4563 : }
4564 1220 : else if (rvalue->expr_type == EXPR_FUNCTION)
4565 : {
4566 115 : if (rvalue->value.function.esym)
4567 115 : s2 = rvalue->value.function.esym->result;
4568 : else
4569 0 : s2 = rvalue->symtree->n.sym->result;
4570 :
4571 115 : name = s2->name;
4572 : }
4573 1105 : else if (rvalue->expr_type == EXPR_NULL && !rvalue->symtree)
4574 : return true;
4575 : else
4576 : {
4577 1093 : s2 = rvalue->symtree->n.sym;
4578 1093 : name = s2->name;
4579 : }
4580 :
4581 1275 : if (s2 && s2->attr.proc_pointer && s2->ts.interface)
4582 1275 : s2 = s2->ts.interface;
4583 :
4584 : /* Special check for the case of absent interface on the lvalue.
4585 : * All other interface checks are done below. */
4586 1275 : if (!s1 && comp1 && comp1->attr.subroutine && s2 && s2->attr.function)
4587 : {
4588 1 : gfc_error ("Interface mismatch in procedure pointer assignment "
4589 : "at %L: %qs is not a subroutine", &rvalue->where, name);
4590 1 : return false;
4591 : }
4592 :
4593 : /* F08:7.2.2.4 (4) */
4594 1272 : if (s2 && gfc_explicit_interface_required (s2, err, sizeof(err)))
4595 : {
4596 275 : if (comp1 && !s1)
4597 : {
4598 2 : gfc_error ("Explicit interface required for component %qs at %L: %s",
4599 : comp1->name, &lvalue->where, err);
4600 2 : return false;
4601 : }
4602 273 : else if (s1->attr.if_source == IFSRC_UNKNOWN)
4603 : {
4604 2 : gfc_error ("Explicit interface required for %qs at %L: %s",
4605 : s1->name, &lvalue->where, err);
4606 2 : return false;
4607 : }
4608 : }
4609 1270 : if (s1 && gfc_explicit_interface_required (s1, err, sizeof(err)))
4610 : {
4611 287 : if (comp2 && !s2)
4612 : {
4613 2 : gfc_error ("Explicit interface required for component %qs at %L: %s",
4614 : comp2->name, &rvalue->where, err);
4615 2 : return false;
4616 : }
4617 285 : else if (s2->attr.if_source == IFSRC_UNKNOWN)
4618 : {
4619 2 : gfc_error ("Explicit interface required for %qs at %L: %s",
4620 : s2->name, &rvalue->where, err);
4621 2 : return false;
4622 : }
4623 : }
4624 :
4625 1266 : if (s1 == s2 || !s1 || !s2)
4626 : return true;
4627 :
4628 768 : if (!gfc_compare_interfaces (s1, s2, name, 0, 1,
4629 : err, sizeof(err), NULL, NULL))
4630 : {
4631 24 : gfc_error ("Interface mismatch in procedure pointer assignment "
4632 : "at %L: %s", &rvalue->where, err);
4633 24 : return false;
4634 : }
4635 :
4636 : /* Check F2008Cor2, C729. */
4637 744 : if (!s2->attr.intrinsic && s2->attr.if_source == IFSRC_UNKNOWN
4638 102 : && !s2->attr.external && !s2->attr.subroutine && !s2->attr.function)
4639 : {
4640 1 : gfc_error ("Procedure pointer target %qs at %L must be either an "
4641 : "intrinsic, host or use associated, referenced or have "
4642 : "the EXTERNAL attribute", s2->name, &rvalue->where);
4643 1 : return false;
4644 : }
4645 :
4646 : return true;
4647 : }
4648 : else
4649 : {
4650 : /* A non-proc pointer cannot point to a constant. */
4651 8069 : if (rvalue->expr_type == EXPR_CONSTANT)
4652 : {
4653 2 : gfc_error_now ("Pointer assignment target cannot be a constant at %L",
4654 : &rvalue->where);
4655 2 : return false;
4656 : }
4657 : }
4658 :
4659 8067 : if (!gfc_compare_types (&lvalue->ts, &rvalue->ts))
4660 : {
4661 : /* Check for F03:C717. */
4662 11 : if (UNLIMITED_POLY (rvalue)
4663 1 : && !(UNLIMITED_POLY (lvalue)
4664 1 : || (lvalue->ts.type == BT_DERIVED
4665 0 : && (lvalue->ts.u.derived->attr.is_bind_c
4666 0 : || lvalue->ts.u.derived->attr.sequence))))
4667 1 : gfc_error ("Data-pointer-object at %L must be unlimited "
4668 : "polymorphic, or of a type with the BIND or SEQUENCE "
4669 : "attribute, to be compatible with an unlimited "
4670 : "polymorphic target", &lvalue->where);
4671 10 : else if (!suppress_type_test)
4672 8 : gfc_error ("Different types in pointer assignment at %L; "
4673 : "attempted assignment of %s to %s", &lvalue->where,
4674 : gfc_typename (rvalue), gfc_typename (lvalue));
4675 : return false;
4676 : }
4677 :
4678 8056 : if (lvalue->ts.type != BT_CLASS && lvalue->ts.kind != rvalue->ts.kind)
4679 : {
4680 0 : gfc_error ("Different kind type parameters in pointer "
4681 : "assignment at %L", &lvalue->where);
4682 0 : return false;
4683 : }
4684 :
4685 8056 : if (lvalue->rank != rvalue->rank && !rank_remap
4686 70 : && !(rvalue->expr_type == EXPR_NULL && is_init_expr))
4687 : {
4688 4 : gfc_error ("Different ranks in pointer assignment at %L", &lvalue->where);
4689 4 : return false;
4690 : }
4691 :
4692 : /* Make sure the vtab is present. */
4693 8052 : if (lvalue->ts.type == BT_CLASS && !UNLIMITED_POLY (rvalue))
4694 1411 : gfc_find_vtab (&rvalue->ts);
4695 :
4696 : /* Check rank remapping. */
4697 8052 : if (rank_remap)
4698 : {
4699 247 : mpz_t lsize, rsize;
4700 :
4701 : /* If this can be determined, check that the target must be at least as
4702 : large as the pointer assigned to it is. */
4703 247 : bool got_lsize = gfc_array_size (lvalue, &lsize);
4704 247 : bool got_rsize = got_lsize && gfc_array_size (rvalue, &rsize);
4705 88 : bool too_small = got_rsize && mpz_cmp (rsize, lsize) < 0;
4706 :
4707 247 : if (too_small)
4708 : {
4709 4 : gfc_error ("Rank remapping target is smaller than size of the"
4710 : " pointer (%ld < %ld) at %L",
4711 : mpz_get_si (rsize), mpz_get_si (lsize),
4712 : &lvalue->where);
4713 4 : mpz_clear (lsize);
4714 4 : mpz_clear (rsize);
4715 10 : return false;
4716 : }
4717 243 : if (got_lsize)
4718 154 : mpz_clear (lsize);
4719 243 : if (got_rsize)
4720 84 : mpz_clear (rsize);
4721 :
4722 : /* An assumed rank target is an experimental F202y feature. */
4723 243 : if (rvalue->rank == -1 && !(gfc_option.allow_std & GFC_STD_F202Y))
4724 : {
4725 1 : gfc_error ("The assumed rank target at %L is an experimental F202y "
4726 : "feature. Use option -std=f202y to enable",
4727 : &rvalue->where);
4728 1 : return false;
4729 : }
4730 :
4731 : /* The target must be either rank one or it must be simply contiguous
4732 : and F2008 must be allowed. */
4733 242 : if (rvalue->rank != 1 && rvalue->rank != -1)
4734 : {
4735 28 : if (!gfc_is_simply_contiguous (rvalue, true, false))
4736 : {
4737 4 : gfc_error ("Rank remapping target must be rank 1 or"
4738 : " simply contiguous at %L", &rvalue->where);
4739 4 : return false;
4740 : }
4741 24 : if (!gfc_notify_std (GFC_STD_F2008, "Rank remapping target is not "
4742 : "rank 1 at %L", &rvalue->where))
4743 : return false;
4744 : }
4745 : }
4746 7805 : else if (rvalue->rank == -1)
4747 : {
4748 0 : gfc_error ("The data-target at %L is an assumed rank object and so the "
4749 : "data-pointer-object %s must have a bounds remapping list "
4750 : "(list of lbound:ubound for each dimension)",
4751 0 : &rvalue->where, lvalue->symtree->name);
4752 0 : return false;
4753 : }
4754 :
4755 8042 : if (rvalue->rank == -1 && !gfc_is_simply_contiguous (rvalue, true, false))
4756 : {
4757 0 : gfc_error ("The assumed rank data-target at %L must be contiguous",
4758 : &rvalue->where);
4759 0 : return false;
4760 : }
4761 :
4762 : /* Now punt if we are dealing with a NULLIFY(X) or X = NULL(X). */
4763 8042 : if (rvalue->expr_type == EXPR_NULL)
4764 : return true;
4765 :
4766 7928 : if (rvalue->expr_type == EXPR_VARIABLE && is_subref_array (rvalue))
4767 772 : lvalue->symtree->n.sym->attr.subref_array_pointer = 1;
4768 :
4769 7928 : attr = gfc_expr_attr (rvalue);
4770 :
4771 : /* The elements of a pointer function result may be spaced by more than
4772 : the element size. */
4773 7928 : if (rvalue->expr_type == EXPR_FUNCTION && rvalue->rank > 0 && attr.pointer)
4774 181 : lvalue->symtree->n.sym->attr.subref_array_pointer = 1;
4775 :
4776 7928 : if (rvalue->expr_type == EXPR_FUNCTION && !attr.pointer)
4777 : {
4778 : /* F2008, C725. For PURE also C1283. Sometimes rvalue is a function call
4779 : to caf_get. Map this to the same error message as below when it is
4780 : still a variable expression. */
4781 1 : if (rvalue->value.function.isym
4782 0 : && rvalue->value.function.isym->id == GFC_ISYM_CAF_GET)
4783 : /* The test above might need to be extend when F08, Note 5.4 has to be
4784 : interpreted in the way that target and pointer with the same coindex
4785 : are allowed. */
4786 0 : gfc_error ("Data target at %L shall not have a coindex",
4787 : &rvalue->where);
4788 : else
4789 1 : gfc_error ("Target expression in pointer assignment "
4790 : "at %L must deliver a pointer result",
4791 : &rvalue->where);
4792 : return false;
4793 : }
4794 :
4795 7927 : if (is_init_expr)
4796 : {
4797 371 : gfc_symbol *sym;
4798 371 : bool target;
4799 371 : gfc_ref *ref;
4800 :
4801 371 : if (gfc_is_size_zero_array (rvalue))
4802 : {
4803 1 : gfc_error ("Zero-sized array detected at %L where an entity with "
4804 : "the TARGET attribute is expected", &rvalue->where);
4805 1 : return false;
4806 : }
4807 370 : else if (!rvalue->symtree)
4808 : {
4809 1 : gfc_error ("Pointer assignment target in initialization expression "
4810 : "does not have the TARGET attribute at %L",
4811 : &rvalue->where);
4812 1 : return false;
4813 : }
4814 :
4815 369 : sym = rvalue->symtree->n.sym;
4816 :
4817 369 : if (sym->ts.type == BT_CLASS && sym->attr.class_ok)
4818 0 : target = CLASS_DATA (sym)->attr.target;
4819 : else
4820 369 : target = sym->attr.target;
4821 :
4822 369 : if (!target && !proc_pointer)
4823 : {
4824 4 : gfc_error ("Pointer assignment target in initialization expression "
4825 : "does not have the TARGET attribute at %L",
4826 : &rvalue->where);
4827 4 : return false;
4828 : }
4829 :
4830 552 : for (ref = rvalue->ref; ref; ref = ref->next)
4831 : {
4832 192 : switch (ref->type)
4833 : {
4834 : case REF_ARRAY:
4835 83 : for (int n = 0; n < ref->u.ar.dimen; n++)
4836 43 : if (!gfc_is_constant_expr (ref->u.ar.start[n])
4837 41 : || !gfc_is_constant_expr (ref->u.ar.end[n])
4838 83 : || !gfc_is_constant_expr (ref->u.ar.stride[n]))
4839 : {
4840 3 : gfc_error ("Every subscript of target specification "
4841 : "at %L must be a constant expression",
4842 : &ref->u.ar.where);
4843 3 : return false;
4844 : }
4845 : break;
4846 :
4847 101 : case REF_SUBSTRING:
4848 101 : if (!gfc_is_constant_expr (ref->u.ss.start)
4849 101 : || !gfc_is_constant_expr (ref->u.ss.end))
4850 : {
4851 2 : gfc_error ("Substring starting and ending points of target "
4852 : "specification at %L must be constant expressions",
4853 2 : &ref->u.ss.start->where);
4854 2 : return false;
4855 : }
4856 : break;
4857 :
4858 : default:
4859 : break;
4860 : }
4861 : }
4862 : }
4863 : else
4864 : {
4865 7556 : if (!attr.target && !attr.pointer)
4866 : {
4867 9 : gfc_error ("Pointer assignment target is neither TARGET "
4868 : "nor POINTER at %L", &rvalue->where);
4869 9 : return false;
4870 : }
4871 : }
4872 :
4873 7907 : if (lvalue->ts.type == BT_CHARACTER)
4874 : {
4875 1397 : bool t = gfc_check_same_strlen (lvalue, rvalue, "pointer assignment");
4876 1397 : if (!t)
4877 : return false;
4878 : }
4879 :
4880 7905 : if (is_pure && gfc_impure_variable (rvalue->symtree->n.sym))
4881 : {
4882 3 : gfc_error ("Bad target in pointer assignment in PURE "
4883 : "procedure at %L", &rvalue->where);
4884 : }
4885 :
4886 7905 : if (is_implicit_pure && gfc_impure_variable (rvalue->symtree->n.sym))
4887 375 : gfc_unset_implicit_pure (gfc_current_ns->proc_name);
4888 :
4889 7905 : if (gfc_has_vector_index (rvalue))
4890 : {
4891 2 : gfc_error ("Pointer assignment with vector subscript "
4892 : "on rhs at %L", &rvalue->where);
4893 2 : return false;
4894 : }
4895 :
4896 7903 : if (attr.is_protected && attr.use_assoc
4897 4 : && !(attr.pointer || attr.proc_pointer))
4898 : {
4899 3 : gfc_error ("Pointer assignment target has PROTECTED "
4900 : "attribute at %L", &rvalue->where);
4901 3 : return false;
4902 : }
4903 :
4904 : /* F2008, C725. For PURE also C1283. */
4905 7900 : if (rvalue->expr_type == EXPR_VARIABLE
4906 7900 : && gfc_is_coindexed (rvalue))
4907 : {
4908 4 : gfc_ref *ref;
4909 5 : for (ref = rvalue->ref; ref; ref = ref->next)
4910 5 : if (ref->type == REF_ARRAY && ref->u.ar.codimen)
4911 : {
4912 4 : gfc_error ("Data target at %L shall not have a coindex",
4913 : &rvalue->where);
4914 4 : return false;
4915 : }
4916 : }
4917 :
4918 : /* Warn for assignments of contiguous pointers to targets which is not
4919 : contiguous. Be lenient in the definition of what counts as
4920 : contiguous. */
4921 :
4922 7896 : if (lhs_attr.contiguous
4923 76 : && lhs_attr.dimension > 0)
4924 : {
4925 72 : if (gfc_is_not_contiguous (rvalue))
4926 : {
4927 6 : gfc_error ("Assignment to contiguous pointer from "
4928 : "non-contiguous target at %L", &rvalue->where);
4929 6 : return false;
4930 : }
4931 66 : if (!gfc_is_simply_contiguous (rvalue, false, true))
4932 8 : gfc_warning (OPT_Wextra, "Assignment to contiguous pointer from "
4933 : "non-contiguous target at %L", &rvalue->where);
4934 : }
4935 :
4936 : /* Warn if it is the LHS pointer may lives longer than the RHS target. */
4937 7890 : if (warn_target_lifetime
4938 15 : && rvalue->expr_type == EXPR_VARIABLE
4939 15 : && !rvalue->symtree->n.sym->attr.save
4940 15 : && !rvalue->symtree->n.sym->attr.pointer && !attr.pointer
4941 13 : && !rvalue->symtree->n.sym->attr.host_assoc
4942 11 : && !rvalue->symtree->n.sym->attr.in_common
4943 11 : && !rvalue->symtree->n.sym->attr.use_assoc
4944 11 : && !rvalue->symtree->n.sym->attr.dummy)
4945 : {
4946 9 : bool warn;
4947 9 : gfc_namespace *ns;
4948 :
4949 18 : warn = lvalue->symtree->n.sym->attr.dummy
4950 9 : || lvalue->symtree->n.sym->attr.result
4951 8 : || lvalue->symtree->n.sym->attr.function
4952 7 : || (lvalue->symtree->n.sym->attr.host_assoc
4953 4 : && lvalue->symtree->n.sym->ns
4954 4 : != rvalue->symtree->n.sym->ns)
4955 4 : || lvalue->symtree->n.sym->attr.use_assoc
4956 13 : || lvalue->symtree->n.sym->attr.in_common;
4957 :
4958 9 : if (rvalue->symtree->n.sym->ns->proc_name
4959 9 : && rvalue->symtree->n.sym->ns->proc_name->attr.flavor != FL_PROCEDURE
4960 3 : && rvalue->symtree->n.sym->ns->proc_name->attr.flavor != FL_PROGRAM)
4961 : for (ns = rvalue->symtree->n.sym->ns;
4962 5 : ns && ns->proc_name && ns->proc_name->attr.flavor != FL_PROCEDURE;
4963 : ns = ns->parent)
4964 3 : if (ns->parent == lvalue->symtree->n.sym->ns)
4965 : {
4966 : warn = true;
4967 : break;
4968 : }
4969 :
4970 9 : if (warn)
4971 5 : gfc_warning (OPT_Wtarget_lifetime,
4972 : "Pointer at %L in pointer assignment might outlive the "
4973 : "pointer target", &lvalue->where);
4974 : }
4975 :
4976 : return true;
4977 : }
4978 :
4979 :
4980 : /* Relative of gfc_check_assign() except that the lvalue is a single
4981 : symbol. Used for initialization assignments. */
4982 :
4983 : bool
4984 534182 : gfc_check_assign_symbol (gfc_symbol *sym, gfc_component *comp, gfc_expr *rvalue)
4985 : {
4986 534182 : gfc_expr lvalue;
4987 534182 : bool r;
4988 534182 : bool pointer, proc_pointer;
4989 :
4990 534182 : memset (&lvalue, '\0', sizeof (gfc_expr));
4991 :
4992 534182 : if (sym && sym->attr.pdt_template && comp && comp->initializer)
4993 : {
4994 558 : int i, flag;
4995 558 : gfc_expr *param_expr;
4996 558 : flag = 0;
4997 :
4998 558 : if (comp->as && comp->as->type == AS_EXPLICIT
4999 20 : && !(comp->ts.type == BT_DERIVED
5000 7 : && comp->ts.u.derived->attr.pdt_template))
5001 : {
5002 : /* Are the bounds of the array parameterized? */
5003 26 : for (i = 0; i < comp->as->rank; i++)
5004 : {
5005 13 : param_expr = gfc_copy_expr (comp->as->lower[i]);
5006 13 : if (gfc_simplify_expr (param_expr, 1)
5007 13 : && param_expr->expr_type != EXPR_CONSTANT)
5008 0 : flag++;
5009 13 : gfc_free_expr (param_expr);
5010 13 : param_expr = gfc_copy_expr (comp->as->upper[i]);
5011 13 : if (gfc_simplify_expr (param_expr, 1)
5012 13 : && param_expr->expr_type != EXPR_CONSTANT)
5013 1 : flag++;
5014 13 : gfc_free_expr (param_expr);
5015 : }
5016 : }
5017 :
5018 : /* Is the character length parameterized? */
5019 558 : if (comp->ts.type == BT_CHARACTER && comp->ts.u.cl->length)
5020 : {
5021 27 : param_expr = gfc_copy_expr (comp->ts.u.cl->length);
5022 27 : if (gfc_simplify_expr (param_expr, 1)
5023 27 : && param_expr->expr_type != EXPR_CONSTANT)
5024 1 : flag++;
5025 27 : gfc_free_expr (param_expr);
5026 : }
5027 :
5028 558 : if (flag)
5029 : {
5030 2 : gfc_error ("The component %qs at %L of derived type %qs has "
5031 : "parameterized type or array length parameters, which is "
5032 : "not compatible with a default initializer",
5033 2 : comp->name, &comp->initializer->where, sym->name);
5034 2 : return false;
5035 : }
5036 : }
5037 :
5038 534180 : lvalue.expr_type = EXPR_VARIABLE;
5039 534180 : lvalue.ts = sym->ts;
5040 534180 : if (sym->as)
5041 : {
5042 17459 : lvalue.rank = sym->as->rank;
5043 17459 : lvalue.corank = sym->as->corank;
5044 : }
5045 534180 : lvalue.symtree = XCNEW (gfc_symtree);
5046 534180 : lvalue.symtree->n.sym = sym;
5047 534180 : lvalue.where = sym->declared_at;
5048 :
5049 534180 : if (comp)
5050 : {
5051 33685 : lvalue.ref = gfc_get_ref ();
5052 33685 : lvalue.ref->type = REF_COMPONENT;
5053 33685 : lvalue.ref->u.c.component = comp;
5054 33685 : lvalue.ref->u.c.sym = sym;
5055 33685 : lvalue.ts = comp->ts;
5056 33685 : lvalue.rank = comp->as ? comp->as->rank : 0;
5057 33685 : lvalue.corank = comp->as ? comp->as->corank : 0;
5058 33685 : lvalue.where = comp->loc;
5059 1052 : pointer = comp->ts.type == BT_CLASS && CLASS_DATA (comp)
5060 34737 : ? CLASS_DATA (comp)->attr.class_pointer : comp->attr.pointer;
5061 33685 : proc_pointer = comp->attr.proc_pointer;
5062 : }
5063 : else
5064 : {
5065 3043 : pointer = sym->ts.type == BT_CLASS && CLASS_DATA (sym)
5066 503538 : ? CLASS_DATA (sym)->attr.class_pointer : sym->attr.pointer;
5067 500495 : proc_pointer = sym->attr.proc_pointer;
5068 : }
5069 :
5070 534180 : if (pointer || proc_pointer)
5071 6202 : r = gfc_check_pointer_assign (&lvalue, rvalue, false, true);
5072 : else
5073 : {
5074 : /* If a conversion function, e.g., __convert_i8_i4, was inserted
5075 : into an array constructor, we should check if it can be reduced
5076 : as an initialization expression. */
5077 527978 : if (rvalue->expr_type == EXPR_FUNCTION
5078 73 : && rvalue->value.function.isym
5079 30 : && (rvalue->value.function.isym->conversion == 1))
5080 0 : gfc_check_init_expr (rvalue);
5081 :
5082 527978 : r = gfc_check_assign (&lvalue, rvalue, 1);
5083 : }
5084 :
5085 534180 : free (lvalue.symtree);
5086 534180 : free (lvalue.ref);
5087 :
5088 534180 : if (!r)
5089 : return r;
5090 :
5091 534129 : if (pointer && rvalue->expr_type != EXPR_NULL && !proc_pointer)
5092 : {
5093 : /* F08:C461. Additional checks for pointer initialization. */
5094 335 : symbol_attribute attr;
5095 335 : attr = gfc_expr_attr (rvalue);
5096 335 : if (attr.allocatable)
5097 : {
5098 2 : gfc_error ("Pointer initialization target at %L "
5099 : "must not be ALLOCATABLE", &rvalue->where);
5100 13 : return false;
5101 : }
5102 333 : if (!attr.target || attr.pointer)
5103 : {
5104 1 : gfc_error ("Pointer initialization target at %L "
5105 : "must have the TARGET attribute", &rvalue->where);
5106 1 : return false;
5107 : }
5108 :
5109 332 : if (!attr.save && rvalue->expr_type == EXPR_VARIABLE
5110 14 : && rvalue->symtree->n.sym->ns->proc_name
5111 14 : && rvalue->symtree->n.sym->ns->proc_name->attr.is_main_program)
5112 : {
5113 4 : rvalue->symtree->n.sym->ns->proc_name->attr.save = SAVE_IMPLICIT;
5114 4 : attr.save = SAVE_IMPLICIT;
5115 : }
5116 :
5117 332 : if (!attr.save)
5118 : {
5119 10 : gfc_error ("Pointer initialization target at %L "
5120 : "must have the SAVE attribute", &rvalue->where);
5121 10 : return false;
5122 : }
5123 : }
5124 :
5125 534116 : if (proc_pointer && rvalue->expr_type != EXPR_NULL)
5126 : {
5127 : /* F08:C1220. Additional checks for procedure pointer initialization. */
5128 65 : symbol_attribute attr = gfc_expr_attr (rvalue);
5129 65 : if (attr.proc_pointer)
5130 : {
5131 1 : gfc_error ("Procedure pointer initialization target at %L "
5132 : "may not be a procedure pointer", &rvalue->where);
5133 3 : return false;
5134 : }
5135 64 : if (attr.proc == PROC_INTERNAL)
5136 : {
5137 1 : gfc_error ("Internal procedure %qs is invalid in "
5138 : "procedure pointer initialization at %L",
5139 1 : rvalue->symtree->name, &rvalue->where);
5140 1 : return false;
5141 : }
5142 63 : if (attr.dummy)
5143 : {
5144 1 : gfc_error ("Dummy procedure %qs is invalid in "
5145 : "procedure pointer initialization at %L",
5146 1 : rvalue->symtree->name, &rvalue->where);
5147 1 : return false;
5148 : }
5149 : }
5150 :
5151 : return true;
5152 : }
5153 :
5154 : /* Build an initializer for a local integer, real, complex, logical, or
5155 : character variable, based on the command line flags finit-local-zero,
5156 : finit-integer=, finit-real=, finit-logical=, and finit-character=.
5157 : With force, an initializer is ALWAYS generated. */
5158 :
5159 : static gfc_expr *
5160 105567 : gfc_build_init_expr (gfc_typespec *ts, locus *where, bool force)
5161 : {
5162 105567 : gfc_expr *init_expr;
5163 :
5164 : /* Try to build an initializer expression. */
5165 105567 : init_expr = gfc_get_constant_expr (ts->type, ts->kind, where);
5166 :
5167 : /* If we want to force generation, make sure we default to zero. */
5168 105567 : gfc_init_local_real init_real = flag_init_real;
5169 105567 : int init_logical = gfc_option.flag_init_logical;
5170 105567 : if (force)
5171 : {
5172 210 : if (init_real == GFC_INIT_REAL_OFF)
5173 : init_real = GFC_INIT_REAL_ZERO;
5174 210 : if (init_logical == GFC_INIT_LOGICAL_OFF)
5175 40 : init_logical = GFC_INIT_LOGICAL_FALSE;
5176 : }
5177 :
5178 : /* We will only initialize integers, reals, complex, logicals, and
5179 : characters, and only if the corresponding command-line flags
5180 : were set. Otherwise, we free init_expr and return null. */
5181 105567 : switch (ts->type)
5182 : {
5183 55238 : case BT_INTEGER:
5184 55238 : if (force || gfc_option.flag_init_integer != GFC_INIT_INTEGER_OFF)
5185 285 : mpz_set_si (init_expr->value.integer,
5186 : gfc_option.flag_init_integer_value);
5187 : else
5188 : {
5189 54953 : gfc_free_expr (init_expr);
5190 54953 : init_expr = NULL;
5191 : }
5192 : break;
5193 :
5194 16255 : case BT_REAL:
5195 16255 : switch (init_real)
5196 : {
5197 0 : case GFC_INIT_REAL_SNAN:
5198 0 : init_expr->is_snan = 1;
5199 : /* Fall through. */
5200 48 : case GFC_INIT_REAL_NAN:
5201 48 : mpfr_set_nan (init_expr->value.real);
5202 48 : break;
5203 :
5204 26 : case GFC_INIT_REAL_INF:
5205 26 : mpfr_set_inf (init_expr->value.real, 1);
5206 26 : break;
5207 :
5208 24 : case GFC_INIT_REAL_NEG_INF:
5209 24 : mpfr_set_inf (init_expr->value.real, -1);
5210 24 : break;
5211 :
5212 63 : case GFC_INIT_REAL_ZERO:
5213 63 : mpfr_set_ui (init_expr->value.real, 0.0, GFC_RND_MODE);
5214 63 : break;
5215 :
5216 16094 : default:
5217 16094 : gfc_free_expr (init_expr);
5218 16094 : init_expr = NULL;
5219 16094 : break;
5220 : }
5221 : break;
5222 :
5223 1727 : case BT_COMPLEX:
5224 1727 : switch (init_real)
5225 : {
5226 0 : case GFC_INIT_REAL_SNAN:
5227 0 : init_expr->is_snan = 1;
5228 : /* Fall through. */
5229 12 : case GFC_INIT_REAL_NAN:
5230 12 : mpfr_set_nan (mpc_realref (init_expr->value.complex));
5231 12 : mpfr_set_nan (mpc_imagref (init_expr->value.complex));
5232 12 : break;
5233 :
5234 0 : case GFC_INIT_REAL_INF:
5235 0 : mpfr_set_inf (mpc_realref (init_expr->value.complex), 1);
5236 0 : mpfr_set_inf (mpc_imagref (init_expr->value.complex), 1);
5237 0 : break;
5238 :
5239 0 : case GFC_INIT_REAL_NEG_INF:
5240 0 : mpfr_set_inf (mpc_realref (init_expr->value.complex), -1);
5241 0 : mpfr_set_inf (mpc_imagref (init_expr->value.complex), -1);
5242 0 : break;
5243 :
5244 24 : case GFC_INIT_REAL_ZERO:
5245 24 : mpc_set_ui (init_expr->value.complex, 0, GFC_MPC_RND_MODE);
5246 24 : break;
5247 :
5248 1691 : default:
5249 1691 : gfc_free_expr (init_expr);
5250 1691 : init_expr = NULL;
5251 1691 : break;
5252 : }
5253 : break;
5254 :
5255 5019 : case BT_LOGICAL:
5256 5019 : if (init_logical == GFC_INIT_LOGICAL_FALSE)
5257 39 : init_expr->value.logical = 0;
5258 4980 : else if (init_logical == GFC_INIT_LOGICAL_TRUE)
5259 26 : init_expr->value.logical = 1;
5260 : else
5261 : {
5262 4954 : gfc_free_expr (init_expr);
5263 4954 : init_expr = NULL;
5264 : }
5265 : break;
5266 :
5267 10052 : case BT_CHARACTER:
5268 : /* For characters, the length must be constant in order to
5269 : create a default initializer. */
5270 10052 : if ((force || gfc_option.flag_init_character == GFC_INIT_CHARACTER_ON)
5271 83 : && ts->u.cl->length
5272 83 : && ts->u.cl->length->expr_type == EXPR_CONSTANT)
5273 : {
5274 76 : HOST_WIDE_INT char_len = gfc_mpz_get_hwi (ts->u.cl->length->value.integer);
5275 76 : init_expr->value.character.length = char_len;
5276 76 : init_expr->value.character.string = gfc_get_wide_string (char_len+1);
5277 320 : for (size_t i = 0; i < (size_t) char_len; i++)
5278 244 : init_expr->value.character.string[i]
5279 244 : = (unsigned char) gfc_option.flag_init_character_value;
5280 : }
5281 : else
5282 : {
5283 9976 : gfc_free_expr (init_expr);
5284 9976 : init_expr = NULL;
5285 : }
5286 9976 : if (!init_expr
5287 9976 : && (force || gfc_option.flag_init_character == GFC_INIT_CHARACTER_ON)
5288 7 : && ts->u.cl->length && flag_max_stack_var_size != 0)
5289 : {
5290 6 : gfc_actual_arglist *arg;
5291 6 : init_expr = gfc_get_expr ();
5292 6 : init_expr->where = *where;
5293 6 : init_expr->ts = *ts;
5294 6 : init_expr->expr_type = EXPR_FUNCTION;
5295 12 : init_expr->value.function.isym =
5296 6 : gfc_intrinsic_function_by_id (GFC_ISYM_REPEAT);
5297 6 : init_expr->value.function.name = "repeat";
5298 6 : arg = gfc_get_actual_arglist ();
5299 6 : arg->expr = gfc_get_character_expr (ts->kind, where, NULL, 1);
5300 6 : arg->expr->value.character.string[0] =
5301 6 : gfc_option.flag_init_character_value;
5302 6 : arg->next = gfc_get_actual_arglist ();
5303 6 : arg->next->expr = gfc_copy_expr (ts->u.cl->length);
5304 6 : init_expr->value.function.actual = arg;
5305 : }
5306 : break;
5307 :
5308 17276 : default:
5309 17276 : gfc_free_expr (init_expr);
5310 17276 : init_expr = NULL;
5311 : }
5312 :
5313 105567 : return init_expr;
5314 : }
5315 :
5316 : /* Invoke gfc_build_init_expr to create an initializer expression, but do not
5317 : * require that an expression be built. */
5318 :
5319 : gfc_expr *
5320 105357 : gfc_build_default_init_expr (gfc_typespec *ts, locus *where)
5321 : {
5322 105357 : return gfc_build_init_expr (ts, where, false);
5323 : }
5324 :
5325 : /* Apply an initialization expression to a typespec. Can be used for symbols or
5326 : components. Similar to add_init_expr_to_sym in decl.cc; could probably be
5327 : combined with some effort. */
5328 :
5329 : void
5330 20222 : gfc_apply_init (gfc_typespec *ts, symbol_attribute *attr, gfc_expr *init)
5331 : {
5332 20222 : if (ts->type == BT_CHARACTER && !attr->pointer && init
5333 405 : && ts->u.cl
5334 405 : && ts->u.cl->length
5335 405 : && ts->u.cl->length->expr_type == EXPR_CONSTANT
5336 401 : && ts->u.cl->length->ts.type == BT_INTEGER)
5337 : {
5338 401 : HOST_WIDE_INT len = gfc_mpz_get_hwi (ts->u.cl->length->value.integer);
5339 :
5340 401 : if (init->expr_type == EXPR_CONSTANT)
5341 282 : gfc_set_constant_character_len (len, init, -1);
5342 119 : else if (init
5343 119 : && init->ts.type == BT_CHARACTER
5344 114 : && init->ts.u.cl && init->ts.u.cl->length
5345 114 : && mpz_cmp (ts->u.cl->length->value.integer,
5346 114 : init->ts.u.cl->length->value.integer))
5347 : {
5348 0 : gfc_constructor *ctor;
5349 0 : ctor = gfc_constructor_first (init->value.constructor);
5350 :
5351 0 : if (ctor)
5352 : {
5353 0 : bool has_ts = (init->ts.u.cl
5354 0 : && init->ts.u.cl->length_from_typespec);
5355 :
5356 : /* Remember the length of the first element for checking
5357 : that all elements *in the constructor* have the same
5358 : length. This need not be the length of the LHS! */
5359 0 : gcc_assert (ctor->expr->expr_type == EXPR_CONSTANT);
5360 0 : gcc_assert (ctor->expr->ts.type == BT_CHARACTER);
5361 0 : gfc_charlen_t first_len = ctor->expr->value.character.length;
5362 :
5363 0 : for ( ; ctor; ctor = gfc_constructor_next (ctor))
5364 0 : if (ctor->expr->expr_type == EXPR_CONSTANT)
5365 : {
5366 0 : gfc_set_constant_character_len (len, ctor->expr,
5367 : has_ts ? -1 : first_len);
5368 0 : if (!ctor->expr->ts.u.cl)
5369 0 : ctor->expr->ts.u.cl
5370 0 : = gfc_new_charlen (gfc_current_ns, ts->u.cl);
5371 : else
5372 0 : ctor->expr->ts.u.cl->length
5373 0 : = gfc_copy_expr (ts->u.cl->length);
5374 : }
5375 : }
5376 : }
5377 : }
5378 20222 : }
5379 :
5380 :
5381 : /* Check whether an expression is a structure constructor and whether it has
5382 : other values than NULL. */
5383 :
5384 : static bool
5385 897 : is_non_empty_structure_constructor (gfc_expr * e)
5386 : {
5387 897 : if (e->expr_type != EXPR_STRUCTURE)
5388 : return false;
5389 :
5390 897 : gfc_constructor *cons = gfc_constructor_first (e->value.constructor);
5391 2380 : while (cons)
5392 : {
5393 1021 : if (!cons->expr || cons->expr->expr_type != EXPR_NULL)
5394 : return true;
5395 586 : cons = gfc_constructor_next (cons);
5396 : }
5397 : return false;
5398 : }
5399 :
5400 :
5401 : /* Check for default initializer; sym->value is not enough
5402 : as it is also set for EXPR_NULL of allocatables. */
5403 :
5404 : bool
5405 7881 : gfc_has_default_initializer (gfc_symbol *der)
5406 : {
5407 7881 : static hash_set<gfc_symbol *> seen_derived_types;
5408 7881 : gfc_component *c;
5409 : /* The rewrite to a result variable and breaks is only needed, because
5410 : there is no scope_guard in C++ yet. */
5411 7881 : bool result = false;
5412 :
5413 7881 : gcc_assert (gfc_fl_struct (der->attr.flavor));
5414 7881 : seen_derived_types.add (der);
5415 15983 : for (c = der->components; c; c = c->next)
5416 8103 : if (gfc_bt_struct (c->ts.type)
5417 9924 : && !seen_derived_types.contains (c->ts.u.derived))
5418 : {
5419 1610 : if (!c->attr.pointer && !c->attr.proc_pointer
5420 1610 : && !(c->attr.allocatable && der == c->ts.u.derived)
5421 3350 : && ((c->initializer
5422 897 : && is_non_empty_structure_constructor (c->initializer))
5423 1175 : || gfc_has_default_initializer (c->ts.u.derived)))
5424 : {
5425 : result = true;
5426 : break;
5427 : }
5428 1235 : if (c->attr.pointer && c->initializer)
5429 : {
5430 : result = true;
5431 : break;
5432 : }
5433 : }
5434 : else
5435 : {
5436 8180 : if (c->initializer)
5437 : {
5438 : result = true;
5439 : break;
5440 : }
5441 : }
5442 :
5443 7881 : seen_derived_types.remove (der);
5444 7881 : return result;
5445 : }
5446 :
5447 :
5448 : /*
5449 : Generate an initializer expression which initializes the entirety of a union.
5450 : A normal structure constructor is insufficient without undue effort, because
5451 : components of maps may be oddly aligned/overlapped. (For example if a
5452 : character is initialized from one map overtop a real from the other, only one
5453 : byte of the real is actually initialized.) Unfortunately we don't know the
5454 : size of the union right now, so we can't generate a proper initializer, but
5455 : we use a NULL expr as a placeholder and do the right thing later in
5456 : gfc_trans_subcomponent_assign.
5457 : */
5458 : static gfc_expr *
5459 15 : generate_union_initializer (gfc_component *un)
5460 : {
5461 15 : if (un == NULL || un->ts.type != BT_UNION)
5462 : return NULL;
5463 :
5464 15 : gfc_expr *placeholder = gfc_get_null_expr (&un->loc);
5465 15 : placeholder->ts = un->ts;
5466 15 : return placeholder;
5467 : }
5468 :
5469 :
5470 : /* Get the user-specified initializer for a union, if any. This means the user
5471 : has said to initialize component(s) of a map. For simplicity's sake we
5472 : only allow the user to initialize the first map. We don't have to worry
5473 : about overlapping initializers as they are released early in resolution (see
5474 : resolve_fl_struct). */
5475 :
5476 : static gfc_expr *
5477 15 : get_union_initializer (gfc_symbol *union_type, gfc_component **map_p)
5478 : {
5479 15 : gfc_component *map;
5480 15 : gfc_expr *init=NULL;
5481 :
5482 15 : if (!union_type || union_type->attr.flavor != FL_UNION)
5483 : return NULL;
5484 :
5485 48 : for (map = union_type->components; map; map = map->next)
5486 : {
5487 33 : if (gfc_has_default_initializer (map->ts.u.derived))
5488 : {
5489 0 : init = gfc_default_initializer (&map->ts);
5490 0 : if (map_p)
5491 0 : *map_p = map;
5492 : break;
5493 : }
5494 : }
5495 :
5496 15 : if (map_p && !init)
5497 15 : *map_p = NULL;
5498 :
5499 : return init;
5500 : }
5501 :
5502 : static bool
5503 166737 : class_allocatable (gfc_component *comp)
5504 : {
5505 3478 : return comp->ts.type == BT_CLASS && comp->attr.class_ok && CLASS_DATA (comp)
5506 170214 : && CLASS_DATA (comp)->attr.allocatable;
5507 : }
5508 :
5509 : static bool
5510 268 : class_pointer (gfc_component *comp)
5511 : {
5512 1 : return comp->ts.type == BT_CLASS && comp->attr.class_ok && CLASS_DATA (comp)
5513 269 : && CLASS_DATA (comp)->attr.pointer;
5514 : }
5515 :
5516 : static bool
5517 186444 : comp_allocatable (gfc_component *comp)
5518 : {
5519 186444 : return comp->attr.allocatable || class_allocatable (comp);
5520 : }
5521 :
5522 : static bool
5523 271 : comp_pointer (gfc_component *comp)
5524 : {
5525 271 : return comp->attr.pointer
5526 268 : || comp->attr.proc_pointer
5527 268 : || comp->attr.class_pointer
5528 539 : || class_pointer (comp);
5529 : }
5530 :
5531 : /* Fetch or generate an initializer for the given component.
5532 : Only generate an initializer if generate is true. */
5533 :
5534 : static gfc_expr *
5535 129483 : component_initializer (gfc_component *c, bool generate)
5536 : {
5537 129483 : gfc_expr *init = NULL;
5538 :
5539 : /* Allocatable components always get EXPR_NULL.
5540 : Pointer components are only initialized when generating, and only if they
5541 : do not already have an initializer. */
5542 129483 : if (comp_allocatable (c) || (generate && comp_pointer (c) && !c->initializer))
5543 : {
5544 13267 : init = gfc_get_null_expr (&c->loc);
5545 13267 : init->ts = c->ts;
5546 13267 : return init;
5547 : }
5548 :
5549 : /* See if we can find the initializer immediately. */
5550 116216 : if (c->initializer || !generate)
5551 : return c->initializer;
5552 :
5553 : /* Recursively handle derived type components. */
5554 243 : else if (c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
5555 18 : init = gfc_generate_initializer (&c->ts, true);
5556 :
5557 225 : else if (c->ts.type == BT_UNION && c->ts.u.derived->components)
5558 : {
5559 15 : gfc_component *map = NULL;
5560 15 : gfc_constructor *ctor;
5561 15 : gfc_expr *user_init;
5562 :
5563 : /* If we don't have a user initializer and we aren't generating one, this
5564 : union has no initializer. */
5565 15 : user_init = get_union_initializer (c->ts.u.derived, &map);
5566 15 : if (!user_init && !generate)
5567 : return NULL;
5568 :
5569 : /* Otherwise use a structure constructor. */
5570 15 : init = gfc_get_structure_constructor_expr (c->ts.type, c->ts.kind,
5571 : &c->loc);
5572 15 : init->ts = c->ts;
5573 :
5574 : /* If we are to generate an initializer for the union, add a constructor
5575 : which initializes the whole union first. */
5576 15 : if (generate)
5577 : {
5578 15 : ctor = gfc_constructor_get ();
5579 15 : ctor->expr = generate_union_initializer (c);
5580 15 : gfc_constructor_append (&init->value.constructor, ctor);
5581 : }
5582 :
5583 : /* If we found an initializer in one of our maps, apply it. Note this
5584 : is applied _after_ the entire-union initializer above if any. */
5585 15 : if (user_init)
5586 : {
5587 0 : ctor = gfc_constructor_get ();
5588 0 : ctor->expr = user_init;
5589 0 : ctor->n.component = map;
5590 0 : gfc_constructor_append (&init->value.constructor, ctor);
5591 : }
5592 15 : }
5593 :
5594 : /* Treat simple components like locals. */
5595 : else
5596 : {
5597 : /* We MUST give an initializer, so force generation. */
5598 210 : init = gfc_build_init_expr (&c->ts, &c->loc, true);
5599 210 : gfc_apply_init (&c->ts, &c->attr, init);
5600 : }
5601 :
5602 : return init;
5603 : }
5604 :
5605 :
5606 : /* Get an expression for a default initializer of a derived type. */
5607 :
5608 : gfc_expr *
5609 29948 : gfc_default_initializer (gfc_typespec *ts)
5610 : {
5611 29948 : return gfc_generate_initializer (ts, false);
5612 : }
5613 :
5614 : /* Generate an initializer expression for an iso_c_binding type
5615 : such as c_[fun]ptr. The appropriate initializer is c_null_[fun]ptr. */
5616 :
5617 : static gfc_expr *
5618 3 : generate_isocbinding_initializer (gfc_symbol *derived)
5619 : {
5620 : /* The initializers have already been built into the c_null_[fun]ptr symbols
5621 : from gen_special_c_interop_ptr. */
5622 3 : gfc_symtree *npsym = NULL;
5623 3 : if (0 == strcmp (derived->name, "c_ptr"))
5624 2 : gfc_find_sym_tree ("c_null_ptr", gfc_current_ns, true, &npsym);
5625 1 : else if (0 == strcmp (derived->name, "c_funptr"))
5626 1 : gfc_find_sym_tree ("c_null_funptr", gfc_current_ns, true, &npsym);
5627 : else
5628 0 : gfc_internal_error ("generate_isocbinding_initializer(): bad iso_c_binding"
5629 : " type, expected %<c_ptr%> or %<c_funptr%>");
5630 3 : if (npsym)
5631 : {
5632 3 : gfc_expr *init = gfc_copy_expr (npsym->n.sym->value);
5633 3 : init->symtree = npsym;
5634 3 : init->ts.is_iso_c = true;
5635 3 : return init;
5636 : }
5637 :
5638 : return NULL;
5639 : }
5640 :
5641 : /* Get or generate an expression for a default initializer of a derived type.
5642 : If -finit-derived is specified, generate default initialization expressions
5643 : for components that lack them when generate is set. */
5644 :
5645 : gfc_expr *
5646 63497 : gfc_generate_initializer (gfc_typespec *ts, bool generate)
5647 : {
5648 63497 : gfc_expr *init, *tmp;
5649 63497 : gfc_component *comp;
5650 :
5651 63497 : generate = flag_init_derived && generate;
5652 :
5653 63497 : if (ts->u.derived->ts.is_iso_c && generate)
5654 3 : return generate_isocbinding_initializer (ts->u.derived);
5655 :
5656 : /* See if we have a default initializer in this, but not in nested
5657 : types (otherwise we could use gfc_has_default_initializer()).
5658 : We don't need to check if we are going to generate them. */
5659 63494 : comp = ts->u.derived->components;
5660 63494 : if (!generate)
5661 : {
5662 111082 : for (; comp; comp = comp->next)
5663 80164 : if (comp->initializer || comp_allocatable (comp))
5664 : break;
5665 : }
5666 :
5667 63494 : if (!comp)
5668 : return NULL;
5669 :
5670 32576 : init = gfc_get_structure_constructor_expr (ts->type, ts->kind,
5671 : &ts->u.derived->declared_at);
5672 32576 : init->ts = *ts;
5673 :
5674 162058 : for (comp = ts->u.derived->components; comp; comp = comp->next)
5675 : {
5676 129483 : gfc_constructor *ctor = gfc_constructor_get();
5677 :
5678 : /* Fetch or generate an initializer for the component. */
5679 129483 : tmp = component_initializer (comp, generate);
5680 129483 : if (tmp)
5681 : {
5682 : /* Save the component ref for STRUCTUREs and UNIONs. */
5683 117385 : if (ts->u.derived->attr.flavor == FL_STRUCT
5684 117385 : || ts->u.derived->attr.flavor == FL_UNION)
5685 343 : ctor->n.component = comp;
5686 :
5687 : /* If the initializer was not generated, we need a copy. */
5688 117385 : ctor->expr = comp->initializer ? gfc_copy_expr (tmp) : tmp;
5689 117385 : if ((comp->ts.type != tmp->ts.type || comp->ts.kind != tmp->ts.kind)
5690 19790 : && !comp->attr.pointer && !comp->attr.proc_pointer)
5691 : {
5692 303 : bool val;
5693 303 : val = gfc_convert_type_warn (ctor->expr, &comp->ts, 1, false);
5694 303 : if (val == false)
5695 : return NULL;
5696 : }
5697 : }
5698 :
5699 129482 : gfc_constructor_append (&init->value.constructor, ctor);
5700 : }
5701 :
5702 : return init;
5703 : }
5704 :
5705 :
5706 : /* Given a symbol, create an expression node with that symbol as a
5707 : variable. If the symbol is array valued, setup a reference of the
5708 : whole array. */
5709 :
5710 : gfc_expr *
5711 15529 : gfc_get_variable_expr (gfc_symtree *var)
5712 : {
5713 15529 : gfc_expr *e;
5714 :
5715 15529 : e = gfc_get_expr ();
5716 15529 : e->expr_type = EXPR_VARIABLE;
5717 15529 : e->symtree = var;
5718 15529 : e->ts = var->n.sym->ts;
5719 :
5720 15529 : if (var->n.sym->attr.flavor != FL_PROCEDURE
5721 11240 : && ((var->n.sym->as != NULL && var->n.sym->ts.type != BT_CLASS)
5722 8551 : || (var->n.sym->ts.type == BT_CLASS && var->n.sym->ts.u.derived
5723 4537 : && CLASS_DATA (var->n.sym)
5724 4537 : && CLASS_DATA (var->n.sym)->as)))
5725 : {
5726 6471 : gfc_array_spec *as = var->n.sym->ts.type == BT_CLASS
5727 4580 : ? CLASS_DATA (var->n.sym)->as
5728 : : var->n.sym->as;
5729 4580 : e->rank = as->rank;
5730 4580 : e->corank = as->corank;
5731 4580 : e->ref = gfc_get_ref ();
5732 4580 : e->ref->type = REF_ARRAY;
5733 4580 : e->ref->u.ar.type = AR_FULL;
5734 4580 : e->ref->u.ar.as = gfc_copy_array_spec (as);
5735 : }
5736 :
5737 15529 : return e;
5738 : }
5739 :
5740 :
5741 : /* Adds a full array reference to an expression, as needed. */
5742 :
5743 : void
5744 45678 : gfc_add_full_array_ref (gfc_expr *e, gfc_array_spec *as)
5745 : {
5746 45678 : gfc_ref *ref;
5747 45692 : for (ref = e->ref; ref; ref = ref->next)
5748 193 : if (!ref->next)
5749 : break;
5750 45678 : if (ref)
5751 : {
5752 179 : ref->next = gfc_get_ref ();
5753 179 : ref = ref->next;
5754 : }
5755 : else
5756 : {
5757 45499 : e->ref = gfc_get_ref ();
5758 45499 : ref = e->ref;
5759 : }
5760 45678 : ref->type = REF_ARRAY;
5761 45678 : ref->u.ar.type = AR_FULL;
5762 45678 : ref->u.ar.dimen = e->rank;
5763 : /* Do not set the corank here, or resolve will not be able to set correct
5764 : dimen-types for the coarray. */
5765 45678 : ref->u.ar.where = e->where;
5766 45678 : ref->u.ar.as = as;
5767 45678 : }
5768 :
5769 :
5770 : gfc_expr *
5771 198155 : gfc_lval_expr_from_sym (gfc_symbol *sym)
5772 : {
5773 198155 : gfc_expr *lval;
5774 198155 : gfc_array_spec *as;
5775 198155 : lval = gfc_get_expr ();
5776 198155 : lval->expr_type = EXPR_VARIABLE;
5777 198155 : lval->where = sym->declared_at;
5778 198155 : lval->ts = sym->ts;
5779 198155 : lval->symtree = gfc_find_symtree (sym->ns->sym_root, sym->name);
5780 :
5781 : /* It will always be a full array. */
5782 198155 : as = IS_CLASS_ARRAY (sym) ? CLASS_DATA (sym)->as : sym->as;
5783 198155 : lval->rank = as ? as->rank : 0;
5784 198155 : lval->corank = as ? as->corank : 0;
5785 198155 : if (lval->rank || lval->corank)
5786 44201 : gfc_add_full_array_ref (lval, as);
5787 198155 : return lval;
5788 : }
5789 :
5790 :
5791 : /* Returns the array_spec of a full array expression. A NULL is
5792 : returned otherwise. */
5793 : gfc_array_spec *
5794 26729 : gfc_get_full_arrayspec_from_expr (gfc_expr *expr)
5795 : {
5796 26729 : gfc_array_spec *as;
5797 26729 : gfc_ref *ref;
5798 :
5799 26729 : if (expr->rank == 0)
5800 : return NULL;
5801 :
5802 : /* Follow any component references. */
5803 26729 : if (expr->expr_type == EXPR_VARIABLE
5804 26729 : || expr->expr_type == EXPR_CONSTANT)
5805 : {
5806 19770 : if (expr->symtree)
5807 19770 : as = expr->symtree->n.sym->as;
5808 : else
5809 : as = NULL;
5810 :
5811 41560 : for (ref = expr->ref; ref; ref = ref->next)
5812 : {
5813 21790 : switch (ref->type)
5814 : {
5815 1845 : case REF_COMPONENT:
5816 1845 : as = ref->u.c.component->as;
5817 1845 : continue;
5818 :
5819 24 : case REF_SUBSTRING:
5820 24 : case REF_INQUIRY:
5821 24 : continue;
5822 :
5823 19921 : case REF_ARRAY:
5824 19921 : {
5825 19921 : switch (ref->u.ar.type)
5826 : {
5827 2189 : case AR_ELEMENT:
5828 2189 : case AR_SECTION:
5829 2189 : case AR_UNKNOWN:
5830 2189 : as = NULL;
5831 2189 : continue;
5832 :
5833 : case AR_FULL:
5834 : break;
5835 : }
5836 : break;
5837 : }
5838 : }
5839 : }
5840 : }
5841 : else
5842 : as = NULL;
5843 :
5844 : return as;
5845 : }
5846 :
5847 :
5848 : /* General expression traversal function. */
5849 :
5850 : bool
5851 1041723 : gfc_traverse_expr (gfc_expr *expr, gfc_symbol *sym,
5852 : bool (*func)(gfc_expr *, gfc_symbol *, int*),
5853 : int f)
5854 : {
5855 1041723 : gfc_array_ref ar;
5856 1041723 : gfc_ref *ref;
5857 1041723 : gfc_actual_arglist *args;
5858 1041723 : gfc_constructor *c;
5859 1041723 : int i;
5860 :
5861 1041723 : if (!expr)
5862 : return false;
5863 :
5864 501166 : if ((*func) (expr, sym, &f))
5865 : return true;
5866 :
5867 : /* Descend into length type parameter of character expressions only for
5868 : non-negative f. */
5869 493502 : if (f >= 0
5870 469999 : && expr->ts.type == BT_CHARACTER
5871 12314 : && expr->ts.u.cl
5872 4407 : && expr->ts.u.cl->length
5873 2570 : && expr->ts.u.cl->length->expr_type != EXPR_CONSTANT
5874 494443 : && gfc_traverse_expr (expr->ts.u.cl->length, sym, func, f))
5875 : return true;
5876 :
5877 493501 : switch (expr->expr_type)
5878 : {
5879 20342 : case EXPR_PPC:
5880 20342 : case EXPR_COMPCALL:
5881 20342 : case EXPR_FUNCTION:
5882 47062 : for (args = expr->value.function.actual; args; args = args->next)
5883 : {
5884 26829 : if (gfc_traverse_expr (args->expr, sym, func, f))
5885 : return true;
5886 : }
5887 : break;
5888 :
5889 : case EXPR_VARIABLE:
5890 : case EXPR_CONSTANT:
5891 : case EXPR_NULL:
5892 : case EXPR_SUBSTRING:
5893 : break;
5894 :
5895 5147 : case EXPR_STRUCTURE:
5896 5147 : case EXPR_ARRAY:
5897 5147 : for (c = gfc_constructor_first (expr->value.constructor);
5898 30681 : c; c = gfc_constructor_next (c))
5899 : {
5900 25534 : if (gfc_traverse_expr (c->expr, sym, func, f))
5901 : return true;
5902 25534 : if (c->iterator)
5903 : {
5904 505 : if (gfc_traverse_expr (c->iterator->var, sym, func, f))
5905 : return true;
5906 505 : if (gfc_traverse_expr (c->iterator->start, sym, func, f))
5907 : return true;
5908 505 : if (gfc_traverse_expr (c->iterator->end, sym, func, f))
5909 : return true;
5910 505 : if (gfc_traverse_expr (c->iterator->step, sym, func, f))
5911 : return true;
5912 : }
5913 : }
5914 : break;
5915 :
5916 10356 : case EXPR_OP:
5917 10356 : if (gfc_traverse_expr (expr->value.op.op1, sym, func, f))
5918 : return true;
5919 8615 : if (gfc_traverse_expr (expr->value.op.op2, sym, func, f))
5920 : return true;
5921 : break;
5922 :
5923 6 : case EXPR_CONDITIONAL:
5924 6 : if (gfc_traverse_expr (expr->value.conditional.condition, sym, func, f))
5925 : return true;
5926 6 : if (gfc_traverse_expr (expr->value.conditional.true_expr, sym, func, f))
5927 : return true;
5928 6 : if (gfc_traverse_expr (expr->value.conditional.false_expr, sym, func, f))
5929 : return true;
5930 : break;
5931 :
5932 0 : default:
5933 0 : gcc_unreachable ();
5934 491229 : break;
5935 : }
5936 :
5937 491229 : ref = expr->ref;
5938 503702 : while (ref != NULL)
5939 : {
5940 16528 : switch (ref->type)
5941 : {
5942 14641 : case REF_ARRAY:
5943 14641 : ar = ref->u.ar;
5944 178781 : for (i = 0; i < GFC_MAX_DIMENSIONS; i++)
5945 : {
5946 168017 : if (gfc_traverse_expr (ar.start[i], sym, func, f))
5947 : return true;
5948 164141 : if (gfc_traverse_expr (ar.end[i], sym, func, f))
5949 : return true;
5950 164140 : if (gfc_traverse_expr (ar.stride[i], sym, func, f))
5951 : return true;
5952 : }
5953 : break;
5954 :
5955 816 : case REF_SUBSTRING:
5956 816 : if (gfc_traverse_expr (ref->u.ss.start, sym, func, f))
5957 : return true;
5958 643 : if (gfc_traverse_expr (ref->u.ss.end, sym, func, f))
5959 : return true;
5960 : break;
5961 :
5962 1067 : case REF_COMPONENT:
5963 1067 : if (f >= 0
5964 1046 : && ref->u.c.component->ts.type == BT_CHARACTER
5965 91 : && ref->u.c.component->ts.u.cl
5966 91 : && ref->u.c.component->ts.u.cl->length
5967 91 : && ref->u.c.component->ts.u.cl->length->expr_type
5968 : != EXPR_CONSTANT
5969 1067 : && gfc_traverse_expr (ref->u.c.component->ts.u.cl->length,
5970 : sym, func, f))
5971 : return true;
5972 :
5973 1067 : if (ref->u.c.component->as)
5974 472 : for (i = 0; i < ref->u.c.component->as->rank
5975 894 : + ref->u.c.component->as->corank; i++)
5976 : {
5977 472 : if (gfc_traverse_expr (ref->u.c.component->as->lower[i],
5978 : sym, func, f))
5979 : return true;
5980 472 : if (gfc_traverse_expr (ref->u.c.component->as->upper[i],
5981 : sym, func, f))
5982 : return true;
5983 : }
5984 : break;
5985 :
5986 : case REF_INQUIRY:
5987 : return false;
5988 :
5989 0 : default:
5990 0 : gcc_unreachable ();
5991 : }
5992 12473 : ref = ref->next;
5993 : }
5994 : return false;
5995 : }
5996 :
5997 : /* Traverse expr, marking all EXPR_VARIABLE symbols referenced. */
5998 :
5999 : static bool
6000 3999 : expr_set_symbols_referenced (gfc_expr *expr,
6001 : gfc_symbol *sym ATTRIBUTE_UNUSED,
6002 : int *f ATTRIBUTE_UNUSED)
6003 : {
6004 3999 : if (expr->expr_type != EXPR_VARIABLE)
6005 : return false;
6006 941 : gfc_set_sym_referenced (expr->symtree->n.sym);
6007 941 : return false;
6008 : }
6009 :
6010 : void
6011 1328 : gfc_expr_set_symbols_referenced (gfc_expr *expr)
6012 : {
6013 1328 : gfc_traverse_expr (expr, NULL, expr_set_symbols_referenced, 0);
6014 1328 : }
6015 :
6016 :
6017 : /* Determine if an expression is a procedure pointer component and return
6018 : the component in that case. Otherwise return NULL. */
6019 :
6020 : gfc_component *
6021 3481584 : gfc_get_proc_ptr_comp (gfc_expr *expr)
6022 : {
6023 3481584 : gfc_ref *ref;
6024 :
6025 3481584 : if (!expr || !expr->ref)
6026 : return NULL;
6027 :
6028 : ref = expr->ref;
6029 313381 : while (ref->next)
6030 : ref = ref->next;
6031 :
6032 282350 : if (ref->type == REF_COMPONENT
6033 24978 : && ref->u.c.component->attr.proc_pointer)
6034 9489 : return ref->u.c.component;
6035 :
6036 : return NULL;
6037 : }
6038 :
6039 :
6040 : /* Determine if an expression is a procedure pointer component. */
6041 :
6042 : bool
6043 1262724 : gfc_is_proc_ptr_comp (gfc_expr *expr)
6044 : {
6045 1262724 : return (gfc_get_proc_ptr_comp (expr) != NULL);
6046 : }
6047 :
6048 :
6049 : /* Determine if an expression is a function with an allocatable class scalar
6050 : result. */
6051 : bool
6052 415568 : gfc_is_alloc_class_scalar_function (gfc_expr *expr)
6053 : {
6054 415568 : if (expr->expr_type == EXPR_FUNCTION
6055 76727 : && ((expr->value.function.esym
6056 42779 : && expr->value.function.esym->result
6057 42778 : && expr->value.function.esym->result->ts.type == BT_CLASS
6058 1091 : && !CLASS_DATA (expr->value.function.esym->result)->attr.dimension
6059 958 : && CLASS_DATA (expr->value.function.esym->result)->attr.allocatable)
6060 76045 : || (expr->ts.type == BT_CLASS
6061 764 : && CLASS_DATA (expr)->attr.allocatable
6062 397 : && !CLASS_DATA (expr)->attr.dimension)))
6063 922 : return true;
6064 :
6065 : return false;
6066 : }
6067 :
6068 :
6069 : /* Determine if an expression is a function with an allocatable class array
6070 : result. */
6071 : bool
6072 177069 : gfc_is_class_array_function (gfc_expr *expr)
6073 : {
6074 177069 : if (expr->expr_type == EXPR_FUNCTION
6075 84363 : && expr->value.function.esym
6076 46666 : && expr->value.function.esym->result
6077 46665 : && expr->value.function.esym->result->ts.type == BT_CLASS
6078 2493 : && CLASS_DATA (expr->value.function.esym->result)->attr.dimension
6079 1572 : && (CLASS_DATA (expr->value.function.esym->result)->attr.allocatable
6080 324 : || CLASS_DATA (expr->value.function.esym->result)->attr.pointer))
6081 1572 : return true;
6082 :
6083 : return false;
6084 : }
6085 :
6086 :
6087 : /* Walk an expression tree and check each variable encountered for being typed.
6088 : If strict is not set, a top-level variable is tolerated untyped in -std=gnu
6089 : mode as is a basic arithmetic expression using those; this is for things in
6090 : legacy-code like:
6091 :
6092 : INTEGER :: arr(n), n
6093 : INTEGER :: arr(n + 1), n
6094 :
6095 : The namespace is needed for IMPLICIT typing. */
6096 :
6097 : static gfc_namespace* check_typed_ns;
6098 :
6099 : static bool
6100 84846 : expr_check_typed_help (gfc_expr* e, gfc_symbol* sym ATTRIBUTE_UNUSED,
6101 : int* f ATTRIBUTE_UNUSED)
6102 : {
6103 84846 : bool t;
6104 :
6105 84846 : if (e->expr_type != EXPR_VARIABLE)
6106 : return false;
6107 :
6108 2607 : gcc_assert (e->symtree);
6109 2607 : t = gfc_check_symbol_typed (e->symtree->n.sym, check_typed_ns,
6110 : true, e->where);
6111 :
6112 2607 : return (!t);
6113 : }
6114 :
6115 : bool
6116 90607 : gfc_expr_check_typed (gfc_expr* e, gfc_namespace* ns, bool strict)
6117 : {
6118 92519 : bool error_found;
6119 :
6120 : /* If this is a top-level variable or EXPR_OP, do the check with strict given
6121 : to us. */
6122 92519 : if (!strict)
6123 : {
6124 92105 : if (e->expr_type == EXPR_VARIABLE && !e->ref)
6125 9639 : return gfc_check_symbol_typed (e->symtree->n.sym, ns, strict, e->where);
6126 :
6127 82466 : if (e->expr_type == EXPR_OP)
6128 : {
6129 2423 : bool t = true;
6130 :
6131 2423 : gcc_assert (e->value.op.op1);
6132 2423 : t = gfc_expr_check_typed (e->value.op.op1, ns, strict);
6133 :
6134 2423 : if (t && e->value.op.op2)
6135 : t = gfc_expr_check_typed (e->value.op.op2, ns, strict);
6136 :
6137 : return t;
6138 : }
6139 : }
6140 :
6141 : /* Otherwise, walk the expression and do it strictly. */
6142 80457 : check_typed_ns = ns;
6143 80457 : error_found = gfc_traverse_expr (e, NULL, &expr_check_typed_help, 0);
6144 :
6145 80457 : return !error_found;
6146 : }
6147 :
6148 :
6149 : /* This function returns true if it contains any references to PDT KIND
6150 : or LEN parameters. */
6151 :
6152 : static bool
6153 185638 : derived_parameter_expr (gfc_expr* e, gfc_symbol* sym ATTRIBUTE_UNUSED,
6154 : int* f ATTRIBUTE_UNUSED)
6155 : {
6156 185638 : if (e->expr_type != EXPR_VARIABLE)
6157 : return false;
6158 :
6159 3990 : gcc_assert (e->symtree);
6160 3990 : if (e->symtree->n.sym->attr.pdt_kind
6161 3180 : || e->symtree->n.sym->attr.pdt_len)
6162 1433 : return true;
6163 :
6164 : return false;
6165 : }
6166 :
6167 :
6168 : bool
6169 153034 : gfc_derived_parameter_expr (gfc_expr *e)
6170 : {
6171 153034 : return gfc_traverse_expr (e, NULL, &derived_parameter_expr, 0);
6172 : }
6173 :
6174 :
6175 : /* This function returns the overall type of a type parameter spec list.
6176 : If all the specs are explicit, SPEC_EXPLICIT is returned. If any of the
6177 : parameters are assumed/deferred then SPEC_ASSUMED/DEFERRED is returned
6178 : unless derived is not NULL. In this latter case, all the LEN parameters
6179 : must be either assumed or deferred for the return argument to be set to
6180 : anything other than SPEC_EXPLICIT. */
6181 :
6182 : gfc_param_spec_type
6183 390 : gfc_spec_list_type (gfc_actual_arglist *param_list, gfc_symbol *derived)
6184 : {
6185 390 : gfc_param_spec_type res = SPEC_EXPLICIT;
6186 390 : gfc_component *c;
6187 390 : bool seen_assumed = false;
6188 390 : bool seen_deferred = false;
6189 390 : bool seen_len = false;
6190 :
6191 390 : if (derived == NULL)
6192 : {
6193 0 : for (; param_list; param_list = param_list->next)
6194 0 : if (param_list->spec_type == SPEC_ASSUMED
6195 0 : || param_list->spec_type == SPEC_DEFERRED)
6196 : return param_list->spec_type;
6197 : }
6198 : else
6199 : {
6200 863 : for (; param_list; param_list = param_list->next)
6201 : {
6202 537 : c = gfc_find_component (derived, param_list->name,
6203 : true, true, NULL);
6204 537 : gcc_assert (c != NULL);
6205 537 : if (c->attr.pdt_kind)
6206 250 : continue;
6207 287 : else if (param_list->spec_type == SPEC_EXPLICIT)
6208 : return SPEC_EXPLICIT;
6209 223 : seen_assumed = param_list->spec_type == SPEC_ASSUMED;
6210 223 : seen_deferred = param_list->spec_type == SPEC_DEFERRED;
6211 223 : if (c->attr.pdt_len)
6212 223 : seen_len = true;
6213 : if (seen_assumed && seen_deferred)
6214 : return SPEC_EXPLICIT;
6215 : }
6216 326 : res = (seen_assumed || !seen_len) ? SPEC_ASSUMED : SPEC_DEFERRED;
6217 : }
6218 : return res;
6219 : }
6220 :
6221 :
6222 : bool
6223 30124 : gfc_ref_this_image (gfc_ref *ref)
6224 : {
6225 30124 : int n;
6226 :
6227 30124 : gcc_assert (ref->type == REF_ARRAY && ref->u.ar.codimen > 0);
6228 :
6229 65539 : for (n = ref->u.ar.dimen; n < ref->u.ar.dimen + ref->u.ar.codimen; n++)
6230 39340 : if (ref->u.ar.dimen_type[n] != DIMEN_THIS_IMAGE)
6231 : return false;
6232 :
6233 : return true;
6234 : }
6235 :
6236 : gfc_expr *
6237 2558 : gfc_find_team_co (gfc_expr *e, enum gfc_array_ref_team_type req_team_type)
6238 : {
6239 2558 : gfc_ref *ref;
6240 :
6241 3804 : for (ref = e->ref; ref; ref = ref->next)
6242 1280 : if (ref->type == REF_ARRAY && ref->u.ar.codimen > 0
6243 1280 : && ref->u.ar.team_type == req_team_type)
6244 34 : return ref->u.ar.team;
6245 :
6246 2524 : if (e->expr_type == EXPR_FUNCTION && e->value.function.actual->expr)
6247 2570 : for (ref = e->value.function.actual->expr->ref; ref;
6248 1292 : ref = ref->next)
6249 1306 : if (ref->type == REF_ARRAY && ref->u.ar.codimen > 0
6250 1278 : && ref->u.ar.team_type == req_team_type)
6251 14 : return ref->u.ar.team;
6252 :
6253 : return NULL;
6254 : }
6255 :
6256 : gfc_expr *
6257 1279 : gfc_find_stat_co (gfc_expr *e)
6258 : {
6259 1279 : gfc_ref *ref;
6260 :
6261 1279 : for (ref = e->ref; ref; ref = ref->next)
6262 640 : if (ref->type == REF_ARRAY && ref->u.ar.codimen > 0)
6263 640 : return ref->u.ar.stat;
6264 :
6265 639 : if (e->value.function.actual->expr)
6266 653 : for (ref = e->value.function.actual->expr->ref; ref;
6267 14 : ref = ref->next)
6268 653 : if (ref->type == REF_ARRAY && ref->u.ar.codimen > 0)
6269 639 : return ref->u.ar.stat;
6270 :
6271 : return NULL;
6272 : }
6273 :
6274 : bool
6275 887076 : gfc_is_coindexed (gfc_expr *e)
6276 : {
6277 887076 : gfc_ref *ref;
6278 :
6279 887076 : if (e->expr_type == EXPR_FUNCTION && e->value.function.isym
6280 532 : && e->value.function.isym->id == GFC_ISYM_CAF_GET)
6281 0 : e = e->value.function.actual->expr;
6282 :
6283 1323434 : for (ref = e->ref; ref; ref = ref->next)
6284 459841 : if (ref->type == REF_ARRAY && ref->u.ar.codimen > 0)
6285 23483 : return !gfc_ref_this_image (ref);
6286 :
6287 : return false;
6288 : }
6289 :
6290 :
6291 : /* Coarrays are variables with a corank but not being coindexed. However, also
6292 : the following is a coarray: A subobject of a coarray is a coarray if it does
6293 : not have any cosubscripts, vector subscripts, allocatable component
6294 : selection, or pointer component selection. (F2008, 2.4.7) */
6295 :
6296 : bool
6297 180799 : gfc_is_coarray (gfc_expr *e)
6298 : {
6299 180799 : gfc_ref *ref;
6300 180799 : gfc_symbol *sym;
6301 180799 : gfc_component *comp;
6302 180799 : bool coindexed;
6303 180799 : bool coarray;
6304 180799 : int i;
6305 :
6306 180799 : if (e->expr_type != EXPR_VARIABLE)
6307 : return false;
6308 :
6309 177992 : coindexed = false;
6310 177992 : sym = e->symtree->n.sym;
6311 :
6312 177992 : if (sym->ts.type == BT_CLASS && sym->attr.class_ok)
6313 18652 : coarray = CLASS_DATA (sym)->attr.codimension;
6314 : else
6315 159340 : coarray = sym->attr.codimension;
6316 :
6317 378312 : for (ref = e->ref; ref; ref = ref->next)
6318 200320 : switch (ref->type)
6319 : {
6320 28544 : case REF_COMPONENT:
6321 28544 : comp = ref->u.c.component;
6322 28544 : if (comp->ts.type == BT_CLASS && comp->attr.class_ok
6323 2772 : && (CLASS_DATA (comp)->attr.class_pointer
6324 2437 : || CLASS_DATA (comp)->attr.allocatable))
6325 : {
6326 2772 : coindexed = false;
6327 2772 : coarray = CLASS_DATA (comp)->attr.codimension;
6328 : }
6329 25772 : else if (comp->attr.pointer || comp->attr.allocatable)
6330 : {
6331 23734 : coindexed = false;
6332 23734 : coarray = comp->attr.codimension;
6333 : }
6334 : break;
6335 :
6336 171290 : case REF_ARRAY:
6337 171290 : if (!coarray)
6338 : break;
6339 :
6340 6694 : if (ref->u.ar.codimen > 0 && !gfc_ref_this_image (ref))
6341 : {
6342 : coindexed = true;
6343 : break;
6344 : }
6345 :
6346 10387 : for (i = 0; i < ref->u.ar.dimen; i++)
6347 4325 : if (ref->u.ar.dimen_type[i] == DIMEN_VECTOR)
6348 : {
6349 : coarray = false;
6350 : break;
6351 : }
6352 : break;
6353 :
6354 : case REF_SUBSTRING:
6355 : case REF_INQUIRY:
6356 : break;
6357 : }
6358 :
6359 177992 : return coarray && !coindexed;
6360 : }
6361 :
6362 :
6363 : /* Check whether the expression has an ultimate allocatable component.
6364 : Being itself allocatable does not count. */
6365 : bool
6366 344 : gfc_has_ultimate_allocatable (gfc_expr *e)
6367 : {
6368 344 : gfc_ref *ref, *last = NULL;
6369 :
6370 344 : if (e->expr_type != EXPR_VARIABLE)
6371 : return false;
6372 :
6373 589 : for (ref = e->ref; ref; ref = ref->next)
6374 245 : if (ref->type == REF_COMPONENT)
6375 10 : last = ref;
6376 :
6377 344 : if (last && last->u.c.component->ts.type == BT_CLASS)
6378 0 : return CLASS_DATA (last->u.c.component)->attr.alloc_comp;
6379 9 : else if (last && last->u.c.component->ts.type == BT_DERIVED)
6380 1 : return last->u.c.component->ts.u.derived->attr.alloc_comp;
6381 335 : else if (last)
6382 : return false;
6383 :
6384 335 : if (e->ts.type == BT_CLASS)
6385 4 : return CLASS_DATA (e)->attr.alloc_comp;
6386 331 : else if (e->ts.type == BT_DERIVED)
6387 147 : return e->ts.u.derived->attr.alloc_comp;
6388 : else
6389 : return false;
6390 : }
6391 :
6392 :
6393 : /* Check whether the expression has an pointer component.
6394 : Being itself a pointer does not count. */
6395 : bool
6396 451 : gfc_has_ultimate_pointer (gfc_expr *e)
6397 : {
6398 451 : gfc_ref *ref, *last = NULL;
6399 :
6400 451 : if (e->expr_type != EXPR_VARIABLE)
6401 : return false;
6402 :
6403 1150 : for (ref = e->ref; ref; ref = ref->next)
6404 699 : if (ref->type == REF_COMPONENT)
6405 156 : last = ref;
6406 :
6407 451 : if (last && last->u.c.component->ts.type == BT_CLASS)
6408 0 : return CLASS_DATA (last->u.c.component)->attr.pointer_comp;
6409 141 : else if (last && last->u.c.component->ts.type == BT_DERIVED)
6410 4 : return last->u.c.component->ts.u.derived->attr.pointer_comp;
6411 310 : else if (last)
6412 : return false;
6413 :
6414 310 : if (e->ts.type == BT_CLASS)
6415 2 : return CLASS_DATA (e)->attr.pointer_comp;
6416 308 : else if (e->ts.type == BT_DERIVED)
6417 6 : return e->ts.u.derived->attr.pointer_comp;
6418 : else
6419 : return false;
6420 : }
6421 :
6422 :
6423 : /* Check whether an expression is "simply contiguous", cf. F2008, 6.5.4.
6424 : Note: A scalar is not regarded as "simply contiguous" by the standard.
6425 : if bool is not strict, some further checks are done - for instance,
6426 : a "(::1)" is accepted. */
6427 :
6428 : bool
6429 26187 : gfc_is_simply_contiguous (gfc_expr *expr, bool strict, bool permit_element)
6430 : {
6431 26187 : bool colon;
6432 26187 : int i;
6433 26187 : gfc_array_ref *ar = NULL;
6434 26187 : gfc_ref *ref, *part_ref = NULL;
6435 26187 : gfc_symbol *sym;
6436 :
6437 26187 : if (expr->expr_type == EXPR_ARRAY)
6438 : return true;
6439 :
6440 25915 : if (expr->expr_type == EXPR_NULL)
6441 : {
6442 : /* F2018:16.9.144 NULL ([MOLD]):
6443 : "If MOLD is present, the characteristics are the same as MOLD."
6444 : "If MOLD is absent, the characteristics of the result are
6445 : determined by the entity with which the reference is associated."
6446 : F2018:15.3.2.2 characteristics attributes include CONTIGUOUS. */
6447 7 : if (expr->ts.type == BT_UNKNOWN)
6448 : return true;
6449 : else
6450 6 : return (gfc_variable_attr (expr).contiguous
6451 12 : || gfc_variable_attr (expr).allocatable);
6452 : }
6453 :
6454 25908 : if (expr->expr_type == EXPR_FUNCTION)
6455 : {
6456 366 : if (expr->value.function.isym)
6457 : /* TRANSPOSE is the only intrinsic that may return a
6458 : non-contiguous array. It's treated as a special case in
6459 : gfc_conv_expr_descriptor too. */
6460 328 : return (expr->value.function.isym->id != GFC_ISYM_TRANSPOSE);
6461 38 : else if (expr->value.function.esym)
6462 : /* Only a pointer to an array without the contiguous attribute
6463 : can be non-contiguous as a result value. */
6464 36 : return (expr->value.function.esym->result->attr.contiguous
6465 36 : || !expr->value.function.esym->result->attr.pointer);
6466 : else
6467 : {
6468 : /* Type-bound procedures. */
6469 2 : gfc_symbol *s = expr->symtree->n.sym;
6470 2 : if (s->ts.type != BT_CLASS && s->ts.type != BT_DERIVED)
6471 : return false;
6472 :
6473 2 : gfc_ref *rc = NULL;
6474 7 : for (gfc_ref *r = expr->ref; r; r = r->next)
6475 5 : if (r->type == REF_COMPONENT)
6476 5 : rc = r;
6477 :
6478 2 : if (rc == NULL || rc->u.c.component == NULL
6479 2 : || rc->u.c.component->ts.interface == NULL)
6480 : return false;
6481 :
6482 2 : return rc->u.c.component->ts.interface->attr.contiguous;
6483 : }
6484 : }
6485 25542 : else if (expr->expr_type != EXPR_VARIABLE)
6486 : return false;
6487 :
6488 25489 : if (!permit_element && expr->rank == 0)
6489 : return false;
6490 :
6491 25488 : sym = expr->symtree->n.sym;
6492 25488 : const gfc_typespec *prev_ts = &sym->ts;
6493 54747 : for (ref = expr->ref; ref; ref = ref->next)
6494 : {
6495 29366 : if (ar)
6496 : return false; /* Array shall be last part-ref. */
6497 :
6498 29274 : if (ref->type == REF_COMPONENT)
6499 : {
6500 : /* If the reference is to a class container's _data component, keep
6501 : part_ref pointing to the class container, so that the attributes of
6502 : both the class container and its _data component can be queried
6503 : from part_ref. */
6504 3437 : if (!(prev_ts->type == BT_CLASS
6505 235 : && prev_ts->u.derived->attr.is_class
6506 235 : && strcmp (ref->u.c.component->name, "_data") == 0))
6507 3288 : part_ref = ref;
6508 3437 : prev_ts = &ref->u.c.component->ts;
6509 : }
6510 25837 : else if (ref->type == REF_SUBSTRING)
6511 : return false;
6512 25830 : else if (ref->type == REF_INQUIRY)
6513 : return false;
6514 25822 : else if (ref->u.ar.type != AR_ELEMENT)
6515 24834 : ar = &ref->u.ar;
6516 : }
6517 :
6518 25381 : if ((part_ref
6519 2649 : && part_ref->u.c.component
6520 2649 : && !part_ref->u.c.component->attr.contiguous
6521 2640 : && IS_POINTER (part_ref->u.c.component))
6522 : || (!part_ref
6523 22732 : && !sym->attr.contiguous
6524 : /* Associate names may be associated with a simply contiguous target
6525 : without having the contiguous attribute set. Skip them here, and
6526 : handle them below. */
6527 16359 : && !sym->attr.associate_var
6528 16067 : && (IS_POINTER (sym)
6529 14126 : || (sym->ts.type == BT_CLASS
6530 134 : && sym->ts.u.derived->attr.is_class
6531 134 : && CLASS_DATA (sym)->as
6532 134 : && (CLASS_DATA (sym)->as->type == AS_ASSUMED_RANK
6533 134 : || CLASS_DATA (sym)->as->type == AS_ASSUMED_SHAPE))
6534 14054 : || (sym->as
6535 13992 : && (sym->as->type == AS_ASSUMED_RANK
6536 13564 : || sym->as->type == AS_ASSUMED_SHAPE)))))
6537 : return false;
6538 :
6539 : /* An associate variable may point to a non-contiguous target. */
6540 19228 : if (!part_ref
6541 19228 : && sym->attr.associate_var && !sym->attr.contiguous
6542 292 : && sym->assoc
6543 292 : && sym->assoc->target
6544 292 : && !gfc_is_simply_contiguous (sym->assoc->target, strict,
6545 : permit_element))
6546 : return false;
6547 :
6548 20598 : if (!ar || ar->type == AR_FULL)
6549 : return true;
6550 :
6551 6824 : gcc_assert (ar->type == AR_SECTION);
6552 :
6553 : /* Check for simply contiguous array */
6554 : colon = true;
6555 13122 : for (i = 0; i < ar->dimen; i++)
6556 : {
6557 7576 : if (ar->dimen_type[i] == DIMEN_VECTOR)
6558 : return false;
6559 :
6560 7576 : if (ar->dimen_type[i] == DIMEN_ELEMENT)
6561 : {
6562 27 : colon = false;
6563 27 : continue;
6564 : }
6565 :
6566 7549 : gcc_assert (ar->dimen_type[i] == DIMEN_RANGE);
6567 :
6568 :
6569 : /* If the previous section was not contiguous, that's an error,
6570 : unless we have effective only one element and checking is not
6571 : strict. */
6572 7549 : if (!colon && (strict || !ar->start[i] || !ar->end[i]
6573 95 : || ar->start[i]->expr_type != EXPR_CONSTANT
6574 93 : || ar->end[i]->expr_type != EXPR_CONSTANT
6575 51 : || mpz_cmp (ar->start[i]->value.integer,
6576 51 : ar->end[i]->value.integer) != 0))
6577 : return false;
6578 :
6579 : /* Following the standard, "(::1)" or - if known at compile time -
6580 : "(lbound:ubound)" are not simply contiguous; if strict
6581 : is false, they are regarded as simply contiguous. */
6582 7349 : if (ar->stride[i] && (strict || ar->stride[i]->expr_type != EXPR_CONSTANT
6583 1075 : || ar->stride[i]->ts.type != BT_INTEGER
6584 1075 : || mpz_cmp_si (ar->stride[i]->value.integer, 1) != 0))
6585 : return false;
6586 :
6587 6271 : if (ar->start[i]
6588 4075 : && (strict || ar->start[i]->expr_type != EXPR_CONSTANT
6589 4015 : || !ar->as->lower[i]
6590 2208 : || ar->as->lower[i]->expr_type != EXPR_CONSTANT
6591 2208 : || mpz_cmp (ar->start[i]->value.integer,
6592 2208 : ar->as->lower[i]->value.integer) != 0))
6593 6271 : colon = false;
6594 :
6595 6271 : if (ar->end[i]
6596 4096 : && (strict || ar->end[i]->expr_type != EXPR_CONSTANT
6597 3577 : || !ar->as->upper[i]
6598 2066 : || ar->as->upper[i]->expr_type != EXPR_CONSTANT
6599 2066 : || mpz_cmp (ar->end[i]->value.integer,
6600 2066 : ar->as->upper[i]->value.integer) != 0))
6601 6298 : colon = false;
6602 : }
6603 :
6604 : return true;
6605 : }
6606 :
6607 : /* Return true if the expression is guaranteed to be non-contiguous,
6608 : false if we cannot prove anything. It is probably best to call
6609 : this after gfc_is_simply_contiguous. If neither of them returns
6610 : true, we cannot say (at compile-time). */
6611 :
6612 : bool
6613 2819 : gfc_is_not_contiguous (gfc_expr *array)
6614 : {
6615 2819 : int i;
6616 2819 : gfc_array_ref *ar = NULL;
6617 2819 : gfc_ref *ref;
6618 2819 : bool previous_incomplete;
6619 :
6620 7028 : for (ref = array->ref; ref; ref = ref->next)
6621 : {
6622 : /* Array-ref shall be last ref. */
6623 :
6624 4269 : if (ar && ar->type != AR_ELEMENT)
6625 : return true;
6626 :
6627 4209 : if (ref->type == REF_ARRAY)
6628 2849 : ar = &ref->u.ar;
6629 : }
6630 :
6631 2759 : if (ar == NULL || ar->type != AR_SECTION)
6632 : return false;
6633 :
6634 : previous_incomplete = false;
6635 :
6636 : /* Check if we can prove that the array is not contiguous. */
6637 :
6638 1749 : for (i = 0; i < ar->dimen; i++)
6639 : {
6640 977 : mpz_t arr_size, ref_size;
6641 :
6642 977 : if (gfc_ref_dimen_size (ar, i, &ref_size, NULL))
6643 : {
6644 425 : if (gfc_dep_difference (ar->as->upper[i], ar->as->lower[i], &arr_size))
6645 : {
6646 : /* a(2:4,2:) is known to be non-contiguous, but
6647 : a(2:4,i:i) can be contiguous. */
6648 67 : mpz_add_ui (arr_size, arr_size, 1L);
6649 67 : if (previous_incomplete && mpz_cmp_si (ref_size, 1) != 0)
6650 : {
6651 6 : mpz_clear (arr_size);
6652 6 : mpz_clear (ref_size);
6653 19 : return true;
6654 : }
6655 61 : else if (mpz_cmp (arr_size, ref_size) != 0)
6656 34 : previous_incomplete = true;
6657 :
6658 61 : mpz_clear (arr_size);
6659 : }
6660 :
6661 : /* Check for a(::2), i.e. where the stride is not unity.
6662 : This is only done if there is more than one element in
6663 : the reference along this dimension. */
6664 :
6665 419 : if (mpz_cmp_ui (ref_size, 1) > 0 && ar->type == AR_SECTION
6666 413 : && ar->dimen_type[i] == DIMEN_RANGE
6667 413 : && ar->stride[i] && ar->stride[i]->expr_type == EXPR_CONSTANT
6668 21 : && mpz_cmp_si (ar->stride[i]->value.integer, 1) != 0)
6669 : {
6670 13 : mpz_clear (ref_size);
6671 13 : return true;
6672 : }
6673 :
6674 406 : mpz_clear (ref_size);
6675 : }
6676 : }
6677 : /* We didn't find anything definitive. */
6678 : return false;
6679 : }
6680 :
6681 : /* Build call to an intrinsic procedure. The number of arguments has to be
6682 : passed (rather than ending the list with a NULL value) because we may
6683 : want to add arguments but with a NULL-expression. */
6684 :
6685 : gfc_expr*
6686 23835 : gfc_build_intrinsic_call (gfc_namespace *ns, gfc_isym_id id, const char* name,
6687 : locus where, unsigned numarg, ...)
6688 : {
6689 23835 : gfc_expr* result;
6690 23835 : gfc_actual_arglist* atail;
6691 23835 : gfc_intrinsic_sym* isym;
6692 23835 : va_list ap;
6693 23835 : unsigned i;
6694 23835 : const char *mangled_name = gfc_get_string (GFC_PREFIX ("%s"), name);
6695 :
6696 23835 : isym = gfc_intrinsic_function_by_id (id);
6697 23835 : gcc_assert (isym);
6698 :
6699 23835 : result = gfc_get_expr ();
6700 23835 : result->expr_type = EXPR_FUNCTION;
6701 23835 : result->ts = isym->ts;
6702 23835 : result->where = where;
6703 23835 : result->value.function.name = mangled_name;
6704 23835 : result->value.function.isym = isym;
6705 :
6706 23835 : gfc_get_sym_tree (mangled_name, ns, &result->symtree, false);
6707 23835 : gfc_commit_symbol (result->symtree->n.sym);
6708 23835 : gcc_assert (result->symtree
6709 : && (result->symtree->n.sym->attr.flavor == FL_PROCEDURE
6710 : || result->symtree->n.sym->attr.flavor == FL_UNKNOWN));
6711 23835 : result->symtree->n.sym->intmod_sym_id = id;
6712 23835 : result->symtree->n.sym->attr.flavor = FL_PROCEDURE;
6713 23835 : result->symtree->n.sym->attr.intrinsic = 1;
6714 23835 : result->symtree->n.sym->attr.artificial = 1;
6715 :
6716 23835 : va_start (ap, numarg);
6717 23835 : atail = NULL;
6718 81654 : for (i = 0; i < numarg; ++i)
6719 : {
6720 57819 : if (atail)
6721 : {
6722 33984 : atail->next = gfc_get_actual_arglist ();
6723 33984 : atail = atail->next;
6724 : }
6725 : else
6726 23835 : atail = result->value.function.actual = gfc_get_actual_arglist ();
6727 :
6728 57819 : atail->expr = va_arg (ap, gfc_expr*);
6729 : }
6730 23835 : va_end (ap);
6731 :
6732 23835 : return result;
6733 : }
6734 :
6735 :
6736 : /* Check if a symbol referenced in a submodule is declared in the ancestor
6737 : module and not accessed by use-association, and that the submodule is a
6738 : descendant. */
6739 :
6740 : static bool
6741 4 : sym_is_from_ancestor (gfc_symbol *sym)
6742 : {
6743 4 : const char dot[2] = ".";
6744 : /* Symbols take the form module.submodule_ or module.name_. */
6745 4 : char ancestor_module[2 * GFC_MAX_SYMBOL_LEN + 2];
6746 4 : char *ancestor;
6747 :
6748 4 : if (sym == NULL
6749 4 : || sym->attr.use_assoc
6750 4 : || !sym->attr.used_in_submodule
6751 4 : || !sym->module
6752 4 : || !sym->ns->proc_name
6753 4 : || !sym->ns->proc_name->name)
6754 : return false;
6755 :
6756 4 : memset (ancestor_module, '\0', sizeof (ancestor_module));
6757 4 : strcpy (ancestor_module, sym->ns->proc_name->name);
6758 4 : ancestor = strtok (ancestor_module, dot);
6759 4 : return strcmp (ancestor, sym->module) == 0;
6760 : }
6761 :
6762 :
6763 : /* Check if an expression may appear in a variable definition context
6764 : (F2008, 16.6.7) or pointer association context (F2008, 16.6.8).
6765 : This is called from the various places when resolving
6766 : the pieces that make up such a context.
6767 : If own_scope is true (applies to, e.g., ac-implied-do/data-implied-do
6768 : variables), some checks are not performed.
6769 :
6770 : Optionally, a possible error message can be suppressed if context is NULL
6771 : and just the return status (true / false) be requested. */
6772 :
6773 : bool
6774 422844 : gfc_check_vardef_context (gfc_expr* e, bool pointer, bool alloc_obj,
6775 : bool own_scope, const char* context)
6776 : {
6777 422844 : gfc_symbol* sym = NULL;
6778 422844 : bool is_pointer;
6779 422844 : bool check_intentin;
6780 422844 : bool ptr_component;
6781 422844 : symbol_attribute attr;
6782 422844 : gfc_ref* ref;
6783 422844 : int i;
6784 :
6785 422844 : if (e->expr_type == EXPR_VARIABLE)
6786 : {
6787 422740 : gcc_assert (e->symtree);
6788 422740 : sym = e->symtree->n.sym;
6789 : }
6790 104 : else if (e->expr_type == EXPR_FUNCTION)
6791 : {
6792 48 : gcc_assert (e->symtree);
6793 48 : sym = e->value.function.esym ? e->value.function.esym : e->symtree->n.sym;
6794 : }
6795 :
6796 422844 : attr = gfc_expr_attr (e);
6797 422844 : if (!pointer && e->expr_type == EXPR_FUNCTION && attr.pointer)
6798 : {
6799 46 : if (!(gfc_option.allow_std & GFC_STD_F2008))
6800 : {
6801 1 : if (context)
6802 1 : gfc_error ("Fortran 2008: Pointer functions in variable definition"
6803 : " context (%s) at %L", context, &e->where);
6804 : return false;
6805 : }
6806 : }
6807 422798 : else if (e->expr_type != EXPR_VARIABLE)
6808 : {
6809 58 : if (context)
6810 55 : gfc_error ("Non-variable expression in variable definition context (%s)"
6811 : " at %L", context, &e->where);
6812 : return false;
6813 : }
6814 :
6815 422785 : if (!pointer && sym->attr.flavor == FL_PARAMETER)
6816 : {
6817 5 : if (context)
6818 5 : gfc_error ("Named constant %qs in variable definition context (%s)"
6819 : " at %L", sym->name, context, &e->where);
6820 : return false;
6821 : }
6822 405467 : if (!pointer && sym->attr.flavor != FL_VARIABLE
6823 11029 : && !(sym->attr.flavor == FL_PROCEDURE && sym == sym->result)
6824 595 : && !(sym->attr.flavor == FL_PROCEDURE && sym->attr.proc_pointer)
6825 33 : && !(sym->attr.flavor == FL_PROCEDURE
6826 33 : && sym->attr.function && attr.pointer))
6827 : {
6828 0 : if (context)
6829 0 : gfc_error ("%qs in variable definition context (%s) at %L is not"
6830 : " a variable", sym->name, context, &e->where);
6831 : return false;
6832 : }
6833 :
6834 : /* Find out whether the expr is a pointer; this also means following
6835 : component references to the last one. */
6836 422780 : is_pointer = (attr.pointer || attr.proc_pointer);
6837 422780 : if (pointer && !is_pointer)
6838 : {
6839 10 : if (context)
6840 5 : gfc_error ("Non-POINTER in pointer association context (%s)"
6841 : " at %L", context, &e->where);
6842 : return false;
6843 : }
6844 :
6845 422770 : if (e->ts.type == BT_DERIVED
6846 22544 : && e->ts.u.derived == NULL)
6847 : {
6848 1 : if (context)
6849 1 : gfc_error ("Type inaccessible in variable definition context (%s) "
6850 : "at %L", context, &e->where);
6851 : return false;
6852 : }
6853 :
6854 : /* F2008, C1303. */
6855 422769 : if (!alloc_obj
6856 389632 : && (attr.lock_comp
6857 389632 : || (e->ts.type == BT_DERIVED
6858 17288 : && e->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
6859 42 : && e->ts.u.derived->intmod_sym_id == ISOFORTRAN_LOCK_TYPE)))
6860 : {
6861 3 : if (context)
6862 3 : gfc_error ("LOCK_TYPE in variable definition context (%s) at %L",
6863 : context, &e->where);
6864 : return false;
6865 : }
6866 :
6867 : /* TS18508, C702/C203. */
6868 389629 : if (!alloc_obj
6869 : && (attr.lock_comp
6870 389629 : || (e->ts.type == BT_DERIVED
6871 17285 : && e->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
6872 39 : && e->ts.u.derived->intmod_sym_id == ISOFORTRAN_EVENT_TYPE)))
6873 : {
6874 0 : if (context)
6875 0 : gfc_error ("LOCK_EVENT in variable definition context (%s) at %L",
6876 : context, &e->where);
6877 : return false;
6878 : }
6879 :
6880 : /* INTENT(IN) dummy argument. Check this, unless the object itself is the
6881 : component of sub-component of a pointer; we need to distinguish
6882 : assignment to a pointer component from pointer-assignment to a pointer
6883 : component. Note that (normal) assignment to procedure pointers is not
6884 : possible. */
6885 422766 : check_intentin = !own_scope;
6886 15235 : ptr_component = (sym->ts.type == BT_CLASS && sym->ts.u.derived
6887 15235 : && CLASS_DATA (sym))
6888 438001 : ? CLASS_DATA (sym)->attr.class_pointer : sym->attr.pointer;
6889 560474 : for (ref = e->ref; ref && check_intentin; ref = ref->next)
6890 : {
6891 : /* Associate-targets need special handling. Subobjects of an object with
6892 : the PROTECTED attribute inherit this attribute. */
6893 137716 : if (ptr_component && ref->type == REF_COMPONENT
6894 2403 : && !sym->assoc && !sym->attr.is_protected)
6895 137716 : check_intentin = false;
6896 137716 : if (ref->type == REF_COMPONENT)
6897 : {
6898 33218 : gfc_component *comp = ref->u.c.component;
6899 2611 : ptr_component = (comp->ts.type == BT_CLASS && comp->attr.class_ok)
6900 35829 : ? CLASS_DATA (comp)->attr.class_pointer
6901 30607 : : comp->attr.pointer;
6902 33218 : if (ptr_component && !pointer)
6903 4454 : check_intentin = false;
6904 : }
6905 137716 : if (ref->type == REF_INQUIRY
6906 90 : && (ref->u.i == INQUIRY_KIND || ref->u.i == INQUIRY_LEN))
6907 : {
6908 8 : if (context)
6909 16 : gfc_error ("%qs parameter inquiry for %qs in "
6910 : "variable definition context (%s) at %L",
6911 : ref->u.i == INQUIRY_KIND ? "KIND" : "LEN",
6912 : sym->name, context, &e->where);
6913 : return false;
6914 : }
6915 : }
6916 :
6917 : /* See if the INTENT(IN) check should apply to an ASSOCIATE target. */
6918 422758 : if (check_intentin && sym->assoc && sym->assoc->target)
6919 : {
6920 : gfc_expr *target;
6921 : gfc_symbol *tsym;
6922 :
6923 2090 : check_intentin = false;
6924 :
6925 : /* Walk through associate target chain to find a dummy argument. */
6926 2090 : for (target = sym->assoc->target; target; target = tsym->assoc->target)
6927 : {
6928 2090 : tsym = target->symtree ? target->symtree->n.sym : NULL;
6929 :
6930 2082 : if (tsym == NULL)
6931 : break;
6932 :
6933 2082 : if (tsym->attr.dummy)
6934 : {
6935 941 : check_intentin = (tsym->attr.intent == INTENT_IN);
6936 941 : break;
6937 : }
6938 :
6939 1141 : if (tsym->assoc == NULL)
6940 : break;
6941 : }
6942 : }
6943 :
6944 411067 : if (check_intentin
6945 409197 : && (sym->attr.intent == INTENT_IN
6946 409094 : || (sym->attr.select_type_temporary && sym->assoc
6947 7 : && sym->assoc->target && sym->assoc->target->symtree
6948 7 : && sym->assoc->target->symtree->n.sym->attr.intent == INTENT_IN)))
6949 : {
6950 103 : const char *name = (sym->attr.select_type_temporary
6951 106 : ? sym->assoc->target->symtree->name : sym->name);
6952 106 : if (pointer && is_pointer)
6953 : {
6954 18 : if (context)
6955 18 : gfc_error ("Dummy argument %qs with INTENT(IN) in pointer"
6956 : " association context (%s) at %L",
6957 : name, context, &e->where);
6958 : return false;
6959 : }
6960 88 : if (!pointer && !is_pointer && !sym->attr.pointer)
6961 : {
6962 30 : if (context)
6963 17 : gfc_error ("Dummy argument %qs with INTENT(IN) in variable"
6964 : " definition context (%s) at %L",
6965 : name, context, &e->where);
6966 : return false;
6967 : }
6968 : }
6969 :
6970 : /* PROTECTED and use-associated. */
6971 422710 : if (sym->attr.is_protected
6972 263 : && (sym->attr.use_assoc
6973 201 : || (sym->attr.used_in_submodule && !sym_is_from_ancestor (sym)))
6974 63 : && !own_scope
6975 422771 : && (check_intentin || !pointer))
6976 : {
6977 61 : if (pointer && is_pointer)
6978 : {
6979 16 : if (context)
6980 16 : gfc_error ("Variable %qs is PROTECTED and cannot appear in a "
6981 : "pointer association context (%s) at %L",
6982 : sym->name, context, &e->where);
6983 : return false;
6984 : }
6985 45 : if (!pointer && !is_pointer)
6986 : {
6987 25 : if (context)
6988 24 : gfc_error ("Variable %qs is PROTECTED and cannot appear in a "
6989 : "variable definition context (%s) at %L",
6990 : sym->name, context, &e->where);
6991 : return false;
6992 : }
6993 : }
6994 :
6995 : /* Variable not assignable from a PURE procedure but appears in
6996 : variable definition context. */
6997 1253495 : own_scope = own_scope
6998 422669 : || (sym->attr.result && sym->ns->proc_name
6999 9272 : && sym == sym->ns->proc_name->result);
7000 408165 : if (!pointer && !own_scope && gfc_pure (NULL) && gfc_impure_variable (sym))
7001 : {
7002 8 : if (context)
7003 8 : gfc_error ("Variable %qs cannot appear in a variable definition"
7004 : " context (%s) at %L in PURE procedure",
7005 : sym->name, context, &e->where);
7006 : return false;
7007 : }
7008 :
7009 399869 : if (!pointer && context && gfc_implicit_pure (NULL)
7010 436212 : && gfc_impure_variable (sym))
7011 : {
7012 1220 : gfc_namespace *ns;
7013 1220 : gfc_symbol *sym;
7014 :
7015 1318 : for (ns = gfc_current_ns; ns; ns = ns->parent)
7016 : {
7017 1318 : sym = ns->proc_name;
7018 1318 : if (sym == NULL)
7019 : break;
7020 1318 : if (sym->attr.flavor == FL_PROCEDURE)
7021 : {
7022 1220 : sym->attr.implicit_pure = 0;
7023 1220 : break;
7024 : }
7025 : }
7026 : }
7027 : /* Check variable definition context for associate-names. */
7028 422661 : if ((!pointer || check_intentin)
7029 422145 : && sym->assoc && !sym->attr.select_rank_temporary)
7030 : {
7031 1391 : const char* name;
7032 1391 : gfc_association_list* assoc;
7033 :
7034 1391 : gcc_assert (sym->assoc->target);
7035 :
7036 : /* If this is a SELECT TYPE temporary (the association is used internally
7037 : for SELECT TYPE), silently go over to the target. */
7038 1391 : if (sym->attr.select_type_temporary)
7039 : {
7040 999 : gfc_expr* t = sym->assoc->target;
7041 :
7042 999 : gcc_assert (t->expr_type == EXPR_VARIABLE);
7043 999 : name = t->symtree->name;
7044 :
7045 999 : if (t->symtree->n.sym->assoc)
7046 : assoc = t->symtree->n.sym->assoc;
7047 : else
7048 881 : assoc = sym->assoc;
7049 : }
7050 : else
7051 : {
7052 392 : name = sym->name;
7053 392 : assoc = sym->assoc;
7054 : }
7055 1391 : gcc_assert (name && assoc);
7056 :
7057 : /* Is association to a valid variable? */
7058 1391 : if (!assoc->variable)
7059 : {
7060 13 : if (context)
7061 : {
7062 13 : if (assoc->target->expr_type == EXPR_VARIABLE
7063 13 : && gfc_has_vector_index (assoc->target))
7064 4 : gfc_error ("%qs at %L associated to vector-indexed target"
7065 : " cannot be used in a variable definition"
7066 : " context (%s)",
7067 : name, &e->where, context);
7068 : else
7069 9 : gfc_error ("%qs at %L associated to expression"
7070 : " cannot be used in a variable definition"
7071 : " context (%s)",
7072 : name, &e->where, context);
7073 : }
7074 : return false;
7075 : }
7076 1378 : else if (context && gfc_is_ptr_fcn (assoc->target))
7077 : {
7078 11 : if (!gfc_notify_std (GFC_STD_F2018, "%qs at %L associated to "
7079 : "pointer function target being used in a "
7080 : "variable definition context (%s)", name,
7081 : &e->where, context))
7082 : return false;
7083 7 : else if (gfc_has_vector_index (e))
7084 : {
7085 0 : gfc_error ("%qs at %L associated to vector-indexed target"
7086 : " cannot be used in a variable definition"
7087 : " context (%s)",
7088 : name, &e->where, context);
7089 0 : return false;
7090 : }
7091 : }
7092 :
7093 : /* Target must be allowed to appear in a variable definition context.
7094 : Check valid assignment to pointers and invalid reassociations. */
7095 1374 : if (!gfc_check_vardef_context (assoc->target, pointer, false, false, NULL)
7096 1374 : && (!ptr_component || pointer))
7097 : {
7098 9 : if (context)
7099 6 : gfc_error ("Associate-name %qs cannot appear in a variable"
7100 : " definition context (%s) at %L because its target"
7101 : " at %L cannot, either",
7102 : name, context, &e->where,
7103 6 : &assoc->target->where);
7104 : return false;
7105 : }
7106 : }
7107 :
7108 : /* Check for same value in vector expression subscript. */
7109 :
7110 422635 : if (e->rank > 0)
7111 164002 : for (ref = e->ref; ref != NULL; ref = ref->next)
7112 82681 : if (ref->type == REF_ARRAY && ref->u.ar.type == AR_SECTION)
7113 20794 : for (i = 0; i < GFC_MAX_DIMENSIONS
7114 32517 : && ref->u.ar.dimen_type[i] != 0; i++)
7115 20801 : if (ref->u.ar.dimen_type[i] == DIMEN_VECTOR)
7116 : {
7117 500 : gfc_expr *arr = ref->u.ar.start[i];
7118 500 : if (arr->expr_type == EXPR_ARRAY)
7119 : {
7120 265 : gfc_constructor *c, *n;
7121 265 : gfc_expr *ec, *en;
7122 :
7123 265 : for (c = gfc_constructor_first (arr->value.constructor);
7124 820 : c != NULL; c = gfc_constructor_next (c))
7125 : {
7126 562 : if (c == NULL || c->iterator != NULL)
7127 12 : continue;
7128 :
7129 550 : ec = c->expr;
7130 :
7131 909 : for (n = gfc_constructor_next (c); n != NULL;
7132 359 : n = gfc_constructor_next (n))
7133 : {
7134 366 : if (n->iterator != NULL)
7135 12 : continue;
7136 :
7137 354 : en = n->expr;
7138 354 : if (gfc_dep_compare_expr (ec, en) == 0)
7139 : {
7140 7 : if (context)
7141 7 : gfc_error_now ("Elements with the same value "
7142 : "at %L and %L in vector "
7143 : "subscript in a variable "
7144 : "definition context (%s)",
7145 : &(ec->where), &(en->where),
7146 : context);
7147 : return false;
7148 : }
7149 : }
7150 : }
7151 : }
7152 : }
7153 :
7154 : return true;
7155 : }
7156 :
7157 : gfc_expr*
7158 12 : gfc_pdt_find_component_copy_initializer (gfc_symbol *sym, const char *name)
7159 : {
7160 : /* The actual length of a pdt is in its components. In the
7161 : initializer of the current ref is only the default value.
7162 : Therefore traverse the chain of components and pick the correct
7163 : one's initializer expressions. */
7164 12 : for (gfc_component *comp = sym->ts.u.derived->components; comp != NULL;
7165 0 : comp = comp->next)
7166 : {
7167 12 : if (!strcmp (comp->name, name))
7168 12 : return gfc_copy_expr (comp->initializer);
7169 : }
7170 : return NULL;
7171 : }
7172 :
7173 :
7174 : /* Test for parameterized array or string components. */
7175 :
7176 11929 : bool has_parameterized_comps (gfc_symbol * der_type)
7177 : {
7178 11929 : bool parameterized_comps = false;
7179 :
7180 11929 : if (!der_type->attr.pdt_type && !der_type->attr.pdt_comp)
7181 : return false;
7182 :
7183 13094 : for (gfc_component *c = der_type->components; c; c = c->next)
7184 9490 : if (c->attr.pdt_array || c->attr.pdt_string)
7185 : parameterized_comps = true;
7186 7439 : else if (IS_PDT (c) && strcmp (der_type->name, c->ts.u.derived->name)
7187 524 : && has_parameterized_comps (c->ts.u.derived))
7188 : parameterized_comps = true;
7189 :
7190 : return parameterized_comps;
7191 : }
|