2 Copyright (C) 2000-2025 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/>. */
21 /* Since target arithmetic must be done on the host, there has to
22 be some way of evaluating arithmetic expressions as the host
23 would evaluate them. We use the GNU MP library and the MPFR
24 library to do arithmetic, and this file provides the interface. */
28 #include "coretypes.h"
32 #include "target-memory.h"
33 #include "constructor.h"
37 /* MPFR does not have a direct replacement for mpz_set_f() from GMP.
38 It's easily implemented with a few calls though. */
41 gfc_mpfr_to_mpz (mpz_t z
, mpfr_t x
, locus
*where
)
45 if (mpfr_inf_p (x
) || mpfr_nan_p (x
))
47 gfc_error ("Conversion of an Infinity or Not-a-Number at %L "
53 e
= mpfr_get_z_exp (z
, x
);
56 mpz_mul_2exp (z
, z
, e
);
58 mpz_tdiv_q_2exp (z
, z
, -e
);
61 /* Reduce an unsigned number to within its range. */
64 gfc_reduce_unsigned (gfc_expr
*e
)
67 gcc_checking_assert (e
->expr_type
== EXPR_CONSTANT
68 && e
->ts
.type
== BT_UNSIGNED
);
69 k
= gfc_validate_kind (BT_UNSIGNED
, e
->ts
.kind
, false);
70 mpz_and (e
->value
.integer
, e
->value
.integer
, gfc_unsigned_kinds
[k
].huge
);
72 /* Set the model number precision by the requested KIND. */
75 gfc_set_model_kind (int kind
)
77 int index
= gfc_validate_kind (BT_REAL
, kind
, false);
80 base2prec
= gfc_real_kinds
[index
].digits
;
81 if (gfc_real_kinds
[index
].radix
!= 2)
82 base2prec
*= gfc_real_kinds
[index
].radix
/ 2;
83 mpfr_set_default_prec (base2prec
);
87 /* Set the model number precision from mpfr_t x. */
90 gfc_set_model (mpfr_t x
)
92 mpfr_set_default_prec (mpfr_get_prec (x
));
96 /* Given an arithmetic error code, return a pointer to a string that
97 explains the error. */
100 gfc_arith_error (arith code
)
107 p
= G_("Arithmetic OK at %L");
110 p
= G_("Arithmetic overflow at %L");
112 case ARITH_UNDERFLOW
:
113 p
= G_("Arithmetic underflow at %L");
116 p
= G_("Arithmetic NaN at %L");
119 p
= G_("Division by zero at %L");
121 case ARITH_INCOMMENSURATE
:
122 p
= G_("Array operands are incommensurate at %L");
124 case ARITH_ASYMMETRIC
:
125 p
= G_("Integer outside symmetric range implied by Standard Fortran"
128 case ARITH_WRONGCONCAT
:
129 p
= G_("Illegal type in character concatenation at %L");
131 case ARITH_INVALID_TYPE
:
132 p
= G_("Invalid type in arithmetic operation at %L");
134 case ARITH_UNSIGNED_TRUNCATED
:
135 p
= G_("Unsigned constant truncated at %L");
137 case ARITH_UNSIGNED_NEGATIVE
:
138 p
= G_("Negation of unsigned constant at %L not permitted");
141 gfc_internal_error ("gfc_arith_error(): Bad error code");
148 /* Check if a certain arithmetic error code is severe enough to prevent
149 further simplification, as opposed to errors thrown by the range check
150 (e.g. overflow) or arithmetic exceptions that are tolerated with
154 is_hard_arith_error (arith code
)
160 case ARITH_UNDERFLOW
:
163 case ARITH_ASYMMETRIC
:
172 /* Get things ready to do math. */
175 gfc_arith_init_1 (void)
177 gfc_integer_info
*int_info
;
178 gfc_unsigned_info
*uint_info
;
179 gfc_real_info
*real_info
;
183 mpfr_set_default_prec (128);
186 /* Convert the minimum and maximum values for each kind into their
187 GNU MP representation. */
188 for (int_info
= gfc_integer_kinds
; int_info
->kind
!= 0; int_info
++)
191 mpz_init (int_info
->huge
);
192 mpz_set_ui (int_info
->huge
, int_info
->radix
);
193 mpz_pow_ui (int_info
->huge
, int_info
->huge
, int_info
->digits
);
194 mpz_sub_ui (int_info
->huge
, int_info
->huge
, 1);
196 /* These are the numbers that are actually representable by the
197 target. For bases other than two, this needs to be changed. */
198 if (int_info
->radix
!= 2)
199 gfc_internal_error ("Fix min_int calculation");
201 /* See PRs 13490 and 17912, related to integer ranges.
202 The pedantic_min_int exists for range checking when a program
203 is compiled with -pedantic, and reflects the belief that
204 Standard Fortran requires integers to be symmetrical, i.e.
205 every negative integer must have a representable positive
206 absolute value, and vice versa. */
208 mpz_init (int_info
->pedantic_min_int
);
209 mpz_neg (int_info
->pedantic_min_int
, int_info
->huge
);
211 mpz_init (int_info
->min_int
);
212 mpz_sub_ui (int_info
->min_int
, int_info
->pedantic_min_int
, 1);
215 mpfr_set_z (a
, int_info
->huge
, GFC_RND_MODE
);
216 mpfr_log10 (a
, a
, GFC_RND_MODE
);
218 int_info
->range
= (int) mpfr_get_si (a
, GFC_RND_MODE
);
221 /* Similar, for UNSIGNED. */
224 for (uint_info
= gfc_unsigned_kinds
; uint_info
->kind
!= 0; uint_info
++)
226 /* UNSIGNED is radix 2. */
227 gcc_assert (uint_info
->radix
== 2);
229 mpz_init (uint_info
->huge
);
230 mpz_set_ui (uint_info
->huge
, 2);
231 mpz_pow_ui (uint_info
->huge
, uint_info
->huge
, uint_info
->digits
);
232 mpz_sub_ui (uint_info
->huge
, uint_info
->huge
, 1);
234 /* int_min - the smallest number we can reasonably convert from. */
236 mpz_init (uint_info
->int_min
);
237 mpz_set_ui (uint_info
->int_min
, 2);
238 mpz_pow_ui (uint_info
->int_min
, uint_info
->int_min
,
239 uint_info
->digits
- 1);
240 mpz_neg (uint_info
->int_min
, uint_info
->int_min
);
243 mpfr_set_z (a
, uint_info
->huge
, GFC_RND_MODE
);
244 mpfr_log10 (a
, a
, GFC_RND_MODE
);
246 uint_info
->range
= (int) mpfr_get_si (a
, GFC_RND_MODE
);
253 for (real_info
= gfc_real_kinds
; real_info
->kind
!= 0; real_info
++)
255 gfc_set_model_kind (real_info
->kind
);
260 /* huge(x) = (1 - b**(-p)) * b**(emax-1) * b */
262 mpfr_init (real_info
->huge
);
263 mpfr_set_ui (real_info
->huge
, 1, GFC_RND_MODE
);
264 mpfr_set_ui (a
, real_info
->radix
, GFC_RND_MODE
);
265 mpfr_pow_si (a
, a
, -real_info
->digits
, GFC_RND_MODE
);
266 mpfr_sub (real_info
->huge
, real_info
->huge
, a
, GFC_RND_MODE
);
269 mpfr_set_ui (a
, real_info
->radix
, GFC_RND_MODE
);
270 mpfr_pow_ui (a
, a
, real_info
->max_exponent
- 1, GFC_RND_MODE
);
272 /* (1 - b**(-p)) * b**(emax-1) */
273 mpfr_mul (real_info
->huge
, real_info
->huge
, a
, GFC_RND_MODE
);
275 /* (1 - b**(-p)) * b**(emax-1) * b */
276 mpfr_mul_ui (real_info
->huge
, real_info
->huge
, real_info
->radix
,
279 /* tiny(x) = b**(emin-1) */
280 mpfr_init (real_info
->tiny
);
281 mpfr_set_ui (real_info
->tiny
, real_info
->radix
, GFC_RND_MODE
);
282 mpfr_pow_si (real_info
->tiny
, real_info
->tiny
,
283 real_info
->min_exponent
- 1, GFC_RND_MODE
);
285 /* subnormal (x) = b**(emin - digit) */
286 mpfr_init (real_info
->subnormal
);
287 mpfr_set_ui (real_info
->subnormal
, real_info
->radix
, GFC_RND_MODE
);
288 mpfr_pow_si (real_info
->subnormal
, real_info
->subnormal
,
289 real_info
->min_exponent
- real_info
->digits
, GFC_RND_MODE
);
291 /* epsilon(x) = b**(1-p) */
292 mpfr_init (real_info
->epsilon
);
293 mpfr_set_ui (real_info
->epsilon
, real_info
->radix
, GFC_RND_MODE
);
294 mpfr_pow_si (real_info
->epsilon
, real_info
->epsilon
,
295 1 - real_info
->digits
, GFC_RND_MODE
);
297 /* range(x) = int(min(log10(huge(x)), -log10(tiny)) */
298 mpfr_log10 (a
, real_info
->huge
, GFC_RND_MODE
);
299 mpfr_log10 (b
, real_info
->tiny
, GFC_RND_MODE
);
300 mpfr_neg (b
, b
, GFC_RND_MODE
);
303 mpfr_min (a
, a
, b
, GFC_RND_MODE
);
305 real_info
->range
= (int) mpfr_get_si (a
, GFC_RND_MODE
);
307 /* precision(x) = int((p - 1) * log10(b)) + k */
308 mpfr_set_ui (a
, real_info
->radix
, GFC_RND_MODE
);
309 mpfr_log10 (a
, a
, GFC_RND_MODE
);
310 mpfr_mul_ui (a
, a
, real_info
->digits
- 1, GFC_RND_MODE
);
312 real_info
->precision
= (int) mpfr_get_si (a
, GFC_RND_MODE
);
314 /* If the radix is an integral power of 10, add one to the precision. */
315 for (i
= 10; i
<= real_info
->radix
; i
*= 10)
316 if (i
== real_info
->radix
)
317 real_info
->precision
++;
319 mpfr_clears (a
, b
, NULL
);
324 /* Clean up, get rid of numeric constants. */
327 gfc_arith_done_1 (void)
329 gfc_integer_info
*ip
;
332 for (ip
= gfc_integer_kinds
; ip
->kind
; ip
++)
334 mpz_clear (ip
->min_int
);
335 mpz_clear (ip
->pedantic_min_int
);
336 mpz_clear (ip
->huge
);
339 for (rp
= gfc_real_kinds
; rp
->kind
; rp
++)
340 mpfr_clears (rp
->epsilon
, rp
->huge
, rp
->tiny
, rp
->subnormal
, NULL
);
346 /* Given a wide character value and a character kind, determine whether
347 the character is representable for that kind. */
349 gfc_check_character_range (gfc_char_t c
, int kind
)
351 /* As wide characters are stored as 32-bit values, they're all
352 representable in UCS=4. */
357 return c
<= 255 ? true : false;
363 /* Given an integer and a kind, make sure that the integer lies within
364 the range of the kind. Returns ARITH_OK, ARITH_ASYMMETRIC or
368 gfc_check_integer_range (mpz_t p
, int kind
)
373 i
= gfc_validate_kind (BT_INTEGER
, kind
, false);
378 if (mpz_cmp (p
, gfc_integer_kinds
[i
].pedantic_min_int
) < 0)
379 result
= ARITH_ASYMMETRIC
;
383 if (flag_range_check
== 0)
386 if (mpz_cmp (p
, gfc_integer_kinds
[i
].min_int
) < 0
387 || mpz_cmp (p
, gfc_integer_kinds
[i
].huge
) > 0)
388 result
= ARITH_OVERFLOW
;
395 gfc_check_unsigned_range (mpz_t p
, int kind
)
399 i
= gfc_validate_kind (BT_UNSIGNED
, kind
, false);
401 if (pedantic
&& mpz_cmp_si (p
, 0) < 0)
402 return ARITH_UNSIGNED_NEGATIVE
;
404 if (mpz_cmp (p
, gfc_unsigned_kinds
[i
].int_min
) < 0)
405 return ARITH_UNSIGNED_TRUNCATED
;
407 if (mpz_cmp (p
, gfc_unsigned_kinds
[i
].huge
) > 0)
408 return ARITH_UNSIGNED_TRUNCATED
;
413 /* Given a real and a kind, make sure that the real lies within the
414 range of the kind. Returns ARITH_OK, ARITH_OVERFLOW or
418 gfc_check_real_range (mpfr_t p
, int kind
)
424 i
= gfc_validate_kind (BT_REAL
, kind
, false);
428 mpfr_abs (q
, p
, GFC_RND_MODE
);
434 if (flag_range_check
!= 0)
435 retval
= ARITH_OVERFLOW
;
437 else if (mpfr_nan_p (p
))
439 if (flag_range_check
!= 0)
442 else if (mpfr_sgn (q
) == 0)
447 else if (mpfr_cmp (q
, gfc_real_kinds
[i
].huge
) > 0)
449 if (flag_range_check
== 0)
450 mpfr_set_inf (p
, mpfr_sgn (p
));
452 retval
= ARITH_OVERFLOW
;
454 else if (mpfr_cmp (q
, gfc_real_kinds
[i
].subnormal
) < 0)
456 if (flag_range_check
== 0)
458 if (mpfr_sgn (p
) < 0)
460 mpfr_set_ui (p
, 0, GFC_RND_MODE
);
461 mpfr_set_si (q
, -1, GFC_RND_MODE
);
462 mpfr_copysign (p
, p
, q
, GFC_RND_MODE
);
465 mpfr_set_ui (p
, 0, GFC_RND_MODE
);
468 retval
= ARITH_UNDERFLOW
;
470 else if (mpfr_cmp (q
, gfc_real_kinds
[i
].tiny
) < 0)
472 mpfr_exp_t emin
, emax
;
475 /* Save current values of emin and emax. */
476 emin
= mpfr_get_emin ();
477 emax
= mpfr_get_emax ();
479 /* Set emin and emax for the current model number. */
480 en
= gfc_real_kinds
[i
].min_exponent
- gfc_real_kinds
[i
].digits
+ 1;
481 mpfr_set_emin ((mpfr_exp_t
) en
);
482 mpfr_set_emax ((mpfr_exp_t
) gfc_real_kinds
[i
].max_exponent
);
483 mpfr_check_range (q
, 0, GFC_RND_MODE
);
484 mpfr_subnormalize (q
, 0, GFC_RND_MODE
);
486 /* Reset emin and emax. */
487 mpfr_set_emin (emin
);
488 mpfr_set_emax (emax
);
490 /* Copy sign if needed. */
491 if (mpfr_sgn (p
) < 0)
492 mpfr_neg (p
, q
, MPFR_RNDN
);
494 mpfr_set (p
, q
, MPFR_RNDN
);
503 /* Low-level arithmetic functions. All of these subroutines assume
504 that all operands are of the same type and return an operand of the
505 same type. The other thing about these subroutines is that they
506 can fail in various ways -- overflow, underflow, division by zero,
507 zero raised to the zero, etc. */
510 gfc_arith_not (gfc_expr
*op1
, gfc_expr
**resultp
)
514 if (op1
->ts
.type
!= BT_LOGICAL
)
515 return ARITH_INVALID_TYPE
;
517 result
= gfc_get_constant_expr (BT_LOGICAL
, op1
->ts
.kind
, &op1
->where
);
518 result
->value
.logical
= !op1
->value
.logical
;
526 gfc_arith_and (gfc_expr
*op1
, gfc_expr
*op2
, gfc_expr
**resultp
)
530 if (op1
->ts
.type
!= BT_LOGICAL
|| op2
->ts
.type
!= BT_LOGICAL
)
531 return ARITH_INVALID_TYPE
;
533 result
= gfc_get_constant_expr (BT_LOGICAL
, gfc_kind_max (op1
, op2
),
535 result
->value
.logical
= op1
->value
.logical
&& op2
->value
.logical
;
543 gfc_arith_or (gfc_expr
*op1
, gfc_expr
*op2
, gfc_expr
**resultp
)
547 if (op1
->ts
.type
!= BT_LOGICAL
|| op2
->ts
.type
!= BT_LOGICAL
)
548 return ARITH_INVALID_TYPE
;
550 result
= gfc_get_constant_expr (BT_LOGICAL
, gfc_kind_max (op1
, op2
),
552 result
->value
.logical
= op1
->value
.logical
|| op2
->value
.logical
;
560 gfc_arith_eqv (gfc_expr
*op1
, gfc_expr
*op2
, gfc_expr
**resultp
)
564 if (op1
->ts
.type
!= BT_LOGICAL
|| op2
->ts
.type
!= BT_LOGICAL
)
565 return ARITH_INVALID_TYPE
;
567 result
= gfc_get_constant_expr (BT_LOGICAL
, gfc_kind_max (op1
, op2
),
569 result
->value
.logical
= op1
->value
.logical
== op2
->value
.logical
;
577 gfc_arith_neqv (gfc_expr
*op1
, gfc_expr
*op2
, gfc_expr
**resultp
)
581 if (op1
->ts
.type
!= BT_LOGICAL
|| op2
->ts
.type
!= BT_LOGICAL
)
582 return ARITH_INVALID_TYPE
;
584 result
= gfc_get_constant_expr (BT_LOGICAL
, gfc_kind_max (op1
, op2
),
586 result
->value
.logical
= op1
->value
.logical
!= op2
->value
.logical
;
593 /* Make sure a constant numeric expression is within the range for
594 its type and kind. Note that there's also a gfc_check_range(),
595 but that one deals with the intrinsic RANGE function. */
598 gfc_range_check (gfc_expr
*e
)
606 rc
= gfc_check_integer_range (e
->value
.integer
, e
->ts
.kind
);
610 rc
= gfc_check_unsigned_range (e
->value
.integer
, e
->ts
.kind
);
614 rc
= gfc_check_real_range (e
->value
.real
, e
->ts
.kind
);
615 if (rc
== ARITH_UNDERFLOW
)
616 mpfr_set_ui (e
->value
.real
, 0, GFC_RND_MODE
);
617 if (rc
== ARITH_OVERFLOW
)
618 mpfr_set_inf (e
->value
.real
, mpfr_sgn (e
->value
.real
));
620 mpfr_set_nan (e
->value
.real
);
624 rc
= gfc_check_real_range (mpc_realref (e
->value
.complex), e
->ts
.kind
);
625 if (rc
== ARITH_UNDERFLOW
)
626 mpfr_set_ui (mpc_realref (e
->value
.complex), 0, GFC_RND_MODE
);
627 if (rc
== ARITH_OVERFLOW
)
628 mpfr_set_inf (mpc_realref (e
->value
.complex),
629 mpfr_sgn (mpc_realref (e
->value
.complex)));
631 mpfr_set_nan (mpc_realref (e
->value
.complex));
633 rc2
= gfc_check_real_range (mpc_imagref (e
->value
.complex), e
->ts
.kind
);
634 if (rc
== ARITH_UNDERFLOW
)
635 mpfr_set_ui (mpc_imagref (e
->value
.complex), 0, GFC_RND_MODE
);
636 if (rc
== ARITH_OVERFLOW
)
637 mpfr_set_inf (mpc_imagref (e
->value
.complex),
638 mpfr_sgn (mpc_imagref (e
->value
.complex)));
640 mpfr_set_nan (mpc_imagref (e
->value
.complex));
647 gfc_internal_error ("gfc_range_check(): Bad type");
654 /* Several of the following routines use the same set of statements to
655 check the validity of the result. Encapsulate the checking here. */
658 check_result (arith rc
, gfc_expr
*x
, gfc_expr
*r
, gfc_expr
**rp
)
662 if (val
== ARITH_UNDERFLOW
)
665 gfc_warning (OPT_Wunderflow
, gfc_arith_error (val
), &x
->where
);
669 if (val
== ARITH_ASYMMETRIC
)
671 gfc_warning (0, gfc_arith_error (val
), &x
->where
);
675 if (is_hard_arith_error (val
))
684 /* It may seem silly to have a subroutine that actually computes the
685 unary plus of a constant, but it prevents us from making exceptions
686 in the code elsewhere. Used for unary plus and parenthesized
690 gfc_arith_identity (gfc_expr
*op1
, gfc_expr
**resultp
)
692 *resultp
= gfc_copy_expr (op1
);
698 gfc_arith_uminus (gfc_expr
*op1
, gfc_expr
**resultp
)
703 result
= gfc_get_constant_expr (op1
->ts
.type
, op1
->ts
.kind
, &op1
->where
);
705 switch (op1
->ts
.type
)
708 mpz_neg (result
->value
.integer
, op1
->value
.integer
);
714 return check_result (ARITH_UNSIGNED_NEGATIVE
, op1
, result
, resultp
);
716 mpz_neg (result
->value
.integer
, op1
->value
.integer
);
721 mpfr_neg (result
->value
.real
, op1
->value
.real
, GFC_RND_MODE
);
725 mpc_neg (result
->value
.complex, op1
->value
.complex, GFC_MPC_RND_MODE
);
729 gfc_internal_error ("gfc_arith_uminus(): Bad basic type");
732 rc
= gfc_range_check (result
);
733 if (op1
->ts
.type
== BT_UNSIGNED
)
737 gfc_warning (0, gfc_arith_error (rc
), &op1
->where
);
740 gfc_reduce_unsigned (result
);
742 return check_result (rc
, op1
, result
, resultp
);
747 gfc_arith_plus (gfc_expr
*op1
, gfc_expr
*op2
, gfc_expr
**resultp
)
752 if (op1
->ts
.type
!= op2
->ts
.type
)
753 return ARITH_INVALID_TYPE
;
755 result
= gfc_get_constant_expr (op1
->ts
.type
, op1
->ts
.kind
, &op1
->where
);
757 switch (op1
->ts
.type
)
760 mpz_add (result
->value
.integer
, op1
->value
.integer
, op2
->value
.integer
);
764 mpz_add (result
->value
.integer
, op1
->value
.integer
, op2
->value
.integer
);
765 gfc_reduce_unsigned (result
);
769 mpfr_add (result
->value
.real
, op1
->value
.real
, op2
->value
.real
,
774 mpc_add (result
->value
.complex, op1
->value
.complex, op2
->value
.complex,
779 gfc_internal_error ("gfc_arith_plus(): Bad basic type");
782 rc
= gfc_range_check (result
);
784 return check_result (rc
, op1
, result
, resultp
);
789 gfc_arith_minus (gfc_expr
*op1
, gfc_expr
*op2
, gfc_expr
**resultp
)
794 if (op1
->ts
.type
!= op2
->ts
.type
)
795 return ARITH_INVALID_TYPE
;
797 result
= gfc_get_constant_expr (op1
->ts
.type
, op1
->ts
.kind
, &op1
->where
);
799 switch (op1
->ts
.type
)
802 mpz_sub (result
->value
.integer
, op1
->value
.integer
, op2
->value
.integer
);
806 mpz_sub (result
->value
.integer
, op1
->value
.integer
, op2
->value
.integer
);
807 gfc_reduce_unsigned (result
);
811 mpfr_sub (result
->value
.real
, op1
->value
.real
, op2
->value
.real
,
816 mpc_sub (result
->value
.complex, op1
->value
.complex,
817 op2
->value
.complex, GFC_MPC_RND_MODE
);
821 gfc_internal_error ("gfc_arith_minus(): Bad basic type");
824 rc
= gfc_range_check (result
);
826 return check_result (rc
, op1
, result
, resultp
);
831 gfc_arith_times (gfc_expr
*op1
, gfc_expr
*op2
, gfc_expr
**resultp
)
836 if (op1
->ts
.type
!= op2
->ts
.type
)
837 return ARITH_INVALID_TYPE
;
839 result
= gfc_get_constant_expr (op1
->ts
.type
, op1
->ts
.kind
, &op1
->where
);
841 switch (op1
->ts
.type
)
844 mpz_mul (result
->value
.integer
, op1
->value
.integer
, op2
->value
.integer
);
848 mpz_mul (result
->value
.integer
, op1
->value
.integer
, op2
->value
.integer
);
849 gfc_reduce_unsigned (result
);
853 mpfr_mul (result
->value
.real
, op1
->value
.real
, op2
->value
.real
,
858 gfc_set_model (mpc_realref (op1
->value
.complex));
859 mpc_mul (result
->value
.complex, op1
->value
.complex, op2
->value
.complex,
864 gfc_internal_error ("gfc_arith_times(): Bad basic type");
867 rc
= gfc_range_check (result
);
869 return check_result (rc
, op1
, result
, resultp
);
874 gfc_arith_divide (gfc_expr
*op1
, gfc_expr
*op2
, gfc_expr
**resultp
)
879 if (op1
->ts
.type
!= op2
->ts
.type
)
880 return ARITH_INVALID_TYPE
;
884 result
= gfc_get_constant_expr (op1
->ts
.type
, op1
->ts
.kind
, &op1
->where
);
886 switch (op1
->ts
.type
)
890 if (mpz_sgn (op2
->value
.integer
) == 0)
896 if (warn_integer_division
)
900 mpz_tdiv_qr (result
->value
.integer
, r
, op1
->value
.integer
,
903 if (mpz_cmp_si (r
, 0) != 0)
906 p
= mpz_get_str (NULL
, 10, result
->value
.integer
);
907 gfc_warning (OPT_Winteger_division
, "Integer division "
908 "truncated to constant %qs at %L", p
,
915 mpz_tdiv_q (result
->value
.integer
, op1
->value
.integer
,
921 /* Set "Division by zero" only for regular numerator. */
922 if (flag_range_check
== 1
923 && mpfr_zero_p (op2
->value
.real
)
924 && mpfr_regular_p (op1
->value
.real
))
927 mpfr_div (result
->value
.real
, op1
->value
.real
, op2
->value
.real
,
932 /* Set "Division by zero" only for regular numerator. */
933 if (flag_range_check
== 1
934 && mpfr_zero_p (mpc_realref (op2
->value
.complex))
935 && mpfr_zero_p (mpc_imagref (op2
->value
.complex))
936 && ((mpfr_regular_p (mpc_realref (op1
->value
.complex))
937 && mpfr_number_p (mpc_imagref (op1
->value
.complex)))
938 || (mpfr_regular_p (mpc_imagref (op1
->value
.complex))
939 && mpfr_number_p (mpc_realref (op1
->value
.complex)))))
942 gfc_set_model (mpc_realref (op1
->value
.complex));
943 if (mpc_cmp_si_si (op2
->value
.complex, 0, 0) == 0)
945 /* In Fortran, return (NaN + NaN I) for any zero divisor. See
947 mpfr_set_nan (mpc_realref (result
->value
.complex));
948 mpfr_set_nan (mpc_imagref (result
->value
.complex));
951 mpc_div (result
->value
.complex, op1
->value
.complex, op2
->value
.complex,
956 gfc_internal_error ("gfc_arith_divide(): Bad basic type");
960 rc
= gfc_range_check (result
);
962 return check_result (rc
, op1
, result
, resultp
);
965 /* Raise a number to a power. */
968 arith_power (gfc_expr
*op1
, gfc_expr
*op2
, gfc_expr
**resultp
)
974 if (!gfc_numeric_ts (&op1
->ts
) || !gfc_numeric_ts (&op2
->ts
))
975 return ARITH_INVALID_TYPE
;
977 /* The result type is derived from op1 and must be compatible with the
978 result of the simplification. Otherwise postpone simplification until
979 after operand conversions usually done by gfc_type_convert_binary. */
980 if ((op1
->ts
.type
== BT_INTEGER
&& op2
->ts
.type
!= BT_INTEGER
)
981 || (op1
->ts
.type
== BT_REAL
&& op2
->ts
.type
== BT_COMPLEX
))
982 return ARITH_NOT_REDUCED
;
985 result
= gfc_get_constant_expr (op1
->ts
.type
, op1
->ts
.kind
, &op1
->where
);
987 switch (op2
->ts
.type
)
990 power_sign
= mpz_sgn (op2
->value
.integer
);
994 /* Handle something to the zeroth power. Since we're dealing
995 with integral exponents, there is no ambiguity in the
996 limiting procedure used to determine the value of 0**0. */
997 switch (op1
->ts
.type
)
1000 mpz_set_ui (result
->value
.integer
, 1);
1004 mpfr_set_ui (result
->value
.real
, 1, GFC_RND_MODE
);
1008 mpc_set_ui (result
->value
.complex, 1, GFC_MPC_RND_MODE
);
1012 gfc_internal_error ("arith_power(): Bad base");
1017 switch (op1
->ts
.type
)
1021 /* First, we simplify the cases of op1 == 1, 0 or -1. */
1022 if (mpz_cmp_si (op1
->value
.integer
, 1) == 0)
1025 mpz_set_si (result
->value
.integer
, 1);
1027 else if (mpz_cmp_si (op1
->value
.integer
, 0) == 0)
1029 /* 0**op2 == 0, if op2 > 0
1030 0**op2 overflow, if op2 < 0 ; in that case, we
1031 set the result to 0 and return ARITH_DIV0. */
1032 mpz_set_si (result
->value
.integer
, 0);
1033 if (mpz_cmp_si (op2
->value
.integer
, 0) < 0)
1036 else if (mpz_cmp_si (op1
->value
.integer
, -1) == 0)
1038 /* (-1)**op2 == (-1)**(mod(op2,2)) */
1039 unsigned int odd
= mpz_fdiv_ui (op2
->value
.integer
, 2);
1041 mpz_set_si (result
->value
.integer
, -1);
1043 mpz_set_si (result
->value
.integer
, 1);
1045 /* Then, we take care of op2 < 0. */
1046 else if (mpz_cmp_si (op2
->value
.integer
, 0) < 0)
1048 /* if op2 < 0, op1**op2 == 0 because abs(op1) > 1. */
1049 mpz_set_si (result
->value
.integer
, 0);
1050 if (warn_integer_division
)
1051 gfc_warning_now (OPT_Winteger_division
, "Negative "
1052 "exponent of integer has zero "
1053 "result at %L", &result
->where
);
1057 /* We have abs(op1) > 1 and op2 > 1.
1058 If op2 > bit_size(op1), we'll have an out-of-range
1062 k
= gfc_validate_kind (BT_INTEGER
, op1
->ts
.kind
, false);
1063 power
= gfc_integer_kinds
[k
].bit_size
;
1064 if (mpz_cmp_si (op2
->value
.integer
, power
) < 0)
1066 gfc_extract_int (op2
, &power
);
1067 mpz_pow_ui (result
->value
.integer
, op1
->value
.integer
,
1069 rc
= gfc_range_check (result
);
1070 if (rc
== ARITH_OVERFLOW
)
1071 gfc_error_now ("Result of exponentiation at %L "
1072 "exceeds the range of %s", &op1
->where
,
1073 gfc_typename (&(op1
->ts
)));
1077 /* Provide a nonsense value to propagate up. */
1078 mpz_set (result
->value
.integer
,
1079 gfc_integer_kinds
[k
].huge
);
1080 mpz_add_ui (result
->value
.integer
,
1081 result
->value
.integer
, 1);
1082 rc
= ARITH_OVERFLOW
;
1089 mpfr_pow_z (result
->value
.real
, op1
->value
.real
,
1090 op2
->value
.integer
, GFC_RND_MODE
);
1094 mpc_pow_z (result
->value
.complex, op1
->value
.complex,
1095 op2
->value
.integer
, GFC_MPC_RND_MODE
);
1106 if (gfc_init_expr_flag
)
1108 if (!gfc_notify_std (GFC_STD_F2003
, "Noninteger "
1109 "exponent in an initialization "
1110 "expression at %L", &op2
->where
))
1112 gfc_free_expr (result
);
1113 return ARITH_PROHIBIT
;
1117 if (mpfr_cmp_si (op1
->value
.real
, 0) < 0)
1119 gfc_error ("Raising a negative REAL at %L to "
1120 "a REAL power is prohibited", &op1
->where
);
1121 gfc_free_expr (result
);
1122 return ARITH_PROHIBIT
;
1125 mpfr_pow (result
->value
.real
, op1
->value
.real
, op2
->value
.real
,
1131 if (gfc_init_expr_flag
)
1133 if (!gfc_notify_std (GFC_STD_F2003
, "Noninteger "
1134 "exponent in an initialization "
1135 "expression at %L", &op2
->where
))
1137 gfc_free_expr (result
);
1138 return ARITH_PROHIBIT
;
1142 mpc_pow (result
->value
.complex, op1
->value
.complex,
1143 op2
->value
.complex, GFC_MPC_RND_MODE
);
1150 gcc_assert (op1
->ts
.type
== BT_UNSIGNED
);
1151 k
= gfc_validate_kind (BT_UNSIGNED
, op1
->ts
.kind
, false);
1152 /* Exponentiation is performed modulo x = 2**n. */
1154 mpz_add_ui (x
, gfc_unsigned_kinds
[k
].huge
, 1);
1155 mpz_powm (result
->value
.integer
, op1
->value
.integer
,
1156 op2
->value
.integer
, x
);
1161 gfc_internal_error ("arith_power(): unknown type");
1165 rc
= gfc_range_check (result
);
1167 return check_result (rc
, op1
, result
, resultp
);
1171 /* Concatenate two string constants. */
1174 gfc_arith_concat (gfc_expr
*op1
, gfc_expr
*op2
, gfc_expr
**resultp
)
1179 /* By cleverly playing around with constructors, it is possible
1180 to get mismatching types here. */
1181 if (op1
->ts
.type
!= BT_CHARACTER
|| op2
->ts
.type
!= BT_CHARACTER
1182 || op1
->ts
.kind
!= op2
->ts
.kind
)
1183 return ARITH_WRONGCONCAT
;
1185 result
= gfc_get_constant_expr (BT_CHARACTER
, op1
->ts
.kind
,
1188 len
= op1
->value
.character
.length
+ op2
->value
.character
.length
;
1190 result
->value
.character
.string
= gfc_get_wide_string (len
+ 1);
1191 result
->value
.character
.length
= len
;
1193 memcpy (result
->value
.character
.string
, op1
->value
.character
.string
,
1194 op1
->value
.character
.length
* sizeof (gfc_char_t
));
1196 memcpy (&result
->value
.character
.string
[op1
->value
.character
.length
],
1197 op2
->value
.character
.string
,
1198 op2
->value
.character
.length
* sizeof (gfc_char_t
));
1200 result
->value
.character
.string
[len
] = '\0';
1207 /* Comparison between real values; returns 0 if (op1 .op. op2) is true.
1208 This function mimics mpfr_cmp but takes NaN into account. */
1211 compare_real (gfc_expr
*op1
, gfc_expr
*op2
, gfc_intrinsic_op op
)
1217 rc
= mpfr_equal_p (op1
->value
.real
, op2
->value
.real
) ? 0 : 1;
1220 rc
= mpfr_greater_p (op1
->value
.real
, op2
->value
.real
) ? 1 : -1;
1223 rc
= mpfr_greaterequal_p (op1
->value
.real
, op2
->value
.real
) ? 1 : -1;
1226 rc
= mpfr_less_p (op1
->value
.real
, op2
->value
.real
) ? -1 : 1;
1229 rc
= mpfr_lessequal_p (op1
->value
.real
, op2
->value
.real
) ? -1 : 1;
1232 gfc_internal_error ("compare_real(): Bad operator");
1238 /* Comparison operators. Assumes that the two expression nodes
1239 contain two constants of the same type. The op argument is
1240 needed to handle NaN correctly. */
1243 gfc_compare_expr (gfc_expr
*op1
, gfc_expr
*op2
, gfc_intrinsic_op op
)
1247 switch (op1
->ts
.type
)
1251 rc
= mpz_cmp (op1
->value
.integer
, op2
->value
.integer
);
1255 rc
= compare_real (op1
, op2
, op
);
1259 rc
= gfc_compare_string (op1
, op2
);
1263 rc
= ((!op1
->value
.logical
&& op2
->value
.logical
)
1264 || (op1
->value
.logical
&& !op2
->value
.logical
));
1268 gcc_assert (op
== INTRINSIC_EQ
);
1269 rc
= mpc_cmp (op1
->value
.complex, op2
->value
.complex);
1273 gfc_internal_error ("gfc_compare_expr(): Bad basic type");
1280 /* Compare a pair of complex numbers. Naturally, this is only for
1281 equality and inequality. */
1284 compare_complex (gfc_expr
*op1
, gfc_expr
*op2
)
1286 return mpc_cmp (op1
->value
.complex, op2
->value
.complex) == 0;
1290 /* Given two constant strings and the inverse collating sequence, compare the
1291 strings. We return -1 for a < b, 0 for a == b and 1 for a > b.
1292 We use the processor's default collating sequence. */
1295 gfc_compare_string (gfc_expr
*a
, gfc_expr
*b
)
1297 size_t len
, alen
, blen
, i
;
1300 alen
= a
->value
.character
.length
;
1301 blen
= b
->value
.character
.length
;
1303 len
= MAX(alen
, blen
);
1305 for (i
= 0; i
< len
; i
++)
1307 ac
= ((i
< alen
) ? a
->value
.character
.string
[i
] : ' ');
1308 bc
= ((i
< blen
) ? b
->value
.character
.string
[i
] : ' ');
1316 /* Strings are equal */
1322 gfc_compare_with_Cstring (gfc_expr
*a
, const char *b
, bool case_sensitive
)
1324 size_t len
, alen
, blen
, i
;
1327 alen
= a
->value
.character
.length
;
1330 len
= MAX(alen
, blen
);
1332 for (i
= 0; i
< len
; i
++)
1334 ac
= ((i
< alen
) ? a
->value
.character
.string
[i
] : ' ');
1335 bc
= ((i
< blen
) ? b
[i
] : ' ');
1337 if (!case_sensitive
)
1349 /* Strings are equal */
1354 /* Specific comparison subroutines. */
1357 gfc_arith_eq (gfc_expr
*op1
, gfc_expr
*op2
, gfc_expr
**resultp
)
1361 if (op1
->ts
.type
!= op2
->ts
.type
)
1362 return ARITH_INVALID_TYPE
;
1364 result
= gfc_get_constant_expr (BT_LOGICAL
, gfc_default_logical_kind
,
1366 result
->value
.logical
= (op1
->ts
.type
== BT_COMPLEX
)
1367 ? compare_complex (op1
, op2
)
1368 : (gfc_compare_expr (op1
, op2
, INTRINSIC_EQ
) == 0);
1376 gfc_arith_ne (gfc_expr
*op1
, gfc_expr
*op2
, gfc_expr
**resultp
)
1380 if (op1
->ts
.type
!= op2
->ts
.type
)
1381 return ARITH_INVALID_TYPE
;
1383 result
= gfc_get_constant_expr (BT_LOGICAL
, gfc_default_logical_kind
,
1385 result
->value
.logical
= (op1
->ts
.type
== BT_COMPLEX
)
1386 ? !compare_complex (op1
, op2
)
1387 : (gfc_compare_expr (op1
, op2
, INTRINSIC_EQ
) != 0);
1395 gfc_arith_gt (gfc_expr
*op1
, gfc_expr
*op2
, gfc_expr
**resultp
)
1399 if (op1
->ts
.type
!= op2
->ts
.type
)
1400 return ARITH_INVALID_TYPE
;
1402 result
= gfc_get_constant_expr (BT_LOGICAL
, gfc_default_logical_kind
,
1404 result
->value
.logical
= (gfc_compare_expr (op1
, op2
, INTRINSIC_GT
) > 0);
1412 gfc_arith_ge (gfc_expr
*op1
, gfc_expr
*op2
, gfc_expr
**resultp
)
1416 if (op1
->ts
.type
!= op2
->ts
.type
)
1417 return ARITH_INVALID_TYPE
;
1419 result
= gfc_get_constant_expr (BT_LOGICAL
, gfc_default_logical_kind
,
1421 result
->value
.logical
= (gfc_compare_expr (op1
, op2
, INTRINSIC_GE
) >= 0);
1429 gfc_arith_lt (gfc_expr
*op1
, gfc_expr
*op2
, gfc_expr
**resultp
)
1433 if (op1
->ts
.type
!= op2
->ts
.type
)
1434 return ARITH_INVALID_TYPE
;
1436 result
= gfc_get_constant_expr (BT_LOGICAL
, gfc_default_logical_kind
,
1438 result
->value
.logical
= (gfc_compare_expr (op1
, op2
, INTRINSIC_LT
) < 0);
1446 gfc_arith_le (gfc_expr
*op1
, gfc_expr
*op2
, gfc_expr
**resultp
)
1450 if (op1
->ts
.type
!= op2
->ts
.type
)
1451 return ARITH_INVALID_TYPE
;
1453 result
= gfc_get_constant_expr (BT_LOGICAL
, gfc_default_logical_kind
,
1455 result
->value
.logical
= (gfc_compare_expr (op1
, op2
, INTRINSIC_LE
) <= 0);
1463 reduce_unary (arith (*eval
) (gfc_expr
*, gfc_expr
**), gfc_expr
*op
,
1466 gfc_constructor_base head
;
1471 if (op
->expr_type
== EXPR_CONSTANT
)
1472 return eval (op
, result
);
1474 if (op
->expr_type
!= EXPR_ARRAY
)
1475 return ARITH_NOT_REDUCED
;
1478 head
= gfc_constructor_copy (op
->value
.constructor
);
1479 for (c
= gfc_constructor_first (head
); c
; c
= gfc_constructor_next (c
))
1481 arith rc_tmp
= reduce_unary (eval
, c
->expr
, &r
);
1483 /* Remember first recoverable ("soft") error encountered during
1484 reduction and continue, but terminate on serious errors. */
1485 if (is_hard_arith_error (rc_tmp
))
1490 else if (rc_tmp
!= ARITH_OK
&& rc
== ARITH_OK
)
1493 gfc_replace_expr (c
->expr
, r
);
1496 if (is_hard_arith_error (rc
))
1497 gfc_constructor_free (head
);
1500 gfc_constructor
*c
= gfc_constructor_first (head
);
1503 /* Handle zero-sized arrays. */
1504 r
= gfc_get_array_expr (op
->ts
.type
, op
->ts
.kind
, &op
->where
);
1508 r
= gfc_get_array_expr (c
->expr
->ts
.type
, c
->expr
->ts
.kind
,
1511 r
->shape
= gfc_copy_shape (op
->shape
, op
->rank
);
1513 r
->corank
= op
->corank
;
1514 r
->value
.constructor
= head
;
1523 reduce_binary_ac (arith (*eval
) (gfc_expr
*, gfc_expr
*, gfc_expr
**),
1524 gfc_expr
*op1
, gfc_expr
*op2
, gfc_expr
**result
)
1526 gfc_constructor_base head
;
1529 arith rc
= ARITH_OK
;
1531 head
= gfc_constructor_copy (op1
->value
.constructor
);
1532 for (c
= gfc_constructor_first (head
); c
; c
= gfc_constructor_next (c
))
1536 gfc_simplify_expr (c
->expr
, 0);
1538 if (c
->expr
->expr_type
== EXPR_CONSTANT
)
1539 rc_tmp
= eval (c
->expr
, op2
, &r
);
1540 else if (c
->expr
->expr_type
!= EXPR_ARRAY
)
1541 rc_tmp
= ARITH_NOT_REDUCED
;
1543 rc_tmp
= reduce_binary_ac (eval
, c
->expr
, op2
, &r
);
1545 /* Remember first recoverable ("soft") error encountered during
1546 reduction and continue, but terminate on serious errors. */
1547 if (is_hard_arith_error (rc_tmp
))
1552 else if (rc_tmp
!= ARITH_OK
&& rc
== ARITH_OK
)
1555 gfc_replace_expr (c
->expr
, r
);
1558 if (is_hard_arith_error (rc
))
1559 gfc_constructor_free (head
);
1562 gfc_constructor
*c
= gfc_constructor_first (head
);
1565 r
= gfc_get_array_expr (c
->expr
->ts
.type
, c
->expr
->ts
.kind
,
1567 r
->shape
= gfc_copy_shape (op1
->shape
, op1
->rank
);
1571 gcc_assert (op1
->ts
.type
!= BT_UNKNOWN
);
1572 r
= gfc_get_array_expr (op1
->ts
.type
, op1
->ts
.kind
,
1574 r
->shape
= gfc_get_shape (op1
->rank
);
1576 r
->rank
= op1
->rank
;
1577 r
->corank
= op1
->corank
;
1578 r
->value
.constructor
= head
;
1587 reduce_binary_ca (arith (*eval
) (gfc_expr
*, gfc_expr
*, gfc_expr
**),
1588 gfc_expr
*op1
, gfc_expr
*op2
, gfc_expr
**result
)
1590 gfc_constructor_base head
;
1593 arith rc
= ARITH_OK
;
1595 head
= gfc_constructor_copy (op2
->value
.constructor
);
1596 for (c
= gfc_constructor_first (head
); c
; c
= gfc_constructor_next (c
))
1600 gfc_simplify_expr (c
->expr
, 0);
1602 if (c
->expr
->expr_type
== EXPR_CONSTANT
)
1603 rc_tmp
= eval (op1
, c
->expr
, &r
);
1604 else if (c
->expr
->expr_type
!= EXPR_ARRAY
)
1605 rc_tmp
= ARITH_NOT_REDUCED
;
1607 rc_tmp
= reduce_binary_ca (eval
, op1
, c
->expr
, &r
);
1609 /* Remember first recoverable ("soft") error encountered during
1610 reduction and continue, but terminate on serious errors. */
1611 if (is_hard_arith_error (rc_tmp
))
1616 else if (rc_tmp
!= ARITH_OK
&& rc
== ARITH_OK
)
1619 gfc_replace_expr (c
->expr
, r
);
1622 if (is_hard_arith_error (rc
))
1623 gfc_constructor_free (head
);
1626 gfc_constructor
*c
= gfc_constructor_first (head
);
1629 r
= gfc_get_array_expr (c
->expr
->ts
.type
, c
->expr
->ts
.kind
,
1631 r
->shape
= gfc_copy_shape (op2
->shape
, op2
->rank
);
1635 gcc_assert (op2
->ts
.type
!= BT_UNKNOWN
);
1636 r
= gfc_get_array_expr (op2
->ts
.type
, op2
->ts
.kind
,
1638 r
->shape
= gfc_get_shape (op2
->rank
);
1640 r
->rank
= op2
->rank
;
1641 r
->corank
= op2
->corank
;
1642 r
->value
.constructor
= head
;
1650 /* We need a forward declaration of reduce_binary. */
1651 static arith
reduce_binary (arith (*eval
) (gfc_expr
*, gfc_expr
*, gfc_expr
**),
1652 gfc_expr
*op1
, gfc_expr
*op2
, gfc_expr
**result
);
1656 reduce_binary_aa (arith (*eval
) (gfc_expr
*, gfc_expr
*, gfc_expr
**),
1657 gfc_expr
*op1
, gfc_expr
*op2
, gfc_expr
**result
)
1659 gfc_constructor_base head
;
1660 gfc_constructor
*c
, *d
;
1662 arith rc
= ARITH_OK
;
1664 if (!gfc_check_conformance (op1
, op2
, _("elemental binary operation")))
1665 return ARITH_INCOMMENSURATE
;
1667 head
= gfc_constructor_copy (op1
->value
.constructor
);
1668 for (c
= gfc_constructor_first (head
),
1669 d
= gfc_constructor_first (op2
->value
.constructor
);
1671 c
= gfc_constructor_next (c
), d
= gfc_constructor_next (d
))
1673 arith rc_tmp
= reduce_binary (eval
, c
->expr
, d
->expr
, &r
);
1675 /* Remember first recoverable ("soft") error encountered during
1676 reduction and continue, but terminate on serious errors. */
1677 if (is_hard_arith_error (rc_tmp
))
1682 else if (rc_tmp
!= ARITH_OK
&& rc
== ARITH_OK
)
1685 gfc_replace_expr (c
->expr
, r
);
1688 if (rc
== ARITH_OK
&& (c
|| d
))
1689 rc
= ARITH_INCOMMENSURATE
;
1691 if (is_hard_arith_error (rc
))
1692 gfc_constructor_free (head
);
1695 gfc_constructor
*c
= gfc_constructor_first (head
);
1698 /* Handle zero-sized arrays. */
1699 r
= gfc_get_array_expr (op1
->ts
.type
, op1
->ts
.kind
, &op1
->where
);
1703 r
= gfc_get_array_expr (c
->expr
->ts
.type
, c
->expr
->ts
.kind
,
1706 r
->shape
= gfc_copy_shape (op1
->shape
, op1
->rank
);
1707 r
->rank
= op1
->rank
;
1708 r
->corank
= op1
->corank
;
1709 r
->value
.constructor
= head
;
1718 reduce_binary (arith (*eval
) (gfc_expr
*, gfc_expr
*, gfc_expr
**),
1719 gfc_expr
*op1
, gfc_expr
*op2
, gfc_expr
**result
)
1721 if (op1
->expr_type
== EXPR_CONSTANT
&& op2
->expr_type
== EXPR_CONSTANT
)
1722 return eval (op1
, op2
, result
);
1724 if (op1
->expr_type
== EXPR_CONSTANT
&& op2
->expr_type
== EXPR_ARRAY
)
1725 return reduce_binary_ca (eval
, op1
, op2
, result
);
1727 if (op1
->expr_type
== EXPR_ARRAY
&& op2
->expr_type
== EXPR_CONSTANT
)
1728 return reduce_binary_ac (eval
, op1
, op2
, result
);
1730 if (op1
->expr_type
!= EXPR_ARRAY
|| op2
->expr_type
!= EXPR_ARRAY
)
1731 return ARITH_NOT_REDUCED
;
1733 return reduce_binary_aa (eval
, op1
, op2
, result
);
1739 arith (*f2
)(gfc_expr
*, gfc_expr
**);
1740 arith (*f3
)(gfc_expr
*, gfc_expr
*, gfc_expr
**);
1744 /* High level arithmetic subroutines. These subroutines go into
1745 eval_intrinsic(), which can do one of several things to its
1746 operands. If the operands are incompatible with the intrinsic
1747 operation, we return a node pointing to the operands and hope that
1748 an operator interface is found during resolution.
1750 If the operands are compatible and are constants, then we try doing
1751 the arithmetic. We also handle the cases where either or both
1752 operands are array constructors. */
1755 eval_intrinsic (gfc_intrinsic_op op
,
1756 eval_f eval
, gfc_expr
*op1
, gfc_expr
*op2
)
1758 gfc_expr temp
, *result
;
1765 gfc_clear_ts (&temp
.ts
);
1771 if (op1
->ts
.type
!= BT_LOGICAL
)
1774 temp
.ts
.type
= BT_LOGICAL
;
1775 temp
.ts
.kind
= gfc_default_logical_kind
;
1779 /* Logical binary operators */
1782 case INTRINSIC_NEQV
:
1784 if (op1
->ts
.type
!= BT_LOGICAL
|| op2
->ts
.type
!= BT_LOGICAL
)
1787 temp
.ts
.type
= BT_LOGICAL
;
1788 temp
.ts
.kind
= gfc_default_logical_kind
;
1793 case INTRINSIC_UPLUS
:
1794 case INTRINSIC_UMINUS
:
1795 if (!gfc_numeric_ts (&op1
->ts
))
1802 case INTRINSIC_PARENTHESES
:
1807 /* Additional restrictions for ordering relations. */
1809 case INTRINSIC_GE_OS
:
1811 case INTRINSIC_LT_OS
:
1813 case INTRINSIC_LE_OS
:
1815 case INTRINSIC_GT_OS
:
1816 if (op1
->ts
.type
== BT_COMPLEX
|| op2
->ts
.type
== BT_COMPLEX
)
1818 temp
.ts
.type
= BT_LOGICAL
;
1819 temp
.ts
.kind
= gfc_default_logical_kind
;
1825 case INTRINSIC_EQ_OS
:
1827 case INTRINSIC_NE_OS
:
1828 if (op1
->ts
.type
== BT_CHARACTER
&& op2
->ts
.type
== BT_CHARACTER
)
1831 temp
.ts
.type
= BT_LOGICAL
;
1832 temp
.ts
.kind
= gfc_default_logical_kind
;
1834 /* If kind mismatch, exit and we'll error out later. */
1835 if (op1
->ts
.kind
!= op2
->ts
.kind
)
1842 /* Numeric binary */
1843 case INTRINSIC_POWER
:
1844 if (pedantic
&& (op1
->ts
.type
== BT_UNSIGNED
|| op2
->ts
.type
== BT_UNSIGNED
))
1846 gfc_error ("Unsigned exponentiation not permitted with -pedantic "
1847 "at %L", &op1
->where
);
1853 case INTRINSIC_PLUS
:
1854 case INTRINSIC_MINUS
:
1855 case INTRINSIC_TIMES
:
1856 case INTRINSIC_DIVIDE
:
1857 if (!gfc_numeric_ts (&op1
->ts
) || !gfc_numeric_ts (&op2
->ts
))
1860 if (flag_unsigned
&& gfc_invalid_unsigned_ops (op1
, op2
))
1863 /* Do not perform conversions if operands are not conformable as
1864 required for the binary intrinsic operators (F2018:10.1.5).
1865 Defer to a possibly overloading user-defined operator. */
1866 if (!gfc_op_rank_conformable (op1
, op2
))
1869 /* Insert any necessary type conversions to make the operands
1872 temp
.expr_type
= EXPR_OP
;
1873 gfc_clear_ts (&temp
.ts
);
1874 temp
.value
.op
.op
= op
;
1876 temp
.value
.op
.op1
= op1
;
1877 temp
.value
.op
.op2
= op2
;
1879 gfc_type_convert_binary (&temp
, warn_conversion
|| warn_conversion_extra
);
1881 if (op
== INTRINSIC_EQ
|| op
== INTRINSIC_NE
1882 || op
== INTRINSIC_GE
|| op
== INTRINSIC_GT
1883 || op
== INTRINSIC_LE
|| op
== INTRINSIC_LT
1884 || op
== INTRINSIC_EQ_OS
|| op
== INTRINSIC_NE_OS
1885 || op
== INTRINSIC_GE_OS
|| op
== INTRINSIC_GT_OS
1886 || op
== INTRINSIC_LE_OS
|| op
== INTRINSIC_LT_OS
)
1888 temp
.ts
.type
= BT_LOGICAL
;
1889 temp
.ts
.kind
= gfc_default_logical_kind
;
1895 /* Character binary */
1896 case INTRINSIC_CONCAT
:
1897 if (op1
->ts
.type
!= BT_CHARACTER
|| op2
->ts
.type
!= BT_CHARACTER
1898 || op1
->ts
.kind
!= op2
->ts
.kind
)
1901 temp
.ts
.type
= BT_CHARACTER
;
1902 temp
.ts
.kind
= op1
->ts
.kind
;
1906 case INTRINSIC_USER
:
1910 gfc_internal_error ("eval_intrinsic(): Bad operator");
1913 if (op1
->expr_type
!= EXPR_CONSTANT
1914 && (op1
->expr_type
!= EXPR_ARRAY
1915 || !gfc_is_constant_expr (op1
) || !gfc_expanded_ac (op1
)))
1919 && op2
->expr_type
!= EXPR_CONSTANT
1920 && (op2
->expr_type
!= EXPR_ARRAY
1921 || !gfc_is_constant_expr (op2
) || !gfc_expanded_ac (op2
)))
1925 rc
= reduce_unary (eval
.f2
, op1
, &result
);
1927 rc
= reduce_binary (eval
.f3
, op1
, op2
, &result
);
1929 if (rc
== ARITH_INVALID_TYPE
|| rc
== ARITH_NOT_REDUCED
)
1932 /* Something went wrong. */
1933 if (op
== INTRINSIC_POWER
&& rc
== ARITH_PROHIBIT
)
1938 gfc_error (gfc_arith_error (rc
), &op1
->where
);
1939 if (rc
== ARITH_OVERFLOW
)
1942 if (rc
== ARITH_DIV0
&& op2
->ts
.type
== BT_INTEGER
)
1943 gfc_seen_div0
= true;
1950 gfc_free_expr (op1
);
1951 gfc_free_expr (op2
);
1955 /* Create a run-time expression. */
1956 result
= gfc_get_operator_expr (&op1
->where
, op
, op1
, op2
);
1957 result
->ts
= temp
.ts
;
1962 /* Modify type of expression for zero size array. */
1965 eval_type_intrinsic0 (gfc_intrinsic_op iop
, gfc_expr
*op
)
1968 gfc_internal_error ("eval_type_intrinsic0(): op NULL");
1973 case INTRINSIC_GE_OS
:
1975 case INTRINSIC_LT_OS
:
1977 case INTRINSIC_LE_OS
:
1979 case INTRINSIC_GT_OS
:
1981 case INTRINSIC_EQ_OS
:
1983 case INTRINSIC_NE_OS
:
1984 op
->ts
.type
= BT_LOGICAL
;
1985 op
->ts
.kind
= gfc_default_logical_kind
;
1996 /* Return nonzero if the expression is a zero size array. */
1999 gfc_zero_size_array (gfc_expr
*e
)
2001 if (e
== NULL
|| e
->expr_type
!= EXPR_ARRAY
)
2004 return e
->value
.constructor
== NULL
;
2008 /* Reduce a binary expression where at least one of the operands
2009 involves a zero-length array. Returns NULL if neither of the
2010 operands is a zero-length array. */
2013 reduce_binary0 (gfc_expr
*op1
, gfc_expr
*op2
)
2015 if (gfc_zero_size_array (op1
))
2017 gfc_free_expr (op2
);
2021 if (gfc_zero_size_array (op2
))
2023 gfc_free_expr (op1
);
2032 eval_intrinsic_f2 (gfc_intrinsic_op op
,
2033 arith (*eval
) (gfc_expr
*, gfc_expr
**),
2034 gfc_expr
*op1
, gfc_expr
*op2
)
2041 if (gfc_zero_size_array (op1
))
2042 return eval_type_intrinsic0 (op
, op1
);
2046 result
= reduce_binary0 (op1
, op2
);
2048 return eval_type_intrinsic0 (op
, result
);
2052 return eval_intrinsic (op
, f
, op1
, op2
);
2057 eval_intrinsic_f3 (gfc_intrinsic_op op
,
2058 arith (*eval
) (gfc_expr
*, gfc_expr
*, gfc_expr
**),
2059 gfc_expr
*op1
, gfc_expr
*op2
)
2067 result
= reduce_binary0 (op1
, op2
);
2069 return eval_type_intrinsic0(op
, result
);
2072 return eval_intrinsic (op
, f
, op1
, op2
);
2077 gfc_parentheses (gfc_expr
*op
)
2079 if (gfc_is_constant_expr (op
))
2082 return eval_intrinsic_f2 (INTRINSIC_PARENTHESES
, gfc_arith_identity
,
2087 gfc_uplus (gfc_expr
*op
)
2089 return eval_intrinsic_f2 (INTRINSIC_UPLUS
, gfc_arith_identity
, op
, NULL
);
2094 gfc_uminus (gfc_expr
*op
)
2096 return eval_intrinsic_f2 (INTRINSIC_UMINUS
, gfc_arith_uminus
, op
, NULL
);
2101 gfc_add (gfc_expr
*op1
, gfc_expr
*op2
)
2103 return eval_intrinsic_f3 (INTRINSIC_PLUS
, gfc_arith_plus
, op1
, op2
);
2108 gfc_subtract (gfc_expr
*op1
, gfc_expr
*op2
)
2110 return eval_intrinsic_f3 (INTRINSIC_MINUS
, gfc_arith_minus
, op1
, op2
);
2115 gfc_multiply (gfc_expr
*op1
, gfc_expr
*op2
)
2117 return eval_intrinsic_f3 (INTRINSIC_TIMES
, gfc_arith_times
, op1
, op2
);
2122 gfc_divide (gfc_expr
*op1
, gfc_expr
*op2
)
2124 return eval_intrinsic_f3 (INTRINSIC_DIVIDE
, gfc_arith_divide
, op1
, op2
);
2129 gfc_power (gfc_expr
*op1
, gfc_expr
*op2
)
2131 return eval_intrinsic_f3 (INTRINSIC_POWER
, arith_power
, op1
, op2
);
2136 gfc_concat (gfc_expr
*op1
, gfc_expr
*op2
)
2138 return eval_intrinsic_f3 (INTRINSIC_CONCAT
, gfc_arith_concat
, op1
, op2
);
2143 gfc_and (gfc_expr
*op1
, gfc_expr
*op2
)
2145 return eval_intrinsic_f3 (INTRINSIC_AND
, gfc_arith_and
, op1
, op2
);
2150 gfc_or (gfc_expr
*op1
, gfc_expr
*op2
)
2152 return eval_intrinsic_f3 (INTRINSIC_OR
, gfc_arith_or
, op1
, op2
);
2157 gfc_not (gfc_expr
*op1
)
2159 return eval_intrinsic_f2 (INTRINSIC_NOT
, gfc_arith_not
, op1
, NULL
);
2164 gfc_eqv (gfc_expr
*op1
, gfc_expr
*op2
)
2166 return eval_intrinsic_f3 (INTRINSIC_EQV
, gfc_arith_eqv
, op1
, op2
);
2171 gfc_neqv (gfc_expr
*op1
, gfc_expr
*op2
)
2173 return eval_intrinsic_f3 (INTRINSIC_NEQV
, gfc_arith_neqv
, op1
, op2
);
2178 gfc_eq (gfc_expr
*op1
, gfc_expr
*op2
, gfc_intrinsic_op op
)
2180 return eval_intrinsic_f3 (op
, gfc_arith_eq
, op1
, op2
);
2185 gfc_ne (gfc_expr
*op1
, gfc_expr
*op2
, gfc_intrinsic_op op
)
2187 return eval_intrinsic_f3 (op
, gfc_arith_ne
, op1
, op2
);
2192 gfc_gt (gfc_expr
*op1
, gfc_expr
*op2
, gfc_intrinsic_op op
)
2194 return eval_intrinsic_f3 (op
, gfc_arith_gt
, op1
, op2
);
2199 gfc_ge (gfc_expr
*op1
, gfc_expr
*op2
, gfc_intrinsic_op op
)
2201 return eval_intrinsic_f3 (op
, gfc_arith_ge
, op1
, op2
);
2206 gfc_lt (gfc_expr
*op1
, gfc_expr
*op2
, gfc_intrinsic_op op
)
2208 return eval_intrinsic_f3 (op
, gfc_arith_lt
, op1
, op2
);
2213 gfc_le (gfc_expr
*op1
, gfc_expr
*op2
, gfc_intrinsic_op op
)
2215 return eval_intrinsic_f3 (op
, gfc_arith_le
, op1
, op2
);
2219 /******* Simplification of intrinsic functions with constant arguments *****/
2222 /* Deal with an arithmetic error. */
2225 arith_error (arith rc
, gfc_typespec
*from
, gfc_typespec
*to
, locus
*where
)
2230 gfc_error ("Arithmetic OK converting %s to %s at %L",
2231 gfc_typename (from
), gfc_typename (to
), where
);
2233 case ARITH_OVERFLOW
:
2234 gfc_error ("Arithmetic overflow converting %s to %s at %L. This check "
2235 "can be disabled with the option %<-fno-range-check%>",
2236 gfc_typename (from
), gfc_typename (to
), where
);
2238 case ARITH_UNDERFLOW
:
2239 gfc_error ("Arithmetic underflow converting %s to %s at %L. This check "
2240 "can be disabled with the option %<-fno-range-check%>",
2241 gfc_typename (from
), gfc_typename (to
), where
);
2244 gfc_error ("Arithmetic NaN converting %s to %s at %L. This check "
2245 "can be disabled with the option %<-fno-range-check%>",
2246 gfc_typename (from
), gfc_typename (to
), where
);
2249 gfc_error ("Division by zero converting %s to %s at %L",
2250 gfc_typename (from
), gfc_typename (to
), where
);
2252 case ARITH_INCOMMENSURATE
:
2253 gfc_error ("Array operands are incommensurate converting %s to %s at %L",
2254 gfc_typename (from
), gfc_typename (to
), where
);
2256 case ARITH_ASYMMETRIC
:
2257 gfc_error ("Integer outside symmetric range implied by Standard Fortran"
2258 " converting %s to %s at %L",
2259 gfc_typename (from
), gfc_typename (to
), where
);
2262 gfc_internal_error ("gfc_arith_error(): Bad error code");
2265 /* TODO: Do something about the error, i.e., throw exception, return
2269 /* Returns true if significant bits were lost when converting real
2270 constant r from from_kind to to_kind. */
2273 wprecision_real_real (mpfr_t r
, int from_kind
, int to_kind
)
2278 gfc_set_model_kind (to_kind
);
2280 gfc_set_model_kind (from_kind
);
2283 mpfr_set (rv
, r
, GFC_RND_MODE
);
2284 mpfr_sub (diff
, rv
, r
, GFC_RND_MODE
);
2286 ret
= ! mpfr_zero_p (diff
);
2292 /* Return true if conversion from an integer to a real loses precision. */
2295 wprecision_int_real (mpz_t n
, mpfr_t r
)
2300 mpfr_get_z (i
, r
, GFC_RND_MODE
);
2302 ret
= mpz_cmp_si (i
, 0) != 0;
2307 /* Convert integers to integers; we can reuse this for also converting
2311 gfc_int2int (gfc_expr
*src
, int kind
)
2316 if (src
->ts
.type
!= BT_INTEGER
&& src
->ts
.type
!= BT_UNSIGNED
)
2319 result
= gfc_get_constant_expr (BT_INTEGER
, kind
, &src
->where
);
2321 mpz_set (result
->value
.integer
, src
->value
.integer
);
2323 if ((rc
= gfc_check_integer_range (result
->value
.integer
, kind
)) != ARITH_OK
)
2325 if (rc
== ARITH_ASYMMETRIC
)
2327 gfc_warning (0, gfc_arith_error (rc
), &src
->where
);
2331 arith_error (rc
, &src
->ts
, &result
->ts
, &src
->where
);
2332 gfc_free_expr (result
);
2337 /* If we do not trap numeric overflow, we need to convert the number to
2338 signed, throwing away high-order bits if necessary. */
2339 if (flag_range_check
== 0)
2343 k
= gfc_validate_kind (BT_INTEGER
, kind
, false);
2344 gfc_convert_mpz_to_signed (result
->value
.integer
,
2345 gfc_integer_kinds
[k
].bit_size
);
2347 if (warn_conversion
&& !src
->do_not_warn
&& kind
< src
->ts
.kind
)
2348 gfc_warning_now (OPT_Wconversion
, "Conversion from %qs to %qs at %L",
2349 gfc_typename (&src
->ts
), gfc_typename (&result
->ts
),
2356 /* Convert integers to reals. */
2359 gfc_int2real (gfc_expr
*src
, int kind
)
2364 if (src
->ts
.type
!= BT_INTEGER
)
2367 result
= gfc_get_constant_expr (BT_REAL
, kind
, &src
->where
);
2369 mpfr_set_z (result
->value
.real
, src
->value
.integer
, GFC_RND_MODE
);
2371 if ((rc
= gfc_check_real_range (result
->value
.real
, kind
)) != ARITH_OK
)
2373 arith_error (rc
, &src
->ts
, &result
->ts
, &src
->where
);
2374 gfc_free_expr (result
);
2379 && wprecision_int_real (src
->value
.integer
, result
->value
.real
))
2380 gfc_warning (OPT_Wconversion
, "Change of value in conversion "
2381 "from %qs to %qs at %L",
2382 gfc_typename (&src
->ts
),
2383 gfc_typename (&result
->ts
),
2390 /* Convert default integer to default complex. */
2393 gfc_int2complex (gfc_expr
*src
, int kind
)
2398 if (src
->ts
.type
!= BT_INTEGER
)
2401 result
= gfc_get_constant_expr (BT_COMPLEX
, kind
, &src
->where
);
2403 mpc_set_z (result
->value
.complex, src
->value
.integer
, GFC_MPC_RND_MODE
);
2405 if ((rc
= gfc_check_real_range (mpc_realref (result
->value
.complex), kind
))
2408 arith_error (rc
, &src
->ts
, &result
->ts
, &src
->where
);
2409 gfc_free_expr (result
);
2414 && wprecision_int_real (src
->value
.integer
,
2415 mpc_realref (result
->value
.complex)))
2416 gfc_warning_now (OPT_Wconversion
, "Change of value in conversion "
2417 "from %qs to %qs at %L",
2418 gfc_typename (&src
->ts
),
2419 gfc_typename (&result
->ts
),
2425 /* Convert unsigned to unsigned, or integer to unsigned. */
2428 gfc_uint2uint (gfc_expr
*src
, int kind
)
2433 if (src
->ts
.type
!= BT_UNSIGNED
&& src
->ts
.type
!= BT_INTEGER
)
2436 result
= gfc_get_constant_expr (BT_UNSIGNED
, kind
, &src
->where
);
2437 mpz_set (result
->value
.integer
, src
->value
.integer
);
2439 rc
= gfc_range_check (result
);
2441 gfc_warning (OPT_Wconversion
, gfc_arith_error (rc
), &result
->where
);
2443 gfc_reduce_unsigned (result
);
2448 gfc_int2uint (gfc_expr
*src
, int kind
)
2450 return gfc_uint2uint (src
, kind
);
2454 gfc_uint2int (gfc_expr
*src
, int kind
)
2456 return gfc_int2int (src
, kind
);
2459 /* Convert UNSIGNED to reals. */
2462 gfc_uint2real (gfc_expr
*src
, int kind
)
2467 if (src
->ts
.type
!= BT_UNSIGNED
)
2470 result
= gfc_get_constant_expr (BT_REAL
, kind
, &src
->where
);
2472 mpfr_set_z (result
->value
.real
, src
->value
.integer
, GFC_RND_MODE
);
2474 if ((rc
= gfc_check_real_range (result
->value
.real
, kind
)) != ARITH_OK
)
2476 /* This should be rare, just in case. */
2477 arith_error (rc
, &src
->ts
, &result
->ts
, &src
->where
);
2478 gfc_free_expr (result
);
2483 && wprecision_int_real (src
->value
.integer
, result
->value
.real
))
2484 gfc_warning (OPT_Wconversion
, "Change of value in conversion "
2485 "from %qs to %qs at %L",
2486 gfc_typename (&src
->ts
),
2487 gfc_typename (&result
->ts
),
2493 /* Convert default integer to default complex. */
2496 gfc_uint2complex (gfc_expr
*src
, int kind
)
2501 if (src
->ts
.type
!= BT_UNSIGNED
)
2504 result
= gfc_get_constant_expr (BT_COMPLEX
, kind
, &src
->where
);
2506 mpc_set_z (result
->value
.complex, src
->value
.integer
, GFC_MPC_RND_MODE
);
2508 if ((rc
= gfc_check_real_range (mpc_realref (result
->value
.complex), kind
))
2511 /* This should be rare, just in case. */
2512 arith_error (rc
, &src
->ts
, &result
->ts
, &src
->where
);
2513 gfc_free_expr (result
);
2518 && wprecision_int_real (src
->value
.integer
,
2519 mpc_realref (result
->value
.complex)))
2520 gfc_warning_now (OPT_Wconversion
, "Change of value in conversion "
2521 "from %qs to %qs at %L",
2522 gfc_typename (&src
->ts
),
2523 gfc_typename (&result
->ts
),
2529 /* Convert default real to default integer. */
2532 gfc_real2int (gfc_expr
*src
, int kind
)
2536 bool did_warn
= false;
2538 if (src
->ts
.type
!= BT_REAL
)
2541 result
= gfc_get_constant_expr (BT_INTEGER
, kind
, &src
->where
);
2543 gfc_mpfr_to_mpz (result
->value
.integer
, src
->value
.real
, &src
->where
);
2545 if ((rc
= gfc_check_integer_range (result
->value
.integer
, kind
)) != ARITH_OK
)
2547 arith_error (rc
, &src
->ts
, &result
->ts
, &src
->where
);
2548 gfc_free_expr (result
);
2552 /* If there was a fractional part, warn about this. */
2554 if (warn_conversion
)
2558 mpfr_frac (f
, src
->value
.real
, GFC_RND_MODE
);
2559 if (mpfr_cmp_si (f
, 0) != 0)
2561 gfc_warning_now (OPT_Wconversion
, "Change of value in conversion "
2562 "from %qs to %qs at %L", gfc_typename (&src
->ts
),
2563 gfc_typename (&result
->ts
), &src
->where
);
2568 if (!did_warn
&& warn_conversion_extra
)
2570 gfc_warning_now (OPT_Wconversion_extra
, "Conversion from %qs to %qs "
2571 "at %L", gfc_typename (&src
->ts
),
2572 gfc_typename (&result
->ts
), &src
->where
);
2578 /* Convert real to unsigned. */
2581 gfc_real2uint (gfc_expr
*src
, int kind
)
2585 bool did_warn
= false;
2587 if (src
->ts
.type
!= BT_REAL
)
2590 result
= gfc_get_constant_expr (BT_UNSIGNED
, kind
, &src
->where
);
2592 gfc_mpfr_to_mpz (result
->value
.integer
, src
->value
.real
, &src
->where
);
2593 if ((rc
= gfc_check_unsigned_range (result
->value
.integer
, kind
)) != ARITH_OK
)
2594 gfc_warning (OPT_Wconversion
, gfc_arith_error (rc
), &result
->where
);
2596 gfc_reduce_unsigned (result
);
2598 /* If there was a fractional part, warn about this. */
2600 if (warn_conversion
)
2604 mpfr_frac (f
, src
->value
.real
, GFC_RND_MODE
);
2605 if (mpfr_cmp_si (f
, 0) != 0)
2607 gfc_warning_now (OPT_Wconversion
, "Change of value in conversion "
2608 "from %qs to %qs at %L", gfc_typename (&src
->ts
),
2609 gfc_typename (&result
->ts
), &src
->where
);
2614 if (!did_warn
&& warn_conversion_extra
)
2616 gfc_warning_now (OPT_Wconversion_extra
, "Conversion from %qs to %qs "
2617 "at %L", gfc_typename (&src
->ts
),
2618 gfc_typename (&result
->ts
), &src
->where
);
2624 /* Convert real to real. */
2627 gfc_real2real (gfc_expr
*src
, int kind
)
2631 bool did_warn
= false;
2633 if (src
->ts
.type
!= BT_REAL
)
2636 result
= gfc_get_constant_expr (BT_REAL
, kind
, &src
->where
);
2638 mpfr_set (result
->value
.real
, src
->value
.real
, GFC_RND_MODE
);
2640 rc
= gfc_check_real_range (result
->value
.real
, kind
);
2642 if (rc
== ARITH_UNDERFLOW
)
2645 gfc_warning (OPT_Woverflow
, gfc_arith_error (rc
), &src
->where
);
2646 mpfr_set_ui (result
->value
.real
, 0, GFC_RND_MODE
);
2648 else if (rc
!= ARITH_OK
)
2650 arith_error (rc
, &src
->ts
, &result
->ts
, &src
->where
);
2651 gfc_free_expr (result
);
2655 /* As a special bonus, don't warn about REAL values which are not changed by
2656 the conversion if -Wconversion is specified and -Wconversion-extra is
2659 if ((warn_conversion
|| warn_conversion_extra
) && src
->ts
.kind
> kind
)
2661 int w
= warn_conversion
? OPT_Wconversion
: OPT_Wconversion_extra
;
2663 /* Calculate the difference between the constant and the rounded
2664 value and check it against zero. */
2666 if (wprecision_real_real (src
->value
.real
, src
->ts
.kind
, kind
))
2668 gfc_warning_now (w
, "Change of value in conversion from "
2670 gfc_typename (&src
->ts
), gfc_typename (&result
->ts
),
2672 /* Make sure the conversion warning is not emitted again. */
2677 if (!did_warn
&& warn_conversion_extra
)
2678 gfc_warning_now (OPT_Wconversion_extra
, "Conversion from %qs to %qs "
2679 "at %L", gfc_typename(&src
->ts
),
2680 gfc_typename(&result
->ts
), &src
->where
);
2686 /* Convert real to complex. */
2689 gfc_real2complex (gfc_expr
*src
, int kind
)
2693 bool did_warn
= false;
2695 if (src
->ts
.type
!= BT_REAL
)
2698 result
= gfc_get_constant_expr (BT_COMPLEX
, kind
, &src
->where
);
2700 mpc_set_fr (result
->value
.complex, src
->value
.real
, GFC_MPC_RND_MODE
);
2702 rc
= gfc_check_real_range (mpc_realref (result
->value
.complex), kind
);
2704 if (rc
== ARITH_UNDERFLOW
)
2707 gfc_warning (OPT_Woverflow
, gfc_arith_error (rc
), &src
->where
);
2708 mpfr_set_ui (mpc_realref (result
->value
.complex), 0, GFC_RND_MODE
);
2710 else if (rc
!= ARITH_OK
)
2712 arith_error (rc
, &src
->ts
, &result
->ts
, &src
->where
);
2713 gfc_free_expr (result
);
2717 if ((warn_conversion
|| warn_conversion_extra
) && src
->ts
.kind
> kind
)
2719 int w
= warn_conversion
? OPT_Wconversion
: OPT_Wconversion_extra
;
2721 if (wprecision_real_real (src
->value
.real
, src
->ts
.kind
, kind
))
2723 gfc_warning_now (w
, "Change of value in conversion from "
2725 gfc_typename (&src
->ts
), gfc_typename (&result
->ts
),
2727 /* Make sure the conversion warning is not emitted again. */
2732 if (!did_warn
&& warn_conversion_extra
)
2733 gfc_warning_now (OPT_Wconversion_extra
, "Conversion from %qs to %qs "
2734 "at %L", gfc_typename(&src
->ts
),
2735 gfc_typename(&result
->ts
), &src
->where
);
2741 /* Convert complex to integer. */
2744 gfc_complex2int (gfc_expr
*src
, int kind
)
2748 bool did_warn
= false;
2750 if (src
->ts
.type
!= BT_COMPLEX
)
2753 result
= gfc_get_constant_expr (BT_INTEGER
, kind
, &src
->where
);
2755 gfc_mpfr_to_mpz (result
->value
.integer
, mpc_realref (src
->value
.complex),
2758 if ((rc
= gfc_check_integer_range (result
->value
.integer
, kind
)) != ARITH_OK
)
2760 arith_error (rc
, &src
->ts
, &result
->ts
, &src
->where
);
2761 gfc_free_expr (result
);
2765 if (warn_conversion
|| warn_conversion_extra
)
2767 int w
= warn_conversion
? OPT_Wconversion
: OPT_Wconversion_extra
;
2769 /* See if we discarded an imaginary part. */
2770 if (mpfr_cmp_si (mpc_imagref (src
->value
.complex), 0) != 0)
2772 gfc_warning_now (w
, "Non-zero imaginary part discarded "
2773 "in conversion from %qs to %qs at %L",
2774 gfc_typename(&src
->ts
), gfc_typename (&result
->ts
),
2783 mpfr_frac (f
, src
->value
.real
, GFC_RND_MODE
);
2784 if (mpfr_cmp_si (f
, 0) != 0)
2786 gfc_warning_now (w
, "Change of value in conversion from "
2787 "%qs to %qs at %L", gfc_typename (&src
->ts
),
2788 gfc_typename (&result
->ts
), &src
->where
);
2794 if (!did_warn
&& warn_conversion_extra
)
2796 gfc_warning_now (OPT_Wconversion_extra
, "Conversion from %qs to %qs "
2797 "at %L", gfc_typename (&src
->ts
),
2798 gfc_typename (&result
->ts
), &src
->where
);
2805 /* Convert complex to integer. */
2808 gfc_complex2uint (gfc_expr
*src
, int kind
)
2812 bool did_warn
= false;
2814 if (src
->ts
.type
!= BT_COMPLEX
)
2817 result
= gfc_get_constant_expr (BT_UNSIGNED
, kind
, &src
->where
);
2819 gfc_mpfr_to_mpz (result
->value
.integer
, mpc_realref (src
->value
.complex),
2822 if ((rc
= gfc_check_unsigned_range (result
->value
.integer
, kind
)) != ARITH_OK
)
2823 gfc_warning (OPT_Wconversion
, gfc_arith_error (rc
), &result
->where
);
2825 gfc_reduce_unsigned (result
);
2827 if (warn_conversion
|| warn_conversion_extra
)
2829 int w
= warn_conversion
? OPT_Wconversion
: OPT_Wconversion_extra
;
2831 /* See if we discarded an imaginary part. */
2832 if (mpfr_cmp_si (mpc_imagref (src
->value
.complex), 0) != 0)
2834 gfc_warning_now (w
, "Non-zero imaginary part discarded "
2835 "in conversion from %qs to %qs at %L",
2836 gfc_typename(&src
->ts
), gfc_typename (&result
->ts
),
2846 mpfr_frac (f
, src
->value
.real
, GFC_RND_MODE
);
2847 if (mpfr_cmp_si (f
, 0) != 0)
2849 gfc_warning_now (w
, "Change of value in conversion from "
2850 "%qs to %qs at %L", gfc_typename (&src
->ts
),
2851 gfc_typename (&result
->ts
), &src
->where
);
2857 if (!did_warn
&& warn_conversion_extra
)
2859 gfc_warning_now (OPT_Wconversion_extra
, "Conversion from %qs to %qs "
2860 "at %L", gfc_typename (&src
->ts
),
2861 gfc_typename (&result
->ts
), &src
->where
);
2869 /* Convert complex to real. */
2872 gfc_complex2real (gfc_expr
*src
, int kind
)
2876 bool did_warn
= false;
2878 if (src
->ts
.type
!= BT_COMPLEX
)
2881 result
= gfc_get_constant_expr (BT_REAL
, kind
, &src
->where
);
2883 mpc_real (result
->value
.real
, src
->value
.complex, GFC_RND_MODE
);
2885 rc
= gfc_check_real_range (result
->value
.real
, kind
);
2887 if (rc
== ARITH_UNDERFLOW
)
2890 gfc_warning (OPT_Woverflow
, gfc_arith_error (rc
), &src
->where
);
2891 mpfr_set_ui (result
->value
.real
, 0, GFC_RND_MODE
);
2895 arith_error (rc
, &src
->ts
, &result
->ts
, &src
->where
);
2896 gfc_free_expr (result
);
2900 if (warn_conversion
|| warn_conversion_extra
)
2902 int w
= warn_conversion
? OPT_Wconversion
: OPT_Wconversion_extra
;
2904 /* See if we discarded an imaginary part. */
2905 if (mpfr_cmp_si (mpc_imagref (src
->value
.complex), 0) != 0)
2907 gfc_warning (w
, "Non-zero imaginary part discarded "
2908 "in conversion from %qs to %qs at %L",
2909 gfc_typename(&src
->ts
), gfc_typename (&result
->ts
),
2914 /* Calculate the difference between the real constant and the rounded
2915 value and check it against zero. */
2917 if (kind
> src
->ts
.kind
2918 && wprecision_real_real (mpc_realref (src
->value
.complex),
2919 src
->ts
.kind
, kind
))
2921 gfc_warning_now (w
, "Change of value in conversion from "
2923 gfc_typename (&src
->ts
), gfc_typename (&result
->ts
),
2925 /* Make sure the conversion warning is not emitted again. */
2930 if (!did_warn
&& warn_conversion_extra
)
2931 gfc_warning_now (OPT_Wconversion
, "Conversion from %qs to %qs at %L",
2932 gfc_typename(&src
->ts
), gfc_typename (&result
->ts
),
2939 /* Convert complex to complex. */
2942 gfc_complex2complex (gfc_expr
*src
, int kind
)
2946 bool did_warn
= false;
2948 if (src
->ts
.type
!= BT_COMPLEX
)
2951 result
= gfc_get_constant_expr (BT_COMPLEX
, kind
, &src
->where
);
2953 mpc_set (result
->value
.complex, src
->value
.complex, GFC_MPC_RND_MODE
);
2955 rc
= gfc_check_real_range (mpc_realref (result
->value
.complex), kind
);
2957 if (rc
== ARITH_UNDERFLOW
)
2960 gfc_warning (OPT_Woverflow
, gfc_arith_error (rc
), &src
->where
);
2961 mpfr_set_ui (mpc_realref (result
->value
.complex), 0, GFC_RND_MODE
);
2963 else if (rc
!= ARITH_OK
)
2965 arith_error (rc
, &src
->ts
, &result
->ts
, &src
->where
);
2966 gfc_free_expr (result
);
2970 rc
= gfc_check_real_range (mpc_imagref (result
->value
.complex), kind
);
2972 if (rc
== ARITH_UNDERFLOW
)
2975 gfc_warning (OPT_Woverflow
, gfc_arith_error (rc
), &src
->where
);
2976 mpfr_set_ui (mpc_imagref (result
->value
.complex), 0, GFC_RND_MODE
);
2978 else if (rc
!= ARITH_OK
)
2980 arith_error (rc
, &src
->ts
, &result
->ts
, &src
->where
);
2981 gfc_free_expr (result
);
2985 if ((warn_conversion
|| warn_conversion_extra
) && src
->ts
.kind
> kind
2986 && (wprecision_real_real (mpc_realref (src
->value
.complex),
2988 || wprecision_real_real (mpc_imagref (src
->value
.complex),
2989 src
->ts
.kind
, kind
)))
2991 int w
= warn_conversion
? OPT_Wconversion
: OPT_Wconversion_extra
;
2993 gfc_warning_now (w
, "Change of value in conversion from "
2995 gfc_typename (&src
->ts
), gfc_typename (&result
->ts
),
3000 if (!did_warn
&& warn_conversion_extra
&& src
->ts
.kind
!= kind
)
3001 gfc_warning_now (OPT_Wconversion_extra
, "Conversion from %qs to %qs "
3002 "at %L", gfc_typename(&src
->ts
),
3003 gfc_typename (&result
->ts
), &src
->where
);
3009 /* Logical kind conversion. */
3012 gfc_log2log (gfc_expr
*src
, int kind
)
3016 if (src
->ts
.type
!= BT_LOGICAL
)
3019 result
= gfc_get_constant_expr (BT_LOGICAL
, kind
, &src
->where
);
3020 result
->value
.logical
= src
->value
.logical
;
3026 /* Convert logical to integer. */
3029 gfc_log2int (gfc_expr
*src
, int kind
)
3033 if (src
->ts
.type
!= BT_LOGICAL
)
3036 result
= gfc_get_constant_expr (BT_INTEGER
, kind
, &src
->where
);
3037 mpz_set_si (result
->value
.integer
, src
->value
.logical
);
3042 /* Convert logical to unsigned. */
3045 gfc_log2uint (gfc_expr
*src
, int kind
)
3049 if (src
->ts
.type
!= BT_LOGICAL
)
3052 result
= gfc_get_constant_expr (BT_UNSIGNED
, kind
, &src
->where
);
3053 mpz_set_si (result
->value
.integer
, src
->value
.logical
);
3059 /* Convert integer to logical. */
3062 gfc_int2log (gfc_expr
*src
, int kind
)
3066 if (src
->ts
.type
!= BT_INTEGER
)
3069 result
= gfc_get_constant_expr (BT_LOGICAL
, kind
, &src
->where
);
3070 result
->value
.logical
= (mpz_cmp_si (src
->value
.integer
, 0) != 0);
3075 /* Convert unsigned to logical. */
3078 gfc_uint2log (gfc_expr
*src
, int kind
)
3082 if (src
->ts
.type
!= BT_UNSIGNED
)
3085 result
= gfc_get_constant_expr (BT_LOGICAL
, kind
, &src
->where
);
3086 result
->value
.logical
= (mpz_cmp_si (src
->value
.integer
, 0) != 0);
3091 /* Convert character to character. We only use wide strings internally,
3092 so we only set the kind. */
3095 gfc_character2character (gfc_expr
*src
, int kind
)
3098 result
= gfc_copy_expr (src
);
3099 result
->ts
.kind
= kind
;
3104 /* Helper function to set the representation in a Hollerith conversion.
3105 This assumes that the ts.type and ts.kind of the result have already
3109 hollerith2representation (gfc_expr
*result
, gfc_expr
*src
)
3111 size_t src_len
, result_len
;
3113 src_len
= src
->representation
.length
- src
->ts
.u
.pad
;
3114 gfc_target_expr_size (result
, &result_len
);
3116 if (src_len
> result_len
)
3118 gfc_warning (OPT_Wcharacter_truncation
, "The Hollerith constant at %L "
3119 "is truncated in conversion to %qs", &src
->where
,
3120 gfc_typename(&result
->ts
));
3123 result
->representation
.string
= XCNEWVEC (char, result_len
+ 1);
3124 memcpy (result
->representation
.string
, src
->representation
.string
,
3125 MIN (result_len
, src_len
));
3127 if (src_len
< result_len
)
3128 memset (&result
->representation
.string
[src_len
], ' ', result_len
- src_len
);
3130 result
->representation
.string
[result_len
] = '\0'; /* For debugger */
3131 result
->representation
.length
= result_len
;
3135 /* Helper function to set the representation in a character conversion.
3136 This assumes that the ts.type and ts.kind of the result have already
3140 character2representation (gfc_expr
*result
, gfc_expr
*src
)
3142 size_t src_len
, result_len
, i
;
3143 src_len
= src
->value
.character
.length
;
3144 gfc_target_expr_size (result
, &result_len
);
3146 if (src_len
> result_len
)
3147 gfc_warning (OPT_Wcharacter_truncation
, "The character constant at %L is "
3148 "truncated in conversion to %s", &src
->where
,
3149 gfc_typename(&result
->ts
));
3151 result
->representation
.string
= XCNEWVEC (char, result_len
+ 1);
3153 for (i
= 0; i
< MIN (result_len
, src_len
); i
++)
3154 result
->representation
.string
[i
] = (char) src
->value
.character
.string
[i
];
3156 if (src_len
< result_len
)
3157 memset (&result
->representation
.string
[src_len
], ' ',
3158 result_len
- src_len
);
3160 result
->representation
.string
[result_len
] = '\0'; /* For debugger. */
3161 result
->representation
.length
= result_len
;
3164 /* Convert Hollerith to integer. The constant will be padded or truncated. */
3167 gfc_hollerith2int (gfc_expr
*src
, int kind
)
3170 result
= gfc_get_constant_expr (BT_INTEGER
, kind
, &src
->where
);
3172 hollerith2representation (result
, src
);
3173 gfc_interpret_integer (kind
, (unsigned char *) result
->representation
.string
,
3174 result
->representation
.length
, result
->value
.integer
);
3179 /* Convert character to integer. The constant will be padded or truncated. */
3182 gfc_character2int (gfc_expr
*src
, int kind
)
3185 result
= gfc_get_constant_expr (BT_INTEGER
, kind
, &src
->where
);
3187 character2representation (result
, src
);
3188 gfc_interpret_integer (kind
, (unsigned char *) result
->representation
.string
,
3189 result
->representation
.length
, result
->value
.integer
);
3193 /* Convert Hollerith to real. The constant will be padded or truncated. */
3196 gfc_hollerith2real (gfc_expr
*src
, int kind
)
3199 result
= gfc_get_constant_expr (BT_REAL
, kind
, &src
->where
);
3201 hollerith2representation (result
, src
);
3202 if (gfc_interpret_float (kind
,
3203 (unsigned char *) result
->representation
.string
,
3204 result
->representation
.length
, result
->value
.real
))
3210 /* Convert character to real. The constant will be padded or truncated. */
3213 gfc_character2real (gfc_expr
*src
, int kind
)
3216 result
= gfc_get_constant_expr (BT_REAL
, kind
, &src
->where
);
3218 character2representation (result
, src
);
3219 gfc_interpret_float (kind
, (unsigned char *) result
->representation
.string
,
3220 result
->representation
.length
, result
->value
.real
);
3226 /* Convert Hollerith to complex. The constant will be padded or truncated. */
3229 gfc_hollerith2complex (gfc_expr
*src
, int kind
)
3232 result
= gfc_get_constant_expr (BT_COMPLEX
, kind
, &src
->where
);
3234 hollerith2representation (result
, src
);
3235 gfc_interpret_complex (kind
, (unsigned char *) result
->representation
.string
,
3236 result
->representation
.length
, result
->value
.complex);
3241 /* Convert character to complex. The constant will be padded or truncated. */
3244 gfc_character2complex (gfc_expr
*src
, int kind
)
3247 result
= gfc_get_constant_expr (BT_COMPLEX
, kind
, &src
->where
);
3249 character2representation (result
, src
);
3250 gfc_interpret_complex (kind
, (unsigned char *) result
->representation
.string
,
3251 result
->representation
.length
, result
->value
.complex);
3257 /* Convert Hollerith to character. */
3260 gfc_hollerith2character (gfc_expr
*src
, int kind
)
3264 result
= gfc_copy_expr (src
);
3265 result
->ts
.type
= BT_CHARACTER
;
3266 result
->ts
.kind
= kind
;
3267 result
->ts
.u
.pad
= 0;
3269 result
->value
.character
.length
= result
->representation
.length
;
3270 result
->value
.character
.string
3271 = gfc_char_to_widechar (result
->representation
.string
);
3277 /* Convert Hollerith to logical. The constant will be padded or truncated. */
3280 gfc_hollerith2logical (gfc_expr
*src
, int kind
)
3283 result
= gfc_get_constant_expr (BT_LOGICAL
, kind
, &src
->where
);
3285 hollerith2representation (result
, src
);
3286 gfc_interpret_logical (kind
, (unsigned char *) result
->representation
.string
,
3287 result
->representation
.length
, &result
->value
.logical
);
3292 /* Convert character to logical. The constant will be padded or truncated. */
3295 gfc_character2logical (gfc_expr
*src
, int kind
)
3298 result
= gfc_get_constant_expr (BT_LOGICAL
, kind
, &src
->where
);
3300 character2representation (result
, src
);
3301 gfc_interpret_logical (kind
, (unsigned char *) result
->representation
.string
,
3302 result
->representation
.length
, &result
->value
.logical
);