d: Merge upstream dmd 56589f0f4, druntime 651389b5, phobos 1516ecad9.
[official-gcc.git] / gcc / fortran / expr.cc
blobbe94c18c836611289ef3a102f7ffec1e4d64c468
1 /* Routines for manipulation of expression nodes.
2 Copyright (C) 2000-2022 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 mpz_init (e->value.integer);
163 break;
165 case BT_REAL:
166 gfc_set_model_kind (kind);
167 mpfr_init (e->value.real);
168 break;
170 case BT_COMPLEX:
171 gfc_set_model_kind (kind);
172 mpc_init2 (e->value.complex, mpfr_get_default_prec());
173 break;
175 default:
176 break;
179 return e;
183 /* Get a new expression node that is an string constant.
184 If no string is passed, a string of len is allocated,
185 blanked and null-terminated. */
187 gfc_expr *
188 gfc_get_character_expr (int kind, locus *where, const char *src, gfc_charlen_t len)
190 gfc_expr *e;
191 gfc_char_t *dest;
193 if (!src)
195 dest = gfc_get_wide_string (len + 1);
196 gfc_wide_memset (dest, ' ', len);
197 dest[len] = '\0';
199 else
200 dest = gfc_char_to_widechar (src);
202 e = gfc_get_constant_expr (BT_CHARACTER, kind,
203 where ? where : &gfc_current_locus);
204 e->value.character.string = dest;
205 e->value.character.length = len;
207 return e;
211 /* Get a new expression node that is an integer constant. */
213 gfc_expr *
214 gfc_get_int_expr (int kind, locus *where, HOST_WIDE_INT value)
216 gfc_expr *p;
217 p = gfc_get_constant_expr (BT_INTEGER, kind,
218 where ? where : &gfc_current_locus);
220 const wide_int w = wi::shwi (value, kind * BITS_PER_UNIT);
221 wi::to_mpz (w, p->value.integer, SIGNED);
223 return p;
227 /* Get a new expression node that is a logical constant. */
229 gfc_expr *
230 gfc_get_logical_expr (int kind, locus *where, bool value)
232 gfc_expr *p;
233 p = gfc_get_constant_expr (BT_LOGICAL, kind,
234 where ? where : &gfc_current_locus);
236 p->value.logical = value;
238 return p;
242 gfc_expr *
243 gfc_get_iokind_expr (locus *where, io_kind k)
245 gfc_expr *e;
247 /* Set the types to something compatible with iokind. This is needed to
248 get through gfc_free_expr later since iokind really has no Basic Type,
249 BT, of its own. */
251 e = gfc_get_expr ();
252 e->expr_type = EXPR_CONSTANT;
253 e->ts.type = BT_LOGICAL;
254 e->value.iokind = k;
255 e->where = *where;
257 return e;
261 /* Given an expression pointer, return a copy of the expression. This
262 subroutine is recursive. */
264 gfc_expr *
265 gfc_copy_expr (gfc_expr *p)
267 gfc_expr *q;
268 gfc_char_t *s;
269 char *c;
271 if (p == NULL)
272 return NULL;
274 q = gfc_get_expr ();
275 *q = *p;
277 switch (q->expr_type)
279 case EXPR_SUBSTRING:
280 s = gfc_get_wide_string (p->value.character.length + 1);
281 q->value.character.string = s;
282 memcpy (s, p->value.character.string,
283 (p->value.character.length + 1) * sizeof (gfc_char_t));
284 break;
286 case EXPR_CONSTANT:
287 /* Copy target representation, if it exists. */
288 if (p->representation.string)
290 c = XCNEWVEC (char, p->representation.length + 1);
291 q->representation.string = c;
292 memcpy (c, p->representation.string, (p->representation.length + 1));
295 /* Copy the values of any pointer components of p->value. */
296 switch (q->ts.type)
298 case BT_INTEGER:
299 mpz_init_set (q->value.integer, p->value.integer);
300 break;
302 case BT_REAL:
303 gfc_set_model_kind (q->ts.kind);
304 mpfr_init (q->value.real);
305 mpfr_set (q->value.real, p->value.real, GFC_RND_MODE);
306 break;
308 case BT_COMPLEX:
309 gfc_set_model_kind (q->ts.kind);
310 mpc_init2 (q->value.complex, mpfr_get_default_prec());
311 mpc_set (q->value.complex, p->value.complex, GFC_MPC_RND_MODE);
312 break;
314 case BT_CHARACTER:
315 if (p->representation.string
316 && p->ts.kind == gfc_default_character_kind)
317 q->value.character.string
318 = gfc_char_to_widechar (q->representation.string);
319 else
321 s = gfc_get_wide_string (p->value.character.length + 1);
322 q->value.character.string = s;
324 /* This is the case for the C_NULL_CHAR named constant. */
325 if (p->value.character.length == 0
326 && (p->ts.is_c_interop || p->ts.is_iso_c))
328 *s = '\0';
329 /* Need to set the length to 1 to make sure the NUL
330 terminator is copied. */
331 q->value.character.length = 1;
333 else
334 memcpy (s, p->value.character.string,
335 (p->value.character.length + 1) * sizeof (gfc_char_t));
337 break;
339 case BT_HOLLERITH:
340 case BT_LOGICAL:
341 case_bt_struct:
342 case BT_CLASS:
343 case BT_ASSUMED:
344 break; /* Already done. */
346 case BT_BOZ:
347 q->boz.len = p->boz.len;
348 q->boz.rdx = p->boz.rdx;
349 q->boz.str = XCNEWVEC (char, q->boz.len + 1);
350 strncpy (q->boz.str, p->boz.str, p->boz.len);
351 break;
353 case BT_PROCEDURE:
354 case BT_VOID:
355 /* Should never be reached. */
356 case BT_UNKNOWN:
357 gfc_internal_error ("gfc_copy_expr(): Bad expr node");
358 /* Not reached. */
361 break;
363 case EXPR_OP:
364 switch (q->value.op.op)
366 case INTRINSIC_NOT:
367 case INTRINSIC_PARENTHESES:
368 case INTRINSIC_UPLUS:
369 case INTRINSIC_UMINUS:
370 q->value.op.op1 = gfc_copy_expr (p->value.op.op1);
371 break;
373 default: /* Binary operators. */
374 q->value.op.op1 = gfc_copy_expr (p->value.op.op1);
375 q->value.op.op2 = gfc_copy_expr (p->value.op.op2);
376 break;
379 break;
381 case EXPR_FUNCTION:
382 q->value.function.actual =
383 gfc_copy_actual_arglist (p->value.function.actual);
384 break;
386 case EXPR_COMPCALL:
387 case EXPR_PPC:
388 q->value.compcall.actual =
389 gfc_copy_actual_arglist (p->value.compcall.actual);
390 q->value.compcall.tbp = p->value.compcall.tbp;
391 break;
393 case EXPR_STRUCTURE:
394 case EXPR_ARRAY:
395 q->value.constructor = gfc_constructor_copy (p->value.constructor);
396 break;
398 case EXPR_VARIABLE:
399 case EXPR_NULL:
400 break;
402 case EXPR_UNKNOWN:
403 gcc_unreachable ();
406 q->shape = gfc_copy_shape (p->shape, p->rank);
408 q->ref = gfc_copy_ref (p->ref);
410 if (p->param_list)
411 q->param_list = gfc_copy_actual_arglist (p->param_list);
413 return q;
417 void
418 gfc_clear_shape (mpz_t *shape, int rank)
420 int i;
422 for (i = 0; i < rank; i++)
423 mpz_clear (shape[i]);
427 void
428 gfc_free_shape (mpz_t **shape, int rank)
430 if (*shape == NULL)
431 return;
433 gfc_clear_shape (*shape, rank);
434 free (*shape);
435 *shape = NULL;
439 /* Workhorse function for gfc_free_expr() that frees everything
440 beneath an expression node, but not the node itself. This is
441 useful when we want to simplify a node and replace it with
442 something else or the expression node belongs to another structure. */
444 static void
445 free_expr0 (gfc_expr *e)
447 switch (e->expr_type)
449 case EXPR_CONSTANT:
450 /* Free any parts of the value that need freeing. */
451 switch (e->ts.type)
453 case BT_INTEGER:
454 mpz_clear (e->value.integer);
455 break;
457 case BT_REAL:
458 mpfr_clear (e->value.real);
459 break;
461 case BT_CHARACTER:
462 free (e->value.character.string);
463 break;
465 case BT_COMPLEX:
466 mpc_clear (e->value.complex);
467 break;
469 default:
470 break;
473 /* Free the representation. */
474 free (e->representation.string);
476 break;
478 case EXPR_OP:
479 if (e->value.op.op1 != NULL)
480 gfc_free_expr (e->value.op.op1);
481 if (e->value.op.op2 != NULL)
482 gfc_free_expr (e->value.op.op2);
483 break;
485 case EXPR_FUNCTION:
486 gfc_free_actual_arglist (e->value.function.actual);
487 break;
489 case EXPR_COMPCALL:
490 case EXPR_PPC:
491 gfc_free_actual_arglist (e->value.compcall.actual);
492 break;
494 case EXPR_VARIABLE:
495 break;
497 case EXPR_ARRAY:
498 case EXPR_STRUCTURE:
499 gfc_constructor_free (e->value.constructor);
500 break;
502 case EXPR_SUBSTRING:
503 free (e->value.character.string);
504 break;
506 case EXPR_NULL:
507 break;
509 default:
510 gfc_internal_error ("free_expr0(): Bad expr type");
513 /* Free a shape array. */
514 gfc_free_shape (&e->shape, e->rank);
516 gfc_free_ref_list (e->ref);
518 gfc_free_actual_arglist (e->param_list);
520 memset (e, '\0', sizeof (gfc_expr));
524 /* Free an expression node and everything beneath it. */
526 void
527 gfc_free_expr (gfc_expr *e)
529 if (e == NULL)
530 return;
531 free_expr0 (e);
532 free (e);
536 /* Free an argument list and everything below it. */
538 void
539 gfc_free_actual_arglist (gfc_actual_arglist *a1)
541 gfc_actual_arglist *a2;
543 while (a1)
545 a2 = a1->next;
546 if (a1->expr)
547 gfc_free_expr (a1->expr);
548 free (a1);
549 a1 = a2;
554 /* Copy an arglist structure and all of the arguments. */
556 gfc_actual_arglist *
557 gfc_copy_actual_arglist (gfc_actual_arglist *p)
559 gfc_actual_arglist *head, *tail, *new_arg;
561 head = tail = NULL;
563 for (; p; p = p->next)
565 new_arg = gfc_get_actual_arglist ();
566 *new_arg = *p;
568 new_arg->expr = gfc_copy_expr (p->expr);
569 new_arg->next = NULL;
571 if (head == NULL)
572 head = new_arg;
573 else
574 tail->next = new_arg;
576 tail = new_arg;
579 return head;
583 /* Free a list of reference structures. */
585 void
586 gfc_free_ref_list (gfc_ref *p)
588 gfc_ref *q;
589 int i;
591 for (; p; p = q)
593 q = p->next;
595 switch (p->type)
597 case REF_ARRAY:
598 for (i = 0; i < GFC_MAX_DIMENSIONS; i++)
600 gfc_free_expr (p->u.ar.start[i]);
601 gfc_free_expr (p->u.ar.end[i]);
602 gfc_free_expr (p->u.ar.stride[i]);
605 break;
607 case REF_SUBSTRING:
608 gfc_free_expr (p->u.ss.start);
609 gfc_free_expr (p->u.ss.end);
610 break;
612 case REF_COMPONENT:
613 case REF_INQUIRY:
614 break;
617 free (p);
622 /* Graft the *src expression onto the *dest subexpression. */
624 void
625 gfc_replace_expr (gfc_expr *dest, gfc_expr *src)
627 free_expr0 (dest);
628 *dest = *src;
629 free (src);
633 /* Try to extract an integer constant from the passed expression node.
634 Return true if some error occurred, false on success. If REPORT_ERROR
635 is non-zero, emit error, for positive REPORT_ERROR using gfc_error,
636 for negative using gfc_error_now. */
638 bool
639 gfc_extract_int (gfc_expr *expr, int *result, int report_error)
641 gfc_ref *ref;
643 /* A KIND component is a parameter too. The expression for it
644 is stored in the initializer and should be consistent with
645 the tests below. */
646 if (gfc_expr_attr(expr).pdt_kind)
648 for (ref = expr->ref; ref; ref = ref->next)
650 if (ref->u.c.component->attr.pdt_kind)
651 expr = ref->u.c.component->initializer;
655 if (expr->expr_type != EXPR_CONSTANT)
657 if (report_error > 0)
658 gfc_error ("Constant expression required at %C");
659 else if (report_error < 0)
660 gfc_error_now ("Constant expression required at %C");
661 return true;
664 if (expr->ts.type != BT_INTEGER)
666 if (report_error > 0)
667 gfc_error ("Integer expression required at %C");
668 else if (report_error < 0)
669 gfc_error_now ("Integer expression required at %C");
670 return true;
673 if ((mpz_cmp_si (expr->value.integer, INT_MAX) > 0)
674 || (mpz_cmp_si (expr->value.integer, INT_MIN) < 0))
676 if (report_error > 0)
677 gfc_error ("Integer value too large in expression at %C");
678 else if (report_error < 0)
679 gfc_error_now ("Integer value too large in expression at %C");
680 return true;
683 *result = (int) mpz_get_si (expr->value.integer);
685 return false;
689 /* Same as gfc_extract_int, but use a HWI. */
691 bool
692 gfc_extract_hwi (gfc_expr *expr, HOST_WIDE_INT *result, int report_error)
694 gfc_ref *ref;
696 /* A KIND component is a parameter too. The expression for it is
697 stored in the initializer and should be consistent with the tests
698 below. */
699 if (gfc_expr_attr(expr).pdt_kind)
701 for (ref = expr->ref; ref; ref = ref->next)
703 if (ref->u.c.component->attr.pdt_kind)
704 expr = ref->u.c.component->initializer;
708 if (expr->expr_type != EXPR_CONSTANT)
710 if (report_error > 0)
711 gfc_error ("Constant expression required at %C");
712 else if (report_error < 0)
713 gfc_error_now ("Constant expression required at %C");
714 return true;
717 if (expr->ts.type != BT_INTEGER)
719 if (report_error > 0)
720 gfc_error ("Integer expression required at %C");
721 else if (report_error < 0)
722 gfc_error_now ("Integer expression required at %C");
723 return true;
726 /* Use long_long_integer_type_node to determine when to saturate. */
727 const wide_int val = wi::from_mpz (long_long_integer_type_node,
728 expr->value.integer, false);
730 if (!wi::fits_shwi_p (val))
732 if (report_error > 0)
733 gfc_error ("Integer value too large in expression at %C");
734 else if (report_error < 0)
735 gfc_error_now ("Integer value too large in expression at %C");
736 return true;
739 *result = val.to_shwi ();
741 return false;
745 /* Recursively copy a list of reference structures. */
747 gfc_ref *
748 gfc_copy_ref (gfc_ref *src)
750 gfc_array_ref *ar;
751 gfc_ref *dest;
753 if (src == NULL)
754 return NULL;
756 dest = gfc_get_ref ();
757 dest->type = src->type;
759 switch (src->type)
761 case REF_ARRAY:
762 ar = gfc_copy_array_ref (&src->u.ar);
763 dest->u.ar = *ar;
764 free (ar);
765 break;
767 case REF_COMPONENT:
768 dest->u.c = src->u.c;
769 break;
771 case REF_INQUIRY:
772 dest->u.i = src->u.i;
773 break;
775 case REF_SUBSTRING:
776 dest->u.ss = src->u.ss;
777 dest->u.ss.start = gfc_copy_expr (src->u.ss.start);
778 dest->u.ss.end = gfc_copy_expr (src->u.ss.end);
779 break;
782 dest->next = gfc_copy_ref (src->next);
784 return dest;
788 /* Detect whether an expression has any vector index array references. */
791 gfc_has_vector_index (gfc_expr *e)
793 gfc_ref *ref;
794 int i;
795 for (ref = e->ref; ref; ref = ref->next)
796 if (ref->type == REF_ARRAY)
797 for (i = 0; i < ref->u.ar.dimen; i++)
798 if (ref->u.ar.dimen_type[i] == DIMEN_VECTOR)
799 return 1;
800 return 0;
804 /* Copy a shape array. */
806 mpz_t *
807 gfc_copy_shape (mpz_t *shape, int rank)
809 mpz_t *new_shape;
810 int n;
812 if (shape == NULL)
813 return NULL;
815 new_shape = gfc_get_shape (rank);
817 for (n = 0; n < rank; n++)
818 mpz_init_set (new_shape[n], shape[n]);
820 return new_shape;
824 /* Copy a shape array excluding dimension N, where N is an integer
825 constant expression. Dimensions are numbered in Fortran style --
826 starting with ONE.
828 So, if the original shape array contains R elements
829 { s1 ... sN-1 sN sN+1 ... sR-1 sR}
830 the result contains R-1 elements:
831 { s1 ... sN-1 sN+1 ... sR-1}
833 If anything goes wrong -- N is not a constant, its value is out
834 of range -- or anything else, just returns NULL. */
836 mpz_t *
837 gfc_copy_shape_excluding (mpz_t *shape, int rank, gfc_expr *dim)
839 mpz_t *new_shape, *s;
840 int i, n;
842 if (shape == NULL
843 || rank <= 1
844 || dim == NULL
845 || dim->expr_type != EXPR_CONSTANT
846 || dim->ts.type != BT_INTEGER)
847 return NULL;
849 n = mpz_get_si (dim->value.integer);
850 n--; /* Convert to zero based index. */
851 if (n < 0 || n >= rank)
852 return NULL;
854 s = new_shape = gfc_get_shape (rank - 1);
856 for (i = 0; i < rank; i++)
858 if (i == n)
859 continue;
860 mpz_init_set (*s, shape[i]);
861 s++;
864 return new_shape;
868 /* Return the maximum kind of two expressions. In general, higher
869 kind numbers mean more precision for numeric types. */
872 gfc_kind_max (gfc_expr *e1, gfc_expr *e2)
874 return (e1->ts.kind > e2->ts.kind) ? e1->ts.kind : e2->ts.kind;
878 /* Returns nonzero if the type is numeric, zero otherwise. */
880 static int
881 numeric_type (bt type)
883 return type == BT_COMPLEX || type == BT_REAL || type == BT_INTEGER;
887 /* Returns nonzero if the typespec is a numeric type, zero otherwise. */
890 gfc_numeric_ts (gfc_typespec *ts)
892 return numeric_type (ts->type);
896 /* Return an expression node with an optional argument list attached.
897 A variable number of gfc_expr pointers are strung together in an
898 argument list with a NULL pointer terminating the list. */
900 gfc_expr *
901 gfc_build_conversion (gfc_expr *e)
903 gfc_expr *p;
905 p = gfc_get_expr ();
906 p->expr_type = EXPR_FUNCTION;
907 p->symtree = NULL;
908 p->value.function.actual = gfc_get_actual_arglist ();
909 p->value.function.actual->expr = e;
911 return p;
915 /* Given an expression node with some sort of numeric binary
916 expression, insert type conversions required to make the operands
917 have the same type. Conversion warnings are disabled if wconversion
918 is set to 0.
920 The exception is that the operands of an exponential don't have to
921 have the same type. If possible, the base is promoted to the type
922 of the exponent. For example, 1**2.3 becomes 1.0**2.3, but
923 1.0**2 stays as it is. */
925 void
926 gfc_type_convert_binary (gfc_expr *e, int wconversion)
928 gfc_expr *op1, *op2;
930 op1 = e->value.op.op1;
931 op2 = e->value.op.op2;
933 if (op1->ts.type == BT_UNKNOWN || op2->ts.type == BT_UNKNOWN)
935 gfc_clear_ts (&e->ts);
936 return;
939 /* Kind conversions of same type. */
940 if (op1->ts.type == op2->ts.type)
942 if (op1->ts.kind == op2->ts.kind)
944 /* No type conversions. */
945 e->ts = op1->ts;
946 goto done;
949 if (op1->ts.kind > op2->ts.kind)
950 gfc_convert_type_warn (op2, &op1->ts, 2, wconversion);
951 else
952 gfc_convert_type_warn (op1, &op2->ts, 2, wconversion);
954 e->ts = op1->ts;
955 goto done;
958 /* Integer combined with real or complex. */
959 if (op2->ts.type == BT_INTEGER)
961 e->ts = op1->ts;
963 /* Special case for ** operator. */
964 if (e->value.op.op == INTRINSIC_POWER)
965 goto done;
967 gfc_convert_type_warn (e->value.op.op2, &e->ts, 2, wconversion);
968 goto done;
971 if (op1->ts.type == BT_INTEGER)
973 e->ts = op2->ts;
974 gfc_convert_type_warn (e->value.op.op1, &e->ts, 2, wconversion);
975 goto done;
978 /* Real combined with complex. */
979 e->ts.type = BT_COMPLEX;
980 if (op1->ts.kind > op2->ts.kind)
981 e->ts.kind = op1->ts.kind;
982 else
983 e->ts.kind = op2->ts.kind;
984 if (op1->ts.type != BT_COMPLEX || op1->ts.kind != e->ts.kind)
985 gfc_convert_type_warn (e->value.op.op1, &e->ts, 2, wconversion);
986 if (op2->ts.type != BT_COMPLEX || op2->ts.kind != e->ts.kind)
987 gfc_convert_type_warn (e->value.op.op2, &e->ts, 2, wconversion);
989 done:
990 return;
994 /* Standard intrinsics listed under F2018:10.1.12 (6), which are excluded in
995 constant expressions, except TRANSFER (c.f. item (8)), which would need
996 separate treatment. */
998 static bool
999 is_non_constant_intrinsic (gfc_expr *e)
1001 if (e->expr_type == EXPR_FUNCTION
1002 && e->value.function.isym)
1004 switch (e->value.function.isym->id)
1006 case GFC_ISYM_COMMAND_ARGUMENT_COUNT:
1007 case GFC_ISYM_GET_TEAM:
1008 case GFC_ISYM_NULL:
1009 case GFC_ISYM_NUM_IMAGES:
1010 case GFC_ISYM_TEAM_NUMBER:
1011 case GFC_ISYM_THIS_IMAGE:
1012 return true;
1014 default:
1015 return false;
1018 return false;
1022 /* Determine if an expression is constant in the sense of F08:7.1.12.
1023 * This function expects that the expression has already been simplified. */
1025 bool
1026 gfc_is_constant_expr (gfc_expr *e)
1028 gfc_constructor *c;
1029 gfc_actual_arglist *arg;
1031 if (e == NULL)
1032 return true;
1034 switch (e->expr_type)
1036 case EXPR_OP:
1037 return (gfc_is_constant_expr (e->value.op.op1)
1038 && (e->value.op.op2 == NULL
1039 || gfc_is_constant_expr (e->value.op.op2)));
1041 case EXPR_VARIABLE:
1042 /* The only context in which this can occur is in a parameterized
1043 derived type declaration, so returning true is OK. */
1044 if (e->symtree->n.sym->attr.pdt_len
1045 || e->symtree->n.sym->attr.pdt_kind)
1046 return true;
1047 return false;
1049 case EXPR_FUNCTION:
1050 case EXPR_PPC:
1051 case EXPR_COMPCALL:
1052 gcc_assert (e->symtree || e->value.function.esym
1053 || e->value.function.isym);
1055 /* Check for intrinsics excluded in constant expressions. */
1056 if (e->value.function.isym && is_non_constant_intrinsic (e))
1057 return false;
1059 /* Call to intrinsic with at least one argument. */
1060 if (e->value.function.isym && e->value.function.actual)
1062 for (arg = e->value.function.actual; arg; arg = arg->next)
1063 if (!gfc_is_constant_expr (arg->expr))
1064 return false;
1067 if (e->value.function.isym
1068 && (e->value.function.isym->elemental
1069 || e->value.function.isym->pure
1070 || e->value.function.isym->inquiry
1071 || e->value.function.isym->transformational))
1072 return true;
1074 return false;
1076 case EXPR_CONSTANT:
1077 case EXPR_NULL:
1078 return true;
1080 case EXPR_SUBSTRING:
1081 return e->ref == NULL || (gfc_is_constant_expr (e->ref->u.ss.start)
1082 && gfc_is_constant_expr (e->ref->u.ss.end));
1084 case EXPR_ARRAY:
1085 case EXPR_STRUCTURE:
1086 c = gfc_constructor_first (e->value.constructor);
1087 if ((e->expr_type == EXPR_ARRAY) && c && c->iterator)
1088 return gfc_constant_ac (e);
1090 for (; c; c = gfc_constructor_next (c))
1091 if (!gfc_is_constant_expr (c->expr))
1092 return false;
1094 return true;
1097 default:
1098 gfc_internal_error ("gfc_is_constant_expr(): Unknown expression type");
1099 return false;
1104 /* Is true if the expression or symbol is a passed CFI descriptor. */
1105 bool
1106 is_CFI_desc (gfc_symbol *sym, gfc_expr *e)
1108 if (sym == NULL
1109 && e && e->expr_type == EXPR_VARIABLE)
1110 sym = e->symtree->n.sym;
1112 if (sym && sym->attr.dummy
1113 && sym->ns->proc_name->attr.is_bind_c
1114 && (sym->attr.pointer
1115 || sym->attr.allocatable
1116 || (sym->attr.dimension
1117 && (sym->as->type == AS_ASSUMED_SHAPE
1118 || sym->as->type == AS_ASSUMED_RANK))
1119 || (sym->ts.type == BT_CHARACTER
1120 && (!sym->ts.u.cl || !sym->ts.u.cl->length))))
1121 return true;
1123 return false;
1127 /* Is true if an array reference is followed by a component or substring
1128 reference. */
1129 bool
1130 is_subref_array (gfc_expr * e)
1132 gfc_ref * ref;
1133 bool seen_array;
1134 gfc_symbol *sym;
1136 if (e->expr_type != EXPR_VARIABLE)
1137 return false;
1139 sym = e->symtree->n.sym;
1141 if (sym->attr.subref_array_pointer)
1142 return true;
1144 seen_array = false;
1146 for (ref = e->ref; ref; ref = ref->next)
1148 /* If we haven't seen the array reference and this is an intrinsic,
1149 what follows cannot be a subreference array, unless there is a
1150 substring reference. */
1151 if (!seen_array && ref->type == REF_COMPONENT
1152 && ref->u.c.component->ts.type != BT_CHARACTER
1153 && ref->u.c.component->ts.type != BT_CLASS
1154 && !gfc_bt_struct (ref->u.c.component->ts.type))
1155 return false;
1157 if (ref->type == REF_ARRAY
1158 && ref->u.ar.type != AR_ELEMENT)
1159 seen_array = true;
1161 if (seen_array
1162 && ref->type != REF_ARRAY)
1163 return seen_array;
1166 if (sym->ts.type == BT_CLASS
1167 && sym->attr.dummy
1168 && CLASS_DATA (sym)->attr.dimension
1169 && CLASS_DATA (sym)->attr.class_pointer)
1170 return true;
1172 return false;
1176 /* Try to collapse intrinsic expressions. */
1178 static bool
1179 simplify_intrinsic_op (gfc_expr *p, int type)
1181 gfc_intrinsic_op op;
1182 gfc_expr *op1, *op2, *result;
1184 if (p->value.op.op == INTRINSIC_USER)
1185 return true;
1187 op1 = p->value.op.op1;
1188 op2 = p->value.op.op2;
1189 op = p->value.op.op;
1191 if (!gfc_simplify_expr (op1, type))
1192 return false;
1193 if (!gfc_simplify_expr (op2, type))
1194 return false;
1196 if (!gfc_is_constant_expr (op1)
1197 || (op2 != NULL && !gfc_is_constant_expr (op2)))
1198 return true;
1200 /* Rip p apart. */
1201 p->value.op.op1 = NULL;
1202 p->value.op.op2 = NULL;
1204 switch (op)
1206 case INTRINSIC_PARENTHESES:
1207 result = gfc_parentheses (op1);
1208 break;
1210 case INTRINSIC_UPLUS:
1211 result = gfc_uplus (op1);
1212 break;
1214 case INTRINSIC_UMINUS:
1215 result = gfc_uminus (op1);
1216 break;
1218 case INTRINSIC_PLUS:
1219 result = gfc_add (op1, op2);
1220 break;
1222 case INTRINSIC_MINUS:
1223 result = gfc_subtract (op1, op2);
1224 break;
1226 case INTRINSIC_TIMES:
1227 result = gfc_multiply (op1, op2);
1228 break;
1230 case INTRINSIC_DIVIDE:
1231 result = gfc_divide (op1, op2);
1232 break;
1234 case INTRINSIC_POWER:
1235 result = gfc_power (op1, op2);
1236 break;
1238 case INTRINSIC_CONCAT:
1239 result = gfc_concat (op1, op2);
1240 break;
1242 case INTRINSIC_EQ:
1243 case INTRINSIC_EQ_OS:
1244 result = gfc_eq (op1, op2, op);
1245 break;
1247 case INTRINSIC_NE:
1248 case INTRINSIC_NE_OS:
1249 result = gfc_ne (op1, op2, op);
1250 break;
1252 case INTRINSIC_GT:
1253 case INTRINSIC_GT_OS:
1254 result = gfc_gt (op1, op2, op);
1255 break;
1257 case INTRINSIC_GE:
1258 case INTRINSIC_GE_OS:
1259 result = gfc_ge (op1, op2, op);
1260 break;
1262 case INTRINSIC_LT:
1263 case INTRINSIC_LT_OS:
1264 result = gfc_lt (op1, op2, op);
1265 break;
1267 case INTRINSIC_LE:
1268 case INTRINSIC_LE_OS:
1269 result = gfc_le (op1, op2, op);
1270 break;
1272 case INTRINSIC_NOT:
1273 result = gfc_not (op1);
1274 break;
1276 case INTRINSIC_AND:
1277 result = gfc_and (op1, op2);
1278 break;
1280 case INTRINSIC_OR:
1281 result = gfc_or (op1, op2);
1282 break;
1284 case INTRINSIC_EQV:
1285 result = gfc_eqv (op1, op2);
1286 break;
1288 case INTRINSIC_NEQV:
1289 result = gfc_neqv (op1, op2);
1290 break;
1292 default:
1293 gfc_internal_error ("simplify_intrinsic_op(): Bad operator");
1296 if (result == NULL)
1298 gfc_free_expr (op1);
1299 gfc_free_expr (op2);
1300 return false;
1303 result->rank = p->rank;
1304 result->where = p->where;
1305 gfc_replace_expr (p, result);
1307 return true;
1311 /* Subroutine to simplify constructor expressions. Mutually recursive
1312 with gfc_simplify_expr(). */
1314 static bool
1315 simplify_constructor (gfc_constructor_base base, int type)
1317 gfc_constructor *c;
1318 gfc_expr *p;
1320 for (c = gfc_constructor_first (base); c; c = gfc_constructor_next (c))
1322 if (c->iterator
1323 && (!gfc_simplify_expr(c->iterator->start, type)
1324 || !gfc_simplify_expr (c->iterator->end, type)
1325 || !gfc_simplify_expr (c->iterator->step, type)))
1326 return false;
1328 if (c->expr)
1330 /* Try and simplify a copy. Replace the original if successful
1331 but keep going through the constructor at all costs. Not
1332 doing so can make a dog's dinner of complicated things. */
1333 p = gfc_copy_expr (c->expr);
1335 if (!gfc_simplify_expr (p, type))
1337 gfc_free_expr (p);
1338 continue;
1341 gfc_replace_expr (c->expr, p);
1345 return true;
1349 /* Pull a single array element out of an array constructor. */
1351 static bool
1352 find_array_element (gfc_constructor_base base, gfc_array_ref *ar,
1353 gfc_constructor **rval)
1355 unsigned long nelemen;
1356 int i;
1357 mpz_t delta;
1358 mpz_t offset;
1359 mpz_t span;
1360 mpz_t tmp;
1361 gfc_constructor *cons;
1362 gfc_expr *e;
1363 bool t;
1365 t = true;
1366 e = NULL;
1368 mpz_init_set_ui (offset, 0);
1369 mpz_init (delta);
1370 mpz_init (tmp);
1371 mpz_init_set_ui (span, 1);
1372 for (i = 0; i < ar->dimen; i++)
1374 if (!gfc_reduce_init_expr (ar->as->lower[i])
1375 || !gfc_reduce_init_expr (ar->as->upper[i])
1376 || ar->as->upper[i]->expr_type != EXPR_CONSTANT
1377 || ar->as->lower[i]->expr_type != EXPR_CONSTANT)
1379 t = false;
1380 cons = NULL;
1381 goto depart;
1384 e = ar->start[i];
1385 if (e->expr_type != EXPR_CONSTANT)
1387 cons = NULL;
1388 goto depart;
1391 /* Check the bounds. */
1392 if ((ar->as->upper[i]
1393 && mpz_cmp (e->value.integer,
1394 ar->as->upper[i]->value.integer) > 0)
1395 || (mpz_cmp (e->value.integer,
1396 ar->as->lower[i]->value.integer) < 0))
1398 gfc_error ("Index in dimension %d is out of bounds "
1399 "at %L", i + 1, &ar->c_where[i]);
1400 cons = NULL;
1401 t = false;
1402 goto depart;
1405 mpz_sub (delta, e->value.integer, ar->as->lower[i]->value.integer);
1406 mpz_mul (delta, delta, span);
1407 mpz_add (offset, offset, delta);
1409 mpz_set_ui (tmp, 1);
1410 mpz_add (tmp, tmp, ar->as->upper[i]->value.integer);
1411 mpz_sub (tmp, tmp, ar->as->lower[i]->value.integer);
1412 mpz_mul (span, span, tmp);
1415 for (cons = gfc_constructor_first (base), nelemen = mpz_get_ui (offset);
1416 cons && nelemen > 0; cons = gfc_constructor_next (cons), nelemen--)
1418 if (cons->iterator)
1420 cons = NULL;
1421 goto depart;
1425 depart:
1426 mpz_clear (delta);
1427 mpz_clear (offset);
1428 mpz_clear (span);
1429 mpz_clear (tmp);
1430 *rval = cons;
1431 return t;
1435 /* Find a component of a structure constructor. */
1437 static gfc_constructor *
1438 find_component_ref (gfc_constructor_base base, gfc_ref *ref)
1440 gfc_component *pick = ref->u.c.component;
1441 gfc_constructor *c = gfc_constructor_first (base);
1443 gfc_symbol *dt = ref->u.c.sym;
1444 int ext = dt->attr.extension;
1446 /* For extended types, check if the desired component is in one of the
1447 * parent types. */
1448 while (ext > 0 && gfc_find_component (dt->components->ts.u.derived,
1449 pick->name, true, true, NULL))
1451 dt = dt->components->ts.u.derived;
1452 c = gfc_constructor_first (c->expr->value.constructor);
1453 ext--;
1456 gfc_component *comp = dt->components;
1457 while (comp != pick)
1459 comp = comp->next;
1460 c = gfc_constructor_next (c);
1463 return c;
1467 /* Replace an expression with the contents of a constructor, removing
1468 the subobject reference in the process. */
1470 static void
1471 remove_subobject_ref (gfc_expr *p, gfc_constructor *cons)
1473 gfc_expr *e;
1475 if (cons)
1477 e = cons->expr;
1478 cons->expr = NULL;
1480 else
1481 e = gfc_copy_expr (p);
1482 e->ref = p->ref->next;
1483 p->ref->next = NULL;
1484 gfc_replace_expr (p, e);
1488 /* Pull an array section out of an array constructor. */
1490 static bool
1491 find_array_section (gfc_expr *expr, gfc_ref *ref)
1493 int idx;
1494 int rank;
1495 int d;
1496 int shape_i;
1497 int limit;
1498 long unsigned one = 1;
1499 bool incr_ctr;
1500 mpz_t start[GFC_MAX_DIMENSIONS];
1501 mpz_t end[GFC_MAX_DIMENSIONS];
1502 mpz_t stride[GFC_MAX_DIMENSIONS];
1503 mpz_t delta[GFC_MAX_DIMENSIONS];
1504 mpz_t ctr[GFC_MAX_DIMENSIONS];
1505 mpz_t delta_mpz;
1506 mpz_t tmp_mpz;
1507 mpz_t nelts;
1508 mpz_t ptr;
1509 gfc_constructor_base base;
1510 gfc_constructor *cons, *vecsub[GFC_MAX_DIMENSIONS];
1511 gfc_expr *begin;
1512 gfc_expr *finish;
1513 gfc_expr *step;
1514 gfc_expr *upper;
1515 gfc_expr *lower;
1516 bool t;
1518 t = true;
1520 base = expr->value.constructor;
1521 expr->value.constructor = NULL;
1523 rank = ref->u.ar.as->rank;
1525 if (expr->shape == NULL)
1526 expr->shape = gfc_get_shape (rank);
1528 mpz_init_set_ui (delta_mpz, one);
1529 mpz_init_set_ui (nelts, one);
1530 mpz_init (tmp_mpz);
1532 /* Do the initialization now, so that we can cleanup without
1533 keeping track of where we were. */
1534 for (d = 0; d < rank; d++)
1536 mpz_init (delta[d]);
1537 mpz_init (start[d]);
1538 mpz_init (end[d]);
1539 mpz_init (ctr[d]);
1540 mpz_init (stride[d]);
1541 vecsub[d] = NULL;
1544 /* Build the counters to clock through the array reference. */
1545 shape_i = 0;
1546 for (d = 0; d < rank; d++)
1548 /* Make this stretch of code easier on the eye! */
1549 begin = ref->u.ar.start[d];
1550 finish = ref->u.ar.end[d];
1551 step = ref->u.ar.stride[d];
1552 lower = ref->u.ar.as->lower[d];
1553 upper = ref->u.ar.as->upper[d];
1555 if (ref->u.ar.dimen_type[d] == DIMEN_VECTOR) /* Vector subscript. */
1557 gfc_constructor *ci;
1558 gcc_assert (begin);
1560 if (begin->expr_type != EXPR_ARRAY || !gfc_is_constant_expr (begin))
1562 t = false;
1563 goto cleanup;
1566 gcc_assert (begin->rank == 1);
1567 /* Zero-sized arrays have no shape and no elements, stop early. */
1568 if (!begin->shape)
1570 mpz_init_set_ui (nelts, 0);
1571 break;
1574 vecsub[d] = gfc_constructor_first (begin->value.constructor);
1575 mpz_set (ctr[d], vecsub[d]->expr->value.integer);
1576 mpz_mul (nelts, nelts, begin->shape[0]);
1577 mpz_set (expr->shape[shape_i++], begin->shape[0]);
1579 /* Check bounds. */
1580 for (ci = vecsub[d]; ci; ci = gfc_constructor_next (ci))
1582 if (mpz_cmp (ci->expr->value.integer, upper->value.integer) > 0
1583 || mpz_cmp (ci->expr->value.integer,
1584 lower->value.integer) < 0)
1586 gfc_error ("index in dimension %d is out of bounds "
1587 "at %L", d + 1, &ref->u.ar.c_where[d]);
1588 t = false;
1589 goto cleanup;
1593 else
1595 if ((begin && begin->expr_type != EXPR_CONSTANT)
1596 || (finish && finish->expr_type != EXPR_CONSTANT)
1597 || (step && step->expr_type != EXPR_CONSTANT)
1598 || !lower
1599 || !upper)
1601 t = false;
1602 goto cleanup;
1605 /* Obtain the stride. */
1606 if (step)
1607 mpz_set (stride[d], step->value.integer);
1608 else
1609 mpz_set_ui (stride[d], one);
1611 if (mpz_cmp_ui (stride[d], 0) == 0)
1612 mpz_set_ui (stride[d], one);
1614 /* Obtain the start value for the index. */
1615 if (begin)
1616 mpz_set (start[d], begin->value.integer);
1617 else
1618 mpz_set (start[d], lower->value.integer);
1620 mpz_set (ctr[d], start[d]);
1622 /* Obtain the end value for the index. */
1623 if (finish)
1624 mpz_set (end[d], finish->value.integer);
1625 else
1626 mpz_set (end[d], upper->value.integer);
1628 /* Separate 'if' because elements sometimes arrive with
1629 non-null end. */
1630 if (ref->u.ar.dimen_type[d] == DIMEN_ELEMENT)
1631 mpz_set (end [d], begin->value.integer);
1633 /* Check the bounds. */
1634 if (mpz_cmp (ctr[d], upper->value.integer) > 0
1635 || mpz_cmp (end[d], upper->value.integer) > 0
1636 || mpz_cmp (ctr[d], lower->value.integer) < 0
1637 || mpz_cmp (end[d], lower->value.integer) < 0)
1639 gfc_error ("index in dimension %d is out of bounds "
1640 "at %L", d + 1, &ref->u.ar.c_where[d]);
1641 t = false;
1642 goto cleanup;
1645 /* Calculate the number of elements and the shape. */
1646 mpz_set (tmp_mpz, stride[d]);
1647 mpz_add (tmp_mpz, end[d], tmp_mpz);
1648 mpz_sub (tmp_mpz, tmp_mpz, ctr[d]);
1649 mpz_div (tmp_mpz, tmp_mpz, stride[d]);
1650 mpz_mul (nelts, nelts, tmp_mpz);
1652 /* An element reference reduces the rank of the expression; don't
1653 add anything to the shape array. */
1654 if (ref->u.ar.dimen_type[d] != DIMEN_ELEMENT)
1655 mpz_set (expr->shape[shape_i++], tmp_mpz);
1658 /* Calculate the 'stride' (=delta) for conversion of the
1659 counter values into the index along the constructor. */
1660 mpz_set (delta[d], delta_mpz);
1661 mpz_sub (tmp_mpz, upper->value.integer, lower->value.integer);
1662 mpz_add_ui (tmp_mpz, tmp_mpz, one);
1663 mpz_mul (delta_mpz, delta_mpz, tmp_mpz);
1666 mpz_init (ptr);
1667 cons = gfc_constructor_first (base);
1669 /* Now clock through the array reference, calculating the index in
1670 the source constructor and transferring the elements to the new
1671 constructor. */
1672 for (idx = 0; idx < (int) mpz_get_si (nelts); idx++)
1674 mpz_init_set_ui (ptr, 0);
1676 incr_ctr = true;
1677 for (d = 0; d < rank; d++)
1679 mpz_set (tmp_mpz, ctr[d]);
1680 mpz_sub (tmp_mpz, tmp_mpz, ref->u.ar.as->lower[d]->value.integer);
1681 mpz_mul (tmp_mpz, tmp_mpz, delta[d]);
1682 mpz_add (ptr, ptr, tmp_mpz);
1684 if (!incr_ctr) continue;
1686 if (ref->u.ar.dimen_type[d] == DIMEN_VECTOR) /* Vector subscript. */
1688 gcc_assert(vecsub[d]);
1690 if (!gfc_constructor_next (vecsub[d]))
1691 vecsub[d] = gfc_constructor_first (ref->u.ar.start[d]->value.constructor);
1692 else
1694 vecsub[d] = gfc_constructor_next (vecsub[d]);
1695 incr_ctr = false;
1697 mpz_set (ctr[d], vecsub[d]->expr->value.integer);
1699 else
1701 mpz_add (ctr[d], ctr[d], stride[d]);
1703 if (mpz_cmp_ui (stride[d], 0) > 0
1704 ? mpz_cmp (ctr[d], end[d]) > 0
1705 : mpz_cmp (ctr[d], end[d]) < 0)
1706 mpz_set (ctr[d], start[d]);
1707 else
1708 incr_ctr = false;
1712 limit = mpz_get_ui (ptr);
1713 if (limit >= flag_max_array_constructor)
1715 gfc_error ("The number of elements in the array constructor "
1716 "at %L requires an increase of the allowed %d "
1717 "upper limit. See %<-fmax-array-constructor%> "
1718 "option", &expr->where, flag_max_array_constructor);
1719 return false;
1722 cons = gfc_constructor_lookup (base, limit);
1723 if (cons == NULL)
1725 gfc_error ("Error in array constructor referenced at %L",
1726 &ref->u.ar.where);
1727 t = false;
1728 goto cleanup;
1730 gfc_constructor_append_expr (&expr->value.constructor,
1731 gfc_copy_expr (cons->expr), NULL);
1734 mpz_clear (ptr);
1736 cleanup:
1738 mpz_clear (delta_mpz);
1739 mpz_clear (tmp_mpz);
1740 mpz_clear (nelts);
1741 for (d = 0; d < rank; d++)
1743 mpz_clear (delta[d]);
1744 mpz_clear (start[d]);
1745 mpz_clear (end[d]);
1746 mpz_clear (ctr[d]);
1747 mpz_clear (stride[d]);
1749 gfc_constructor_free (base);
1750 return t;
1753 /* Pull a substring out of an expression. */
1755 static bool
1756 find_substring_ref (gfc_expr *p, gfc_expr **newp)
1758 gfc_charlen_t end;
1759 gfc_charlen_t start;
1760 gfc_charlen_t length;
1761 gfc_char_t *chr;
1763 if (p->ref->u.ss.start->expr_type != EXPR_CONSTANT
1764 || p->ref->u.ss.end->expr_type != EXPR_CONSTANT)
1765 return false;
1767 *newp = gfc_copy_expr (p);
1768 free ((*newp)->value.character.string);
1770 end = (gfc_charlen_t) mpz_get_si (p->ref->u.ss.end->value.integer);
1771 start = (gfc_charlen_t) mpz_get_si (p->ref->u.ss.start->value.integer);
1772 if (end >= start)
1773 length = end - start + 1;
1774 else
1775 length = 0;
1777 chr = (*newp)->value.character.string = gfc_get_wide_string (length + 1);
1778 (*newp)->value.character.length = length;
1779 memcpy (chr, &p->value.character.string[start - 1],
1780 length * sizeof (gfc_char_t));
1781 chr[length] = '\0';
1782 return true;
1786 /* Pull an inquiry result out of an expression. */
1788 static bool
1789 find_inquiry_ref (gfc_expr *p, gfc_expr **newp)
1791 gfc_ref *ref;
1792 gfc_ref *inquiry = NULL;
1793 gfc_expr *tmp;
1795 tmp = gfc_copy_expr (p);
1797 if (tmp->ref && tmp->ref->type == REF_INQUIRY)
1799 inquiry = tmp->ref;
1800 tmp->ref = NULL;
1802 else
1804 for (ref = tmp->ref; ref; ref = ref->next)
1805 if (ref->next && ref->next->type == REF_INQUIRY)
1807 inquiry = ref->next;
1808 ref->next = NULL;
1812 if (!inquiry)
1814 gfc_free_expr (tmp);
1815 return false;
1818 gfc_resolve_expr (tmp);
1820 /* In principle there can be more than one inquiry reference. */
1821 for (; inquiry; inquiry = inquiry->next)
1823 switch (inquiry->u.i)
1825 case INQUIRY_LEN:
1826 if (tmp->ts.type != BT_CHARACTER)
1827 goto cleanup;
1829 if (!gfc_notify_std (GFC_STD_F2003, "LEN part_ref at %C"))
1830 goto cleanup;
1832 if (tmp->ts.u.cl->length
1833 && tmp->ts.u.cl->length->expr_type == EXPR_CONSTANT)
1834 *newp = gfc_copy_expr (tmp->ts.u.cl->length);
1835 else if (tmp->expr_type == EXPR_CONSTANT)
1836 *newp = gfc_get_int_expr (gfc_default_integer_kind,
1837 NULL, tmp->value.character.length);
1838 else
1839 goto cleanup;
1841 break;
1843 case INQUIRY_KIND:
1844 if (tmp->ts.type == BT_DERIVED || tmp->ts.type == BT_CLASS)
1845 goto cleanup;
1847 if (!gfc_notify_std (GFC_STD_F2003, "KIND part_ref at %C"))
1848 goto cleanup;
1850 *newp = gfc_get_int_expr (gfc_default_integer_kind,
1851 NULL, tmp->ts.kind);
1852 break;
1854 case INQUIRY_RE:
1855 if (tmp->ts.type != BT_COMPLEX || tmp->expr_type != EXPR_CONSTANT)
1856 goto cleanup;
1858 if (!gfc_notify_std (GFC_STD_F2008, "RE part_ref at %C"))
1859 goto cleanup;
1861 *newp = gfc_get_constant_expr (BT_REAL, tmp->ts.kind, &tmp->where);
1862 mpfr_set ((*newp)->value.real,
1863 mpc_realref (tmp->value.complex), GFC_RND_MODE);
1864 break;
1866 case INQUIRY_IM:
1867 if (tmp->ts.type != BT_COMPLEX || tmp->expr_type != EXPR_CONSTANT)
1868 goto cleanup;
1870 if (!gfc_notify_std (GFC_STD_F2008, "IM part_ref at %C"))
1871 goto cleanup;
1873 *newp = gfc_get_constant_expr (BT_REAL, tmp->ts.kind, &tmp->where);
1874 mpfr_set ((*newp)->value.real,
1875 mpc_imagref (tmp->value.complex), GFC_RND_MODE);
1876 break;
1878 tmp = gfc_copy_expr (*newp);
1881 if (!(*newp))
1882 goto cleanup;
1883 else if ((*newp)->expr_type != EXPR_CONSTANT)
1885 gfc_free_expr (*newp);
1886 goto cleanup;
1889 gfc_free_expr (tmp);
1890 return true;
1892 cleanup:
1893 gfc_free_expr (tmp);
1894 return false;
1899 /* Simplify a subobject reference of a constructor. This occurs when
1900 parameter variable values are substituted. */
1902 static bool
1903 simplify_const_ref (gfc_expr *p)
1905 gfc_constructor *cons, *c;
1906 gfc_expr *newp = NULL;
1907 gfc_ref *last_ref;
1909 while (p->ref)
1911 switch (p->ref->type)
1913 case REF_ARRAY:
1914 switch (p->ref->u.ar.type)
1916 case AR_ELEMENT:
1917 /* <type/kind spec>, parameter :: x(<int>) = scalar_expr
1918 will generate this. */
1919 if (p->expr_type != EXPR_ARRAY)
1921 remove_subobject_ref (p, NULL);
1922 break;
1924 if (!find_array_element (p->value.constructor, &p->ref->u.ar, &cons))
1925 return false;
1927 if (!cons)
1928 return true;
1930 remove_subobject_ref (p, cons);
1931 break;
1933 case AR_SECTION:
1934 if (!find_array_section (p, p->ref))
1935 return false;
1936 p->ref->u.ar.type = AR_FULL;
1938 /* Fall through. */
1940 case AR_FULL:
1941 if (p->ref->next != NULL
1942 && (p->ts.type == BT_CHARACTER || gfc_bt_struct (p->ts.type)))
1944 for (c = gfc_constructor_first (p->value.constructor);
1945 c; c = gfc_constructor_next (c))
1947 c->expr->ref = gfc_copy_ref (p->ref->next);
1948 if (!simplify_const_ref (c->expr))
1949 return false;
1952 if (gfc_bt_struct (p->ts.type)
1953 && p->ref->next
1954 && (c = gfc_constructor_first (p->value.constructor)))
1956 /* There may have been component references. */
1957 p->ts = c->expr->ts;
1960 last_ref = p->ref;
1961 for (; last_ref->next; last_ref = last_ref->next) {};
1963 if (p->ts.type == BT_CHARACTER
1964 && last_ref->type == REF_SUBSTRING)
1966 /* If this is a CHARACTER array and we possibly took
1967 a substring out of it, update the type-spec's
1968 character length according to the first element
1969 (as all should have the same length). */
1970 gfc_charlen_t string_len;
1971 if ((c = gfc_constructor_first (p->value.constructor)))
1973 const gfc_expr* first = c->expr;
1974 gcc_assert (first->expr_type == EXPR_CONSTANT);
1975 gcc_assert (first->ts.type == BT_CHARACTER);
1976 string_len = first->value.character.length;
1978 else
1979 string_len = 0;
1981 if (!p->ts.u.cl)
1983 if (p->symtree)
1984 p->ts.u.cl = gfc_new_charlen (p->symtree->n.sym->ns,
1985 NULL);
1986 else
1987 p->ts.u.cl = gfc_new_charlen (gfc_current_ns,
1988 NULL);
1990 else
1991 gfc_free_expr (p->ts.u.cl->length);
1993 p->ts.u.cl->length
1994 = gfc_get_int_expr (gfc_charlen_int_kind,
1995 NULL, string_len);
1998 gfc_free_ref_list (p->ref);
1999 p->ref = NULL;
2000 break;
2002 default:
2003 return true;
2006 break;
2008 case REF_COMPONENT:
2009 cons = find_component_ref (p->value.constructor, p->ref);
2010 remove_subobject_ref (p, cons);
2011 break;
2013 case REF_INQUIRY:
2014 if (!find_inquiry_ref (p, &newp))
2015 return false;
2017 gfc_replace_expr (p, newp);
2018 gfc_free_ref_list (p->ref);
2019 p->ref = NULL;
2020 break;
2022 case REF_SUBSTRING:
2023 if (!find_substring_ref (p, &newp))
2024 return false;
2026 gfc_replace_expr (p, newp);
2027 gfc_free_ref_list (p->ref);
2028 p->ref = NULL;
2029 break;
2033 return true;
2037 /* Simplify a chain of references. */
2039 static bool
2040 simplify_ref_chain (gfc_ref *ref, int type, gfc_expr **p)
2042 int n;
2043 gfc_expr *newp;
2045 for (; ref; ref = ref->next)
2047 switch (ref->type)
2049 case REF_ARRAY:
2050 for (n = 0; n < ref->u.ar.dimen; n++)
2052 if (!gfc_simplify_expr (ref->u.ar.start[n], type))
2053 return false;
2054 if (!gfc_simplify_expr (ref->u.ar.end[n], type))
2055 return false;
2056 if (!gfc_simplify_expr (ref->u.ar.stride[n], type))
2057 return false;
2059 break;
2061 case REF_SUBSTRING:
2062 if (!gfc_simplify_expr (ref->u.ss.start, type))
2063 return false;
2064 if (!gfc_simplify_expr (ref->u.ss.end, type))
2065 return false;
2066 break;
2068 case REF_INQUIRY:
2069 if (!find_inquiry_ref (*p, &newp))
2070 return false;
2072 gfc_replace_expr (*p, newp);
2073 gfc_free_ref_list ((*p)->ref);
2074 (*p)->ref = NULL;
2075 return true;
2077 default:
2078 break;
2081 return true;
2085 /* Try to substitute the value of a parameter variable. */
2087 static bool
2088 simplify_parameter_variable (gfc_expr *p, int type)
2090 gfc_expr *e;
2091 bool t;
2093 /* Set rank and check array ref; as resolve_variable calls
2094 gfc_simplify_expr, call gfc_resolve_ref + gfc_expression_rank instead. */
2095 if (!gfc_resolve_ref (p))
2097 gfc_error_check ();
2098 return false;
2100 gfc_expression_rank (p);
2102 /* Is this an inquiry? */
2103 bool inquiry = false;
2104 gfc_ref* ref = p->ref;
2105 while (ref)
2107 if (ref->type == REF_INQUIRY)
2108 break;
2109 ref = ref->next;
2111 if (ref && ref->type == REF_INQUIRY)
2112 inquiry = ref->u.i == INQUIRY_LEN || ref->u.i == INQUIRY_KIND;
2114 if (gfc_is_size_zero_array (p))
2116 if (p->expr_type == EXPR_ARRAY)
2117 return true;
2119 e = gfc_get_expr ();
2120 e->expr_type = EXPR_ARRAY;
2121 e->ts = p->ts;
2122 e->rank = p->rank;
2123 e->value.constructor = NULL;
2124 e->shape = gfc_copy_shape (p->shape, p->rank);
2125 e->where = p->where;
2126 /* If %kind and %len are not used then we're done, otherwise
2127 drop through for simplification. */
2128 if (!inquiry)
2130 gfc_replace_expr (p, e);
2131 return true;
2134 else
2136 e = gfc_copy_expr (p->symtree->n.sym->value);
2137 if (e == NULL)
2138 return false;
2140 gfc_free_shape (&e->shape, e->rank);
2141 e->shape = gfc_copy_shape (p->shape, p->rank);
2142 e->rank = p->rank;
2144 if (e->ts.type == BT_CHARACTER && p->ts.u.cl)
2145 e->ts = p->ts;
2148 if (e->ts.type == BT_CHARACTER && e->ts.u.cl == NULL)
2149 e->ts.u.cl = gfc_new_charlen (gfc_current_ns, p->ts.u.cl);
2151 /* Do not copy subobject refs for constant. */
2152 if (e->expr_type != EXPR_CONSTANT && p->ref != NULL)
2153 e->ref = gfc_copy_ref (p->ref);
2154 t = gfc_simplify_expr (e, type);
2155 e->where = p->where;
2157 /* Only use the simplification if it eliminated all subobject references. */
2158 if (t && !e->ref)
2159 gfc_replace_expr (p, e);
2160 else
2161 gfc_free_expr (e);
2163 return t;
2167 static bool
2168 scalarize_intrinsic_call (gfc_expr *, bool init_flag);
2170 /* Given an expression, simplify it by collapsing constant
2171 expressions. Most simplification takes place when the expression
2172 tree is being constructed. If an intrinsic function is simplified
2173 at some point, we get called again to collapse the result against
2174 other constants.
2176 We work by recursively simplifying expression nodes, simplifying
2177 intrinsic functions where possible, which can lead to further
2178 constant collapsing. If an operator has constant operand(s), we
2179 rip the expression apart, and rebuild it, hoping that it becomes
2180 something simpler.
2182 The expression type is defined for:
2183 0 Basic expression parsing
2184 1 Simplifying array constructors -- will substitute
2185 iterator values.
2186 Returns false on error, true otherwise.
2187 NOTE: Will return true even if the expression cannot be simplified. */
2189 bool
2190 gfc_simplify_expr (gfc_expr *p, int type)
2192 gfc_actual_arglist *ap;
2193 gfc_intrinsic_sym* isym = NULL;
2196 if (p == NULL)
2197 return true;
2199 switch (p->expr_type)
2201 case EXPR_CONSTANT:
2202 if (p->ref && p->ref->type == REF_INQUIRY)
2203 simplify_ref_chain (p->ref, type, &p);
2204 break;
2205 case EXPR_NULL:
2206 break;
2208 case EXPR_FUNCTION:
2209 // For array-bound functions, we don't need to optimize
2210 // the 'array' argument. In particular, if the argument
2211 // is a PARAMETER, simplifying might convert an EXPR_VARIABLE
2212 // into an EXPR_ARRAY; the latter has lbound = 1, the former
2213 // can have any lbound.
2214 ap = p->value.function.actual;
2215 if (p->value.function.isym &&
2216 (p->value.function.isym->id == GFC_ISYM_LBOUND
2217 || p->value.function.isym->id == GFC_ISYM_UBOUND
2218 || p->value.function.isym->id == GFC_ISYM_LCOBOUND
2219 || p->value.function.isym->id == GFC_ISYM_UCOBOUND
2220 || p->value.function.isym->id == GFC_ISYM_SHAPE))
2221 ap = ap->next;
2223 for ( ; ap; ap = ap->next)
2224 if (!gfc_simplify_expr (ap->expr, type))
2225 return false;
2227 if (p->value.function.isym != NULL
2228 && gfc_intrinsic_func_interface (p, 1) == MATCH_ERROR)
2229 return false;
2231 if (p->symtree && (p->value.function.isym || p->ts.type == BT_UNKNOWN))
2233 isym = gfc_find_function (p->symtree->n.sym->name);
2234 if (isym && isym->elemental)
2235 scalarize_intrinsic_call (p, false);
2238 break;
2240 case EXPR_SUBSTRING:
2241 if (!simplify_ref_chain (p->ref, type, &p))
2242 return false;
2244 if (gfc_is_constant_expr (p))
2246 gfc_char_t *s;
2247 HOST_WIDE_INT start, end;
2249 start = 0;
2250 if (p->ref && p->ref->u.ss.start)
2252 gfc_extract_hwi (p->ref->u.ss.start, &start);
2253 start--; /* Convert from one-based to zero-based. */
2256 end = p->value.character.length;
2257 if (p->ref && p->ref->u.ss.end)
2258 gfc_extract_hwi (p->ref->u.ss.end, &end);
2260 if (end < start)
2261 end = start;
2263 s = gfc_get_wide_string (end - start + 2);
2264 memcpy (s, p->value.character.string + start,
2265 (end - start) * sizeof (gfc_char_t));
2266 s[end - start + 1] = '\0'; /* TODO: C-style string. */
2267 free (p->value.character.string);
2268 p->value.character.string = s;
2269 p->value.character.length = end - start;
2270 p->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
2271 p->ts.u.cl->length = gfc_get_int_expr (gfc_charlen_int_kind,
2272 NULL,
2273 p->value.character.length);
2274 gfc_free_ref_list (p->ref);
2275 p->ref = NULL;
2276 p->expr_type = EXPR_CONSTANT;
2278 break;
2280 case EXPR_OP:
2281 if (!simplify_intrinsic_op (p, type))
2282 return false;
2283 break;
2285 case EXPR_VARIABLE:
2286 /* Only substitute array parameter variables if we are in an
2287 initialization expression, or we want a subsection. */
2288 if (p->symtree->n.sym->attr.flavor == FL_PARAMETER
2289 && (gfc_init_expr_flag || p->ref
2290 || p->symtree->n.sym->value->expr_type != EXPR_ARRAY))
2292 if (!simplify_parameter_variable (p, type))
2293 return false;
2294 break;
2297 if (type == 1)
2299 gfc_simplify_iterator_var (p);
2302 /* Simplify subcomponent references. */
2303 if (!simplify_ref_chain (p->ref, type, &p))
2304 return false;
2306 break;
2308 case EXPR_STRUCTURE:
2309 case EXPR_ARRAY:
2310 if (!simplify_ref_chain (p->ref, type, &p))
2311 return false;
2313 /* If the following conditions hold, we found something like kind type
2314 inquiry of the form a(2)%kind while simplify the ref chain. */
2315 if (p->expr_type == EXPR_CONSTANT && !p->ref && !p->rank && !p->shape)
2316 return true;
2318 if (!simplify_constructor (p->value.constructor, type))
2319 return false;
2321 if (p->expr_type == EXPR_ARRAY && p->ref && p->ref->type == REF_ARRAY
2322 && p->ref->u.ar.type == AR_FULL)
2323 gfc_expand_constructor (p, false);
2325 if (!simplify_const_ref (p))
2326 return false;
2328 break;
2330 case EXPR_COMPCALL:
2331 case EXPR_PPC:
2332 break;
2334 case EXPR_UNKNOWN:
2335 gcc_unreachable ();
2338 return true;
2342 /* Try simplification of an expression via gfc_simplify_expr.
2343 When an error occurs (arithmetic or otherwise), roll back. */
2345 bool
2346 gfc_try_simplify_expr (gfc_expr *e, int type)
2348 gfc_expr *n;
2349 bool t, saved_div0;
2351 if (e == NULL || e->expr_type == EXPR_CONSTANT)
2352 return true;
2354 saved_div0 = gfc_seen_div0;
2355 gfc_seen_div0 = false;
2356 n = gfc_copy_expr (e);
2357 t = gfc_simplify_expr (n, type) && !gfc_seen_div0;
2358 if (t)
2359 gfc_replace_expr (e, n);
2360 else
2361 gfc_free_expr (n);
2362 gfc_seen_div0 = saved_div0;
2363 return t;
2367 /* Returns the type of an expression with the exception that iterator
2368 variables are automatically integers no matter what else they may
2369 be declared as. */
2371 static bt
2372 et0 (gfc_expr *e)
2374 if (e->expr_type == EXPR_VARIABLE && gfc_check_iter_variable (e))
2375 return BT_INTEGER;
2377 return e->ts.type;
2381 /* Scalarize an expression for an elemental intrinsic call. */
2383 static bool
2384 scalarize_intrinsic_call (gfc_expr *e, bool init_flag)
2386 gfc_actual_arglist *a, *b;
2387 gfc_constructor_base ctor;
2388 gfc_constructor *args[5] = {}; /* Avoid uninitialized warnings. */
2389 gfc_constructor *ci, *new_ctor;
2390 gfc_expr *expr, *old, *p;
2391 int n, i, rank[5], array_arg;
2393 if (e == NULL)
2394 return false;
2396 a = e->value.function.actual;
2397 for (; a; a = a->next)
2398 if (a->expr && !gfc_is_constant_expr (a->expr))
2399 return false;
2401 /* Find which, if any, arguments are arrays. Assume that the old
2402 expression carries the type information and that the first arg
2403 that is an array expression carries all the shape information.*/
2404 n = array_arg = 0;
2405 a = e->value.function.actual;
2406 for (; a; a = a->next)
2408 n++;
2409 if (!a->expr || a->expr->expr_type != EXPR_ARRAY)
2410 continue;
2411 array_arg = n;
2412 expr = gfc_copy_expr (a->expr);
2413 break;
2416 if (!array_arg)
2417 return false;
2419 old = gfc_copy_expr (e);
2421 gfc_constructor_free (expr->value.constructor);
2422 expr->value.constructor = NULL;
2423 expr->ts = old->ts;
2424 expr->where = old->where;
2425 expr->expr_type = EXPR_ARRAY;
2427 /* Copy the array argument constructors into an array, with nulls
2428 for the scalars. */
2429 n = 0;
2430 a = old->value.function.actual;
2431 for (; a; a = a->next)
2433 /* Check that this is OK for an initialization expression. */
2434 if (a->expr && init_flag && !gfc_check_init_expr (a->expr))
2435 goto cleanup;
2437 rank[n] = 0;
2438 if (a->expr && a->expr->rank && a->expr->expr_type == EXPR_VARIABLE)
2440 rank[n] = a->expr->rank;
2441 ctor = a->expr->symtree->n.sym->value->value.constructor;
2442 args[n] = gfc_constructor_first (ctor);
2444 else if (a->expr && a->expr->expr_type == EXPR_ARRAY)
2446 if (a->expr->rank)
2447 rank[n] = a->expr->rank;
2448 else
2449 rank[n] = 1;
2450 ctor = gfc_constructor_copy (a->expr->value.constructor);
2451 args[n] = gfc_constructor_first (ctor);
2453 else
2454 args[n] = NULL;
2456 n++;
2459 /* Using the array argument as the master, step through the array
2460 calling the function for each element and advancing the array
2461 constructors together. */
2462 for (ci = args[array_arg - 1]; ci; ci = gfc_constructor_next (ci))
2464 new_ctor = gfc_constructor_append_expr (&expr->value.constructor,
2465 gfc_copy_expr (old), NULL);
2467 gfc_free_actual_arglist (new_ctor->expr->value.function.actual);
2468 a = NULL;
2469 b = old->value.function.actual;
2470 for (i = 0; i < n; i++)
2472 if (a == NULL)
2473 new_ctor->expr->value.function.actual
2474 = a = gfc_get_actual_arglist ();
2475 else
2477 a->next = gfc_get_actual_arglist ();
2478 a = a->next;
2481 if (args[i])
2482 a->expr = gfc_copy_expr (args[i]->expr);
2483 else
2484 a->expr = gfc_copy_expr (b->expr);
2486 b = b->next;
2489 /* Simplify the function calls. If the simplification fails, the
2490 error will be flagged up down-stream or the library will deal
2491 with it. */
2492 p = gfc_copy_expr (new_ctor->expr);
2494 if (!gfc_simplify_expr (p, init_flag))
2495 gfc_free_expr (p);
2496 else
2497 gfc_replace_expr (new_ctor->expr, p);
2499 for (i = 0; i < n; i++)
2500 if (args[i])
2501 args[i] = gfc_constructor_next (args[i]);
2503 for (i = 1; i < n; i++)
2504 if (rank[i] && ((args[i] != NULL && args[array_arg - 1] == NULL)
2505 || (args[i] == NULL && args[array_arg - 1] != NULL)))
2506 goto compliance;
2509 free_expr0 (e);
2510 *e = *expr;
2511 /* Free "expr" but not the pointers it contains. */
2512 free (expr);
2513 gfc_free_expr (old);
2514 return true;
2516 compliance:
2517 gfc_error_now ("elemental function arguments at %C are not compliant");
2519 cleanup:
2520 gfc_free_expr (expr);
2521 gfc_free_expr (old);
2522 return false;
2526 static bool
2527 check_intrinsic_op (gfc_expr *e, bool (*check_function) (gfc_expr *))
2529 gfc_expr *op1 = e->value.op.op1;
2530 gfc_expr *op2 = e->value.op.op2;
2532 if (!(*check_function)(op1))
2533 return false;
2535 switch (e->value.op.op)
2537 case INTRINSIC_UPLUS:
2538 case INTRINSIC_UMINUS:
2539 if (!numeric_type (et0 (op1)))
2540 goto not_numeric;
2541 break;
2543 case INTRINSIC_EQ:
2544 case INTRINSIC_EQ_OS:
2545 case INTRINSIC_NE:
2546 case INTRINSIC_NE_OS:
2547 case INTRINSIC_GT:
2548 case INTRINSIC_GT_OS:
2549 case INTRINSIC_GE:
2550 case INTRINSIC_GE_OS:
2551 case INTRINSIC_LT:
2552 case INTRINSIC_LT_OS:
2553 case INTRINSIC_LE:
2554 case INTRINSIC_LE_OS:
2555 if (!(*check_function)(op2))
2556 return false;
2558 if (!(et0 (op1) == BT_CHARACTER && et0 (op2) == BT_CHARACTER)
2559 && !(numeric_type (et0 (op1)) && numeric_type (et0 (op2))))
2561 gfc_error ("Numeric or CHARACTER operands are required in "
2562 "expression at %L", &e->where);
2563 return false;
2565 break;
2567 case INTRINSIC_PLUS:
2568 case INTRINSIC_MINUS:
2569 case INTRINSIC_TIMES:
2570 case INTRINSIC_DIVIDE:
2571 case INTRINSIC_POWER:
2572 if (!(*check_function)(op2))
2573 return false;
2575 if (!numeric_type (et0 (op1)) || !numeric_type (et0 (op2)))
2576 goto not_numeric;
2578 break;
2580 case INTRINSIC_CONCAT:
2581 if (!(*check_function)(op2))
2582 return false;
2584 if (et0 (op1) != BT_CHARACTER || et0 (op2) != BT_CHARACTER)
2586 gfc_error ("Concatenation operator in expression at %L "
2587 "must have two CHARACTER operands", &op1->where);
2588 return false;
2591 if (op1->ts.kind != op2->ts.kind)
2593 gfc_error ("Concat operator at %L must concatenate strings of the "
2594 "same kind", &e->where);
2595 return false;
2598 break;
2600 case INTRINSIC_NOT:
2601 if (et0 (op1) != BT_LOGICAL)
2603 gfc_error (".NOT. operator in expression at %L must have a LOGICAL "
2604 "operand", &op1->where);
2605 return false;
2608 break;
2610 case INTRINSIC_AND:
2611 case INTRINSIC_OR:
2612 case INTRINSIC_EQV:
2613 case INTRINSIC_NEQV:
2614 if (!(*check_function)(op2))
2615 return false;
2617 if (et0 (op1) != BT_LOGICAL || et0 (op2) != BT_LOGICAL)
2619 gfc_error ("LOGICAL operands are required in expression at %L",
2620 &e->where);
2621 return false;
2624 break;
2626 case INTRINSIC_PARENTHESES:
2627 break;
2629 default:
2630 gfc_error ("Only intrinsic operators can be used in expression at %L",
2631 &e->where);
2632 return false;
2635 return true;
2637 not_numeric:
2638 gfc_error ("Numeric operands are required in expression at %L", &e->where);
2640 return false;
2643 /* F2003, 7.1.7 (3): In init expression, allocatable components
2644 must not be data-initialized. */
2645 static bool
2646 check_alloc_comp_init (gfc_expr *e)
2648 gfc_component *comp;
2649 gfc_constructor *ctor;
2651 gcc_assert (e->expr_type == EXPR_STRUCTURE);
2652 gcc_assert (e->ts.type == BT_DERIVED || e->ts.type == BT_CLASS);
2654 for (comp = e->ts.u.derived->components,
2655 ctor = gfc_constructor_first (e->value.constructor);
2656 comp; comp = comp->next, ctor = gfc_constructor_next (ctor))
2658 if (comp->attr.allocatable && ctor->expr
2659 && ctor->expr->expr_type != EXPR_NULL)
2661 gfc_error ("Invalid initialization expression for ALLOCATABLE "
2662 "component %qs in structure constructor at %L",
2663 comp->name, &ctor->expr->where);
2664 return false;
2668 return true;
2671 static match
2672 check_init_expr_arguments (gfc_expr *e)
2674 gfc_actual_arglist *ap;
2676 for (ap = e->value.function.actual; ap; ap = ap->next)
2677 if (!gfc_check_init_expr (ap->expr))
2678 return MATCH_ERROR;
2680 return MATCH_YES;
2683 static bool check_restricted (gfc_expr *);
2685 /* F95, 7.1.6.1, Initialization expressions, (7)
2686 F2003, 7.1.7 Initialization expression, (8)
2687 F2008, 7.1.12 Constant expression, (4) */
2689 static match
2690 check_inquiry (gfc_expr *e, int not_restricted)
2692 const char *name;
2693 const char *const *functions;
2695 static const char *const inquiry_func_f95[] = {
2696 "lbound", "shape", "size", "ubound",
2697 "bit_size", "len", "kind",
2698 "digits", "epsilon", "huge", "maxexponent", "minexponent",
2699 "precision", "radix", "range", "tiny",
2700 NULL
2703 static const char *const inquiry_func_f2003[] = {
2704 "lbound", "shape", "size", "ubound",
2705 "bit_size", "len", "kind",
2706 "digits", "epsilon", "huge", "maxexponent", "minexponent",
2707 "precision", "radix", "range", "tiny",
2708 "new_line", NULL
2711 /* std=f2008+ or -std=gnu */
2712 static const char *const inquiry_func_gnu[] = {
2713 "lbound", "shape", "size", "ubound",
2714 "bit_size", "len", "kind",
2715 "digits", "epsilon", "huge", "maxexponent", "minexponent",
2716 "precision", "radix", "range", "tiny",
2717 "new_line", "storage_size", NULL
2720 int i = 0;
2721 gfc_actual_arglist *ap;
2722 gfc_symbol *sym;
2723 gfc_symbol *asym;
2725 if (!e->value.function.isym
2726 || !e->value.function.isym->inquiry)
2727 return MATCH_NO;
2729 /* An undeclared parameter will get us here (PR25018). */
2730 if (e->symtree == NULL)
2731 return MATCH_NO;
2733 sym = e->symtree->n.sym;
2735 if (sym->from_intmod)
2737 if (sym->from_intmod == INTMOD_ISO_FORTRAN_ENV
2738 && sym->intmod_sym_id != ISOFORTRAN_COMPILER_OPTIONS
2739 && sym->intmod_sym_id != ISOFORTRAN_COMPILER_VERSION)
2740 return MATCH_NO;
2742 if (sym->from_intmod == INTMOD_ISO_C_BINDING
2743 && sym->intmod_sym_id != ISOCBINDING_C_SIZEOF)
2744 return MATCH_NO;
2746 else
2748 name = sym->name;
2750 functions = inquiry_func_gnu;
2751 if (gfc_option.warn_std & GFC_STD_F2003)
2752 functions = inquiry_func_f2003;
2753 if (gfc_option.warn_std & GFC_STD_F95)
2754 functions = inquiry_func_f95;
2756 for (i = 0; functions[i]; i++)
2757 if (strcmp (functions[i], name) == 0)
2758 break;
2760 if (functions[i] == NULL)
2761 return MATCH_ERROR;
2764 /* At this point we have an inquiry function with a variable argument. The
2765 type of the variable might be undefined, but we need it now, because the
2766 arguments of these functions are not allowed to be undefined. */
2768 for (ap = e->value.function.actual; ap; ap = ap->next)
2770 if (!ap->expr)
2771 continue;
2773 asym = ap->expr->symtree ? ap->expr->symtree->n.sym : NULL;
2775 if (ap->expr->ts.type == BT_UNKNOWN)
2777 if (asym && asym->ts.type == BT_UNKNOWN
2778 && !gfc_set_default_type (asym, 0, gfc_current_ns))
2779 return MATCH_NO;
2781 ap->expr->ts = asym->ts;
2784 if (asym && asym->assoc && asym->assoc->target
2785 && asym->assoc->target->expr_type == EXPR_CONSTANT)
2787 gfc_free_expr (ap->expr);
2788 ap->expr = gfc_copy_expr (asym->assoc->target);
2791 /* Assumed character length will not reduce to a constant expression
2792 with LEN, as required by the standard. */
2793 if (i == 5 && not_restricted && asym
2794 && asym->ts.type == BT_CHARACTER
2795 && ((asym->ts.u.cl && asym->ts.u.cl->length == NULL)
2796 || asym->ts.deferred))
2798 gfc_error ("Assumed or deferred character length variable %qs "
2799 "in constant expression at %L",
2800 asym->name, &ap->expr->where);
2801 return MATCH_ERROR;
2803 else if (not_restricted && !gfc_check_init_expr (ap->expr))
2804 return MATCH_ERROR;
2806 if (not_restricted == 0
2807 && ap->expr->expr_type != EXPR_VARIABLE
2808 && !check_restricted (ap->expr))
2809 return MATCH_ERROR;
2811 if (not_restricted == 0
2812 && ap->expr->expr_type == EXPR_VARIABLE
2813 && asym->attr.dummy && asym->attr.optional)
2814 return MATCH_NO;
2817 return MATCH_YES;
2821 /* F95, 7.1.6.1, Initialization expressions, (5)
2822 F2003, 7.1.7 Initialization expression, (5) */
2824 static match
2825 check_transformational (gfc_expr *e)
2827 static const char * const trans_func_f95[] = {
2828 "repeat", "reshape", "selected_int_kind",
2829 "selected_real_kind", "transfer", "trim", NULL
2832 static const char * const trans_func_f2003[] = {
2833 "all", "any", "count", "dot_product", "matmul", "null", "pack",
2834 "product", "repeat", "reshape", "selected_char_kind", "selected_int_kind",
2835 "selected_real_kind", "spread", "sum", "transfer", "transpose",
2836 "trim", "unpack", NULL
2839 static const char * const trans_func_f2008[] = {
2840 "all", "any", "count", "dot_product", "matmul", "null", "pack",
2841 "product", "repeat", "reshape", "selected_char_kind", "selected_int_kind",
2842 "selected_real_kind", "spread", "sum", "transfer", "transpose",
2843 "trim", "unpack", "findloc", NULL
2846 int i;
2847 const char *name;
2848 const char *const *functions;
2850 if (!e->value.function.isym
2851 || !e->value.function.isym->transformational)
2852 return MATCH_NO;
2854 name = e->symtree->n.sym->name;
2856 if (gfc_option.allow_std & GFC_STD_F2008)
2857 functions = trans_func_f2008;
2858 else if (gfc_option.allow_std & GFC_STD_F2003)
2859 functions = trans_func_f2003;
2860 else
2861 functions = trans_func_f95;
2863 /* NULL() is dealt with below. */
2864 if (strcmp ("null", name) == 0)
2865 return MATCH_NO;
2867 for (i = 0; functions[i]; i++)
2868 if (strcmp (functions[i], name) == 0)
2869 break;
2871 if (functions[i] == NULL)
2873 gfc_error ("transformational intrinsic %qs at %L is not permitted "
2874 "in an initialization expression", name, &e->where);
2875 return MATCH_ERROR;
2878 return check_init_expr_arguments (e);
2882 /* F95, 7.1.6.1, Initialization expressions, (6)
2883 F2003, 7.1.7 Initialization expression, (6) */
2885 static match
2886 check_null (gfc_expr *e)
2888 if (strcmp ("null", e->symtree->n.sym->name) != 0)
2889 return MATCH_NO;
2891 return check_init_expr_arguments (e);
2895 static match
2896 check_elemental (gfc_expr *e)
2898 if (!e->value.function.isym
2899 || !e->value.function.isym->elemental)
2900 return MATCH_NO;
2902 if (e->ts.type != BT_INTEGER
2903 && e->ts.type != BT_CHARACTER
2904 && !gfc_notify_std (GFC_STD_F2003, "Evaluation of nonstandard "
2905 "initialization expression at %L", &e->where))
2906 return MATCH_ERROR;
2908 return check_init_expr_arguments (e);
2912 static match
2913 check_conversion (gfc_expr *e)
2915 if (!e->value.function.isym
2916 || !e->value.function.isym->conversion)
2917 return MATCH_NO;
2919 return check_init_expr_arguments (e);
2923 /* Verify that an expression is an initialization expression. A side
2924 effect is that the expression tree is reduced to a single constant
2925 node if all goes well. This would normally happen when the
2926 expression is constructed but function references are assumed to be
2927 intrinsics in the context of initialization expressions. If
2928 false is returned an error message has been generated. */
2930 bool
2931 gfc_check_init_expr (gfc_expr *e)
2933 match m;
2934 bool t;
2936 if (e == NULL)
2937 return true;
2939 switch (e->expr_type)
2941 case EXPR_OP:
2942 t = check_intrinsic_op (e, gfc_check_init_expr);
2943 if (t)
2944 t = gfc_simplify_expr (e, 0);
2946 break;
2948 case EXPR_FUNCTION:
2949 t = false;
2952 bool conversion;
2953 gfc_intrinsic_sym* isym = NULL;
2954 gfc_symbol* sym = e->symtree->n.sym;
2956 /* Simplify here the intrinsics from the IEEE_ARITHMETIC and
2957 IEEE_EXCEPTIONS modules. */
2958 int mod = sym->from_intmod;
2959 if (mod == INTMOD_NONE && sym->generic)
2960 mod = sym->generic->sym->from_intmod;
2961 if (mod == INTMOD_IEEE_ARITHMETIC || mod == INTMOD_IEEE_EXCEPTIONS)
2963 gfc_expr *new_expr = gfc_simplify_ieee_functions (e);
2964 if (new_expr)
2966 gfc_replace_expr (e, new_expr);
2967 t = true;
2968 break;
2972 /* If a conversion function, e.g., __convert_i8_i4, was inserted
2973 into an array constructor, we need to skip the error check here.
2974 Conversion errors are caught below in scalarize_intrinsic_call. */
2975 conversion = e->value.function.isym
2976 && (e->value.function.isym->conversion == 1);
2978 if (!conversion && (!gfc_is_intrinsic (sym, 0, e->where)
2979 || (m = gfc_intrinsic_func_interface (e, 0)) == MATCH_NO))
2981 gfc_error ("Function %qs in initialization expression at %L "
2982 "must be an intrinsic function",
2983 e->symtree->n.sym->name, &e->where);
2984 break;
2987 if ((m = check_conversion (e)) == MATCH_NO
2988 && (m = check_inquiry (e, 1)) == MATCH_NO
2989 && (m = check_null (e)) == MATCH_NO
2990 && (m = check_transformational (e)) == MATCH_NO
2991 && (m = check_elemental (e)) == MATCH_NO)
2993 gfc_error ("Intrinsic function %qs at %L is not permitted "
2994 "in an initialization expression",
2995 e->symtree->n.sym->name, &e->where);
2996 m = MATCH_ERROR;
2999 if (m == MATCH_ERROR)
3000 return false;
3002 /* Try to scalarize an elemental intrinsic function that has an
3003 array argument. */
3004 isym = gfc_find_function (e->symtree->n.sym->name);
3005 if (isym && isym->elemental
3006 && (t = scalarize_intrinsic_call (e, true)))
3007 break;
3010 if (m == MATCH_YES)
3011 t = gfc_simplify_expr (e, 0);
3013 break;
3015 case EXPR_VARIABLE:
3016 t = true;
3018 /* This occurs when parsing pdt templates. */
3019 if (gfc_expr_attr (e).pdt_kind)
3020 break;
3022 if (gfc_check_iter_variable (e))
3023 break;
3025 if (e->symtree->n.sym->attr.flavor == FL_PARAMETER)
3027 /* A PARAMETER shall not be used to define itself, i.e.
3028 REAL, PARAMETER :: x = transfer(0, x)
3029 is invalid. */
3030 if (!e->symtree->n.sym->value)
3032 gfc_error ("PARAMETER %qs is used at %L before its definition "
3033 "is complete", e->symtree->n.sym->name, &e->where);
3034 t = false;
3036 else
3037 t = simplify_parameter_variable (e, 0);
3039 break;
3042 if (gfc_in_match_data ())
3043 break;
3045 t = false;
3047 if (e->symtree->n.sym->as)
3049 switch (e->symtree->n.sym->as->type)
3051 case AS_ASSUMED_SIZE:
3052 gfc_error ("Assumed size array %qs at %L is not permitted "
3053 "in an initialization expression",
3054 e->symtree->n.sym->name, &e->where);
3055 break;
3057 case AS_ASSUMED_SHAPE:
3058 gfc_error ("Assumed shape array %qs at %L is not permitted "
3059 "in an initialization expression",
3060 e->symtree->n.sym->name, &e->where);
3061 break;
3063 case AS_DEFERRED:
3064 if (!e->symtree->n.sym->attr.allocatable
3065 && !e->symtree->n.sym->attr.pointer
3066 && e->symtree->n.sym->attr.dummy)
3067 gfc_error ("Assumed-shape array %qs at %L is not permitted "
3068 "in an initialization expression",
3069 e->symtree->n.sym->name, &e->where);
3070 else
3071 gfc_error ("Deferred array %qs at %L is not permitted "
3072 "in an initialization expression",
3073 e->symtree->n.sym->name, &e->where);
3074 break;
3076 case AS_EXPLICIT:
3077 gfc_error ("Array %qs at %L is a variable, which does "
3078 "not reduce to a constant expression",
3079 e->symtree->n.sym->name, &e->where);
3080 break;
3082 case AS_ASSUMED_RANK:
3083 gfc_error ("Assumed-rank array %qs at %L is not permitted "
3084 "in an initialization expression",
3085 e->symtree->n.sym->name, &e->where);
3086 break;
3088 default:
3089 gcc_unreachable();
3092 else
3093 gfc_error ("Parameter %qs at %L has not been declared or is "
3094 "a variable, which does not reduce to a constant "
3095 "expression", e->symtree->name, &e->where);
3097 break;
3099 case EXPR_CONSTANT:
3100 case EXPR_NULL:
3101 t = true;
3102 break;
3104 case EXPR_SUBSTRING:
3105 if (e->ref)
3107 t = gfc_check_init_expr (e->ref->u.ss.start);
3108 if (!t)
3109 break;
3111 t = gfc_check_init_expr (e->ref->u.ss.end);
3112 if (t)
3113 t = gfc_simplify_expr (e, 0);
3115 else
3116 t = false;
3117 break;
3119 case EXPR_STRUCTURE:
3120 t = e->ts.is_iso_c ? true : false;
3121 if (t)
3122 break;
3124 t = check_alloc_comp_init (e);
3125 if (!t)
3126 break;
3128 t = gfc_check_constructor (e, gfc_check_init_expr);
3129 if (!t)
3130 break;
3132 break;
3134 case EXPR_ARRAY:
3135 t = gfc_check_constructor (e, gfc_check_init_expr);
3136 if (!t)
3137 break;
3139 t = gfc_expand_constructor (e, true);
3140 if (!t)
3141 break;
3143 t = gfc_check_constructor_type (e);
3144 break;
3146 default:
3147 gfc_internal_error ("check_init_expr(): Unknown expression type");
3150 return t;
3153 /* Reduces a general expression to an initialization expression (a constant).
3154 This used to be part of gfc_match_init_expr.
3155 Note that this function doesn't free the given expression on false. */
3157 bool
3158 gfc_reduce_init_expr (gfc_expr *expr)
3160 bool t;
3162 gfc_init_expr_flag = true;
3163 t = gfc_resolve_expr (expr);
3164 if (t)
3165 t = gfc_check_init_expr (expr);
3166 gfc_init_expr_flag = false;
3168 if (!t || !expr)
3169 return false;
3171 if (expr->expr_type == EXPR_ARRAY)
3173 if (!gfc_check_constructor_type (expr))
3174 return false;
3175 if (!gfc_expand_constructor (expr, true))
3176 return false;
3179 return true;
3183 /* Match an initialization expression. We work by first matching an
3184 expression, then reducing it to a constant. */
3186 match
3187 gfc_match_init_expr (gfc_expr **result)
3189 gfc_expr *expr;
3190 match m;
3191 bool t;
3193 expr = NULL;
3195 gfc_init_expr_flag = true;
3197 m = gfc_match_expr (&expr);
3198 if (m != MATCH_YES)
3200 gfc_init_expr_flag = false;
3201 return m;
3204 if (gfc_derived_parameter_expr (expr))
3206 *result = expr;
3207 gfc_init_expr_flag = false;
3208 return m;
3211 t = gfc_reduce_init_expr (expr);
3212 if (!t)
3214 gfc_free_expr (expr);
3215 gfc_init_expr_flag = false;
3216 return MATCH_ERROR;
3219 *result = expr;
3220 gfc_init_expr_flag = false;
3222 return MATCH_YES;
3226 /* Given an actual argument list, test to see that each argument is a
3227 restricted expression and optionally if the expression type is
3228 integer or character. */
3230 static bool
3231 restricted_args (gfc_actual_arglist *a)
3233 for (; a; a = a->next)
3235 if (!check_restricted (a->expr))
3236 return false;
3239 return true;
3243 /************* Restricted/specification expressions *************/
3246 /* Make sure a non-intrinsic function is a specification function,
3247 * see F08:7.1.11.5. */
3249 static bool
3250 external_spec_function (gfc_expr *e)
3252 gfc_symbol *f;
3254 f = e->value.function.esym;
3256 /* IEEE functions allowed are "a reference to a transformational function
3257 from the intrinsic module IEEE_ARITHMETIC or IEEE_EXCEPTIONS", and
3258 "inquiry function from the intrinsic modules IEEE_ARITHMETIC and
3259 IEEE_EXCEPTIONS". */
3260 if (f->from_intmod == INTMOD_IEEE_ARITHMETIC
3261 || f->from_intmod == INTMOD_IEEE_EXCEPTIONS)
3263 if (!strcmp (f->name, "ieee_selected_real_kind")
3264 || !strcmp (f->name, "ieee_support_rounding")
3265 || !strcmp (f->name, "ieee_support_flag")
3266 || !strcmp (f->name, "ieee_support_halting")
3267 || !strcmp (f->name, "ieee_support_datatype")
3268 || !strcmp (f->name, "ieee_support_denormal")
3269 || !strcmp (f->name, "ieee_support_subnormal")
3270 || !strcmp (f->name, "ieee_support_divide")
3271 || !strcmp (f->name, "ieee_support_inf")
3272 || !strcmp (f->name, "ieee_support_io")
3273 || !strcmp (f->name, "ieee_support_nan")
3274 || !strcmp (f->name, "ieee_support_sqrt")
3275 || !strcmp (f->name, "ieee_support_standard")
3276 || !strcmp (f->name, "ieee_support_underflow_control"))
3277 goto function_allowed;
3280 if (f->attr.proc == PROC_ST_FUNCTION)
3282 gfc_error ("Specification function %qs at %L cannot be a statement "
3283 "function", f->name, &e->where);
3284 return false;
3287 if (f->attr.proc == PROC_INTERNAL)
3289 gfc_error ("Specification function %qs at %L cannot be an internal "
3290 "function", f->name, &e->where);
3291 return false;
3294 if (!f->attr.pure && !f->attr.elemental)
3296 gfc_error ("Specification function %qs at %L must be PURE", f->name,
3297 &e->where);
3298 return false;
3301 /* F08:7.1.11.6. */
3302 if (f->attr.recursive
3303 && !gfc_notify_std (GFC_STD_F2003,
3304 "Specification function %qs "
3305 "at %L cannot be RECURSIVE", f->name, &e->where))
3306 return false;
3308 function_allowed:
3309 return restricted_args (e->value.function.actual);
3313 /* Check to see that a function reference to an intrinsic is a
3314 restricted expression. */
3316 static bool
3317 restricted_intrinsic (gfc_expr *e)
3319 /* TODO: Check constraints on inquiry functions. 7.1.6.2 (7). */
3320 if (check_inquiry (e, 0) == MATCH_YES)
3321 return true;
3323 return restricted_args (e->value.function.actual);
3327 /* Check the expressions of an actual arglist. Used by check_restricted. */
3329 static bool
3330 check_arglist (gfc_actual_arglist* arg, bool (*checker) (gfc_expr*))
3332 for (; arg; arg = arg->next)
3333 if (!checker (arg->expr))
3334 return false;
3336 return true;
3340 /* Check the subscription expressions of a reference chain with a checking
3341 function; used by check_restricted. */
3343 static bool
3344 check_references (gfc_ref* ref, bool (*checker) (gfc_expr*))
3346 int dim;
3348 if (!ref)
3349 return true;
3351 switch (ref->type)
3353 case REF_ARRAY:
3354 for (dim = 0; dim < ref->u.ar.dimen; ++dim)
3356 if (!checker (ref->u.ar.start[dim]))
3357 return false;
3358 if (!checker (ref->u.ar.end[dim]))
3359 return false;
3360 if (!checker (ref->u.ar.stride[dim]))
3361 return false;
3363 break;
3365 case REF_COMPONENT:
3366 /* Nothing needed, just proceed to next reference. */
3367 break;
3369 case REF_SUBSTRING:
3370 if (!checker (ref->u.ss.start))
3371 return false;
3372 if (!checker (ref->u.ss.end))
3373 return false;
3374 break;
3376 default:
3377 gcc_unreachable ();
3378 break;
3381 return check_references (ref->next, checker);
3384 /* Return true if ns is a parent of the current ns. */
3386 static bool
3387 is_parent_of_current_ns (gfc_namespace *ns)
3389 gfc_namespace *p;
3390 for (p = gfc_current_ns->parent; p; p = p->parent)
3391 if (ns == p)
3392 return true;
3394 return false;
3397 /* Verify that an expression is a restricted expression. Like its
3398 cousin check_init_expr(), an error message is generated if we
3399 return false. */
3401 static bool
3402 check_restricted (gfc_expr *e)
3404 gfc_symbol* sym;
3405 bool t;
3407 if (e == NULL)
3408 return true;
3410 switch (e->expr_type)
3412 case EXPR_OP:
3413 t = check_intrinsic_op (e, check_restricted);
3414 if (t)
3415 t = gfc_simplify_expr (e, 0);
3417 break;
3419 case EXPR_FUNCTION:
3420 if (e->value.function.esym)
3422 t = check_arglist (e->value.function.actual, &check_restricted);
3423 if (t)
3424 t = external_spec_function (e);
3426 else
3428 if (e->value.function.isym && e->value.function.isym->inquiry)
3429 t = true;
3430 else
3431 t = check_arglist (e->value.function.actual, &check_restricted);
3433 if (t)
3434 t = restricted_intrinsic (e);
3436 break;
3438 case EXPR_VARIABLE:
3439 sym = e->symtree->n.sym;
3440 t = false;
3442 /* If a dummy argument appears in a context that is valid for a
3443 restricted expression in an elemental procedure, it will have
3444 already been simplified away once we get here. Therefore we
3445 don't need to jump through hoops to distinguish valid from
3446 invalid cases. Allowed in F2008 and F2018. */
3447 if (gfc_notification_std (GFC_STD_F2008)
3448 && sym->attr.dummy && sym->ns == gfc_current_ns
3449 && sym->ns->proc_name && sym->ns->proc_name->attr.elemental)
3451 gfc_error_now ("Dummy argument %qs not "
3452 "allowed in expression at %L",
3453 sym->name, &e->where);
3454 break;
3457 if (sym->attr.optional)
3459 gfc_error ("Dummy argument %qs at %L cannot be OPTIONAL",
3460 sym->name, &e->where);
3461 break;
3464 if (sym->attr.intent == INTENT_OUT)
3466 gfc_error ("Dummy argument %qs at %L cannot be INTENT(OUT)",
3467 sym->name, &e->where);
3468 break;
3471 /* Check reference chain if any. */
3472 if (!check_references (e->ref, &check_restricted))
3473 break;
3475 /* gfc_is_formal_arg broadcasts that a formal argument list is being
3476 processed in resolve.cc(resolve_formal_arglist). This is done so
3477 that host associated dummy array indices are accepted (PR23446).
3478 This mechanism also does the same for the specification expressions
3479 of array-valued functions. */
3480 if (e->error
3481 || sym->attr.in_common
3482 || sym->attr.use_assoc
3483 || sym->attr.dummy
3484 || sym->attr.implied_index
3485 || sym->attr.flavor == FL_PARAMETER
3486 || is_parent_of_current_ns (sym->ns)
3487 || (sym->ns->proc_name != NULL
3488 && sym->ns->proc_name->attr.flavor == FL_MODULE)
3489 || (gfc_is_formal_arg () && (sym->ns == gfc_current_ns)))
3491 t = true;
3492 break;
3495 gfc_error ("Variable %qs cannot appear in the expression at %L",
3496 sym->name, &e->where);
3497 /* Prevent a repetition of the error. */
3498 e->error = 1;
3499 break;
3501 case EXPR_NULL:
3502 case EXPR_CONSTANT:
3503 t = true;
3504 break;
3506 case EXPR_SUBSTRING:
3507 t = gfc_specification_expr (e->ref->u.ss.start);
3508 if (!t)
3509 break;
3511 t = gfc_specification_expr (e->ref->u.ss.end);
3512 if (t)
3513 t = gfc_simplify_expr (e, 0);
3515 break;
3517 case EXPR_STRUCTURE:
3518 t = gfc_check_constructor (e, check_restricted);
3519 break;
3521 case EXPR_ARRAY:
3522 t = gfc_check_constructor (e, check_restricted);
3523 break;
3525 default:
3526 gfc_internal_error ("check_restricted(): Unknown expression type");
3529 return t;
3533 /* Check to see that an expression is a specification expression. If
3534 we return false, an error has been generated. */
3536 bool
3537 gfc_specification_expr (gfc_expr *e)
3539 gfc_component *comp;
3541 if (e == NULL)
3542 return true;
3544 if (e->ts.type != BT_INTEGER)
3546 gfc_error ("Expression at %L must be of INTEGER type, found %s",
3547 &e->where, gfc_basic_typename (e->ts.type));
3548 return false;
3551 comp = gfc_get_proc_ptr_comp (e);
3552 if (e->expr_type == EXPR_FUNCTION
3553 && !e->value.function.isym
3554 && !e->value.function.esym
3555 && !gfc_pure (e->symtree->n.sym)
3556 && (!comp || !comp->attr.pure))
3558 gfc_error ("Function %qs at %L must be PURE",
3559 e->symtree->n.sym->name, &e->where);
3560 /* Prevent repeat error messages. */
3561 e->symtree->n.sym->attr.pure = 1;
3562 return false;
3565 if (e->rank != 0)
3567 gfc_error ("Expression at %L must be scalar", &e->where);
3568 return false;
3571 if (!gfc_simplify_expr (e, 0))
3572 return false;
3574 return check_restricted (e);
3578 /************** Expression conformance checks. *************/
3580 /* Given two expressions, make sure that the arrays are conformable. */
3582 bool
3583 gfc_check_conformance (gfc_expr *op1, gfc_expr *op2, const char *optype_msgid, ...)
3585 int op1_flag, op2_flag, d;
3586 mpz_t op1_size, op2_size;
3587 bool t;
3589 va_list argp;
3590 char buffer[240];
3592 if (op1->rank == 0 || op2->rank == 0)
3593 return true;
3595 va_start (argp, optype_msgid);
3596 d = vsnprintf (buffer, sizeof (buffer), optype_msgid, argp);
3597 va_end (argp);
3598 if (d < 1 || d >= (int) sizeof (buffer)) /* Reject truncation. */
3599 gfc_internal_error ("optype_msgid overflow: %d", d);
3601 if (op1->rank != op2->rank)
3603 gfc_error ("Incompatible ranks in %s (%d and %d) at %L", _(buffer),
3604 op1->rank, op2->rank, &op1->where);
3605 return false;
3608 t = true;
3610 for (d = 0; d < op1->rank; d++)
3612 op1_flag = gfc_array_dimen_size(op1, d, &op1_size);
3613 op2_flag = gfc_array_dimen_size(op2, d, &op2_size);
3615 if (op1_flag && op2_flag && mpz_cmp (op1_size, op2_size) != 0)
3617 gfc_error ("Different shape for %s at %L on dimension %d "
3618 "(%d and %d)", _(buffer), &op1->where, d + 1,
3619 (int) mpz_get_si (op1_size),
3620 (int) mpz_get_si (op2_size));
3622 t = false;
3625 if (op1_flag)
3626 mpz_clear (op1_size);
3627 if (op2_flag)
3628 mpz_clear (op2_size);
3630 if (!t)
3631 return false;
3634 return true;
3638 /* Given an assignable expression and an arbitrary expression, make
3639 sure that the assignment can take place. Only add a call to the intrinsic
3640 conversion routines, when allow_convert is set. When this assign is a
3641 coarray call, then the convert is done by the coarray routine implictly and
3642 adding the intrinsic conversion would do harm in most cases. */
3644 bool
3645 gfc_check_assign (gfc_expr *lvalue, gfc_expr *rvalue, int conform,
3646 bool allow_convert)
3648 gfc_symbol *sym;
3649 gfc_ref *ref;
3650 int has_pointer;
3652 sym = lvalue->symtree->n.sym;
3654 /* See if this is the component or subcomponent of a pointer and guard
3655 against assignment to LEN or KIND part-refs. */
3656 has_pointer = sym->attr.pointer;
3657 for (ref = lvalue->ref; ref; ref = ref->next)
3659 if (!has_pointer && ref->type == REF_COMPONENT
3660 && ref->u.c.component->attr.pointer)
3661 has_pointer = 1;
3662 else if (ref->type == REF_INQUIRY
3663 && (ref->u.i == INQUIRY_LEN || ref->u.i == INQUIRY_KIND))
3665 gfc_error ("Assignment to a LEN or KIND part_ref at %L is not "
3666 "allowed", &lvalue->where);
3667 return false;
3671 /* 12.5.2.2, Note 12.26: The result variable is very similar to any other
3672 variable local to a function subprogram. Its existence begins when
3673 execution of the function is initiated and ends when execution of the
3674 function is terminated...
3675 Therefore, the left hand side is no longer a variable, when it is: */
3676 if (sym->attr.flavor == FL_PROCEDURE && sym->attr.proc != PROC_ST_FUNCTION
3677 && !sym->attr.external)
3679 bool bad_proc;
3680 bad_proc = false;
3682 /* (i) Use associated; */
3683 if (sym->attr.use_assoc)
3684 bad_proc = true;
3686 /* (ii) The assignment is in the main program; or */
3687 if (gfc_current_ns->proc_name
3688 && gfc_current_ns->proc_name->attr.is_main_program)
3689 bad_proc = true;
3691 /* (iii) A module or internal procedure... */
3692 if (gfc_current_ns->proc_name
3693 && (gfc_current_ns->proc_name->attr.proc == PROC_INTERNAL
3694 || gfc_current_ns->proc_name->attr.proc == PROC_MODULE)
3695 && gfc_current_ns->parent
3696 && (!(gfc_current_ns->parent->proc_name->attr.function
3697 || gfc_current_ns->parent->proc_name->attr.subroutine)
3698 || gfc_current_ns->parent->proc_name->attr.is_main_program))
3700 /* ... that is not a function... */
3701 if (gfc_current_ns->proc_name
3702 && !gfc_current_ns->proc_name->attr.function)
3703 bad_proc = true;
3705 /* ... or is not an entry and has a different name. */
3706 if (!sym->attr.entry && sym->name != gfc_current_ns->proc_name->name)
3707 bad_proc = true;
3710 /* (iv) Host associated and not the function symbol or the
3711 parent result. This picks up sibling references, which
3712 cannot be entries. */
3713 if (!sym->attr.entry
3714 && sym->ns == gfc_current_ns->parent
3715 && sym != gfc_current_ns->proc_name
3716 && sym != gfc_current_ns->parent->proc_name->result)
3717 bad_proc = true;
3719 if (bad_proc)
3721 gfc_error ("%qs at %L is not a VALUE", sym->name, &lvalue->where);
3722 return false;
3725 else
3727 /* Reject assigning to an external symbol. For initializers, this
3728 was already done before, in resolve_fl_procedure. */
3729 if (sym->attr.flavor == FL_PROCEDURE && sym->attr.external
3730 && sym->attr.proc != PROC_MODULE && !rvalue->error)
3732 gfc_error ("Illegal assignment to external procedure at %L",
3733 &lvalue->where);
3734 return false;
3738 if (rvalue->rank != 0 && lvalue->rank != rvalue->rank)
3740 gfc_error ("Incompatible ranks %d and %d in assignment at %L",
3741 lvalue->rank, rvalue->rank, &lvalue->where);
3742 return false;
3745 if (lvalue->ts.type == BT_UNKNOWN)
3747 gfc_error ("Variable type is UNKNOWN in assignment at %L",
3748 &lvalue->where);
3749 return false;
3752 if (rvalue->expr_type == EXPR_NULL)
3754 if (has_pointer && (ref == NULL || ref->next == NULL)
3755 && lvalue->symtree->n.sym->attr.data)
3756 return true;
3757 else
3759 gfc_error ("NULL appears on right-hand side in assignment at %L",
3760 &rvalue->where);
3761 return false;
3765 /* This is possibly a typo: x = f() instead of x => f(). */
3766 if (warn_surprising
3767 && rvalue->expr_type == EXPR_FUNCTION && gfc_expr_attr (rvalue).pointer)
3768 gfc_warning (OPT_Wsurprising,
3769 "POINTER-valued function appears on right-hand side of "
3770 "assignment at %L", &rvalue->where);
3772 /* Check size of array assignments. */
3773 if (lvalue->rank != 0 && rvalue->rank != 0
3774 && !gfc_check_conformance (lvalue, rvalue, _("array assignment")))
3775 return false;
3777 /* Handle the case of a BOZ literal on the RHS. */
3778 if (rvalue->ts.type == BT_BOZ)
3780 if (lvalue->symtree->n.sym->attr.data)
3782 if (lvalue->ts.type == BT_INTEGER
3783 && gfc_boz2int (rvalue, lvalue->ts.kind))
3784 return true;
3786 if (lvalue->ts.type == BT_REAL
3787 && gfc_boz2real (rvalue, lvalue->ts.kind))
3789 if (gfc_invalid_boz ("BOZ literal constant near %L cannot "
3790 "be assigned to a REAL variable",
3791 &rvalue->where))
3792 return false;
3793 return true;
3797 if (!lvalue->symtree->n.sym->attr.data
3798 && gfc_invalid_boz ("BOZ literal constant at %L is neither a "
3799 "data-stmt-constant nor an actual argument to "
3800 "INT, REAL, DBLE, or CMPLX intrinsic function",
3801 &rvalue->where))
3802 return false;
3804 if (lvalue->ts.type == BT_INTEGER
3805 && gfc_boz2int (rvalue, lvalue->ts.kind))
3806 return true;
3808 if (lvalue->ts.type == BT_REAL
3809 && gfc_boz2real (rvalue, lvalue->ts.kind))
3810 return true;
3812 gfc_error ("BOZ literal constant near %L cannot be assigned to a "
3813 "%qs variable", &rvalue->where, gfc_typename (lvalue));
3814 return false;
3817 if (gfc_expr_attr (lvalue).pdt_kind || gfc_expr_attr (lvalue).pdt_len)
3819 gfc_error ("The assignment to a KIND or LEN component of a "
3820 "parameterized type at %L is not allowed",
3821 &lvalue->where);
3822 return false;
3825 if (gfc_compare_types (&lvalue->ts, &rvalue->ts))
3826 return true;
3828 /* Only DATA Statements come here. */
3829 if (!conform)
3831 locus *where;
3833 /* Numeric can be converted to any other numeric. And Hollerith can be
3834 converted to any other type. */
3835 if ((gfc_numeric_ts (&lvalue->ts) && gfc_numeric_ts (&rvalue->ts))
3836 || rvalue->ts.type == BT_HOLLERITH)
3837 return true;
3839 if (flag_dec_char_conversions && (gfc_numeric_ts (&lvalue->ts)
3840 || lvalue->ts.type == BT_LOGICAL)
3841 && rvalue->ts.type == BT_CHARACTER
3842 && rvalue->ts.kind == gfc_default_character_kind)
3843 return true;
3845 if (lvalue->ts.type == BT_LOGICAL && rvalue->ts.type == BT_LOGICAL)
3846 return true;
3848 where = lvalue->where.lb ? &lvalue->where : &rvalue->where;
3849 gfc_error ("Incompatible types in DATA statement at %L; attempted "
3850 "conversion of %s to %s", where,
3851 gfc_typename (rvalue), gfc_typename (lvalue));
3853 return false;
3856 /* Assignment is the only case where character variables of different
3857 kind values can be converted into one another. */
3858 if (lvalue->ts.type == BT_CHARACTER && rvalue->ts.type == BT_CHARACTER)
3860 if (lvalue->ts.kind != rvalue->ts.kind && allow_convert)
3861 return gfc_convert_chartype (rvalue, &lvalue->ts);
3862 else
3863 return true;
3866 if (!allow_convert)
3867 return true;
3869 return gfc_convert_type (rvalue, &lvalue->ts, 1);
3873 /* Check that a pointer assignment is OK. We first check lvalue, and
3874 we only check rvalue if it's not an assignment to NULL() or a
3875 NULLIFY statement. */
3877 bool
3878 gfc_check_pointer_assign (gfc_expr *lvalue, gfc_expr *rvalue,
3879 bool suppress_type_test, bool is_init_expr)
3881 symbol_attribute attr, lhs_attr;
3882 gfc_ref *ref;
3883 bool is_pure, is_implicit_pure, rank_remap;
3884 int proc_pointer;
3885 bool same_rank;
3887 if (!lvalue->symtree)
3888 return false;
3890 lhs_attr = gfc_expr_attr (lvalue);
3891 if (lvalue->ts.type == BT_UNKNOWN && !lhs_attr.proc_pointer)
3893 gfc_error ("Pointer assignment target is not a POINTER at %L",
3894 &lvalue->where);
3895 return false;
3898 if (lhs_attr.flavor == FL_PROCEDURE && lhs_attr.use_assoc
3899 && !lhs_attr.proc_pointer)
3901 gfc_error ("%qs in the pointer assignment at %L cannot be an "
3902 "l-value since it is a procedure",
3903 lvalue->symtree->n.sym->name, &lvalue->where);
3904 return false;
3907 proc_pointer = lvalue->symtree->n.sym->attr.proc_pointer;
3909 rank_remap = false;
3910 same_rank = lvalue->rank == rvalue->rank;
3911 for (ref = lvalue->ref; ref; ref = ref->next)
3913 if (ref->type == REF_COMPONENT)
3914 proc_pointer = ref->u.c.component->attr.proc_pointer;
3916 if (ref->type == REF_ARRAY && ref->next == NULL)
3918 int dim;
3920 if (ref->u.ar.type == AR_FULL)
3921 break;
3923 if (ref->u.ar.type != AR_SECTION)
3925 gfc_error ("Expected bounds specification for %qs at %L",
3926 lvalue->symtree->n.sym->name, &lvalue->where);
3927 return false;
3930 if (!gfc_notify_std (GFC_STD_F2003, "Bounds specification "
3931 "for %qs in pointer assignment at %L",
3932 lvalue->symtree->n.sym->name, &lvalue->where))
3933 return false;
3935 /* Fortran standard (e.g. F2018, 10.2.2 Pointer assignment):
3937 * (C1017) If bounds-spec-list is specified, the number of
3938 * bounds-specs shall equal the rank of data-pointer-object.
3940 * If bounds-spec-list appears, it specifies the lower bounds.
3942 * (C1018) If bounds-remapping-list is specified, the number of
3943 * bounds-remappings shall equal the rank of data-pointer-object.
3945 * If bounds-remapping-list appears, it specifies the upper and
3946 * lower bounds of each dimension of the pointer; the pointer target
3947 * shall be simply contiguous or of rank one.
3949 * (C1019) If bounds-remapping-list is not specified, the ranks of
3950 * data-pointer-object and data-target shall be the same.
3952 * Thus when bounds are given, all lbounds are necessary and either
3953 * all or none of the upper bounds; no strides are allowed. If the
3954 * upper bounds are present, we may do rank remapping. */
3955 for (dim = 0; dim < ref->u.ar.dimen; ++dim)
3957 if (ref->u.ar.stride[dim])
3959 gfc_error ("Stride must not be present at %L",
3960 &lvalue->where);
3961 return false;
3963 if (!same_rank && (!ref->u.ar.start[dim] ||!ref->u.ar.end[dim]))
3965 gfc_error ("Rank remapping requires a "
3966 "list of %<lower-bound : upper-bound%> "
3967 "specifications at %L", &lvalue->where);
3968 return false;
3970 if (!ref->u.ar.start[dim]
3971 || ref->u.ar.dimen_type[dim] != DIMEN_RANGE)
3973 gfc_error ("Expected list of %<lower-bound :%> or "
3974 "list of %<lower-bound : upper-bound%> "
3975 "specifications at %L", &lvalue->where);
3976 return false;
3979 if (dim == 0)
3980 rank_remap = (ref->u.ar.end[dim] != NULL);
3981 else
3983 if ((rank_remap && !ref->u.ar.end[dim]))
3985 gfc_error ("Rank remapping requires a "
3986 "list of %<lower-bound : upper-bound%> "
3987 "specifications at %L", &lvalue->where);
3988 return false;
3990 if (!rank_remap && ref->u.ar.end[dim])
3992 gfc_error ("Expected list of %<lower-bound :%> or "
3993 "list of %<lower-bound : upper-bound%> "
3994 "specifications at %L", &lvalue->where);
3995 return false;
4002 is_pure = gfc_pure (NULL);
4003 is_implicit_pure = gfc_implicit_pure (NULL);
4005 /* If rvalue is a NULL() or NULLIFY, we're done. Otherwise the type,
4006 kind, etc for lvalue and rvalue must match, and rvalue must be a
4007 pure variable if we're in a pure function. */
4008 if (rvalue->expr_type == EXPR_NULL && rvalue->ts.type == BT_UNKNOWN)
4009 return true;
4011 /* F2008, C723 (pointer) and C726 (proc-pointer); for PURE also C1283. */
4012 if (lvalue->expr_type == EXPR_VARIABLE
4013 && gfc_is_coindexed (lvalue))
4015 gfc_ref *ref;
4016 for (ref = lvalue->ref; ref; ref = ref->next)
4017 if (ref->type == REF_ARRAY && ref->u.ar.codimen)
4019 gfc_error ("Pointer object at %L shall not have a coindex",
4020 &lvalue->where);
4021 return false;
4025 /* Checks on rvalue for procedure pointer assignments. */
4026 if (proc_pointer)
4028 char err[200];
4029 gfc_symbol *s1,*s2;
4030 gfc_component *comp1, *comp2;
4031 const char *name;
4033 attr = gfc_expr_attr (rvalue);
4034 if (!((rvalue->expr_type == EXPR_NULL)
4035 || (rvalue->expr_type == EXPR_FUNCTION && attr.proc_pointer)
4036 || (rvalue->expr_type == EXPR_VARIABLE && attr.proc_pointer)
4037 || (rvalue->expr_type == EXPR_VARIABLE
4038 && attr.flavor == FL_PROCEDURE)))
4040 gfc_error ("Invalid procedure pointer assignment at %L",
4041 &rvalue->where);
4042 return false;
4045 if (rvalue->expr_type == EXPR_VARIABLE && !attr.proc_pointer)
4047 /* Check for intrinsics. */
4048 gfc_symbol *sym = rvalue->symtree->n.sym;
4049 if (!sym->attr.intrinsic
4050 && (gfc_is_intrinsic (sym, 0, sym->declared_at)
4051 || gfc_is_intrinsic (sym, 1, sym->declared_at)))
4053 sym->attr.intrinsic = 1;
4054 gfc_resolve_intrinsic (sym, &rvalue->where);
4055 attr = gfc_expr_attr (rvalue);
4057 /* Check for result of embracing function. */
4058 if (sym->attr.function && sym->result == sym)
4060 gfc_namespace *ns;
4062 for (ns = gfc_current_ns; ns; ns = ns->parent)
4063 if (sym == ns->proc_name)
4065 gfc_error ("Function result %qs is invalid as proc-target "
4066 "in procedure pointer assignment at %L",
4067 sym->name, &rvalue->where);
4068 return false;
4072 if (attr.abstract)
4074 gfc_error ("Abstract interface %qs is invalid "
4075 "in procedure pointer assignment at %L",
4076 rvalue->symtree->name, &rvalue->where);
4077 return false;
4079 /* Check for F08:C729. */
4080 if (attr.flavor == FL_PROCEDURE)
4082 if (attr.proc == PROC_ST_FUNCTION)
4084 gfc_error ("Statement function %qs is invalid "
4085 "in procedure pointer assignment at %L",
4086 rvalue->symtree->name, &rvalue->where);
4087 return false;
4089 if (attr.proc == PROC_INTERNAL &&
4090 !gfc_notify_std(GFC_STD_F2008, "Internal procedure %qs "
4091 "is invalid in procedure pointer assignment "
4092 "at %L", rvalue->symtree->name, &rvalue->where))
4093 return false;
4094 if (attr.intrinsic && gfc_intrinsic_actual_ok (rvalue->symtree->name,
4095 attr.subroutine) == 0)
4097 gfc_error ("Intrinsic %qs at %L is invalid in procedure pointer "
4098 "assignment", rvalue->symtree->name, &rvalue->where);
4099 return false;
4102 /* Check for F08:C730. */
4103 if (attr.elemental && !attr.intrinsic)
4105 gfc_error ("Nonintrinsic elemental procedure %qs is invalid "
4106 "in procedure pointer assignment at %L",
4107 rvalue->symtree->name, &rvalue->where);
4108 return false;
4111 /* Ensure that the calling convention is the same. As other attributes
4112 such as DLLEXPORT may differ, one explicitly only tests for the
4113 calling conventions. */
4114 if (rvalue->expr_type == EXPR_VARIABLE
4115 && lvalue->symtree->n.sym->attr.ext_attr
4116 != rvalue->symtree->n.sym->attr.ext_attr)
4118 symbol_attribute calls;
4120 calls.ext_attr = 0;
4121 gfc_add_ext_attribute (&calls, EXT_ATTR_CDECL, NULL);
4122 gfc_add_ext_attribute (&calls, EXT_ATTR_STDCALL, NULL);
4123 gfc_add_ext_attribute (&calls, EXT_ATTR_FASTCALL, NULL);
4125 if ((calls.ext_attr & lvalue->symtree->n.sym->attr.ext_attr)
4126 != (calls.ext_attr & rvalue->symtree->n.sym->attr.ext_attr))
4128 gfc_error ("Mismatch in the procedure pointer assignment "
4129 "at %L: mismatch in the calling convention",
4130 &rvalue->where);
4131 return false;
4135 comp1 = gfc_get_proc_ptr_comp (lvalue);
4136 if (comp1)
4137 s1 = comp1->ts.interface;
4138 else
4140 s1 = lvalue->symtree->n.sym;
4141 if (s1->ts.interface)
4142 s1 = s1->ts.interface;
4145 comp2 = gfc_get_proc_ptr_comp (rvalue);
4146 if (comp2)
4148 if (rvalue->expr_type == EXPR_FUNCTION)
4150 s2 = comp2->ts.interface->result;
4151 name = s2->name;
4153 else
4155 s2 = comp2->ts.interface;
4156 name = comp2->name;
4159 else if (rvalue->expr_type == EXPR_FUNCTION)
4161 if (rvalue->value.function.esym)
4162 s2 = rvalue->value.function.esym->result;
4163 else
4164 s2 = rvalue->symtree->n.sym->result;
4166 name = s2->name;
4168 else
4170 s2 = rvalue->symtree->n.sym;
4171 name = s2->name;
4174 if (s2 && s2->attr.proc_pointer && s2->ts.interface)
4175 s2 = s2->ts.interface;
4177 /* Special check for the case of absent interface on the lvalue.
4178 * All other interface checks are done below. */
4179 if (!s1 && comp1 && comp1->attr.subroutine && s2 && s2->attr.function)
4181 gfc_error ("Interface mismatch in procedure pointer assignment "
4182 "at %L: %qs is not a subroutine", &rvalue->where, name);
4183 return false;
4186 /* F08:7.2.2.4 (4) */
4187 if (s2 && gfc_explicit_interface_required (s2, err, sizeof(err)))
4189 if (comp1 && !s1)
4191 gfc_error ("Explicit interface required for component %qs at %L: %s",
4192 comp1->name, &lvalue->where, err);
4193 return false;
4195 else if (s1->attr.if_source == IFSRC_UNKNOWN)
4197 gfc_error ("Explicit interface required for %qs at %L: %s",
4198 s1->name, &lvalue->where, err);
4199 return false;
4202 if (s1 && gfc_explicit_interface_required (s1, err, sizeof(err)))
4204 if (comp2 && !s2)
4206 gfc_error ("Explicit interface required for component %qs at %L: %s",
4207 comp2->name, &rvalue->where, err);
4208 return false;
4210 else if (s2->attr.if_source == IFSRC_UNKNOWN)
4212 gfc_error ("Explicit interface required for %qs at %L: %s",
4213 s2->name, &rvalue->where, err);
4214 return false;
4218 if (s1 == s2 || !s1 || !s2)
4219 return true;
4221 if (!gfc_compare_interfaces (s1, s2, name, 0, 1,
4222 err, sizeof(err), NULL, NULL))
4224 gfc_error ("Interface mismatch in procedure pointer assignment "
4225 "at %L: %s", &rvalue->where, err);
4226 return false;
4229 /* Check F2008Cor2, C729. */
4230 if (!s2->attr.intrinsic && s2->attr.if_source == IFSRC_UNKNOWN
4231 && !s2->attr.external && !s2->attr.subroutine && !s2->attr.function)
4233 gfc_error ("Procedure pointer target %qs at %L must be either an "
4234 "intrinsic, host or use associated, referenced or have "
4235 "the EXTERNAL attribute", s2->name, &rvalue->where);
4236 return false;
4239 return true;
4241 else
4243 /* A non-proc pointer cannot point to a constant. */
4244 if (rvalue->expr_type == EXPR_CONSTANT)
4246 gfc_error_now ("Pointer assignment target cannot be a constant at %L",
4247 &rvalue->where);
4248 return false;
4252 if (!gfc_compare_types (&lvalue->ts, &rvalue->ts))
4254 /* Check for F03:C717. */
4255 if (UNLIMITED_POLY (rvalue)
4256 && !(UNLIMITED_POLY (lvalue)
4257 || (lvalue->ts.type == BT_DERIVED
4258 && (lvalue->ts.u.derived->attr.is_bind_c
4259 || lvalue->ts.u.derived->attr.sequence))))
4260 gfc_error ("Data-pointer-object at %L must be unlimited "
4261 "polymorphic, or of a type with the BIND or SEQUENCE "
4262 "attribute, to be compatible with an unlimited "
4263 "polymorphic target", &lvalue->where);
4264 else if (!suppress_type_test)
4265 gfc_error ("Different types in pointer assignment at %L; "
4266 "attempted assignment of %s to %s", &lvalue->where,
4267 gfc_typename (rvalue), gfc_typename (lvalue));
4268 return false;
4271 if (lvalue->ts.type != BT_CLASS && lvalue->ts.kind != rvalue->ts.kind)
4273 gfc_error ("Different kind type parameters in pointer "
4274 "assignment at %L", &lvalue->where);
4275 return false;
4278 if (lvalue->rank != rvalue->rank && !rank_remap)
4280 gfc_error ("Different ranks in pointer assignment at %L", &lvalue->where);
4281 return false;
4284 /* Make sure the vtab is present. */
4285 if (lvalue->ts.type == BT_CLASS && !UNLIMITED_POLY (rvalue))
4286 gfc_find_vtab (&rvalue->ts);
4288 /* Check rank remapping. */
4289 if (rank_remap)
4291 mpz_t lsize, rsize;
4293 /* If this can be determined, check that the target must be at least as
4294 large as the pointer assigned to it is. */
4295 if (gfc_array_size (lvalue, &lsize)
4296 && gfc_array_size (rvalue, &rsize)
4297 && mpz_cmp (rsize, lsize) < 0)
4299 gfc_error ("Rank remapping target is smaller than size of the"
4300 " pointer (%ld < %ld) at %L",
4301 mpz_get_si (rsize), mpz_get_si (lsize),
4302 &lvalue->where);
4303 return false;
4306 /* The target must be either rank one or it must be simply contiguous
4307 and F2008 must be allowed. */
4308 if (rvalue->rank != 1)
4310 if (!gfc_is_simply_contiguous (rvalue, true, false))
4312 gfc_error ("Rank remapping target must be rank 1 or"
4313 " simply contiguous at %L", &rvalue->where);
4314 return false;
4316 if (!gfc_notify_std (GFC_STD_F2008, "Rank remapping target is not "
4317 "rank 1 at %L", &rvalue->where))
4318 return false;
4322 /* Now punt if we are dealing with a NULLIFY(X) or X = NULL(X). */
4323 if (rvalue->expr_type == EXPR_NULL)
4324 return true;
4326 if (rvalue->expr_type == EXPR_VARIABLE && is_subref_array (rvalue))
4327 lvalue->symtree->n.sym->attr.subref_array_pointer = 1;
4329 attr = gfc_expr_attr (rvalue);
4331 if (rvalue->expr_type == EXPR_FUNCTION && !attr.pointer)
4333 /* F2008, C725. For PURE also C1283. Sometimes rvalue is a function call
4334 to caf_get. Map this to the same error message as below when it is
4335 still a variable expression. */
4336 if (rvalue->value.function.isym
4337 && rvalue->value.function.isym->id == GFC_ISYM_CAF_GET)
4338 /* The test above might need to be extend when F08, Note 5.4 has to be
4339 interpreted in the way that target and pointer with the same coindex
4340 are allowed. */
4341 gfc_error ("Data target at %L shall not have a coindex",
4342 &rvalue->where);
4343 else
4344 gfc_error ("Target expression in pointer assignment "
4345 "at %L must deliver a pointer result",
4346 &rvalue->where);
4347 return false;
4350 if (is_init_expr)
4352 gfc_symbol *sym;
4353 bool target;
4354 gfc_ref *ref;
4356 if (gfc_is_size_zero_array (rvalue))
4358 gfc_error ("Zero-sized array detected at %L where an entity with "
4359 "the TARGET attribute is expected", &rvalue->where);
4360 return false;
4362 else if (!rvalue->symtree)
4364 gfc_error ("Pointer assignment target in initialization expression "
4365 "does not have the TARGET attribute at %L",
4366 &rvalue->where);
4367 return false;
4370 sym = rvalue->symtree->n.sym;
4372 if (sym->ts.type == BT_CLASS && sym->attr.class_ok)
4373 target = CLASS_DATA (sym)->attr.target;
4374 else
4375 target = sym->attr.target;
4377 if (!target && !proc_pointer)
4379 gfc_error ("Pointer assignment target in initialization expression "
4380 "does not have the TARGET attribute at %L",
4381 &rvalue->where);
4382 return false;
4385 for (ref = rvalue->ref; ref; ref = ref->next)
4387 switch (ref->type)
4389 case REF_ARRAY:
4390 for (int n = 0; n < ref->u.ar.dimen; n++)
4391 if (!gfc_is_constant_expr (ref->u.ar.start[n])
4392 || !gfc_is_constant_expr (ref->u.ar.end[n])
4393 || !gfc_is_constant_expr (ref->u.ar.stride[n]))
4395 gfc_error ("Every subscript of target specification "
4396 "at %L must be a constant expression",
4397 &ref->u.ar.where);
4398 return false;
4400 break;
4402 case REF_SUBSTRING:
4403 if (!gfc_is_constant_expr (ref->u.ss.start)
4404 || !gfc_is_constant_expr (ref->u.ss.end))
4406 gfc_error ("Substring starting and ending points of target "
4407 "specification at %L must be constant expressions",
4408 &ref->u.ss.start->where);
4409 return false;
4411 break;
4413 default:
4414 break;
4418 else
4420 if (!attr.target && !attr.pointer)
4422 gfc_error ("Pointer assignment target is neither TARGET "
4423 "nor POINTER at %L", &rvalue->where);
4424 return false;
4428 if (lvalue->ts.type == BT_CHARACTER)
4430 bool t = gfc_check_same_strlen (lvalue, rvalue, "pointer assignment");
4431 if (!t)
4432 return false;
4435 if (is_pure && gfc_impure_variable (rvalue->symtree->n.sym))
4437 gfc_error ("Bad target in pointer assignment in PURE "
4438 "procedure at %L", &rvalue->where);
4441 if (is_implicit_pure && gfc_impure_variable (rvalue->symtree->n.sym))
4442 gfc_unset_implicit_pure (gfc_current_ns->proc_name);
4444 if (gfc_has_vector_index (rvalue))
4446 gfc_error ("Pointer assignment with vector subscript "
4447 "on rhs at %L", &rvalue->where);
4448 return false;
4451 if (attr.is_protected && attr.use_assoc
4452 && !(attr.pointer || attr.proc_pointer))
4454 gfc_error ("Pointer assignment target has PROTECTED "
4455 "attribute at %L", &rvalue->where);
4456 return false;
4459 /* F2008, C725. For PURE also C1283. */
4460 if (rvalue->expr_type == EXPR_VARIABLE
4461 && gfc_is_coindexed (rvalue))
4463 gfc_ref *ref;
4464 for (ref = rvalue->ref; ref; ref = ref->next)
4465 if (ref->type == REF_ARRAY && ref->u.ar.codimen)
4467 gfc_error ("Data target at %L shall not have a coindex",
4468 &rvalue->where);
4469 return false;
4473 /* Warn for assignments of contiguous pointers to targets which is not
4474 contiguous. Be lenient in the definition of what counts as
4475 contiguous. */
4477 if (lhs_attr.contiguous
4478 && lhs_attr.dimension > 0)
4480 if (gfc_is_not_contiguous (rvalue))
4482 gfc_error ("Assignment to contiguous pointer from "
4483 "non-contiguous target at %L", &rvalue->where);
4484 return false;
4486 if (!gfc_is_simply_contiguous (rvalue, false, true))
4487 gfc_warning (OPT_Wextra, "Assignment to contiguous pointer from "
4488 "non-contiguous target at %L", &rvalue->where);
4491 /* Warn if it is the LHS pointer may lives longer than the RHS target. */
4492 if (warn_target_lifetime
4493 && rvalue->expr_type == EXPR_VARIABLE
4494 && !rvalue->symtree->n.sym->attr.save
4495 && !rvalue->symtree->n.sym->attr.pointer && !attr.pointer
4496 && !rvalue->symtree->n.sym->attr.host_assoc
4497 && !rvalue->symtree->n.sym->attr.in_common
4498 && !rvalue->symtree->n.sym->attr.use_assoc
4499 && !rvalue->symtree->n.sym->attr.dummy)
4501 bool warn;
4502 gfc_namespace *ns;
4504 warn = lvalue->symtree->n.sym->attr.dummy
4505 || lvalue->symtree->n.sym->attr.result
4506 || lvalue->symtree->n.sym->attr.function
4507 || (lvalue->symtree->n.sym->attr.host_assoc
4508 && lvalue->symtree->n.sym->ns
4509 != rvalue->symtree->n.sym->ns)
4510 || lvalue->symtree->n.sym->attr.use_assoc
4511 || lvalue->symtree->n.sym->attr.in_common;
4513 if (rvalue->symtree->n.sym->ns->proc_name
4514 && rvalue->symtree->n.sym->ns->proc_name->attr.flavor != FL_PROCEDURE
4515 && rvalue->symtree->n.sym->ns->proc_name->attr.flavor != FL_PROGRAM)
4516 for (ns = rvalue->symtree->n.sym->ns;
4517 ns && ns->proc_name && ns->proc_name->attr.flavor != FL_PROCEDURE;
4518 ns = ns->parent)
4519 if (ns->parent == lvalue->symtree->n.sym->ns)
4521 warn = true;
4522 break;
4525 if (warn)
4526 gfc_warning (OPT_Wtarget_lifetime,
4527 "Pointer at %L in pointer assignment might outlive the "
4528 "pointer target", &lvalue->where);
4531 return true;
4535 /* Relative of gfc_check_assign() except that the lvalue is a single
4536 symbol. Used for initialization assignments. */
4538 bool
4539 gfc_check_assign_symbol (gfc_symbol *sym, gfc_component *comp, gfc_expr *rvalue)
4541 gfc_expr lvalue;
4542 bool r;
4543 bool pointer, proc_pointer;
4545 memset (&lvalue, '\0', sizeof (gfc_expr));
4547 lvalue.expr_type = EXPR_VARIABLE;
4548 lvalue.ts = sym->ts;
4549 if (sym->as)
4550 lvalue.rank = sym->as->rank;
4551 lvalue.symtree = XCNEW (gfc_symtree);
4552 lvalue.symtree->n.sym = sym;
4553 lvalue.where = sym->declared_at;
4555 if (comp)
4557 lvalue.ref = gfc_get_ref ();
4558 lvalue.ref->type = REF_COMPONENT;
4559 lvalue.ref->u.c.component = comp;
4560 lvalue.ref->u.c.sym = sym;
4561 lvalue.ts = comp->ts;
4562 lvalue.rank = comp->as ? comp->as->rank : 0;
4563 lvalue.where = comp->loc;
4564 pointer = comp->ts.type == BT_CLASS && CLASS_DATA (comp)
4565 ? CLASS_DATA (comp)->attr.class_pointer : comp->attr.pointer;
4566 proc_pointer = comp->attr.proc_pointer;
4568 else
4570 pointer = sym->ts.type == BT_CLASS && CLASS_DATA (sym)
4571 ? CLASS_DATA (sym)->attr.class_pointer : sym->attr.pointer;
4572 proc_pointer = sym->attr.proc_pointer;
4575 if (pointer || proc_pointer)
4576 r = gfc_check_pointer_assign (&lvalue, rvalue, false, true);
4577 else
4579 /* If a conversion function, e.g., __convert_i8_i4, was inserted
4580 into an array constructor, we should check if it can be reduced
4581 as an initialization expression. */
4582 if (rvalue->expr_type == EXPR_FUNCTION
4583 && rvalue->value.function.isym
4584 && (rvalue->value.function.isym->conversion == 1))
4585 gfc_check_init_expr (rvalue);
4587 r = gfc_check_assign (&lvalue, rvalue, 1);
4590 free (lvalue.symtree);
4591 free (lvalue.ref);
4593 if (!r)
4594 return r;
4596 if (pointer && rvalue->expr_type != EXPR_NULL && !proc_pointer)
4598 /* F08:C461. Additional checks for pointer initialization. */
4599 symbol_attribute attr;
4600 attr = gfc_expr_attr (rvalue);
4601 if (attr.allocatable)
4603 gfc_error ("Pointer initialization target at %L "
4604 "must not be ALLOCATABLE", &rvalue->where);
4605 return false;
4607 if (!attr.target || attr.pointer)
4609 gfc_error ("Pointer initialization target at %L "
4610 "must have the TARGET attribute", &rvalue->where);
4611 return false;
4614 if (!attr.save && rvalue->expr_type == EXPR_VARIABLE
4615 && rvalue->symtree->n.sym->ns->proc_name
4616 && rvalue->symtree->n.sym->ns->proc_name->attr.is_main_program)
4618 rvalue->symtree->n.sym->ns->proc_name->attr.save = SAVE_IMPLICIT;
4619 attr.save = SAVE_IMPLICIT;
4622 if (!attr.save)
4624 gfc_error ("Pointer initialization target at %L "
4625 "must have the SAVE attribute", &rvalue->where);
4626 return false;
4630 if (proc_pointer && rvalue->expr_type != EXPR_NULL)
4632 /* F08:C1220. Additional checks for procedure pointer initialization. */
4633 symbol_attribute attr = gfc_expr_attr (rvalue);
4634 if (attr.proc_pointer)
4636 gfc_error ("Procedure pointer initialization target at %L "
4637 "may not be a procedure pointer", &rvalue->where);
4638 return false;
4640 if (attr.proc == PROC_INTERNAL)
4642 gfc_error ("Internal procedure %qs is invalid in "
4643 "procedure pointer initialization at %L",
4644 rvalue->symtree->name, &rvalue->where);
4645 return false;
4647 if (attr.dummy)
4649 gfc_error ("Dummy procedure %qs is invalid in "
4650 "procedure pointer initialization at %L",
4651 rvalue->symtree->name, &rvalue->where);
4652 return false;
4656 return true;
4659 /* Build an initializer for a local integer, real, complex, logical, or
4660 character variable, based on the command line flags finit-local-zero,
4661 finit-integer=, finit-real=, finit-logical=, and finit-character=.
4662 With force, an initializer is ALWAYS generated. */
4664 static gfc_expr *
4665 gfc_build_init_expr (gfc_typespec *ts, locus *where, bool force)
4667 gfc_expr *init_expr;
4669 /* Try to build an initializer expression. */
4670 init_expr = gfc_get_constant_expr (ts->type, ts->kind, where);
4672 /* If we want to force generation, make sure we default to zero. */
4673 gfc_init_local_real init_real = flag_init_real;
4674 int init_logical = gfc_option.flag_init_logical;
4675 if (force)
4677 if (init_real == GFC_INIT_REAL_OFF)
4678 init_real = GFC_INIT_REAL_ZERO;
4679 if (init_logical == GFC_INIT_LOGICAL_OFF)
4680 init_logical = GFC_INIT_LOGICAL_FALSE;
4683 /* We will only initialize integers, reals, complex, logicals, and
4684 characters, and only if the corresponding command-line flags
4685 were set. Otherwise, we free init_expr and return null. */
4686 switch (ts->type)
4688 case BT_INTEGER:
4689 if (force || gfc_option.flag_init_integer != GFC_INIT_INTEGER_OFF)
4690 mpz_set_si (init_expr->value.integer,
4691 gfc_option.flag_init_integer_value);
4692 else
4694 gfc_free_expr (init_expr);
4695 init_expr = NULL;
4697 break;
4699 case BT_REAL:
4700 switch (init_real)
4702 case GFC_INIT_REAL_SNAN:
4703 init_expr->is_snan = 1;
4704 /* Fall through. */
4705 case GFC_INIT_REAL_NAN:
4706 mpfr_set_nan (init_expr->value.real);
4707 break;
4709 case GFC_INIT_REAL_INF:
4710 mpfr_set_inf (init_expr->value.real, 1);
4711 break;
4713 case GFC_INIT_REAL_NEG_INF:
4714 mpfr_set_inf (init_expr->value.real, -1);
4715 break;
4717 case GFC_INIT_REAL_ZERO:
4718 mpfr_set_ui (init_expr->value.real, 0.0, GFC_RND_MODE);
4719 break;
4721 default:
4722 gfc_free_expr (init_expr);
4723 init_expr = NULL;
4724 break;
4726 break;
4728 case BT_COMPLEX:
4729 switch (init_real)
4731 case GFC_INIT_REAL_SNAN:
4732 init_expr->is_snan = 1;
4733 /* Fall through. */
4734 case GFC_INIT_REAL_NAN:
4735 mpfr_set_nan (mpc_realref (init_expr->value.complex));
4736 mpfr_set_nan (mpc_imagref (init_expr->value.complex));
4737 break;
4739 case GFC_INIT_REAL_INF:
4740 mpfr_set_inf (mpc_realref (init_expr->value.complex), 1);
4741 mpfr_set_inf (mpc_imagref (init_expr->value.complex), 1);
4742 break;
4744 case GFC_INIT_REAL_NEG_INF:
4745 mpfr_set_inf (mpc_realref (init_expr->value.complex), -1);
4746 mpfr_set_inf (mpc_imagref (init_expr->value.complex), -1);
4747 break;
4749 case GFC_INIT_REAL_ZERO:
4750 mpc_set_ui (init_expr->value.complex, 0, GFC_MPC_RND_MODE);
4751 break;
4753 default:
4754 gfc_free_expr (init_expr);
4755 init_expr = NULL;
4756 break;
4758 break;
4760 case BT_LOGICAL:
4761 if (init_logical == GFC_INIT_LOGICAL_FALSE)
4762 init_expr->value.logical = 0;
4763 else if (init_logical == GFC_INIT_LOGICAL_TRUE)
4764 init_expr->value.logical = 1;
4765 else
4767 gfc_free_expr (init_expr);
4768 init_expr = NULL;
4770 break;
4772 case BT_CHARACTER:
4773 /* For characters, the length must be constant in order to
4774 create a default initializer. */
4775 if ((force || gfc_option.flag_init_character == GFC_INIT_CHARACTER_ON)
4776 && ts->u.cl->length
4777 && ts->u.cl->length->expr_type == EXPR_CONSTANT)
4779 HOST_WIDE_INT char_len = gfc_mpz_get_hwi (ts->u.cl->length->value.integer);
4780 init_expr->value.character.length = char_len;
4781 init_expr->value.character.string = gfc_get_wide_string (char_len+1);
4782 for (size_t i = 0; i < (size_t) char_len; i++)
4783 init_expr->value.character.string[i]
4784 = (unsigned char) gfc_option.flag_init_character_value;
4786 else
4788 gfc_free_expr (init_expr);
4789 init_expr = NULL;
4791 if (!init_expr
4792 && (force || gfc_option.flag_init_character == GFC_INIT_CHARACTER_ON)
4793 && ts->u.cl->length && flag_max_stack_var_size != 0)
4795 gfc_actual_arglist *arg;
4796 init_expr = gfc_get_expr ();
4797 init_expr->where = *where;
4798 init_expr->ts = *ts;
4799 init_expr->expr_type = EXPR_FUNCTION;
4800 init_expr->value.function.isym =
4801 gfc_intrinsic_function_by_id (GFC_ISYM_REPEAT);
4802 init_expr->value.function.name = "repeat";
4803 arg = gfc_get_actual_arglist ();
4804 arg->expr = gfc_get_character_expr (ts->kind, where, NULL, 1);
4805 arg->expr->value.character.string[0] =
4806 gfc_option.flag_init_character_value;
4807 arg->next = gfc_get_actual_arglist ();
4808 arg->next->expr = gfc_copy_expr (ts->u.cl->length);
4809 init_expr->value.function.actual = arg;
4811 break;
4813 default:
4814 gfc_free_expr (init_expr);
4815 init_expr = NULL;
4818 return init_expr;
4821 /* Invoke gfc_build_init_expr to create an initializer expression, but do not
4822 * require that an expression be built. */
4824 gfc_expr *
4825 gfc_build_default_init_expr (gfc_typespec *ts, locus *where)
4827 return gfc_build_init_expr (ts, where, false);
4830 /* Apply an initialization expression to a typespec. Can be used for symbols or
4831 components. Similar to add_init_expr_to_sym in decl.cc; could probably be
4832 combined with some effort. */
4834 void
4835 gfc_apply_init (gfc_typespec *ts, symbol_attribute *attr, gfc_expr *init)
4837 if (ts->type == BT_CHARACTER && !attr->pointer && init
4838 && ts->u.cl
4839 && ts->u.cl->length
4840 && ts->u.cl->length->expr_type == EXPR_CONSTANT
4841 && ts->u.cl->length->ts.type == BT_INTEGER)
4843 HOST_WIDE_INT len = gfc_mpz_get_hwi (ts->u.cl->length->value.integer);
4845 if (init->expr_type == EXPR_CONSTANT)
4846 gfc_set_constant_character_len (len, init, -1);
4847 else if (init
4848 && init->ts.type == BT_CHARACTER
4849 && init->ts.u.cl && init->ts.u.cl->length
4850 && mpz_cmp (ts->u.cl->length->value.integer,
4851 init->ts.u.cl->length->value.integer))
4853 gfc_constructor *ctor;
4854 ctor = gfc_constructor_first (init->value.constructor);
4856 if (ctor)
4858 bool has_ts = (init->ts.u.cl
4859 && init->ts.u.cl->length_from_typespec);
4861 /* Remember the length of the first element for checking
4862 that all elements *in the constructor* have the same
4863 length. This need not be the length of the LHS! */
4864 gcc_assert (ctor->expr->expr_type == EXPR_CONSTANT);
4865 gcc_assert (ctor->expr->ts.type == BT_CHARACTER);
4866 gfc_charlen_t first_len = ctor->expr->value.character.length;
4868 for ( ; ctor; ctor = gfc_constructor_next (ctor))
4869 if (ctor->expr->expr_type == EXPR_CONSTANT)
4871 gfc_set_constant_character_len (len, ctor->expr,
4872 has_ts ? -1 : first_len);
4873 if (!ctor->expr->ts.u.cl)
4874 ctor->expr->ts.u.cl
4875 = gfc_new_charlen (gfc_current_ns, ts->u.cl);
4876 else
4877 ctor->expr->ts.u.cl->length
4878 = gfc_copy_expr (ts->u.cl->length);
4886 /* Check whether an expression is a structure constructor and whether it has
4887 other values than NULL. */
4889 static bool
4890 is_non_empty_structure_constructor (gfc_expr * e)
4892 if (e->expr_type != EXPR_STRUCTURE)
4893 return false;
4895 gfc_constructor *cons = gfc_constructor_first (e->value.constructor);
4896 while (cons)
4898 if (!cons->expr || cons->expr->expr_type != EXPR_NULL)
4899 return true;
4900 cons = gfc_constructor_next (cons);
4902 return false;
4906 /* Check for default initializer; sym->value is not enough
4907 as it is also set for EXPR_NULL of allocatables. */
4909 bool
4910 gfc_has_default_initializer (gfc_symbol *der)
4912 gfc_component *c;
4914 gcc_assert (gfc_fl_struct (der->attr.flavor));
4915 for (c = der->components; c; c = c->next)
4916 if (gfc_bt_struct (c->ts.type))
4918 if (!c->attr.pointer && !c->attr.proc_pointer
4919 && !(c->attr.allocatable && der == c->ts.u.derived)
4920 && ((c->initializer
4921 && is_non_empty_structure_constructor (c->initializer))
4922 || gfc_has_default_initializer (c->ts.u.derived)))
4923 return true;
4924 if (c->attr.pointer && c->initializer)
4925 return true;
4927 else
4929 if (c->initializer)
4930 return true;
4933 return false;
4938 Generate an initializer expression which initializes the entirety of a union.
4939 A normal structure constructor is insufficient without undue effort, because
4940 components of maps may be oddly aligned/overlapped. (For example if a
4941 character is initialized from one map overtop a real from the other, only one
4942 byte of the real is actually initialized.) Unfortunately we don't know the
4943 size of the union right now, so we can't generate a proper initializer, but
4944 we use a NULL expr as a placeholder and do the right thing later in
4945 gfc_trans_subcomponent_assign.
4947 static gfc_expr *
4948 generate_union_initializer (gfc_component *un)
4950 if (un == NULL || un->ts.type != BT_UNION)
4951 return NULL;
4953 gfc_expr *placeholder = gfc_get_null_expr (&un->loc);
4954 placeholder->ts = un->ts;
4955 return placeholder;
4959 /* Get the user-specified initializer for a union, if any. This means the user
4960 has said to initialize component(s) of a map. For simplicity's sake we
4961 only allow the user to initialize the first map. We don't have to worry
4962 about overlapping initializers as they are released early in resolution (see
4963 resolve_fl_struct). */
4965 static gfc_expr *
4966 get_union_initializer (gfc_symbol *union_type, gfc_component **map_p)
4968 gfc_component *map;
4969 gfc_expr *init=NULL;
4971 if (!union_type || union_type->attr.flavor != FL_UNION)
4972 return NULL;
4974 for (map = union_type->components; map; map = map->next)
4976 if (gfc_has_default_initializer (map->ts.u.derived))
4978 init = gfc_default_initializer (&map->ts);
4979 if (map_p)
4980 *map_p = map;
4981 break;
4985 if (map_p && !init)
4986 *map_p = NULL;
4988 return init;
4991 static bool
4992 class_allocatable (gfc_component *comp)
4994 return comp->ts.type == BT_CLASS && CLASS_DATA (comp)
4995 && CLASS_DATA (comp)->attr.allocatable;
4998 static bool
4999 class_pointer (gfc_component *comp)
5001 return comp->ts.type == BT_CLASS && CLASS_DATA (comp)
5002 && CLASS_DATA (comp)->attr.pointer;
5005 static bool
5006 comp_allocatable (gfc_component *comp)
5008 return comp->attr.allocatable || class_allocatable (comp);
5011 static bool
5012 comp_pointer (gfc_component *comp)
5014 return comp->attr.pointer
5015 || comp->attr.proc_pointer
5016 || comp->attr.class_pointer
5017 || class_pointer (comp);
5020 /* Fetch or generate an initializer for the given component.
5021 Only generate an initializer if generate is true. */
5023 static gfc_expr *
5024 component_initializer (gfc_component *c, bool generate)
5026 gfc_expr *init = NULL;
5028 /* Allocatable components always get EXPR_NULL.
5029 Pointer components are only initialized when generating, and only if they
5030 do not already have an initializer. */
5031 if (comp_allocatable (c) || (generate && comp_pointer (c) && !c->initializer))
5033 init = gfc_get_null_expr (&c->loc);
5034 init->ts = c->ts;
5035 return init;
5038 /* See if we can find the initializer immediately. */
5039 if (c->initializer || !generate)
5040 return c->initializer;
5042 /* Recursively handle derived type components. */
5043 else if (c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
5044 init = gfc_generate_initializer (&c->ts, true);
5046 else if (c->ts.type == BT_UNION && c->ts.u.derived->components)
5048 gfc_component *map = NULL;
5049 gfc_constructor *ctor;
5050 gfc_expr *user_init;
5052 /* If we don't have a user initializer and we aren't generating one, this
5053 union has no initializer. */
5054 user_init = get_union_initializer (c->ts.u.derived, &map);
5055 if (!user_init && !generate)
5056 return NULL;
5058 /* Otherwise use a structure constructor. */
5059 init = gfc_get_structure_constructor_expr (c->ts.type, c->ts.kind,
5060 &c->loc);
5061 init->ts = c->ts;
5063 /* If we are to generate an initializer for the union, add a constructor
5064 which initializes the whole union first. */
5065 if (generate)
5067 ctor = gfc_constructor_get ();
5068 ctor->expr = generate_union_initializer (c);
5069 gfc_constructor_append (&init->value.constructor, ctor);
5072 /* If we found an initializer in one of our maps, apply it. Note this
5073 is applied _after_ the entire-union initializer above if any. */
5074 if (user_init)
5076 ctor = gfc_constructor_get ();
5077 ctor->expr = user_init;
5078 ctor->n.component = map;
5079 gfc_constructor_append (&init->value.constructor, ctor);
5083 /* Treat simple components like locals. */
5084 else
5086 /* We MUST give an initializer, so force generation. */
5087 init = gfc_build_init_expr (&c->ts, &c->loc, true);
5088 gfc_apply_init (&c->ts, &c->attr, init);
5091 return init;
5095 /* Get an expression for a default initializer of a derived type. */
5097 gfc_expr *
5098 gfc_default_initializer (gfc_typespec *ts)
5100 return gfc_generate_initializer (ts, false);
5103 /* Generate an initializer expression for an iso_c_binding type
5104 such as c_[fun]ptr. The appropriate initializer is c_null_[fun]ptr. */
5106 static gfc_expr *
5107 generate_isocbinding_initializer (gfc_symbol *derived)
5109 /* The initializers have already been built into the c_null_[fun]ptr symbols
5110 from gen_special_c_interop_ptr. */
5111 gfc_symtree *npsym = NULL;
5112 if (0 == strcmp (derived->name, "c_ptr"))
5113 gfc_find_sym_tree ("c_null_ptr", gfc_current_ns, true, &npsym);
5114 else if (0 == strcmp (derived->name, "c_funptr"))
5115 gfc_find_sym_tree ("c_null_funptr", gfc_current_ns, true, &npsym);
5116 else
5117 gfc_internal_error ("generate_isocbinding_initializer(): bad iso_c_binding"
5118 " type, expected %<c_ptr%> or %<c_funptr%>");
5119 if (npsym)
5121 gfc_expr *init = gfc_copy_expr (npsym->n.sym->value);
5122 init->symtree = npsym;
5123 init->ts.is_iso_c = true;
5124 return init;
5127 return NULL;
5130 /* Get or generate an expression for a default initializer of a derived type.
5131 If -finit-derived is specified, generate default initialization expressions
5132 for components that lack them when generate is set. */
5134 gfc_expr *
5135 gfc_generate_initializer (gfc_typespec *ts, bool generate)
5137 gfc_expr *init, *tmp;
5138 gfc_component *comp;
5140 generate = flag_init_derived && generate;
5142 if (ts->u.derived->ts.is_iso_c && generate)
5143 return generate_isocbinding_initializer (ts->u.derived);
5145 /* See if we have a default initializer in this, but not in nested
5146 types (otherwise we could use gfc_has_default_initializer()).
5147 We don't need to check if we are going to generate them. */
5148 comp = ts->u.derived->components;
5149 if (!generate)
5151 for (; comp; comp = comp->next)
5152 if (comp->initializer || comp_allocatable (comp))
5153 break;
5156 if (!comp)
5157 return NULL;
5159 init = gfc_get_structure_constructor_expr (ts->type, ts->kind,
5160 &ts->u.derived->declared_at);
5161 init->ts = *ts;
5163 for (comp = ts->u.derived->components; comp; comp = comp->next)
5165 gfc_constructor *ctor = gfc_constructor_get();
5167 /* Fetch or generate an initializer for the component. */
5168 tmp = component_initializer (comp, generate);
5169 if (tmp)
5171 /* Save the component ref for STRUCTUREs and UNIONs. */
5172 if (ts->u.derived->attr.flavor == FL_STRUCT
5173 || ts->u.derived->attr.flavor == FL_UNION)
5174 ctor->n.component = comp;
5176 /* If the initializer was not generated, we need a copy. */
5177 ctor->expr = comp->initializer ? gfc_copy_expr (tmp) : tmp;
5178 if ((comp->ts.type != tmp->ts.type || comp->ts.kind != tmp->ts.kind)
5179 && !comp->attr.pointer && !comp->attr.proc_pointer)
5181 bool val;
5182 val = gfc_convert_type_warn (ctor->expr, &comp->ts, 1, false);
5183 if (val == false)
5184 return NULL;
5188 gfc_constructor_append (&init->value.constructor, ctor);
5191 return init;
5195 /* Given a symbol, create an expression node with that symbol as a
5196 variable. If the symbol is array valued, setup a reference of the
5197 whole array. */
5199 gfc_expr *
5200 gfc_get_variable_expr (gfc_symtree *var)
5202 gfc_expr *e;
5204 e = gfc_get_expr ();
5205 e->expr_type = EXPR_VARIABLE;
5206 e->symtree = var;
5207 e->ts = var->n.sym->ts;
5209 if (var->n.sym->attr.flavor != FL_PROCEDURE
5210 && ((var->n.sym->as != NULL && var->n.sym->ts.type != BT_CLASS)
5211 || (var->n.sym->ts.type == BT_CLASS && var->n.sym->ts.u.derived
5212 && CLASS_DATA (var->n.sym)
5213 && CLASS_DATA (var->n.sym)->as)))
5215 e->rank = var->n.sym->ts.type == BT_CLASS
5216 ? CLASS_DATA (var->n.sym)->as->rank : var->n.sym->as->rank;
5217 e->ref = gfc_get_ref ();
5218 e->ref->type = REF_ARRAY;
5219 e->ref->u.ar.type = AR_FULL;
5220 e->ref->u.ar.as = gfc_copy_array_spec (var->n.sym->ts.type == BT_CLASS
5221 ? CLASS_DATA (var->n.sym)->as
5222 : var->n.sym->as);
5225 return e;
5229 /* Adds a full array reference to an expression, as needed. */
5231 void
5232 gfc_add_full_array_ref (gfc_expr *e, gfc_array_spec *as)
5234 gfc_ref *ref;
5235 for (ref = e->ref; ref; ref = ref->next)
5236 if (!ref->next)
5237 break;
5238 if (ref)
5240 ref->next = gfc_get_ref ();
5241 ref = ref->next;
5243 else
5245 e->ref = gfc_get_ref ();
5246 ref = e->ref;
5248 ref->type = REF_ARRAY;
5249 ref->u.ar.type = AR_FULL;
5250 ref->u.ar.dimen = e->rank;
5251 ref->u.ar.where = e->where;
5252 ref->u.ar.as = as;
5256 gfc_expr *
5257 gfc_lval_expr_from_sym (gfc_symbol *sym)
5259 gfc_expr *lval;
5260 gfc_array_spec *as;
5261 lval = gfc_get_expr ();
5262 lval->expr_type = EXPR_VARIABLE;
5263 lval->where = sym->declared_at;
5264 lval->ts = sym->ts;
5265 lval->symtree = gfc_find_symtree (sym->ns->sym_root, sym->name);
5267 /* It will always be a full array. */
5268 as = IS_CLASS_ARRAY (sym) ? CLASS_DATA (sym)->as : sym->as;
5269 lval->rank = as ? as->rank : 0;
5270 if (lval->rank)
5271 gfc_add_full_array_ref (lval, as);
5272 return lval;
5276 /* Returns the array_spec of a full array expression. A NULL is
5277 returned otherwise. */
5278 gfc_array_spec *
5279 gfc_get_full_arrayspec_from_expr (gfc_expr *expr)
5281 gfc_array_spec *as;
5282 gfc_ref *ref;
5284 if (expr->rank == 0)
5285 return NULL;
5287 /* Follow any component references. */
5288 if (expr->expr_type == EXPR_VARIABLE
5289 || expr->expr_type == EXPR_CONSTANT)
5291 if (expr->symtree)
5292 as = expr->symtree->n.sym->as;
5293 else
5294 as = NULL;
5296 for (ref = expr->ref; ref; ref = ref->next)
5298 switch (ref->type)
5300 case REF_COMPONENT:
5301 as = ref->u.c.component->as;
5302 continue;
5304 case REF_SUBSTRING:
5305 case REF_INQUIRY:
5306 continue;
5308 case REF_ARRAY:
5310 switch (ref->u.ar.type)
5312 case AR_ELEMENT:
5313 case AR_SECTION:
5314 case AR_UNKNOWN:
5315 as = NULL;
5316 continue;
5318 case AR_FULL:
5319 break;
5321 break;
5326 else
5327 as = NULL;
5329 return as;
5333 /* General expression traversal function. */
5335 bool
5336 gfc_traverse_expr (gfc_expr *expr, gfc_symbol *sym,
5337 bool (*func)(gfc_expr *, gfc_symbol *, int*),
5338 int f)
5340 gfc_array_ref ar;
5341 gfc_ref *ref;
5342 gfc_actual_arglist *args;
5343 gfc_constructor *c;
5344 int i;
5346 if (!expr)
5347 return false;
5349 if ((*func) (expr, sym, &f))
5350 return true;
5352 if (expr->ts.type == BT_CHARACTER
5353 && expr->ts.u.cl
5354 && expr->ts.u.cl->length
5355 && expr->ts.u.cl->length->expr_type != EXPR_CONSTANT
5356 && gfc_traverse_expr (expr->ts.u.cl->length, sym, func, f))
5357 return true;
5359 switch (expr->expr_type)
5361 case EXPR_PPC:
5362 case EXPR_COMPCALL:
5363 case EXPR_FUNCTION:
5364 for (args = expr->value.function.actual; args; args = args->next)
5366 if (gfc_traverse_expr (args->expr, sym, func, f))
5367 return true;
5369 break;
5371 case EXPR_VARIABLE:
5372 case EXPR_CONSTANT:
5373 case EXPR_NULL:
5374 case EXPR_SUBSTRING:
5375 break;
5377 case EXPR_STRUCTURE:
5378 case EXPR_ARRAY:
5379 for (c = gfc_constructor_first (expr->value.constructor);
5380 c; c = gfc_constructor_next (c))
5382 if (gfc_traverse_expr (c->expr, sym, func, f))
5383 return true;
5384 if (c->iterator)
5386 if (gfc_traverse_expr (c->iterator->var, sym, func, f))
5387 return true;
5388 if (gfc_traverse_expr (c->iterator->start, sym, func, f))
5389 return true;
5390 if (gfc_traverse_expr (c->iterator->end, sym, func, f))
5391 return true;
5392 if (gfc_traverse_expr (c->iterator->step, sym, func, f))
5393 return true;
5396 break;
5398 case EXPR_OP:
5399 if (gfc_traverse_expr (expr->value.op.op1, sym, func, f))
5400 return true;
5401 if (gfc_traverse_expr (expr->value.op.op2, sym, func, f))
5402 return true;
5403 break;
5405 default:
5406 gcc_unreachable ();
5407 break;
5410 ref = expr->ref;
5411 while (ref != NULL)
5413 switch (ref->type)
5415 case REF_ARRAY:
5416 ar = ref->u.ar;
5417 for (i = 0; i < GFC_MAX_DIMENSIONS; i++)
5419 if (gfc_traverse_expr (ar.start[i], sym, func, f))
5420 return true;
5421 if (gfc_traverse_expr (ar.end[i], sym, func, f))
5422 return true;
5423 if (gfc_traverse_expr (ar.stride[i], sym, func, f))
5424 return true;
5426 break;
5428 case REF_SUBSTRING:
5429 if (gfc_traverse_expr (ref->u.ss.start, sym, func, f))
5430 return true;
5431 if (gfc_traverse_expr (ref->u.ss.end, sym, func, f))
5432 return true;
5433 break;
5435 case REF_COMPONENT:
5436 if (ref->u.c.component->ts.type == BT_CHARACTER
5437 && ref->u.c.component->ts.u.cl
5438 && ref->u.c.component->ts.u.cl->length
5439 && ref->u.c.component->ts.u.cl->length->expr_type
5440 != EXPR_CONSTANT
5441 && gfc_traverse_expr (ref->u.c.component->ts.u.cl->length,
5442 sym, func, f))
5443 return true;
5445 if (ref->u.c.component->as)
5446 for (i = 0; i < ref->u.c.component->as->rank
5447 + ref->u.c.component->as->corank; i++)
5449 if (gfc_traverse_expr (ref->u.c.component->as->lower[i],
5450 sym, func, f))
5451 return true;
5452 if (gfc_traverse_expr (ref->u.c.component->as->upper[i],
5453 sym, func, f))
5454 return true;
5456 break;
5458 case REF_INQUIRY:
5459 return true;
5461 default:
5462 gcc_unreachable ();
5464 ref = ref->next;
5466 return false;
5469 /* Traverse expr, marking all EXPR_VARIABLE symbols referenced. */
5471 static bool
5472 expr_set_symbols_referenced (gfc_expr *expr,
5473 gfc_symbol *sym ATTRIBUTE_UNUSED,
5474 int *f ATTRIBUTE_UNUSED)
5476 if (expr->expr_type != EXPR_VARIABLE)
5477 return false;
5478 gfc_set_sym_referenced (expr->symtree->n.sym);
5479 return false;
5482 void
5483 gfc_expr_set_symbols_referenced (gfc_expr *expr)
5485 gfc_traverse_expr (expr, NULL, expr_set_symbols_referenced, 0);
5489 /* Determine if an expression is a procedure pointer component and return
5490 the component in that case. Otherwise return NULL. */
5492 gfc_component *
5493 gfc_get_proc_ptr_comp (gfc_expr *expr)
5495 gfc_ref *ref;
5497 if (!expr || !expr->ref)
5498 return NULL;
5500 ref = expr->ref;
5501 while (ref->next)
5502 ref = ref->next;
5504 if (ref->type == REF_COMPONENT
5505 && ref->u.c.component->attr.proc_pointer)
5506 return ref->u.c.component;
5508 return NULL;
5512 /* Determine if an expression is a procedure pointer component. */
5514 bool
5515 gfc_is_proc_ptr_comp (gfc_expr *expr)
5517 return (gfc_get_proc_ptr_comp (expr) != NULL);
5521 /* Determine if an expression is a function with an allocatable class scalar
5522 result. */
5523 bool
5524 gfc_is_alloc_class_scalar_function (gfc_expr *expr)
5526 if (expr->expr_type == EXPR_FUNCTION
5527 && expr->value.function.esym
5528 && expr->value.function.esym->result
5529 && expr->value.function.esym->result->ts.type == BT_CLASS
5530 && !CLASS_DATA (expr->value.function.esym->result)->attr.dimension
5531 && CLASS_DATA (expr->value.function.esym->result)->attr.allocatable)
5532 return true;
5534 return false;
5538 /* Determine if an expression is a function with an allocatable class array
5539 result. */
5540 bool
5541 gfc_is_class_array_function (gfc_expr *expr)
5543 if (expr->expr_type == EXPR_FUNCTION
5544 && expr->value.function.esym
5545 && expr->value.function.esym->result
5546 && expr->value.function.esym->result->ts.type == BT_CLASS
5547 && CLASS_DATA (expr->value.function.esym->result)->attr.dimension
5548 && (CLASS_DATA (expr->value.function.esym->result)->attr.allocatable
5549 || CLASS_DATA (expr->value.function.esym->result)->attr.pointer))
5550 return true;
5552 return false;
5556 /* Walk an expression tree and check each variable encountered for being typed.
5557 If strict is not set, a top-level variable is tolerated untyped in -std=gnu
5558 mode as is a basic arithmetic expression using those; this is for things in
5559 legacy-code like:
5561 INTEGER :: arr(n), n
5562 INTEGER :: arr(n + 1), n
5564 The namespace is needed for IMPLICIT typing. */
5566 static gfc_namespace* check_typed_ns;
5568 static bool
5569 expr_check_typed_help (gfc_expr* e, gfc_symbol* sym ATTRIBUTE_UNUSED,
5570 int* f ATTRIBUTE_UNUSED)
5572 bool t;
5574 if (e->expr_type != EXPR_VARIABLE)
5575 return false;
5577 gcc_assert (e->symtree);
5578 t = gfc_check_symbol_typed (e->symtree->n.sym, check_typed_ns,
5579 true, e->where);
5581 return (!t);
5584 bool
5585 gfc_expr_check_typed (gfc_expr* e, gfc_namespace* ns, bool strict)
5587 bool error_found;
5589 /* If this is a top-level variable or EXPR_OP, do the check with strict given
5590 to us. */
5591 if (!strict)
5593 if (e->expr_type == EXPR_VARIABLE && !e->ref)
5594 return gfc_check_symbol_typed (e->symtree->n.sym, ns, strict, e->where);
5596 if (e->expr_type == EXPR_OP)
5598 bool t = true;
5600 gcc_assert (e->value.op.op1);
5601 t = gfc_expr_check_typed (e->value.op.op1, ns, strict);
5603 if (t && e->value.op.op2)
5604 t = gfc_expr_check_typed (e->value.op.op2, ns, strict);
5606 return t;
5610 /* Otherwise, walk the expression and do it strictly. */
5611 check_typed_ns = ns;
5612 error_found = gfc_traverse_expr (e, NULL, &expr_check_typed_help, 0);
5614 return error_found ? false : true;
5618 /* This function returns true if it contains any references to PDT KIND
5619 or LEN parameters. */
5621 static bool
5622 derived_parameter_expr (gfc_expr* e, gfc_symbol* sym ATTRIBUTE_UNUSED,
5623 int* f ATTRIBUTE_UNUSED)
5625 if (e->expr_type != EXPR_VARIABLE)
5626 return false;
5628 gcc_assert (e->symtree);
5629 if (e->symtree->n.sym->attr.pdt_kind
5630 || e->symtree->n.sym->attr.pdt_len)
5631 return true;
5633 return false;
5637 bool
5638 gfc_derived_parameter_expr (gfc_expr *e)
5640 return gfc_traverse_expr (e, NULL, &derived_parameter_expr, 0);
5644 /* This function returns the overall type of a type parameter spec list.
5645 If all the specs are explicit, SPEC_EXPLICIT is returned. If any of the
5646 parameters are assumed/deferred then SPEC_ASSUMED/DEFERRED is returned
5647 unless derived is not NULL. In this latter case, all the LEN parameters
5648 must be either assumed or deferred for the return argument to be set to
5649 anything other than SPEC_EXPLICIT. */
5651 gfc_param_spec_type
5652 gfc_spec_list_type (gfc_actual_arglist *param_list, gfc_symbol *derived)
5654 gfc_param_spec_type res = SPEC_EXPLICIT;
5655 gfc_component *c;
5656 bool seen_assumed = false;
5657 bool seen_deferred = false;
5659 if (derived == NULL)
5661 for (; param_list; param_list = param_list->next)
5662 if (param_list->spec_type == SPEC_ASSUMED
5663 || param_list->spec_type == SPEC_DEFERRED)
5664 return param_list->spec_type;
5666 else
5668 for (; param_list; param_list = param_list->next)
5670 c = gfc_find_component (derived, param_list->name,
5671 true, true, NULL);
5672 gcc_assert (c != NULL);
5673 if (c->attr.pdt_kind)
5674 continue;
5675 else if (param_list->spec_type == SPEC_EXPLICIT)
5676 return SPEC_EXPLICIT;
5677 seen_assumed = param_list->spec_type == SPEC_ASSUMED;
5678 seen_deferred = param_list->spec_type == SPEC_DEFERRED;
5679 if (seen_assumed && seen_deferred)
5680 return SPEC_EXPLICIT;
5682 res = seen_assumed ? SPEC_ASSUMED : SPEC_DEFERRED;
5684 return res;
5688 bool
5689 gfc_ref_this_image (gfc_ref *ref)
5691 int n;
5693 gcc_assert (ref->type == REF_ARRAY && ref->u.ar.codimen > 0);
5695 for (n = ref->u.ar.dimen; n < ref->u.ar.dimen + ref->u.ar.codimen; n++)
5696 if (ref->u.ar.dimen_type[n] != DIMEN_THIS_IMAGE)
5697 return false;
5699 return true;
5702 gfc_expr *
5703 gfc_find_team_co (gfc_expr *e)
5705 gfc_ref *ref;
5707 for (ref = e->ref; ref; ref = ref->next)
5708 if (ref->type == REF_ARRAY && ref->u.ar.codimen > 0)
5709 return ref->u.ar.team;
5711 if (e->value.function.actual->expr)
5712 for (ref = e->value.function.actual->expr->ref; ref;
5713 ref = ref->next)
5714 if (ref->type == REF_ARRAY && ref->u.ar.codimen > 0)
5715 return ref->u.ar.team;
5717 return NULL;
5720 gfc_expr *
5721 gfc_find_stat_co (gfc_expr *e)
5723 gfc_ref *ref;
5725 for (ref = e->ref; ref; ref = ref->next)
5726 if (ref->type == REF_ARRAY && ref->u.ar.codimen > 0)
5727 return ref->u.ar.stat;
5729 if (e->value.function.actual->expr)
5730 for (ref = e->value.function.actual->expr->ref; ref;
5731 ref = ref->next)
5732 if (ref->type == REF_ARRAY && ref->u.ar.codimen > 0)
5733 return ref->u.ar.stat;
5735 return NULL;
5738 bool
5739 gfc_is_coindexed (gfc_expr *e)
5741 gfc_ref *ref;
5743 for (ref = e->ref; ref; ref = ref->next)
5744 if (ref->type == REF_ARRAY && ref->u.ar.codimen > 0)
5745 return !gfc_ref_this_image (ref);
5747 return false;
5751 /* Coarrays are variables with a corank but not being coindexed. However, also
5752 the following is a coarray: A subobject of a coarray is a coarray if it does
5753 not have any cosubscripts, vector subscripts, allocatable component
5754 selection, or pointer component selection. (F2008, 2.4.7) */
5756 bool
5757 gfc_is_coarray (gfc_expr *e)
5759 gfc_ref *ref;
5760 gfc_symbol *sym;
5761 gfc_component *comp;
5762 bool coindexed;
5763 bool coarray;
5764 int i;
5766 if (e->expr_type != EXPR_VARIABLE)
5767 return false;
5769 coindexed = false;
5770 sym = e->symtree->n.sym;
5772 if (sym->ts.type == BT_CLASS && sym->attr.class_ok)
5773 coarray = CLASS_DATA (sym)->attr.codimension;
5774 else
5775 coarray = sym->attr.codimension;
5777 for (ref = e->ref; ref; ref = ref->next)
5778 switch (ref->type)
5780 case REF_COMPONENT:
5781 comp = ref->u.c.component;
5782 if (comp->ts.type == BT_CLASS && comp->attr.class_ok
5783 && (CLASS_DATA (comp)->attr.class_pointer
5784 || CLASS_DATA (comp)->attr.allocatable))
5786 coindexed = false;
5787 coarray = CLASS_DATA (comp)->attr.codimension;
5789 else if (comp->attr.pointer || comp->attr.allocatable)
5791 coindexed = false;
5792 coarray = comp->attr.codimension;
5794 break;
5796 case REF_ARRAY:
5797 if (!coarray)
5798 break;
5800 if (ref->u.ar.codimen > 0 && !gfc_ref_this_image (ref))
5802 coindexed = true;
5803 break;
5806 for (i = 0; i < ref->u.ar.dimen; i++)
5807 if (ref->u.ar.dimen_type[i] == DIMEN_VECTOR)
5809 coarray = false;
5810 break;
5812 break;
5814 case REF_SUBSTRING:
5815 case REF_INQUIRY:
5816 break;
5819 return coarray && !coindexed;
5824 gfc_get_corank (gfc_expr *e)
5826 int corank;
5827 gfc_ref *ref;
5829 if (!gfc_is_coarray (e))
5830 return 0;
5832 if (e->ts.type == BT_CLASS && e->ts.u.derived->components)
5833 corank = e->ts.u.derived->components->as
5834 ? e->ts.u.derived->components->as->corank : 0;
5835 else
5836 corank = e->symtree->n.sym->as ? e->symtree->n.sym->as->corank : 0;
5838 for (ref = e->ref; ref; ref = ref->next)
5840 if (ref->type == REF_ARRAY)
5841 corank = ref->u.ar.as->corank;
5842 gcc_assert (ref->type != REF_SUBSTRING);
5845 return corank;
5849 /* Check whether the expression has an ultimate allocatable component.
5850 Being itself allocatable does not count. */
5851 bool
5852 gfc_has_ultimate_allocatable (gfc_expr *e)
5854 gfc_ref *ref, *last = NULL;
5856 if (e->expr_type != EXPR_VARIABLE)
5857 return false;
5859 for (ref = e->ref; ref; ref = ref->next)
5860 if (ref->type == REF_COMPONENT)
5861 last = ref;
5863 if (last && last->u.c.component->ts.type == BT_CLASS)
5864 return CLASS_DATA (last->u.c.component)->attr.alloc_comp;
5865 else if (last && last->u.c.component->ts.type == BT_DERIVED)
5866 return last->u.c.component->ts.u.derived->attr.alloc_comp;
5867 else if (last)
5868 return false;
5870 if (e->ts.type == BT_CLASS)
5871 return CLASS_DATA (e)->attr.alloc_comp;
5872 else if (e->ts.type == BT_DERIVED)
5873 return e->ts.u.derived->attr.alloc_comp;
5874 else
5875 return false;
5879 /* Check whether the expression has an pointer component.
5880 Being itself a pointer does not count. */
5881 bool
5882 gfc_has_ultimate_pointer (gfc_expr *e)
5884 gfc_ref *ref, *last = NULL;
5886 if (e->expr_type != EXPR_VARIABLE)
5887 return false;
5889 for (ref = e->ref; ref; ref = ref->next)
5890 if (ref->type == REF_COMPONENT)
5891 last = ref;
5893 if (last && last->u.c.component->ts.type == BT_CLASS)
5894 return CLASS_DATA (last->u.c.component)->attr.pointer_comp;
5895 else if (last && last->u.c.component->ts.type == BT_DERIVED)
5896 return last->u.c.component->ts.u.derived->attr.pointer_comp;
5897 else if (last)
5898 return false;
5900 if (e->ts.type == BT_CLASS)
5901 return CLASS_DATA (e)->attr.pointer_comp;
5902 else if (e->ts.type == BT_DERIVED)
5903 return e->ts.u.derived->attr.pointer_comp;
5904 else
5905 return false;
5909 /* Check whether an expression is "simply contiguous", cf. F2008, 6.5.4.
5910 Note: A scalar is not regarded as "simply contiguous" by the standard.
5911 if bool is not strict, some further checks are done - for instance,
5912 a "(::1)" is accepted. */
5914 bool
5915 gfc_is_simply_contiguous (gfc_expr *expr, bool strict, bool permit_element)
5917 bool colon;
5918 int i;
5919 gfc_array_ref *ar = NULL;
5920 gfc_ref *ref, *part_ref = NULL;
5921 gfc_symbol *sym;
5923 if (expr->expr_type == EXPR_ARRAY)
5924 return true;
5926 if (expr->expr_type == EXPR_FUNCTION)
5928 if (expr->value.function.isym)
5929 /* TRANSPOSE is the only intrinsic that may return a
5930 non-contiguous array. It's treated as a special case in
5931 gfc_conv_expr_descriptor too. */
5932 return (expr->value.function.isym->id != GFC_ISYM_TRANSPOSE);
5933 else if (expr->value.function.esym)
5934 /* Only a pointer to an array without the contiguous attribute
5935 can be non-contiguous as a result value. */
5936 return (expr->value.function.esym->result->attr.contiguous
5937 || !expr->value.function.esym->result->attr.pointer);
5938 else
5940 /* Type-bound procedures. */
5941 gfc_symbol *s = expr->symtree->n.sym;
5942 if (s->ts.type != BT_CLASS && s->ts.type != BT_DERIVED)
5943 return false;
5945 gfc_ref *rc = NULL;
5946 for (gfc_ref *r = expr->ref; r; r = r->next)
5947 if (r->type == REF_COMPONENT)
5948 rc = r;
5950 if (rc == NULL || rc->u.c.component == NULL
5951 || rc->u.c.component->ts.interface == NULL)
5952 return false;
5954 return rc->u.c.component->ts.interface->attr.contiguous;
5957 else if (expr->expr_type != EXPR_VARIABLE)
5958 return false;
5960 if (!permit_element && expr->rank == 0)
5961 return false;
5963 for (ref = expr->ref; ref; ref = ref->next)
5965 if (ar)
5966 return false; /* Array shall be last part-ref. */
5968 if (ref->type == REF_COMPONENT)
5969 part_ref = ref;
5970 else if (ref->type == REF_SUBSTRING)
5971 return false;
5972 else if (ref->type == REF_INQUIRY)
5973 return false;
5974 else if (ref->u.ar.type != AR_ELEMENT)
5975 ar = &ref->u.ar;
5978 sym = expr->symtree->n.sym;
5979 if (expr->ts.type != BT_CLASS
5980 && ((part_ref
5981 && !part_ref->u.c.component->attr.contiguous
5982 && part_ref->u.c.component->attr.pointer)
5983 || (!part_ref
5984 && !sym->attr.contiguous
5985 && (sym->attr.pointer
5986 || (sym->as && sym->as->type == AS_ASSUMED_RANK)
5987 || (sym->as && sym->as->type == AS_ASSUMED_SHAPE)))))
5988 return false;
5990 if (!ar || ar->type == AR_FULL)
5991 return true;
5993 gcc_assert (ar->type == AR_SECTION);
5995 /* Check for simply contiguous array */
5996 colon = true;
5997 for (i = 0; i < ar->dimen; i++)
5999 if (ar->dimen_type[i] == DIMEN_VECTOR)
6000 return false;
6002 if (ar->dimen_type[i] == DIMEN_ELEMENT)
6004 colon = false;
6005 continue;
6008 gcc_assert (ar->dimen_type[i] == DIMEN_RANGE);
6011 /* If the previous section was not contiguous, that's an error,
6012 unless we have effective only one element and checking is not
6013 strict. */
6014 if (!colon && (strict || !ar->start[i] || !ar->end[i]
6015 || ar->start[i]->expr_type != EXPR_CONSTANT
6016 || ar->end[i]->expr_type != EXPR_CONSTANT
6017 || mpz_cmp (ar->start[i]->value.integer,
6018 ar->end[i]->value.integer) != 0))
6019 return false;
6021 /* Following the standard, "(::1)" or - if known at compile time -
6022 "(lbound:ubound)" are not simply contiguous; if strict
6023 is false, they are regarded as simply contiguous. */
6024 if (ar->stride[i] && (strict || ar->stride[i]->expr_type != EXPR_CONSTANT
6025 || ar->stride[i]->ts.type != BT_INTEGER
6026 || mpz_cmp_si (ar->stride[i]->value.integer, 1) != 0))
6027 return false;
6029 if (ar->start[i]
6030 && (strict || ar->start[i]->expr_type != EXPR_CONSTANT
6031 || !ar->as->lower[i]
6032 || ar->as->lower[i]->expr_type != EXPR_CONSTANT
6033 || mpz_cmp (ar->start[i]->value.integer,
6034 ar->as->lower[i]->value.integer) != 0))
6035 colon = false;
6037 if (ar->end[i]
6038 && (strict || ar->end[i]->expr_type != EXPR_CONSTANT
6039 || !ar->as->upper[i]
6040 || ar->as->upper[i]->expr_type != EXPR_CONSTANT
6041 || mpz_cmp (ar->end[i]->value.integer,
6042 ar->as->upper[i]->value.integer) != 0))
6043 colon = false;
6046 return true;
6049 /* Return true if the expression is guaranteed to be non-contiguous,
6050 false if we cannot prove anything. It is probably best to call
6051 this after gfc_is_simply_contiguous. If neither of them returns
6052 true, we cannot say (at compile-time). */
6054 bool
6055 gfc_is_not_contiguous (gfc_expr *array)
6057 int i;
6058 gfc_array_ref *ar = NULL;
6059 gfc_ref *ref;
6060 bool previous_incomplete;
6062 for (ref = array->ref; ref; ref = ref->next)
6064 /* Array-ref shall be last ref. */
6066 if (ar && ar->type != AR_ELEMENT)
6067 return true;
6069 if (ref->type == REF_ARRAY)
6070 ar = &ref->u.ar;
6073 if (ar == NULL || ar->type != AR_SECTION)
6074 return false;
6076 previous_incomplete = false;
6078 /* Check if we can prove that the array is not contiguous. */
6080 for (i = 0; i < ar->dimen; i++)
6082 mpz_t arr_size, ref_size;
6084 if (gfc_ref_dimen_size (ar, i, &ref_size, NULL))
6086 if (gfc_dep_difference (ar->as->upper[i], ar->as->lower[i], &arr_size))
6088 /* a(2:4,2:) is known to be non-contiguous, but
6089 a(2:4,i:i) can be contiguous. */
6090 mpz_add_ui (arr_size, arr_size, 1L);
6091 if (previous_incomplete && mpz_cmp_si (ref_size, 1) != 0)
6093 mpz_clear (arr_size);
6094 mpz_clear (ref_size);
6095 return true;
6097 else if (mpz_cmp (arr_size, ref_size) != 0)
6098 previous_incomplete = true;
6100 mpz_clear (arr_size);
6103 /* Check for a(::2), i.e. where the stride is not unity.
6104 This is only done if there is more than one element in
6105 the reference along this dimension. */
6107 if (mpz_cmp_ui (ref_size, 1) > 0 && ar->type == AR_SECTION
6108 && ar->dimen_type[i] == DIMEN_RANGE
6109 && ar->stride[i] && ar->stride[i]->expr_type == EXPR_CONSTANT
6110 && mpz_cmp_si (ar->stride[i]->value.integer, 1) != 0)
6112 mpz_clear (ref_size);
6113 return true;
6116 mpz_clear (ref_size);
6119 /* We didn't find anything definitive. */
6120 return false;
6123 /* Build call to an intrinsic procedure. The number of arguments has to be
6124 passed (rather than ending the list with a NULL value) because we may
6125 want to add arguments but with a NULL-expression. */
6127 gfc_expr*
6128 gfc_build_intrinsic_call (gfc_namespace *ns, gfc_isym_id id, const char* name,
6129 locus where, unsigned numarg, ...)
6131 gfc_expr* result;
6132 gfc_actual_arglist* atail;
6133 gfc_intrinsic_sym* isym;
6134 va_list ap;
6135 unsigned i;
6136 const char *mangled_name = gfc_get_string (GFC_PREFIX ("%s"), name);
6138 isym = gfc_intrinsic_function_by_id (id);
6139 gcc_assert (isym);
6141 result = gfc_get_expr ();
6142 result->expr_type = EXPR_FUNCTION;
6143 result->ts = isym->ts;
6144 result->where = where;
6145 result->value.function.name = mangled_name;
6146 result->value.function.isym = isym;
6148 gfc_get_sym_tree (mangled_name, ns, &result->symtree, false);
6149 gfc_commit_symbol (result->symtree->n.sym);
6150 gcc_assert (result->symtree
6151 && (result->symtree->n.sym->attr.flavor == FL_PROCEDURE
6152 || result->symtree->n.sym->attr.flavor == FL_UNKNOWN));
6153 result->symtree->n.sym->intmod_sym_id = id;
6154 result->symtree->n.sym->attr.flavor = FL_PROCEDURE;
6155 result->symtree->n.sym->attr.intrinsic = 1;
6156 result->symtree->n.sym->attr.artificial = 1;
6158 va_start (ap, numarg);
6159 atail = NULL;
6160 for (i = 0; i < numarg; ++i)
6162 if (atail)
6164 atail->next = gfc_get_actual_arglist ();
6165 atail = atail->next;
6167 else
6168 atail = result->value.function.actual = gfc_get_actual_arglist ();
6170 atail->expr = va_arg (ap, gfc_expr*);
6172 va_end (ap);
6174 return result;
6178 /* Check if an expression may appear in a variable definition context
6179 (F2008, 16.6.7) or pointer association context (F2008, 16.6.8).
6180 This is called from the various places when resolving
6181 the pieces that make up such a context.
6182 If own_scope is true (applies to, e.g., ac-implied-do/data-implied-do
6183 variables), some checks are not performed.
6185 Optionally, a possible error message can be suppressed if context is NULL
6186 and just the return status (true / false) be requested. */
6188 bool
6189 gfc_check_vardef_context (gfc_expr* e, bool pointer, bool alloc_obj,
6190 bool own_scope, const char* context)
6192 gfc_symbol* sym = NULL;
6193 bool is_pointer;
6194 bool check_intentin;
6195 bool ptr_component;
6196 symbol_attribute attr;
6197 gfc_ref* ref;
6198 int i;
6200 if (e->expr_type == EXPR_VARIABLE)
6202 gcc_assert (e->symtree);
6203 sym = e->symtree->n.sym;
6205 else if (e->expr_type == EXPR_FUNCTION)
6207 gcc_assert (e->symtree);
6208 sym = e->value.function.esym ? e->value.function.esym : e->symtree->n.sym;
6211 attr = gfc_expr_attr (e);
6212 if (!pointer && e->expr_type == EXPR_FUNCTION && attr.pointer)
6214 if (!(gfc_option.allow_std & GFC_STD_F2008))
6216 if (context)
6217 gfc_error ("Fortran 2008: Pointer functions in variable definition"
6218 " context (%s) at %L", context, &e->where);
6219 return false;
6222 else if (e->expr_type != EXPR_VARIABLE)
6224 if (context)
6225 gfc_error ("Non-variable expression in variable definition context (%s)"
6226 " at %L", context, &e->where);
6227 return false;
6230 if (!pointer && sym->attr.flavor == FL_PARAMETER)
6232 if (context)
6233 gfc_error ("Named constant %qs in variable definition context (%s)"
6234 " at %L", sym->name, context, &e->where);
6235 return false;
6237 if (!pointer && sym->attr.flavor != FL_VARIABLE
6238 && !(sym->attr.flavor == FL_PROCEDURE && sym == sym->result)
6239 && !(sym->attr.flavor == FL_PROCEDURE && sym->attr.proc_pointer)
6240 && !(sym->attr.flavor == FL_PROCEDURE
6241 && sym->attr.function && sym->attr.pointer))
6243 if (context)
6244 gfc_error ("%qs in variable definition context (%s) at %L is not"
6245 " a variable", sym->name, context, &e->where);
6246 return false;
6249 /* Find out whether the expr is a pointer; this also means following
6250 component references to the last one. */
6251 is_pointer = (attr.pointer || attr.proc_pointer);
6252 if (pointer && !is_pointer)
6254 if (context)
6255 gfc_error ("Non-POINTER in pointer association context (%s)"
6256 " at %L", context, &e->where);
6257 return false;
6260 if (e->ts.type == BT_DERIVED
6261 && e->ts.u.derived == NULL)
6263 if (context)
6264 gfc_error ("Type inaccessible in variable definition context (%s) "
6265 "at %L", context, &e->where);
6266 return false;
6269 /* F2008, C1303. */
6270 if (!alloc_obj
6271 && (attr.lock_comp
6272 || (e->ts.type == BT_DERIVED
6273 && e->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
6274 && e->ts.u.derived->intmod_sym_id == ISOFORTRAN_LOCK_TYPE)))
6276 if (context)
6277 gfc_error ("LOCK_TYPE in variable definition context (%s) at %L",
6278 context, &e->where);
6279 return false;
6282 /* TS18508, C702/C203. */
6283 if (!alloc_obj
6284 && (attr.lock_comp
6285 || (e->ts.type == BT_DERIVED
6286 && e->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
6287 && e->ts.u.derived->intmod_sym_id == ISOFORTRAN_EVENT_TYPE)))
6289 if (context)
6290 gfc_error ("LOCK_EVENT in variable definition context (%s) at %L",
6291 context, &e->where);
6292 return false;
6295 /* INTENT(IN) dummy argument. Check this, unless the object itself is the
6296 component of sub-component of a pointer; we need to distinguish
6297 assignment to a pointer component from pointer-assignment to a pointer
6298 component. Note that (normal) assignment to procedure pointers is not
6299 possible. */
6300 check_intentin = !own_scope;
6301 ptr_component = (sym->ts.type == BT_CLASS && sym->ts.u.derived
6302 && CLASS_DATA (sym))
6303 ? CLASS_DATA (sym)->attr.class_pointer : sym->attr.pointer;
6304 for (ref = e->ref; ref && check_intentin; ref = ref->next)
6306 if (ptr_component && ref->type == REF_COMPONENT)
6307 check_intentin = false;
6308 if (ref->type == REF_COMPONENT)
6310 gfc_component *comp = ref->u.c.component;
6311 ptr_component = (comp->ts.type == BT_CLASS && comp->attr.class_ok)
6312 ? CLASS_DATA (comp)->attr.class_pointer
6313 : comp->attr.pointer;
6314 if (ptr_component && !pointer)
6315 check_intentin = false;
6317 if (ref->type == REF_INQUIRY
6318 && (ref->u.i == INQUIRY_KIND || ref->u.i == INQUIRY_LEN))
6320 if (context)
6321 gfc_error ("%qs parameter inquiry for %qs in "
6322 "variable definition context (%s) at %L",
6323 ref->u.i == INQUIRY_KIND ? "KIND" : "LEN",
6324 sym->name, context, &e->where);
6325 return false;
6329 if (check_intentin
6330 && (sym->attr.intent == INTENT_IN
6331 || (sym->attr.select_type_temporary && sym->assoc
6332 && sym->assoc->target && sym->assoc->target->symtree
6333 && sym->assoc->target->symtree->n.sym->attr.intent == INTENT_IN)))
6335 if (pointer && is_pointer)
6337 if (context)
6338 gfc_error ("Dummy argument %qs with INTENT(IN) in pointer"
6339 " association context (%s) at %L",
6340 sym->name, context, &e->where);
6341 return false;
6343 if (!pointer && !is_pointer && !sym->attr.pointer)
6345 const char *name = sym->attr.select_type_temporary
6346 ? sym->assoc->target->symtree->name : sym->name;
6347 if (context)
6348 gfc_error ("Dummy argument %qs with INTENT(IN) in variable"
6349 " definition context (%s) at %L",
6350 name, context, &e->where);
6351 return false;
6355 /* PROTECTED and use-associated. */
6356 if (sym->attr.is_protected && sym->attr.use_assoc && check_intentin)
6358 if (pointer && is_pointer)
6360 if (context)
6361 gfc_error ("Variable %qs is PROTECTED and cannot appear in a"
6362 " pointer association context (%s) at %L",
6363 sym->name, context, &e->where);
6364 return false;
6366 if (!pointer && !is_pointer)
6368 if (context)
6369 gfc_error ("Variable %qs is PROTECTED and cannot appear in a"
6370 " variable definition context (%s) at %L",
6371 sym->name, context, &e->where);
6372 return false;
6376 /* Variable not assignable from a PURE procedure but appears in
6377 variable definition context. */
6378 own_scope = own_scope
6379 || (sym->attr.result && sym->ns->proc_name
6380 && sym == sym->ns->proc_name->result);
6381 if (!pointer && !own_scope && gfc_pure (NULL) && gfc_impure_variable (sym))
6383 if (context)
6384 gfc_error ("Variable %qs cannot appear in a variable definition"
6385 " context (%s) at %L in PURE procedure",
6386 sym->name, context, &e->where);
6387 return false;
6390 if (!pointer && context && gfc_implicit_pure (NULL)
6391 && gfc_impure_variable (sym))
6393 gfc_namespace *ns;
6394 gfc_symbol *sym;
6396 for (ns = gfc_current_ns; ns; ns = ns->parent)
6398 sym = ns->proc_name;
6399 if (sym == NULL)
6400 break;
6401 if (sym->attr.flavor == FL_PROCEDURE)
6403 sym->attr.implicit_pure = 0;
6404 break;
6408 /* Check variable definition context for associate-names. */
6409 if (!pointer && sym->assoc && !sym->attr.select_rank_temporary)
6411 const char* name;
6412 gfc_association_list* assoc;
6414 gcc_assert (sym->assoc->target);
6416 /* If this is a SELECT TYPE temporary (the association is used internally
6417 for SELECT TYPE), silently go over to the target. */
6418 if (sym->attr.select_type_temporary)
6420 gfc_expr* t = sym->assoc->target;
6422 gcc_assert (t->expr_type == EXPR_VARIABLE);
6423 name = t->symtree->name;
6425 if (t->symtree->n.sym->assoc)
6426 assoc = t->symtree->n.sym->assoc;
6427 else
6428 assoc = sym->assoc;
6430 else
6432 name = sym->name;
6433 assoc = sym->assoc;
6435 gcc_assert (name && assoc);
6437 /* Is association to a valid variable? */
6438 if (!assoc->variable)
6440 if (context)
6442 if (assoc->target->expr_type == EXPR_VARIABLE)
6443 gfc_error ("%qs at %L associated to vector-indexed target"
6444 " cannot be used in a variable definition"
6445 " context (%s)",
6446 name, &e->where, context);
6447 else
6448 gfc_error ("%qs at %L associated to expression"
6449 " cannot be used in a variable definition"
6450 " context (%s)",
6451 name, &e->where, context);
6453 return false;
6456 /* Target must be allowed to appear in a variable definition context. */
6457 if (!gfc_check_vardef_context (assoc->target, pointer, false, false, NULL))
6459 if (context)
6460 gfc_error ("Associate-name %qs cannot appear in a variable"
6461 " definition context (%s) at %L because its target"
6462 " at %L cannot, either",
6463 name, context, &e->where,
6464 &assoc->target->where);
6465 return false;
6469 /* Check for same value in vector expression subscript. */
6471 if (e->rank > 0)
6472 for (ref = e->ref; ref != NULL; ref = ref->next)
6473 if (ref->type == REF_ARRAY && ref->u.ar.type == AR_SECTION)
6474 for (i = 0; i < GFC_MAX_DIMENSIONS
6475 && ref->u.ar.dimen_type[i] != 0; i++)
6476 if (ref->u.ar.dimen_type[i] == DIMEN_VECTOR)
6478 gfc_expr *arr = ref->u.ar.start[i];
6479 if (arr->expr_type == EXPR_ARRAY)
6481 gfc_constructor *c, *n;
6482 gfc_expr *ec, *en;
6484 for (c = gfc_constructor_first (arr->value.constructor);
6485 c != NULL; c = gfc_constructor_next (c))
6487 if (c == NULL || c->iterator != NULL)
6488 continue;
6490 ec = c->expr;
6492 for (n = gfc_constructor_next (c); n != NULL;
6493 n = gfc_constructor_next (n))
6495 if (n->iterator != NULL)
6496 continue;
6498 en = n->expr;
6499 if (gfc_dep_compare_expr (ec, en) == 0)
6501 if (context)
6502 gfc_error_now ("Elements with the same value "
6503 "at %L and %L in vector "
6504 "subscript in a variable "
6505 "definition context (%s)",
6506 &(ec->where), &(en->where),
6507 context);
6508 return false;
6515 return true;