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