2012-11-29 Marc Glisse <marc.glisse@inria.fr>
[official-gcc.git] / gcc / cp / cvt.c
blob4ba7642db202da74b84c2f24f0c03914618f33a4
1 /* Language-level data type conversion for GNU C++.
2 Copyright (C) 1987, 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010,
4 2011, 2012 Free Software Foundation, Inc.
5 Hacked by Michael Tiemann (tiemann@cygnus.com)
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3, or (at your option)
12 any later version.
14 GCC is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
24 /* This file contains the functions for converting C++ expressions
25 to different data types. The only entry point is `convert'.
26 Every language front end must have a `convert' function
27 but what kind of conversions it does will depend on the language. */
29 #include "config.h"
30 #include "system.h"
31 #include "coretypes.h"
32 #include "tm.h"
33 #include "tree.h"
34 #include "flags.h"
35 #include "cp-tree.h"
36 #include "intl.h"
37 #include "convert.h"
38 #include "decl.h"
39 #include "target.h"
41 static tree cp_convert_to_pointer (tree, tree, tsubst_flags_t);
42 static tree convert_to_pointer_force (tree, tree, tsubst_flags_t);
43 static tree build_type_conversion (tree, tree);
44 static tree build_up_reference (tree, tree, int, tree, tsubst_flags_t);
45 static void warn_ref_binding (location_t, tree, tree, tree);
47 /* Change of width--truncation and extension of integers or reals--
48 is represented with NOP_EXPR. Proper functioning of many things
49 assumes that no other conversions can be NOP_EXPRs.
51 Conversion between integer and pointer is represented with CONVERT_EXPR.
52 Converting integer to real uses FLOAT_EXPR
53 and real to integer uses FIX_TRUNC_EXPR.
55 Here is a list of all the functions that assume that widening and
56 narrowing is always done with a NOP_EXPR:
57 In convert.c, convert_to_integer.
58 In c-typeck.c, build_binary_op_nodefault (boolean ops),
59 and c_common_truthvalue_conversion.
60 In expr.c: expand_expr, for operands of a MULT_EXPR.
61 In fold-const.c: fold.
62 In tree.c: get_narrower and get_unwidened.
64 C++: in multiple-inheritance, converting between pointers may involve
65 adjusting them by a delta stored within the class definition. */
67 /* Subroutines of `convert'. */
69 /* if converting pointer to pointer
70 if dealing with classes, check for derived->base or vice versa
71 else if dealing with method pointers, delegate
72 else convert blindly
73 else if converting class, pass off to build_type_conversion
74 else try C-style pointer conversion. */
76 static tree
77 cp_convert_to_pointer (tree type, tree expr, tsubst_flags_t complain)
79 tree intype = TREE_TYPE (expr);
80 enum tree_code form;
81 tree rval;
82 location_t loc = EXPR_LOC_OR_HERE (expr);
84 if (intype == error_mark_node)
85 return error_mark_node;
87 if (MAYBE_CLASS_TYPE_P (intype))
89 intype = complete_type (intype);
90 if (!COMPLETE_TYPE_P (intype))
92 if (complain & tf_error)
93 error_at (loc, "can%'t convert from incomplete type %qT to %qT",
94 intype, type);
95 return error_mark_node;
98 rval = build_type_conversion (type, expr);
99 if (rval)
101 if ((complain & tf_error)
102 && rval == error_mark_node)
103 error_at (loc, "conversion of %qE from %qT to %qT is ambiguous",
104 expr, intype, type);
105 return rval;
109 /* Handle anachronistic conversions from (::*)() to cv void* or (*)(). */
110 if (TREE_CODE (type) == POINTER_TYPE
111 && (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
112 || VOID_TYPE_P (TREE_TYPE (type))))
114 if (TYPE_PTRMEMFUNC_P (intype)
115 || TREE_CODE (intype) == METHOD_TYPE)
116 return convert_member_func_to_ptr (type, expr, complain);
117 if (TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE)
118 return build_nop (type, expr);
119 intype = TREE_TYPE (expr);
122 if (expr == error_mark_node)
123 return error_mark_node;
125 form = TREE_CODE (intype);
127 if (POINTER_TYPE_P (intype))
129 intype = TYPE_MAIN_VARIANT (intype);
131 if (TYPE_MAIN_VARIANT (type) != intype
132 && TREE_CODE (type) == POINTER_TYPE
133 && TREE_CODE (TREE_TYPE (type)) == RECORD_TYPE
134 && MAYBE_CLASS_TYPE_P (TREE_TYPE (type))
135 && MAYBE_CLASS_TYPE_P (TREE_TYPE (intype))
136 && TREE_CODE (TREE_TYPE (intype)) == RECORD_TYPE)
138 enum tree_code code = PLUS_EXPR;
139 tree binfo;
140 tree intype_class;
141 tree type_class;
142 bool same_p;
144 intype_class = TREE_TYPE (intype);
145 type_class = TREE_TYPE (type);
147 same_p = same_type_p (TYPE_MAIN_VARIANT (intype_class),
148 TYPE_MAIN_VARIANT (type_class));
149 binfo = NULL_TREE;
150 /* Try derived to base conversion. */
151 if (!same_p)
152 binfo = lookup_base (intype_class, type_class, ba_check,
153 NULL, complain);
154 if (!same_p && !binfo)
156 /* Try base to derived conversion. */
157 binfo = lookup_base (type_class, intype_class, ba_check,
158 NULL, complain);
159 code = MINUS_EXPR;
161 if (binfo == error_mark_node)
162 return error_mark_node;
163 if (binfo || same_p)
165 if (binfo)
166 expr = build_base_path (code, expr, binfo, 0, complain);
167 /* Add any qualifier conversions. */
168 return build_nop (type, expr);
172 if (TYPE_PTRMEMFUNC_P (type))
174 if (complain & tf_error)
175 error_at (loc, "cannot convert %qE from type %qT to type %qT",
176 expr, intype, type);
177 return error_mark_node;
180 return build_nop (type, expr);
182 else if ((TYPE_PTRDATAMEM_P (type) && TYPE_PTRDATAMEM_P (intype))
183 || (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype)))
184 return convert_ptrmem (type, expr, /*allow_inverse_p=*/false,
185 /*c_cast_p=*/false, complain);
186 else if (TYPE_PTRMEMFUNC_P (intype))
188 if (!warn_pmf2ptr)
190 if (TREE_CODE (expr) == PTRMEM_CST)
191 return cp_convert_to_pointer (type, PTRMEM_CST_MEMBER (expr),
192 complain);
193 else if (TREE_CODE (expr) == OFFSET_REF)
195 tree object = TREE_OPERAND (expr, 0);
196 return get_member_function_from_ptrfunc (&object,
197 TREE_OPERAND (expr, 1),
198 complain);
201 error_at (loc, "cannot convert %qE from type %qT to type %qT",
202 expr, intype, type);
203 return error_mark_node;
206 if (null_ptr_cst_p (expr))
208 if ((complain & tf_warning)
209 && c_inhibit_evaluation_warnings == 0
210 && !NULLPTR_TYPE_P (TREE_TYPE (expr)))
211 warning_at (loc, OPT_Wzero_as_null_pointer_constant,
212 "zero as null pointer constant");
214 if (TYPE_PTRMEMFUNC_P (type))
215 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), expr, 0,
216 /*c_cast_p=*/false, complain);
218 if (TYPE_PTRDATAMEM_P (type))
220 /* A NULL pointer-to-member is represented by -1, not by
221 zero. */
222 expr = build_int_cst_type (type, -1);
224 else
225 expr = build_int_cst (type, 0);
227 return expr;
229 else if (TYPE_PTRMEM_P (type) && INTEGRAL_CODE_P (form))
231 if (complain & tf_error)
232 error_at (loc, "invalid conversion from %qT to %qT", intype, type);
233 return error_mark_node;
236 if (INTEGRAL_CODE_P (form))
238 if (TYPE_PRECISION (intype) == POINTER_SIZE)
239 return build1 (CONVERT_EXPR, type, expr);
240 expr = cp_convert (c_common_type_for_size (POINTER_SIZE, 0), expr,
241 complain);
242 /* Modes may be different but sizes should be the same. There
243 is supposed to be some integral type that is the same width
244 as a pointer. */
245 gcc_assert (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (expr)))
246 == GET_MODE_SIZE (TYPE_MODE (type)));
248 return convert_to_pointer (type, expr);
251 if (type_unknown_p (expr))
252 return instantiate_type (type, expr, complain);
254 if (complain & tf_error)
255 error_at (loc, "cannot convert %qE from type %qT to type %qT",
256 expr, intype, type);
257 return error_mark_node;
260 /* Like convert, except permit conversions to take place which
261 are not normally allowed due to access restrictions
262 (such as conversion from sub-type to private super-type). */
264 static tree
265 convert_to_pointer_force (tree type, tree expr, tsubst_flags_t complain)
267 tree intype = TREE_TYPE (expr);
268 enum tree_code form = TREE_CODE (intype);
270 if (form == POINTER_TYPE)
272 intype = TYPE_MAIN_VARIANT (intype);
274 if (TYPE_MAIN_VARIANT (type) != intype
275 && TREE_CODE (TREE_TYPE (type)) == RECORD_TYPE
276 && MAYBE_CLASS_TYPE_P (TREE_TYPE (type))
277 && MAYBE_CLASS_TYPE_P (TREE_TYPE (intype))
278 && TREE_CODE (TREE_TYPE (intype)) == RECORD_TYPE)
280 enum tree_code code = PLUS_EXPR;
281 tree binfo;
283 binfo = lookup_base (TREE_TYPE (intype), TREE_TYPE (type),
284 ba_unique, NULL, complain);
285 if (!binfo)
287 binfo = lookup_base (TREE_TYPE (type), TREE_TYPE (intype),
288 ba_unique, NULL, complain);
289 code = MINUS_EXPR;
291 if (binfo == error_mark_node)
292 return error_mark_node;
293 if (binfo)
295 expr = build_base_path (code, expr, binfo, 0, complain);
296 if (expr == error_mark_node)
297 return error_mark_node;
298 /* Add any qualifier conversions. */
299 if (!same_type_p (TREE_TYPE (TREE_TYPE (expr)),
300 TREE_TYPE (type)))
301 expr = build_nop (type, expr);
302 return expr;
307 return cp_convert_to_pointer (type, expr, complain);
310 /* We are passing something to a function which requires a reference.
311 The type we are interested in is in TYPE. The initial
312 value we have to begin with is in ARG.
314 FLAGS controls how we manage access checking.
315 DIRECT_BIND in FLAGS controls how any temporaries are generated.
316 If DIRECT_BIND is set, DECL is the reference we're binding to. */
318 static tree
319 build_up_reference (tree type, tree arg, int flags, tree decl,
320 tsubst_flags_t complain)
322 tree rval;
323 tree argtype = TREE_TYPE (arg);
324 tree target_type = TREE_TYPE (type);
326 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
328 if ((flags & DIRECT_BIND) && ! real_lvalue_p (arg))
330 /* Create a new temporary variable. We can't just use a TARGET_EXPR
331 here because it needs to live as long as DECL. */
332 tree targ = arg;
334 arg = make_temporary_var_for_ref_to_temp (decl, target_type);
336 /* Process the initializer for the declaration. */
337 DECL_INITIAL (arg) = targ;
338 cp_finish_decl (arg, targ, /*init_const_expr_p=*/false, NULL_TREE,
339 LOOKUP_ONLYCONVERTING|DIRECT_BIND);
341 else if (!(flags & DIRECT_BIND) && ! lvalue_p (arg))
342 return get_target_expr_sfinae (arg, complain);
344 /* If we had a way to wrap this up, and say, if we ever needed its
345 address, transform all occurrences of the register, into a memory
346 reference we could win better. */
347 rval = cp_build_addr_expr (arg, complain);
348 if (rval == error_mark_node)
349 return error_mark_node;
351 if ((flags & LOOKUP_PROTECT)
352 && TYPE_MAIN_VARIANT (argtype) != TYPE_MAIN_VARIANT (target_type)
353 && MAYBE_CLASS_TYPE_P (argtype)
354 && MAYBE_CLASS_TYPE_P (target_type))
356 /* We go through lookup_base for the access control. */
357 tree binfo = lookup_base (argtype, target_type, ba_check,
358 NULL, complain);
359 if (binfo == error_mark_node)
360 return error_mark_node;
361 if (binfo == NULL_TREE)
362 return error_not_base_type (target_type, argtype);
363 rval = build_base_path (PLUS_EXPR, rval, binfo, 1, complain);
365 else
366 rval
367 = convert_to_pointer_force (build_pointer_type (target_type),
368 rval, complain);
369 return build_nop (type, rval);
372 /* Subroutine of convert_to_reference. REFTYPE is the target reference type.
373 INTYPE is the original rvalue type and DECL is an optional _DECL node
374 for diagnostics.
376 [dcl.init.ref] says that if an rvalue is used to
377 initialize a reference, then the reference must be to a
378 non-volatile const type. */
380 static void
381 warn_ref_binding (location_t loc, tree reftype, tree intype, tree decl)
383 tree ttl = TREE_TYPE (reftype);
385 if (!CP_TYPE_CONST_NON_VOLATILE_P (ttl))
387 const char *msg;
389 if (CP_TYPE_VOLATILE_P (ttl) && decl)
390 msg = G_("initialization of volatile reference type %q#T from "
391 "rvalue of type %qT");
392 else if (CP_TYPE_VOLATILE_P (ttl))
393 msg = G_("conversion to volatile reference type %q#T "
394 "from rvalue of type %qT");
395 else if (decl)
396 msg = G_("initialization of non-const reference type %q#T from "
397 "rvalue of type %qT");
398 else
399 msg = G_("conversion to non-const reference type %q#T from "
400 "rvalue of type %qT");
402 permerror (loc, msg, reftype, intype);
406 /* For C++: Only need to do one-level references, but cannot
407 get tripped up on signed/unsigned differences.
409 DECL is either NULL_TREE or the _DECL node for a reference that is being
410 initialized. It can be error_mark_node if we don't know the _DECL but
411 we know it's an initialization. */
413 tree
414 convert_to_reference (tree reftype, tree expr, int convtype,
415 int flags, tree decl, tsubst_flags_t complain)
417 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (reftype));
418 tree intype;
419 tree rval = NULL_TREE;
420 tree rval_as_conversion = NULL_TREE;
421 bool can_convert_intype_to_type;
422 location_t loc = EXPR_LOC_OR_HERE (expr);
424 if (TREE_CODE (type) == FUNCTION_TYPE
425 && TREE_TYPE (expr) == unknown_type_node)
426 expr = instantiate_type (type, expr, complain);
428 if (expr == error_mark_node)
429 return error_mark_node;
431 intype = TREE_TYPE (expr);
433 gcc_assert (TREE_CODE (intype) != REFERENCE_TYPE);
434 gcc_assert (TREE_CODE (reftype) == REFERENCE_TYPE);
436 intype = TYPE_MAIN_VARIANT (intype);
438 can_convert_intype_to_type = can_convert (type, intype, complain);
440 if (!can_convert_intype_to_type
441 && (convtype & CONV_IMPLICIT) && MAYBE_CLASS_TYPE_P (intype)
442 && ! (flags & LOOKUP_NO_CONVERSION))
444 /* Look for a user-defined conversion to lvalue that we can use. */
446 rval_as_conversion
447 = build_type_conversion (reftype, expr);
449 if (rval_as_conversion && rval_as_conversion != error_mark_node
450 && real_lvalue_p (rval_as_conversion))
452 expr = rval_as_conversion;
453 rval_as_conversion = NULL_TREE;
454 intype = type;
455 can_convert_intype_to_type = 1;
459 if (((convtype & CONV_STATIC) && can_convert (intype, type, complain))
460 || ((convtype & CONV_IMPLICIT) && can_convert_intype_to_type))
463 tree ttl = TREE_TYPE (reftype);
464 tree ttr = lvalue_type (expr);
466 if ((complain & tf_warning)
467 && ! real_lvalue_p (expr))
468 warn_ref_binding (loc, reftype, intype, decl);
470 if (! (convtype & CONV_CONST)
471 && !at_least_as_qualified_p (ttl, ttr))
473 if (complain & tf_error)
474 permerror (loc, "conversion from %qT to %qT discards qualifiers",
475 ttr, reftype);
476 else
477 return error_mark_node;
481 return build_up_reference (reftype, expr, flags, decl, complain);
483 else if ((convtype & CONV_REINTERPRET) && lvalue_p (expr))
485 /* When casting an lvalue to a reference type, just convert into
486 a pointer to the new type and deference it. This is allowed
487 by San Diego WP section 5.2.9 paragraph 12, though perhaps it
488 should be done directly (jason). (int &)ri ---> *(int*)&ri */
490 /* B* bp; A& ar = (A&)bp; is valid, but it's probably not what they
491 meant. */
492 if ((complain & tf_warning)
493 && TREE_CODE (intype) == POINTER_TYPE
494 && (comptypes (TREE_TYPE (intype), type,
495 COMPARE_BASE | COMPARE_DERIVED)))
496 warning_at (loc, 0, "casting %qT to %qT does not dereference pointer",
497 intype, reftype);
499 rval = cp_build_addr_expr (expr, complain);
500 if (rval != error_mark_node)
501 rval = convert_force (build_pointer_type (TREE_TYPE (reftype)),
502 rval, 0, complain);
503 if (rval != error_mark_node)
504 rval = build1 (NOP_EXPR, reftype, rval);
506 else
508 rval = convert_for_initialization (NULL_TREE, type, expr, flags,
509 ICR_CONVERTING, 0, 0, complain);
510 if (rval == NULL_TREE || rval == error_mark_node)
511 return rval;
512 if (complain & tf_warning)
513 warn_ref_binding (loc, reftype, intype, decl);
514 rval = build_up_reference (reftype, rval, flags, decl, complain);
517 if (rval)
519 /* If we found a way to convert earlier, then use it. */
520 return rval;
523 if (complain & tf_error)
524 error_at (loc, "cannot convert type %qT to type %qT", intype, reftype);
526 return error_mark_node;
529 /* We are using a reference VAL for its value. Bash that reference all the
530 way down to its lowest form. */
532 tree
533 convert_from_reference (tree val)
535 if (TREE_TYPE (val)
536 && TREE_CODE (TREE_TYPE (val)) == REFERENCE_TYPE)
538 tree t = TREE_TYPE (TREE_TYPE (val));
539 tree ref = build1 (INDIRECT_REF, t, val);
541 mark_exp_read (val);
542 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
543 so that we get the proper error message if the result is used
544 to assign to. Also, &* is supposed to be a no-op. */
545 TREE_READONLY (ref) = CP_TYPE_CONST_P (t);
546 TREE_THIS_VOLATILE (ref) = CP_TYPE_VOLATILE_P (t);
547 TREE_SIDE_EFFECTS (ref)
548 = (TREE_THIS_VOLATILE (ref) || TREE_SIDE_EFFECTS (val));
549 val = ref;
552 return val;
555 /* Really perform an lvalue-to-rvalue conversion, including copying an
556 argument of class type into a temporary. */
558 tree
559 force_rvalue (tree expr, tsubst_flags_t complain)
561 tree type = TREE_TYPE (expr);
562 if (MAYBE_CLASS_TYPE_P (type) && TREE_CODE (expr) != TARGET_EXPR)
564 vec<tree, va_gc> *args = make_tree_vector_single (expr);
565 expr = build_special_member_call (NULL_TREE, complete_ctor_identifier,
566 &args, type, LOOKUP_NORMAL, complain);
567 release_tree_vector (args);
568 expr = build_cplus_new (type, expr, complain);
570 else
571 expr = decay_conversion (expr, complain);
573 return expr;
577 /* If EXPR and ORIG are INTEGER_CSTs, return a version of EXPR that has
578 TREE_OVERFLOW set only if it is set in ORIG. Otherwise, return EXPR
579 unchanged. */
581 static tree
582 ignore_overflows (tree expr, tree orig)
584 if (TREE_CODE (expr) == INTEGER_CST
585 && TREE_CODE (orig) == INTEGER_CST
586 && TREE_OVERFLOW (expr) != TREE_OVERFLOW (orig))
588 gcc_assert (!TREE_OVERFLOW (orig));
589 /* Ensure constant sharing. */
590 expr = build_int_cst_wide (TREE_TYPE (expr),
591 TREE_INT_CST_LOW (expr),
592 TREE_INT_CST_HIGH (expr));
594 return expr;
597 /* Fold away simple conversions, but make sure TREE_OVERFLOW is set
598 properly. */
600 tree
601 cp_fold_convert (tree type, tree expr)
603 tree conv = fold_convert (type, expr);
604 conv = ignore_overflows (conv, expr);
605 return conv;
608 /* C++ conversions, preference to static cast conversions. */
610 tree
611 cp_convert (tree type, tree expr, tsubst_flags_t complain)
613 return ocp_convert (type, expr, CONV_OLD_CONVERT, LOOKUP_NORMAL, complain);
616 /* C++ equivalent of convert_and_check but using cp_convert as the
617 conversion function.
619 Convert EXPR to TYPE, warning about conversion problems with constants.
620 Invoke this function on every expression that is converted implicitly,
621 i.e. because of language rules and not because of an explicit cast. */
623 tree
624 cp_convert_and_check (tree type, tree expr, tsubst_flags_t complain)
626 tree result;
628 if (TREE_TYPE (expr) == type)
629 return expr;
631 result = cp_convert (type, expr, complain);
633 if ((complain & tf_warning)
634 && c_inhibit_evaluation_warnings == 0
635 && !TREE_OVERFLOW_P (expr)
636 && result != error_mark_node)
637 warnings_for_convert_and_check (type, expr, result);
639 return result;
642 /* Conversion...
644 FLAGS indicates how we should behave. */
646 tree
647 ocp_convert (tree type, tree expr, int convtype, int flags,
648 tsubst_flags_t complain)
650 tree e = expr;
651 enum tree_code code = TREE_CODE (type);
652 const char *invalid_conv_diag;
653 tree e1;
654 location_t loc = EXPR_LOC_OR_HERE (expr);
656 if (error_operand_p (e) || type == error_mark_node)
657 return error_mark_node;
659 complete_type (type);
660 complete_type (TREE_TYPE (expr));
662 if ((invalid_conv_diag
663 = targetm.invalid_conversion (TREE_TYPE (expr), type)))
665 if (complain & tf_error)
666 error (invalid_conv_diag);
667 return error_mark_node;
670 /* FIXME remove when moving to c_fully_fold model. */
671 /* FIXME do we still need this test? */
672 if (!CLASS_TYPE_P (type))
673 e = integral_constant_value (e);
674 if (error_operand_p (e))
675 return error_mark_node;
677 if (MAYBE_CLASS_TYPE_P (type) && (convtype & CONV_FORCE_TEMP))
678 /* We need a new temporary; don't take this shortcut. */;
679 else if (same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (e)))
681 if (same_type_p (type, TREE_TYPE (e)))
682 /* The call to fold will not always remove the NOP_EXPR as
683 might be expected, since if one of the types is a typedef;
684 the comparison in fold is just equality of pointers, not a
685 call to comptypes. We don't call fold in this case because
686 that can result in infinite recursion; fold will call
687 convert, which will call ocp_convert, etc. */
688 return e;
689 /* For complex data types, we need to perform componentwise
690 conversion. */
691 else if (TREE_CODE (type) == COMPLEX_TYPE)
692 return fold_if_not_in_template (convert_to_complex (type, e));
693 else if (TREE_CODE (type) == VECTOR_TYPE)
694 return fold_if_not_in_template (convert_to_vector (type, e));
695 else if (TREE_CODE (e) == TARGET_EXPR)
697 /* Don't build a NOP_EXPR of class type. Instead, change the
698 type of the temporary. */
699 TREE_TYPE (e) = TREE_TYPE (TARGET_EXPR_SLOT (e)) = type;
700 return e;
702 else
704 /* We shouldn't be treating objects of ADDRESSABLE type as
705 rvalues. */
706 gcc_assert (!TREE_ADDRESSABLE (type));
707 return fold_if_not_in_template (build_nop (type, e));
711 e1 = targetm.convert_to_type (type, e);
712 if (e1)
713 return e1;
715 if (code == VOID_TYPE && (convtype & CONV_STATIC))
717 e = convert_to_void (e, ICV_CAST, complain);
718 return e;
721 if (INTEGRAL_CODE_P (code))
723 tree intype = TREE_TYPE (e);
724 tree converted;
726 if (TREE_CODE (type) == ENUMERAL_TYPE)
728 /* enum = enum, enum = int, enum = float, (enum)pointer are all
729 errors. */
730 if (((INTEGRAL_OR_ENUMERATION_TYPE_P (intype)
731 || TREE_CODE (intype) == REAL_TYPE)
732 && ! (convtype & CONV_STATIC))
733 || TREE_CODE (intype) == POINTER_TYPE)
735 if (complain & tf_error)
736 permerror (loc, "conversion from %q#T to %q#T", intype, type);
737 else
738 return error_mark_node;
741 /* [expr.static.cast]
743 8. A value of integral or enumeration type can be explicitly
744 converted to an enumeration type. The value is unchanged if
745 the original value is within the range of the enumeration
746 values. Otherwise, the resulting enumeration value is
747 unspecified. */
748 if ((complain & tf_warning)
749 && TREE_CODE (e) == INTEGER_CST
750 && !int_fits_type_p (e, ENUM_UNDERLYING_TYPE (type)))
751 warning_at (loc, OPT_Wconversion,
752 "the result of the conversion is unspecified because "
753 "%qE is outside the range of type %qT",
754 expr, type);
756 if (MAYBE_CLASS_TYPE_P (intype))
758 tree rval;
759 rval = build_type_conversion (type, e);
760 if (rval)
761 return rval;
762 if (complain & tf_error)
763 error_at (loc, "%q#T used where a %qT was expected", intype, type);
764 return error_mark_node;
766 if (code == BOOLEAN_TYPE)
768 if (TREE_CODE (intype) == VOID_TYPE)
770 if (complain & tf_error)
771 error_at (loc,
772 "could not convert %qE from %<void%> to %<bool%>",
773 expr);
774 return error_mark_node;
777 /* We can't implicitly convert a scoped enum to bool, so convert
778 to the underlying type first. */
779 if (SCOPED_ENUM_P (intype) && (convtype & CONV_STATIC))
780 e = build_nop (ENUM_UNDERLYING_TYPE (intype), e);
781 return cp_truthvalue_conversion (e);
784 converted = fold_if_not_in_template (convert_to_integer (type, e));
786 /* Ignore any integer overflow caused by the conversion. */
787 return ignore_overflows (converted, e);
789 if (NULLPTR_TYPE_P (type) && e && null_ptr_cst_p (e))
790 return nullptr_node;
791 if (POINTER_TYPE_P (type) || TYPE_PTRMEM_P (type))
792 return fold_if_not_in_template (cp_convert_to_pointer (type, e, complain));
793 if (code == VECTOR_TYPE)
795 tree in_vtype = TREE_TYPE (e);
796 if (MAYBE_CLASS_TYPE_P (in_vtype))
798 tree ret_val;
799 ret_val = build_type_conversion (type, e);
800 if (ret_val)
801 return ret_val;
802 if (complain & tf_error)
803 error_at (loc, "%q#T used where a %qT was expected",
804 in_vtype, type);
805 return error_mark_node;
807 return fold_if_not_in_template (convert_to_vector (type, e));
809 if (code == REAL_TYPE || code == COMPLEX_TYPE)
811 if (MAYBE_CLASS_TYPE_P (TREE_TYPE (e)))
813 tree rval;
814 rval = build_type_conversion (type, e);
815 if (rval)
816 return rval;
817 else if (complain & tf_error)
818 error_at (loc,
819 "%q#T used where a floating point value was expected",
820 TREE_TYPE (e));
822 if (code == REAL_TYPE)
823 return fold_if_not_in_template (convert_to_real (type, e));
824 else if (code == COMPLEX_TYPE)
825 return fold_if_not_in_template (convert_to_complex (type, e));
828 /* New C++ semantics: since assignment is now based on
829 memberwise copying, if the rhs type is derived from the
830 lhs type, then we may still do a conversion. */
831 if (RECORD_OR_UNION_CODE_P (code))
833 tree dtype = TREE_TYPE (e);
834 tree ctor = NULL_TREE;
836 dtype = TYPE_MAIN_VARIANT (dtype);
838 /* Conversion between aggregate types. New C++ semantics allow
839 objects of derived type to be cast to objects of base type.
840 Old semantics only allowed this between pointers.
842 There may be some ambiguity between using a constructor
843 vs. using a type conversion operator when both apply. */
845 ctor = e;
847 if (abstract_virtuals_error_sfinae (NULL_TREE, type, complain))
848 return error_mark_node;
850 if (BRACE_ENCLOSED_INITIALIZER_P (ctor))
851 ctor = perform_implicit_conversion (type, ctor, complain);
852 else if ((flags & LOOKUP_ONLYCONVERTING)
853 && ! (CLASS_TYPE_P (dtype) && DERIVED_FROM_P (type, dtype)))
854 /* For copy-initialization, first we create a temp of the proper type
855 with a user-defined conversion sequence, then we direct-initialize
856 the target with the temp (see [dcl.init]). */
857 ctor = build_user_type_conversion (type, ctor, flags, complain);
858 else
860 vec<tree, va_gc> *ctor_vec = make_tree_vector_single (ctor);
861 ctor = build_special_member_call (NULL_TREE,
862 complete_ctor_identifier,
863 &ctor_vec,
864 type, flags, complain);
865 release_tree_vector (ctor_vec);
867 if (ctor)
868 return build_cplus_new (type, ctor, complain);
871 if (complain & tf_error)
873 /* If the conversion failed and expr was an invalid use of pointer to
874 member function, try to report a meaningful error. */
875 if (invalid_nonstatic_memfn_p (expr, complain))
876 /* We displayed the error message. */;
877 else
878 error_at (loc, "conversion from %qT to non-scalar type %qT requested",
879 TREE_TYPE (expr), type);
881 return error_mark_node;
884 /* When an expression is used in a void context, its value is discarded and
885 no lvalue-rvalue and similar conversions happen [expr.static.cast/4,
886 stmt.expr/1, expr.comma/1]. This permits dereferencing an incomplete type
887 in a void context. The C++ standard does not define what an `access' to an
888 object is, but there is reason to believe that it is the lvalue to rvalue
889 conversion -- if it were not, `*&*p = 1' would violate [expr]/4 in that it
890 accesses `*p' not to calculate the value to be stored. But, dcl.type.cv/8
891 indicates that volatile semantics should be the same between C and C++
892 where ever possible. C leaves it implementation defined as to what
893 constitutes an access to a volatile. So, we interpret `*vp' as a read of
894 the volatile object `vp' points to, unless that is an incomplete type. For
895 volatile references we do not do this interpretation, because that would
896 make it impossible to ignore the reference return value from functions. We
897 issue warnings in the confusing cases.
899 The IMPLICIT is ICV_CAST when the user is explicitly converting an expression
900 to void via a cast. If an expression is being implicitly converted, IMPLICIT
901 indicates the context of the implicit conversion. */
903 tree
904 convert_to_void (tree expr, impl_conv_void implicit, tsubst_flags_t complain)
906 location_t loc = EXPR_LOC_OR_HERE (expr);
908 if (expr == error_mark_node
909 || TREE_TYPE (expr) == error_mark_node)
910 return error_mark_node;
912 if (implicit == ICV_CAST)
913 mark_exp_read (expr);
914 else
916 tree exprv = expr;
918 while (TREE_CODE (exprv) == COMPOUND_EXPR)
919 exprv = TREE_OPERAND (exprv, 1);
920 if (DECL_P (exprv)
921 || handled_component_p (exprv)
922 || TREE_CODE (exprv) == INDIRECT_REF)
923 /* Expr is not being 'used' here, otherwise we whould have
924 called mark_{rl}value_use use here, which would have in turn
925 called mark_exp_read. Rather, we call mark_exp_read directly
926 to avoid some warnings when
927 -Wunused-but-set-{variable,parameter} is in effect. */
928 mark_exp_read (exprv);
931 if (!TREE_TYPE (expr))
932 return expr;
933 if (invalid_nonstatic_memfn_p (expr, complain))
934 return error_mark_node;
935 if (TREE_CODE (expr) == PSEUDO_DTOR_EXPR)
937 if (complain & tf_error)
938 error_at (loc, "pseudo-destructor is not called");
939 return error_mark_node;
941 if (VOID_TYPE_P (TREE_TYPE (expr)))
942 return expr;
943 switch (TREE_CODE (expr))
945 case COND_EXPR:
947 /* The two parts of a cond expr might be separate lvalues. */
948 tree op1 = TREE_OPERAND (expr,1);
949 tree op2 = TREE_OPERAND (expr,2);
950 bool side_effects = ((op1 && TREE_SIDE_EFFECTS (op1))
951 || TREE_SIDE_EFFECTS (op2));
952 tree new_op1, new_op2;
953 new_op1 = NULL_TREE;
954 if (implicit != ICV_CAST && !side_effects)
956 if (op1)
957 new_op1 = convert_to_void (op1, ICV_SECOND_OF_COND, complain);
958 new_op2 = convert_to_void (op2, ICV_THIRD_OF_COND, complain);
960 else
962 if (op1)
963 new_op1 = convert_to_void (op1, ICV_CAST, complain);
964 new_op2 = convert_to_void (op2, ICV_CAST, complain);
967 expr = build3 (COND_EXPR, TREE_TYPE (new_op2),
968 TREE_OPERAND (expr, 0), new_op1, new_op2);
969 break;
972 case COMPOUND_EXPR:
974 /* The second part of a compound expr contains the value. */
975 tree op1 = TREE_OPERAND (expr,1);
976 tree new_op1;
977 if (implicit != ICV_CAST && !TREE_NO_WARNING (expr))
978 new_op1 = convert_to_void (op1, ICV_RIGHT_OF_COMMA, complain);
979 else
980 new_op1 = convert_to_void (op1, ICV_CAST, complain);
982 if (new_op1 != op1)
984 tree t = build2 (COMPOUND_EXPR, TREE_TYPE (new_op1),
985 TREE_OPERAND (expr, 0), new_op1);
986 expr = t;
989 break;
992 case NON_LVALUE_EXPR:
993 case NOP_EXPR:
994 /* These have already decayed to rvalue. */
995 break;
997 case CALL_EXPR: /* We have a special meaning for volatile void fn(). */
998 break;
1000 case INDIRECT_REF:
1002 tree type = TREE_TYPE (expr);
1003 int is_reference = TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0)))
1004 == REFERENCE_TYPE;
1005 int is_volatile = TYPE_VOLATILE (type);
1006 int is_complete = COMPLETE_TYPE_P (complete_type (type));
1008 /* Can't load the value if we don't know the type. */
1009 if (is_volatile && !is_complete)
1011 if (complain & tf_warning)
1012 switch (implicit)
1014 case ICV_CAST:
1015 warning_at (loc, 0, "conversion to void will not access "
1016 "object of incomplete type %qT", type);
1017 break;
1018 case ICV_SECOND_OF_COND:
1019 warning_at (loc, 0, "indirection will not access object of "
1020 "incomplete type %qT in second operand "
1021 "of conditional expression", type);
1022 break;
1023 case ICV_THIRD_OF_COND:
1024 warning_at (loc, 0, "indirection will not access object of "
1025 "incomplete type %qT in third operand "
1026 "of conditional expression", type);
1027 break;
1028 case ICV_RIGHT_OF_COMMA:
1029 warning_at (loc, 0, "indirection will not access object of "
1030 "incomplete type %qT in right operand of "
1031 "comma operator", type);
1032 break;
1033 case ICV_LEFT_OF_COMMA:
1034 warning_at (loc, 0, "indirection will not access object of "
1035 "incomplete type %qT in left operand of "
1036 "comma operator", type);
1037 break;
1038 case ICV_STATEMENT:
1039 warning_at (loc, 0, "indirection will not access object of "
1040 "incomplete type %qT in statement", type);
1041 break;
1042 case ICV_THIRD_IN_FOR:
1043 warning_at (loc, 0, "indirection will not access object of "
1044 "incomplete type %qT in for increment "
1045 "expression", type);
1046 break;
1047 default:
1048 gcc_unreachable ();
1051 /* Don't load the value if this is an implicit dereference, or if
1052 the type needs to be handled by ctors/dtors. */
1053 else if (is_volatile && is_reference)
1055 if (complain & tf_warning)
1056 switch (implicit)
1058 case ICV_CAST:
1059 warning_at (loc, 0, "conversion to void will not access "
1060 "object of type %qT", type);
1061 break;
1062 case ICV_SECOND_OF_COND:
1063 warning_at (loc, 0, "implicit dereference will not access "
1064 "object of type %qT in second operand of "
1065 "conditional expression", type);
1066 break;
1067 case ICV_THIRD_OF_COND:
1068 warning_at (loc, 0, "implicit dereference will not access "
1069 "object of type %qT in third operand of "
1070 "conditional expression", type);
1071 break;
1072 case ICV_RIGHT_OF_COMMA:
1073 warning_at (loc, 0, "implicit dereference will not access "
1074 "object of type %qT in right operand of "
1075 "comma operator", type);
1076 break;
1077 case ICV_LEFT_OF_COMMA:
1078 warning_at (loc, 0, "implicit dereference will not access "
1079 "object of type %qT in left operand of comma "
1080 "operator", type);
1081 break;
1082 case ICV_STATEMENT:
1083 warning_at (loc, 0, "implicit dereference will not access "
1084 "object of type %qT in statement", type);
1085 break;
1086 case ICV_THIRD_IN_FOR:
1087 warning_at (loc, 0, "implicit dereference will not access "
1088 "object of type %qT in for increment expression",
1089 type);
1090 break;
1091 default:
1092 gcc_unreachable ();
1095 else if (is_volatile && TREE_ADDRESSABLE (type))
1097 if (complain & tf_warning)
1098 switch (implicit)
1100 case ICV_CAST:
1101 warning_at (loc, 0, "conversion to void will not access "
1102 "object of non-trivially-copyable type %qT",
1103 type);
1104 break;
1105 case ICV_SECOND_OF_COND:
1106 warning_at (loc, 0, "indirection will not access object of "
1107 "non-trivially-copyable type %qT in second "
1108 "operand of conditional expression", type);
1109 break;
1110 case ICV_THIRD_OF_COND:
1111 warning_at (loc, 0, "indirection will not access object of "
1112 "non-trivially-copyable type %qT in third "
1113 "operand of conditional expression", type);
1114 break;
1115 case ICV_RIGHT_OF_COMMA:
1116 warning_at (loc, 0, "indirection will not access object of "
1117 "non-trivially-copyable type %qT in right "
1118 "operand of comma operator", type);
1119 break;
1120 case ICV_LEFT_OF_COMMA:
1121 warning_at (loc, 0, "indirection will not access object of "
1122 "non-trivially-copyable type %qT in left "
1123 "operand of comma operator", type);
1124 break;
1125 case ICV_STATEMENT:
1126 warning_at (loc, 0, "indirection will not access object of "
1127 "non-trivially-copyable type %qT in statement",
1128 type);
1129 break;
1130 case ICV_THIRD_IN_FOR:
1131 warning_at (loc, 0, "indirection will not access object of "
1132 "non-trivially-copyable type %qT in for "
1133 "increment expression", type);
1134 break;
1135 default:
1136 gcc_unreachable ();
1139 if (is_reference || !is_volatile || !is_complete || TREE_ADDRESSABLE (type))
1141 /* Emit a warning (if enabled) when the "effect-less" INDIRECT_REF
1142 operation is stripped off. Note that we don't warn about
1143 - an expression with TREE_NO_WARNING set. (For an example of
1144 such expressions, see build_over_call in call.c.)
1145 - automatic dereferencing of references, since the user cannot
1146 control it. (See also warn_if_unused_value() in c-common.c.) */
1147 if (warn_unused_value
1148 && implicit != ICV_CAST
1149 && (complain & tf_warning)
1150 && !TREE_NO_WARNING (expr)
1151 && !is_reference)
1152 warning_at (loc, OPT_Wunused_value, "value computed is not used");
1153 expr = TREE_OPERAND (expr, 0);
1156 break;
1159 case VAR_DECL:
1161 /* External variables might be incomplete. */
1162 tree type = TREE_TYPE (expr);
1163 int is_complete = COMPLETE_TYPE_P (complete_type (type));
1165 if (TYPE_VOLATILE (type) && !is_complete && (complain & tf_warning))
1166 switch (implicit)
1168 case ICV_CAST:
1169 warning_at (loc, 0, "conversion to void will not access "
1170 "object %qE of incomplete type %qT", expr, type);
1171 break;
1172 case ICV_SECOND_OF_COND:
1173 warning_at (loc, 0, "variable %qE of incomplete type %qT will "
1174 "not be accessed in second operand of "
1175 "conditional expression", expr, type);
1176 break;
1177 case ICV_THIRD_OF_COND:
1178 warning_at (loc, 0, "variable %qE of incomplete type %qT will "
1179 "not be accessed in third operand of "
1180 "conditional expression", expr, type);
1181 break;
1182 case ICV_RIGHT_OF_COMMA:
1183 warning_at (loc, 0, "variable %qE of incomplete type %qT will "
1184 "not be accessed in right operand of comma operator",
1185 expr, type);
1186 break;
1187 case ICV_LEFT_OF_COMMA:
1188 warning_at (loc, 0, "variable %qE of incomplete type %qT will "
1189 "not be accessed in left operand of comma operator",
1190 expr, type);
1191 break;
1192 case ICV_STATEMENT:
1193 warning_at (loc, 0, "variable %qE of incomplete type %qT will "
1194 "not be accessed in statement", expr, type);
1195 break;
1196 case ICV_THIRD_IN_FOR:
1197 warning_at (loc, 0, "variable %qE of incomplete type %qT will "
1198 "not be accessed in for increment expression",
1199 expr, type);
1200 break;
1201 default:
1202 gcc_unreachable ();
1205 break;
1208 case TARGET_EXPR:
1209 /* Don't bother with the temporary object returned from a function if
1210 we don't use it and don't need to destroy it. We'll still
1211 allocate space for it in expand_call or declare_return_variable,
1212 but we don't need to track it through all the tree phases. */
1213 if (TARGET_EXPR_IMPLICIT_P (expr)
1214 && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (expr)))
1216 tree init = TARGET_EXPR_INITIAL (expr);
1217 if (TREE_CODE (init) == AGGR_INIT_EXPR
1218 && !AGGR_INIT_VIA_CTOR_P (init))
1220 tree fn = AGGR_INIT_EXPR_FN (init);
1221 expr = build_call_array_loc (input_location,
1222 TREE_TYPE (TREE_TYPE (TREE_TYPE (fn))),
1224 aggr_init_expr_nargs (init),
1225 AGGR_INIT_EXPR_ARGP (init));
1228 break;
1230 default:;
1232 expr = resolve_nondeduced_context (expr);
1234 tree probe = expr;
1236 if (TREE_CODE (probe) == ADDR_EXPR)
1237 probe = TREE_OPERAND (expr, 0);
1238 if (type_unknown_p (probe))
1240 /* [over.over] enumerates the places where we can take the address
1241 of an overloaded function, and this is not one of them. */
1242 if (complain & tf_error)
1243 switch (implicit)
1245 case ICV_CAST:
1246 error_at (loc, "conversion to void "
1247 "cannot resolve address of overloaded function");
1248 break;
1249 case ICV_SECOND_OF_COND:
1250 error_at (loc, "second operand of conditional expression "
1251 "cannot resolve address of overloaded function");
1252 break;
1253 case ICV_THIRD_OF_COND:
1254 error_at (loc, "third operand of conditional expression "
1255 "cannot resolve address of overloaded function");
1256 break;
1257 case ICV_RIGHT_OF_COMMA:
1258 error_at (loc, "right operand of comma operator "
1259 "cannot resolve address of overloaded function");
1260 break;
1261 case ICV_LEFT_OF_COMMA:
1262 error_at (loc, "left operand of comma operator "
1263 "cannot resolve address of overloaded function");
1264 break;
1265 case ICV_STATEMENT:
1266 error_at (loc, "statement "
1267 "cannot resolve address of overloaded function");
1268 break;
1269 case ICV_THIRD_IN_FOR:
1270 error_at (loc, "for increment expression "
1271 "cannot resolve address of overloaded function");
1272 break;
1274 else
1275 return error_mark_node;
1276 expr = void_zero_node;
1278 else if (implicit != ICV_CAST && probe == expr && is_overloaded_fn (probe))
1280 /* Only warn when there is no &. */
1281 if (complain & tf_warning)
1282 switch (implicit)
1284 case ICV_SECOND_OF_COND:
1285 warning_at (loc, OPT_Waddress,
1286 "second operand of conditional expression "
1287 "is a reference, not call, to function %qE", expr);
1288 break;
1289 case ICV_THIRD_OF_COND:
1290 warning_at (loc, OPT_Waddress,
1291 "third operand of conditional expression "
1292 "is a reference, not call, to function %qE", expr);
1293 break;
1294 case ICV_RIGHT_OF_COMMA:
1295 warning_at (loc, OPT_Waddress,
1296 "right operand of comma operator "
1297 "is a reference, not call, to function %qE", expr);
1298 break;
1299 case ICV_LEFT_OF_COMMA:
1300 warning_at (loc, OPT_Waddress,
1301 "left operand of comma operator "
1302 "is a reference, not call, to function %qE", expr);
1303 break;
1304 case ICV_STATEMENT:
1305 warning_at (loc, OPT_Waddress,
1306 "statement is a reference, not call, to function %qE",
1307 expr);
1308 break;
1309 case ICV_THIRD_IN_FOR:
1310 warning_at (loc, OPT_Waddress,
1311 "for increment expression "
1312 "is a reference, not call, to function %qE", expr);
1313 break;
1314 default:
1315 gcc_unreachable ();
1318 if (TREE_CODE (expr) == COMPONENT_REF)
1319 expr = TREE_OPERAND (expr, 0);
1323 if (expr != error_mark_node && !VOID_TYPE_P (TREE_TYPE (expr)))
1325 if (implicit != ICV_CAST
1326 && warn_unused_value
1327 && !TREE_NO_WARNING (expr)
1328 && !processing_template_decl)
1330 /* The middle end does not warn about expressions that have
1331 been explicitly cast to void, so we must do so here. */
1332 if (!TREE_SIDE_EFFECTS (expr)) {
1333 if (complain & tf_warning)
1334 switch (implicit)
1336 case ICV_SECOND_OF_COND:
1337 warning_at (loc, OPT_Wunused_value,
1338 "second operand of conditional expression "
1339 "has no effect");
1340 break;
1341 case ICV_THIRD_OF_COND:
1342 warning_at (loc, OPT_Wunused_value,
1343 "third operand of conditional expression "
1344 "has no effect");
1345 break;
1346 case ICV_RIGHT_OF_COMMA:
1347 warning_at (loc, OPT_Wunused_value,
1348 "right operand of comma operator has no effect");
1349 break;
1350 case ICV_LEFT_OF_COMMA:
1351 warning_at (loc, OPT_Wunused_value,
1352 "left operand of comma operator has no effect");
1353 break;
1354 case ICV_STATEMENT:
1355 warning_at (loc, OPT_Wunused_value,
1356 "statement has no effect");
1357 break;
1358 case ICV_THIRD_IN_FOR:
1359 warning_at (loc, OPT_Wunused_value,
1360 "for increment expression has no effect");
1361 break;
1362 default:
1363 gcc_unreachable ();
1366 else
1368 tree e;
1369 enum tree_code code;
1370 enum tree_code_class tclass;
1372 e = expr;
1373 /* We might like to warn about (say) "(int) f()", as the
1374 cast has no effect, but the compiler itself will
1375 generate implicit conversions under some
1376 circumstances. (For example a block copy will be
1377 turned into a call to "__builtin_memcpy", with a
1378 conversion of the return value to an appropriate
1379 type.) So, to avoid false positives, we strip
1380 conversions. Do not use STRIP_NOPs because it will
1381 not strip conversions to "void", as that is not a
1382 mode-preserving conversion. */
1383 while (TREE_CODE (e) == NOP_EXPR)
1384 e = TREE_OPERAND (e, 0);
1386 code = TREE_CODE (e);
1387 tclass = TREE_CODE_CLASS (code);
1388 if ((tclass == tcc_comparison
1389 || tclass == tcc_unary
1390 || (tclass == tcc_binary
1391 && !(code == MODIFY_EXPR
1392 || code == INIT_EXPR
1393 || code == PREDECREMENT_EXPR
1394 || code == PREINCREMENT_EXPR
1395 || code == POSTDECREMENT_EXPR
1396 || code == POSTINCREMENT_EXPR)))
1397 && (complain & tf_warning))
1398 warning_at (loc, OPT_Wunused_value, "value computed is not used");
1401 expr = build1 (CONVERT_EXPR, void_type_node, expr);
1403 if (! TREE_SIDE_EFFECTS (expr))
1404 expr = void_zero_node;
1405 return expr;
1408 /* Create an expression whose value is that of EXPR,
1409 converted to type TYPE. The TREE_TYPE of the value
1410 is always TYPE. This function implements all reasonable
1411 conversions; callers should filter out those that are
1412 not permitted by the language being compiled.
1414 Most of this routine is from build_reinterpret_cast.
1416 The back end cannot call cp_convert (what was convert) because
1417 conversions to/from basetypes may involve memory references
1418 (vbases) and adding or subtracting small values (multiple
1419 inheritance), but it calls convert from the constant folding code
1420 on subtrees of already built trees after it has ripped them apart.
1422 Also, if we ever support range variables, we'll probably also have to
1423 do a little bit more work. */
1425 tree
1426 convert (tree type, tree expr)
1428 tree intype;
1430 if (type == error_mark_node || expr == error_mark_node)
1431 return error_mark_node;
1433 intype = TREE_TYPE (expr);
1435 if (POINTER_TYPE_P (type) && POINTER_TYPE_P (intype))
1436 return fold_if_not_in_template (build_nop (type, expr));
1438 return ocp_convert (type, expr, CONV_OLD_CONVERT,
1439 LOOKUP_NORMAL|LOOKUP_NO_CONVERSION,
1440 tf_warning_or_error);
1443 /* Like cp_convert, except permit conversions to take place which
1444 are not normally allowed due to access restrictions
1445 (such as conversion from sub-type to private super-type). */
1447 tree
1448 convert_force (tree type, tree expr, int convtype, tsubst_flags_t complain)
1450 tree e = expr;
1451 enum tree_code code = TREE_CODE (type);
1453 if (code == REFERENCE_TYPE)
1454 return (fold_if_not_in_template
1455 (convert_to_reference (type, e, CONV_C_CAST, 0,
1456 NULL_TREE, complain)));
1458 if (code == POINTER_TYPE)
1459 return fold_if_not_in_template (convert_to_pointer_force (type, e,
1460 complain));
1462 /* From typeck.c convert_for_assignment */
1463 if (((TREE_CODE (TREE_TYPE (e)) == POINTER_TYPE && TREE_CODE (e) == ADDR_EXPR
1464 && TREE_CODE (TREE_TYPE (e)) == POINTER_TYPE
1465 && TREE_CODE (TREE_TYPE (TREE_TYPE (e))) == METHOD_TYPE)
1466 || integer_zerop (e)
1467 || TYPE_PTRMEMFUNC_P (TREE_TYPE (e)))
1468 && TYPE_PTRMEMFUNC_P (type))
1469 /* compatible pointer to member functions. */
1470 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), e, 1,
1471 /*c_cast_p=*/1, complain);
1473 return ocp_convert (type, e, CONV_C_CAST|convtype, LOOKUP_NORMAL, complain);
1476 /* Convert an aggregate EXPR to type XTYPE. If a conversion
1477 exists, return the attempted conversion. This may
1478 return ERROR_MARK_NODE if the conversion is not
1479 allowed (references private members, etc).
1480 If no conversion exists, NULL_TREE is returned.
1482 FIXME: Ambiguity checking is wrong. Should choose one by the implicit
1483 object parameter, or by the second standard conversion sequence if
1484 that doesn't do it. This will probably wait for an overloading rewrite.
1485 (jason 8/9/95) */
1487 static tree
1488 build_type_conversion (tree xtype, tree expr)
1490 /* C++: check to see if we can convert this aggregate type
1491 into the required type. */
1492 return build_user_type_conversion (xtype, expr, LOOKUP_NORMAL,
1493 tf_warning_or_error);
1496 /* Convert the given EXPR to one of a group of types suitable for use in an
1497 expression. DESIRES is a combination of various WANT_* flags (q.v.)
1498 which indicates which types are suitable. If COMPLAIN is true, complain
1499 about ambiguity; otherwise, the caller will deal with it. */
1501 tree
1502 build_expr_type_conversion (int desires, tree expr, bool complain)
1504 tree basetype = TREE_TYPE (expr);
1505 tree conv = NULL_TREE;
1506 tree winner = NULL_TREE;
1508 if (expr == null_node
1509 && (desires & WANT_INT)
1510 && !(desires & WANT_NULL))
1512 source_location loc =
1513 expansion_point_location_if_in_system_header (input_location);
1515 warning_at (loc, OPT_Wconversion_null,
1516 "converting NULL to non-pointer type");
1519 if (basetype == error_mark_node)
1520 return error_mark_node;
1522 if (! MAYBE_CLASS_TYPE_P (basetype))
1523 switch (TREE_CODE (basetype))
1525 case INTEGER_TYPE:
1526 if ((desires & WANT_NULL) && null_ptr_cst_p (expr))
1527 return expr;
1528 /* else fall through... */
1530 case BOOLEAN_TYPE:
1531 return (desires & WANT_INT) ? expr : NULL_TREE;
1532 case ENUMERAL_TYPE:
1533 return (desires & WANT_ENUM) ? expr : NULL_TREE;
1534 case REAL_TYPE:
1535 return (desires & WANT_FLOAT) ? expr : NULL_TREE;
1536 case POINTER_TYPE:
1537 return (desires & WANT_POINTER) ? expr : NULL_TREE;
1539 case FUNCTION_TYPE:
1540 case ARRAY_TYPE:
1541 return (desires & WANT_POINTER) ? decay_conversion (expr,
1542 tf_warning_or_error)
1543 : NULL_TREE;
1545 case COMPLEX_TYPE:
1546 case VECTOR_TYPE:
1547 if ((desires & WANT_VECTOR_OR_COMPLEX) == 0)
1548 return NULL_TREE;
1549 switch (TREE_CODE (TREE_TYPE (basetype)))
1551 case INTEGER_TYPE:
1552 case BOOLEAN_TYPE:
1553 return (desires & WANT_INT) ? expr : NULL_TREE;
1554 case ENUMERAL_TYPE:
1555 return (desires & WANT_ENUM) ? expr : NULL_TREE;
1556 case REAL_TYPE:
1557 return (desires & WANT_FLOAT) ? expr : NULL_TREE;
1558 default:
1559 return NULL_TREE;
1562 default:
1563 return NULL_TREE;
1566 /* The code for conversions from class type is currently only used for
1567 delete expressions. Other expressions are handled by build_new_op. */
1568 if (!complete_type_or_maybe_complain (basetype, expr, complain))
1569 return error_mark_node;
1570 if (!TYPE_HAS_CONVERSION (basetype))
1571 return NULL_TREE;
1573 for (conv = lookup_conversions (basetype); conv; conv = TREE_CHAIN (conv))
1575 int win = 0;
1576 tree candidate;
1577 tree cand = TREE_VALUE (conv);
1578 cand = OVL_CURRENT (cand);
1580 if (winner && winner == cand)
1581 continue;
1583 if (DECL_NONCONVERTING_P (cand))
1584 continue;
1586 if (TREE_CODE (cand) == TEMPLATE_DECL)
1588 if (complain)
1590 error ("ambiguous default type conversion from %qT",
1591 basetype);
1592 error (" candidate conversions include %qD", cand);
1594 return error_mark_node;
1597 candidate = non_reference (TREE_TYPE (TREE_TYPE (cand)));
1599 switch (TREE_CODE (candidate))
1601 case BOOLEAN_TYPE:
1602 case INTEGER_TYPE:
1603 win = (desires & WANT_INT); break;
1604 case ENUMERAL_TYPE:
1605 win = (desires & WANT_ENUM); break;
1606 case REAL_TYPE:
1607 win = (desires & WANT_FLOAT); break;
1608 case POINTER_TYPE:
1609 win = (desires & WANT_POINTER); break;
1611 case COMPLEX_TYPE:
1612 case VECTOR_TYPE:
1613 if ((desires & WANT_VECTOR_OR_COMPLEX) == 0)
1614 break;
1615 switch (TREE_CODE (TREE_TYPE (candidate)))
1617 case BOOLEAN_TYPE:
1618 case INTEGER_TYPE:
1619 win = (desires & WANT_INT); break;
1620 case ENUMERAL_TYPE:
1621 win = (desires & WANT_ENUM); break;
1622 case REAL_TYPE:
1623 win = (desires & WANT_FLOAT); break;
1624 default:
1625 break;
1627 break;
1629 default:
1630 break;
1633 if (win)
1635 if (winner)
1637 if (complain)
1639 error ("ambiguous default type conversion from %qT",
1640 basetype);
1641 error (" candidate conversions include %qD and %qD",
1642 winner, cand);
1644 return error_mark_node;
1646 else
1647 winner = cand;
1651 if (winner)
1653 tree type = non_reference (TREE_TYPE (TREE_TYPE (winner)));
1654 return build_user_type_conversion (type, expr, LOOKUP_NORMAL,
1655 tf_warning_or_error);
1658 return NULL_TREE;
1661 /* Implements integral promotion (4.1) and float->double promotion. */
1663 tree
1664 type_promotes_to (tree type)
1666 tree promoted_type;
1668 if (type == error_mark_node)
1669 return error_mark_node;
1671 type = TYPE_MAIN_VARIANT (type);
1673 /* Check for promotions of target-defined types first. */
1674 promoted_type = targetm.promoted_type (type);
1675 if (promoted_type)
1676 return promoted_type;
1678 /* bool always promotes to int (not unsigned), even if it's the same
1679 size. */
1680 if (TREE_CODE (type) == BOOLEAN_TYPE)
1681 type = integer_type_node;
1683 /* Scoped enums don't promote, but pretend they do for backward ABI bug
1684 compatibility wrt varargs. */
1685 else if (SCOPED_ENUM_P (type) && abi_version_at_least (6))
1688 /* Normally convert enums to int, but convert wide enums to something
1689 wider. */
1690 else if (TREE_CODE (type) == ENUMERAL_TYPE
1691 || type == char16_type_node
1692 || type == char32_type_node
1693 || type == wchar_type_node)
1695 int precision = MAX (TYPE_PRECISION (type),
1696 TYPE_PRECISION (integer_type_node));
1697 tree totype = c_common_type_for_size (precision, 0);
1698 if (SCOPED_ENUM_P (type))
1699 warning (OPT_Wabi, "scoped enum %qT will not promote to an integral "
1700 "type in a future version of GCC", type);
1701 if (TREE_CODE (type) == ENUMERAL_TYPE)
1702 type = ENUM_UNDERLYING_TYPE (type);
1703 if (TYPE_UNSIGNED (type)
1704 && ! int_fits_type_p (TYPE_MAX_VALUE (type), totype))
1705 type = c_common_type_for_size (precision, 1);
1706 else
1707 type = totype;
1709 else if (c_promoting_integer_type_p (type))
1711 /* Retain unsignedness if really not getting bigger. */
1712 if (TYPE_UNSIGNED (type)
1713 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
1714 type = unsigned_type_node;
1715 else
1716 type = integer_type_node;
1718 else if (type == float_type_node)
1719 type = double_type_node;
1721 return type;
1724 /* The routines below this point are carefully written to conform to
1725 the standard. They use the same terminology, and follow the rules
1726 closely. Although they are used only in pt.c at the moment, they
1727 should presumably be used everywhere in the future. */
1729 /* Attempt to perform qualification conversions on EXPR to convert it
1730 to TYPE. Return the resulting expression, or error_mark_node if
1731 the conversion was impossible. */
1733 tree
1734 perform_qualification_conversions (tree type, tree expr)
1736 tree expr_type;
1738 expr_type = TREE_TYPE (expr);
1740 if (same_type_p (type, expr_type))
1741 return expr;
1742 else if (TYPE_PTR_P (type) && TYPE_PTR_P (expr_type)
1743 && comp_ptr_ttypes (TREE_TYPE (type), TREE_TYPE (expr_type)))
1744 return build_nop (type, expr);
1745 else if (TYPE_PTRMEM_P (type) && TYPE_PTRMEM_P (expr_type)
1746 && same_type_p (TYPE_PTRMEM_CLASS_TYPE (type),
1747 TYPE_PTRMEM_CLASS_TYPE (expr_type))
1748 && comp_ptr_ttypes (TYPE_PTRMEM_POINTED_TO_TYPE (type),
1749 TYPE_PTRMEM_POINTED_TO_TYPE (expr_type)))
1750 return build_nop (type, expr);
1751 else
1752 return error_mark_node;