]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/convert.c
Make Filesystem TS tests pass in C++17 mode
[thirdparty/gcc.git] / gcc / convert.c
CommitLineData
5e6908ea 1/* Utility routines for data type conversion for GCC.
85ec4feb 2 Copyright (C) 1987-2018 Free Software Foundation, Inc.
76e616db 3
1322177d 4This file is part of GCC.
76e616db 5
1322177d
LB
6GCC is free software; you can redistribute it and/or modify it under
7the terms of the GNU General Public License as published by the Free
9dcd6f09 8Software Foundation; either version 3, or (at your option) any later
1322177d 9version.
76e616db 10
1322177d
LB
11GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12WARRANTY; without even the implied warranty of MERCHANTABILITY or
13FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14for more details.
76e616db
BK
15
16You should have received a copy of the GNU General Public License
9dcd6f09
NC
17along with GCC; see the file COPYING3. If not see
18<http://www.gnu.org/licenses/>. */
76e616db
BK
19
20
21/* These routines are somewhat language-independent utility function
0f41302f 22 intended to be called by the language-specific convert () functions. */
76e616db
BK
23
24#include "config.h"
c5c76735 25#include "system.h"
4977bab6 26#include "coretypes.h"
957060b5 27#include "target.h"
76e616db 28#include "tree.h"
957060b5 29#include "diagnostic-core.h"
40e23961 30#include "fold-const.h"
d8a2d370 31#include "stor-layout.h"
76e616db 32#include "convert.h"
b0c48229 33#include "langhooks.h"
9b2b7279 34#include "builtins.h"
85a16bf8 35#include "ubsan.h"
314e6352
ML
36#include "stringpool.h"
37#include "attribs.h"
45b2222a 38#include "asan.h"
76e616db 39
269e63b7
KT
40#define maybe_fold_build1_loc(FOLD_P, LOC, CODE, TYPE, EXPR) \
41 ((FOLD_P) ? fold_build1_loc (LOC, CODE, TYPE, EXPR) \
42 : build1_loc (LOC, CODE, TYPE, EXPR))
43#define maybe_fold_build2_loc(FOLD_P, LOC, CODE, TYPE, EXPR1, EXPR2) \
44 ((FOLD_P) ? fold_build2_loc (LOC, CODE, TYPE, EXPR1, EXPR2) \
45 : build2_loc (LOC, CODE, TYPE, EXPR1, EXPR2))
46
0a931ce5 47/* Convert EXPR to some pointer or reference type TYPE.
98c76e3c 48 EXPR must be pointer, reference, integer, enumeral, or literal zero;
269e63b7
KT
49 in other cases error is called. If FOLD_P is true, try to fold the
50 expression. */
76e616db 51
269e63b7
KT
52static tree
53convert_to_pointer_1 (tree type, tree expr, bool fold_p)
76e616db 54{
db3927fb 55 location_t loc = EXPR_LOCATION (expr);
0a931ce5
RS
56 if (TREE_TYPE (expr) == type)
57 return expr;
58
f5963e61 59 switch (TREE_CODE (TREE_TYPE (expr)))
76e616db 60 {
f5963e61
JL
61 case POINTER_TYPE:
62 case REFERENCE_TYPE:
09e881c9
BE
63 {
64 /* If the pointers point to different address spaces, conversion needs
65 to be done via a ADDR_SPACE_CONVERT_EXPR instead of a NOP_EXPR. */
66 addr_space_t to_as = TYPE_ADDR_SPACE (TREE_TYPE (type));
67 addr_space_t from_as = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (expr)));
68
69 if (to_as == from_as)
269e63b7 70 return maybe_fold_build1_loc (fold_p, loc, NOP_EXPR, type, expr);
09e881c9 71 else
269e63b7
KT
72 return maybe_fold_build1_loc (fold_p, loc, ADDR_SPACE_CONVERT_EXPR,
73 type, expr);
09e881c9 74 }
f5963e61
JL
75
76 case INTEGER_TYPE:
77 case ENUMERAL_TYPE:
78 case BOOLEAN_TYPE:
cf157324
OH
79 {
80 /* If the input precision differs from the target pointer type
81 precision, first convert the input expression to an integer type of
82 the target precision. Some targets, e.g. VMS, need several pointer
83 sizes to coexist so the latter isn't necessarily POINTER_SIZE. */
84 unsigned int pprec = TYPE_PRECISION (type);
85 unsigned int eprec = TYPE_PRECISION (TREE_TYPE (expr));
86
269e63b7
KT
87 if (eprec != pprec)
88 expr
89 = maybe_fold_build1_loc (fold_p, loc, NOP_EXPR,
90 lang_hooks.types.type_for_size (pprec, 0),
91 expr);
cf157324 92 }
269e63b7 93 return maybe_fold_build1_loc (fold_p, loc, CONVERT_EXPR, type, expr);
76e616db 94
f5963e61
JL
95 default:
96 error ("cannot convert to a pointer type");
269e63b7 97 return convert_to_pointer_1 (type, integer_zero_node, fold_p);
f5963e61 98 }
76e616db
BK
99}
100
269e63b7
KT
101/* A wrapper around convert_to_pointer_1 that always folds the
102 expression. */
103
104tree
105convert_to_pointer (tree type, tree expr)
106{
107 return convert_to_pointer_1 (type, expr, true);
108}
109
110/* A wrapper around convert_to_pointer_1 that only folds the
415594bb 111 expression if DOFOLD, or if it is CONSTANT_CLASS_P. */
269e63b7
KT
112
113tree
415594bb 114convert_to_pointer_maybe_fold (tree type, tree expr, bool dofold)
269e63b7 115{
415594bb 116 return convert_to_pointer_1 (type, expr, dofold || CONSTANT_CLASS_P (expr));
269e63b7 117}
4977bab6 118
76e616db
BK
119/* Convert EXPR to some floating-point type TYPE.
120
0f996086 121 EXPR must be float, fixed-point, integer, or enumeral;
269e63b7
KT
122 in other cases error is called. If FOLD_P is true, try to fold
123 the expression. */
76e616db 124
269e63b7
KT
125static tree
126convert_to_real_1 (tree type, tree expr, bool fold_p)
76e616db 127{
27a6aa72 128 enum built_in_function fcode = builtin_mathfn_code (expr);
4977bab6 129 tree itype = TREE_TYPE (expr);
269e63b7 130 location_t loc = EXPR_LOCATION (expr);
4977bab6 131
c05eeebc
JJ
132 if (TREE_CODE (expr) == COMPOUND_EXPR)
133 {
269e63b7 134 tree t = convert_to_real_1 (type, TREE_OPERAND (expr, 1), fold_p);
c05eeebc
JJ
135 if (t == TREE_OPERAND (expr, 1))
136 return expr;
137 return build2_loc (EXPR_LOCATION (expr), COMPOUND_EXPR, TREE_TYPE (t),
138 TREE_OPERAND (expr, 0), t);
139 }
140
4b207444
JH
141 /* Disable until we figure out how to decide whether the functions are
142 present in runtime. */
4977bab6 143 /* Convert (float)sqrt((double)x) where x is float into sqrtf(x) */
78bd5210 144 if (optimize
4977bab6
ZW
145 && (TYPE_MODE (type) == TYPE_MODE (double_type_node)
146 || TYPE_MODE (type) == TYPE_MODE (float_type_node)))
147 {
b3810360
KG
148 switch (fcode)
149 {
150#define CASE_MATHFN(FN) case BUILT_IN_##FN: case BUILT_IN_##FN##L:
1fb7e3af 151 CASE_MATHFN (COSH)
b3810360 152 CASE_MATHFN (EXP)
1fb7e3af
KG
153 CASE_MATHFN (EXP10)
154 CASE_MATHFN (EXP2)
f060a261 155 CASE_MATHFN (EXPM1)
1fb7e3af
KG
156 CASE_MATHFN (GAMMA)
157 CASE_MATHFN (J0)
158 CASE_MATHFN (J1)
159 CASE_MATHFN (LGAMMA)
1fb7e3af 160 CASE_MATHFN (POW10)
1fb7e3af 161 CASE_MATHFN (SINH)
1fb7e3af
KG
162 CASE_MATHFN (TGAMMA)
163 CASE_MATHFN (Y0)
164 CASE_MATHFN (Y1)
f060a261
RG
165 /* The above functions may set errno differently with float
166 input or output so this transformation is not safe with
167 -fmath-errno. */
168 if (flag_errno_math)
169 break;
81fea426 170 gcc_fallthrough ();
f060a261
RG
171 CASE_MATHFN (ACOS)
172 CASE_MATHFN (ACOSH)
173 CASE_MATHFN (ASIN)
174 CASE_MATHFN (ASINH)
175 CASE_MATHFN (ATAN)
176 CASE_MATHFN (ATANH)
177 CASE_MATHFN (CBRT)
178 CASE_MATHFN (COS)
179 CASE_MATHFN (ERF)
180 CASE_MATHFN (ERFC)
f060a261
RG
181 CASE_MATHFN (LOG)
182 CASE_MATHFN (LOG10)
183 CASE_MATHFN (LOG2)
184 CASE_MATHFN (LOG1P)
f060a261 185 CASE_MATHFN (SIN)
f060a261
RG
186 CASE_MATHFN (TAN)
187 CASE_MATHFN (TANH)
247dbcf4
CH
188 /* The above functions are not safe to do this conversion. */
189 if (!flag_unsafe_math_optimizations)
190 break;
81fea426 191 gcc_fallthrough ();
247dbcf4
CH
192 CASE_MATHFN (SQRT)
193 CASE_MATHFN (FABS)
194 CASE_MATHFN (LOGB)
b3810360 195#undef CASE_MATHFN
4977bab6 196 {
5039610b 197 tree arg0 = strip_float_extensions (CALL_EXPR_ARG (expr, 0));
b3810360
KG
198 tree newtype = type;
199
200 /* We have (outertype)sqrt((innertype)x). Choose the wider mode from
201 the both as the safe type for operation. */
202 if (TYPE_PRECISION (TREE_TYPE (arg0)) > TYPE_PRECISION (type))
203 newtype = TREE_TYPE (arg0);
204
247dbcf4
CH
205 /* We consider to convert
206
207 (T1) sqrtT2 ((T2) exprT3)
208 to
209 (T1) sqrtT4 ((T4) exprT3)
210
211 , where T1 is TYPE, T2 is ITYPE, T3 is TREE_TYPE (ARG0),
212 and T4 is NEWTYPE. All those types are of floating point types.
213 T4 (NEWTYPE) should be narrower than T2 (ITYPE). This conversion
214 is safe only if P1 >= P2*2+2, where P1 and P2 are precisions of
215 T2 and T4. See the following URL for a reference:
216 http://stackoverflow.com/questions/9235456/determining-
217 floating-point-square-root
218 */
219 if ((fcode == BUILT_IN_SQRT || fcode == BUILT_IN_SQRTL)
220 && !flag_unsafe_math_optimizations)
221 {
222 /* The following conversion is unsafe even the precision condition
223 below is satisfied:
224
225 (float) sqrtl ((long double) double_val) -> (float) sqrt (double_val)
226 */
227 if (TYPE_MODE (type) != TYPE_MODE (newtype))
228 break;
229
230 int p1 = REAL_MODE_FORMAT (TYPE_MODE (itype))->p;
231 int p2 = REAL_MODE_FORMAT (TYPE_MODE (newtype))->p;
232 if (p1 < p2 * 2 + 2)
233 break;
234 }
235
b3810360
KG
236 /* Be careful about integer to fp conversions.
237 These may overflow still. */
238 if (FLOAT_TYPE_P (TREE_TYPE (arg0))
239 && TYPE_PRECISION (newtype) < TYPE_PRECISION (itype)
240 && (TYPE_MODE (newtype) == TYPE_MODE (double_type_node)
241 || TYPE_MODE (newtype) == TYPE_MODE (float_type_node)))
247dbcf4 242 {
b3810360 243 tree fn = mathfn_built_in (newtype, fcode);
b3810360 244 if (fn)
269e63b7
KT
245 {
246 tree arg = convert_to_real_1 (newtype, arg0, fold_p);
247 expr = build_call_expr (fn, 1, arg);
248 if (newtype == type)
249 return expr;
250 }
b3810360 251 }
4977bab6 252 }
b3810360
KG
253 default:
254 break;
4977bab6
ZW
255 }
256 }
257
258 /* Propagate the cast into the operation. */
259 if (itype != type && FLOAT_TYPE_P (type))
260 switch (TREE_CODE (expr))
261 {
4f76e46b 262 /* Convert (float)-x into -(float)x. This is safe for
18b0ea8f 263 round-to-nearest rounding mode when the inner type is float. */
4977bab6
ZW
264 case ABS_EXPR:
265 case NEGATE_EXPR:
4f76e46b 266 if (!flag_rounding_math
18b0ea8f
MM
267 && FLOAT_TYPE_P (itype)
268 && TYPE_PRECISION (type) < TYPE_PRECISION (itype))
269e63b7
KT
269 {
270 tree arg = convert_to_real_1 (type, TREE_OPERAND (expr, 0),
271 fold_p);
272 return build1 (TREE_CODE (expr), type, arg);
273 }
b1a6f8db 274 break;
beb235f8 275 /* Convert (outertype)((innertype0)a+(innertype1)b)
4977bab6
ZW
276 into ((newtype)a+(newtype)b) where newtype
277 is the widest mode from all of these. */
278 case PLUS_EXPR:
279 case MINUS_EXPR:
280 case MULT_EXPR:
281 case RDIV_EXPR:
282 {
283 tree arg0 = strip_float_extensions (TREE_OPERAND (expr, 0));
284 tree arg1 = strip_float_extensions (TREE_OPERAND (expr, 1));
285
286 if (FLOAT_TYPE_P (TREE_TYPE (arg0))
20ded7a6
JM
287 && FLOAT_TYPE_P (TREE_TYPE (arg1))
288 && DECIMAL_FLOAT_TYPE_P (itype) == DECIMAL_FLOAT_TYPE_P (type))
4977bab6
ZW
289 {
290 tree newtype = type;
15ed7b52
JG
291
292 if (TYPE_MODE (TREE_TYPE (arg0)) == SDmode
20ded7a6
JM
293 || TYPE_MODE (TREE_TYPE (arg1)) == SDmode
294 || TYPE_MODE (type) == SDmode)
15ed7b52
JG
295 newtype = dfloat32_type_node;
296 if (TYPE_MODE (TREE_TYPE (arg0)) == DDmode
20ded7a6
JM
297 || TYPE_MODE (TREE_TYPE (arg1)) == DDmode
298 || TYPE_MODE (type) == DDmode)
15ed7b52
JG
299 newtype = dfloat64_type_node;
300 if (TYPE_MODE (TREE_TYPE (arg0)) == TDmode
20ded7a6
JM
301 || TYPE_MODE (TREE_TYPE (arg1)) == TDmode
302 || TYPE_MODE (type) == TDmode)
15ed7b52
JG
303 newtype = dfloat128_type_node;
304 if (newtype == dfloat32_type_node
305 || newtype == dfloat64_type_node
306 || newtype == dfloat128_type_node)
307 {
308 expr = build2 (TREE_CODE (expr), newtype,
269e63b7
KT
309 convert_to_real_1 (newtype, arg0,
310 fold_p),
311 convert_to_real_1 (newtype, arg1,
312 fold_p));
15ed7b52
JG
313 if (newtype == type)
314 return expr;
315 break;
316 }
317
4977bab6
ZW
318 if (TYPE_PRECISION (TREE_TYPE (arg0)) > TYPE_PRECISION (newtype))
319 newtype = TREE_TYPE (arg0);
320 if (TYPE_PRECISION (TREE_TYPE (arg1)) > TYPE_PRECISION (newtype))
321 newtype = TREE_TYPE (arg1);
20ded7a6
JM
322 /* Sometimes this transformation is safe (cannot
323 change results through affecting double rounding
324 cases) and sometimes it is not. If NEWTYPE is
325 wider than TYPE, e.g. (float)((long double)double
326 + (long double)double) converted to
327 (float)(double + double), the transformation is
328 unsafe regardless of the details of the types
329 involved; double rounding can arise if the result
330 of NEWTYPE arithmetic is a NEWTYPE value half way
331 between two representable TYPE values but the
332 exact value is sufficiently different (in the
333 right direction) for this difference to be
334 visible in ITYPE arithmetic. If NEWTYPE is the
335 same as TYPE, however, the transformation may be
336 safe depending on the types involved: it is safe
337 if the ITYPE has strictly more than twice as many
338 mantissa bits as TYPE, can represent infinities
339 and NaNs if the TYPE can, and has sufficient
340 exponent range for the product or ratio of two
341 values representable in the TYPE to be within the
342 range of normal values of ITYPE. */
343 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (itype)
344 && (flag_unsafe_math_optimizations
345 || (TYPE_PRECISION (newtype) == TYPE_PRECISION (type)
346 && real_can_shorten_arithmetic (TYPE_MODE (itype),
8ce94e44
JM
347 TYPE_MODE (type))
348 && !excess_precision_type (newtype))))
4977bab6 349 {
3244e67d 350 expr = build2 (TREE_CODE (expr), newtype,
269e63b7
KT
351 convert_to_real_1 (newtype, arg0,
352 fold_p),
353 convert_to_real_1 (newtype, arg1,
354 fold_p));
4977bab6
ZW
355 if (newtype == type)
356 return expr;
357 }
358 }
359 }
360 break;
361 default:
362 break;
363 }
364
f5963e61
JL
365 switch (TREE_CODE (TREE_TYPE (expr)))
366 {
367 case REAL_TYPE:
5fc89bfd
JJ
368 /* Ignore the conversion if we don't need to store intermediate
369 results and neither type is a decimal float. */
e87eed2a
DM
370 return build1_loc (loc,
371 (flag_float_store
372 || DECIMAL_FLOAT_TYPE_P (type)
373 || DECIMAL_FLOAT_TYPE_P (itype))
374 ? CONVERT_EXPR : NOP_EXPR, type, expr);
f5963e61
JL
375
376 case INTEGER_TYPE:
377 case ENUMERAL_TYPE:
378 case BOOLEAN_TYPE:
f5963e61
JL
379 return build1 (FLOAT_EXPR, type, expr);
380
0f996086
CF
381 case FIXED_POINT_TYPE:
382 return build1 (FIXED_CONVERT_EXPR, type, expr);
383
f5963e61
JL
384 case COMPLEX_TYPE:
385 return convert (type,
269e63b7
KT
386 maybe_fold_build1_loc (fold_p, loc, REALPART_EXPR,
387 TREE_TYPE (TREE_TYPE (expr)),
388 expr));
f5963e61
JL
389
390 case POINTER_TYPE:
391 case REFERENCE_TYPE:
392 error ("pointer value used where a floating point value was expected");
269e63b7 393 return convert_to_real_1 (type, integer_zero_node, fold_p);
f5963e61
JL
394
395 default:
396 error ("aggregate value used where a float was expected");
269e63b7 397 return convert_to_real_1 (type, integer_zero_node, fold_p);
f5963e61 398 }
76e616db
BK
399}
400
269e63b7
KT
401/* A wrapper around convert_to_real_1 that always folds the
402 expression. */
403
404tree
405convert_to_real (tree type, tree expr)
406{
407 return convert_to_real_1 (type, expr, true);
408}
409
410/* A wrapper around convert_to_real_1 that only folds the
415594bb 411 expression if DOFOLD, or if it is CONSTANT_CLASS_P. */
269e63b7
KT
412
413tree
415594bb 414convert_to_real_maybe_fold (tree type, tree expr, bool dofold)
269e63b7 415{
415594bb 416 return convert_to_real_1 (type, expr, dofold || CONSTANT_CLASS_P (expr));
269e63b7
KT
417}
418
2d143ba8
RB
419/* Try to narrow EX_FORM ARG0 ARG1 in narrowed arg types producing a
420 result in TYPE. */
421
422static tree
423do_narrow (location_t loc,
424 enum tree_code ex_form, tree type, tree arg0, tree arg1,
425 tree expr, unsigned inprec, unsigned outprec, bool dofold)
426{
427 /* Do the arithmetic in type TYPEX,
428 then convert result to TYPE. */
429 tree typex = type;
430
431 /* Can't do arithmetic in enumeral types
432 so use an integer type that will hold the values. */
433 if (TREE_CODE (typex) == ENUMERAL_TYPE)
434 typex = lang_hooks.types.type_for_size (TYPE_PRECISION (typex),
435 TYPE_UNSIGNED (typex));
436
8d2b48ae
MP
437 /* The type demotion below might cause doing unsigned arithmetic
438 instead of signed, and thus hide overflow bugs. */
439 if ((ex_form == PLUS_EXPR || ex_form == MINUS_EXPR)
440 && !TYPE_UNSIGNED (typex)
441 && sanitize_flags_p (SANITIZE_SI_OVERFLOW))
442 return NULL_TREE;
443
2d143ba8
RB
444 /* But now perhaps TYPEX is as wide as INPREC.
445 In that case, do nothing special here.
446 (Otherwise would recurse infinitely in convert. */
447 if (TYPE_PRECISION (typex) != inprec)
448 {
449 /* Don't do unsigned arithmetic where signed was wanted,
450 or vice versa.
451 Exception: if both of the original operands were
452 unsigned then we can safely do the work as unsigned.
453 Exception: shift operations take their type solely
454 from the first argument.
455 Exception: the LSHIFT_EXPR case above requires that
456 we perform this operation unsigned lest we produce
457 signed-overflow undefinedness.
458 And we may need to do it as unsigned
459 if we truncate to the original size. */
460 if (TYPE_UNSIGNED (TREE_TYPE (expr))
461 || (TYPE_UNSIGNED (TREE_TYPE (arg0))
462 && (TYPE_UNSIGNED (TREE_TYPE (arg1))
463 || ex_form == LSHIFT_EXPR
464 || ex_form == RSHIFT_EXPR
465 || ex_form == LROTATE_EXPR
466 || ex_form == RROTATE_EXPR))
467 || ex_form == LSHIFT_EXPR
468 /* If we have !flag_wrapv, and either ARG0 or
469 ARG1 is of a signed type, we have to do
470 PLUS_EXPR, MINUS_EXPR or MULT_EXPR in an unsigned
471 type in case the operation in outprec precision
472 could overflow. Otherwise, we would introduce
473 signed-overflow undefinedness. */
87c4dace
RB
474 || ((!(INTEGRAL_TYPE_P (TREE_TYPE (arg0))
475 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0)))
476 || !(INTEGRAL_TYPE_P (TREE_TYPE (arg1))
477 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg1))))
2d143ba8
RB
478 && ((TYPE_PRECISION (TREE_TYPE (arg0)) * 2u
479 > outprec)
480 || (TYPE_PRECISION (TREE_TYPE (arg1)) * 2u
481 > outprec))
482 && (ex_form == PLUS_EXPR
483 || ex_form == MINUS_EXPR
484 || ex_form == MULT_EXPR)))
485 {
486 if (!TYPE_UNSIGNED (typex))
487 typex = unsigned_type_for (typex);
488 }
489 else
490 {
491 if (TYPE_UNSIGNED (typex))
492 typex = signed_type_for (typex);
493 }
494 /* We should do away with all this once we have a proper
495 type promotion/demotion pass, see PR45397. */
496 expr = maybe_fold_build2_loc (dofold, loc, ex_form, typex,
497 convert (typex, arg0),
498 convert (typex, arg1));
499 return convert (type, expr);
500 }
501
502 return NULL_TREE;
503}
504
76e616db
BK
505/* Convert EXPR to some integer (or enum) type TYPE.
506
0f996086
CF
507 EXPR must be pointer, integer, discrete (enum, char, or bool), float,
508 fixed-point or vector; in other cases error is called.
76e616db 509
269e63b7
KT
510 If DOFOLD is TRUE, we try to simplify newly-created patterns by folding.
511
76e616db
BK
512 The result of this is always supposed to be a newly created tree node
513 not in use in any existing structure. */
514
269e63b7
KT
515static tree
516convert_to_integer_1 (tree type, tree expr, bool dofold)
76e616db 517{
f5963e61
JL
518 enum tree_code ex_form = TREE_CODE (expr);
519 tree intype = TREE_TYPE (expr);
a5e0cd1d
MG
520 unsigned int inprec = element_precision (intype);
521 unsigned int outprec = element_precision (type);
85a16bf8 522 location_t loc = EXPR_LOCATION (expr);
76e616db 523
9c4cb3a3
MM
524 /* An INTEGER_TYPE cannot be incomplete, but an ENUMERAL_TYPE can
525 be. Consider `enum E = { a, b = (enum E) 3 };'. */
d0f062fb 526 if (!COMPLETE_TYPE_P (type))
9c4cb3a3
MM
527 {
528 error ("conversion to incomplete type");
529 return error_mark_node;
530 }
531
c05eeebc
JJ
532 if (ex_form == COMPOUND_EXPR)
533 {
269e63b7 534 tree t = convert_to_integer_1 (type, TREE_OPERAND (expr, 1), dofold);
c05eeebc
JJ
535 if (t == TREE_OPERAND (expr, 1))
536 return expr;
537 return build2_loc (EXPR_LOCATION (expr), COMPOUND_EXPR, TREE_TYPE (t),
538 TREE_OPERAND (expr, 0), t);
539 }
540
332d782c
KG
541 /* Convert e.g. (long)round(d) -> lround(d). */
542 /* If we're converting to char, we may encounter differing behavior
543 between converting from double->char vs double->long->char.
544 We're in "undefined" territory but we prefer to be conservative,
545 so only proceed in "unsafe" math mode. */
546 if (optimize
547 && (flag_unsafe_math_optimizations
d2be4368
KG
548 || (long_integer_type_node
549 && outprec >= TYPE_PRECISION (long_integer_type_node))))
332d782c
KG
550 {
551 tree s_expr = strip_float_extensions (expr);
552 tree s_intype = TREE_TYPE (s_expr);
553 const enum built_in_function fcode = builtin_mathfn_code (s_expr);
554 tree fn = 0;
b8698a0f 555
332d782c
KG
556 switch (fcode)
557 {
ea6a6627 558 CASE_FLT_FN (BUILT_IN_CEIL):
c6cfa2bf 559 CASE_FLT_FN_FLOATN_NX (BUILT_IN_CEIL):
1c432a0c 560 /* Only convert in ISO C99 mode. */
d33d9e47 561 if (!targetm.libc_has_function (function_c99_misc))
1c432a0c 562 break;
6c32ee74
UB
563 if (outprec < TYPE_PRECISION (integer_type_node)
564 || (outprec == TYPE_PRECISION (integer_type_node)
738764ef 565 && !TYPE_UNSIGNED (type)))
6c32ee74
UB
566 fn = mathfn_built_in (s_intype, BUILT_IN_ICEIL);
567 else if (outprec == TYPE_PRECISION (long_integer_type_node)
568 && !TYPE_UNSIGNED (type))
f94b1661 569 fn = mathfn_built_in (s_intype, BUILT_IN_LCEIL);
738764ef
RS
570 else if (outprec == TYPE_PRECISION (long_long_integer_type_node)
571 && !TYPE_UNSIGNED (type))
572 fn = mathfn_built_in (s_intype, BUILT_IN_LLCEIL);
f94b1661
UB
573 break;
574
ea6a6627 575 CASE_FLT_FN (BUILT_IN_FLOOR):
c6cfa2bf 576 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FLOOR):
1c432a0c 577 /* Only convert in ISO C99 mode. */
d33d9e47 578 if (!targetm.libc_has_function (function_c99_misc))
1c432a0c 579 break;
6c32ee74
UB
580 if (outprec < TYPE_PRECISION (integer_type_node)
581 || (outprec == TYPE_PRECISION (integer_type_node)
738764ef 582 && !TYPE_UNSIGNED (type)))
6c32ee74
UB
583 fn = mathfn_built_in (s_intype, BUILT_IN_IFLOOR);
584 else if (outprec == TYPE_PRECISION (long_integer_type_node)
585 && !TYPE_UNSIGNED (type))
d8b42d06 586 fn = mathfn_built_in (s_intype, BUILT_IN_LFLOOR);
738764ef
RS
587 else if (outprec == TYPE_PRECISION (long_long_integer_type_node)
588 && !TYPE_UNSIGNED (type))
589 fn = mathfn_built_in (s_intype, BUILT_IN_LLFLOOR);
d8b42d06
UB
590 break;
591
ea6a6627 592 CASE_FLT_FN (BUILT_IN_ROUND):
c6cfa2bf 593 CASE_FLT_FN_FLOATN_NX (BUILT_IN_ROUND):
25be91ac
KT
594 /* Only convert in ISO C99 mode and with -fno-math-errno. */
595 if (!targetm.libc_has_function (function_c99_misc) || flag_errno_math)
44782c0c 596 break;
6c32ee74
UB
597 if (outprec < TYPE_PRECISION (integer_type_node)
598 || (outprec == TYPE_PRECISION (integer_type_node)
738764ef 599 && !TYPE_UNSIGNED (type)))
6c32ee74
UB
600 fn = mathfn_built_in (s_intype, BUILT_IN_IROUND);
601 else if (outprec == TYPE_PRECISION (long_integer_type_node)
602 && !TYPE_UNSIGNED (type))
332d782c 603 fn = mathfn_built_in (s_intype, BUILT_IN_LROUND);
738764ef
RS
604 else if (outprec == TYPE_PRECISION (long_long_integer_type_node)
605 && !TYPE_UNSIGNED (type))
606 fn = mathfn_built_in (s_intype, BUILT_IN_LLROUND);
332d782c
KG
607 break;
608
65bda21f 609 CASE_FLT_FN (BUILT_IN_NEARBYINT):
c6cfa2bf 610 CASE_FLT_FN_FLOATN_NX (BUILT_IN_NEARBYINT):
65bda21f 611 /* Only convert nearbyint* if we can ignore math exceptions. */
332d782c
KG
612 if (flag_trapping_math)
613 break;
81fea426 614 gcc_fallthrough ();
65bda21f 615 CASE_FLT_FN (BUILT_IN_RINT):
c6cfa2bf 616 CASE_FLT_FN_FLOATN_NX (BUILT_IN_RINT):
371e764d
KT
617 /* Only convert in ISO C99 mode and with -fno-math-errno. */
618 if (!targetm.libc_has_function (function_c99_misc) || flag_errno_math)
44782c0c 619 break;
6c32ee74
UB
620 if (outprec < TYPE_PRECISION (integer_type_node)
621 || (outprec == TYPE_PRECISION (integer_type_node)
738764ef 622 && !TYPE_UNSIGNED (type)))
6c32ee74 623 fn = mathfn_built_in (s_intype, BUILT_IN_IRINT);
44782c0c 624 else if (outprec == TYPE_PRECISION (long_integer_type_node)
6c32ee74 625 && !TYPE_UNSIGNED (type))
738764ef
RS
626 fn = mathfn_built_in (s_intype, BUILT_IN_LRINT);
627 else if (outprec == TYPE_PRECISION (long_long_integer_type_node)
628 && !TYPE_UNSIGNED (type))
629 fn = mathfn_built_in (s_intype, BUILT_IN_LLRINT);
332d782c 630 break;
2ec76fdb 631
ea6a6627 632 CASE_FLT_FN (BUILT_IN_TRUNC):
c6cfa2bf 633 CASE_FLT_FN_FLOATN_NX (BUILT_IN_TRUNC):
269e63b7 634 return convert_to_integer_1 (type, CALL_EXPR_ARG (s_expr, 0), dofold);
2ec76fdb 635
332d782c
KG
636 default:
637 break;
638 }
b8698a0f 639
332d782c
KG
640 if (fn)
641 {
5039610b 642 tree newexpr = build_call_expr (fn, 1, CALL_EXPR_ARG (s_expr, 0));
269e63b7 643 return convert_to_integer_1 (type, newexpr, dofold);
332d782c
KG
644 }
645 }
646
2c2f70e1
UB
647 /* Convert (int)logb(d) -> ilogb(d). */
648 if (optimize
649 && flag_unsafe_math_optimizations
650 && !flag_trapping_math && !flag_errno_math && flag_finite_math_only
651 && integer_type_node
652 && (outprec > TYPE_PRECISION (integer_type_node)
653 || (outprec == TYPE_PRECISION (integer_type_node)
654 && !TYPE_UNSIGNED (type))))
655 {
656 tree s_expr = strip_float_extensions (expr);
657 tree s_intype = TREE_TYPE (s_expr);
658 const enum built_in_function fcode = builtin_mathfn_code (s_expr);
659 tree fn = 0;
b8698a0f 660
2c2f70e1
UB
661 switch (fcode)
662 {
663 CASE_FLT_FN (BUILT_IN_LOGB):
664 fn = mathfn_built_in (s_intype, BUILT_IN_ILOGB);
665 break;
666
667 default:
668 break;
669 }
670
671 if (fn)
672 {
673 tree newexpr = build_call_expr (fn, 1, CALL_EXPR_ARG (s_expr, 0));
269e63b7 674 return convert_to_integer_1 (type, newexpr, dofold);
2c2f70e1
UB
675 }
676 }
677
f5963e61 678 switch (TREE_CODE (intype))
76e616db 679 {
f5963e61
JL
680 case POINTER_TYPE:
681 case REFERENCE_TYPE:
6ffc0c0c 682 if (integer_zerop (expr) && !TREE_OVERFLOW (expr))
97471d8f
RS
683 return build_int_cst (type, 0);
684
c767899e
OH
685 /* Convert to an unsigned integer of the correct width first, and from
686 there widen/truncate to the required type. Some targets support the
687 coexistence of multiple valid pointer sizes, so fetch the one we need
688 from the type. */
269e63b7
KT
689 if (!dofold)
690 return build1 (CONVERT_EXPR, type, expr);
97471d8f 691 expr = fold_build1 (CONVERT_EXPR,
c767899e
OH
692 lang_hooks.types.type_for_size
693 (TYPE_PRECISION (intype), 0),
97471d8f 694 expr);
e7a6c127 695 return fold_convert (type, expr);
76e616db 696
f5963e61
JL
697 case INTEGER_TYPE:
698 case ENUMERAL_TYPE:
699 case BOOLEAN_TYPE:
6175f578 700 case OFFSET_TYPE:
f5963e61 701 /* If this is a logical operation, which just returns 0 or 1, we can
a338ab5a 702 change the type of the expression. */
76e616db 703
6615c446 704 if (TREE_CODE_CLASS (ex_form) == tcc_comparison)
76e616db 705 {
5dfa45d0 706 expr = copy_node (expr);
76e616db
BK
707 TREE_TYPE (expr) = type;
708 return expr;
709 }
f5963e61 710
f5963e61
JL
711 /* If we are widening the type, put in an explicit conversion.
712 Similarly if we are not changing the width. After this, we know
713 we are truncating EXPR. */
714
76e616db 715 else if (outprec >= inprec)
4b0d3cbe
MM
716 {
717 enum tree_code code;
718
719 /* If the precision of the EXPR's type is K bits and the
720 destination mode has more bits, and the sign is changing,
721 it is not safe to use a NOP_EXPR. For example, suppose
722 that EXPR's type is a 3-bit unsigned integer type, the
723 TYPE is a 3-bit signed integer type, and the machine mode
724 for the types is 8-bit QImode. In that case, the
725 conversion necessitates an explicit sign-extension. In
726 the signed-to-unsigned case the high-order bits have to
727 be cleared. */
8df83eae 728 if (TYPE_UNSIGNED (type) != TYPE_UNSIGNED (TREE_TYPE (expr))
2be65d9e 729 && !type_has_mode_precision_p (TREE_TYPE (expr)))
4b0d3cbe
MM
730 code = CONVERT_EXPR;
731 else
732 code = NOP_EXPR;
733
269e63b7 734 return maybe_fold_build1_loc (dofold, loc, code, type, expr);
4b0d3cbe 735 }
76e616db 736
1c013b45
RK
737 /* If TYPE is an enumeral type or a type with a precision less
738 than the number of bits in its mode, do the conversion to the
739 type corresponding to its mode, then do a nop conversion
740 to TYPE. */
741 else if (TREE_CODE (type) == ENUMERAL_TYPE
bb94ec76 742 || maybe_ne (outprec, GET_MODE_PRECISION (TYPE_MODE (type))))
32913637
NS
743 {
744 expr = convert (lang_hooks.types.type_for_mode
745 (TYPE_MODE (type), TYPE_UNSIGNED (type)), expr);
746 return maybe_fold_build1_loc (dofold, loc, NOP_EXPR, type, expr);
747 }
1c013b45 748
ab29fdfc
RK
749 /* Here detect when we can distribute the truncation down past some
750 arithmetic. For example, if adding two longs and converting to an
751 int, we can equally well convert both to ints and then add.
752 For the operations handled here, such truncation distribution
753 is always safe.
754 It is desirable in these cases:
755 1) when truncating down to full-word from a larger size
756 2) when truncating takes no work.
757 3) when at least one operand of the arithmetic has been extended
758 (as by C's default conversions). In this case we need two conversions
759 if we do the arithmetic as already requested, so we might as well
760 truncate both and then combine. Perhaps that way we need only one.
761
762 Note that in general we cannot do the arithmetic in a type
763 shorter than the desired result of conversion, even if the operands
764 are both extended from a shorter type, because they might overflow
765 if combined in that type. The exceptions to this--the times when
766 two narrow values can be combined in their narrow type even to
767 make a wider result--are handled by "shorten" in build_binary_op. */
76e616db 768
415594bb 769 if (dofold)
c0328be3 770 switch (ex_form)
d977cb9c 771 {
c0328be3
JM
772 case RSHIFT_EXPR:
773 /* We can pass truncation down through right shifting
774 when the shift count is a nonpositive constant. */
775 if (TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST
776 && tree_int_cst_sgn (TREE_OPERAND (expr, 1)) <= 0)
d977cb9c
RG
777 goto trunc1;
778 break;
d977cb9c 779
c0328be3
JM
780 case LSHIFT_EXPR:
781 /* We can pass truncation down through left shifting
782 when the shift count is a nonnegative constant and
783 the target type is unsigned. */
784 if (TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST
785 && tree_int_cst_sgn (TREE_OPERAND (expr, 1)) >= 0
786 && TYPE_UNSIGNED (type)
787 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
76e616db 788 {
c0328be3
JM
789 /* If shift count is less than the width of the truncated type,
790 really shift. */
791 if (tree_int_cst_lt (TREE_OPERAND (expr, 1), TYPE_SIZE (type)))
792 /* In this case, shifting is like multiplication. */
793 goto trunc1;
794 else
76e616db 795 {
c0328be3
JM
796 /* If it is >= that width, result is zero.
797 Handling this with trunc1 would give the wrong result:
798 (int) ((long long) a << 32) is well defined (as 0)
799 but (int) a << 32 is undefined and would get a
800 warning. */
801
802 tree t = build_int_cst (type, 0);
803
804 /* If the original expression had side-effects, we must
805 preserve it. */
806 if (TREE_SIDE_EFFECTS (expr))
807 return build2 (COMPOUND_EXPR, type, expr, t);
ceef8ce4 808 else
c0328be3 809 return t;
76e616db
BK
810 }
811 }
c0328be3 812 break;
76e616db 813
c0328be3
JM
814 case TRUNC_DIV_EXPR:
815 {
2d143ba8
RB
816 tree arg0 = get_unwidened (TREE_OPERAND (expr, 0), NULL_TREE);
817 tree arg1 = get_unwidened (TREE_OPERAND (expr, 1), NULL_TREE);
c0328be3
JM
818
819 /* Don't distribute unless the output precision is at least as
820 big as the actual inputs and it has the same signedness. */
821 if (outprec >= TYPE_PRECISION (TREE_TYPE (arg0))
822 && outprec >= TYPE_PRECISION (TREE_TYPE (arg1))
823 /* If signedness of arg0 and arg1 don't match,
824 we can't necessarily find a type to compare them in. */
825 && (TYPE_UNSIGNED (TREE_TYPE (arg0))
826 == TYPE_UNSIGNED (TREE_TYPE (arg1)))
827 /* Do not change the sign of the division. */
828 && (TYPE_UNSIGNED (TREE_TYPE (expr))
829 == TYPE_UNSIGNED (TREE_TYPE (arg0)))
830 /* Either require unsigned division or a division by
831 a constant that is not -1. */
832 && (TYPE_UNSIGNED (TREE_TYPE (arg0))
833 || (TREE_CODE (arg1) == INTEGER_CST
834 && !integer_all_onesp (arg1))))
2d143ba8
RB
835 {
836 tree tem = do_narrow (loc, ex_form, type, arg0, arg1,
837 expr, inprec, outprec, dofold);
838 if (tem)
839 return tem;
840 }
c0328be3
JM
841 break;
842 }
76e616db 843
c0328be3
JM
844 case MAX_EXPR:
845 case MIN_EXPR:
846 case MULT_EXPR:
847 {
848 tree arg0 = get_unwidened (TREE_OPERAND (expr, 0), type);
849 tree arg1 = get_unwidened (TREE_OPERAND (expr, 1), type);
850
851 /* Don't distribute unless the output precision is at least as
852 big as the actual inputs. Otherwise, the comparison of the
853 truncated values will be wrong. */
854 if (outprec >= TYPE_PRECISION (TREE_TYPE (arg0))
855 && outprec >= TYPE_PRECISION (TREE_TYPE (arg1))
856 /* If signedness of arg0 and arg1 don't match,
857 we can't necessarily find a type to compare them in. */
858 && (TYPE_UNSIGNED (TREE_TYPE (arg0))
859 == TYPE_UNSIGNED (TREE_TYPE (arg1))))
860 goto trunc1;
861 break;
862 }
863
864 case PLUS_EXPR:
865 case MINUS_EXPR:
866 case BIT_AND_EXPR:
867 case BIT_IOR_EXPR:
868 case BIT_XOR_EXPR:
869 trunc1:
870 {
871 tree arg0 = get_unwidened (TREE_OPERAND (expr, 0), type);
872 tree arg1 = get_unwidened (TREE_OPERAND (expr, 1), type);
873
874 /* Do not try to narrow operands of pointer subtraction;
875 that will interfere with other folding. */
876 if (ex_form == MINUS_EXPR
877 && CONVERT_EXPR_P (arg0)
878 && CONVERT_EXPR_P (arg1)
879 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (arg0, 0)))
880 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (arg1, 0))))
881 break;
882
883 if (outprec >= BITS_PER_WORD
bb149ca2 884 || targetm.truly_noop_truncation (outprec, inprec)
c0328be3
JM
885 || inprec > TYPE_PRECISION (TREE_TYPE (arg0))
886 || inprec > TYPE_PRECISION (TREE_TYPE (arg1)))
887 {
2d143ba8
RB
888 tree tem = do_narrow (loc, ex_form, type, arg0, arg1,
889 expr, inprec, outprec, dofold);
890 if (tem)
891 return tem;
c0328be3
JM
892 }
893 }
3767c0fd 894 break;
c0328be3
JM
895
896 case NEGATE_EXPR:
c253525e
MP
897 /* Using unsigned arithmetic for signed types may hide overflow
898 bugs. */
899 if (!TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (expr, 0)))
900 && sanitize_flags_p (SANITIZE_SI_OVERFLOW))
901 break;
902 /* Fall through. */
c0328be3
JM
903 case BIT_NOT_EXPR:
904 /* This is not correct for ABS_EXPR,
905 since we must test the sign before truncation. */
906 {
907 /* Do the arithmetic in type TYPEX,
908 then convert result to TYPE. */
909 tree typex = type;
910
911 /* Can't do arithmetic in enumeral types
912 so use an integer type that will hold the values. */
913 if (TREE_CODE (typex) == ENUMERAL_TYPE)
914 typex
915 = lang_hooks.types.type_for_size (TYPE_PRECISION (typex),
916 TYPE_UNSIGNED (typex));
917
918 if (!TYPE_UNSIGNED (typex))
c253525e 919 typex = unsigned_type_for (typex);
c0328be3
JM
920 return convert (type,
921 fold_build1 (ex_form, typex,
922 convert (typex,
923 TREE_OPERAND (expr, 0))));
924 }
925
926 CASE_CONVERT:
cf098191
RS
927 {
928 tree argtype = TREE_TYPE (TREE_OPERAND (expr, 0));
929 /* Don't introduce a "can't convert between vector values
930 of different size" error. */
931 if (TREE_CODE (argtype) == VECTOR_TYPE
932 && maybe_ne (GET_MODE_SIZE (TYPE_MODE (argtype)),
933 GET_MODE_SIZE (TYPE_MODE (type))))
934 break;
935 }
c0328be3
JM
936 /* If truncating after truncating, might as well do all at once.
937 If truncating after extending, we may get rid of wasted work. */
938 return convert (type, get_unwidened (TREE_OPERAND (expr, 0), type));
939
940 case COND_EXPR:
941 /* It is sometimes worthwhile to push the narrowing down through
942 the conditional and never loses. A COND_EXPR may have a throw
943 as one operand, which then has void type. Just leave void
944 operands as they are. */
269e63b7
KT
945 return
946 fold_build3 (COND_EXPR, type, TREE_OPERAND (expr, 0),
947 VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (expr, 1)))
948 ? TREE_OPERAND (expr, 1)
949 : convert (type, TREE_OPERAND (expr, 1)),
950 VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (expr, 2)))
951 ? TREE_OPERAND (expr, 2)
952 : convert (type, TREE_OPERAND (expr, 2)));
76e616db 953
c0328be3
JM
954 default:
955 break;
956 }
76e616db 957
c53153e7
JH
958 /* When parsing long initializers, we might end up with a lot of casts.
959 Shortcut this. */
960 if (TREE_CODE (expr) == INTEGER_CST)
961 return fold_convert (type, expr);
0b87eff5 962 return build1 (CONVERT_EXPR, type, expr);
76e616db 963
f5963e61 964 case REAL_TYPE:
f34ebeb2
ML
965 if (sanitize_flags_p (SANITIZE_FLOAT_CAST)
966 && current_function_decl != NULL_TREE)
85a16bf8
MP
967 {
968 expr = save_expr (expr);
6b131d5b 969 tree check = ubsan_instrument_float_cast (loc, type, expr);
85a16bf8 970 expr = build1 (FIX_TRUNC_EXPR, type, expr);
6b131d5b 971 if (check == NULL_TREE)
85a16bf8 972 return expr;
269e63b7
KT
973 return maybe_fold_build2_loc (dofold, loc, COMPOUND_EXPR,
974 TREE_TYPE (expr), check, expr);
85a16bf8
MP
975 }
976 else
977 return build1 (FIX_TRUNC_EXPR, type, expr);
76e616db 978
0f996086
CF
979 case FIXED_POINT_TYPE:
980 return build1 (FIXED_CONVERT_EXPR, type, expr);
981
f5963e61 982 case COMPLEX_TYPE:
269e63b7
KT
983 expr = maybe_fold_build1_loc (dofold, loc, REALPART_EXPR,
984 TREE_TYPE (TREE_TYPE (expr)), expr);
985 return convert (type, expr);
0b127821 986
0b4565c9 987 case VECTOR_TYPE:
3a021db2 988 if (!tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (TREE_TYPE (expr))))
0b4565c9 989 {
b8f75b8c
MG
990 error ("can%'t convert a vector of type %qT"
991 " to type %qT which has different size",
992 TREE_TYPE (expr), type);
0b4565c9
BS
993 return error_mark_node;
994 }
4d3c798d 995 return build1 (VIEW_CONVERT_EXPR, type, expr);
0b4565c9 996
f5963e61
JL
997 default:
998 error ("aggregate value used where an integer was expected");
999 return convert (type, integer_zero_node);
1000 }
76e616db 1001}
0b127821 1002
269e63b7
KT
1003/* Convert EXPR to some integer (or enum) type TYPE.
1004
1005 EXPR must be pointer, integer, discrete (enum, char, or bool), float,
1006 fixed-point or vector; in other cases error is called.
1007
1008 The result of this is always supposed to be a newly created tree node
1009 not in use in any existing structure. */
0b127821
RS
1010
1011tree
269e63b7 1012convert_to_integer (tree type, tree expr)
0b127821 1013{
269e63b7
KT
1014 return convert_to_integer_1 (type, expr, true);
1015}
1016
415594bb
JM
1017/* A wrapper around convert_to_complex_1 that only folds the
1018 expression if DOFOLD, or if it is CONSTANT_CLASS_P. */
269e63b7
KT
1019
1020tree
415594bb 1021convert_to_integer_maybe_fold (tree type, tree expr, bool dofold)
269e63b7 1022{
415594bb 1023 return convert_to_integer_1 (type, expr, dofold || CONSTANT_CLASS_P (expr));
269e63b7
KT
1024}
1025
1026/* Convert EXPR to the complex type TYPE in the usual ways. If FOLD_P is
1027 true, try to fold the expression. */
1028
1029static tree
1030convert_to_complex_1 (tree type, tree expr, bool fold_p)
1031{
1032 location_t loc = EXPR_LOCATION (expr);
0b127821 1033 tree subtype = TREE_TYPE (type);
159b3be1 1034
f5963e61 1035 switch (TREE_CODE (TREE_TYPE (expr)))
0b127821 1036 {
f5963e61 1037 case REAL_TYPE:
0f996086 1038 case FIXED_POINT_TYPE:
f5963e61
JL
1039 case INTEGER_TYPE:
1040 case ENUMERAL_TYPE:
1041 case BOOLEAN_TYPE:
3244e67d
RS
1042 return build2 (COMPLEX_EXPR, type, convert (subtype, expr),
1043 convert (subtype, integer_zero_node));
0b127821 1044
f5963e61
JL
1045 case COMPLEX_TYPE:
1046 {
1047 tree elt_type = TREE_TYPE (TREE_TYPE (expr));
1048
1049 if (TYPE_MAIN_VARIANT (elt_type) == TYPE_MAIN_VARIANT (subtype))
1050 return expr;
c05eeebc
JJ
1051 else if (TREE_CODE (expr) == COMPOUND_EXPR)
1052 {
269e63b7
KT
1053 tree t = convert_to_complex_1 (type, TREE_OPERAND (expr, 1),
1054 fold_p);
c05eeebc
JJ
1055 if (t == TREE_OPERAND (expr, 1))
1056 return expr;
1057 return build2_loc (EXPR_LOCATION (expr), COMPOUND_EXPR,
1058 TREE_TYPE (t), TREE_OPERAND (expr, 0), t);
269e63b7 1059 }
f5963e61 1060 else if (TREE_CODE (expr) == COMPLEX_EXPR)
269e63b7
KT
1061 return maybe_fold_build2_loc (fold_p, loc, COMPLEX_EXPR, type,
1062 convert (subtype,
1063 TREE_OPERAND (expr, 0)),
1064 convert (subtype,
1065 TREE_OPERAND (expr, 1)));
f5963e61
JL
1066 else
1067 {
1068 expr = save_expr (expr);
269e63b7
KT
1069 tree realp = maybe_fold_build1_loc (fold_p, loc, REALPART_EXPR,
1070 TREE_TYPE (TREE_TYPE (expr)),
1071 expr);
1072 tree imagp = maybe_fold_build1_loc (fold_p, loc, IMAGPART_EXPR,
1073 TREE_TYPE (TREE_TYPE (expr)),
1074 expr);
1075 return maybe_fold_build2_loc (fold_p, loc, COMPLEX_EXPR, type,
1076 convert (subtype, realp),
1077 convert (subtype, imagp));
f5963e61
JL
1078 }
1079 }
0b127821 1080
f5963e61
JL
1081 case POINTER_TYPE:
1082 case REFERENCE_TYPE:
1083 error ("pointer value used where a complex was expected");
269e63b7 1084 return convert_to_complex_1 (type, integer_zero_node, fold_p);
f5963e61
JL
1085
1086 default:
1087 error ("aggregate value used where a complex was expected");
269e63b7 1088 return convert_to_complex_1 (type, integer_zero_node, fold_p);
f5963e61 1089 }
0b127821 1090}
0b4565c9 1091
269e63b7
KT
1092/* A wrapper around convert_to_complex_1 that always folds the
1093 expression. */
1094
1095tree
1096convert_to_complex (tree type, tree expr)
1097{
1098 return convert_to_complex_1 (type, expr, true);
1099}
1100
1101/* A wrapper around convert_to_complex_1 that only folds the
415594bb 1102 expression if DOFOLD, or if it is CONSTANT_CLASS_P. */
269e63b7
KT
1103
1104tree
415594bb 1105convert_to_complex_maybe_fold (tree type, tree expr, bool dofold)
269e63b7 1106{
415594bb 1107 return convert_to_complex_1 (type, expr, dofold || CONSTANT_CLASS_P (expr));
269e63b7
KT
1108}
1109
0b4565c9
BS
1110/* Convert EXPR to the vector type TYPE in the usual ways. */
1111
1112tree
159b3be1 1113convert_to_vector (tree type, tree expr)
0b4565c9 1114{
0b4565c9
BS
1115 switch (TREE_CODE (TREE_TYPE (expr)))
1116 {
1117 case INTEGER_TYPE:
1118 case VECTOR_TYPE:
3a021db2 1119 if (!tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (TREE_TYPE (expr))))
0b4565c9 1120 {
b8f75b8c
MG
1121 error ("can%'t convert a value of type %qT"
1122 " to vector type %qT which has different size",
1123 TREE_TYPE (expr), type);
0b4565c9
BS
1124 return error_mark_node;
1125 }
4d3c798d 1126 return build1 (VIEW_CONVERT_EXPR, type, expr);
0b4565c9
BS
1127
1128 default:
d8a07487 1129 error ("can%'t convert value to a vector");
273d67e7 1130 return error_mark_node;
0b4565c9
BS
1131 }
1132}
0f996086
CF
1133
1134/* Convert EXPR to some fixed-point type TYPE.
1135
1136 EXPR must be fixed-point, float, integer, or enumeral;
1137 in other cases error is called. */
1138
1139tree
1140convert_to_fixed (tree type, tree expr)
1141{
1142 if (integer_zerop (expr))
1143 {
1144 tree fixed_zero_node = build_fixed (type, FCONST0 (TYPE_MODE (type)));
1145 return fixed_zero_node;
1146 }
1147 else if (integer_onep (expr) && ALL_SCALAR_ACCUM_MODE_P (TYPE_MODE (type)))
1148 {
1149 tree fixed_one_node = build_fixed (type, FCONST1 (TYPE_MODE (type)));
1150 return fixed_one_node;
1151 }
1152
1153 switch (TREE_CODE (TREE_TYPE (expr)))
1154 {
1155 case FIXED_POINT_TYPE:
1156 case INTEGER_TYPE:
1157 case ENUMERAL_TYPE:
1158 case BOOLEAN_TYPE:
1159 case REAL_TYPE:
1160 return build1 (FIXED_CONVERT_EXPR, type, expr);
1161
1162 case COMPLEX_TYPE:
1163 return convert (type,
1164 fold_build1 (REALPART_EXPR,
1165 TREE_TYPE (TREE_TYPE (expr)), expr));
1166
1167 default:
1168 error ("aggregate value used where a fixed-point was expected");
1169 return error_mark_node;
1170 }
1171}