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 89897646 : gfc_get_expr (void)
46 : {
47 89897646 : gfc_expr *e;
48 :
49 89897646 : e = XCNEW (gfc_expr);
50 89897646 : gfc_clear_ts (&e->ts);
51 89897646 : e->shape = NULL;
52 89897646 : e->ref = NULL;
53 89897646 : e->symtree = NULL;
54 89897646 : 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 180741 : gfc_get_array_expr (bt type, int kind, locus *where)
63 : {
64 180741 : gfc_expr *e;
65 :
66 180741 : e = gfc_get_expr ();
67 180741 : e->expr_type = EXPR_ARRAY;
68 180741 : e->value.constructor = NULL;
69 180741 : e->rank = 1;
70 180741 : e->shape = NULL;
71 :
72 180741 : e->ts.type = type;
73 180741 : e->ts.kind = kind;
74 180741 : if (where)
75 179421 : e->where = *where;
76 :
77 180741 : return e;
78 : }
79 :
80 :
81 : /* Get a new expression node that is the NULL expression. */
82 :
83 : gfc_expr *
84 55992 : gfc_get_null_expr (locus *where)
85 : {
86 55992 : gfc_expr *e;
87 :
88 55992 : e = gfc_get_expr ();
89 55992 : e->expr_type = EXPR_NULL;
90 55992 : e->ts.type = BT_UNKNOWN;
91 :
92 55992 : if (where)
93 16091 : e->where = *where;
94 :
95 55992 : return e;
96 : }
97 :
98 :
99 : /* Get a new expression node that is an operator expression node. */
100 :
101 : gfc_expr *
102 1637542 : gfc_get_operator_expr (locus *where, gfc_intrinsic_op op,
103 : gfc_expr *op1, gfc_expr *op2)
104 : {
105 1637542 : gfc_expr *e;
106 :
107 1637542 : e = gfc_get_expr ();
108 1637542 : e->expr_type = EXPR_OP;
109 1637542 : e->value.op.op = op;
110 1637542 : e->value.op.op1 = op1;
111 1637542 : e->value.op.op2 = op2;
112 :
113 1637542 : if (where)
114 1637542 : e->where = *where;
115 :
116 1637542 : 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 38506 : gfc_get_structure_constructor_expr (bt type, int kind, locus *where)
144 : {
145 38506 : gfc_expr *e;
146 :
147 38506 : e = gfc_get_expr ();
148 38506 : e->expr_type = EXPR_STRUCTURE;
149 38506 : e->value.constructor = NULL;
150 :
151 38506 : e->ts.type = type;
152 38506 : e->ts.kind = kind;
153 38506 : if (where)
154 38506 : e->where = *where;
155 :
156 38506 : 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 31841680 : gfc_get_constant_expr (bt type, int kind, locus *where)
164 : {
165 31841680 : gfc_expr *e;
166 :
167 31841680 : if (!where)
168 0 : gfc_internal_error ("gfc_get_constant_expr(): locus %<where%> cannot be "
169 : "NULL");
170 :
171 31841680 : e = gfc_get_expr ();
172 :
173 31841680 : e->expr_type = EXPR_CONSTANT;
174 31841680 : e->ts.type = type;
175 31841680 : e->ts.kind = kind;
176 31841680 : e->where = *where;
177 :
178 31841680 : switch (type)
179 : {
180 30871600 : case BT_INTEGER:
181 30871600 : case BT_UNSIGNED:
182 30871600 : mpz_init (e->value.integer);
183 30871600 : break;
184 :
185 416825 : case BT_REAL:
186 416825 : gfc_set_model_kind (kind);
187 416825 : mpfr_init (e->value.real);
188 416825 : break;
189 :
190 19891 : case BT_COMPLEX:
191 19891 : gfc_set_model_kind (kind);
192 19891 : mpc_init2 (e->value.complex, mpfr_get_default_prec());
193 19891 : break;
194 :
195 : default:
196 : break;
197 : }
198 :
199 31841680 : 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 359124 : gfc_get_character_expr (int kind, locus *where, const char *src, gfc_charlen_t len)
209 : {
210 359124 : gfc_expr *e;
211 359124 : gfc_char_t *dest;
212 :
213 359124 : if (!src)
214 : {
215 357407 : dest = gfc_get_wide_string (len + 1);
216 357407 : gfc_wide_memset (dest, ' ', len);
217 357407 : dest[len] = '\0';
218 : }
219 : else
220 1717 : dest = gfc_char_to_widechar (src);
221 :
222 360927 : e = gfc_get_constant_expr (BT_CHARACTER, kind,
223 : where ? where : &gfc_current_locus);
224 359124 : e->value.character.string = dest;
225 359124 : e->value.character.length = len;
226 :
227 359124 : return e;
228 : }
229 :
230 :
231 : /* Get a new expression node that is an integer constant. */
232 :
233 : gfc_expr *
234 14532315 : gfc_get_int_expr (int kind, locus *where, HOST_WIDE_INT value)
235 : {
236 14532315 : gfc_expr *p;
237 29022548 : p = gfc_get_constant_expr (BT_INTEGER, kind,
238 : where ? where : &gfc_current_locus);
239 :
240 14532315 : const wide_int w = wi::shwi (value, kind * BITS_PER_UNIT);
241 14532315 : wi::to_mpz (w, p->value.integer, SIGNED);
242 :
243 14532315 : return p;
244 14532315 : }
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 77830 : gfc_get_logical_expr (int kind, locus *where, bool value)
264 : {
265 77830 : gfc_expr *p;
266 89218 : p = gfc_get_constant_expr (BT_LOGICAL, kind,
267 : where ? where : &gfc_current_locus);
268 :
269 77830 : p->value.logical = value;
270 :
271 77830 : return p;
272 : }
273 :
274 :
275 : gfc_expr *
276 34198 : gfc_get_iokind_expr (locus *where, io_kind k)
277 : {
278 34198 : 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 34198 : e = gfc_get_expr ();
285 34198 : e->expr_type = EXPR_CONSTANT;
286 34198 : e->ts.type = BT_LOGICAL;
287 34198 : e->value.iokind = k;
288 34198 : e->where = *where;
289 :
290 34198 : 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 57660032 : gfc_copy_expr (gfc_expr *p)
299 : {
300 57660032 : gfc_expr *q;
301 57660032 : gfc_char_t *s;
302 57660032 : char *c;
303 :
304 57660032 : if (p == NULL)
305 : return NULL;
306 :
307 48626933 : q = gfc_get_expr ();
308 48626933 : *q = *p;
309 :
310 48626933 : 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 17075791 : case EXPR_CONSTANT:
320 : /* Copy target representation, if it exists. */
321 17075791 : 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 17075791 : switch (q->ts.type)
330 : {
331 15280757 : case BT_INTEGER:
332 15280757 : case BT_UNSIGNED:
333 15280757 : mpz_init_set (q->value.integer, p->value.integer);
334 15280757 : break;
335 :
336 351777 : case BT_REAL:
337 351777 : gfc_set_model_kind (q->ts.kind);
338 351777 : mpfr_init (q->value.real);
339 351777 : mpfr_set (q->value.real, p->value.real, GFC_RND_MODE);
340 351777 : 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 313045 : case BT_CHARACTER:
349 313045 : 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 310960 : s = gfc_get_wide_string (p->value.character.length + 1);
357 310960 : q->value.character.string = s;
358 :
359 : /* This is the case for the C_NULL_CHAR named constant. */
360 310960 : 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 310960 : memcpy (s, p->value.character.string,
370 310960 : (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 16476000 : case EXPR_OP:
399 16476000 : switch (q->value.op.op)
400 : {
401 5270583 : case INTRINSIC_NOT:
402 5270583 : case INTRINSIC_PARENTHESES:
403 5270583 : case INTRINSIC_UPLUS:
404 5270583 : case INTRINSIC_UMINUS:
405 5270583 : q->value.op.op1 = gfc_copy_expr (p->value.op.op1);
406 5270583 : break;
407 :
408 11205417 : default: /* Binary operators. */
409 11205417 : q->value.op.op1 = gfc_copy_expr (p->value.op.op1);
410 11205417 : q->value.op.op2 = gfc_copy_expr (p->value.op.op2);
411 11205417 : 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 425182 : case EXPR_FUNCTION:
426 850364 : q->value.function.actual =
427 425182 : gfc_copy_actual_arglist (p->value.function.actual);
428 425182 : 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 126068 : case EXPR_STRUCTURE:
438 126068 : case EXPR_ARRAY:
439 126068 : q->value.constructor = gfc_constructor_copy (p->value.constructor);
440 126068 : 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 48626933 : q->shape = gfc_copy_shape (p->shape, p->rank);
451 :
452 48626933 : q->ref = gfc_copy_ref (p->ref);
453 :
454 48626933 : if (p->param_list)
455 2133 : q->param_list = gfc_copy_actual_arglist (p->param_list);
456 :
457 : return q;
458 : }
459 :
460 :
461 : void
462 462617 : gfc_clear_shape (mpz_t *shape, int rank)
463 : {
464 462617 : int i;
465 :
466 1057503 : for (i = 0; i < rank; i++)
467 594886 : mpz_clear (shape[i]);
468 462617 : }
469 :
470 :
471 : void
472 89647097 : gfc_free_shape (mpz_t **shape, int rank)
473 : {
474 89647097 : if (*shape == NULL)
475 : return;
476 :
477 448425 : gfc_clear_shape (*shape, rank);
478 448425 : free (*shape);
479 448425 : *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 89624745 : free_expr0 (gfc_expr *e)
490 : {
491 89624745 : switch (e->expr_type)
492 : {
493 49191892 : case EXPR_CONSTANT:
494 : /* Free any parts of the value that need freeing. */
495 49191892 : switch (e->ts.type)
496 : {
497 46389510 : case BT_INTEGER:
498 46389510 : case BT_UNSIGNED:
499 46389510 : mpz_clear (e->value.integer);
500 46389510 : break;
501 :
502 768832 : case BT_REAL:
503 768832 : mpfr_clear (e->value.real);
504 768832 : break;
505 :
506 690261 : case BT_CHARACTER:
507 690261 : free (e->value.character.string);
508 690261 : break;
509 :
510 47671 : case BT_COMPLEX:
511 47671 : mpc_clear (e->value.complex);
512 47671 : 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 49191892 : free (e->representation.string);
524 :
525 49191892 : break;
526 :
527 18149580 : case EXPR_OP:
528 18149580 : if (e->value.op.op1 != NULL)
529 1710036 : gfc_free_expr (e->value.op.op1);
530 18149580 : if (e->value.op.op2 != NULL)
531 1552382 : 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 1985379 : case EXPR_FUNCTION:
541 1985379 : gfc_free_actual_arglist (e->value.function.actual);
542 1985379 : break;
543 :
544 3769 : case EXPR_COMPCALL:
545 3769 : case EXPR_PPC:
546 3769 : gfc_free_actual_arglist (e->value.compcall.actual);
547 3769 : break;
548 :
549 : case EXPR_VARIABLE:
550 : break;
551 :
552 371716 : case EXPR_ARRAY:
553 371716 : case EXPR_STRUCTURE:
554 371716 : gfc_constructor_free (e->value.constructor);
555 371716 : 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 89624745 : gfc_free_shape (&e->shape, e->rank);
570 :
571 89624745 : gfc_free_ref_list (e->ref);
572 :
573 89624745 : gfc_free_actual_arglist (e->param_list);
574 :
575 89624745 : memset (e, '\0', sizeof (gfc_expr));
576 89624745 : }
577 :
578 :
579 : /* Free an expression node and everything beneath it. */
580 :
581 : void
582 126706412 : gfc_free_expr (gfc_expr *e)
583 : {
584 126706412 : if (e == NULL)
585 : return;
586 58605148 : free_expr0 (e);
587 58605148 : free (e);
588 : }
589 :
590 :
591 : /* Free an argument list and everything below it. */
592 :
593 : void
594 91764874 : gfc_free_actual_arglist (gfc_actual_arglist *a1)
595 : {
596 91764874 : gfc_actual_arglist *a2;
597 :
598 95094796 : while (a1)
599 : {
600 3329922 : a2 = a1->next;
601 3329922 : if (a1->expr)
602 3027312 : gfc_free_expr (a1->expr);
603 3329922 : free (a1->associated_dummy);
604 3329922 : free (a1);
605 3329922 : a1 = a2;
606 : }
607 91764874 : }
608 :
609 :
610 : /* Copy an arglist structure and all of the arguments. */
611 :
612 : gfc_actual_arglist *
613 434655 : gfc_copy_actual_arglist (gfc_actual_arglist *p)
614 : {
615 434655 : gfc_actual_arglist *head, *tail, *new_arg;
616 :
617 434655 : head = tail = NULL;
618 :
619 1239343 : for (; p; p = p->next)
620 : {
621 804688 : new_arg = gfc_get_actual_arglist ();
622 804688 : *new_arg = *p;
623 :
624 804688 : if (p->associated_dummy != NULL)
625 : {
626 695291 : new_arg->associated_dummy = gfc_get_dummy_arg ();
627 695291 : *new_arg->associated_dummy = *p->associated_dummy;
628 : }
629 :
630 804688 : new_arg->expr = gfc_copy_expr (p->expr);
631 804688 : new_arg->next = NULL;
632 :
633 804688 : if (head == NULL)
634 : head = new_arg;
635 : else
636 373834 : tail->next = new_arg;
637 :
638 804688 : tail = new_arg;
639 : }
640 :
641 434655 : return head;
642 : }
643 :
644 :
645 : /* Free a list of reference structures. */
646 :
647 : void
648 89737236 : gfc_free_ref_list (gfc_ref *p)
649 : {
650 89737236 : gfc_ref *q;
651 89737236 : int i;
652 :
653 91151586 : for (; p; p = q)
654 : {
655 1414350 : q = p->next;
656 :
657 1414350 : switch (p->type)
658 : {
659 : case REF_ARRAY:
660 16778864 : for (i = 0; i < GFC_MAX_DIMENSIONS; i++)
661 : {
662 15730185 : gfc_free_expr (p->u.ar.start[i]);
663 15730185 : gfc_free_expr (p->u.ar.end[i]);
664 15730185 : gfc_free_expr (p->u.ar.stride[i]);
665 : }
666 :
667 1048679 : gfc_free_expr (p->u.ar.stat);
668 1048679 : gfc_free_expr (p->u.ar.team);
669 1048679 : break;
670 :
671 28115 : case REF_SUBSTRING:
672 28115 : gfc_free_expr (p->u.ss.start);
673 28115 : gfc_free_expr (p->u.ss.end);
674 28115 : break;
675 :
676 : case REF_COMPONENT:
677 : case REF_INQUIRY:
678 : break;
679 : }
680 :
681 1414350 : free (p);
682 : }
683 89737236 : }
684 :
685 :
686 : /* Graft the *src expression onto the *dest subexpression. */
687 :
688 : void
689 31019163 : gfc_replace_expr (gfc_expr *dest, gfc_expr *src)
690 : {
691 31019163 : free_expr0 (dest);
692 31019163 : *dest = *src;
693 31019163 : free (src);
694 31019163 : }
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 482714 : gfc_extract_int (gfc_expr *expr, int *result, int report_error)
704 : {
705 482714 : 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 482714 : 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 482714 : if (expr->expr_type != EXPR_CONSTANT)
720 : {
721 1129 : if (report_error > 0)
722 1114 : 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 481585 : 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 481093 : if ((mpz_cmp_si (expr->value.integer, INT_MAX) > 0)
738 481093 : || (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 481093 : *result = (int) mpz_get_si (expr->value.integer);
748 :
749 481093 : return false;
750 : }
751 :
752 : /* Same as gfc_extract_int, but use a HWI. */
753 :
754 : bool
755 11220 : gfc_extract_hwi (gfc_expr *expr, HOST_WIDE_INT *result, int report_error)
756 : {
757 11220 : 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 11220 : 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 11220 : 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 11044 : 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 11044 : const wide_int val = wi::from_mpz (long_long_integer_type_node,
791 11044 : expr->value.integer, false);
792 :
793 11044 : 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 11044 : *result = val.to_shwi ();
803 :
804 11044 : return false;
805 11044 : }
806 :
807 :
808 : /* Recursively copy a list of reference structures. */
809 :
810 : gfc_ref *
811 48923108 : gfc_copy_ref (gfc_ref *src)
812 : {
813 48923108 : gfc_array_ref *ar;
814 48923108 : gfc_ref *dest;
815 :
816 48923108 : if (src == NULL)
817 : return NULL;
818 :
819 268437 : dest = gfc_get_ref ();
820 268437 : dest->type = src->type;
821 :
822 268437 : switch (src->type)
823 : {
824 192078 : case REF_ARRAY:
825 192078 : ar = gfc_copy_array_ref (&src->u.ar);
826 192078 : dest->u.ar = *ar;
827 192078 : free (ar);
828 192078 : break;
829 :
830 62585 : case REF_COMPONENT:
831 62585 : dest->u.c = src->u.c;
832 62585 : break;
833 :
834 2821 : case REF_INQUIRY:
835 2821 : dest->u.i = src->u.i;
836 2821 : break;
837 :
838 10953 : case REF_SUBSTRING:
839 10953 : dest->u.ss = src->u.ss;
840 10953 : dest->u.ss.start = gfc_copy_expr (src->u.ss.start);
841 10953 : dest->u.ss.end = gfc_copy_expr (src->u.ss.end);
842 10953 : break;
843 : }
844 :
845 268437 : dest->next = gfc_copy_ref (src->next);
846 :
847 268437 : return dest;
848 : }
849 :
850 :
851 : /* Detect whether an expression has any vector index array references. */
852 :
853 : bool
854 39745 : gfc_has_vector_index (gfc_expr *e)
855 : {
856 39745 : gfc_ref *ref;
857 39745 : int i;
858 47616 : for (ref = e->ref; ref; ref = ref->next)
859 7881 : if (ref->type == REF_ARRAY)
860 13085 : for (i = 0; i < ref->u.ar.dimen; i++)
861 7057 : if (ref->u.ar.dimen_type[i] == DIMEN_VECTOR)
862 : return 1;
863 : return 0;
864 : }
865 :
866 :
867 : bool
868 2711 : gfc_is_ptr_fcn (gfc_expr *e)
869 : {
870 2711 : return e != NULL && e->expr_type == EXPR_FUNCTION
871 3416 : && gfc_expr_attr (e).pointer;
872 : }
873 :
874 :
875 : /* Copy a shape array. */
876 :
877 : mpz_t *
878 48899959 : gfc_copy_shape (mpz_t *shape, int rank)
879 : {
880 48899959 : mpz_t *new_shape;
881 48899959 : int n;
882 :
883 48899959 : if (shape == NULL)
884 : return NULL;
885 :
886 158397 : new_shape = gfc_get_shape (rank);
887 :
888 531634 : for (n = 0; n < rank; n++)
889 214840 : 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 2994 : gfc_copy_shape_excluding (mpz_t *shape, int rank, gfc_expr *dim)
909 : {
910 2994 : mpz_t *new_shape, *s;
911 2994 : int i, n;
912 :
913 2994 : if (shape == NULL
914 2994 : || rank <= 1
915 2428 : || dim == NULL
916 2428 : || dim->expr_type != EXPR_CONSTANT
917 2155 : || dim->ts.type != BT_INTEGER)
918 : return NULL;
919 :
920 2155 : n = mpz_get_si (dim->value.integer);
921 2155 : n--; /* Convert to zero based index. */
922 2155 : if (n < 0 || n >= rank)
923 : return NULL;
924 :
925 2155 : s = new_shape = gfc_get_shape (rank - 1);
926 :
927 9193 : for (i = 0; i < rank; i++)
928 : {
929 4883 : if (i == n)
930 2155 : continue;
931 2728 : mpz_init_set (*s, shape[i]);
932 2728 : 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 97801 : gfc_kind_max (gfc_expr *e1, gfc_expr *e2)
944 : {
945 97801 : 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 25725497 : 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 25720602 : gfc_numeric_ts (gfc_typespec *ts)
963 : {
964 25720602 : 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 137860 : gfc_build_conversion (gfc_expr *e)
974 : {
975 137860 : gfc_expr *p;
976 :
977 137860 : p = gfc_get_expr ();
978 137860 : p->expr_type = EXPR_FUNCTION;
979 137860 : p->symtree = NULL;
980 137860 : p->value.function.actual = gfc_get_actual_arglist ();
981 137860 : p->value.function.actual->expr = e;
982 :
983 137860 : 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 12279545 : gfc_type_convert_binary (gfc_expr *e, int wconversion)
999 : {
1000 12279545 : gfc_expr *op1, *op2;
1001 :
1002 12279545 : op1 = e->value.op.op1;
1003 12279545 : op2 = e->value.op.op2;
1004 :
1005 12279545 : 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 12279545 : if (op1->ts.type == op2->ts.type)
1013 : {
1014 12257029 : if (op1->ts.kind == op2->ts.kind)
1015 : {
1016 : /* No type conversions. */
1017 12143083 : e->ts = op1->ts;
1018 12143083 : goto done;
1019 : }
1020 :
1021 : /* Unsigned exponentiation is special, we need the type of the first
1022 : argument here because of modulo arithmetic. */
1023 113946 : 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 29568 : if (op1->ts.kind > op2->ts.kind)
1030 21991 : gfc_convert_type_warn (op2, &op1->ts, 2, wconversion);
1031 : else
1032 7577 : gfc_convert_type_warn (op1, &op2->ts, 2, wconversion);
1033 :
1034 29568 : e->ts = op1->ts;
1035 29568 : goto done;
1036 : }
1037 :
1038 : /* Integer combined with real or complex. */
1039 22516 : if (op2->ts.type == BT_INTEGER)
1040 : {
1041 17142 : e->ts = op1->ts;
1042 :
1043 : /* Special case for ** operator. */
1044 17142 : if (e->value.op.op == INTRINSIC_POWER)
1045 4791 : goto done;
1046 :
1047 12351 : gfc_convert_type_warn (e->value.op.op2, &e->ts, 2, wconversion);
1048 12351 : 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 306520 : is_non_constant_intrinsic (gfc_expr *e)
1080 : {
1081 306520 : if (e->expr_type == EXPR_FUNCTION
1082 306520 : && e->value.function.isym)
1083 : {
1084 306520 : 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 302469 : default:
1095 302469 : 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 45589128 : gfc_is_constant_expr (gfc_expr *e)
1107 : {
1108 45589128 : gfc_constructor *c;
1109 45589128 : gfc_actual_arglist *arg;
1110 :
1111 45589128 : if (e == NULL)
1112 : return true;
1113 :
1114 45568955 : switch (e->expr_type)
1115 : {
1116 1147202 : case EXPR_OP:
1117 1147202 : return (gfc_is_constant_expr (e->value.op.op1)
1118 1147202 : && (e->value.op.op2 == NULL
1119 106159 : || 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 1537287 : 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 1537287 : if (e->symtree->n.sym->attr.pdt_len
1130 1533371 : || e->symtree->n.sym->attr.pdt_kind)
1131 : return true;
1132 : return false;
1133 :
1134 372928 : case EXPR_FUNCTION:
1135 372928 : case EXPR_PPC:
1136 372928 : case EXPR_COMPCALL:
1137 372928 : gcc_assert (e->symtree || e->value.function.esym
1138 : || e->value.function.isym);
1139 :
1140 : /* Check for intrinsics excluded in constant expressions. */
1141 372928 : if (e->value.function.isym && is_non_constant_intrinsic (e))
1142 : return false;
1143 :
1144 : /* Call to intrinsic with at least one argument. */
1145 368877 : if (e->value.function.isym && e->value.function.actual)
1146 : {
1147 310163 : for (arg = e->value.function.actual; arg; arg = arg->next)
1148 306849 : if (!gfc_is_constant_expr (arg->expr))
1149 : return false;
1150 : }
1151 :
1152 69882 : 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 166336 : case EXPR_ARRAY:
1170 166336 : case EXPR_STRUCTURE:
1171 166336 : c = gfc_constructor_first (e->value.constructor);
1172 166336 : if ((e->expr_type == EXPR_ARRAY) && c && c->iterator)
1173 6348 : return gfc_constant_ac (e);
1174 :
1175 1977773 : for (; c; c = gfc_constructor_next (c))
1176 1829997 : 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 769974 : is_CFI_desc (gfc_symbol *sym, gfc_expr *e)
1192 : {
1193 769974 : if (sym == NULL
1194 769974 : && e && e->expr_type == EXPR_VARIABLE)
1195 189496 : sym = e->symtree->n.sym;
1196 :
1197 769974 : if (sym && sym->attr.dummy && sym->ns && sym->ns->proc_name
1198 321063 : && sym->ns->proc_name->attr.is_bind_c
1199 78048 : && (sym->attr.pointer
1200 73662 : || sym->attr.allocatable
1201 70399 : || (sym->attr.dimension
1202 42563 : && (sym->as->type == AS_ASSUMED_SHAPE
1203 26295 : || sym->as->type == AS_ASSUMED_RANK))
1204 43256 : || (sym->ts.type == BT_CHARACTER
1205 14718 : && (!sym->ts.u.cl || !sym->ts.u.cl->length))))
1206 47242 : 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 286442 : is_subref_array (gfc_expr * e)
1216 : {
1217 286442 : gfc_ref * ref;
1218 286442 : bool seen_array;
1219 286442 : gfc_symbol *sym;
1220 :
1221 286442 : if (e->expr_type != EXPR_VARIABLE)
1222 : return false;
1223 :
1224 285199 : sym = e->symtree->n.sym;
1225 :
1226 285199 : if (sym->attr.subref_array_pointer
1227 285199 : || gfc_is_span_addressed_dummy (sym))
1228 : return true;
1229 :
1230 279542 : seen_array = false;
1231 :
1232 586078 : 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 309867 : if (!seen_array && ref->type == REF_COMPONENT
1238 32927 : && ref->next == NULL
1239 4844 : && ref->u.c.component->ts.type != BT_CHARACTER
1240 4823 : && ref->u.c.component->ts.type != BT_CLASS
1241 4415 : && !gfc_bt_struct (ref->u.c.component->ts.type))
1242 : return false;
1243 :
1244 309752 : if (ref->type == REF_ARRAY
1245 273391 : && ref->u.ar.type != AR_ELEMENT)
1246 : seen_array = true;
1247 :
1248 39989 : if (seen_array
1249 272979 : && ref->type != REF_ARRAY)
1250 : return seen_array;
1251 : }
1252 :
1253 276211 : if (sym->ts.type == BT_CLASS
1254 22432 : && sym->attr.dummy
1255 6574 : && CLASS_DATA (sym)->attr.dimension
1256 4054 : && CLASS_DATA (sym)->attr.class_pointer)
1257 659 : return true;
1258 :
1259 : return false;
1260 : }
1261 :
1262 :
1263 : /* Try to collapse intrinsic expressions. */
1264 :
1265 : static bool
1266 17363190 : simplify_intrinsic_op (gfc_expr *p, int type)
1267 : {
1268 17363190 : gfc_intrinsic_op op;
1269 17363190 : gfc_expr *op1, *op2, *result;
1270 :
1271 17363190 : if (p->value.op.op == INTRINSIC_USER)
1272 : return true;
1273 :
1274 17363187 : op1 = p->value.op.op1;
1275 17363187 : op2 = p->value.op.op2;
1276 17363187 : op = p->value.op.op;
1277 :
1278 17363187 : if (!gfc_simplify_expr (op1, type))
1279 : return false;
1280 17362793 : if (!gfc_simplify_expr (op2, type))
1281 : return false;
1282 :
1283 17362733 : if (!gfc_is_constant_expr (op1)
1284 17362733 : || (op2 != NULL && !gfc_is_constant_expr (op2)))
1285 : return true;
1286 :
1287 : /* Rip p apart. */
1288 16439566 : p->value.op.op1 = NULL;
1289 16439566 : p->value.op.op2 = NULL;
1290 :
1291 16439566 : switch (op)
1292 : {
1293 5258746 : case INTRINSIC_PARENTHESES:
1294 5258746 : result = gfc_parentheses (op1);
1295 5258746 : 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 10282663 : case INTRINSIC_PLUS:
1306 10282663 : result = gfc_add (op1, op2);
1307 10282663 : break;
1308 :
1309 504599 : case INTRINSIC_MINUS:
1310 504599 : result = gfc_subtract (op1, op2);
1311 504599 : break;
1312 :
1313 338872 : case INTRINSIC_TIMES:
1314 338872 : result = gfc_multiply (op1, op2);
1315 338872 : 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 2499 : case INTRINSIC_CONCAT:
1326 2499 : result = gfc_concat (op1, op2);
1327 2499 : 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 21008 : case INTRINSIC_NE:
1335 21008 : case INTRINSIC_NE_OS:
1336 21008 : result = gfc_ne (op1, op2, op);
1337 21008 : 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 16439566 : if (result == NULL)
1384 : {
1385 55 : gfc_free_expr (op1);
1386 55 : gfc_free_expr (op2);
1387 55 : return false;
1388 : }
1389 :
1390 16439511 : result->rank = p->rank;
1391 16439511 : result->corank = p->corank;
1392 16439511 : result->where = p->where;
1393 16439511 : gfc_replace_expr (p, result);
1394 :
1395 16439511 : 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 142226 : simplify_constructor (gfc_constructor_base base, int type)
1441 : {
1442 142226 : gfc_constructor *c;
1443 142226 : gfc_expr *p;
1444 :
1445 853709 : for (c = gfc_constructor_first (base); c; c = gfc_constructor_next (c))
1446 : {
1447 711483 : if (c->iterator
1448 711483 : && (!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 711483 : 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 48769 : p = gfc_copy_expr (c->expr);
1459 :
1460 48769 : if (!gfc_simplify_expr (p, type))
1461 : {
1462 10 : gfc_free_expr (p);
1463 10 : continue;
1464 : }
1465 :
1466 48759 : 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 4884 : find_array_element (gfc_constructor_base base, gfc_array_ref *ar,
1478 : gfc_constructor **rval)
1479 : {
1480 4884 : unsigned long nelemen;
1481 4884 : int i;
1482 4884 : mpz_t delta;
1483 4884 : mpz_t offset;
1484 4884 : mpz_t span;
1485 4884 : mpz_t tmp;
1486 4884 : gfc_constructor *cons;
1487 4884 : gfc_expr *e;
1488 4884 : bool t;
1489 :
1490 4884 : t = true;
1491 4884 : e = NULL;
1492 :
1493 4884 : mpz_init_set_ui (offset, 0);
1494 4884 : mpz_init (delta);
1495 4884 : mpz_init (tmp);
1496 4884 : mpz_init_set_ui (span, 1);
1497 12639 : for (i = 0; i < ar->dimen; i++)
1498 : {
1499 4951 : if (!gfc_reduce_init_expr (ar->as->lower[i])
1500 4946 : || !gfc_reduce_init_expr (ar->as->upper[i])
1501 4946 : || ar->as->upper[i]->expr_type != EXPR_CONSTANT
1502 9893 : || ar->as->lower[i]->expr_type != EXPR_CONSTANT)
1503 : {
1504 9 : t = false;
1505 9 : cons = NULL;
1506 9 : goto depart;
1507 : }
1508 :
1509 4942 : e = ar->start[i];
1510 4942 : if (e->expr_type != EXPR_CONSTANT)
1511 : {
1512 2062 : cons = NULL;
1513 2062 : 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 4884 : mpz_clear (delta);
1552 4884 : mpz_clear (offset);
1553 4884 : mpz_clear (span);
1554 4884 : mpz_clear (tmp);
1555 4884 : *rval = cons;
1556 4884 : 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 1334 : find_array_section (gfc_expr *expr, gfc_ref *ref)
1617 : {
1618 1334 : int idx;
1619 1334 : int rank;
1620 1334 : int d;
1621 1334 : int shape_i;
1622 1334 : int limit;
1623 1334 : long unsigned one = 1;
1624 1334 : bool incr_ctr;
1625 1334 : mpz_t start[GFC_MAX_DIMENSIONS];
1626 1334 : mpz_t end[GFC_MAX_DIMENSIONS];
1627 1334 : mpz_t stride[GFC_MAX_DIMENSIONS];
1628 1334 : mpz_t delta[GFC_MAX_DIMENSIONS];
1629 1334 : mpz_t ctr[GFC_MAX_DIMENSIONS];
1630 1334 : mpz_t delta_mpz;
1631 1334 : mpz_t tmp_mpz;
1632 1334 : mpz_t nelts;
1633 1334 : mpz_t ptr;
1634 1334 : gfc_constructor_base base;
1635 1334 : gfc_constructor *cons, *vecsub[GFC_MAX_DIMENSIONS];
1636 1334 : gfc_expr *begin;
1637 1334 : gfc_expr *finish;
1638 1334 : gfc_expr *step;
1639 1334 : gfc_expr *upper;
1640 1334 : gfc_expr *lower;
1641 1334 : bool t;
1642 :
1643 1334 : t = true;
1644 :
1645 1334 : base = expr->value.constructor;
1646 1334 : expr->value.constructor = NULL;
1647 :
1648 1334 : rank = ref->u.ar.as->rank;
1649 :
1650 1334 : if (expr->shape == NULL)
1651 243 : expr->shape = gfc_get_shape (rank);
1652 :
1653 1334 : mpz_init_set_ui (delta_mpz, one);
1654 1334 : mpz_init_set_ui (nelts, one);
1655 1334 : mpz_init (tmp_mpz);
1656 1334 : mpz_init (ptr);
1657 :
1658 : /* Do the initialization now, so that we can cleanup without
1659 : keeping track of where we were. */
1660 4541 : for (d = 0; d < rank; d++)
1661 : {
1662 1873 : mpz_init (delta[d]);
1663 1873 : mpz_init (start[d]);
1664 1873 : mpz_init (end[d]);
1665 1873 : mpz_init (ctr[d]);
1666 1873 : mpz_init (stride[d]);
1667 1873 : vecsub[d] = NULL;
1668 : }
1669 :
1670 : /* Build the counters to clock through the array reference. */
1671 : shape_i = 0;
1672 2505 : for (d = 0; d < rank; d++)
1673 : {
1674 : /* Make this stretch of code easier on the eye! */
1675 1626 : begin = ref->u.ar.start[d];
1676 1626 : finish = ref->u.ar.end[d];
1677 1626 : step = ref->u.ar.stride[d];
1678 1626 : lower = ref->u.ar.as->lower[d];
1679 1626 : upper = ref->u.ar.as->upper[d];
1680 :
1681 1626 : if (!lower || !upper
1682 1616 : || lower->expr_type != EXPR_CONSTANT
1683 1616 : || upper->expr_type != EXPR_CONSTANT
1684 1616 : || lower->ts.type != BT_INTEGER
1685 1616 : || upper->ts.type != BT_INTEGER)
1686 : {
1687 11 : t = false;
1688 11 : goto cleanup;
1689 : }
1690 :
1691 1615 : 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 1545 : if ((begin && begin->expr_type != EXPR_CONSTANT)
1732 1173 : || (finish && finish->expr_type != EXPR_CONSTANT)
1733 1143 : || (step && step->expr_type != EXPR_CONSTANT))
1734 : {
1735 402 : t = false;
1736 402 : goto cleanup;
1737 : }
1738 :
1739 : /* Obtain the stride. */
1740 1143 : if (step)
1741 118 : mpz_set (stride[d], step->value.integer);
1742 : else
1743 1025 : mpz_set_ui (stride[d], one);
1744 :
1745 1143 : 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 1143 : if (begin)
1750 855 : mpz_set (start[d], begin->value.integer);
1751 : else
1752 288 : mpz_set (start[d], lower->value.integer);
1753 :
1754 1143 : mpz_set (ctr[d], start[d]);
1755 :
1756 : /* Obtain the end value for the index. */
1757 1143 : if (finish)
1758 626 : mpz_set (end[d], finish->value.integer);
1759 : else
1760 517 : mpz_set (end[d], upper->value.integer);
1761 :
1762 : /* Separate 'if' because elements sometimes arrive with
1763 : non-null end. */
1764 1143 : if (ref->u.ar.dimen_type[d] == DIMEN_ELEMENT)
1765 248 : mpz_set (end [d], begin->value.integer);
1766 :
1767 : /* Check the bounds. */
1768 1143 : if (mpz_cmp (ctr[d], upper->value.integer) > 0
1769 1124 : || mpz_cmp (end[d], upper->value.integer) > 0
1770 1124 : || mpz_cmp (ctr[d], lower->value.integer) < 0
1771 1109 : || 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 1109 : mpz_set (tmp_mpz, stride[d]);
1781 1109 : mpz_add (tmp_mpz, end[d], tmp_mpz);
1782 1109 : mpz_sub (tmp_mpz, tmp_mpz, ctr[d]);
1783 1109 : mpz_div (tmp_mpz, tmp_mpz, stride[d]);
1784 1109 : 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 1109 : if (ref->u.ar.dimen_type[d] != DIMEN_ELEMENT)
1789 861 : 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 1171 : mpz_set (delta[d], delta_mpz);
1795 1171 : mpz_sub (tmp_mpz, upper->value.integer, lower->value.integer);
1796 1171 : mpz_add_ui (tmp_mpz, tmp_mpz, one);
1797 1171 : 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 1334 : mpz_clear (delta_mpz);
1871 1334 : mpz_clear (tmp_mpz);
1872 1334 : mpz_clear (nelts);
1873 4541 : for (d = 0; d < rank; d++)
1874 : {
1875 1873 : mpz_clear (delta[d]);
1876 1873 : mpz_clear (start[d]);
1877 1873 : mpz_clear (end[d]);
1878 1873 : mpz_clear (ctr[d]);
1879 1873 : mpz_clear (stride[d]);
1880 : }
1881 1334 : mpz_clear (ptr);
1882 1334 : gfc_constructor_free (base);
1883 1334 : 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 144840 : simplify_const_ref (gfc_expr *p)
2158 : {
2159 144840 : gfc_constructor *cons, *c;
2160 144840 : gfc_expr *newp = NULL;
2161 144840 : gfc_ref *last_ref;
2162 :
2163 304614 : while (p->ref)
2164 : {
2165 17479 : switch (p->ref->type)
2166 : {
2167 14422 : case REF_ARRAY:
2168 : /* <type/kind spec>, parameter :: x(<int>) = scalar_expr
2169 : will generate this. */
2170 14422 : 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 14377 : switch (p->ref->u.ar.type)
2186 : {
2187 4884 : case AR_ELEMENT:
2188 4884 : if (!find_array_element (p->value.constructor, &p->ref->u.ar, &cons))
2189 : return false;
2190 :
2191 4866 : if (!cons)
2192 : return true;
2193 :
2194 2804 : remove_subobject_ref (p, cons);
2195 2804 : break;
2196 :
2197 1334 : case AR_SECTION:
2198 1334 : 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 15261634 : simplify_ref_chain (gfc_ref *ref, int type, gfc_expr **p)
2305 : {
2306 15261634 : int n;
2307 15261634 : gfc_expr *newp = NULL;
2308 :
2309 15622458 : for (; ref; ref = ref->next)
2310 : {
2311 363450 : switch (ref->type)
2312 : {
2313 : case REF_ARRAY:
2314 612926 : for (n = 0; n < ref->u.ar.dimen; n++)
2315 : {
2316 336498 : if (!gfc_simplify_expr (ref->u.ar.start[n], type))
2317 : return false;
2318 336498 : if (!gfc_simplify_expr (ref->u.ar.end[n], type))
2319 : return false;
2320 336498 : if (!gfc_simplify_expr (ref->u.ar.stride[n], type))
2321 : return false;
2322 : }
2323 : break;
2324 :
2325 15592 : case REF_SUBSTRING:
2326 15592 : if (!gfc_simplify_expr (ref->u.ss.start, type))
2327 : return false;
2328 15592 : 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 15071 : simplify_parameter_variable (gfc_expr *p, int type)
2353 : {
2354 15071 : gfc_expr *e;
2355 15071 : 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 15071 : if (!gfc_resolve_ref (p))
2360 : {
2361 1 : gfc_error_check ();
2362 1 : return false;
2363 : }
2364 15070 : gfc_expression_rank (p);
2365 :
2366 : /* Is this an inquiry? */
2367 15070 : bool inquiry = false;
2368 15070 : gfc_ref* ref = p->ref;
2369 30997 : while (ref)
2370 : {
2371 16055 : if (ref->type == REF_INQUIRY)
2372 : break;
2373 15927 : ref = ref->next;
2374 : }
2375 15070 : if (ref && ref->type == REF_INQUIRY)
2376 128 : inquiry = ref->u.i == INQUIRY_LEN || ref->u.i == INQUIRY_KIND;
2377 :
2378 15070 : 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 14380 : e = gfc_copy_expr (p->symtree->n.sym->value);
2402 14380 : if (e == NULL)
2403 : return false;
2404 :
2405 14375 : gfc_free_shape (&e->shape, e->rank);
2406 14375 : e->shape = gfc_copy_shape (p->shape, p->rank);
2407 14375 : e->rank = p->rank;
2408 14375 : e->corank = p->corank;
2409 :
2410 14375 : if (e->ts.type == BT_CHARACTER && p->ts.u.cl)
2411 3534 : e->ts = p->ts;
2412 : }
2413 :
2414 14445 : 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 14445 : if (e->expr_type != EXPR_CONSTANT && p->ref != NULL)
2419 14438 : e->ref = gfc_copy_ref (p->ref);
2420 14445 : t = gfc_simplify_expr (e, type);
2421 14445 : e->where = p->where;
2422 :
2423 : /* Only use the simplification if it eliminated all subobject references. */
2424 14445 : if (t && !e->ref)
2425 11897 : gfc_replace_expr (p, e);
2426 : else
2427 2548 : 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 56790462 : gfc_simplify_expr (gfc_expr *p, int type)
2457 : {
2458 56790462 : gfc_actual_arglist *ap;
2459 56790462 : gfc_intrinsic_sym* isym = NULL;
2460 :
2461 :
2462 56790462 : if (p == NULL)
2463 : return true;
2464 :
2465 50421586 : switch (p->expr_type)
2466 : {
2467 17175594 : case EXPR_CONSTANT:
2468 17175594 : 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 604034 : 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 604034 : ap = p->value.function.actual;
2481 604034 : if (p->value.function.isym &&
2482 566338 : (p->value.function.isym->id == GFC_ISYM_LBOUND
2483 553112 : || p->value.function.isym->id == GFC_ISYM_UBOUND
2484 545119 : || p->value.function.isym->id == GFC_ISYM_LCOBOUND
2485 544845 : || p->value.function.isym->id == GFC_ISYM_UCOBOUND
2486 544563 : || p->value.function.isym->id == GFC_ISYM_SHAPE))
2487 26636 : ap = ap->next;
2488 :
2489 1732125 : for ( ; ap; ap = ap->next)
2490 1128305 : if (!gfc_simplify_expr (ap->expr, type))
2491 : return false;
2492 :
2493 603820 : if (p->value.function.isym != NULL
2494 603820 : && gfc_intrinsic_func_interface (p, 1) == MATCH_ERROR)
2495 : return false;
2496 :
2497 603761 : if (p->symtree && (p->value.function.isym || p->ts.type == BT_UNKNOWN))
2498 : {
2499 246422 : isym = gfc_find_function (p->symtree->n.sym->name);
2500 246422 : if (isym && isym->elemental)
2501 124939 : 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 17363190 : case EXPR_OP:
2550 17363190 : 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 15131505 : case EXPR_VARIABLE:
2560 : /* Only substitute array parameter variables if we are in an
2561 : initialization expression, or we want a subsection. */
2562 15131505 : if (p->symtree->n.sym->attr.flavor == FL_PARAMETER
2563 14663 : && (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 14662 : if (!simplify_parameter_variable (p, type))
2568 : return false;
2569 14192 : if (!iter_stack)
2570 : break;
2571 : }
2572 :
2573 15117737 : if (type == 1)
2574 : {
2575 14086763 : gfc_simplify_iterator_var (p);
2576 : }
2577 :
2578 : /* Simplify subcomponent references. */
2579 15117737 : if (!simplify_ref_chain (p->ref, type, &p))
2580 : return false;
2581 :
2582 : break;
2583 :
2584 142410 : case EXPR_STRUCTURE:
2585 142410 : case EXPR_ARRAY:
2586 142410 : 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 142409 : if (p->expr_type == EXPR_CONSTANT && !p->ref && !p->rank && !p->shape)
2592 : return true;
2593 :
2594 142226 : if (!simplify_constructor (p->value.constructor, type))
2595 : return false;
2596 :
2597 142226 : if (p->expr_type == EXPR_ARRAY && p->ref && p->ref->type == REF_ARRAY
2598 14341 : && p->ref->u.ar.type == AR_FULL)
2599 8148 : gfc_expand_constructor (p, false);
2600 :
2601 142226 : 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 5112 : et0 (gfc_expr *e)
2649 : {
2650 5112 : if (e->expr_type == EXPR_VARIABLE && gfc_check_iter_variable (e))
2651 : return BT_INTEGER;
2652 :
2653 5112 : return e->ts.type;
2654 : }
2655 :
2656 :
2657 : /* Scalarize an expression for an elemental intrinsic call. */
2658 :
2659 : static bool
2660 125179 : scalarize_intrinsic_call (gfc_expr *e, bool init_flag)
2661 : {
2662 125179 : gfc_actual_arglist *a, *b;
2663 125179 : gfc_constructor_base ctor;
2664 125179 : gfc_constructor *args[5] = {}; /* Avoid uninitialized warnings. */
2665 125179 : gfc_constructor *ci, *new_ctor;
2666 125179 : gfc_expr *expr, *old, *p;
2667 125179 : int n, i, rank[5], array_arg;
2668 :
2669 125179 : if (e == NULL)
2670 : return false;
2671 :
2672 125179 : a = e->value.function.actual;
2673 133167 : for (; a; a = a->next)
2674 132401 : 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 3796 : check_intrinsic_op (gfc_expr *e, bool (*check_function) (gfc_expr *))
2804 : {
2805 3796 : gfc_expr *op1 = e->value.op.op1;
2806 3796 : gfc_expr *op2 = e->value.op.op2;
2807 :
2808 3796 : if (!(*check_function)(op1))
2809 : return false;
2810 :
2811 2982 : 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 2251 : case INTRINSIC_PLUS:
2844 2251 : case INTRINSIC_MINUS:
2845 2251 : case INTRINSIC_TIMES:
2846 2251 : case INTRINSIC_DIVIDE:
2847 2251 : case INTRINSIC_POWER:
2848 2251 : if (!(*check_function)(op2))
2849 : return false;
2850 :
2851 2107 : 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 2297 : check_alloc_comp_init (gfc_expr *e)
2923 : {
2924 2297 : gfc_component *comp;
2925 2297 : gfc_constructor *ctor;
2926 :
2927 2297 : gcc_assert (e->expr_type == EXPR_STRUCTURE);
2928 2297 : gcc_assert (e->ts.type == BT_DERIVED || e->ts.type == BT_CLASS);
2929 :
2930 2297 : for (comp = e->ts.u.derived->components,
2931 2297 : ctor = gfc_constructor_first (e->value.constructor);
2932 5328 : comp; comp = comp->next, ctor = gfc_constructor_next (ctor))
2933 : {
2934 3032 : if (comp->attr.allocatable && ctor->expr
2935 37 : && 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 4222 : check_inquiry (gfc_expr *e, int not_restricted)
2967 : {
2968 4222 : const char *name;
2969 4222 : const char *const *functions;
2970 :
2971 4222 : 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 4222 : 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 4222 : 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 4222 : int i = 0;
2997 4222 : gfc_actual_arglist *ap;
2998 4222 : gfc_symbol *sym;
2999 4222 : gfc_symbol *asym;
3000 :
3001 4222 : if (!e->value.function.isym
3002 4116 : || !e->value.function.isym->inquiry)
3003 : return MATCH_NO;
3004 :
3005 : /* An undeclared parameter will get us here (PR25018). */
3006 2871 : if (e->symtree == NULL)
3007 : return MATCH_NO;
3008 :
3009 2869 : sym = e->symtree->n.sym;
3010 :
3011 2869 : 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 2867 : name = sym->name;
3025 :
3026 2867 : functions = inquiry_func_gnu;
3027 2867 : if (gfc_option.warn_std & GFC_STD_F2003)
3028 0 : functions = inquiry_func_f2003;
3029 2867 : if (gfc_option.warn_std & GFC_STD_F95)
3030 0 : functions = inquiry_func_f95;
3031 :
3032 12026 : for (i = 0; functions[i]; i++)
3033 12021 : if (strcmp (functions[i], name) == 0)
3034 : break;
3035 :
3036 2867 : 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 9173 : for (ap = e->value.function.actual; ap; ap = ap->next)
3045 : {
3046 6830 : if (!ap->expr)
3047 3345 : continue;
3048 :
3049 3485 : asym = ap->expr->symtree ? ap->expr->symtree->n.sym : NULL;
3050 :
3051 3485 : 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 3485 : 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 3485 : if (i == 5 && not_restricted && asym
3070 423 : && asym->ts.type == BT_CHARACTER
3071 417 : && ((asym->ts.u.cl && asym->ts.u.cl->length == NULL)
3072 49 : || asym->ts.deferred))
3073 : {
3074 368 : gfc_error ("Assumed or deferred character length variable %qs "
3075 : "in constant expression at %L",
3076 368 : asym->name, &ap->expr->where);
3077 368 : return MATCH_ERROR;
3078 : }
3079 3117 : else if (not_restricted && !gfc_check_init_expr (ap->expr))
3080 : return MATCH_ERROR;
3081 :
3082 2969 : if (not_restricted == 0
3083 2949 : && ap->expr->expr_type != EXPR_VARIABLE
3084 3648 : && !check_restricted (ap->expr))
3085 : return MATCH_ERROR;
3086 :
3087 2967 : if (not_restricted == 0
3088 2947 : && ap->expr->expr_type == EXPR_VARIABLE
3089 2270 : && 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 1130 : check_conversion (gfc_expr *e)
3199 : {
3200 1130 : if (!e->value.function.isym
3201 1130 : || !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 477072 : gfc_check_init_expr (gfc_expr *e)
3217 : {
3218 477072 : match m;
3219 477072 : bool t;
3220 :
3221 477072 : if (e == NULL)
3222 : return true;
3223 :
3224 477031 : switch (e->expr_type)
3225 : {
3226 1011 : case EXPR_OP:
3227 1011 : t = check_intrinsic_op (e, gfc_check_init_expr);
3228 1011 : 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 1688 : case EXPR_FUNCTION:
3248 1688 : t = false;
3249 :
3250 1688 : {
3251 1688 : bool conversion;
3252 1688 : gfc_intrinsic_sym* isym = NULL;
3253 1688 : gfc_symbol* sym = e->symtree->n.sym;
3254 :
3255 : /* Simplify here the intrinsics from the IEEE_ARITHMETIC and
3256 : IEEE_EXCEPTIONS modules. */
3257 1688 : int mod = sym->from_intmod;
3258 1688 : if (mod == INTMOD_NONE && sym->generic)
3259 192 : mod = sym->generic->sym->from_intmod;
3260 1688 : 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 3849 : conversion = e->value.function.isym
3275 1361 : && (e->value.function.isym->conversion == 1);
3276 :
3277 1358 : if (!conversion && (!gfc_is_intrinsic (sym, 0, e->where)
3278 1143 : || (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 1130 : if ((m = check_conversion (e)) == MATCH_NO
3287 1127 : && (m = check_inquiry (e, 1)) == MATCH_NO
3288 594 : && (m = check_null (e)) == MATCH_NO
3289 594 : && (m = check_transformational (e)) == MATCH_NO
3290 1615 : && (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 1130 : 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 4878 : case EXPR_VARIABLE:
3315 4878 : t = true;
3316 :
3317 : /* This occurs when parsing pdt templates. */
3318 4878 : if (gfc_expr_attr (e).pdt_kind)
3319 : break;
3320 :
3321 4816 : if (gfc_check_iter_variable (e))
3322 : break;
3323 :
3324 4800 : 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 4382 : if (gfc_in_match_data ())
3342 : break;
3343 :
3344 4365 : t = false;
3345 :
3346 4365 : 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 4211 : 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 2411 : case EXPR_STRUCTURE:
3419 2411 : t = e->ts.is_iso_c;
3420 2411 : if (t)
3421 : break;
3422 :
3423 2297 : t = check_alloc_comp_init (e);
3424 2297 : if (!t)
3425 : break;
3426 :
3427 2296 : t = gfc_check_constructor (e, gfc_check_init_expr);
3428 2296 : if (!t)
3429 : break;
3430 :
3431 2296 : break;
3432 :
3433 5199 : case EXPR_ARRAY:
3434 5199 : t = gfc_check_constructor (e, gfc_check_init_expr);
3435 5199 : if (!t)
3436 : break;
3437 :
3438 5180 : t = gfc_expand_constructor (e, true);
3439 5180 : if (!t)
3440 : break;
3441 :
3442 5159 : t = gfc_check_constructor_type (e);
3443 5159 : 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 115061 : gfc_reduce_init_expr (gfc_expr *expr)
3458 : {
3459 115061 : bool t;
3460 :
3461 : /* It is far too early to resolve a class compcall. Punt to resolution. */
3462 115061 : if (expr && expr->expr_type == EXPR_COMPCALL
3463 25 : && expr->symtree->n.sym->ts.type == BT_CLASS)
3464 : return false;
3465 :
3466 115036 : gfc_init_expr_flag = true;
3467 115036 : t = gfc_resolve_expr (expr);
3468 115036 : if (t)
3469 114895 : t = gfc_check_init_expr (expr);
3470 115036 : gfc_init_expr_flag = false;
3471 :
3472 115036 : if (!t || !expr)
3473 : return false;
3474 :
3475 109873 : if (expr->expr_type == EXPR_ARRAY)
3476 : {
3477 5412 : if (!gfc_check_constructor_type (expr))
3478 : return false;
3479 5412 : 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 99567 : gfc_match_init_expr (gfc_expr **result)
3492 : {
3493 99567 : gfc_expr *expr;
3494 99567 : match m;
3495 99567 : bool t;
3496 :
3497 99567 : expr = NULL;
3498 :
3499 99567 : gfc_init_expr_flag = true;
3500 :
3501 99567 : m = gfc_match_expr (&expr);
3502 99567 : if (m != MATCH_YES)
3503 : {
3504 115 : gfc_init_expr_flag = false;
3505 115 : return m;
3506 : }
3507 :
3508 99452 : if (expr->expr_type != EXPR_FUNCTION && gfc_derived_parameter_expr (expr))
3509 : {
3510 421 : *result = expr;
3511 421 : gfc_init_expr_flag = false;
3512 421 : return m;
3513 : }
3514 :
3515 99031 : t = gfc_reduce_init_expr (expr);
3516 99031 : if (!t)
3517 : {
3518 671 : gfc_free_expr (expr);
3519 671 : gfc_init_expr_flag = false;
3520 671 : return MATCH_ERROR;
3521 : }
3522 :
3523 98360 : *result = expr;
3524 98360 : gfc_init_expr_flag = false;
3525 :
3526 98360 : 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 3095 : restricted_intrinsic (gfc_expr *e)
3622 : {
3623 : /* TODO: Check constraints on inquiry functions. 7.1.6.2 (7). */
3624 3095 : 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 17975 : check_references (gfc_ref* ref, bool (*checker) (gfc_expr*))
3649 : {
3650 18869 : int dim;
3651 :
3652 18869 : if (!ref)
3653 : return true;
3654 :
3655 897 : switch (ref->type)
3656 : {
3657 : case REF_ARRAY:
3658 1436 : for (dim = 0; dim < ref->u.ar.dimen; ++dim)
3659 : {
3660 725 : if (!checker (ref->u.ar.start[dim]))
3661 : return false;
3662 723 : if (!checker (ref->u.ar.end[dim]))
3663 : return false;
3664 723 : 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 894 : break;
3683 : }
3684 :
3685 894 : 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 463876 : check_restricted (gfc_expr *e)
3707 : {
3708 463876 : gfc_symbol* sym;
3709 463876 : bool t;
3710 :
3711 463876 : if (e == NULL)
3712 : return true;
3713 :
3714 461285 : switch (e->expr_type)
3715 : {
3716 2785 : case EXPR_OP:
3717 2785 : t = check_intrinsic_op (e, check_restricted);
3718 2785 : if (t)
3719 2783 : 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 3682 : case EXPR_FUNCTION:
3738 3682 : 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 3103 : 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 3095 : t = restricted_intrinsic (e);
3753 : }
3754 : break;
3755 :
3756 17981 : case EXPR_VARIABLE:
3757 17981 : sym = e->symtree->n.sym;
3758 17981 : 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 17981 : if (gfc_notification_std (GFC_STD_F2008)
3766 49 : && sym->attr.dummy && sym->ns == gfc_current_ns
3767 18030 : && 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 17977 : 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 17975 : 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 17975 : if (!check_references (e->ref, &check_restricted))
3791 : break;
3792 :
3793 17972 : if (e->error
3794 17952 : || sym->attr.in_common
3795 17757 : || sym->attr.use_assoc
3796 12447 : || sym->attr.used_in_submodule
3797 12446 : || sym->attr.dummy
3798 606 : || sym->attr.implied_index
3799 606 : || sym->attr.flavor == FL_PARAMETER
3800 19068 : || 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 487079 : gfc_specification_expr (gfc_expr *e)
3849 : {
3850 487079 : gfc_component *comp;
3851 :
3852 487079 : if (e == NULL)
3853 : return true;
3854 :
3855 451541 : 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 451515 : comp = gfc_get_proc_ptr_comp (e);
3863 451515 : if (e->expr_type == EXPR_FUNCTION
3864 2408 : && !e->value.function.isym
3865 392 : && !e->value.function.esym
3866 109 : && !gfc_pure (e->symtree->n.sym)
3867 451617 : && (!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 451512 : if (e->rank != 0)
3877 : {
3878 3 : gfc_error ("Expression at %L must be scalar", &e->where);
3879 3 : return false;
3880 : }
3881 :
3882 451509 : if (!gfc_simplify_expr (e, 0))
3883 : return false;
3884 :
3885 451503 : 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 199825 : gfc_check_conformance (gfc_expr *op1, gfc_expr *op2, const char *optype_msgid, ...)
3895 : {
3896 199825 : int op1_flag, op2_flag, d;
3897 199825 : mpz_t op1_size, op2_size;
3898 199825 : bool t;
3899 :
3900 199825 : va_list argp;
3901 199825 : char buffer[240];
3902 :
3903 199825 : if (op1->rank == 0 || op2->rank == 0)
3904 : return true;
3905 :
3906 72962 : va_start (argp, optype_msgid);
3907 72962 : d = vsnprintf (buffer, sizeof (buffer), optype_msgid, argp);
3908 72962 : va_end (argp);
3909 72962 : if (d < 1 || d >= (int) sizeof (buffer)) /* Reject truncation. */
3910 0 : gfc_internal_error ("optype_msgid overflow: %d", d);
3911 :
3912 72962 : 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 175316 : for (d = 0; d < op1->rank; d++)
3922 : {
3923 102456 : op1_flag = gfc_array_dimen_size(op1, d, &op1_size);
3924 102456 : op2_flag = gfc_array_dimen_size(op2, d, &op2_size);
3925 :
3926 102456 : 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 102456 : if (op1_flag)
3937 67373 : mpz_clear (op1_size);
3938 102456 : if (op2_flag)
3939 77746 : mpz_clear (op2_size);
3940 :
3941 102456 : 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 4918 : get_parm_list_from_expr (gfc_expr *expr)
3953 : {
3954 4918 : gfc_actual_arglist *a = NULL;
3955 4918 : gfc_constructor *c;
3956 :
3957 4918 : if (expr->expr_type == EXPR_STRUCTURE)
3958 2188 : a = expr->param_list;
3959 2730 : 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 2696 : else if (expr->expr_type == EXPR_VARIABLE)
3971 2463 : a = expr->symtree->n.sym->param_list;
3972 :
3973 4918 : return a;
3974 : }
3975 :
3976 : bool
3977 2459 : gfc_check_type_spec_parms (gfc_expr *expr1, gfc_expr *expr2,
3978 : const char *context)
3979 : {
3980 2459 : bool t = true;
3981 2459 : gfc_actual_arglist *a1, *a2;
3982 :
3983 2459 : gcc_assert (expr1->ts.type == BT_DERIVED
3984 : && expr1->ts.u.derived->attr.pdt_type);
3985 :
3986 2459 : a1 = get_parm_list_from_expr (expr1);
3987 2459 : a2 = get_parm_list_from_expr (expr2);
3988 :
3989 5368 : for (; a1 && a2; a1 = a1->next, a2 = a2->next)
3990 : {
3991 450 : if (a1->expr && a1->expr->expr_type == EXPR_CONSTANT
3992 426 : && a2->expr && a2->expr->expr_type == EXPR_CONSTANT
3993 413 : && !strcmp (a1->name, a2->name)
3994 389 : && 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 2459 : 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 519143 : bool gfc_pdt_requires_hidden_arg (gfc_symbol *arg)
4014 : {
4015 519143 : if (IS_PDT (arg)
4016 4292 : && (arg->attr.allocatable || arg->attr.pointer))
4017 : return true;
4018 :
4019 486707 : if (IS_CLASS_PDT (arg)
4020 770 : && (CLASS_DATA (arg)->attr.allocatable
4021 614 : || CLASS_DATA (arg)->attr.class_pointer))
4022 156 : 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 833306 : gfc_check_assign (gfc_expr *lvalue, gfc_expr *rvalue, int conform,
4036 : bool allow_convert)
4037 : {
4038 833306 : gfc_symbol *sym;
4039 833306 : gfc_ref *ref;
4040 833306 : int has_pointer;
4041 :
4042 833306 : 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 833306 : has_pointer = sym->attr.pointer;
4047 978157 : for (ref = lvalue->ref; ref; ref = ref->next)
4048 : {
4049 144851 : if (!has_pointer && ref->type == REF_COMPONENT
4050 48922 : && ref->u.c.component->attr.pointer)
4051 : has_pointer = 1;
4052 143796 : 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 833306 : if (sym->attr.flavor == FL_PROCEDURE && sym->attr.proc != PROC_ST_FUNCTION
4067 8799 : && !sym->attr.external)
4068 : {
4069 8789 : bool bad_proc;
4070 8789 : bad_proc = false;
4071 :
4072 : /* (i) Use associated; */
4073 8789 : if (sym->attr.use_assoc)
4074 0 : bad_proc = true;
4075 :
4076 : /* (ii) The assignment is in the main program; or */
4077 8789 : if (gfc_current_ns->proc_name
4078 8788 : && gfc_current_ns->proc_name->attr.is_main_program)
4079 8789 : bad_proc = true;
4080 :
4081 : /* (iii) A module or internal procedure... */
4082 8789 : if (gfc_current_ns->proc_name
4083 8788 : && (gfc_current_ns->proc_name->attr.proc == PROC_INTERNAL
4084 5054 : || gfc_current_ns->proc_name->attr.proc == PROC_MODULE)
4085 6246 : && gfc_current_ns->parent
4086 5703 : && (!(gfc_current_ns->parent->proc_name->attr.function
4087 5550 : || 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 5233 : if (gfc_current_ns->proc_name
4092 5233 : && !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 5233 : if (!sym->attr.entry && sym->name != gfc_current_ns->proc_name->name)
4097 8789 : 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 8789 : if (!sym->attr.entry
4104 7987 : && sym->ns == gfc_current_ns->parent
4105 5458 : && sym != gfc_current_ns->proc_name
4106 72 : && sym != gfc_current_ns->parent->proc_name->result)
4107 : bad_proc = true;
4108 :
4109 8788 : 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 824517 : 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 833303 : 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 833278 : 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 833278 : 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 833268 : if (warn_surprising
4163 833268 : && 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 81961 : if (lvalue->rank != 0 && rvalue->rank != 0
4170 886704 : && !gfc_check_conformance (lvalue, rvalue, _("array assignment")))
4171 : return false;
4172 :
4173 : /* Handle the case of a BOZ literal on the RHS. */
4174 833236 : 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 832995 : 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 60463 : if (lvalue->ts.type == BT_DERIVED && lvalue->ts.u.derived->attr.pdt_type
4224 835330 : && !gfc_check_type_spec_parms (lvalue, rvalue, "in assignment"))
4225 : return false;
4226 :
4227 832989 : if (gfc_compare_types (&lvalue->ts, &rvalue->ts))
4228 : return true;
4229 :
4230 : /* Only DATA Statements come here. */
4231 19760 : 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 18236 : 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 17842 : if (!allow_convert)
4270 : return true;
4271 :
4272 17842 : 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 17223 : gfc_check_pointer_assign (gfc_expr *lvalue, gfc_expr *rvalue,
4282 : bool suppress_type_test, bool is_init_expr)
4283 : {
4284 17223 : symbol_attribute attr, lhs_attr;
4285 17223 : gfc_ref *ref;
4286 17223 : bool is_pure, is_implicit_pure, rank_remap;
4287 17223 : int proc_pointer;
4288 17223 : bool same_rank;
4289 :
4290 17223 : if (!lvalue->symtree)
4291 : return false;
4292 :
4293 17222 : lhs_attr = gfc_expr_attr (lvalue);
4294 17222 : 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 17222 : 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 17222 : proc_pointer = lvalue->symtree->n.sym->attr.proc_pointer;
4311 :
4312 17222 : rank_remap = false;
4313 17222 : same_rank = lvalue->rank == rvalue->rank;
4314 24896 : for (ref = lvalue->ref; ref; ref = ref->next)
4315 : {
4316 12012 : if (ref->type == REF_COMPONENT)
4317 6829 : proc_pointer = ref->u.c.component->attr.proc_pointer;
4318 :
4319 12012 : if (ref->type == REF_ARRAY && ref->next == NULL)
4320 : {
4321 4711 : int dim;
4322 :
4323 4711 : 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 17211 : is_pure = gfc_pure (NULL);
4406 17211 : 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 17211 : 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 9536 : if (lvalue->expr_type == EXPR_VARIABLE
4416 9536 : && 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 9531 : 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 8219 : 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 8217 : 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 8206 : 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 8206 : 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 8202 : if (lvalue->ts.type == BT_CLASS && !UNLIMITED_POLY (rvalue))
4694 1481 : gfc_find_vtab (&rvalue->ts);
4695 :
4696 : /* Check rank remapping. */
4697 8202 : 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 7955 : 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 8192 : 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 8192 : if (rvalue->expr_type == EXPR_NULL)
4764 : return true;
4765 :
4766 8006 : if (rvalue->expr_type == EXPR_VARIABLE && is_subref_array (rvalue))
4767 772 : lvalue->symtree->n.sym->attr.subref_array_pointer = 1;
4768 :
4769 8006 : 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 8006 : if (rvalue->expr_type == EXPR_FUNCTION && rvalue->rank > 0 && attr.pointer)
4774 181 : lvalue->symtree->n.sym->attr.subref_array_pointer = 1;
4775 :
4776 8006 : 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 8005 : 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 7634 : if (!attr.target && !attr.pointer)
4866 : {
4867 10 : gfc_error ("Pointer assignment target is neither TARGET "
4868 : "nor POINTER at %L", &rvalue->where);
4869 10 : return false;
4870 : }
4871 : }
4872 :
4873 7984 : 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 7982 : 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 7982 : if (is_implicit_pure && gfc_impure_variable (rvalue->symtree->n.sym))
4887 375 : gfc_unset_implicit_pure (gfc_current_ns->proc_name);
4888 :
4889 7982 : 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 7980 : 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 7977 : if (rvalue->expr_type == EXPR_VARIABLE
4906 7977 : && 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 7973 : 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 7967 : 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 539279 : gfc_check_assign_symbol (gfc_symbol *sym, gfc_component *comp, gfc_expr *rvalue)
4985 : {
4986 539279 : gfc_expr lvalue;
4987 539279 : bool r;
4988 539279 : bool pointer, proc_pointer;
4989 :
4990 539279 : memset (&lvalue, '\0', sizeof (gfc_expr));
4991 :
4992 539279 : if (sym && sym->attr.pdt_template && comp && comp->initializer)
4993 : {
4994 701 : int i, flag;
4995 701 : gfc_expr *param_expr;
4996 701 : flag = 0;
4997 :
4998 701 : if (comp->as && comp->as->type == AS_EXPLICIT
4999 62 : && !(comp->ts.type == BT_DERIVED
5000 7 : && comp->ts.u.derived->attr.pdt_template))
5001 : {
5002 : /* Are the bounds of the array parameterized? */
5003 110 : for (i = 0; i < comp->as->rank; i++)
5004 : {
5005 55 : param_expr = gfc_copy_expr (comp->as->lower[i]);
5006 55 : if (gfc_simplify_expr (param_expr, 1)
5007 55 : && param_expr->expr_type != EXPR_CONSTANT)
5008 0 : flag++;
5009 55 : gfc_free_expr (param_expr);
5010 55 : param_expr = gfc_copy_expr (comp->as->upper[i]);
5011 55 : if (gfc_simplify_expr (param_expr, 1)
5012 55 : && param_expr->expr_type != EXPR_CONSTANT)
5013 1 : flag++;
5014 55 : gfc_free_expr (param_expr);
5015 : }
5016 : }
5017 :
5018 : /* Is the character length parameterized? */
5019 701 : if (comp->ts.type == BT_CHARACTER && comp->ts.u.cl->length)
5020 : {
5021 69 : param_expr = gfc_copy_expr (comp->ts.u.cl->length);
5022 69 : if (gfc_simplify_expr (param_expr, 1)
5023 69 : && param_expr->expr_type != EXPR_CONSTANT)
5024 1 : flag++;
5025 69 : gfc_free_expr (param_expr);
5026 : }
5027 :
5028 701 : 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 539277 : lvalue.expr_type = EXPR_VARIABLE;
5039 539277 : lvalue.ts = sym->ts;
5040 539277 : if (sym->as)
5041 : {
5042 17639 : lvalue.rank = sym->as->rank;
5043 17639 : lvalue.corank = sym->as->corank;
5044 : }
5045 539277 : lvalue.symtree = XCNEW (gfc_symtree);
5046 539277 : lvalue.symtree->n.sym = sym;
5047 539277 : lvalue.where = sym->declared_at;
5048 :
5049 539277 : if (comp)
5050 : {
5051 35505 : lvalue.ref = gfc_get_ref ();
5052 35505 : lvalue.ref->type = REF_COMPONENT;
5053 35505 : lvalue.ref->u.c.component = comp;
5054 35505 : lvalue.ref->u.c.sym = sym;
5055 35505 : lvalue.ts = comp->ts;
5056 35505 : lvalue.rank = comp->as ? comp->as->rank : 0;
5057 35505 : lvalue.corank = comp->as ? comp->as->corank : 0;
5058 35505 : lvalue.where = comp->loc;
5059 1076 : pointer = comp->ts.type == BT_CLASS && CLASS_DATA (comp)
5060 36581 : ? CLASS_DATA (comp)->attr.class_pointer : comp->attr.pointer;
5061 35505 : proc_pointer = comp->attr.proc_pointer;
5062 : }
5063 : else
5064 : {
5065 3166 : pointer = sym->ts.type == BT_CLASS && CLASS_DATA (sym)
5066 506938 : ? CLASS_DATA (sym)->attr.class_pointer : sym->attr.pointer;
5067 503772 : proc_pointer = sym->attr.proc_pointer;
5068 : }
5069 :
5070 539277 : if (pointer || proc_pointer)
5071 6340 : 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 532937 : if (rvalue->expr_type == EXPR_FUNCTION
5078 52 : && rvalue->value.function.isym
5079 30 : && (rvalue->value.function.isym->conversion == 1))
5080 0 : gfc_check_init_expr (rvalue);
5081 :
5082 532937 : r = gfc_check_assign (&lvalue, rvalue, 1);
5083 : }
5084 :
5085 539277 : free (lvalue.symtree);
5086 539277 : free (lvalue.ref);
5087 :
5088 539277 : if (!r)
5089 : return r;
5090 :
5091 539226 : 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 539213 : 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 106632 : gfc_build_init_expr (gfc_typespec *ts, locus *where, bool force)
5161 : {
5162 106632 : gfc_expr *init_expr;
5163 :
5164 : /* Try to build an initializer expression. */
5165 106632 : 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 106632 : gfc_init_local_real init_real = flag_init_real;
5169 106632 : int init_logical = gfc_option.flag_init_logical;
5170 106632 : 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 106632 : switch (ts->type)
5182 : {
5183 55853 : case BT_INTEGER:
5184 55853 : 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 55568 : gfc_free_expr (init_expr);
5190 55568 : init_expr = NULL;
5191 : }
5192 : break;
5193 :
5194 16268 : case BT_REAL:
5195 16268 : 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 16107 : default:
5217 16107 : gfc_free_expr (init_expr);
5218 16107 : init_expr = NULL;
5219 16107 : 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 5022 : case BT_LOGICAL:
5256 5022 : if (init_logical == GFC_INIT_LOGICAL_FALSE)
5257 39 : init_expr->value.logical = 0;
5258 4983 : else if (init_logical == GFC_INIT_LOGICAL_TRUE)
5259 26 : init_expr->value.logical = 1;
5260 : else
5261 : {
5262 4957 : gfc_free_expr (init_expr);
5263 4957 : init_expr = NULL;
5264 : }
5265 : break;
5266 :
5267 10123 : case BT_CHARACTER:
5268 : /* For characters, the length must be constant in order to
5269 : create a default initializer. */
5270 10123 : 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 10047 : gfc_free_expr (init_expr);
5284 10047 : init_expr = NULL;
5285 : }
5286 10047 : if (!init_expr
5287 10047 : && (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 17639 : default:
5309 17639 : gfc_free_expr (init_expr);
5310 17639 : init_expr = NULL;
5311 : }
5312 :
5313 106632 : 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 106422 : gfc_build_default_init_expr (gfc_typespec *ts, locus *where)
5321 : {
5322 106422 : 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 20728 : gfc_apply_init (gfc_typespec *ts, symbol_attribute *attr, gfc_expr *init)
5331 : {
5332 20728 : if (ts->type == BT_CHARACTER && !attr->pointer && init
5333 423 : && ts->u.cl
5334 423 : && ts->u.cl->length
5335 423 : && ts->u.cl->length->expr_type == EXPR_CONSTANT
5336 419 : && ts->u.cl->length->ts.type == BT_INTEGER)
5337 : {
5338 419 : HOST_WIDE_INT len = gfc_mpz_get_hwi (ts->u.cl->length->value.integer);
5339 :
5340 419 : if (init->expr_type == EXPR_CONSTANT)
5341 288 : gfc_set_constant_character_len (len, init, -1);
5342 131 : else if (init
5343 131 : && init->ts.type == BT_CHARACTER
5344 126 : && init->ts.u.cl && init->ts.u.cl->length
5345 126 : && mpz_cmp (ts->u.cl->length->value.integer,
5346 126 : 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 20728 : }
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 939 : is_non_empty_structure_constructor (gfc_expr * e)
5386 : {
5387 939 : if (e->expr_type != EXPR_STRUCTURE)
5388 : return false;
5389 :
5390 939 : gfc_constructor *cons = gfc_constructor_first (e->value.constructor);
5391 2470 : while (cons)
5392 : {
5393 1063 : if (!cons->expr || cons->expr->expr_type != EXPR_NULL)
5394 : return true;
5395 592 : 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 8010 : gfc_has_default_initializer (gfc_symbol *der)
5406 : {
5407 8010 : static hash_set<gfc_symbol *> seen_derived_types;
5408 8010 : 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 8010 : bool result = false;
5412 :
5413 8010 : gcc_assert (gfc_fl_struct (der->attr.flavor));
5414 8010 : seen_derived_types.add (der);
5415 16161 : for (c = der->components; c; c = c->next)
5416 8188 : if (gfc_bt_struct (c->ts.type)
5417 10051 : && !seen_derived_types.contains (c->ts.u.derived))
5418 : {
5419 1652 : if (!c->attr.pointer && !c->attr.proc_pointer
5420 1652 : && !(c->attr.allocatable && der == c->ts.u.derived)
5421 3434 : && ((c->initializer
5422 939 : && is_non_empty_structure_constructor (c->initializer))
5423 1181 : || gfc_has_default_initializer (c->ts.u.derived)))
5424 : {
5425 : result = true;
5426 : break;
5427 : }
5428 1241 : if (c->attr.pointer && c->initializer)
5429 : {
5430 : result = true;
5431 : break;
5432 : }
5433 : }
5434 : else
5435 : {
5436 8265 : if (c->initializer)
5437 : {
5438 : result = true;
5439 : break;
5440 : }
5441 : }
5442 :
5443 8010 : seen_derived_types.remove (der);
5444 8010 : 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 171969 : class_allocatable (gfc_component *comp)
5504 : {
5505 3490 : return comp->ts.type == BT_CLASS && comp->attr.class_ok && CLASS_DATA (comp)
5506 175458 : && 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 192257 : comp_allocatable (gfc_component *comp)
5518 : {
5519 192257 : 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 133959 : component_initializer (gfc_component *c, bool generate)
5536 : {
5537 133959 : 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 133959 : if (comp_allocatable (c) || (generate && comp_pointer (c) && !c->initializer))
5543 : {
5544 13616 : init = gfc_get_null_expr (&c->loc);
5545 13616 : init->ts = c->ts;
5546 13616 : return init;
5547 : }
5548 :
5549 : /* See if we can find the initializer immediately. */
5550 120343 : 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 30786 : gfc_default_initializer (gfc_typespec *ts)
5610 : {
5611 30786 : 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 65078 : gfc_generate_initializer (gfc_typespec *ts, bool generate)
5647 : {
5648 65078 : gfc_expr *init, *tmp;
5649 65078 : gfc_component *comp;
5650 :
5651 65078 : generate = flag_init_derived && generate;
5652 :
5653 65078 : 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 65075 : comp = ts->u.derived->components;
5660 65075 : if (!generate)
5661 : {
5662 113768 : for (; comp; comp = comp->next)
5663 82418 : if (comp->initializer || comp_allocatable (comp))
5664 : break;
5665 : }
5666 :
5667 65075 : if (!comp)
5668 : return NULL;
5669 :
5670 33725 : init = gfc_get_structure_constructor_expr (ts->type, ts->kind,
5671 : &ts->u.derived->declared_at);
5672 33725 : init->ts = *ts;
5673 :
5674 167683 : for (comp = ts->u.derived->components; comp; comp = comp->next)
5675 : {
5676 133959 : gfc_constructor *ctor = gfc_constructor_get();
5677 :
5678 : /* Fetch or generate an initializer for the component. */
5679 133959 : tmp = component_initializer (comp, generate);
5680 133959 : if (tmp)
5681 : {
5682 : /* Save the component ref for STRUCTUREs and UNIONs. */
5683 121455 : if (ts->u.derived->attr.flavor == FL_STRUCT
5684 121455 : || 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 121455 : ctor->expr = comp->initializer ? gfc_copy_expr (tmp) : tmp;
5689 121455 : if ((comp->ts.type != tmp->ts.type || comp->ts.kind != tmp->ts.kind)
5690 20647 : && !comp->attr.pointer && !comp->attr.proc_pointer)
5691 : {
5692 315 : bool val;
5693 315 : val = gfc_convert_type_warn (ctor->expr, &comp->ts, 1, false);
5694 315 : if (val == false)
5695 : return NULL;
5696 : }
5697 : }
5698 :
5699 133958 : 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 20589 : gfc_get_variable_expr (gfc_symtree *var)
5712 : {
5713 20589 : gfc_expr *e;
5714 :
5715 20589 : e = gfc_get_expr ();
5716 20589 : e->expr_type = EXPR_VARIABLE;
5717 20589 : e->symtree = var;
5718 20589 : e->ts = var->n.sym->ts;
5719 :
5720 20589 : if (var->n.sym->attr.flavor != FL_PROCEDURE
5721 15655 : && ((var->n.sym->as != NULL && var->n.sym->ts.type != BT_CLASS)
5722 12966 : || (var->n.sym->ts.type == BT_CLASS && var->n.sym->ts.u.derived
5723 8935 : && CLASS_DATA (var->n.sym)
5724 8935 : && CLASS_DATA (var->n.sym)->as)))
5725 : {
5726 6535 : gfc_array_spec *as = var->n.sym->ts.type == BT_CLASS
5727 4612 : ? CLASS_DATA (var->n.sym)->as
5728 : : var->n.sym->as;
5729 4612 : e->rank = as->rank;
5730 4612 : e->corank = as->corank;
5731 4612 : e->ref = gfc_get_ref ();
5732 4612 : e->ref->type = REF_ARRAY;
5733 4612 : e->ref->u.ar.type = AR_FULL;
5734 4612 : e->ref->u.ar.as = gfc_copy_array_spec (as);
5735 : }
5736 :
5737 20589 : return e;
5738 : }
5739 :
5740 :
5741 : /* Adds a full array reference to an expression, as needed. */
5742 :
5743 : void
5744 50358 : gfc_add_full_array_ref (gfc_expr *e, gfc_array_spec *as)
5745 : {
5746 50358 : gfc_ref *ref;
5747 50372 : for (ref = e->ref; ref; ref = ref->next)
5748 199 : if (!ref->next)
5749 : break;
5750 50358 : if (ref)
5751 : {
5752 185 : ref->next = gfc_get_ref ();
5753 185 : ref = ref->next;
5754 : }
5755 : else
5756 : {
5757 50173 : e->ref = gfc_get_ref ();
5758 50173 : ref = e->ref;
5759 : }
5760 50358 : ref->type = REF_ARRAY;
5761 50358 : ref->u.ar.type = AR_FULL;
5762 50358 : 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 50358 : ref->u.ar.where = e->where;
5766 50358 : ref->u.ar.as = as;
5767 50358 : }
5768 :
5769 :
5770 : gfc_expr *
5771 215087 : gfc_lval_expr_from_sym (gfc_symbol *sym)
5772 : {
5773 215087 : gfc_expr *lval;
5774 215087 : gfc_array_spec *as;
5775 215087 : lval = gfc_get_expr ();
5776 215087 : lval->expr_type = EXPR_VARIABLE;
5777 215087 : lval->where = sym->declared_at;
5778 215087 : lval->ts = sym->ts;
5779 215087 : lval->symtree = gfc_find_symtree (sym->ns->sym_root, sym->name);
5780 :
5781 : /* It will always be a full array. */
5782 215087 : as = IS_CLASS_ARRAY (sym) ? CLASS_DATA (sym)->as : sym->as;
5783 215087 : lval->rank = as ? as->rank : 0;
5784 215087 : lval->corank = as ? as->corank : 0;
5785 215087 : if (lval->rank || lval->corank)
5786 48867 : gfc_add_full_array_ref (lval, as);
5787 215087 : 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 26912 : gfc_get_full_arrayspec_from_expr (gfc_expr *expr)
5795 : {
5796 26912 : gfc_array_spec *as;
5797 26912 : gfc_ref *ref;
5798 :
5799 26912 : if (expr->rank == 0)
5800 : return NULL;
5801 :
5802 : /* Follow any component references. */
5803 26912 : if (expr->expr_type == EXPR_VARIABLE
5804 26912 : || expr->expr_type == EXPR_CONSTANT)
5805 : {
5806 19802 : if (expr->symtree)
5807 19802 : as = expr->symtree->n.sym->as;
5808 : else
5809 : as = NULL;
5810 :
5811 41636 : for (ref = expr->ref; ref; ref = ref->next)
5812 : {
5813 21834 : switch (ref->type)
5814 : {
5815 1857 : case REF_COMPONENT:
5816 1857 : as = ref->u.c.component->as;
5817 1857 : continue;
5818 :
5819 24 : case REF_SUBSTRING:
5820 24 : case REF_INQUIRY:
5821 24 : continue;
5822 :
5823 19953 : case REF_ARRAY:
5824 19953 : {
5825 19953 : switch (ref->u.ar.type)
5826 : {
5827 2195 : case AR_ELEMENT:
5828 2195 : case AR_SECTION:
5829 2195 : case AR_UNKNOWN:
5830 2195 : as = NULL;
5831 2195 : 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 1080469 : gfc_traverse_expr (gfc_expr *expr, gfc_symbol *sym,
5852 : bool (*func)(gfc_expr *, gfc_symbol *, int*),
5853 : int f)
5854 : {
5855 1080469 : gfc_array_ref ar;
5856 1080469 : gfc_ref *ref;
5857 1080469 : gfc_actual_arglist *args;
5858 1080469 : gfc_constructor *c;
5859 1080469 : int i;
5860 :
5861 1080469 : if (!expr)
5862 : return false;
5863 :
5864 509294 : 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 501464 : if (f >= 0
5870 477148 : && expr->ts.type == BT_CHARACTER
5871 12633 : && expr->ts.u.cl
5872 4515 : && expr->ts.u.cl->length
5873 2618 : && expr->ts.u.cl->length->expr_type != EXPR_CONSTANT
5874 502411 : && gfc_traverse_expr (expr->ts.u.cl->length, sym, func, f))
5875 : return true;
5876 :
5877 501463 : switch (expr->expr_type)
5878 : {
5879 21660 : case EXPR_PPC:
5880 21660 : case EXPR_COMPCALL:
5881 21660 : case EXPR_FUNCTION:
5882 49722 : for (args = expr->value.function.actual; args; args = args->next)
5883 : {
5884 28171 : 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 5321 : case EXPR_STRUCTURE:
5896 5321 : case EXPR_ARRAY:
5897 5321 : for (c = gfc_constructor_first (expr->value.constructor);
5898 31369 : c; c = gfc_constructor_next (c))
5899 : {
5900 26048 : if (gfc_traverse_expr (c->expr, sym, func, f))
5901 : return true;
5902 26048 : 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 10725 : case EXPR_OP:
5917 10725 : if (gfc_traverse_expr (expr->value.op.op1, sym, func, f))
5918 : return true;
5919 8978 : 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 499185 : break;
5935 : }
5936 :
5937 499185 : ref = expr->ref;
5938 512342 : while (ref != NULL)
5939 : {
5940 17242 : switch (ref->type)
5941 : {
5942 15343 : case REF_ARRAY:
5943 15343 : ar = ref->u.ar;
5944 189575 : for (i = 0; i < GFC_MAX_DIMENSIONS; i++)
5945 : {
5946 178139 : if (gfc_traverse_expr (ar.start[i], sym, func, f))
5947 : return true;
5948 174233 : if (gfc_traverse_expr (ar.end[i], sym, func, f))
5949 : return true;
5950 174232 : 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 1079 : case REF_COMPONENT:
5963 1079 : if (f >= 0
5964 1058 : && ref->u.c.component->ts.type == BT_CHARACTER
5965 97 : && ref->u.c.component->ts.u.cl
5966 97 : && ref->u.c.component->ts.u.cl->length
5967 97 : && ref->u.c.component->ts.u.cl->length->expr_type
5968 : != EXPR_CONSTANT
5969 1085 : && gfc_traverse_expr (ref->u.c.component->ts.u.cl->length,
5970 : sym, func, f))
5971 : return true;
5972 :
5973 1079 : if (ref->u.c.component->as)
5974 478 : for (i = 0; i < ref->u.c.component->as->rank
5975 906 : + ref->u.c.component->as->corank; i++)
5976 : {
5977 478 : if (gfc_traverse_expr (ref->u.c.component->as->lower[i],
5978 : sym, func, f))
5979 : return true;
5980 478 : 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 13157 : 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 3504798 : gfc_get_proc_ptr_comp (gfc_expr *expr)
6022 : {
6023 3504798 : gfc_ref *ref;
6024 :
6025 3504798 : if (!expr || !expr->ref)
6026 : return NULL;
6027 :
6028 : ref = expr->ref;
6029 315320 : while (ref->next)
6030 : ref = ref->next;
6031 :
6032 283943 : if (ref->type == REF_COMPONENT
6033 25413 : && ref->u.c.component->attr.proc_pointer)
6034 9501 : 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 1269678 : gfc_is_proc_ptr_comp (gfc_expr *expr)
6044 : {
6045 1269678 : 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 420801 : gfc_is_alloc_class_scalar_function (gfc_expr *expr)
6053 : {
6054 420801 : if (expr->expr_type == EXPR_FUNCTION
6055 77516 : && ((expr->value.function.esym
6056 43078 : && expr->value.function.esym->result
6057 43077 : && expr->value.function.esym->result->ts.type == BT_CLASS
6058 1103 : && !CLASS_DATA (expr->value.function.esym->result)->attr.dimension
6059 964 : && CLASS_DATA (expr->value.function.esym->result)->attr.allocatable)
6060 76828 : || (expr->ts.type == BT_CLASS
6061 764 : && CLASS_DATA (expr)->attr.allocatable
6062 397 : && !CLASS_DATA (expr)->attr.dimension)))
6063 928 : 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 179505 : gfc_is_class_array_function (gfc_expr *expr)
6073 : {
6074 179505 : if (expr->expr_type == EXPR_FUNCTION
6075 85168 : && expr->value.function.esym
6076 47179 : && expr->value.function.esym->result
6077 47178 : && expr->value.function.esym->result->ts.type == BT_CLASS
6078 2553 : && CLASS_DATA (expr->value.function.esym->result)->attr.dimension
6079 1620 : && (CLASS_DATA (expr->value.function.esym->result)->attr.allocatable
6080 324 : || CLASS_DATA (expr->value.function.esym->result)->attr.pointer))
6081 1620 : 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 85487 : expr_check_typed_help (gfc_expr* e, gfc_symbol* sym ATTRIBUTE_UNUSED,
6101 : int* f ATTRIBUTE_UNUSED)
6102 : {
6103 85487 : bool t;
6104 :
6105 85487 : if (e->expr_type != EXPR_VARIABLE)
6106 : return false;
6107 :
6108 2643 : gcc_assert (e->symtree);
6109 2643 : t = gfc_check_symbol_typed (e->symtree->n.sym, check_typed_ns,
6110 : true, e->where);
6111 :
6112 2643 : return (!t);
6113 : }
6114 :
6115 : bool
6116 91304 : gfc_expr_check_typed (gfc_expr* e, gfc_namespace* ns, bool strict)
6117 : {
6118 93222 : 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 93222 : if (!strict)
6123 : {
6124 92796 : if (e->expr_type == EXPR_VARIABLE && !e->ref)
6125 9743 : return gfc_check_symbol_typed (e->symtree->n.sym, ns, strict, e->where);
6126 :
6127 83053 : if (e->expr_type == EXPR_OP)
6128 : {
6129 2429 : bool t = true;
6130 :
6131 2429 : gcc_assert (e->value.op.op1);
6132 2429 : t = gfc_expr_check_typed (e->value.op.op1, ns, strict);
6133 :
6134 2429 : 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 81050 : check_typed_ns = ns;
6143 81050 : error_found = gfc_traverse_expr (e, NULL, &expr_check_typed_help, 0);
6144 :
6145 81050 : 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 187191 : derived_parameter_expr (gfc_expr* e, gfc_symbol* sym ATTRIBUTE_UNUSED,
6154 : int* f ATTRIBUTE_UNUSED)
6155 : {
6156 187191 : if (e->expr_type != EXPR_VARIABLE)
6157 : return false;
6158 :
6159 4126 : gcc_assert (e->symtree);
6160 4126 : if (e->symtree->n.sym->attr.pdt_kind
6161 3242 : || e->symtree->n.sym->attr.pdt_len)
6162 1569 : return true;
6163 :
6164 : return false;
6165 : }
6166 :
6167 :
6168 : bool
6169 154203 : gfc_derived_parameter_expr (gfc_expr *e)
6170 : {
6171 154203 : 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 408 : gfc_spec_list_type (gfc_actual_arglist *param_list, gfc_symbol *derived)
6184 : {
6185 408 : gfc_param_spec_type res = SPEC_EXPLICIT;
6186 408 : gfc_component *c;
6187 408 : bool seen_assumed = false;
6188 408 : bool seen_deferred = false;
6189 408 : bool seen_len = false;
6190 :
6191 408 : 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 893 : for (; param_list; param_list = param_list->next)
6201 : {
6202 555 : c = gfc_find_component (derived, param_list->name,
6203 : true, true, NULL);
6204 555 : gcc_assert (c != NULL);
6205 555 : if (c->attr.pdt_kind)
6206 262 : continue;
6207 293 : 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 338 : res = (seen_assumed || !seen_len) ? SPEC_ASSUMED : SPEC_DEFERRED;
6217 : }
6218 : return res;
6219 : }
6220 :
6221 :
6222 : bool
6223 32881 : gfc_ref_this_image (gfc_ref *ref)
6224 : {
6225 32881 : int n;
6226 :
6227 32881 : gcc_assert (ref->type == REF_ARRAY && ref->u.ar.codimen > 0);
6228 :
6229 70866 : for (n = ref->u.ar.dimen; n < ref->u.ar.dimen + ref->u.ar.codimen; n++)
6230 42121 : 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 999866 : gfc_is_coindexed (gfc_expr *e)
6276 : {
6277 999866 : gfc_ref *ref;
6278 :
6279 999866 : 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 1543386 : for (ref = e->ref; ref; ref = ref->next)
6284 569759 : if (ref->type == REF_ARRAY && ref->u.ar.codimen > 0)
6285 26239 : 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 190149 : gfc_is_coarray (gfc_expr *e)
6298 : {
6299 190149 : gfc_ref *ref;
6300 190149 : gfc_symbol *sym;
6301 190149 : gfc_component *comp;
6302 190149 : bool coindexed;
6303 190149 : bool coarray;
6304 190149 : int i;
6305 :
6306 190149 : if (e->expr_type != EXPR_VARIABLE)
6307 : return false;
6308 :
6309 187250 : coindexed = false;
6310 187250 : sym = e->symtree->n.sym;
6311 :
6312 187250 : if (sym->ts.type == BT_CLASS && sym->attr.class_ok)
6313 19244 : coarray = CLASS_DATA (sym)->attr.codimension;
6314 : else
6315 168006 : coarray = sym->attr.codimension;
6316 :
6317 397205 : for (ref = e->ref; ref; ref = ref->next)
6318 209955 : switch (ref->type)
6319 : {
6320 29106 : case REF_COMPONENT:
6321 29106 : comp = ref->u.c.component;
6322 29106 : if (comp->ts.type == BT_CLASS && comp->attr.class_ok
6323 2802 : && (CLASS_DATA (comp)->attr.class_pointer
6324 2437 : || CLASS_DATA (comp)->attr.allocatable))
6325 : {
6326 2802 : coindexed = false;
6327 2802 : coarray = CLASS_DATA (comp)->attr.codimension;
6328 : }
6329 26304 : else if (comp->attr.pointer || comp->attr.allocatable)
6330 : {
6331 24230 : coindexed = false;
6332 24230 : coarray = comp->attr.codimension;
6333 : }
6334 : break;
6335 :
6336 180363 : case REF_ARRAY:
6337 180363 : if (!coarray)
6338 : break;
6339 :
6340 6695 : if (ref->u.ar.codimen > 0 && !gfc_ref_this_image (ref))
6341 : {
6342 : coindexed = true;
6343 : break;
6344 : }
6345 :
6346 10388 : 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 187250 : 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 453 : gfc_has_ultimate_pointer (gfc_expr *e)
6397 : {
6398 453 : gfc_ref *ref, *last = NULL;
6399 :
6400 453 : if (e->expr_type != EXPR_VARIABLE)
6401 : return false;
6402 :
6403 1154 : for (ref = e->ref; ref; ref = ref->next)
6404 701 : if (ref->type == REF_COMPONENT)
6405 156 : last = ref;
6406 :
6407 453 : 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 312 : else if (last)
6412 : return false;
6413 :
6414 312 : if (e->ts.type == BT_CLASS)
6415 2 : return CLASS_DATA (e)->attr.pointer_comp;
6416 310 : 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 129408 : gfc_is_simply_contiguous (gfc_expr *expr, bool strict, bool permit_element)
6430 : {
6431 129408 : bool colon;
6432 129408 : int i;
6433 129408 : gfc_array_ref *ar = NULL;
6434 129408 : gfc_ref *ref, *part_ref = NULL;
6435 129408 : gfc_symbol *sym;
6436 :
6437 129408 : if (expr->expr_type == EXPR_ARRAY)
6438 : return true;
6439 :
6440 129136 : 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 129129 : if (expr->expr_type == EXPR_FUNCTION)
6455 : {
6456 379 : 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 329 : return (expr->value.function.isym->id != GFC_ISYM_TRANSPOSE);
6461 50 : 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 48 : return (expr->value.function.esym->result->attr.contiguous
6465 48 : || !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 128750 : else if (expr->expr_type != EXPR_VARIABLE)
6486 : return false;
6487 :
6488 128697 : if (!permit_element && expr->rank == 0)
6489 : return false;
6490 :
6491 128696 : sym = expr->symtree->n.sym;
6492 128696 : const gfc_typespec *prev_ts = &sym->ts;
6493 261459 : for (ref = expr->ref; ref; ref = ref->next)
6494 : {
6495 132960 : if (ar)
6496 : return false; /* Array shall be last part-ref. */
6497 :
6498 132778 : 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 3733 : if (!(prev_ts->type == BT_CLASS
6505 531 : && prev_ts->u.derived->attr.is_class
6506 531 : && strcmp (ref->u.c.component->name, "_data") == 0))
6507 3300 : part_ref = ref;
6508 3733 : prev_ts = &ref->u.c.component->ts;
6509 : }
6510 129045 : else if (ref->type == REF_SUBSTRING)
6511 : return false;
6512 129038 : else if (ref->type == REF_INQUIRY)
6513 : return false;
6514 129030 : else if (ref->u.ar.type != AR_ELEMENT)
6515 110864 : ar = &ref->u.ar;
6516 : }
6517 :
6518 128499 : if ((part_ref
6519 2661 : && part_ref->u.c.component
6520 2661 : && !part_ref->u.c.component->attr.contiguous
6521 2652 : && IS_POINTER (part_ref->u.c.component))
6522 : || (!part_ref
6523 125838 : && !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 118686 : && !sym->attr.associate_var
6528 117757 : && (IS_POINTER (sym)
6529 112745 : || (sym->ts.type == BT_CLASS
6530 1340 : && sym->ts.u.derived->attr.is_class
6531 1340 : && CLASS_DATA (sym)->as
6532 1340 : && (CLASS_DATA (sym)->as->type == AS_ASSUMED_RANK
6533 1316 : || CLASS_DATA (sym)->as->type == AS_ASSUMED_SHAPE))
6534 112637 : || (sym->as
6535 111405 : && (sym->as->type == AS_ASSUMED_RANK
6536 110867 : || sym->as->type == AS_ASSUMED_SHAPE)))))
6537 : return false;
6538 :
6539 : /* An associate variable may point to a non-contiguous target. */
6540 117304 : if (!part_ref
6541 117304 : && sym->attr.associate_var && !sym->attr.contiguous
6542 929 : && sym->assoc
6543 929 : && sym->assoc->target
6544 929 : && !gfc_is_simply_contiguous (sym->assoc->target, strict,
6545 : permit_element))
6546 : return false;
6547 :
6548 118276 : if (!ar || ar->type == AR_FULL)
6549 : return true;
6550 :
6551 19782 : gcc_assert (ar->type == AR_SECTION);
6552 :
6553 : /* Check for simply contiguous array */
6554 : colon = true;
6555 41718 : for (i = 0; i < ar->dimen; i++)
6556 : {
6557 26426 : if (ar->dimen_type[i] == DIMEN_VECTOR)
6558 : return false;
6559 :
6560 25933 : if (ar->dimen_type[i] == DIMEN_ELEMENT)
6561 : {
6562 : /* An extent of one does not hurt as long as we aren't strict. */
6563 2840 : if (ar->as && ar->as->lower[i] && ar->as->upper[i]
6564 1820 : && ar->start[i]
6565 1820 : && gfc_dep_compare_expr (ar->as->lower[i], ar->as->upper[i]) == 0
6566 2841 : && gfc_dep_compare_expr (ar->as->lower[i], ar->start[i]) == 0)
6567 1 : continue;
6568 :
6569 2839 : colon = false;
6570 2839 : continue;
6571 : }
6572 :
6573 23093 : gcc_assert (ar->dimen_type[i] == DIMEN_RANGE);
6574 :
6575 :
6576 : /* If the previous section was not contiguous, that's an error,
6577 : unless we have effective only one element and checking is not
6578 : strict. */
6579 23093 : if (!colon && (strict || !ar->start[i] || !ar->end[i]
6580 665 : || ar->start[i]->expr_type != EXPR_CONSTANT
6581 537 : || ar->end[i]->expr_type != EXPR_CONSTANT
6582 363 : || mpz_cmp (ar->start[i]->value.integer,
6583 363 : ar->end[i]->value.integer) != 0))
6584 : return false;
6585 :
6586 : /* Following the standard, "(::1)" or - if known at compile time -
6587 : "(lbound:ubound)" are not simply contiguous; if strict
6588 : is false, they are regarded as simply contiguous. */
6589 20916 : if (ar->stride[i] && (strict || ar->stride[i]->expr_type != EXPR_CONSTANT
6590 1707 : || ar->stride[i]->ts.type != BT_INTEGER
6591 1707 : || mpz_cmp_si (ar->stride[i]->value.integer, 1) != 0))
6592 : return false;
6593 :
6594 19096 : if (ar->start[i]
6595 7161 : && (strict || ar->start[i]->expr_type != EXPR_CONSTANT
6596 6815 : || !ar->as->lower[i]
6597 4580 : || ar->as->lower[i]->expr_type != EXPR_CONSTANT
6598 4580 : || mpz_cmp (ar->start[i]->value.integer,
6599 4580 : ar->as->lower[i]->value.integer) != 0))
6600 19096 : colon = false;
6601 :
6602 19096 : if (ar->end[i]
6603 7282 : && (strict || ar->end[i]->expr_type != EXPR_CONSTANT
6604 5980 : || !ar->as->upper[i]
6605 4110 : || ar->as->upper[i]->expr_type != EXPR_CONSTANT
6606 4104 : || mpz_cmp (ar->end[i]->value.integer,
6607 4104 : ar->as->upper[i]->value.integer) != 0))
6608 21936 : colon = false;
6609 : }
6610 :
6611 : return true;
6612 : }
6613 :
6614 : /* Return true if the expression is guaranteed to be non-contiguous,
6615 : false if we cannot prove anything. It is probably best to call
6616 : this after gfc_is_simply_contiguous. If neither of them returns
6617 : true, we cannot say (at compile-time). */
6618 :
6619 : bool
6620 2879 : gfc_is_not_contiguous (gfc_expr *array)
6621 : {
6622 2879 : int i;
6623 2879 : gfc_array_ref *ar = NULL;
6624 2879 : gfc_ref *ref;
6625 2879 : bool previous_incomplete;
6626 :
6627 7148 : for (ref = array->ref; ref; ref = ref->next)
6628 : {
6629 : /* Array-ref shall be last ref. */
6630 :
6631 4335 : if (ar && ar->type != AR_ELEMENT)
6632 : return true;
6633 :
6634 4269 : if (ref->type == REF_ARRAY)
6635 2909 : ar = &ref->u.ar;
6636 : }
6637 :
6638 2813 : if (ar == NULL || ar->type != AR_SECTION)
6639 : return false;
6640 :
6641 : previous_incomplete = false;
6642 :
6643 : /* Check if we can prove that the array is not contiguous. */
6644 :
6645 1899 : for (i = 0; i < ar->dimen; i++)
6646 : {
6647 1073 : mpz_t arr_size, ref_size;
6648 :
6649 1073 : if (gfc_ref_dimen_size (ar, i, &ref_size, NULL))
6650 : {
6651 491 : if (gfc_dep_difference (ar->as->upper[i], ar->as->lower[i], &arr_size))
6652 : {
6653 : /* a(2:4,2:) is known to be non-contiguous, but
6654 : a(2:4,i:i) can be contiguous. */
6655 91 : mpz_add_ui (arr_size, arr_size, 1L);
6656 91 : if (previous_incomplete && mpz_cmp_si (ref_size, 1) != 0)
6657 : {
6658 6 : mpz_clear (arr_size);
6659 6 : mpz_clear (ref_size);
6660 19 : return true;
6661 : }
6662 85 : else if (mpz_cmp (arr_size, ref_size) != 0)
6663 52 : previous_incomplete = true;
6664 :
6665 85 : mpz_clear (arr_size);
6666 : }
6667 :
6668 : /* Check for a(::2), i.e. where the stride is not unity.
6669 : This is only done if there is more than one element in
6670 : the reference along this dimension. */
6671 :
6672 485 : if (mpz_cmp_ui (ref_size, 1) > 0 && ar->type == AR_SECTION
6673 473 : && ar->dimen_type[i] == DIMEN_RANGE
6674 419 : && ar->stride[i] && ar->stride[i]->expr_type == EXPR_CONSTANT
6675 21 : && mpz_cmp_si (ar->stride[i]->value.integer, 1) != 0)
6676 : {
6677 13 : mpz_clear (ref_size);
6678 13 : return true;
6679 : }
6680 :
6681 472 : mpz_clear (ref_size);
6682 : }
6683 : }
6684 : /* We didn't find anything definitive. */
6685 : return false;
6686 : }
6687 :
6688 : /* Build call to an intrinsic procedure. The number of arguments has to be
6689 : passed (rather than ending the list with a NULL value) because we may
6690 : want to add arguments but with a NULL-expression. */
6691 :
6692 : gfc_expr*
6693 25442 : gfc_build_intrinsic_call (gfc_namespace *ns, gfc_isym_id id, const char* name,
6694 : locus where, unsigned numarg, ...)
6695 : {
6696 25442 : gfc_expr* result;
6697 25442 : gfc_actual_arglist* atail;
6698 25442 : gfc_intrinsic_sym* isym;
6699 25442 : va_list ap;
6700 25442 : unsigned i;
6701 25442 : const char *mangled_name = gfc_get_string (GFC_PREFIX ("%s"), name);
6702 :
6703 25442 : isym = gfc_intrinsic_function_by_id (id);
6704 25442 : gcc_assert (isym);
6705 :
6706 25442 : result = gfc_get_expr ();
6707 25442 : result->expr_type = EXPR_FUNCTION;
6708 25442 : result->ts = isym->ts;
6709 25442 : result->where = where;
6710 25442 : result->value.function.name = mangled_name;
6711 25442 : result->value.function.isym = isym;
6712 :
6713 25442 : gfc_get_sym_tree (mangled_name, ns, &result->symtree, false);
6714 25442 : gfc_commit_symbol (result->symtree->n.sym);
6715 25442 : gcc_assert (result->symtree
6716 : && (result->symtree->n.sym->attr.flavor == FL_PROCEDURE
6717 : || result->symtree->n.sym->attr.flavor == FL_UNKNOWN));
6718 25442 : result->symtree->n.sym->intmod_sym_id = id;
6719 25442 : result->symtree->n.sym->attr.flavor = FL_PROCEDURE;
6720 25442 : result->symtree->n.sym->attr.intrinsic = 1;
6721 25442 : result->symtree->n.sym->attr.artificial = 1;
6722 :
6723 25442 : va_start (ap, numarg);
6724 25442 : atail = NULL;
6725 86788 : for (i = 0; i < numarg; ++i)
6726 : {
6727 61346 : if (atail)
6728 : {
6729 35904 : atail->next = gfc_get_actual_arglist ();
6730 35904 : atail = atail->next;
6731 : }
6732 : else
6733 25442 : atail = result->value.function.actual = gfc_get_actual_arglist ();
6734 :
6735 61346 : atail->expr = va_arg (ap, gfc_expr*);
6736 : }
6737 25442 : va_end (ap);
6738 :
6739 25442 : return result;
6740 : }
6741 :
6742 :
6743 : /* Check if a symbol referenced in a submodule is declared in the ancestor
6744 : module and not accessed by use-association, and that the submodule is a
6745 : descendant. */
6746 :
6747 : static bool
6748 4 : sym_is_from_ancestor (gfc_symbol *sym)
6749 : {
6750 4 : const char dot[2] = ".";
6751 : /* Symbols take the form module.submodule_ or module.name_. */
6752 4 : char ancestor_module[2 * GFC_MAX_SYMBOL_LEN + 2];
6753 4 : char *ancestor;
6754 :
6755 4 : if (sym == NULL
6756 4 : || sym->attr.use_assoc
6757 4 : || !sym->attr.used_in_submodule
6758 4 : || !sym->module
6759 4 : || !sym->ns->proc_name
6760 4 : || !sym->ns->proc_name->name)
6761 : return false;
6762 :
6763 4 : memset (ancestor_module, '\0', sizeof (ancestor_module));
6764 4 : strcpy (ancestor_module, sym->ns->proc_name->name);
6765 4 : ancestor = strtok (ancestor_module, dot);
6766 4 : return strcmp (ancestor, sym->module) == 0;
6767 : }
6768 :
6769 :
6770 : /* Check if an expression may appear in a variable definition context
6771 : (F2008, 16.6.7) or pointer association context (F2008, 16.6.8).
6772 : This is called from the various places when resolving
6773 : the pieces that make up such a context.
6774 : If own_scope is true (applies to, e.g., ac-implied-do/data-implied-do
6775 : variables), some checks are not performed.
6776 :
6777 : Optionally, a possible error message can be suppressed if context is NULL
6778 : and just the return status (true / false) be requested. */
6779 :
6780 : bool
6781 426100 : gfc_check_vardef_context (gfc_expr* e, bool pointer, bool alloc_obj,
6782 : bool own_scope, const char* context)
6783 : {
6784 426100 : gfc_symbol* sym = NULL;
6785 426100 : bool is_pointer;
6786 426100 : bool check_intentin;
6787 426100 : bool ptr_component;
6788 426100 : symbol_attribute attr;
6789 426100 : gfc_ref* ref;
6790 426100 : int i;
6791 :
6792 426100 : if (e->expr_type == EXPR_VARIABLE)
6793 : {
6794 425996 : gcc_assert (e->symtree);
6795 425996 : sym = e->symtree->n.sym;
6796 : }
6797 104 : else if (e->expr_type == EXPR_FUNCTION)
6798 : {
6799 48 : gcc_assert (e->symtree);
6800 48 : sym = e->value.function.esym ? e->value.function.esym : e->symtree->n.sym;
6801 : }
6802 :
6803 426100 : attr = gfc_expr_attr (e);
6804 426100 : if (!pointer && e->expr_type == EXPR_FUNCTION && attr.pointer)
6805 : {
6806 46 : if (!(gfc_option.allow_std & GFC_STD_F2008))
6807 : {
6808 1 : if (context)
6809 1 : gfc_error ("Fortran 2008: Pointer functions in variable definition"
6810 : " context (%s) at %L", context, &e->where);
6811 : return false;
6812 : }
6813 : }
6814 426054 : else if (e->expr_type != EXPR_VARIABLE)
6815 : {
6816 58 : if (context)
6817 55 : gfc_error ("Non-variable expression in variable definition context (%s)"
6818 : " at %L", context, &e->where);
6819 : return false;
6820 : }
6821 :
6822 426041 : if (!pointer && sym->attr.flavor == FL_PARAMETER)
6823 : {
6824 5 : if (context)
6825 5 : gfc_error ("Named constant %qs in variable definition context (%s)"
6826 : " at %L", sym->name, context, &e->where);
6827 : return false;
6828 : }
6829 408632 : if (!pointer && sym->attr.flavor != FL_VARIABLE
6830 11059 : && !(sym->attr.flavor == FL_PROCEDURE && sym == sym->result)
6831 595 : && !(sym->attr.flavor == FL_PROCEDURE && sym->attr.proc_pointer)
6832 33 : && !(sym->attr.flavor == FL_PROCEDURE
6833 33 : && sym->attr.function && attr.pointer))
6834 : {
6835 0 : if (context)
6836 0 : gfc_error ("%qs in variable definition context (%s) at %L is not"
6837 : " a variable", sym->name, context, &e->where);
6838 : return false;
6839 : }
6840 :
6841 : /* Find out whether the expr is a pointer; this also means following
6842 : component references to the last one. */
6843 426036 : is_pointer = (attr.pointer || attr.proc_pointer);
6844 426036 : if (pointer && !is_pointer)
6845 : {
6846 14 : if (context)
6847 9 : gfc_error ("Non-POINTER in pointer association context (%s)"
6848 : " at %L", context, &e->where);
6849 : return false;
6850 : }
6851 :
6852 426022 : if (e->ts.type == BT_DERIVED
6853 22813 : && e->ts.u.derived == NULL)
6854 : {
6855 1 : if (context)
6856 1 : gfc_error ("Type inaccessible in variable definition context (%s) "
6857 : "at %L", context, &e->where);
6858 : return false;
6859 : }
6860 :
6861 : /* F2008, C1303. */
6862 426021 : if (!alloc_obj
6863 392553 : && (attr.lock_comp
6864 392553 : || (e->ts.type == BT_DERIVED
6865 17521 : && e->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
6866 42 : && e->ts.u.derived->intmod_sym_id == ISOFORTRAN_LOCK_TYPE)))
6867 : {
6868 3 : if (context)
6869 3 : gfc_error ("LOCK_TYPE in variable definition context (%s) at %L",
6870 : context, &e->where);
6871 : return false;
6872 : }
6873 :
6874 : /* TS18508, C702/C203. */
6875 392550 : if (!alloc_obj
6876 : && (attr.lock_comp
6877 392550 : || (e->ts.type == BT_DERIVED
6878 17518 : && e->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
6879 39 : && e->ts.u.derived->intmod_sym_id == ISOFORTRAN_EVENT_TYPE)))
6880 : {
6881 0 : if (context)
6882 0 : gfc_error ("LOCK_EVENT in variable definition context (%s) at %L",
6883 : context, &e->where);
6884 : return false;
6885 : }
6886 :
6887 : /* INTENT(IN) dummy argument. Check this, unless the object itself is the
6888 : component of sub-component of a pointer; we need to distinguish
6889 : assignment to a pointer component from pointer-assignment to a pointer
6890 : component. Note that (normal) assignment to procedure pointers is not
6891 : possible. */
6892 426018 : check_intentin = !own_scope;
6893 15744 : ptr_component = (sym->ts.type == BT_CLASS && sym->ts.u.derived
6894 15744 : && CLASS_DATA (sym))
6895 441762 : ? CLASS_DATA (sym)->attr.class_pointer : sym->attr.pointer;
6896 565183 : for (ref = e->ref; ref && check_intentin; ref = ref->next)
6897 : {
6898 : /* Associate-targets need special handling. Subobjects of an object with
6899 : the PROTECTED attribute inherit this attribute. */
6900 139173 : if (ptr_component && ref->type == REF_COMPONENT
6901 2438 : && !sym->assoc && !sym->attr.is_protected)
6902 139173 : check_intentin = false;
6903 139173 : if (ref->type == REF_COMPONENT)
6904 : {
6905 33754 : gfc_component *comp = ref->u.c.component;
6906 2641 : ptr_component = (comp->ts.type == BT_CLASS && comp->attr.class_ok)
6907 36395 : ? CLASS_DATA (comp)->attr.class_pointer
6908 31113 : : comp->attr.pointer;
6909 33754 : if (ptr_component && !pointer)
6910 4499 : check_intentin = false;
6911 : }
6912 139173 : if (ref->type == REF_INQUIRY
6913 90 : && (ref->u.i == INQUIRY_KIND || ref->u.i == INQUIRY_LEN))
6914 : {
6915 8 : if (context)
6916 16 : gfc_error ("%qs parameter inquiry for %qs in "
6917 : "variable definition context (%s) at %L",
6918 : ref->u.i == INQUIRY_KIND ? "KIND" : "LEN",
6919 : sym->name, context, &e->where);
6920 : return false;
6921 : }
6922 : }
6923 :
6924 : /* See if the INTENT(IN) check should apply to an ASSOCIATE target. */
6925 426010 : if (check_intentin && sym->assoc && sym->assoc->target)
6926 : {
6927 : gfc_expr *target;
6928 : gfc_symbol *tsym;
6929 :
6930 2116 : check_intentin = false;
6931 :
6932 : /* Walk through associate target chain to find a dummy argument. */
6933 2116 : for (target = sym->assoc->target; target; target = tsym->assoc->target)
6934 : {
6935 2116 : tsym = target->symtree ? target->symtree->n.sym : NULL;
6936 :
6937 2108 : if (tsym == NULL)
6938 : break;
6939 :
6940 2108 : if (tsym->attr.dummy)
6941 : {
6942 973 : check_intentin = (tsym->attr.intent == INTENT_IN);
6943 973 : break;
6944 : }
6945 :
6946 1135 : if (tsym->assoc == NULL)
6947 : break;
6948 : }
6949 : }
6950 :
6951 414135 : if (check_intentin
6952 412247 : && (sym->attr.intent == INTENT_IN
6953 412140 : || (sym->attr.select_type_temporary && sym->assoc
6954 15 : && sym->assoc->target && sym->assoc->target->symtree
6955 15 : && sym->assoc->target->symtree->n.sym->attr.intent == INTENT_IN)))
6956 : {
6957 107 : gfc_symtree *dummy = (sym->attr.select_type_temporary
6958 118 : ? sym->assoc->target->symtree : e->symtree);
6959 118 : const char *name = dummy->name;
6960 118 : if (pointer && is_pointer)
6961 : {
6962 18 : if (context)
6963 18 : gfc_error ("Dummy argument %qs with INTENT(IN) in pointer"
6964 : " association context (%s) at %L",
6965 : name, context, &e->where);
6966 : return false;
6967 : }
6968 : /* Only a nonpointer INTENT(IN) object is barred here (F2023:C846). */
6969 100 : if (!pointer && !is_pointer && !IS_POINTER (dummy->n.sym))
6970 : {
6971 32 : if (context)
6972 19 : gfc_error ("Dummy argument %qs with INTENT(IN) in variable"
6973 : " definition context (%s) at %L",
6974 : name, context, &e->where);
6975 : return false;
6976 : }
6977 : }
6978 :
6979 : /* PROTECTED and use-associated. */
6980 425960 : if (sym->attr.is_protected
6981 263 : && (sym->attr.use_assoc
6982 201 : || (sym->attr.used_in_submodule && !sym_is_from_ancestor (sym)))
6983 63 : && !own_scope
6984 426021 : && (check_intentin || !pointer))
6985 : {
6986 61 : if (pointer && is_pointer)
6987 : {
6988 16 : if (context)
6989 16 : gfc_error ("Variable %qs is PROTECTED and cannot appear in a "
6990 : "pointer association context (%s) at %L",
6991 : sym->name, context, &e->where);
6992 : return false;
6993 : }
6994 45 : if (!pointer && !is_pointer)
6995 : {
6996 25 : if (context)
6997 24 : gfc_error ("Variable %qs is PROTECTED and cannot appear in a "
6998 : "variable definition context (%s) at %L",
6999 : sym->name, context, &e->where);
7000 : return false;
7001 : }
7002 : }
7003 :
7004 : /* Variable not assignable from a PURE procedure but appears in
7005 : variable definition context. */
7006 1263022 : own_scope = own_scope
7007 425919 : || (sym->attr.result && sym->ns->proc_name
7008 9374 : && sym == sym->ns->proc_name->result);
7009 411192 : if (!pointer && !own_scope && gfc_pure (NULL) && gfc_impure_variable (sym))
7010 : {
7011 8 : if (context)
7012 8 : gfc_error ("Variable %qs cannot appear in a variable definition"
7013 : " context (%s) at %L in PURE procedure",
7014 : sym->name, context, &e->where);
7015 : return false;
7016 : }
7017 :
7018 402984 : if (!pointer && context && gfc_implicit_pure (NULL)
7019 439677 : && gfc_impure_variable (sym))
7020 : {
7021 1263 : gfc_namespace *ns;
7022 1263 : gfc_symbol *sym;
7023 :
7024 1361 : for (ns = gfc_current_ns; ns; ns = ns->parent)
7025 : {
7026 1361 : sym = ns->proc_name;
7027 1361 : if (sym == NULL)
7028 : break;
7029 1361 : if (sym->attr.flavor == FL_PROCEDURE)
7030 : {
7031 1263 : sym->attr.implicit_pure = 0;
7032 1263 : break;
7033 : }
7034 : }
7035 : }
7036 : /* Check variable definition context for associate-names. */
7037 425911 : if ((!pointer || check_intentin)
7038 425401 : && sym->assoc && !sym->attr.select_rank_temporary)
7039 : {
7040 1421 : const char* name;
7041 1421 : gfc_association_list* assoc;
7042 :
7043 1421 : gcc_assert (sym->assoc->target);
7044 :
7045 : /* If this is a SELECT TYPE temporary (the association is used internally
7046 : for SELECT TYPE), silently go over to the target. */
7047 1421 : if (sym->attr.select_type_temporary)
7048 : {
7049 1029 : gfc_expr* t = sym->assoc->target;
7050 :
7051 1029 : gcc_assert (t->expr_type == EXPR_VARIABLE);
7052 1029 : name = t->symtree->name;
7053 :
7054 1029 : if (t->symtree->n.sym->assoc)
7055 : assoc = t->symtree->n.sym->assoc;
7056 : else
7057 911 : assoc = sym->assoc;
7058 : }
7059 : else
7060 : {
7061 392 : name = sym->name;
7062 392 : assoc = sym->assoc;
7063 : }
7064 1421 : gcc_assert (name && assoc);
7065 :
7066 : /* Is association to a valid variable? */
7067 1421 : if (!assoc->variable)
7068 : {
7069 13 : if (context)
7070 : {
7071 13 : if (assoc->target->expr_type == EXPR_VARIABLE
7072 13 : && gfc_has_vector_index (assoc->target))
7073 4 : gfc_error ("%qs at %L associated to vector-indexed target"
7074 : " cannot be used in a variable definition"
7075 : " context (%s)",
7076 : name, &e->where, context);
7077 : else
7078 9 : gfc_error ("%qs at %L associated to expression"
7079 : " cannot be used in a variable definition"
7080 : " context (%s)",
7081 : name, &e->where, context);
7082 : }
7083 : return false;
7084 : }
7085 1408 : else if (context && gfc_is_ptr_fcn (assoc->target))
7086 : {
7087 11 : if (!gfc_notify_std (GFC_STD_F2018, "%qs at %L associated to "
7088 : "pointer function target being used in a "
7089 : "variable definition context (%s)", name,
7090 : &e->where, context))
7091 : return false;
7092 7 : else if (gfc_has_vector_index (e))
7093 : {
7094 0 : gfc_error ("%qs at %L associated to vector-indexed target"
7095 : " cannot be used in a variable definition"
7096 : " context (%s)",
7097 : name, &e->where, context);
7098 0 : return false;
7099 : }
7100 : }
7101 :
7102 : /* Target must be allowed to appear in a variable definition context.
7103 : Check valid assignment to pointers and invalid reassociations. */
7104 1404 : if (!gfc_check_vardef_context (assoc->target, pointer, false, false, NULL)
7105 1404 : && (!ptr_component || pointer))
7106 : {
7107 9 : if (context)
7108 6 : gfc_error ("Associate-name %qs cannot appear in a variable"
7109 : " definition context (%s) at %L because its target"
7110 : " at %L cannot, either",
7111 : name, context, &e->where,
7112 6 : &assoc->target->where);
7113 : return false;
7114 : }
7115 : }
7116 :
7117 : /* Check for same value in vector expression subscript. */
7118 :
7119 425885 : if (e->rank > 0)
7120 165650 : for (ref = e->ref; ref != NULL; ref = ref->next)
7121 83598 : if (ref->type == REF_ARRAY && ref->u.ar.type == AR_SECTION)
7122 20836 : for (i = 0; i < GFC_MAX_DIMENSIONS
7123 32580 : && ref->u.ar.dimen_type[i] != 0; i++)
7124 20843 : if (ref->u.ar.dimen_type[i] == DIMEN_VECTOR)
7125 : {
7126 500 : gfc_expr *arr = ref->u.ar.start[i];
7127 500 : if (arr->expr_type == EXPR_ARRAY)
7128 : {
7129 265 : gfc_constructor *c, *n;
7130 265 : gfc_expr *ec, *en;
7131 :
7132 265 : for (c = gfc_constructor_first (arr->value.constructor);
7133 820 : c != NULL; c = gfc_constructor_next (c))
7134 : {
7135 562 : if (c == NULL || c->iterator != NULL)
7136 12 : continue;
7137 :
7138 550 : ec = c->expr;
7139 :
7140 909 : for (n = gfc_constructor_next (c); n != NULL;
7141 359 : n = gfc_constructor_next (n))
7142 : {
7143 366 : if (n->iterator != NULL)
7144 12 : continue;
7145 :
7146 354 : en = n->expr;
7147 354 : if (gfc_dep_compare_expr (ec, en) == 0)
7148 : {
7149 7 : if (context)
7150 7 : gfc_error_now ("Elements with the same value "
7151 : "at %L and %L in vector "
7152 : "subscript in a variable "
7153 : "definition context (%s)",
7154 : &(ec->where), &(en->where),
7155 : context);
7156 : return false;
7157 : }
7158 : }
7159 : }
7160 : }
7161 : }
7162 :
7163 : return true;
7164 : }
7165 :
7166 : gfc_expr*
7167 24 : gfc_pdt_find_component_copy_initializer (gfc_symbol *sym, const char *name)
7168 : {
7169 : /* The actual length of a pdt is in its components. In the
7170 : initializer of the current ref is only the default value.
7171 : Therefore traverse the chain of components and pick the correct
7172 : one's initializer expressions. */
7173 24 : for (gfc_component *comp = sym->ts.u.derived->components; comp != NULL;
7174 0 : comp = comp->next)
7175 : {
7176 24 : if (!strcmp (comp->name, name))
7177 24 : return gfc_copy_expr (comp->initializer);
7178 : }
7179 : return NULL;
7180 : }
7181 :
7182 :
7183 : /* Test for parameterized array or string components. */
7184 :
7185 12507 : bool has_parameterized_comps (gfc_symbol * der_type)
7186 : {
7187 12507 : bool parameterized_comps = false;
7188 :
7189 12507 : if (!der_type->attr.pdt_type && !der_type->attr.pdt_comp)
7190 : return false;
7191 :
7192 14894 : for (gfc_component *c = der_type->components; c; c = c->next)
7193 10864 : if (c->attr.pdt_array || c->attr.pdt_string)
7194 : parameterized_comps = true;
7195 8485 : else if (IS_PDT (c) && strcmp (der_type->name, c->ts.u.derived->name)
7196 561 : && has_parameterized_comps (c->ts.u.derived))
7197 : parameterized_comps = true;
7198 :
7199 : return parameterized_comps;
7200 : }
|