Remove -fopenmp in dg-options in libgomp.c
[official-gcc.git] / gcc / cp / cvt.c
blob9f93c681bd134696e292e987a13953bd9ea8cd6e
1 /* Language-level data type conversion for GNU C++.
2 Copyright (C) 1987-2015 Free Software Foundation, Inc.
3 Hacked by Michael Tiemann (tiemann@cygnus.com)
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
22 /* This file contains the functions for converting C++ expressions
23 to different data types. The only entry point is `convert'.
24 Every language front end must have a `convert' function
25 but what kind of conversions it does will depend on the language. */
27 #include "config.h"
28 #include "system.h"
29 #include "coretypes.h"
30 #include "tm.h"
31 #include "input.h"
32 #include "alias.h"
33 #include "symtab.h"
34 #include "tree.h"
35 #include "stor-layout.h"
36 #include "flags.h"
37 #include "cp-tree.h"
38 #include "intl.h"
39 #include "convert.h"
40 #include "decl.h"
41 #include "target.h"
43 static tree cp_convert_to_pointer (tree, tree, tsubst_flags_t);
44 static tree convert_to_pointer_force (tree, tree, tsubst_flags_t);
45 static tree build_type_conversion (tree, tree);
46 static tree build_up_reference (tree, tree, int, tree, tsubst_flags_t);
47 static void diagnose_ref_binding (location_t, tree, tree, tree);
49 /* Change of width--truncation and extension of integers or reals--
50 is represented with NOP_EXPR. Proper functioning of many things
51 assumes that no other conversions can be NOP_EXPRs.
53 Conversion between integer and pointer is represented with CONVERT_EXPR.
54 Converting integer to real uses FLOAT_EXPR
55 and real to integer uses FIX_TRUNC_EXPR.
57 Here is a list of all the functions that assume that widening and
58 narrowing is always done with a NOP_EXPR:
59 In convert.c, convert_to_integer.
60 In c-typeck.c, build_binary_op_nodefault (boolean ops),
61 and c_common_truthvalue_conversion.
62 In expr.c: expand_expr, for operands of a MULT_EXPR.
63 In fold-const.c: fold.
64 In tree.c: get_narrower and get_unwidened.
66 C++: in multiple-inheritance, converting between pointers may involve
67 adjusting them by a delta stored within the class definition. */
69 /* Subroutines of `convert'. */
71 /* if converting pointer to pointer
72 if dealing with classes, check for derived->base or vice versa
73 else if dealing with method pointers, delegate
74 else convert blindly
75 else if converting class, pass off to build_type_conversion
76 else try C-style pointer conversion. */
78 static tree
79 cp_convert_to_pointer (tree type, tree expr, tsubst_flags_t complain)
81 tree intype = TREE_TYPE (expr);
82 enum tree_code form;
83 tree rval;
84 location_t loc = EXPR_LOC_OR_LOC (expr, input_location);
86 if (intype == error_mark_node)
87 return error_mark_node;
89 if (MAYBE_CLASS_TYPE_P (intype))
91 intype = complete_type (intype);
92 if (!COMPLETE_TYPE_P (intype))
94 if (complain & tf_error)
95 error_at (loc, "can%'t convert from incomplete type %qT to %qT",
96 intype, type);
97 return error_mark_node;
100 rval = build_type_conversion (type, expr);
101 if (rval)
103 if ((complain & tf_error)
104 && rval == error_mark_node)
105 error_at (loc, "conversion of %qE from %qT to %qT is ambiguous",
106 expr, intype, type);
107 return rval;
111 /* Handle anachronistic conversions from (::*)() to cv void* or (*)(). */
112 if (TYPE_PTR_P (type)
113 && (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
114 || VOID_TYPE_P (TREE_TYPE (type))))
116 if (TYPE_PTRMEMFUNC_P (intype)
117 || TREE_CODE (intype) == METHOD_TYPE)
118 return convert_member_func_to_ptr (type, expr, complain);
119 if (TYPE_PTR_P (TREE_TYPE (expr)))
120 return build_nop (type, expr);
121 intype = TREE_TYPE (expr);
124 if (expr == error_mark_node)
125 return error_mark_node;
127 form = TREE_CODE (intype);
129 if (POINTER_TYPE_P (intype))
131 intype = TYPE_MAIN_VARIANT (intype);
133 if (TYPE_MAIN_VARIANT (type) != intype
134 && TYPE_PTR_P (type)
135 && TREE_CODE (TREE_TYPE (type)) == RECORD_TYPE
136 && MAYBE_CLASS_TYPE_P (TREE_TYPE (type))
137 && MAYBE_CLASS_TYPE_P (TREE_TYPE (intype))
138 && TREE_CODE (TREE_TYPE (intype)) == RECORD_TYPE)
140 enum tree_code code = PLUS_EXPR;
141 tree binfo;
142 tree intype_class;
143 tree type_class;
144 bool same_p;
146 intype_class = TREE_TYPE (intype);
147 type_class = TREE_TYPE (type);
149 same_p = same_type_p (TYPE_MAIN_VARIANT (intype_class),
150 TYPE_MAIN_VARIANT (type_class));
151 binfo = NULL_TREE;
152 /* Try derived to base conversion. */
153 if (!same_p)
154 binfo = lookup_base (intype_class, type_class, ba_check,
155 NULL, complain);
156 if (!same_p && !binfo)
158 /* Try base to derived conversion. */
159 binfo = lookup_base (type_class, intype_class, ba_check,
160 NULL, complain);
161 code = MINUS_EXPR;
163 if (binfo == error_mark_node)
164 return error_mark_node;
165 if (binfo || same_p)
167 if (binfo)
168 expr = build_base_path (code, expr, binfo, 0, complain);
169 /* Add any qualifier conversions. */
170 return build_nop (type, expr);
174 if (TYPE_PTRMEMFUNC_P (type))
176 if (complain & tf_error)
177 error_at (loc, "cannot convert %qE from type %qT to type %qT",
178 expr, intype, type);
179 return error_mark_node;
182 return build_nop (type, expr);
184 else if ((TYPE_PTRDATAMEM_P (type) && TYPE_PTRDATAMEM_P (intype))
185 || (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype)))
186 return convert_ptrmem (type, expr, /*allow_inverse_p=*/false,
187 /*c_cast_p=*/false, complain);
188 else if (TYPE_PTRMEMFUNC_P (intype))
190 if (!warn_pmf2ptr)
192 if (TREE_CODE (expr) == PTRMEM_CST)
193 return cp_convert_to_pointer (type, PTRMEM_CST_MEMBER (expr),
194 complain);
195 else if (TREE_CODE (expr) == OFFSET_REF)
197 tree object = TREE_OPERAND (expr, 0);
198 return get_member_function_from_ptrfunc (&object,
199 TREE_OPERAND (expr, 1),
200 complain);
203 if (complain & tf_error)
204 error_at (loc, "cannot convert %qE from type %qT to type %qT",
205 expr, intype, type);
206 return error_mark_node;
209 if (null_ptr_cst_p (expr))
211 if (TYPE_PTRMEMFUNC_P (type))
212 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), expr, 0,
213 /*c_cast_p=*/false, complain);
215 if (complain & tf_warning)
216 maybe_warn_zero_as_null_pointer_constant (expr, loc);
218 /* A NULL pointer-to-data-member is represented by -1, not by
219 zero. */
220 tree val = (TYPE_PTRDATAMEM_P (type)
221 ? build_int_cst_type (type, -1)
222 : build_int_cst (type, 0));
224 return (TREE_SIDE_EFFECTS (expr)
225 ? build2 (COMPOUND_EXPR, type, expr, val) : val);
227 else if (TYPE_PTRMEM_P (type) && INTEGRAL_CODE_P (form))
229 if (complain & tf_error)
230 error_at (loc, "invalid conversion from %qT to %qT", intype, type);
231 return error_mark_node;
234 if (INTEGRAL_CODE_P (form))
236 if (TYPE_PRECISION (intype) == POINTER_SIZE)
237 return build1 (CONVERT_EXPR, type, expr);
238 expr = cp_convert (c_common_type_for_size (POINTER_SIZE, 0), expr,
239 complain);
240 /* Modes may be different but sizes should be the same. There
241 is supposed to be some integral type that is the same width
242 as a pointer. */
243 gcc_assert (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (expr)))
244 == GET_MODE_SIZE (TYPE_MODE (type)));
246 return convert_to_pointer (type, expr);
249 if (type_unknown_p (expr))
250 return instantiate_type (type, expr, complain);
252 if (complain & tf_error)
253 error_at (loc, "cannot convert %qE from type %qT to type %qT",
254 expr, intype, type);
255 return error_mark_node;
258 /* Like convert, except permit conversions to take place which
259 are not normally allowed due to access restrictions
260 (such as conversion from sub-type to private super-type). */
262 static tree
263 convert_to_pointer_force (tree type, tree expr, tsubst_flags_t complain)
265 tree intype = TREE_TYPE (expr);
266 enum tree_code form = TREE_CODE (intype);
268 if (form == POINTER_TYPE)
270 intype = TYPE_MAIN_VARIANT (intype);
272 if (TYPE_MAIN_VARIANT (type) != intype
273 && TREE_CODE (TREE_TYPE (type)) == RECORD_TYPE
274 && MAYBE_CLASS_TYPE_P (TREE_TYPE (type))
275 && MAYBE_CLASS_TYPE_P (TREE_TYPE (intype))
276 && TREE_CODE (TREE_TYPE (intype)) == RECORD_TYPE)
278 enum tree_code code = PLUS_EXPR;
279 tree binfo;
281 binfo = lookup_base (TREE_TYPE (intype), TREE_TYPE (type),
282 ba_unique, NULL, complain);
283 if (!binfo)
285 binfo = lookup_base (TREE_TYPE (type), TREE_TYPE (intype),
286 ba_unique, NULL, complain);
287 code = MINUS_EXPR;
289 if (binfo == error_mark_node)
290 return error_mark_node;
291 if (binfo)
293 expr = build_base_path (code, expr, binfo, 0, complain);
294 if (expr == error_mark_node)
295 return error_mark_node;
296 /* Add any qualifier conversions. */
297 if (!same_type_p (TREE_TYPE (TREE_TYPE (expr)),
298 TREE_TYPE (type)))
299 expr = build_nop (type, expr);
300 return expr;
305 return cp_convert_to_pointer (type, expr, complain);
308 /* We are passing something to a function which requires a reference.
309 The type we are interested in is in TYPE. The initial
310 value we have to begin with is in ARG.
312 FLAGS controls how we manage access checking.
313 DIRECT_BIND in FLAGS controls how any temporaries are generated.
314 If DIRECT_BIND is set, DECL is the reference we're binding to. */
316 static tree
317 build_up_reference (tree type, tree arg, int flags, tree decl,
318 tsubst_flags_t complain)
320 tree rval;
321 tree argtype = TREE_TYPE (arg);
322 tree target_type = TREE_TYPE (type);
324 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
326 if ((flags & DIRECT_BIND) && ! real_lvalue_p (arg))
328 /* Create a new temporary variable. We can't just use a TARGET_EXPR
329 here because it needs to live as long as DECL. */
330 tree targ = arg;
332 arg = make_temporary_var_for_ref_to_temp (decl, target_type);
334 /* Process the initializer for the declaration. */
335 DECL_INITIAL (arg) = targ;
336 cp_finish_decl (arg, targ, /*init_const_expr_p=*/false, NULL_TREE,
337 LOOKUP_ONLYCONVERTING|DIRECT_BIND);
339 else if (!(flags & DIRECT_BIND) && ! lvalue_p (arg))
340 return get_target_expr_sfinae (arg, complain);
342 /* If we had a way to wrap this up, and say, if we ever needed its
343 address, transform all occurrences of the register, into a memory
344 reference we could win better. */
345 rval = cp_build_addr_expr (arg, complain);
346 if (rval == error_mark_node)
347 return error_mark_node;
349 if ((flags & LOOKUP_PROTECT)
350 && TYPE_MAIN_VARIANT (argtype) != TYPE_MAIN_VARIANT (target_type)
351 && MAYBE_CLASS_TYPE_P (argtype)
352 && MAYBE_CLASS_TYPE_P (target_type))
354 /* We go through lookup_base for the access control. */
355 tree binfo = lookup_base (argtype, target_type, ba_check,
356 NULL, complain);
357 if (binfo == error_mark_node)
358 return error_mark_node;
359 if (binfo == NULL_TREE)
360 return error_not_base_type (target_type, argtype);
361 rval = build_base_path (PLUS_EXPR, rval, binfo, 1, complain);
363 else
364 rval
365 = convert_to_pointer_force (build_pointer_type (target_type),
366 rval, complain);
367 return build_nop (type, rval);
370 /* Subroutine of convert_to_reference. REFTYPE is the target reference type.
371 INTYPE is the original rvalue type and DECL is an optional _DECL node
372 for diagnostics.
374 [dcl.init.ref] says that if an rvalue is used to
375 initialize a reference, then the reference must be to a
376 non-volatile const type. */
378 static void
379 diagnose_ref_binding (location_t loc, tree reftype, tree intype, tree decl)
381 tree ttl = TREE_TYPE (reftype);
383 if (!CP_TYPE_CONST_NON_VOLATILE_P (ttl))
385 const char *msg;
387 if (CP_TYPE_VOLATILE_P (ttl) && decl)
388 msg = G_("initialization of volatile reference type %q#T from "
389 "rvalue of type %qT");
390 else if (CP_TYPE_VOLATILE_P (ttl))
391 msg = G_("conversion to volatile reference type %q#T "
392 "from rvalue of type %qT");
393 else if (decl)
394 msg = G_("initialization of non-const reference type %q#T from "
395 "rvalue of type %qT");
396 else
397 msg = G_("conversion to non-const reference type %q#T from "
398 "rvalue of type %qT");
400 permerror (loc, msg, reftype, intype);
404 /* For C++: Only need to do one-level references, but cannot
405 get tripped up on signed/unsigned differences.
407 DECL is either NULL_TREE or the _DECL node for a reference that is being
408 initialized. It can be error_mark_node if we don't know the _DECL but
409 we know it's an initialization. */
411 tree
412 convert_to_reference (tree reftype, tree expr, int convtype,
413 int flags, tree decl, tsubst_flags_t complain)
415 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (reftype));
416 tree intype;
417 tree rval = NULL_TREE;
418 tree rval_as_conversion = NULL_TREE;
419 bool can_convert_intype_to_type;
420 location_t loc = EXPR_LOC_OR_LOC (expr, input_location);
422 if (TREE_CODE (type) == FUNCTION_TYPE
423 && TREE_TYPE (expr) == unknown_type_node)
424 expr = instantiate_type (type, expr, complain);
426 if (expr == error_mark_node)
427 return error_mark_node;
429 intype = TREE_TYPE (expr);
431 gcc_assert (TREE_CODE (intype) != REFERENCE_TYPE);
432 gcc_assert (TREE_CODE (reftype) == REFERENCE_TYPE);
434 intype = TYPE_MAIN_VARIANT (intype);
436 can_convert_intype_to_type = can_convert_standard (type, intype, complain);
438 if (!can_convert_intype_to_type
439 && (convtype & CONV_IMPLICIT) && MAYBE_CLASS_TYPE_P (intype)
440 && ! (flags & LOOKUP_NO_CONVERSION))
442 /* Look for a user-defined conversion to lvalue that we can use. */
444 rval_as_conversion
445 = build_type_conversion (reftype, expr);
447 if (rval_as_conversion && rval_as_conversion != error_mark_node
448 && real_lvalue_p (rval_as_conversion))
450 expr = rval_as_conversion;
451 rval_as_conversion = NULL_TREE;
452 intype = type;
453 can_convert_intype_to_type = 1;
457 if (((convtype & CONV_STATIC)
458 && can_convert_standard (intype, type, complain))
459 || ((convtype & CONV_IMPLICIT) && can_convert_intype_to_type))
462 tree ttl = TREE_TYPE (reftype);
463 tree ttr = lvalue_type (expr);
465 if ((complain & tf_error)
466 && ! real_lvalue_p (expr))
467 diagnose_ref_binding (loc, reftype, intype, decl);
469 if (! (convtype & CONV_CONST)
470 && !at_least_as_qualified_p (ttl, ttr))
472 if (complain & tf_error)
473 permerror (loc, "conversion from %qT to %qT discards qualifiers",
474 ttr, reftype);
475 else
476 return error_mark_node;
480 return build_up_reference (reftype, expr, flags, decl, complain);
482 else if ((convtype & CONV_REINTERPRET) && lvalue_p (expr))
484 /* When casting an lvalue to a reference type, just convert into
485 a pointer to the new type and deference it. This is allowed
486 by San Diego WP section 5.2.9 paragraph 12, though perhaps it
487 should be done directly (jason). (int &)ri ---> *(int*)&ri */
489 /* B* bp; A& ar = (A&)bp; is valid, but it's probably not what they
490 meant. */
491 if ((complain & tf_warning)
492 && TYPE_PTR_P (intype)
493 && (comptypes (TREE_TYPE (intype), type,
494 COMPARE_BASE | COMPARE_DERIVED)))
495 warning_at (loc, 0, "casting %qT to %qT does not dereference pointer",
496 intype, reftype);
498 rval = cp_build_addr_expr (expr, complain);
499 if (rval != error_mark_node)
500 rval = convert_force (build_pointer_type (TREE_TYPE (reftype)),
501 rval, 0, complain);
502 if (rval != error_mark_node)
503 rval = build1 (NOP_EXPR, reftype, rval);
505 else
507 rval = convert_for_initialization (NULL_TREE, type, expr, flags,
508 ICR_CONVERTING, 0, 0, complain);
509 if (rval == NULL_TREE || rval == error_mark_node)
510 return rval;
511 if (complain & tf_error)
512 diagnose_ref_binding (loc, reftype, intype, decl);
513 rval = build_up_reference (reftype, rval, flags, decl, complain);
516 if (rval)
518 /* If we found a way to convert earlier, then use it. */
519 return rval;
522 if (complain & tf_error)
523 error_at (loc, "cannot convert type %qT to type %qT", intype, reftype);
525 return error_mark_node;
528 /* We are using a reference VAL for its value. Bash that reference all the
529 way down to its lowest form. */
531 tree
532 convert_from_reference (tree val)
534 if (TREE_TYPE (val)
535 && TREE_CODE (TREE_TYPE (val)) == REFERENCE_TYPE)
537 tree t = TREE_TYPE (TREE_TYPE (val));
538 tree ref = build1 (INDIRECT_REF, t, val);
540 mark_exp_read (val);
541 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
542 so that we get the proper error message if the result is used
543 to assign to. Also, &* is supposed to be a no-op. */
544 TREE_READONLY (ref) = CP_TYPE_CONST_P (t);
545 TREE_THIS_VOLATILE (ref) = CP_TYPE_VOLATILE_P (t);
546 TREE_SIDE_EFFECTS (ref)
547 = (TREE_THIS_VOLATILE (ref) || TREE_SIDE_EFFECTS (val));
548 val = ref;
551 return val;
554 /* Really perform an lvalue-to-rvalue conversion, including copying an
555 argument of class type into a temporary. */
557 tree
558 force_rvalue (tree expr, tsubst_flags_t complain)
560 tree type = TREE_TYPE (expr);
561 if (MAYBE_CLASS_TYPE_P (type) && TREE_CODE (expr) != TARGET_EXPR)
563 vec<tree, va_gc> *args = make_tree_vector_single (expr);
564 expr = build_special_member_call (NULL_TREE, complete_ctor_identifier,
565 &args, type, LOOKUP_NORMAL, complain);
566 release_tree_vector (args);
567 expr = build_cplus_new (type, expr, complain);
569 else
570 expr = decay_conversion (expr, complain);
572 return expr;
576 /* If EXPR and ORIG are INTEGER_CSTs, return a version of EXPR that has
577 TREE_OVERFLOW set only if it is set in ORIG. Otherwise, return EXPR
578 unchanged. */
580 static tree
581 ignore_overflows (tree expr, tree orig)
583 if (TREE_CODE (expr) == INTEGER_CST
584 && TREE_CODE (orig) == INTEGER_CST
585 && TREE_OVERFLOW (expr) != TREE_OVERFLOW (orig))
587 gcc_assert (!TREE_OVERFLOW (orig));
588 /* Ensure constant sharing. */
589 expr = wide_int_to_tree (TREE_TYPE (expr), expr);
591 return expr;
594 /* Fold away simple conversions, but make sure TREE_OVERFLOW is set
595 properly. */
597 tree
598 cp_fold_convert (tree type, tree expr)
600 tree conv;
601 if (TREE_TYPE (expr) == type)
602 conv = expr;
603 else if (TREE_CODE (expr) == PTRMEM_CST)
605 /* Avoid wrapping a PTRMEM_CST in NOP_EXPR. */
606 conv = copy_node (expr);
607 TREE_TYPE (conv) = type;
609 else
611 conv = fold_convert (type, expr);
612 conv = ignore_overflows (conv, expr);
614 return conv;
617 /* C++ conversions, preference to static cast conversions. */
619 tree
620 cp_convert (tree type, tree expr, tsubst_flags_t complain)
622 return ocp_convert (type, expr, CONV_OLD_CONVERT, LOOKUP_NORMAL, complain);
625 /* C++ equivalent of convert_and_check but using cp_convert as the
626 conversion function.
628 Convert EXPR to TYPE, warning about conversion problems with constants.
629 Invoke this function on every expression that is converted implicitly,
630 i.e. because of language rules and not because of an explicit cast. */
632 tree
633 cp_convert_and_check (tree type, tree expr, tsubst_flags_t complain)
635 tree result;
637 if (TREE_TYPE (expr) == type)
638 return expr;
640 result = cp_convert (type, expr, complain);
642 if ((complain & tf_warning)
643 && c_inhibit_evaluation_warnings == 0)
645 tree folded = maybe_constant_value (expr);
646 tree stripped = folded;
647 tree folded_result
648 = folded != expr ? cp_convert (type, folded, complain) : result;
650 /* maybe_constant_value wraps an INTEGER_CST with TREE_OVERFLOW in a
651 NOP_EXPR so that it isn't TREE_CONSTANT anymore. */
652 STRIP_NOPS (stripped);
654 if (!TREE_OVERFLOW_P (stripped)
655 && folded_result != error_mark_node)
656 warnings_for_convert_and_check (input_location, type, folded,
657 folded_result);
660 return result;
663 /* Conversion...
665 FLAGS indicates how we should behave. */
667 tree
668 ocp_convert (tree type, tree expr, int convtype, int flags,
669 tsubst_flags_t complain)
671 tree e = expr;
672 enum tree_code code = TREE_CODE (type);
673 const char *invalid_conv_diag;
674 tree e1;
675 location_t loc = EXPR_LOC_OR_LOC (expr, input_location);
677 if (error_operand_p (e) || type == error_mark_node)
678 return error_mark_node;
680 complete_type (type);
681 complete_type (TREE_TYPE (expr));
683 if ((invalid_conv_diag
684 = targetm.invalid_conversion (TREE_TYPE (expr), type)))
686 if (complain & tf_error)
687 error (invalid_conv_diag);
688 return error_mark_node;
691 /* FIXME remove when moving to c_fully_fold model. */
692 e = scalar_constant_value (e);
693 if (error_operand_p (e))
694 return error_mark_node;
696 if (MAYBE_CLASS_TYPE_P (type) && (convtype & CONV_FORCE_TEMP))
697 /* We need a new temporary; don't take this shortcut. */;
698 else if (same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (e)))
700 if (same_type_p (type, TREE_TYPE (e)))
701 /* The call to fold will not always remove the NOP_EXPR as
702 might be expected, since if one of the types is a typedef;
703 the comparison in fold is just equality of pointers, not a
704 call to comptypes. We don't call fold in this case because
705 that can result in infinite recursion; fold will call
706 convert, which will call ocp_convert, etc. */
707 return e;
708 /* For complex data types, we need to perform componentwise
709 conversion. */
710 else if (TREE_CODE (type) == COMPLEX_TYPE)
711 return fold_if_not_in_template (convert_to_complex (type, e));
712 else if (TREE_CODE (type) == VECTOR_TYPE)
713 return fold_if_not_in_template (convert_to_vector (type, e));
714 else if (TREE_CODE (e) == TARGET_EXPR)
716 /* Don't build a NOP_EXPR of class type. Instead, change the
717 type of the temporary. */
718 TREE_TYPE (e) = TREE_TYPE (TARGET_EXPR_SLOT (e)) = type;
719 return e;
721 else
723 /* We shouldn't be treating objects of ADDRESSABLE type as
724 rvalues. */
725 gcc_assert (!TREE_ADDRESSABLE (type));
726 return fold_if_not_in_template (build_nop (type, e));
730 e1 = targetm.convert_to_type (type, e);
731 if (e1)
732 return e1;
734 if (code == VOID_TYPE && (convtype & CONV_STATIC))
736 e = convert_to_void (e, ICV_CAST, complain);
737 return e;
740 if (INTEGRAL_CODE_P (code))
742 tree intype = TREE_TYPE (e);
743 tree converted;
745 if (TREE_CODE (type) == ENUMERAL_TYPE)
747 /* enum = enum, enum = int, enum = float, (enum)pointer are all
748 errors. */
749 if (((INTEGRAL_OR_ENUMERATION_TYPE_P (intype)
750 || TREE_CODE (intype) == REAL_TYPE)
751 && ! (convtype & CONV_STATIC))
752 || TYPE_PTR_P (intype))
754 if (complain & tf_error)
755 permerror (loc, "conversion from %q#T to %q#T", intype, type);
756 else
757 return error_mark_node;
760 /* [expr.static.cast]
762 8. A value of integral or enumeration type can be explicitly
763 converted to an enumeration type. The value is unchanged if
764 the original value is within the range of the enumeration
765 values. Otherwise, the resulting enumeration value is
766 unspecified. */
767 if ((complain & tf_warning)
768 && TREE_CODE (e) == INTEGER_CST
769 && ENUM_UNDERLYING_TYPE (type)
770 && !int_fits_type_p (e, ENUM_UNDERLYING_TYPE (type)))
771 warning_at (loc, OPT_Wconversion,
772 "the result of the conversion is unspecified because "
773 "%qE is outside the range of type %qT",
774 expr, type);
776 if (MAYBE_CLASS_TYPE_P (intype))
778 tree rval;
779 rval = build_type_conversion (type, e);
780 if (rval)
781 return rval;
782 if (complain & tf_error)
783 error_at (loc, "%q#T used where a %qT was expected", intype, type);
784 return error_mark_node;
786 if (code == BOOLEAN_TYPE)
788 if (VOID_TYPE_P (intype))
790 if (complain & tf_error)
791 error_at (loc,
792 "could not convert %qE from %<void%> to %<bool%>",
793 expr);
794 return error_mark_node;
797 /* We can't implicitly convert a scoped enum to bool, so convert
798 to the underlying type first. */
799 if (SCOPED_ENUM_P (intype) && (convtype & CONV_STATIC))
800 e = build_nop (ENUM_UNDERLYING_TYPE (intype), e);
801 return cp_truthvalue_conversion (e);
804 converted = fold_if_not_in_template (convert_to_integer (type, e));
806 /* Ignore any integer overflow caused by the conversion. */
807 return ignore_overflows (converted, e);
809 if (NULLPTR_TYPE_P (type) && e && null_ptr_cst_p (e))
811 if (complain & tf_warning)
812 maybe_warn_zero_as_null_pointer_constant (e, loc);
813 return nullptr_node;
815 if (POINTER_TYPE_P (type) || TYPE_PTRMEM_P (type))
816 return fold_if_not_in_template (cp_convert_to_pointer (type, e, complain));
817 if (code == VECTOR_TYPE)
819 tree in_vtype = TREE_TYPE (e);
820 if (MAYBE_CLASS_TYPE_P (in_vtype))
822 tree ret_val;
823 ret_val = build_type_conversion (type, e);
824 if (ret_val)
825 return ret_val;
826 if (complain & tf_error)
827 error_at (loc, "%q#T used where a %qT was expected",
828 in_vtype, type);
829 return error_mark_node;
831 return fold_if_not_in_template (convert_to_vector (type, e));
833 if (code == REAL_TYPE || code == COMPLEX_TYPE)
835 if (MAYBE_CLASS_TYPE_P (TREE_TYPE (e)))
837 tree rval;
838 rval = build_type_conversion (type, e);
839 if (rval)
840 return rval;
841 else if (complain & tf_error)
842 error_at (loc,
843 "%q#T used where a floating point value was expected",
844 TREE_TYPE (e));
846 if (code == REAL_TYPE)
847 return fold_if_not_in_template (convert_to_real (type, e));
848 else if (code == COMPLEX_TYPE)
849 return fold_if_not_in_template (convert_to_complex (type, e));
852 /* New C++ semantics: since assignment is now based on
853 memberwise copying, if the rhs type is derived from the
854 lhs type, then we may still do a conversion. */
855 if (RECORD_OR_UNION_CODE_P (code))
857 tree dtype = TREE_TYPE (e);
858 tree ctor = NULL_TREE;
860 dtype = TYPE_MAIN_VARIANT (dtype);
862 /* Conversion between aggregate types. New C++ semantics allow
863 objects of derived type to be cast to objects of base type.
864 Old semantics only allowed this between pointers.
866 There may be some ambiguity between using a constructor
867 vs. using a type conversion operator when both apply. */
869 ctor = e;
871 if (abstract_virtuals_error_sfinae (NULL_TREE, type, complain))
872 return error_mark_node;
874 if (BRACE_ENCLOSED_INITIALIZER_P (ctor))
875 ctor = perform_implicit_conversion (type, ctor, complain);
876 else if ((flags & LOOKUP_ONLYCONVERTING)
877 && ! (CLASS_TYPE_P (dtype) && DERIVED_FROM_P (type, dtype)))
878 /* For copy-initialization, first we create a temp of the proper type
879 with a user-defined conversion sequence, then we direct-initialize
880 the target with the temp (see [dcl.init]). */
881 ctor = build_user_type_conversion (type, ctor, flags, complain);
882 else
884 vec<tree, va_gc> *ctor_vec = make_tree_vector_single (ctor);
885 ctor = build_special_member_call (NULL_TREE,
886 complete_ctor_identifier,
887 &ctor_vec,
888 type, flags, complain);
889 release_tree_vector (ctor_vec);
891 if (ctor)
892 return build_cplus_new (type, ctor, complain);
895 if (complain & tf_error)
897 /* If the conversion failed and expr was an invalid use of pointer to
898 member function, try to report a meaningful error. */
899 if (invalid_nonstatic_memfn_p (loc, expr, complain))
900 /* We displayed the error message. */;
901 else
902 error_at (loc, "conversion from %qT to non-scalar type %qT requested",
903 TREE_TYPE (expr), type);
905 return error_mark_node;
908 /* When an expression is used in a void context, its value is discarded and
909 no lvalue-rvalue and similar conversions happen [expr.static.cast/4,
910 stmt.expr/1, expr.comma/1]. This permits dereferencing an incomplete type
911 in a void context. The C++ standard does not define what an `access' to an
912 object is, but there is reason to believe that it is the lvalue to rvalue
913 conversion -- if it were not, `*&*p = 1' would violate [expr]/4 in that it
914 accesses `*p' not to calculate the value to be stored. But, dcl.type.cv/8
915 indicates that volatile semantics should be the same between C and C++
916 where ever possible. C leaves it implementation defined as to what
917 constitutes an access to a volatile. So, we interpret `*vp' as a read of
918 the volatile object `vp' points to, unless that is an incomplete type. For
919 volatile references we do not do this interpretation, because that would
920 make it impossible to ignore the reference return value from functions. We
921 issue warnings in the confusing cases.
923 The IMPLICIT is ICV_CAST when the user is explicitly converting an expression
924 to void via a cast. If an expression is being implicitly converted, IMPLICIT
925 indicates the context of the implicit conversion. */
927 tree
928 convert_to_void (tree expr, impl_conv_void implicit, tsubst_flags_t complain)
930 location_t loc = EXPR_LOC_OR_LOC (expr, input_location);
932 if (expr == error_mark_node
933 || TREE_TYPE (expr) == error_mark_node)
934 return error_mark_node;
936 if (implicit == ICV_CAST)
937 mark_exp_read (expr);
938 else
940 tree exprv = expr;
942 while (TREE_CODE (exprv) == COMPOUND_EXPR)
943 exprv = TREE_OPERAND (exprv, 1);
944 if (DECL_P (exprv)
945 || handled_component_p (exprv)
946 || INDIRECT_REF_P (exprv))
947 /* Expr is not being 'used' here, otherwise we whould have
948 called mark_{rl}value_use use here, which would have in turn
949 called mark_exp_read. Rather, we call mark_exp_read directly
950 to avoid some warnings when
951 -Wunused-but-set-{variable,parameter} is in effect. */
952 mark_exp_read (exprv);
955 if (!TREE_TYPE (expr))
956 return expr;
957 if (invalid_nonstatic_memfn_p (loc, expr, complain))
958 return error_mark_node;
959 if (TREE_CODE (expr) == PSEUDO_DTOR_EXPR)
961 if (complain & tf_error)
962 error_at (loc, "pseudo-destructor is not called");
963 return error_mark_node;
965 if (VOID_TYPE_P (TREE_TYPE (expr)))
966 return expr;
967 switch (TREE_CODE (expr))
969 case COND_EXPR:
971 /* The two parts of a cond expr might be separate lvalues. */
972 tree op1 = TREE_OPERAND (expr,1);
973 tree op2 = TREE_OPERAND (expr,2);
974 bool side_effects = ((op1 && TREE_SIDE_EFFECTS (op1))
975 || TREE_SIDE_EFFECTS (op2));
976 tree new_op1, new_op2;
977 new_op1 = NULL_TREE;
978 if (implicit != ICV_CAST && !side_effects)
980 if (op1)
981 new_op1 = convert_to_void (op1, ICV_SECOND_OF_COND, complain);
982 new_op2 = convert_to_void (op2, ICV_THIRD_OF_COND, complain);
984 else
986 if (op1)
987 new_op1 = convert_to_void (op1, ICV_CAST, complain);
988 new_op2 = convert_to_void (op2, ICV_CAST, complain);
991 expr = build3 (COND_EXPR, TREE_TYPE (new_op2),
992 TREE_OPERAND (expr, 0), new_op1, new_op2);
993 break;
996 case COMPOUND_EXPR:
998 /* The second part of a compound expr contains the value. */
999 tree op1 = TREE_OPERAND (expr,1);
1000 tree new_op1;
1001 if (implicit != ICV_CAST && !TREE_NO_WARNING (expr))
1002 new_op1 = convert_to_void (op1, ICV_RIGHT_OF_COMMA, complain);
1003 else
1004 new_op1 = convert_to_void (op1, ICV_CAST, complain);
1006 if (new_op1 != op1)
1008 tree t = build2 (COMPOUND_EXPR, TREE_TYPE (new_op1),
1009 TREE_OPERAND (expr, 0), new_op1);
1010 expr = t;
1013 break;
1016 case NON_LVALUE_EXPR:
1017 case NOP_EXPR:
1018 /* These have already decayed to rvalue. */
1019 break;
1021 case CALL_EXPR: /* We have a special meaning for volatile void fn(). */
1022 break;
1024 case INDIRECT_REF:
1026 tree type = TREE_TYPE (expr);
1027 int is_reference = TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0)))
1028 == REFERENCE_TYPE;
1029 int is_volatile = TYPE_VOLATILE (type);
1030 int is_complete = COMPLETE_TYPE_P (complete_type (type));
1032 /* Can't load the value if we don't know the type. */
1033 if (is_volatile && !is_complete)
1035 if (complain & tf_warning)
1036 switch (implicit)
1038 case ICV_CAST:
1039 warning_at (loc, 0, "conversion to void will not access "
1040 "object of incomplete type %qT", type);
1041 break;
1042 case ICV_SECOND_OF_COND:
1043 warning_at (loc, 0, "indirection will not access object of "
1044 "incomplete type %qT in second operand "
1045 "of conditional expression", type);
1046 break;
1047 case ICV_THIRD_OF_COND:
1048 warning_at (loc, 0, "indirection will not access object of "
1049 "incomplete type %qT in third operand "
1050 "of conditional expression", type);
1051 break;
1052 case ICV_RIGHT_OF_COMMA:
1053 warning_at (loc, 0, "indirection will not access object of "
1054 "incomplete type %qT in right operand of "
1055 "comma operator", type);
1056 break;
1057 case ICV_LEFT_OF_COMMA:
1058 warning_at (loc, 0, "indirection will not access object of "
1059 "incomplete type %qT in left operand of "
1060 "comma operator", type);
1061 break;
1062 case ICV_STATEMENT:
1063 warning_at (loc, 0, "indirection will not access object of "
1064 "incomplete type %qT in statement", type);
1065 break;
1066 case ICV_THIRD_IN_FOR:
1067 warning_at (loc, 0, "indirection will not access object of "
1068 "incomplete type %qT in for increment "
1069 "expression", type);
1070 break;
1071 default:
1072 gcc_unreachable ();
1075 /* Don't load the value if this is an implicit dereference, or if
1076 the type needs to be handled by ctors/dtors. */
1077 else if (is_volatile && is_reference)
1079 if (complain & tf_warning)
1080 switch (implicit)
1082 case ICV_CAST:
1083 warning_at (loc, 0, "conversion to void will not access "
1084 "object of type %qT", type);
1085 break;
1086 case ICV_SECOND_OF_COND:
1087 warning_at (loc, 0, "implicit dereference will not access "
1088 "object of type %qT in second operand of "
1089 "conditional expression", type);
1090 break;
1091 case ICV_THIRD_OF_COND:
1092 warning_at (loc, 0, "implicit dereference will not access "
1093 "object of type %qT in third operand of "
1094 "conditional expression", type);
1095 break;
1096 case ICV_RIGHT_OF_COMMA:
1097 warning_at (loc, 0, "implicit dereference will not access "
1098 "object of type %qT in right operand of "
1099 "comma operator", type);
1100 break;
1101 case ICV_LEFT_OF_COMMA:
1102 warning_at (loc, 0, "implicit dereference will not access "
1103 "object of type %qT in left operand of comma "
1104 "operator", type);
1105 break;
1106 case ICV_STATEMENT:
1107 warning_at (loc, 0, "implicit dereference will not access "
1108 "object of type %qT in statement", type);
1109 break;
1110 case ICV_THIRD_IN_FOR:
1111 warning_at (loc, 0, "implicit dereference will not access "
1112 "object of type %qT in for increment expression",
1113 type);
1114 break;
1115 default:
1116 gcc_unreachable ();
1119 else if (is_volatile && TREE_ADDRESSABLE (type))
1121 if (complain & tf_warning)
1122 switch (implicit)
1124 case ICV_CAST:
1125 warning_at (loc, 0, "conversion to void will not access "
1126 "object of non-trivially-copyable type %qT",
1127 type);
1128 break;
1129 case ICV_SECOND_OF_COND:
1130 warning_at (loc, 0, "indirection will not access object of "
1131 "non-trivially-copyable type %qT in second "
1132 "operand of conditional expression", type);
1133 break;
1134 case ICV_THIRD_OF_COND:
1135 warning_at (loc, 0, "indirection will not access object of "
1136 "non-trivially-copyable type %qT in third "
1137 "operand of conditional expression", type);
1138 break;
1139 case ICV_RIGHT_OF_COMMA:
1140 warning_at (loc, 0, "indirection will not access object of "
1141 "non-trivially-copyable type %qT in right "
1142 "operand of comma operator", type);
1143 break;
1144 case ICV_LEFT_OF_COMMA:
1145 warning_at (loc, 0, "indirection will not access object of "
1146 "non-trivially-copyable type %qT in left "
1147 "operand of comma operator", type);
1148 break;
1149 case ICV_STATEMENT:
1150 warning_at (loc, 0, "indirection will not access object of "
1151 "non-trivially-copyable type %qT in statement",
1152 type);
1153 break;
1154 case ICV_THIRD_IN_FOR:
1155 warning_at (loc, 0, "indirection will not access object of "
1156 "non-trivially-copyable type %qT in for "
1157 "increment expression", type);
1158 break;
1159 default:
1160 gcc_unreachable ();
1163 if (is_reference || !is_volatile || !is_complete || TREE_ADDRESSABLE (type))
1165 /* Emit a warning (if enabled) when the "effect-less" INDIRECT_REF
1166 operation is stripped off. Note that we don't warn about
1167 - an expression with TREE_NO_WARNING set. (For an example of
1168 such expressions, see build_over_call in call.c.)
1169 - automatic dereferencing of references, since the user cannot
1170 control it. (See also warn_if_unused_value() in c-common.c.) */
1171 if (warn_unused_value
1172 && implicit != ICV_CAST
1173 && (complain & tf_warning)
1174 && !TREE_NO_WARNING (expr)
1175 && !is_reference)
1176 warning_at (loc, OPT_Wunused_value, "value computed is not used");
1177 expr = TREE_OPERAND (expr, 0);
1180 break;
1183 case VAR_DECL:
1185 /* External variables might be incomplete. */
1186 tree type = TREE_TYPE (expr);
1187 int is_complete = COMPLETE_TYPE_P (complete_type (type));
1189 if (TYPE_VOLATILE (type) && !is_complete && (complain & tf_warning))
1190 switch (implicit)
1192 case ICV_CAST:
1193 warning_at (loc, 0, "conversion to void will not access "
1194 "object %qE of incomplete type %qT", expr, type);
1195 break;
1196 case ICV_SECOND_OF_COND:
1197 warning_at (loc, 0, "variable %qE of incomplete type %qT will "
1198 "not be accessed in second operand of "
1199 "conditional expression", expr, type);
1200 break;
1201 case ICV_THIRD_OF_COND:
1202 warning_at (loc, 0, "variable %qE of incomplete type %qT will "
1203 "not be accessed in third operand of "
1204 "conditional expression", expr, type);
1205 break;
1206 case ICV_RIGHT_OF_COMMA:
1207 warning_at (loc, 0, "variable %qE of incomplete type %qT will "
1208 "not be accessed in right operand of comma operator",
1209 expr, type);
1210 break;
1211 case ICV_LEFT_OF_COMMA:
1212 warning_at (loc, 0, "variable %qE of incomplete type %qT will "
1213 "not be accessed in left operand of comma operator",
1214 expr, type);
1215 break;
1216 case ICV_STATEMENT:
1217 warning_at (loc, 0, "variable %qE of incomplete type %qT will "
1218 "not be accessed in statement", expr, type);
1219 break;
1220 case ICV_THIRD_IN_FOR:
1221 warning_at (loc, 0, "variable %qE of incomplete type %qT will "
1222 "not be accessed in for increment expression",
1223 expr, type);
1224 break;
1225 default:
1226 gcc_unreachable ();
1229 break;
1232 case TARGET_EXPR:
1233 /* Don't bother with the temporary object returned from a function if
1234 we don't use it and don't need to destroy it. We'll still
1235 allocate space for it in expand_call or declare_return_variable,
1236 but we don't need to track it through all the tree phases. */
1237 if (TARGET_EXPR_IMPLICIT_P (expr)
1238 && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (expr)))
1240 tree init = TARGET_EXPR_INITIAL (expr);
1241 if (TREE_CODE (init) == AGGR_INIT_EXPR
1242 && !AGGR_INIT_VIA_CTOR_P (init))
1244 tree fn = AGGR_INIT_EXPR_FN (init);
1245 expr = build_call_array_loc (input_location,
1246 TREE_TYPE (TREE_TYPE (TREE_TYPE (fn))),
1248 aggr_init_expr_nargs (init),
1249 AGGR_INIT_EXPR_ARGP (init));
1252 break;
1254 default:;
1256 expr = resolve_nondeduced_context (expr);
1258 tree probe = expr;
1260 if (TREE_CODE (probe) == ADDR_EXPR)
1261 probe = TREE_OPERAND (expr, 0);
1262 if (type_unknown_p (probe))
1264 /* [over.over] enumerates the places where we can take the address
1265 of an overloaded function, and this is not one of them. */
1266 if (complain & tf_error)
1267 switch (implicit)
1269 case ICV_CAST:
1270 error_at (loc, "conversion to void "
1271 "cannot resolve address of overloaded function");
1272 break;
1273 case ICV_SECOND_OF_COND:
1274 error_at (loc, "second operand of conditional expression "
1275 "cannot resolve address of overloaded function");
1276 break;
1277 case ICV_THIRD_OF_COND:
1278 error_at (loc, "third operand of conditional expression "
1279 "cannot resolve address of overloaded function");
1280 break;
1281 case ICV_RIGHT_OF_COMMA:
1282 error_at (loc, "right operand of comma operator "
1283 "cannot resolve address of overloaded function");
1284 break;
1285 case ICV_LEFT_OF_COMMA:
1286 error_at (loc, "left operand of comma operator "
1287 "cannot resolve address of overloaded function");
1288 break;
1289 case ICV_STATEMENT:
1290 error_at (loc, "statement "
1291 "cannot resolve address of overloaded function");
1292 break;
1293 case ICV_THIRD_IN_FOR:
1294 error_at (loc, "for increment expression "
1295 "cannot resolve address of overloaded function");
1296 break;
1298 else
1299 return error_mark_node;
1300 expr = void_node;
1302 else if (implicit != ICV_CAST && probe == expr && is_overloaded_fn (probe))
1304 /* Only warn when there is no &. */
1305 if (complain & tf_warning)
1306 switch (implicit)
1308 case ICV_SECOND_OF_COND:
1309 warning_at (loc, OPT_Waddress,
1310 "second operand of conditional expression "
1311 "is a reference, not call, to function %qE", expr);
1312 break;
1313 case ICV_THIRD_OF_COND:
1314 warning_at (loc, OPT_Waddress,
1315 "third operand of conditional expression "
1316 "is a reference, not call, to function %qE", expr);
1317 break;
1318 case ICV_RIGHT_OF_COMMA:
1319 warning_at (loc, OPT_Waddress,
1320 "right operand of comma operator "
1321 "is a reference, not call, to function %qE", expr);
1322 break;
1323 case ICV_LEFT_OF_COMMA:
1324 warning_at (loc, OPT_Waddress,
1325 "left operand of comma operator "
1326 "is a reference, not call, to function %qE", expr);
1327 break;
1328 case ICV_STATEMENT:
1329 warning_at (loc, OPT_Waddress,
1330 "statement is a reference, not call, to function %qE",
1331 expr);
1332 break;
1333 case ICV_THIRD_IN_FOR:
1334 warning_at (loc, OPT_Waddress,
1335 "for increment expression "
1336 "is a reference, not call, to function %qE", expr);
1337 break;
1338 default:
1339 gcc_unreachable ();
1342 if (TREE_CODE (expr) == COMPONENT_REF)
1343 expr = TREE_OPERAND (expr, 0);
1347 if (expr != error_mark_node && !VOID_TYPE_P (TREE_TYPE (expr)))
1349 if (implicit != ICV_CAST
1350 && warn_unused_value
1351 && !TREE_NO_WARNING (expr)
1352 && !processing_template_decl)
1354 /* The middle end does not warn about expressions that have
1355 been explicitly cast to void, so we must do so here. */
1356 if (!TREE_SIDE_EFFECTS (expr)) {
1357 if (complain & tf_warning)
1358 switch (implicit)
1360 case ICV_SECOND_OF_COND:
1361 warning_at (loc, OPT_Wunused_value,
1362 "second operand of conditional expression "
1363 "has no effect");
1364 break;
1365 case ICV_THIRD_OF_COND:
1366 warning_at (loc, OPT_Wunused_value,
1367 "third operand of conditional expression "
1368 "has no effect");
1369 break;
1370 case ICV_RIGHT_OF_COMMA:
1371 warning_at (loc, OPT_Wunused_value,
1372 "right operand of comma operator has no effect");
1373 break;
1374 case ICV_LEFT_OF_COMMA:
1375 warning_at (loc, OPT_Wunused_value,
1376 "left operand of comma operator has no effect");
1377 break;
1378 case ICV_STATEMENT:
1379 warning_at (loc, OPT_Wunused_value,
1380 "statement has no effect");
1381 break;
1382 case ICV_THIRD_IN_FOR:
1383 warning_at (loc, OPT_Wunused_value,
1384 "for increment expression has no effect");
1385 break;
1386 default:
1387 gcc_unreachable ();
1390 else
1392 tree e;
1393 enum tree_code code;
1394 enum tree_code_class tclass;
1396 e = expr;
1397 /* We might like to warn about (say) "(int) f()", as the
1398 cast has no effect, but the compiler itself will
1399 generate implicit conversions under some
1400 circumstances. (For example a block copy will be
1401 turned into a call to "__builtin_memcpy", with a
1402 conversion of the return value to an appropriate
1403 type.) So, to avoid false positives, we strip
1404 conversions. Do not use STRIP_NOPs because it will
1405 not strip conversions to "void", as that is not a
1406 mode-preserving conversion. */
1407 while (TREE_CODE (e) == NOP_EXPR)
1408 e = TREE_OPERAND (e, 0);
1410 code = TREE_CODE (e);
1411 tclass = TREE_CODE_CLASS (code);
1412 if ((tclass == tcc_comparison
1413 || tclass == tcc_unary
1414 || (tclass == tcc_binary
1415 && !(code == MODIFY_EXPR
1416 || code == INIT_EXPR
1417 || code == PREDECREMENT_EXPR
1418 || code == PREINCREMENT_EXPR
1419 || code == POSTDECREMENT_EXPR
1420 || code == POSTINCREMENT_EXPR))
1421 || code == VEC_PERM_EXPR
1422 || code == VEC_COND_EXPR)
1423 && (complain & tf_warning))
1424 warning_at (loc, OPT_Wunused_value, "value computed is not used");
1427 expr = build1 (CONVERT_EXPR, void_type_node, expr);
1429 if (! TREE_SIDE_EFFECTS (expr))
1430 expr = void_node;
1431 return expr;
1434 /* Create an expression whose value is that of EXPR,
1435 converted to type TYPE. The TREE_TYPE of the value
1436 is always TYPE. This function implements all reasonable
1437 conversions; callers should filter out those that are
1438 not permitted by the language being compiled.
1440 Most of this routine is from build_reinterpret_cast.
1442 The back end cannot call cp_convert (what was convert) because
1443 conversions to/from basetypes may involve memory references
1444 (vbases) and adding or subtracting small values (multiple
1445 inheritance), but it calls convert from the constant folding code
1446 on subtrees of already built trees after it has ripped them apart.
1448 Also, if we ever support range variables, we'll probably also have to
1449 do a little bit more work. */
1451 tree
1452 convert (tree type, tree expr)
1454 tree intype;
1456 if (type == error_mark_node || expr == error_mark_node)
1457 return error_mark_node;
1459 intype = TREE_TYPE (expr);
1461 if (POINTER_TYPE_P (type) && POINTER_TYPE_P (intype))
1462 return fold_if_not_in_template (build_nop (type, expr));
1464 return ocp_convert (type, expr, CONV_OLD_CONVERT,
1465 LOOKUP_NORMAL|LOOKUP_NO_CONVERSION,
1466 tf_warning_or_error);
1469 /* Like cp_convert, except permit conversions to take place which
1470 are not normally allowed due to access restrictions
1471 (such as conversion from sub-type to private super-type). */
1473 tree
1474 convert_force (tree type, tree expr, int convtype, tsubst_flags_t complain)
1476 tree e = expr;
1477 enum tree_code code = TREE_CODE (type);
1479 if (code == REFERENCE_TYPE)
1480 return (fold_if_not_in_template
1481 (convert_to_reference (type, e, CONV_C_CAST, 0,
1482 NULL_TREE, complain)));
1484 if (code == POINTER_TYPE)
1485 return fold_if_not_in_template (convert_to_pointer_force (type, e,
1486 complain));
1488 /* From typeck.c convert_for_assignment */
1489 if (((TYPE_PTR_P (TREE_TYPE (e)) && TREE_CODE (e) == ADDR_EXPR
1490 && TREE_CODE (TREE_TYPE (TREE_TYPE (e))) == METHOD_TYPE)
1491 || integer_zerop (e)
1492 || TYPE_PTRMEMFUNC_P (TREE_TYPE (e)))
1493 && TYPE_PTRMEMFUNC_P (type))
1494 /* compatible pointer to member functions. */
1495 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), e, 1,
1496 /*c_cast_p=*/1, complain);
1498 return ocp_convert (type, e, CONV_C_CAST|convtype, LOOKUP_NORMAL, complain);
1501 /* Convert an aggregate EXPR to type XTYPE. If a conversion
1502 exists, return the attempted conversion. This may
1503 return ERROR_MARK_NODE if the conversion is not
1504 allowed (references private members, etc).
1505 If no conversion exists, NULL_TREE is returned.
1507 FIXME: Ambiguity checking is wrong. Should choose one by the implicit
1508 object parameter, or by the second standard conversion sequence if
1509 that doesn't do it. This will probably wait for an overloading rewrite.
1510 (jason 8/9/95) */
1512 static tree
1513 build_type_conversion (tree xtype, tree expr)
1515 /* C++: check to see if we can convert this aggregate type
1516 into the required type. */
1517 return build_user_type_conversion (xtype, expr, LOOKUP_NORMAL,
1518 tf_warning_or_error);
1521 /* Convert the given EXPR to one of a group of types suitable for use in an
1522 expression. DESIRES is a combination of various WANT_* flags (q.v.)
1523 which indicates which types are suitable. If COMPLAIN is true, complain
1524 about ambiguity; otherwise, the caller will deal with it. */
1526 tree
1527 build_expr_type_conversion (int desires, tree expr, bool complain)
1529 tree basetype = TREE_TYPE (expr);
1530 tree conv = NULL_TREE;
1531 tree winner = NULL_TREE;
1533 if (expr == null_node
1534 && (desires & WANT_INT)
1535 && !(desires & WANT_NULL))
1537 source_location loc =
1538 expansion_point_location_if_in_system_header (input_location);
1540 warning_at (loc, OPT_Wconversion_null,
1541 "converting NULL to non-pointer type");
1544 if (basetype == error_mark_node)
1545 return error_mark_node;
1547 if (! MAYBE_CLASS_TYPE_P (basetype))
1548 switch (TREE_CODE (basetype))
1550 case INTEGER_TYPE:
1551 if ((desires & WANT_NULL) && null_ptr_cst_p (expr))
1552 return expr;
1553 /* else fall through... */
1555 case BOOLEAN_TYPE:
1556 return (desires & WANT_INT) ? expr : NULL_TREE;
1557 case ENUMERAL_TYPE:
1558 return (desires & WANT_ENUM) ? expr : NULL_TREE;
1559 case REAL_TYPE:
1560 return (desires & WANT_FLOAT) ? expr : NULL_TREE;
1561 case POINTER_TYPE:
1562 return (desires & WANT_POINTER) ? expr : NULL_TREE;
1564 case FUNCTION_TYPE:
1565 case ARRAY_TYPE:
1566 return (desires & WANT_POINTER) ? decay_conversion (expr,
1567 tf_warning_or_error)
1568 : NULL_TREE;
1570 case COMPLEX_TYPE:
1571 case VECTOR_TYPE:
1572 if ((desires & WANT_VECTOR_OR_COMPLEX) == 0)
1573 return NULL_TREE;
1574 switch (TREE_CODE (TREE_TYPE (basetype)))
1576 case INTEGER_TYPE:
1577 case BOOLEAN_TYPE:
1578 return (desires & WANT_INT) ? expr : NULL_TREE;
1579 case ENUMERAL_TYPE:
1580 return (desires & WANT_ENUM) ? expr : NULL_TREE;
1581 case REAL_TYPE:
1582 return (desires & WANT_FLOAT) ? expr : NULL_TREE;
1583 default:
1584 return NULL_TREE;
1587 default:
1588 return NULL_TREE;
1591 /* The code for conversions from class type is currently only used for
1592 delete expressions. Other expressions are handled by build_new_op. */
1593 if (!complete_type_or_maybe_complain (basetype, expr, complain))
1594 return error_mark_node;
1595 if (!TYPE_HAS_CONVERSION (basetype))
1596 return NULL_TREE;
1598 for (conv = lookup_conversions (basetype); conv; conv = TREE_CHAIN (conv))
1600 int win = 0;
1601 tree candidate;
1602 tree cand = TREE_VALUE (conv);
1603 cand = OVL_CURRENT (cand);
1605 if (winner && winner == cand)
1606 continue;
1608 if (DECL_NONCONVERTING_P (cand))
1609 continue;
1611 candidate = non_reference (TREE_TYPE (TREE_TYPE (cand)));
1613 switch (TREE_CODE (candidate))
1615 case BOOLEAN_TYPE:
1616 case INTEGER_TYPE:
1617 win = (desires & WANT_INT); break;
1618 case ENUMERAL_TYPE:
1619 win = (desires & WANT_ENUM); break;
1620 case REAL_TYPE:
1621 win = (desires & WANT_FLOAT); break;
1622 case POINTER_TYPE:
1623 win = (desires & WANT_POINTER); break;
1625 case COMPLEX_TYPE:
1626 case VECTOR_TYPE:
1627 if ((desires & WANT_VECTOR_OR_COMPLEX) == 0)
1628 break;
1629 switch (TREE_CODE (TREE_TYPE (candidate)))
1631 case BOOLEAN_TYPE:
1632 case INTEGER_TYPE:
1633 win = (desires & WANT_INT); break;
1634 case ENUMERAL_TYPE:
1635 win = (desires & WANT_ENUM); break;
1636 case REAL_TYPE:
1637 win = (desires & WANT_FLOAT); break;
1638 default:
1639 break;
1641 break;
1643 default:
1644 /* A wildcard could be instantiated to match any desired
1645 type, but we can't deduce the template argument. */
1646 if (WILDCARD_TYPE_P (candidate))
1647 win = true;
1648 break;
1651 if (win)
1653 if (TREE_CODE (cand) == TEMPLATE_DECL)
1655 if (complain)
1656 error ("default type conversion can't deduce template"
1657 " argument for %qD", cand);
1658 return error_mark_node;
1661 if (winner)
1663 tree winner_type
1664 = non_reference (TREE_TYPE (TREE_TYPE (winner)));
1666 if (!same_type_ignoring_top_level_qualifiers_p (winner_type,
1667 candidate))
1669 if (complain)
1671 error ("ambiguous default type conversion from %qT",
1672 basetype);
1673 inform (input_location,
1674 " candidate conversions include %qD and %qD",
1675 winner, cand);
1677 return error_mark_node;
1681 winner = cand;
1685 if (winner)
1687 tree type = non_reference (TREE_TYPE (TREE_TYPE (winner)));
1688 return build_user_type_conversion (type, expr, LOOKUP_NORMAL,
1689 tf_warning_or_error);
1692 return NULL_TREE;
1695 /* Implements integral promotion (4.1) and float->double promotion. */
1697 tree
1698 type_promotes_to (tree type)
1700 tree promoted_type;
1702 if (type == error_mark_node)
1703 return error_mark_node;
1705 type = TYPE_MAIN_VARIANT (type);
1707 /* Check for promotions of target-defined types first. */
1708 promoted_type = targetm.promoted_type (type);
1709 if (promoted_type)
1710 return promoted_type;
1712 /* bool always promotes to int (not unsigned), even if it's the same
1713 size. */
1714 if (TREE_CODE (type) == BOOLEAN_TYPE)
1715 type = integer_type_node;
1717 /* Normally convert enums to int, but convert wide enums to something
1718 wider. Scoped enums don't promote, but pretend they do for backward
1719 ABI bug compatibility wrt varargs. */
1720 else if (TREE_CODE (type) == ENUMERAL_TYPE
1721 || type == char16_type_node
1722 || type == char32_type_node
1723 || type == wchar_type_node)
1725 int precision = MAX (TYPE_PRECISION (type),
1726 TYPE_PRECISION (integer_type_node));
1727 tree totype = c_common_type_for_size (precision, 0);
1728 tree prom = type;
1729 if (TREE_CODE (prom) == ENUMERAL_TYPE)
1730 prom = ENUM_UNDERLYING_TYPE (prom);
1731 if (TYPE_UNSIGNED (prom)
1732 && ! int_fits_type_p (TYPE_MAX_VALUE (prom), totype))
1733 prom = c_common_type_for_size (precision, 1);
1734 else
1735 prom = totype;
1736 if (SCOPED_ENUM_P (type))
1738 if (abi_version_crosses (6)
1739 && TYPE_MODE (prom) != TYPE_MODE (type))
1740 warning (OPT_Wabi, "scoped enum %qT passed through ... as "
1741 "%qT before -fabi-version=6, %qT after",
1742 type, prom, ENUM_UNDERLYING_TYPE (type));
1743 if (!abi_version_at_least (6))
1744 type = prom;
1746 else
1747 type = prom;
1749 else if (c_promoting_integer_type_p (type))
1751 /* Retain unsignedness if really not getting bigger. */
1752 if (TYPE_UNSIGNED (type)
1753 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
1754 type = unsigned_type_node;
1755 else
1756 type = integer_type_node;
1758 else if (type == float_type_node)
1759 type = double_type_node;
1761 return type;
1764 /* The routines below this point are carefully written to conform to
1765 the standard. They use the same terminology, and follow the rules
1766 closely. Although they are used only in pt.c at the moment, they
1767 should presumably be used everywhere in the future. */
1769 /* Attempt to perform qualification conversions on EXPR to convert it
1770 to TYPE. Return the resulting expression, or error_mark_node if
1771 the conversion was impossible. */
1773 tree
1774 perform_qualification_conversions (tree type, tree expr)
1776 tree expr_type;
1778 expr_type = TREE_TYPE (expr);
1780 if (same_type_p (type, expr_type))
1781 return expr;
1782 else if (TYPE_PTR_P (type) && TYPE_PTR_P (expr_type)
1783 && comp_ptr_ttypes (TREE_TYPE (type), TREE_TYPE (expr_type)))
1784 return build_nop (type, expr);
1785 else if (TYPE_PTRMEM_P (type) && TYPE_PTRMEM_P (expr_type)
1786 && same_type_p (TYPE_PTRMEM_CLASS_TYPE (type),
1787 TYPE_PTRMEM_CLASS_TYPE (expr_type))
1788 && comp_ptr_ttypes (TYPE_PTRMEM_POINTED_TO_TYPE (type),
1789 TYPE_PTRMEM_POINTED_TO_TYPE (expr_type)))
1790 return build_nop (type, expr);
1791 else
1792 return error_mark_node;