* cse.c (cse_insn): Fix loop to stop at VOIDmode.
[official-gcc.git] / gcc / cp / cvt.c
blobfed4ab2910c4a570b779a9e70cc59a22e6f378b8
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
4 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 "convert.h"
37 #include "toplev.h"
38 #include "decl.h"
39 #include "target.h"
41 static tree cp_convert_to_pointer (tree, tree);
42 static tree convert_to_pointer_force (tree, tree);
43 static tree build_type_conversion (tree, tree);
44 static tree build_up_reference (tree, tree, int, tree);
45 static void warn_ref_binding (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)
79 tree intype = TREE_TYPE (expr);
80 enum tree_code form;
81 tree rval;
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 error ("can't convert from incomplete type %qT to %qT",
91 intype, type);
92 return error_mark_node;
95 rval = build_type_conversion (type, expr);
96 if (rval)
98 if (rval == error_mark_node)
99 error ("conversion of %qE from %qT to %qT is ambiguous",
100 expr, intype, type);
101 return rval;
105 /* Handle anachronistic conversions from (::*)() to cv void* or (*)(). */
106 if (TREE_CODE (type) == POINTER_TYPE
107 && (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
108 || VOID_TYPE_P (TREE_TYPE (type))))
110 if (TYPE_PTRMEMFUNC_P (intype)
111 || TREE_CODE (intype) == METHOD_TYPE)
112 return convert_member_func_to_ptr (type, expr);
113 if (TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE)
114 return build_nop (type, expr);
115 intype = TREE_TYPE (expr);
118 if (expr == error_mark_node)
119 return error_mark_node;
121 form = TREE_CODE (intype);
123 if (POINTER_TYPE_P (intype))
125 intype = TYPE_MAIN_VARIANT (intype);
127 if (TYPE_MAIN_VARIANT (type) != intype
128 && TREE_CODE (type) == POINTER_TYPE
129 && TREE_CODE (TREE_TYPE (type)) == RECORD_TYPE
130 && MAYBE_CLASS_TYPE_P (TREE_TYPE (type))
131 && MAYBE_CLASS_TYPE_P (TREE_TYPE (intype))
132 && TREE_CODE (TREE_TYPE (intype)) == RECORD_TYPE)
134 enum tree_code code = PLUS_EXPR;
135 tree binfo;
136 tree intype_class;
137 tree type_class;
138 bool same_p;
140 intype_class = TREE_TYPE (intype);
141 type_class = TREE_TYPE (type);
143 same_p = same_type_p (TYPE_MAIN_VARIANT (intype_class),
144 TYPE_MAIN_VARIANT (type_class));
145 binfo = NULL_TREE;
146 /* Try derived to base conversion. */
147 if (!same_p)
148 binfo = lookup_base (intype_class, type_class, ba_check, NULL);
149 if (!same_p && !binfo)
151 /* Try base to derived conversion. */
152 binfo = lookup_base (type_class, intype_class, ba_check, NULL);
153 code = MINUS_EXPR;
155 if (binfo == error_mark_node)
156 return error_mark_node;
157 if (binfo || same_p)
159 if (binfo)
160 expr = build_base_path (code, expr, binfo, 0);
161 /* Add any qualifier conversions. */
162 return build_nop (type, expr);
166 if (TYPE_PTRMEMFUNC_P (type))
168 error ("cannot convert %qE from type %qT to type %qT",
169 expr, intype, type);
170 return error_mark_node;
173 return build_nop (type, expr);
175 else if ((TYPE_PTRMEM_P (type) && TYPE_PTRMEM_P (intype))
176 || (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype)))
177 return convert_ptrmem (type, expr, /*allow_inverse_p=*/false,
178 /*c_cast_p=*/false);
179 else if (TYPE_PTRMEMFUNC_P (intype))
181 if (!warn_pmf2ptr)
183 if (TREE_CODE (expr) == PTRMEM_CST)
184 return cp_convert_to_pointer (type,
185 PTRMEM_CST_MEMBER (expr));
186 else if (TREE_CODE (expr) == OFFSET_REF)
188 tree object = TREE_OPERAND (expr, 0);
189 return get_member_function_from_ptrfunc (&object,
190 TREE_OPERAND (expr, 1));
193 error ("cannot convert %qE from type %qT to type %qT",
194 expr, intype, type);
195 return error_mark_node;
198 if (integer_zerop (expr))
200 if (TYPE_PTRMEMFUNC_P (type))
201 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), expr, 0,
202 /*c_cast_p=*/false);
204 if (TYPE_PTRMEM_P (type))
206 /* A NULL pointer-to-member is represented by -1, not by
207 zero. */
208 expr = build_int_cst_type (type, -1);
210 else
211 expr = build_int_cst (type, 0);
213 return expr;
215 else if (TYPE_PTR_TO_MEMBER_P (type) && INTEGRAL_CODE_P (form))
217 error ("invalid conversion from %qT to %qT", intype, type);
218 return error_mark_node;
221 if (INTEGRAL_CODE_P (form))
223 if (TYPE_PRECISION (intype) == POINTER_SIZE)
224 return build1 (CONVERT_EXPR, type, expr);
225 expr = cp_convert (c_common_type_for_size (POINTER_SIZE, 0), expr);
226 /* Modes may be different but sizes should be the same. There
227 is supposed to be some integral type that is the same width
228 as a pointer. */
229 gcc_assert (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (expr)))
230 == GET_MODE_SIZE (TYPE_MODE (type)));
232 return convert_to_pointer (type, expr);
235 if (type_unknown_p (expr))
236 return instantiate_type (type, expr, tf_warning_or_error);
238 error ("cannot convert %qE from type %qT to type %qT",
239 expr, intype, type);
240 return error_mark_node;
243 /* Like convert, except permit conversions to take place which
244 are not normally allowed due to access restrictions
245 (such as conversion from sub-type to private super-type). */
247 static tree
248 convert_to_pointer_force (tree type, tree expr)
250 tree intype = TREE_TYPE (expr);
251 enum tree_code form = TREE_CODE (intype);
253 if (form == POINTER_TYPE)
255 intype = TYPE_MAIN_VARIANT (intype);
257 if (TYPE_MAIN_VARIANT (type) != intype
258 && TREE_CODE (TREE_TYPE (type)) == RECORD_TYPE
259 && MAYBE_CLASS_TYPE_P (TREE_TYPE (type))
260 && MAYBE_CLASS_TYPE_P (TREE_TYPE (intype))
261 && TREE_CODE (TREE_TYPE (intype)) == RECORD_TYPE)
263 enum tree_code code = PLUS_EXPR;
264 tree binfo;
266 binfo = lookup_base (TREE_TYPE (intype), TREE_TYPE (type),
267 ba_unique, NULL);
268 if (!binfo)
270 binfo = lookup_base (TREE_TYPE (type), TREE_TYPE (intype),
271 ba_unique, NULL);
272 code = MINUS_EXPR;
274 if (binfo == error_mark_node)
275 return error_mark_node;
276 if (binfo)
278 expr = build_base_path (code, expr, binfo, 0);
279 if (expr == error_mark_node)
280 return error_mark_node;
281 /* Add any qualifier conversions. */
282 if (!same_type_p (TREE_TYPE (TREE_TYPE (expr)),
283 TREE_TYPE (type)))
284 expr = build_nop (type, expr);
285 return expr;
290 return cp_convert_to_pointer (type, expr);
293 /* We are passing something to a function which requires a reference.
294 The type we are interested in is in TYPE. The initial
295 value we have to begin with is in ARG.
297 FLAGS controls how we manage access checking.
298 DIRECT_BIND in FLAGS controls how any temporaries are generated.
299 If DIRECT_BIND is set, DECL is the reference we're binding to. */
301 static tree
302 build_up_reference (tree type, tree arg, int flags, tree decl)
304 tree rval;
305 tree argtype = TREE_TYPE (arg);
306 tree target_type = TREE_TYPE (type);
308 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
310 if ((flags & DIRECT_BIND) && ! real_lvalue_p (arg))
312 /* Create a new temporary variable. We can't just use a TARGET_EXPR
313 here because it needs to live as long as DECL. */
314 tree targ = arg;
316 arg = make_temporary_var_for_ref_to_temp (decl, TREE_TYPE (arg));
318 /* Process the initializer for the declaration. */
319 DECL_INITIAL (arg) = targ;
320 cp_finish_decl (arg, targ, /*init_const_expr_p=*/false, NULL_TREE,
321 LOOKUP_ONLYCONVERTING|DIRECT_BIND);
323 else if (!(flags & DIRECT_BIND) && ! lvalue_p (arg))
324 return get_target_expr (arg);
326 /* If we had a way to wrap this up, and say, if we ever needed its
327 address, transform all occurrences of the register, into a memory
328 reference we could win better. */
329 rval = cp_build_unary_op (ADDR_EXPR, arg, 1, tf_warning_or_error);
330 if (rval == error_mark_node)
331 return error_mark_node;
333 if ((flags & LOOKUP_PROTECT)
334 && TYPE_MAIN_VARIANT (argtype) != TYPE_MAIN_VARIANT (target_type)
335 && MAYBE_CLASS_TYPE_P (argtype)
336 && MAYBE_CLASS_TYPE_P (target_type))
338 /* We go through lookup_base for the access control. */
339 tree binfo = lookup_base (argtype, target_type, ba_check, NULL);
340 if (binfo == error_mark_node)
341 return error_mark_node;
342 if (binfo == NULL_TREE)
343 return error_not_base_type (target_type, argtype);
344 rval = build_base_path (PLUS_EXPR, rval, binfo, 1);
346 else
347 rval
348 = convert_to_pointer_force (build_pointer_type (target_type), rval);
349 return build_nop (type, rval);
352 /* Subroutine of convert_to_reference. REFTYPE is the target reference type.
353 INTYPE is the original rvalue type and DECL is an optional _DECL node
354 for diagnostics.
356 [dcl.init.ref] says that if an rvalue is used to
357 initialize a reference, then the reference must be to a
358 non-volatile const type. */
360 static void
361 warn_ref_binding (tree reftype, tree intype, tree decl)
363 tree ttl = TREE_TYPE (reftype);
365 if (!CP_TYPE_CONST_NON_VOLATILE_P (ttl))
367 const char *msg;
369 if (CP_TYPE_VOLATILE_P (ttl) && decl)
370 msg = "initialization of volatile reference type %q#T from"
371 " rvalue of type %qT";
372 else if (CP_TYPE_VOLATILE_P (ttl))
373 msg = "conversion to volatile reference type %q#T "
374 " from rvalue of type %qT";
375 else if (decl)
376 msg = "initialization of non-const reference type %q#T from"
377 " rvalue of type %qT";
378 else
379 msg = "conversion to non-const reference type %q#T from"
380 " rvalue of type %qT";
382 permerror (input_location, msg, reftype, intype);
386 /* For C++: Only need to do one-level references, but cannot
387 get tripped up on signed/unsigned differences.
389 DECL is either NULL_TREE or the _DECL node for a reference that is being
390 initialized. It can be error_mark_node if we don't know the _DECL but
391 we know it's an initialization. */
393 tree
394 convert_to_reference (tree reftype, tree expr, int convtype,
395 int flags, tree decl)
397 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (reftype));
398 tree intype;
399 tree rval = NULL_TREE;
400 tree rval_as_conversion = NULL_TREE;
401 bool can_convert_intype_to_type;
403 if (TREE_CODE (type) == FUNCTION_TYPE
404 && TREE_TYPE (expr) == unknown_type_node)
405 expr = instantiate_type (type, expr,
406 (flags & LOOKUP_COMPLAIN)
407 ? tf_warning_or_error : tf_none);
409 if (expr == error_mark_node)
410 return error_mark_node;
412 intype = TREE_TYPE (expr);
414 gcc_assert (TREE_CODE (intype) != REFERENCE_TYPE);
415 gcc_assert (TREE_CODE (reftype) == REFERENCE_TYPE);
417 intype = TYPE_MAIN_VARIANT (intype);
419 can_convert_intype_to_type = can_convert (type, intype);
420 if (!can_convert_intype_to_type
421 && (convtype & CONV_IMPLICIT) && MAYBE_CLASS_TYPE_P (intype)
422 && ! (flags & LOOKUP_NO_CONVERSION))
424 /* Look for a user-defined conversion to lvalue that we can use. */
426 rval_as_conversion
427 = build_type_conversion (reftype, expr);
429 if (rval_as_conversion && rval_as_conversion != error_mark_node
430 && real_lvalue_p (rval_as_conversion))
432 expr = rval_as_conversion;
433 rval_as_conversion = NULL_TREE;
434 intype = type;
435 can_convert_intype_to_type = 1;
439 if (((convtype & CONV_STATIC) && can_convert (intype, type))
440 || ((convtype & CONV_IMPLICIT) && can_convert_intype_to_type))
442 if (flags & LOOKUP_COMPLAIN)
444 tree ttl = TREE_TYPE (reftype);
445 tree ttr = lvalue_type (expr);
447 if (! real_lvalue_p (expr))
448 warn_ref_binding (reftype, intype, decl);
450 if (! (convtype & CONV_CONST)
451 && !at_least_as_qualified_p (ttl, ttr))
452 permerror (input_location, "conversion from %qT to %qT discards qualifiers",
453 ttr, reftype);
456 return build_up_reference (reftype, expr, flags, decl);
458 else if ((convtype & CONV_REINTERPRET) && lvalue_p (expr))
460 /* When casting an lvalue to a reference type, just convert into
461 a pointer to the new type and deference it. This is allowed
462 by San Diego WP section 5.2.9 paragraph 12, though perhaps it
463 should be done directly (jason). (int &)ri ---> *(int*)&ri */
465 /* B* bp; A& ar = (A&)bp; is valid, but it's probably not what they
466 meant. */
467 if (TREE_CODE (intype) == POINTER_TYPE
468 && (comptypes (TREE_TYPE (intype), type,
469 COMPARE_BASE | COMPARE_DERIVED)))
470 warning (0, "casting %qT to %qT does not dereference pointer",
471 intype, reftype);
473 rval = cp_build_unary_op (ADDR_EXPR, expr, 0, tf_warning_or_error);
474 if (rval != error_mark_node)
475 rval = convert_force (build_pointer_type (TREE_TYPE (reftype)),
476 rval, 0);
477 if (rval != error_mark_node)
478 rval = build1 (NOP_EXPR, reftype, rval);
480 else
482 rval = convert_for_initialization (NULL_TREE, type, expr, flags,
483 "converting", 0, 0,
484 tf_warning_or_error);
485 if (rval == NULL_TREE || rval == error_mark_node)
486 return rval;
487 warn_ref_binding (reftype, intype, decl);
488 rval = build_up_reference (reftype, rval, flags, decl);
491 if (rval)
493 /* If we found a way to convert earlier, then use it. */
494 return rval;
497 if (flags & LOOKUP_COMPLAIN)
498 error ("cannot convert type %qT to type %qT", intype, reftype);
500 return error_mark_node;
503 /* We are using a reference VAL for its value. Bash that reference all the
504 way down to its lowest form. */
506 tree
507 convert_from_reference (tree val)
509 if (TREE_CODE (TREE_TYPE (val)) == REFERENCE_TYPE)
511 tree t = canonical_type_variant (TREE_TYPE (TREE_TYPE (val)));
512 tree ref = build1 (INDIRECT_REF, t, val);
514 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
515 so that we get the proper error message if the result is used
516 to assign to. Also, &* is supposed to be a no-op. */
517 TREE_READONLY (ref) = CP_TYPE_CONST_P (t);
518 TREE_THIS_VOLATILE (ref) = CP_TYPE_VOLATILE_P (t);
519 TREE_SIDE_EFFECTS (ref)
520 = (TREE_THIS_VOLATILE (ref) || TREE_SIDE_EFFECTS (val));
521 REFERENCE_REF_P (ref) = 1;
522 val = ref;
525 return val;
528 /* Really perform an lvalue-to-rvalue conversion, including copying an
529 argument of class type into a temporary. */
531 tree
532 force_rvalue (tree expr)
534 if (MAYBE_CLASS_TYPE_P (TREE_TYPE (expr)) && TREE_CODE (expr) != TARGET_EXPR)
535 expr = ocp_convert (TREE_TYPE (expr), expr,
536 CONV_IMPLICIT|CONV_FORCE_TEMP, LOOKUP_NORMAL);
537 else
538 expr = decay_conversion (expr);
540 return expr;
543 /* C++ conversions, preference to static cast conversions. */
545 tree
546 cp_convert (tree type, tree expr)
548 return ocp_convert (type, expr, CONV_OLD_CONVERT, LOOKUP_NORMAL);
551 /* C++ equivalent of convert_and_check but using cp_convert as the
552 conversion function.
554 Convert EXPR to TYPE, warning about conversion problems with constants.
555 Invoke this function on every expression that is converted implicitly,
556 i.e. because of language rules and not because of an explicit cast. */
558 tree
559 cp_convert_and_check (tree type, tree expr)
561 tree result;
563 if (TREE_TYPE (expr) == type)
564 return expr;
566 result = cp_convert (type, expr);
568 if (!skip_evaluation && !TREE_OVERFLOW_P (expr) && result != error_mark_node)
569 warnings_for_convert_and_check (type, expr, result);
571 return result;
574 /* Conversion...
576 FLAGS indicates how we should behave. */
578 tree
579 ocp_convert (tree type, tree expr, int convtype, int flags)
581 tree e = expr;
582 enum tree_code code = TREE_CODE (type);
583 const char *invalid_conv_diag;
585 if (error_operand_p (e) || type == error_mark_node)
586 return error_mark_node;
588 complete_type (type);
589 complete_type (TREE_TYPE (expr));
591 if ((invalid_conv_diag
592 = targetm.invalid_conversion (TREE_TYPE (expr), type)))
594 error (invalid_conv_diag);
595 return error_mark_node;
598 e = integral_constant_value (e);
600 if (MAYBE_CLASS_TYPE_P (type) && (convtype & CONV_FORCE_TEMP))
601 /* We need a new temporary; don't take this shortcut. */;
602 else if (same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (e)))
604 if (same_type_p (type, TREE_TYPE (e)))
605 /* The call to fold will not always remove the NOP_EXPR as
606 might be expected, since if one of the types is a typedef;
607 the comparison in fold is just equality of pointers, not a
608 call to comptypes. We don't call fold in this case because
609 that can result in infinite recursion; fold will call
610 convert, which will call ocp_convert, etc. */
611 return e;
612 /* For complex data types, we need to perform componentwise
613 conversion. */
614 else if (TREE_CODE (type) == COMPLEX_TYPE)
615 return fold_if_not_in_template (convert_to_complex (type, e));
616 else if (TREE_CODE (e) == TARGET_EXPR)
618 /* Don't build a NOP_EXPR of class type. Instead, change the
619 type of the temporary. */
620 TREE_TYPE (e) = TREE_TYPE (TARGET_EXPR_SLOT (e)) = type;
621 return e;
623 else
625 /* We shouldn't be treating objects of ADDRESSABLE type as
626 rvalues. */
627 gcc_assert (!TREE_ADDRESSABLE (type));
628 return fold_if_not_in_template (build_nop (type, e));
632 if (code == VOID_TYPE && (convtype & CONV_STATIC))
634 e = convert_to_void (e, /*implicit=*/NULL, tf_warning_or_error);
635 return e;
638 if (INTEGRAL_CODE_P (code))
640 tree intype = TREE_TYPE (e);
642 if (TREE_CODE (type) == ENUMERAL_TYPE)
644 /* enum = enum, enum = int, enum = float, (enum)pointer are all
645 errors. */
646 if (((INTEGRAL_OR_ENUMERATION_TYPE_P (intype)
647 || TREE_CODE (intype) == REAL_TYPE)
648 && ! (convtype & CONV_STATIC))
649 || TREE_CODE (intype) == POINTER_TYPE)
651 if (flags & LOOKUP_COMPLAIN)
652 permerror (input_location, "conversion from %q#T to %q#T", intype, type);
654 if (!flag_permissive)
655 return error_mark_node;
658 /* [expr.static.cast]
660 8. A value of integral or enumeration type can be explicitly
661 converted to an enumeration type. The value is unchanged if
662 the original value is within the range of the enumeration
663 values. Otherwise, the resulting enumeration value is
664 unspecified. */
665 if (TREE_CODE (expr) == INTEGER_CST && !int_fits_type_p (expr, type))
666 warning (OPT_Wconversion,
667 "the result of the conversion is unspecified because "
668 "%qE is outside the range of type %qT",
669 expr, type);
671 if (MAYBE_CLASS_TYPE_P (intype))
673 tree rval;
674 rval = build_type_conversion (type, e);
675 if (rval)
676 return rval;
677 if (flags & LOOKUP_COMPLAIN)
678 error ("%q#T used where a %qT was expected", intype, type);
679 return error_mark_node;
681 if (code == BOOLEAN_TYPE)
682 return cp_truthvalue_conversion (e);
684 return fold_if_not_in_template (convert_to_integer (type, e));
686 if (POINTER_TYPE_P (type) || TYPE_PTR_TO_MEMBER_P (type))
687 return fold_if_not_in_template (cp_convert_to_pointer (type, e));
688 if (code == VECTOR_TYPE)
690 tree in_vtype = TREE_TYPE (e);
691 if (MAYBE_CLASS_TYPE_P (in_vtype))
693 tree ret_val;
694 ret_val = build_type_conversion (type, e);
695 if (ret_val)
696 return ret_val;
697 if (flags & LOOKUP_COMPLAIN)
698 error ("%q#T used where a %qT was expected", in_vtype, type);
699 return error_mark_node;
701 return fold_if_not_in_template (convert_to_vector (type, e));
703 if (code == REAL_TYPE || code == COMPLEX_TYPE)
705 if (MAYBE_CLASS_TYPE_P (TREE_TYPE (e)))
707 tree rval;
708 rval = build_type_conversion (type, e);
709 if (rval)
710 return rval;
711 else
712 if (flags & LOOKUP_COMPLAIN)
713 error ("%q#T used where a floating point value was expected",
714 TREE_TYPE (e));
716 if (code == REAL_TYPE)
717 return fold_if_not_in_template (convert_to_real (type, e));
718 else if (code == COMPLEX_TYPE)
719 return fold_if_not_in_template (convert_to_complex (type, e));
722 /* New C++ semantics: since assignment is now based on
723 memberwise copying, if the rhs type is derived from the
724 lhs type, then we may still do a conversion. */
725 if (RECORD_OR_UNION_CODE_P (code))
727 tree dtype = TREE_TYPE (e);
728 tree ctor = NULL_TREE;
730 dtype = TYPE_MAIN_VARIANT (dtype);
732 /* Conversion between aggregate types. New C++ semantics allow
733 objects of derived type to be cast to objects of base type.
734 Old semantics only allowed this between pointers.
736 There may be some ambiguity between using a constructor
737 vs. using a type conversion operator when both apply. */
739 ctor = e;
741 if (abstract_virtuals_error (NULL_TREE, type))
742 return error_mark_node;
744 if (BRACE_ENCLOSED_INITIALIZER_P (ctor))
745 ctor = perform_implicit_conversion (type, ctor, tf_warning_or_error);
746 else if ((flags & LOOKUP_ONLYCONVERTING)
747 && ! (CLASS_TYPE_P (dtype) && DERIVED_FROM_P (type, dtype)))
748 /* For copy-initialization, first we create a temp of the proper type
749 with a user-defined conversion sequence, then we direct-initialize
750 the target with the temp (see [dcl.init]). */
751 ctor = build_user_type_conversion (type, ctor, flags);
752 else
753 ctor = build_special_member_call (NULL_TREE,
754 complete_ctor_identifier,
755 build_tree_list (NULL_TREE, ctor),
756 type, flags,
757 tf_warning_or_error);
758 if (ctor)
759 return build_cplus_new (type, ctor);
762 if (flags & LOOKUP_COMPLAIN)
763 error ("conversion from %qT to non-scalar type %qT requested",
764 TREE_TYPE (expr), type);
765 return error_mark_node;
768 /* When an expression is used in a void context, its value is discarded and
769 no lvalue-rvalue and similar conversions happen [expr.static.cast/4,
770 stmt.expr/1, expr.comma/1]. This permits dereferencing an incomplete type
771 in a void context. The C++ standard does not define what an `access' to an
772 object is, but there is reason to believe that it is the lvalue to rvalue
773 conversion -- if it were not, `*&*p = 1' would violate [expr]/4 in that it
774 accesses `*p' not to calculate the value to be stored. But, dcl.type.cv/8
775 indicates that volatile semantics should be the same between C and C++
776 where ever possible. C leaves it implementation defined as to what
777 constitutes an access to a volatile. So, we interpret `*vp' as a read of
778 the volatile object `vp' points to, unless that is an incomplete type. For
779 volatile references we do not do this interpretation, because that would
780 make it impossible to ignore the reference return value from functions. We
781 issue warnings in the confusing cases.
783 IMPLICIT is tells us the context of an implicit void conversion. */
785 tree
786 convert_to_void (tree expr, const char *implicit, tsubst_flags_t complain)
788 if (expr == error_mark_node
789 || TREE_TYPE (expr) == error_mark_node)
790 return error_mark_node;
791 if (!TREE_TYPE (expr))
792 return expr;
793 if (invalid_nonstatic_memfn_p (expr, complain))
794 return error_mark_node;
795 if (TREE_CODE (expr) == PSEUDO_DTOR_EXPR)
797 if (complain & tf_error)
798 error ("pseudo-destructor is not called");
799 return error_mark_node;
801 if (VOID_TYPE_P (TREE_TYPE (expr)))
802 return expr;
803 switch (TREE_CODE (expr))
805 case COND_EXPR:
807 /* The two parts of a cond expr might be separate lvalues. */
808 tree op1 = TREE_OPERAND (expr,1);
809 tree op2 = TREE_OPERAND (expr,2);
810 tree new_op1 = convert_to_void
811 (op1, (implicit && !TREE_SIDE_EFFECTS (op2)
812 ? "second operand of conditional" : NULL), complain);
813 tree new_op2 = convert_to_void
814 (op2, (implicit && !TREE_SIDE_EFFECTS (op1)
815 ? "third operand of conditional" : NULL), complain);
817 expr = build3 (COND_EXPR, TREE_TYPE (new_op1),
818 TREE_OPERAND (expr, 0), new_op1, new_op2);
819 break;
822 case COMPOUND_EXPR:
824 /* The second part of a compound expr contains the value. */
825 tree op1 = TREE_OPERAND (expr,1);
826 tree new_op1 = convert_to_void
827 (op1, (implicit && !TREE_NO_WARNING (expr)
828 ? "right-hand operand of comma" : NULL), complain);
830 if (new_op1 != op1)
832 tree t = build2 (COMPOUND_EXPR, TREE_TYPE (new_op1),
833 TREE_OPERAND (expr, 0), new_op1);
834 expr = t;
837 break;
840 case NON_LVALUE_EXPR:
841 case NOP_EXPR:
842 /* These have already decayed to rvalue. */
843 break;
845 case CALL_EXPR: /* We have a special meaning for volatile void fn(). */
846 break;
848 case INDIRECT_REF:
850 tree type = TREE_TYPE (expr);
851 int is_reference = TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0)))
852 == REFERENCE_TYPE;
853 int is_volatile = TYPE_VOLATILE (type);
854 int is_complete = COMPLETE_TYPE_P (complete_type (type));
856 /* Can't load the value if we don't know the type. */
857 if (is_volatile && !is_complete)
859 if (complain & tf_warning)
860 warning (0, "object of incomplete type %qT will not be accessed in %s",
861 type, implicit ? implicit : "void context");
863 /* Don't load the value if this is an implicit dereference, or if
864 the type needs to be handled by ctors/dtors. */
865 else if (is_volatile && (is_reference || TREE_ADDRESSABLE (type)))
867 if (complain & tf_warning)
868 warning (0, "object of type %qT will not be accessed in %s",
869 TREE_TYPE (TREE_OPERAND (expr, 0)),
870 implicit ? implicit : "void context");
872 if (is_reference || !is_volatile || !is_complete || TREE_ADDRESSABLE (type))
873 expr = TREE_OPERAND (expr, 0);
875 break;
878 case VAR_DECL:
880 /* External variables might be incomplete. */
881 tree type = TREE_TYPE (expr);
882 int is_complete = COMPLETE_TYPE_P (complete_type (type));
884 if (TYPE_VOLATILE (type) && !is_complete && (complain & tf_warning))
885 warning (0, "object %qE of incomplete type %qT will not be accessed in %s",
886 expr, type, implicit ? implicit : "void context");
887 break;
890 case TARGET_EXPR:
891 /* Don't bother with the temporary object returned from a function if
892 we don't use it and don't need to destroy it. We'll still
893 allocate space for it in expand_call or declare_return_variable,
894 but we don't need to track it through all the tree phases. */
895 if (TARGET_EXPR_IMPLICIT_P (expr)
896 && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (expr)))
898 tree init = TARGET_EXPR_INITIAL (expr);
899 if (TREE_CODE (init) == AGGR_INIT_EXPR
900 && !AGGR_INIT_VIA_CTOR_P (init))
902 tree fn = AGGR_INIT_EXPR_FN (init);
903 expr = build_call_array (TREE_TYPE (TREE_TYPE (TREE_TYPE (fn))),
905 aggr_init_expr_nargs (init),
906 AGGR_INIT_EXPR_ARGP (init));
909 break;
911 default:;
914 tree probe = expr;
916 if (TREE_CODE (probe) == ADDR_EXPR)
917 probe = TREE_OPERAND (expr, 0);
918 if (type_unknown_p (probe))
920 /* [over.over] enumerates the places where we can take the address
921 of an overloaded function, and this is not one of them. */
922 if (complain & tf_error)
923 error ("%s cannot resolve address of overloaded function",
924 implicit ? implicit : "void cast");
925 else
926 return error_mark_node;
927 expr = void_zero_node;
929 else if (implicit && probe == expr && is_overloaded_fn (probe))
931 /* Only warn when there is no &. */
932 if (complain & tf_warning)
933 warning (OPT_Waddress, "%s is a reference, not call, to function %qE",
934 implicit, expr);
935 if (TREE_CODE (expr) == COMPONENT_REF)
936 expr = TREE_OPERAND (expr, 0);
940 if (expr != error_mark_node && !VOID_TYPE_P (TREE_TYPE (expr)))
942 if (implicit
943 && warn_unused_value
944 && !TREE_NO_WARNING (expr)
945 && !processing_template_decl)
947 /* The middle end does not warn about expressions that have
948 been explicitly cast to void, so we must do so here. */
949 if (!TREE_SIDE_EFFECTS (expr)) {
950 if (complain & tf_warning)
951 warning (OPT_Wunused_value, "%s has no effect", implicit);
953 else
955 tree e;
956 enum tree_code code;
957 enum tree_code_class tclass;
959 e = expr;
960 /* We might like to warn about (say) "(int) f()", as the
961 cast has no effect, but the compiler itself will
962 generate implicit conversions under some
963 circumstances. (For example a block copy will be
964 turned into a call to "__builtin_memcpy", with a
965 conversion of the return value to an appropriate
966 type.) So, to avoid false positives, we strip
967 conversions. Do not use STRIP_NOPs because it will
968 not strip conversions to "void", as that is not a
969 mode-preserving conversion. */
970 while (TREE_CODE (e) == NOP_EXPR)
971 e = TREE_OPERAND (e, 0);
973 code = TREE_CODE (e);
974 tclass = TREE_CODE_CLASS (code);
975 if ((tclass == tcc_comparison
976 || tclass == tcc_unary
977 || (tclass == tcc_binary
978 && !(code == MODIFY_EXPR
979 || code == INIT_EXPR
980 || code == PREDECREMENT_EXPR
981 || code == PREINCREMENT_EXPR
982 || code == POSTDECREMENT_EXPR
983 || code == POSTINCREMENT_EXPR)))
984 && (complain & tf_warning))
985 warning (OPT_Wunused_value, "value computed is not used");
988 expr = build1 (CONVERT_EXPR, void_type_node, expr);
990 if (! TREE_SIDE_EFFECTS (expr))
991 expr = void_zero_node;
992 return expr;
995 /* Create an expression whose value is that of EXPR,
996 converted to type TYPE. The TREE_TYPE of the value
997 is always TYPE. This function implements all reasonable
998 conversions; callers should filter out those that are
999 not permitted by the language being compiled.
1001 Most of this routine is from build_reinterpret_cast.
1003 The back end cannot call cp_convert (what was convert) because
1004 conversions to/from basetypes may involve memory references
1005 (vbases) and adding or subtracting small values (multiple
1006 inheritance), but it calls convert from the constant folding code
1007 on subtrees of already built trees after it has ripped them apart.
1009 Also, if we ever support range variables, we'll probably also have to
1010 do a little bit more work. */
1012 tree
1013 convert (tree type, tree expr)
1015 tree intype;
1017 if (type == error_mark_node || expr == error_mark_node)
1018 return error_mark_node;
1020 intype = TREE_TYPE (expr);
1022 if (POINTER_TYPE_P (type) && POINTER_TYPE_P (intype))
1023 return fold_if_not_in_template (build_nop (type, expr));
1025 return ocp_convert (type, expr, CONV_OLD_CONVERT,
1026 LOOKUP_NORMAL|LOOKUP_NO_CONVERSION);
1029 /* Like cp_convert, except permit conversions to take place which
1030 are not normally allowed due to access restrictions
1031 (such as conversion from sub-type to private super-type). */
1033 tree
1034 convert_force (tree type, tree expr, int convtype)
1036 tree e = expr;
1037 enum tree_code code = TREE_CODE (type);
1039 if (code == REFERENCE_TYPE)
1040 return (fold_if_not_in_template
1041 (convert_to_reference (type, e, CONV_C_CAST, LOOKUP_COMPLAIN,
1042 NULL_TREE)));
1044 if (code == POINTER_TYPE)
1045 return fold_if_not_in_template (convert_to_pointer_force (type, e));
1047 /* From typeck.c convert_for_assignment */
1048 if (((TREE_CODE (TREE_TYPE (e)) == POINTER_TYPE && TREE_CODE (e) == ADDR_EXPR
1049 && TREE_CODE (TREE_TYPE (e)) == POINTER_TYPE
1050 && TREE_CODE (TREE_TYPE (TREE_TYPE (e))) == METHOD_TYPE)
1051 || integer_zerop (e)
1052 || TYPE_PTRMEMFUNC_P (TREE_TYPE (e)))
1053 && TYPE_PTRMEMFUNC_P (type))
1054 /* compatible pointer to member functions. */
1055 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), e, 1,
1056 /*c_cast_p=*/1);
1058 return ocp_convert (type, e, CONV_C_CAST|convtype, LOOKUP_NORMAL);
1061 /* Convert an aggregate EXPR to type XTYPE. If a conversion
1062 exists, return the attempted conversion. This may
1063 return ERROR_MARK_NODE if the conversion is not
1064 allowed (references private members, etc).
1065 If no conversion exists, NULL_TREE is returned.
1067 FIXME: Ambiguity checking is wrong. Should choose one by the implicit
1068 object parameter, or by the second standard conversion sequence if
1069 that doesn't do it. This will probably wait for an overloading rewrite.
1070 (jason 8/9/95) */
1072 static tree
1073 build_type_conversion (tree xtype, tree expr)
1075 /* C++: check to see if we can convert this aggregate type
1076 into the required type. */
1077 return build_user_type_conversion (xtype, expr, LOOKUP_NORMAL);
1080 /* Convert the given EXPR to one of a group of types suitable for use in an
1081 expression. DESIRES is a combination of various WANT_* flags (q.v.)
1082 which indicates which types are suitable. If COMPLAIN is true, complain
1083 about ambiguity; otherwise, the caller will deal with it. */
1085 tree
1086 build_expr_type_conversion (int desires, tree expr, bool complain)
1088 tree basetype = TREE_TYPE (expr);
1089 tree conv = NULL_TREE;
1090 tree winner = NULL_TREE;
1092 if (expr == null_node
1093 && (desires & WANT_INT)
1094 && !(desires & WANT_NULL))
1095 warning (OPT_Wconversion, "converting NULL to non-pointer type");
1097 basetype = TREE_TYPE (expr);
1099 if (basetype == error_mark_node)
1100 return error_mark_node;
1102 if (! MAYBE_CLASS_TYPE_P (basetype))
1103 switch (TREE_CODE (basetype))
1105 case INTEGER_TYPE:
1106 if ((desires & WANT_NULL) && null_ptr_cst_p (expr))
1107 return expr;
1108 /* else fall through... */
1110 case BOOLEAN_TYPE:
1111 return (desires & WANT_INT) ? expr : NULL_TREE;
1112 case ENUMERAL_TYPE:
1113 return (desires & WANT_ENUM) ? expr : NULL_TREE;
1114 case REAL_TYPE:
1115 return (desires & WANT_FLOAT) ? expr : NULL_TREE;
1116 case POINTER_TYPE:
1117 return (desires & WANT_POINTER) ? expr : NULL_TREE;
1119 case FUNCTION_TYPE:
1120 case ARRAY_TYPE:
1121 return (desires & WANT_POINTER) ? decay_conversion (expr)
1122 : NULL_TREE;
1124 case VECTOR_TYPE:
1125 if ((desires & WANT_VECTOR) == 0)
1126 return NULL_TREE;
1127 switch (TREE_CODE (TREE_TYPE (basetype)))
1129 case INTEGER_TYPE:
1130 case BOOLEAN_TYPE:
1131 return (desires & WANT_INT) ? expr : NULL_TREE;
1132 case ENUMERAL_TYPE:
1133 return (desires & WANT_ENUM) ? expr : NULL_TREE;
1134 case REAL_TYPE:
1135 return (desires & WANT_FLOAT) ? expr : NULL_TREE;
1136 default:
1137 return NULL_TREE;
1140 default:
1141 return NULL_TREE;
1144 /* The code for conversions from class type is currently only used for
1145 delete expressions. Other expressions are handled by build_new_op. */
1146 if (!complete_type_or_else (basetype, expr))
1147 return error_mark_node;
1148 if (!TYPE_HAS_CONVERSION (basetype))
1149 return NULL_TREE;
1151 for (conv = lookup_conversions (basetype); conv; conv = TREE_CHAIN (conv))
1153 int win = 0;
1154 tree candidate;
1155 tree cand = TREE_VALUE (conv);
1157 if (winner && winner == cand)
1158 continue;
1160 candidate = non_reference (TREE_TYPE (TREE_TYPE (cand)));
1162 switch (TREE_CODE (candidate))
1164 case BOOLEAN_TYPE:
1165 case INTEGER_TYPE:
1166 win = (desires & WANT_INT); break;
1167 case ENUMERAL_TYPE:
1168 win = (desires & WANT_ENUM); break;
1169 case REAL_TYPE:
1170 win = (desires & WANT_FLOAT); break;
1171 case POINTER_TYPE:
1172 win = (desires & WANT_POINTER); break;
1174 case VECTOR_TYPE:
1175 if ((desires & WANT_VECTOR) == 0)
1176 break;
1177 switch (TREE_CODE (TREE_TYPE (candidate)))
1179 case BOOLEAN_TYPE:
1180 case INTEGER_TYPE:
1181 win = (desires & WANT_INT); break;
1182 case ENUMERAL_TYPE:
1183 win = (desires & WANT_ENUM); break;
1184 case REAL_TYPE:
1185 win = (desires & WANT_FLOAT); break;
1186 default:
1187 break;
1189 break;
1191 default:
1192 break;
1195 if (win)
1197 if (winner)
1199 if (complain)
1201 error ("ambiguous default type conversion from %qT",
1202 basetype);
1203 error (" candidate conversions include %qD and %qD",
1204 winner, cand);
1206 return error_mark_node;
1208 else
1209 winner = cand;
1213 if (winner)
1215 tree type = non_reference (TREE_TYPE (TREE_TYPE (winner)));
1216 return build_user_type_conversion (type, expr, LOOKUP_NORMAL);
1219 return NULL_TREE;
1222 /* Implements integral promotion (4.1) and float->double promotion. */
1224 tree
1225 type_promotes_to (tree type)
1227 if (type == error_mark_node)
1228 return error_mark_node;
1230 type = TYPE_MAIN_VARIANT (type);
1232 /* bool always promotes to int (not unsigned), even if it's the same
1233 size. */
1234 if (type == boolean_type_node)
1235 type = integer_type_node;
1237 /* Normally convert enums to int, but convert wide enums to something
1238 wider. */
1239 else if (TREE_CODE (type) == ENUMERAL_TYPE
1240 || type == char16_type_node
1241 || type == char32_type_node
1242 || type == wchar_type_node)
1244 int precision = MAX (TYPE_PRECISION (type),
1245 TYPE_PRECISION (integer_type_node));
1246 tree totype = c_common_type_for_size (precision, 0);
1247 if (TYPE_UNSIGNED (type)
1248 && ! int_fits_type_p (TYPE_MAX_VALUE (type), totype))
1249 type = c_common_type_for_size (precision, 1);
1250 else
1251 type = totype;
1253 else if (c_promoting_integer_type_p (type))
1255 /* Retain unsignedness if really not getting bigger. */
1256 if (TYPE_UNSIGNED (type)
1257 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
1258 type = unsigned_type_node;
1259 else
1260 type = integer_type_node;
1262 else if (type == float_type_node)
1263 type = double_type_node;
1265 return type;
1268 /* The routines below this point are carefully written to conform to
1269 the standard. They use the same terminology, and follow the rules
1270 closely. Although they are used only in pt.c at the moment, they
1271 should presumably be used everywhere in the future. */
1273 /* Attempt to perform qualification conversions on EXPR to convert it
1274 to TYPE. Return the resulting expression, or error_mark_node if
1275 the conversion was impossible. */
1277 tree
1278 perform_qualification_conversions (tree type, tree expr)
1280 tree expr_type;
1282 expr_type = TREE_TYPE (expr);
1284 if (same_type_p (type, expr_type))
1285 return expr;
1286 else if (TYPE_PTR_P (type) && TYPE_PTR_P (expr_type)
1287 && comp_ptr_ttypes (TREE_TYPE (type), TREE_TYPE (expr_type)))
1288 return build_nop (type, expr);
1289 else if (TYPE_PTR_TO_MEMBER_P (type)
1290 && TYPE_PTR_TO_MEMBER_P (expr_type)
1291 && same_type_p (TYPE_PTRMEM_CLASS_TYPE (type),
1292 TYPE_PTRMEM_CLASS_TYPE (expr_type))
1293 && comp_ptr_ttypes (TYPE_PTRMEM_POINTED_TO_TYPE (type),
1294 TYPE_PTRMEM_POINTED_TO_TYPE (expr_type)))
1295 return build_nop (type, expr);
1296 else
1297 return error_mark_node;