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 Free Software Foundation, Inc.
4 Hacked by Michael Tiemann (tiemann@cygnus.com)
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING. If not, write to
20 the Free Software Foundation, 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
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. */
31 #include "coretypes.h"
40 static tree cp_convert_to_pointer (tree, tree, bool);
41 static tree convert_to_pointer_force (tree, tree);
42 static tree build_up_reference (tree, tree, int, tree);
43 static void warn_ref_binding (tree, tree, tree);
45 /* Change of width--truncation and extension of integers or reals--
46 is represented with NOP_EXPR. Proper functioning of many things
47 assumes that no other conversions can be NOP_EXPRs.
49 Conversion between integer and pointer is represented with CONVERT_EXPR.
50 Converting integer to real uses FLOAT_EXPR
51 and real to integer uses FIX_TRUNC_EXPR.
53 Here is a list of all the functions that assume that widening and
54 narrowing is always done with a NOP_EXPR:
55 In convert.c, convert_to_integer.
56 In c-typeck.c, build_binary_op_nodefault (boolean ops),
57 and c_common_truthvalue_conversion.
58 In expr.c: expand_expr, for operands of a MULT_EXPR.
59 In fold-const.c: fold.
60 In tree.c: get_narrower and get_unwidened.
62 C++: in multiple-inheritance, converting between pointers may involve
63 adjusting them by a delta stored within the class definition. */
65 /* Subroutines of `convert'. */
67 /* if converting pointer to pointer
68 if dealing with classes, check for derived->base or vice versa
69 else if dealing with method pointers, delegate
71 else if converting class, pass off to build_type_conversion
72 else try C-style pointer conversion. If FORCE is true then allow
73 conversions via virtual bases (these are permitted by reinterpret_cast,
74 but not static_cast). */
77 cp_convert_to_pointer (tree type, tree expr, bool force)
79 tree intype = TREE_TYPE (expr);
82 if (intype == error_mark_node)
83 return error_mark_node;
85 if (IS_AGGR_TYPE (intype))
87 intype = complete_type (intype);
88 if (!COMPLETE_TYPE_P (intype))
90 error ("can't convert from incomplete type `%T' to `%T'",
92 return error_mark_node;
95 rval = build_type_conversion (type, expr);
98 if (rval == error_mark_node)
99 error ("conversion of `%E' from `%T' to `%T' is ambiguous",
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 && IS_AGGR_TYPE (TREE_TYPE (type))
131 && IS_AGGR_TYPE (TREE_TYPE (intype))
132 && TREE_CODE (TREE_TYPE (intype)) == RECORD_TYPE)
134 enum tree_code code = PLUS_EXPR;
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));
146 /* Try derived to base conversion. */
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);
155 if (binfo == error_mark_node)
156 return error_mark_node;
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 `%E' from type `%T' to type `%T'",
170 return error_mark_node;
173 return build_nop (type, expr);
175 else if (TYPE_PTRMEM_P (type) && TYPE_PTRMEM_P (intype))
180 enum tree_code code = PLUS_EXPR;
183 b1 = TYPE_PTRMEM_CLASS_TYPE (type);
184 b2 = TYPE_PTRMEM_CLASS_TYPE (intype);
185 binfo = lookup_base (b1, b2, ba_check, &bk);
188 binfo = lookup_base (b2, b1, ba_check, &bk);
191 if (binfo == error_mark_node)
192 return error_mark_node;
194 if (bk == bk_via_virtual)
197 warning ("pointer to member cast from `%T' to `%T' is via virtual base",
201 error ("pointer to member cast from `%T' to `%T' is via virtual base",
203 return error_mark_node;
205 /* This is a reinterpret cast, whose result is unspecified.
206 We choose to do nothing. */
207 return build1 (NOP_EXPR, type, expr);
210 if (TREE_CODE (expr) == PTRMEM_CST)
211 expr = cplus_expand_constant (expr);
213 if (binfo && !integer_zerop (BINFO_OFFSET (binfo)))
214 expr = size_binop (code,
215 build_nop (sizetype, expr),
216 BINFO_OFFSET (binfo));
217 return build_nop (type, expr);
219 else if (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype))
220 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), expr, 0);
221 else if (TYPE_PTRMEMFUNC_P (intype))
225 if (TREE_CODE (expr) == PTRMEM_CST)
226 return cp_convert_to_pointer (type,
227 PTRMEM_CST_MEMBER (expr),
229 else if (TREE_CODE (expr) == OFFSET_REF)
231 tree object = TREE_OPERAND (expr, 0);
232 return get_member_function_from_ptrfunc (&object,
233 TREE_OPERAND (expr, 1));
236 error ("cannot convert `%E' from type `%T' to type `%T'",
238 return error_mark_node;
241 if (integer_zerop (expr))
243 if (TYPE_PTRMEMFUNC_P (type))
244 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), expr, 0);
246 if (TYPE_PTRMEM_P (type))
248 /* A NULL pointer-to-member is represented by -1, not by
250 expr = build_int_cst (type, -1);
251 /* Fix up the representation of -1 if appropriate. */
252 expr = force_fit_type (expr, 0, false, false);
255 expr = build_int_cst (type, 0);
259 else if (TYPE_PTR_TO_MEMBER_P (type) && INTEGRAL_CODE_P (form))
261 error ("invalid conversion from '%T' to '%T'", intype, type);
262 return error_mark_node;
265 if (INTEGRAL_CODE_P (form))
267 if (TYPE_PRECISION (intype) == POINTER_SIZE)
268 return build1 (CONVERT_EXPR, type, expr);
269 expr = cp_convert (c_common_type_for_size (POINTER_SIZE, 0), expr);
270 /* Modes may be different but sizes should be the same. There
271 is supposed to be some integral type that is the same width
273 gcc_assert (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (expr)))
274 == GET_MODE_SIZE (TYPE_MODE (type)));
276 return convert_to_pointer (type, expr);
279 if (type_unknown_p (expr))
280 return instantiate_type (type, expr, tf_error | tf_warning);
282 error ("cannot convert `%E' from type `%T' to type `%T'",
284 return error_mark_node;
287 /* Like convert, except permit conversions to take place which
288 are not normally allowed due to access restrictions
289 (such as conversion from sub-type to private super-type). */
292 convert_to_pointer_force (tree type, tree expr)
294 tree intype = TREE_TYPE (expr);
295 enum tree_code form = TREE_CODE (intype);
297 if (form == POINTER_TYPE)
299 intype = TYPE_MAIN_VARIANT (intype);
301 if (TYPE_MAIN_VARIANT (type) != intype
302 && TREE_CODE (TREE_TYPE (type)) == RECORD_TYPE
303 && IS_AGGR_TYPE (TREE_TYPE (type))
304 && IS_AGGR_TYPE (TREE_TYPE (intype))
305 && TREE_CODE (TREE_TYPE (intype)) == RECORD_TYPE)
307 enum tree_code code = PLUS_EXPR;
310 binfo = lookup_base (TREE_TYPE (intype), TREE_TYPE (type),
314 binfo = lookup_base (TREE_TYPE (type), TREE_TYPE (intype),
318 if (binfo == error_mark_node)
319 return error_mark_node;
322 expr = build_base_path (code, expr, binfo, 0);
323 if (expr == error_mark_node)
324 return error_mark_node;
325 /* Add any qualifier conversions. */
326 if (!same_type_p (TREE_TYPE (TREE_TYPE (expr)),
328 expr = build_nop (type, expr);
334 return cp_convert_to_pointer (type, expr, true);
337 /* We are passing something to a function which requires a reference.
338 The type we are interested in is in TYPE. The initial
339 value we have to begin with is in ARG.
341 FLAGS controls how we manage access checking.
342 DIRECT_BIND in FLAGS controls how any temporaries are generated.
343 If DIRECT_BIND is set, DECL is the reference we're binding to. */
346 build_up_reference (tree type, tree arg, int flags, tree decl)
349 tree argtype = TREE_TYPE (arg);
350 tree target_type = TREE_TYPE (type);
352 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
354 if ((flags & DIRECT_BIND) && ! real_lvalue_p (arg))
356 /* Create a new temporary variable. We can't just use a TARGET_EXPR
357 here because it needs to live as long as DECL. */
360 arg = make_temporary_var_for_ref_to_temp (decl, TREE_TYPE (arg));
362 /* Process the initializer for the declaration. */
363 DECL_INITIAL (arg) = targ;
364 cp_finish_decl (arg, targ, NULL_TREE,
365 LOOKUP_ONLYCONVERTING|DIRECT_BIND);
367 else if (!(flags & DIRECT_BIND) && ! lvalue_p (arg))
368 return get_target_expr (arg);
370 /* If we had a way to wrap this up, and say, if we ever needed its
371 address, transform all occurrences of the register, into a memory
372 reference we could win better. */
373 rval = build_unary_op (ADDR_EXPR, arg, 1);
374 if (rval == error_mark_node)
375 return error_mark_node;
377 if ((flags & LOOKUP_PROTECT)
378 && TYPE_MAIN_VARIANT (argtype) != TYPE_MAIN_VARIANT (target_type)
379 && IS_AGGR_TYPE (argtype)
380 && IS_AGGR_TYPE (target_type))
382 /* We go through lookup_base for the access control. */
383 tree binfo = lookup_base (argtype, target_type, ba_check, NULL);
384 if (binfo == error_mark_node)
385 return error_mark_node;
386 if (binfo == NULL_TREE)
387 return error_not_base_type (target_type, argtype);
388 rval = build_base_path (PLUS_EXPR, rval, binfo, 1);
392 = convert_to_pointer_force (build_pointer_type (target_type), rval);
393 return build_nop (type, rval);
396 /* Subroutine of convert_to_reference. REFTYPE is the target reference type.
397 INTYPE is the original rvalue type and DECL is an optional _DECL node
400 [dcl.init.ref] says that if an rvalue is used to
401 initialize a reference, then the reference must be to a
402 non-volatile const type. */
405 warn_ref_binding (tree reftype, tree intype, tree decl)
407 tree ttl = TREE_TYPE (reftype);
409 if (!CP_TYPE_CONST_NON_VOLATILE_P (ttl))
413 if (CP_TYPE_VOLATILE_P (ttl) && decl)
414 msg = "initialization of volatile reference type `%#T' from rvalue of type `%T'";
415 else if (CP_TYPE_VOLATILE_P (ttl))
416 msg = "conversion to volatile reference type `%#T' from rvalue of type `%T'";
418 msg = "initialization of non-const reference type `%#T' from rvalue of type `%T'";
420 msg = "conversion to non-const reference type `%#T' from rvalue of type `%T'";
422 pedwarn (msg, reftype, intype);
426 /* For C++: Only need to do one-level references, but cannot
427 get tripped up on signed/unsigned differences.
429 DECL is either NULL_TREE or the _DECL node for a reference that is being
430 initialized. It can be error_mark_node if we don't know the _DECL but
431 we know it's an initialization. */
434 convert_to_reference (tree reftype, tree expr, int convtype,
435 int flags, tree decl)
437 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (reftype));
439 tree rval = NULL_TREE;
440 tree rval_as_conversion = NULL_TREE;
441 bool can_convert_intype_to_type;
443 if (TREE_CODE (type) == FUNCTION_TYPE
444 && TREE_TYPE (expr) == unknown_type_node)
445 expr = instantiate_type (type, expr,
446 (flags & LOOKUP_COMPLAIN)
447 ? tf_error | tf_warning : tf_none);
449 expr = convert_from_reference (expr);
451 if (expr == error_mark_node)
452 return error_mark_node;
454 intype = TREE_TYPE (expr);
456 gcc_assert (TREE_CODE (intype) != REFERENCE_TYPE);
458 intype = TYPE_MAIN_VARIANT (intype);
460 can_convert_intype_to_type = can_convert (type, intype);
461 if (!can_convert_intype_to_type
462 && (convtype & CONV_IMPLICIT) && IS_AGGR_TYPE (intype)
463 && ! (flags & LOOKUP_NO_CONVERSION))
465 /* Look for a user-defined conversion to lvalue that we can use. */
468 = build_type_conversion (reftype, expr);
470 if (rval_as_conversion && rval_as_conversion != error_mark_node
471 && real_lvalue_p (rval_as_conversion))
473 expr = rval_as_conversion;
474 rval_as_conversion = NULL_TREE;
476 can_convert_intype_to_type = 1;
480 if (((convtype & CONV_STATIC) && can_convert (intype, type))
481 || ((convtype & CONV_IMPLICIT) && can_convert_intype_to_type))
483 if (flags & LOOKUP_COMPLAIN)
485 tree ttl = TREE_TYPE (reftype);
486 tree ttr = lvalue_type (expr);
488 if (! real_lvalue_p (expr))
489 warn_ref_binding (reftype, intype, decl);
491 if (! (convtype & CONV_CONST)
492 && !at_least_as_qualified_p (ttl, ttr))
493 pedwarn ("conversion from `%T' to `%T' discards qualifiers",
497 return build_up_reference (reftype, expr, flags, decl);
499 else if ((convtype & CONV_REINTERPRET) && lvalue_p (expr))
501 /* When casting an lvalue to a reference type, just convert into
502 a pointer to the new type and deference it. This is allowed
503 by San Diego WP section 5.2.9 paragraph 12, though perhaps it
504 should be done directly (jason). (int &)ri ---> *(int*)&ri */
506 /* B* bp; A& ar = (A&)bp; is valid, but it's probably not what they
508 if (TREE_CODE (intype) == POINTER_TYPE
509 && (comptypes (TREE_TYPE (intype), type,
510 COMPARE_BASE | COMPARE_DERIVED)))
511 warning ("casting `%T' to `%T' does not dereference pointer",
514 rval = build_unary_op (ADDR_EXPR, expr, 0);
515 if (rval != error_mark_node)
516 rval = convert_force (build_pointer_type (TREE_TYPE (reftype)),
518 if (rval != error_mark_node)
519 rval = build1 (NOP_EXPR, reftype, rval);
523 rval = convert_for_initialization (NULL_TREE, type, expr, flags,
525 if (rval == NULL_TREE || rval == error_mark_node)
527 warn_ref_binding (reftype, intype, decl);
528 rval = build_up_reference (reftype, rval, flags, decl);
533 /* If we found a way to convert earlier, then use it. */
537 if (flags & LOOKUP_COMPLAIN)
538 error ("cannot convert type `%T' to type `%T'", intype, reftype);
540 return error_mark_node;
543 /* We are using a reference VAL for its value. Bash that reference all the
544 way down to its lowest form. */
547 convert_from_reference (tree val)
549 if (TREE_CODE (TREE_TYPE (val)) == REFERENCE_TYPE)
550 return build_indirect_ref (val, NULL);
554 /* Implicitly convert the lvalue EXPR to another lvalue of type TOTYPE,
555 preserving cv-qualification. */
558 convert_lvalue (tree totype, tree expr)
560 totype = cp_build_qualified_type (totype, TYPE_QUALS (TREE_TYPE (expr)));
561 totype = build_reference_type (totype);
562 expr = convert_to_reference (totype, expr, CONV_IMPLICIT, LOOKUP_NORMAL,
564 return convert_from_reference (expr);
567 /* Really perform an lvalue-to-rvalue conversion, including copying an
568 argument of class type into a temporary. */
571 force_rvalue (tree expr)
573 if (IS_AGGR_TYPE (TREE_TYPE (expr)) && TREE_CODE (expr) != TARGET_EXPR)
574 expr = ocp_convert (TREE_TYPE (expr), expr,
575 CONV_IMPLICIT|CONV_FORCE_TEMP, LOOKUP_NORMAL);
577 expr = decay_conversion (expr);
582 /* C++ conversions, preference to static cast conversions. */
585 cp_convert (tree type, tree expr)
587 return ocp_convert (type, expr, CONV_OLD_CONVERT, LOOKUP_NORMAL);
592 FLAGS indicates how we should behave. */
595 ocp_convert (tree type, tree expr, int convtype, int flags)
598 enum tree_code code = TREE_CODE (type);
600 if (error_operand_p (e) || type == error_mark_node)
601 return error_mark_node;
603 complete_type (type);
604 complete_type (TREE_TYPE (expr));
606 e = decl_constant_value (e);
608 if (IS_AGGR_TYPE (type) && (convtype & CONV_FORCE_TEMP)
609 /* Some internal structures (vtable_entry_type, sigtbl_ptr_type)
610 don't go through finish_struct, so they don't have the synthesized
611 constructors. So don't force a temporary. */
612 && TYPE_HAS_CONSTRUCTOR (type))
613 /* We need a new temporary; don't take this shortcut. */;
614 else if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (TREE_TYPE (e)))
616 if (same_type_p (type, TREE_TYPE (e)))
617 /* The call to fold will not always remove the NOP_EXPR as
618 might be expected, since if one of the types is a typedef;
619 the comparison in fold is just equality of pointers, not a
620 call to comptypes. We don't call fold in this case because
621 that can result in infinite recursion; fold will call
622 convert, which will call ocp_convert, etc. */
624 /* For complex data types, we need to perform componentwise
626 else if (TREE_CODE (type) == COMPLEX_TYPE)
627 return fold_if_not_in_template (convert_to_complex (type, e));
628 else if (TREE_CODE (e) == TARGET_EXPR)
630 /* Don't build a NOP_EXPR of class type. Instead, change the
631 type of the temporary. Only allow this for cv-qual changes,
633 gcc_assert (same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (e)),
634 TYPE_MAIN_VARIANT (type)));
635 TREE_TYPE (e) = TREE_TYPE (TARGET_EXPR_SLOT (e)) = type;
640 /* We shouldn't be treating objects of ADDRESSABLE type as
642 gcc_assert (!TREE_ADDRESSABLE (type));
643 return fold_if_not_in_template (build_nop (type, e));
647 if (code == VOID_TYPE && (convtype & CONV_STATIC))
649 e = convert_to_void (e, /*implicit=*/NULL);
653 if (INTEGRAL_CODE_P (code))
655 tree intype = TREE_TYPE (e);
656 /* enum = enum, enum = int, enum = float, (enum)pointer are all
658 if (TREE_CODE (type) == ENUMERAL_TYPE
659 && ((ARITHMETIC_TYPE_P (intype) && ! (convtype & CONV_STATIC))
660 || (TREE_CODE (intype) == POINTER_TYPE)))
662 pedwarn ("conversion from `%#T' to `%#T'", intype, type);
664 if (flag_pedantic_errors)
665 return error_mark_node;
667 if (IS_AGGR_TYPE (intype))
670 rval = build_type_conversion (type, e);
673 if (flags & LOOKUP_COMPLAIN)
674 error ("`%#T' used where a `%T' was expected", intype, type);
675 return error_mark_node;
677 if (code == BOOLEAN_TYPE)
678 return cp_truthvalue_conversion (e);
680 return fold_if_not_in_template (convert_to_integer (type, e));
682 if (POINTER_TYPE_P (type) || TYPE_PTR_TO_MEMBER_P (type))
683 return fold_if_not_in_template (cp_convert_to_pointer (type, e, false));
684 if (code == VECTOR_TYPE)
686 tree in_vtype = TREE_TYPE (e);
687 if (IS_AGGR_TYPE (in_vtype))
690 ret_val = build_type_conversion (type, e);
693 if (flags & LOOKUP_COMPLAIN)
694 error ("`%#T' used where a `%T' was expected", in_vtype, type);
695 return error_mark_node;
697 return fold_if_not_in_template (convert_to_vector (type, e));
699 if (code == REAL_TYPE || code == COMPLEX_TYPE)
701 if (IS_AGGR_TYPE (TREE_TYPE (e)))
704 rval = build_type_conversion (type, e);
708 if (flags & LOOKUP_COMPLAIN)
709 error ("`%#T' used where a floating point value was expected",
712 if (code == REAL_TYPE)
713 return fold_if_not_in_template (convert_to_real (type, e));
714 else if (code == COMPLEX_TYPE)
715 return fold_if_not_in_template (convert_to_complex (type, e));
718 /* New C++ semantics: since assignment is now based on
719 memberwise copying, if the rhs type is derived from the
720 lhs type, then we may still do a conversion. */
721 if (IS_AGGR_TYPE_CODE (code))
723 tree dtype = TREE_TYPE (e);
724 tree ctor = NULL_TREE;
726 dtype = TYPE_MAIN_VARIANT (dtype);
728 /* Conversion between aggregate types. New C++ semantics allow
729 objects of derived type to be cast to objects of base type.
730 Old semantics only allowed this between pointers.
732 There may be some ambiguity between using a constructor
733 vs. using a type conversion operator when both apply. */
737 if (abstract_virtuals_error (NULL_TREE, type))
738 return error_mark_node;
740 if ((flags & LOOKUP_ONLYCONVERTING)
741 && ! (IS_AGGR_TYPE (dtype) && DERIVED_FROM_P (type, dtype)))
742 /* For copy-initialization, first we create a temp of the proper type
743 with a user-defined conversion sequence, then we direct-initialize
744 the target with the temp (see [dcl.init]). */
745 ctor = build_user_type_conversion (type, ctor, flags);
747 ctor = build_special_member_call (NULL_TREE,
748 complete_ctor_identifier,
749 build_tree_list (NULL_TREE, ctor),
752 return build_cplus_new (type, ctor);
755 if (flags & LOOKUP_COMPLAIN)
756 error ("conversion from `%T' to non-scalar type `%T' requested",
757 TREE_TYPE (expr), type);
758 return error_mark_node;
761 /* When an expression is used in a void context, its value is discarded and
762 no lvalue-rvalue and similar conversions happen [expr.static.cast/4,
763 stmt.expr/1, expr.comma/1]. This permits dereferencing an incomplete type
764 in a void context. The C++ standard does not define what an `access' to an
765 object is, but there is reason to believe that it is the lvalue to rvalue
766 conversion -- if it were not, `*&*p = 1' would violate [expr]/4 in that it
767 accesses `*p' not to calculate the value to be stored. But, dcl.type.cv/8
768 indicates that volatile semantics should be the same between C and C++
769 where ever possible. C leaves it implementation defined as to what
770 constitutes an access to a volatile. So, we interpret `*vp' as a read of
771 the volatile object `vp' points to, unless that is an incomplete type. For
772 volatile references we do not do this interpretation, because that would
773 make it impossible to ignore the reference return value from functions. We
774 issue warnings in the confusing cases.
776 IMPLICIT is tells us the context of an implicit void conversion. */
779 convert_to_void (tree expr, const char *implicit)
781 if (expr == error_mark_node
782 || TREE_TYPE (expr) == error_mark_node)
783 return error_mark_node;
784 if (!TREE_TYPE (expr))
786 if (invalid_nonstatic_memfn_p (expr))
787 return error_mark_node;
788 if (VOID_TYPE_P (TREE_TYPE (expr)))
790 switch (TREE_CODE (expr))
794 /* The two parts of a cond expr might be separate lvalues. */
795 tree op1 = TREE_OPERAND (expr,1);
796 tree op2 = TREE_OPERAND (expr,2);
797 tree new_op1 = convert_to_void
798 (op1, (implicit && !TREE_SIDE_EFFECTS (op2)
799 ? "second operand of conditional" : NULL));
800 tree new_op2 = convert_to_void
801 (op2, (implicit && !TREE_SIDE_EFFECTS (op1)
802 ? "third operand of conditional" : NULL));
804 expr = build3 (COND_EXPR, TREE_TYPE (new_op1),
805 TREE_OPERAND (expr, 0), new_op1, new_op2);
811 /* The second part of a compound expr contains the value. */
812 tree op1 = TREE_OPERAND (expr,1);
813 tree new_op1 = convert_to_void
814 (op1, (implicit && !TREE_NO_WARNING (expr)
815 ? "right-hand operand of comma" : NULL));
819 tree t = build2 (COMPOUND_EXPR, TREE_TYPE (new_op1),
820 TREE_OPERAND (expr, 0), new_op1);
827 case NON_LVALUE_EXPR:
829 /* These have already decayed to rvalue. */
832 case CALL_EXPR: /* We have a special meaning for volatile void fn(). */
837 tree type = TREE_TYPE (expr);
838 int is_reference = TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0)))
840 int is_volatile = TYPE_VOLATILE (type);
841 int is_complete = COMPLETE_TYPE_P (complete_type (type));
843 if (is_volatile && !is_complete)
844 warning ("object of incomplete type `%T' will not be accessed in %s",
845 type, implicit ? implicit : "void context");
846 else if (is_reference && is_volatile)
847 warning ("object of type `%T' will not be accessed in %s",
848 TREE_TYPE (TREE_OPERAND (expr, 0)),
849 implicit ? implicit : "void context");
850 if (is_reference || !is_volatile || !is_complete)
851 expr = TREE_OPERAND (expr, 0);
858 /* External variables might be incomplete. */
859 tree type = TREE_TYPE (expr);
860 int is_complete = COMPLETE_TYPE_P (complete_type (type));
862 if (TYPE_VOLATILE (type) && !is_complete)
863 warning ("object `%E' of incomplete type `%T' will not be accessed in %s",
864 expr, type, implicit ? implicit : "void context");
873 if (TREE_CODE (probe) == ADDR_EXPR)
874 probe = TREE_OPERAND (expr, 0);
875 if (type_unknown_p (probe))
877 /* [over.over] enumerates the places where we can take the address
878 of an overloaded function, and this is not one of them. */
879 pedwarn ("%s cannot resolve address of overloaded function",
880 implicit ? implicit : "void cast");
881 expr = void_zero_node;
883 else if (implicit && probe == expr && is_overloaded_fn (probe))
884 /* Only warn when there is no &. */
885 warning ("%s is a reference, not call, to function `%E'",
889 if (expr != error_mark_node && !VOID_TYPE_P (TREE_TYPE (expr)))
891 if (implicit && warn_unused_value
892 && !TREE_SIDE_EFFECTS (expr) && !TREE_NO_WARNING (expr))
893 warning ("%s has no effect", implicit);
894 expr = build1 (CONVERT_EXPR, void_type_node, expr);
899 /* Create an expression whose value is that of EXPR,
900 converted to type TYPE. The TREE_TYPE of the value
901 is always TYPE. This function implements all reasonable
902 conversions; callers should filter out those that are
903 not permitted by the language being compiled.
905 Most of this routine is from build_reinterpret_cast.
907 The backend cannot call cp_convert (what was convert) because
908 conversions to/from basetypes may involve memory references
909 (vbases) and adding or subtracting small values (multiple
910 inheritance), but it calls convert from the constant folding code
911 on subtrees of already built trees after it has ripped them apart.
913 Also, if we ever support range variables, we'll probably also have to
914 do a little bit more work. */
917 convert (tree type, tree expr)
921 if (type == error_mark_node || expr == error_mark_node)
922 return error_mark_node;
924 intype = TREE_TYPE (expr);
926 if (POINTER_TYPE_P (type) && POINTER_TYPE_P (intype))
928 expr = decl_constant_value (expr);
929 return fold_if_not_in_template (build_nop (type, expr));
932 return ocp_convert (type, expr, CONV_OLD_CONVERT,
933 LOOKUP_NORMAL|LOOKUP_NO_CONVERSION);
936 /* Like cp_convert, except permit conversions to take place which
937 are not normally allowed due to access restrictions
938 (such as conversion from sub-type to private super-type). */
941 convert_force (tree type, tree expr, int convtype)
944 enum tree_code code = TREE_CODE (type);
946 if (code == REFERENCE_TYPE)
947 return (fold_if_not_in_template
948 (convert_to_reference (type, e, CONV_C_CAST, LOOKUP_COMPLAIN,
950 else if (TREE_CODE (TREE_TYPE (e)) == REFERENCE_TYPE)
951 e = convert_from_reference (e);
953 if (code == POINTER_TYPE)
954 return fold_if_not_in_template (convert_to_pointer_force (type, e));
956 /* From typeck.c convert_for_assignment */
957 if (((TREE_CODE (TREE_TYPE (e)) == POINTER_TYPE && TREE_CODE (e) == ADDR_EXPR
958 && TREE_CODE (TREE_TYPE (e)) == POINTER_TYPE
959 && TREE_CODE (TREE_TYPE (TREE_TYPE (e))) == METHOD_TYPE)
961 || TYPE_PTRMEMFUNC_P (TREE_TYPE (e)))
962 && TYPE_PTRMEMFUNC_P (type))
964 /* compatible pointer to member functions. */
965 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), e, 1);
968 return ocp_convert (type, e, CONV_C_CAST|convtype, LOOKUP_NORMAL);
971 /* Convert an aggregate EXPR to type XTYPE. If a conversion
972 exists, return the attempted conversion. This may
973 return ERROR_MARK_NODE if the conversion is not
974 allowed (references private members, etc).
975 If no conversion exists, NULL_TREE is returned.
977 FIXME: Ambiguity checking is wrong. Should choose one by the implicit
978 object parameter, or by the second standard conversion sequence if
979 that doesn't do it. This will probably wait for an overloading rewrite.
983 build_type_conversion (tree xtype, tree expr)
985 /* C++: check to see if we can convert this aggregate type
986 into the required type. */
987 return build_user_type_conversion (xtype, expr, LOOKUP_NORMAL);
990 /* Convert the given EXPR to one of a group of types suitable for use in an
991 expression. DESIRES is a combination of various WANT_* flags (q.v.)
992 which indicates which types are suitable. If COMPLAIN is true, complain
993 about ambiguity; otherwise, the caller will deal with it. */
996 build_expr_type_conversion (int desires, tree expr, bool complain)
998 tree basetype = TREE_TYPE (expr);
999 tree conv = NULL_TREE;
1000 tree winner = NULL_TREE;
1002 if (expr == null_node
1003 && (desires & WANT_INT)
1004 && !(desires & WANT_NULL))
1005 warning ("converting NULL to non-pointer type");
1007 expr = convert_from_reference (expr);
1008 basetype = TREE_TYPE (expr);
1010 if (basetype == error_mark_node)
1011 return error_mark_node;
1013 if (! IS_AGGR_TYPE (basetype))
1014 switch (TREE_CODE (basetype))
1017 if ((desires & WANT_NULL) && null_ptr_cst_p (expr))
1019 /* else fall through... */
1022 return (desires & WANT_INT) ? expr : NULL_TREE;
1024 return (desires & WANT_ENUM) ? expr : NULL_TREE;
1026 return (desires & WANT_FLOAT) ? expr : NULL_TREE;
1028 return (desires & WANT_POINTER) ? expr : NULL_TREE;
1032 return (desires & WANT_POINTER) ? decay_conversion (expr)
1038 /* The code for conversions from class type is currently only used for
1039 delete expressions. Other expressions are handled by build_new_op. */
1040 if (!complete_type_or_else (basetype, expr))
1041 return error_mark_node;
1042 if (!TYPE_HAS_CONVERSION (basetype))
1045 for (conv = lookup_conversions (basetype); conv; conv = TREE_CHAIN (conv))
1049 tree cand = TREE_VALUE (conv);
1051 if (winner && winner == cand)
1054 candidate = non_reference (TREE_TYPE (TREE_TYPE (cand)));
1056 switch (TREE_CODE (candidate))
1060 win = (desires & WANT_INT); break;
1062 win = (desires & WANT_ENUM); break;
1064 win = (desires & WANT_FLOAT); break;
1066 win = (desires & WANT_POINTER); break;
1078 error ("ambiguous default type conversion from `%T'",
1080 error (" candidate conversions include `%D' and `%D'",
1083 return error_mark_node;
1092 tree type = non_reference (TREE_TYPE (TREE_TYPE (winner)));
1093 return build_user_type_conversion (type, expr, LOOKUP_NORMAL);
1099 /* Implements integral promotion (4.1) and float->double promotion. */
1102 type_promotes_to (tree type)
1104 if (type == error_mark_node)
1105 return error_mark_node;
1107 type = TYPE_MAIN_VARIANT (type);
1109 /* bool always promotes to int (not unsigned), even if it's the same
1111 if (type == boolean_type_node)
1112 type = integer_type_node;
1114 /* Normally convert enums to int, but convert wide enums to something
1116 else if (TREE_CODE (type) == ENUMERAL_TYPE
1117 || type == wchar_type_node)
1119 int precision = MAX (TYPE_PRECISION (type),
1120 TYPE_PRECISION (integer_type_node));
1121 tree totype = c_common_type_for_size (precision, 0);
1122 if (TYPE_UNSIGNED (type)
1123 && ! int_fits_type_p (TYPE_MAX_VALUE (type), totype))
1124 type = c_common_type_for_size (precision, 1);
1128 else if (c_promoting_integer_type_p (type))
1130 /* Retain unsignedness if really not getting bigger. */
1131 if (TYPE_UNSIGNED (type)
1132 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
1133 type = unsigned_type_node;
1135 type = integer_type_node;
1137 else if (type == float_type_node)
1138 type = double_type_node;
1143 /* The routines below this point are carefully written to conform to
1144 the standard. They use the same terminology, and follow the rules
1145 closely. Although they are used only in pt.c at the moment, they
1146 should presumably be used everywhere in the future. */
1148 /* Attempt to perform qualification conversions on EXPR to convert it
1149 to TYPE. Return the resulting expression, or error_mark_node if
1150 the conversion was impossible. */
1153 perform_qualification_conversions (tree type, tree expr)
1157 expr_type = TREE_TYPE (expr);
1159 if (TYPE_PTR_P (type) && TYPE_PTR_P (expr_type)
1160 && comp_ptr_ttypes (TREE_TYPE (type), TREE_TYPE (expr_type)))
1161 return build_nop (type, expr);
1162 else if (TYPE_PTR_TO_MEMBER_P (type)
1163 && TYPE_PTR_TO_MEMBER_P (expr_type)
1164 && same_type_p (TYPE_PTRMEM_CLASS_TYPE (type),
1165 TYPE_PTRMEM_CLASS_TYPE (expr_type))
1166 && comp_ptr_ttypes (TYPE_PTRMEM_POINTED_TO_TYPE (type),
1167 TYPE_PTRMEM_POINTED_TO_TYPE (expr_type)))
1168 return build_nop (type, expr);
1170 return error_mark_node;