1 /* Routines for manipulation of expression nodes.
2 Copyright (C) 2000-2022 Free Software Foundation, Inc.
3 Contributed by Andy Vaught
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
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
;
162 mpz_init (e
->value
.integer
);
166 gfc_set_model_kind (kind
);
167 mpfr_init (e
->value
.real
);
171 gfc_set_model_kind (kind
);
172 mpc_init2 (e
->value
.complex, mpfr_get_default_prec());
183 /* Get a new expression node that is an string constant.
184 If no string is passed, a string of len is allocated,
185 blanked and null-terminated. */
188 gfc_get_character_expr (int kind
, locus
*where
, const char *src
, gfc_charlen_t len
)
195 dest
= gfc_get_wide_string (len
+ 1);
196 gfc_wide_memset (dest
, ' ', len
);
200 dest
= gfc_char_to_widechar (src
);
202 e
= gfc_get_constant_expr (BT_CHARACTER
, kind
,
203 where
? where
: &gfc_current_locus
);
204 e
->value
.character
.string
= dest
;
205 e
->value
.character
.length
= len
;
211 /* Get a new expression node that is an integer constant. */
214 gfc_get_int_expr (int kind
, locus
*where
, HOST_WIDE_INT value
)
217 p
= gfc_get_constant_expr (BT_INTEGER
, kind
,
218 where
? where
: &gfc_current_locus
);
220 const wide_int w
= wi::shwi (value
, kind
* BITS_PER_UNIT
);
221 wi::to_mpz (w
, p
->value
.integer
, SIGNED
);
227 /* Get a new expression node that is a logical constant. */
230 gfc_get_logical_expr (int kind
, locus
*where
, bool value
)
233 p
= gfc_get_constant_expr (BT_LOGICAL
, kind
,
234 where
? where
: &gfc_current_locus
);
236 p
->value
.logical
= value
;
243 gfc_get_iokind_expr (locus
*where
, io_kind k
)
247 /* Set the types to something compatible with iokind. This is needed to
248 get through gfc_free_expr later since iokind really has no Basic Type,
252 e
->expr_type
= EXPR_CONSTANT
;
253 e
->ts
.type
= BT_LOGICAL
;
261 /* Given an expression pointer, return a copy of the expression. This
262 subroutine is recursive. */
265 gfc_copy_expr (gfc_expr
*p
)
277 switch (q
->expr_type
)
280 s
= gfc_get_wide_string (p
->value
.character
.length
+ 1);
281 q
->value
.character
.string
= s
;
282 memcpy (s
, p
->value
.character
.string
,
283 (p
->value
.character
.length
+ 1) * sizeof (gfc_char_t
));
287 /* Copy target representation, if it exists. */
288 if (p
->representation
.string
)
290 c
= XCNEWVEC (char, p
->representation
.length
+ 1);
291 q
->representation
.string
= c
;
292 memcpy (c
, p
->representation
.string
, (p
->representation
.length
+ 1));
295 /* Copy the values of any pointer components of p->value. */
299 mpz_init_set (q
->value
.integer
, p
->value
.integer
);
303 gfc_set_model_kind (q
->ts
.kind
);
304 mpfr_init (q
->value
.real
);
305 mpfr_set (q
->value
.real
, p
->value
.real
, GFC_RND_MODE
);
309 gfc_set_model_kind (q
->ts
.kind
);
310 mpc_init2 (q
->value
.complex, mpfr_get_default_prec());
311 mpc_set (q
->value
.complex, p
->value
.complex, GFC_MPC_RND_MODE
);
315 if (p
->representation
.string
316 && p
->ts
.kind
== gfc_default_character_kind
)
317 q
->value
.character
.string
318 = gfc_char_to_widechar (q
->representation
.string
);
321 s
= gfc_get_wide_string (p
->value
.character
.length
+ 1);
322 q
->value
.character
.string
= s
;
324 /* This is the case for the C_NULL_CHAR named constant. */
325 if (p
->value
.character
.length
== 0
326 && (p
->ts
.is_c_interop
|| p
->ts
.is_iso_c
))
329 /* Need to set the length to 1 to make sure the NUL
330 terminator is copied. */
331 q
->value
.character
.length
= 1;
334 memcpy (s
, p
->value
.character
.string
,
335 (p
->value
.character
.length
+ 1) * sizeof (gfc_char_t
));
344 break; /* Already done. */
347 q
->boz
.len
= p
->boz
.len
;
348 q
->boz
.rdx
= p
->boz
.rdx
;
349 q
->boz
.str
= XCNEWVEC (char, q
->boz
.len
+ 1);
350 strncpy (q
->boz
.str
, p
->boz
.str
, p
->boz
.len
);
355 /* Should never be reached. */
357 gfc_internal_error ("gfc_copy_expr(): Bad expr node");
364 switch (q
->value
.op
.op
)
367 case INTRINSIC_PARENTHESES
:
368 case INTRINSIC_UPLUS
:
369 case INTRINSIC_UMINUS
:
370 q
->value
.op
.op1
= gfc_copy_expr (p
->value
.op
.op1
);
373 default: /* Binary operators. */
374 q
->value
.op
.op1
= gfc_copy_expr (p
->value
.op
.op1
);
375 q
->value
.op
.op2
= gfc_copy_expr (p
->value
.op
.op2
);
382 q
->value
.function
.actual
=
383 gfc_copy_actual_arglist (p
->value
.function
.actual
);
388 q
->value
.compcall
.actual
=
389 gfc_copy_actual_arglist (p
->value
.compcall
.actual
);
390 q
->value
.compcall
.tbp
= p
->value
.compcall
.tbp
;
395 q
->value
.constructor
= gfc_constructor_copy (p
->value
.constructor
);
406 q
->shape
= gfc_copy_shape (p
->shape
, p
->rank
);
408 q
->ref
= gfc_copy_ref (p
->ref
);
411 q
->param_list
= gfc_copy_actual_arglist (p
->param_list
);
418 gfc_clear_shape (mpz_t
*shape
, int rank
)
422 for (i
= 0; i
< rank
; i
++)
423 mpz_clear (shape
[i
]);
428 gfc_free_shape (mpz_t
**shape
, int rank
)
433 gfc_clear_shape (*shape
, rank
);
439 /* Workhorse function for gfc_free_expr() that frees everything
440 beneath an expression node, but not the node itself. This is
441 useful when we want to simplify a node and replace it with
442 something else or the expression node belongs to another structure. */
445 free_expr0 (gfc_expr
*e
)
447 switch (e
->expr_type
)
450 /* Free any parts of the value that need freeing. */
454 mpz_clear (e
->value
.integer
);
458 mpfr_clear (e
->value
.real
);
462 free (e
->value
.character
.string
);
466 mpc_clear (e
->value
.complex);
473 /* Free the representation. */
474 free (e
->representation
.string
);
479 if (e
->value
.op
.op1
!= NULL
)
480 gfc_free_expr (e
->value
.op
.op1
);
481 if (e
->value
.op
.op2
!= NULL
)
482 gfc_free_expr (e
->value
.op
.op2
);
486 gfc_free_actual_arglist (e
->value
.function
.actual
);
491 gfc_free_actual_arglist (e
->value
.compcall
.actual
);
499 gfc_constructor_free (e
->value
.constructor
);
503 free (e
->value
.character
.string
);
510 gfc_internal_error ("free_expr0(): Bad expr type");
513 /* Free a shape array. */
514 gfc_free_shape (&e
->shape
, e
->rank
);
516 gfc_free_ref_list (e
->ref
);
518 gfc_free_actual_arglist (e
->param_list
);
520 memset (e
, '\0', sizeof (gfc_expr
));
524 /* Free an expression node and everything beneath it. */
527 gfc_free_expr (gfc_expr
*e
)
536 /* Free an argument list and everything below it. */
539 gfc_free_actual_arglist (gfc_actual_arglist
*a1
)
541 gfc_actual_arglist
*a2
;
547 gfc_free_expr (a1
->expr
);
554 /* Copy an arglist structure and all of the arguments. */
557 gfc_copy_actual_arglist (gfc_actual_arglist
*p
)
559 gfc_actual_arglist
*head
, *tail
, *new_arg
;
563 for (; p
; p
= p
->next
)
565 new_arg
= gfc_get_actual_arglist ();
568 new_arg
->expr
= gfc_copy_expr (p
->expr
);
569 new_arg
->next
= NULL
;
574 tail
->next
= new_arg
;
583 /* Free a list of reference structures. */
586 gfc_free_ref_list (gfc_ref
*p
)
598 for (i
= 0; i
< GFC_MAX_DIMENSIONS
; i
++)
600 gfc_free_expr (p
->u
.ar
.start
[i
]);
601 gfc_free_expr (p
->u
.ar
.end
[i
]);
602 gfc_free_expr (p
->u
.ar
.stride
[i
]);
608 gfc_free_expr (p
->u
.ss
.start
);
609 gfc_free_expr (p
->u
.ss
.end
);
622 /* Graft the *src expression onto the *dest subexpression. */
625 gfc_replace_expr (gfc_expr
*dest
, gfc_expr
*src
)
633 /* Try to extract an integer constant from the passed expression node.
634 Return true if some error occurred, false on success. If REPORT_ERROR
635 is non-zero, emit error, for positive REPORT_ERROR using gfc_error,
636 for negative using gfc_error_now. */
639 gfc_extract_int (gfc_expr
*expr
, int *result
, int report_error
)
643 /* A KIND component is a parameter too. The expression for it
644 is stored in the initializer and should be consistent with
646 if (gfc_expr_attr(expr
).pdt_kind
)
648 for (ref
= expr
->ref
; ref
; ref
= ref
->next
)
650 if (ref
->u
.c
.component
->attr
.pdt_kind
)
651 expr
= ref
->u
.c
.component
->initializer
;
655 if (expr
->expr_type
!= EXPR_CONSTANT
)
657 if (report_error
> 0)
658 gfc_error ("Constant expression required at %C");
659 else if (report_error
< 0)
660 gfc_error_now ("Constant expression required at %C");
664 if (expr
->ts
.type
!= BT_INTEGER
)
666 if (report_error
> 0)
667 gfc_error ("Integer expression required at %C");
668 else if (report_error
< 0)
669 gfc_error_now ("Integer expression required at %C");
673 if ((mpz_cmp_si (expr
->value
.integer
, INT_MAX
) > 0)
674 || (mpz_cmp_si (expr
->value
.integer
, INT_MIN
) < 0))
676 if (report_error
> 0)
677 gfc_error ("Integer value too large in expression at %C");
678 else if (report_error
< 0)
679 gfc_error_now ("Integer value too large in expression at %C");
683 *result
= (int) mpz_get_si (expr
->value
.integer
);
689 /* Same as gfc_extract_int, but use a HWI. */
692 gfc_extract_hwi (gfc_expr
*expr
, HOST_WIDE_INT
*result
, int report_error
)
696 /* A KIND component is a parameter too. The expression for it is
697 stored in the initializer and should be consistent with the tests
699 if (gfc_expr_attr(expr
).pdt_kind
)
701 for (ref
= expr
->ref
; ref
; ref
= ref
->next
)
703 if (ref
->u
.c
.component
->attr
.pdt_kind
)
704 expr
= ref
->u
.c
.component
->initializer
;
708 if (expr
->expr_type
!= EXPR_CONSTANT
)
710 if (report_error
> 0)
711 gfc_error ("Constant expression required at %C");
712 else if (report_error
< 0)
713 gfc_error_now ("Constant expression required at %C");
717 if (expr
->ts
.type
!= BT_INTEGER
)
719 if (report_error
> 0)
720 gfc_error ("Integer expression required at %C");
721 else if (report_error
< 0)
722 gfc_error_now ("Integer expression required at %C");
726 /* Use long_long_integer_type_node to determine when to saturate. */
727 const wide_int val
= wi::from_mpz (long_long_integer_type_node
,
728 expr
->value
.integer
, false);
730 if (!wi::fits_shwi_p (val
))
732 if (report_error
> 0)
733 gfc_error ("Integer value too large in expression at %C");
734 else if (report_error
< 0)
735 gfc_error_now ("Integer value too large in expression at %C");
739 *result
= val
.to_shwi ();
745 /* Recursively copy a list of reference structures. */
748 gfc_copy_ref (gfc_ref
*src
)
756 dest
= gfc_get_ref ();
757 dest
->type
= src
->type
;
762 ar
= gfc_copy_array_ref (&src
->u
.ar
);
768 dest
->u
.c
= src
->u
.c
;
772 dest
->u
.i
= src
->u
.i
;
776 dest
->u
.ss
= src
->u
.ss
;
777 dest
->u
.ss
.start
= gfc_copy_expr (src
->u
.ss
.start
);
778 dest
->u
.ss
.end
= gfc_copy_expr (src
->u
.ss
.end
);
782 dest
->next
= gfc_copy_ref (src
->next
);
788 /* Detect whether an expression has any vector index array references. */
791 gfc_has_vector_index (gfc_expr
*e
)
795 for (ref
= e
->ref
; ref
; ref
= ref
->next
)
796 if (ref
->type
== REF_ARRAY
)
797 for (i
= 0; i
< ref
->u
.ar
.dimen
; i
++)
798 if (ref
->u
.ar
.dimen_type
[i
] == DIMEN_VECTOR
)
804 /* Copy a shape array. */
807 gfc_copy_shape (mpz_t
*shape
, int rank
)
815 new_shape
= gfc_get_shape (rank
);
817 for (n
= 0; n
< rank
; n
++)
818 mpz_init_set (new_shape
[n
], shape
[n
]);
824 /* Copy a shape array excluding dimension N, where N is an integer
825 constant expression. Dimensions are numbered in Fortran style --
828 So, if the original shape array contains R elements
829 { s1 ... sN-1 sN sN+1 ... sR-1 sR}
830 the result contains R-1 elements:
831 { s1 ... sN-1 sN+1 ... sR-1}
833 If anything goes wrong -- N is not a constant, its value is out
834 of range -- or anything else, just returns NULL. */
837 gfc_copy_shape_excluding (mpz_t
*shape
, int rank
, gfc_expr
*dim
)
839 mpz_t
*new_shape
, *s
;
845 || dim
->expr_type
!= EXPR_CONSTANT
846 || dim
->ts
.type
!= BT_INTEGER
)
849 n
= mpz_get_si (dim
->value
.integer
);
850 n
--; /* Convert to zero based index. */
851 if (n
< 0 || n
>= rank
)
854 s
= new_shape
= gfc_get_shape (rank
- 1);
856 for (i
= 0; i
< rank
; i
++)
860 mpz_init_set (*s
, shape
[i
]);
868 /* Return the maximum kind of two expressions. In general, higher
869 kind numbers mean more precision for numeric types. */
872 gfc_kind_max (gfc_expr
*e1
, gfc_expr
*e2
)
874 return (e1
->ts
.kind
> e2
->ts
.kind
) ? e1
->ts
.kind
: e2
->ts
.kind
;
878 /* Returns nonzero if the type is numeric, zero otherwise. */
881 numeric_type (bt type
)
883 return type
== BT_COMPLEX
|| type
== BT_REAL
|| type
== BT_INTEGER
;
887 /* Returns nonzero if the typespec is a numeric type, zero otherwise. */
890 gfc_numeric_ts (gfc_typespec
*ts
)
892 return numeric_type (ts
->type
);
896 /* Return an expression node with an optional argument list attached.
897 A variable number of gfc_expr pointers are strung together in an
898 argument list with a NULL pointer terminating the list. */
901 gfc_build_conversion (gfc_expr
*e
)
906 p
->expr_type
= EXPR_FUNCTION
;
908 p
->value
.function
.actual
= gfc_get_actual_arglist ();
909 p
->value
.function
.actual
->expr
= e
;
915 /* Given an expression node with some sort of numeric binary
916 expression, insert type conversions required to make the operands
917 have the same type. Conversion warnings are disabled if wconversion
920 The exception is that the operands of an exponential don't have to
921 have the same type. If possible, the base is promoted to the type
922 of the exponent. For example, 1**2.3 becomes 1.0**2.3, but
923 1.0**2 stays as it is. */
926 gfc_type_convert_binary (gfc_expr
*e
, int wconversion
)
930 op1
= e
->value
.op
.op1
;
931 op2
= e
->value
.op
.op2
;
933 if (op1
->ts
.type
== BT_UNKNOWN
|| op2
->ts
.type
== BT_UNKNOWN
)
935 gfc_clear_ts (&e
->ts
);
939 /* Kind conversions of same type. */
940 if (op1
->ts
.type
== op2
->ts
.type
)
942 if (op1
->ts
.kind
== op2
->ts
.kind
)
944 /* No type conversions. */
949 if (op1
->ts
.kind
> op2
->ts
.kind
)
950 gfc_convert_type_warn (op2
, &op1
->ts
, 2, wconversion
);
952 gfc_convert_type_warn (op1
, &op2
->ts
, 2, wconversion
);
958 /* Integer combined with real or complex. */
959 if (op2
->ts
.type
== BT_INTEGER
)
963 /* Special case for ** operator. */
964 if (e
->value
.op
.op
== INTRINSIC_POWER
)
967 gfc_convert_type_warn (e
->value
.op
.op2
, &e
->ts
, 2, wconversion
);
971 if (op1
->ts
.type
== BT_INTEGER
)
974 gfc_convert_type_warn (e
->value
.op
.op1
, &e
->ts
, 2, wconversion
);
978 /* Real combined with complex. */
979 e
->ts
.type
= BT_COMPLEX
;
980 if (op1
->ts
.kind
> op2
->ts
.kind
)
981 e
->ts
.kind
= op1
->ts
.kind
;
983 e
->ts
.kind
= op2
->ts
.kind
;
984 if (op1
->ts
.type
!= BT_COMPLEX
|| op1
->ts
.kind
!= e
->ts
.kind
)
985 gfc_convert_type_warn (e
->value
.op
.op1
, &e
->ts
, 2, wconversion
);
986 if (op2
->ts
.type
!= BT_COMPLEX
|| op2
->ts
.kind
!= e
->ts
.kind
)
987 gfc_convert_type_warn (e
->value
.op
.op2
, &e
->ts
, 2, wconversion
);
994 /* Standard intrinsics listed under F2018:10.1.12 (6), which are excluded in
995 constant expressions, except TRANSFER (c.f. item (8)), which would need
996 separate treatment. */
999 is_non_constant_intrinsic (gfc_expr
*e
)
1001 if (e
->expr_type
== EXPR_FUNCTION
1002 && e
->value
.function
.isym
)
1004 switch (e
->value
.function
.isym
->id
)
1006 case GFC_ISYM_COMMAND_ARGUMENT_COUNT
:
1007 case GFC_ISYM_GET_TEAM
:
1009 case GFC_ISYM_NUM_IMAGES
:
1010 case GFC_ISYM_TEAM_NUMBER
:
1011 case GFC_ISYM_THIS_IMAGE
:
1022 /* Determine if an expression is constant in the sense of F08:7.1.12.
1023 * This function expects that the expression has already been simplified. */
1026 gfc_is_constant_expr (gfc_expr
*e
)
1029 gfc_actual_arglist
*arg
;
1034 switch (e
->expr_type
)
1037 return (gfc_is_constant_expr (e
->value
.op
.op1
)
1038 && (e
->value
.op
.op2
== NULL
1039 || gfc_is_constant_expr (e
->value
.op
.op2
)));
1042 /* The only context in which this can occur is in a parameterized
1043 derived type declaration, so returning true is OK. */
1044 if (e
->symtree
->n
.sym
->attr
.pdt_len
1045 || e
->symtree
->n
.sym
->attr
.pdt_kind
)
1052 gcc_assert (e
->symtree
|| e
->value
.function
.esym
1053 || e
->value
.function
.isym
);
1055 /* Check for intrinsics excluded in constant expressions. */
1056 if (e
->value
.function
.isym
&& is_non_constant_intrinsic (e
))
1059 /* Call to intrinsic with at least one argument. */
1060 if (e
->value
.function
.isym
&& e
->value
.function
.actual
)
1062 for (arg
= e
->value
.function
.actual
; arg
; arg
= arg
->next
)
1063 if (!gfc_is_constant_expr (arg
->expr
))
1067 if (e
->value
.function
.isym
1068 && (e
->value
.function
.isym
->elemental
1069 || e
->value
.function
.isym
->pure
1070 || e
->value
.function
.isym
->inquiry
1071 || e
->value
.function
.isym
->transformational
))
1080 case EXPR_SUBSTRING
:
1081 return e
->ref
== NULL
|| (gfc_is_constant_expr (e
->ref
->u
.ss
.start
)
1082 && gfc_is_constant_expr (e
->ref
->u
.ss
.end
));
1085 case EXPR_STRUCTURE
:
1086 c
= gfc_constructor_first (e
->value
.constructor
);
1087 if ((e
->expr_type
== EXPR_ARRAY
) && c
&& c
->iterator
)
1088 return gfc_constant_ac (e
);
1090 for (; c
; c
= gfc_constructor_next (c
))
1091 if (!gfc_is_constant_expr (c
->expr
))
1098 gfc_internal_error ("gfc_is_constant_expr(): Unknown expression type");
1104 /* Is true if the expression or symbol is a passed CFI descriptor. */
1106 is_CFI_desc (gfc_symbol
*sym
, gfc_expr
*e
)
1109 && e
&& e
->expr_type
== EXPR_VARIABLE
)
1110 sym
= e
->symtree
->n
.sym
;
1112 if (sym
&& sym
->attr
.dummy
1113 && sym
->ns
->proc_name
->attr
.is_bind_c
1114 && (sym
->attr
.pointer
1115 || sym
->attr
.allocatable
1116 || (sym
->attr
.dimension
1117 && (sym
->as
->type
== AS_ASSUMED_SHAPE
1118 || sym
->as
->type
== AS_ASSUMED_RANK
))
1119 || (sym
->ts
.type
== BT_CHARACTER
1120 && (!sym
->ts
.u
.cl
|| !sym
->ts
.u
.cl
->length
))))
1127 /* Is true if an array reference is followed by a component or substring
1130 is_subref_array (gfc_expr
* e
)
1136 if (e
->expr_type
!= EXPR_VARIABLE
)
1139 sym
= e
->symtree
->n
.sym
;
1141 if (sym
->attr
.subref_array_pointer
)
1146 for (ref
= e
->ref
; ref
; ref
= ref
->next
)
1148 /* If we haven't seen the array reference and this is an intrinsic,
1149 what follows cannot be a subreference array, unless there is a
1150 substring reference. */
1151 if (!seen_array
&& ref
->type
== REF_COMPONENT
1152 && ref
->u
.c
.component
->ts
.type
!= BT_CHARACTER
1153 && ref
->u
.c
.component
->ts
.type
!= BT_CLASS
1154 && !gfc_bt_struct (ref
->u
.c
.component
->ts
.type
))
1157 if (ref
->type
== REF_ARRAY
1158 && ref
->u
.ar
.type
!= AR_ELEMENT
)
1162 && ref
->type
!= REF_ARRAY
)
1166 if (sym
->ts
.type
== BT_CLASS
1168 && CLASS_DATA (sym
)->attr
.dimension
1169 && CLASS_DATA (sym
)->attr
.class_pointer
)
1176 /* Try to collapse intrinsic expressions. */
1179 simplify_intrinsic_op (gfc_expr
*p
, int type
)
1181 gfc_intrinsic_op op
;
1182 gfc_expr
*op1
, *op2
, *result
;
1184 if (p
->value
.op
.op
== INTRINSIC_USER
)
1187 op1
= p
->value
.op
.op1
;
1188 op2
= p
->value
.op
.op2
;
1189 op
= p
->value
.op
.op
;
1191 if (!gfc_simplify_expr (op1
, type
))
1193 if (!gfc_simplify_expr (op2
, type
))
1196 if (!gfc_is_constant_expr (op1
)
1197 || (op2
!= NULL
&& !gfc_is_constant_expr (op2
)))
1201 p
->value
.op
.op1
= NULL
;
1202 p
->value
.op
.op2
= NULL
;
1206 case INTRINSIC_PARENTHESES
:
1207 result
= gfc_parentheses (op1
);
1210 case INTRINSIC_UPLUS
:
1211 result
= gfc_uplus (op1
);
1214 case INTRINSIC_UMINUS
:
1215 result
= gfc_uminus (op1
);
1218 case INTRINSIC_PLUS
:
1219 result
= gfc_add (op1
, op2
);
1222 case INTRINSIC_MINUS
:
1223 result
= gfc_subtract (op1
, op2
);
1226 case INTRINSIC_TIMES
:
1227 result
= gfc_multiply (op1
, op2
);
1230 case INTRINSIC_DIVIDE
:
1231 result
= gfc_divide (op1
, op2
);
1234 case INTRINSIC_POWER
:
1235 result
= gfc_power (op1
, op2
);
1238 case INTRINSIC_CONCAT
:
1239 result
= gfc_concat (op1
, op2
);
1243 case INTRINSIC_EQ_OS
:
1244 result
= gfc_eq (op1
, op2
, op
);
1248 case INTRINSIC_NE_OS
:
1249 result
= gfc_ne (op1
, op2
, op
);
1253 case INTRINSIC_GT_OS
:
1254 result
= gfc_gt (op1
, op2
, op
);
1258 case INTRINSIC_GE_OS
:
1259 result
= gfc_ge (op1
, op2
, op
);
1263 case INTRINSIC_LT_OS
:
1264 result
= gfc_lt (op1
, op2
, op
);
1268 case INTRINSIC_LE_OS
:
1269 result
= gfc_le (op1
, op2
, op
);
1273 result
= gfc_not (op1
);
1277 result
= gfc_and (op1
, op2
);
1281 result
= gfc_or (op1
, op2
);
1285 result
= gfc_eqv (op1
, op2
);
1288 case INTRINSIC_NEQV
:
1289 result
= gfc_neqv (op1
, op2
);
1293 gfc_internal_error ("simplify_intrinsic_op(): Bad operator");
1298 gfc_free_expr (op1
);
1299 gfc_free_expr (op2
);
1303 result
->rank
= p
->rank
;
1304 result
->where
= p
->where
;
1305 gfc_replace_expr (p
, result
);
1311 /* Subroutine to simplify constructor expressions. Mutually recursive
1312 with gfc_simplify_expr(). */
1315 simplify_constructor (gfc_constructor_base base
, int type
)
1320 for (c
= gfc_constructor_first (base
); c
; c
= gfc_constructor_next (c
))
1323 && (!gfc_simplify_expr(c
->iterator
->start
, type
)
1324 || !gfc_simplify_expr (c
->iterator
->end
, type
)
1325 || !gfc_simplify_expr (c
->iterator
->step
, type
)))
1330 /* Try and simplify a copy. Replace the original if successful
1331 but keep going through the constructor at all costs. Not
1332 doing so can make a dog's dinner of complicated things. */
1333 p
= gfc_copy_expr (c
->expr
);
1335 if (!gfc_simplify_expr (p
, type
))
1341 gfc_replace_expr (c
->expr
, p
);
1349 /* Pull a single array element out of an array constructor. */
1352 find_array_element (gfc_constructor_base base
, gfc_array_ref
*ar
,
1353 gfc_constructor
**rval
)
1355 unsigned long nelemen
;
1361 gfc_constructor
*cons
;
1368 mpz_init_set_ui (offset
, 0);
1371 mpz_init_set_ui (span
, 1);
1372 for (i
= 0; i
< ar
->dimen
; i
++)
1374 if (!gfc_reduce_init_expr (ar
->as
->lower
[i
])
1375 || !gfc_reduce_init_expr (ar
->as
->upper
[i
])
1376 || ar
->as
->upper
[i
]->expr_type
!= EXPR_CONSTANT
1377 || ar
->as
->lower
[i
]->expr_type
!= EXPR_CONSTANT
)
1385 if (e
->expr_type
!= EXPR_CONSTANT
)
1391 /* Check the bounds. */
1392 if ((ar
->as
->upper
[i
]
1393 && mpz_cmp (e
->value
.integer
,
1394 ar
->as
->upper
[i
]->value
.integer
) > 0)
1395 || (mpz_cmp (e
->value
.integer
,
1396 ar
->as
->lower
[i
]->value
.integer
) < 0))
1398 gfc_error ("Index in dimension %d is out of bounds "
1399 "at %L", i
+ 1, &ar
->c_where
[i
]);
1405 mpz_sub (delta
, e
->value
.integer
, ar
->as
->lower
[i
]->value
.integer
);
1406 mpz_mul (delta
, delta
, span
);
1407 mpz_add (offset
, offset
, delta
);
1409 mpz_set_ui (tmp
, 1);
1410 mpz_add (tmp
, tmp
, ar
->as
->upper
[i
]->value
.integer
);
1411 mpz_sub (tmp
, tmp
, ar
->as
->lower
[i
]->value
.integer
);
1412 mpz_mul (span
, span
, tmp
);
1415 for (cons
= gfc_constructor_first (base
), nelemen
= mpz_get_ui (offset
);
1416 cons
&& nelemen
> 0; cons
= gfc_constructor_next (cons
), nelemen
--)
1435 /* Find a component of a structure constructor. */
1437 static gfc_constructor
*
1438 find_component_ref (gfc_constructor_base base
, gfc_ref
*ref
)
1440 gfc_component
*pick
= ref
->u
.c
.component
;
1441 gfc_constructor
*c
= gfc_constructor_first (base
);
1443 gfc_symbol
*dt
= ref
->u
.c
.sym
;
1444 int ext
= dt
->attr
.extension
;
1446 /* For extended types, check if the desired component is in one of the
1448 while (ext
> 0 && gfc_find_component (dt
->components
->ts
.u
.derived
,
1449 pick
->name
, true, true, NULL
))
1451 dt
= dt
->components
->ts
.u
.derived
;
1452 c
= gfc_constructor_first (c
->expr
->value
.constructor
);
1456 gfc_component
*comp
= dt
->components
;
1457 while (comp
!= pick
)
1460 c
= gfc_constructor_next (c
);
1467 /* Replace an expression with the contents of a constructor, removing
1468 the subobject reference in the process. */
1471 remove_subobject_ref (gfc_expr
*p
, gfc_constructor
*cons
)
1481 e
= gfc_copy_expr (p
);
1482 e
->ref
= p
->ref
->next
;
1483 p
->ref
->next
= NULL
;
1484 gfc_replace_expr (p
, e
);
1488 /* Pull an array section out of an array constructor. */
1491 find_array_section (gfc_expr
*expr
, gfc_ref
*ref
)
1498 long unsigned one
= 1;
1500 mpz_t start
[GFC_MAX_DIMENSIONS
];
1501 mpz_t end
[GFC_MAX_DIMENSIONS
];
1502 mpz_t stride
[GFC_MAX_DIMENSIONS
];
1503 mpz_t delta
[GFC_MAX_DIMENSIONS
];
1504 mpz_t ctr
[GFC_MAX_DIMENSIONS
];
1509 gfc_constructor_base base
;
1510 gfc_constructor
*cons
, *vecsub
[GFC_MAX_DIMENSIONS
];
1520 base
= expr
->value
.constructor
;
1521 expr
->value
.constructor
= NULL
;
1523 rank
= ref
->u
.ar
.as
->rank
;
1525 if (expr
->shape
== NULL
)
1526 expr
->shape
= gfc_get_shape (rank
);
1528 mpz_init_set_ui (delta_mpz
, one
);
1529 mpz_init_set_ui (nelts
, one
);
1532 /* Do the initialization now, so that we can cleanup without
1533 keeping track of where we were. */
1534 for (d
= 0; d
< rank
; d
++)
1536 mpz_init (delta
[d
]);
1537 mpz_init (start
[d
]);
1540 mpz_init (stride
[d
]);
1544 /* Build the counters to clock through the array reference. */
1546 for (d
= 0; d
< rank
; d
++)
1548 /* Make this stretch of code easier on the eye! */
1549 begin
= ref
->u
.ar
.start
[d
];
1550 finish
= ref
->u
.ar
.end
[d
];
1551 step
= ref
->u
.ar
.stride
[d
];
1552 lower
= ref
->u
.ar
.as
->lower
[d
];
1553 upper
= ref
->u
.ar
.as
->upper
[d
];
1555 if (ref
->u
.ar
.dimen_type
[d
] == DIMEN_VECTOR
) /* Vector subscript. */
1557 gfc_constructor
*ci
;
1560 if (begin
->expr_type
!= EXPR_ARRAY
|| !gfc_is_constant_expr (begin
))
1566 gcc_assert (begin
->rank
== 1);
1567 /* Zero-sized arrays have no shape and no elements, stop early. */
1570 mpz_init_set_ui (nelts
, 0);
1574 vecsub
[d
] = gfc_constructor_first (begin
->value
.constructor
);
1575 mpz_set (ctr
[d
], vecsub
[d
]->expr
->value
.integer
);
1576 mpz_mul (nelts
, nelts
, begin
->shape
[0]);
1577 mpz_set (expr
->shape
[shape_i
++], begin
->shape
[0]);
1580 for (ci
= vecsub
[d
]; ci
; ci
= gfc_constructor_next (ci
))
1582 if (mpz_cmp (ci
->expr
->value
.integer
, upper
->value
.integer
) > 0
1583 || mpz_cmp (ci
->expr
->value
.integer
,
1584 lower
->value
.integer
) < 0)
1586 gfc_error ("index in dimension %d is out of bounds "
1587 "at %L", d
+ 1, &ref
->u
.ar
.c_where
[d
]);
1595 if ((begin
&& begin
->expr_type
!= EXPR_CONSTANT
)
1596 || (finish
&& finish
->expr_type
!= EXPR_CONSTANT
)
1597 || (step
&& step
->expr_type
!= EXPR_CONSTANT
)
1605 /* Obtain the stride. */
1607 mpz_set (stride
[d
], step
->value
.integer
);
1609 mpz_set_ui (stride
[d
], one
);
1611 if (mpz_cmp_ui (stride
[d
], 0) == 0)
1612 mpz_set_ui (stride
[d
], one
);
1614 /* Obtain the start value for the index. */
1616 mpz_set (start
[d
], begin
->value
.integer
);
1618 mpz_set (start
[d
], lower
->value
.integer
);
1620 mpz_set (ctr
[d
], start
[d
]);
1622 /* Obtain the end value for the index. */
1624 mpz_set (end
[d
], finish
->value
.integer
);
1626 mpz_set (end
[d
], upper
->value
.integer
);
1628 /* Separate 'if' because elements sometimes arrive with
1630 if (ref
->u
.ar
.dimen_type
[d
] == DIMEN_ELEMENT
)
1631 mpz_set (end
[d
], begin
->value
.integer
);
1633 /* Check the bounds. */
1634 if (mpz_cmp (ctr
[d
], upper
->value
.integer
) > 0
1635 || mpz_cmp (end
[d
], upper
->value
.integer
) > 0
1636 || mpz_cmp (ctr
[d
], lower
->value
.integer
) < 0
1637 || mpz_cmp (end
[d
], lower
->value
.integer
) < 0)
1639 gfc_error ("index in dimension %d is out of bounds "
1640 "at %L", d
+ 1, &ref
->u
.ar
.c_where
[d
]);
1645 /* Calculate the number of elements and the shape. */
1646 mpz_set (tmp_mpz
, stride
[d
]);
1647 mpz_add (tmp_mpz
, end
[d
], tmp_mpz
);
1648 mpz_sub (tmp_mpz
, tmp_mpz
, ctr
[d
]);
1649 mpz_div (tmp_mpz
, tmp_mpz
, stride
[d
]);
1650 mpz_mul (nelts
, nelts
, tmp_mpz
);
1652 /* An element reference reduces the rank of the expression; don't
1653 add anything to the shape array. */
1654 if (ref
->u
.ar
.dimen_type
[d
] != DIMEN_ELEMENT
)
1655 mpz_set (expr
->shape
[shape_i
++], tmp_mpz
);
1658 /* Calculate the 'stride' (=delta) for conversion of the
1659 counter values into the index along the constructor. */
1660 mpz_set (delta
[d
], delta_mpz
);
1661 mpz_sub (tmp_mpz
, upper
->value
.integer
, lower
->value
.integer
);
1662 mpz_add_ui (tmp_mpz
, tmp_mpz
, one
);
1663 mpz_mul (delta_mpz
, delta_mpz
, tmp_mpz
);
1667 cons
= gfc_constructor_first (base
);
1669 /* Now clock through the array reference, calculating the index in
1670 the source constructor and transferring the elements to the new
1672 for (idx
= 0; idx
< (int) mpz_get_si (nelts
); idx
++)
1674 mpz_init_set_ui (ptr
, 0);
1677 for (d
= 0; d
< rank
; d
++)
1679 mpz_set (tmp_mpz
, ctr
[d
]);
1680 mpz_sub (tmp_mpz
, tmp_mpz
, ref
->u
.ar
.as
->lower
[d
]->value
.integer
);
1681 mpz_mul (tmp_mpz
, tmp_mpz
, delta
[d
]);
1682 mpz_add (ptr
, ptr
, tmp_mpz
);
1684 if (!incr_ctr
) continue;
1686 if (ref
->u
.ar
.dimen_type
[d
] == DIMEN_VECTOR
) /* Vector subscript. */
1688 gcc_assert(vecsub
[d
]);
1690 if (!gfc_constructor_next (vecsub
[d
]))
1691 vecsub
[d
] = gfc_constructor_first (ref
->u
.ar
.start
[d
]->value
.constructor
);
1694 vecsub
[d
] = gfc_constructor_next (vecsub
[d
]);
1697 mpz_set (ctr
[d
], vecsub
[d
]->expr
->value
.integer
);
1701 mpz_add (ctr
[d
], ctr
[d
], stride
[d
]);
1703 if (mpz_cmp_ui (stride
[d
], 0) > 0
1704 ? mpz_cmp (ctr
[d
], end
[d
]) > 0
1705 : mpz_cmp (ctr
[d
], end
[d
]) < 0)
1706 mpz_set (ctr
[d
], start
[d
]);
1712 limit
= mpz_get_ui (ptr
);
1713 if (limit
>= flag_max_array_constructor
)
1715 gfc_error ("The number of elements in the array constructor "
1716 "at %L requires an increase of the allowed %d "
1717 "upper limit. See %<-fmax-array-constructor%> "
1718 "option", &expr
->where
, flag_max_array_constructor
);
1722 cons
= gfc_constructor_lookup (base
, limit
);
1725 gfc_error ("Error in array constructor referenced at %L",
1730 gfc_constructor_append_expr (&expr
->value
.constructor
,
1731 gfc_copy_expr (cons
->expr
), NULL
);
1738 mpz_clear (delta_mpz
);
1739 mpz_clear (tmp_mpz
);
1741 for (d
= 0; d
< rank
; d
++)
1743 mpz_clear (delta
[d
]);
1744 mpz_clear (start
[d
]);
1747 mpz_clear (stride
[d
]);
1749 gfc_constructor_free (base
);
1753 /* Pull a substring out of an expression. */
1756 find_substring_ref (gfc_expr
*p
, gfc_expr
**newp
)
1759 gfc_charlen_t start
;
1760 gfc_charlen_t length
;
1763 if (p
->ref
->u
.ss
.start
->expr_type
!= EXPR_CONSTANT
1764 || p
->ref
->u
.ss
.end
->expr_type
!= EXPR_CONSTANT
)
1767 *newp
= gfc_copy_expr (p
);
1768 free ((*newp
)->value
.character
.string
);
1770 end
= (gfc_charlen_t
) mpz_get_si (p
->ref
->u
.ss
.end
->value
.integer
);
1771 start
= (gfc_charlen_t
) mpz_get_si (p
->ref
->u
.ss
.start
->value
.integer
);
1773 length
= end
- start
+ 1;
1777 chr
= (*newp
)->value
.character
.string
= gfc_get_wide_string (length
+ 1);
1778 (*newp
)->value
.character
.length
= length
;
1779 memcpy (chr
, &p
->value
.character
.string
[start
- 1],
1780 length
* sizeof (gfc_char_t
));
1786 /* Pull an inquiry result out of an expression. */
1789 find_inquiry_ref (gfc_expr
*p
, gfc_expr
**newp
)
1792 gfc_ref
*inquiry
= NULL
;
1795 tmp
= gfc_copy_expr (p
);
1797 if (tmp
->ref
&& tmp
->ref
->type
== REF_INQUIRY
)
1804 for (ref
= tmp
->ref
; ref
; ref
= ref
->next
)
1805 if (ref
->next
&& ref
->next
->type
== REF_INQUIRY
)
1807 inquiry
= ref
->next
;
1814 gfc_free_expr (tmp
);
1818 gfc_resolve_expr (tmp
);
1820 /* In principle there can be more than one inquiry reference. */
1821 for (; inquiry
; inquiry
= inquiry
->next
)
1823 switch (inquiry
->u
.i
)
1826 if (tmp
->ts
.type
!= BT_CHARACTER
)
1829 if (!gfc_notify_std (GFC_STD_F2003
, "LEN part_ref at %C"))
1832 if (tmp
->ts
.u
.cl
->length
1833 && tmp
->ts
.u
.cl
->length
->expr_type
== EXPR_CONSTANT
)
1834 *newp
= gfc_copy_expr (tmp
->ts
.u
.cl
->length
);
1835 else if (tmp
->expr_type
== EXPR_CONSTANT
)
1836 *newp
= gfc_get_int_expr (gfc_default_integer_kind
,
1837 NULL
, tmp
->value
.character
.length
);
1844 if (tmp
->ts
.type
== BT_DERIVED
|| tmp
->ts
.type
== BT_CLASS
)
1847 if (!gfc_notify_std (GFC_STD_F2003
, "KIND part_ref at %C"))
1850 *newp
= gfc_get_int_expr (gfc_default_integer_kind
,
1851 NULL
, tmp
->ts
.kind
);
1855 if (tmp
->ts
.type
!= BT_COMPLEX
|| tmp
->expr_type
!= EXPR_CONSTANT
)
1858 if (!gfc_notify_std (GFC_STD_F2008
, "RE part_ref at %C"))
1861 *newp
= gfc_get_constant_expr (BT_REAL
, tmp
->ts
.kind
, &tmp
->where
);
1862 mpfr_set ((*newp
)->value
.real
,
1863 mpc_realref (tmp
->value
.complex), GFC_RND_MODE
);
1867 if (tmp
->ts
.type
!= BT_COMPLEX
|| tmp
->expr_type
!= EXPR_CONSTANT
)
1870 if (!gfc_notify_std (GFC_STD_F2008
, "IM part_ref at %C"))
1873 *newp
= gfc_get_constant_expr (BT_REAL
, tmp
->ts
.kind
, &tmp
->where
);
1874 mpfr_set ((*newp
)->value
.real
,
1875 mpc_imagref (tmp
->value
.complex), GFC_RND_MODE
);
1878 tmp
= gfc_copy_expr (*newp
);
1883 else if ((*newp
)->expr_type
!= EXPR_CONSTANT
)
1885 gfc_free_expr (*newp
);
1889 gfc_free_expr (tmp
);
1893 gfc_free_expr (tmp
);
1899 /* Simplify a subobject reference of a constructor. This occurs when
1900 parameter variable values are substituted. */
1903 simplify_const_ref (gfc_expr
*p
)
1905 gfc_constructor
*cons
, *c
;
1906 gfc_expr
*newp
= NULL
;
1911 switch (p
->ref
->type
)
1914 switch (p
->ref
->u
.ar
.type
)
1917 /* <type/kind spec>, parameter :: x(<int>) = scalar_expr
1918 will generate this. */
1919 if (p
->expr_type
!= EXPR_ARRAY
)
1921 remove_subobject_ref (p
, NULL
);
1924 if (!find_array_element (p
->value
.constructor
, &p
->ref
->u
.ar
, &cons
))
1930 remove_subobject_ref (p
, cons
);
1934 if (!find_array_section (p
, p
->ref
))
1936 p
->ref
->u
.ar
.type
= AR_FULL
;
1941 if (p
->ref
->next
!= NULL
1942 && (p
->ts
.type
== BT_CHARACTER
|| gfc_bt_struct (p
->ts
.type
)))
1944 for (c
= gfc_constructor_first (p
->value
.constructor
);
1945 c
; c
= gfc_constructor_next (c
))
1947 c
->expr
->ref
= gfc_copy_ref (p
->ref
->next
);
1948 if (!simplify_const_ref (c
->expr
))
1952 if (gfc_bt_struct (p
->ts
.type
)
1954 && (c
= gfc_constructor_first (p
->value
.constructor
)))
1956 /* There may have been component references. */
1957 p
->ts
= c
->expr
->ts
;
1961 for (; last_ref
->next
; last_ref
= last_ref
->next
) {};
1963 if (p
->ts
.type
== BT_CHARACTER
1964 && last_ref
->type
== REF_SUBSTRING
)
1966 /* If this is a CHARACTER array and we possibly took
1967 a substring out of it, update the type-spec's
1968 character length according to the first element
1969 (as all should have the same length). */
1970 gfc_charlen_t string_len
;
1971 if ((c
= gfc_constructor_first (p
->value
.constructor
)))
1973 const gfc_expr
* first
= c
->expr
;
1974 gcc_assert (first
->expr_type
== EXPR_CONSTANT
);
1975 gcc_assert (first
->ts
.type
== BT_CHARACTER
);
1976 string_len
= first
->value
.character
.length
;
1984 p
->ts
.u
.cl
= gfc_new_charlen (p
->symtree
->n
.sym
->ns
,
1987 p
->ts
.u
.cl
= gfc_new_charlen (gfc_current_ns
,
1991 gfc_free_expr (p
->ts
.u
.cl
->length
);
1994 = gfc_get_int_expr (gfc_charlen_int_kind
,
1998 gfc_free_ref_list (p
->ref
);
2009 cons
= find_component_ref (p
->value
.constructor
, p
->ref
);
2010 remove_subobject_ref (p
, cons
);
2014 if (!find_inquiry_ref (p
, &newp
))
2017 gfc_replace_expr (p
, newp
);
2018 gfc_free_ref_list (p
->ref
);
2023 if (!find_substring_ref (p
, &newp
))
2026 gfc_replace_expr (p
, newp
);
2027 gfc_free_ref_list (p
->ref
);
2037 /* Simplify a chain of references. */
2040 simplify_ref_chain (gfc_ref
*ref
, int type
, gfc_expr
**p
)
2045 for (; ref
; ref
= ref
->next
)
2050 for (n
= 0; n
< ref
->u
.ar
.dimen
; n
++)
2052 if (!gfc_simplify_expr (ref
->u
.ar
.start
[n
], type
))
2054 if (!gfc_simplify_expr (ref
->u
.ar
.end
[n
], type
))
2056 if (!gfc_simplify_expr (ref
->u
.ar
.stride
[n
], type
))
2062 if (!gfc_simplify_expr (ref
->u
.ss
.start
, type
))
2064 if (!gfc_simplify_expr (ref
->u
.ss
.end
, type
))
2069 if (!find_inquiry_ref (*p
, &newp
))
2072 gfc_replace_expr (*p
, newp
);
2073 gfc_free_ref_list ((*p
)->ref
);
2085 /* Try to substitute the value of a parameter variable. */
2088 simplify_parameter_variable (gfc_expr
*p
, int type
)
2093 /* Set rank and check array ref; as resolve_variable calls
2094 gfc_simplify_expr, call gfc_resolve_ref + gfc_expression_rank instead. */
2095 if (!gfc_resolve_ref (p
))
2100 gfc_expression_rank (p
);
2102 /* Is this an inquiry? */
2103 bool inquiry
= false;
2104 gfc_ref
* ref
= p
->ref
;
2107 if (ref
->type
== REF_INQUIRY
)
2111 if (ref
&& ref
->type
== REF_INQUIRY
)
2112 inquiry
= ref
->u
.i
== INQUIRY_LEN
|| ref
->u
.i
== INQUIRY_KIND
;
2114 if (gfc_is_size_zero_array (p
))
2116 if (p
->expr_type
== EXPR_ARRAY
)
2119 e
= gfc_get_expr ();
2120 e
->expr_type
= EXPR_ARRAY
;
2123 e
->value
.constructor
= NULL
;
2124 e
->shape
= gfc_copy_shape (p
->shape
, p
->rank
);
2125 e
->where
= p
->where
;
2126 /* If %kind and %len are not used then we're done, otherwise
2127 drop through for simplification. */
2130 gfc_replace_expr (p
, e
);
2136 e
= gfc_copy_expr (p
->symtree
->n
.sym
->value
);
2140 gfc_free_shape (&e
->shape
, e
->rank
);
2141 e
->shape
= gfc_copy_shape (p
->shape
, p
->rank
);
2144 if (e
->ts
.type
== BT_CHARACTER
&& p
->ts
.u
.cl
)
2148 if (e
->ts
.type
== BT_CHARACTER
&& e
->ts
.u
.cl
== NULL
)
2149 e
->ts
.u
.cl
= gfc_new_charlen (gfc_current_ns
, p
->ts
.u
.cl
);
2151 /* Do not copy subobject refs for constant. */
2152 if (e
->expr_type
!= EXPR_CONSTANT
&& p
->ref
!= NULL
)
2153 e
->ref
= gfc_copy_ref (p
->ref
);
2154 t
= gfc_simplify_expr (e
, type
);
2155 e
->where
= p
->where
;
2157 /* Only use the simplification if it eliminated all subobject references. */
2159 gfc_replace_expr (p
, e
);
2168 scalarize_intrinsic_call (gfc_expr
*, bool init_flag
);
2170 /* Given an expression, simplify it by collapsing constant
2171 expressions. Most simplification takes place when the expression
2172 tree is being constructed. If an intrinsic function is simplified
2173 at some point, we get called again to collapse the result against
2176 We work by recursively simplifying expression nodes, simplifying
2177 intrinsic functions where possible, which can lead to further
2178 constant collapsing. If an operator has constant operand(s), we
2179 rip the expression apart, and rebuild it, hoping that it becomes
2182 The expression type is defined for:
2183 0 Basic expression parsing
2184 1 Simplifying array constructors -- will substitute
2186 Returns false on error, true otherwise.
2187 NOTE: Will return true even if the expression cannot be simplified. */
2190 gfc_simplify_expr (gfc_expr
*p
, int type
)
2192 gfc_actual_arglist
*ap
;
2193 gfc_intrinsic_sym
* isym
= NULL
;
2199 switch (p
->expr_type
)
2202 if (p
->ref
&& p
->ref
->type
== REF_INQUIRY
)
2203 simplify_ref_chain (p
->ref
, type
, &p
);
2209 // For array-bound functions, we don't need to optimize
2210 // the 'array' argument. In particular, if the argument
2211 // is a PARAMETER, simplifying might convert an EXPR_VARIABLE
2212 // into an EXPR_ARRAY; the latter has lbound = 1, the former
2213 // can have any lbound.
2214 ap
= p
->value
.function
.actual
;
2215 if (p
->value
.function
.isym
&&
2216 (p
->value
.function
.isym
->id
== GFC_ISYM_LBOUND
2217 || p
->value
.function
.isym
->id
== GFC_ISYM_UBOUND
2218 || p
->value
.function
.isym
->id
== GFC_ISYM_LCOBOUND
2219 || p
->value
.function
.isym
->id
== GFC_ISYM_UCOBOUND
2220 || p
->value
.function
.isym
->id
== GFC_ISYM_SHAPE
))
2223 for ( ; ap
; ap
= ap
->next
)
2224 if (!gfc_simplify_expr (ap
->expr
, type
))
2227 if (p
->value
.function
.isym
!= NULL
2228 && gfc_intrinsic_func_interface (p
, 1) == MATCH_ERROR
)
2231 if (p
->symtree
&& (p
->value
.function
.isym
|| p
->ts
.type
== BT_UNKNOWN
))
2233 isym
= gfc_find_function (p
->symtree
->n
.sym
->name
);
2234 if (isym
&& isym
->elemental
)
2235 scalarize_intrinsic_call (p
, false);
2240 case EXPR_SUBSTRING
:
2241 if (!simplify_ref_chain (p
->ref
, type
, &p
))
2244 if (gfc_is_constant_expr (p
))
2247 HOST_WIDE_INT start
, end
;
2250 if (p
->ref
&& p
->ref
->u
.ss
.start
)
2252 gfc_extract_hwi (p
->ref
->u
.ss
.start
, &start
);
2253 start
--; /* Convert from one-based to zero-based. */
2256 end
= p
->value
.character
.length
;
2257 if (p
->ref
&& p
->ref
->u
.ss
.end
)
2258 gfc_extract_hwi (p
->ref
->u
.ss
.end
, &end
);
2263 s
= gfc_get_wide_string (end
- start
+ 2);
2264 memcpy (s
, p
->value
.character
.string
+ start
,
2265 (end
- start
) * sizeof (gfc_char_t
));
2266 s
[end
- start
+ 1] = '\0'; /* TODO: C-style string. */
2267 free (p
->value
.character
.string
);
2268 p
->value
.character
.string
= s
;
2269 p
->value
.character
.length
= end
- start
;
2270 p
->ts
.u
.cl
= gfc_new_charlen (gfc_current_ns
, NULL
);
2271 p
->ts
.u
.cl
->length
= gfc_get_int_expr (gfc_charlen_int_kind
,
2273 p
->value
.character
.length
);
2274 gfc_free_ref_list (p
->ref
);
2276 p
->expr_type
= EXPR_CONSTANT
;
2281 if (!simplify_intrinsic_op (p
, type
))
2286 /* Only substitute array parameter variables if we are in an
2287 initialization expression, or we want a subsection. */
2288 if (p
->symtree
->n
.sym
->attr
.flavor
== FL_PARAMETER
2289 && (gfc_init_expr_flag
|| p
->ref
2290 || p
->symtree
->n
.sym
->value
->expr_type
!= EXPR_ARRAY
))
2292 if (!simplify_parameter_variable (p
, type
))
2299 gfc_simplify_iterator_var (p
);
2302 /* Simplify subcomponent references. */
2303 if (!simplify_ref_chain (p
->ref
, type
, &p
))
2308 case EXPR_STRUCTURE
:
2310 if (!simplify_ref_chain (p
->ref
, type
, &p
))
2313 /* If the following conditions hold, we found something like kind type
2314 inquiry of the form a(2)%kind while simplify the ref chain. */
2315 if (p
->expr_type
== EXPR_CONSTANT
&& !p
->ref
&& !p
->rank
&& !p
->shape
)
2318 if (!simplify_constructor (p
->value
.constructor
, type
))
2321 if (p
->expr_type
== EXPR_ARRAY
&& p
->ref
&& p
->ref
->type
== REF_ARRAY
2322 && p
->ref
->u
.ar
.type
== AR_FULL
)
2323 gfc_expand_constructor (p
, false);
2325 if (!simplify_const_ref (p
))
2342 /* Try simplification of an expression via gfc_simplify_expr.
2343 When an error occurs (arithmetic or otherwise), roll back. */
2346 gfc_try_simplify_expr (gfc_expr
*e
, int type
)
2351 if (e
== NULL
|| e
->expr_type
== EXPR_CONSTANT
)
2354 saved_div0
= gfc_seen_div0
;
2355 gfc_seen_div0
= false;
2356 n
= gfc_copy_expr (e
);
2357 t
= gfc_simplify_expr (n
, type
) && !gfc_seen_div0
;
2359 gfc_replace_expr (e
, n
);
2362 gfc_seen_div0
= saved_div0
;
2367 /* Returns the type of an expression with the exception that iterator
2368 variables are automatically integers no matter what else they may
2374 if (e
->expr_type
== EXPR_VARIABLE
&& gfc_check_iter_variable (e
))
2381 /* Scalarize an expression for an elemental intrinsic call. */
2384 scalarize_intrinsic_call (gfc_expr
*e
, bool init_flag
)
2386 gfc_actual_arglist
*a
, *b
;
2387 gfc_constructor_base ctor
;
2388 gfc_constructor
*args
[5] = {}; /* Avoid uninitialized warnings. */
2389 gfc_constructor
*ci
, *new_ctor
;
2390 gfc_expr
*expr
, *old
, *p
;
2391 int n
, i
, rank
[5], array_arg
;
2396 a
= e
->value
.function
.actual
;
2397 for (; a
; a
= a
->next
)
2398 if (a
->expr
&& !gfc_is_constant_expr (a
->expr
))
2401 /* Find which, if any, arguments are arrays. Assume that the old
2402 expression carries the type information and that the first arg
2403 that is an array expression carries all the shape information.*/
2405 a
= e
->value
.function
.actual
;
2406 for (; a
; a
= a
->next
)
2409 if (!a
->expr
|| a
->expr
->expr_type
!= EXPR_ARRAY
)
2412 expr
= gfc_copy_expr (a
->expr
);
2419 old
= gfc_copy_expr (e
);
2421 gfc_constructor_free (expr
->value
.constructor
);
2422 expr
->value
.constructor
= NULL
;
2424 expr
->where
= old
->where
;
2425 expr
->expr_type
= EXPR_ARRAY
;
2427 /* Copy the array argument constructors into an array, with nulls
2430 a
= old
->value
.function
.actual
;
2431 for (; a
; a
= a
->next
)
2433 /* Check that this is OK for an initialization expression. */
2434 if (a
->expr
&& init_flag
&& !gfc_check_init_expr (a
->expr
))
2438 if (a
->expr
&& a
->expr
->rank
&& a
->expr
->expr_type
== EXPR_VARIABLE
)
2440 rank
[n
] = a
->expr
->rank
;
2441 ctor
= a
->expr
->symtree
->n
.sym
->value
->value
.constructor
;
2442 args
[n
] = gfc_constructor_first (ctor
);
2444 else if (a
->expr
&& a
->expr
->expr_type
== EXPR_ARRAY
)
2447 rank
[n
] = a
->expr
->rank
;
2450 ctor
= gfc_constructor_copy (a
->expr
->value
.constructor
);
2451 args
[n
] = gfc_constructor_first (ctor
);
2459 /* Using the array argument as the master, step through the array
2460 calling the function for each element and advancing the array
2461 constructors together. */
2462 for (ci
= args
[array_arg
- 1]; ci
; ci
= gfc_constructor_next (ci
))
2464 new_ctor
= gfc_constructor_append_expr (&expr
->value
.constructor
,
2465 gfc_copy_expr (old
), NULL
);
2467 gfc_free_actual_arglist (new_ctor
->expr
->value
.function
.actual
);
2469 b
= old
->value
.function
.actual
;
2470 for (i
= 0; i
< n
; i
++)
2473 new_ctor
->expr
->value
.function
.actual
2474 = a
= gfc_get_actual_arglist ();
2477 a
->next
= gfc_get_actual_arglist ();
2482 a
->expr
= gfc_copy_expr (args
[i
]->expr
);
2484 a
->expr
= gfc_copy_expr (b
->expr
);
2489 /* Simplify the function calls. If the simplification fails, the
2490 error will be flagged up down-stream or the library will deal
2492 p
= gfc_copy_expr (new_ctor
->expr
);
2494 if (!gfc_simplify_expr (p
, init_flag
))
2497 gfc_replace_expr (new_ctor
->expr
, p
);
2499 for (i
= 0; i
< n
; i
++)
2501 args
[i
] = gfc_constructor_next (args
[i
]);
2503 for (i
= 1; i
< n
; i
++)
2504 if (rank
[i
] && ((args
[i
] != NULL
&& args
[array_arg
- 1] == NULL
)
2505 || (args
[i
] == NULL
&& args
[array_arg
- 1] != NULL
)))
2511 /* Free "expr" but not the pointers it contains. */
2513 gfc_free_expr (old
);
2517 gfc_error_now ("elemental function arguments at %C are not compliant");
2520 gfc_free_expr (expr
);
2521 gfc_free_expr (old
);
2527 check_intrinsic_op (gfc_expr
*e
, bool (*check_function
) (gfc_expr
*))
2529 gfc_expr
*op1
= e
->value
.op
.op1
;
2530 gfc_expr
*op2
= e
->value
.op
.op2
;
2532 if (!(*check_function
)(op1
))
2535 switch (e
->value
.op
.op
)
2537 case INTRINSIC_UPLUS
:
2538 case INTRINSIC_UMINUS
:
2539 if (!numeric_type (et0 (op1
)))
2544 case INTRINSIC_EQ_OS
:
2546 case INTRINSIC_NE_OS
:
2548 case INTRINSIC_GT_OS
:
2550 case INTRINSIC_GE_OS
:
2552 case INTRINSIC_LT_OS
:
2554 case INTRINSIC_LE_OS
:
2555 if (!(*check_function
)(op2
))
2558 if (!(et0 (op1
) == BT_CHARACTER
&& et0 (op2
) == BT_CHARACTER
)
2559 && !(numeric_type (et0 (op1
)) && numeric_type (et0 (op2
))))
2561 gfc_error ("Numeric or CHARACTER operands are required in "
2562 "expression at %L", &e
->where
);
2567 case INTRINSIC_PLUS
:
2568 case INTRINSIC_MINUS
:
2569 case INTRINSIC_TIMES
:
2570 case INTRINSIC_DIVIDE
:
2571 case INTRINSIC_POWER
:
2572 if (!(*check_function
)(op2
))
2575 if (!numeric_type (et0 (op1
)) || !numeric_type (et0 (op2
)))
2580 case INTRINSIC_CONCAT
:
2581 if (!(*check_function
)(op2
))
2584 if (et0 (op1
) != BT_CHARACTER
|| et0 (op2
) != BT_CHARACTER
)
2586 gfc_error ("Concatenation operator in expression at %L "
2587 "must have two CHARACTER operands", &op1
->where
);
2591 if (op1
->ts
.kind
!= op2
->ts
.kind
)
2593 gfc_error ("Concat operator at %L must concatenate strings of the "
2594 "same kind", &e
->where
);
2601 if (et0 (op1
) != BT_LOGICAL
)
2603 gfc_error (".NOT. operator in expression at %L must have a LOGICAL "
2604 "operand", &op1
->where
);
2613 case INTRINSIC_NEQV
:
2614 if (!(*check_function
)(op2
))
2617 if (et0 (op1
) != BT_LOGICAL
|| et0 (op2
) != BT_LOGICAL
)
2619 gfc_error ("LOGICAL operands are required in expression at %L",
2626 case INTRINSIC_PARENTHESES
:
2630 gfc_error ("Only intrinsic operators can be used in expression at %L",
2638 gfc_error ("Numeric operands are required in expression at %L", &e
->where
);
2643 /* F2003, 7.1.7 (3): In init expression, allocatable components
2644 must not be data-initialized. */
2646 check_alloc_comp_init (gfc_expr
*e
)
2648 gfc_component
*comp
;
2649 gfc_constructor
*ctor
;
2651 gcc_assert (e
->expr_type
== EXPR_STRUCTURE
);
2652 gcc_assert (e
->ts
.type
== BT_DERIVED
|| e
->ts
.type
== BT_CLASS
);
2654 for (comp
= e
->ts
.u
.derived
->components
,
2655 ctor
= gfc_constructor_first (e
->value
.constructor
);
2656 comp
; comp
= comp
->next
, ctor
= gfc_constructor_next (ctor
))
2658 if (comp
->attr
.allocatable
&& ctor
->expr
2659 && ctor
->expr
->expr_type
!= EXPR_NULL
)
2661 gfc_error ("Invalid initialization expression for ALLOCATABLE "
2662 "component %qs in structure constructor at %L",
2663 comp
->name
, &ctor
->expr
->where
);
2672 check_init_expr_arguments (gfc_expr
*e
)
2674 gfc_actual_arglist
*ap
;
2676 for (ap
= e
->value
.function
.actual
; ap
; ap
= ap
->next
)
2677 if (!gfc_check_init_expr (ap
->expr
))
2683 static bool check_restricted (gfc_expr
*);
2685 /* F95, 7.1.6.1, Initialization expressions, (7)
2686 F2003, 7.1.7 Initialization expression, (8)
2687 F2008, 7.1.12 Constant expression, (4) */
2690 check_inquiry (gfc_expr
*e
, int not_restricted
)
2693 const char *const *functions
;
2695 static const char *const inquiry_func_f95
[] = {
2696 "lbound", "shape", "size", "ubound",
2697 "bit_size", "len", "kind",
2698 "digits", "epsilon", "huge", "maxexponent", "minexponent",
2699 "precision", "radix", "range", "tiny",
2703 static const char *const inquiry_func_f2003
[] = {
2704 "lbound", "shape", "size", "ubound",
2705 "bit_size", "len", "kind",
2706 "digits", "epsilon", "huge", "maxexponent", "minexponent",
2707 "precision", "radix", "range", "tiny",
2711 /* std=f2008+ or -std=gnu */
2712 static const char *const inquiry_func_gnu
[] = {
2713 "lbound", "shape", "size", "ubound",
2714 "bit_size", "len", "kind",
2715 "digits", "epsilon", "huge", "maxexponent", "minexponent",
2716 "precision", "radix", "range", "tiny",
2717 "new_line", "storage_size", NULL
2721 gfc_actual_arglist
*ap
;
2725 if (!e
->value
.function
.isym
2726 || !e
->value
.function
.isym
->inquiry
)
2729 /* An undeclared parameter will get us here (PR25018). */
2730 if (e
->symtree
== NULL
)
2733 sym
= e
->symtree
->n
.sym
;
2735 if (sym
->from_intmod
)
2737 if (sym
->from_intmod
== INTMOD_ISO_FORTRAN_ENV
2738 && sym
->intmod_sym_id
!= ISOFORTRAN_COMPILER_OPTIONS
2739 && sym
->intmod_sym_id
!= ISOFORTRAN_COMPILER_VERSION
)
2742 if (sym
->from_intmod
== INTMOD_ISO_C_BINDING
2743 && sym
->intmod_sym_id
!= ISOCBINDING_C_SIZEOF
)
2750 functions
= inquiry_func_gnu
;
2751 if (gfc_option
.warn_std
& GFC_STD_F2003
)
2752 functions
= inquiry_func_f2003
;
2753 if (gfc_option
.warn_std
& GFC_STD_F95
)
2754 functions
= inquiry_func_f95
;
2756 for (i
= 0; functions
[i
]; i
++)
2757 if (strcmp (functions
[i
], name
) == 0)
2760 if (functions
[i
] == NULL
)
2764 /* At this point we have an inquiry function with a variable argument. The
2765 type of the variable might be undefined, but we need it now, because the
2766 arguments of these functions are not allowed to be undefined. */
2768 for (ap
= e
->value
.function
.actual
; ap
; ap
= ap
->next
)
2773 asym
= ap
->expr
->symtree
? ap
->expr
->symtree
->n
.sym
: NULL
;
2775 if (ap
->expr
->ts
.type
== BT_UNKNOWN
)
2777 if (asym
&& asym
->ts
.type
== BT_UNKNOWN
2778 && !gfc_set_default_type (asym
, 0, gfc_current_ns
))
2781 ap
->expr
->ts
= asym
->ts
;
2784 if (asym
&& asym
->assoc
&& asym
->assoc
->target
2785 && asym
->assoc
->target
->expr_type
== EXPR_CONSTANT
)
2787 gfc_free_expr (ap
->expr
);
2788 ap
->expr
= gfc_copy_expr (asym
->assoc
->target
);
2791 /* Assumed character length will not reduce to a constant expression
2792 with LEN, as required by the standard. */
2793 if (i
== 5 && not_restricted
&& asym
2794 && asym
->ts
.type
== BT_CHARACTER
2795 && ((asym
->ts
.u
.cl
&& asym
->ts
.u
.cl
->length
== NULL
)
2796 || asym
->ts
.deferred
))
2798 gfc_error ("Assumed or deferred character length variable %qs "
2799 "in constant expression at %L",
2800 asym
->name
, &ap
->expr
->where
);
2803 else if (not_restricted
&& !gfc_check_init_expr (ap
->expr
))
2806 if (not_restricted
== 0
2807 && ap
->expr
->expr_type
!= EXPR_VARIABLE
2808 && !check_restricted (ap
->expr
))
2811 if (not_restricted
== 0
2812 && ap
->expr
->expr_type
== EXPR_VARIABLE
2813 && asym
->attr
.dummy
&& asym
->attr
.optional
)
2821 /* F95, 7.1.6.1, Initialization expressions, (5)
2822 F2003, 7.1.7 Initialization expression, (5) */
2825 check_transformational (gfc_expr
*e
)
2827 static const char * const trans_func_f95
[] = {
2828 "repeat", "reshape", "selected_int_kind",
2829 "selected_real_kind", "transfer", "trim", NULL
2832 static const char * const trans_func_f2003
[] = {
2833 "all", "any", "count", "dot_product", "matmul", "null", "pack",
2834 "product", "repeat", "reshape", "selected_char_kind", "selected_int_kind",
2835 "selected_real_kind", "spread", "sum", "transfer", "transpose",
2836 "trim", "unpack", NULL
2839 static const char * const trans_func_f2008
[] = {
2840 "all", "any", "count", "dot_product", "matmul", "null", "pack",
2841 "product", "repeat", "reshape", "selected_char_kind", "selected_int_kind",
2842 "selected_real_kind", "spread", "sum", "transfer", "transpose",
2843 "trim", "unpack", "findloc", NULL
2848 const char *const *functions
;
2850 if (!e
->value
.function
.isym
2851 || !e
->value
.function
.isym
->transformational
)
2854 name
= e
->symtree
->n
.sym
->name
;
2856 if (gfc_option
.allow_std
& GFC_STD_F2008
)
2857 functions
= trans_func_f2008
;
2858 else if (gfc_option
.allow_std
& GFC_STD_F2003
)
2859 functions
= trans_func_f2003
;
2861 functions
= trans_func_f95
;
2863 /* NULL() is dealt with below. */
2864 if (strcmp ("null", name
) == 0)
2867 for (i
= 0; functions
[i
]; i
++)
2868 if (strcmp (functions
[i
], name
) == 0)
2871 if (functions
[i
] == NULL
)
2873 gfc_error ("transformational intrinsic %qs at %L is not permitted "
2874 "in an initialization expression", name
, &e
->where
);
2878 return check_init_expr_arguments (e
);
2882 /* F95, 7.1.6.1, Initialization expressions, (6)
2883 F2003, 7.1.7 Initialization expression, (6) */
2886 check_null (gfc_expr
*e
)
2888 if (strcmp ("null", e
->symtree
->n
.sym
->name
) != 0)
2891 return check_init_expr_arguments (e
);
2896 check_elemental (gfc_expr
*e
)
2898 if (!e
->value
.function
.isym
2899 || !e
->value
.function
.isym
->elemental
)
2902 if (e
->ts
.type
!= BT_INTEGER
2903 && e
->ts
.type
!= BT_CHARACTER
2904 && !gfc_notify_std (GFC_STD_F2003
, "Evaluation of nonstandard "
2905 "initialization expression at %L", &e
->where
))
2908 return check_init_expr_arguments (e
);
2913 check_conversion (gfc_expr
*e
)
2915 if (!e
->value
.function
.isym
2916 || !e
->value
.function
.isym
->conversion
)
2919 return check_init_expr_arguments (e
);
2923 /* Verify that an expression is an initialization expression. A side
2924 effect is that the expression tree is reduced to a single constant
2925 node if all goes well. This would normally happen when the
2926 expression is constructed but function references are assumed to be
2927 intrinsics in the context of initialization expressions. If
2928 false is returned an error message has been generated. */
2931 gfc_check_init_expr (gfc_expr
*e
)
2939 switch (e
->expr_type
)
2942 t
= check_intrinsic_op (e
, gfc_check_init_expr
);
2944 t
= gfc_simplify_expr (e
, 0);
2953 gfc_intrinsic_sym
* isym
= NULL
;
2954 gfc_symbol
* sym
= e
->symtree
->n
.sym
;
2956 /* Simplify here the intrinsics from the IEEE_ARITHMETIC and
2957 IEEE_EXCEPTIONS modules. */
2958 int mod
= sym
->from_intmod
;
2959 if (mod
== INTMOD_NONE
&& sym
->generic
)
2960 mod
= sym
->generic
->sym
->from_intmod
;
2961 if (mod
== INTMOD_IEEE_ARITHMETIC
|| mod
== INTMOD_IEEE_EXCEPTIONS
)
2963 gfc_expr
*new_expr
= gfc_simplify_ieee_functions (e
);
2966 gfc_replace_expr (e
, new_expr
);
2972 /* If a conversion function, e.g., __convert_i8_i4, was inserted
2973 into an array constructor, we need to skip the error check here.
2974 Conversion errors are caught below in scalarize_intrinsic_call. */
2975 conversion
= e
->value
.function
.isym
2976 && (e
->value
.function
.isym
->conversion
== 1);
2978 if (!conversion
&& (!gfc_is_intrinsic (sym
, 0, e
->where
)
2979 || (m
= gfc_intrinsic_func_interface (e
, 0)) == MATCH_NO
))
2981 gfc_error ("Function %qs in initialization expression at %L "
2982 "must be an intrinsic function",
2983 e
->symtree
->n
.sym
->name
, &e
->where
);
2987 if ((m
= check_conversion (e
)) == MATCH_NO
2988 && (m
= check_inquiry (e
, 1)) == MATCH_NO
2989 && (m
= check_null (e
)) == MATCH_NO
2990 && (m
= check_transformational (e
)) == MATCH_NO
2991 && (m
= check_elemental (e
)) == MATCH_NO
)
2993 gfc_error ("Intrinsic function %qs at %L is not permitted "
2994 "in an initialization expression",
2995 e
->symtree
->n
.sym
->name
, &e
->where
);
2999 if (m
== MATCH_ERROR
)
3002 /* Try to scalarize an elemental intrinsic function that has an
3004 isym
= gfc_find_function (e
->symtree
->n
.sym
->name
);
3005 if (isym
&& isym
->elemental
3006 && (t
= scalarize_intrinsic_call (e
, true)))
3011 t
= gfc_simplify_expr (e
, 0);
3018 /* This occurs when parsing pdt templates. */
3019 if (gfc_expr_attr (e
).pdt_kind
)
3022 if (gfc_check_iter_variable (e
))
3025 if (e
->symtree
->n
.sym
->attr
.flavor
== FL_PARAMETER
)
3027 /* A PARAMETER shall not be used to define itself, i.e.
3028 REAL, PARAMETER :: x = transfer(0, x)
3030 if (!e
->symtree
->n
.sym
->value
)
3032 gfc_error ("PARAMETER %qs is used at %L before its definition "
3033 "is complete", e
->symtree
->n
.sym
->name
, &e
->where
);
3037 t
= simplify_parameter_variable (e
, 0);
3042 if (gfc_in_match_data ())
3047 if (e
->symtree
->n
.sym
->as
)
3049 switch (e
->symtree
->n
.sym
->as
->type
)
3051 case AS_ASSUMED_SIZE
:
3052 gfc_error ("Assumed size array %qs at %L is not permitted "
3053 "in an initialization expression",
3054 e
->symtree
->n
.sym
->name
, &e
->where
);
3057 case AS_ASSUMED_SHAPE
:
3058 gfc_error ("Assumed shape array %qs at %L is not permitted "
3059 "in an initialization expression",
3060 e
->symtree
->n
.sym
->name
, &e
->where
);
3064 if (!e
->symtree
->n
.sym
->attr
.allocatable
3065 && !e
->symtree
->n
.sym
->attr
.pointer
3066 && e
->symtree
->n
.sym
->attr
.dummy
)
3067 gfc_error ("Assumed-shape array %qs at %L is not permitted "
3068 "in an initialization expression",
3069 e
->symtree
->n
.sym
->name
, &e
->where
);
3071 gfc_error ("Deferred array %qs at %L is not permitted "
3072 "in an initialization expression",
3073 e
->symtree
->n
.sym
->name
, &e
->where
);
3077 gfc_error ("Array %qs at %L is a variable, which does "
3078 "not reduce to a constant expression",
3079 e
->symtree
->n
.sym
->name
, &e
->where
);
3082 case AS_ASSUMED_RANK
:
3083 gfc_error ("Assumed-rank array %qs at %L is not permitted "
3084 "in an initialization expression",
3085 e
->symtree
->n
.sym
->name
, &e
->where
);
3093 gfc_error ("Parameter %qs at %L has not been declared or is "
3094 "a variable, which does not reduce to a constant "
3095 "expression", e
->symtree
->name
, &e
->where
);
3104 case EXPR_SUBSTRING
:
3107 t
= gfc_check_init_expr (e
->ref
->u
.ss
.start
);
3111 t
= gfc_check_init_expr (e
->ref
->u
.ss
.end
);
3113 t
= gfc_simplify_expr (e
, 0);
3119 case EXPR_STRUCTURE
:
3120 t
= e
->ts
.is_iso_c
? true : false;
3124 t
= check_alloc_comp_init (e
);
3128 t
= gfc_check_constructor (e
, gfc_check_init_expr
);
3135 t
= gfc_check_constructor (e
, gfc_check_init_expr
);
3139 t
= gfc_expand_constructor (e
, true);
3143 t
= gfc_check_constructor_type (e
);
3147 gfc_internal_error ("check_init_expr(): Unknown expression type");
3153 /* Reduces a general expression to an initialization expression (a constant).
3154 This used to be part of gfc_match_init_expr.
3155 Note that this function doesn't free the given expression on false. */
3158 gfc_reduce_init_expr (gfc_expr
*expr
)
3162 gfc_init_expr_flag
= true;
3163 t
= gfc_resolve_expr (expr
);
3165 t
= gfc_check_init_expr (expr
);
3166 gfc_init_expr_flag
= false;
3171 if (expr
->expr_type
== EXPR_ARRAY
)
3173 if (!gfc_check_constructor_type (expr
))
3175 if (!gfc_expand_constructor (expr
, true))
3183 /* Match an initialization expression. We work by first matching an
3184 expression, then reducing it to a constant. */
3187 gfc_match_init_expr (gfc_expr
**result
)
3195 gfc_init_expr_flag
= true;
3197 m
= gfc_match_expr (&expr
);
3200 gfc_init_expr_flag
= false;
3204 if (gfc_derived_parameter_expr (expr
))
3207 gfc_init_expr_flag
= false;
3211 t
= gfc_reduce_init_expr (expr
);
3214 gfc_free_expr (expr
);
3215 gfc_init_expr_flag
= false;
3220 gfc_init_expr_flag
= false;
3226 /* Given an actual argument list, test to see that each argument is a
3227 restricted expression and optionally if the expression type is
3228 integer or character. */
3231 restricted_args (gfc_actual_arglist
*a
)
3233 for (; a
; a
= a
->next
)
3235 if (!check_restricted (a
->expr
))
3243 /************* Restricted/specification expressions *************/
3246 /* Make sure a non-intrinsic function is a specification function,
3247 * see F08:7.1.11.5. */
3250 external_spec_function (gfc_expr
*e
)
3254 f
= e
->value
.function
.esym
;
3256 /* IEEE functions allowed are "a reference to a transformational function
3257 from the intrinsic module IEEE_ARITHMETIC or IEEE_EXCEPTIONS", and
3258 "inquiry function from the intrinsic modules IEEE_ARITHMETIC and
3259 IEEE_EXCEPTIONS". */
3260 if (f
->from_intmod
== INTMOD_IEEE_ARITHMETIC
3261 || f
->from_intmod
== INTMOD_IEEE_EXCEPTIONS
)
3263 if (!strcmp (f
->name
, "ieee_selected_real_kind")
3264 || !strcmp (f
->name
, "ieee_support_rounding")
3265 || !strcmp (f
->name
, "ieee_support_flag")
3266 || !strcmp (f
->name
, "ieee_support_halting")
3267 || !strcmp (f
->name
, "ieee_support_datatype")
3268 || !strcmp (f
->name
, "ieee_support_denormal")
3269 || !strcmp (f
->name
, "ieee_support_subnormal")
3270 || !strcmp (f
->name
, "ieee_support_divide")
3271 || !strcmp (f
->name
, "ieee_support_inf")
3272 || !strcmp (f
->name
, "ieee_support_io")
3273 || !strcmp (f
->name
, "ieee_support_nan")
3274 || !strcmp (f
->name
, "ieee_support_sqrt")
3275 || !strcmp (f
->name
, "ieee_support_standard")
3276 || !strcmp (f
->name
, "ieee_support_underflow_control"))
3277 goto function_allowed
;
3280 if (f
->attr
.proc
== PROC_ST_FUNCTION
)
3282 gfc_error ("Specification function %qs at %L cannot be a statement "
3283 "function", f
->name
, &e
->where
);
3287 if (f
->attr
.proc
== PROC_INTERNAL
)
3289 gfc_error ("Specification function %qs at %L cannot be an internal "
3290 "function", f
->name
, &e
->where
);
3294 if (!f
->attr
.pure
&& !f
->attr
.elemental
)
3296 gfc_error ("Specification function %qs at %L must be PURE", f
->name
,
3302 if (f
->attr
.recursive
3303 && !gfc_notify_std (GFC_STD_F2003
,
3304 "Specification function %qs "
3305 "at %L cannot be RECURSIVE", f
->name
, &e
->where
))
3309 return restricted_args (e
->value
.function
.actual
);
3313 /* Check to see that a function reference to an intrinsic is a
3314 restricted expression. */
3317 restricted_intrinsic (gfc_expr
*e
)
3319 /* TODO: Check constraints on inquiry functions. 7.1.6.2 (7). */
3320 if (check_inquiry (e
, 0) == MATCH_YES
)
3323 return restricted_args (e
->value
.function
.actual
);
3327 /* Check the expressions of an actual arglist. Used by check_restricted. */
3330 check_arglist (gfc_actual_arglist
* arg
, bool (*checker
) (gfc_expr
*))
3332 for (; arg
; arg
= arg
->next
)
3333 if (!checker (arg
->expr
))
3340 /* Check the subscription expressions of a reference chain with a checking
3341 function; used by check_restricted. */
3344 check_references (gfc_ref
* ref
, bool (*checker
) (gfc_expr
*))
3354 for (dim
= 0; dim
< ref
->u
.ar
.dimen
; ++dim
)
3356 if (!checker (ref
->u
.ar
.start
[dim
]))
3358 if (!checker (ref
->u
.ar
.end
[dim
]))
3360 if (!checker (ref
->u
.ar
.stride
[dim
]))
3366 /* Nothing needed, just proceed to next reference. */
3370 if (!checker (ref
->u
.ss
.start
))
3372 if (!checker (ref
->u
.ss
.end
))
3381 return check_references (ref
->next
, checker
);
3384 /* Return true if ns is a parent of the current ns. */
3387 is_parent_of_current_ns (gfc_namespace
*ns
)
3390 for (p
= gfc_current_ns
->parent
; p
; p
= p
->parent
)
3397 /* Verify that an expression is a restricted expression. Like its
3398 cousin check_init_expr(), an error message is generated if we
3402 check_restricted (gfc_expr
*e
)
3410 switch (e
->expr_type
)
3413 t
= check_intrinsic_op (e
, check_restricted
);
3415 t
= gfc_simplify_expr (e
, 0);
3420 if (e
->value
.function
.esym
)
3422 t
= check_arglist (e
->value
.function
.actual
, &check_restricted
);
3424 t
= external_spec_function (e
);
3428 if (e
->value
.function
.isym
&& e
->value
.function
.isym
->inquiry
)
3431 t
= check_arglist (e
->value
.function
.actual
, &check_restricted
);
3434 t
= restricted_intrinsic (e
);
3439 sym
= e
->symtree
->n
.sym
;
3442 /* If a dummy argument appears in a context that is valid for a
3443 restricted expression in an elemental procedure, it will have
3444 already been simplified away once we get here. Therefore we
3445 don't need to jump through hoops to distinguish valid from
3446 invalid cases. Allowed in F2008 and F2018. */
3447 if (gfc_notification_std (GFC_STD_F2008
)
3448 && sym
->attr
.dummy
&& sym
->ns
== gfc_current_ns
3449 && sym
->ns
->proc_name
&& sym
->ns
->proc_name
->attr
.elemental
)
3451 gfc_error_now ("Dummy argument %qs not "
3452 "allowed in expression at %L",
3453 sym
->name
, &e
->where
);
3457 if (sym
->attr
.optional
)
3459 gfc_error ("Dummy argument %qs at %L cannot be OPTIONAL",
3460 sym
->name
, &e
->where
);
3464 if (sym
->attr
.intent
== INTENT_OUT
)
3466 gfc_error ("Dummy argument %qs at %L cannot be INTENT(OUT)",
3467 sym
->name
, &e
->where
);
3471 /* Check reference chain if any. */
3472 if (!check_references (e
->ref
, &check_restricted
))
3475 /* gfc_is_formal_arg broadcasts that a formal argument list is being
3476 processed in resolve.cc(resolve_formal_arglist). This is done so
3477 that host associated dummy array indices are accepted (PR23446).
3478 This mechanism also does the same for the specification expressions
3479 of array-valued functions. */
3481 || sym
->attr
.in_common
3482 || sym
->attr
.use_assoc
3484 || sym
->attr
.implied_index
3485 || sym
->attr
.flavor
== FL_PARAMETER
3486 || is_parent_of_current_ns (sym
->ns
)
3487 || (sym
->ns
->proc_name
!= NULL
3488 && sym
->ns
->proc_name
->attr
.flavor
== FL_MODULE
)
3489 || (gfc_is_formal_arg () && (sym
->ns
== gfc_current_ns
)))
3495 gfc_error ("Variable %qs cannot appear in the expression at %L",
3496 sym
->name
, &e
->where
);
3497 /* Prevent a repetition of the error. */
3506 case EXPR_SUBSTRING
:
3507 t
= gfc_specification_expr (e
->ref
->u
.ss
.start
);
3511 t
= gfc_specification_expr (e
->ref
->u
.ss
.end
);
3513 t
= gfc_simplify_expr (e
, 0);
3517 case EXPR_STRUCTURE
:
3518 t
= gfc_check_constructor (e
, check_restricted
);
3522 t
= gfc_check_constructor (e
, check_restricted
);
3526 gfc_internal_error ("check_restricted(): Unknown expression type");
3533 /* Check to see that an expression is a specification expression. If
3534 we return false, an error has been generated. */
3537 gfc_specification_expr (gfc_expr
*e
)
3539 gfc_component
*comp
;
3544 if (e
->ts
.type
!= BT_INTEGER
)
3546 gfc_error ("Expression at %L must be of INTEGER type, found %s",
3547 &e
->where
, gfc_basic_typename (e
->ts
.type
));
3551 comp
= gfc_get_proc_ptr_comp (e
);
3552 if (e
->expr_type
== EXPR_FUNCTION
3553 && !e
->value
.function
.isym
3554 && !e
->value
.function
.esym
3555 && !gfc_pure (e
->symtree
->n
.sym
)
3556 && (!comp
|| !comp
->attr
.pure
))
3558 gfc_error ("Function %qs at %L must be PURE",
3559 e
->symtree
->n
.sym
->name
, &e
->where
);
3560 /* Prevent repeat error messages. */
3561 e
->symtree
->n
.sym
->attr
.pure
= 1;
3567 gfc_error ("Expression at %L must be scalar", &e
->where
);
3571 if (!gfc_simplify_expr (e
, 0))
3574 return check_restricted (e
);
3578 /************** Expression conformance checks. *************/
3580 /* Given two expressions, make sure that the arrays are conformable. */
3583 gfc_check_conformance (gfc_expr
*op1
, gfc_expr
*op2
, const char *optype_msgid
, ...)
3585 int op1_flag
, op2_flag
, d
;
3586 mpz_t op1_size
, op2_size
;
3592 if (op1
->rank
== 0 || op2
->rank
== 0)
3595 va_start (argp
, optype_msgid
);
3596 d
= vsnprintf (buffer
, sizeof (buffer
), optype_msgid
, argp
);
3598 if (d
< 1 || d
>= (int) sizeof (buffer
)) /* Reject truncation. */
3599 gfc_internal_error ("optype_msgid overflow: %d", d
);
3601 if (op1
->rank
!= op2
->rank
)
3603 gfc_error ("Incompatible ranks in %s (%d and %d) at %L", _(buffer
),
3604 op1
->rank
, op2
->rank
, &op1
->where
);
3610 for (d
= 0; d
< op1
->rank
; d
++)
3612 op1_flag
= gfc_array_dimen_size(op1
, d
, &op1_size
);
3613 op2_flag
= gfc_array_dimen_size(op2
, d
, &op2_size
);
3615 if (op1_flag
&& op2_flag
&& mpz_cmp (op1_size
, op2_size
) != 0)
3617 gfc_error ("Different shape for %s at %L on dimension %d "
3618 "(%d and %d)", _(buffer
), &op1
->where
, d
+ 1,
3619 (int) mpz_get_si (op1_size
),
3620 (int) mpz_get_si (op2_size
));
3626 mpz_clear (op1_size
);
3628 mpz_clear (op2_size
);
3638 /* Given an assignable expression and an arbitrary expression, make
3639 sure that the assignment can take place. Only add a call to the intrinsic
3640 conversion routines, when allow_convert is set. When this assign is a
3641 coarray call, then the convert is done by the coarray routine implictly and
3642 adding the intrinsic conversion would do harm in most cases. */
3645 gfc_check_assign (gfc_expr
*lvalue
, gfc_expr
*rvalue
, int conform
,
3652 sym
= lvalue
->symtree
->n
.sym
;
3654 /* See if this is the component or subcomponent of a pointer and guard
3655 against assignment to LEN or KIND part-refs. */
3656 has_pointer
= sym
->attr
.pointer
;
3657 for (ref
= lvalue
->ref
; ref
; ref
= ref
->next
)
3659 if (!has_pointer
&& ref
->type
== REF_COMPONENT
3660 && ref
->u
.c
.component
->attr
.pointer
)
3662 else if (ref
->type
== REF_INQUIRY
3663 && (ref
->u
.i
== INQUIRY_LEN
|| ref
->u
.i
== INQUIRY_KIND
))
3665 gfc_error ("Assignment to a LEN or KIND part_ref at %L is not "
3666 "allowed", &lvalue
->where
);
3671 /* 12.5.2.2, Note 12.26: The result variable is very similar to any other
3672 variable local to a function subprogram. Its existence begins when
3673 execution of the function is initiated and ends when execution of the
3674 function is terminated...
3675 Therefore, the left hand side is no longer a variable, when it is: */
3676 if (sym
->attr
.flavor
== FL_PROCEDURE
&& sym
->attr
.proc
!= PROC_ST_FUNCTION
3677 && !sym
->attr
.external
)
3682 /* (i) Use associated; */
3683 if (sym
->attr
.use_assoc
)
3686 /* (ii) The assignment is in the main program; or */
3687 if (gfc_current_ns
->proc_name
3688 && gfc_current_ns
->proc_name
->attr
.is_main_program
)
3691 /* (iii) A module or internal procedure... */
3692 if (gfc_current_ns
->proc_name
3693 && (gfc_current_ns
->proc_name
->attr
.proc
== PROC_INTERNAL
3694 || gfc_current_ns
->proc_name
->attr
.proc
== PROC_MODULE
)
3695 && gfc_current_ns
->parent
3696 && (!(gfc_current_ns
->parent
->proc_name
->attr
.function
3697 || gfc_current_ns
->parent
->proc_name
->attr
.subroutine
)
3698 || gfc_current_ns
->parent
->proc_name
->attr
.is_main_program
))
3700 /* ... that is not a function... */
3701 if (gfc_current_ns
->proc_name
3702 && !gfc_current_ns
->proc_name
->attr
.function
)
3705 /* ... or is not an entry and has a different name. */
3706 if (!sym
->attr
.entry
&& sym
->name
!= gfc_current_ns
->proc_name
->name
)
3710 /* (iv) Host associated and not the function symbol or the
3711 parent result. This picks up sibling references, which
3712 cannot be entries. */
3713 if (!sym
->attr
.entry
3714 && sym
->ns
== gfc_current_ns
->parent
3715 && sym
!= gfc_current_ns
->proc_name
3716 && sym
!= gfc_current_ns
->parent
->proc_name
->result
)
3721 gfc_error ("%qs at %L is not a VALUE", sym
->name
, &lvalue
->where
);
3727 /* Reject assigning to an external symbol. For initializers, this
3728 was already done before, in resolve_fl_procedure. */
3729 if (sym
->attr
.flavor
== FL_PROCEDURE
&& sym
->attr
.external
3730 && sym
->attr
.proc
!= PROC_MODULE
&& !rvalue
->error
)
3732 gfc_error ("Illegal assignment to external procedure at %L",
3738 if (rvalue
->rank
!= 0 && lvalue
->rank
!= rvalue
->rank
)
3740 gfc_error ("Incompatible ranks %d and %d in assignment at %L",
3741 lvalue
->rank
, rvalue
->rank
, &lvalue
->where
);
3745 if (lvalue
->ts
.type
== BT_UNKNOWN
)
3747 gfc_error ("Variable type is UNKNOWN in assignment at %L",
3752 if (rvalue
->expr_type
== EXPR_NULL
)
3754 if (has_pointer
&& (ref
== NULL
|| ref
->next
== NULL
)
3755 && lvalue
->symtree
->n
.sym
->attr
.data
)
3759 gfc_error ("NULL appears on right-hand side in assignment at %L",
3765 /* This is possibly a typo: x = f() instead of x => f(). */
3767 && rvalue
->expr_type
== EXPR_FUNCTION
&& gfc_expr_attr (rvalue
).pointer
)
3768 gfc_warning (OPT_Wsurprising
,
3769 "POINTER-valued function appears on right-hand side of "
3770 "assignment at %L", &rvalue
->where
);
3772 /* Check size of array assignments. */
3773 if (lvalue
->rank
!= 0 && rvalue
->rank
!= 0
3774 && !gfc_check_conformance (lvalue
, rvalue
, _("array assignment")))
3777 /* Handle the case of a BOZ literal on the RHS. */
3778 if (rvalue
->ts
.type
== BT_BOZ
)
3780 if (lvalue
->symtree
->n
.sym
->attr
.data
)
3782 if (lvalue
->ts
.type
== BT_INTEGER
3783 && gfc_boz2int (rvalue
, lvalue
->ts
.kind
))
3786 if (lvalue
->ts
.type
== BT_REAL
3787 && gfc_boz2real (rvalue
, lvalue
->ts
.kind
))
3789 if (gfc_invalid_boz ("BOZ literal constant near %L cannot "
3790 "be assigned to a REAL variable",
3797 if (!lvalue
->symtree
->n
.sym
->attr
.data
3798 && gfc_invalid_boz ("BOZ literal constant at %L is neither a "
3799 "data-stmt-constant nor an actual argument to "
3800 "INT, REAL, DBLE, or CMPLX intrinsic function",
3804 if (lvalue
->ts
.type
== BT_INTEGER
3805 && gfc_boz2int (rvalue
, lvalue
->ts
.kind
))
3808 if (lvalue
->ts
.type
== BT_REAL
3809 && gfc_boz2real (rvalue
, lvalue
->ts
.kind
))
3812 gfc_error ("BOZ literal constant near %L cannot be assigned to a "
3813 "%qs variable", &rvalue
->where
, gfc_typename (lvalue
));
3817 if (gfc_expr_attr (lvalue
).pdt_kind
|| gfc_expr_attr (lvalue
).pdt_len
)
3819 gfc_error ("The assignment to a KIND or LEN component of a "
3820 "parameterized type at %L is not allowed",
3825 if (gfc_compare_types (&lvalue
->ts
, &rvalue
->ts
))
3828 /* Only DATA Statements come here. */
3833 /* Numeric can be converted to any other numeric. And Hollerith can be
3834 converted to any other type. */
3835 if ((gfc_numeric_ts (&lvalue
->ts
) && gfc_numeric_ts (&rvalue
->ts
))
3836 || rvalue
->ts
.type
== BT_HOLLERITH
)
3839 if (flag_dec_char_conversions
&& (gfc_numeric_ts (&lvalue
->ts
)
3840 || lvalue
->ts
.type
== BT_LOGICAL
)
3841 && rvalue
->ts
.type
== BT_CHARACTER
3842 && rvalue
->ts
.kind
== gfc_default_character_kind
)
3845 if (lvalue
->ts
.type
== BT_LOGICAL
&& rvalue
->ts
.type
== BT_LOGICAL
)
3848 where
= lvalue
->where
.lb
? &lvalue
->where
: &rvalue
->where
;
3849 gfc_error ("Incompatible types in DATA statement at %L; attempted "
3850 "conversion of %s to %s", where
,
3851 gfc_typename (rvalue
), gfc_typename (lvalue
));
3856 /* Assignment is the only case where character variables of different
3857 kind values can be converted into one another. */
3858 if (lvalue
->ts
.type
== BT_CHARACTER
&& rvalue
->ts
.type
== BT_CHARACTER
)
3860 if (lvalue
->ts
.kind
!= rvalue
->ts
.kind
&& allow_convert
)
3861 return gfc_convert_chartype (rvalue
, &lvalue
->ts
);
3869 return gfc_convert_type (rvalue
, &lvalue
->ts
, 1);
3873 /* Check that a pointer assignment is OK. We first check lvalue, and
3874 we only check rvalue if it's not an assignment to NULL() or a
3875 NULLIFY statement. */
3878 gfc_check_pointer_assign (gfc_expr
*lvalue
, gfc_expr
*rvalue
,
3879 bool suppress_type_test
, bool is_init_expr
)
3881 symbol_attribute attr
, lhs_attr
;
3883 bool is_pure
, is_implicit_pure
, rank_remap
;
3887 if (!lvalue
->symtree
)
3890 lhs_attr
= gfc_expr_attr (lvalue
);
3891 if (lvalue
->ts
.type
== BT_UNKNOWN
&& !lhs_attr
.proc_pointer
)
3893 gfc_error ("Pointer assignment target is not a POINTER at %L",
3898 if (lhs_attr
.flavor
== FL_PROCEDURE
&& lhs_attr
.use_assoc
3899 && !lhs_attr
.proc_pointer
)
3901 gfc_error ("%qs in the pointer assignment at %L cannot be an "
3902 "l-value since it is a procedure",
3903 lvalue
->symtree
->n
.sym
->name
, &lvalue
->where
);
3907 proc_pointer
= lvalue
->symtree
->n
.sym
->attr
.proc_pointer
;
3910 same_rank
= lvalue
->rank
== rvalue
->rank
;
3911 for (ref
= lvalue
->ref
; ref
; ref
= ref
->next
)
3913 if (ref
->type
== REF_COMPONENT
)
3914 proc_pointer
= ref
->u
.c
.component
->attr
.proc_pointer
;
3916 if (ref
->type
== REF_ARRAY
&& ref
->next
== NULL
)
3920 if (ref
->u
.ar
.type
== AR_FULL
)
3923 if (ref
->u
.ar
.type
!= AR_SECTION
)
3925 gfc_error ("Expected bounds specification for %qs at %L",
3926 lvalue
->symtree
->n
.sym
->name
, &lvalue
->where
);
3930 if (!gfc_notify_std (GFC_STD_F2003
, "Bounds specification "
3931 "for %qs in pointer assignment at %L",
3932 lvalue
->symtree
->n
.sym
->name
, &lvalue
->where
))
3935 /* Fortran standard (e.g. F2018, 10.2.2 Pointer assignment):
3937 * (C1017) If bounds-spec-list is specified, the number of
3938 * bounds-specs shall equal the rank of data-pointer-object.
3940 * If bounds-spec-list appears, it specifies the lower bounds.
3942 * (C1018) If bounds-remapping-list is specified, the number of
3943 * bounds-remappings shall equal the rank of data-pointer-object.
3945 * If bounds-remapping-list appears, it specifies the upper and
3946 * lower bounds of each dimension of the pointer; the pointer target
3947 * shall be simply contiguous or of rank one.
3949 * (C1019) If bounds-remapping-list is not specified, the ranks of
3950 * data-pointer-object and data-target shall be the same.
3952 * Thus when bounds are given, all lbounds are necessary and either
3953 * all or none of the upper bounds; no strides are allowed. If the
3954 * upper bounds are present, we may do rank remapping. */
3955 for (dim
= 0; dim
< ref
->u
.ar
.dimen
; ++dim
)
3957 if (ref
->u
.ar
.stride
[dim
])
3959 gfc_error ("Stride must not be present at %L",
3963 if (!same_rank
&& (!ref
->u
.ar
.start
[dim
] ||!ref
->u
.ar
.end
[dim
]))
3965 gfc_error ("Rank remapping requires a "
3966 "list of %<lower-bound : upper-bound%> "
3967 "specifications at %L", &lvalue
->where
);
3970 if (!ref
->u
.ar
.start
[dim
]
3971 || ref
->u
.ar
.dimen_type
[dim
] != DIMEN_RANGE
)
3973 gfc_error ("Expected list of %<lower-bound :%> or "
3974 "list of %<lower-bound : upper-bound%> "
3975 "specifications at %L", &lvalue
->where
);
3980 rank_remap
= (ref
->u
.ar
.end
[dim
] != NULL
);
3983 if ((rank_remap
&& !ref
->u
.ar
.end
[dim
]))
3985 gfc_error ("Rank remapping requires a "
3986 "list of %<lower-bound : upper-bound%> "
3987 "specifications at %L", &lvalue
->where
);
3990 if (!rank_remap
&& ref
->u
.ar
.end
[dim
])
3992 gfc_error ("Expected list of %<lower-bound :%> or "
3993 "list of %<lower-bound : upper-bound%> "
3994 "specifications at %L", &lvalue
->where
);
4002 is_pure
= gfc_pure (NULL
);
4003 is_implicit_pure
= gfc_implicit_pure (NULL
);
4005 /* If rvalue is a NULL() or NULLIFY, we're done. Otherwise the type,
4006 kind, etc for lvalue and rvalue must match, and rvalue must be a
4007 pure variable if we're in a pure function. */
4008 if (rvalue
->expr_type
== EXPR_NULL
&& rvalue
->ts
.type
== BT_UNKNOWN
)
4011 /* F2008, C723 (pointer) and C726 (proc-pointer); for PURE also C1283. */
4012 if (lvalue
->expr_type
== EXPR_VARIABLE
4013 && gfc_is_coindexed (lvalue
))
4016 for (ref
= lvalue
->ref
; ref
; ref
= ref
->next
)
4017 if (ref
->type
== REF_ARRAY
&& ref
->u
.ar
.codimen
)
4019 gfc_error ("Pointer object at %L shall not have a coindex",
4025 /* Checks on rvalue for procedure pointer assignments. */
4030 gfc_component
*comp1
, *comp2
;
4033 attr
= gfc_expr_attr (rvalue
);
4034 if (!((rvalue
->expr_type
== EXPR_NULL
)
4035 || (rvalue
->expr_type
== EXPR_FUNCTION
&& attr
.proc_pointer
)
4036 || (rvalue
->expr_type
== EXPR_VARIABLE
&& attr
.proc_pointer
)
4037 || (rvalue
->expr_type
== EXPR_VARIABLE
4038 && attr
.flavor
== FL_PROCEDURE
)))
4040 gfc_error ("Invalid procedure pointer assignment at %L",
4045 if (rvalue
->expr_type
== EXPR_VARIABLE
&& !attr
.proc_pointer
)
4047 /* Check for intrinsics. */
4048 gfc_symbol
*sym
= rvalue
->symtree
->n
.sym
;
4049 if (!sym
->attr
.intrinsic
4050 && (gfc_is_intrinsic (sym
, 0, sym
->declared_at
)
4051 || gfc_is_intrinsic (sym
, 1, sym
->declared_at
)))
4053 sym
->attr
.intrinsic
= 1;
4054 gfc_resolve_intrinsic (sym
, &rvalue
->where
);
4055 attr
= gfc_expr_attr (rvalue
);
4057 /* Check for result of embracing function. */
4058 if (sym
->attr
.function
&& sym
->result
== sym
)
4062 for (ns
= gfc_current_ns
; ns
; ns
= ns
->parent
)
4063 if (sym
== ns
->proc_name
)
4065 gfc_error ("Function result %qs is invalid as proc-target "
4066 "in procedure pointer assignment at %L",
4067 sym
->name
, &rvalue
->where
);
4074 gfc_error ("Abstract interface %qs is invalid "
4075 "in procedure pointer assignment at %L",
4076 rvalue
->symtree
->name
, &rvalue
->where
);
4079 /* Check for F08:C729. */
4080 if (attr
.flavor
== FL_PROCEDURE
)
4082 if (attr
.proc
== PROC_ST_FUNCTION
)
4084 gfc_error ("Statement function %qs is invalid "
4085 "in procedure pointer assignment at %L",
4086 rvalue
->symtree
->name
, &rvalue
->where
);
4089 if (attr
.proc
== PROC_INTERNAL
&&
4090 !gfc_notify_std(GFC_STD_F2008
, "Internal procedure %qs "
4091 "is invalid in procedure pointer assignment "
4092 "at %L", rvalue
->symtree
->name
, &rvalue
->where
))
4094 if (attr
.intrinsic
&& gfc_intrinsic_actual_ok (rvalue
->symtree
->name
,
4095 attr
.subroutine
) == 0)
4097 gfc_error ("Intrinsic %qs at %L is invalid in procedure pointer "
4098 "assignment", rvalue
->symtree
->name
, &rvalue
->where
);
4102 /* Check for F08:C730. */
4103 if (attr
.elemental
&& !attr
.intrinsic
)
4105 gfc_error ("Nonintrinsic elemental procedure %qs is invalid "
4106 "in procedure pointer assignment at %L",
4107 rvalue
->symtree
->name
, &rvalue
->where
);
4111 /* Ensure that the calling convention is the same. As other attributes
4112 such as DLLEXPORT may differ, one explicitly only tests for the
4113 calling conventions. */
4114 if (rvalue
->expr_type
== EXPR_VARIABLE
4115 && lvalue
->symtree
->n
.sym
->attr
.ext_attr
4116 != rvalue
->symtree
->n
.sym
->attr
.ext_attr
)
4118 symbol_attribute calls
;
4121 gfc_add_ext_attribute (&calls
, EXT_ATTR_CDECL
, NULL
);
4122 gfc_add_ext_attribute (&calls
, EXT_ATTR_STDCALL
, NULL
);
4123 gfc_add_ext_attribute (&calls
, EXT_ATTR_FASTCALL
, NULL
);
4125 if ((calls
.ext_attr
& lvalue
->symtree
->n
.sym
->attr
.ext_attr
)
4126 != (calls
.ext_attr
& rvalue
->symtree
->n
.sym
->attr
.ext_attr
))
4128 gfc_error ("Mismatch in the procedure pointer assignment "
4129 "at %L: mismatch in the calling convention",
4135 comp1
= gfc_get_proc_ptr_comp (lvalue
);
4137 s1
= comp1
->ts
.interface
;
4140 s1
= lvalue
->symtree
->n
.sym
;
4141 if (s1
->ts
.interface
)
4142 s1
= s1
->ts
.interface
;
4145 comp2
= gfc_get_proc_ptr_comp (rvalue
);
4148 if (rvalue
->expr_type
== EXPR_FUNCTION
)
4150 s2
= comp2
->ts
.interface
->result
;
4155 s2
= comp2
->ts
.interface
;
4159 else if (rvalue
->expr_type
== EXPR_FUNCTION
)
4161 if (rvalue
->value
.function
.esym
)
4162 s2
= rvalue
->value
.function
.esym
->result
;
4164 s2
= rvalue
->symtree
->n
.sym
->result
;
4170 s2
= rvalue
->symtree
->n
.sym
;
4174 if (s2
&& s2
->attr
.proc_pointer
&& s2
->ts
.interface
)
4175 s2
= s2
->ts
.interface
;
4177 /* Special check for the case of absent interface on the lvalue.
4178 * All other interface checks are done below. */
4179 if (!s1
&& comp1
&& comp1
->attr
.subroutine
&& s2
&& s2
->attr
.function
)
4181 gfc_error ("Interface mismatch in procedure pointer assignment "
4182 "at %L: %qs is not a subroutine", &rvalue
->where
, name
);
4186 /* F08:7.2.2.4 (4) */
4187 if (s2
&& gfc_explicit_interface_required (s2
, err
, sizeof(err
)))
4191 gfc_error ("Explicit interface required for component %qs at %L: %s",
4192 comp1
->name
, &lvalue
->where
, err
);
4195 else if (s1
->attr
.if_source
== IFSRC_UNKNOWN
)
4197 gfc_error ("Explicit interface required for %qs at %L: %s",
4198 s1
->name
, &lvalue
->where
, err
);
4202 if (s1
&& gfc_explicit_interface_required (s1
, err
, sizeof(err
)))
4206 gfc_error ("Explicit interface required for component %qs at %L: %s",
4207 comp2
->name
, &rvalue
->where
, err
);
4210 else if (s2
->attr
.if_source
== IFSRC_UNKNOWN
)
4212 gfc_error ("Explicit interface required for %qs at %L: %s",
4213 s2
->name
, &rvalue
->where
, err
);
4218 if (s1
== s2
|| !s1
|| !s2
)
4221 if (!gfc_compare_interfaces (s1
, s2
, name
, 0, 1,
4222 err
, sizeof(err
), NULL
, NULL
))
4224 gfc_error ("Interface mismatch in procedure pointer assignment "
4225 "at %L: %s", &rvalue
->where
, err
);
4229 /* Check F2008Cor2, C729. */
4230 if (!s2
->attr
.intrinsic
&& s2
->attr
.if_source
== IFSRC_UNKNOWN
4231 && !s2
->attr
.external
&& !s2
->attr
.subroutine
&& !s2
->attr
.function
)
4233 gfc_error ("Procedure pointer target %qs at %L must be either an "
4234 "intrinsic, host or use associated, referenced or have "
4235 "the EXTERNAL attribute", s2
->name
, &rvalue
->where
);
4243 /* A non-proc pointer cannot point to a constant. */
4244 if (rvalue
->expr_type
== EXPR_CONSTANT
)
4246 gfc_error_now ("Pointer assignment target cannot be a constant at %L",
4252 if (!gfc_compare_types (&lvalue
->ts
, &rvalue
->ts
))
4254 /* Check for F03:C717. */
4255 if (UNLIMITED_POLY (rvalue
)
4256 && !(UNLIMITED_POLY (lvalue
)
4257 || (lvalue
->ts
.type
== BT_DERIVED
4258 && (lvalue
->ts
.u
.derived
->attr
.is_bind_c
4259 || lvalue
->ts
.u
.derived
->attr
.sequence
))))
4260 gfc_error ("Data-pointer-object at %L must be unlimited "
4261 "polymorphic, or of a type with the BIND or SEQUENCE "
4262 "attribute, to be compatible with an unlimited "
4263 "polymorphic target", &lvalue
->where
);
4264 else if (!suppress_type_test
)
4265 gfc_error ("Different types in pointer assignment at %L; "
4266 "attempted assignment of %s to %s", &lvalue
->where
,
4267 gfc_typename (rvalue
), gfc_typename (lvalue
));
4271 if (lvalue
->ts
.type
!= BT_CLASS
&& lvalue
->ts
.kind
!= rvalue
->ts
.kind
)
4273 gfc_error ("Different kind type parameters in pointer "
4274 "assignment at %L", &lvalue
->where
);
4278 if (lvalue
->rank
!= rvalue
->rank
&& !rank_remap
)
4280 gfc_error ("Different ranks in pointer assignment at %L", &lvalue
->where
);
4284 /* Make sure the vtab is present. */
4285 if (lvalue
->ts
.type
== BT_CLASS
&& !UNLIMITED_POLY (rvalue
))
4286 gfc_find_vtab (&rvalue
->ts
);
4288 /* Check rank remapping. */
4293 /* If this can be determined, check that the target must be at least as
4294 large as the pointer assigned to it is. */
4295 if (gfc_array_size (lvalue
, &lsize
)
4296 && gfc_array_size (rvalue
, &rsize
)
4297 && mpz_cmp (rsize
, lsize
) < 0)
4299 gfc_error ("Rank remapping target is smaller than size of the"
4300 " pointer (%ld < %ld) at %L",
4301 mpz_get_si (rsize
), mpz_get_si (lsize
),
4306 /* The target must be either rank one or it must be simply contiguous
4307 and F2008 must be allowed. */
4308 if (rvalue
->rank
!= 1)
4310 if (!gfc_is_simply_contiguous (rvalue
, true, false))
4312 gfc_error ("Rank remapping target must be rank 1 or"
4313 " simply contiguous at %L", &rvalue
->where
);
4316 if (!gfc_notify_std (GFC_STD_F2008
, "Rank remapping target is not "
4317 "rank 1 at %L", &rvalue
->where
))
4322 /* Now punt if we are dealing with a NULLIFY(X) or X = NULL(X). */
4323 if (rvalue
->expr_type
== EXPR_NULL
)
4326 if (rvalue
->expr_type
== EXPR_VARIABLE
&& is_subref_array (rvalue
))
4327 lvalue
->symtree
->n
.sym
->attr
.subref_array_pointer
= 1;
4329 attr
= gfc_expr_attr (rvalue
);
4331 if (rvalue
->expr_type
== EXPR_FUNCTION
&& !attr
.pointer
)
4333 /* F2008, C725. For PURE also C1283. Sometimes rvalue is a function call
4334 to caf_get. Map this to the same error message as below when it is
4335 still a variable expression. */
4336 if (rvalue
->value
.function
.isym
4337 && rvalue
->value
.function
.isym
->id
== GFC_ISYM_CAF_GET
)
4338 /* The test above might need to be extend when F08, Note 5.4 has to be
4339 interpreted in the way that target and pointer with the same coindex
4341 gfc_error ("Data target at %L shall not have a coindex",
4344 gfc_error ("Target expression in pointer assignment "
4345 "at %L must deliver a pointer result",
4356 if (gfc_is_size_zero_array (rvalue
))
4358 gfc_error ("Zero-sized array detected at %L where an entity with "
4359 "the TARGET attribute is expected", &rvalue
->where
);
4362 else if (!rvalue
->symtree
)
4364 gfc_error ("Pointer assignment target in initialization expression "
4365 "does not have the TARGET attribute at %L",
4370 sym
= rvalue
->symtree
->n
.sym
;
4372 if (sym
->ts
.type
== BT_CLASS
&& sym
->attr
.class_ok
)
4373 target
= CLASS_DATA (sym
)->attr
.target
;
4375 target
= sym
->attr
.target
;
4377 if (!target
&& !proc_pointer
)
4379 gfc_error ("Pointer assignment target in initialization expression "
4380 "does not have the TARGET attribute at %L",
4385 for (ref
= rvalue
->ref
; ref
; ref
= ref
->next
)
4390 for (int n
= 0; n
< ref
->u
.ar
.dimen
; n
++)
4391 if (!gfc_is_constant_expr (ref
->u
.ar
.start
[n
])
4392 || !gfc_is_constant_expr (ref
->u
.ar
.end
[n
])
4393 || !gfc_is_constant_expr (ref
->u
.ar
.stride
[n
]))
4395 gfc_error ("Every subscript of target specification "
4396 "at %L must be a constant expression",
4403 if (!gfc_is_constant_expr (ref
->u
.ss
.start
)
4404 || !gfc_is_constant_expr (ref
->u
.ss
.end
))
4406 gfc_error ("Substring starting and ending points of target "
4407 "specification at %L must be constant expressions",
4408 &ref
->u
.ss
.start
->where
);
4420 if (!attr
.target
&& !attr
.pointer
)
4422 gfc_error ("Pointer assignment target is neither TARGET "
4423 "nor POINTER at %L", &rvalue
->where
);
4428 if (lvalue
->ts
.type
== BT_CHARACTER
)
4430 bool t
= gfc_check_same_strlen (lvalue
, rvalue
, "pointer assignment");
4435 if (is_pure
&& gfc_impure_variable (rvalue
->symtree
->n
.sym
))
4437 gfc_error ("Bad target in pointer assignment in PURE "
4438 "procedure at %L", &rvalue
->where
);
4441 if (is_implicit_pure
&& gfc_impure_variable (rvalue
->symtree
->n
.sym
))
4442 gfc_unset_implicit_pure (gfc_current_ns
->proc_name
);
4444 if (gfc_has_vector_index (rvalue
))
4446 gfc_error ("Pointer assignment with vector subscript "
4447 "on rhs at %L", &rvalue
->where
);
4451 if (attr
.is_protected
&& attr
.use_assoc
4452 && !(attr
.pointer
|| attr
.proc_pointer
))
4454 gfc_error ("Pointer assignment target has PROTECTED "
4455 "attribute at %L", &rvalue
->where
);
4459 /* F2008, C725. For PURE also C1283. */
4460 if (rvalue
->expr_type
== EXPR_VARIABLE
4461 && gfc_is_coindexed (rvalue
))
4464 for (ref
= rvalue
->ref
; ref
; ref
= ref
->next
)
4465 if (ref
->type
== REF_ARRAY
&& ref
->u
.ar
.codimen
)
4467 gfc_error ("Data target at %L shall not have a coindex",
4473 /* Warn for assignments of contiguous pointers to targets which is not
4474 contiguous. Be lenient in the definition of what counts as
4477 if (lhs_attr
.contiguous
4478 && lhs_attr
.dimension
> 0)
4480 if (gfc_is_not_contiguous (rvalue
))
4482 gfc_error ("Assignment to contiguous pointer from "
4483 "non-contiguous target at %L", &rvalue
->where
);
4486 if (!gfc_is_simply_contiguous (rvalue
, false, true))
4487 gfc_warning (OPT_Wextra
, "Assignment to contiguous pointer from "
4488 "non-contiguous target at %L", &rvalue
->where
);
4491 /* Warn if it is the LHS pointer may lives longer than the RHS target. */
4492 if (warn_target_lifetime
4493 && rvalue
->expr_type
== EXPR_VARIABLE
4494 && !rvalue
->symtree
->n
.sym
->attr
.save
4495 && !rvalue
->symtree
->n
.sym
->attr
.pointer
&& !attr
.pointer
4496 && !rvalue
->symtree
->n
.sym
->attr
.host_assoc
4497 && !rvalue
->symtree
->n
.sym
->attr
.in_common
4498 && !rvalue
->symtree
->n
.sym
->attr
.use_assoc
4499 && !rvalue
->symtree
->n
.sym
->attr
.dummy
)
4504 warn
= lvalue
->symtree
->n
.sym
->attr
.dummy
4505 || lvalue
->symtree
->n
.sym
->attr
.result
4506 || lvalue
->symtree
->n
.sym
->attr
.function
4507 || (lvalue
->symtree
->n
.sym
->attr
.host_assoc
4508 && lvalue
->symtree
->n
.sym
->ns
4509 != rvalue
->symtree
->n
.sym
->ns
)
4510 || lvalue
->symtree
->n
.sym
->attr
.use_assoc
4511 || lvalue
->symtree
->n
.sym
->attr
.in_common
;
4513 if (rvalue
->symtree
->n
.sym
->ns
->proc_name
4514 && rvalue
->symtree
->n
.sym
->ns
->proc_name
->attr
.flavor
!= FL_PROCEDURE
4515 && rvalue
->symtree
->n
.sym
->ns
->proc_name
->attr
.flavor
!= FL_PROGRAM
)
4516 for (ns
= rvalue
->symtree
->n
.sym
->ns
;
4517 ns
&& ns
->proc_name
&& ns
->proc_name
->attr
.flavor
!= FL_PROCEDURE
;
4519 if (ns
->parent
== lvalue
->symtree
->n
.sym
->ns
)
4526 gfc_warning (OPT_Wtarget_lifetime
,
4527 "Pointer at %L in pointer assignment might outlive the "
4528 "pointer target", &lvalue
->where
);
4535 /* Relative of gfc_check_assign() except that the lvalue is a single
4536 symbol. Used for initialization assignments. */
4539 gfc_check_assign_symbol (gfc_symbol
*sym
, gfc_component
*comp
, gfc_expr
*rvalue
)
4543 bool pointer
, proc_pointer
;
4545 memset (&lvalue
, '\0', sizeof (gfc_expr
));
4547 lvalue
.expr_type
= EXPR_VARIABLE
;
4548 lvalue
.ts
= sym
->ts
;
4550 lvalue
.rank
= sym
->as
->rank
;
4551 lvalue
.symtree
= XCNEW (gfc_symtree
);
4552 lvalue
.symtree
->n
.sym
= sym
;
4553 lvalue
.where
= sym
->declared_at
;
4557 lvalue
.ref
= gfc_get_ref ();
4558 lvalue
.ref
->type
= REF_COMPONENT
;
4559 lvalue
.ref
->u
.c
.component
= comp
;
4560 lvalue
.ref
->u
.c
.sym
= sym
;
4561 lvalue
.ts
= comp
->ts
;
4562 lvalue
.rank
= comp
->as
? comp
->as
->rank
: 0;
4563 lvalue
.where
= comp
->loc
;
4564 pointer
= comp
->ts
.type
== BT_CLASS
&& CLASS_DATA (comp
)
4565 ? CLASS_DATA (comp
)->attr
.class_pointer
: comp
->attr
.pointer
;
4566 proc_pointer
= comp
->attr
.proc_pointer
;
4570 pointer
= sym
->ts
.type
== BT_CLASS
&& CLASS_DATA (sym
)
4571 ? CLASS_DATA (sym
)->attr
.class_pointer
: sym
->attr
.pointer
;
4572 proc_pointer
= sym
->attr
.proc_pointer
;
4575 if (pointer
|| proc_pointer
)
4576 r
= gfc_check_pointer_assign (&lvalue
, rvalue
, false, true);
4579 /* If a conversion function, e.g., __convert_i8_i4, was inserted
4580 into an array constructor, we should check if it can be reduced
4581 as an initialization expression. */
4582 if (rvalue
->expr_type
== EXPR_FUNCTION
4583 && rvalue
->value
.function
.isym
4584 && (rvalue
->value
.function
.isym
->conversion
== 1))
4585 gfc_check_init_expr (rvalue
);
4587 r
= gfc_check_assign (&lvalue
, rvalue
, 1);
4590 free (lvalue
.symtree
);
4596 if (pointer
&& rvalue
->expr_type
!= EXPR_NULL
&& !proc_pointer
)
4598 /* F08:C461. Additional checks for pointer initialization. */
4599 symbol_attribute attr
;
4600 attr
= gfc_expr_attr (rvalue
);
4601 if (attr
.allocatable
)
4603 gfc_error ("Pointer initialization target at %L "
4604 "must not be ALLOCATABLE", &rvalue
->where
);
4607 if (!attr
.target
|| attr
.pointer
)
4609 gfc_error ("Pointer initialization target at %L "
4610 "must have the TARGET attribute", &rvalue
->where
);
4614 if (!attr
.save
&& rvalue
->expr_type
== EXPR_VARIABLE
4615 && rvalue
->symtree
->n
.sym
->ns
->proc_name
4616 && rvalue
->symtree
->n
.sym
->ns
->proc_name
->attr
.is_main_program
)
4618 rvalue
->symtree
->n
.sym
->ns
->proc_name
->attr
.save
= SAVE_IMPLICIT
;
4619 attr
.save
= SAVE_IMPLICIT
;
4624 gfc_error ("Pointer initialization target at %L "
4625 "must have the SAVE attribute", &rvalue
->where
);
4630 if (proc_pointer
&& rvalue
->expr_type
!= EXPR_NULL
)
4632 /* F08:C1220. Additional checks for procedure pointer initialization. */
4633 symbol_attribute attr
= gfc_expr_attr (rvalue
);
4634 if (attr
.proc_pointer
)
4636 gfc_error ("Procedure pointer initialization target at %L "
4637 "may not be a procedure pointer", &rvalue
->where
);
4640 if (attr
.proc
== PROC_INTERNAL
)
4642 gfc_error ("Internal procedure %qs is invalid in "
4643 "procedure pointer initialization at %L",
4644 rvalue
->symtree
->name
, &rvalue
->where
);
4649 gfc_error ("Dummy procedure %qs is invalid in "
4650 "procedure pointer initialization at %L",
4651 rvalue
->symtree
->name
, &rvalue
->where
);
4659 /* Build an initializer for a local integer, real, complex, logical, or
4660 character variable, based on the command line flags finit-local-zero,
4661 finit-integer=, finit-real=, finit-logical=, and finit-character=.
4662 With force, an initializer is ALWAYS generated. */
4665 gfc_build_init_expr (gfc_typespec
*ts
, locus
*where
, bool force
)
4667 gfc_expr
*init_expr
;
4669 /* Try to build an initializer expression. */
4670 init_expr
= gfc_get_constant_expr (ts
->type
, ts
->kind
, where
);
4672 /* If we want to force generation, make sure we default to zero. */
4673 gfc_init_local_real init_real
= flag_init_real
;
4674 int init_logical
= gfc_option
.flag_init_logical
;
4677 if (init_real
== GFC_INIT_REAL_OFF
)
4678 init_real
= GFC_INIT_REAL_ZERO
;
4679 if (init_logical
== GFC_INIT_LOGICAL_OFF
)
4680 init_logical
= GFC_INIT_LOGICAL_FALSE
;
4683 /* We will only initialize integers, reals, complex, logicals, and
4684 characters, and only if the corresponding command-line flags
4685 were set. Otherwise, we free init_expr and return null. */
4689 if (force
|| gfc_option
.flag_init_integer
!= GFC_INIT_INTEGER_OFF
)
4690 mpz_set_si (init_expr
->value
.integer
,
4691 gfc_option
.flag_init_integer_value
);
4694 gfc_free_expr (init_expr
);
4702 case GFC_INIT_REAL_SNAN
:
4703 init_expr
->is_snan
= 1;
4705 case GFC_INIT_REAL_NAN
:
4706 mpfr_set_nan (init_expr
->value
.real
);
4709 case GFC_INIT_REAL_INF
:
4710 mpfr_set_inf (init_expr
->value
.real
, 1);
4713 case GFC_INIT_REAL_NEG_INF
:
4714 mpfr_set_inf (init_expr
->value
.real
, -1);
4717 case GFC_INIT_REAL_ZERO
:
4718 mpfr_set_ui (init_expr
->value
.real
, 0.0, GFC_RND_MODE
);
4722 gfc_free_expr (init_expr
);
4731 case GFC_INIT_REAL_SNAN
:
4732 init_expr
->is_snan
= 1;
4734 case GFC_INIT_REAL_NAN
:
4735 mpfr_set_nan (mpc_realref (init_expr
->value
.complex));
4736 mpfr_set_nan (mpc_imagref (init_expr
->value
.complex));
4739 case GFC_INIT_REAL_INF
:
4740 mpfr_set_inf (mpc_realref (init_expr
->value
.complex), 1);
4741 mpfr_set_inf (mpc_imagref (init_expr
->value
.complex), 1);
4744 case GFC_INIT_REAL_NEG_INF
:
4745 mpfr_set_inf (mpc_realref (init_expr
->value
.complex), -1);
4746 mpfr_set_inf (mpc_imagref (init_expr
->value
.complex), -1);
4749 case GFC_INIT_REAL_ZERO
:
4750 mpc_set_ui (init_expr
->value
.complex, 0, GFC_MPC_RND_MODE
);
4754 gfc_free_expr (init_expr
);
4761 if (init_logical
== GFC_INIT_LOGICAL_FALSE
)
4762 init_expr
->value
.logical
= 0;
4763 else if (init_logical
== GFC_INIT_LOGICAL_TRUE
)
4764 init_expr
->value
.logical
= 1;
4767 gfc_free_expr (init_expr
);
4773 /* For characters, the length must be constant in order to
4774 create a default initializer. */
4775 if ((force
|| gfc_option
.flag_init_character
== GFC_INIT_CHARACTER_ON
)
4777 && ts
->u
.cl
->length
->expr_type
== EXPR_CONSTANT
)
4779 HOST_WIDE_INT char_len
= gfc_mpz_get_hwi (ts
->u
.cl
->length
->value
.integer
);
4780 init_expr
->value
.character
.length
= char_len
;
4781 init_expr
->value
.character
.string
= gfc_get_wide_string (char_len
+1);
4782 for (size_t i
= 0; i
< (size_t) char_len
; i
++)
4783 init_expr
->value
.character
.string
[i
]
4784 = (unsigned char) gfc_option
.flag_init_character_value
;
4788 gfc_free_expr (init_expr
);
4792 && (force
|| gfc_option
.flag_init_character
== GFC_INIT_CHARACTER_ON
)
4793 && ts
->u
.cl
->length
&& flag_max_stack_var_size
!= 0)
4795 gfc_actual_arglist
*arg
;
4796 init_expr
= gfc_get_expr ();
4797 init_expr
->where
= *where
;
4798 init_expr
->ts
= *ts
;
4799 init_expr
->expr_type
= EXPR_FUNCTION
;
4800 init_expr
->value
.function
.isym
=
4801 gfc_intrinsic_function_by_id (GFC_ISYM_REPEAT
);
4802 init_expr
->value
.function
.name
= "repeat";
4803 arg
= gfc_get_actual_arglist ();
4804 arg
->expr
= gfc_get_character_expr (ts
->kind
, where
, NULL
, 1);
4805 arg
->expr
->value
.character
.string
[0] =
4806 gfc_option
.flag_init_character_value
;
4807 arg
->next
= gfc_get_actual_arglist ();
4808 arg
->next
->expr
= gfc_copy_expr (ts
->u
.cl
->length
);
4809 init_expr
->value
.function
.actual
= arg
;
4814 gfc_free_expr (init_expr
);
4821 /* Invoke gfc_build_init_expr to create an initializer expression, but do not
4822 * require that an expression be built. */
4825 gfc_build_default_init_expr (gfc_typespec
*ts
, locus
*where
)
4827 return gfc_build_init_expr (ts
, where
, false);
4830 /* Apply an initialization expression to a typespec. Can be used for symbols or
4831 components. Similar to add_init_expr_to_sym in decl.cc; could probably be
4832 combined with some effort. */
4835 gfc_apply_init (gfc_typespec
*ts
, symbol_attribute
*attr
, gfc_expr
*init
)
4837 if (ts
->type
== BT_CHARACTER
&& !attr
->pointer
&& init
4840 && ts
->u
.cl
->length
->expr_type
== EXPR_CONSTANT
4841 && ts
->u
.cl
->length
->ts
.type
== BT_INTEGER
)
4843 HOST_WIDE_INT len
= gfc_mpz_get_hwi (ts
->u
.cl
->length
->value
.integer
);
4845 if (init
->expr_type
== EXPR_CONSTANT
)
4846 gfc_set_constant_character_len (len
, init
, -1);
4848 && init
->ts
.type
== BT_CHARACTER
4849 && init
->ts
.u
.cl
&& init
->ts
.u
.cl
->length
4850 && mpz_cmp (ts
->u
.cl
->length
->value
.integer
,
4851 init
->ts
.u
.cl
->length
->value
.integer
))
4853 gfc_constructor
*ctor
;
4854 ctor
= gfc_constructor_first (init
->value
.constructor
);
4858 bool has_ts
= (init
->ts
.u
.cl
4859 && init
->ts
.u
.cl
->length_from_typespec
);
4861 /* Remember the length of the first element for checking
4862 that all elements *in the constructor* have the same
4863 length. This need not be the length of the LHS! */
4864 gcc_assert (ctor
->expr
->expr_type
== EXPR_CONSTANT
);
4865 gcc_assert (ctor
->expr
->ts
.type
== BT_CHARACTER
);
4866 gfc_charlen_t first_len
= ctor
->expr
->value
.character
.length
;
4868 for ( ; ctor
; ctor
= gfc_constructor_next (ctor
))
4869 if (ctor
->expr
->expr_type
== EXPR_CONSTANT
)
4871 gfc_set_constant_character_len (len
, ctor
->expr
,
4872 has_ts
? -1 : first_len
);
4873 if (!ctor
->expr
->ts
.u
.cl
)
4875 = gfc_new_charlen (gfc_current_ns
, ts
->u
.cl
);
4877 ctor
->expr
->ts
.u
.cl
->length
4878 = gfc_copy_expr (ts
->u
.cl
->length
);
4886 /* Check whether an expression is a structure constructor and whether it has
4887 other values than NULL. */
4890 is_non_empty_structure_constructor (gfc_expr
* e
)
4892 if (e
->expr_type
!= EXPR_STRUCTURE
)
4895 gfc_constructor
*cons
= gfc_constructor_first (e
->value
.constructor
);
4898 if (!cons
->expr
|| cons
->expr
->expr_type
!= EXPR_NULL
)
4900 cons
= gfc_constructor_next (cons
);
4906 /* Check for default initializer; sym->value is not enough
4907 as it is also set for EXPR_NULL of allocatables. */
4910 gfc_has_default_initializer (gfc_symbol
*der
)
4914 gcc_assert (gfc_fl_struct (der
->attr
.flavor
));
4915 for (c
= der
->components
; c
; c
= c
->next
)
4916 if (gfc_bt_struct (c
->ts
.type
))
4918 if (!c
->attr
.pointer
&& !c
->attr
.proc_pointer
4919 && !(c
->attr
.allocatable
&& der
== c
->ts
.u
.derived
)
4921 && is_non_empty_structure_constructor (c
->initializer
))
4922 || gfc_has_default_initializer (c
->ts
.u
.derived
)))
4924 if (c
->attr
.pointer
&& c
->initializer
)
4938 Generate an initializer expression which initializes the entirety of a union.
4939 A normal structure constructor is insufficient without undue effort, because
4940 components of maps may be oddly aligned/overlapped. (For example if a
4941 character is initialized from one map overtop a real from the other, only one
4942 byte of the real is actually initialized.) Unfortunately we don't know the
4943 size of the union right now, so we can't generate a proper initializer, but
4944 we use a NULL expr as a placeholder and do the right thing later in
4945 gfc_trans_subcomponent_assign.
4948 generate_union_initializer (gfc_component
*un
)
4950 if (un
== NULL
|| un
->ts
.type
!= BT_UNION
)
4953 gfc_expr
*placeholder
= gfc_get_null_expr (&un
->loc
);
4954 placeholder
->ts
= un
->ts
;
4959 /* Get the user-specified initializer for a union, if any. This means the user
4960 has said to initialize component(s) of a map. For simplicity's sake we
4961 only allow the user to initialize the first map. We don't have to worry
4962 about overlapping initializers as they are released early in resolution (see
4963 resolve_fl_struct). */
4966 get_union_initializer (gfc_symbol
*union_type
, gfc_component
**map_p
)
4969 gfc_expr
*init
=NULL
;
4971 if (!union_type
|| union_type
->attr
.flavor
!= FL_UNION
)
4974 for (map
= union_type
->components
; map
; map
= map
->next
)
4976 if (gfc_has_default_initializer (map
->ts
.u
.derived
))
4978 init
= gfc_default_initializer (&map
->ts
);
4992 class_allocatable (gfc_component
*comp
)
4994 return comp
->ts
.type
== BT_CLASS
&& CLASS_DATA (comp
)
4995 && CLASS_DATA (comp
)->attr
.allocatable
;
4999 class_pointer (gfc_component
*comp
)
5001 return comp
->ts
.type
== BT_CLASS
&& CLASS_DATA (comp
)
5002 && CLASS_DATA (comp
)->attr
.pointer
;
5006 comp_allocatable (gfc_component
*comp
)
5008 return comp
->attr
.allocatable
|| class_allocatable (comp
);
5012 comp_pointer (gfc_component
*comp
)
5014 return comp
->attr
.pointer
5015 || comp
->attr
.proc_pointer
5016 || comp
->attr
.class_pointer
5017 || class_pointer (comp
);
5020 /* Fetch or generate an initializer for the given component.
5021 Only generate an initializer if generate is true. */
5024 component_initializer (gfc_component
*c
, bool generate
)
5026 gfc_expr
*init
= NULL
;
5028 /* Allocatable components always get EXPR_NULL.
5029 Pointer components are only initialized when generating, and only if they
5030 do not already have an initializer. */
5031 if (comp_allocatable (c
) || (generate
&& comp_pointer (c
) && !c
->initializer
))
5033 init
= gfc_get_null_expr (&c
->loc
);
5038 /* See if we can find the initializer immediately. */
5039 if (c
->initializer
|| !generate
)
5040 return c
->initializer
;
5042 /* Recursively handle derived type components. */
5043 else if (c
->ts
.type
== BT_DERIVED
|| c
->ts
.type
== BT_CLASS
)
5044 init
= gfc_generate_initializer (&c
->ts
, true);
5046 else if (c
->ts
.type
== BT_UNION
&& c
->ts
.u
.derived
->components
)
5048 gfc_component
*map
= NULL
;
5049 gfc_constructor
*ctor
;
5050 gfc_expr
*user_init
;
5052 /* If we don't have a user initializer and we aren't generating one, this
5053 union has no initializer. */
5054 user_init
= get_union_initializer (c
->ts
.u
.derived
, &map
);
5055 if (!user_init
&& !generate
)
5058 /* Otherwise use a structure constructor. */
5059 init
= gfc_get_structure_constructor_expr (c
->ts
.type
, c
->ts
.kind
,
5063 /* If we are to generate an initializer for the union, add a constructor
5064 which initializes the whole union first. */
5067 ctor
= gfc_constructor_get ();
5068 ctor
->expr
= generate_union_initializer (c
);
5069 gfc_constructor_append (&init
->value
.constructor
, ctor
);
5072 /* If we found an initializer in one of our maps, apply it. Note this
5073 is applied _after_ the entire-union initializer above if any. */
5076 ctor
= gfc_constructor_get ();
5077 ctor
->expr
= user_init
;
5078 ctor
->n
.component
= map
;
5079 gfc_constructor_append (&init
->value
.constructor
, ctor
);
5083 /* Treat simple components like locals. */
5086 /* We MUST give an initializer, so force generation. */
5087 init
= gfc_build_init_expr (&c
->ts
, &c
->loc
, true);
5088 gfc_apply_init (&c
->ts
, &c
->attr
, init
);
5095 /* Get an expression for a default initializer of a derived type. */
5098 gfc_default_initializer (gfc_typespec
*ts
)
5100 return gfc_generate_initializer (ts
, false);
5103 /* Generate an initializer expression for an iso_c_binding type
5104 such as c_[fun]ptr. The appropriate initializer is c_null_[fun]ptr. */
5107 generate_isocbinding_initializer (gfc_symbol
*derived
)
5109 /* The initializers have already been built into the c_null_[fun]ptr symbols
5110 from gen_special_c_interop_ptr. */
5111 gfc_symtree
*npsym
= NULL
;
5112 if (0 == strcmp (derived
->name
, "c_ptr"))
5113 gfc_find_sym_tree ("c_null_ptr", gfc_current_ns
, true, &npsym
);
5114 else if (0 == strcmp (derived
->name
, "c_funptr"))
5115 gfc_find_sym_tree ("c_null_funptr", gfc_current_ns
, true, &npsym
);
5117 gfc_internal_error ("generate_isocbinding_initializer(): bad iso_c_binding"
5118 " type, expected %<c_ptr%> or %<c_funptr%>");
5121 gfc_expr
*init
= gfc_copy_expr (npsym
->n
.sym
->value
);
5122 init
->symtree
= npsym
;
5123 init
->ts
.is_iso_c
= true;
5130 /* Get or generate an expression for a default initializer of a derived type.
5131 If -finit-derived is specified, generate default initialization expressions
5132 for components that lack them when generate is set. */
5135 gfc_generate_initializer (gfc_typespec
*ts
, bool generate
)
5137 gfc_expr
*init
, *tmp
;
5138 gfc_component
*comp
;
5140 generate
= flag_init_derived
&& generate
;
5142 if (ts
->u
.derived
->ts
.is_iso_c
&& generate
)
5143 return generate_isocbinding_initializer (ts
->u
.derived
);
5145 /* See if we have a default initializer in this, but not in nested
5146 types (otherwise we could use gfc_has_default_initializer()).
5147 We don't need to check if we are going to generate them. */
5148 comp
= ts
->u
.derived
->components
;
5151 for (; comp
; comp
= comp
->next
)
5152 if (comp
->initializer
|| comp_allocatable (comp
))
5159 init
= gfc_get_structure_constructor_expr (ts
->type
, ts
->kind
,
5160 &ts
->u
.derived
->declared_at
);
5163 for (comp
= ts
->u
.derived
->components
; comp
; comp
= comp
->next
)
5165 gfc_constructor
*ctor
= gfc_constructor_get();
5167 /* Fetch or generate an initializer for the component. */
5168 tmp
= component_initializer (comp
, generate
);
5171 /* Save the component ref for STRUCTUREs and UNIONs. */
5172 if (ts
->u
.derived
->attr
.flavor
== FL_STRUCT
5173 || ts
->u
.derived
->attr
.flavor
== FL_UNION
)
5174 ctor
->n
.component
= comp
;
5176 /* If the initializer was not generated, we need a copy. */
5177 ctor
->expr
= comp
->initializer
? gfc_copy_expr (tmp
) : tmp
;
5178 if ((comp
->ts
.type
!= tmp
->ts
.type
|| comp
->ts
.kind
!= tmp
->ts
.kind
)
5179 && !comp
->attr
.pointer
&& !comp
->attr
.proc_pointer
)
5182 val
= gfc_convert_type_warn (ctor
->expr
, &comp
->ts
, 1, false);
5188 gfc_constructor_append (&init
->value
.constructor
, ctor
);
5195 /* Given a symbol, create an expression node with that symbol as a
5196 variable. If the symbol is array valued, setup a reference of the
5200 gfc_get_variable_expr (gfc_symtree
*var
)
5204 e
= gfc_get_expr ();
5205 e
->expr_type
= EXPR_VARIABLE
;
5207 e
->ts
= var
->n
.sym
->ts
;
5209 if (var
->n
.sym
->attr
.flavor
!= FL_PROCEDURE
5210 && ((var
->n
.sym
->as
!= NULL
&& var
->n
.sym
->ts
.type
!= BT_CLASS
)
5211 || (var
->n
.sym
->ts
.type
== BT_CLASS
&& var
->n
.sym
->ts
.u
.derived
5212 && CLASS_DATA (var
->n
.sym
)
5213 && CLASS_DATA (var
->n
.sym
)->as
)))
5215 e
->rank
= var
->n
.sym
->ts
.type
== BT_CLASS
5216 ? CLASS_DATA (var
->n
.sym
)->as
->rank
: var
->n
.sym
->as
->rank
;
5217 e
->ref
= gfc_get_ref ();
5218 e
->ref
->type
= REF_ARRAY
;
5219 e
->ref
->u
.ar
.type
= AR_FULL
;
5220 e
->ref
->u
.ar
.as
= gfc_copy_array_spec (var
->n
.sym
->ts
.type
== BT_CLASS
5221 ? CLASS_DATA (var
->n
.sym
)->as
5229 /* Adds a full array reference to an expression, as needed. */
5232 gfc_add_full_array_ref (gfc_expr
*e
, gfc_array_spec
*as
)
5235 for (ref
= e
->ref
; ref
; ref
= ref
->next
)
5240 ref
->next
= gfc_get_ref ();
5245 e
->ref
= gfc_get_ref ();
5248 ref
->type
= REF_ARRAY
;
5249 ref
->u
.ar
.type
= AR_FULL
;
5250 ref
->u
.ar
.dimen
= e
->rank
;
5251 ref
->u
.ar
.where
= e
->where
;
5257 gfc_lval_expr_from_sym (gfc_symbol
*sym
)
5261 lval
= gfc_get_expr ();
5262 lval
->expr_type
= EXPR_VARIABLE
;
5263 lval
->where
= sym
->declared_at
;
5265 lval
->symtree
= gfc_find_symtree (sym
->ns
->sym_root
, sym
->name
);
5267 /* It will always be a full array. */
5268 as
= IS_CLASS_ARRAY (sym
) ? CLASS_DATA (sym
)->as
: sym
->as
;
5269 lval
->rank
= as
? as
->rank
: 0;
5271 gfc_add_full_array_ref (lval
, as
);
5276 /* Returns the array_spec of a full array expression. A NULL is
5277 returned otherwise. */
5279 gfc_get_full_arrayspec_from_expr (gfc_expr
*expr
)
5284 if (expr
->rank
== 0)
5287 /* Follow any component references. */
5288 if (expr
->expr_type
== EXPR_VARIABLE
5289 || expr
->expr_type
== EXPR_CONSTANT
)
5292 as
= expr
->symtree
->n
.sym
->as
;
5296 for (ref
= expr
->ref
; ref
; ref
= ref
->next
)
5301 as
= ref
->u
.c
.component
->as
;
5310 switch (ref
->u
.ar
.type
)
5333 /* General expression traversal function. */
5336 gfc_traverse_expr (gfc_expr
*expr
, gfc_symbol
*sym
,
5337 bool (*func
)(gfc_expr
*, gfc_symbol
*, int*),
5342 gfc_actual_arglist
*args
;
5349 if ((*func
) (expr
, sym
, &f
))
5352 if (expr
->ts
.type
== BT_CHARACTER
5354 && expr
->ts
.u
.cl
->length
5355 && expr
->ts
.u
.cl
->length
->expr_type
!= EXPR_CONSTANT
5356 && gfc_traverse_expr (expr
->ts
.u
.cl
->length
, sym
, func
, f
))
5359 switch (expr
->expr_type
)
5364 for (args
= expr
->value
.function
.actual
; args
; args
= args
->next
)
5366 if (gfc_traverse_expr (args
->expr
, sym
, func
, f
))
5374 case EXPR_SUBSTRING
:
5377 case EXPR_STRUCTURE
:
5379 for (c
= gfc_constructor_first (expr
->value
.constructor
);
5380 c
; c
= gfc_constructor_next (c
))
5382 if (gfc_traverse_expr (c
->expr
, sym
, func
, f
))
5386 if (gfc_traverse_expr (c
->iterator
->var
, sym
, func
, f
))
5388 if (gfc_traverse_expr (c
->iterator
->start
, sym
, func
, f
))
5390 if (gfc_traverse_expr (c
->iterator
->end
, sym
, func
, f
))
5392 if (gfc_traverse_expr (c
->iterator
->step
, sym
, func
, f
))
5399 if (gfc_traverse_expr (expr
->value
.op
.op1
, sym
, func
, f
))
5401 if (gfc_traverse_expr (expr
->value
.op
.op2
, sym
, func
, f
))
5417 for (i
= 0; i
< GFC_MAX_DIMENSIONS
; i
++)
5419 if (gfc_traverse_expr (ar
.start
[i
], sym
, func
, f
))
5421 if (gfc_traverse_expr (ar
.end
[i
], sym
, func
, f
))
5423 if (gfc_traverse_expr (ar
.stride
[i
], sym
, func
, f
))
5429 if (gfc_traverse_expr (ref
->u
.ss
.start
, sym
, func
, f
))
5431 if (gfc_traverse_expr (ref
->u
.ss
.end
, sym
, func
, f
))
5436 if (ref
->u
.c
.component
->ts
.type
== BT_CHARACTER
5437 && ref
->u
.c
.component
->ts
.u
.cl
5438 && ref
->u
.c
.component
->ts
.u
.cl
->length
5439 && ref
->u
.c
.component
->ts
.u
.cl
->length
->expr_type
5441 && gfc_traverse_expr (ref
->u
.c
.component
->ts
.u
.cl
->length
,
5445 if (ref
->u
.c
.component
->as
)
5446 for (i
= 0; i
< ref
->u
.c
.component
->as
->rank
5447 + ref
->u
.c
.component
->as
->corank
; i
++)
5449 if (gfc_traverse_expr (ref
->u
.c
.component
->as
->lower
[i
],
5452 if (gfc_traverse_expr (ref
->u
.c
.component
->as
->upper
[i
],
5469 /* Traverse expr, marking all EXPR_VARIABLE symbols referenced. */
5472 expr_set_symbols_referenced (gfc_expr
*expr
,
5473 gfc_symbol
*sym ATTRIBUTE_UNUSED
,
5474 int *f ATTRIBUTE_UNUSED
)
5476 if (expr
->expr_type
!= EXPR_VARIABLE
)
5478 gfc_set_sym_referenced (expr
->symtree
->n
.sym
);
5483 gfc_expr_set_symbols_referenced (gfc_expr
*expr
)
5485 gfc_traverse_expr (expr
, NULL
, expr_set_symbols_referenced
, 0);
5489 /* Determine if an expression is a procedure pointer component and return
5490 the component in that case. Otherwise return NULL. */
5493 gfc_get_proc_ptr_comp (gfc_expr
*expr
)
5497 if (!expr
|| !expr
->ref
)
5504 if (ref
->type
== REF_COMPONENT
5505 && ref
->u
.c
.component
->attr
.proc_pointer
)
5506 return ref
->u
.c
.component
;
5512 /* Determine if an expression is a procedure pointer component. */
5515 gfc_is_proc_ptr_comp (gfc_expr
*expr
)
5517 return (gfc_get_proc_ptr_comp (expr
) != NULL
);
5521 /* Determine if an expression is a function with an allocatable class scalar
5524 gfc_is_alloc_class_scalar_function (gfc_expr
*expr
)
5526 if (expr
->expr_type
== EXPR_FUNCTION
5527 && expr
->value
.function
.esym
5528 && expr
->value
.function
.esym
->result
5529 && expr
->value
.function
.esym
->result
->ts
.type
== BT_CLASS
5530 && !CLASS_DATA (expr
->value
.function
.esym
->result
)->attr
.dimension
5531 && CLASS_DATA (expr
->value
.function
.esym
->result
)->attr
.allocatable
)
5538 /* Determine if an expression is a function with an allocatable class array
5541 gfc_is_class_array_function (gfc_expr
*expr
)
5543 if (expr
->expr_type
== EXPR_FUNCTION
5544 && expr
->value
.function
.esym
5545 && expr
->value
.function
.esym
->result
5546 && expr
->value
.function
.esym
->result
->ts
.type
== BT_CLASS
5547 && CLASS_DATA (expr
->value
.function
.esym
->result
)->attr
.dimension
5548 && (CLASS_DATA (expr
->value
.function
.esym
->result
)->attr
.allocatable
5549 || CLASS_DATA (expr
->value
.function
.esym
->result
)->attr
.pointer
))
5556 /* Walk an expression tree and check each variable encountered for being typed.
5557 If strict is not set, a top-level variable is tolerated untyped in -std=gnu
5558 mode as is a basic arithmetic expression using those; this is for things in
5561 INTEGER :: arr(n), n
5562 INTEGER :: arr(n + 1), n
5564 The namespace is needed for IMPLICIT typing. */
5566 static gfc_namespace
* check_typed_ns
;
5569 expr_check_typed_help (gfc_expr
* e
, gfc_symbol
* sym ATTRIBUTE_UNUSED
,
5570 int* f ATTRIBUTE_UNUSED
)
5574 if (e
->expr_type
!= EXPR_VARIABLE
)
5577 gcc_assert (e
->symtree
);
5578 t
= gfc_check_symbol_typed (e
->symtree
->n
.sym
, check_typed_ns
,
5585 gfc_expr_check_typed (gfc_expr
* e
, gfc_namespace
* ns
, bool strict
)
5589 /* If this is a top-level variable or EXPR_OP, do the check with strict given
5593 if (e
->expr_type
== EXPR_VARIABLE
&& !e
->ref
)
5594 return gfc_check_symbol_typed (e
->symtree
->n
.sym
, ns
, strict
, e
->where
);
5596 if (e
->expr_type
== EXPR_OP
)
5600 gcc_assert (e
->value
.op
.op1
);
5601 t
= gfc_expr_check_typed (e
->value
.op
.op1
, ns
, strict
);
5603 if (t
&& e
->value
.op
.op2
)
5604 t
= gfc_expr_check_typed (e
->value
.op
.op2
, ns
, strict
);
5610 /* Otherwise, walk the expression and do it strictly. */
5611 check_typed_ns
= ns
;
5612 error_found
= gfc_traverse_expr (e
, NULL
, &expr_check_typed_help
, 0);
5614 return error_found
? false : true;
5618 /* This function returns true if it contains any references to PDT KIND
5619 or LEN parameters. */
5622 derived_parameter_expr (gfc_expr
* e
, gfc_symbol
* sym ATTRIBUTE_UNUSED
,
5623 int* f ATTRIBUTE_UNUSED
)
5625 if (e
->expr_type
!= EXPR_VARIABLE
)
5628 gcc_assert (e
->symtree
);
5629 if (e
->symtree
->n
.sym
->attr
.pdt_kind
5630 || e
->symtree
->n
.sym
->attr
.pdt_len
)
5638 gfc_derived_parameter_expr (gfc_expr
*e
)
5640 return gfc_traverse_expr (e
, NULL
, &derived_parameter_expr
, 0);
5644 /* This function returns the overall type of a type parameter spec list.
5645 If all the specs are explicit, SPEC_EXPLICIT is returned. If any of the
5646 parameters are assumed/deferred then SPEC_ASSUMED/DEFERRED is returned
5647 unless derived is not NULL. In this latter case, all the LEN parameters
5648 must be either assumed or deferred for the return argument to be set to
5649 anything other than SPEC_EXPLICIT. */
5652 gfc_spec_list_type (gfc_actual_arglist
*param_list
, gfc_symbol
*derived
)
5654 gfc_param_spec_type res
= SPEC_EXPLICIT
;
5656 bool seen_assumed
= false;
5657 bool seen_deferred
= false;
5659 if (derived
== NULL
)
5661 for (; param_list
; param_list
= param_list
->next
)
5662 if (param_list
->spec_type
== SPEC_ASSUMED
5663 || param_list
->spec_type
== SPEC_DEFERRED
)
5664 return param_list
->spec_type
;
5668 for (; param_list
; param_list
= param_list
->next
)
5670 c
= gfc_find_component (derived
, param_list
->name
,
5672 gcc_assert (c
!= NULL
);
5673 if (c
->attr
.pdt_kind
)
5675 else if (param_list
->spec_type
== SPEC_EXPLICIT
)
5676 return SPEC_EXPLICIT
;
5677 seen_assumed
= param_list
->spec_type
== SPEC_ASSUMED
;
5678 seen_deferred
= param_list
->spec_type
== SPEC_DEFERRED
;
5679 if (seen_assumed
&& seen_deferred
)
5680 return SPEC_EXPLICIT
;
5682 res
= seen_assumed
? SPEC_ASSUMED
: SPEC_DEFERRED
;
5689 gfc_ref_this_image (gfc_ref
*ref
)
5693 gcc_assert (ref
->type
== REF_ARRAY
&& ref
->u
.ar
.codimen
> 0);
5695 for (n
= ref
->u
.ar
.dimen
; n
< ref
->u
.ar
.dimen
+ ref
->u
.ar
.codimen
; n
++)
5696 if (ref
->u
.ar
.dimen_type
[n
] != DIMEN_THIS_IMAGE
)
5703 gfc_find_team_co (gfc_expr
*e
)
5707 for (ref
= e
->ref
; ref
; ref
= ref
->next
)
5708 if (ref
->type
== REF_ARRAY
&& ref
->u
.ar
.codimen
> 0)
5709 return ref
->u
.ar
.team
;
5711 if (e
->value
.function
.actual
->expr
)
5712 for (ref
= e
->value
.function
.actual
->expr
->ref
; ref
;
5714 if (ref
->type
== REF_ARRAY
&& ref
->u
.ar
.codimen
> 0)
5715 return ref
->u
.ar
.team
;
5721 gfc_find_stat_co (gfc_expr
*e
)
5725 for (ref
= e
->ref
; ref
; ref
= ref
->next
)
5726 if (ref
->type
== REF_ARRAY
&& ref
->u
.ar
.codimen
> 0)
5727 return ref
->u
.ar
.stat
;
5729 if (e
->value
.function
.actual
->expr
)
5730 for (ref
= e
->value
.function
.actual
->expr
->ref
; ref
;
5732 if (ref
->type
== REF_ARRAY
&& ref
->u
.ar
.codimen
> 0)
5733 return ref
->u
.ar
.stat
;
5739 gfc_is_coindexed (gfc_expr
*e
)
5743 for (ref
= e
->ref
; ref
; ref
= ref
->next
)
5744 if (ref
->type
== REF_ARRAY
&& ref
->u
.ar
.codimen
> 0)
5745 return !gfc_ref_this_image (ref
);
5751 /* Coarrays are variables with a corank but not being coindexed. However, also
5752 the following is a coarray: A subobject of a coarray is a coarray if it does
5753 not have any cosubscripts, vector subscripts, allocatable component
5754 selection, or pointer component selection. (F2008, 2.4.7) */
5757 gfc_is_coarray (gfc_expr
*e
)
5761 gfc_component
*comp
;
5766 if (e
->expr_type
!= EXPR_VARIABLE
)
5770 sym
= e
->symtree
->n
.sym
;
5772 if (sym
->ts
.type
== BT_CLASS
&& sym
->attr
.class_ok
)
5773 coarray
= CLASS_DATA (sym
)->attr
.codimension
;
5775 coarray
= sym
->attr
.codimension
;
5777 for (ref
= e
->ref
; ref
; ref
= ref
->next
)
5781 comp
= ref
->u
.c
.component
;
5782 if (comp
->ts
.type
== BT_CLASS
&& comp
->attr
.class_ok
5783 && (CLASS_DATA (comp
)->attr
.class_pointer
5784 || CLASS_DATA (comp
)->attr
.allocatable
))
5787 coarray
= CLASS_DATA (comp
)->attr
.codimension
;
5789 else if (comp
->attr
.pointer
|| comp
->attr
.allocatable
)
5792 coarray
= comp
->attr
.codimension
;
5800 if (ref
->u
.ar
.codimen
> 0 && !gfc_ref_this_image (ref
))
5806 for (i
= 0; i
< ref
->u
.ar
.dimen
; i
++)
5807 if (ref
->u
.ar
.dimen_type
[i
] == DIMEN_VECTOR
)
5819 return coarray
&& !coindexed
;
5824 gfc_get_corank (gfc_expr
*e
)
5829 if (!gfc_is_coarray (e
))
5832 if (e
->ts
.type
== BT_CLASS
&& e
->ts
.u
.derived
->components
)
5833 corank
= e
->ts
.u
.derived
->components
->as
5834 ? e
->ts
.u
.derived
->components
->as
->corank
: 0;
5836 corank
= e
->symtree
->n
.sym
->as
? e
->symtree
->n
.sym
->as
->corank
: 0;
5838 for (ref
= e
->ref
; ref
; ref
= ref
->next
)
5840 if (ref
->type
== REF_ARRAY
)
5841 corank
= ref
->u
.ar
.as
->corank
;
5842 gcc_assert (ref
->type
!= REF_SUBSTRING
);
5849 /* Check whether the expression has an ultimate allocatable component.
5850 Being itself allocatable does not count. */
5852 gfc_has_ultimate_allocatable (gfc_expr
*e
)
5854 gfc_ref
*ref
, *last
= NULL
;
5856 if (e
->expr_type
!= EXPR_VARIABLE
)
5859 for (ref
= e
->ref
; ref
; ref
= ref
->next
)
5860 if (ref
->type
== REF_COMPONENT
)
5863 if (last
&& last
->u
.c
.component
->ts
.type
== BT_CLASS
)
5864 return CLASS_DATA (last
->u
.c
.component
)->attr
.alloc_comp
;
5865 else if (last
&& last
->u
.c
.component
->ts
.type
== BT_DERIVED
)
5866 return last
->u
.c
.component
->ts
.u
.derived
->attr
.alloc_comp
;
5870 if (e
->ts
.type
== BT_CLASS
)
5871 return CLASS_DATA (e
)->attr
.alloc_comp
;
5872 else if (e
->ts
.type
== BT_DERIVED
)
5873 return e
->ts
.u
.derived
->attr
.alloc_comp
;
5879 /* Check whether the expression has an pointer component.
5880 Being itself a pointer does not count. */
5882 gfc_has_ultimate_pointer (gfc_expr
*e
)
5884 gfc_ref
*ref
, *last
= NULL
;
5886 if (e
->expr_type
!= EXPR_VARIABLE
)
5889 for (ref
= e
->ref
; ref
; ref
= ref
->next
)
5890 if (ref
->type
== REF_COMPONENT
)
5893 if (last
&& last
->u
.c
.component
->ts
.type
== BT_CLASS
)
5894 return CLASS_DATA (last
->u
.c
.component
)->attr
.pointer_comp
;
5895 else if (last
&& last
->u
.c
.component
->ts
.type
== BT_DERIVED
)
5896 return last
->u
.c
.component
->ts
.u
.derived
->attr
.pointer_comp
;
5900 if (e
->ts
.type
== BT_CLASS
)
5901 return CLASS_DATA (e
)->attr
.pointer_comp
;
5902 else if (e
->ts
.type
== BT_DERIVED
)
5903 return e
->ts
.u
.derived
->attr
.pointer_comp
;
5909 /* Check whether an expression is "simply contiguous", cf. F2008, 6.5.4.
5910 Note: A scalar is not regarded as "simply contiguous" by the standard.
5911 if bool is not strict, some further checks are done - for instance,
5912 a "(::1)" is accepted. */
5915 gfc_is_simply_contiguous (gfc_expr
*expr
, bool strict
, bool permit_element
)
5919 gfc_array_ref
*ar
= NULL
;
5920 gfc_ref
*ref
, *part_ref
= NULL
;
5923 if (expr
->expr_type
== EXPR_ARRAY
)
5926 if (expr
->expr_type
== EXPR_FUNCTION
)
5928 if (expr
->value
.function
.isym
)
5929 /* TRANSPOSE is the only intrinsic that may return a
5930 non-contiguous array. It's treated as a special case in
5931 gfc_conv_expr_descriptor too. */
5932 return (expr
->value
.function
.isym
->id
!= GFC_ISYM_TRANSPOSE
);
5933 else if (expr
->value
.function
.esym
)
5934 /* Only a pointer to an array without the contiguous attribute
5935 can be non-contiguous as a result value. */
5936 return (expr
->value
.function
.esym
->result
->attr
.contiguous
5937 || !expr
->value
.function
.esym
->result
->attr
.pointer
);
5940 /* Type-bound procedures. */
5941 gfc_symbol
*s
= expr
->symtree
->n
.sym
;
5942 if (s
->ts
.type
!= BT_CLASS
&& s
->ts
.type
!= BT_DERIVED
)
5946 for (gfc_ref
*r
= expr
->ref
; r
; r
= r
->next
)
5947 if (r
->type
== REF_COMPONENT
)
5950 if (rc
== NULL
|| rc
->u
.c
.component
== NULL
5951 || rc
->u
.c
.component
->ts
.interface
== NULL
)
5954 return rc
->u
.c
.component
->ts
.interface
->attr
.contiguous
;
5957 else if (expr
->expr_type
!= EXPR_VARIABLE
)
5960 if (!permit_element
&& expr
->rank
== 0)
5963 for (ref
= expr
->ref
; ref
; ref
= ref
->next
)
5966 return false; /* Array shall be last part-ref. */
5968 if (ref
->type
== REF_COMPONENT
)
5970 else if (ref
->type
== REF_SUBSTRING
)
5972 else if (ref
->type
== REF_INQUIRY
)
5974 else if (ref
->u
.ar
.type
!= AR_ELEMENT
)
5978 sym
= expr
->symtree
->n
.sym
;
5979 if (expr
->ts
.type
!= BT_CLASS
5981 && !part_ref
->u
.c
.component
->attr
.contiguous
5982 && part_ref
->u
.c
.component
->attr
.pointer
)
5984 && !sym
->attr
.contiguous
5985 && (sym
->attr
.pointer
5986 || (sym
->as
&& sym
->as
->type
== AS_ASSUMED_RANK
)
5987 || (sym
->as
&& sym
->as
->type
== AS_ASSUMED_SHAPE
)))))
5990 if (!ar
|| ar
->type
== AR_FULL
)
5993 gcc_assert (ar
->type
== AR_SECTION
);
5995 /* Check for simply contiguous array */
5997 for (i
= 0; i
< ar
->dimen
; i
++)
5999 if (ar
->dimen_type
[i
] == DIMEN_VECTOR
)
6002 if (ar
->dimen_type
[i
] == DIMEN_ELEMENT
)
6008 gcc_assert (ar
->dimen_type
[i
] == DIMEN_RANGE
);
6011 /* If the previous section was not contiguous, that's an error,
6012 unless we have effective only one element and checking is not
6014 if (!colon
&& (strict
|| !ar
->start
[i
] || !ar
->end
[i
]
6015 || ar
->start
[i
]->expr_type
!= EXPR_CONSTANT
6016 || ar
->end
[i
]->expr_type
!= EXPR_CONSTANT
6017 || mpz_cmp (ar
->start
[i
]->value
.integer
,
6018 ar
->end
[i
]->value
.integer
) != 0))
6021 /* Following the standard, "(::1)" or - if known at compile time -
6022 "(lbound:ubound)" are not simply contiguous; if strict
6023 is false, they are regarded as simply contiguous. */
6024 if (ar
->stride
[i
] && (strict
|| ar
->stride
[i
]->expr_type
!= EXPR_CONSTANT
6025 || ar
->stride
[i
]->ts
.type
!= BT_INTEGER
6026 || mpz_cmp_si (ar
->stride
[i
]->value
.integer
, 1) != 0))
6030 && (strict
|| ar
->start
[i
]->expr_type
!= EXPR_CONSTANT
6031 || !ar
->as
->lower
[i
]
6032 || ar
->as
->lower
[i
]->expr_type
!= EXPR_CONSTANT
6033 || mpz_cmp (ar
->start
[i
]->value
.integer
,
6034 ar
->as
->lower
[i
]->value
.integer
) != 0))
6038 && (strict
|| ar
->end
[i
]->expr_type
!= EXPR_CONSTANT
6039 || !ar
->as
->upper
[i
]
6040 || ar
->as
->upper
[i
]->expr_type
!= EXPR_CONSTANT
6041 || mpz_cmp (ar
->end
[i
]->value
.integer
,
6042 ar
->as
->upper
[i
]->value
.integer
) != 0))
6049 /* Return true if the expression is guaranteed to be non-contiguous,
6050 false if we cannot prove anything. It is probably best to call
6051 this after gfc_is_simply_contiguous. If neither of them returns
6052 true, we cannot say (at compile-time). */
6055 gfc_is_not_contiguous (gfc_expr
*array
)
6058 gfc_array_ref
*ar
= NULL
;
6060 bool previous_incomplete
;
6062 for (ref
= array
->ref
; ref
; ref
= ref
->next
)
6064 /* Array-ref shall be last ref. */
6066 if (ar
&& ar
->type
!= AR_ELEMENT
)
6069 if (ref
->type
== REF_ARRAY
)
6073 if (ar
== NULL
|| ar
->type
!= AR_SECTION
)
6076 previous_incomplete
= false;
6078 /* Check if we can prove that the array is not contiguous. */
6080 for (i
= 0; i
< ar
->dimen
; i
++)
6082 mpz_t arr_size
, ref_size
;
6084 if (gfc_ref_dimen_size (ar
, i
, &ref_size
, NULL
))
6086 if (gfc_dep_difference (ar
->as
->upper
[i
], ar
->as
->lower
[i
], &arr_size
))
6088 /* a(2:4,2:) is known to be non-contiguous, but
6089 a(2:4,i:i) can be contiguous. */
6090 mpz_add_ui (arr_size
, arr_size
, 1L);
6091 if (previous_incomplete
&& mpz_cmp_si (ref_size
, 1) != 0)
6093 mpz_clear (arr_size
);
6094 mpz_clear (ref_size
);
6097 else if (mpz_cmp (arr_size
, ref_size
) != 0)
6098 previous_incomplete
= true;
6100 mpz_clear (arr_size
);
6103 /* Check for a(::2), i.e. where the stride is not unity.
6104 This is only done if there is more than one element in
6105 the reference along this dimension. */
6107 if (mpz_cmp_ui (ref_size
, 1) > 0 && ar
->type
== AR_SECTION
6108 && ar
->dimen_type
[i
] == DIMEN_RANGE
6109 && ar
->stride
[i
] && ar
->stride
[i
]->expr_type
== EXPR_CONSTANT
6110 && mpz_cmp_si (ar
->stride
[i
]->value
.integer
, 1) != 0)
6112 mpz_clear (ref_size
);
6116 mpz_clear (ref_size
);
6119 /* We didn't find anything definitive. */
6123 /* Build call to an intrinsic procedure. The number of arguments has to be
6124 passed (rather than ending the list with a NULL value) because we may
6125 want to add arguments but with a NULL-expression. */
6128 gfc_build_intrinsic_call (gfc_namespace
*ns
, gfc_isym_id id
, const char* name
,
6129 locus where
, unsigned numarg
, ...)
6132 gfc_actual_arglist
* atail
;
6133 gfc_intrinsic_sym
* isym
;
6136 const char *mangled_name
= gfc_get_string (GFC_PREFIX ("%s"), name
);
6138 isym
= gfc_intrinsic_function_by_id (id
);
6141 result
= gfc_get_expr ();
6142 result
->expr_type
= EXPR_FUNCTION
;
6143 result
->ts
= isym
->ts
;
6144 result
->where
= where
;
6145 result
->value
.function
.name
= mangled_name
;
6146 result
->value
.function
.isym
= isym
;
6148 gfc_get_sym_tree (mangled_name
, ns
, &result
->symtree
, false);
6149 gfc_commit_symbol (result
->symtree
->n
.sym
);
6150 gcc_assert (result
->symtree
6151 && (result
->symtree
->n
.sym
->attr
.flavor
== FL_PROCEDURE
6152 || result
->symtree
->n
.sym
->attr
.flavor
== FL_UNKNOWN
));
6153 result
->symtree
->n
.sym
->intmod_sym_id
= id
;
6154 result
->symtree
->n
.sym
->attr
.flavor
= FL_PROCEDURE
;
6155 result
->symtree
->n
.sym
->attr
.intrinsic
= 1;
6156 result
->symtree
->n
.sym
->attr
.artificial
= 1;
6158 va_start (ap
, numarg
);
6160 for (i
= 0; i
< numarg
; ++i
)
6164 atail
->next
= gfc_get_actual_arglist ();
6165 atail
= atail
->next
;
6168 atail
= result
->value
.function
.actual
= gfc_get_actual_arglist ();
6170 atail
->expr
= va_arg (ap
, gfc_expr
*);
6178 /* Check if an expression may appear in a variable definition context
6179 (F2008, 16.6.7) or pointer association context (F2008, 16.6.8).
6180 This is called from the various places when resolving
6181 the pieces that make up such a context.
6182 If own_scope is true (applies to, e.g., ac-implied-do/data-implied-do
6183 variables), some checks are not performed.
6185 Optionally, a possible error message can be suppressed if context is NULL
6186 and just the return status (true / false) be requested. */
6189 gfc_check_vardef_context (gfc_expr
* e
, bool pointer
, bool alloc_obj
,
6190 bool own_scope
, const char* context
)
6192 gfc_symbol
* sym
= NULL
;
6194 bool check_intentin
;
6196 symbol_attribute attr
;
6200 if (e
->expr_type
== EXPR_VARIABLE
)
6202 gcc_assert (e
->symtree
);
6203 sym
= e
->symtree
->n
.sym
;
6205 else if (e
->expr_type
== EXPR_FUNCTION
)
6207 gcc_assert (e
->symtree
);
6208 sym
= e
->value
.function
.esym
? e
->value
.function
.esym
: e
->symtree
->n
.sym
;
6211 attr
= gfc_expr_attr (e
);
6212 if (!pointer
&& e
->expr_type
== EXPR_FUNCTION
&& attr
.pointer
)
6214 if (!(gfc_option
.allow_std
& GFC_STD_F2008
))
6217 gfc_error ("Fortran 2008: Pointer functions in variable definition"
6218 " context (%s) at %L", context
, &e
->where
);
6222 else if (e
->expr_type
!= EXPR_VARIABLE
)
6225 gfc_error ("Non-variable expression in variable definition context (%s)"
6226 " at %L", context
, &e
->where
);
6230 if (!pointer
&& sym
->attr
.flavor
== FL_PARAMETER
)
6233 gfc_error ("Named constant %qs in variable definition context (%s)"
6234 " at %L", sym
->name
, context
, &e
->where
);
6237 if (!pointer
&& sym
->attr
.flavor
!= FL_VARIABLE
6238 && !(sym
->attr
.flavor
== FL_PROCEDURE
&& sym
== sym
->result
)
6239 && !(sym
->attr
.flavor
== FL_PROCEDURE
&& sym
->attr
.proc_pointer
)
6240 && !(sym
->attr
.flavor
== FL_PROCEDURE
6241 && sym
->attr
.function
&& sym
->attr
.pointer
))
6244 gfc_error ("%qs in variable definition context (%s) at %L is not"
6245 " a variable", sym
->name
, context
, &e
->where
);
6249 /* Find out whether the expr is a pointer; this also means following
6250 component references to the last one. */
6251 is_pointer
= (attr
.pointer
|| attr
.proc_pointer
);
6252 if (pointer
&& !is_pointer
)
6255 gfc_error ("Non-POINTER in pointer association context (%s)"
6256 " at %L", context
, &e
->where
);
6260 if (e
->ts
.type
== BT_DERIVED
6261 && e
->ts
.u
.derived
== NULL
)
6264 gfc_error ("Type inaccessible in variable definition context (%s) "
6265 "at %L", context
, &e
->where
);
6272 || (e
->ts
.type
== BT_DERIVED
6273 && e
->ts
.u
.derived
->from_intmod
== INTMOD_ISO_FORTRAN_ENV
6274 && e
->ts
.u
.derived
->intmod_sym_id
== ISOFORTRAN_LOCK_TYPE
)))
6277 gfc_error ("LOCK_TYPE in variable definition context (%s) at %L",
6278 context
, &e
->where
);
6282 /* TS18508, C702/C203. */
6285 || (e
->ts
.type
== BT_DERIVED
6286 && e
->ts
.u
.derived
->from_intmod
== INTMOD_ISO_FORTRAN_ENV
6287 && e
->ts
.u
.derived
->intmod_sym_id
== ISOFORTRAN_EVENT_TYPE
)))
6290 gfc_error ("LOCK_EVENT in variable definition context (%s) at %L",
6291 context
, &e
->where
);
6295 /* INTENT(IN) dummy argument. Check this, unless the object itself is the
6296 component of sub-component of a pointer; we need to distinguish
6297 assignment to a pointer component from pointer-assignment to a pointer
6298 component. Note that (normal) assignment to procedure pointers is not
6300 check_intentin
= !own_scope
;
6301 ptr_component
= (sym
->ts
.type
== BT_CLASS
&& sym
->ts
.u
.derived
6302 && CLASS_DATA (sym
))
6303 ? CLASS_DATA (sym
)->attr
.class_pointer
: sym
->attr
.pointer
;
6304 for (ref
= e
->ref
; ref
&& check_intentin
; ref
= ref
->next
)
6306 if (ptr_component
&& ref
->type
== REF_COMPONENT
)
6307 check_intentin
= false;
6308 if (ref
->type
== REF_COMPONENT
)
6310 gfc_component
*comp
= ref
->u
.c
.component
;
6311 ptr_component
= (comp
->ts
.type
== BT_CLASS
&& comp
->attr
.class_ok
)
6312 ? CLASS_DATA (comp
)->attr
.class_pointer
6313 : comp
->attr
.pointer
;
6314 if (ptr_component
&& !pointer
)
6315 check_intentin
= false;
6317 if (ref
->type
== REF_INQUIRY
6318 && (ref
->u
.i
== INQUIRY_KIND
|| ref
->u
.i
== INQUIRY_LEN
))
6321 gfc_error ("%qs parameter inquiry for %qs in "
6322 "variable definition context (%s) at %L",
6323 ref
->u
.i
== INQUIRY_KIND
? "KIND" : "LEN",
6324 sym
->name
, context
, &e
->where
);
6330 && (sym
->attr
.intent
== INTENT_IN
6331 || (sym
->attr
.select_type_temporary
&& sym
->assoc
6332 && sym
->assoc
->target
&& sym
->assoc
->target
->symtree
6333 && sym
->assoc
->target
->symtree
->n
.sym
->attr
.intent
== INTENT_IN
)))
6335 if (pointer
&& is_pointer
)
6338 gfc_error ("Dummy argument %qs with INTENT(IN) in pointer"
6339 " association context (%s) at %L",
6340 sym
->name
, context
, &e
->where
);
6343 if (!pointer
&& !is_pointer
&& !sym
->attr
.pointer
)
6345 const char *name
= sym
->attr
.select_type_temporary
6346 ? sym
->assoc
->target
->symtree
->name
: sym
->name
;
6348 gfc_error ("Dummy argument %qs with INTENT(IN) in variable"
6349 " definition context (%s) at %L",
6350 name
, context
, &e
->where
);
6355 /* PROTECTED and use-associated. */
6356 if (sym
->attr
.is_protected
&& sym
->attr
.use_assoc
&& check_intentin
)
6358 if (pointer
&& is_pointer
)
6361 gfc_error ("Variable %qs is PROTECTED and cannot appear in a"
6362 " pointer association context (%s) at %L",
6363 sym
->name
, context
, &e
->where
);
6366 if (!pointer
&& !is_pointer
)
6369 gfc_error ("Variable %qs is PROTECTED and cannot appear in a"
6370 " variable definition context (%s) at %L",
6371 sym
->name
, context
, &e
->where
);
6376 /* Variable not assignable from a PURE procedure but appears in
6377 variable definition context. */
6378 own_scope
= own_scope
6379 || (sym
->attr
.result
&& sym
->ns
->proc_name
6380 && sym
== sym
->ns
->proc_name
->result
);
6381 if (!pointer
&& !own_scope
&& gfc_pure (NULL
) && gfc_impure_variable (sym
))
6384 gfc_error ("Variable %qs cannot appear in a variable definition"
6385 " context (%s) at %L in PURE procedure",
6386 sym
->name
, context
, &e
->where
);
6390 if (!pointer
&& context
&& gfc_implicit_pure (NULL
)
6391 && gfc_impure_variable (sym
))
6396 for (ns
= gfc_current_ns
; ns
; ns
= ns
->parent
)
6398 sym
= ns
->proc_name
;
6401 if (sym
->attr
.flavor
== FL_PROCEDURE
)
6403 sym
->attr
.implicit_pure
= 0;
6408 /* Check variable definition context for associate-names. */
6409 if (!pointer
&& sym
->assoc
&& !sym
->attr
.select_rank_temporary
)
6412 gfc_association_list
* assoc
;
6414 gcc_assert (sym
->assoc
->target
);
6416 /* If this is a SELECT TYPE temporary (the association is used internally
6417 for SELECT TYPE), silently go over to the target. */
6418 if (sym
->attr
.select_type_temporary
)
6420 gfc_expr
* t
= sym
->assoc
->target
;
6422 gcc_assert (t
->expr_type
== EXPR_VARIABLE
);
6423 name
= t
->symtree
->name
;
6425 if (t
->symtree
->n
.sym
->assoc
)
6426 assoc
= t
->symtree
->n
.sym
->assoc
;
6435 gcc_assert (name
&& assoc
);
6437 /* Is association to a valid variable? */
6438 if (!assoc
->variable
)
6442 if (assoc
->target
->expr_type
== EXPR_VARIABLE
)
6443 gfc_error ("%qs at %L associated to vector-indexed target"
6444 " cannot be used in a variable definition"
6446 name
, &e
->where
, context
);
6448 gfc_error ("%qs at %L associated to expression"
6449 " cannot be used in a variable definition"
6451 name
, &e
->where
, context
);
6456 /* Target must be allowed to appear in a variable definition context. */
6457 if (!gfc_check_vardef_context (assoc
->target
, pointer
, false, false, NULL
))
6460 gfc_error ("Associate-name %qs cannot appear in a variable"
6461 " definition context (%s) at %L because its target"
6462 " at %L cannot, either",
6463 name
, context
, &e
->where
,
6464 &assoc
->target
->where
);
6469 /* Check for same value in vector expression subscript. */
6472 for (ref
= e
->ref
; ref
!= NULL
; ref
= ref
->next
)
6473 if (ref
->type
== REF_ARRAY
&& ref
->u
.ar
.type
== AR_SECTION
)
6474 for (i
= 0; i
< GFC_MAX_DIMENSIONS
6475 && ref
->u
.ar
.dimen_type
[i
] != 0; i
++)
6476 if (ref
->u
.ar
.dimen_type
[i
] == DIMEN_VECTOR
)
6478 gfc_expr
*arr
= ref
->u
.ar
.start
[i
];
6479 if (arr
->expr_type
== EXPR_ARRAY
)
6481 gfc_constructor
*c
, *n
;
6484 for (c
= gfc_constructor_first (arr
->value
.constructor
);
6485 c
!= NULL
; c
= gfc_constructor_next (c
))
6487 if (c
== NULL
|| c
->iterator
!= NULL
)
6492 for (n
= gfc_constructor_next (c
); n
!= NULL
;
6493 n
= gfc_constructor_next (n
))
6495 if (n
->iterator
!= NULL
)
6499 if (gfc_dep_compare_expr (ec
, en
) == 0)
6502 gfc_error_now ("Elements with the same value "
6503 "at %L and %L in vector "
6504 "subscript in a variable "
6505 "definition context (%s)",
6506 &(ec
->where
), &(en
->where
),