libcpp, c, middle-end: Optimize initializers using #embed in C
[official-gcc.git] / gcc / fortran / expr.cc
blob36baa9bb4c8e612964eebabf1a9938d85d7b29c6
1 /* Routines for manipulation of expression nodes.
2 Copyright (C) 2000-2024 Free Software Foundation, Inc.
3 Contributed by Andy Vaught
5 This file is part of GCC.
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.
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.
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/>. */
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"
33 /* The following set of functions provide access to gfc_expr* of
34 various types - actual all but EXPR_FUNCTION and EXPR_VARIABLE.
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. */
42 /* Get a new expression node. */
44 gfc_expr *
45 gfc_get_expr (void)
47 gfc_expr *e;
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;
58 /* Get a new expression node that is an array constructor
59 of given type and kind. */
61 gfc_expr *
62 gfc_get_array_expr (bt type, int kind, locus *where)
64 gfc_expr *e;
66 e = gfc_get_expr ();
67 e->expr_type = EXPR_ARRAY;
68 e->value.constructor = NULL;
69 e->rank = 1;
70 e->shape = NULL;
72 e->ts.type = type;
73 e->ts.kind = kind;
74 if (where)
75 e->where = *where;
77 return e;
81 /* Get a new expression node that is the NULL expression. */
83 gfc_expr *
84 gfc_get_null_expr (locus *where)
86 gfc_expr *e;
88 e = gfc_get_expr ();
89 e->expr_type = EXPR_NULL;
90 e->ts.type = BT_UNKNOWN;
92 if (where)
93 e->where = *where;
95 return e;
99 /* Get a new expression node that is an operator expression node. */
101 gfc_expr *
102 gfc_get_operator_expr (locus *where, gfc_intrinsic_op op,
103 gfc_expr *op1, gfc_expr *op2)
105 gfc_expr *e;
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;
113 if (where)
114 e->where = *where;
116 return e;
120 /* Get a new expression node that is an structure constructor
121 of given type and kind. */
123 gfc_expr *
124 gfc_get_structure_constructor_expr (bt type, int kind, locus *where)
126 gfc_expr *e;
128 e = gfc_get_expr ();
129 e->expr_type = EXPR_STRUCTURE;
130 e->value.constructor = NULL;
132 e->ts.type = type;
133 e->ts.kind = kind;
134 if (where)
135 e->where = *where;
137 return e;
141 /* Get a new expression node that is an constant of given type and kind. */
143 gfc_expr *
144 gfc_get_constant_expr (bt type, int kind, locus *where)
146 gfc_expr *e;
148 if (!where)
149 gfc_internal_error ("gfc_get_constant_expr(): locus %<where%> cannot be "
150 "NULL");
152 e = gfc_get_expr ();
154 e->expr_type = EXPR_CONSTANT;
155 e->ts.type = type;
156 e->ts.kind = kind;
157 e->where = *where;
159 switch (type)
161 case BT_INTEGER:
162 case BT_UNSIGNED:
163 mpz_init (e->value.integer);
164 break;
166 case BT_REAL:
167 gfc_set_model_kind (kind);
168 mpfr_init (e->value.real);
169 break;
171 case BT_COMPLEX:
172 gfc_set_model_kind (kind);
173 mpc_init2 (e->value.complex, mpfr_get_default_prec());
174 break;
176 default:
177 break;
180 return e;
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. */
188 gfc_expr *
189 gfc_get_character_expr (int kind, locus *where, const char *src, gfc_charlen_t len)
191 gfc_expr *e;
192 gfc_char_t *dest;
194 if (!src)
196 dest = gfc_get_wide_string (len + 1);
197 gfc_wide_memset (dest, ' ', len);
198 dest[len] = '\0';
200 else
201 dest = gfc_char_to_widechar (src);
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;
208 return e;
212 /* Get a new expression node that is an integer constant. */
214 gfc_expr *
215 gfc_get_int_expr (int kind, locus *where, HOST_WIDE_INT value)
217 gfc_expr *p;
218 p = gfc_get_constant_expr (BT_INTEGER, kind,
219 where ? where : &gfc_current_locus);
221 const wide_int w = wi::shwi (value, kind * BITS_PER_UNIT);
222 wi::to_mpz (w, p->value.integer, SIGNED);
224 return p;
227 /* Get a new expression node that is an unsigned constant. */
229 gfc_expr *
230 gfc_get_unsigned_expr (int kind, locus *where, HOST_WIDE_INT value)
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);
238 return p;
241 /* Get a new expression node that is a logical constant. */
243 gfc_expr *
244 gfc_get_logical_expr (int kind, locus *where, bool value)
246 gfc_expr *p;
247 p = gfc_get_constant_expr (BT_LOGICAL, kind,
248 where ? where : &gfc_current_locus);
250 p->value.logical = value;
252 return p;
256 gfc_expr *
257 gfc_get_iokind_expr (locus *where, io_kind k)
259 gfc_expr *e;
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. */
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;
271 return e;
275 /* Given an expression pointer, return a copy of the expression. This
276 subroutine is recursive. */
278 gfc_expr *
279 gfc_copy_expr (gfc_expr *p)
281 gfc_expr *q;
282 gfc_char_t *s;
283 char *c;
285 if (p == NULL)
286 return NULL;
288 q = gfc_get_expr ();
289 *q = *p;
291 switch (q->expr_type)
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;
300 case EXPR_CONSTANT:
301 /* Copy target representation, if it exists. */
302 if (p->representation.string)
304 c = XCNEWVEC (char, p->representation.length + 1);
305 q->representation.string = c;
306 memcpy (c, p->representation.string, (p->representation.length + 1));
309 /* Copy the values of any pointer components of p->value. */
310 switch (q->ts.type)
312 case BT_INTEGER:
313 case BT_UNSIGNED:
314 mpz_init_set (q->value.integer, p->value.integer);
315 break;
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;
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;
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
336 s = gfc_get_wide_string (p->value.character.length + 1);
337 q->value.character.string = s;
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))
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;
348 else
349 memcpy (s, p->value.character.string,
350 (p->value.character.length + 1) * sizeof (gfc_char_t));
352 break;
354 case BT_HOLLERITH:
355 case BT_LOGICAL:
356 case_bt_struct:
357 case BT_CLASS:
358 case BT_ASSUMED:
359 break; /* Already done. */
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;
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. */
376 break;
378 case EXPR_OP:
379 switch (q->value.op.op)
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;
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;
394 break;
396 case EXPR_FUNCTION:
397 q->value.function.actual =
398 gfc_copy_actual_arglist (p->value.function.actual);
399 break;
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;
408 case EXPR_STRUCTURE:
409 case EXPR_ARRAY:
410 q->value.constructor = gfc_constructor_copy (p->value.constructor);
411 break;
413 case EXPR_VARIABLE:
414 case EXPR_NULL:
415 break;
417 case EXPR_UNKNOWN:
418 gcc_unreachable ();
421 q->shape = gfc_copy_shape (p->shape, p->rank);
423 q->ref = gfc_copy_ref (p->ref);
425 if (p->param_list)
426 q->param_list = gfc_copy_actual_arglist (p->param_list);
428 return q;
432 void
433 gfc_clear_shape (mpz_t *shape, int rank)
435 int i;
437 for (i = 0; i < rank; i++)
438 mpz_clear (shape[i]);
442 void
443 gfc_free_shape (mpz_t **shape, int rank)
445 if (*shape == NULL)
446 return;
448 gfc_clear_shape (*shape, rank);
449 free (*shape);
450 *shape = NULL;
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. */
459 static void
460 free_expr0 (gfc_expr *e)
462 switch (e->expr_type)
464 case EXPR_CONSTANT:
465 /* Free any parts of the value that need freeing. */
466 switch (e->ts.type)
468 case BT_INTEGER:
469 mpz_clear (e->value.integer);
470 break;
472 case BT_REAL:
473 mpfr_clear (e->value.real);
474 break;
476 case BT_CHARACTER:
477 free (e->value.character.string);
478 break;
480 case BT_COMPLEX:
481 mpc_clear (e->value.complex);
482 break;
484 case BT_BOZ:
485 free (e->boz.str);
486 break;
488 default:
489 break;
492 /* Free the representation. */
493 free (e->representation.string);
495 break;
497 case EXPR_OP:
498 if (e->value.op.op1 != NULL)
499 gfc_free_expr (e->value.op.op1);
500 if (e->value.op.op2 != NULL)
501 gfc_free_expr (e->value.op.op2);
502 break;
504 case EXPR_FUNCTION:
505 gfc_free_actual_arglist (e->value.function.actual);
506 break;
508 case EXPR_COMPCALL:
509 case EXPR_PPC:
510 gfc_free_actual_arglist (e->value.compcall.actual);
511 break;
513 case EXPR_VARIABLE:
514 break;
516 case EXPR_ARRAY:
517 case EXPR_STRUCTURE:
518 gfc_constructor_free (e->value.constructor);
519 break;
521 case EXPR_SUBSTRING:
522 free (e->value.character.string);
523 break;
525 case EXPR_NULL:
526 break;
528 default:
529 gfc_internal_error ("free_expr0(): Bad expr type");
532 /* Free a shape array. */
533 gfc_free_shape (&e->shape, e->rank);
535 gfc_free_ref_list (e->ref);
537 gfc_free_actual_arglist (e->param_list);
539 memset (e, '\0', sizeof (gfc_expr));
543 /* Free an expression node and everything beneath it. */
545 void
546 gfc_free_expr (gfc_expr *e)
548 if (e == NULL)
549 return;
550 free_expr0 (e);
551 free (e);
555 /* Free an argument list and everything below it. */
557 void
558 gfc_free_actual_arglist (gfc_actual_arglist *a1)
560 gfc_actual_arglist *a2;
562 while (a1)
564 a2 = a1->next;
565 if (a1->expr)
566 gfc_free_expr (a1->expr);
567 free (a1->associated_dummy);
568 free (a1);
569 a1 = a2;
574 /* Copy an arglist structure and all of the arguments. */
576 gfc_actual_arglist *
577 gfc_copy_actual_arglist (gfc_actual_arglist *p)
579 gfc_actual_arglist *head, *tail, *new_arg;
581 head = tail = NULL;
583 for (; p; p = p->next)
585 new_arg = gfc_get_actual_arglist ();
586 *new_arg = *p;
588 if (p->associated_dummy != NULL)
590 new_arg->associated_dummy = gfc_get_dummy_arg ();
591 *new_arg->associated_dummy = *p->associated_dummy;
594 new_arg->expr = gfc_copy_expr (p->expr);
595 new_arg->next = NULL;
597 if (head == NULL)
598 head = new_arg;
599 else
600 tail->next = new_arg;
602 tail = new_arg;
605 return head;
609 /* Free a list of reference structures. */
611 void
612 gfc_free_ref_list (gfc_ref *p)
614 gfc_ref *q;
615 int i;
617 for (; p; p = q)
619 q = p->next;
621 switch (p->type)
623 case REF_ARRAY:
624 for (i = 0; i < GFC_MAX_DIMENSIONS; i++)
626 gfc_free_expr (p->u.ar.start[i]);
627 gfc_free_expr (p->u.ar.end[i]);
628 gfc_free_expr (p->u.ar.stride[i]);
631 break;
633 case REF_SUBSTRING:
634 gfc_free_expr (p->u.ss.start);
635 gfc_free_expr (p->u.ss.end);
636 break;
638 case REF_COMPONENT:
639 case REF_INQUIRY:
640 break;
643 free (p);
648 /* Graft the *src expression onto the *dest subexpression. */
650 void
651 gfc_replace_expr (gfc_expr *dest, gfc_expr *src)
653 free_expr0 (dest);
654 *dest = *src;
655 free (src);
659 /* Try to extract an integer constant from the passed expression node.
660 Return true if some error occurred, false on success. If REPORT_ERROR
661 is non-zero, emit error, for positive REPORT_ERROR using gfc_error,
662 for negative using gfc_error_now. */
664 bool
665 gfc_extract_int (gfc_expr *expr, int *result, int report_error)
667 gfc_ref *ref;
669 /* A KIND component is a parameter too. The expression for it
670 is stored in the initializer and should be consistent with
671 the tests below. */
672 if (gfc_expr_attr(expr).pdt_kind)
674 for (ref = expr->ref; ref; ref = ref->next)
676 if (ref->u.c.component->attr.pdt_kind)
677 expr = ref->u.c.component->initializer;
681 if (expr->expr_type != EXPR_CONSTANT)
683 if (report_error > 0)
684 gfc_error ("Constant expression required at %C");
685 else if (report_error < 0)
686 gfc_error_now ("Constant expression required at %C");
687 return true;
690 if (expr->ts.type != BT_INTEGER)
692 if (report_error > 0)
693 gfc_error ("Integer expression required at %C");
694 else if (report_error < 0)
695 gfc_error_now ("Integer expression required at %C");
696 return true;
699 if ((mpz_cmp_si (expr->value.integer, INT_MAX) > 0)
700 || (mpz_cmp_si (expr->value.integer, INT_MIN) < 0))
702 if (report_error > 0)
703 gfc_error ("Integer value too large in expression at %C");
704 else if (report_error < 0)
705 gfc_error_now ("Integer value too large in expression at %C");
706 return true;
709 *result = (int) mpz_get_si (expr->value.integer);
711 return false;
714 /* Same as gfc_extract_int, but use a HWI. */
716 bool
717 gfc_extract_hwi (gfc_expr *expr, HOST_WIDE_INT *result, int report_error)
719 gfc_ref *ref;
721 /* A KIND component is a parameter too. The expression for it is
722 stored in the initializer and should be consistent with the tests
723 below. */
724 if (gfc_expr_attr(expr).pdt_kind)
726 for (ref = expr->ref; ref; ref = ref->next)
728 if (ref->u.c.component->attr.pdt_kind)
729 expr = ref->u.c.component->initializer;
733 if (expr->expr_type != EXPR_CONSTANT)
735 if (report_error > 0)
736 gfc_error ("Constant expression required at %C");
737 else if (report_error < 0)
738 gfc_error_now ("Constant expression required at %C");
739 return true;
742 if (expr->ts.type != BT_INTEGER)
744 if (report_error > 0)
745 gfc_error ("Integer expression required at %C");
746 else if (report_error < 0)
747 gfc_error_now ("Integer expression required at %C");
748 return true;
751 /* Use long_long_integer_type_node to determine when to saturate. */
752 const wide_int val = wi::from_mpz (long_long_integer_type_node,
753 expr->value.integer, false);
755 if (!wi::fits_shwi_p (val))
757 if (report_error > 0)
758 gfc_error ("Integer value too large in expression at %C");
759 else if (report_error < 0)
760 gfc_error_now ("Integer value too large in expression at %C");
761 return true;
764 *result = val.to_shwi ();
766 return false;
770 /* Recursively copy a list of reference structures. */
772 gfc_ref *
773 gfc_copy_ref (gfc_ref *src)
775 gfc_array_ref *ar;
776 gfc_ref *dest;
778 if (src == NULL)
779 return NULL;
781 dest = gfc_get_ref ();
782 dest->type = src->type;
784 switch (src->type)
786 case REF_ARRAY:
787 ar = gfc_copy_array_ref (&src->u.ar);
788 dest->u.ar = *ar;
789 free (ar);
790 break;
792 case REF_COMPONENT:
793 dest->u.c = src->u.c;
794 break;
796 case REF_INQUIRY:
797 dest->u.i = src->u.i;
798 break;
800 case REF_SUBSTRING:
801 dest->u.ss = src->u.ss;
802 dest->u.ss.start = gfc_copy_expr (src->u.ss.start);
803 dest->u.ss.end = gfc_copy_expr (src->u.ss.end);
804 break;
807 dest->next = gfc_copy_ref (src->next);
809 return dest;
813 /* Detect whether an expression has any vector index array references. */
815 bool
816 gfc_has_vector_index (gfc_expr *e)
818 gfc_ref *ref;
819 int i;
820 for (ref = e->ref; ref; ref = ref->next)
821 if (ref->type == REF_ARRAY)
822 for (i = 0; i < ref->u.ar.dimen; i++)
823 if (ref->u.ar.dimen_type[i] == DIMEN_VECTOR)
824 return 1;
825 return 0;
829 bool
830 gfc_is_ptr_fcn (gfc_expr *e)
832 return e != NULL && e->expr_type == EXPR_FUNCTION
833 && gfc_expr_attr (e).pointer;
837 /* Copy a shape array. */
839 mpz_t *
840 gfc_copy_shape (mpz_t *shape, int rank)
842 mpz_t *new_shape;
843 int n;
845 if (shape == NULL)
846 return NULL;
848 new_shape = gfc_get_shape (rank);
850 for (n = 0; n < rank; n++)
851 mpz_init_set (new_shape[n], shape[n]);
853 return new_shape;
857 /* Copy a shape array excluding dimension N, where N is an integer
858 constant expression. Dimensions are numbered in Fortran style --
859 starting with ONE.
861 So, if the original shape array contains R elements
862 { s1 ... sN-1 sN sN+1 ... sR-1 sR}
863 the result contains R-1 elements:
864 { s1 ... sN-1 sN+1 ... sR-1}
866 If anything goes wrong -- N is not a constant, its value is out
867 of range -- or anything else, just returns NULL. */
869 mpz_t *
870 gfc_copy_shape_excluding (mpz_t *shape, int rank, gfc_expr *dim)
872 mpz_t *new_shape, *s;
873 int i, n;
875 if (shape == NULL
876 || rank <= 1
877 || dim == NULL
878 || dim->expr_type != EXPR_CONSTANT
879 || dim->ts.type != BT_INTEGER)
880 return NULL;
882 n = mpz_get_si (dim->value.integer);
883 n--; /* Convert to zero based index. */
884 if (n < 0 || n >= rank)
885 return NULL;
887 s = new_shape = gfc_get_shape (rank - 1);
889 for (i = 0; i < rank; i++)
891 if (i == n)
892 continue;
893 mpz_init_set (*s, shape[i]);
894 s++;
897 return new_shape;
901 /* Return the maximum kind of two expressions. In general, higher
902 kind numbers mean more precision for numeric types. */
905 gfc_kind_max (gfc_expr *e1, gfc_expr *e2)
907 return (e1->ts.kind > e2->ts.kind) ? e1->ts.kind : e2->ts.kind;
911 /* Returns nonzero if the type is numeric, zero otherwise. */
913 static bool
914 numeric_type (bt type)
916 return type == BT_COMPLEX || type == BT_REAL || type == BT_INTEGER
917 || type == BT_UNSIGNED;
921 /* Returns nonzero if the typespec is a numeric type, zero otherwise. */
923 bool
924 gfc_numeric_ts (gfc_typespec *ts)
926 return numeric_type (ts->type);
930 /* Return an expression node with an optional argument list attached.
931 A variable number of gfc_expr pointers are strung together in an
932 argument list with a NULL pointer terminating the list. */
934 gfc_expr *
935 gfc_build_conversion (gfc_expr *e)
937 gfc_expr *p;
939 p = gfc_get_expr ();
940 p->expr_type = EXPR_FUNCTION;
941 p->symtree = NULL;
942 p->value.function.actual = gfc_get_actual_arglist ();
943 p->value.function.actual->expr = e;
945 return p;
949 /* Given an expression node with some sort of numeric binary
950 expression, insert type conversions required to make the operands
951 have the same type. Conversion warnings are disabled if wconversion
952 is set to 0.
954 The exception is that the operands of an exponential don't have to
955 have the same type. If possible, the base is promoted to the type
956 of the exponent. For example, 1**2.3 becomes 1.0**2.3, but
957 1.0**2 stays as it is. */
959 void
960 gfc_type_convert_binary (gfc_expr *e, int wconversion)
962 gfc_expr *op1, *op2;
964 op1 = e->value.op.op1;
965 op2 = e->value.op.op2;
967 if (op1->ts.type == BT_UNKNOWN || op2->ts.type == BT_UNKNOWN)
969 gfc_clear_ts (&e->ts);
970 return;
973 /* Kind conversions of same type. */
974 if (op1->ts.type == op2->ts.type)
976 if (op1->ts.kind == op2->ts.kind)
978 /* No type conversions. */
979 e->ts = op1->ts;
980 goto done;
983 if (op1->ts.kind > op2->ts.kind)
984 gfc_convert_type_warn (op2, &op1->ts, 2, wconversion);
985 else
986 gfc_convert_type_warn (op1, &op2->ts, 2, wconversion);
988 e->ts = op1->ts;
989 goto done;
992 /* Integer combined with real or complex. */
993 if (op2->ts.type == BT_INTEGER)
995 e->ts = op1->ts;
997 /* Special case for ** operator. */
998 if (e->value.op.op == INTRINSIC_POWER)
999 goto done;
1001 gfc_convert_type_warn (e->value.op.op2, &e->ts, 2, wconversion);
1002 goto done;
1005 if (op1->ts.type == BT_INTEGER)
1007 e->ts = op2->ts;
1008 gfc_convert_type_warn (e->value.op.op1, &e->ts, 2, wconversion);
1009 goto done;
1012 /* Real combined with complex. */
1013 e->ts.type = BT_COMPLEX;
1014 if (op1->ts.kind > op2->ts.kind)
1015 e->ts.kind = op1->ts.kind;
1016 else
1017 e->ts.kind = op2->ts.kind;
1018 if (op1->ts.type != BT_COMPLEX || op1->ts.kind != e->ts.kind)
1019 gfc_convert_type_warn (e->value.op.op1, &e->ts, 2, wconversion);
1020 if (op2->ts.type != BT_COMPLEX || op2->ts.kind != e->ts.kind)
1021 gfc_convert_type_warn (e->value.op.op2, &e->ts, 2, wconversion);
1023 done:
1024 return;
1028 /* Standard intrinsics listed under F2018:10.1.12 (6), which are excluded in
1029 constant expressions, except TRANSFER (c.f. item (8)), which would need
1030 separate treatment. */
1032 static bool
1033 is_non_constant_intrinsic (gfc_expr *e)
1035 if (e->expr_type == EXPR_FUNCTION
1036 && e->value.function.isym)
1038 switch (e->value.function.isym->id)
1040 case GFC_ISYM_COMMAND_ARGUMENT_COUNT:
1041 case GFC_ISYM_GET_TEAM:
1042 case GFC_ISYM_NULL:
1043 case GFC_ISYM_NUM_IMAGES:
1044 case GFC_ISYM_TEAM_NUMBER:
1045 case GFC_ISYM_THIS_IMAGE:
1046 return true;
1048 default:
1049 return false;
1052 return false;
1056 /* Determine if an expression is constant in the sense of F08:7.1.12.
1057 * This function expects that the expression has already been simplified. */
1059 bool
1060 gfc_is_constant_expr (gfc_expr *e)
1062 gfc_constructor *c;
1063 gfc_actual_arglist *arg;
1065 if (e == NULL)
1066 return true;
1068 switch (e->expr_type)
1070 case EXPR_OP:
1071 return (gfc_is_constant_expr (e->value.op.op1)
1072 && (e->value.op.op2 == NULL
1073 || gfc_is_constant_expr (e->value.op.op2)));
1075 case EXPR_VARIABLE:
1076 /* The only context in which this can occur is in a parameterized
1077 derived type declaration, so returning true is OK. */
1078 if (e->symtree->n.sym->attr.pdt_len
1079 || e->symtree->n.sym->attr.pdt_kind)
1080 return true;
1081 return false;
1083 case EXPR_FUNCTION:
1084 case EXPR_PPC:
1085 case EXPR_COMPCALL:
1086 gcc_assert (e->symtree || e->value.function.esym
1087 || e->value.function.isym);
1089 /* Check for intrinsics excluded in constant expressions. */
1090 if (e->value.function.isym && is_non_constant_intrinsic (e))
1091 return false;
1093 /* Call to intrinsic with at least one argument. */
1094 if (e->value.function.isym && e->value.function.actual)
1096 for (arg = e->value.function.actual; arg; arg = arg->next)
1097 if (!gfc_is_constant_expr (arg->expr))
1098 return false;
1101 if (e->value.function.isym
1102 && (e->value.function.isym->elemental
1103 || e->value.function.isym->pure
1104 || e->value.function.isym->inquiry
1105 || e->value.function.isym->transformational))
1106 return true;
1108 return false;
1110 case EXPR_CONSTANT:
1111 case EXPR_NULL:
1112 return true;
1114 case EXPR_SUBSTRING:
1115 return e->ref == NULL || (gfc_is_constant_expr (e->ref->u.ss.start)
1116 && gfc_is_constant_expr (e->ref->u.ss.end));
1118 case EXPR_ARRAY:
1119 case EXPR_STRUCTURE:
1120 c = gfc_constructor_first (e->value.constructor);
1121 if ((e->expr_type == EXPR_ARRAY) && c && c->iterator)
1122 return gfc_constant_ac (e);
1124 for (; c; c = gfc_constructor_next (c))
1125 if (!gfc_is_constant_expr (c->expr))
1126 return false;
1128 return true;
1131 default:
1132 gfc_internal_error ("gfc_is_constant_expr(): Unknown expression type");
1133 return false;
1138 /* Is true if the expression or symbol is a passed CFI descriptor. */
1139 bool
1140 is_CFI_desc (gfc_symbol *sym, gfc_expr *e)
1142 if (sym == NULL
1143 && e && e->expr_type == EXPR_VARIABLE)
1144 sym = e->symtree->n.sym;
1146 if (sym && sym->attr.dummy
1147 && sym->ns->proc_name->attr.is_bind_c
1148 && (sym->attr.pointer
1149 || sym->attr.allocatable
1150 || (sym->attr.dimension
1151 && (sym->as->type == AS_ASSUMED_SHAPE
1152 || sym->as->type == AS_ASSUMED_RANK))
1153 || (sym->ts.type == BT_CHARACTER
1154 && (!sym->ts.u.cl || !sym->ts.u.cl->length))))
1155 return true;
1157 return false;
1161 /* Is true if an array reference is followed by a component or substring
1162 reference. */
1163 bool
1164 is_subref_array (gfc_expr * e)
1166 gfc_ref * ref;
1167 bool seen_array;
1168 gfc_symbol *sym;
1170 if (e->expr_type != EXPR_VARIABLE)
1171 return false;
1173 sym = e->symtree->n.sym;
1175 if (sym->attr.subref_array_pointer)
1176 return true;
1178 seen_array = false;
1180 for (ref = e->ref; ref; ref = ref->next)
1182 /* If we haven't seen the array reference and this is an intrinsic,
1183 what follows cannot be a subreference array, unless there is a
1184 substring reference. */
1185 if (!seen_array && ref->type == REF_COMPONENT
1186 && ref->u.c.component->ts.type != BT_CHARACTER
1187 && ref->u.c.component->ts.type != BT_CLASS
1188 && !gfc_bt_struct (ref->u.c.component->ts.type))
1189 return false;
1191 if (ref->type == REF_ARRAY
1192 && ref->u.ar.type != AR_ELEMENT)
1193 seen_array = true;
1195 if (seen_array
1196 && ref->type != REF_ARRAY)
1197 return seen_array;
1200 if (sym->ts.type == BT_CLASS
1201 && sym->attr.dummy
1202 && CLASS_DATA (sym)->attr.dimension
1203 && CLASS_DATA (sym)->attr.class_pointer)
1204 return true;
1206 return false;
1210 /* Try to collapse intrinsic expressions. */
1212 static bool
1213 simplify_intrinsic_op (gfc_expr *p, int type)
1215 gfc_intrinsic_op op;
1216 gfc_expr *op1, *op2, *result;
1218 if (p->value.op.op == INTRINSIC_USER)
1219 return true;
1221 op1 = p->value.op.op1;
1222 op2 = p->value.op.op2;
1223 op = p->value.op.op;
1225 if (!gfc_simplify_expr (op1, type))
1226 return false;
1227 if (!gfc_simplify_expr (op2, type))
1228 return false;
1230 if (!gfc_is_constant_expr (op1)
1231 || (op2 != NULL && !gfc_is_constant_expr (op2)))
1232 return true;
1234 /* Rip p apart. */
1235 p->value.op.op1 = NULL;
1236 p->value.op.op2 = NULL;
1238 switch (op)
1240 case INTRINSIC_PARENTHESES:
1241 result = gfc_parentheses (op1);
1242 break;
1244 case INTRINSIC_UPLUS:
1245 result = gfc_uplus (op1);
1246 break;
1248 case INTRINSIC_UMINUS:
1249 result = gfc_uminus (op1);
1250 break;
1252 case INTRINSIC_PLUS:
1253 result = gfc_add (op1, op2);
1254 break;
1256 case INTRINSIC_MINUS:
1257 result = gfc_subtract (op1, op2);
1258 break;
1260 case INTRINSIC_TIMES:
1261 result = gfc_multiply (op1, op2);
1262 break;
1264 case INTRINSIC_DIVIDE:
1265 result = gfc_divide (op1, op2);
1266 break;
1268 case INTRINSIC_POWER:
1269 result = gfc_power (op1, op2);
1270 break;
1272 case INTRINSIC_CONCAT:
1273 result = gfc_concat (op1, op2);
1274 break;
1276 case INTRINSIC_EQ:
1277 case INTRINSIC_EQ_OS:
1278 result = gfc_eq (op1, op2, op);
1279 break;
1281 case INTRINSIC_NE:
1282 case INTRINSIC_NE_OS:
1283 result = gfc_ne (op1, op2, op);
1284 break;
1286 case INTRINSIC_GT:
1287 case INTRINSIC_GT_OS:
1288 result = gfc_gt (op1, op2, op);
1289 break;
1291 case INTRINSIC_GE:
1292 case INTRINSIC_GE_OS:
1293 result = gfc_ge (op1, op2, op);
1294 break;
1296 case INTRINSIC_LT:
1297 case INTRINSIC_LT_OS:
1298 result = gfc_lt (op1, op2, op);
1299 break;
1301 case INTRINSIC_LE:
1302 case INTRINSIC_LE_OS:
1303 result = gfc_le (op1, op2, op);
1304 break;
1306 case INTRINSIC_NOT:
1307 result = gfc_not (op1);
1308 break;
1310 case INTRINSIC_AND:
1311 result = gfc_and (op1, op2);
1312 break;
1314 case INTRINSIC_OR:
1315 result = gfc_or (op1, op2);
1316 break;
1318 case INTRINSIC_EQV:
1319 result = gfc_eqv (op1, op2);
1320 break;
1322 case INTRINSIC_NEQV:
1323 result = gfc_neqv (op1, op2);
1324 break;
1326 default:
1327 gfc_internal_error ("simplify_intrinsic_op(): Bad operator");
1330 if (result == NULL)
1332 gfc_free_expr (op1);
1333 gfc_free_expr (op2);
1334 return false;
1337 result->rank = p->rank;
1338 result->corank = p->corank;
1339 result->where = p->where;
1340 gfc_replace_expr (p, result);
1342 return true;
1346 /* Subroutine to simplify constructor expressions. Mutually recursive
1347 with gfc_simplify_expr(). */
1349 static bool
1350 simplify_constructor (gfc_constructor_base base, int type)
1352 gfc_constructor *c;
1353 gfc_expr *p;
1355 for (c = gfc_constructor_first (base); c; c = gfc_constructor_next (c))
1357 if (c->iterator
1358 && (!gfc_simplify_expr(c->iterator->start, type)
1359 || !gfc_simplify_expr (c->iterator->end, type)
1360 || !gfc_simplify_expr (c->iterator->step, type)))
1361 return false;
1363 if (c->expr)
1365 /* Try and simplify a copy. Replace the original if successful
1366 but keep going through the constructor at all costs. Not
1367 doing so can make a dog's dinner of complicated things. */
1368 p = gfc_copy_expr (c->expr);
1370 if (!gfc_simplify_expr (p, type))
1372 gfc_free_expr (p);
1373 continue;
1376 gfc_replace_expr (c->expr, p);
1380 return true;
1384 /* Pull a single array element out of an array constructor. */
1386 static bool
1387 find_array_element (gfc_constructor_base base, gfc_array_ref *ar,
1388 gfc_constructor **rval)
1390 unsigned long nelemen;
1391 int i;
1392 mpz_t delta;
1393 mpz_t offset;
1394 mpz_t span;
1395 mpz_t tmp;
1396 gfc_constructor *cons;
1397 gfc_expr *e;
1398 bool t;
1400 t = true;
1401 e = NULL;
1403 mpz_init_set_ui (offset, 0);
1404 mpz_init (delta);
1405 mpz_init (tmp);
1406 mpz_init_set_ui (span, 1);
1407 for (i = 0; i < ar->dimen; i++)
1409 if (!gfc_reduce_init_expr (ar->as->lower[i])
1410 || !gfc_reduce_init_expr (ar->as->upper[i])
1411 || ar->as->upper[i]->expr_type != EXPR_CONSTANT
1412 || ar->as->lower[i]->expr_type != EXPR_CONSTANT)
1414 t = false;
1415 cons = NULL;
1416 goto depart;
1419 e = ar->start[i];
1420 if (e->expr_type != EXPR_CONSTANT)
1422 cons = NULL;
1423 goto depart;
1426 /* Check the bounds. */
1427 if ((ar->as->upper[i]
1428 && mpz_cmp (e->value.integer,
1429 ar->as->upper[i]->value.integer) > 0)
1430 || (mpz_cmp (e->value.integer,
1431 ar->as->lower[i]->value.integer) < 0))
1433 gfc_error ("Index in dimension %d is out of bounds "
1434 "at %L", i + 1, &ar->c_where[i]);
1435 cons = NULL;
1436 t = false;
1437 goto depart;
1440 mpz_sub (delta, e->value.integer, ar->as->lower[i]->value.integer);
1441 mpz_mul (delta, delta, span);
1442 mpz_add (offset, offset, delta);
1444 mpz_set_ui (tmp, 1);
1445 mpz_add (tmp, tmp, ar->as->upper[i]->value.integer);
1446 mpz_sub (tmp, tmp, ar->as->lower[i]->value.integer);
1447 mpz_mul (span, span, tmp);
1450 for (cons = gfc_constructor_first (base), nelemen = mpz_get_ui (offset);
1451 cons && nelemen > 0; cons = gfc_constructor_next (cons), nelemen--)
1453 if (cons->iterator)
1455 cons = NULL;
1456 goto depart;
1460 depart:
1461 mpz_clear (delta);
1462 mpz_clear (offset);
1463 mpz_clear (span);
1464 mpz_clear (tmp);
1465 *rval = cons;
1466 return t;
1470 /* Find a component of a structure constructor. */
1472 static gfc_constructor *
1473 find_component_ref (gfc_constructor_base base, gfc_ref *ref)
1475 gfc_component *pick = ref->u.c.component;
1476 gfc_constructor *c = gfc_constructor_first (base);
1478 gfc_symbol *dt = ref->u.c.sym;
1479 int ext = dt->attr.extension;
1481 /* For extended types, check if the desired component is in one of the
1482 * parent types. */
1483 while (ext > 0 && gfc_find_component (dt->components->ts.u.derived,
1484 pick->name, true, true, NULL))
1486 dt = dt->components->ts.u.derived;
1487 c = gfc_constructor_first (c->expr->value.constructor);
1488 ext--;
1491 gfc_component *comp = dt->components;
1492 while (comp != pick)
1494 comp = comp->next;
1495 c = gfc_constructor_next (c);
1498 return c;
1502 /* Replace an expression with the contents of a constructor, removing
1503 the subobject reference in the process. */
1505 static void
1506 remove_subobject_ref (gfc_expr *p, gfc_constructor *cons)
1508 gfc_expr *e;
1510 if (cons)
1512 e = cons->expr;
1513 cons->expr = NULL;
1515 else
1516 e = gfc_copy_expr (p);
1517 e->ref = p->ref->next;
1518 p->ref->next = NULL;
1519 gfc_replace_expr (p, e);
1523 /* Pull an array section out of an array constructor. */
1525 static bool
1526 find_array_section (gfc_expr *expr, gfc_ref *ref)
1528 int idx;
1529 int rank;
1530 int d;
1531 int shape_i;
1532 int limit;
1533 long unsigned one = 1;
1534 bool incr_ctr;
1535 mpz_t start[GFC_MAX_DIMENSIONS];
1536 mpz_t end[GFC_MAX_DIMENSIONS];
1537 mpz_t stride[GFC_MAX_DIMENSIONS];
1538 mpz_t delta[GFC_MAX_DIMENSIONS];
1539 mpz_t ctr[GFC_MAX_DIMENSIONS];
1540 mpz_t delta_mpz;
1541 mpz_t tmp_mpz;
1542 mpz_t nelts;
1543 mpz_t ptr;
1544 gfc_constructor_base base;
1545 gfc_constructor *cons, *vecsub[GFC_MAX_DIMENSIONS];
1546 gfc_expr *begin;
1547 gfc_expr *finish;
1548 gfc_expr *step;
1549 gfc_expr *upper;
1550 gfc_expr *lower;
1551 bool t;
1553 t = true;
1555 base = expr->value.constructor;
1556 expr->value.constructor = NULL;
1558 rank = ref->u.ar.as->rank;
1560 if (expr->shape == NULL)
1561 expr->shape = gfc_get_shape (rank);
1563 mpz_init_set_ui (delta_mpz, one);
1564 mpz_init_set_ui (nelts, one);
1565 mpz_init (tmp_mpz);
1566 mpz_init (ptr);
1568 /* Do the initialization now, so that we can cleanup without
1569 keeping track of where we were. */
1570 for (d = 0; d < rank; d++)
1572 mpz_init (delta[d]);
1573 mpz_init (start[d]);
1574 mpz_init (end[d]);
1575 mpz_init (ctr[d]);
1576 mpz_init (stride[d]);
1577 vecsub[d] = NULL;
1580 /* Build the counters to clock through the array reference. */
1581 shape_i = 0;
1582 for (d = 0; d < rank; d++)
1584 /* Make this stretch of code easier on the eye! */
1585 begin = ref->u.ar.start[d];
1586 finish = ref->u.ar.end[d];
1587 step = ref->u.ar.stride[d];
1588 lower = ref->u.ar.as->lower[d];
1589 upper = ref->u.ar.as->upper[d];
1591 if (!lower || !upper
1592 || lower->expr_type != EXPR_CONSTANT
1593 || upper->expr_type != EXPR_CONSTANT
1594 || lower->ts.type != BT_INTEGER
1595 || upper->ts.type != BT_INTEGER)
1597 t = false;
1598 goto cleanup;
1601 if (ref->u.ar.dimen_type[d] == DIMEN_VECTOR) /* Vector subscript. */
1603 gfc_constructor *ci;
1604 gcc_assert (begin);
1606 if (begin->expr_type != EXPR_ARRAY || !gfc_is_constant_expr (begin))
1608 t = false;
1609 goto cleanup;
1612 gcc_assert (begin->rank == 1);
1613 /* Zero-sized arrays have no shape and no elements, stop early. */
1614 if (!begin->shape)
1616 mpz_init_set_ui (nelts, 0);
1617 break;
1620 vecsub[d] = gfc_constructor_first (begin->value.constructor);
1621 mpz_set (ctr[d], vecsub[d]->expr->value.integer);
1622 mpz_mul (nelts, nelts, begin->shape[0]);
1623 mpz_set (expr->shape[shape_i++], begin->shape[0]);
1625 /* Check bounds. */
1626 for (ci = vecsub[d]; ci; ci = gfc_constructor_next (ci))
1628 if (mpz_cmp (ci->expr->value.integer, upper->value.integer) > 0
1629 || mpz_cmp (ci->expr->value.integer,
1630 lower->value.integer) < 0)
1632 gfc_error ("index in dimension %d is out of bounds "
1633 "at %L", d + 1, &ref->u.ar.c_where[d]);
1634 t = false;
1635 goto cleanup;
1639 else
1641 if ((begin && begin->expr_type != EXPR_CONSTANT)
1642 || (finish && finish->expr_type != EXPR_CONSTANT)
1643 || (step && step->expr_type != EXPR_CONSTANT))
1645 t = false;
1646 goto cleanup;
1649 /* Obtain the stride. */
1650 if (step)
1651 mpz_set (stride[d], step->value.integer);
1652 else
1653 mpz_set_ui (stride[d], one);
1655 if (mpz_cmp_ui (stride[d], 0) == 0)
1656 mpz_set_ui (stride[d], one);
1658 /* Obtain the start value for the index. */
1659 if (begin)
1660 mpz_set (start[d], begin->value.integer);
1661 else
1662 mpz_set (start[d], lower->value.integer);
1664 mpz_set (ctr[d], start[d]);
1666 /* Obtain the end value for the index. */
1667 if (finish)
1668 mpz_set (end[d], finish->value.integer);
1669 else
1670 mpz_set (end[d], upper->value.integer);
1672 /* Separate 'if' because elements sometimes arrive with
1673 non-null end. */
1674 if (ref->u.ar.dimen_type[d] == DIMEN_ELEMENT)
1675 mpz_set (end [d], begin->value.integer);
1677 /* Check the bounds. */
1678 if (mpz_cmp (ctr[d], upper->value.integer) > 0
1679 || mpz_cmp (end[d], upper->value.integer) > 0
1680 || mpz_cmp (ctr[d], lower->value.integer) < 0
1681 || mpz_cmp (end[d], lower->value.integer) < 0)
1683 gfc_error ("index in dimension %d is out of bounds "
1684 "at %L", d + 1, &ref->u.ar.c_where[d]);
1685 t = false;
1686 goto cleanup;
1689 /* Calculate the number of elements and the shape. */
1690 mpz_set (tmp_mpz, stride[d]);
1691 mpz_add (tmp_mpz, end[d], tmp_mpz);
1692 mpz_sub (tmp_mpz, tmp_mpz, ctr[d]);
1693 mpz_div (tmp_mpz, tmp_mpz, stride[d]);
1694 mpz_mul (nelts, nelts, tmp_mpz);
1696 /* An element reference reduces the rank of the expression; don't
1697 add anything to the shape array. */
1698 if (ref->u.ar.dimen_type[d] != DIMEN_ELEMENT)
1699 mpz_set (expr->shape[shape_i++], tmp_mpz);
1702 /* Calculate the 'stride' (=delta) for conversion of the
1703 counter values into the index along the constructor. */
1704 mpz_set (delta[d], delta_mpz);
1705 mpz_sub (tmp_mpz, upper->value.integer, lower->value.integer);
1706 mpz_add_ui (tmp_mpz, tmp_mpz, one);
1707 mpz_mul (delta_mpz, delta_mpz, tmp_mpz);
1710 cons = gfc_constructor_first (base);
1712 /* Now clock through the array reference, calculating the index in
1713 the source constructor and transferring the elements to the new
1714 constructor. */
1715 for (idx = 0; idx < (int) mpz_get_si (nelts); idx++)
1717 mpz_init_set_ui (ptr, 0);
1719 incr_ctr = true;
1720 for (d = 0; d < rank; d++)
1722 mpz_set (tmp_mpz, ctr[d]);
1723 mpz_sub (tmp_mpz, tmp_mpz, ref->u.ar.as->lower[d]->value.integer);
1724 mpz_mul (tmp_mpz, tmp_mpz, delta[d]);
1725 mpz_add (ptr, ptr, tmp_mpz);
1727 if (!incr_ctr) continue;
1729 if (ref->u.ar.dimen_type[d] == DIMEN_VECTOR) /* Vector subscript. */
1731 gcc_assert(vecsub[d]);
1733 if (!gfc_constructor_next (vecsub[d]))
1734 vecsub[d] = gfc_constructor_first (ref->u.ar.start[d]->value.constructor);
1735 else
1737 vecsub[d] = gfc_constructor_next (vecsub[d]);
1738 incr_ctr = false;
1740 mpz_set (ctr[d], vecsub[d]->expr->value.integer);
1742 else
1744 mpz_add (ctr[d], ctr[d], stride[d]);
1746 if (mpz_cmp_ui (stride[d], 0) > 0
1747 ? mpz_cmp (ctr[d], end[d]) > 0
1748 : mpz_cmp (ctr[d], end[d]) < 0)
1749 mpz_set (ctr[d], start[d]);
1750 else
1751 incr_ctr = false;
1755 limit = mpz_get_ui (ptr);
1756 if (limit >= flag_max_array_constructor)
1758 gfc_error ("The number of elements in the array constructor "
1759 "at %L requires an increase of the allowed %d "
1760 "upper limit. See %<-fmax-array-constructor%> "
1761 "option", &expr->where, flag_max_array_constructor);
1762 t = false;
1763 goto cleanup;
1766 cons = gfc_constructor_lookup (base, limit);
1767 if (cons == NULL)
1769 gfc_error ("Error in array constructor referenced at %L",
1770 &ref->u.ar.where);
1771 t = false;
1772 goto cleanup;
1774 gfc_constructor_append_expr (&expr->value.constructor,
1775 gfc_copy_expr (cons->expr), NULL);
1778 cleanup:
1780 mpz_clear (delta_mpz);
1781 mpz_clear (tmp_mpz);
1782 mpz_clear (nelts);
1783 for (d = 0; d < rank; d++)
1785 mpz_clear (delta[d]);
1786 mpz_clear (start[d]);
1787 mpz_clear (end[d]);
1788 mpz_clear (ctr[d]);
1789 mpz_clear (stride[d]);
1791 mpz_clear (ptr);
1792 gfc_constructor_free (base);
1793 return t;
1796 /* Pull a substring out of an expression. */
1798 static bool
1799 find_substring_ref (gfc_expr *p, gfc_expr **newp)
1801 gfc_charlen_t end;
1802 gfc_charlen_t start;
1803 gfc_charlen_t length;
1804 gfc_char_t *chr;
1806 if (p->ref->u.ss.start->expr_type != EXPR_CONSTANT
1807 || p->ref->u.ss.end->expr_type != EXPR_CONSTANT)
1808 return false;
1810 *newp = gfc_copy_expr (p);
1811 free ((*newp)->value.character.string);
1813 end = (gfc_charlen_t) mpz_get_si (p->ref->u.ss.end->value.integer);
1814 start = (gfc_charlen_t) mpz_get_si (p->ref->u.ss.start->value.integer);
1815 if (end >= start)
1816 length = end - start + 1;
1817 else
1818 length = 0;
1820 chr = (*newp)->value.character.string = gfc_get_wide_string (length + 1);
1821 (*newp)->value.character.length = length;
1822 memcpy (chr, &p->value.character.string[start - 1],
1823 length * sizeof (gfc_char_t));
1824 chr[length] = '\0';
1825 return true;
1829 /* Pull an inquiry result out of an expression. */
1831 static bool
1832 find_inquiry_ref (gfc_expr *p, gfc_expr **newp)
1834 gfc_ref *ref;
1835 gfc_ref *inquiry = NULL;
1836 gfc_expr *tmp;
1838 tmp = gfc_copy_expr (p);
1840 if (tmp->ref && tmp->ref->type == REF_INQUIRY)
1842 inquiry = tmp->ref;
1843 tmp->ref = NULL;
1845 else
1847 for (ref = tmp->ref; ref; ref = ref->next)
1848 if (ref->next && ref->next->type == REF_INQUIRY)
1850 inquiry = ref->next;
1851 ref->next = NULL;
1855 if (!inquiry)
1857 gfc_free_expr (tmp);
1858 return false;
1861 gfc_resolve_expr (tmp);
1863 /* Leave these to the backend since the type and kind is not confirmed until
1864 resolution. */
1865 if (IS_INFERRED_TYPE (tmp))
1866 goto cleanup;
1868 /* In principle there can be more than one inquiry reference. */
1869 for (; inquiry; inquiry = inquiry->next)
1871 switch (inquiry->u.i)
1873 case INQUIRY_LEN:
1874 if (tmp->ts.type != BT_CHARACTER)
1875 goto cleanup;
1877 if (!gfc_notify_std (GFC_STD_F2003, "LEN part_ref at %C"))
1878 goto cleanup;
1880 if (tmp->ts.u.cl->length
1881 && tmp->ts.u.cl->length->expr_type == EXPR_CONSTANT)
1882 *newp = gfc_copy_expr (tmp->ts.u.cl->length);
1883 else if (tmp->expr_type == EXPR_CONSTANT)
1884 *newp = gfc_get_int_expr (gfc_default_integer_kind,
1885 NULL, tmp->value.character.length);
1886 else if (gfc_init_expr_flag
1887 && tmp->ts.u.cl->length->symtree->n.sym->attr.pdt_len)
1888 *newp = gfc_pdt_find_component_copy_initializer (tmp->symtree->n
1889 .sym,
1890 tmp->ts.u.cl
1891 ->length->symtree
1892 ->n.sym->name);
1893 else
1894 goto cleanup;
1896 break;
1898 case INQUIRY_KIND:
1899 if (tmp->ts.type == BT_DERIVED || tmp->ts.type == BT_CLASS)
1900 goto cleanup;
1902 if (!gfc_notify_std (GFC_STD_F2003, "KIND part_ref at %C"))
1903 goto cleanup;
1905 *newp = gfc_get_int_expr (gfc_default_integer_kind,
1906 NULL, tmp->ts.kind);
1907 break;
1909 case INQUIRY_RE:
1910 if (tmp->ts.type != BT_COMPLEX || tmp->expr_type != EXPR_CONSTANT)
1911 goto cleanup;
1913 if (!gfc_notify_std (GFC_STD_F2008, "RE part_ref at %C"))
1914 goto cleanup;
1916 *newp = gfc_get_constant_expr (BT_REAL, tmp->ts.kind, &tmp->where);
1917 mpfr_set ((*newp)->value.real,
1918 mpc_realref (tmp->value.complex), GFC_RND_MODE);
1919 break;
1921 case INQUIRY_IM:
1922 if (tmp->ts.type != BT_COMPLEX || tmp->expr_type != EXPR_CONSTANT)
1923 goto cleanup;
1925 if (!gfc_notify_std (GFC_STD_F2008, "IM part_ref at %C"))
1926 goto cleanup;
1928 *newp = gfc_get_constant_expr (BT_REAL, tmp->ts.kind, &tmp->where);
1929 mpfr_set ((*newp)->value.real,
1930 mpc_imagref (tmp->value.complex), GFC_RND_MODE);
1931 break;
1933 // TODO: Fix leaking expr tmp, when simplify is done twice.
1934 if (inquiry->next)
1935 gfc_replace_expr (tmp, *newp);
1938 if (!(*newp))
1939 goto cleanup;
1940 else if ((*newp)->expr_type != EXPR_CONSTANT)
1942 gfc_free_expr (*newp);
1943 goto cleanup;
1946 gfc_free_expr (tmp);
1947 return true;
1949 cleanup:
1950 gfc_free_expr (tmp);
1951 return false;
1956 /* Simplify a subobject reference of a constructor. This occurs when
1957 parameter variable values are substituted. */
1959 static bool
1960 simplify_const_ref (gfc_expr *p)
1962 gfc_constructor *cons, *c;
1963 gfc_expr *newp = NULL;
1964 gfc_ref *last_ref;
1966 while (p->ref)
1968 switch (p->ref->type)
1970 case REF_ARRAY:
1971 switch (p->ref->u.ar.type)
1973 case AR_ELEMENT:
1974 /* <type/kind spec>, parameter :: x(<int>) = scalar_expr
1975 will generate this. */
1976 if (p->expr_type != EXPR_ARRAY)
1978 remove_subobject_ref (p, NULL);
1979 break;
1981 if (!find_array_element (p->value.constructor, &p->ref->u.ar, &cons))
1982 return false;
1984 if (!cons)
1985 return true;
1987 remove_subobject_ref (p, cons);
1988 break;
1990 case AR_SECTION:
1991 if (!find_array_section (p, p->ref))
1992 return false;
1993 p->ref->u.ar.type = AR_FULL;
1995 /* Fall through. */
1997 case AR_FULL:
1998 if (p->ref->next != NULL
1999 && (p->ts.type == BT_CHARACTER || gfc_bt_struct (p->ts.type)))
2001 for (c = gfc_constructor_first (p->value.constructor);
2002 c; c = gfc_constructor_next (c))
2004 c->expr->ref = gfc_copy_ref (p->ref->next);
2005 if (!simplify_const_ref (c->expr))
2006 return false;
2009 if (gfc_bt_struct (p->ts.type)
2010 && p->ref->next
2011 && (c = gfc_constructor_first (p->value.constructor)))
2013 /* There may have been component references. */
2014 p->ts = c->expr->ts;
2017 last_ref = p->ref;
2018 for (; last_ref->next; last_ref = last_ref->next) {};
2020 if (p->ts.type == BT_CHARACTER
2021 && last_ref->type == REF_SUBSTRING)
2023 /* If this is a CHARACTER array and we possibly took
2024 a substring out of it, update the type-spec's
2025 character length according to the first element
2026 (as all should have the same length). */
2027 gfc_charlen_t string_len;
2028 if ((c = gfc_constructor_first (p->value.constructor)))
2030 const gfc_expr* first = c->expr;
2031 gcc_assert (first->expr_type == EXPR_CONSTANT);
2032 gcc_assert (first->ts.type == BT_CHARACTER);
2033 string_len = first->value.character.length;
2035 else
2036 string_len = 0;
2038 if (!p->ts.u.cl)
2040 if (p->symtree)
2041 p->ts.u.cl = gfc_new_charlen (p->symtree->n.sym->ns,
2042 NULL);
2043 else
2044 p->ts.u.cl = gfc_new_charlen (gfc_current_ns,
2045 NULL);
2047 else
2048 gfc_free_expr (p->ts.u.cl->length);
2050 p->ts.u.cl->length
2051 = gfc_get_int_expr (gfc_charlen_int_kind,
2052 NULL, string_len);
2055 gfc_free_ref_list (p->ref);
2056 p->ref = NULL;
2057 break;
2059 default:
2060 return true;
2063 break;
2065 case REF_COMPONENT:
2066 cons = find_component_ref (p->value.constructor, p->ref);
2067 remove_subobject_ref (p, cons);
2068 break;
2070 case REF_INQUIRY:
2071 if (!find_inquiry_ref (p, &newp))
2072 return false;
2074 gfc_replace_expr (p, newp);
2075 gfc_free_ref_list (p->ref);
2076 p->ref = NULL;
2077 break;
2079 case REF_SUBSTRING:
2080 if (!find_substring_ref (p, &newp))
2081 return false;
2083 gfc_replace_expr (p, newp);
2084 gfc_free_ref_list (p->ref);
2085 p->ref = NULL;
2086 break;
2090 return true;
2094 /* Simplify a chain of references. */
2096 static bool
2097 simplify_ref_chain (gfc_ref *ref, int type, gfc_expr **p)
2099 int n;
2100 gfc_expr *newp = NULL;
2102 for (; ref; ref = ref->next)
2104 switch (ref->type)
2106 case REF_ARRAY:
2107 for (n = 0; n < ref->u.ar.dimen; n++)
2109 if (!gfc_simplify_expr (ref->u.ar.start[n], type))
2110 return false;
2111 if (!gfc_simplify_expr (ref->u.ar.end[n], type))
2112 return false;
2113 if (!gfc_simplify_expr (ref->u.ar.stride[n], type))
2114 return false;
2116 break;
2118 case REF_SUBSTRING:
2119 if (!gfc_simplify_expr (ref->u.ss.start, type))
2120 return false;
2121 if (!gfc_simplify_expr (ref->u.ss.end, type))
2122 return false;
2123 break;
2125 case REF_INQUIRY:
2126 if (!find_inquiry_ref (*p, &newp))
2127 return false;
2129 gfc_replace_expr (*p, newp);
2130 gfc_free_ref_list ((*p)->ref);
2131 (*p)->ref = NULL;
2132 return true;
2134 default:
2135 break;
2138 return true;
2142 /* Try to substitute the value of a parameter variable. */
2144 static bool
2145 simplify_parameter_variable (gfc_expr *p, int type)
2147 gfc_expr *e;
2148 bool t;
2150 /* Set rank and check array ref; as resolve_variable calls
2151 gfc_simplify_expr, call gfc_resolve_ref + gfc_expression_rank instead. */
2152 if (!gfc_resolve_ref (p))
2154 gfc_error_check ();
2155 return false;
2157 gfc_expression_rank (p);
2159 /* Is this an inquiry? */
2160 bool inquiry = false;
2161 gfc_ref* ref = p->ref;
2162 while (ref)
2164 if (ref->type == REF_INQUIRY)
2165 break;
2166 ref = ref->next;
2168 if (ref && ref->type == REF_INQUIRY)
2169 inquiry = ref->u.i == INQUIRY_LEN || ref->u.i == INQUIRY_KIND;
2171 if (gfc_is_size_zero_array (p))
2173 if (p->expr_type == EXPR_ARRAY)
2174 return true;
2176 e = gfc_get_expr ();
2177 e->expr_type = EXPR_ARRAY;
2178 e->ts = p->ts;
2179 e->rank = p->rank;
2180 e->corank = p->corank;
2181 e->value.constructor = NULL;
2182 e->shape = gfc_copy_shape (p->shape, p->rank);
2183 e->where = p->where;
2184 /* If %kind and %len are not used then we're done, otherwise
2185 drop through for simplification. */
2186 if (!inquiry)
2188 gfc_replace_expr (p, e);
2189 return true;
2192 else
2194 e = gfc_copy_expr (p->symtree->n.sym->value);
2195 if (e == NULL)
2196 return false;
2198 gfc_free_shape (&e->shape, e->rank);
2199 e->shape = gfc_copy_shape (p->shape, p->rank);
2200 e->rank = p->rank;
2201 e->corank = p->corank;
2203 if (e->ts.type == BT_CHARACTER && p->ts.u.cl)
2204 e->ts = p->ts;
2207 if (e->ts.type == BT_CHARACTER && e->ts.u.cl == NULL)
2208 e->ts.u.cl = gfc_new_charlen (gfc_current_ns, p->ts.u.cl);
2210 /* Do not copy subobject refs for constant. */
2211 if (e->expr_type != EXPR_CONSTANT && p->ref != NULL)
2212 e->ref = gfc_copy_ref (p->ref);
2213 t = gfc_simplify_expr (e, type);
2214 e->where = p->where;
2216 /* Only use the simplification if it eliminated all subobject references. */
2217 if (t && !e->ref)
2218 gfc_replace_expr (p, e);
2219 else
2220 gfc_free_expr (e);
2222 return t;
2226 static bool
2227 scalarize_intrinsic_call (gfc_expr *, bool init_flag);
2229 /* Given an expression, simplify it by collapsing constant
2230 expressions. Most simplification takes place when the expression
2231 tree is being constructed. If an intrinsic function is simplified
2232 at some point, we get called again to collapse the result against
2233 other constants.
2235 We work by recursively simplifying expression nodes, simplifying
2236 intrinsic functions where possible, which can lead to further
2237 constant collapsing. If an operator has constant operand(s), we
2238 rip the expression apart, and rebuild it, hoping that it becomes
2239 something simpler.
2241 The expression type is defined for:
2242 0 Basic expression parsing
2243 1 Simplifying array constructors -- will substitute
2244 iterator values.
2245 Returns false on error, true otherwise.
2246 NOTE: Will return true even if the expression cannot be simplified. */
2248 bool
2249 gfc_simplify_expr (gfc_expr *p, int type)
2251 gfc_actual_arglist *ap;
2252 gfc_intrinsic_sym* isym = NULL;
2255 if (p == NULL)
2256 return true;
2258 switch (p->expr_type)
2260 case EXPR_CONSTANT:
2261 if (p->ref && p->ref->type == REF_INQUIRY)
2262 simplify_ref_chain (p->ref, type, &p);
2263 break;
2264 case EXPR_NULL:
2265 break;
2267 case EXPR_FUNCTION:
2268 // For array-bound functions, we don't need to optimize
2269 // the 'array' argument. In particular, if the argument
2270 // is a PARAMETER, simplifying might convert an EXPR_VARIABLE
2271 // into an EXPR_ARRAY; the latter has lbound = 1, the former
2272 // can have any lbound.
2273 ap = p->value.function.actual;
2274 if (p->value.function.isym &&
2275 (p->value.function.isym->id == GFC_ISYM_LBOUND
2276 || p->value.function.isym->id == GFC_ISYM_UBOUND
2277 || p->value.function.isym->id == GFC_ISYM_LCOBOUND
2278 || p->value.function.isym->id == GFC_ISYM_UCOBOUND
2279 || p->value.function.isym->id == GFC_ISYM_SHAPE))
2280 ap = ap->next;
2282 for ( ; ap; ap = ap->next)
2283 if (!gfc_simplify_expr (ap->expr, type))
2284 return false;
2286 if (p->value.function.isym != NULL
2287 && gfc_intrinsic_func_interface (p, 1) == MATCH_ERROR)
2288 return false;
2290 if (p->symtree && (p->value.function.isym || p->ts.type == BT_UNKNOWN))
2292 isym = gfc_find_function (p->symtree->n.sym->name);
2293 if (isym && isym->elemental)
2294 scalarize_intrinsic_call (p, false);
2297 break;
2299 case EXPR_SUBSTRING:
2300 if (!simplify_ref_chain (p->ref, type, &p))
2301 return false;
2303 if (gfc_is_constant_expr (p))
2305 gfc_char_t *s;
2306 HOST_WIDE_INT start, end;
2308 start = 0;
2309 if (p->ref && p->ref->u.ss.start)
2311 gfc_extract_hwi (p->ref->u.ss.start, &start);
2312 start--; /* Convert from one-based to zero-based. */
2315 end = p->value.character.length;
2316 if (p->ref && p->ref->u.ss.end)
2317 gfc_extract_hwi (p->ref->u.ss.end, &end);
2319 if (end < start)
2320 end = start;
2322 s = gfc_get_wide_string (end - start + 2);
2323 memcpy (s, p->value.character.string + start,
2324 (end - start) * sizeof (gfc_char_t));
2325 s[end - start + 1] = '\0'; /* TODO: C-style string. */
2326 free (p->value.character.string);
2327 p->value.character.string = s;
2328 p->value.character.length = end - start;
2329 p->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
2330 p->ts.u.cl->length = gfc_get_int_expr (gfc_charlen_int_kind,
2331 NULL,
2332 p->value.character.length);
2333 gfc_free_ref_list (p->ref);
2334 p->ref = NULL;
2335 p->expr_type = EXPR_CONSTANT;
2337 break;
2339 case EXPR_OP:
2340 if (!simplify_intrinsic_op (p, type))
2341 return false;
2342 break;
2344 case EXPR_VARIABLE:
2345 /* Only substitute array parameter variables if we are in an
2346 initialization expression, or we want a subsection. */
2347 if (p->symtree->n.sym->attr.flavor == FL_PARAMETER
2348 && (gfc_init_expr_flag || p->ref
2349 || (p->symtree->n.sym->value
2350 && p->symtree->n.sym->value->expr_type != EXPR_ARRAY)))
2352 if (!simplify_parameter_variable (p, type))
2353 return false;
2354 break;
2357 if (type == 1)
2359 gfc_simplify_iterator_var (p);
2362 /* Simplify subcomponent references. */
2363 if (!simplify_ref_chain (p->ref, type, &p))
2364 return false;
2366 break;
2368 case EXPR_STRUCTURE:
2369 case EXPR_ARRAY:
2370 if (!simplify_ref_chain (p->ref, type, &p))
2371 return false;
2373 /* If the following conditions hold, we found something like kind type
2374 inquiry of the form a(2)%kind while simplify the ref chain. */
2375 if (p->expr_type == EXPR_CONSTANT && !p->ref && !p->rank && !p->shape)
2376 return true;
2378 if (!simplify_constructor (p->value.constructor, type))
2379 return false;
2381 if (p->expr_type == EXPR_ARRAY && p->ref && p->ref->type == REF_ARRAY
2382 && p->ref->u.ar.type == AR_FULL)
2383 gfc_expand_constructor (p, false);
2385 if (!simplify_const_ref (p))
2386 return false;
2388 break;
2390 case EXPR_COMPCALL:
2391 case EXPR_PPC:
2392 break;
2394 case EXPR_UNKNOWN:
2395 gcc_unreachable ();
2398 return true;
2402 /* Try simplification of an expression via gfc_simplify_expr.
2403 When an error occurs (arithmetic or otherwise), roll back. */
2405 bool
2406 gfc_try_simplify_expr (gfc_expr *e, int type)
2408 gfc_expr *n;
2409 bool t, saved_div0;
2411 if (e == NULL || e->expr_type == EXPR_CONSTANT)
2412 return true;
2414 saved_div0 = gfc_seen_div0;
2415 gfc_seen_div0 = false;
2416 n = gfc_copy_expr (e);
2417 t = gfc_simplify_expr (n, type) && !gfc_seen_div0;
2418 if (t)
2419 gfc_replace_expr (e, n);
2420 else
2421 gfc_free_expr (n);
2422 gfc_seen_div0 = saved_div0;
2423 return t;
2427 /* Returns the type of an expression with the exception that iterator
2428 variables are automatically integers no matter what else they may
2429 be declared as. */
2431 static bt
2432 et0 (gfc_expr *e)
2434 if (e->expr_type == EXPR_VARIABLE && gfc_check_iter_variable (e))
2435 return BT_INTEGER;
2437 return e->ts.type;
2441 /* Scalarize an expression for an elemental intrinsic call. */
2443 static bool
2444 scalarize_intrinsic_call (gfc_expr *e, bool init_flag)
2446 gfc_actual_arglist *a, *b;
2447 gfc_constructor_base ctor;
2448 gfc_constructor *args[5] = {}; /* Avoid uninitialized warnings. */
2449 gfc_constructor *ci, *new_ctor;
2450 gfc_expr *expr, *old, *p;
2451 int n, i, rank[5], array_arg;
2453 if (e == NULL)
2454 return false;
2456 a = e->value.function.actual;
2457 for (; a; a = a->next)
2458 if (a->expr && !gfc_is_constant_expr (a->expr))
2459 return false;
2461 /* Find which, if any, arguments are arrays. Assume that the old
2462 expression carries the type information and that the first arg
2463 that is an array expression carries all the shape information.*/
2464 n = array_arg = 0;
2465 a = e->value.function.actual;
2466 for (; a; a = a->next)
2468 n++;
2469 if (!a->expr || a->expr->expr_type != EXPR_ARRAY)
2470 continue;
2471 array_arg = n;
2472 expr = gfc_copy_expr (a->expr);
2473 break;
2476 if (!array_arg)
2477 return false;
2479 old = gfc_copy_expr (e);
2481 gfc_constructor_free (expr->value.constructor);
2482 expr->value.constructor = NULL;
2483 expr->ts = old->ts;
2484 expr->where = old->where;
2485 expr->expr_type = EXPR_ARRAY;
2487 /* Copy the array argument constructors into an array, with nulls
2488 for the scalars. */
2489 n = 0;
2490 a = old->value.function.actual;
2491 for (; a; a = a->next)
2493 /* Check that this is OK for an initialization expression. */
2494 if (a->expr && init_flag && !gfc_check_init_expr (a->expr))
2495 goto cleanup;
2497 rank[n] = 0;
2498 if (a->expr && a->expr->rank && a->expr->expr_type == EXPR_VARIABLE)
2500 rank[n] = a->expr->rank;
2501 ctor = a->expr->symtree->n.sym->value->value.constructor;
2502 args[n] = gfc_constructor_first (ctor);
2504 else if (a->expr && a->expr->expr_type == EXPR_ARRAY)
2506 if (a->expr->rank)
2507 rank[n] = a->expr->rank;
2508 else
2509 rank[n] = 1;
2510 ctor = gfc_constructor_copy (a->expr->value.constructor);
2511 args[n] = gfc_constructor_first (ctor);
2513 else
2514 args[n] = NULL;
2516 n++;
2519 /* Using the array argument as the master, step through the array
2520 calling the function for each element and advancing the array
2521 constructors together. */
2522 for (ci = args[array_arg - 1]; ci; ci = gfc_constructor_next (ci))
2524 new_ctor = gfc_constructor_append_expr (&expr->value.constructor,
2525 gfc_copy_expr (old), NULL);
2527 gfc_free_actual_arglist (new_ctor->expr->value.function.actual);
2528 a = NULL;
2529 b = old->value.function.actual;
2530 for (i = 0; i < n; i++)
2532 if (a == NULL)
2533 new_ctor->expr->value.function.actual
2534 = a = gfc_get_actual_arglist ();
2535 else
2537 a->next = gfc_get_actual_arglist ();
2538 a = a->next;
2541 if (args[i])
2542 a->expr = gfc_copy_expr (args[i]->expr);
2543 else
2544 a->expr = gfc_copy_expr (b->expr);
2546 b = b->next;
2549 /* Simplify the function calls. If the simplification fails, the
2550 error will be flagged up down-stream or the library will deal
2551 with it. */
2552 p = gfc_copy_expr (new_ctor->expr);
2554 if (!gfc_simplify_expr (p, init_flag))
2555 gfc_free_expr (p);
2556 else
2557 gfc_replace_expr (new_ctor->expr, p);
2559 for (i = 0; i < n; i++)
2560 if (args[i])
2561 args[i] = gfc_constructor_next (args[i]);
2563 for (i = 1; i < n; i++)
2564 if (rank[i] && ((args[i] != NULL && args[array_arg - 1] == NULL)
2565 || (args[i] == NULL && args[array_arg - 1] != NULL)))
2566 goto compliance;
2569 free_expr0 (e);
2570 *e = *expr;
2571 /* Free "expr" but not the pointers it contains. */
2572 free (expr);
2573 gfc_free_expr (old);
2574 return true;
2576 compliance:
2577 gfc_error_now ("elemental function arguments at %C are not compliant");
2579 cleanup:
2580 gfc_free_expr (expr);
2581 gfc_free_expr (old);
2582 return false;
2586 static bool
2587 check_intrinsic_op (gfc_expr *e, bool (*check_function) (gfc_expr *))
2589 gfc_expr *op1 = e->value.op.op1;
2590 gfc_expr *op2 = e->value.op.op2;
2592 if (!(*check_function)(op1))
2593 return false;
2595 switch (e->value.op.op)
2597 case INTRINSIC_UPLUS:
2598 case INTRINSIC_UMINUS:
2599 if (!numeric_type (et0 (op1)))
2600 goto not_numeric;
2601 break;
2603 case INTRINSIC_EQ:
2604 case INTRINSIC_EQ_OS:
2605 case INTRINSIC_NE:
2606 case INTRINSIC_NE_OS:
2607 case INTRINSIC_GT:
2608 case INTRINSIC_GT_OS:
2609 case INTRINSIC_GE:
2610 case INTRINSIC_GE_OS:
2611 case INTRINSIC_LT:
2612 case INTRINSIC_LT_OS:
2613 case INTRINSIC_LE:
2614 case INTRINSIC_LE_OS:
2615 if (!(*check_function)(op2))
2616 return false;
2618 if (!(et0 (op1) == BT_CHARACTER && et0 (op2) == BT_CHARACTER)
2619 && !(numeric_type (et0 (op1)) && numeric_type (et0 (op2))))
2621 gfc_error ("Numeric or CHARACTER operands are required in "
2622 "expression at %L", &e->where);
2623 return false;
2625 break;
2627 case INTRINSIC_PLUS:
2628 case INTRINSIC_MINUS:
2629 case INTRINSIC_TIMES:
2630 case INTRINSIC_DIVIDE:
2631 case INTRINSIC_POWER:
2632 if (!(*check_function)(op2))
2633 return false;
2635 if (!numeric_type (et0 (op1)) || !numeric_type (et0 (op2)))
2636 goto not_numeric;
2638 break;
2640 case INTRINSIC_CONCAT:
2641 if (!(*check_function)(op2))
2642 return false;
2644 if (et0 (op1) != BT_CHARACTER || et0 (op2) != BT_CHARACTER)
2646 gfc_error ("Concatenation operator in expression at %L "
2647 "must have two CHARACTER operands", &op1->where);
2648 return false;
2651 if (op1->ts.kind != op2->ts.kind)
2653 gfc_error ("Concat operator at %L must concatenate strings of the "
2654 "same kind", &e->where);
2655 return false;
2658 break;
2660 case INTRINSIC_NOT:
2661 if (et0 (op1) != BT_LOGICAL)
2663 gfc_error (".NOT. operator in expression at %L must have a LOGICAL "
2664 "operand", &op1->where);
2665 return false;
2668 break;
2670 case INTRINSIC_AND:
2671 case INTRINSIC_OR:
2672 case INTRINSIC_EQV:
2673 case INTRINSIC_NEQV:
2674 if (!(*check_function)(op2))
2675 return false;
2677 if (et0 (op1) != BT_LOGICAL || et0 (op2) != BT_LOGICAL)
2679 gfc_error ("LOGICAL operands are required in expression at %L",
2680 &e->where);
2681 return false;
2684 break;
2686 case INTRINSIC_PARENTHESES:
2687 break;
2689 default:
2690 gfc_error ("Only intrinsic operators can be used in expression at %L",
2691 &e->where);
2692 return false;
2695 return true;
2697 not_numeric:
2698 gfc_error ("Numeric operands are required in expression at %L", &e->where);
2700 return false;
2703 /* F2003, 7.1.7 (3): In init expression, allocatable components
2704 must not be data-initialized. */
2705 static bool
2706 check_alloc_comp_init (gfc_expr *e)
2708 gfc_component *comp;
2709 gfc_constructor *ctor;
2711 gcc_assert (e->expr_type == EXPR_STRUCTURE);
2712 gcc_assert (e->ts.type == BT_DERIVED || e->ts.type == BT_CLASS);
2714 for (comp = e->ts.u.derived->components,
2715 ctor = gfc_constructor_first (e->value.constructor);
2716 comp; comp = comp->next, ctor = gfc_constructor_next (ctor))
2718 if (comp->attr.allocatable && ctor->expr
2719 && ctor->expr->expr_type != EXPR_NULL)
2721 gfc_error ("Invalid initialization expression for ALLOCATABLE "
2722 "component %qs in structure constructor at %L",
2723 comp->name, &ctor->expr->where);
2724 return false;
2728 return true;
2731 static match
2732 check_init_expr_arguments (gfc_expr *e)
2734 gfc_actual_arglist *ap;
2736 for (ap = e->value.function.actual; ap; ap = ap->next)
2737 if (!gfc_check_init_expr (ap->expr))
2738 return MATCH_ERROR;
2740 return MATCH_YES;
2743 static bool check_restricted (gfc_expr *);
2745 /* F95, 7.1.6.1, Initialization expressions, (7)
2746 F2003, 7.1.7 Initialization expression, (8)
2747 F2008, 7.1.12 Constant expression, (4) */
2749 static match
2750 check_inquiry (gfc_expr *e, int not_restricted)
2752 const char *name;
2753 const char *const *functions;
2755 static const char *const inquiry_func_f95[] = {
2756 "lbound", "shape", "size", "ubound",
2757 "bit_size", "len", "kind",
2758 "digits", "epsilon", "huge", "maxexponent", "minexponent",
2759 "precision", "radix", "range", "tiny",
2760 NULL
2763 static const char *const inquiry_func_f2003[] = {
2764 "lbound", "shape", "size", "ubound",
2765 "bit_size", "len", "kind",
2766 "digits", "epsilon", "huge", "maxexponent", "minexponent",
2767 "precision", "radix", "range", "tiny",
2768 "new_line", NULL
2771 /* std=f2008+ or -std=gnu */
2772 static const char *const inquiry_func_gnu[] = {
2773 "lbound", "shape", "size", "ubound",
2774 "bit_size", "len", "kind",
2775 "digits", "epsilon", "huge", "maxexponent", "minexponent",
2776 "precision", "radix", "range", "tiny",
2777 "new_line", "storage_size", NULL
2780 int i = 0;
2781 gfc_actual_arglist *ap;
2782 gfc_symbol *sym;
2783 gfc_symbol *asym;
2785 if (!e->value.function.isym
2786 || !e->value.function.isym->inquiry)
2787 return MATCH_NO;
2789 /* An undeclared parameter will get us here (PR25018). */
2790 if (e->symtree == NULL)
2791 return MATCH_NO;
2793 sym = e->symtree->n.sym;
2795 if (sym->from_intmod)
2797 if (sym->from_intmod == INTMOD_ISO_FORTRAN_ENV
2798 && sym->intmod_sym_id != ISOFORTRAN_COMPILER_OPTIONS
2799 && sym->intmod_sym_id != ISOFORTRAN_COMPILER_VERSION)
2800 return MATCH_NO;
2802 if (sym->from_intmod == INTMOD_ISO_C_BINDING
2803 && sym->intmod_sym_id != ISOCBINDING_C_SIZEOF)
2804 return MATCH_NO;
2806 else
2808 name = sym->name;
2810 functions = inquiry_func_gnu;
2811 if (gfc_option.warn_std & GFC_STD_F2003)
2812 functions = inquiry_func_f2003;
2813 if (gfc_option.warn_std & GFC_STD_F95)
2814 functions = inquiry_func_f95;
2816 for (i = 0; functions[i]; i++)
2817 if (strcmp (functions[i], name) == 0)
2818 break;
2820 if (functions[i] == NULL)
2821 return MATCH_ERROR;
2824 /* At this point we have an inquiry function with a variable argument. The
2825 type of the variable might be undefined, but we need it now, because the
2826 arguments of these functions are not allowed to be undefined. */
2828 for (ap = e->value.function.actual; ap; ap = ap->next)
2830 if (!ap->expr)
2831 continue;
2833 asym = ap->expr->symtree ? ap->expr->symtree->n.sym : NULL;
2835 if (ap->expr->ts.type == BT_UNKNOWN)
2837 if (asym && asym->ts.type == BT_UNKNOWN
2838 && !gfc_set_default_type (asym, 0, gfc_current_ns))
2839 return MATCH_NO;
2841 ap->expr->ts = asym->ts;
2844 if (asym && asym->assoc && asym->assoc->target
2845 && asym->assoc->target->expr_type == EXPR_CONSTANT)
2847 gfc_free_expr (ap->expr);
2848 ap->expr = gfc_copy_expr (asym->assoc->target);
2851 /* Assumed character length will not reduce to a constant expression
2852 with LEN, as required by the standard. */
2853 if (i == 5 && not_restricted && asym
2854 && asym->ts.type == BT_CHARACTER
2855 && ((asym->ts.u.cl && asym->ts.u.cl->length == NULL)
2856 || asym->ts.deferred))
2858 gfc_error ("Assumed or deferred character length variable %qs "
2859 "in constant expression at %L",
2860 asym->name, &ap->expr->where);
2861 return MATCH_ERROR;
2863 else if (not_restricted && !gfc_check_init_expr (ap->expr))
2864 return MATCH_ERROR;
2866 if (not_restricted == 0
2867 && ap->expr->expr_type != EXPR_VARIABLE
2868 && !check_restricted (ap->expr))
2869 return MATCH_ERROR;
2871 if (not_restricted == 0
2872 && ap->expr->expr_type == EXPR_VARIABLE
2873 && asym->attr.dummy && asym->attr.optional)
2874 return MATCH_NO;
2877 return MATCH_YES;
2881 /* F95, 7.1.6.1, Initialization expressions, (5)
2882 F2003, 7.1.7 Initialization expression, (5) */
2884 static match
2885 check_transformational (gfc_expr *e)
2887 static const char * const trans_func_f95[] = {
2888 "repeat", "reshape", "selected_int_kind",
2889 "selected_real_kind", "transfer", "trim", NULL
2892 static const char * const trans_func_f2003[] = {
2893 "all", "any", "count", "dot_product", "matmul", "null", "pack",
2894 "product", "repeat", "reshape", "selected_char_kind", "selected_int_kind",
2895 "selected_real_kind", "spread", "sum", "transfer", "transpose",
2896 "trim", "unpack", NULL
2899 static const char * const trans_func_f2008[] = {
2900 "all", "any", "count", "dot_product", "matmul", "null", "pack",
2901 "product", "repeat", "reshape", "selected_char_kind", "selected_int_kind",
2902 "selected_real_kind", "spread", "sum", "transfer", "transpose",
2903 "trim", "unpack", "findloc", NULL
2906 static const char * const trans_func_f2023[] = {
2907 "all", "any", "count", "dot_product", "matmul", "null", "pack",
2908 "product", "repeat", "reshape", "selected_char_kind", "selected_int_kind",
2909 "selected_logical_kind", "selected_real_kind", "spread", "sum", "transfer",
2910 "transpose", "trim", "unpack", "findloc", NULL
2913 int i;
2914 const char *name;
2915 const char *const *functions;
2917 if (!e->value.function.isym
2918 || !e->value.function.isym->transformational)
2919 return MATCH_NO;
2921 name = e->symtree->n.sym->name;
2923 if (gfc_option.allow_std & GFC_STD_F2023)
2924 functions = trans_func_f2023;
2925 else if (gfc_option.allow_std & GFC_STD_F2008)
2926 functions = trans_func_f2008;
2927 else if (gfc_option.allow_std & GFC_STD_F2003)
2928 functions = trans_func_f2003;
2929 else
2930 functions = trans_func_f95;
2932 /* NULL() is dealt with below. */
2933 if (strcmp ("null", name) == 0)
2934 return MATCH_NO;
2936 for (i = 0; functions[i]; i++)
2937 if (strcmp (functions[i], name) == 0)
2938 break;
2940 if (functions[i] == NULL)
2942 gfc_error ("transformational intrinsic %qs at %L is not permitted "
2943 "in an initialization expression", name, &e->where);
2944 return MATCH_ERROR;
2947 return check_init_expr_arguments (e);
2951 /* F95, 7.1.6.1, Initialization expressions, (6)
2952 F2003, 7.1.7 Initialization expression, (6) */
2954 static match
2955 check_null (gfc_expr *e)
2957 if (strcmp ("null", e->symtree->n.sym->name) != 0)
2958 return MATCH_NO;
2960 return check_init_expr_arguments (e);
2964 static match
2965 check_elemental (gfc_expr *e)
2967 if (!e->value.function.isym
2968 || !e->value.function.isym->elemental)
2969 return MATCH_NO;
2971 if (e->ts.type != BT_INTEGER
2972 && e->ts.type != BT_CHARACTER
2973 && !gfc_notify_std (GFC_STD_F2003, "Evaluation of nonstandard "
2974 "initialization expression at %L", &e->where))
2975 return MATCH_ERROR;
2977 return check_init_expr_arguments (e);
2981 static match
2982 check_conversion (gfc_expr *e)
2984 if (!e->value.function.isym
2985 || !e->value.function.isym->conversion)
2986 return MATCH_NO;
2988 return check_init_expr_arguments (e);
2992 /* Verify that an expression is an initialization expression. A side
2993 effect is that the expression tree is reduced to a single constant
2994 node if all goes well. This would normally happen when the
2995 expression is constructed but function references are assumed to be
2996 intrinsics in the context of initialization expressions. If
2997 false is returned an error message has been generated. */
2999 bool
3000 gfc_check_init_expr (gfc_expr *e)
3002 match m;
3003 bool t;
3005 if (e == NULL)
3006 return true;
3008 switch (e->expr_type)
3010 case EXPR_OP:
3011 t = check_intrinsic_op (e, gfc_check_init_expr);
3012 if (t)
3013 t = gfc_simplify_expr (e, 0);
3015 break;
3017 case EXPR_FUNCTION:
3018 t = false;
3021 bool conversion;
3022 gfc_intrinsic_sym* isym = NULL;
3023 gfc_symbol* sym = e->symtree->n.sym;
3025 /* Simplify here the intrinsics from the IEEE_ARITHMETIC and
3026 IEEE_EXCEPTIONS modules. */
3027 int mod = sym->from_intmod;
3028 if (mod == INTMOD_NONE && sym->generic)
3029 mod = sym->generic->sym->from_intmod;
3030 if (mod == INTMOD_IEEE_ARITHMETIC || mod == INTMOD_IEEE_EXCEPTIONS)
3032 gfc_expr *new_expr = gfc_simplify_ieee_functions (e);
3033 if (new_expr)
3035 gfc_replace_expr (e, new_expr);
3036 t = true;
3037 break;
3041 /* If a conversion function, e.g., __convert_i8_i4, was inserted
3042 into an array constructor, we need to skip the error check here.
3043 Conversion errors are caught below in scalarize_intrinsic_call. */
3044 conversion = e->value.function.isym
3045 && (e->value.function.isym->conversion == 1);
3047 if (!conversion && (!gfc_is_intrinsic (sym, 0, e->where)
3048 || (m = gfc_intrinsic_func_interface (e, 0)) == MATCH_NO))
3050 gfc_error ("Function %qs in initialization expression at %L "
3051 "must be an intrinsic function",
3052 e->symtree->n.sym->name, &e->where);
3053 break;
3056 if ((m = check_conversion (e)) == MATCH_NO
3057 && (m = check_inquiry (e, 1)) == MATCH_NO
3058 && (m = check_null (e)) == MATCH_NO
3059 && (m = check_transformational (e)) == MATCH_NO
3060 && (m = check_elemental (e)) == MATCH_NO)
3062 gfc_error ("Intrinsic function %qs at %L is not permitted "
3063 "in an initialization expression",
3064 e->symtree->n.sym->name, &e->where);
3065 m = MATCH_ERROR;
3068 if (m == MATCH_ERROR)
3069 return false;
3071 /* Try to scalarize an elemental intrinsic function that has an
3072 array argument. */
3073 isym = gfc_find_function (e->symtree->n.sym->name);
3074 if (isym && isym->elemental
3075 && (t = scalarize_intrinsic_call (e, true)))
3076 break;
3079 if (m == MATCH_YES)
3080 t = gfc_simplify_expr (e, 0);
3082 break;
3084 case EXPR_VARIABLE:
3085 t = true;
3087 /* This occurs when parsing pdt templates. */
3088 if (gfc_expr_attr (e).pdt_kind)
3089 break;
3091 if (gfc_check_iter_variable (e))
3092 break;
3094 if (e->symtree->n.sym->attr.flavor == FL_PARAMETER)
3096 /* A PARAMETER shall not be used to define itself, i.e.
3097 REAL, PARAMETER :: x = transfer(0, x)
3098 is invalid. */
3099 if (!e->symtree->n.sym->value)
3101 gfc_error ("PARAMETER %qs is used at %L before its definition "
3102 "is complete", e->symtree->n.sym->name, &e->where);
3103 t = false;
3105 else
3106 t = simplify_parameter_variable (e, 0);
3108 break;
3111 if (gfc_in_match_data ())
3112 break;
3114 t = false;
3116 if (e->symtree->n.sym->as)
3118 switch (e->symtree->n.sym->as->type)
3120 case AS_ASSUMED_SIZE:
3121 gfc_error ("Assumed size array %qs at %L is not permitted "
3122 "in an initialization expression",
3123 e->symtree->n.sym->name, &e->where);
3124 break;
3126 case AS_ASSUMED_SHAPE:
3127 gfc_error ("Assumed shape array %qs at %L is not permitted "
3128 "in an initialization expression",
3129 e->symtree->n.sym->name, &e->where);
3130 break;
3132 case AS_DEFERRED:
3133 if (!e->symtree->n.sym->attr.allocatable
3134 && !e->symtree->n.sym->attr.pointer
3135 && e->symtree->n.sym->attr.dummy)
3136 gfc_error ("Assumed-shape array %qs at %L is not permitted "
3137 "in an initialization expression",
3138 e->symtree->n.sym->name, &e->where);
3139 else
3140 gfc_error ("Deferred array %qs at %L is not permitted "
3141 "in an initialization expression",
3142 e->symtree->n.sym->name, &e->where);
3143 break;
3145 case AS_EXPLICIT:
3146 gfc_error ("Array %qs at %L is a variable, which does "
3147 "not reduce to a constant expression",
3148 e->symtree->n.sym->name, &e->where);
3149 break;
3151 case AS_ASSUMED_RANK:
3152 gfc_error ("Assumed-rank array %qs at %L is not permitted "
3153 "in an initialization expression",
3154 e->symtree->n.sym->name, &e->where);
3155 break;
3157 default:
3158 gcc_unreachable();
3161 else
3162 gfc_error ("Parameter %qs at %L has not been declared or is "
3163 "a variable, which does not reduce to a constant "
3164 "expression", e->symtree->name, &e->where);
3166 break;
3168 case EXPR_CONSTANT:
3169 case EXPR_NULL:
3170 t = true;
3171 break;
3173 case EXPR_SUBSTRING:
3174 if (e->ref)
3176 t = gfc_check_init_expr (e->ref->u.ss.start);
3177 if (!t)
3178 break;
3180 t = gfc_check_init_expr (e->ref->u.ss.end);
3181 if (t)
3182 t = gfc_simplify_expr (e, 0);
3184 else
3185 t = false;
3186 break;
3188 case EXPR_STRUCTURE:
3189 t = e->ts.is_iso_c ? true : false;
3190 if (t)
3191 break;
3193 t = check_alloc_comp_init (e);
3194 if (!t)
3195 break;
3197 t = gfc_check_constructor (e, gfc_check_init_expr);
3198 if (!t)
3199 break;
3201 break;
3203 case EXPR_ARRAY:
3204 t = gfc_check_constructor (e, gfc_check_init_expr);
3205 if (!t)
3206 break;
3208 t = gfc_expand_constructor (e, true);
3209 if (!t)
3210 break;
3212 t = gfc_check_constructor_type (e);
3213 break;
3215 default:
3216 gfc_internal_error ("check_init_expr(): Unknown expression type");
3219 return t;
3222 /* Reduces a general expression to an initialization expression (a constant).
3223 This used to be part of gfc_match_init_expr.
3224 Note that this function doesn't free the given expression on false. */
3226 bool
3227 gfc_reduce_init_expr (gfc_expr *expr)
3229 bool t;
3231 /* It is far too early to resolve a class compcall. Punt to resolution. */
3232 if (expr && expr->expr_type == EXPR_COMPCALL
3233 && expr->symtree->n.sym->ts.type == BT_CLASS)
3234 return false;
3236 gfc_init_expr_flag = true;
3237 t = gfc_resolve_expr (expr);
3238 if (t)
3239 t = gfc_check_init_expr (expr);
3240 gfc_init_expr_flag = false;
3242 if (!t || !expr)
3243 return false;
3245 if (expr->expr_type == EXPR_ARRAY)
3247 if (!gfc_check_constructor_type (expr))
3248 return false;
3249 if (!gfc_expand_constructor (expr, true))
3250 return false;
3253 return true;
3257 /* Match an initialization expression. We work by first matching an
3258 expression, then reducing it to a constant. */
3260 match
3261 gfc_match_init_expr (gfc_expr **result)
3263 gfc_expr *expr;
3264 match m;
3265 bool t;
3267 expr = NULL;
3269 gfc_init_expr_flag = true;
3271 m = gfc_match_expr (&expr);
3272 if (m != MATCH_YES)
3274 gfc_init_expr_flag = false;
3275 return m;
3278 if (expr->expr_type != EXPR_FUNCTION && gfc_derived_parameter_expr (expr))
3280 *result = expr;
3281 gfc_init_expr_flag = false;
3282 return m;
3285 t = gfc_reduce_init_expr (expr);
3286 if (!t)
3288 gfc_free_expr (expr);
3289 gfc_init_expr_flag = false;
3290 return MATCH_ERROR;
3293 *result = expr;
3294 gfc_init_expr_flag = false;
3296 return MATCH_YES;
3300 /* Given an actual argument list, test to see that each argument is a
3301 restricted expression and optionally if the expression type is
3302 integer or character. */
3304 static bool
3305 restricted_args (gfc_actual_arglist *a)
3307 for (; a; a = a->next)
3309 if (!check_restricted (a->expr))
3310 return false;
3313 return true;
3317 /************* Restricted/specification expressions *************/
3320 /* Make sure a non-intrinsic function is a specification function,
3321 * see F08:7.1.11.5. */
3323 static bool
3324 external_spec_function (gfc_expr *e)
3326 gfc_symbol *f;
3328 f = e->value.function.esym;
3330 /* IEEE functions allowed are "a reference to a transformational function
3331 from the intrinsic module IEEE_ARITHMETIC or IEEE_EXCEPTIONS", and
3332 "inquiry function from the intrinsic modules IEEE_ARITHMETIC and
3333 IEEE_EXCEPTIONS". */
3334 if (f->from_intmod == INTMOD_IEEE_ARITHMETIC
3335 || f->from_intmod == INTMOD_IEEE_EXCEPTIONS)
3337 if (!strcmp (f->name, "ieee_selected_real_kind")
3338 || !strcmp (f->name, "ieee_support_rounding")
3339 || !strcmp (f->name, "ieee_support_flag")
3340 || !strcmp (f->name, "ieee_support_halting")
3341 || !strcmp (f->name, "ieee_support_datatype")
3342 || !strcmp (f->name, "ieee_support_denormal")
3343 || !strcmp (f->name, "ieee_support_subnormal")
3344 || !strcmp (f->name, "ieee_support_divide")
3345 || !strcmp (f->name, "ieee_support_inf")
3346 || !strcmp (f->name, "ieee_support_io")
3347 || !strcmp (f->name, "ieee_support_nan")
3348 || !strcmp (f->name, "ieee_support_sqrt")
3349 || !strcmp (f->name, "ieee_support_standard")
3350 || !strcmp (f->name, "ieee_support_underflow_control"))
3351 goto function_allowed;
3354 if (f->attr.proc == PROC_ST_FUNCTION)
3356 gfc_error ("Specification function %qs at %L cannot be a statement "
3357 "function", f->name, &e->where);
3358 return false;
3361 if (f->attr.proc == PROC_INTERNAL)
3363 gfc_error ("Specification function %qs at %L cannot be an internal "
3364 "function", f->name, &e->where);
3365 return false;
3368 if (!f->attr.pure && !f->attr.elemental)
3370 gfc_error ("Specification function %qs at %L must be PURE", f->name,
3371 &e->where);
3372 return false;
3375 /* F08:7.1.11.6. */
3376 if (f->attr.recursive
3377 && !gfc_notify_std (GFC_STD_F2003,
3378 "Specification function %qs "
3379 "at %L cannot be RECURSIVE", f->name, &e->where))
3380 return false;
3382 function_allowed:
3383 return restricted_args (e->value.function.actual);
3387 /* Check to see that a function reference to an intrinsic is a
3388 restricted expression. */
3390 static bool
3391 restricted_intrinsic (gfc_expr *e)
3393 /* TODO: Check constraints on inquiry functions. 7.1.6.2 (7). */
3394 if (check_inquiry (e, 0) == MATCH_YES)
3395 return true;
3397 return restricted_args (e->value.function.actual);
3401 /* Check the expressions of an actual arglist. Used by check_restricted. */
3403 static bool
3404 check_arglist (gfc_actual_arglist* arg, bool (*checker) (gfc_expr*))
3406 for (; arg; arg = arg->next)
3407 if (!checker (arg->expr))
3408 return false;
3410 return true;
3414 /* Check the subscription expressions of a reference chain with a checking
3415 function; used by check_restricted. */
3417 static bool
3418 check_references (gfc_ref* ref, bool (*checker) (gfc_expr*))
3420 int dim;
3422 if (!ref)
3423 return true;
3425 switch (ref->type)
3427 case REF_ARRAY:
3428 for (dim = 0; dim < ref->u.ar.dimen; ++dim)
3430 if (!checker (ref->u.ar.start[dim]))
3431 return false;
3432 if (!checker (ref->u.ar.end[dim]))
3433 return false;
3434 if (!checker (ref->u.ar.stride[dim]))
3435 return false;
3437 break;
3439 case REF_COMPONENT:
3440 /* Nothing needed, just proceed to next reference. */
3441 break;
3443 case REF_SUBSTRING:
3444 if (!checker (ref->u.ss.start))
3445 return false;
3446 if (!checker (ref->u.ss.end))
3447 return false;
3448 break;
3450 default:
3451 gcc_unreachable ();
3452 break;
3455 return check_references (ref->next, checker);
3458 /* Return true if ns is a parent of the current ns. */
3460 static bool
3461 is_parent_of_current_ns (gfc_namespace *ns)
3463 gfc_namespace *p;
3464 for (p = gfc_current_ns->parent; p; p = p->parent)
3465 if (ns == p)
3466 return true;
3468 return false;
3471 /* Verify that an expression is a restricted expression. Like its
3472 cousin check_init_expr(), an error message is generated if we
3473 return false. */
3475 static bool
3476 check_restricted (gfc_expr *e)
3478 gfc_symbol* sym;
3479 bool t;
3481 if (e == NULL)
3482 return true;
3484 switch (e->expr_type)
3486 case EXPR_OP:
3487 t = check_intrinsic_op (e, check_restricted);
3488 if (t)
3489 t = gfc_simplify_expr (e, 0);
3491 break;
3493 case EXPR_FUNCTION:
3494 if (e->value.function.esym)
3496 t = check_arglist (e->value.function.actual, &check_restricted);
3497 if (t)
3498 t = external_spec_function (e);
3500 else
3502 if (e->value.function.isym && e->value.function.isym->inquiry)
3503 t = true;
3504 else
3505 t = check_arglist (e->value.function.actual, &check_restricted);
3507 if (t)
3508 t = restricted_intrinsic (e);
3510 break;
3512 case EXPR_VARIABLE:
3513 sym = e->symtree->n.sym;
3514 t = false;
3516 /* If a dummy argument appears in a context that is valid for a
3517 restricted expression in an elemental procedure, it will have
3518 already been simplified away once we get here. Therefore we
3519 don't need to jump through hoops to distinguish valid from
3520 invalid cases. Allowed in F2008 and F2018. */
3521 if (gfc_notification_std (GFC_STD_F2008)
3522 && sym->attr.dummy && sym->ns == gfc_current_ns
3523 && sym->ns->proc_name && sym->ns->proc_name->attr.elemental)
3525 gfc_error_now ("Dummy argument %qs not "
3526 "allowed in expression at %L",
3527 sym->name, &e->where);
3528 break;
3531 if (sym->attr.optional)
3533 gfc_error ("Dummy argument %qs at %L cannot be OPTIONAL",
3534 sym->name, &e->where);
3535 break;
3538 if (sym->attr.intent == INTENT_OUT)
3540 gfc_error ("Dummy argument %qs at %L cannot be INTENT(OUT)",
3541 sym->name, &e->where);
3542 break;
3545 /* Check reference chain if any. */
3546 if (!check_references (e->ref, &check_restricted))
3547 break;
3549 if (e->error
3550 || sym->attr.in_common
3551 || sym->attr.use_assoc
3552 || sym->attr.used_in_submodule
3553 || sym->attr.dummy
3554 || sym->attr.implied_index
3555 || sym->attr.flavor == FL_PARAMETER
3556 || is_parent_of_current_ns (gfc_get_spec_ns (sym)))
3558 t = true;
3559 break;
3562 gfc_error ("Variable %qs cannot appear in the expression at %L",
3563 sym->name, &e->where);
3564 /* Prevent a repetition of the error. */
3565 e->error = 1;
3566 break;
3568 case EXPR_NULL:
3569 case EXPR_CONSTANT:
3570 t = true;
3571 break;
3573 case EXPR_SUBSTRING:
3574 t = gfc_specification_expr (e->ref->u.ss.start);
3575 if (!t)
3576 break;
3578 t = gfc_specification_expr (e->ref->u.ss.end);
3579 if (t)
3580 t = gfc_simplify_expr (e, 0);
3582 break;
3584 case EXPR_STRUCTURE:
3585 t = gfc_check_constructor (e, check_restricted);
3586 break;
3588 case EXPR_ARRAY:
3589 t = gfc_check_constructor (e, check_restricted);
3590 break;
3592 default:
3593 gfc_internal_error ("check_restricted(): Unknown expression type");
3596 return t;
3600 /* Check to see that an expression is a specification expression. If
3601 we return false, an error has been generated. */
3603 bool
3604 gfc_specification_expr (gfc_expr *e)
3606 gfc_component *comp;
3608 if (e == NULL)
3609 return true;
3611 if (e->ts.type != BT_INTEGER)
3613 gfc_error ("Expression at %L must be of INTEGER type, found %s",
3614 &e->where, gfc_basic_typename (e->ts.type));
3615 return false;
3618 comp = gfc_get_proc_ptr_comp (e);
3619 if (e->expr_type == EXPR_FUNCTION
3620 && !e->value.function.isym
3621 && !e->value.function.esym
3622 && !gfc_pure (e->symtree->n.sym)
3623 && (!comp || !comp->attr.pure))
3625 gfc_error ("Function %qs at %L must be PURE",
3626 e->symtree->n.sym->name, &e->where);
3627 /* Prevent repeat error messages. */
3628 e->symtree->n.sym->attr.pure = 1;
3629 return false;
3632 if (e->rank != 0)
3634 gfc_error ("Expression at %L must be scalar", &e->where);
3635 return false;
3638 if (!gfc_simplify_expr (e, 0))
3639 return false;
3641 return check_restricted (e);
3645 /************** Expression conformance checks. *************/
3647 /* Given two expressions, make sure that the arrays are conformable. */
3649 bool
3650 gfc_check_conformance (gfc_expr *op1, gfc_expr *op2, const char *optype_msgid, ...)
3652 int op1_flag, op2_flag, d;
3653 mpz_t op1_size, op2_size;
3654 bool t;
3656 va_list argp;
3657 char buffer[240];
3659 if (op1->rank == 0 || op2->rank == 0)
3660 return true;
3662 va_start (argp, optype_msgid);
3663 d = vsnprintf (buffer, sizeof (buffer), optype_msgid, argp);
3664 va_end (argp);
3665 if (d < 1 || d >= (int) sizeof (buffer)) /* Reject truncation. */
3666 gfc_internal_error ("optype_msgid overflow: %d", d);
3668 if (op1->rank != op2->rank)
3670 gfc_error ("Incompatible ranks in %s (%d and %d) at %L", _(buffer),
3671 op1->rank, op2->rank, &op1->where);
3672 return false;
3675 t = true;
3677 for (d = 0; d < op1->rank; d++)
3679 op1_flag = gfc_array_dimen_size(op1, d, &op1_size);
3680 op2_flag = gfc_array_dimen_size(op2, d, &op2_size);
3682 if (op1_flag && op2_flag && mpz_cmp (op1_size, op2_size) != 0)
3684 gfc_error ("Different shape for %s at %L on dimension %d "
3685 "(%d and %d)", _(buffer), &op1->where, d + 1,
3686 (int) mpz_get_si (op1_size),
3687 (int) mpz_get_si (op2_size));
3689 t = false;
3692 if (op1_flag)
3693 mpz_clear (op1_size);
3694 if (op2_flag)
3695 mpz_clear (op2_size);
3697 if (!t)
3698 return false;
3701 return true;
3705 /* Given an assignable expression and an arbitrary expression, make
3706 sure that the assignment can take place. Only add a call to the intrinsic
3707 conversion routines, when allow_convert is set. When this assign is a
3708 coarray call, then the convert is done by the coarray routine implicitly and
3709 adding the intrinsic conversion would do harm in most cases. */
3711 bool
3712 gfc_check_assign (gfc_expr *lvalue, gfc_expr *rvalue, int conform,
3713 bool allow_convert)
3715 gfc_symbol *sym;
3716 gfc_ref *ref;
3717 int has_pointer;
3719 sym = lvalue->symtree->n.sym;
3721 /* See if this is the component or subcomponent of a pointer and guard
3722 against assignment to LEN or KIND part-refs. */
3723 has_pointer = sym->attr.pointer;
3724 for (ref = lvalue->ref; ref; ref = ref->next)
3726 if (!has_pointer && ref->type == REF_COMPONENT
3727 && ref->u.c.component->attr.pointer)
3728 has_pointer = 1;
3729 else if (ref->type == REF_INQUIRY
3730 && (ref->u.i == INQUIRY_LEN || ref->u.i == INQUIRY_KIND))
3732 gfc_error ("Assignment to a LEN or KIND part_ref at %L is not "
3733 "allowed", &lvalue->where);
3734 return false;
3738 /* 12.5.2.2, Note 12.26: The result variable is very similar to any other
3739 variable local to a function subprogram. Its existence begins when
3740 execution of the function is initiated and ends when execution of the
3741 function is terminated...
3742 Therefore, the left hand side is no longer a variable, when it is: */
3743 if (sym->attr.flavor == FL_PROCEDURE && sym->attr.proc != PROC_ST_FUNCTION
3744 && !sym->attr.external)
3746 bool bad_proc;
3747 bad_proc = false;
3749 /* (i) Use associated; */
3750 if (sym->attr.use_assoc)
3751 bad_proc = true;
3753 /* (ii) The assignment is in the main program; or */
3754 if (gfc_current_ns->proc_name
3755 && gfc_current_ns->proc_name->attr.is_main_program)
3756 bad_proc = true;
3758 /* (iii) A module or internal procedure... */
3759 if (gfc_current_ns->proc_name
3760 && (gfc_current_ns->proc_name->attr.proc == PROC_INTERNAL
3761 || gfc_current_ns->proc_name->attr.proc == PROC_MODULE)
3762 && gfc_current_ns->parent
3763 && (!(gfc_current_ns->parent->proc_name->attr.function
3764 || gfc_current_ns->parent->proc_name->attr.subroutine)
3765 || gfc_current_ns->parent->proc_name->attr.is_main_program))
3767 /* ... that is not a function... */
3768 if (gfc_current_ns->proc_name
3769 && !gfc_current_ns->proc_name->attr.function)
3770 bad_proc = true;
3772 /* ... or is not an entry and has a different name. */
3773 if (!sym->attr.entry && sym->name != gfc_current_ns->proc_name->name)
3774 bad_proc = true;
3777 /* (iv) Host associated and not the function symbol or the
3778 parent result. This picks up sibling references, which
3779 cannot be entries. */
3780 if (!sym->attr.entry
3781 && sym->ns == gfc_current_ns->parent
3782 && sym != gfc_current_ns->proc_name
3783 && sym != gfc_current_ns->parent->proc_name->result)
3784 bad_proc = true;
3786 if (bad_proc)
3788 gfc_error ("%qs at %L is not a VALUE", sym->name, &lvalue->where);
3789 return false;
3792 else
3794 /* Reject assigning to an external symbol. For initializers, this
3795 was already done before, in resolve_fl_procedure. */
3796 if (sym->attr.flavor == FL_PROCEDURE && sym->attr.external
3797 && sym->attr.proc != PROC_MODULE && !rvalue->error)
3799 gfc_error ("Illegal assignment to external procedure at %L",
3800 &lvalue->where);
3801 return false;
3805 if (rvalue->rank != 0 && lvalue->rank != rvalue->rank)
3807 gfc_error ("Incompatible ranks %d and %d in assignment at %L",
3808 lvalue->rank, rvalue->rank, &lvalue->where);
3809 return false;
3812 if (lvalue->ts.type == BT_UNKNOWN)
3814 gfc_error ("Variable type is UNKNOWN in assignment at %L",
3815 &lvalue->where);
3816 return false;
3819 if (rvalue->expr_type == EXPR_NULL)
3821 if (has_pointer && (ref == NULL || ref->next == NULL)
3822 && lvalue->symtree->n.sym->attr.data)
3823 return true;
3824 else
3826 gfc_error ("NULL appears on right-hand side in assignment at %L",
3827 &rvalue->where);
3828 return false;
3832 /* This is possibly a typo: x = f() instead of x => f(). */
3833 if (warn_surprising
3834 && rvalue->expr_type == EXPR_FUNCTION && gfc_expr_attr (rvalue).pointer)
3835 gfc_warning (OPT_Wsurprising,
3836 "POINTER-valued function appears on right-hand side of "
3837 "assignment at %L", &rvalue->where);
3839 /* Check size of array assignments. */
3840 if (lvalue->rank != 0 && rvalue->rank != 0
3841 && !gfc_check_conformance (lvalue, rvalue, _("array assignment")))
3842 return false;
3844 /* Handle the case of a BOZ literal on the RHS. */
3845 if (rvalue->ts.type == BT_BOZ)
3847 if (lvalue->symtree->n.sym->attr.data)
3849 if (lvalue->ts.type == BT_INTEGER
3850 && gfc_boz2int (rvalue, lvalue->ts.kind))
3851 return true;
3853 if (lvalue->ts.type == BT_REAL
3854 && gfc_boz2real (rvalue, lvalue->ts.kind))
3856 if (gfc_invalid_boz ("BOZ literal constant near %L cannot "
3857 "be assigned to a REAL variable",
3858 &rvalue->where))
3859 return false;
3860 return true;
3864 if (!lvalue->symtree->n.sym->attr.data
3865 && gfc_invalid_boz ("BOZ literal constant at %L is neither a "
3866 "data-stmt-constant nor an actual argument to "
3867 "INT, REAL, DBLE, or CMPLX intrinsic function",
3868 &rvalue->where))
3869 return false;
3871 if (lvalue->ts.type == BT_INTEGER
3872 && gfc_boz2int (rvalue, lvalue->ts.kind))
3873 return true;
3875 if (lvalue->ts.type == BT_REAL
3876 && gfc_boz2real (rvalue, lvalue->ts.kind))
3877 return true;
3879 gfc_error ("BOZ literal constant near %L cannot be assigned to a "
3880 "%qs variable", &rvalue->where, gfc_typename (lvalue));
3881 return false;
3884 if (gfc_expr_attr (lvalue).pdt_kind || gfc_expr_attr (lvalue).pdt_len)
3886 gfc_error ("The assignment to a KIND or LEN component of a "
3887 "parameterized type at %L is not allowed",
3888 &lvalue->where);
3889 return false;
3892 if (gfc_compare_types (&lvalue->ts, &rvalue->ts))
3893 return true;
3895 /* Only DATA Statements come here. */
3896 if (!conform)
3898 locus *where;
3900 /* Numeric can be converted to any other numeric. And Hollerith can be
3901 converted to any other type. */
3902 if ((gfc_numeric_ts (&lvalue->ts) && gfc_numeric_ts (&rvalue->ts))
3903 || rvalue->ts.type == BT_HOLLERITH)
3904 return true;
3906 if (flag_dec_char_conversions && (gfc_numeric_ts (&lvalue->ts)
3907 || lvalue->ts.type == BT_LOGICAL)
3908 && rvalue->ts.type == BT_CHARACTER
3909 && rvalue->ts.kind == gfc_default_character_kind)
3910 return true;
3912 if (lvalue->ts.type == BT_LOGICAL && rvalue->ts.type == BT_LOGICAL)
3913 return true;
3915 where = lvalue->where.lb ? &lvalue->where : &rvalue->where;
3916 gfc_error ("Incompatible types in DATA statement at %L; attempted "
3917 "conversion of %s to %s", where,
3918 gfc_typename (rvalue), gfc_typename (lvalue));
3920 return false;
3923 /* Assignment is the only case where character variables of different
3924 kind values can be converted into one another. */
3925 if (lvalue->ts.type == BT_CHARACTER && rvalue->ts.type == BT_CHARACTER)
3927 if (lvalue->ts.kind != rvalue->ts.kind && allow_convert)
3928 return gfc_convert_chartype (rvalue, &lvalue->ts);
3929 else
3930 return true;
3933 if (!allow_convert)
3934 return true;
3936 return gfc_convert_type (rvalue, &lvalue->ts, 1);
3940 /* Check that a pointer assignment is OK. We first check lvalue, and
3941 we only check rvalue if it's not an assignment to NULL() or a
3942 NULLIFY statement. */
3944 bool
3945 gfc_check_pointer_assign (gfc_expr *lvalue, gfc_expr *rvalue,
3946 bool suppress_type_test, bool is_init_expr)
3948 symbol_attribute attr, lhs_attr;
3949 gfc_ref *ref;
3950 bool is_pure, is_implicit_pure, rank_remap;
3951 int proc_pointer;
3952 bool same_rank;
3954 if (!lvalue->symtree)
3955 return false;
3957 lhs_attr = gfc_expr_attr (lvalue);
3958 if (lvalue->ts.type == BT_UNKNOWN && !lhs_attr.proc_pointer)
3960 gfc_error ("Pointer assignment target is not a POINTER at %L",
3961 &lvalue->where);
3962 return false;
3965 if (lhs_attr.flavor == FL_PROCEDURE && lhs_attr.use_assoc
3966 && !lhs_attr.proc_pointer)
3968 gfc_error ("%qs in the pointer assignment at %L cannot be an "
3969 "l-value since it is a procedure",
3970 lvalue->symtree->n.sym->name, &lvalue->where);
3971 return false;
3974 proc_pointer = lvalue->symtree->n.sym->attr.proc_pointer;
3976 rank_remap = false;
3977 same_rank = lvalue->rank == rvalue->rank;
3978 for (ref = lvalue->ref; ref; ref = ref->next)
3980 if (ref->type == REF_COMPONENT)
3981 proc_pointer = ref->u.c.component->attr.proc_pointer;
3983 if (ref->type == REF_ARRAY && ref->next == NULL)
3985 int dim;
3987 if (ref->u.ar.type == AR_FULL)
3988 break;
3990 if (ref->u.ar.type != AR_SECTION)
3992 gfc_error ("Expected bounds specification for %qs at %L",
3993 lvalue->symtree->n.sym->name, &lvalue->where);
3994 return false;
3997 if (!gfc_notify_std (GFC_STD_F2003, "Bounds specification "
3998 "for %qs in pointer assignment at %L",
3999 lvalue->symtree->n.sym->name, &lvalue->where))
4000 return false;
4002 /* Fortran standard (e.g. F2018, 10.2.2 Pointer assignment):
4004 * (C1017) If bounds-spec-list is specified, the number of
4005 * bounds-specs shall equal the rank of data-pointer-object.
4007 * If bounds-spec-list appears, it specifies the lower bounds.
4009 * (C1018) If bounds-remapping-list is specified, the number of
4010 * bounds-remappings shall equal the rank of data-pointer-object.
4012 * If bounds-remapping-list appears, it specifies the upper and
4013 * lower bounds of each dimension of the pointer; the pointer target
4014 * shall be simply contiguous or of rank one.
4016 * (C1019) If bounds-remapping-list is not specified, the ranks of
4017 * data-pointer-object and data-target shall be the same.
4019 * Thus when bounds are given, all lbounds are necessary and either
4020 * all or none of the upper bounds; no strides are allowed. If the
4021 * upper bounds are present, we may do rank remapping. */
4022 for (dim = 0; dim < ref->u.ar.dimen; ++dim)
4024 if (ref->u.ar.stride[dim])
4026 gfc_error ("Stride must not be present at %L",
4027 &lvalue->where);
4028 return false;
4030 if (!same_rank && (!ref->u.ar.start[dim] ||!ref->u.ar.end[dim]))
4032 gfc_error ("Rank remapping requires a "
4033 "list of %<lower-bound : upper-bound%> "
4034 "specifications at %L", &lvalue->where);
4035 return false;
4037 if (!ref->u.ar.start[dim]
4038 || ref->u.ar.dimen_type[dim] != DIMEN_RANGE)
4040 gfc_error ("Expected list of %<lower-bound :%> or "
4041 "list of %<lower-bound : upper-bound%> "
4042 "specifications at %L", &lvalue->where);
4043 return false;
4046 if (dim == 0)
4047 rank_remap = (ref->u.ar.end[dim] != NULL);
4048 else
4050 if ((rank_remap && !ref->u.ar.end[dim]))
4052 gfc_error ("Rank remapping requires a "
4053 "list of %<lower-bound : upper-bound%> "
4054 "specifications at %L", &lvalue->where);
4055 return false;
4057 if (!rank_remap && ref->u.ar.end[dim])
4059 gfc_error ("Expected list of %<lower-bound :%> or "
4060 "list of %<lower-bound : upper-bound%> "
4061 "specifications at %L", &lvalue->where);
4062 return false;
4069 is_pure = gfc_pure (NULL);
4070 is_implicit_pure = gfc_implicit_pure (NULL);
4072 /* If rvalue is a NULL() or NULLIFY, we're done. Otherwise the type,
4073 kind, etc for lvalue and rvalue must match, and rvalue must be a
4074 pure variable if we're in a pure function. */
4075 if (rvalue->expr_type == EXPR_NULL && rvalue->ts.type == BT_UNKNOWN)
4076 return true;
4078 /* F2008, C723 (pointer) and C726 (proc-pointer); for PURE also C1283. */
4079 if (lvalue->expr_type == EXPR_VARIABLE
4080 && gfc_is_coindexed (lvalue))
4082 gfc_ref *ref;
4083 for (ref = lvalue->ref; ref; ref = ref->next)
4084 if (ref->type == REF_ARRAY && ref->u.ar.codimen)
4086 gfc_error ("Pointer object at %L shall not have a coindex",
4087 &lvalue->where);
4088 return false;
4092 /* Checks on rvalue for procedure pointer assignments. */
4093 if (proc_pointer)
4095 char err[200];
4096 gfc_symbol *s1,*s2;
4097 gfc_component *comp1, *comp2;
4098 const char *name;
4100 attr = gfc_expr_attr (rvalue);
4101 if (!((rvalue->expr_type == EXPR_NULL)
4102 || (rvalue->expr_type == EXPR_FUNCTION && attr.proc_pointer)
4103 || (rvalue->expr_type == EXPR_VARIABLE && attr.proc_pointer)
4104 || (rvalue->expr_type == EXPR_VARIABLE
4105 && attr.flavor == FL_PROCEDURE)))
4107 gfc_error ("Invalid procedure pointer assignment at %L",
4108 &rvalue->where);
4109 return false;
4112 if (rvalue->expr_type == EXPR_VARIABLE && !attr.proc_pointer)
4114 /* Check for intrinsics. */
4115 gfc_symbol *sym = rvalue->symtree->n.sym;
4116 if (!sym->attr.intrinsic
4117 && (gfc_is_intrinsic (sym, 0, sym->declared_at)
4118 || gfc_is_intrinsic (sym, 1, sym->declared_at)))
4120 sym->attr.intrinsic = 1;
4121 gfc_resolve_intrinsic (sym, &rvalue->where);
4122 attr = gfc_expr_attr (rvalue);
4124 /* Check for result of embracing function. */
4125 if (sym->attr.function && sym->result == sym)
4127 gfc_namespace *ns;
4129 for (ns = gfc_current_ns; ns; ns = ns->parent)
4130 if (sym == ns->proc_name)
4132 gfc_error ("Function result %qs is invalid as proc-target "
4133 "in procedure pointer assignment at %L",
4134 sym->name, &rvalue->where);
4135 return false;
4139 if (attr.abstract)
4141 gfc_error ("Abstract interface %qs is invalid "
4142 "in procedure pointer assignment at %L",
4143 rvalue->symtree->name, &rvalue->where);
4144 return false;
4146 /* Check for F08:C729. */
4147 if (attr.flavor == FL_PROCEDURE)
4149 if (attr.proc == PROC_ST_FUNCTION)
4151 gfc_error ("Statement function %qs is invalid "
4152 "in procedure pointer assignment at %L",
4153 rvalue->symtree->name, &rvalue->where);
4154 return false;
4156 if (attr.proc == PROC_INTERNAL &&
4157 !gfc_notify_std(GFC_STD_F2008, "Internal procedure %qs "
4158 "is invalid in procedure pointer assignment "
4159 "at %L", rvalue->symtree->name, &rvalue->where))
4160 return false;
4161 if (attr.intrinsic && gfc_intrinsic_actual_ok (rvalue->symtree->name,
4162 attr.subroutine) == 0)
4164 gfc_error ("Intrinsic %qs at %L is invalid in procedure pointer "
4165 "assignment", rvalue->symtree->name, &rvalue->where);
4166 return false;
4169 /* Check for F08:C730. */
4170 if (attr.elemental && !attr.intrinsic)
4172 gfc_error ("Nonintrinsic elemental procedure %qs is invalid "
4173 "in procedure pointer assignment at %L",
4174 rvalue->symtree->name, &rvalue->where);
4175 return false;
4178 /* Ensure that the calling convention is the same. As other attributes
4179 such as DLLEXPORT may differ, one explicitly only tests for the
4180 calling conventions. */
4181 if (rvalue->expr_type == EXPR_VARIABLE
4182 && lvalue->symtree->n.sym->attr.ext_attr
4183 != rvalue->symtree->n.sym->attr.ext_attr)
4185 symbol_attribute calls;
4187 calls.ext_attr = 0;
4188 gfc_add_ext_attribute (&calls, EXT_ATTR_CDECL, NULL);
4189 gfc_add_ext_attribute (&calls, EXT_ATTR_STDCALL, NULL);
4190 gfc_add_ext_attribute (&calls, EXT_ATTR_FASTCALL, NULL);
4192 if ((calls.ext_attr & lvalue->symtree->n.sym->attr.ext_attr)
4193 != (calls.ext_attr & rvalue->symtree->n.sym->attr.ext_attr))
4195 gfc_error ("Mismatch in the procedure pointer assignment "
4196 "at %L: mismatch in the calling convention",
4197 &rvalue->where);
4198 return false;
4202 comp1 = gfc_get_proc_ptr_comp (lvalue);
4203 if (comp1)
4204 s1 = comp1->ts.interface;
4205 else
4207 s1 = lvalue->symtree->n.sym;
4208 if (s1->ts.interface)
4209 s1 = s1->ts.interface;
4212 comp2 = gfc_get_proc_ptr_comp (rvalue);
4213 if (comp2)
4215 if (rvalue->expr_type == EXPR_FUNCTION)
4217 s2 = comp2->ts.interface->result;
4218 name = s2->name;
4220 else
4222 s2 = comp2->ts.interface;
4223 name = comp2->name;
4226 else if (rvalue->expr_type == EXPR_FUNCTION)
4228 if (rvalue->value.function.esym)
4229 s2 = rvalue->value.function.esym->result;
4230 else
4231 s2 = rvalue->symtree->n.sym->result;
4233 name = s2->name;
4235 else
4237 s2 = rvalue->symtree->n.sym;
4238 name = s2->name;
4241 if (s2 && s2->attr.proc_pointer && s2->ts.interface)
4242 s2 = s2->ts.interface;
4244 /* Special check for the case of absent interface on the lvalue.
4245 * All other interface checks are done below. */
4246 if (!s1 && comp1 && comp1->attr.subroutine && s2 && s2->attr.function)
4248 gfc_error ("Interface mismatch in procedure pointer assignment "
4249 "at %L: %qs is not a subroutine", &rvalue->where, name);
4250 return false;
4253 /* F08:7.2.2.4 (4) */
4254 if (s2 && gfc_explicit_interface_required (s2, err, sizeof(err)))
4256 if (comp1 && !s1)
4258 gfc_error ("Explicit interface required for component %qs at %L: %s",
4259 comp1->name, &lvalue->where, err);
4260 return false;
4262 else if (s1->attr.if_source == IFSRC_UNKNOWN)
4264 gfc_error ("Explicit interface required for %qs at %L: %s",
4265 s1->name, &lvalue->where, err);
4266 return false;
4269 if (s1 && gfc_explicit_interface_required (s1, err, sizeof(err)))
4271 if (comp2 && !s2)
4273 gfc_error ("Explicit interface required for component %qs at %L: %s",
4274 comp2->name, &rvalue->where, err);
4275 return false;
4277 else if (s2->attr.if_source == IFSRC_UNKNOWN)
4279 gfc_error ("Explicit interface required for %qs at %L: %s",
4280 s2->name, &rvalue->where, err);
4281 return false;
4285 if (s1 == s2 || !s1 || !s2)
4286 return true;
4288 if (!gfc_compare_interfaces (s1, s2, name, 0, 1,
4289 err, sizeof(err), NULL, NULL))
4291 gfc_error ("Interface mismatch in procedure pointer assignment "
4292 "at %L: %s", &rvalue->where, err);
4293 return false;
4296 /* Check F2008Cor2, C729. */
4297 if (!s2->attr.intrinsic && s2->attr.if_source == IFSRC_UNKNOWN
4298 && !s2->attr.external && !s2->attr.subroutine && !s2->attr.function)
4300 gfc_error ("Procedure pointer target %qs at %L must be either an "
4301 "intrinsic, host or use associated, referenced or have "
4302 "the EXTERNAL attribute", s2->name, &rvalue->where);
4303 return false;
4306 return true;
4308 else
4310 /* A non-proc pointer cannot point to a constant. */
4311 if (rvalue->expr_type == EXPR_CONSTANT)
4313 gfc_error_now ("Pointer assignment target cannot be a constant at %L",
4314 &rvalue->where);
4315 return false;
4319 if (!gfc_compare_types (&lvalue->ts, &rvalue->ts))
4321 /* Check for F03:C717. */
4322 if (UNLIMITED_POLY (rvalue)
4323 && !(UNLIMITED_POLY (lvalue)
4324 || (lvalue->ts.type == BT_DERIVED
4325 && (lvalue->ts.u.derived->attr.is_bind_c
4326 || lvalue->ts.u.derived->attr.sequence))))
4327 gfc_error ("Data-pointer-object at %L must be unlimited "
4328 "polymorphic, or of a type with the BIND or SEQUENCE "
4329 "attribute, to be compatible with an unlimited "
4330 "polymorphic target", &lvalue->where);
4331 else if (!suppress_type_test)
4332 gfc_error ("Different types in pointer assignment at %L; "
4333 "attempted assignment of %s to %s", &lvalue->where,
4334 gfc_typename (rvalue), gfc_typename (lvalue));
4335 return false;
4338 if (lvalue->ts.type != BT_CLASS && lvalue->ts.kind != rvalue->ts.kind)
4340 gfc_error ("Different kind type parameters in pointer "
4341 "assignment at %L", &lvalue->where);
4342 return false;
4345 if (lvalue->rank != rvalue->rank && !rank_remap)
4347 gfc_error ("Different ranks in pointer assignment at %L", &lvalue->where);
4348 return false;
4351 /* Make sure the vtab is present. */
4352 if (lvalue->ts.type == BT_CLASS && !UNLIMITED_POLY (rvalue))
4353 gfc_find_vtab (&rvalue->ts);
4355 /* Check rank remapping. */
4356 if (rank_remap)
4358 mpz_t lsize, rsize;
4360 /* If this can be determined, check that the target must be at least as
4361 large as the pointer assigned to it is. */
4362 if (gfc_array_size (lvalue, &lsize)
4363 && gfc_array_size (rvalue, &rsize)
4364 && mpz_cmp (rsize, lsize) < 0)
4366 gfc_error ("Rank remapping target is smaller than size of the"
4367 " pointer (%ld < %ld) at %L",
4368 mpz_get_si (rsize), mpz_get_si (lsize),
4369 &lvalue->where);
4370 return false;
4373 /* The target must be either rank one or it must be simply contiguous
4374 and F2008 must be allowed. */
4375 if (rvalue->rank != 1)
4377 if (!gfc_is_simply_contiguous (rvalue, true, false))
4379 gfc_error ("Rank remapping target must be rank 1 or"
4380 " simply contiguous at %L", &rvalue->where);
4381 return false;
4383 if (!gfc_notify_std (GFC_STD_F2008, "Rank remapping target is not "
4384 "rank 1 at %L", &rvalue->where))
4385 return false;
4389 /* Now punt if we are dealing with a NULLIFY(X) or X = NULL(X). */
4390 if (rvalue->expr_type == EXPR_NULL)
4391 return true;
4393 if (rvalue->expr_type == EXPR_VARIABLE && is_subref_array (rvalue))
4394 lvalue->symtree->n.sym->attr.subref_array_pointer = 1;
4396 attr = gfc_expr_attr (rvalue);
4398 if (rvalue->expr_type == EXPR_FUNCTION && !attr.pointer)
4400 /* F2008, C725. For PURE also C1283. Sometimes rvalue is a function call
4401 to caf_get. Map this to the same error message as below when it is
4402 still a variable expression. */
4403 if (rvalue->value.function.isym
4404 && rvalue->value.function.isym->id == GFC_ISYM_CAF_GET)
4405 /* The test above might need to be extend when F08, Note 5.4 has to be
4406 interpreted in the way that target and pointer with the same coindex
4407 are allowed. */
4408 gfc_error ("Data target at %L shall not have a coindex",
4409 &rvalue->where);
4410 else
4411 gfc_error ("Target expression in pointer assignment "
4412 "at %L must deliver a pointer result",
4413 &rvalue->where);
4414 return false;
4417 if (is_init_expr)
4419 gfc_symbol *sym;
4420 bool target;
4421 gfc_ref *ref;
4423 if (gfc_is_size_zero_array (rvalue))
4425 gfc_error ("Zero-sized array detected at %L where an entity with "
4426 "the TARGET attribute is expected", &rvalue->where);
4427 return false;
4429 else if (!rvalue->symtree)
4431 gfc_error ("Pointer assignment target in initialization expression "
4432 "does not have the TARGET attribute at %L",
4433 &rvalue->where);
4434 return false;
4437 sym = rvalue->symtree->n.sym;
4439 if (sym->ts.type == BT_CLASS && sym->attr.class_ok)
4440 target = CLASS_DATA (sym)->attr.target;
4441 else
4442 target = sym->attr.target;
4444 if (!target && !proc_pointer)
4446 gfc_error ("Pointer assignment target in initialization expression "
4447 "does not have the TARGET attribute at %L",
4448 &rvalue->where);
4449 return false;
4452 for (ref = rvalue->ref; ref; ref = ref->next)
4454 switch (ref->type)
4456 case REF_ARRAY:
4457 for (int n = 0; n < ref->u.ar.dimen; n++)
4458 if (!gfc_is_constant_expr (ref->u.ar.start[n])
4459 || !gfc_is_constant_expr (ref->u.ar.end[n])
4460 || !gfc_is_constant_expr (ref->u.ar.stride[n]))
4462 gfc_error ("Every subscript of target specification "
4463 "at %L must be a constant expression",
4464 &ref->u.ar.where);
4465 return false;
4467 break;
4469 case REF_SUBSTRING:
4470 if (!gfc_is_constant_expr (ref->u.ss.start)
4471 || !gfc_is_constant_expr (ref->u.ss.end))
4473 gfc_error ("Substring starting and ending points of target "
4474 "specification at %L must be constant expressions",
4475 &ref->u.ss.start->where);
4476 return false;
4478 break;
4480 default:
4481 break;
4485 else
4487 if (!attr.target && !attr.pointer)
4489 gfc_error ("Pointer assignment target is neither TARGET "
4490 "nor POINTER at %L", &rvalue->where);
4491 return false;
4495 if (lvalue->ts.type == BT_CHARACTER)
4497 bool t = gfc_check_same_strlen (lvalue, rvalue, "pointer assignment");
4498 if (!t)
4499 return false;
4502 if (is_pure && gfc_impure_variable (rvalue->symtree->n.sym))
4504 gfc_error ("Bad target in pointer assignment in PURE "
4505 "procedure at %L", &rvalue->where);
4508 if (is_implicit_pure && gfc_impure_variable (rvalue->symtree->n.sym))
4509 gfc_unset_implicit_pure (gfc_current_ns->proc_name);
4511 if (gfc_has_vector_index (rvalue))
4513 gfc_error ("Pointer assignment with vector subscript "
4514 "on rhs at %L", &rvalue->where);
4515 return false;
4518 if (attr.is_protected && attr.use_assoc
4519 && !(attr.pointer || attr.proc_pointer))
4521 gfc_error ("Pointer assignment target has PROTECTED "
4522 "attribute at %L", &rvalue->where);
4523 return false;
4526 /* F2008, C725. For PURE also C1283. */
4527 if (rvalue->expr_type == EXPR_VARIABLE
4528 && gfc_is_coindexed (rvalue))
4530 gfc_ref *ref;
4531 for (ref = rvalue->ref; ref; ref = ref->next)
4532 if (ref->type == REF_ARRAY && ref->u.ar.codimen)
4534 gfc_error ("Data target at %L shall not have a coindex",
4535 &rvalue->where);
4536 return false;
4540 /* Warn for assignments of contiguous pointers to targets which is not
4541 contiguous. Be lenient in the definition of what counts as
4542 contiguous. */
4544 if (lhs_attr.contiguous
4545 && lhs_attr.dimension > 0)
4547 if (gfc_is_not_contiguous (rvalue))
4549 gfc_error ("Assignment to contiguous pointer from "
4550 "non-contiguous target at %L", &rvalue->where);
4551 return false;
4553 if (!gfc_is_simply_contiguous (rvalue, false, true))
4554 gfc_warning (OPT_Wextra, "Assignment to contiguous pointer from "
4555 "non-contiguous target at %L", &rvalue->where);
4558 /* Warn if it is the LHS pointer may lives longer than the RHS target. */
4559 if (warn_target_lifetime
4560 && rvalue->expr_type == EXPR_VARIABLE
4561 && !rvalue->symtree->n.sym->attr.save
4562 && !rvalue->symtree->n.sym->attr.pointer && !attr.pointer
4563 && !rvalue->symtree->n.sym->attr.host_assoc
4564 && !rvalue->symtree->n.sym->attr.in_common
4565 && !rvalue->symtree->n.sym->attr.use_assoc
4566 && !rvalue->symtree->n.sym->attr.dummy)
4568 bool warn;
4569 gfc_namespace *ns;
4571 warn = lvalue->symtree->n.sym->attr.dummy
4572 || lvalue->symtree->n.sym->attr.result
4573 || lvalue->symtree->n.sym->attr.function
4574 || (lvalue->symtree->n.sym->attr.host_assoc
4575 && lvalue->symtree->n.sym->ns
4576 != rvalue->symtree->n.sym->ns)
4577 || lvalue->symtree->n.sym->attr.use_assoc
4578 || lvalue->symtree->n.sym->attr.in_common;
4580 if (rvalue->symtree->n.sym->ns->proc_name
4581 && rvalue->symtree->n.sym->ns->proc_name->attr.flavor != FL_PROCEDURE
4582 && rvalue->symtree->n.sym->ns->proc_name->attr.flavor != FL_PROGRAM)
4583 for (ns = rvalue->symtree->n.sym->ns;
4584 ns && ns->proc_name && ns->proc_name->attr.flavor != FL_PROCEDURE;
4585 ns = ns->parent)
4586 if (ns->parent == lvalue->symtree->n.sym->ns)
4588 warn = true;
4589 break;
4592 if (warn)
4593 gfc_warning (OPT_Wtarget_lifetime,
4594 "Pointer at %L in pointer assignment might outlive the "
4595 "pointer target", &lvalue->where);
4598 return true;
4602 /* Relative of gfc_check_assign() except that the lvalue is a single
4603 symbol. Used for initialization assignments. */
4605 bool
4606 gfc_check_assign_symbol (gfc_symbol *sym, gfc_component *comp, gfc_expr *rvalue)
4608 gfc_expr lvalue;
4609 bool r;
4610 bool pointer, proc_pointer;
4612 memset (&lvalue, '\0', sizeof (gfc_expr));
4614 lvalue.expr_type = EXPR_VARIABLE;
4615 lvalue.ts = sym->ts;
4616 if (sym->as)
4618 lvalue.rank = sym->as->rank;
4619 lvalue.corank = sym->as->corank;
4621 lvalue.symtree = XCNEW (gfc_symtree);
4622 lvalue.symtree->n.sym = sym;
4623 lvalue.where = sym->declared_at;
4625 if (comp)
4627 lvalue.ref = gfc_get_ref ();
4628 lvalue.ref->type = REF_COMPONENT;
4629 lvalue.ref->u.c.component = comp;
4630 lvalue.ref->u.c.sym = sym;
4631 lvalue.ts = comp->ts;
4632 lvalue.rank = comp->as ? comp->as->rank : 0;
4633 lvalue.corank = comp->as ? comp->as->corank : 0;
4634 lvalue.where = comp->loc;
4635 pointer = comp->ts.type == BT_CLASS && CLASS_DATA (comp)
4636 ? CLASS_DATA (comp)->attr.class_pointer : comp->attr.pointer;
4637 proc_pointer = comp->attr.proc_pointer;
4639 else
4641 pointer = sym->ts.type == BT_CLASS && CLASS_DATA (sym)
4642 ? CLASS_DATA (sym)->attr.class_pointer : sym->attr.pointer;
4643 proc_pointer = sym->attr.proc_pointer;
4646 if (pointer || proc_pointer)
4647 r = gfc_check_pointer_assign (&lvalue, rvalue, false, true);
4648 else
4650 /* If a conversion function, e.g., __convert_i8_i4, was inserted
4651 into an array constructor, we should check if it can be reduced
4652 as an initialization expression. */
4653 if (rvalue->expr_type == EXPR_FUNCTION
4654 && rvalue->value.function.isym
4655 && (rvalue->value.function.isym->conversion == 1))
4656 gfc_check_init_expr (rvalue);
4658 r = gfc_check_assign (&lvalue, rvalue, 1);
4661 free (lvalue.symtree);
4662 free (lvalue.ref);
4664 if (!r)
4665 return r;
4667 if (pointer && rvalue->expr_type != EXPR_NULL && !proc_pointer)
4669 /* F08:C461. Additional checks for pointer initialization. */
4670 symbol_attribute attr;
4671 attr = gfc_expr_attr (rvalue);
4672 if (attr.allocatable)
4674 gfc_error ("Pointer initialization target at %L "
4675 "must not be ALLOCATABLE", &rvalue->where);
4676 return false;
4678 if (!attr.target || attr.pointer)
4680 gfc_error ("Pointer initialization target at %L "
4681 "must have the TARGET attribute", &rvalue->where);
4682 return false;
4685 if (!attr.save && rvalue->expr_type == EXPR_VARIABLE
4686 && rvalue->symtree->n.sym->ns->proc_name
4687 && rvalue->symtree->n.sym->ns->proc_name->attr.is_main_program)
4689 rvalue->symtree->n.sym->ns->proc_name->attr.save = SAVE_IMPLICIT;
4690 attr.save = SAVE_IMPLICIT;
4693 if (!attr.save)
4695 gfc_error ("Pointer initialization target at %L "
4696 "must have the SAVE attribute", &rvalue->where);
4697 return false;
4701 if (proc_pointer && rvalue->expr_type != EXPR_NULL)
4703 /* F08:C1220. Additional checks for procedure pointer initialization. */
4704 symbol_attribute attr = gfc_expr_attr (rvalue);
4705 if (attr.proc_pointer)
4707 gfc_error ("Procedure pointer initialization target at %L "
4708 "may not be a procedure pointer", &rvalue->where);
4709 return false;
4711 if (attr.proc == PROC_INTERNAL)
4713 gfc_error ("Internal procedure %qs is invalid in "
4714 "procedure pointer initialization at %L",
4715 rvalue->symtree->name, &rvalue->where);
4716 return false;
4718 if (attr.dummy)
4720 gfc_error ("Dummy procedure %qs is invalid in "
4721 "procedure pointer initialization at %L",
4722 rvalue->symtree->name, &rvalue->where);
4723 return false;
4727 return true;
4730 /* Build an initializer for a local integer, real, complex, logical, or
4731 character variable, based on the command line flags finit-local-zero,
4732 finit-integer=, finit-real=, finit-logical=, and finit-character=.
4733 With force, an initializer is ALWAYS generated. */
4735 static gfc_expr *
4736 gfc_build_init_expr (gfc_typespec *ts, locus *where, bool force)
4738 gfc_expr *init_expr;
4740 /* Try to build an initializer expression. */
4741 init_expr = gfc_get_constant_expr (ts->type, ts->kind, where);
4743 /* If we want to force generation, make sure we default to zero. */
4744 gfc_init_local_real init_real = flag_init_real;
4745 int init_logical = gfc_option.flag_init_logical;
4746 if (force)
4748 if (init_real == GFC_INIT_REAL_OFF)
4749 init_real = GFC_INIT_REAL_ZERO;
4750 if (init_logical == GFC_INIT_LOGICAL_OFF)
4751 init_logical = GFC_INIT_LOGICAL_FALSE;
4754 /* We will only initialize integers, reals, complex, logicals, and
4755 characters, and only if the corresponding command-line flags
4756 were set. Otherwise, we free init_expr and return null. */
4757 switch (ts->type)
4759 case BT_INTEGER:
4760 if (force || gfc_option.flag_init_integer != GFC_INIT_INTEGER_OFF)
4761 mpz_set_si (init_expr->value.integer,
4762 gfc_option.flag_init_integer_value);
4763 else
4765 gfc_free_expr (init_expr);
4766 init_expr = NULL;
4768 break;
4770 case BT_REAL:
4771 switch (init_real)
4773 case GFC_INIT_REAL_SNAN:
4774 init_expr->is_snan = 1;
4775 /* Fall through. */
4776 case GFC_INIT_REAL_NAN:
4777 mpfr_set_nan (init_expr->value.real);
4778 break;
4780 case GFC_INIT_REAL_INF:
4781 mpfr_set_inf (init_expr->value.real, 1);
4782 break;
4784 case GFC_INIT_REAL_NEG_INF:
4785 mpfr_set_inf (init_expr->value.real, -1);
4786 break;
4788 case GFC_INIT_REAL_ZERO:
4789 mpfr_set_ui (init_expr->value.real, 0.0, GFC_RND_MODE);
4790 break;
4792 default:
4793 gfc_free_expr (init_expr);
4794 init_expr = NULL;
4795 break;
4797 break;
4799 case BT_COMPLEX:
4800 switch (init_real)
4802 case GFC_INIT_REAL_SNAN:
4803 init_expr->is_snan = 1;
4804 /* Fall through. */
4805 case GFC_INIT_REAL_NAN:
4806 mpfr_set_nan (mpc_realref (init_expr->value.complex));
4807 mpfr_set_nan (mpc_imagref (init_expr->value.complex));
4808 break;
4810 case GFC_INIT_REAL_INF:
4811 mpfr_set_inf (mpc_realref (init_expr->value.complex), 1);
4812 mpfr_set_inf (mpc_imagref (init_expr->value.complex), 1);
4813 break;
4815 case GFC_INIT_REAL_NEG_INF:
4816 mpfr_set_inf (mpc_realref (init_expr->value.complex), -1);
4817 mpfr_set_inf (mpc_imagref (init_expr->value.complex), -1);
4818 break;
4820 case GFC_INIT_REAL_ZERO:
4821 mpc_set_ui (init_expr->value.complex, 0, GFC_MPC_RND_MODE);
4822 break;
4824 default:
4825 gfc_free_expr (init_expr);
4826 init_expr = NULL;
4827 break;
4829 break;
4831 case BT_LOGICAL:
4832 if (init_logical == GFC_INIT_LOGICAL_FALSE)
4833 init_expr->value.logical = 0;
4834 else if (init_logical == GFC_INIT_LOGICAL_TRUE)
4835 init_expr->value.logical = 1;
4836 else
4838 gfc_free_expr (init_expr);
4839 init_expr = NULL;
4841 break;
4843 case BT_CHARACTER:
4844 /* For characters, the length must be constant in order to
4845 create a default initializer. */
4846 if ((force || gfc_option.flag_init_character == GFC_INIT_CHARACTER_ON)
4847 && ts->u.cl->length
4848 && ts->u.cl->length->expr_type == EXPR_CONSTANT)
4850 HOST_WIDE_INT char_len = gfc_mpz_get_hwi (ts->u.cl->length->value.integer);
4851 init_expr->value.character.length = char_len;
4852 init_expr->value.character.string = gfc_get_wide_string (char_len+1);
4853 for (size_t i = 0; i < (size_t) char_len; i++)
4854 init_expr->value.character.string[i]
4855 = (unsigned char) gfc_option.flag_init_character_value;
4857 else
4859 gfc_free_expr (init_expr);
4860 init_expr = NULL;
4862 if (!init_expr
4863 && (force || gfc_option.flag_init_character == GFC_INIT_CHARACTER_ON)
4864 && ts->u.cl->length && flag_max_stack_var_size != 0)
4866 gfc_actual_arglist *arg;
4867 init_expr = gfc_get_expr ();
4868 init_expr->where = *where;
4869 init_expr->ts = *ts;
4870 init_expr->expr_type = EXPR_FUNCTION;
4871 init_expr->value.function.isym =
4872 gfc_intrinsic_function_by_id (GFC_ISYM_REPEAT);
4873 init_expr->value.function.name = "repeat";
4874 arg = gfc_get_actual_arglist ();
4875 arg->expr = gfc_get_character_expr (ts->kind, where, NULL, 1);
4876 arg->expr->value.character.string[0] =
4877 gfc_option.flag_init_character_value;
4878 arg->next = gfc_get_actual_arglist ();
4879 arg->next->expr = gfc_copy_expr (ts->u.cl->length);
4880 init_expr->value.function.actual = arg;
4882 break;
4884 default:
4885 gfc_free_expr (init_expr);
4886 init_expr = NULL;
4889 return init_expr;
4892 /* Invoke gfc_build_init_expr to create an initializer expression, but do not
4893 * require that an expression be built. */
4895 gfc_expr *
4896 gfc_build_default_init_expr (gfc_typespec *ts, locus *where)
4898 return gfc_build_init_expr (ts, where, false);
4901 /* Apply an initialization expression to a typespec. Can be used for symbols or
4902 components. Similar to add_init_expr_to_sym in decl.cc; could probably be
4903 combined with some effort. */
4905 void
4906 gfc_apply_init (gfc_typespec *ts, symbol_attribute *attr, gfc_expr *init)
4908 if (ts->type == BT_CHARACTER && !attr->pointer && init
4909 && ts->u.cl
4910 && ts->u.cl->length
4911 && ts->u.cl->length->expr_type == EXPR_CONSTANT
4912 && ts->u.cl->length->ts.type == BT_INTEGER)
4914 HOST_WIDE_INT len = gfc_mpz_get_hwi (ts->u.cl->length->value.integer);
4916 if (init->expr_type == EXPR_CONSTANT)
4917 gfc_set_constant_character_len (len, init, -1);
4918 else if (init
4919 && init->ts.type == BT_CHARACTER
4920 && init->ts.u.cl && init->ts.u.cl->length
4921 && mpz_cmp (ts->u.cl->length->value.integer,
4922 init->ts.u.cl->length->value.integer))
4924 gfc_constructor *ctor;
4925 ctor = gfc_constructor_first (init->value.constructor);
4927 if (ctor)
4929 bool has_ts = (init->ts.u.cl
4930 && init->ts.u.cl->length_from_typespec);
4932 /* Remember the length of the first element for checking
4933 that all elements *in the constructor* have the same
4934 length. This need not be the length of the LHS! */
4935 gcc_assert (ctor->expr->expr_type == EXPR_CONSTANT);
4936 gcc_assert (ctor->expr->ts.type == BT_CHARACTER);
4937 gfc_charlen_t first_len = ctor->expr->value.character.length;
4939 for ( ; ctor; ctor = gfc_constructor_next (ctor))
4940 if (ctor->expr->expr_type == EXPR_CONSTANT)
4942 gfc_set_constant_character_len (len, ctor->expr,
4943 has_ts ? -1 : first_len);
4944 if (!ctor->expr->ts.u.cl)
4945 ctor->expr->ts.u.cl
4946 = gfc_new_charlen (gfc_current_ns, ts->u.cl);
4947 else
4948 ctor->expr->ts.u.cl->length
4949 = gfc_copy_expr (ts->u.cl->length);
4957 /* Check whether an expression is a structure constructor and whether it has
4958 other values than NULL. */
4960 static bool
4961 is_non_empty_structure_constructor (gfc_expr * e)
4963 if (e->expr_type != EXPR_STRUCTURE)
4964 return false;
4966 gfc_constructor *cons = gfc_constructor_first (e->value.constructor);
4967 while (cons)
4969 if (!cons->expr || cons->expr->expr_type != EXPR_NULL)
4970 return true;
4971 cons = gfc_constructor_next (cons);
4973 return false;
4977 /* Check for default initializer; sym->value is not enough
4978 as it is also set for EXPR_NULL of allocatables. */
4980 bool
4981 gfc_has_default_initializer (gfc_symbol *der)
4983 gfc_component *c;
4985 gcc_assert (gfc_fl_struct (der->attr.flavor));
4986 for (c = der->components; c; c = c->next)
4987 if (gfc_bt_struct (c->ts.type))
4989 if (!c->attr.pointer && !c->attr.proc_pointer
4990 && !(c->attr.allocatable && der == c->ts.u.derived)
4991 && ((c->initializer
4992 && is_non_empty_structure_constructor (c->initializer))
4993 || gfc_has_default_initializer (c->ts.u.derived)))
4994 return true;
4995 if (c->attr.pointer && c->initializer)
4996 return true;
4998 else
5000 if (c->initializer)
5001 return true;
5004 return false;
5009 Generate an initializer expression which initializes the entirety of a union.
5010 A normal structure constructor is insufficient without undue effort, because
5011 components of maps may be oddly aligned/overlapped. (For example if a
5012 character is initialized from one map overtop a real from the other, only one
5013 byte of the real is actually initialized.) Unfortunately we don't know the
5014 size of the union right now, so we can't generate a proper initializer, but
5015 we use a NULL expr as a placeholder and do the right thing later in
5016 gfc_trans_subcomponent_assign.
5018 static gfc_expr *
5019 generate_union_initializer (gfc_component *un)
5021 if (un == NULL || un->ts.type != BT_UNION)
5022 return NULL;
5024 gfc_expr *placeholder = gfc_get_null_expr (&un->loc);
5025 placeholder->ts = un->ts;
5026 return placeholder;
5030 /* Get the user-specified initializer for a union, if any. This means the user
5031 has said to initialize component(s) of a map. For simplicity's sake we
5032 only allow the user to initialize the first map. We don't have to worry
5033 about overlapping initializers as they are released early in resolution (see
5034 resolve_fl_struct). */
5036 static gfc_expr *
5037 get_union_initializer (gfc_symbol *union_type, gfc_component **map_p)
5039 gfc_component *map;
5040 gfc_expr *init=NULL;
5042 if (!union_type || union_type->attr.flavor != FL_UNION)
5043 return NULL;
5045 for (map = union_type->components; map; map = map->next)
5047 if (gfc_has_default_initializer (map->ts.u.derived))
5049 init = gfc_default_initializer (&map->ts);
5050 if (map_p)
5051 *map_p = map;
5052 break;
5056 if (map_p && !init)
5057 *map_p = NULL;
5059 return init;
5062 static bool
5063 class_allocatable (gfc_component *comp)
5065 return comp->ts.type == BT_CLASS && comp->attr.class_ok && CLASS_DATA (comp)
5066 && CLASS_DATA (comp)->attr.allocatable;
5069 static bool
5070 class_pointer (gfc_component *comp)
5072 return comp->ts.type == BT_CLASS && comp->attr.class_ok && CLASS_DATA (comp)
5073 && CLASS_DATA (comp)->attr.pointer;
5076 static bool
5077 comp_allocatable (gfc_component *comp)
5079 return comp->attr.allocatable || class_allocatable (comp);
5082 static bool
5083 comp_pointer (gfc_component *comp)
5085 return comp->attr.pointer
5086 || comp->attr.proc_pointer
5087 || comp->attr.class_pointer
5088 || class_pointer (comp);
5091 /* Fetch or generate an initializer for the given component.
5092 Only generate an initializer if generate is true. */
5094 static gfc_expr *
5095 component_initializer (gfc_component *c, bool generate)
5097 gfc_expr *init = NULL;
5099 /* Allocatable components always get EXPR_NULL.
5100 Pointer components are only initialized when generating, and only if they
5101 do not already have an initializer. */
5102 if (comp_allocatable (c) || (generate && comp_pointer (c) && !c->initializer))
5104 init = gfc_get_null_expr (&c->loc);
5105 init->ts = c->ts;
5106 return init;
5109 /* See if we can find the initializer immediately. */
5110 if (c->initializer || !generate)
5111 return c->initializer;
5113 /* Recursively handle derived type components. */
5114 else if (c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
5115 init = gfc_generate_initializer (&c->ts, true);
5117 else if (c->ts.type == BT_UNION && c->ts.u.derived->components)
5119 gfc_component *map = NULL;
5120 gfc_constructor *ctor;
5121 gfc_expr *user_init;
5123 /* If we don't have a user initializer and we aren't generating one, this
5124 union has no initializer. */
5125 user_init = get_union_initializer (c->ts.u.derived, &map);
5126 if (!user_init && !generate)
5127 return NULL;
5129 /* Otherwise use a structure constructor. */
5130 init = gfc_get_structure_constructor_expr (c->ts.type, c->ts.kind,
5131 &c->loc);
5132 init->ts = c->ts;
5134 /* If we are to generate an initializer for the union, add a constructor
5135 which initializes the whole union first. */
5136 if (generate)
5138 ctor = gfc_constructor_get ();
5139 ctor->expr = generate_union_initializer (c);
5140 gfc_constructor_append (&init->value.constructor, ctor);
5143 /* If we found an initializer in one of our maps, apply it. Note this
5144 is applied _after_ the entire-union initializer above if any. */
5145 if (user_init)
5147 ctor = gfc_constructor_get ();
5148 ctor->expr = user_init;
5149 ctor->n.component = map;
5150 gfc_constructor_append (&init->value.constructor, ctor);
5154 /* Treat simple components like locals. */
5155 else
5157 /* We MUST give an initializer, so force generation. */
5158 init = gfc_build_init_expr (&c->ts, &c->loc, true);
5159 gfc_apply_init (&c->ts, &c->attr, init);
5162 return init;
5166 /* Get an expression for a default initializer of a derived type. */
5168 gfc_expr *
5169 gfc_default_initializer (gfc_typespec *ts)
5171 return gfc_generate_initializer (ts, false);
5174 /* Generate an initializer expression for an iso_c_binding type
5175 such as c_[fun]ptr. The appropriate initializer is c_null_[fun]ptr. */
5177 static gfc_expr *
5178 generate_isocbinding_initializer (gfc_symbol *derived)
5180 /* The initializers have already been built into the c_null_[fun]ptr symbols
5181 from gen_special_c_interop_ptr. */
5182 gfc_symtree *npsym = NULL;
5183 if (0 == strcmp (derived->name, "c_ptr"))
5184 gfc_find_sym_tree ("c_null_ptr", gfc_current_ns, true, &npsym);
5185 else if (0 == strcmp (derived->name, "c_funptr"))
5186 gfc_find_sym_tree ("c_null_funptr", gfc_current_ns, true, &npsym);
5187 else
5188 gfc_internal_error ("generate_isocbinding_initializer(): bad iso_c_binding"
5189 " type, expected %<c_ptr%> or %<c_funptr%>");
5190 if (npsym)
5192 gfc_expr *init = gfc_copy_expr (npsym->n.sym->value);
5193 init->symtree = npsym;
5194 init->ts.is_iso_c = true;
5195 return init;
5198 return NULL;
5201 /* Get or generate an expression for a default initializer of a derived type.
5202 If -finit-derived is specified, generate default initialization expressions
5203 for components that lack them when generate is set. */
5205 gfc_expr *
5206 gfc_generate_initializer (gfc_typespec *ts, bool generate)
5208 gfc_expr *init, *tmp;
5209 gfc_component *comp;
5211 generate = flag_init_derived && generate;
5213 if (ts->u.derived->ts.is_iso_c && generate)
5214 return generate_isocbinding_initializer (ts->u.derived);
5216 /* See if we have a default initializer in this, but not in nested
5217 types (otherwise we could use gfc_has_default_initializer()).
5218 We don't need to check if we are going to generate them. */
5219 comp = ts->u.derived->components;
5220 if (!generate)
5222 for (; comp; comp = comp->next)
5223 if (comp->initializer || comp_allocatable (comp))
5224 break;
5227 if (!comp)
5228 return NULL;
5230 init = gfc_get_structure_constructor_expr (ts->type, ts->kind,
5231 &ts->u.derived->declared_at);
5232 init->ts = *ts;
5234 for (comp = ts->u.derived->components; comp; comp = comp->next)
5236 gfc_constructor *ctor = gfc_constructor_get();
5238 /* Fetch or generate an initializer for the component. */
5239 tmp = component_initializer (comp, generate);
5240 if (tmp)
5242 /* Save the component ref for STRUCTUREs and UNIONs. */
5243 if (ts->u.derived->attr.flavor == FL_STRUCT
5244 || ts->u.derived->attr.flavor == FL_UNION)
5245 ctor->n.component = comp;
5247 /* If the initializer was not generated, we need a copy. */
5248 ctor->expr = comp->initializer ? gfc_copy_expr (tmp) : tmp;
5249 if ((comp->ts.type != tmp->ts.type || comp->ts.kind != tmp->ts.kind)
5250 && !comp->attr.pointer && !comp->attr.proc_pointer)
5252 bool val;
5253 val = gfc_convert_type_warn (ctor->expr, &comp->ts, 1, false);
5254 if (val == false)
5255 return NULL;
5259 gfc_constructor_append (&init->value.constructor, ctor);
5262 return init;
5266 /* Given a symbol, create an expression node with that symbol as a
5267 variable. If the symbol is array valued, setup a reference of the
5268 whole array. */
5270 gfc_expr *
5271 gfc_get_variable_expr (gfc_symtree *var)
5273 gfc_expr *e;
5275 e = gfc_get_expr ();
5276 e->expr_type = EXPR_VARIABLE;
5277 e->symtree = var;
5278 e->ts = var->n.sym->ts;
5280 if (var->n.sym->attr.flavor != FL_PROCEDURE
5281 && ((var->n.sym->as != NULL && var->n.sym->ts.type != BT_CLASS)
5282 || (var->n.sym->ts.type == BT_CLASS && var->n.sym->ts.u.derived
5283 && CLASS_DATA (var->n.sym)
5284 && CLASS_DATA (var->n.sym)->as)))
5286 gfc_array_spec *as = var->n.sym->ts.type == BT_CLASS
5287 ? CLASS_DATA (var->n.sym)->as
5288 : var->n.sym->as;
5289 e->rank = as->rank;
5290 e->corank = as->corank;
5291 e->ref = gfc_get_ref ();
5292 e->ref->type = REF_ARRAY;
5293 e->ref->u.ar.type = AR_FULL;
5294 e->ref->u.ar.as = gfc_copy_array_spec (as);
5297 return e;
5301 /* Adds a full array reference to an expression, as needed. */
5303 void
5304 gfc_add_full_array_ref (gfc_expr *e, gfc_array_spec *as)
5306 gfc_ref *ref;
5307 for (ref = e->ref; ref; ref = ref->next)
5308 if (!ref->next)
5309 break;
5310 if (ref)
5312 ref->next = gfc_get_ref ();
5313 ref = ref->next;
5315 else
5317 e->ref = gfc_get_ref ();
5318 ref = e->ref;
5320 ref->type = REF_ARRAY;
5321 ref->u.ar.type = AR_FULL;
5322 ref->u.ar.dimen = e->rank;
5323 /* Do not set the corank here, or resolve will not be able to set correct
5324 dimen-types for the coarray. */
5325 ref->u.ar.where = e->where;
5326 ref->u.ar.as = as;
5330 gfc_expr *
5331 gfc_lval_expr_from_sym (gfc_symbol *sym)
5333 gfc_expr *lval;
5334 gfc_array_spec *as;
5335 lval = gfc_get_expr ();
5336 lval->expr_type = EXPR_VARIABLE;
5337 lval->where = sym->declared_at;
5338 lval->ts = sym->ts;
5339 lval->symtree = gfc_find_symtree (sym->ns->sym_root, sym->name);
5341 /* It will always be a full array. */
5342 as = IS_CLASS_ARRAY (sym) ? CLASS_DATA (sym)->as : sym->as;
5343 lval->rank = as ? as->rank : 0;
5344 lval->corank = as ? as->corank : 0;
5345 if (lval->rank || lval->corank)
5346 gfc_add_full_array_ref (lval, as);
5347 return lval;
5351 /* Returns the array_spec of a full array expression. A NULL is
5352 returned otherwise. */
5353 gfc_array_spec *
5354 gfc_get_full_arrayspec_from_expr (gfc_expr *expr)
5356 gfc_array_spec *as;
5357 gfc_ref *ref;
5359 if (expr->rank == 0)
5360 return NULL;
5362 /* Follow any component references. */
5363 if (expr->expr_type == EXPR_VARIABLE
5364 || expr->expr_type == EXPR_CONSTANT)
5366 if (expr->symtree)
5367 as = expr->symtree->n.sym->as;
5368 else
5369 as = NULL;
5371 for (ref = expr->ref; ref; ref = ref->next)
5373 switch (ref->type)
5375 case REF_COMPONENT:
5376 as = ref->u.c.component->as;
5377 continue;
5379 case REF_SUBSTRING:
5380 case REF_INQUIRY:
5381 continue;
5383 case REF_ARRAY:
5385 switch (ref->u.ar.type)
5387 case AR_ELEMENT:
5388 case AR_SECTION:
5389 case AR_UNKNOWN:
5390 as = NULL;
5391 continue;
5393 case AR_FULL:
5394 break;
5396 break;
5401 else
5402 as = NULL;
5404 return as;
5408 /* General expression traversal function. */
5410 bool
5411 gfc_traverse_expr (gfc_expr *expr, gfc_symbol *sym,
5412 bool (*func)(gfc_expr *, gfc_symbol *, int*),
5413 int f)
5415 gfc_array_ref ar;
5416 gfc_ref *ref;
5417 gfc_actual_arglist *args;
5418 gfc_constructor *c;
5419 int i;
5421 if (!expr)
5422 return false;
5424 if ((*func) (expr, sym, &f))
5425 return true;
5427 if (expr->ts.type == BT_CHARACTER
5428 && expr->ts.u.cl
5429 && expr->ts.u.cl->length
5430 && expr->ts.u.cl->length->expr_type != EXPR_CONSTANT
5431 && gfc_traverse_expr (expr->ts.u.cl->length, sym, func, f))
5432 return true;
5434 switch (expr->expr_type)
5436 case EXPR_PPC:
5437 case EXPR_COMPCALL:
5438 case EXPR_FUNCTION:
5439 for (args = expr->value.function.actual; args; args = args->next)
5441 if (gfc_traverse_expr (args->expr, sym, func, f))
5442 return true;
5444 break;
5446 case EXPR_VARIABLE:
5447 case EXPR_CONSTANT:
5448 case EXPR_NULL:
5449 case EXPR_SUBSTRING:
5450 break;
5452 case EXPR_STRUCTURE:
5453 case EXPR_ARRAY:
5454 for (c = gfc_constructor_first (expr->value.constructor);
5455 c; c = gfc_constructor_next (c))
5457 if (gfc_traverse_expr (c->expr, sym, func, f))
5458 return true;
5459 if (c->iterator)
5461 if (gfc_traverse_expr (c->iterator->var, sym, func, f))
5462 return true;
5463 if (gfc_traverse_expr (c->iterator->start, sym, func, f))
5464 return true;
5465 if (gfc_traverse_expr (c->iterator->end, sym, func, f))
5466 return true;
5467 if (gfc_traverse_expr (c->iterator->step, sym, func, f))
5468 return true;
5471 break;
5473 case EXPR_OP:
5474 if (gfc_traverse_expr (expr->value.op.op1, sym, func, f))
5475 return true;
5476 if (gfc_traverse_expr (expr->value.op.op2, sym, func, f))
5477 return true;
5478 break;
5480 default:
5481 gcc_unreachable ();
5482 break;
5485 ref = expr->ref;
5486 while (ref != NULL)
5488 switch (ref->type)
5490 case REF_ARRAY:
5491 ar = ref->u.ar;
5492 for (i = 0; i < GFC_MAX_DIMENSIONS; i++)
5494 if (gfc_traverse_expr (ar.start[i], sym, func, f))
5495 return true;
5496 if (gfc_traverse_expr (ar.end[i], sym, func, f))
5497 return true;
5498 if (gfc_traverse_expr (ar.stride[i], sym, func, f))
5499 return true;
5501 break;
5503 case REF_SUBSTRING:
5504 if (gfc_traverse_expr (ref->u.ss.start, sym, func, f))
5505 return true;
5506 if (gfc_traverse_expr (ref->u.ss.end, sym, func, f))
5507 return true;
5508 break;
5510 case REF_COMPONENT:
5511 if (ref->u.c.component->ts.type == BT_CHARACTER
5512 && ref->u.c.component->ts.u.cl
5513 && ref->u.c.component->ts.u.cl->length
5514 && ref->u.c.component->ts.u.cl->length->expr_type
5515 != EXPR_CONSTANT
5516 && gfc_traverse_expr (ref->u.c.component->ts.u.cl->length,
5517 sym, func, f))
5518 return true;
5520 if (ref->u.c.component->as)
5521 for (i = 0; i < ref->u.c.component->as->rank
5522 + ref->u.c.component->as->corank; i++)
5524 if (gfc_traverse_expr (ref->u.c.component->as->lower[i],
5525 sym, func, f))
5526 return true;
5527 if (gfc_traverse_expr (ref->u.c.component->as->upper[i],
5528 sym, func, f))
5529 return true;
5531 break;
5533 case REF_INQUIRY:
5534 return false;
5536 default:
5537 gcc_unreachable ();
5539 ref = ref->next;
5541 return false;
5544 /* Traverse expr, marking all EXPR_VARIABLE symbols referenced. */
5546 static bool
5547 expr_set_symbols_referenced (gfc_expr *expr,
5548 gfc_symbol *sym ATTRIBUTE_UNUSED,
5549 int *f ATTRIBUTE_UNUSED)
5551 if (expr->expr_type != EXPR_VARIABLE)
5552 return false;
5553 gfc_set_sym_referenced (expr->symtree->n.sym);
5554 return false;
5557 void
5558 gfc_expr_set_symbols_referenced (gfc_expr *expr)
5560 gfc_traverse_expr (expr, NULL, expr_set_symbols_referenced, 0);
5564 /* Determine if an expression is a procedure pointer component and return
5565 the component in that case. Otherwise return NULL. */
5567 gfc_component *
5568 gfc_get_proc_ptr_comp (gfc_expr *expr)
5570 gfc_ref *ref;
5572 if (!expr || !expr->ref)
5573 return NULL;
5575 ref = expr->ref;
5576 while (ref->next)
5577 ref = ref->next;
5579 if (ref->type == REF_COMPONENT
5580 && ref->u.c.component->attr.proc_pointer)
5581 return ref->u.c.component;
5583 return NULL;
5587 /* Determine if an expression is a procedure pointer component. */
5589 bool
5590 gfc_is_proc_ptr_comp (gfc_expr *expr)
5592 return (gfc_get_proc_ptr_comp (expr) != NULL);
5596 /* Determine if an expression is a function with an allocatable class scalar
5597 result. */
5598 bool
5599 gfc_is_alloc_class_scalar_function (gfc_expr *expr)
5601 if (expr->expr_type == EXPR_FUNCTION
5602 && ((expr->value.function.esym
5603 && expr->value.function.esym->result
5604 && expr->value.function.esym->result->ts.type == BT_CLASS
5605 && !CLASS_DATA (expr->value.function.esym->result)->attr.dimension
5606 && CLASS_DATA (expr->value.function.esym->result)->attr.allocatable)
5607 || (expr->ts.type == BT_CLASS
5608 && CLASS_DATA (expr)->attr.allocatable
5609 && !CLASS_DATA (expr)->attr.dimension)))
5610 return true;
5612 return false;
5616 /* Determine if an expression is a function with an allocatable class array
5617 result. */
5618 bool
5619 gfc_is_class_array_function (gfc_expr *expr)
5621 if (expr->expr_type == EXPR_FUNCTION
5622 && expr->value.function.esym
5623 && expr->value.function.esym->result
5624 && expr->value.function.esym->result->ts.type == BT_CLASS
5625 && CLASS_DATA (expr->value.function.esym->result)->attr.dimension
5626 && (CLASS_DATA (expr->value.function.esym->result)->attr.allocatable
5627 || CLASS_DATA (expr->value.function.esym->result)->attr.pointer))
5628 return true;
5630 return false;
5634 /* Walk an expression tree and check each variable encountered for being typed.
5635 If strict is not set, a top-level variable is tolerated untyped in -std=gnu
5636 mode as is a basic arithmetic expression using those; this is for things in
5637 legacy-code like:
5639 INTEGER :: arr(n), n
5640 INTEGER :: arr(n + 1), n
5642 The namespace is needed for IMPLICIT typing. */
5644 static gfc_namespace* check_typed_ns;
5646 static bool
5647 expr_check_typed_help (gfc_expr* e, gfc_symbol* sym ATTRIBUTE_UNUSED,
5648 int* f ATTRIBUTE_UNUSED)
5650 bool t;
5652 if (e->expr_type != EXPR_VARIABLE)
5653 return false;
5655 gcc_assert (e->symtree);
5656 t = gfc_check_symbol_typed (e->symtree->n.sym, check_typed_ns,
5657 true, e->where);
5659 return (!t);
5662 bool
5663 gfc_expr_check_typed (gfc_expr* e, gfc_namespace* ns, bool strict)
5665 bool error_found;
5667 /* If this is a top-level variable or EXPR_OP, do the check with strict given
5668 to us. */
5669 if (!strict)
5671 if (e->expr_type == EXPR_VARIABLE && !e->ref)
5672 return gfc_check_symbol_typed (e->symtree->n.sym, ns, strict, e->where);
5674 if (e->expr_type == EXPR_OP)
5676 bool t = true;
5678 gcc_assert (e->value.op.op1);
5679 t = gfc_expr_check_typed (e->value.op.op1, ns, strict);
5681 if (t && e->value.op.op2)
5682 t = gfc_expr_check_typed (e->value.op.op2, ns, strict);
5684 return t;
5688 /* Otherwise, walk the expression and do it strictly. */
5689 check_typed_ns = ns;
5690 error_found = gfc_traverse_expr (e, NULL, &expr_check_typed_help, 0);
5692 return error_found ? false : true;
5696 /* This function returns true if it contains any references to PDT KIND
5697 or LEN parameters. */
5699 static bool
5700 derived_parameter_expr (gfc_expr* e, gfc_symbol* sym ATTRIBUTE_UNUSED,
5701 int* f ATTRIBUTE_UNUSED)
5703 if (e->expr_type != EXPR_VARIABLE)
5704 return false;
5706 gcc_assert (e->symtree);
5707 if (e->symtree->n.sym->attr.pdt_kind
5708 || e->symtree->n.sym->attr.pdt_len)
5709 return true;
5711 return false;
5715 bool
5716 gfc_derived_parameter_expr (gfc_expr *e)
5718 return gfc_traverse_expr (e, NULL, &derived_parameter_expr, 0);
5722 /* This function returns the overall type of a type parameter spec list.
5723 If all the specs are explicit, SPEC_EXPLICIT is returned. If any of the
5724 parameters are assumed/deferred then SPEC_ASSUMED/DEFERRED is returned
5725 unless derived is not NULL. In this latter case, all the LEN parameters
5726 must be either assumed or deferred for the return argument to be set to
5727 anything other than SPEC_EXPLICIT. */
5729 gfc_param_spec_type
5730 gfc_spec_list_type (gfc_actual_arglist *param_list, gfc_symbol *derived)
5732 gfc_param_spec_type res = SPEC_EXPLICIT;
5733 gfc_component *c;
5734 bool seen_assumed = false;
5735 bool seen_deferred = false;
5737 if (derived == NULL)
5739 for (; param_list; param_list = param_list->next)
5740 if (param_list->spec_type == SPEC_ASSUMED
5741 || param_list->spec_type == SPEC_DEFERRED)
5742 return param_list->spec_type;
5744 else
5746 for (; param_list; param_list = param_list->next)
5748 c = gfc_find_component (derived, param_list->name,
5749 true, true, NULL);
5750 gcc_assert (c != NULL);
5751 if (c->attr.pdt_kind)
5752 continue;
5753 else if (param_list->spec_type == SPEC_EXPLICIT)
5754 return SPEC_EXPLICIT;
5755 seen_assumed = param_list->spec_type == SPEC_ASSUMED;
5756 seen_deferred = param_list->spec_type == SPEC_DEFERRED;
5757 if (seen_assumed && seen_deferred)
5758 return SPEC_EXPLICIT;
5760 res = seen_assumed ? SPEC_ASSUMED : SPEC_DEFERRED;
5762 return res;
5766 bool
5767 gfc_ref_this_image (gfc_ref *ref)
5769 int n;
5771 gcc_assert (ref->type == REF_ARRAY && ref->u.ar.codimen > 0);
5773 for (n = ref->u.ar.dimen; n < ref->u.ar.dimen + ref->u.ar.codimen; n++)
5774 if (ref->u.ar.dimen_type[n] != DIMEN_THIS_IMAGE)
5775 return false;
5777 return true;
5780 gfc_expr *
5781 gfc_find_team_co (gfc_expr *e)
5783 gfc_ref *ref;
5785 for (ref = e->ref; ref; ref = ref->next)
5786 if (ref->type == REF_ARRAY && ref->u.ar.codimen > 0)
5787 return ref->u.ar.team;
5789 if (e->value.function.actual->expr)
5790 for (ref = e->value.function.actual->expr->ref; ref;
5791 ref = ref->next)
5792 if (ref->type == REF_ARRAY && ref->u.ar.codimen > 0)
5793 return ref->u.ar.team;
5795 return NULL;
5798 gfc_expr *
5799 gfc_find_stat_co (gfc_expr *e)
5801 gfc_ref *ref;
5803 for (ref = e->ref; ref; ref = ref->next)
5804 if (ref->type == REF_ARRAY && ref->u.ar.codimen > 0)
5805 return ref->u.ar.stat;
5807 if (e->value.function.actual->expr)
5808 for (ref = e->value.function.actual->expr->ref; ref;
5809 ref = ref->next)
5810 if (ref->type == REF_ARRAY && ref->u.ar.codimen > 0)
5811 return ref->u.ar.stat;
5813 return NULL;
5816 bool
5817 gfc_is_coindexed (gfc_expr *e)
5819 gfc_ref *ref;
5821 if (e->expr_type == EXPR_FUNCTION && e->value.function.isym
5822 && e->value.function.isym->id == GFC_ISYM_CAF_GET)
5823 e = e->value.function.actual->expr;
5825 for (ref = e->ref; ref; ref = ref->next)
5826 if (ref->type == REF_ARRAY && ref->u.ar.codimen > 0)
5827 return !gfc_ref_this_image (ref);
5829 return false;
5833 /* Coarrays are variables with a corank but not being coindexed. However, also
5834 the following is a coarray: A subobject of a coarray is a coarray if it does
5835 not have any cosubscripts, vector subscripts, allocatable component
5836 selection, or pointer component selection. (F2008, 2.4.7) */
5838 bool
5839 gfc_is_coarray (gfc_expr *e)
5841 gfc_ref *ref;
5842 gfc_symbol *sym;
5843 gfc_component *comp;
5844 bool coindexed;
5845 bool coarray;
5846 int i;
5848 if (e->expr_type != EXPR_VARIABLE)
5849 return false;
5851 coindexed = false;
5852 sym = e->symtree->n.sym;
5854 if (sym->ts.type == BT_CLASS && sym->attr.class_ok)
5855 coarray = CLASS_DATA (sym)->attr.codimension;
5856 else
5857 coarray = sym->attr.codimension;
5859 for (ref = e->ref; ref; ref = ref->next)
5860 switch (ref->type)
5862 case REF_COMPONENT:
5863 comp = ref->u.c.component;
5864 if (comp->ts.type == BT_CLASS && comp->attr.class_ok
5865 && (CLASS_DATA (comp)->attr.class_pointer
5866 || CLASS_DATA (comp)->attr.allocatable))
5868 coindexed = false;
5869 coarray = CLASS_DATA (comp)->attr.codimension;
5871 else if (comp->attr.pointer || comp->attr.allocatable)
5873 coindexed = false;
5874 coarray = comp->attr.codimension;
5876 break;
5878 case REF_ARRAY:
5879 if (!coarray)
5880 break;
5882 if (ref->u.ar.codimen > 0 && !gfc_ref_this_image (ref))
5884 coindexed = true;
5885 break;
5888 for (i = 0; i < ref->u.ar.dimen; i++)
5889 if (ref->u.ar.dimen_type[i] == DIMEN_VECTOR)
5891 coarray = false;
5892 break;
5894 break;
5896 case REF_SUBSTRING:
5897 case REF_INQUIRY:
5898 break;
5901 return coarray && !coindexed;
5905 /* Check whether the expression has an ultimate allocatable component.
5906 Being itself allocatable does not count. */
5907 bool
5908 gfc_has_ultimate_allocatable (gfc_expr *e)
5910 gfc_ref *ref, *last = NULL;
5912 if (e->expr_type != EXPR_VARIABLE)
5913 return false;
5915 for (ref = e->ref; ref; ref = ref->next)
5916 if (ref->type == REF_COMPONENT)
5917 last = ref;
5919 if (last && last->u.c.component->ts.type == BT_CLASS)
5920 return CLASS_DATA (last->u.c.component)->attr.alloc_comp;
5921 else if (last && last->u.c.component->ts.type == BT_DERIVED)
5922 return last->u.c.component->ts.u.derived->attr.alloc_comp;
5923 else if (last)
5924 return false;
5926 if (e->ts.type == BT_CLASS)
5927 return CLASS_DATA (e)->attr.alloc_comp;
5928 else if (e->ts.type == BT_DERIVED)
5929 return e->ts.u.derived->attr.alloc_comp;
5930 else
5931 return false;
5935 /* Check whether the expression has an pointer component.
5936 Being itself a pointer does not count. */
5937 bool
5938 gfc_has_ultimate_pointer (gfc_expr *e)
5940 gfc_ref *ref, *last = NULL;
5942 if (e->expr_type != EXPR_VARIABLE)
5943 return false;
5945 for (ref = e->ref; ref; ref = ref->next)
5946 if (ref->type == REF_COMPONENT)
5947 last = ref;
5949 if (last && last->u.c.component->ts.type == BT_CLASS)
5950 return CLASS_DATA (last->u.c.component)->attr.pointer_comp;
5951 else if (last && last->u.c.component->ts.type == BT_DERIVED)
5952 return last->u.c.component->ts.u.derived->attr.pointer_comp;
5953 else if (last)
5954 return false;
5956 if (e->ts.type == BT_CLASS)
5957 return CLASS_DATA (e)->attr.pointer_comp;
5958 else if (e->ts.type == BT_DERIVED)
5959 return e->ts.u.derived->attr.pointer_comp;
5960 else
5961 return false;
5965 /* Check whether an expression is "simply contiguous", cf. F2008, 6.5.4.
5966 Note: A scalar is not regarded as "simply contiguous" by the standard.
5967 if bool is not strict, some further checks are done - for instance,
5968 a "(::1)" is accepted. */
5970 bool
5971 gfc_is_simply_contiguous (gfc_expr *expr, bool strict, bool permit_element)
5973 bool colon;
5974 int i;
5975 gfc_array_ref *ar = NULL;
5976 gfc_ref *ref, *part_ref = NULL;
5977 gfc_symbol *sym;
5979 if (expr->expr_type == EXPR_ARRAY)
5980 return true;
5982 if (expr->expr_type == EXPR_NULL)
5984 /* F2018:16.9.144 NULL ([MOLD]):
5985 "If MOLD is present, the characteristics are the same as MOLD."
5986 "If MOLD is absent, the characteristics of the result are
5987 determined by the entity with which the reference is associated."
5988 F2018:15.3.2.2 characteristics attributes include CONTIGUOUS. */
5989 if (expr->ts.type == BT_UNKNOWN)
5990 return true;
5991 else
5992 return (gfc_variable_attr (expr, NULL).contiguous
5993 || gfc_variable_attr (expr, NULL).allocatable);
5996 if (expr->expr_type == EXPR_FUNCTION)
5998 if (expr->value.function.isym)
5999 /* TRANSPOSE is the only intrinsic that may return a
6000 non-contiguous array. It's treated as a special case in
6001 gfc_conv_expr_descriptor too. */
6002 return (expr->value.function.isym->id != GFC_ISYM_TRANSPOSE);
6003 else if (expr->value.function.esym)
6004 /* Only a pointer to an array without the contiguous attribute
6005 can be non-contiguous as a result value. */
6006 return (expr->value.function.esym->result->attr.contiguous
6007 || !expr->value.function.esym->result->attr.pointer);
6008 else
6010 /* Type-bound procedures. */
6011 gfc_symbol *s = expr->symtree->n.sym;
6012 if (s->ts.type != BT_CLASS && s->ts.type != BT_DERIVED)
6013 return false;
6015 gfc_ref *rc = NULL;
6016 for (gfc_ref *r = expr->ref; r; r = r->next)
6017 if (r->type == REF_COMPONENT)
6018 rc = r;
6020 if (rc == NULL || rc->u.c.component == NULL
6021 || rc->u.c.component->ts.interface == NULL)
6022 return false;
6024 return rc->u.c.component->ts.interface->attr.contiguous;
6027 else if (expr->expr_type != EXPR_VARIABLE)
6028 return false;
6030 if (!permit_element && expr->rank == 0)
6031 return false;
6033 for (ref = expr->ref; ref; ref = ref->next)
6035 if (ar)
6036 return false; /* Array shall be last part-ref. */
6038 if (ref->type == REF_COMPONENT)
6039 part_ref = ref;
6040 else if (ref->type == REF_SUBSTRING)
6041 return false;
6042 else if (ref->type == REF_INQUIRY)
6043 return false;
6044 else if (ref->u.ar.type != AR_ELEMENT)
6045 ar = &ref->u.ar;
6048 sym = expr->symtree->n.sym;
6049 if ((part_ref
6050 && part_ref->u.c.component
6051 && !part_ref->u.c.component->attr.contiguous
6052 && IS_POINTER (part_ref->u.c.component))
6053 || (!part_ref
6054 && expr->ts.type != BT_CLASS
6055 && !sym->attr.contiguous
6056 && (sym->attr.pointer
6057 || (sym->as && sym->as->type == AS_ASSUMED_RANK)
6058 || (sym->as && sym->as->type == AS_ASSUMED_SHAPE))))
6059 return false;
6061 if (!ar || ar->type == AR_FULL)
6062 return true;
6064 gcc_assert (ar->type == AR_SECTION);
6066 /* Check for simply contiguous array */
6067 colon = true;
6068 for (i = 0; i < ar->dimen; i++)
6070 if (ar->dimen_type[i] == DIMEN_VECTOR)
6071 return false;
6073 if (ar->dimen_type[i] == DIMEN_ELEMENT)
6075 colon = false;
6076 continue;
6079 gcc_assert (ar->dimen_type[i] == DIMEN_RANGE);
6082 /* If the previous section was not contiguous, that's an error,
6083 unless we have effective only one element and checking is not
6084 strict. */
6085 if (!colon && (strict || !ar->start[i] || !ar->end[i]
6086 || ar->start[i]->expr_type != EXPR_CONSTANT
6087 || ar->end[i]->expr_type != EXPR_CONSTANT
6088 || mpz_cmp (ar->start[i]->value.integer,
6089 ar->end[i]->value.integer) != 0))
6090 return false;
6092 /* Following the standard, "(::1)" or - if known at compile time -
6093 "(lbound:ubound)" are not simply contiguous; if strict
6094 is false, they are regarded as simply contiguous. */
6095 if (ar->stride[i] && (strict || ar->stride[i]->expr_type != EXPR_CONSTANT
6096 || ar->stride[i]->ts.type != BT_INTEGER
6097 || mpz_cmp_si (ar->stride[i]->value.integer, 1) != 0))
6098 return false;
6100 if (ar->start[i]
6101 && (strict || ar->start[i]->expr_type != EXPR_CONSTANT
6102 || !ar->as->lower[i]
6103 || ar->as->lower[i]->expr_type != EXPR_CONSTANT
6104 || mpz_cmp (ar->start[i]->value.integer,
6105 ar->as->lower[i]->value.integer) != 0))
6106 colon = false;
6108 if (ar->end[i]
6109 && (strict || ar->end[i]->expr_type != EXPR_CONSTANT
6110 || !ar->as->upper[i]
6111 || ar->as->upper[i]->expr_type != EXPR_CONSTANT
6112 || mpz_cmp (ar->end[i]->value.integer,
6113 ar->as->upper[i]->value.integer) != 0))
6114 colon = false;
6117 return true;
6120 /* Return true if the expression is guaranteed to be non-contiguous,
6121 false if we cannot prove anything. It is probably best to call
6122 this after gfc_is_simply_contiguous. If neither of them returns
6123 true, we cannot say (at compile-time). */
6125 bool
6126 gfc_is_not_contiguous (gfc_expr *array)
6128 int i;
6129 gfc_array_ref *ar = NULL;
6130 gfc_ref *ref;
6131 bool previous_incomplete;
6133 for (ref = array->ref; ref; ref = ref->next)
6135 /* Array-ref shall be last ref. */
6137 if (ar && ar->type != AR_ELEMENT)
6138 return true;
6140 if (ref->type == REF_ARRAY)
6141 ar = &ref->u.ar;
6144 if (ar == NULL || ar->type != AR_SECTION)
6145 return false;
6147 previous_incomplete = false;
6149 /* Check if we can prove that the array is not contiguous. */
6151 for (i = 0; i < ar->dimen; i++)
6153 mpz_t arr_size, ref_size;
6155 if (gfc_ref_dimen_size (ar, i, &ref_size, NULL))
6157 if (gfc_dep_difference (ar->as->upper[i], ar->as->lower[i], &arr_size))
6159 /* a(2:4,2:) is known to be non-contiguous, but
6160 a(2:4,i:i) can be contiguous. */
6161 mpz_add_ui (arr_size, arr_size, 1L);
6162 if (previous_incomplete && mpz_cmp_si (ref_size, 1) != 0)
6164 mpz_clear (arr_size);
6165 mpz_clear (ref_size);
6166 return true;
6168 else if (mpz_cmp (arr_size, ref_size) != 0)
6169 previous_incomplete = true;
6171 mpz_clear (arr_size);
6174 /* Check for a(::2), i.e. where the stride is not unity.
6175 This is only done if there is more than one element in
6176 the reference along this dimension. */
6178 if (mpz_cmp_ui (ref_size, 1) > 0 && ar->type == AR_SECTION
6179 && ar->dimen_type[i] == DIMEN_RANGE
6180 && ar->stride[i] && ar->stride[i]->expr_type == EXPR_CONSTANT
6181 && mpz_cmp_si (ar->stride[i]->value.integer, 1) != 0)
6183 mpz_clear (ref_size);
6184 return true;
6187 mpz_clear (ref_size);
6190 /* We didn't find anything definitive. */
6191 return false;
6194 /* Build call to an intrinsic procedure. The number of arguments has to be
6195 passed (rather than ending the list with a NULL value) because we may
6196 want to add arguments but with a NULL-expression. */
6198 gfc_expr*
6199 gfc_build_intrinsic_call (gfc_namespace *ns, gfc_isym_id id, const char* name,
6200 locus where, unsigned numarg, ...)
6202 gfc_expr* result;
6203 gfc_actual_arglist* atail;
6204 gfc_intrinsic_sym* isym;
6205 va_list ap;
6206 unsigned i;
6207 const char *mangled_name = gfc_get_string (GFC_PREFIX ("%s"), name);
6209 isym = gfc_intrinsic_function_by_id (id);
6210 gcc_assert (isym);
6212 result = gfc_get_expr ();
6213 result->expr_type = EXPR_FUNCTION;
6214 result->ts = isym->ts;
6215 result->where = where;
6216 result->value.function.name = mangled_name;
6217 result->value.function.isym = isym;
6219 gfc_get_sym_tree (mangled_name, ns, &result->symtree, false);
6220 gfc_commit_symbol (result->symtree->n.sym);
6221 gcc_assert (result->symtree
6222 && (result->symtree->n.sym->attr.flavor == FL_PROCEDURE
6223 || result->symtree->n.sym->attr.flavor == FL_UNKNOWN));
6224 result->symtree->n.sym->intmod_sym_id = id;
6225 result->symtree->n.sym->attr.flavor = FL_PROCEDURE;
6226 result->symtree->n.sym->attr.intrinsic = 1;
6227 result->symtree->n.sym->attr.artificial = 1;
6229 va_start (ap, numarg);
6230 atail = NULL;
6231 for (i = 0; i < numarg; ++i)
6233 if (atail)
6235 atail->next = gfc_get_actual_arglist ();
6236 atail = atail->next;
6238 else
6239 atail = result->value.function.actual = gfc_get_actual_arglist ();
6241 atail->expr = va_arg (ap, gfc_expr*);
6243 va_end (ap);
6245 return result;
6249 /* Check if an expression may appear in a variable definition context
6250 (F2008, 16.6.7) or pointer association context (F2008, 16.6.8).
6251 This is called from the various places when resolving
6252 the pieces that make up such a context.
6253 If own_scope is true (applies to, e.g., ac-implied-do/data-implied-do
6254 variables), some checks are not performed.
6256 Optionally, a possible error message can be suppressed if context is NULL
6257 and just the return status (true / false) be requested. */
6259 bool
6260 gfc_check_vardef_context (gfc_expr* e, bool pointer, bool alloc_obj,
6261 bool own_scope, const char* context)
6263 gfc_symbol* sym = NULL;
6264 bool is_pointer;
6265 bool check_intentin;
6266 bool ptr_component;
6267 symbol_attribute attr;
6268 gfc_ref* ref;
6269 int i;
6271 if (e->expr_type == EXPR_VARIABLE)
6273 gcc_assert (e->symtree);
6274 sym = e->symtree->n.sym;
6276 else if (e->expr_type == EXPR_FUNCTION)
6278 gcc_assert (e->symtree);
6279 sym = e->value.function.esym ? e->value.function.esym : e->symtree->n.sym;
6282 attr = gfc_expr_attr (e);
6283 if (!pointer && e->expr_type == EXPR_FUNCTION && attr.pointer)
6285 if (!(gfc_option.allow_std & GFC_STD_F2008))
6287 if (context)
6288 gfc_error ("Fortran 2008: Pointer functions in variable definition"
6289 " context (%s) at %L", context, &e->where);
6290 return false;
6293 else if (e->expr_type != EXPR_VARIABLE)
6295 if (context)
6296 gfc_error ("Non-variable expression in variable definition context (%s)"
6297 " at %L", context, &e->where);
6298 return false;
6301 if (!pointer && sym->attr.flavor == FL_PARAMETER)
6303 if (context)
6304 gfc_error ("Named constant %qs in variable definition context (%s)"
6305 " at %L", sym->name, context, &e->where);
6306 return false;
6308 if (!pointer && sym->attr.flavor != FL_VARIABLE
6309 && !(sym->attr.flavor == FL_PROCEDURE && sym == sym->result)
6310 && !(sym->attr.flavor == FL_PROCEDURE && sym->attr.proc_pointer)
6311 && !(sym->attr.flavor == FL_PROCEDURE
6312 && sym->attr.function && attr.pointer))
6314 if (context)
6315 gfc_error ("%qs in variable definition context (%s) at %L is not"
6316 " a variable", sym->name, context, &e->where);
6317 return false;
6320 /* Find out whether the expr is a pointer; this also means following
6321 component references to the last one. */
6322 is_pointer = (attr.pointer || attr.proc_pointer);
6323 if (pointer && !is_pointer)
6325 if (context)
6326 gfc_error ("Non-POINTER in pointer association context (%s)"
6327 " at %L", context, &e->where);
6328 return false;
6331 if (e->ts.type == BT_DERIVED
6332 && e->ts.u.derived == NULL)
6334 if (context)
6335 gfc_error ("Type inaccessible in variable definition context (%s) "
6336 "at %L", context, &e->where);
6337 return false;
6340 /* F2008, C1303. */
6341 if (!alloc_obj
6342 && (attr.lock_comp
6343 || (e->ts.type == BT_DERIVED
6344 && e->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
6345 && e->ts.u.derived->intmod_sym_id == ISOFORTRAN_LOCK_TYPE)))
6347 if (context)
6348 gfc_error ("LOCK_TYPE in variable definition context (%s) at %L",
6349 context, &e->where);
6350 return false;
6353 /* TS18508, C702/C203. */
6354 if (!alloc_obj
6355 && (attr.lock_comp
6356 || (e->ts.type == BT_DERIVED
6357 && e->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
6358 && e->ts.u.derived->intmod_sym_id == ISOFORTRAN_EVENT_TYPE)))
6360 if (context)
6361 gfc_error ("LOCK_EVENT in variable definition context (%s) at %L",
6362 context, &e->where);
6363 return false;
6366 /* INTENT(IN) dummy argument. Check this, unless the object itself is the
6367 component of sub-component of a pointer; we need to distinguish
6368 assignment to a pointer component from pointer-assignment to a pointer
6369 component. Note that (normal) assignment to procedure pointers is not
6370 possible. */
6371 check_intentin = !own_scope;
6372 ptr_component = (sym->ts.type == BT_CLASS && sym->ts.u.derived
6373 && CLASS_DATA (sym))
6374 ? CLASS_DATA (sym)->attr.class_pointer : sym->attr.pointer;
6375 for (ref = e->ref; ref && check_intentin; ref = ref->next)
6377 if (ptr_component && ref->type == REF_COMPONENT)
6378 check_intentin = false;
6379 if (ref->type == REF_COMPONENT)
6381 gfc_component *comp = ref->u.c.component;
6382 ptr_component = (comp->ts.type == BT_CLASS && comp->attr.class_ok)
6383 ? CLASS_DATA (comp)->attr.class_pointer
6384 : comp->attr.pointer;
6385 if (ptr_component && !pointer)
6386 check_intentin = false;
6388 if (ref->type == REF_INQUIRY
6389 && (ref->u.i == INQUIRY_KIND || ref->u.i == INQUIRY_LEN))
6391 if (context)
6392 gfc_error ("%qs parameter inquiry for %qs in "
6393 "variable definition context (%s) at %L",
6394 ref->u.i == INQUIRY_KIND ? "KIND" : "LEN",
6395 sym->name, context, &e->where);
6396 return false;
6400 if (check_intentin
6401 && (sym->attr.intent == INTENT_IN
6402 || (sym->attr.select_type_temporary && sym->assoc
6403 && sym->assoc->target && sym->assoc->target->symtree
6404 && sym->assoc->target->symtree->n.sym->attr.intent == INTENT_IN)))
6406 if (pointer && is_pointer)
6408 if (context)
6409 gfc_error ("Dummy argument %qs with INTENT(IN) in pointer"
6410 " association context (%s) at %L",
6411 sym->name, context, &e->where);
6412 return false;
6414 if (!pointer && !is_pointer && !sym->attr.pointer)
6416 const char *name = sym->attr.select_type_temporary
6417 ? sym->assoc->target->symtree->name : sym->name;
6418 if (context)
6419 gfc_error ("Dummy argument %qs with INTENT(IN) in variable"
6420 " definition context (%s) at %L",
6421 name, context, &e->where);
6422 return false;
6426 /* PROTECTED and use-associated. */
6427 if (sym->attr.is_protected && sym->attr.use_assoc && check_intentin)
6429 if (pointer && is_pointer)
6431 if (context)
6432 gfc_error ("Variable %qs is PROTECTED and cannot appear in a"
6433 " pointer association context (%s) at %L",
6434 sym->name, context, &e->where);
6435 return false;
6437 if (!pointer && !is_pointer)
6439 if (context)
6440 gfc_error ("Variable %qs is PROTECTED and cannot appear in a"
6441 " variable definition context (%s) at %L",
6442 sym->name, context, &e->where);
6443 return false;
6447 /* Variable not assignable from a PURE procedure but appears in
6448 variable definition context. */
6449 own_scope = own_scope
6450 || (sym->attr.result && sym->ns->proc_name
6451 && sym == sym->ns->proc_name->result);
6452 if (!pointer && !own_scope && gfc_pure (NULL) && gfc_impure_variable (sym))
6454 if (context)
6455 gfc_error ("Variable %qs cannot appear in a variable definition"
6456 " context (%s) at %L in PURE procedure",
6457 sym->name, context, &e->where);
6458 return false;
6461 if (!pointer && context && gfc_implicit_pure (NULL)
6462 && gfc_impure_variable (sym))
6464 gfc_namespace *ns;
6465 gfc_symbol *sym;
6467 for (ns = gfc_current_ns; ns; ns = ns->parent)
6469 sym = ns->proc_name;
6470 if (sym == NULL)
6471 break;
6472 if (sym->attr.flavor == FL_PROCEDURE)
6474 sym->attr.implicit_pure = 0;
6475 break;
6479 /* Check variable definition context for associate-names. */
6480 if (!pointer && sym->assoc && !sym->attr.select_rank_temporary)
6482 const char* name;
6483 gfc_association_list* assoc;
6485 gcc_assert (sym->assoc->target);
6487 /* If this is a SELECT TYPE temporary (the association is used internally
6488 for SELECT TYPE), silently go over to the target. */
6489 if (sym->attr.select_type_temporary)
6491 gfc_expr* t = sym->assoc->target;
6493 gcc_assert (t->expr_type == EXPR_VARIABLE);
6494 name = t->symtree->name;
6496 if (t->symtree->n.sym->assoc)
6497 assoc = t->symtree->n.sym->assoc;
6498 else
6499 assoc = sym->assoc;
6501 else
6503 name = sym->name;
6504 assoc = sym->assoc;
6506 gcc_assert (name && assoc);
6508 /* Is association to a valid variable? */
6509 if (!assoc->variable)
6511 if (context)
6513 if (assoc->target->expr_type == EXPR_VARIABLE
6514 && gfc_has_vector_index (assoc->target))
6515 gfc_error ("%qs at %L associated to vector-indexed target"
6516 " cannot be used in a variable definition"
6517 " context (%s)",
6518 name, &e->where, context);
6519 else
6520 gfc_error ("%qs at %L associated to expression"
6521 " cannot be used in a variable definition"
6522 " context (%s)",
6523 name, &e->where, context);
6525 return false;
6527 else if (context && gfc_is_ptr_fcn (assoc->target))
6529 if (!gfc_notify_std (GFC_STD_F2018, "%qs at %L associated to "
6530 "pointer function target being used in a "
6531 "variable definition context (%s)", name,
6532 &e->where, context))
6533 return false;
6534 else if (gfc_has_vector_index (e))
6536 gfc_error ("%qs at %L associated to vector-indexed target"
6537 " cannot be used in a variable definition"
6538 " context (%s)",
6539 name, &e->where, context);
6540 return false;
6544 /* Target must be allowed to appear in a variable definition context. */
6545 if (!gfc_check_vardef_context (assoc->target, pointer, false, false, NULL))
6547 if (context)
6548 gfc_error ("Associate-name %qs cannot appear in a variable"
6549 " definition context (%s) at %L because its target"
6550 " at %L cannot, either",
6551 name, context, &e->where,
6552 &assoc->target->where);
6553 return false;
6557 /* Check for same value in vector expression subscript. */
6559 if (e->rank > 0)
6560 for (ref = e->ref; ref != NULL; ref = ref->next)
6561 if (ref->type == REF_ARRAY && ref->u.ar.type == AR_SECTION)
6562 for (i = 0; i < GFC_MAX_DIMENSIONS
6563 && ref->u.ar.dimen_type[i] != 0; i++)
6564 if (ref->u.ar.dimen_type[i] == DIMEN_VECTOR)
6566 gfc_expr *arr = ref->u.ar.start[i];
6567 if (arr->expr_type == EXPR_ARRAY)
6569 gfc_constructor *c, *n;
6570 gfc_expr *ec, *en;
6572 for (c = gfc_constructor_first (arr->value.constructor);
6573 c != NULL; c = gfc_constructor_next (c))
6575 if (c == NULL || c->iterator != NULL)
6576 continue;
6578 ec = c->expr;
6580 for (n = gfc_constructor_next (c); n != NULL;
6581 n = gfc_constructor_next (n))
6583 if (n->iterator != NULL)
6584 continue;
6586 en = n->expr;
6587 if (gfc_dep_compare_expr (ec, en) == 0)
6589 if (context)
6590 gfc_error_now ("Elements with the same value "
6591 "at %L and %L in vector "
6592 "subscript in a variable "
6593 "definition context (%s)",
6594 &(ec->where), &(en->where),
6595 context);
6596 return false;
6603 return true;
6606 gfc_expr*
6607 gfc_pdt_find_component_copy_initializer (gfc_symbol *sym, const char *name)
6609 /* The actual length of a pdt is in its components. In the
6610 initializer of the current ref is only the default value.
6611 Therefore traverse the chain of components and pick the correct
6612 one's initializer expressions. */
6613 for (gfc_component *comp = sym->ts.u.derived->components; comp != NULL;
6614 comp = comp->next)
6616 if (!strcmp (comp->name, name))
6617 return gfc_copy_expr (comp->initializer);
6619 return NULL;