1 /* Routines for manipulation of expression nodes.
2 Copyright (C) 2000-2024 Free Software Foundation, Inc.
3 Contributed by Andy Vaught
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
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
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/>. */
23 #include "coretypes.h"
28 #include "target-memory.h" /* for gfc_convert_boz */
29 #include "constructor.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. */
50 gfc_clear_ts (&e
->ts
);
58 /* Get a new expression node that is an array constructor
59 of given type and kind. */
62 gfc_get_array_expr (bt type
, int kind
, locus
*where
)
67 e
->expr_type
= EXPR_ARRAY
;
68 e
->value
.constructor
= NULL
;
81 /* Get a new expression node that is the NULL expression. */
84 gfc_get_null_expr (locus
*where
)
89 e
->expr_type
= EXPR_NULL
;
90 e
->ts
.type
= BT_UNKNOWN
;
99 /* Get a new expression node that is an operator expression node. */
102 gfc_get_operator_expr (locus
*where
, gfc_intrinsic_op op
,
103 gfc_expr
*op1
, gfc_expr
*op2
)
108 e
->expr_type
= EXPR_OP
;
110 e
->value
.op
.op1
= op1
;
111 e
->value
.op
.op2
= op2
;
120 /* Get a new expression node that is an structure constructor
121 of given type and kind. */
124 gfc_get_structure_constructor_expr (bt type
, int kind
, locus
*where
)
129 e
->expr_type
= EXPR_STRUCTURE
;
130 e
->value
.constructor
= NULL
;
141 /* Get a new expression node that is an constant of given type and kind. */
144 gfc_get_constant_expr (bt type
, int kind
, locus
*where
)
149 gfc_internal_error ("gfc_get_constant_expr(): locus %<where%> cannot be "
154 e
->expr_type
= EXPR_CONSTANT
;
163 mpz_init (e
->value
.integer
);
167 gfc_set_model_kind (kind
);
168 mpfr_init (e
->value
.real
);
172 gfc_set_model_kind (kind
);
173 mpc_init2 (e
->value
.complex, mpfr_get_default_prec());
184 /* Get a new expression node that is an string constant.
185 If no string is passed, a string of len is allocated,
186 blanked and null-terminated. */
189 gfc_get_character_expr (int kind
, locus
*where
, const char *src
, gfc_charlen_t len
)
196 dest
= gfc_get_wide_string (len
+ 1);
197 gfc_wide_memset (dest
, ' ', len
);
201 dest
= gfc_char_to_widechar (src
);
203 e
= gfc_get_constant_expr (BT_CHARACTER
, kind
,
204 where
? where
: &gfc_current_locus
);
205 e
->value
.character
.string
= dest
;
206 e
->value
.character
.length
= len
;
212 /* Get a new expression node that is an integer constant. */
215 gfc_get_int_expr (int kind
, locus
*where
, HOST_WIDE_INT value
)
218 p
= gfc_get_constant_expr (BT_INTEGER
, kind
,
219 where
? where
: &gfc_current_locus
);
221 const wide_int w
= wi::shwi (value
, kind
* BITS_PER_UNIT
);
222 wi::to_mpz (w
, p
->value
.integer
, SIGNED
);
227 /* Get a new expression node that is an unsigned constant. */
230 gfc_get_unsigned_expr (int kind
, locus
*where
, HOST_WIDE_INT value
)
233 p
= gfc_get_constant_expr (BT_UNSIGNED
, kind
,
234 where
? where
: &gfc_current_locus
);
235 const wide_int w
= wi::shwi (value
, kind
* BITS_PER_UNIT
);
236 wi::to_mpz (w
, p
->value
.integer
, UNSIGNED
);
241 /* Get a new expression node that is a logical constant. */
244 gfc_get_logical_expr (int kind
, locus
*where
, bool value
)
247 p
= gfc_get_constant_expr (BT_LOGICAL
, kind
,
248 where
? where
: &gfc_current_locus
);
250 p
->value
.logical
= value
;
257 gfc_get_iokind_expr (locus
*where
, io_kind k
)
261 /* Set the types to something compatible with iokind. This is needed to
262 get through gfc_free_expr later since iokind really has no Basic Type,
266 e
->expr_type
= EXPR_CONSTANT
;
267 e
->ts
.type
= BT_LOGICAL
;
275 /* Given an expression pointer, return a copy of the expression. This
276 subroutine is recursive. */
279 gfc_copy_expr (gfc_expr
*p
)
291 switch (q
->expr_type
)
294 s
= gfc_get_wide_string (p
->value
.character
.length
+ 1);
295 q
->value
.character
.string
= s
;
296 memcpy (s
, p
->value
.character
.string
,
297 (p
->value
.character
.length
+ 1) * sizeof (gfc_char_t
));
301 /* Copy target representation, if it exists. */
302 if (p
->representation
.string
)
304 c
= XCNEWVEC (char, p
->representation
.length
+ 1);
305 q
->representation
.string
= c
;
306 memcpy (c
, p
->representation
.string
, (p
->representation
.length
+ 1));
309 /* Copy the values of any pointer components of p->value. */
314 mpz_init_set (q
->value
.integer
, p
->value
.integer
);
318 gfc_set_model_kind (q
->ts
.kind
);
319 mpfr_init (q
->value
.real
);
320 mpfr_set (q
->value
.real
, p
->value
.real
, GFC_RND_MODE
);
324 gfc_set_model_kind (q
->ts
.kind
);
325 mpc_init2 (q
->value
.complex, mpfr_get_default_prec());
326 mpc_set (q
->value
.complex, p
->value
.complex, GFC_MPC_RND_MODE
);
330 if (p
->representation
.string
331 && p
->ts
.kind
== gfc_default_character_kind
)
332 q
->value
.character
.string
333 = gfc_char_to_widechar (q
->representation
.string
);
336 s
= gfc_get_wide_string (p
->value
.character
.length
+ 1);
337 q
->value
.character
.string
= s
;
339 /* This is the case for the C_NULL_CHAR named constant. */
340 if (p
->value
.character
.length
== 0
341 && (p
->ts
.is_c_interop
|| p
->ts
.is_iso_c
))
344 /* Need to set the length to 1 to make sure the NUL
345 terminator is copied. */
346 q
->value
.character
.length
= 1;
349 memcpy (s
, p
->value
.character
.string
,
350 (p
->value
.character
.length
+ 1) * sizeof (gfc_char_t
));
359 break; /* Already done. */
362 q
->boz
.len
= p
->boz
.len
;
363 q
->boz
.rdx
= p
->boz
.rdx
;
364 q
->boz
.str
= XCNEWVEC (char, q
->boz
.len
+ 1);
365 strncpy (q
->boz
.str
, p
->boz
.str
, p
->boz
.len
);
370 /* Should never be reached. */
372 gfc_internal_error ("gfc_copy_expr(): Bad expr node");
379 switch (q
->value
.op
.op
)
382 case INTRINSIC_PARENTHESES
:
383 case INTRINSIC_UPLUS
:
384 case INTRINSIC_UMINUS
:
385 q
->value
.op
.op1
= gfc_copy_expr (p
->value
.op
.op1
);
388 default: /* Binary operators. */
389 q
->value
.op
.op1
= gfc_copy_expr (p
->value
.op
.op1
);
390 q
->value
.op
.op2
= gfc_copy_expr (p
->value
.op
.op2
);
397 q
->value
.function
.actual
=
398 gfc_copy_actual_arglist (p
->value
.function
.actual
);
403 q
->value
.compcall
.actual
=
404 gfc_copy_actual_arglist (p
->value
.compcall
.actual
);
405 q
->value
.compcall
.tbp
= p
->value
.compcall
.tbp
;
410 q
->value
.constructor
= gfc_constructor_copy (p
->value
.constructor
);
421 q
->shape
= gfc_copy_shape (p
->shape
, p
->rank
);
423 q
->ref
= gfc_copy_ref (p
->ref
);
426 q
->param_list
= gfc_copy_actual_arglist (p
->param_list
);
433 gfc_clear_shape (mpz_t
*shape
, int rank
)
437 for (i
= 0; i
< rank
; i
++)
438 mpz_clear (shape
[i
]);
443 gfc_free_shape (mpz_t
**shape
, int rank
)
448 gfc_clear_shape (*shape
, rank
);
454 /* Workhorse function for gfc_free_expr() that frees everything
455 beneath an expression node, but not the node itself. This is
456 useful when we want to simplify a node and replace it with
457 something else or the expression node belongs to another structure. */
460 free_expr0 (gfc_expr
*e
)
462 switch (e
->expr_type
)
465 /* Free any parts of the value that need freeing. */
469 mpz_clear (e
->value
.integer
);
473 mpfr_clear (e
->value
.real
);
477 free (e
->value
.character
.string
);
481 mpc_clear (e
->value
.complex);
492 /* Free the representation. */
493 free (e
->representation
.string
);
498 if (e
->value
.op
.op1
!= NULL
)
499 gfc_free_expr (e
->value
.op
.op1
);
500 if (e
->value
.op
.op2
!= NULL
)
501 gfc_free_expr (e
->value
.op
.op2
);
505 gfc_free_actual_arglist (e
->value
.function
.actual
);
510 gfc_free_actual_arglist (e
->value
.compcall
.actual
);
518 gfc_constructor_free (e
->value
.constructor
);
522 free (e
->value
.character
.string
);
529 gfc_internal_error ("free_expr0(): Bad expr type");
532 /* Free a shape array. */
533 gfc_free_shape (&e
->shape
, e
->rank
);
535 gfc_free_ref_list (e
->ref
);
537 gfc_free_actual_arglist (e
->param_list
);
539 memset (e
, '\0', sizeof (gfc_expr
));
543 /* Free an expression node and everything beneath it. */
546 gfc_free_expr (gfc_expr
*e
)
555 /* Free an argument list and everything below it. */
558 gfc_free_actual_arglist (gfc_actual_arglist
*a1
)
560 gfc_actual_arglist
*a2
;
566 gfc_free_expr (a1
->expr
);
567 free (a1
->associated_dummy
);
574 /* Copy an arglist structure and all of the arguments. */
577 gfc_copy_actual_arglist (gfc_actual_arglist
*p
)
579 gfc_actual_arglist
*head
, *tail
, *new_arg
;
583 for (; p
; p
= p
->next
)
585 new_arg
= gfc_get_actual_arglist ();
588 if (p
->associated_dummy
!= NULL
)
590 new_arg
->associated_dummy
= gfc_get_dummy_arg ();
591 *new_arg
->associated_dummy
= *p
->associated_dummy
;
594 new_arg
->expr
= gfc_copy_expr (p
->expr
);
595 new_arg
->next
= NULL
;
600 tail
->next
= new_arg
;
609 /* Free a list of reference structures. */
612 gfc_free_ref_list (gfc_ref
*p
)
624 for (i
= 0; i
< GFC_MAX_DIMENSIONS
; i
++)
626 gfc_free_expr (p
->u
.ar
.start
[i
]);
627 gfc_free_expr (p
->u
.ar
.end
[i
]);
628 gfc_free_expr (p
->u
.ar
.stride
[i
]);
634 gfc_free_expr (p
->u
.ss
.start
);
635 gfc_free_expr (p
->u
.ss
.end
);
648 /* Graft the *src expression onto the *dest subexpression. */
651 gfc_replace_expr (gfc_expr
*dest
, gfc_expr
*src
)
659 /* Try to extract an integer constant from the passed expression node.
660 Return true if some error occurred, false on success. If REPORT_ERROR
661 is non-zero, emit error, for positive REPORT_ERROR using gfc_error,
662 for negative using gfc_error_now. */
665 gfc_extract_int (gfc_expr
*expr
, int *result
, int report_error
)
669 /* A KIND component is a parameter too. The expression for it
670 is stored in the initializer and should be consistent with
672 if (gfc_expr_attr(expr
).pdt_kind
)
674 for (ref
= expr
->ref
; ref
; ref
= ref
->next
)
676 if (ref
->u
.c
.component
->attr
.pdt_kind
)
677 expr
= ref
->u
.c
.component
->initializer
;
681 if (expr
->expr_type
!= EXPR_CONSTANT
)
683 if (report_error
> 0)
684 gfc_error ("Constant expression required at %C");
685 else if (report_error
< 0)
686 gfc_error_now ("Constant expression required at %C");
690 if (expr
->ts
.type
!= BT_INTEGER
)
692 if (report_error
> 0)
693 gfc_error ("Integer expression required at %C");
694 else if (report_error
< 0)
695 gfc_error_now ("Integer expression required at %C");
699 if ((mpz_cmp_si (expr
->value
.integer
, INT_MAX
) > 0)
700 || (mpz_cmp_si (expr
->value
.integer
, INT_MIN
) < 0))
702 if (report_error
> 0)
703 gfc_error ("Integer value too large in expression at %C");
704 else if (report_error
< 0)
705 gfc_error_now ("Integer value too large in expression at %C");
709 *result
= (int) mpz_get_si (expr
->value
.integer
);
714 /* Same as gfc_extract_int, but use a HWI. */
717 gfc_extract_hwi (gfc_expr
*expr
, HOST_WIDE_INT
*result
, int report_error
)
721 /* A KIND component is a parameter too. The expression for it is
722 stored in the initializer and should be consistent with the tests
724 if (gfc_expr_attr(expr
).pdt_kind
)
726 for (ref
= expr
->ref
; ref
; ref
= ref
->next
)
728 if (ref
->u
.c
.component
->attr
.pdt_kind
)
729 expr
= ref
->u
.c
.component
->initializer
;
733 if (expr
->expr_type
!= EXPR_CONSTANT
)
735 if (report_error
> 0)
736 gfc_error ("Constant expression required at %C");
737 else if (report_error
< 0)
738 gfc_error_now ("Constant expression required at %C");
742 if (expr
->ts
.type
!= BT_INTEGER
)
744 if (report_error
> 0)
745 gfc_error ("Integer expression required at %C");
746 else if (report_error
< 0)
747 gfc_error_now ("Integer expression required at %C");
751 /* Use long_long_integer_type_node to determine when to saturate. */
752 const wide_int val
= wi::from_mpz (long_long_integer_type_node
,
753 expr
->value
.integer
, false);
755 if (!wi::fits_shwi_p (val
))
757 if (report_error
> 0)
758 gfc_error ("Integer value too large in expression at %C");
759 else if (report_error
< 0)
760 gfc_error_now ("Integer value too large in expression at %C");
764 *result
= val
.to_shwi ();
770 /* Recursively copy a list of reference structures. */
773 gfc_copy_ref (gfc_ref
*src
)
781 dest
= gfc_get_ref ();
782 dest
->type
= src
->type
;
787 ar
= gfc_copy_array_ref (&src
->u
.ar
);
793 dest
->u
.c
= src
->u
.c
;
797 dest
->u
.i
= src
->u
.i
;
801 dest
->u
.ss
= src
->u
.ss
;
802 dest
->u
.ss
.start
= gfc_copy_expr (src
->u
.ss
.start
);
803 dest
->u
.ss
.end
= gfc_copy_expr (src
->u
.ss
.end
);
807 dest
->next
= gfc_copy_ref (src
->next
);
813 /* Detect whether an expression has any vector index array references. */
816 gfc_has_vector_index (gfc_expr
*e
)
820 for (ref
= e
->ref
; ref
; ref
= ref
->next
)
821 if (ref
->type
== REF_ARRAY
)
822 for (i
= 0; i
< ref
->u
.ar
.dimen
; i
++)
823 if (ref
->u
.ar
.dimen_type
[i
] == DIMEN_VECTOR
)
830 gfc_is_ptr_fcn (gfc_expr
*e
)
832 return e
!= NULL
&& e
->expr_type
== EXPR_FUNCTION
833 && gfc_expr_attr (e
).pointer
;
837 /* Copy a shape array. */
840 gfc_copy_shape (mpz_t
*shape
, int rank
)
848 new_shape
= gfc_get_shape (rank
);
850 for (n
= 0; n
< rank
; n
++)
851 mpz_init_set (new_shape
[n
], shape
[n
]);
857 /* Copy a shape array excluding dimension N, where N is an integer
858 constant expression. Dimensions are numbered in Fortran style --
861 So, if the original shape array contains R elements
862 { s1 ... sN-1 sN sN+1 ... sR-1 sR}
863 the result contains R-1 elements:
864 { s1 ... sN-1 sN+1 ... sR-1}
866 If anything goes wrong -- N is not a constant, its value is out
867 of range -- or anything else, just returns NULL. */
870 gfc_copy_shape_excluding (mpz_t
*shape
, int rank
, gfc_expr
*dim
)
872 mpz_t
*new_shape
, *s
;
878 || dim
->expr_type
!= EXPR_CONSTANT
879 || dim
->ts
.type
!= BT_INTEGER
)
882 n
= mpz_get_si (dim
->value
.integer
);
883 n
--; /* Convert to zero based index. */
884 if (n
< 0 || n
>= rank
)
887 s
= new_shape
= gfc_get_shape (rank
- 1);
889 for (i
= 0; i
< rank
; i
++)
893 mpz_init_set (*s
, shape
[i
]);
901 /* Return the maximum kind of two expressions. In general, higher
902 kind numbers mean more precision for numeric types. */
905 gfc_kind_max (gfc_expr
*e1
, gfc_expr
*e2
)
907 return (e1
->ts
.kind
> e2
->ts
.kind
) ? e1
->ts
.kind
: e2
->ts
.kind
;
911 /* Returns nonzero if the type is numeric, zero otherwise. */
914 numeric_type (bt type
)
916 return type
== BT_COMPLEX
|| type
== BT_REAL
|| type
== BT_INTEGER
917 || type
== BT_UNSIGNED
;
921 /* Returns nonzero if the typespec is a numeric type, zero otherwise. */
924 gfc_numeric_ts (gfc_typespec
*ts
)
926 return numeric_type (ts
->type
);
930 /* Return an expression node with an optional argument list attached.
931 A variable number of gfc_expr pointers are strung together in an
932 argument list with a NULL pointer terminating the list. */
935 gfc_build_conversion (gfc_expr
*e
)
940 p
->expr_type
= EXPR_FUNCTION
;
942 p
->value
.function
.actual
= gfc_get_actual_arglist ();
943 p
->value
.function
.actual
->expr
= e
;
949 /* Given an expression node with some sort of numeric binary
950 expression, insert type conversions required to make the operands
951 have the same type. Conversion warnings are disabled if wconversion
954 The exception is that the operands of an exponential don't have to
955 have the same type. If possible, the base is promoted to the type
956 of the exponent. For example, 1**2.3 becomes 1.0**2.3, but
957 1.0**2 stays as it is. */
960 gfc_type_convert_binary (gfc_expr
*e
, int wconversion
)
964 op1
= e
->value
.op
.op1
;
965 op2
= e
->value
.op
.op2
;
967 if (op1
->ts
.type
== BT_UNKNOWN
|| op2
->ts
.type
== BT_UNKNOWN
)
969 gfc_clear_ts (&e
->ts
);
973 /* Kind conversions of same type. */
974 if (op1
->ts
.type
== op2
->ts
.type
)
976 if (op1
->ts
.kind
== op2
->ts
.kind
)
978 /* No type conversions. */
983 if (op1
->ts
.kind
> op2
->ts
.kind
)
984 gfc_convert_type_warn (op2
, &op1
->ts
, 2, wconversion
);
986 gfc_convert_type_warn (op1
, &op2
->ts
, 2, wconversion
);
992 /* Integer combined with real or complex. */
993 if (op2
->ts
.type
== BT_INTEGER
)
997 /* Special case for ** operator. */
998 if (e
->value
.op
.op
== INTRINSIC_POWER
)
1001 gfc_convert_type_warn (e
->value
.op
.op2
, &e
->ts
, 2, wconversion
);
1005 if (op1
->ts
.type
== BT_INTEGER
)
1008 gfc_convert_type_warn (e
->value
.op
.op1
, &e
->ts
, 2, wconversion
);
1012 /* Real combined with complex. */
1013 e
->ts
.type
= BT_COMPLEX
;
1014 if (op1
->ts
.kind
> op2
->ts
.kind
)
1015 e
->ts
.kind
= op1
->ts
.kind
;
1017 e
->ts
.kind
= op2
->ts
.kind
;
1018 if (op1
->ts
.type
!= BT_COMPLEX
|| op1
->ts
.kind
!= e
->ts
.kind
)
1019 gfc_convert_type_warn (e
->value
.op
.op1
, &e
->ts
, 2, wconversion
);
1020 if (op2
->ts
.type
!= BT_COMPLEX
|| op2
->ts
.kind
!= e
->ts
.kind
)
1021 gfc_convert_type_warn (e
->value
.op
.op2
, &e
->ts
, 2, wconversion
);
1028 /* Standard intrinsics listed under F2018:10.1.12 (6), which are excluded in
1029 constant expressions, except TRANSFER (c.f. item (8)), which would need
1030 separate treatment. */
1033 is_non_constant_intrinsic (gfc_expr
*e
)
1035 if (e
->expr_type
== EXPR_FUNCTION
1036 && e
->value
.function
.isym
)
1038 switch (e
->value
.function
.isym
->id
)
1040 case GFC_ISYM_COMMAND_ARGUMENT_COUNT
:
1041 case GFC_ISYM_GET_TEAM
:
1043 case GFC_ISYM_NUM_IMAGES
:
1044 case GFC_ISYM_TEAM_NUMBER
:
1045 case GFC_ISYM_THIS_IMAGE
:
1056 /* Determine if an expression is constant in the sense of F08:7.1.12.
1057 * This function expects that the expression has already been simplified. */
1060 gfc_is_constant_expr (gfc_expr
*e
)
1063 gfc_actual_arglist
*arg
;
1068 switch (e
->expr_type
)
1071 return (gfc_is_constant_expr (e
->value
.op
.op1
)
1072 && (e
->value
.op
.op2
== NULL
1073 || gfc_is_constant_expr (e
->value
.op
.op2
)));
1076 /* The only context in which this can occur is in a parameterized
1077 derived type declaration, so returning true is OK. */
1078 if (e
->symtree
->n
.sym
->attr
.pdt_len
1079 || e
->symtree
->n
.sym
->attr
.pdt_kind
)
1086 gcc_assert (e
->symtree
|| e
->value
.function
.esym
1087 || e
->value
.function
.isym
);
1089 /* Check for intrinsics excluded in constant expressions. */
1090 if (e
->value
.function
.isym
&& is_non_constant_intrinsic (e
))
1093 /* Call to intrinsic with at least one argument. */
1094 if (e
->value
.function
.isym
&& e
->value
.function
.actual
)
1096 for (arg
= e
->value
.function
.actual
; arg
; arg
= arg
->next
)
1097 if (!gfc_is_constant_expr (arg
->expr
))
1101 if (e
->value
.function
.isym
1102 && (e
->value
.function
.isym
->elemental
1103 || e
->value
.function
.isym
->pure
1104 || e
->value
.function
.isym
->inquiry
1105 || e
->value
.function
.isym
->transformational
))
1114 case EXPR_SUBSTRING
:
1115 return e
->ref
== NULL
|| (gfc_is_constant_expr (e
->ref
->u
.ss
.start
)
1116 && gfc_is_constant_expr (e
->ref
->u
.ss
.end
));
1119 case EXPR_STRUCTURE
:
1120 c
= gfc_constructor_first (e
->value
.constructor
);
1121 if ((e
->expr_type
== EXPR_ARRAY
) && c
&& c
->iterator
)
1122 return gfc_constant_ac (e
);
1124 for (; c
; c
= gfc_constructor_next (c
))
1125 if (!gfc_is_constant_expr (c
->expr
))
1132 gfc_internal_error ("gfc_is_constant_expr(): Unknown expression type");
1138 /* Is true if the expression or symbol is a passed CFI descriptor. */
1140 is_CFI_desc (gfc_symbol
*sym
, gfc_expr
*e
)
1143 && e
&& e
->expr_type
== EXPR_VARIABLE
)
1144 sym
= e
->symtree
->n
.sym
;
1146 if (sym
&& sym
->attr
.dummy
1147 && sym
->ns
->proc_name
->attr
.is_bind_c
1148 && (sym
->attr
.pointer
1149 || sym
->attr
.allocatable
1150 || (sym
->attr
.dimension
1151 && (sym
->as
->type
== AS_ASSUMED_SHAPE
1152 || sym
->as
->type
== AS_ASSUMED_RANK
))
1153 || (sym
->ts
.type
== BT_CHARACTER
1154 && (!sym
->ts
.u
.cl
|| !sym
->ts
.u
.cl
->length
))))
1161 /* Is true if an array reference is followed by a component or substring
1164 is_subref_array (gfc_expr
* e
)
1170 if (e
->expr_type
!= EXPR_VARIABLE
)
1173 sym
= e
->symtree
->n
.sym
;
1175 if (sym
->attr
.subref_array_pointer
)
1180 for (ref
= e
->ref
; ref
; ref
= ref
->next
)
1182 /* If we haven't seen the array reference and this is an intrinsic,
1183 what follows cannot be a subreference array, unless there is a
1184 substring reference. */
1185 if (!seen_array
&& ref
->type
== REF_COMPONENT
1186 && ref
->u
.c
.component
->ts
.type
!= BT_CHARACTER
1187 && ref
->u
.c
.component
->ts
.type
!= BT_CLASS
1188 && !gfc_bt_struct (ref
->u
.c
.component
->ts
.type
))
1191 if (ref
->type
== REF_ARRAY
1192 && ref
->u
.ar
.type
!= AR_ELEMENT
)
1196 && ref
->type
!= REF_ARRAY
)
1200 if (sym
->ts
.type
== BT_CLASS
1202 && CLASS_DATA (sym
)->attr
.dimension
1203 && CLASS_DATA (sym
)->attr
.class_pointer
)
1210 /* Try to collapse intrinsic expressions. */
1213 simplify_intrinsic_op (gfc_expr
*p
, int type
)
1215 gfc_intrinsic_op op
;
1216 gfc_expr
*op1
, *op2
, *result
;
1218 if (p
->value
.op
.op
== INTRINSIC_USER
)
1221 op1
= p
->value
.op
.op1
;
1222 op2
= p
->value
.op
.op2
;
1223 op
= p
->value
.op
.op
;
1225 if (!gfc_simplify_expr (op1
, type
))
1227 if (!gfc_simplify_expr (op2
, type
))
1230 if (!gfc_is_constant_expr (op1
)
1231 || (op2
!= NULL
&& !gfc_is_constant_expr (op2
)))
1235 p
->value
.op
.op1
= NULL
;
1236 p
->value
.op
.op2
= NULL
;
1240 case INTRINSIC_PARENTHESES
:
1241 result
= gfc_parentheses (op1
);
1244 case INTRINSIC_UPLUS
:
1245 result
= gfc_uplus (op1
);
1248 case INTRINSIC_UMINUS
:
1249 result
= gfc_uminus (op1
);
1252 case INTRINSIC_PLUS
:
1253 result
= gfc_add (op1
, op2
);
1256 case INTRINSIC_MINUS
:
1257 result
= gfc_subtract (op1
, op2
);
1260 case INTRINSIC_TIMES
:
1261 result
= gfc_multiply (op1
, op2
);
1264 case INTRINSIC_DIVIDE
:
1265 result
= gfc_divide (op1
, op2
);
1268 case INTRINSIC_POWER
:
1269 result
= gfc_power (op1
, op2
);
1272 case INTRINSIC_CONCAT
:
1273 result
= gfc_concat (op1
, op2
);
1277 case INTRINSIC_EQ_OS
:
1278 result
= gfc_eq (op1
, op2
, op
);
1282 case INTRINSIC_NE_OS
:
1283 result
= gfc_ne (op1
, op2
, op
);
1287 case INTRINSIC_GT_OS
:
1288 result
= gfc_gt (op1
, op2
, op
);
1292 case INTRINSIC_GE_OS
:
1293 result
= gfc_ge (op1
, op2
, op
);
1297 case INTRINSIC_LT_OS
:
1298 result
= gfc_lt (op1
, op2
, op
);
1302 case INTRINSIC_LE_OS
:
1303 result
= gfc_le (op1
, op2
, op
);
1307 result
= gfc_not (op1
);
1311 result
= gfc_and (op1
, op2
);
1315 result
= gfc_or (op1
, op2
);
1319 result
= gfc_eqv (op1
, op2
);
1322 case INTRINSIC_NEQV
:
1323 result
= gfc_neqv (op1
, op2
);
1327 gfc_internal_error ("simplify_intrinsic_op(): Bad operator");
1332 gfc_free_expr (op1
);
1333 gfc_free_expr (op2
);
1337 result
->rank
= p
->rank
;
1338 result
->corank
= p
->corank
;
1339 result
->where
= p
->where
;
1340 gfc_replace_expr (p
, result
);
1346 /* Subroutine to simplify constructor expressions. Mutually recursive
1347 with gfc_simplify_expr(). */
1350 simplify_constructor (gfc_constructor_base base
, int type
)
1355 for (c
= gfc_constructor_first (base
); c
; c
= gfc_constructor_next (c
))
1358 && (!gfc_simplify_expr(c
->iterator
->start
, type
)
1359 || !gfc_simplify_expr (c
->iterator
->end
, type
)
1360 || !gfc_simplify_expr (c
->iterator
->step
, type
)))
1365 /* Try and simplify a copy. Replace the original if successful
1366 but keep going through the constructor at all costs. Not
1367 doing so can make a dog's dinner of complicated things. */
1368 p
= gfc_copy_expr (c
->expr
);
1370 if (!gfc_simplify_expr (p
, type
))
1376 gfc_replace_expr (c
->expr
, p
);
1384 /* Pull a single array element out of an array constructor. */
1387 find_array_element (gfc_constructor_base base
, gfc_array_ref
*ar
,
1388 gfc_constructor
**rval
)
1390 unsigned long nelemen
;
1396 gfc_constructor
*cons
;
1403 mpz_init_set_ui (offset
, 0);
1406 mpz_init_set_ui (span
, 1);
1407 for (i
= 0; i
< ar
->dimen
; i
++)
1409 if (!gfc_reduce_init_expr (ar
->as
->lower
[i
])
1410 || !gfc_reduce_init_expr (ar
->as
->upper
[i
])
1411 || ar
->as
->upper
[i
]->expr_type
!= EXPR_CONSTANT
1412 || ar
->as
->lower
[i
]->expr_type
!= EXPR_CONSTANT
)
1420 if (e
->expr_type
!= EXPR_CONSTANT
)
1426 /* Check the bounds. */
1427 if ((ar
->as
->upper
[i
]
1428 && mpz_cmp (e
->value
.integer
,
1429 ar
->as
->upper
[i
]->value
.integer
) > 0)
1430 || (mpz_cmp (e
->value
.integer
,
1431 ar
->as
->lower
[i
]->value
.integer
) < 0))
1433 gfc_error ("Index in dimension %d is out of bounds "
1434 "at %L", i
+ 1, &ar
->c_where
[i
]);
1440 mpz_sub (delta
, e
->value
.integer
, ar
->as
->lower
[i
]->value
.integer
);
1441 mpz_mul (delta
, delta
, span
);
1442 mpz_add (offset
, offset
, delta
);
1444 mpz_set_ui (tmp
, 1);
1445 mpz_add (tmp
, tmp
, ar
->as
->upper
[i
]->value
.integer
);
1446 mpz_sub (tmp
, tmp
, ar
->as
->lower
[i
]->value
.integer
);
1447 mpz_mul (span
, span
, tmp
);
1450 for (cons
= gfc_constructor_first (base
), nelemen
= mpz_get_ui (offset
);
1451 cons
&& nelemen
> 0; cons
= gfc_constructor_next (cons
), nelemen
--)
1470 /* Find a component of a structure constructor. */
1472 static gfc_constructor
*
1473 find_component_ref (gfc_constructor_base base
, gfc_ref
*ref
)
1475 gfc_component
*pick
= ref
->u
.c
.component
;
1476 gfc_constructor
*c
= gfc_constructor_first (base
);
1478 gfc_symbol
*dt
= ref
->u
.c
.sym
;
1479 int ext
= dt
->attr
.extension
;
1481 /* For extended types, check if the desired component is in one of the
1483 while (ext
> 0 && gfc_find_component (dt
->components
->ts
.u
.derived
,
1484 pick
->name
, true, true, NULL
))
1486 dt
= dt
->components
->ts
.u
.derived
;
1487 c
= gfc_constructor_first (c
->expr
->value
.constructor
);
1491 gfc_component
*comp
= dt
->components
;
1492 while (comp
!= pick
)
1495 c
= gfc_constructor_next (c
);
1502 /* Replace an expression with the contents of a constructor, removing
1503 the subobject reference in the process. */
1506 remove_subobject_ref (gfc_expr
*p
, gfc_constructor
*cons
)
1516 e
= gfc_copy_expr (p
);
1517 e
->ref
= p
->ref
->next
;
1518 p
->ref
->next
= NULL
;
1519 gfc_replace_expr (p
, e
);
1523 /* Pull an array section out of an array constructor. */
1526 find_array_section (gfc_expr
*expr
, gfc_ref
*ref
)
1533 long unsigned one
= 1;
1535 mpz_t start
[GFC_MAX_DIMENSIONS
];
1536 mpz_t end
[GFC_MAX_DIMENSIONS
];
1537 mpz_t stride
[GFC_MAX_DIMENSIONS
];
1538 mpz_t delta
[GFC_MAX_DIMENSIONS
];
1539 mpz_t ctr
[GFC_MAX_DIMENSIONS
];
1544 gfc_constructor_base base
;
1545 gfc_constructor
*cons
, *vecsub
[GFC_MAX_DIMENSIONS
];
1555 base
= expr
->value
.constructor
;
1556 expr
->value
.constructor
= NULL
;
1558 rank
= ref
->u
.ar
.as
->rank
;
1560 if (expr
->shape
== NULL
)
1561 expr
->shape
= gfc_get_shape (rank
);
1563 mpz_init_set_ui (delta_mpz
, one
);
1564 mpz_init_set_ui (nelts
, one
);
1568 /* Do the initialization now, so that we can cleanup without
1569 keeping track of where we were. */
1570 for (d
= 0; d
< rank
; d
++)
1572 mpz_init (delta
[d
]);
1573 mpz_init (start
[d
]);
1576 mpz_init (stride
[d
]);
1580 /* Build the counters to clock through the array reference. */
1582 for (d
= 0; d
< rank
; d
++)
1584 /* Make this stretch of code easier on the eye! */
1585 begin
= ref
->u
.ar
.start
[d
];
1586 finish
= ref
->u
.ar
.end
[d
];
1587 step
= ref
->u
.ar
.stride
[d
];
1588 lower
= ref
->u
.ar
.as
->lower
[d
];
1589 upper
= ref
->u
.ar
.as
->upper
[d
];
1591 if (!lower
|| !upper
1592 || lower
->expr_type
!= EXPR_CONSTANT
1593 || upper
->expr_type
!= EXPR_CONSTANT
1594 || lower
->ts
.type
!= BT_INTEGER
1595 || upper
->ts
.type
!= BT_INTEGER
)
1601 if (ref
->u
.ar
.dimen_type
[d
] == DIMEN_VECTOR
) /* Vector subscript. */
1603 gfc_constructor
*ci
;
1606 if (begin
->expr_type
!= EXPR_ARRAY
|| !gfc_is_constant_expr (begin
))
1612 gcc_assert (begin
->rank
== 1);
1613 /* Zero-sized arrays have no shape and no elements, stop early. */
1616 mpz_init_set_ui (nelts
, 0);
1620 vecsub
[d
] = gfc_constructor_first (begin
->value
.constructor
);
1621 mpz_set (ctr
[d
], vecsub
[d
]->expr
->value
.integer
);
1622 mpz_mul (nelts
, nelts
, begin
->shape
[0]);
1623 mpz_set (expr
->shape
[shape_i
++], begin
->shape
[0]);
1626 for (ci
= vecsub
[d
]; ci
; ci
= gfc_constructor_next (ci
))
1628 if (mpz_cmp (ci
->expr
->value
.integer
, upper
->value
.integer
) > 0
1629 || mpz_cmp (ci
->expr
->value
.integer
,
1630 lower
->value
.integer
) < 0)
1632 gfc_error ("index in dimension %d is out of bounds "
1633 "at %L", d
+ 1, &ref
->u
.ar
.c_where
[d
]);
1641 if ((begin
&& begin
->expr_type
!= EXPR_CONSTANT
)
1642 || (finish
&& finish
->expr_type
!= EXPR_CONSTANT
)
1643 || (step
&& step
->expr_type
!= EXPR_CONSTANT
))
1649 /* Obtain the stride. */
1651 mpz_set (stride
[d
], step
->value
.integer
);
1653 mpz_set_ui (stride
[d
], one
);
1655 if (mpz_cmp_ui (stride
[d
], 0) == 0)
1656 mpz_set_ui (stride
[d
], one
);
1658 /* Obtain the start value for the index. */
1660 mpz_set (start
[d
], begin
->value
.integer
);
1662 mpz_set (start
[d
], lower
->value
.integer
);
1664 mpz_set (ctr
[d
], start
[d
]);
1666 /* Obtain the end value for the index. */
1668 mpz_set (end
[d
], finish
->value
.integer
);
1670 mpz_set (end
[d
], upper
->value
.integer
);
1672 /* Separate 'if' because elements sometimes arrive with
1674 if (ref
->u
.ar
.dimen_type
[d
] == DIMEN_ELEMENT
)
1675 mpz_set (end
[d
], begin
->value
.integer
);
1677 /* Check the bounds. */
1678 if (mpz_cmp (ctr
[d
], upper
->value
.integer
) > 0
1679 || mpz_cmp (end
[d
], upper
->value
.integer
) > 0
1680 || mpz_cmp (ctr
[d
], lower
->value
.integer
) < 0
1681 || mpz_cmp (end
[d
], lower
->value
.integer
) < 0)
1683 gfc_error ("index in dimension %d is out of bounds "
1684 "at %L", d
+ 1, &ref
->u
.ar
.c_where
[d
]);
1689 /* Calculate the number of elements and the shape. */
1690 mpz_set (tmp_mpz
, stride
[d
]);
1691 mpz_add (tmp_mpz
, end
[d
], tmp_mpz
);
1692 mpz_sub (tmp_mpz
, tmp_mpz
, ctr
[d
]);
1693 mpz_div (tmp_mpz
, tmp_mpz
, stride
[d
]);
1694 mpz_mul (nelts
, nelts
, tmp_mpz
);
1696 /* An element reference reduces the rank of the expression; don't
1697 add anything to the shape array. */
1698 if (ref
->u
.ar
.dimen_type
[d
] != DIMEN_ELEMENT
)
1699 mpz_set (expr
->shape
[shape_i
++], tmp_mpz
);
1702 /* Calculate the 'stride' (=delta) for conversion of the
1703 counter values into the index along the constructor. */
1704 mpz_set (delta
[d
], delta_mpz
);
1705 mpz_sub (tmp_mpz
, upper
->value
.integer
, lower
->value
.integer
);
1706 mpz_add_ui (tmp_mpz
, tmp_mpz
, one
);
1707 mpz_mul (delta_mpz
, delta_mpz
, tmp_mpz
);
1710 cons
= gfc_constructor_first (base
);
1712 /* Now clock through the array reference, calculating the index in
1713 the source constructor and transferring the elements to the new
1715 for (idx
= 0; idx
< (int) mpz_get_si (nelts
); idx
++)
1717 mpz_init_set_ui (ptr
, 0);
1720 for (d
= 0; d
< rank
; d
++)
1722 mpz_set (tmp_mpz
, ctr
[d
]);
1723 mpz_sub (tmp_mpz
, tmp_mpz
, ref
->u
.ar
.as
->lower
[d
]->value
.integer
);
1724 mpz_mul (tmp_mpz
, tmp_mpz
, delta
[d
]);
1725 mpz_add (ptr
, ptr
, tmp_mpz
);
1727 if (!incr_ctr
) continue;
1729 if (ref
->u
.ar
.dimen_type
[d
] == DIMEN_VECTOR
) /* Vector subscript. */
1731 gcc_assert(vecsub
[d
]);
1733 if (!gfc_constructor_next (vecsub
[d
]))
1734 vecsub
[d
] = gfc_constructor_first (ref
->u
.ar
.start
[d
]->value
.constructor
);
1737 vecsub
[d
] = gfc_constructor_next (vecsub
[d
]);
1740 mpz_set (ctr
[d
], vecsub
[d
]->expr
->value
.integer
);
1744 mpz_add (ctr
[d
], ctr
[d
], stride
[d
]);
1746 if (mpz_cmp_ui (stride
[d
], 0) > 0
1747 ? mpz_cmp (ctr
[d
], end
[d
]) > 0
1748 : mpz_cmp (ctr
[d
], end
[d
]) < 0)
1749 mpz_set (ctr
[d
], start
[d
]);
1755 limit
= mpz_get_ui (ptr
);
1756 if (limit
>= flag_max_array_constructor
)
1758 gfc_error ("The number of elements in the array constructor "
1759 "at %L requires an increase of the allowed %d "
1760 "upper limit. See %<-fmax-array-constructor%> "
1761 "option", &expr
->where
, flag_max_array_constructor
);
1766 cons
= gfc_constructor_lookup (base
, limit
);
1769 gfc_error ("Error in array constructor referenced at %L",
1774 gfc_constructor_append_expr (&expr
->value
.constructor
,
1775 gfc_copy_expr (cons
->expr
), NULL
);
1780 mpz_clear (delta_mpz
);
1781 mpz_clear (tmp_mpz
);
1783 for (d
= 0; d
< rank
; d
++)
1785 mpz_clear (delta
[d
]);
1786 mpz_clear (start
[d
]);
1789 mpz_clear (stride
[d
]);
1792 gfc_constructor_free (base
);
1796 /* Pull a substring out of an expression. */
1799 find_substring_ref (gfc_expr
*p
, gfc_expr
**newp
)
1802 gfc_charlen_t start
;
1803 gfc_charlen_t length
;
1806 if (p
->ref
->u
.ss
.start
->expr_type
!= EXPR_CONSTANT
1807 || p
->ref
->u
.ss
.end
->expr_type
!= EXPR_CONSTANT
)
1810 *newp
= gfc_copy_expr (p
);
1811 free ((*newp
)->value
.character
.string
);
1813 end
= (gfc_charlen_t
) mpz_get_si (p
->ref
->u
.ss
.end
->value
.integer
);
1814 start
= (gfc_charlen_t
) mpz_get_si (p
->ref
->u
.ss
.start
->value
.integer
);
1816 length
= end
- start
+ 1;
1820 chr
= (*newp
)->value
.character
.string
= gfc_get_wide_string (length
+ 1);
1821 (*newp
)->value
.character
.length
= length
;
1822 memcpy (chr
, &p
->value
.character
.string
[start
- 1],
1823 length
* sizeof (gfc_char_t
));
1829 /* Pull an inquiry result out of an expression. */
1832 find_inquiry_ref (gfc_expr
*p
, gfc_expr
**newp
)
1835 gfc_ref
*inquiry
= NULL
;
1838 tmp
= gfc_copy_expr (p
);
1840 if (tmp
->ref
&& tmp
->ref
->type
== REF_INQUIRY
)
1847 for (ref
= tmp
->ref
; ref
; ref
= ref
->next
)
1848 if (ref
->next
&& ref
->next
->type
== REF_INQUIRY
)
1850 inquiry
= ref
->next
;
1857 gfc_free_expr (tmp
);
1861 gfc_resolve_expr (tmp
);
1863 /* Leave these to the backend since the type and kind is not confirmed until
1865 if (IS_INFERRED_TYPE (tmp
))
1868 /* In principle there can be more than one inquiry reference. */
1869 for (; inquiry
; inquiry
= inquiry
->next
)
1871 switch (inquiry
->u
.i
)
1874 if (tmp
->ts
.type
!= BT_CHARACTER
)
1877 if (!gfc_notify_std (GFC_STD_F2003
, "LEN part_ref at %C"))
1880 if (tmp
->ts
.u
.cl
->length
1881 && tmp
->ts
.u
.cl
->length
->expr_type
== EXPR_CONSTANT
)
1882 *newp
= gfc_copy_expr (tmp
->ts
.u
.cl
->length
);
1883 else if (tmp
->expr_type
== EXPR_CONSTANT
)
1884 *newp
= gfc_get_int_expr (gfc_default_integer_kind
,
1885 NULL
, tmp
->value
.character
.length
);
1886 else if (gfc_init_expr_flag
1887 && tmp
->ts
.u
.cl
->length
->symtree
->n
.sym
->attr
.pdt_len
)
1888 *newp
= gfc_pdt_find_component_copy_initializer (tmp
->symtree
->n
1899 if (tmp
->ts
.type
== BT_DERIVED
|| tmp
->ts
.type
== BT_CLASS
)
1902 if (!gfc_notify_std (GFC_STD_F2003
, "KIND part_ref at %C"))
1905 *newp
= gfc_get_int_expr (gfc_default_integer_kind
,
1906 NULL
, tmp
->ts
.kind
);
1910 if (tmp
->ts
.type
!= BT_COMPLEX
|| tmp
->expr_type
!= EXPR_CONSTANT
)
1913 if (!gfc_notify_std (GFC_STD_F2008
, "RE part_ref at %C"))
1916 *newp
= gfc_get_constant_expr (BT_REAL
, tmp
->ts
.kind
, &tmp
->where
);
1917 mpfr_set ((*newp
)->value
.real
,
1918 mpc_realref (tmp
->value
.complex), GFC_RND_MODE
);
1922 if (tmp
->ts
.type
!= BT_COMPLEX
|| tmp
->expr_type
!= EXPR_CONSTANT
)
1925 if (!gfc_notify_std (GFC_STD_F2008
, "IM part_ref at %C"))
1928 *newp
= gfc_get_constant_expr (BT_REAL
, tmp
->ts
.kind
, &tmp
->where
);
1929 mpfr_set ((*newp
)->value
.real
,
1930 mpc_imagref (tmp
->value
.complex), GFC_RND_MODE
);
1933 // TODO: Fix leaking expr tmp, when simplify is done twice.
1935 gfc_replace_expr (tmp
, *newp
);
1940 else if ((*newp
)->expr_type
!= EXPR_CONSTANT
)
1942 gfc_free_expr (*newp
);
1946 gfc_free_expr (tmp
);
1950 gfc_free_expr (tmp
);
1956 /* Simplify a subobject reference of a constructor. This occurs when
1957 parameter variable values are substituted. */
1960 simplify_const_ref (gfc_expr
*p
)
1962 gfc_constructor
*cons
, *c
;
1963 gfc_expr
*newp
= NULL
;
1968 switch (p
->ref
->type
)
1971 switch (p
->ref
->u
.ar
.type
)
1974 /* <type/kind spec>, parameter :: x(<int>) = scalar_expr
1975 will generate this. */
1976 if (p
->expr_type
!= EXPR_ARRAY
)
1978 remove_subobject_ref (p
, NULL
);
1981 if (!find_array_element (p
->value
.constructor
, &p
->ref
->u
.ar
, &cons
))
1987 remove_subobject_ref (p
, cons
);
1991 if (!find_array_section (p
, p
->ref
))
1993 p
->ref
->u
.ar
.type
= AR_FULL
;
1998 if (p
->ref
->next
!= NULL
1999 && (p
->ts
.type
== BT_CHARACTER
|| gfc_bt_struct (p
->ts
.type
)))
2001 for (c
= gfc_constructor_first (p
->value
.constructor
);
2002 c
; c
= gfc_constructor_next (c
))
2004 c
->expr
->ref
= gfc_copy_ref (p
->ref
->next
);
2005 if (!simplify_const_ref (c
->expr
))
2009 if (gfc_bt_struct (p
->ts
.type
)
2011 && (c
= gfc_constructor_first (p
->value
.constructor
)))
2013 /* There may have been component references. */
2014 p
->ts
= c
->expr
->ts
;
2018 for (; last_ref
->next
; last_ref
= last_ref
->next
) {};
2020 if (p
->ts
.type
== BT_CHARACTER
2021 && last_ref
->type
== REF_SUBSTRING
)
2023 /* If this is a CHARACTER array and we possibly took
2024 a substring out of it, update the type-spec's
2025 character length according to the first element
2026 (as all should have the same length). */
2027 gfc_charlen_t string_len
;
2028 if ((c
= gfc_constructor_first (p
->value
.constructor
)))
2030 const gfc_expr
* first
= c
->expr
;
2031 gcc_assert (first
->expr_type
== EXPR_CONSTANT
);
2032 gcc_assert (first
->ts
.type
== BT_CHARACTER
);
2033 string_len
= first
->value
.character
.length
;
2041 p
->ts
.u
.cl
= gfc_new_charlen (p
->symtree
->n
.sym
->ns
,
2044 p
->ts
.u
.cl
= gfc_new_charlen (gfc_current_ns
,
2048 gfc_free_expr (p
->ts
.u
.cl
->length
);
2051 = gfc_get_int_expr (gfc_charlen_int_kind
,
2055 gfc_free_ref_list (p
->ref
);
2066 cons
= find_component_ref (p
->value
.constructor
, p
->ref
);
2067 remove_subobject_ref (p
, cons
);
2071 if (!find_inquiry_ref (p
, &newp
))
2074 gfc_replace_expr (p
, newp
);
2075 gfc_free_ref_list (p
->ref
);
2080 if (!find_substring_ref (p
, &newp
))
2083 gfc_replace_expr (p
, newp
);
2084 gfc_free_ref_list (p
->ref
);
2094 /* Simplify a chain of references. */
2097 simplify_ref_chain (gfc_ref
*ref
, int type
, gfc_expr
**p
)
2100 gfc_expr
*newp
= NULL
;
2102 for (; ref
; ref
= ref
->next
)
2107 for (n
= 0; n
< ref
->u
.ar
.dimen
; n
++)
2109 if (!gfc_simplify_expr (ref
->u
.ar
.start
[n
], type
))
2111 if (!gfc_simplify_expr (ref
->u
.ar
.end
[n
], type
))
2113 if (!gfc_simplify_expr (ref
->u
.ar
.stride
[n
], type
))
2119 if (!gfc_simplify_expr (ref
->u
.ss
.start
, type
))
2121 if (!gfc_simplify_expr (ref
->u
.ss
.end
, type
))
2126 if (!find_inquiry_ref (*p
, &newp
))
2129 gfc_replace_expr (*p
, newp
);
2130 gfc_free_ref_list ((*p
)->ref
);
2142 /* Try to substitute the value of a parameter variable. */
2145 simplify_parameter_variable (gfc_expr
*p
, int type
)
2150 /* Set rank and check array ref; as resolve_variable calls
2151 gfc_simplify_expr, call gfc_resolve_ref + gfc_expression_rank instead. */
2152 if (!gfc_resolve_ref (p
))
2157 gfc_expression_rank (p
);
2159 /* Is this an inquiry? */
2160 bool inquiry
= false;
2161 gfc_ref
* ref
= p
->ref
;
2164 if (ref
->type
== REF_INQUIRY
)
2168 if (ref
&& ref
->type
== REF_INQUIRY
)
2169 inquiry
= ref
->u
.i
== INQUIRY_LEN
|| ref
->u
.i
== INQUIRY_KIND
;
2171 if (gfc_is_size_zero_array (p
))
2173 if (p
->expr_type
== EXPR_ARRAY
)
2176 e
= gfc_get_expr ();
2177 e
->expr_type
= EXPR_ARRAY
;
2180 e
->corank
= p
->corank
;
2181 e
->value
.constructor
= NULL
;
2182 e
->shape
= gfc_copy_shape (p
->shape
, p
->rank
);
2183 e
->where
= p
->where
;
2184 /* If %kind and %len are not used then we're done, otherwise
2185 drop through for simplification. */
2188 gfc_replace_expr (p
, e
);
2194 e
= gfc_copy_expr (p
->symtree
->n
.sym
->value
);
2198 gfc_free_shape (&e
->shape
, e
->rank
);
2199 e
->shape
= gfc_copy_shape (p
->shape
, p
->rank
);
2201 e
->corank
= p
->corank
;
2203 if (e
->ts
.type
== BT_CHARACTER
&& p
->ts
.u
.cl
)
2207 if (e
->ts
.type
== BT_CHARACTER
&& e
->ts
.u
.cl
== NULL
)
2208 e
->ts
.u
.cl
= gfc_new_charlen (gfc_current_ns
, p
->ts
.u
.cl
);
2210 /* Do not copy subobject refs for constant. */
2211 if (e
->expr_type
!= EXPR_CONSTANT
&& p
->ref
!= NULL
)
2212 e
->ref
= gfc_copy_ref (p
->ref
);
2213 t
= gfc_simplify_expr (e
, type
);
2214 e
->where
= p
->where
;
2216 /* Only use the simplification if it eliminated all subobject references. */
2218 gfc_replace_expr (p
, e
);
2227 scalarize_intrinsic_call (gfc_expr
*, bool init_flag
);
2229 /* Given an expression, simplify it by collapsing constant
2230 expressions. Most simplification takes place when the expression
2231 tree is being constructed. If an intrinsic function is simplified
2232 at some point, we get called again to collapse the result against
2235 We work by recursively simplifying expression nodes, simplifying
2236 intrinsic functions where possible, which can lead to further
2237 constant collapsing. If an operator has constant operand(s), we
2238 rip the expression apart, and rebuild it, hoping that it becomes
2241 The expression type is defined for:
2242 0 Basic expression parsing
2243 1 Simplifying array constructors -- will substitute
2245 Returns false on error, true otherwise.
2246 NOTE: Will return true even if the expression cannot be simplified. */
2249 gfc_simplify_expr (gfc_expr
*p
, int type
)
2251 gfc_actual_arglist
*ap
;
2252 gfc_intrinsic_sym
* isym
= NULL
;
2258 switch (p
->expr_type
)
2261 if (p
->ref
&& p
->ref
->type
== REF_INQUIRY
)
2262 simplify_ref_chain (p
->ref
, type
, &p
);
2268 // For array-bound functions, we don't need to optimize
2269 // the 'array' argument. In particular, if the argument
2270 // is a PARAMETER, simplifying might convert an EXPR_VARIABLE
2271 // into an EXPR_ARRAY; the latter has lbound = 1, the former
2272 // can have any lbound.
2273 ap
= p
->value
.function
.actual
;
2274 if (p
->value
.function
.isym
&&
2275 (p
->value
.function
.isym
->id
== GFC_ISYM_LBOUND
2276 || p
->value
.function
.isym
->id
== GFC_ISYM_UBOUND
2277 || p
->value
.function
.isym
->id
== GFC_ISYM_LCOBOUND
2278 || p
->value
.function
.isym
->id
== GFC_ISYM_UCOBOUND
2279 || p
->value
.function
.isym
->id
== GFC_ISYM_SHAPE
))
2282 for ( ; ap
; ap
= ap
->next
)
2283 if (!gfc_simplify_expr (ap
->expr
, type
))
2286 if (p
->value
.function
.isym
!= NULL
2287 && gfc_intrinsic_func_interface (p
, 1) == MATCH_ERROR
)
2290 if (p
->symtree
&& (p
->value
.function
.isym
|| p
->ts
.type
== BT_UNKNOWN
))
2292 isym
= gfc_find_function (p
->symtree
->n
.sym
->name
);
2293 if (isym
&& isym
->elemental
)
2294 scalarize_intrinsic_call (p
, false);
2299 case EXPR_SUBSTRING
:
2300 if (!simplify_ref_chain (p
->ref
, type
, &p
))
2303 if (gfc_is_constant_expr (p
))
2306 HOST_WIDE_INT start
, end
;
2309 if (p
->ref
&& p
->ref
->u
.ss
.start
)
2311 gfc_extract_hwi (p
->ref
->u
.ss
.start
, &start
);
2312 start
--; /* Convert from one-based to zero-based. */
2315 end
= p
->value
.character
.length
;
2316 if (p
->ref
&& p
->ref
->u
.ss
.end
)
2317 gfc_extract_hwi (p
->ref
->u
.ss
.end
, &end
);
2322 s
= gfc_get_wide_string (end
- start
+ 2);
2323 memcpy (s
, p
->value
.character
.string
+ start
,
2324 (end
- start
) * sizeof (gfc_char_t
));
2325 s
[end
- start
+ 1] = '\0'; /* TODO: C-style string. */
2326 free (p
->value
.character
.string
);
2327 p
->value
.character
.string
= s
;
2328 p
->value
.character
.length
= end
- start
;
2329 p
->ts
.u
.cl
= gfc_new_charlen (gfc_current_ns
, NULL
);
2330 p
->ts
.u
.cl
->length
= gfc_get_int_expr (gfc_charlen_int_kind
,
2332 p
->value
.character
.length
);
2333 gfc_free_ref_list (p
->ref
);
2335 p
->expr_type
= EXPR_CONSTANT
;
2340 if (!simplify_intrinsic_op (p
, type
))
2345 /* Only substitute array parameter variables if we are in an
2346 initialization expression, or we want a subsection. */
2347 if (p
->symtree
->n
.sym
->attr
.flavor
== FL_PARAMETER
2348 && (gfc_init_expr_flag
|| p
->ref
2349 || (p
->symtree
->n
.sym
->value
2350 && p
->symtree
->n
.sym
->value
->expr_type
!= EXPR_ARRAY
)))
2352 if (!simplify_parameter_variable (p
, type
))
2359 gfc_simplify_iterator_var (p
);
2362 /* Simplify subcomponent references. */
2363 if (!simplify_ref_chain (p
->ref
, type
, &p
))
2368 case EXPR_STRUCTURE
:
2370 if (!simplify_ref_chain (p
->ref
, type
, &p
))
2373 /* If the following conditions hold, we found something like kind type
2374 inquiry of the form a(2)%kind while simplify the ref chain. */
2375 if (p
->expr_type
== EXPR_CONSTANT
&& !p
->ref
&& !p
->rank
&& !p
->shape
)
2378 if (!simplify_constructor (p
->value
.constructor
, type
))
2381 if (p
->expr_type
== EXPR_ARRAY
&& p
->ref
&& p
->ref
->type
== REF_ARRAY
2382 && p
->ref
->u
.ar
.type
== AR_FULL
)
2383 gfc_expand_constructor (p
, false);
2385 if (!simplify_const_ref (p
))
2402 /* Try simplification of an expression via gfc_simplify_expr.
2403 When an error occurs (arithmetic or otherwise), roll back. */
2406 gfc_try_simplify_expr (gfc_expr
*e
, int type
)
2411 if (e
== NULL
|| e
->expr_type
== EXPR_CONSTANT
)
2414 saved_div0
= gfc_seen_div0
;
2415 gfc_seen_div0
= false;
2416 n
= gfc_copy_expr (e
);
2417 t
= gfc_simplify_expr (n
, type
) && !gfc_seen_div0
;
2419 gfc_replace_expr (e
, n
);
2422 gfc_seen_div0
= saved_div0
;
2427 /* Returns the type of an expression with the exception that iterator
2428 variables are automatically integers no matter what else they may
2434 if (e
->expr_type
== EXPR_VARIABLE
&& gfc_check_iter_variable (e
))
2441 /* Scalarize an expression for an elemental intrinsic call. */
2444 scalarize_intrinsic_call (gfc_expr
*e
, bool init_flag
)
2446 gfc_actual_arglist
*a
, *b
;
2447 gfc_constructor_base ctor
;
2448 gfc_constructor
*args
[5] = {}; /* Avoid uninitialized warnings. */
2449 gfc_constructor
*ci
, *new_ctor
;
2450 gfc_expr
*expr
, *old
, *p
;
2451 int n
, i
, rank
[5], array_arg
;
2456 a
= e
->value
.function
.actual
;
2457 for (; a
; a
= a
->next
)
2458 if (a
->expr
&& !gfc_is_constant_expr (a
->expr
))
2461 /* Find which, if any, arguments are arrays. Assume that the old
2462 expression carries the type information and that the first arg
2463 that is an array expression carries all the shape information.*/
2465 a
= e
->value
.function
.actual
;
2466 for (; a
; a
= a
->next
)
2469 if (!a
->expr
|| a
->expr
->expr_type
!= EXPR_ARRAY
)
2472 expr
= gfc_copy_expr (a
->expr
);
2479 old
= gfc_copy_expr (e
);
2481 gfc_constructor_free (expr
->value
.constructor
);
2482 expr
->value
.constructor
= NULL
;
2484 expr
->where
= old
->where
;
2485 expr
->expr_type
= EXPR_ARRAY
;
2487 /* Copy the array argument constructors into an array, with nulls
2490 a
= old
->value
.function
.actual
;
2491 for (; a
; a
= a
->next
)
2493 /* Check that this is OK for an initialization expression. */
2494 if (a
->expr
&& init_flag
&& !gfc_check_init_expr (a
->expr
))
2498 if (a
->expr
&& a
->expr
->rank
&& a
->expr
->expr_type
== EXPR_VARIABLE
)
2500 rank
[n
] = a
->expr
->rank
;
2501 ctor
= a
->expr
->symtree
->n
.sym
->value
->value
.constructor
;
2502 args
[n
] = gfc_constructor_first (ctor
);
2504 else if (a
->expr
&& a
->expr
->expr_type
== EXPR_ARRAY
)
2507 rank
[n
] = a
->expr
->rank
;
2510 ctor
= gfc_constructor_copy (a
->expr
->value
.constructor
);
2511 args
[n
] = gfc_constructor_first (ctor
);
2519 /* Using the array argument as the master, step through the array
2520 calling the function for each element and advancing the array
2521 constructors together. */
2522 for (ci
= args
[array_arg
- 1]; ci
; ci
= gfc_constructor_next (ci
))
2524 new_ctor
= gfc_constructor_append_expr (&expr
->value
.constructor
,
2525 gfc_copy_expr (old
), NULL
);
2527 gfc_free_actual_arglist (new_ctor
->expr
->value
.function
.actual
);
2529 b
= old
->value
.function
.actual
;
2530 for (i
= 0; i
< n
; i
++)
2533 new_ctor
->expr
->value
.function
.actual
2534 = a
= gfc_get_actual_arglist ();
2537 a
->next
= gfc_get_actual_arglist ();
2542 a
->expr
= gfc_copy_expr (args
[i
]->expr
);
2544 a
->expr
= gfc_copy_expr (b
->expr
);
2549 /* Simplify the function calls. If the simplification fails, the
2550 error will be flagged up down-stream or the library will deal
2552 p
= gfc_copy_expr (new_ctor
->expr
);
2554 if (!gfc_simplify_expr (p
, init_flag
))
2557 gfc_replace_expr (new_ctor
->expr
, p
);
2559 for (i
= 0; i
< n
; i
++)
2561 args
[i
] = gfc_constructor_next (args
[i
]);
2563 for (i
= 1; i
< n
; i
++)
2564 if (rank
[i
] && ((args
[i
] != NULL
&& args
[array_arg
- 1] == NULL
)
2565 || (args
[i
] == NULL
&& args
[array_arg
- 1] != NULL
)))
2571 /* Free "expr" but not the pointers it contains. */
2573 gfc_free_expr (old
);
2577 gfc_error_now ("elemental function arguments at %C are not compliant");
2580 gfc_free_expr (expr
);
2581 gfc_free_expr (old
);
2587 check_intrinsic_op (gfc_expr
*e
, bool (*check_function
) (gfc_expr
*))
2589 gfc_expr
*op1
= e
->value
.op
.op1
;
2590 gfc_expr
*op2
= e
->value
.op
.op2
;
2592 if (!(*check_function
)(op1
))
2595 switch (e
->value
.op
.op
)
2597 case INTRINSIC_UPLUS
:
2598 case INTRINSIC_UMINUS
:
2599 if (!numeric_type (et0 (op1
)))
2604 case INTRINSIC_EQ_OS
:
2606 case INTRINSIC_NE_OS
:
2608 case INTRINSIC_GT_OS
:
2610 case INTRINSIC_GE_OS
:
2612 case INTRINSIC_LT_OS
:
2614 case INTRINSIC_LE_OS
:
2615 if (!(*check_function
)(op2
))
2618 if (!(et0 (op1
) == BT_CHARACTER
&& et0 (op2
) == BT_CHARACTER
)
2619 && !(numeric_type (et0 (op1
)) && numeric_type (et0 (op2
))))
2621 gfc_error ("Numeric or CHARACTER operands are required in "
2622 "expression at %L", &e
->where
);
2627 case INTRINSIC_PLUS
:
2628 case INTRINSIC_MINUS
:
2629 case INTRINSIC_TIMES
:
2630 case INTRINSIC_DIVIDE
:
2631 case INTRINSIC_POWER
:
2632 if (!(*check_function
)(op2
))
2635 if (!numeric_type (et0 (op1
)) || !numeric_type (et0 (op2
)))
2640 case INTRINSIC_CONCAT
:
2641 if (!(*check_function
)(op2
))
2644 if (et0 (op1
) != BT_CHARACTER
|| et0 (op2
) != BT_CHARACTER
)
2646 gfc_error ("Concatenation operator in expression at %L "
2647 "must have two CHARACTER operands", &op1
->where
);
2651 if (op1
->ts
.kind
!= op2
->ts
.kind
)
2653 gfc_error ("Concat operator at %L must concatenate strings of the "
2654 "same kind", &e
->where
);
2661 if (et0 (op1
) != BT_LOGICAL
)
2663 gfc_error (".NOT. operator in expression at %L must have a LOGICAL "
2664 "operand", &op1
->where
);
2673 case INTRINSIC_NEQV
:
2674 if (!(*check_function
)(op2
))
2677 if (et0 (op1
) != BT_LOGICAL
|| et0 (op2
) != BT_LOGICAL
)
2679 gfc_error ("LOGICAL operands are required in expression at %L",
2686 case INTRINSIC_PARENTHESES
:
2690 gfc_error ("Only intrinsic operators can be used in expression at %L",
2698 gfc_error ("Numeric operands are required in expression at %L", &e
->where
);
2703 /* F2003, 7.1.7 (3): In init expression, allocatable components
2704 must not be data-initialized. */
2706 check_alloc_comp_init (gfc_expr
*e
)
2708 gfc_component
*comp
;
2709 gfc_constructor
*ctor
;
2711 gcc_assert (e
->expr_type
== EXPR_STRUCTURE
);
2712 gcc_assert (e
->ts
.type
== BT_DERIVED
|| e
->ts
.type
== BT_CLASS
);
2714 for (comp
= e
->ts
.u
.derived
->components
,
2715 ctor
= gfc_constructor_first (e
->value
.constructor
);
2716 comp
; comp
= comp
->next
, ctor
= gfc_constructor_next (ctor
))
2718 if (comp
->attr
.allocatable
&& ctor
->expr
2719 && ctor
->expr
->expr_type
!= EXPR_NULL
)
2721 gfc_error ("Invalid initialization expression for ALLOCATABLE "
2722 "component %qs in structure constructor at %L",
2723 comp
->name
, &ctor
->expr
->where
);
2732 check_init_expr_arguments (gfc_expr
*e
)
2734 gfc_actual_arglist
*ap
;
2736 for (ap
= e
->value
.function
.actual
; ap
; ap
= ap
->next
)
2737 if (!gfc_check_init_expr (ap
->expr
))
2743 static bool check_restricted (gfc_expr
*);
2745 /* F95, 7.1.6.1, Initialization expressions, (7)
2746 F2003, 7.1.7 Initialization expression, (8)
2747 F2008, 7.1.12 Constant expression, (4) */
2750 check_inquiry (gfc_expr
*e
, int not_restricted
)
2753 const char *const *functions
;
2755 static const char *const inquiry_func_f95
[] = {
2756 "lbound", "shape", "size", "ubound",
2757 "bit_size", "len", "kind",
2758 "digits", "epsilon", "huge", "maxexponent", "minexponent",
2759 "precision", "radix", "range", "tiny",
2763 static const char *const inquiry_func_f2003
[] = {
2764 "lbound", "shape", "size", "ubound",
2765 "bit_size", "len", "kind",
2766 "digits", "epsilon", "huge", "maxexponent", "minexponent",
2767 "precision", "radix", "range", "tiny",
2771 /* std=f2008+ or -std=gnu */
2772 static const char *const inquiry_func_gnu
[] = {
2773 "lbound", "shape", "size", "ubound",
2774 "bit_size", "len", "kind",
2775 "digits", "epsilon", "huge", "maxexponent", "minexponent",
2776 "precision", "radix", "range", "tiny",
2777 "new_line", "storage_size", NULL
2781 gfc_actual_arglist
*ap
;
2785 if (!e
->value
.function
.isym
2786 || !e
->value
.function
.isym
->inquiry
)
2789 /* An undeclared parameter will get us here (PR25018). */
2790 if (e
->symtree
== NULL
)
2793 sym
= e
->symtree
->n
.sym
;
2795 if (sym
->from_intmod
)
2797 if (sym
->from_intmod
== INTMOD_ISO_FORTRAN_ENV
2798 && sym
->intmod_sym_id
!= ISOFORTRAN_COMPILER_OPTIONS
2799 && sym
->intmod_sym_id
!= ISOFORTRAN_COMPILER_VERSION
)
2802 if (sym
->from_intmod
== INTMOD_ISO_C_BINDING
2803 && sym
->intmod_sym_id
!= ISOCBINDING_C_SIZEOF
)
2810 functions
= inquiry_func_gnu
;
2811 if (gfc_option
.warn_std
& GFC_STD_F2003
)
2812 functions
= inquiry_func_f2003
;
2813 if (gfc_option
.warn_std
& GFC_STD_F95
)
2814 functions
= inquiry_func_f95
;
2816 for (i
= 0; functions
[i
]; i
++)
2817 if (strcmp (functions
[i
], name
) == 0)
2820 if (functions
[i
] == NULL
)
2824 /* At this point we have an inquiry function with a variable argument. The
2825 type of the variable might be undefined, but we need it now, because the
2826 arguments of these functions are not allowed to be undefined. */
2828 for (ap
= e
->value
.function
.actual
; ap
; ap
= ap
->next
)
2833 asym
= ap
->expr
->symtree
? ap
->expr
->symtree
->n
.sym
: NULL
;
2835 if (ap
->expr
->ts
.type
== BT_UNKNOWN
)
2837 if (asym
&& asym
->ts
.type
== BT_UNKNOWN
2838 && !gfc_set_default_type (asym
, 0, gfc_current_ns
))
2841 ap
->expr
->ts
= asym
->ts
;
2844 if (asym
&& asym
->assoc
&& asym
->assoc
->target
2845 && asym
->assoc
->target
->expr_type
== EXPR_CONSTANT
)
2847 gfc_free_expr (ap
->expr
);
2848 ap
->expr
= gfc_copy_expr (asym
->assoc
->target
);
2851 /* Assumed character length will not reduce to a constant expression
2852 with LEN, as required by the standard. */
2853 if (i
== 5 && not_restricted
&& asym
2854 && asym
->ts
.type
== BT_CHARACTER
2855 && ((asym
->ts
.u
.cl
&& asym
->ts
.u
.cl
->length
== NULL
)
2856 || asym
->ts
.deferred
))
2858 gfc_error ("Assumed or deferred character length variable %qs "
2859 "in constant expression at %L",
2860 asym
->name
, &ap
->expr
->where
);
2863 else if (not_restricted
&& !gfc_check_init_expr (ap
->expr
))
2866 if (not_restricted
== 0
2867 && ap
->expr
->expr_type
!= EXPR_VARIABLE
2868 && !check_restricted (ap
->expr
))
2871 if (not_restricted
== 0
2872 && ap
->expr
->expr_type
== EXPR_VARIABLE
2873 && asym
->attr
.dummy
&& asym
->attr
.optional
)
2881 /* F95, 7.1.6.1, Initialization expressions, (5)
2882 F2003, 7.1.7 Initialization expression, (5) */
2885 check_transformational (gfc_expr
*e
)
2887 static const char * const trans_func_f95
[] = {
2888 "repeat", "reshape", "selected_int_kind",
2889 "selected_real_kind", "transfer", "trim", NULL
2892 static const char * const trans_func_f2003
[] = {
2893 "all", "any", "count", "dot_product", "matmul", "null", "pack",
2894 "product", "repeat", "reshape", "selected_char_kind", "selected_int_kind",
2895 "selected_real_kind", "spread", "sum", "transfer", "transpose",
2896 "trim", "unpack", NULL
2899 static const char * const trans_func_f2008
[] = {
2900 "all", "any", "count", "dot_product", "matmul", "null", "pack",
2901 "product", "repeat", "reshape", "selected_char_kind", "selected_int_kind",
2902 "selected_real_kind", "spread", "sum", "transfer", "transpose",
2903 "trim", "unpack", "findloc", NULL
2906 static const char * const trans_func_f2023
[] = {
2907 "all", "any", "count", "dot_product", "matmul", "null", "pack",
2908 "product", "repeat", "reshape", "selected_char_kind", "selected_int_kind",
2909 "selected_logical_kind", "selected_real_kind", "spread", "sum", "transfer",
2910 "transpose", "trim", "unpack", "findloc", NULL
2915 const char *const *functions
;
2917 if (!e
->value
.function
.isym
2918 || !e
->value
.function
.isym
->transformational
)
2921 name
= e
->symtree
->n
.sym
->name
;
2923 if (gfc_option
.allow_std
& GFC_STD_F2023
)
2924 functions
= trans_func_f2023
;
2925 else if (gfc_option
.allow_std
& GFC_STD_F2008
)
2926 functions
= trans_func_f2008
;
2927 else if (gfc_option
.allow_std
& GFC_STD_F2003
)
2928 functions
= trans_func_f2003
;
2930 functions
= trans_func_f95
;
2932 /* NULL() is dealt with below. */
2933 if (strcmp ("null", name
) == 0)
2936 for (i
= 0; functions
[i
]; i
++)
2937 if (strcmp (functions
[i
], name
) == 0)
2940 if (functions
[i
] == NULL
)
2942 gfc_error ("transformational intrinsic %qs at %L is not permitted "
2943 "in an initialization expression", name
, &e
->where
);
2947 return check_init_expr_arguments (e
);
2951 /* F95, 7.1.6.1, Initialization expressions, (6)
2952 F2003, 7.1.7 Initialization expression, (6) */
2955 check_null (gfc_expr
*e
)
2957 if (strcmp ("null", e
->symtree
->n
.sym
->name
) != 0)
2960 return check_init_expr_arguments (e
);
2965 check_elemental (gfc_expr
*e
)
2967 if (!e
->value
.function
.isym
2968 || !e
->value
.function
.isym
->elemental
)
2971 if (e
->ts
.type
!= BT_INTEGER
2972 && e
->ts
.type
!= BT_CHARACTER
2973 && !gfc_notify_std (GFC_STD_F2003
, "Evaluation of nonstandard "
2974 "initialization expression at %L", &e
->where
))
2977 return check_init_expr_arguments (e
);
2982 check_conversion (gfc_expr
*e
)
2984 if (!e
->value
.function
.isym
2985 || !e
->value
.function
.isym
->conversion
)
2988 return check_init_expr_arguments (e
);
2992 /* Verify that an expression is an initialization expression. A side
2993 effect is that the expression tree is reduced to a single constant
2994 node if all goes well. This would normally happen when the
2995 expression is constructed but function references are assumed to be
2996 intrinsics in the context of initialization expressions. If
2997 false is returned an error message has been generated. */
3000 gfc_check_init_expr (gfc_expr
*e
)
3008 switch (e
->expr_type
)
3011 t
= check_intrinsic_op (e
, gfc_check_init_expr
);
3013 t
= gfc_simplify_expr (e
, 0);
3022 gfc_intrinsic_sym
* isym
= NULL
;
3023 gfc_symbol
* sym
= e
->symtree
->n
.sym
;
3025 /* Simplify here the intrinsics from the IEEE_ARITHMETIC and
3026 IEEE_EXCEPTIONS modules. */
3027 int mod
= sym
->from_intmod
;
3028 if (mod
== INTMOD_NONE
&& sym
->generic
)
3029 mod
= sym
->generic
->sym
->from_intmod
;
3030 if (mod
== INTMOD_IEEE_ARITHMETIC
|| mod
== INTMOD_IEEE_EXCEPTIONS
)
3032 gfc_expr
*new_expr
= gfc_simplify_ieee_functions (e
);
3035 gfc_replace_expr (e
, new_expr
);
3041 /* If a conversion function, e.g., __convert_i8_i4, was inserted
3042 into an array constructor, we need to skip the error check here.
3043 Conversion errors are caught below in scalarize_intrinsic_call. */
3044 conversion
= e
->value
.function
.isym
3045 && (e
->value
.function
.isym
->conversion
== 1);
3047 if (!conversion
&& (!gfc_is_intrinsic (sym
, 0, e
->where
)
3048 || (m
= gfc_intrinsic_func_interface (e
, 0)) == MATCH_NO
))
3050 gfc_error ("Function %qs in initialization expression at %L "
3051 "must be an intrinsic function",
3052 e
->symtree
->n
.sym
->name
, &e
->where
);
3056 if ((m
= check_conversion (e
)) == MATCH_NO
3057 && (m
= check_inquiry (e
, 1)) == MATCH_NO
3058 && (m
= check_null (e
)) == MATCH_NO
3059 && (m
= check_transformational (e
)) == MATCH_NO
3060 && (m
= check_elemental (e
)) == MATCH_NO
)
3062 gfc_error ("Intrinsic function %qs at %L is not permitted "
3063 "in an initialization expression",
3064 e
->symtree
->n
.sym
->name
, &e
->where
);
3068 if (m
== MATCH_ERROR
)
3071 /* Try to scalarize an elemental intrinsic function that has an
3073 isym
= gfc_find_function (e
->symtree
->n
.sym
->name
);
3074 if (isym
&& isym
->elemental
3075 && (t
= scalarize_intrinsic_call (e
, true)))
3080 t
= gfc_simplify_expr (e
, 0);
3087 /* This occurs when parsing pdt templates. */
3088 if (gfc_expr_attr (e
).pdt_kind
)
3091 if (gfc_check_iter_variable (e
))
3094 if (e
->symtree
->n
.sym
->attr
.flavor
== FL_PARAMETER
)
3096 /* A PARAMETER shall not be used to define itself, i.e.
3097 REAL, PARAMETER :: x = transfer(0, x)
3099 if (!e
->symtree
->n
.sym
->value
)
3101 gfc_error ("PARAMETER %qs is used at %L before its definition "
3102 "is complete", e
->symtree
->n
.sym
->name
, &e
->where
);
3106 t
= simplify_parameter_variable (e
, 0);
3111 if (gfc_in_match_data ())
3116 if (e
->symtree
->n
.sym
->as
)
3118 switch (e
->symtree
->n
.sym
->as
->type
)
3120 case AS_ASSUMED_SIZE
:
3121 gfc_error ("Assumed size array %qs at %L is not permitted "
3122 "in an initialization expression",
3123 e
->symtree
->n
.sym
->name
, &e
->where
);
3126 case AS_ASSUMED_SHAPE
:
3127 gfc_error ("Assumed shape array %qs at %L is not permitted "
3128 "in an initialization expression",
3129 e
->symtree
->n
.sym
->name
, &e
->where
);
3133 if (!e
->symtree
->n
.sym
->attr
.allocatable
3134 && !e
->symtree
->n
.sym
->attr
.pointer
3135 && e
->symtree
->n
.sym
->attr
.dummy
)
3136 gfc_error ("Assumed-shape array %qs at %L is not permitted "
3137 "in an initialization expression",
3138 e
->symtree
->n
.sym
->name
, &e
->where
);
3140 gfc_error ("Deferred array %qs at %L is not permitted "
3141 "in an initialization expression",
3142 e
->symtree
->n
.sym
->name
, &e
->where
);
3146 gfc_error ("Array %qs at %L is a variable, which does "
3147 "not reduce to a constant expression",
3148 e
->symtree
->n
.sym
->name
, &e
->where
);
3151 case AS_ASSUMED_RANK
:
3152 gfc_error ("Assumed-rank array %qs at %L is not permitted "
3153 "in an initialization expression",
3154 e
->symtree
->n
.sym
->name
, &e
->where
);
3162 gfc_error ("Parameter %qs at %L has not been declared or is "
3163 "a variable, which does not reduce to a constant "
3164 "expression", e
->symtree
->name
, &e
->where
);
3173 case EXPR_SUBSTRING
:
3176 t
= gfc_check_init_expr (e
->ref
->u
.ss
.start
);
3180 t
= gfc_check_init_expr (e
->ref
->u
.ss
.end
);
3182 t
= gfc_simplify_expr (e
, 0);
3188 case EXPR_STRUCTURE
:
3189 t
= e
->ts
.is_iso_c
? true : false;
3193 t
= check_alloc_comp_init (e
);
3197 t
= gfc_check_constructor (e
, gfc_check_init_expr
);
3204 t
= gfc_check_constructor (e
, gfc_check_init_expr
);
3208 t
= gfc_expand_constructor (e
, true);
3212 t
= gfc_check_constructor_type (e
);
3216 gfc_internal_error ("check_init_expr(): Unknown expression type");
3222 /* Reduces a general expression to an initialization expression (a constant).
3223 This used to be part of gfc_match_init_expr.
3224 Note that this function doesn't free the given expression on false. */
3227 gfc_reduce_init_expr (gfc_expr
*expr
)
3231 /* It is far too early to resolve a class compcall. Punt to resolution. */
3232 if (expr
&& expr
->expr_type
== EXPR_COMPCALL
3233 && expr
->symtree
->n
.sym
->ts
.type
== BT_CLASS
)
3236 gfc_init_expr_flag
= true;
3237 t
= gfc_resolve_expr (expr
);
3239 t
= gfc_check_init_expr (expr
);
3240 gfc_init_expr_flag
= false;
3245 if (expr
->expr_type
== EXPR_ARRAY
)
3247 if (!gfc_check_constructor_type (expr
))
3249 if (!gfc_expand_constructor (expr
, true))
3257 /* Match an initialization expression. We work by first matching an
3258 expression, then reducing it to a constant. */
3261 gfc_match_init_expr (gfc_expr
**result
)
3269 gfc_init_expr_flag
= true;
3271 m
= gfc_match_expr (&expr
);
3274 gfc_init_expr_flag
= false;
3278 if (expr
->expr_type
!= EXPR_FUNCTION
&& gfc_derived_parameter_expr (expr
))
3281 gfc_init_expr_flag
= false;
3285 t
= gfc_reduce_init_expr (expr
);
3288 gfc_free_expr (expr
);
3289 gfc_init_expr_flag
= false;
3294 gfc_init_expr_flag
= false;
3300 /* Given an actual argument list, test to see that each argument is a
3301 restricted expression and optionally if the expression type is
3302 integer or character. */
3305 restricted_args (gfc_actual_arglist
*a
)
3307 for (; a
; a
= a
->next
)
3309 if (!check_restricted (a
->expr
))
3317 /************* Restricted/specification expressions *************/
3320 /* Make sure a non-intrinsic function is a specification function,
3321 * see F08:7.1.11.5. */
3324 external_spec_function (gfc_expr
*e
)
3328 f
= e
->value
.function
.esym
;
3330 /* IEEE functions allowed are "a reference to a transformational function
3331 from the intrinsic module IEEE_ARITHMETIC or IEEE_EXCEPTIONS", and
3332 "inquiry function from the intrinsic modules IEEE_ARITHMETIC and
3333 IEEE_EXCEPTIONS". */
3334 if (f
->from_intmod
== INTMOD_IEEE_ARITHMETIC
3335 || f
->from_intmod
== INTMOD_IEEE_EXCEPTIONS
)
3337 if (!strcmp (f
->name
, "ieee_selected_real_kind")
3338 || !strcmp (f
->name
, "ieee_support_rounding")
3339 || !strcmp (f
->name
, "ieee_support_flag")
3340 || !strcmp (f
->name
, "ieee_support_halting")
3341 || !strcmp (f
->name
, "ieee_support_datatype")
3342 || !strcmp (f
->name
, "ieee_support_denormal")
3343 || !strcmp (f
->name
, "ieee_support_subnormal")
3344 || !strcmp (f
->name
, "ieee_support_divide")
3345 || !strcmp (f
->name
, "ieee_support_inf")
3346 || !strcmp (f
->name
, "ieee_support_io")
3347 || !strcmp (f
->name
, "ieee_support_nan")
3348 || !strcmp (f
->name
, "ieee_support_sqrt")
3349 || !strcmp (f
->name
, "ieee_support_standard")
3350 || !strcmp (f
->name
, "ieee_support_underflow_control"))
3351 goto function_allowed
;
3354 if (f
->attr
.proc
== PROC_ST_FUNCTION
)
3356 gfc_error ("Specification function %qs at %L cannot be a statement "
3357 "function", f
->name
, &e
->where
);
3361 if (f
->attr
.proc
== PROC_INTERNAL
)
3363 gfc_error ("Specification function %qs at %L cannot be an internal "
3364 "function", f
->name
, &e
->where
);
3368 if (!f
->attr
.pure
&& !f
->attr
.elemental
)
3370 gfc_error ("Specification function %qs at %L must be PURE", f
->name
,
3376 if (f
->attr
.recursive
3377 && !gfc_notify_std (GFC_STD_F2003
,
3378 "Specification function %qs "
3379 "at %L cannot be RECURSIVE", f
->name
, &e
->where
))
3383 return restricted_args (e
->value
.function
.actual
);
3387 /* Check to see that a function reference to an intrinsic is a
3388 restricted expression. */
3391 restricted_intrinsic (gfc_expr
*e
)
3393 /* TODO: Check constraints on inquiry functions. 7.1.6.2 (7). */
3394 if (check_inquiry (e
, 0) == MATCH_YES
)
3397 return restricted_args (e
->value
.function
.actual
);
3401 /* Check the expressions of an actual arglist. Used by check_restricted. */
3404 check_arglist (gfc_actual_arglist
* arg
, bool (*checker
) (gfc_expr
*))
3406 for (; arg
; arg
= arg
->next
)
3407 if (!checker (arg
->expr
))
3414 /* Check the subscription expressions of a reference chain with a checking
3415 function; used by check_restricted. */
3418 check_references (gfc_ref
* ref
, bool (*checker
) (gfc_expr
*))
3428 for (dim
= 0; dim
< ref
->u
.ar
.dimen
; ++dim
)
3430 if (!checker (ref
->u
.ar
.start
[dim
]))
3432 if (!checker (ref
->u
.ar
.end
[dim
]))
3434 if (!checker (ref
->u
.ar
.stride
[dim
]))
3440 /* Nothing needed, just proceed to next reference. */
3444 if (!checker (ref
->u
.ss
.start
))
3446 if (!checker (ref
->u
.ss
.end
))
3455 return check_references (ref
->next
, checker
);
3458 /* Return true if ns is a parent of the current ns. */
3461 is_parent_of_current_ns (gfc_namespace
*ns
)
3464 for (p
= gfc_current_ns
->parent
; p
; p
= p
->parent
)
3471 /* Verify that an expression is a restricted expression. Like its
3472 cousin check_init_expr(), an error message is generated if we
3476 check_restricted (gfc_expr
*e
)
3484 switch (e
->expr_type
)
3487 t
= check_intrinsic_op (e
, check_restricted
);
3489 t
= gfc_simplify_expr (e
, 0);
3494 if (e
->value
.function
.esym
)
3496 t
= check_arglist (e
->value
.function
.actual
, &check_restricted
);
3498 t
= external_spec_function (e
);
3502 if (e
->value
.function
.isym
&& e
->value
.function
.isym
->inquiry
)
3505 t
= check_arglist (e
->value
.function
.actual
, &check_restricted
);
3508 t
= restricted_intrinsic (e
);
3513 sym
= e
->symtree
->n
.sym
;
3516 /* If a dummy argument appears in a context that is valid for a
3517 restricted expression in an elemental procedure, it will have
3518 already been simplified away once we get here. Therefore we
3519 don't need to jump through hoops to distinguish valid from
3520 invalid cases. Allowed in F2008 and F2018. */
3521 if (gfc_notification_std (GFC_STD_F2008
)
3522 && sym
->attr
.dummy
&& sym
->ns
== gfc_current_ns
3523 && sym
->ns
->proc_name
&& sym
->ns
->proc_name
->attr
.elemental
)
3525 gfc_error_now ("Dummy argument %qs not "
3526 "allowed in expression at %L",
3527 sym
->name
, &e
->where
);
3531 if (sym
->attr
.optional
)
3533 gfc_error ("Dummy argument %qs at %L cannot be OPTIONAL",
3534 sym
->name
, &e
->where
);
3538 if (sym
->attr
.intent
== INTENT_OUT
)
3540 gfc_error ("Dummy argument %qs at %L cannot be INTENT(OUT)",
3541 sym
->name
, &e
->where
);
3545 /* Check reference chain if any. */
3546 if (!check_references (e
->ref
, &check_restricted
))
3550 || sym
->attr
.in_common
3551 || sym
->attr
.use_assoc
3552 || sym
->attr
.used_in_submodule
3554 || sym
->attr
.implied_index
3555 || sym
->attr
.flavor
== FL_PARAMETER
3556 || is_parent_of_current_ns (gfc_get_spec_ns (sym
)))
3562 gfc_error ("Variable %qs cannot appear in the expression at %L",
3563 sym
->name
, &e
->where
);
3564 /* Prevent a repetition of the error. */
3573 case EXPR_SUBSTRING
:
3574 t
= gfc_specification_expr (e
->ref
->u
.ss
.start
);
3578 t
= gfc_specification_expr (e
->ref
->u
.ss
.end
);
3580 t
= gfc_simplify_expr (e
, 0);
3584 case EXPR_STRUCTURE
:
3585 t
= gfc_check_constructor (e
, check_restricted
);
3589 t
= gfc_check_constructor (e
, check_restricted
);
3593 gfc_internal_error ("check_restricted(): Unknown expression type");
3600 /* Check to see that an expression is a specification expression. If
3601 we return false, an error has been generated. */
3604 gfc_specification_expr (gfc_expr
*e
)
3606 gfc_component
*comp
;
3611 if (e
->ts
.type
!= BT_INTEGER
)
3613 gfc_error ("Expression at %L must be of INTEGER type, found %s",
3614 &e
->where
, gfc_basic_typename (e
->ts
.type
));
3618 comp
= gfc_get_proc_ptr_comp (e
);
3619 if (e
->expr_type
== EXPR_FUNCTION
3620 && !e
->value
.function
.isym
3621 && !e
->value
.function
.esym
3622 && !gfc_pure (e
->symtree
->n
.sym
)
3623 && (!comp
|| !comp
->attr
.pure
))
3625 gfc_error ("Function %qs at %L must be PURE",
3626 e
->symtree
->n
.sym
->name
, &e
->where
);
3627 /* Prevent repeat error messages. */
3628 e
->symtree
->n
.sym
->attr
.pure
= 1;
3634 gfc_error ("Expression at %L must be scalar", &e
->where
);
3638 if (!gfc_simplify_expr (e
, 0))
3641 return check_restricted (e
);
3645 /************** Expression conformance checks. *************/
3647 /* Given two expressions, make sure that the arrays are conformable. */
3650 gfc_check_conformance (gfc_expr
*op1
, gfc_expr
*op2
, const char *optype_msgid
, ...)
3652 int op1_flag
, op2_flag
, d
;
3653 mpz_t op1_size
, op2_size
;
3659 if (op1
->rank
== 0 || op2
->rank
== 0)
3662 va_start (argp
, optype_msgid
);
3663 d
= vsnprintf (buffer
, sizeof (buffer
), optype_msgid
, argp
);
3665 if (d
< 1 || d
>= (int) sizeof (buffer
)) /* Reject truncation. */
3666 gfc_internal_error ("optype_msgid overflow: %d", d
);
3668 if (op1
->rank
!= op2
->rank
)
3670 gfc_error ("Incompatible ranks in %s (%d and %d) at %L", _(buffer
),
3671 op1
->rank
, op2
->rank
, &op1
->where
);
3677 for (d
= 0; d
< op1
->rank
; d
++)
3679 op1_flag
= gfc_array_dimen_size(op1
, d
, &op1_size
);
3680 op2_flag
= gfc_array_dimen_size(op2
, d
, &op2_size
);
3682 if (op1_flag
&& op2_flag
&& mpz_cmp (op1_size
, op2_size
) != 0)
3684 gfc_error ("Different shape for %s at %L on dimension %d "
3685 "(%d and %d)", _(buffer
), &op1
->where
, d
+ 1,
3686 (int) mpz_get_si (op1_size
),
3687 (int) mpz_get_si (op2_size
));
3693 mpz_clear (op1_size
);
3695 mpz_clear (op2_size
);
3705 /* Given an assignable expression and an arbitrary expression, make
3706 sure that the assignment can take place. Only add a call to the intrinsic
3707 conversion routines, when allow_convert is set. When this assign is a
3708 coarray call, then the convert is done by the coarray routine implicitly and
3709 adding the intrinsic conversion would do harm in most cases. */
3712 gfc_check_assign (gfc_expr
*lvalue
, gfc_expr
*rvalue
, int conform
,
3719 sym
= lvalue
->symtree
->n
.sym
;
3721 /* See if this is the component or subcomponent of a pointer and guard
3722 against assignment to LEN or KIND part-refs. */
3723 has_pointer
= sym
->attr
.pointer
;
3724 for (ref
= lvalue
->ref
; ref
; ref
= ref
->next
)
3726 if (!has_pointer
&& ref
->type
== REF_COMPONENT
3727 && ref
->u
.c
.component
->attr
.pointer
)
3729 else if (ref
->type
== REF_INQUIRY
3730 && (ref
->u
.i
== INQUIRY_LEN
|| ref
->u
.i
== INQUIRY_KIND
))
3732 gfc_error ("Assignment to a LEN or KIND part_ref at %L is not "
3733 "allowed", &lvalue
->where
);
3738 /* 12.5.2.2, Note 12.26: The result variable is very similar to any other
3739 variable local to a function subprogram. Its existence begins when
3740 execution of the function is initiated and ends when execution of the
3741 function is terminated...
3742 Therefore, the left hand side is no longer a variable, when it is: */
3743 if (sym
->attr
.flavor
== FL_PROCEDURE
&& sym
->attr
.proc
!= PROC_ST_FUNCTION
3744 && !sym
->attr
.external
)
3749 /* (i) Use associated; */
3750 if (sym
->attr
.use_assoc
)
3753 /* (ii) The assignment is in the main program; or */
3754 if (gfc_current_ns
->proc_name
3755 && gfc_current_ns
->proc_name
->attr
.is_main_program
)
3758 /* (iii) A module or internal procedure... */
3759 if (gfc_current_ns
->proc_name
3760 && (gfc_current_ns
->proc_name
->attr
.proc
== PROC_INTERNAL
3761 || gfc_current_ns
->proc_name
->attr
.proc
== PROC_MODULE
)
3762 && gfc_current_ns
->parent
3763 && (!(gfc_current_ns
->parent
->proc_name
->attr
.function
3764 || gfc_current_ns
->parent
->proc_name
->attr
.subroutine
)
3765 || gfc_current_ns
->parent
->proc_name
->attr
.is_main_program
))
3767 /* ... that is not a function... */
3768 if (gfc_current_ns
->proc_name
3769 && !gfc_current_ns
->proc_name
->attr
.function
)
3772 /* ... or is not an entry and has a different name. */
3773 if (!sym
->attr
.entry
&& sym
->name
!= gfc_current_ns
->proc_name
->name
)
3777 /* (iv) Host associated and not the function symbol or the
3778 parent result. This picks up sibling references, which
3779 cannot be entries. */
3780 if (!sym
->attr
.entry
3781 && sym
->ns
== gfc_current_ns
->parent
3782 && sym
!= gfc_current_ns
->proc_name
3783 && sym
!= gfc_current_ns
->parent
->proc_name
->result
)
3788 gfc_error ("%qs at %L is not a VALUE", sym
->name
, &lvalue
->where
);
3794 /* Reject assigning to an external symbol. For initializers, this
3795 was already done before, in resolve_fl_procedure. */
3796 if (sym
->attr
.flavor
== FL_PROCEDURE
&& sym
->attr
.external
3797 && sym
->attr
.proc
!= PROC_MODULE
&& !rvalue
->error
)
3799 gfc_error ("Illegal assignment to external procedure at %L",
3805 if (rvalue
->rank
!= 0 && lvalue
->rank
!= rvalue
->rank
)
3807 gfc_error ("Incompatible ranks %d and %d in assignment at %L",
3808 lvalue
->rank
, rvalue
->rank
, &lvalue
->where
);
3812 if (lvalue
->ts
.type
== BT_UNKNOWN
)
3814 gfc_error ("Variable type is UNKNOWN in assignment at %L",
3819 if (rvalue
->expr_type
== EXPR_NULL
)
3821 if (has_pointer
&& (ref
== NULL
|| ref
->next
== NULL
)
3822 && lvalue
->symtree
->n
.sym
->attr
.data
)
3826 gfc_error ("NULL appears on right-hand side in assignment at %L",
3832 /* This is possibly a typo: x = f() instead of x => f(). */
3834 && rvalue
->expr_type
== EXPR_FUNCTION
&& gfc_expr_attr (rvalue
).pointer
)
3835 gfc_warning (OPT_Wsurprising
,
3836 "POINTER-valued function appears on right-hand side of "
3837 "assignment at %L", &rvalue
->where
);
3839 /* Check size of array assignments. */
3840 if (lvalue
->rank
!= 0 && rvalue
->rank
!= 0
3841 && !gfc_check_conformance (lvalue
, rvalue
, _("array assignment")))
3844 /* Handle the case of a BOZ literal on the RHS. */
3845 if (rvalue
->ts
.type
== BT_BOZ
)
3847 if (lvalue
->symtree
->n
.sym
->attr
.data
)
3849 if (lvalue
->ts
.type
== BT_INTEGER
3850 && gfc_boz2int (rvalue
, lvalue
->ts
.kind
))
3853 if (lvalue
->ts
.type
== BT_REAL
3854 && gfc_boz2real (rvalue
, lvalue
->ts
.kind
))
3856 if (gfc_invalid_boz ("BOZ literal constant near %L cannot "
3857 "be assigned to a REAL variable",
3864 if (!lvalue
->symtree
->n
.sym
->attr
.data
3865 && gfc_invalid_boz ("BOZ literal constant at %L is neither a "
3866 "data-stmt-constant nor an actual argument to "
3867 "INT, REAL, DBLE, or CMPLX intrinsic function",
3871 if (lvalue
->ts
.type
== BT_INTEGER
3872 && gfc_boz2int (rvalue
, lvalue
->ts
.kind
))
3875 if (lvalue
->ts
.type
== BT_REAL
3876 && gfc_boz2real (rvalue
, lvalue
->ts
.kind
))
3879 gfc_error ("BOZ literal constant near %L cannot be assigned to a "
3880 "%qs variable", &rvalue
->where
, gfc_typename (lvalue
));
3884 if (gfc_expr_attr (lvalue
).pdt_kind
|| gfc_expr_attr (lvalue
).pdt_len
)
3886 gfc_error ("The assignment to a KIND or LEN component of a "
3887 "parameterized type at %L is not allowed",
3892 if (gfc_compare_types (&lvalue
->ts
, &rvalue
->ts
))
3895 /* Only DATA Statements come here. */
3900 /* Numeric can be converted to any other numeric. And Hollerith can be
3901 converted to any other type. */
3902 if ((gfc_numeric_ts (&lvalue
->ts
) && gfc_numeric_ts (&rvalue
->ts
))
3903 || rvalue
->ts
.type
== BT_HOLLERITH
)
3906 if (flag_dec_char_conversions
&& (gfc_numeric_ts (&lvalue
->ts
)
3907 || lvalue
->ts
.type
== BT_LOGICAL
)
3908 && rvalue
->ts
.type
== BT_CHARACTER
3909 && rvalue
->ts
.kind
== gfc_default_character_kind
)
3912 if (lvalue
->ts
.type
== BT_LOGICAL
&& rvalue
->ts
.type
== BT_LOGICAL
)
3915 where
= lvalue
->where
.lb
? &lvalue
->where
: &rvalue
->where
;
3916 gfc_error ("Incompatible types in DATA statement at %L; attempted "
3917 "conversion of %s to %s", where
,
3918 gfc_typename (rvalue
), gfc_typename (lvalue
));
3923 /* Assignment is the only case where character variables of different
3924 kind values can be converted into one another. */
3925 if (lvalue
->ts
.type
== BT_CHARACTER
&& rvalue
->ts
.type
== BT_CHARACTER
)
3927 if (lvalue
->ts
.kind
!= rvalue
->ts
.kind
&& allow_convert
)
3928 return gfc_convert_chartype (rvalue
, &lvalue
->ts
);
3936 return gfc_convert_type (rvalue
, &lvalue
->ts
, 1);
3940 /* Check that a pointer assignment is OK. We first check lvalue, and
3941 we only check rvalue if it's not an assignment to NULL() or a
3942 NULLIFY statement. */
3945 gfc_check_pointer_assign (gfc_expr
*lvalue
, gfc_expr
*rvalue
,
3946 bool suppress_type_test
, bool is_init_expr
)
3948 symbol_attribute attr
, lhs_attr
;
3950 bool is_pure
, is_implicit_pure
, rank_remap
;
3954 if (!lvalue
->symtree
)
3957 lhs_attr
= gfc_expr_attr (lvalue
);
3958 if (lvalue
->ts
.type
== BT_UNKNOWN
&& !lhs_attr
.proc_pointer
)
3960 gfc_error ("Pointer assignment target is not a POINTER at %L",
3965 if (lhs_attr
.flavor
== FL_PROCEDURE
&& lhs_attr
.use_assoc
3966 && !lhs_attr
.proc_pointer
)
3968 gfc_error ("%qs in the pointer assignment at %L cannot be an "
3969 "l-value since it is a procedure",
3970 lvalue
->symtree
->n
.sym
->name
, &lvalue
->where
);
3974 proc_pointer
= lvalue
->symtree
->n
.sym
->attr
.proc_pointer
;
3977 same_rank
= lvalue
->rank
== rvalue
->rank
;
3978 for (ref
= lvalue
->ref
; ref
; ref
= ref
->next
)
3980 if (ref
->type
== REF_COMPONENT
)
3981 proc_pointer
= ref
->u
.c
.component
->attr
.proc_pointer
;
3983 if (ref
->type
== REF_ARRAY
&& ref
->next
== NULL
)
3987 if (ref
->u
.ar
.type
== AR_FULL
)
3990 if (ref
->u
.ar
.type
!= AR_SECTION
)
3992 gfc_error ("Expected bounds specification for %qs at %L",
3993 lvalue
->symtree
->n
.sym
->name
, &lvalue
->where
);
3997 if (!gfc_notify_std (GFC_STD_F2003
, "Bounds specification "
3998 "for %qs in pointer assignment at %L",
3999 lvalue
->symtree
->n
.sym
->name
, &lvalue
->where
))
4002 /* Fortran standard (e.g. F2018, 10.2.2 Pointer assignment):
4004 * (C1017) If bounds-spec-list is specified, the number of
4005 * bounds-specs shall equal the rank of data-pointer-object.
4007 * If bounds-spec-list appears, it specifies the lower bounds.
4009 * (C1018) If bounds-remapping-list is specified, the number of
4010 * bounds-remappings shall equal the rank of data-pointer-object.
4012 * If bounds-remapping-list appears, it specifies the upper and
4013 * lower bounds of each dimension of the pointer; the pointer target
4014 * shall be simply contiguous or of rank one.
4016 * (C1019) If bounds-remapping-list is not specified, the ranks of
4017 * data-pointer-object and data-target shall be the same.
4019 * Thus when bounds are given, all lbounds are necessary and either
4020 * all or none of the upper bounds; no strides are allowed. If the
4021 * upper bounds are present, we may do rank remapping. */
4022 for (dim
= 0; dim
< ref
->u
.ar
.dimen
; ++dim
)
4024 if (ref
->u
.ar
.stride
[dim
])
4026 gfc_error ("Stride must not be present at %L",
4030 if (!same_rank
&& (!ref
->u
.ar
.start
[dim
] ||!ref
->u
.ar
.end
[dim
]))
4032 gfc_error ("Rank remapping requires a "
4033 "list of %<lower-bound : upper-bound%> "
4034 "specifications at %L", &lvalue
->where
);
4037 if (!ref
->u
.ar
.start
[dim
]
4038 || ref
->u
.ar
.dimen_type
[dim
] != DIMEN_RANGE
)
4040 gfc_error ("Expected list of %<lower-bound :%> or "
4041 "list of %<lower-bound : upper-bound%> "
4042 "specifications at %L", &lvalue
->where
);
4047 rank_remap
= (ref
->u
.ar
.end
[dim
] != NULL
);
4050 if ((rank_remap
&& !ref
->u
.ar
.end
[dim
]))
4052 gfc_error ("Rank remapping requires a "
4053 "list of %<lower-bound : upper-bound%> "
4054 "specifications at %L", &lvalue
->where
);
4057 if (!rank_remap
&& ref
->u
.ar
.end
[dim
])
4059 gfc_error ("Expected list of %<lower-bound :%> or "
4060 "list of %<lower-bound : upper-bound%> "
4061 "specifications at %L", &lvalue
->where
);
4069 is_pure
= gfc_pure (NULL
);
4070 is_implicit_pure
= gfc_implicit_pure (NULL
);
4072 /* If rvalue is a NULL() or NULLIFY, we're done. Otherwise the type,
4073 kind, etc for lvalue and rvalue must match, and rvalue must be a
4074 pure variable if we're in a pure function. */
4075 if (rvalue
->expr_type
== EXPR_NULL
&& rvalue
->ts
.type
== BT_UNKNOWN
)
4078 /* F2008, C723 (pointer) and C726 (proc-pointer); for PURE also C1283. */
4079 if (lvalue
->expr_type
== EXPR_VARIABLE
4080 && gfc_is_coindexed (lvalue
))
4083 for (ref
= lvalue
->ref
; ref
; ref
= ref
->next
)
4084 if (ref
->type
== REF_ARRAY
&& ref
->u
.ar
.codimen
)
4086 gfc_error ("Pointer object at %L shall not have a coindex",
4092 /* Checks on rvalue for procedure pointer assignments. */
4097 gfc_component
*comp1
, *comp2
;
4100 attr
= gfc_expr_attr (rvalue
);
4101 if (!((rvalue
->expr_type
== EXPR_NULL
)
4102 || (rvalue
->expr_type
== EXPR_FUNCTION
&& attr
.proc_pointer
)
4103 || (rvalue
->expr_type
== EXPR_VARIABLE
&& attr
.proc_pointer
)
4104 || (rvalue
->expr_type
== EXPR_VARIABLE
4105 && attr
.flavor
== FL_PROCEDURE
)))
4107 gfc_error ("Invalid procedure pointer assignment at %L",
4112 if (rvalue
->expr_type
== EXPR_VARIABLE
&& !attr
.proc_pointer
)
4114 /* Check for intrinsics. */
4115 gfc_symbol
*sym
= rvalue
->symtree
->n
.sym
;
4116 if (!sym
->attr
.intrinsic
4117 && (gfc_is_intrinsic (sym
, 0, sym
->declared_at
)
4118 || gfc_is_intrinsic (sym
, 1, sym
->declared_at
)))
4120 sym
->attr
.intrinsic
= 1;
4121 gfc_resolve_intrinsic (sym
, &rvalue
->where
);
4122 attr
= gfc_expr_attr (rvalue
);
4124 /* Check for result of embracing function. */
4125 if (sym
->attr
.function
&& sym
->result
== sym
)
4129 for (ns
= gfc_current_ns
; ns
; ns
= ns
->parent
)
4130 if (sym
== ns
->proc_name
)
4132 gfc_error ("Function result %qs is invalid as proc-target "
4133 "in procedure pointer assignment at %L",
4134 sym
->name
, &rvalue
->where
);
4141 gfc_error ("Abstract interface %qs is invalid "
4142 "in procedure pointer assignment at %L",
4143 rvalue
->symtree
->name
, &rvalue
->where
);
4146 /* Check for F08:C729. */
4147 if (attr
.flavor
== FL_PROCEDURE
)
4149 if (attr
.proc
== PROC_ST_FUNCTION
)
4151 gfc_error ("Statement function %qs is invalid "
4152 "in procedure pointer assignment at %L",
4153 rvalue
->symtree
->name
, &rvalue
->where
);
4156 if (attr
.proc
== PROC_INTERNAL
&&
4157 !gfc_notify_std(GFC_STD_F2008
, "Internal procedure %qs "
4158 "is invalid in procedure pointer assignment "
4159 "at %L", rvalue
->symtree
->name
, &rvalue
->where
))
4161 if (attr
.intrinsic
&& gfc_intrinsic_actual_ok (rvalue
->symtree
->name
,
4162 attr
.subroutine
) == 0)
4164 gfc_error ("Intrinsic %qs at %L is invalid in procedure pointer "
4165 "assignment", rvalue
->symtree
->name
, &rvalue
->where
);
4169 /* Check for F08:C730. */
4170 if (attr
.elemental
&& !attr
.intrinsic
)
4172 gfc_error ("Nonintrinsic elemental procedure %qs is invalid "
4173 "in procedure pointer assignment at %L",
4174 rvalue
->symtree
->name
, &rvalue
->where
);
4178 /* Ensure that the calling convention is the same. As other attributes
4179 such as DLLEXPORT may differ, one explicitly only tests for the
4180 calling conventions. */
4181 if (rvalue
->expr_type
== EXPR_VARIABLE
4182 && lvalue
->symtree
->n
.sym
->attr
.ext_attr
4183 != rvalue
->symtree
->n
.sym
->attr
.ext_attr
)
4185 symbol_attribute calls
;
4188 gfc_add_ext_attribute (&calls
, EXT_ATTR_CDECL
, NULL
);
4189 gfc_add_ext_attribute (&calls
, EXT_ATTR_STDCALL
, NULL
);
4190 gfc_add_ext_attribute (&calls
, EXT_ATTR_FASTCALL
, NULL
);
4192 if ((calls
.ext_attr
& lvalue
->symtree
->n
.sym
->attr
.ext_attr
)
4193 != (calls
.ext_attr
& rvalue
->symtree
->n
.sym
->attr
.ext_attr
))
4195 gfc_error ("Mismatch in the procedure pointer assignment "
4196 "at %L: mismatch in the calling convention",
4202 comp1
= gfc_get_proc_ptr_comp (lvalue
);
4204 s1
= comp1
->ts
.interface
;
4207 s1
= lvalue
->symtree
->n
.sym
;
4208 if (s1
->ts
.interface
)
4209 s1
= s1
->ts
.interface
;
4212 comp2
= gfc_get_proc_ptr_comp (rvalue
);
4215 if (rvalue
->expr_type
== EXPR_FUNCTION
)
4217 s2
= comp2
->ts
.interface
->result
;
4222 s2
= comp2
->ts
.interface
;
4226 else if (rvalue
->expr_type
== EXPR_FUNCTION
)
4228 if (rvalue
->value
.function
.esym
)
4229 s2
= rvalue
->value
.function
.esym
->result
;
4231 s2
= rvalue
->symtree
->n
.sym
->result
;
4237 s2
= rvalue
->symtree
->n
.sym
;
4241 if (s2
&& s2
->attr
.proc_pointer
&& s2
->ts
.interface
)
4242 s2
= s2
->ts
.interface
;
4244 /* Special check for the case of absent interface on the lvalue.
4245 * All other interface checks are done below. */
4246 if (!s1
&& comp1
&& comp1
->attr
.subroutine
&& s2
&& s2
->attr
.function
)
4248 gfc_error ("Interface mismatch in procedure pointer assignment "
4249 "at %L: %qs is not a subroutine", &rvalue
->where
, name
);
4253 /* F08:7.2.2.4 (4) */
4254 if (s2
&& gfc_explicit_interface_required (s2
, err
, sizeof(err
)))
4258 gfc_error ("Explicit interface required for component %qs at %L: %s",
4259 comp1
->name
, &lvalue
->where
, err
);
4262 else if (s1
->attr
.if_source
== IFSRC_UNKNOWN
)
4264 gfc_error ("Explicit interface required for %qs at %L: %s",
4265 s1
->name
, &lvalue
->where
, err
);
4269 if (s1
&& gfc_explicit_interface_required (s1
, err
, sizeof(err
)))
4273 gfc_error ("Explicit interface required for component %qs at %L: %s",
4274 comp2
->name
, &rvalue
->where
, err
);
4277 else if (s2
->attr
.if_source
== IFSRC_UNKNOWN
)
4279 gfc_error ("Explicit interface required for %qs at %L: %s",
4280 s2
->name
, &rvalue
->where
, err
);
4285 if (s1
== s2
|| !s1
|| !s2
)
4288 if (!gfc_compare_interfaces (s1
, s2
, name
, 0, 1,
4289 err
, sizeof(err
), NULL
, NULL
))
4291 gfc_error ("Interface mismatch in procedure pointer assignment "
4292 "at %L: %s", &rvalue
->where
, err
);
4296 /* Check F2008Cor2, C729. */
4297 if (!s2
->attr
.intrinsic
&& s2
->attr
.if_source
== IFSRC_UNKNOWN
4298 && !s2
->attr
.external
&& !s2
->attr
.subroutine
&& !s2
->attr
.function
)
4300 gfc_error ("Procedure pointer target %qs at %L must be either an "
4301 "intrinsic, host or use associated, referenced or have "
4302 "the EXTERNAL attribute", s2
->name
, &rvalue
->where
);
4310 /* A non-proc pointer cannot point to a constant. */
4311 if (rvalue
->expr_type
== EXPR_CONSTANT
)
4313 gfc_error_now ("Pointer assignment target cannot be a constant at %L",
4319 if (!gfc_compare_types (&lvalue
->ts
, &rvalue
->ts
))
4321 /* Check for F03:C717. */
4322 if (UNLIMITED_POLY (rvalue
)
4323 && !(UNLIMITED_POLY (lvalue
)
4324 || (lvalue
->ts
.type
== BT_DERIVED
4325 && (lvalue
->ts
.u
.derived
->attr
.is_bind_c
4326 || lvalue
->ts
.u
.derived
->attr
.sequence
))))
4327 gfc_error ("Data-pointer-object at %L must be unlimited "
4328 "polymorphic, or of a type with the BIND or SEQUENCE "
4329 "attribute, to be compatible with an unlimited "
4330 "polymorphic target", &lvalue
->where
);
4331 else if (!suppress_type_test
)
4332 gfc_error ("Different types in pointer assignment at %L; "
4333 "attempted assignment of %s to %s", &lvalue
->where
,
4334 gfc_typename (rvalue
), gfc_typename (lvalue
));
4338 if (lvalue
->ts
.type
!= BT_CLASS
&& lvalue
->ts
.kind
!= rvalue
->ts
.kind
)
4340 gfc_error ("Different kind type parameters in pointer "
4341 "assignment at %L", &lvalue
->where
);
4345 if (lvalue
->rank
!= rvalue
->rank
&& !rank_remap
)
4347 gfc_error ("Different ranks in pointer assignment at %L", &lvalue
->where
);
4351 /* Make sure the vtab is present. */
4352 if (lvalue
->ts
.type
== BT_CLASS
&& !UNLIMITED_POLY (rvalue
))
4353 gfc_find_vtab (&rvalue
->ts
);
4355 /* Check rank remapping. */
4360 /* If this can be determined, check that the target must be at least as
4361 large as the pointer assigned to it is. */
4362 if (gfc_array_size (lvalue
, &lsize
)
4363 && gfc_array_size (rvalue
, &rsize
)
4364 && mpz_cmp (rsize
, lsize
) < 0)
4366 gfc_error ("Rank remapping target is smaller than size of the"
4367 " pointer (%ld < %ld) at %L",
4368 mpz_get_si (rsize
), mpz_get_si (lsize
),
4373 /* The target must be either rank one or it must be simply contiguous
4374 and F2008 must be allowed. */
4375 if (rvalue
->rank
!= 1)
4377 if (!gfc_is_simply_contiguous (rvalue
, true, false))
4379 gfc_error ("Rank remapping target must be rank 1 or"
4380 " simply contiguous at %L", &rvalue
->where
);
4383 if (!gfc_notify_std (GFC_STD_F2008
, "Rank remapping target is not "
4384 "rank 1 at %L", &rvalue
->where
))
4389 /* Now punt if we are dealing with a NULLIFY(X) or X = NULL(X). */
4390 if (rvalue
->expr_type
== EXPR_NULL
)
4393 if (rvalue
->expr_type
== EXPR_VARIABLE
&& is_subref_array (rvalue
))
4394 lvalue
->symtree
->n
.sym
->attr
.subref_array_pointer
= 1;
4396 attr
= gfc_expr_attr (rvalue
);
4398 if (rvalue
->expr_type
== EXPR_FUNCTION
&& !attr
.pointer
)
4400 /* F2008, C725. For PURE also C1283. Sometimes rvalue is a function call
4401 to caf_get. Map this to the same error message as below when it is
4402 still a variable expression. */
4403 if (rvalue
->value
.function
.isym
4404 && rvalue
->value
.function
.isym
->id
== GFC_ISYM_CAF_GET
)
4405 /* The test above might need to be extend when F08, Note 5.4 has to be
4406 interpreted in the way that target and pointer with the same coindex
4408 gfc_error ("Data target at %L shall not have a coindex",
4411 gfc_error ("Target expression in pointer assignment "
4412 "at %L must deliver a pointer result",
4423 if (gfc_is_size_zero_array (rvalue
))
4425 gfc_error ("Zero-sized array detected at %L where an entity with "
4426 "the TARGET attribute is expected", &rvalue
->where
);
4429 else if (!rvalue
->symtree
)
4431 gfc_error ("Pointer assignment target in initialization expression "
4432 "does not have the TARGET attribute at %L",
4437 sym
= rvalue
->symtree
->n
.sym
;
4439 if (sym
->ts
.type
== BT_CLASS
&& sym
->attr
.class_ok
)
4440 target
= CLASS_DATA (sym
)->attr
.target
;
4442 target
= sym
->attr
.target
;
4444 if (!target
&& !proc_pointer
)
4446 gfc_error ("Pointer assignment target in initialization expression "
4447 "does not have the TARGET attribute at %L",
4452 for (ref
= rvalue
->ref
; ref
; ref
= ref
->next
)
4457 for (int n
= 0; n
< ref
->u
.ar
.dimen
; n
++)
4458 if (!gfc_is_constant_expr (ref
->u
.ar
.start
[n
])
4459 || !gfc_is_constant_expr (ref
->u
.ar
.end
[n
])
4460 || !gfc_is_constant_expr (ref
->u
.ar
.stride
[n
]))
4462 gfc_error ("Every subscript of target specification "
4463 "at %L must be a constant expression",
4470 if (!gfc_is_constant_expr (ref
->u
.ss
.start
)
4471 || !gfc_is_constant_expr (ref
->u
.ss
.end
))
4473 gfc_error ("Substring starting and ending points of target "
4474 "specification at %L must be constant expressions",
4475 &ref
->u
.ss
.start
->where
);
4487 if (!attr
.target
&& !attr
.pointer
)
4489 gfc_error ("Pointer assignment target is neither TARGET "
4490 "nor POINTER at %L", &rvalue
->where
);
4495 if (lvalue
->ts
.type
== BT_CHARACTER
)
4497 bool t
= gfc_check_same_strlen (lvalue
, rvalue
, "pointer assignment");
4502 if (is_pure
&& gfc_impure_variable (rvalue
->symtree
->n
.sym
))
4504 gfc_error ("Bad target in pointer assignment in PURE "
4505 "procedure at %L", &rvalue
->where
);
4508 if (is_implicit_pure
&& gfc_impure_variable (rvalue
->symtree
->n
.sym
))
4509 gfc_unset_implicit_pure (gfc_current_ns
->proc_name
);
4511 if (gfc_has_vector_index (rvalue
))
4513 gfc_error ("Pointer assignment with vector subscript "
4514 "on rhs at %L", &rvalue
->where
);
4518 if (attr
.is_protected
&& attr
.use_assoc
4519 && !(attr
.pointer
|| attr
.proc_pointer
))
4521 gfc_error ("Pointer assignment target has PROTECTED "
4522 "attribute at %L", &rvalue
->where
);
4526 /* F2008, C725. For PURE also C1283. */
4527 if (rvalue
->expr_type
== EXPR_VARIABLE
4528 && gfc_is_coindexed (rvalue
))
4531 for (ref
= rvalue
->ref
; ref
; ref
= ref
->next
)
4532 if (ref
->type
== REF_ARRAY
&& ref
->u
.ar
.codimen
)
4534 gfc_error ("Data target at %L shall not have a coindex",
4540 /* Warn for assignments of contiguous pointers to targets which is not
4541 contiguous. Be lenient in the definition of what counts as
4544 if (lhs_attr
.contiguous
4545 && lhs_attr
.dimension
> 0)
4547 if (gfc_is_not_contiguous (rvalue
))
4549 gfc_error ("Assignment to contiguous pointer from "
4550 "non-contiguous target at %L", &rvalue
->where
);
4553 if (!gfc_is_simply_contiguous (rvalue
, false, true))
4554 gfc_warning (OPT_Wextra
, "Assignment to contiguous pointer from "
4555 "non-contiguous target at %L", &rvalue
->where
);
4558 /* Warn if it is the LHS pointer may lives longer than the RHS target. */
4559 if (warn_target_lifetime
4560 && rvalue
->expr_type
== EXPR_VARIABLE
4561 && !rvalue
->symtree
->n
.sym
->attr
.save
4562 && !rvalue
->symtree
->n
.sym
->attr
.pointer
&& !attr
.pointer
4563 && !rvalue
->symtree
->n
.sym
->attr
.host_assoc
4564 && !rvalue
->symtree
->n
.sym
->attr
.in_common
4565 && !rvalue
->symtree
->n
.sym
->attr
.use_assoc
4566 && !rvalue
->symtree
->n
.sym
->attr
.dummy
)
4571 warn
= lvalue
->symtree
->n
.sym
->attr
.dummy
4572 || lvalue
->symtree
->n
.sym
->attr
.result
4573 || lvalue
->symtree
->n
.sym
->attr
.function
4574 || (lvalue
->symtree
->n
.sym
->attr
.host_assoc
4575 && lvalue
->symtree
->n
.sym
->ns
4576 != rvalue
->symtree
->n
.sym
->ns
)
4577 || lvalue
->symtree
->n
.sym
->attr
.use_assoc
4578 || lvalue
->symtree
->n
.sym
->attr
.in_common
;
4580 if (rvalue
->symtree
->n
.sym
->ns
->proc_name
4581 && rvalue
->symtree
->n
.sym
->ns
->proc_name
->attr
.flavor
!= FL_PROCEDURE
4582 && rvalue
->symtree
->n
.sym
->ns
->proc_name
->attr
.flavor
!= FL_PROGRAM
)
4583 for (ns
= rvalue
->symtree
->n
.sym
->ns
;
4584 ns
&& ns
->proc_name
&& ns
->proc_name
->attr
.flavor
!= FL_PROCEDURE
;
4586 if (ns
->parent
== lvalue
->symtree
->n
.sym
->ns
)
4593 gfc_warning (OPT_Wtarget_lifetime
,
4594 "Pointer at %L in pointer assignment might outlive the "
4595 "pointer target", &lvalue
->where
);
4602 /* Relative of gfc_check_assign() except that the lvalue is a single
4603 symbol. Used for initialization assignments. */
4606 gfc_check_assign_symbol (gfc_symbol
*sym
, gfc_component
*comp
, gfc_expr
*rvalue
)
4610 bool pointer
, proc_pointer
;
4612 memset (&lvalue
, '\0', sizeof (gfc_expr
));
4614 lvalue
.expr_type
= EXPR_VARIABLE
;
4615 lvalue
.ts
= sym
->ts
;
4618 lvalue
.rank
= sym
->as
->rank
;
4619 lvalue
.corank
= sym
->as
->corank
;
4621 lvalue
.symtree
= XCNEW (gfc_symtree
);
4622 lvalue
.symtree
->n
.sym
= sym
;
4623 lvalue
.where
= sym
->declared_at
;
4627 lvalue
.ref
= gfc_get_ref ();
4628 lvalue
.ref
->type
= REF_COMPONENT
;
4629 lvalue
.ref
->u
.c
.component
= comp
;
4630 lvalue
.ref
->u
.c
.sym
= sym
;
4631 lvalue
.ts
= comp
->ts
;
4632 lvalue
.rank
= comp
->as
? comp
->as
->rank
: 0;
4633 lvalue
.corank
= comp
->as
? comp
->as
->corank
: 0;
4634 lvalue
.where
= comp
->loc
;
4635 pointer
= comp
->ts
.type
== BT_CLASS
&& CLASS_DATA (comp
)
4636 ? CLASS_DATA (comp
)->attr
.class_pointer
: comp
->attr
.pointer
;
4637 proc_pointer
= comp
->attr
.proc_pointer
;
4641 pointer
= sym
->ts
.type
== BT_CLASS
&& CLASS_DATA (sym
)
4642 ? CLASS_DATA (sym
)->attr
.class_pointer
: sym
->attr
.pointer
;
4643 proc_pointer
= sym
->attr
.proc_pointer
;
4646 if (pointer
|| proc_pointer
)
4647 r
= gfc_check_pointer_assign (&lvalue
, rvalue
, false, true);
4650 /* If a conversion function, e.g., __convert_i8_i4, was inserted
4651 into an array constructor, we should check if it can be reduced
4652 as an initialization expression. */
4653 if (rvalue
->expr_type
== EXPR_FUNCTION
4654 && rvalue
->value
.function
.isym
4655 && (rvalue
->value
.function
.isym
->conversion
== 1))
4656 gfc_check_init_expr (rvalue
);
4658 r
= gfc_check_assign (&lvalue
, rvalue
, 1);
4661 free (lvalue
.symtree
);
4667 if (pointer
&& rvalue
->expr_type
!= EXPR_NULL
&& !proc_pointer
)
4669 /* F08:C461. Additional checks for pointer initialization. */
4670 symbol_attribute attr
;
4671 attr
= gfc_expr_attr (rvalue
);
4672 if (attr
.allocatable
)
4674 gfc_error ("Pointer initialization target at %L "
4675 "must not be ALLOCATABLE", &rvalue
->where
);
4678 if (!attr
.target
|| attr
.pointer
)
4680 gfc_error ("Pointer initialization target at %L "
4681 "must have the TARGET attribute", &rvalue
->where
);
4685 if (!attr
.save
&& rvalue
->expr_type
== EXPR_VARIABLE
4686 && rvalue
->symtree
->n
.sym
->ns
->proc_name
4687 && rvalue
->symtree
->n
.sym
->ns
->proc_name
->attr
.is_main_program
)
4689 rvalue
->symtree
->n
.sym
->ns
->proc_name
->attr
.save
= SAVE_IMPLICIT
;
4690 attr
.save
= SAVE_IMPLICIT
;
4695 gfc_error ("Pointer initialization target at %L "
4696 "must have the SAVE attribute", &rvalue
->where
);
4701 if (proc_pointer
&& rvalue
->expr_type
!= EXPR_NULL
)
4703 /* F08:C1220. Additional checks for procedure pointer initialization. */
4704 symbol_attribute attr
= gfc_expr_attr (rvalue
);
4705 if (attr
.proc_pointer
)
4707 gfc_error ("Procedure pointer initialization target at %L "
4708 "may not be a procedure pointer", &rvalue
->where
);
4711 if (attr
.proc
== PROC_INTERNAL
)
4713 gfc_error ("Internal procedure %qs is invalid in "
4714 "procedure pointer initialization at %L",
4715 rvalue
->symtree
->name
, &rvalue
->where
);
4720 gfc_error ("Dummy procedure %qs is invalid in "
4721 "procedure pointer initialization at %L",
4722 rvalue
->symtree
->name
, &rvalue
->where
);
4730 /* Build an initializer for a local integer, real, complex, logical, or
4731 character variable, based on the command line flags finit-local-zero,
4732 finit-integer=, finit-real=, finit-logical=, and finit-character=.
4733 With force, an initializer is ALWAYS generated. */
4736 gfc_build_init_expr (gfc_typespec
*ts
, locus
*where
, bool force
)
4738 gfc_expr
*init_expr
;
4740 /* Try to build an initializer expression. */
4741 init_expr
= gfc_get_constant_expr (ts
->type
, ts
->kind
, where
);
4743 /* If we want to force generation, make sure we default to zero. */
4744 gfc_init_local_real init_real
= flag_init_real
;
4745 int init_logical
= gfc_option
.flag_init_logical
;
4748 if (init_real
== GFC_INIT_REAL_OFF
)
4749 init_real
= GFC_INIT_REAL_ZERO
;
4750 if (init_logical
== GFC_INIT_LOGICAL_OFF
)
4751 init_logical
= GFC_INIT_LOGICAL_FALSE
;
4754 /* We will only initialize integers, reals, complex, logicals, and
4755 characters, and only if the corresponding command-line flags
4756 were set. Otherwise, we free init_expr and return null. */
4760 if (force
|| gfc_option
.flag_init_integer
!= GFC_INIT_INTEGER_OFF
)
4761 mpz_set_si (init_expr
->value
.integer
,
4762 gfc_option
.flag_init_integer_value
);
4765 gfc_free_expr (init_expr
);
4773 case GFC_INIT_REAL_SNAN
:
4774 init_expr
->is_snan
= 1;
4776 case GFC_INIT_REAL_NAN
:
4777 mpfr_set_nan (init_expr
->value
.real
);
4780 case GFC_INIT_REAL_INF
:
4781 mpfr_set_inf (init_expr
->value
.real
, 1);
4784 case GFC_INIT_REAL_NEG_INF
:
4785 mpfr_set_inf (init_expr
->value
.real
, -1);
4788 case GFC_INIT_REAL_ZERO
:
4789 mpfr_set_ui (init_expr
->value
.real
, 0.0, GFC_RND_MODE
);
4793 gfc_free_expr (init_expr
);
4802 case GFC_INIT_REAL_SNAN
:
4803 init_expr
->is_snan
= 1;
4805 case GFC_INIT_REAL_NAN
:
4806 mpfr_set_nan (mpc_realref (init_expr
->value
.complex));
4807 mpfr_set_nan (mpc_imagref (init_expr
->value
.complex));
4810 case GFC_INIT_REAL_INF
:
4811 mpfr_set_inf (mpc_realref (init_expr
->value
.complex), 1);
4812 mpfr_set_inf (mpc_imagref (init_expr
->value
.complex), 1);
4815 case GFC_INIT_REAL_NEG_INF
:
4816 mpfr_set_inf (mpc_realref (init_expr
->value
.complex), -1);
4817 mpfr_set_inf (mpc_imagref (init_expr
->value
.complex), -1);
4820 case GFC_INIT_REAL_ZERO
:
4821 mpc_set_ui (init_expr
->value
.complex, 0, GFC_MPC_RND_MODE
);
4825 gfc_free_expr (init_expr
);
4832 if (init_logical
== GFC_INIT_LOGICAL_FALSE
)
4833 init_expr
->value
.logical
= 0;
4834 else if (init_logical
== GFC_INIT_LOGICAL_TRUE
)
4835 init_expr
->value
.logical
= 1;
4838 gfc_free_expr (init_expr
);
4844 /* For characters, the length must be constant in order to
4845 create a default initializer. */
4846 if ((force
|| gfc_option
.flag_init_character
== GFC_INIT_CHARACTER_ON
)
4848 && ts
->u
.cl
->length
->expr_type
== EXPR_CONSTANT
)
4850 HOST_WIDE_INT char_len
= gfc_mpz_get_hwi (ts
->u
.cl
->length
->value
.integer
);
4851 init_expr
->value
.character
.length
= char_len
;
4852 init_expr
->value
.character
.string
= gfc_get_wide_string (char_len
+1);
4853 for (size_t i
= 0; i
< (size_t) char_len
; i
++)
4854 init_expr
->value
.character
.string
[i
]
4855 = (unsigned char) gfc_option
.flag_init_character_value
;
4859 gfc_free_expr (init_expr
);
4863 && (force
|| gfc_option
.flag_init_character
== GFC_INIT_CHARACTER_ON
)
4864 && ts
->u
.cl
->length
&& flag_max_stack_var_size
!= 0)
4866 gfc_actual_arglist
*arg
;
4867 init_expr
= gfc_get_expr ();
4868 init_expr
->where
= *where
;
4869 init_expr
->ts
= *ts
;
4870 init_expr
->expr_type
= EXPR_FUNCTION
;
4871 init_expr
->value
.function
.isym
=
4872 gfc_intrinsic_function_by_id (GFC_ISYM_REPEAT
);
4873 init_expr
->value
.function
.name
= "repeat";
4874 arg
= gfc_get_actual_arglist ();
4875 arg
->expr
= gfc_get_character_expr (ts
->kind
, where
, NULL
, 1);
4876 arg
->expr
->value
.character
.string
[0] =
4877 gfc_option
.flag_init_character_value
;
4878 arg
->next
= gfc_get_actual_arglist ();
4879 arg
->next
->expr
= gfc_copy_expr (ts
->u
.cl
->length
);
4880 init_expr
->value
.function
.actual
= arg
;
4885 gfc_free_expr (init_expr
);
4892 /* Invoke gfc_build_init_expr to create an initializer expression, but do not
4893 * require that an expression be built. */
4896 gfc_build_default_init_expr (gfc_typespec
*ts
, locus
*where
)
4898 return gfc_build_init_expr (ts
, where
, false);
4901 /* Apply an initialization expression to a typespec. Can be used for symbols or
4902 components. Similar to add_init_expr_to_sym in decl.cc; could probably be
4903 combined with some effort. */
4906 gfc_apply_init (gfc_typespec
*ts
, symbol_attribute
*attr
, gfc_expr
*init
)
4908 if (ts
->type
== BT_CHARACTER
&& !attr
->pointer
&& init
4911 && ts
->u
.cl
->length
->expr_type
== EXPR_CONSTANT
4912 && ts
->u
.cl
->length
->ts
.type
== BT_INTEGER
)
4914 HOST_WIDE_INT len
= gfc_mpz_get_hwi (ts
->u
.cl
->length
->value
.integer
);
4916 if (init
->expr_type
== EXPR_CONSTANT
)
4917 gfc_set_constant_character_len (len
, init
, -1);
4919 && init
->ts
.type
== BT_CHARACTER
4920 && init
->ts
.u
.cl
&& init
->ts
.u
.cl
->length
4921 && mpz_cmp (ts
->u
.cl
->length
->value
.integer
,
4922 init
->ts
.u
.cl
->length
->value
.integer
))
4924 gfc_constructor
*ctor
;
4925 ctor
= gfc_constructor_first (init
->value
.constructor
);
4929 bool has_ts
= (init
->ts
.u
.cl
4930 && init
->ts
.u
.cl
->length_from_typespec
);
4932 /* Remember the length of the first element for checking
4933 that all elements *in the constructor* have the same
4934 length. This need not be the length of the LHS! */
4935 gcc_assert (ctor
->expr
->expr_type
== EXPR_CONSTANT
);
4936 gcc_assert (ctor
->expr
->ts
.type
== BT_CHARACTER
);
4937 gfc_charlen_t first_len
= ctor
->expr
->value
.character
.length
;
4939 for ( ; ctor
; ctor
= gfc_constructor_next (ctor
))
4940 if (ctor
->expr
->expr_type
== EXPR_CONSTANT
)
4942 gfc_set_constant_character_len (len
, ctor
->expr
,
4943 has_ts
? -1 : first_len
);
4944 if (!ctor
->expr
->ts
.u
.cl
)
4946 = gfc_new_charlen (gfc_current_ns
, ts
->u
.cl
);
4948 ctor
->expr
->ts
.u
.cl
->length
4949 = gfc_copy_expr (ts
->u
.cl
->length
);
4957 /* Check whether an expression is a structure constructor and whether it has
4958 other values than NULL. */
4961 is_non_empty_structure_constructor (gfc_expr
* e
)
4963 if (e
->expr_type
!= EXPR_STRUCTURE
)
4966 gfc_constructor
*cons
= gfc_constructor_first (e
->value
.constructor
);
4969 if (!cons
->expr
|| cons
->expr
->expr_type
!= EXPR_NULL
)
4971 cons
= gfc_constructor_next (cons
);
4977 /* Check for default initializer; sym->value is not enough
4978 as it is also set for EXPR_NULL of allocatables. */
4981 gfc_has_default_initializer (gfc_symbol
*der
)
4985 gcc_assert (gfc_fl_struct (der
->attr
.flavor
));
4986 for (c
= der
->components
; c
; c
= c
->next
)
4987 if (gfc_bt_struct (c
->ts
.type
))
4989 if (!c
->attr
.pointer
&& !c
->attr
.proc_pointer
4990 && !(c
->attr
.allocatable
&& der
== c
->ts
.u
.derived
)
4992 && is_non_empty_structure_constructor (c
->initializer
))
4993 || gfc_has_default_initializer (c
->ts
.u
.derived
)))
4995 if (c
->attr
.pointer
&& c
->initializer
)
5009 Generate an initializer expression which initializes the entirety of a union.
5010 A normal structure constructor is insufficient without undue effort, because
5011 components of maps may be oddly aligned/overlapped. (For example if a
5012 character is initialized from one map overtop a real from the other, only one
5013 byte of the real is actually initialized.) Unfortunately we don't know the
5014 size of the union right now, so we can't generate a proper initializer, but
5015 we use a NULL expr as a placeholder and do the right thing later in
5016 gfc_trans_subcomponent_assign.
5019 generate_union_initializer (gfc_component
*un
)
5021 if (un
== NULL
|| un
->ts
.type
!= BT_UNION
)
5024 gfc_expr
*placeholder
= gfc_get_null_expr (&un
->loc
);
5025 placeholder
->ts
= un
->ts
;
5030 /* Get the user-specified initializer for a union, if any. This means the user
5031 has said to initialize component(s) of a map. For simplicity's sake we
5032 only allow the user to initialize the first map. We don't have to worry
5033 about overlapping initializers as they are released early in resolution (see
5034 resolve_fl_struct). */
5037 get_union_initializer (gfc_symbol
*union_type
, gfc_component
**map_p
)
5040 gfc_expr
*init
=NULL
;
5042 if (!union_type
|| union_type
->attr
.flavor
!= FL_UNION
)
5045 for (map
= union_type
->components
; map
; map
= map
->next
)
5047 if (gfc_has_default_initializer (map
->ts
.u
.derived
))
5049 init
= gfc_default_initializer (&map
->ts
);
5063 class_allocatable (gfc_component
*comp
)
5065 return comp
->ts
.type
== BT_CLASS
&& comp
->attr
.class_ok
&& CLASS_DATA (comp
)
5066 && CLASS_DATA (comp
)->attr
.allocatable
;
5070 class_pointer (gfc_component
*comp
)
5072 return comp
->ts
.type
== BT_CLASS
&& comp
->attr
.class_ok
&& CLASS_DATA (comp
)
5073 && CLASS_DATA (comp
)->attr
.pointer
;
5077 comp_allocatable (gfc_component
*comp
)
5079 return comp
->attr
.allocatable
|| class_allocatable (comp
);
5083 comp_pointer (gfc_component
*comp
)
5085 return comp
->attr
.pointer
5086 || comp
->attr
.proc_pointer
5087 || comp
->attr
.class_pointer
5088 || class_pointer (comp
);
5091 /* Fetch or generate an initializer for the given component.
5092 Only generate an initializer if generate is true. */
5095 component_initializer (gfc_component
*c
, bool generate
)
5097 gfc_expr
*init
= NULL
;
5099 /* Allocatable components always get EXPR_NULL.
5100 Pointer components are only initialized when generating, and only if they
5101 do not already have an initializer. */
5102 if (comp_allocatable (c
) || (generate
&& comp_pointer (c
) && !c
->initializer
))
5104 init
= gfc_get_null_expr (&c
->loc
);
5109 /* See if we can find the initializer immediately. */
5110 if (c
->initializer
|| !generate
)
5111 return c
->initializer
;
5113 /* Recursively handle derived type components. */
5114 else if (c
->ts
.type
== BT_DERIVED
|| c
->ts
.type
== BT_CLASS
)
5115 init
= gfc_generate_initializer (&c
->ts
, true);
5117 else if (c
->ts
.type
== BT_UNION
&& c
->ts
.u
.derived
->components
)
5119 gfc_component
*map
= NULL
;
5120 gfc_constructor
*ctor
;
5121 gfc_expr
*user_init
;
5123 /* If we don't have a user initializer and we aren't generating one, this
5124 union has no initializer. */
5125 user_init
= get_union_initializer (c
->ts
.u
.derived
, &map
);
5126 if (!user_init
&& !generate
)
5129 /* Otherwise use a structure constructor. */
5130 init
= gfc_get_structure_constructor_expr (c
->ts
.type
, c
->ts
.kind
,
5134 /* If we are to generate an initializer for the union, add a constructor
5135 which initializes the whole union first. */
5138 ctor
= gfc_constructor_get ();
5139 ctor
->expr
= generate_union_initializer (c
);
5140 gfc_constructor_append (&init
->value
.constructor
, ctor
);
5143 /* If we found an initializer in one of our maps, apply it. Note this
5144 is applied _after_ the entire-union initializer above if any. */
5147 ctor
= gfc_constructor_get ();
5148 ctor
->expr
= user_init
;
5149 ctor
->n
.component
= map
;
5150 gfc_constructor_append (&init
->value
.constructor
, ctor
);
5154 /* Treat simple components like locals. */
5157 /* We MUST give an initializer, so force generation. */
5158 init
= gfc_build_init_expr (&c
->ts
, &c
->loc
, true);
5159 gfc_apply_init (&c
->ts
, &c
->attr
, init
);
5166 /* Get an expression for a default initializer of a derived type. */
5169 gfc_default_initializer (gfc_typespec
*ts
)
5171 return gfc_generate_initializer (ts
, false);
5174 /* Generate an initializer expression for an iso_c_binding type
5175 such as c_[fun]ptr. The appropriate initializer is c_null_[fun]ptr. */
5178 generate_isocbinding_initializer (gfc_symbol
*derived
)
5180 /* The initializers have already been built into the c_null_[fun]ptr symbols
5181 from gen_special_c_interop_ptr. */
5182 gfc_symtree
*npsym
= NULL
;
5183 if (0 == strcmp (derived
->name
, "c_ptr"))
5184 gfc_find_sym_tree ("c_null_ptr", gfc_current_ns
, true, &npsym
);
5185 else if (0 == strcmp (derived
->name
, "c_funptr"))
5186 gfc_find_sym_tree ("c_null_funptr", gfc_current_ns
, true, &npsym
);
5188 gfc_internal_error ("generate_isocbinding_initializer(): bad iso_c_binding"
5189 " type, expected %<c_ptr%> or %<c_funptr%>");
5192 gfc_expr
*init
= gfc_copy_expr (npsym
->n
.sym
->value
);
5193 init
->symtree
= npsym
;
5194 init
->ts
.is_iso_c
= true;
5201 /* Get or generate an expression for a default initializer of a derived type.
5202 If -finit-derived is specified, generate default initialization expressions
5203 for components that lack them when generate is set. */
5206 gfc_generate_initializer (gfc_typespec
*ts
, bool generate
)
5208 gfc_expr
*init
, *tmp
;
5209 gfc_component
*comp
;
5211 generate
= flag_init_derived
&& generate
;
5213 if (ts
->u
.derived
->ts
.is_iso_c
&& generate
)
5214 return generate_isocbinding_initializer (ts
->u
.derived
);
5216 /* See if we have a default initializer in this, but not in nested
5217 types (otherwise we could use gfc_has_default_initializer()).
5218 We don't need to check if we are going to generate them. */
5219 comp
= ts
->u
.derived
->components
;
5222 for (; comp
; comp
= comp
->next
)
5223 if (comp
->initializer
|| comp_allocatable (comp
))
5230 init
= gfc_get_structure_constructor_expr (ts
->type
, ts
->kind
,
5231 &ts
->u
.derived
->declared_at
);
5234 for (comp
= ts
->u
.derived
->components
; comp
; comp
= comp
->next
)
5236 gfc_constructor
*ctor
= gfc_constructor_get();
5238 /* Fetch or generate an initializer for the component. */
5239 tmp
= component_initializer (comp
, generate
);
5242 /* Save the component ref for STRUCTUREs and UNIONs. */
5243 if (ts
->u
.derived
->attr
.flavor
== FL_STRUCT
5244 || ts
->u
.derived
->attr
.flavor
== FL_UNION
)
5245 ctor
->n
.component
= comp
;
5247 /* If the initializer was not generated, we need a copy. */
5248 ctor
->expr
= comp
->initializer
? gfc_copy_expr (tmp
) : tmp
;
5249 if ((comp
->ts
.type
!= tmp
->ts
.type
|| comp
->ts
.kind
!= tmp
->ts
.kind
)
5250 && !comp
->attr
.pointer
&& !comp
->attr
.proc_pointer
)
5253 val
= gfc_convert_type_warn (ctor
->expr
, &comp
->ts
, 1, false);
5259 gfc_constructor_append (&init
->value
.constructor
, ctor
);
5266 /* Given a symbol, create an expression node with that symbol as a
5267 variable. If the symbol is array valued, setup a reference of the
5271 gfc_get_variable_expr (gfc_symtree
*var
)
5275 e
= gfc_get_expr ();
5276 e
->expr_type
= EXPR_VARIABLE
;
5278 e
->ts
= var
->n
.sym
->ts
;
5280 if (var
->n
.sym
->attr
.flavor
!= FL_PROCEDURE
5281 && ((var
->n
.sym
->as
!= NULL
&& var
->n
.sym
->ts
.type
!= BT_CLASS
)
5282 || (var
->n
.sym
->ts
.type
== BT_CLASS
&& var
->n
.sym
->ts
.u
.derived
5283 && CLASS_DATA (var
->n
.sym
)
5284 && CLASS_DATA (var
->n
.sym
)->as
)))
5286 gfc_array_spec
*as
= var
->n
.sym
->ts
.type
== BT_CLASS
5287 ? CLASS_DATA (var
->n
.sym
)->as
5290 e
->corank
= as
->corank
;
5291 e
->ref
= gfc_get_ref ();
5292 e
->ref
->type
= REF_ARRAY
;
5293 e
->ref
->u
.ar
.type
= AR_FULL
;
5294 e
->ref
->u
.ar
.as
= gfc_copy_array_spec (as
);
5301 /* Adds a full array reference to an expression, as needed. */
5304 gfc_add_full_array_ref (gfc_expr
*e
, gfc_array_spec
*as
)
5307 for (ref
= e
->ref
; ref
; ref
= ref
->next
)
5312 ref
->next
= gfc_get_ref ();
5317 e
->ref
= gfc_get_ref ();
5320 ref
->type
= REF_ARRAY
;
5321 ref
->u
.ar
.type
= AR_FULL
;
5322 ref
->u
.ar
.dimen
= e
->rank
;
5323 /* Do not set the corank here, or resolve will not be able to set correct
5324 dimen-types for the coarray. */
5325 ref
->u
.ar
.where
= e
->where
;
5331 gfc_lval_expr_from_sym (gfc_symbol
*sym
)
5335 lval
= gfc_get_expr ();
5336 lval
->expr_type
= EXPR_VARIABLE
;
5337 lval
->where
= sym
->declared_at
;
5339 lval
->symtree
= gfc_find_symtree (sym
->ns
->sym_root
, sym
->name
);
5341 /* It will always be a full array. */
5342 as
= IS_CLASS_ARRAY (sym
) ? CLASS_DATA (sym
)->as
: sym
->as
;
5343 lval
->rank
= as
? as
->rank
: 0;
5344 lval
->corank
= as
? as
->corank
: 0;
5345 if (lval
->rank
|| lval
->corank
)
5346 gfc_add_full_array_ref (lval
, as
);
5351 /* Returns the array_spec of a full array expression. A NULL is
5352 returned otherwise. */
5354 gfc_get_full_arrayspec_from_expr (gfc_expr
*expr
)
5359 if (expr
->rank
== 0)
5362 /* Follow any component references. */
5363 if (expr
->expr_type
== EXPR_VARIABLE
5364 || expr
->expr_type
== EXPR_CONSTANT
)
5367 as
= expr
->symtree
->n
.sym
->as
;
5371 for (ref
= expr
->ref
; ref
; ref
= ref
->next
)
5376 as
= ref
->u
.c
.component
->as
;
5385 switch (ref
->u
.ar
.type
)
5408 /* General expression traversal function. */
5411 gfc_traverse_expr (gfc_expr
*expr
, gfc_symbol
*sym
,
5412 bool (*func
)(gfc_expr
*, gfc_symbol
*, int*),
5417 gfc_actual_arglist
*args
;
5424 if ((*func
) (expr
, sym
, &f
))
5427 if (expr
->ts
.type
== BT_CHARACTER
5429 && expr
->ts
.u
.cl
->length
5430 && expr
->ts
.u
.cl
->length
->expr_type
!= EXPR_CONSTANT
5431 && gfc_traverse_expr (expr
->ts
.u
.cl
->length
, sym
, func
, f
))
5434 switch (expr
->expr_type
)
5439 for (args
= expr
->value
.function
.actual
; args
; args
= args
->next
)
5441 if (gfc_traverse_expr (args
->expr
, sym
, func
, f
))
5449 case EXPR_SUBSTRING
:
5452 case EXPR_STRUCTURE
:
5454 for (c
= gfc_constructor_first (expr
->value
.constructor
);
5455 c
; c
= gfc_constructor_next (c
))
5457 if (gfc_traverse_expr (c
->expr
, sym
, func
, f
))
5461 if (gfc_traverse_expr (c
->iterator
->var
, sym
, func
, f
))
5463 if (gfc_traverse_expr (c
->iterator
->start
, sym
, func
, f
))
5465 if (gfc_traverse_expr (c
->iterator
->end
, sym
, func
, f
))
5467 if (gfc_traverse_expr (c
->iterator
->step
, sym
, func
, f
))
5474 if (gfc_traverse_expr (expr
->value
.op
.op1
, sym
, func
, f
))
5476 if (gfc_traverse_expr (expr
->value
.op
.op2
, sym
, func
, f
))
5492 for (i
= 0; i
< GFC_MAX_DIMENSIONS
; i
++)
5494 if (gfc_traverse_expr (ar
.start
[i
], sym
, func
, f
))
5496 if (gfc_traverse_expr (ar
.end
[i
], sym
, func
, f
))
5498 if (gfc_traverse_expr (ar
.stride
[i
], sym
, func
, f
))
5504 if (gfc_traverse_expr (ref
->u
.ss
.start
, sym
, func
, f
))
5506 if (gfc_traverse_expr (ref
->u
.ss
.end
, sym
, func
, f
))
5511 if (ref
->u
.c
.component
->ts
.type
== BT_CHARACTER
5512 && ref
->u
.c
.component
->ts
.u
.cl
5513 && ref
->u
.c
.component
->ts
.u
.cl
->length
5514 && ref
->u
.c
.component
->ts
.u
.cl
->length
->expr_type
5516 && gfc_traverse_expr (ref
->u
.c
.component
->ts
.u
.cl
->length
,
5520 if (ref
->u
.c
.component
->as
)
5521 for (i
= 0; i
< ref
->u
.c
.component
->as
->rank
5522 + ref
->u
.c
.component
->as
->corank
; i
++)
5524 if (gfc_traverse_expr (ref
->u
.c
.component
->as
->lower
[i
],
5527 if (gfc_traverse_expr (ref
->u
.c
.component
->as
->upper
[i
],
5544 /* Traverse expr, marking all EXPR_VARIABLE symbols referenced. */
5547 expr_set_symbols_referenced (gfc_expr
*expr
,
5548 gfc_symbol
*sym ATTRIBUTE_UNUSED
,
5549 int *f ATTRIBUTE_UNUSED
)
5551 if (expr
->expr_type
!= EXPR_VARIABLE
)
5553 gfc_set_sym_referenced (expr
->symtree
->n
.sym
);
5558 gfc_expr_set_symbols_referenced (gfc_expr
*expr
)
5560 gfc_traverse_expr (expr
, NULL
, expr_set_symbols_referenced
, 0);
5564 /* Determine if an expression is a procedure pointer component and return
5565 the component in that case. Otherwise return NULL. */
5568 gfc_get_proc_ptr_comp (gfc_expr
*expr
)
5572 if (!expr
|| !expr
->ref
)
5579 if (ref
->type
== REF_COMPONENT
5580 && ref
->u
.c
.component
->attr
.proc_pointer
)
5581 return ref
->u
.c
.component
;
5587 /* Determine if an expression is a procedure pointer component. */
5590 gfc_is_proc_ptr_comp (gfc_expr
*expr
)
5592 return (gfc_get_proc_ptr_comp (expr
) != NULL
);
5596 /* Determine if an expression is a function with an allocatable class scalar
5599 gfc_is_alloc_class_scalar_function (gfc_expr
*expr
)
5601 if (expr
->expr_type
== EXPR_FUNCTION
5602 && ((expr
->value
.function
.esym
5603 && expr
->value
.function
.esym
->result
5604 && expr
->value
.function
.esym
->result
->ts
.type
== BT_CLASS
5605 && !CLASS_DATA (expr
->value
.function
.esym
->result
)->attr
.dimension
5606 && CLASS_DATA (expr
->value
.function
.esym
->result
)->attr
.allocatable
)
5607 || (expr
->ts
.type
== BT_CLASS
5608 && CLASS_DATA (expr
)->attr
.allocatable
5609 && !CLASS_DATA (expr
)->attr
.dimension
)))
5616 /* Determine if an expression is a function with an allocatable class array
5619 gfc_is_class_array_function (gfc_expr
*expr
)
5621 if (expr
->expr_type
== EXPR_FUNCTION
5622 && expr
->value
.function
.esym
5623 && expr
->value
.function
.esym
->result
5624 && expr
->value
.function
.esym
->result
->ts
.type
== BT_CLASS
5625 && CLASS_DATA (expr
->value
.function
.esym
->result
)->attr
.dimension
5626 && (CLASS_DATA (expr
->value
.function
.esym
->result
)->attr
.allocatable
5627 || CLASS_DATA (expr
->value
.function
.esym
->result
)->attr
.pointer
))
5634 /* Walk an expression tree and check each variable encountered for being typed.
5635 If strict is not set, a top-level variable is tolerated untyped in -std=gnu
5636 mode as is a basic arithmetic expression using those; this is for things in
5639 INTEGER :: arr(n), n
5640 INTEGER :: arr(n + 1), n
5642 The namespace is needed for IMPLICIT typing. */
5644 static gfc_namespace
* check_typed_ns
;
5647 expr_check_typed_help (gfc_expr
* e
, gfc_symbol
* sym ATTRIBUTE_UNUSED
,
5648 int* f ATTRIBUTE_UNUSED
)
5652 if (e
->expr_type
!= EXPR_VARIABLE
)
5655 gcc_assert (e
->symtree
);
5656 t
= gfc_check_symbol_typed (e
->symtree
->n
.sym
, check_typed_ns
,
5663 gfc_expr_check_typed (gfc_expr
* e
, gfc_namespace
* ns
, bool strict
)
5667 /* If this is a top-level variable or EXPR_OP, do the check with strict given
5671 if (e
->expr_type
== EXPR_VARIABLE
&& !e
->ref
)
5672 return gfc_check_symbol_typed (e
->symtree
->n
.sym
, ns
, strict
, e
->where
);
5674 if (e
->expr_type
== EXPR_OP
)
5678 gcc_assert (e
->value
.op
.op1
);
5679 t
= gfc_expr_check_typed (e
->value
.op
.op1
, ns
, strict
);
5681 if (t
&& e
->value
.op
.op2
)
5682 t
= gfc_expr_check_typed (e
->value
.op
.op2
, ns
, strict
);
5688 /* Otherwise, walk the expression and do it strictly. */
5689 check_typed_ns
= ns
;
5690 error_found
= gfc_traverse_expr (e
, NULL
, &expr_check_typed_help
, 0);
5692 return error_found
? false : true;
5696 /* This function returns true if it contains any references to PDT KIND
5697 or LEN parameters. */
5700 derived_parameter_expr (gfc_expr
* e
, gfc_symbol
* sym ATTRIBUTE_UNUSED
,
5701 int* f ATTRIBUTE_UNUSED
)
5703 if (e
->expr_type
!= EXPR_VARIABLE
)
5706 gcc_assert (e
->symtree
);
5707 if (e
->symtree
->n
.sym
->attr
.pdt_kind
5708 || e
->symtree
->n
.sym
->attr
.pdt_len
)
5716 gfc_derived_parameter_expr (gfc_expr
*e
)
5718 return gfc_traverse_expr (e
, NULL
, &derived_parameter_expr
, 0);
5722 /* This function returns the overall type of a type parameter spec list.
5723 If all the specs are explicit, SPEC_EXPLICIT is returned. If any of the
5724 parameters are assumed/deferred then SPEC_ASSUMED/DEFERRED is returned
5725 unless derived is not NULL. In this latter case, all the LEN parameters
5726 must be either assumed or deferred for the return argument to be set to
5727 anything other than SPEC_EXPLICIT. */
5730 gfc_spec_list_type (gfc_actual_arglist
*param_list
, gfc_symbol
*derived
)
5732 gfc_param_spec_type res
= SPEC_EXPLICIT
;
5734 bool seen_assumed
= false;
5735 bool seen_deferred
= false;
5737 if (derived
== NULL
)
5739 for (; param_list
; param_list
= param_list
->next
)
5740 if (param_list
->spec_type
== SPEC_ASSUMED
5741 || param_list
->spec_type
== SPEC_DEFERRED
)
5742 return param_list
->spec_type
;
5746 for (; param_list
; param_list
= param_list
->next
)
5748 c
= gfc_find_component (derived
, param_list
->name
,
5750 gcc_assert (c
!= NULL
);
5751 if (c
->attr
.pdt_kind
)
5753 else if (param_list
->spec_type
== SPEC_EXPLICIT
)
5754 return SPEC_EXPLICIT
;
5755 seen_assumed
= param_list
->spec_type
== SPEC_ASSUMED
;
5756 seen_deferred
= param_list
->spec_type
== SPEC_DEFERRED
;
5757 if (seen_assumed
&& seen_deferred
)
5758 return SPEC_EXPLICIT
;
5760 res
= seen_assumed
? SPEC_ASSUMED
: SPEC_DEFERRED
;
5767 gfc_ref_this_image (gfc_ref
*ref
)
5771 gcc_assert (ref
->type
== REF_ARRAY
&& ref
->u
.ar
.codimen
> 0);
5773 for (n
= ref
->u
.ar
.dimen
; n
< ref
->u
.ar
.dimen
+ ref
->u
.ar
.codimen
; n
++)
5774 if (ref
->u
.ar
.dimen_type
[n
] != DIMEN_THIS_IMAGE
)
5781 gfc_find_team_co (gfc_expr
*e
)
5785 for (ref
= e
->ref
; ref
; ref
= ref
->next
)
5786 if (ref
->type
== REF_ARRAY
&& ref
->u
.ar
.codimen
> 0)
5787 return ref
->u
.ar
.team
;
5789 if (e
->value
.function
.actual
->expr
)
5790 for (ref
= e
->value
.function
.actual
->expr
->ref
; ref
;
5792 if (ref
->type
== REF_ARRAY
&& ref
->u
.ar
.codimen
> 0)
5793 return ref
->u
.ar
.team
;
5799 gfc_find_stat_co (gfc_expr
*e
)
5803 for (ref
= e
->ref
; ref
; ref
= ref
->next
)
5804 if (ref
->type
== REF_ARRAY
&& ref
->u
.ar
.codimen
> 0)
5805 return ref
->u
.ar
.stat
;
5807 if (e
->value
.function
.actual
->expr
)
5808 for (ref
= e
->value
.function
.actual
->expr
->ref
; ref
;
5810 if (ref
->type
== REF_ARRAY
&& ref
->u
.ar
.codimen
> 0)
5811 return ref
->u
.ar
.stat
;
5817 gfc_is_coindexed (gfc_expr
*e
)
5821 if (e
->expr_type
== EXPR_FUNCTION
&& e
->value
.function
.isym
5822 && e
->value
.function
.isym
->id
== GFC_ISYM_CAF_GET
)
5823 e
= e
->value
.function
.actual
->expr
;
5825 for (ref
= e
->ref
; ref
; ref
= ref
->next
)
5826 if (ref
->type
== REF_ARRAY
&& ref
->u
.ar
.codimen
> 0)
5827 return !gfc_ref_this_image (ref
);
5833 /* Coarrays are variables with a corank but not being coindexed. However, also
5834 the following is a coarray: A subobject of a coarray is a coarray if it does
5835 not have any cosubscripts, vector subscripts, allocatable component
5836 selection, or pointer component selection. (F2008, 2.4.7) */
5839 gfc_is_coarray (gfc_expr
*e
)
5843 gfc_component
*comp
;
5848 if (e
->expr_type
!= EXPR_VARIABLE
)
5852 sym
= e
->symtree
->n
.sym
;
5854 if (sym
->ts
.type
== BT_CLASS
&& sym
->attr
.class_ok
)
5855 coarray
= CLASS_DATA (sym
)->attr
.codimension
;
5857 coarray
= sym
->attr
.codimension
;
5859 for (ref
= e
->ref
; ref
; ref
= ref
->next
)
5863 comp
= ref
->u
.c
.component
;
5864 if (comp
->ts
.type
== BT_CLASS
&& comp
->attr
.class_ok
5865 && (CLASS_DATA (comp
)->attr
.class_pointer
5866 || CLASS_DATA (comp
)->attr
.allocatable
))
5869 coarray
= CLASS_DATA (comp
)->attr
.codimension
;
5871 else if (comp
->attr
.pointer
|| comp
->attr
.allocatable
)
5874 coarray
= comp
->attr
.codimension
;
5882 if (ref
->u
.ar
.codimen
> 0 && !gfc_ref_this_image (ref
))
5888 for (i
= 0; i
< ref
->u
.ar
.dimen
; i
++)
5889 if (ref
->u
.ar
.dimen_type
[i
] == DIMEN_VECTOR
)
5901 return coarray
&& !coindexed
;
5905 /* Check whether the expression has an ultimate allocatable component.
5906 Being itself allocatable does not count. */
5908 gfc_has_ultimate_allocatable (gfc_expr
*e
)
5910 gfc_ref
*ref
, *last
= NULL
;
5912 if (e
->expr_type
!= EXPR_VARIABLE
)
5915 for (ref
= e
->ref
; ref
; ref
= ref
->next
)
5916 if (ref
->type
== REF_COMPONENT
)
5919 if (last
&& last
->u
.c
.component
->ts
.type
== BT_CLASS
)
5920 return CLASS_DATA (last
->u
.c
.component
)->attr
.alloc_comp
;
5921 else if (last
&& last
->u
.c
.component
->ts
.type
== BT_DERIVED
)
5922 return last
->u
.c
.component
->ts
.u
.derived
->attr
.alloc_comp
;
5926 if (e
->ts
.type
== BT_CLASS
)
5927 return CLASS_DATA (e
)->attr
.alloc_comp
;
5928 else if (e
->ts
.type
== BT_DERIVED
)
5929 return e
->ts
.u
.derived
->attr
.alloc_comp
;
5935 /* Check whether the expression has an pointer component.
5936 Being itself a pointer does not count. */
5938 gfc_has_ultimate_pointer (gfc_expr
*e
)
5940 gfc_ref
*ref
, *last
= NULL
;
5942 if (e
->expr_type
!= EXPR_VARIABLE
)
5945 for (ref
= e
->ref
; ref
; ref
= ref
->next
)
5946 if (ref
->type
== REF_COMPONENT
)
5949 if (last
&& last
->u
.c
.component
->ts
.type
== BT_CLASS
)
5950 return CLASS_DATA (last
->u
.c
.component
)->attr
.pointer_comp
;
5951 else if (last
&& last
->u
.c
.component
->ts
.type
== BT_DERIVED
)
5952 return last
->u
.c
.component
->ts
.u
.derived
->attr
.pointer_comp
;
5956 if (e
->ts
.type
== BT_CLASS
)
5957 return CLASS_DATA (e
)->attr
.pointer_comp
;
5958 else if (e
->ts
.type
== BT_DERIVED
)
5959 return e
->ts
.u
.derived
->attr
.pointer_comp
;
5965 /* Check whether an expression is "simply contiguous", cf. F2008, 6.5.4.
5966 Note: A scalar is not regarded as "simply contiguous" by the standard.
5967 if bool is not strict, some further checks are done - for instance,
5968 a "(::1)" is accepted. */
5971 gfc_is_simply_contiguous (gfc_expr
*expr
, bool strict
, bool permit_element
)
5975 gfc_array_ref
*ar
= NULL
;
5976 gfc_ref
*ref
, *part_ref
= NULL
;
5979 if (expr
->expr_type
== EXPR_ARRAY
)
5982 if (expr
->expr_type
== EXPR_NULL
)
5984 /* F2018:16.9.144 NULL ([MOLD]):
5985 "If MOLD is present, the characteristics are the same as MOLD."
5986 "If MOLD is absent, the characteristics of the result are
5987 determined by the entity with which the reference is associated."
5988 F2018:15.3.2.2 characteristics attributes include CONTIGUOUS. */
5989 if (expr
->ts
.type
== BT_UNKNOWN
)
5992 return (gfc_variable_attr (expr
, NULL
).contiguous
5993 || gfc_variable_attr (expr
, NULL
).allocatable
);
5996 if (expr
->expr_type
== EXPR_FUNCTION
)
5998 if (expr
->value
.function
.isym
)
5999 /* TRANSPOSE is the only intrinsic that may return a
6000 non-contiguous array. It's treated as a special case in
6001 gfc_conv_expr_descriptor too. */
6002 return (expr
->value
.function
.isym
->id
!= GFC_ISYM_TRANSPOSE
);
6003 else if (expr
->value
.function
.esym
)
6004 /* Only a pointer to an array without the contiguous attribute
6005 can be non-contiguous as a result value. */
6006 return (expr
->value
.function
.esym
->result
->attr
.contiguous
6007 || !expr
->value
.function
.esym
->result
->attr
.pointer
);
6010 /* Type-bound procedures. */
6011 gfc_symbol
*s
= expr
->symtree
->n
.sym
;
6012 if (s
->ts
.type
!= BT_CLASS
&& s
->ts
.type
!= BT_DERIVED
)
6016 for (gfc_ref
*r
= expr
->ref
; r
; r
= r
->next
)
6017 if (r
->type
== REF_COMPONENT
)
6020 if (rc
== NULL
|| rc
->u
.c
.component
== NULL
6021 || rc
->u
.c
.component
->ts
.interface
== NULL
)
6024 return rc
->u
.c
.component
->ts
.interface
->attr
.contiguous
;
6027 else if (expr
->expr_type
!= EXPR_VARIABLE
)
6030 if (!permit_element
&& expr
->rank
== 0)
6033 for (ref
= expr
->ref
; ref
; ref
= ref
->next
)
6036 return false; /* Array shall be last part-ref. */
6038 if (ref
->type
== REF_COMPONENT
)
6040 else if (ref
->type
== REF_SUBSTRING
)
6042 else if (ref
->type
== REF_INQUIRY
)
6044 else if (ref
->u
.ar
.type
!= AR_ELEMENT
)
6048 sym
= expr
->symtree
->n
.sym
;
6050 && part_ref
->u
.c
.component
6051 && !part_ref
->u
.c
.component
->attr
.contiguous
6052 && IS_POINTER (part_ref
->u
.c
.component
))
6054 && expr
->ts
.type
!= BT_CLASS
6055 && !sym
->attr
.contiguous
6056 && (sym
->attr
.pointer
6057 || (sym
->as
&& sym
->as
->type
== AS_ASSUMED_RANK
)
6058 || (sym
->as
&& sym
->as
->type
== AS_ASSUMED_SHAPE
))))
6061 if (!ar
|| ar
->type
== AR_FULL
)
6064 gcc_assert (ar
->type
== AR_SECTION
);
6066 /* Check for simply contiguous array */
6068 for (i
= 0; i
< ar
->dimen
; i
++)
6070 if (ar
->dimen_type
[i
] == DIMEN_VECTOR
)
6073 if (ar
->dimen_type
[i
] == DIMEN_ELEMENT
)
6079 gcc_assert (ar
->dimen_type
[i
] == DIMEN_RANGE
);
6082 /* If the previous section was not contiguous, that's an error,
6083 unless we have effective only one element and checking is not
6085 if (!colon
&& (strict
|| !ar
->start
[i
] || !ar
->end
[i
]
6086 || ar
->start
[i
]->expr_type
!= EXPR_CONSTANT
6087 || ar
->end
[i
]->expr_type
!= EXPR_CONSTANT
6088 || mpz_cmp (ar
->start
[i
]->value
.integer
,
6089 ar
->end
[i
]->value
.integer
) != 0))
6092 /* Following the standard, "(::1)" or - if known at compile time -
6093 "(lbound:ubound)" are not simply contiguous; if strict
6094 is false, they are regarded as simply contiguous. */
6095 if (ar
->stride
[i
] && (strict
|| ar
->stride
[i
]->expr_type
!= EXPR_CONSTANT
6096 || ar
->stride
[i
]->ts
.type
!= BT_INTEGER
6097 || mpz_cmp_si (ar
->stride
[i
]->value
.integer
, 1) != 0))
6101 && (strict
|| ar
->start
[i
]->expr_type
!= EXPR_CONSTANT
6102 || !ar
->as
->lower
[i
]
6103 || ar
->as
->lower
[i
]->expr_type
!= EXPR_CONSTANT
6104 || mpz_cmp (ar
->start
[i
]->value
.integer
,
6105 ar
->as
->lower
[i
]->value
.integer
) != 0))
6109 && (strict
|| ar
->end
[i
]->expr_type
!= EXPR_CONSTANT
6110 || !ar
->as
->upper
[i
]
6111 || ar
->as
->upper
[i
]->expr_type
!= EXPR_CONSTANT
6112 || mpz_cmp (ar
->end
[i
]->value
.integer
,
6113 ar
->as
->upper
[i
]->value
.integer
) != 0))
6120 /* Return true if the expression is guaranteed to be non-contiguous,
6121 false if we cannot prove anything. It is probably best to call
6122 this after gfc_is_simply_contiguous. If neither of them returns
6123 true, we cannot say (at compile-time). */
6126 gfc_is_not_contiguous (gfc_expr
*array
)
6129 gfc_array_ref
*ar
= NULL
;
6131 bool previous_incomplete
;
6133 for (ref
= array
->ref
; ref
; ref
= ref
->next
)
6135 /* Array-ref shall be last ref. */
6137 if (ar
&& ar
->type
!= AR_ELEMENT
)
6140 if (ref
->type
== REF_ARRAY
)
6144 if (ar
== NULL
|| ar
->type
!= AR_SECTION
)
6147 previous_incomplete
= false;
6149 /* Check if we can prove that the array is not contiguous. */
6151 for (i
= 0; i
< ar
->dimen
; i
++)
6153 mpz_t arr_size
, ref_size
;
6155 if (gfc_ref_dimen_size (ar
, i
, &ref_size
, NULL
))
6157 if (gfc_dep_difference (ar
->as
->upper
[i
], ar
->as
->lower
[i
], &arr_size
))
6159 /* a(2:4,2:) is known to be non-contiguous, but
6160 a(2:4,i:i) can be contiguous. */
6161 mpz_add_ui (arr_size
, arr_size
, 1L);
6162 if (previous_incomplete
&& mpz_cmp_si (ref_size
, 1) != 0)
6164 mpz_clear (arr_size
);
6165 mpz_clear (ref_size
);
6168 else if (mpz_cmp (arr_size
, ref_size
) != 0)
6169 previous_incomplete
= true;
6171 mpz_clear (arr_size
);
6174 /* Check for a(::2), i.e. where the stride is not unity.
6175 This is only done if there is more than one element in
6176 the reference along this dimension. */
6178 if (mpz_cmp_ui (ref_size
, 1) > 0 && ar
->type
== AR_SECTION
6179 && ar
->dimen_type
[i
] == DIMEN_RANGE
6180 && ar
->stride
[i
] && ar
->stride
[i
]->expr_type
== EXPR_CONSTANT
6181 && mpz_cmp_si (ar
->stride
[i
]->value
.integer
, 1) != 0)
6183 mpz_clear (ref_size
);
6187 mpz_clear (ref_size
);
6190 /* We didn't find anything definitive. */
6194 /* Build call to an intrinsic procedure. The number of arguments has to be
6195 passed (rather than ending the list with a NULL value) because we may
6196 want to add arguments but with a NULL-expression. */
6199 gfc_build_intrinsic_call (gfc_namespace
*ns
, gfc_isym_id id
, const char* name
,
6200 locus where
, unsigned numarg
, ...)
6203 gfc_actual_arglist
* atail
;
6204 gfc_intrinsic_sym
* isym
;
6207 const char *mangled_name
= gfc_get_string (GFC_PREFIX ("%s"), name
);
6209 isym
= gfc_intrinsic_function_by_id (id
);
6212 result
= gfc_get_expr ();
6213 result
->expr_type
= EXPR_FUNCTION
;
6214 result
->ts
= isym
->ts
;
6215 result
->where
= where
;
6216 result
->value
.function
.name
= mangled_name
;
6217 result
->value
.function
.isym
= isym
;
6219 gfc_get_sym_tree (mangled_name
, ns
, &result
->symtree
, false);
6220 gfc_commit_symbol (result
->symtree
->n
.sym
);
6221 gcc_assert (result
->symtree
6222 && (result
->symtree
->n
.sym
->attr
.flavor
== FL_PROCEDURE
6223 || result
->symtree
->n
.sym
->attr
.flavor
== FL_UNKNOWN
));
6224 result
->symtree
->n
.sym
->intmod_sym_id
= id
;
6225 result
->symtree
->n
.sym
->attr
.flavor
= FL_PROCEDURE
;
6226 result
->symtree
->n
.sym
->attr
.intrinsic
= 1;
6227 result
->symtree
->n
.sym
->attr
.artificial
= 1;
6229 va_start (ap
, numarg
);
6231 for (i
= 0; i
< numarg
; ++i
)
6235 atail
->next
= gfc_get_actual_arglist ();
6236 atail
= atail
->next
;
6239 atail
= result
->value
.function
.actual
= gfc_get_actual_arglist ();
6241 atail
->expr
= va_arg (ap
, gfc_expr
*);
6249 /* Check if an expression may appear in a variable definition context
6250 (F2008, 16.6.7) or pointer association context (F2008, 16.6.8).
6251 This is called from the various places when resolving
6252 the pieces that make up such a context.
6253 If own_scope is true (applies to, e.g., ac-implied-do/data-implied-do
6254 variables), some checks are not performed.
6256 Optionally, a possible error message can be suppressed if context is NULL
6257 and just the return status (true / false) be requested. */
6260 gfc_check_vardef_context (gfc_expr
* e
, bool pointer
, bool alloc_obj
,
6261 bool own_scope
, const char* context
)
6263 gfc_symbol
* sym
= NULL
;
6265 bool check_intentin
;
6267 symbol_attribute attr
;
6271 if (e
->expr_type
== EXPR_VARIABLE
)
6273 gcc_assert (e
->symtree
);
6274 sym
= e
->symtree
->n
.sym
;
6276 else if (e
->expr_type
== EXPR_FUNCTION
)
6278 gcc_assert (e
->symtree
);
6279 sym
= e
->value
.function
.esym
? e
->value
.function
.esym
: e
->symtree
->n
.sym
;
6282 attr
= gfc_expr_attr (e
);
6283 if (!pointer
&& e
->expr_type
== EXPR_FUNCTION
&& attr
.pointer
)
6285 if (!(gfc_option
.allow_std
& GFC_STD_F2008
))
6288 gfc_error ("Fortran 2008: Pointer functions in variable definition"
6289 " context (%s) at %L", context
, &e
->where
);
6293 else if (e
->expr_type
!= EXPR_VARIABLE
)
6296 gfc_error ("Non-variable expression in variable definition context (%s)"
6297 " at %L", context
, &e
->where
);
6301 if (!pointer
&& sym
->attr
.flavor
== FL_PARAMETER
)
6304 gfc_error ("Named constant %qs in variable definition context (%s)"
6305 " at %L", sym
->name
, context
, &e
->where
);
6308 if (!pointer
&& sym
->attr
.flavor
!= FL_VARIABLE
6309 && !(sym
->attr
.flavor
== FL_PROCEDURE
&& sym
== sym
->result
)
6310 && !(sym
->attr
.flavor
== FL_PROCEDURE
&& sym
->attr
.proc_pointer
)
6311 && !(sym
->attr
.flavor
== FL_PROCEDURE
6312 && sym
->attr
.function
&& attr
.pointer
))
6315 gfc_error ("%qs in variable definition context (%s) at %L is not"
6316 " a variable", sym
->name
, context
, &e
->where
);
6320 /* Find out whether the expr is a pointer; this also means following
6321 component references to the last one. */
6322 is_pointer
= (attr
.pointer
|| attr
.proc_pointer
);
6323 if (pointer
&& !is_pointer
)
6326 gfc_error ("Non-POINTER in pointer association context (%s)"
6327 " at %L", context
, &e
->where
);
6331 if (e
->ts
.type
== BT_DERIVED
6332 && e
->ts
.u
.derived
== NULL
)
6335 gfc_error ("Type inaccessible in variable definition context (%s) "
6336 "at %L", context
, &e
->where
);
6343 || (e
->ts
.type
== BT_DERIVED
6344 && e
->ts
.u
.derived
->from_intmod
== INTMOD_ISO_FORTRAN_ENV
6345 && e
->ts
.u
.derived
->intmod_sym_id
== ISOFORTRAN_LOCK_TYPE
)))
6348 gfc_error ("LOCK_TYPE in variable definition context (%s) at %L",
6349 context
, &e
->where
);
6353 /* TS18508, C702/C203. */
6356 || (e
->ts
.type
== BT_DERIVED
6357 && e
->ts
.u
.derived
->from_intmod
== INTMOD_ISO_FORTRAN_ENV
6358 && e
->ts
.u
.derived
->intmod_sym_id
== ISOFORTRAN_EVENT_TYPE
)))
6361 gfc_error ("LOCK_EVENT in variable definition context (%s) at %L",
6362 context
, &e
->where
);
6366 /* INTENT(IN) dummy argument. Check this, unless the object itself is the
6367 component of sub-component of a pointer; we need to distinguish
6368 assignment to a pointer component from pointer-assignment to a pointer
6369 component. Note that (normal) assignment to procedure pointers is not
6371 check_intentin
= !own_scope
;
6372 ptr_component
= (sym
->ts
.type
== BT_CLASS
&& sym
->ts
.u
.derived
6373 && CLASS_DATA (sym
))
6374 ? CLASS_DATA (sym
)->attr
.class_pointer
: sym
->attr
.pointer
;
6375 for (ref
= e
->ref
; ref
&& check_intentin
; ref
= ref
->next
)
6377 if (ptr_component
&& ref
->type
== REF_COMPONENT
)
6378 check_intentin
= false;
6379 if (ref
->type
== REF_COMPONENT
)
6381 gfc_component
*comp
= ref
->u
.c
.component
;
6382 ptr_component
= (comp
->ts
.type
== BT_CLASS
&& comp
->attr
.class_ok
)
6383 ? CLASS_DATA (comp
)->attr
.class_pointer
6384 : comp
->attr
.pointer
;
6385 if (ptr_component
&& !pointer
)
6386 check_intentin
= false;
6388 if (ref
->type
== REF_INQUIRY
6389 && (ref
->u
.i
== INQUIRY_KIND
|| ref
->u
.i
== INQUIRY_LEN
))
6392 gfc_error ("%qs parameter inquiry for %qs in "
6393 "variable definition context (%s) at %L",
6394 ref
->u
.i
== INQUIRY_KIND
? "KIND" : "LEN",
6395 sym
->name
, context
, &e
->where
);
6401 && (sym
->attr
.intent
== INTENT_IN
6402 || (sym
->attr
.select_type_temporary
&& sym
->assoc
6403 && sym
->assoc
->target
&& sym
->assoc
->target
->symtree
6404 && sym
->assoc
->target
->symtree
->n
.sym
->attr
.intent
== INTENT_IN
)))
6406 if (pointer
&& is_pointer
)
6409 gfc_error ("Dummy argument %qs with INTENT(IN) in pointer"
6410 " association context (%s) at %L",
6411 sym
->name
, context
, &e
->where
);
6414 if (!pointer
&& !is_pointer
&& !sym
->attr
.pointer
)
6416 const char *name
= sym
->attr
.select_type_temporary
6417 ? sym
->assoc
->target
->symtree
->name
: sym
->name
;
6419 gfc_error ("Dummy argument %qs with INTENT(IN) in variable"
6420 " definition context (%s) at %L",
6421 name
, context
, &e
->where
);
6426 /* PROTECTED and use-associated. */
6427 if (sym
->attr
.is_protected
&& sym
->attr
.use_assoc
&& check_intentin
)
6429 if (pointer
&& is_pointer
)
6432 gfc_error ("Variable %qs is PROTECTED and cannot appear in a"
6433 " pointer association context (%s) at %L",
6434 sym
->name
, context
, &e
->where
);
6437 if (!pointer
&& !is_pointer
)
6440 gfc_error ("Variable %qs is PROTECTED and cannot appear in a"
6441 " variable definition context (%s) at %L",
6442 sym
->name
, context
, &e
->where
);
6447 /* Variable not assignable from a PURE procedure but appears in
6448 variable definition context. */
6449 own_scope
= own_scope
6450 || (sym
->attr
.result
&& sym
->ns
->proc_name
6451 && sym
== sym
->ns
->proc_name
->result
);
6452 if (!pointer
&& !own_scope
&& gfc_pure (NULL
) && gfc_impure_variable (sym
))
6455 gfc_error ("Variable %qs cannot appear in a variable definition"
6456 " context (%s) at %L in PURE procedure",
6457 sym
->name
, context
, &e
->where
);
6461 if (!pointer
&& context
&& gfc_implicit_pure (NULL
)
6462 && gfc_impure_variable (sym
))
6467 for (ns
= gfc_current_ns
; ns
; ns
= ns
->parent
)
6469 sym
= ns
->proc_name
;
6472 if (sym
->attr
.flavor
== FL_PROCEDURE
)
6474 sym
->attr
.implicit_pure
= 0;
6479 /* Check variable definition context for associate-names. */
6480 if (!pointer
&& sym
->assoc
&& !sym
->attr
.select_rank_temporary
)
6483 gfc_association_list
* assoc
;
6485 gcc_assert (sym
->assoc
->target
);
6487 /* If this is a SELECT TYPE temporary (the association is used internally
6488 for SELECT TYPE), silently go over to the target. */
6489 if (sym
->attr
.select_type_temporary
)
6491 gfc_expr
* t
= sym
->assoc
->target
;
6493 gcc_assert (t
->expr_type
== EXPR_VARIABLE
);
6494 name
= t
->symtree
->name
;
6496 if (t
->symtree
->n
.sym
->assoc
)
6497 assoc
= t
->symtree
->n
.sym
->assoc
;
6506 gcc_assert (name
&& assoc
);
6508 /* Is association to a valid variable? */
6509 if (!assoc
->variable
)
6513 if (assoc
->target
->expr_type
== EXPR_VARIABLE
6514 && gfc_has_vector_index (assoc
->target
))
6515 gfc_error ("%qs at %L associated to vector-indexed target"
6516 " cannot be used in a variable definition"
6518 name
, &e
->where
, context
);
6520 gfc_error ("%qs at %L associated to expression"
6521 " cannot be used in a variable definition"
6523 name
, &e
->where
, context
);
6527 else if (context
&& gfc_is_ptr_fcn (assoc
->target
))
6529 if (!gfc_notify_std (GFC_STD_F2018
, "%qs at %L associated to "
6530 "pointer function target being used in a "
6531 "variable definition context (%s)", name
,
6532 &e
->where
, context
))
6534 else if (gfc_has_vector_index (e
))
6536 gfc_error ("%qs at %L associated to vector-indexed target"
6537 " cannot be used in a variable definition"
6539 name
, &e
->where
, context
);
6544 /* Target must be allowed to appear in a variable definition context. */
6545 if (!gfc_check_vardef_context (assoc
->target
, pointer
, false, false, NULL
))
6548 gfc_error ("Associate-name %qs cannot appear in a variable"
6549 " definition context (%s) at %L because its target"
6550 " at %L cannot, either",
6551 name
, context
, &e
->where
,
6552 &assoc
->target
->where
);
6557 /* Check for same value in vector expression subscript. */
6560 for (ref
= e
->ref
; ref
!= NULL
; ref
= ref
->next
)
6561 if (ref
->type
== REF_ARRAY
&& ref
->u
.ar
.type
== AR_SECTION
)
6562 for (i
= 0; i
< GFC_MAX_DIMENSIONS
6563 && ref
->u
.ar
.dimen_type
[i
] != 0; i
++)
6564 if (ref
->u
.ar
.dimen_type
[i
] == DIMEN_VECTOR
)
6566 gfc_expr
*arr
= ref
->u
.ar
.start
[i
];
6567 if (arr
->expr_type
== EXPR_ARRAY
)
6569 gfc_constructor
*c
, *n
;
6572 for (c
= gfc_constructor_first (arr
->value
.constructor
);
6573 c
!= NULL
; c
= gfc_constructor_next (c
))
6575 if (c
== NULL
|| c
->iterator
!= NULL
)
6580 for (n
= gfc_constructor_next (c
); n
!= NULL
;
6581 n
= gfc_constructor_next (n
))
6583 if (n
->iterator
!= NULL
)
6587 if (gfc_dep_compare_expr (ec
, en
) == 0)
6590 gfc_error_now ("Elements with the same value "
6591 "at %L and %L in vector "
6592 "subscript in a variable "
6593 "definition context (%s)",
6594 &(ec
->where
), &(en
->where
),
6607 gfc_pdt_find_component_copy_initializer (gfc_symbol
*sym
, const char *name
)
6609 /* The actual length of a pdt is in its components. In the
6610 initializer of the current ref is only the default value.
6611 Therefore traverse the chain of components and pick the correct
6612 one's initializer expressions. */
6613 for (gfc_component
*comp
= sym
->ts
.u
.derived
->components
; comp
!= NULL
;
6616 if (!strcmp (comp
->name
, name
))
6617 return gfc_copy_expr (comp
->initializer
);