Debian Codegraph @

gcc/fortran/expr.cc

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