* g++.dg/cpp0x/constexpr-53094-2.C: Ignore non-standard ABI
[official-gcc.git] / gcc / cp / cvt.c
blob8e8ce53325ed7fc8d05b9d220ce87f2b540573a6
1 /* Language-level data type conversion for GNU C++.
2 Copyright (C) 1987-2013 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 "tree.h"
32 #include "flags.h"
33 #include "cp-tree.h"
34 #include "intl.h"
35 #include "convert.h"
36 #include "decl.h"
37 #include "target.h"
39 static tree cp_convert_to_pointer (tree, tree, tsubst_flags_t);
40 static tree convert_to_pointer_force (tree, tree, tsubst_flags_t);
41 static tree build_type_conversion (tree, tree);
42 static tree build_up_reference (tree, tree, int, tree, tsubst_flags_t);
43 static void warn_ref_binding (location_t, tree, tree, tree);
45 /* Change of width--truncation and extension of integers or reals--
46 is represented with NOP_EXPR. Proper functioning of many things
47 assumes that no other conversions can be NOP_EXPRs.
49 Conversion between integer and pointer is represented with CONVERT_EXPR.
50 Converting integer to real uses FLOAT_EXPR
51 and real to integer uses FIX_TRUNC_EXPR.
53 Here is a list of all the functions that assume that widening and
54 narrowing is always done with a NOP_EXPR:
55 In convert.c, convert_to_integer.
56 In c-typeck.c, build_binary_op_nodefault (boolean ops),
57 and c_common_truthvalue_conversion.
58 In expr.c: expand_expr, for operands of a MULT_EXPR.
59 In fold-const.c: fold.
60 In tree.c: get_narrower and get_unwidened.
62 C++: in multiple-inheritance, converting between pointers may involve
63 adjusting them by a delta stored within the class definition. */
65 /* Subroutines of `convert'. */
67 /* if converting pointer to pointer
68 if dealing with classes, check for derived->base or vice versa
69 else if dealing with method pointers, delegate
70 else convert blindly
71 else if converting class, pass off to build_type_conversion
72 else try C-style pointer conversion. */
74 static tree
75 cp_convert_to_pointer (tree type, tree expr, tsubst_flags_t complain)
77 tree intype = TREE_TYPE (expr);
78 enum tree_code form;
79 tree rval;
80 location_t loc = EXPR_LOC_OR_HERE (expr);
82 if (intype == error_mark_node)
83 return error_mark_node;
85 if (MAYBE_CLASS_TYPE_P (intype))
87 intype = complete_type (intype);
88 if (!COMPLETE_TYPE_P (intype))
90 if (complain & tf_error)
91 error_at (loc, "can%'t convert from incomplete type %qT to %qT",
92 intype, type);
93 return error_mark_node;
96 rval = build_type_conversion (type, expr);
97 if (rval)
99 if ((complain & tf_error)
100 && rval == error_mark_node)
101 error_at (loc, "conversion of %qE from %qT to %qT is ambiguous",
102 expr, intype, type);
103 return rval;
107 /* Handle anachronistic conversions from (::*)() to cv void* or (*)(). */
108 if (TREE_CODE (type) == POINTER_TYPE
109 && (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
110 || VOID_TYPE_P (TREE_TYPE (type))))
112 if (TYPE_PTRMEMFUNC_P (intype)
113 || TREE_CODE (intype) == METHOD_TYPE)
114 return convert_member_func_to_ptr (type, expr, complain);
115 if (TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE)
116 return build_nop (type, expr);
117 intype = TREE_TYPE (expr);
120 if (expr == error_mark_node)
121 return error_mark_node;
123 form = TREE_CODE (intype);
125 if (POINTER_TYPE_P (intype))
127 intype = TYPE_MAIN_VARIANT (intype);
129 if (TYPE_MAIN_VARIANT (type) != intype
130 && TREE_CODE (type) == POINTER_TYPE
131 && TREE_CODE (TREE_TYPE (type)) == RECORD_TYPE
132 && MAYBE_CLASS_TYPE_P (TREE_TYPE (type))
133 && MAYBE_CLASS_TYPE_P (TREE_TYPE (intype))
134 && TREE_CODE (TREE_TYPE (intype)) == RECORD_TYPE)
136 enum tree_code code = PLUS_EXPR;
137 tree binfo;
138 tree intype_class;
139 tree type_class;
140 bool same_p;
142 intype_class = TREE_TYPE (intype);
143 type_class = TREE_TYPE (type);
145 same_p = same_type_p (TYPE_MAIN_VARIANT (intype_class),
146 TYPE_MAIN_VARIANT (type_class));
147 binfo = NULL_TREE;
148 /* Try derived to base conversion. */
149 if (!same_p)
150 binfo = lookup_base (intype_class, type_class, ba_check,
151 NULL, complain);
152 if (!same_p && !binfo)
154 /* Try base to derived conversion. */
155 binfo = lookup_base (type_class, intype_class, ba_check,
156 NULL, complain);
157 code = MINUS_EXPR;
159 if (binfo == error_mark_node)
160 return error_mark_node;
161 if (binfo || same_p)
163 if (binfo)
164 expr = build_base_path (code, expr, binfo, 0, complain);
165 /* Add any qualifier conversions. */
166 return build_nop (type, expr);
170 if (TYPE_PTRMEMFUNC_P (type))
172 if (complain & tf_error)
173 error_at (loc, "cannot convert %qE from type %qT to type %qT",
174 expr, intype, type);
175 return error_mark_node;
178 return build_nop (type, expr);
180 else if ((TYPE_PTRDATAMEM_P (type) && TYPE_PTRDATAMEM_P (intype))
181 || (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype)))
182 return convert_ptrmem (type, expr, /*allow_inverse_p=*/false,
183 /*c_cast_p=*/false, complain);
184 else if (TYPE_PTRMEMFUNC_P (intype))
186 if (!warn_pmf2ptr)
188 if (TREE_CODE (expr) == PTRMEM_CST)
189 return cp_convert_to_pointer (type, PTRMEM_CST_MEMBER (expr),
190 complain);
191 else if (TREE_CODE (expr) == OFFSET_REF)
193 tree object = TREE_OPERAND (expr, 0);
194 return get_member_function_from_ptrfunc (&object,
195 TREE_OPERAND (expr, 1),
196 complain);
199 error_at (loc, "cannot convert %qE from type %qT to type %qT",
200 expr, intype, type);
201 return error_mark_node;
204 if (null_ptr_cst_p (expr))
206 if ((complain & tf_warning)
207 && c_inhibit_evaluation_warnings == 0
208 && !NULLPTR_TYPE_P (TREE_TYPE (expr)))
209 warning_at (loc, OPT_Wzero_as_null_pointer_constant,
210 "zero as null pointer constant");
212 if (TYPE_PTRMEMFUNC_P (type))
213 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), expr, 0,
214 /*c_cast_p=*/false, complain);
216 /* A NULL pointer-to-data-member is represented by -1, not by
217 zero. */
218 tree val = (TYPE_PTRDATAMEM_P (type)
219 ? build_int_cst_type (type, -1)
220 : build_int_cst (type, 0));
222 return (TREE_SIDE_EFFECTS (expr)
223 ? build2 (COMPOUND_EXPR, type, expr, val) : val);
225 else if (TYPE_PTRMEM_P (type) && INTEGRAL_CODE_P (form))
227 if (complain & tf_error)
228 error_at (loc, "invalid conversion from %qT to %qT", intype, type);
229 return error_mark_node;
232 if (INTEGRAL_CODE_P (form))
234 if (TYPE_PRECISION (intype) == POINTER_SIZE)
235 return build1 (CONVERT_EXPR, type, expr);
236 expr = cp_convert (c_common_type_for_size (POINTER_SIZE, 0), expr,
237 complain);
238 /* Modes may be different but sizes should be the same. There
239 is supposed to be some integral type that is the same width
240 as a pointer. */
241 gcc_assert (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (expr)))
242 == GET_MODE_SIZE (TYPE_MODE (type)));
244 return convert_to_pointer (type, expr);
247 if (type_unknown_p (expr))
248 return instantiate_type (type, expr, complain);
250 if (complain & tf_error)
251 error_at (loc, "cannot convert %qE from type %qT to type %qT",
252 expr, intype, type);
253 return error_mark_node;
256 /* Like convert, except permit conversions to take place which
257 are not normally allowed due to access restrictions
258 (such as conversion from sub-type to private super-type). */
260 static tree
261 convert_to_pointer_force (tree type, tree expr, tsubst_flags_t complain)
263 tree intype = TREE_TYPE (expr);
264 enum tree_code form = TREE_CODE (intype);
266 if (form == POINTER_TYPE)
268 intype = TYPE_MAIN_VARIANT (intype);
270 if (TYPE_MAIN_VARIANT (type) != intype
271 && TREE_CODE (TREE_TYPE (type)) == RECORD_TYPE
272 && MAYBE_CLASS_TYPE_P (TREE_TYPE (type))
273 && MAYBE_CLASS_TYPE_P (TREE_TYPE (intype))
274 && TREE_CODE (TREE_TYPE (intype)) == RECORD_TYPE)
276 enum tree_code code = PLUS_EXPR;
277 tree binfo;
279 binfo = lookup_base (TREE_TYPE (intype), TREE_TYPE (type),
280 ba_unique, NULL, complain);
281 if (!binfo)
283 binfo = lookup_base (TREE_TYPE (type), TREE_TYPE (intype),
284 ba_unique, NULL, complain);
285 code = MINUS_EXPR;
287 if (binfo == error_mark_node)
288 return error_mark_node;
289 if (binfo)
291 expr = build_base_path (code, expr, binfo, 0, complain);
292 if (expr == error_mark_node)
293 return error_mark_node;
294 /* Add any qualifier conversions. */
295 if (!same_type_p (TREE_TYPE (TREE_TYPE (expr)),
296 TREE_TYPE (type)))
297 expr = build_nop (type, expr);
298 return expr;
303 return cp_convert_to_pointer (type, expr, complain);
306 /* We are passing something to a function which requires a reference.
307 The type we are interested in is in TYPE. The initial
308 value we have to begin with is in ARG.
310 FLAGS controls how we manage access checking.
311 DIRECT_BIND in FLAGS controls how any temporaries are generated.
312 If DIRECT_BIND is set, DECL is the reference we're binding to. */
314 static tree
315 build_up_reference (tree type, tree arg, int flags, tree decl,
316 tsubst_flags_t complain)
318 tree rval;
319 tree argtype = TREE_TYPE (arg);
320 tree target_type = TREE_TYPE (type);
322 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
324 if ((flags & DIRECT_BIND) && ! real_lvalue_p (arg))
326 /* Create a new temporary variable. We can't just use a TARGET_EXPR
327 here because it needs to live as long as DECL. */
328 tree targ = arg;
330 arg = make_temporary_var_for_ref_to_temp (decl, target_type);
332 /* Process the initializer for the declaration. */
333 DECL_INITIAL (arg) = targ;
334 cp_finish_decl (arg, targ, /*init_const_expr_p=*/false, NULL_TREE,
335 LOOKUP_ONLYCONVERTING|DIRECT_BIND);
337 else if (!(flags & DIRECT_BIND) && ! lvalue_p (arg))
338 return get_target_expr_sfinae (arg, complain);
340 /* If we had a way to wrap this up, and say, if we ever needed its
341 address, transform all occurrences of the register, into a memory
342 reference we could win better. */
343 rval = cp_build_addr_expr (arg, complain);
344 if (rval == error_mark_node)
345 return error_mark_node;
347 if ((flags & LOOKUP_PROTECT)
348 && TYPE_MAIN_VARIANT (argtype) != TYPE_MAIN_VARIANT (target_type)
349 && MAYBE_CLASS_TYPE_P (argtype)
350 && MAYBE_CLASS_TYPE_P (target_type))
352 /* We go through lookup_base for the access control. */
353 tree binfo = lookup_base (argtype, target_type, ba_check,
354 NULL, complain);
355 if (binfo == error_mark_node)
356 return error_mark_node;
357 if (binfo == NULL_TREE)
358 return error_not_base_type (target_type, argtype);
359 rval = build_base_path (PLUS_EXPR, rval, binfo, 1, complain);
361 else
362 rval
363 = convert_to_pointer_force (build_pointer_type (target_type),
364 rval, complain);
365 return build_nop (type, rval);
368 /* Subroutine of convert_to_reference. REFTYPE is the target reference type.
369 INTYPE is the original rvalue type and DECL is an optional _DECL node
370 for diagnostics.
372 [dcl.init.ref] says that if an rvalue is used to
373 initialize a reference, then the reference must be to a
374 non-volatile const type. */
376 static void
377 warn_ref_binding (location_t loc, tree reftype, tree intype, tree decl)
379 tree ttl = TREE_TYPE (reftype);
381 if (!CP_TYPE_CONST_NON_VOLATILE_P (ttl))
383 const char *msg;
385 if (CP_TYPE_VOLATILE_P (ttl) && decl)
386 msg = G_("initialization of volatile reference type %q#T from "
387 "rvalue of type %qT");
388 else if (CP_TYPE_VOLATILE_P (ttl))
389 msg = G_("conversion to volatile reference type %q#T "
390 "from rvalue of type %qT");
391 else if (decl)
392 msg = G_("initialization of non-const reference type %q#T from "
393 "rvalue of type %qT");
394 else
395 msg = G_("conversion to non-const reference type %q#T from "
396 "rvalue of type %qT");
398 permerror (loc, msg, reftype, intype);
402 /* For C++: Only need to do one-level references, but cannot
403 get tripped up on signed/unsigned differences.
405 DECL is either NULL_TREE or the _DECL node for a reference that is being
406 initialized. It can be error_mark_node if we don't know the _DECL but
407 we know it's an initialization. */
409 tree
410 convert_to_reference (tree reftype, tree expr, int convtype,
411 int flags, tree decl, tsubst_flags_t complain)
413 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (reftype));
414 tree intype;
415 tree rval = NULL_TREE;
416 tree rval_as_conversion = NULL_TREE;
417 bool can_convert_intype_to_type;
418 location_t loc = EXPR_LOC_OR_HERE (expr);
420 if (TREE_CODE (type) == FUNCTION_TYPE
421 && TREE_TYPE (expr) == unknown_type_node)
422 expr = instantiate_type (type, expr, complain);
424 if (expr == error_mark_node)
425 return error_mark_node;
427 intype = TREE_TYPE (expr);
429 gcc_assert (TREE_CODE (intype) != REFERENCE_TYPE);
430 gcc_assert (TREE_CODE (reftype) == REFERENCE_TYPE);
432 intype = TYPE_MAIN_VARIANT (intype);
434 can_convert_intype_to_type = can_convert (type, intype, complain);
436 if (!can_convert_intype_to_type
437 && (convtype & CONV_IMPLICIT) && MAYBE_CLASS_TYPE_P (intype)
438 && ! (flags & LOOKUP_NO_CONVERSION))
440 /* Look for a user-defined conversion to lvalue that we can use. */
442 rval_as_conversion
443 = build_type_conversion (reftype, expr);
445 if (rval_as_conversion && rval_as_conversion != error_mark_node
446 && real_lvalue_p (rval_as_conversion))
448 expr = rval_as_conversion;
449 rval_as_conversion = NULL_TREE;
450 intype = type;
451 can_convert_intype_to_type = 1;
455 if (((convtype & CONV_STATIC) && can_convert (intype, type, complain))
456 || ((convtype & CONV_IMPLICIT) && can_convert_intype_to_type))
459 tree ttl = TREE_TYPE (reftype);
460 tree ttr = lvalue_type (expr);
462 if ((complain & tf_warning)
463 && ! real_lvalue_p (expr))
464 warn_ref_binding (loc, reftype, intype, decl);
466 if (! (convtype & CONV_CONST)
467 && !at_least_as_qualified_p (ttl, ttr))
469 if (complain & tf_error)
470 permerror (loc, "conversion from %qT to %qT discards qualifiers",
471 ttr, reftype);
472 else
473 return error_mark_node;
477 return build_up_reference (reftype, expr, flags, decl, complain);
479 else if ((convtype & CONV_REINTERPRET) && lvalue_p (expr))
481 /* When casting an lvalue to a reference type, just convert into
482 a pointer to the new type and deference it. This is allowed
483 by San Diego WP section 5.2.9 paragraph 12, though perhaps it
484 should be done directly (jason). (int &)ri ---> *(int*)&ri */
486 /* B* bp; A& ar = (A&)bp; is valid, but it's probably not what they
487 meant. */
488 if ((complain & tf_warning)
489 && TREE_CODE (intype) == POINTER_TYPE
490 && (comptypes (TREE_TYPE (intype), type,
491 COMPARE_BASE | COMPARE_DERIVED)))
492 warning_at (loc, 0, "casting %qT to %qT does not dereference pointer",
493 intype, reftype);
495 rval = cp_build_addr_expr (expr, complain);
496 if (rval != error_mark_node)
497 rval = convert_force (build_pointer_type (TREE_TYPE (reftype)),
498 rval, 0, complain);
499 if (rval != error_mark_node)
500 rval = build1 (NOP_EXPR, reftype, rval);
502 else
504 rval = convert_for_initialization (NULL_TREE, type, expr, flags,
505 ICR_CONVERTING, 0, 0, complain);
506 if (rval == NULL_TREE || rval == error_mark_node)
507 return rval;
508 if (complain & tf_warning)
509 warn_ref_binding (loc, reftype, intype, decl);
510 rval = build_up_reference (reftype, rval, flags, decl, complain);
513 if (rval)
515 /* If we found a way to convert earlier, then use it. */
516 return rval;
519 if (complain & tf_error)
520 error_at (loc, "cannot convert type %qT to type %qT", intype, reftype);
522 return error_mark_node;
525 /* We are using a reference VAL for its value. Bash that reference all the
526 way down to its lowest form. */
528 tree
529 convert_from_reference (tree val)
531 if (TREE_TYPE (val)
532 && TREE_CODE (TREE_TYPE (val)) == REFERENCE_TYPE)
534 tree t = TREE_TYPE (TREE_TYPE (val));
535 tree ref = build1 (INDIRECT_REF, t, val);
537 mark_exp_read (val);
538 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
539 so that we get the proper error message if the result is used
540 to assign to. Also, &* is supposed to be a no-op. */
541 TREE_READONLY (ref) = CP_TYPE_CONST_P (t);
542 TREE_THIS_VOLATILE (ref) = CP_TYPE_VOLATILE_P (t);
543 TREE_SIDE_EFFECTS (ref)
544 = (TREE_THIS_VOLATILE (ref) || TREE_SIDE_EFFECTS (val));
545 val = ref;
548 return val;
551 /* Really perform an lvalue-to-rvalue conversion, including copying an
552 argument of class type into a temporary. */
554 tree
555 force_rvalue (tree expr, tsubst_flags_t complain)
557 tree type = TREE_TYPE (expr);
558 if (MAYBE_CLASS_TYPE_P (type) && TREE_CODE (expr) != TARGET_EXPR)
560 vec<tree, va_gc> *args = make_tree_vector_single (expr);
561 expr = build_special_member_call (NULL_TREE, complete_ctor_identifier,
562 &args, type, LOOKUP_NORMAL, complain);
563 release_tree_vector (args);
564 expr = build_cplus_new (type, expr, complain);
566 else
567 expr = decay_conversion (expr, complain);
569 return expr;
573 /* If EXPR and ORIG are INTEGER_CSTs, return a version of EXPR that has
574 TREE_OVERFLOW set only if it is set in ORIG. Otherwise, return EXPR
575 unchanged. */
577 static tree
578 ignore_overflows (tree expr, tree orig)
580 if (TREE_CODE (expr) == INTEGER_CST
581 && TREE_CODE (orig) == INTEGER_CST
582 && TREE_OVERFLOW (expr) != TREE_OVERFLOW (orig))
584 gcc_assert (!TREE_OVERFLOW (orig));
585 /* Ensure constant sharing. */
586 expr = build_int_cst_wide (TREE_TYPE (expr),
587 TREE_INT_CST_LOW (expr),
588 TREE_INT_CST_HIGH (expr));
590 return expr;
593 /* Fold away simple conversions, but make sure TREE_OVERFLOW is set
594 properly. */
596 tree
597 cp_fold_convert (tree type, tree expr)
599 tree conv = fold_convert (type, expr);
600 conv = ignore_overflows (conv, expr);
601 return conv;
604 /* C++ conversions, preference to static cast conversions. */
606 tree
607 cp_convert (tree type, tree expr, tsubst_flags_t complain)
609 return ocp_convert (type, expr, CONV_OLD_CONVERT, LOOKUP_NORMAL, complain);
612 /* C++ equivalent of convert_and_check but using cp_convert as the
613 conversion function.
615 Convert EXPR to TYPE, warning about conversion problems with constants.
616 Invoke this function on every expression that is converted implicitly,
617 i.e. because of language rules and not because of an explicit cast. */
619 tree
620 cp_convert_and_check (tree type, tree expr, tsubst_flags_t complain)
622 tree result;
624 if (TREE_TYPE (expr) == type)
625 return expr;
627 result = cp_convert (type, expr, complain);
629 if ((complain & tf_warning)
630 && c_inhibit_evaluation_warnings == 0
631 && !TREE_OVERFLOW_P (expr)
632 && result != error_mark_node)
633 warnings_for_convert_and_check (type, expr, result);
635 return result;
638 /* Conversion...
640 FLAGS indicates how we should behave. */
642 tree
643 ocp_convert (tree type, tree expr, int convtype, int flags,
644 tsubst_flags_t complain)
646 tree e = expr;
647 enum tree_code code = TREE_CODE (type);
648 const char *invalid_conv_diag;
649 tree e1;
650 location_t loc = EXPR_LOC_OR_HERE (expr);
652 if (error_operand_p (e) || type == error_mark_node)
653 return error_mark_node;
655 complete_type (type);
656 complete_type (TREE_TYPE (expr));
658 if ((invalid_conv_diag
659 = targetm.invalid_conversion (TREE_TYPE (expr), type)))
661 if (complain & tf_error)
662 error (invalid_conv_diag);
663 return error_mark_node;
666 /* FIXME remove when moving to c_fully_fold model. */
667 /* FIXME do we still need this test? */
668 if (!CLASS_TYPE_P (type))
669 e = integral_constant_value (e);
670 if (error_operand_p (e))
671 return error_mark_node;
673 if (MAYBE_CLASS_TYPE_P (type) && (convtype & CONV_FORCE_TEMP))
674 /* We need a new temporary; don't take this shortcut. */;
675 else if (same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (e)))
677 if (same_type_p (type, TREE_TYPE (e)))
678 /* The call to fold will not always remove the NOP_EXPR as
679 might be expected, since if one of the types is a typedef;
680 the comparison in fold is just equality of pointers, not a
681 call to comptypes. We don't call fold in this case because
682 that can result in infinite recursion; fold will call
683 convert, which will call ocp_convert, etc. */
684 return e;
685 /* For complex data types, we need to perform componentwise
686 conversion. */
687 else if (TREE_CODE (type) == COMPLEX_TYPE)
688 return fold_if_not_in_template (convert_to_complex (type, e));
689 else if (TREE_CODE (type) == VECTOR_TYPE)
690 return fold_if_not_in_template (convert_to_vector (type, e));
691 else if (TREE_CODE (e) == TARGET_EXPR)
693 /* Don't build a NOP_EXPR of class type. Instead, change the
694 type of the temporary. */
695 TREE_TYPE (e) = TREE_TYPE (TARGET_EXPR_SLOT (e)) = type;
696 return e;
698 else
700 /* We shouldn't be treating objects of ADDRESSABLE type as
701 rvalues. */
702 gcc_assert (!TREE_ADDRESSABLE (type));
703 return fold_if_not_in_template (build_nop (type, e));
707 e1 = targetm.convert_to_type (type, e);
708 if (e1)
709 return e1;
711 if (code == VOID_TYPE && (convtype & CONV_STATIC))
713 e = convert_to_void (e, ICV_CAST, complain);
714 return e;
717 if (INTEGRAL_CODE_P (code))
719 tree intype = TREE_TYPE (e);
720 tree converted;
722 if (TREE_CODE (type) == ENUMERAL_TYPE)
724 /* enum = enum, enum = int, enum = float, (enum)pointer are all
725 errors. */
726 if (((INTEGRAL_OR_ENUMERATION_TYPE_P (intype)
727 || TREE_CODE (intype) == REAL_TYPE)
728 && ! (convtype & CONV_STATIC))
729 || TREE_CODE (intype) == POINTER_TYPE)
731 if (complain & tf_error)
732 permerror (loc, "conversion from %q#T to %q#T", intype, type);
733 else
734 return error_mark_node;
737 /* [expr.static.cast]
739 8. A value of integral or enumeration type can be explicitly
740 converted to an enumeration type. The value is unchanged if
741 the original value is within the range of the enumeration
742 values. Otherwise, the resulting enumeration value is
743 unspecified. */
744 if ((complain & tf_warning)
745 && TREE_CODE (e) == INTEGER_CST
746 && !int_fits_type_p (e, ENUM_UNDERLYING_TYPE (type)))
747 warning_at (loc, OPT_Wconversion,
748 "the result of the conversion is unspecified because "
749 "%qE is outside the range of type %qT",
750 expr, type);
752 if (MAYBE_CLASS_TYPE_P (intype))
754 tree rval;
755 rval = build_type_conversion (type, e);
756 if (rval)
757 return rval;
758 if (complain & tf_error)
759 error_at (loc, "%q#T used where a %qT was expected", intype, type);
760 return error_mark_node;
762 if (code == BOOLEAN_TYPE)
764 if (TREE_CODE (intype) == VOID_TYPE)
766 if (complain & tf_error)
767 error_at (loc,
768 "could not convert %qE from %<void%> to %<bool%>",
769 expr);
770 return error_mark_node;
773 /* We can't implicitly convert a scoped enum to bool, so convert
774 to the underlying type first. */
775 if (SCOPED_ENUM_P (intype) && (convtype & CONV_STATIC))
776 e = build_nop (ENUM_UNDERLYING_TYPE (intype), e);
777 return cp_truthvalue_conversion (e);
780 converted = fold_if_not_in_template (convert_to_integer (type, e));
782 /* Ignore any integer overflow caused by the conversion. */
783 return ignore_overflows (converted, e);
785 if (NULLPTR_TYPE_P (type) && e && null_ptr_cst_p (e))
786 return nullptr_node;
787 if (POINTER_TYPE_P (type) || TYPE_PTRMEM_P (type))
788 return fold_if_not_in_template (cp_convert_to_pointer (type, e, complain));
789 if (code == VECTOR_TYPE)
791 tree in_vtype = TREE_TYPE (e);
792 if (MAYBE_CLASS_TYPE_P (in_vtype))
794 tree ret_val;
795 ret_val = build_type_conversion (type, e);
796 if (ret_val)
797 return ret_val;
798 if (complain & tf_error)
799 error_at (loc, "%q#T used where a %qT was expected",
800 in_vtype, type);
801 return error_mark_node;
803 return fold_if_not_in_template (convert_to_vector (type, e));
805 if (code == REAL_TYPE || code == COMPLEX_TYPE)
807 if (MAYBE_CLASS_TYPE_P (TREE_TYPE (e)))
809 tree rval;
810 rval = build_type_conversion (type, e);
811 if (rval)
812 return rval;
813 else if (complain & tf_error)
814 error_at (loc,
815 "%q#T used where a floating point value was expected",
816 TREE_TYPE (e));
818 if (code == REAL_TYPE)
819 return fold_if_not_in_template (convert_to_real (type, e));
820 else if (code == COMPLEX_TYPE)
821 return fold_if_not_in_template (convert_to_complex (type, e));
824 /* New C++ semantics: since assignment is now based on
825 memberwise copying, if the rhs type is derived from the
826 lhs type, then we may still do a conversion. */
827 if (RECORD_OR_UNION_CODE_P (code))
829 tree dtype = TREE_TYPE (e);
830 tree ctor = NULL_TREE;
832 dtype = TYPE_MAIN_VARIANT (dtype);
834 /* Conversion between aggregate types. New C++ semantics allow
835 objects of derived type to be cast to objects of base type.
836 Old semantics only allowed this between pointers.
838 There may be some ambiguity between using a constructor
839 vs. using a type conversion operator when both apply. */
841 ctor = e;
843 if (abstract_virtuals_error_sfinae (NULL_TREE, type, complain))
844 return error_mark_node;
846 if (BRACE_ENCLOSED_INITIALIZER_P (ctor))
847 ctor = perform_implicit_conversion (type, ctor, complain);
848 else if ((flags & LOOKUP_ONLYCONVERTING)
849 && ! (CLASS_TYPE_P (dtype) && DERIVED_FROM_P (type, dtype)))
850 /* For copy-initialization, first we create a temp of the proper type
851 with a user-defined conversion sequence, then we direct-initialize
852 the target with the temp (see [dcl.init]). */
853 ctor = build_user_type_conversion (type, ctor, flags, complain);
854 else
856 vec<tree, va_gc> *ctor_vec = make_tree_vector_single (ctor);
857 ctor = build_special_member_call (NULL_TREE,
858 complete_ctor_identifier,
859 &ctor_vec,
860 type, flags, complain);
861 release_tree_vector (ctor_vec);
863 if (ctor)
864 return build_cplus_new (type, ctor, complain);
867 if (complain & tf_error)
869 /* If the conversion failed and expr was an invalid use of pointer to
870 member function, try to report a meaningful error. */
871 if (invalid_nonstatic_memfn_p (expr, complain))
872 /* We displayed the error message. */;
873 else
874 error_at (loc, "conversion from %qT to non-scalar type %qT requested",
875 TREE_TYPE (expr), type);
877 return error_mark_node;
880 /* When an expression is used in a void context, its value is discarded and
881 no lvalue-rvalue and similar conversions happen [expr.static.cast/4,
882 stmt.expr/1, expr.comma/1]. This permits dereferencing an incomplete type
883 in a void context. The C++ standard does not define what an `access' to an
884 object is, but there is reason to believe that it is the lvalue to rvalue
885 conversion -- if it were not, `*&*p = 1' would violate [expr]/4 in that it
886 accesses `*p' not to calculate the value to be stored. But, dcl.type.cv/8
887 indicates that volatile semantics should be the same between C and C++
888 where ever possible. C leaves it implementation defined as to what
889 constitutes an access to a volatile. So, we interpret `*vp' as a read of
890 the volatile object `vp' points to, unless that is an incomplete type. For
891 volatile references we do not do this interpretation, because that would
892 make it impossible to ignore the reference return value from functions. We
893 issue warnings in the confusing cases.
895 The IMPLICIT is ICV_CAST when the user is explicitly converting an expression
896 to void via a cast. If an expression is being implicitly converted, IMPLICIT
897 indicates the context of the implicit conversion. */
899 tree
900 convert_to_void (tree expr, impl_conv_void implicit, tsubst_flags_t complain)
902 location_t loc = EXPR_LOC_OR_HERE (expr);
904 if (expr == error_mark_node
905 || TREE_TYPE (expr) == error_mark_node)
906 return error_mark_node;
908 if (implicit == ICV_CAST)
909 mark_exp_read (expr);
910 else
912 tree exprv = expr;
914 while (TREE_CODE (exprv) == COMPOUND_EXPR)
915 exprv = TREE_OPERAND (exprv, 1);
916 if (DECL_P (exprv)
917 || handled_component_p (exprv)
918 || TREE_CODE (exprv) == INDIRECT_REF)
919 /* Expr is not being 'used' here, otherwise we whould have
920 called mark_{rl}value_use use here, which would have in turn
921 called mark_exp_read. Rather, we call mark_exp_read directly
922 to avoid some warnings when
923 -Wunused-but-set-{variable,parameter} is in effect. */
924 mark_exp_read (exprv);
927 if (!TREE_TYPE (expr))
928 return expr;
929 if (invalid_nonstatic_memfn_p (expr, complain))
930 return error_mark_node;
931 if (TREE_CODE (expr) == PSEUDO_DTOR_EXPR)
933 if (complain & tf_error)
934 error_at (loc, "pseudo-destructor is not called");
935 return error_mark_node;
937 if (VOID_TYPE_P (TREE_TYPE (expr)))
938 return expr;
939 switch (TREE_CODE (expr))
941 case COND_EXPR:
943 /* The two parts of a cond expr might be separate lvalues. */
944 tree op1 = TREE_OPERAND (expr,1);
945 tree op2 = TREE_OPERAND (expr,2);
946 bool side_effects = ((op1 && TREE_SIDE_EFFECTS (op1))
947 || TREE_SIDE_EFFECTS (op2));
948 tree new_op1, new_op2;
949 new_op1 = NULL_TREE;
950 if (implicit != ICV_CAST && !side_effects)
952 if (op1)
953 new_op1 = convert_to_void (op1, ICV_SECOND_OF_COND, complain);
954 new_op2 = convert_to_void (op2, ICV_THIRD_OF_COND, complain);
956 else
958 if (op1)
959 new_op1 = convert_to_void (op1, ICV_CAST, complain);
960 new_op2 = convert_to_void (op2, ICV_CAST, complain);
963 expr = build3 (COND_EXPR, TREE_TYPE (new_op2),
964 TREE_OPERAND (expr, 0), new_op1, new_op2);
965 break;
968 case COMPOUND_EXPR:
970 /* The second part of a compound expr contains the value. */
971 tree op1 = TREE_OPERAND (expr,1);
972 tree new_op1;
973 if (implicit != ICV_CAST && !TREE_NO_WARNING (expr))
974 new_op1 = convert_to_void (op1, ICV_RIGHT_OF_COMMA, complain);
975 else
976 new_op1 = convert_to_void (op1, ICV_CAST, complain);
978 if (new_op1 != op1)
980 tree t = build2 (COMPOUND_EXPR, TREE_TYPE (new_op1),
981 TREE_OPERAND (expr, 0), new_op1);
982 expr = t;
985 break;
988 case NON_LVALUE_EXPR:
989 case NOP_EXPR:
990 /* These have already decayed to rvalue. */
991 break;
993 case CALL_EXPR: /* We have a special meaning for volatile void fn(). */
994 break;
996 case INDIRECT_REF:
998 tree type = TREE_TYPE (expr);
999 int is_reference = TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0)))
1000 == REFERENCE_TYPE;
1001 int is_volatile = TYPE_VOLATILE (type);
1002 int is_complete = COMPLETE_TYPE_P (complete_type (type));
1004 /* Can't load the value if we don't know the type. */
1005 if (is_volatile && !is_complete)
1007 if (complain & tf_warning)
1008 switch (implicit)
1010 case ICV_CAST:
1011 warning_at (loc, 0, "conversion to void will not access "
1012 "object of incomplete type %qT", type);
1013 break;
1014 case ICV_SECOND_OF_COND:
1015 warning_at (loc, 0, "indirection will not access object of "
1016 "incomplete type %qT in second operand "
1017 "of conditional expression", type);
1018 break;
1019 case ICV_THIRD_OF_COND:
1020 warning_at (loc, 0, "indirection will not access object of "
1021 "incomplete type %qT in third operand "
1022 "of conditional expression", type);
1023 break;
1024 case ICV_RIGHT_OF_COMMA:
1025 warning_at (loc, 0, "indirection will not access object of "
1026 "incomplete type %qT in right operand of "
1027 "comma operator", type);
1028 break;
1029 case ICV_LEFT_OF_COMMA:
1030 warning_at (loc, 0, "indirection will not access object of "
1031 "incomplete type %qT in left operand of "
1032 "comma operator", type);
1033 break;
1034 case ICV_STATEMENT:
1035 warning_at (loc, 0, "indirection will not access object of "
1036 "incomplete type %qT in statement", type);
1037 break;
1038 case ICV_THIRD_IN_FOR:
1039 warning_at (loc, 0, "indirection will not access object of "
1040 "incomplete type %qT in for increment "
1041 "expression", type);
1042 break;
1043 default:
1044 gcc_unreachable ();
1047 /* Don't load the value if this is an implicit dereference, or if
1048 the type needs to be handled by ctors/dtors. */
1049 else if (is_volatile && is_reference)
1051 if (complain & tf_warning)
1052 switch (implicit)
1054 case ICV_CAST:
1055 warning_at (loc, 0, "conversion to void will not access "
1056 "object of type %qT", type);
1057 break;
1058 case ICV_SECOND_OF_COND:
1059 warning_at (loc, 0, "implicit dereference will not access "
1060 "object of type %qT in second operand of "
1061 "conditional expression", type);
1062 break;
1063 case ICV_THIRD_OF_COND:
1064 warning_at (loc, 0, "implicit dereference will not access "
1065 "object of type %qT in third operand of "
1066 "conditional expression", type);
1067 break;
1068 case ICV_RIGHT_OF_COMMA:
1069 warning_at (loc, 0, "implicit dereference will not access "
1070 "object of type %qT in right operand of "
1071 "comma operator", type);
1072 break;
1073 case ICV_LEFT_OF_COMMA:
1074 warning_at (loc, 0, "implicit dereference will not access "
1075 "object of type %qT in left operand of comma "
1076 "operator", type);
1077 break;
1078 case ICV_STATEMENT:
1079 warning_at (loc, 0, "implicit dereference will not access "
1080 "object of type %qT in statement", type);
1081 break;
1082 case ICV_THIRD_IN_FOR:
1083 warning_at (loc, 0, "implicit dereference will not access "
1084 "object of type %qT in for increment expression",
1085 type);
1086 break;
1087 default:
1088 gcc_unreachable ();
1091 else if (is_volatile && TREE_ADDRESSABLE (type))
1093 if (complain & tf_warning)
1094 switch (implicit)
1096 case ICV_CAST:
1097 warning_at (loc, 0, "conversion to void will not access "
1098 "object of non-trivially-copyable type %qT",
1099 type);
1100 break;
1101 case ICV_SECOND_OF_COND:
1102 warning_at (loc, 0, "indirection will not access object of "
1103 "non-trivially-copyable type %qT in second "
1104 "operand of conditional expression", type);
1105 break;
1106 case ICV_THIRD_OF_COND:
1107 warning_at (loc, 0, "indirection will not access object of "
1108 "non-trivially-copyable type %qT in third "
1109 "operand of conditional expression", type);
1110 break;
1111 case ICV_RIGHT_OF_COMMA:
1112 warning_at (loc, 0, "indirection will not access object of "
1113 "non-trivially-copyable type %qT in right "
1114 "operand of comma operator", type);
1115 break;
1116 case ICV_LEFT_OF_COMMA:
1117 warning_at (loc, 0, "indirection will not access object of "
1118 "non-trivially-copyable type %qT in left "
1119 "operand of comma operator", type);
1120 break;
1121 case ICV_STATEMENT:
1122 warning_at (loc, 0, "indirection will not access object of "
1123 "non-trivially-copyable type %qT in statement",
1124 type);
1125 break;
1126 case ICV_THIRD_IN_FOR:
1127 warning_at (loc, 0, "indirection will not access object of "
1128 "non-trivially-copyable type %qT in for "
1129 "increment expression", type);
1130 break;
1131 default:
1132 gcc_unreachable ();
1135 if (is_reference || !is_volatile || !is_complete || TREE_ADDRESSABLE (type))
1137 /* Emit a warning (if enabled) when the "effect-less" INDIRECT_REF
1138 operation is stripped off. Note that we don't warn about
1139 - an expression with TREE_NO_WARNING set. (For an example of
1140 such expressions, see build_over_call in call.c.)
1141 - automatic dereferencing of references, since the user cannot
1142 control it. (See also warn_if_unused_value() in c-common.c.) */
1143 if (warn_unused_value
1144 && implicit != ICV_CAST
1145 && (complain & tf_warning)
1146 && !TREE_NO_WARNING (expr)
1147 && !is_reference)
1148 warning_at (loc, OPT_Wunused_value, "value computed is not used");
1149 expr = TREE_OPERAND (expr, 0);
1152 break;
1155 case VAR_DECL:
1157 /* External variables might be incomplete. */
1158 tree type = TREE_TYPE (expr);
1159 int is_complete = COMPLETE_TYPE_P (complete_type (type));
1161 if (TYPE_VOLATILE (type) && !is_complete && (complain & tf_warning))
1162 switch (implicit)
1164 case ICV_CAST:
1165 warning_at (loc, 0, "conversion to void will not access "
1166 "object %qE of incomplete type %qT", expr, type);
1167 break;
1168 case ICV_SECOND_OF_COND:
1169 warning_at (loc, 0, "variable %qE of incomplete type %qT will "
1170 "not be accessed in second operand of "
1171 "conditional expression", expr, type);
1172 break;
1173 case ICV_THIRD_OF_COND:
1174 warning_at (loc, 0, "variable %qE of incomplete type %qT will "
1175 "not be accessed in third operand of "
1176 "conditional expression", expr, type);
1177 break;
1178 case ICV_RIGHT_OF_COMMA:
1179 warning_at (loc, 0, "variable %qE of incomplete type %qT will "
1180 "not be accessed in right operand of comma operator",
1181 expr, type);
1182 break;
1183 case ICV_LEFT_OF_COMMA:
1184 warning_at (loc, 0, "variable %qE of incomplete type %qT will "
1185 "not be accessed in left operand of comma operator",
1186 expr, type);
1187 break;
1188 case ICV_STATEMENT:
1189 warning_at (loc, 0, "variable %qE of incomplete type %qT will "
1190 "not be accessed in statement", expr, type);
1191 break;
1192 case ICV_THIRD_IN_FOR:
1193 warning_at (loc, 0, "variable %qE of incomplete type %qT will "
1194 "not be accessed in for increment expression",
1195 expr, type);
1196 break;
1197 default:
1198 gcc_unreachable ();
1201 break;
1204 case TARGET_EXPR:
1205 /* Don't bother with the temporary object returned from a function if
1206 we don't use it and don't need to destroy it. We'll still
1207 allocate space for it in expand_call or declare_return_variable,
1208 but we don't need to track it through all the tree phases. */
1209 if (TARGET_EXPR_IMPLICIT_P (expr)
1210 && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (expr)))
1212 tree init = TARGET_EXPR_INITIAL (expr);
1213 if (TREE_CODE (init) == AGGR_INIT_EXPR
1214 && !AGGR_INIT_VIA_CTOR_P (init))
1216 tree fn = AGGR_INIT_EXPR_FN (init);
1217 expr = build_call_array_loc (input_location,
1218 TREE_TYPE (TREE_TYPE (TREE_TYPE (fn))),
1220 aggr_init_expr_nargs (init),
1221 AGGR_INIT_EXPR_ARGP (init));
1224 break;
1226 default:;
1228 expr = resolve_nondeduced_context (expr);
1230 tree probe = expr;
1232 if (TREE_CODE (probe) == ADDR_EXPR)
1233 probe = TREE_OPERAND (expr, 0);
1234 if (type_unknown_p (probe))
1236 /* [over.over] enumerates the places where we can take the address
1237 of an overloaded function, and this is not one of them. */
1238 if (complain & tf_error)
1239 switch (implicit)
1241 case ICV_CAST:
1242 error_at (loc, "conversion to void "
1243 "cannot resolve address of overloaded function");
1244 break;
1245 case ICV_SECOND_OF_COND:
1246 error_at (loc, "second operand of conditional expression "
1247 "cannot resolve address of overloaded function");
1248 break;
1249 case ICV_THIRD_OF_COND:
1250 error_at (loc, "third operand of conditional expression "
1251 "cannot resolve address of overloaded function");
1252 break;
1253 case ICV_RIGHT_OF_COMMA:
1254 error_at (loc, "right operand of comma operator "
1255 "cannot resolve address of overloaded function");
1256 break;
1257 case ICV_LEFT_OF_COMMA:
1258 error_at (loc, "left operand of comma operator "
1259 "cannot resolve address of overloaded function");
1260 break;
1261 case ICV_STATEMENT:
1262 error_at (loc, "statement "
1263 "cannot resolve address of overloaded function");
1264 break;
1265 case ICV_THIRD_IN_FOR:
1266 error_at (loc, "for increment expression "
1267 "cannot resolve address of overloaded function");
1268 break;
1270 else
1271 return error_mark_node;
1272 expr = void_zero_node;
1274 else if (implicit != ICV_CAST && probe == expr && is_overloaded_fn (probe))
1276 /* Only warn when there is no &. */
1277 if (complain & tf_warning)
1278 switch (implicit)
1280 case ICV_SECOND_OF_COND:
1281 warning_at (loc, OPT_Waddress,
1282 "second operand of conditional expression "
1283 "is a reference, not call, to function %qE", expr);
1284 break;
1285 case ICV_THIRD_OF_COND:
1286 warning_at (loc, OPT_Waddress,
1287 "third operand of conditional expression "
1288 "is a reference, not call, to function %qE", expr);
1289 break;
1290 case ICV_RIGHT_OF_COMMA:
1291 warning_at (loc, OPT_Waddress,
1292 "right operand of comma operator "
1293 "is a reference, not call, to function %qE", expr);
1294 break;
1295 case ICV_LEFT_OF_COMMA:
1296 warning_at (loc, OPT_Waddress,
1297 "left operand of comma operator "
1298 "is a reference, not call, to function %qE", expr);
1299 break;
1300 case ICV_STATEMENT:
1301 warning_at (loc, OPT_Waddress,
1302 "statement is a reference, not call, to function %qE",
1303 expr);
1304 break;
1305 case ICV_THIRD_IN_FOR:
1306 warning_at (loc, OPT_Waddress,
1307 "for increment expression "
1308 "is a reference, not call, to function %qE", expr);
1309 break;
1310 default:
1311 gcc_unreachable ();
1314 if (TREE_CODE (expr) == COMPONENT_REF)
1315 expr = TREE_OPERAND (expr, 0);
1319 if (expr != error_mark_node && !VOID_TYPE_P (TREE_TYPE (expr)))
1321 if (implicit != ICV_CAST
1322 && warn_unused_value
1323 && !TREE_NO_WARNING (expr)
1324 && !processing_template_decl)
1326 /* The middle end does not warn about expressions that have
1327 been explicitly cast to void, so we must do so here. */
1328 if (!TREE_SIDE_EFFECTS (expr)) {
1329 if (complain & tf_warning)
1330 switch (implicit)
1332 case ICV_SECOND_OF_COND:
1333 warning_at (loc, OPT_Wunused_value,
1334 "second operand of conditional expression "
1335 "has no effect");
1336 break;
1337 case ICV_THIRD_OF_COND:
1338 warning_at (loc, OPT_Wunused_value,
1339 "third operand of conditional expression "
1340 "has no effect");
1341 break;
1342 case ICV_RIGHT_OF_COMMA:
1343 warning_at (loc, OPT_Wunused_value,
1344 "right operand of comma operator has no effect");
1345 break;
1346 case ICV_LEFT_OF_COMMA:
1347 warning_at (loc, OPT_Wunused_value,
1348 "left operand of comma operator has no effect");
1349 break;
1350 case ICV_STATEMENT:
1351 warning_at (loc, OPT_Wunused_value,
1352 "statement has no effect");
1353 break;
1354 case ICV_THIRD_IN_FOR:
1355 warning_at (loc, OPT_Wunused_value,
1356 "for increment expression has no effect");
1357 break;
1358 default:
1359 gcc_unreachable ();
1362 else
1364 tree e;
1365 enum tree_code code;
1366 enum tree_code_class tclass;
1368 e = expr;
1369 /* We might like to warn about (say) "(int) f()", as the
1370 cast has no effect, but the compiler itself will
1371 generate implicit conversions under some
1372 circumstances. (For example a block copy will be
1373 turned into a call to "__builtin_memcpy", with a
1374 conversion of the return value to an appropriate
1375 type.) So, to avoid false positives, we strip
1376 conversions. Do not use STRIP_NOPs because it will
1377 not strip conversions to "void", as that is not a
1378 mode-preserving conversion. */
1379 while (TREE_CODE (e) == NOP_EXPR)
1380 e = TREE_OPERAND (e, 0);
1382 code = TREE_CODE (e);
1383 tclass = TREE_CODE_CLASS (code);
1384 if ((tclass == tcc_comparison
1385 || tclass == tcc_unary
1386 || (tclass == tcc_binary
1387 && !(code == MODIFY_EXPR
1388 || code == INIT_EXPR
1389 || code == PREDECREMENT_EXPR
1390 || code == PREINCREMENT_EXPR
1391 || code == POSTDECREMENT_EXPR
1392 || code == POSTINCREMENT_EXPR)))
1393 && (complain & tf_warning))
1394 warning_at (loc, OPT_Wunused_value, "value computed is not used");
1397 expr = build1 (CONVERT_EXPR, void_type_node, expr);
1399 if (! TREE_SIDE_EFFECTS (expr))
1400 expr = void_zero_node;
1401 return expr;
1404 /* Create an expression whose value is that of EXPR,
1405 converted to type TYPE. The TREE_TYPE of the value
1406 is always TYPE. This function implements all reasonable
1407 conversions; callers should filter out those that are
1408 not permitted by the language being compiled.
1410 Most of this routine is from build_reinterpret_cast.
1412 The back end cannot call cp_convert (what was convert) because
1413 conversions to/from basetypes may involve memory references
1414 (vbases) and adding or subtracting small values (multiple
1415 inheritance), but it calls convert from the constant folding code
1416 on subtrees of already built trees after it has ripped them apart.
1418 Also, if we ever support range variables, we'll probably also have to
1419 do a little bit more work. */
1421 tree
1422 convert (tree type, tree expr)
1424 tree intype;
1426 if (type == error_mark_node || expr == error_mark_node)
1427 return error_mark_node;
1429 intype = TREE_TYPE (expr);
1431 if (POINTER_TYPE_P (type) && POINTER_TYPE_P (intype))
1432 return fold_if_not_in_template (build_nop (type, expr));
1434 return ocp_convert (type, expr, CONV_OLD_CONVERT,
1435 LOOKUP_NORMAL|LOOKUP_NO_CONVERSION,
1436 tf_warning_or_error);
1439 /* Like cp_convert, except permit conversions to take place which
1440 are not normally allowed due to access restrictions
1441 (such as conversion from sub-type to private super-type). */
1443 tree
1444 convert_force (tree type, tree expr, int convtype, tsubst_flags_t complain)
1446 tree e = expr;
1447 enum tree_code code = TREE_CODE (type);
1449 if (code == REFERENCE_TYPE)
1450 return (fold_if_not_in_template
1451 (convert_to_reference (type, e, CONV_C_CAST, 0,
1452 NULL_TREE, complain)));
1454 if (code == POINTER_TYPE)
1455 return fold_if_not_in_template (convert_to_pointer_force (type, e,
1456 complain));
1458 /* From typeck.c convert_for_assignment */
1459 if (((TREE_CODE (TREE_TYPE (e)) == POINTER_TYPE && TREE_CODE (e) == ADDR_EXPR
1460 && TREE_CODE (TREE_TYPE (e)) == POINTER_TYPE
1461 && TREE_CODE (TREE_TYPE (TREE_TYPE (e))) == METHOD_TYPE)
1462 || integer_zerop (e)
1463 || TYPE_PTRMEMFUNC_P (TREE_TYPE (e)))
1464 && TYPE_PTRMEMFUNC_P (type))
1465 /* compatible pointer to member functions. */
1466 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), e, 1,
1467 /*c_cast_p=*/1, complain);
1469 return ocp_convert (type, e, CONV_C_CAST|convtype, LOOKUP_NORMAL, complain);
1472 /* Convert an aggregate EXPR to type XTYPE. If a conversion
1473 exists, return the attempted conversion. This may
1474 return ERROR_MARK_NODE if the conversion is not
1475 allowed (references private members, etc).
1476 If no conversion exists, NULL_TREE is returned.
1478 FIXME: Ambiguity checking is wrong. Should choose one by the implicit
1479 object parameter, or by the second standard conversion sequence if
1480 that doesn't do it. This will probably wait for an overloading rewrite.
1481 (jason 8/9/95) */
1483 static tree
1484 build_type_conversion (tree xtype, tree expr)
1486 /* C++: check to see if we can convert this aggregate type
1487 into the required type. */
1488 return build_user_type_conversion (xtype, expr, LOOKUP_NORMAL,
1489 tf_warning_or_error);
1492 /* Convert the given EXPR to one of a group of types suitable for use in an
1493 expression. DESIRES is a combination of various WANT_* flags (q.v.)
1494 which indicates which types are suitable. If COMPLAIN is true, complain
1495 about ambiguity; otherwise, the caller will deal with it. */
1497 tree
1498 build_expr_type_conversion (int desires, tree expr, bool complain)
1500 tree basetype = TREE_TYPE (expr);
1501 tree conv = NULL_TREE;
1502 tree winner = NULL_TREE;
1504 if (expr == null_node
1505 && (desires & WANT_INT)
1506 && !(desires & WANT_NULL))
1508 source_location loc =
1509 expansion_point_location_if_in_system_header (input_location);
1511 warning_at (loc, OPT_Wconversion_null,
1512 "converting NULL to non-pointer type");
1515 if (basetype == error_mark_node)
1516 return error_mark_node;
1518 if (! MAYBE_CLASS_TYPE_P (basetype))
1519 switch (TREE_CODE (basetype))
1521 case INTEGER_TYPE:
1522 if ((desires & WANT_NULL) && null_ptr_cst_p (expr))
1523 return expr;
1524 /* else fall through... */
1526 case BOOLEAN_TYPE:
1527 return (desires & WANT_INT) ? expr : NULL_TREE;
1528 case ENUMERAL_TYPE:
1529 return (desires & WANT_ENUM) ? expr : NULL_TREE;
1530 case REAL_TYPE:
1531 return (desires & WANT_FLOAT) ? expr : NULL_TREE;
1532 case POINTER_TYPE:
1533 return (desires & WANT_POINTER) ? expr : NULL_TREE;
1535 case FUNCTION_TYPE:
1536 case ARRAY_TYPE:
1537 return (desires & WANT_POINTER) ? decay_conversion (expr,
1538 tf_warning_or_error)
1539 : NULL_TREE;
1541 case COMPLEX_TYPE:
1542 case VECTOR_TYPE:
1543 if ((desires & WANT_VECTOR_OR_COMPLEX) == 0)
1544 return NULL_TREE;
1545 switch (TREE_CODE (TREE_TYPE (basetype)))
1547 case INTEGER_TYPE:
1548 case BOOLEAN_TYPE:
1549 return (desires & WANT_INT) ? expr : NULL_TREE;
1550 case ENUMERAL_TYPE:
1551 return (desires & WANT_ENUM) ? expr : NULL_TREE;
1552 case REAL_TYPE:
1553 return (desires & WANT_FLOAT) ? expr : NULL_TREE;
1554 default:
1555 return NULL_TREE;
1558 default:
1559 return NULL_TREE;
1562 /* The code for conversions from class type is currently only used for
1563 delete expressions. Other expressions are handled by build_new_op. */
1564 if (!complete_type_or_maybe_complain (basetype, expr, complain))
1565 return error_mark_node;
1566 if (!TYPE_HAS_CONVERSION (basetype))
1567 return NULL_TREE;
1569 for (conv = lookup_conversions (basetype); conv; conv = TREE_CHAIN (conv))
1571 int win = 0;
1572 tree candidate;
1573 tree cand = TREE_VALUE (conv);
1574 cand = OVL_CURRENT (cand);
1576 if (winner && winner == cand)
1577 continue;
1579 if (DECL_NONCONVERTING_P (cand))
1580 continue;
1582 if (TREE_CODE (cand) == TEMPLATE_DECL)
1584 if (complain)
1586 error ("ambiguous default type conversion from %qT",
1587 basetype);
1588 error (" candidate conversions include %qD", cand);
1590 return error_mark_node;
1593 candidate = non_reference (TREE_TYPE (TREE_TYPE (cand)));
1595 switch (TREE_CODE (candidate))
1597 case BOOLEAN_TYPE:
1598 case INTEGER_TYPE:
1599 win = (desires & WANT_INT); break;
1600 case ENUMERAL_TYPE:
1601 win = (desires & WANT_ENUM); break;
1602 case REAL_TYPE:
1603 win = (desires & WANT_FLOAT); break;
1604 case POINTER_TYPE:
1605 win = (desires & WANT_POINTER); break;
1607 case COMPLEX_TYPE:
1608 case VECTOR_TYPE:
1609 if ((desires & WANT_VECTOR_OR_COMPLEX) == 0)
1610 break;
1611 switch (TREE_CODE (TREE_TYPE (candidate)))
1613 case BOOLEAN_TYPE:
1614 case INTEGER_TYPE:
1615 win = (desires & WANT_INT); break;
1616 case ENUMERAL_TYPE:
1617 win = (desires & WANT_ENUM); break;
1618 case REAL_TYPE:
1619 win = (desires & WANT_FLOAT); break;
1620 default:
1621 break;
1623 break;
1625 default:
1626 break;
1629 if (win)
1631 if (winner)
1633 if (complain)
1635 error ("ambiguous default type conversion from %qT",
1636 basetype);
1637 error (" candidate conversions include %qD and %qD",
1638 winner, cand);
1640 return error_mark_node;
1642 else
1643 winner = cand;
1647 if (winner)
1649 tree type = non_reference (TREE_TYPE (TREE_TYPE (winner)));
1650 return build_user_type_conversion (type, expr, LOOKUP_NORMAL,
1651 tf_warning_or_error);
1654 return NULL_TREE;
1657 /* Implements integral promotion (4.1) and float->double promotion. */
1659 tree
1660 type_promotes_to (tree type)
1662 tree promoted_type;
1664 if (type == error_mark_node)
1665 return error_mark_node;
1667 type = TYPE_MAIN_VARIANT (type);
1669 /* Check for promotions of target-defined types first. */
1670 promoted_type = targetm.promoted_type (type);
1671 if (promoted_type)
1672 return promoted_type;
1674 /* bool always promotes to int (not unsigned), even if it's the same
1675 size. */
1676 if (TREE_CODE (type) == BOOLEAN_TYPE)
1677 type = integer_type_node;
1679 /* Scoped enums don't promote, but pretend they do for backward ABI bug
1680 compatibility wrt varargs. */
1681 else if (SCOPED_ENUM_P (type) && abi_version_at_least (6))
1684 /* Normally convert enums to int, but convert wide enums to something
1685 wider. */
1686 else if (TREE_CODE (type) == ENUMERAL_TYPE
1687 || type == char16_type_node
1688 || type == char32_type_node
1689 || type == wchar_type_node)
1691 int precision = MAX (TYPE_PRECISION (type),
1692 TYPE_PRECISION (integer_type_node));
1693 tree totype = c_common_type_for_size (precision, 0);
1694 if (SCOPED_ENUM_P (type))
1695 warning (OPT_Wabi, "scoped enum %qT will not promote to an integral "
1696 "type in a future version of GCC", type);
1697 if (TREE_CODE (type) == ENUMERAL_TYPE)
1698 type = ENUM_UNDERLYING_TYPE (type);
1699 if (TYPE_UNSIGNED (type)
1700 && ! int_fits_type_p (TYPE_MAX_VALUE (type), totype))
1701 type = c_common_type_for_size (precision, 1);
1702 else
1703 type = totype;
1705 else if (c_promoting_integer_type_p (type))
1707 /* Retain unsignedness if really not getting bigger. */
1708 if (TYPE_UNSIGNED (type)
1709 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
1710 type = unsigned_type_node;
1711 else
1712 type = integer_type_node;
1714 else if (type == float_type_node)
1715 type = double_type_node;
1717 return type;
1720 /* The routines below this point are carefully written to conform to
1721 the standard. They use the same terminology, and follow the rules
1722 closely. Although they are used only in pt.c at the moment, they
1723 should presumably be used everywhere in the future. */
1725 /* Attempt to perform qualification conversions on EXPR to convert it
1726 to TYPE. Return the resulting expression, or error_mark_node if
1727 the conversion was impossible. */
1729 tree
1730 perform_qualification_conversions (tree type, tree expr)
1732 tree expr_type;
1734 expr_type = TREE_TYPE (expr);
1736 if (same_type_p (type, expr_type))
1737 return expr;
1738 else if (TYPE_PTR_P (type) && TYPE_PTR_P (expr_type)
1739 && comp_ptr_ttypes (TREE_TYPE (type), TREE_TYPE (expr_type)))
1740 return build_nop (type, expr);
1741 else if (TYPE_PTRMEM_P (type) && TYPE_PTRMEM_P (expr_type)
1742 && same_type_p (TYPE_PTRMEM_CLASS_TYPE (type),
1743 TYPE_PTRMEM_CLASS_TYPE (expr_type))
1744 && comp_ptr_ttypes (TYPE_PTRMEM_POINTED_TO_TYPE (type),
1745 TYPE_PTRMEM_POINTED_TO_TYPE (expr_type)))
1746 return build_nop (type, expr);
1747 else
1748 return error_mark_node;