Sync usage with man page.
[netbsd-mini2440.git] / gnu / dist / gcc4 / gcc / cp / cvt.c
blob84e8a6b404fb4dd76ae167a20c342203a918c5f9
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 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)
11 any later version.
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, 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, 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. */
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, bool);
42 static tree convert_to_pointer_force (tree, tree);
43 static tree build_up_reference (tree, tree, int, tree);
44 static void warn_ref_binding (tree, tree, tree);
46 /* Change of width--truncation and extension of integers or reals--
47 is represented with NOP_EXPR. Proper functioning of many things
48 assumes that no other conversions can be NOP_EXPRs.
50 Conversion between integer and pointer is represented with CONVERT_EXPR.
51 Converting integer to real uses FLOAT_EXPR
52 and real to integer uses FIX_TRUNC_EXPR.
54 Here is a list of all the functions that assume that widening and
55 narrowing is always done with a NOP_EXPR:
56 In convert.c, convert_to_integer.
57 In c-typeck.c, build_binary_op_nodefault (boolean ops),
58 and c_common_truthvalue_conversion.
59 In expr.c: expand_expr, for operands of a MULT_EXPR.
60 In fold-const.c: fold.
61 In tree.c: get_narrower and get_unwidened.
63 C++: in multiple-inheritance, converting between pointers may involve
64 adjusting them by a delta stored within the class definition. */
66 /* Subroutines of `convert'. */
68 /* if converting pointer to pointer
69 if dealing with classes, check for derived->base or vice versa
70 else if dealing with method pointers, delegate
71 else convert blindly
72 else if converting class, pass off to build_type_conversion
73 else try C-style pointer conversion. If FORCE is true then allow
74 conversions via virtual bases (these are permitted by reinterpret_cast,
75 but not static_cast). */
77 static tree
78 cp_convert_to_pointer (tree type, tree expr, bool force)
80 tree intype = TREE_TYPE (expr);
81 enum tree_code form;
82 tree rval;
83 if (intype == error_mark_node)
84 return error_mark_node;
86 if (IS_AGGR_TYPE (intype))
88 intype = complete_type (intype);
89 if (!COMPLETE_TYPE_P (intype))
91 error ("can't convert from incomplete type %qT to %qT",
92 intype, type);
93 return error_mark_node;
96 rval = build_type_conversion (type, expr);
97 if (rval)
99 if (rval == error_mark_node)
100 error ("conversion of %qE from %qT to %qT is ambiguous",
101 expr, intype, type);
102 return rval;
106 /* Handle anachronistic conversions from (::*)() to cv void* or (*)(). */
107 if (TREE_CODE (type) == POINTER_TYPE
108 && (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
109 || VOID_TYPE_P (TREE_TYPE (type))))
111 if (TYPE_PTRMEMFUNC_P (intype)
112 || TREE_CODE (intype) == METHOD_TYPE)
113 return convert_member_func_to_ptr (type, expr);
114 if (TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE)
115 return build_nop (type, expr);
116 intype = TREE_TYPE (expr);
119 if (expr == error_mark_node)
120 return error_mark_node;
122 form = TREE_CODE (intype);
124 if (POINTER_TYPE_P (intype))
126 intype = TYPE_MAIN_VARIANT (intype);
128 if (TYPE_MAIN_VARIANT (type) != intype
129 && TREE_CODE (type) == POINTER_TYPE
130 && TREE_CODE (TREE_TYPE (type)) == RECORD_TYPE
131 && IS_AGGR_TYPE (TREE_TYPE (type))
132 && IS_AGGR_TYPE (TREE_TYPE (intype))
133 && TREE_CODE (TREE_TYPE (intype)) == RECORD_TYPE)
135 enum tree_code code = PLUS_EXPR;
136 tree binfo;
137 tree intype_class;
138 tree type_class;
139 bool same_p;
141 intype_class = TREE_TYPE (intype);
142 type_class = TREE_TYPE (type);
144 same_p = same_type_p (TYPE_MAIN_VARIANT (intype_class),
145 TYPE_MAIN_VARIANT (type_class));
146 binfo = NULL_TREE;
147 /* Try derived to base conversion. */
148 if (!same_p)
149 binfo = lookup_base (intype_class, type_class, ba_check, NULL);
150 if (!same_p && !binfo)
152 /* Try base to derived conversion. */
153 binfo = lookup_base (type_class, intype_class, ba_check, NULL);
154 code = MINUS_EXPR;
156 if (binfo == error_mark_node)
157 return error_mark_node;
158 if (binfo || same_p)
160 if (binfo)
161 expr = build_base_path (code, expr, binfo, 0);
162 /* Add any qualifier conversions. */
163 return build_nop (type, expr);
167 if (TYPE_PTRMEMFUNC_P (type))
169 error ("cannot convert %qE from type %qT to type %qT",
170 expr, intype, type);
171 return error_mark_node;
174 return build_nop (type, expr);
176 else if (TYPE_PTRMEM_P (type) && TYPE_PTRMEM_P (intype))
178 tree b1;
179 tree b2;
180 tree binfo;
181 enum tree_code code = PLUS_EXPR;
182 base_kind bk;
184 b1 = TYPE_PTRMEM_CLASS_TYPE (type);
185 b2 = TYPE_PTRMEM_CLASS_TYPE (intype);
186 binfo = lookup_base (b1, b2, ba_check, &bk);
187 if (!binfo)
189 binfo = lookup_base (b2, b1, ba_check, &bk);
190 code = MINUS_EXPR;
192 if (binfo == error_mark_node)
193 return error_mark_node;
195 if (bk == bk_via_virtual)
197 if (force)
198 warning (0, "pointer to member cast from %qT to %qT is via"
199 " virtual base", intype, type);
200 else
202 error ("pointer to member cast from %qT to %qT is"
203 " via virtual base", intype, type);
204 return error_mark_node;
206 /* This is a reinterpret cast, whose result is unspecified.
207 We choose to do nothing. */
208 return build1 (NOP_EXPR, type, expr);
211 if (TREE_CODE (expr) == PTRMEM_CST)
212 expr = cplus_expand_constant (expr);
214 if (binfo && !integer_zerop (BINFO_OFFSET (binfo)))
215 expr = size_binop (code,
216 build_nop (sizetype, expr),
217 BINFO_OFFSET (binfo));
218 return build_nop (type, expr);
220 else if (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype))
221 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), expr, 0,
222 /*c_cast_p=*/false);
223 else if (TYPE_PTRMEMFUNC_P (intype))
225 if (!warn_pmf2ptr)
227 if (TREE_CODE (expr) == PTRMEM_CST)
228 return cp_convert_to_pointer (type,
229 PTRMEM_CST_MEMBER (expr),
230 force);
231 else if (TREE_CODE (expr) == OFFSET_REF)
233 tree object = TREE_OPERAND (expr, 0);
234 return get_member_function_from_ptrfunc (&object,
235 TREE_OPERAND (expr, 1));
238 error ("cannot convert %qE from type %qT to type %qT",
239 expr, intype, type);
240 return error_mark_node;
243 if (integer_zerop (expr))
245 if (TYPE_PTRMEMFUNC_P (type))
246 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), expr, 0,
247 /*c_cast_p=*/false);
249 if (TYPE_PTRMEM_P (type))
251 /* A NULL pointer-to-member is represented by -1, not by
252 zero. */
253 expr = build_int_cst (type, -1);
254 /* Fix up the representation of -1 if appropriate. */
255 expr = force_fit_type (expr, 0, false, false);
257 else
258 expr = build_int_cst (type, 0);
260 return expr;
262 else if (TYPE_PTR_TO_MEMBER_P (type) && INTEGRAL_CODE_P (form))
264 error ("invalid conversion from %qT to %qT", intype, type);
265 return error_mark_node;
268 if (INTEGRAL_CODE_P (form))
270 if (TYPE_PRECISION (intype) == POINTER_SIZE)
271 return build1 (CONVERT_EXPR, type, expr);
272 expr = cp_convert (c_common_type_for_size (POINTER_SIZE, 0), expr);
273 /* Modes may be different but sizes should be the same. There
274 is supposed to be some integral type that is the same width
275 as a pointer. */
276 gcc_assert (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (expr)))
277 == GET_MODE_SIZE (TYPE_MODE (type)));
279 return convert_to_pointer (type, expr);
282 if (type_unknown_p (expr))
283 return instantiate_type (type, expr, tf_error | tf_warning);
285 error ("cannot convert %qE from type %qT to type %qT",
286 expr, intype, type);
287 return error_mark_node;
290 /* Like convert, except permit conversions to take place which
291 are not normally allowed due to access restrictions
292 (such as conversion from sub-type to private super-type). */
294 static tree
295 convert_to_pointer_force (tree type, tree expr)
297 tree intype = TREE_TYPE (expr);
298 enum tree_code form = TREE_CODE (intype);
300 if (form == POINTER_TYPE)
302 intype = TYPE_MAIN_VARIANT (intype);
304 if (TYPE_MAIN_VARIANT (type) != intype
305 && TREE_CODE (TREE_TYPE (type)) == RECORD_TYPE
306 && IS_AGGR_TYPE (TREE_TYPE (type))
307 && IS_AGGR_TYPE (TREE_TYPE (intype))
308 && TREE_CODE (TREE_TYPE (intype)) == RECORD_TYPE)
310 enum tree_code code = PLUS_EXPR;
311 tree binfo;
313 binfo = lookup_base (TREE_TYPE (intype), TREE_TYPE (type),
314 ba_unique, NULL);
315 if (!binfo)
317 binfo = lookup_base (TREE_TYPE (type), TREE_TYPE (intype),
318 ba_unique, NULL);
319 code = MINUS_EXPR;
321 if (binfo == error_mark_node)
322 return error_mark_node;
323 if (binfo)
325 expr = build_base_path (code, expr, binfo, 0);
326 if (expr == error_mark_node)
327 return error_mark_node;
328 /* Add any qualifier conversions. */
329 if (!same_type_p (TREE_TYPE (TREE_TYPE (expr)),
330 TREE_TYPE (type)))
331 expr = build_nop (type, expr);
332 return expr;
337 return cp_convert_to_pointer (type, expr, true);
340 /* We are passing something to a function which requires a reference.
341 The type we are interested in is in TYPE. The initial
342 value we have to begin with is in ARG.
344 FLAGS controls how we manage access checking.
345 DIRECT_BIND in FLAGS controls how any temporaries are generated.
346 If DIRECT_BIND is set, DECL is the reference we're binding to. */
348 static tree
349 build_up_reference (tree type, tree arg, int flags, tree decl)
351 tree rval;
352 tree argtype = TREE_TYPE (arg);
353 tree target_type = TREE_TYPE (type);
355 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
357 if ((flags & DIRECT_BIND) && ! real_lvalue_p (arg))
359 /* Create a new temporary variable. We can't just use a TARGET_EXPR
360 here because it needs to live as long as DECL. */
361 tree targ = arg;
363 arg = make_temporary_var_for_ref_to_temp (decl, TREE_TYPE (arg));
365 /* Process the initializer for the declaration. */
366 DECL_INITIAL (arg) = targ;
367 cp_finish_decl (arg, targ, /*init_const_expr_p=*/false, NULL_TREE,
368 LOOKUP_ONLYCONVERTING|DIRECT_BIND);
370 else if (!(flags & DIRECT_BIND) && ! lvalue_p (arg))
371 return get_target_expr (arg);
373 /* If we had a way to wrap this up, and say, if we ever needed its
374 address, transform all occurrences of the register, into a memory
375 reference we could win better. */
376 rval = build_unary_op (ADDR_EXPR, arg, 1);
377 if (rval == error_mark_node)
378 return error_mark_node;
380 if ((flags & LOOKUP_PROTECT)
381 && TYPE_MAIN_VARIANT (argtype) != TYPE_MAIN_VARIANT (target_type)
382 && IS_AGGR_TYPE (argtype)
383 && IS_AGGR_TYPE (target_type))
385 /* We go through lookup_base for the access control. */
386 tree binfo = lookup_base (argtype, target_type, ba_check, NULL);
387 if (binfo == error_mark_node)
388 return error_mark_node;
389 if (binfo == NULL_TREE)
390 return error_not_base_type (target_type, argtype);
391 rval = build_base_path (PLUS_EXPR, rval, binfo, 1);
393 else
394 rval
395 = convert_to_pointer_force (build_pointer_type (target_type), rval);
396 return build_nop (type, rval);
399 /* Subroutine of convert_to_reference. REFTYPE is the target reference type.
400 INTYPE is the original rvalue type and DECL is an optional _DECL node
401 for diagnostics.
403 [dcl.init.ref] says that if an rvalue is used to
404 initialize a reference, then the reference must be to a
405 non-volatile const type. */
407 static void
408 warn_ref_binding (tree reftype, tree intype, tree decl)
410 tree ttl = TREE_TYPE (reftype);
412 if (!CP_TYPE_CONST_NON_VOLATILE_P (ttl))
414 const char *msg;
416 if (CP_TYPE_VOLATILE_P (ttl) && decl)
417 msg = "initialization of volatile reference type %q#T from"
418 " rvalue of type %qT";
419 else if (CP_TYPE_VOLATILE_P (ttl))
420 msg = "conversion to volatile reference type %q#T "
421 " from rvalue of type %qT";
422 else if (decl)
423 msg = "initialization of non-const reference type %q#T from"
424 " rvalue of type %qT";
425 else
426 msg = "conversion to non-const reference type %q#T from"
427 " rvalue of type %qT";
429 pedwarn (msg, reftype, intype);
433 /* For C++: Only need to do one-level references, but cannot
434 get tripped up on signed/unsigned differences.
436 DECL is either NULL_TREE or the _DECL node for a reference that is being
437 initialized. It can be error_mark_node if we don't know the _DECL but
438 we know it's an initialization. */
440 tree
441 convert_to_reference (tree reftype, tree expr, int convtype,
442 int flags, tree decl)
444 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (reftype));
445 tree intype;
446 tree rval = NULL_TREE;
447 tree rval_as_conversion = NULL_TREE;
448 bool can_convert_intype_to_type;
450 if (TREE_CODE (type) == FUNCTION_TYPE
451 && TREE_TYPE (expr) == unknown_type_node)
452 expr = instantiate_type (type, expr,
453 (flags & LOOKUP_COMPLAIN)
454 ? tf_error | tf_warning : tf_none);
456 if (expr == error_mark_node)
457 return error_mark_node;
459 intype = TREE_TYPE (expr);
461 gcc_assert (TREE_CODE (intype) != REFERENCE_TYPE);
462 gcc_assert (TREE_CODE (reftype) == REFERENCE_TYPE);
464 intype = TYPE_MAIN_VARIANT (intype);
466 can_convert_intype_to_type = can_convert (type, intype);
467 if (!can_convert_intype_to_type
468 && (convtype & CONV_IMPLICIT) && IS_AGGR_TYPE (intype)
469 && ! (flags & LOOKUP_NO_CONVERSION))
471 /* Look for a user-defined conversion to lvalue that we can use. */
473 rval_as_conversion
474 = build_type_conversion (reftype, expr);
476 if (rval_as_conversion && rval_as_conversion != error_mark_node
477 && real_lvalue_p (rval_as_conversion))
479 expr = rval_as_conversion;
480 rval_as_conversion = NULL_TREE;
481 intype = type;
482 can_convert_intype_to_type = 1;
486 if (((convtype & CONV_STATIC) && can_convert (intype, type))
487 || ((convtype & CONV_IMPLICIT) && can_convert_intype_to_type))
489 if (flags & LOOKUP_COMPLAIN)
491 tree ttl = TREE_TYPE (reftype);
492 tree ttr = lvalue_type (expr);
494 if (! real_lvalue_p (expr))
495 warn_ref_binding (reftype, intype, decl);
497 if (! (convtype & CONV_CONST)
498 && !at_least_as_qualified_p (ttl, ttr))
499 pedwarn ("conversion from %qT to %qT discards qualifiers",
500 ttr, reftype);
503 return build_up_reference (reftype, expr, flags, decl);
505 else if ((convtype & CONV_REINTERPRET) && lvalue_p (expr))
507 /* When casting an lvalue to a reference type, just convert into
508 a pointer to the new type and deference it. This is allowed
509 by San Diego WP section 5.2.9 paragraph 12, though perhaps it
510 should be done directly (jason). (int &)ri ---> *(int*)&ri */
512 /* B* bp; A& ar = (A&)bp; is valid, but it's probably not what they
513 meant. */
514 if (TREE_CODE (intype) == POINTER_TYPE
515 && (comptypes (TREE_TYPE (intype), type,
516 COMPARE_BASE | COMPARE_DERIVED)))
517 warning (0, "casting %qT to %qT does not dereference pointer",
518 intype, reftype);
520 rval = build_unary_op (ADDR_EXPR, expr, 0);
521 if (rval != error_mark_node)
522 rval = convert_force (build_pointer_type (TREE_TYPE (reftype)),
523 rval, 0);
524 if (rval != error_mark_node)
525 rval = build1 (NOP_EXPR, reftype, rval);
527 else
529 rval = convert_for_initialization (NULL_TREE, type, expr, flags,
530 "converting", 0, 0);
531 if (rval == NULL_TREE || rval == error_mark_node)
532 return rval;
533 warn_ref_binding (reftype, intype, decl);
534 rval = build_up_reference (reftype, rval, flags, decl);
537 if (rval)
539 /* If we found a way to convert earlier, then use it. */
540 return rval;
543 if (flags & LOOKUP_COMPLAIN)
544 error ("cannot convert type %qT to type %qT", intype, reftype);
546 return error_mark_node;
549 /* We are using a reference VAL for its value. Bash that reference all the
550 way down to its lowest form. */
552 tree
553 convert_from_reference (tree val)
555 if (TREE_CODE (TREE_TYPE (val)) == REFERENCE_TYPE)
557 tree t = canonical_type_variant (TREE_TYPE (TREE_TYPE (val)));
558 tree ref = build1 (INDIRECT_REF, t, val);
560 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
561 so that we get the proper error message if the result is used
562 to assign to. Also, &* is supposed to be a no-op. */
563 TREE_READONLY (ref) = CP_TYPE_CONST_P (t);
564 TREE_THIS_VOLATILE (ref) = CP_TYPE_VOLATILE_P (t);
565 TREE_SIDE_EFFECTS (ref)
566 = (TREE_THIS_VOLATILE (ref) || TREE_SIDE_EFFECTS (val));
567 REFERENCE_REF_P (ref) = 1;
568 val = ref;
571 return val;
574 /* Really perform an lvalue-to-rvalue conversion, including copying an
575 argument of class type into a temporary. */
577 tree
578 force_rvalue (tree expr)
580 if (IS_AGGR_TYPE (TREE_TYPE (expr)) && TREE_CODE (expr) != TARGET_EXPR)
581 expr = ocp_convert (TREE_TYPE (expr), expr,
582 CONV_IMPLICIT|CONV_FORCE_TEMP, LOOKUP_NORMAL);
583 else
584 expr = decay_conversion (expr);
586 return expr;
589 /* C++ conversions, preference to static cast conversions. */
591 tree
592 cp_convert (tree type, tree expr)
594 return ocp_convert (type, expr, CONV_OLD_CONVERT, LOOKUP_NORMAL);
597 /* Conversion...
599 FLAGS indicates how we should behave. */
601 tree
602 ocp_convert (tree type, tree expr, int convtype, int flags)
604 tree e = expr;
605 enum tree_code code = TREE_CODE (type);
606 const char *invalid_conv_diag;
608 if (error_operand_p (e) || type == error_mark_node)
609 return error_mark_node;
611 complete_type (type);
612 complete_type (TREE_TYPE (expr));
614 if ((invalid_conv_diag
615 = targetm.invalid_conversion (TREE_TYPE (expr), type)))
617 error (invalid_conv_diag);
618 return error_mark_node;
621 e = integral_constant_value (e);
623 if (IS_AGGR_TYPE (type) && (convtype & CONV_FORCE_TEMP)
624 /* Some internal structures (vtable_entry_type, sigtbl_ptr_type)
625 don't go through finish_struct, so they don't have the synthesized
626 constructors. So don't force a temporary. */
627 && TYPE_HAS_CONSTRUCTOR (type))
628 /* We need a new temporary; don't take this shortcut. */;
629 else if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (TREE_TYPE (e)))
631 if (same_type_p (type, TREE_TYPE (e)))
632 /* The call to fold will not always remove the NOP_EXPR as
633 might be expected, since if one of the types is a typedef;
634 the comparison in fold is just equality of pointers, not a
635 call to comptypes. We don't call fold in this case because
636 that can result in infinite recursion; fold will call
637 convert, which will call ocp_convert, etc. */
638 return e;
639 /* For complex data types, we need to perform componentwise
640 conversion. */
641 else if (TREE_CODE (type) == COMPLEX_TYPE)
642 return fold_if_not_in_template (convert_to_complex (type, e));
643 else if (TREE_CODE (e) == TARGET_EXPR)
645 /* Don't build a NOP_EXPR of class type. Instead, change the
646 type of the temporary. Only allow this for cv-qual changes,
647 though. */
648 gcc_assert (same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (e)),
649 TYPE_MAIN_VARIANT (type)));
650 TREE_TYPE (e) = TREE_TYPE (TARGET_EXPR_SLOT (e)) = type;
651 return e;
653 else
655 /* We shouldn't be treating objects of ADDRESSABLE type as
656 rvalues. */
657 gcc_assert (!TREE_ADDRESSABLE (type));
658 return fold_if_not_in_template (build_nop (type, e));
662 if (code == VOID_TYPE && (convtype & CONV_STATIC))
664 e = convert_to_void (e, /*implicit=*/NULL);
665 return e;
668 if (INTEGRAL_CODE_P (code))
670 tree intype = TREE_TYPE (e);
671 /* enum = enum, enum = int, enum = float, (enum)pointer are all
672 errors. */
673 if (TREE_CODE (type) == ENUMERAL_TYPE
674 && (((INTEGRAL_OR_ENUMERATION_TYPE_P (intype)
675 || TREE_CODE (intype) == REAL_TYPE)
676 && ! (convtype & CONV_STATIC))
677 || TREE_CODE (intype) == POINTER_TYPE))
679 if (flags & LOOKUP_COMPLAIN)
680 pedwarn ("conversion from %q#T to %q#T", intype, type);
682 if (flag_pedantic_errors)
683 return error_mark_node;
685 if (IS_AGGR_TYPE (intype))
687 tree rval;
688 rval = build_type_conversion (type, e);
689 if (rval)
690 return rval;
691 if (flags & LOOKUP_COMPLAIN)
692 error ("%q#T used where a %qT was expected", intype, type);
693 return error_mark_node;
695 if (code == BOOLEAN_TYPE)
696 return cp_truthvalue_conversion (e);
698 return fold_if_not_in_template (convert_to_integer (type, e));
700 if (POINTER_TYPE_P (type) || TYPE_PTR_TO_MEMBER_P (type))
701 return fold_if_not_in_template (cp_convert_to_pointer (type, e, false));
702 if (code == VECTOR_TYPE)
704 tree in_vtype = TREE_TYPE (e);
705 if (IS_AGGR_TYPE (in_vtype))
707 tree ret_val;
708 ret_val = build_type_conversion (type, e);
709 if (ret_val)
710 return ret_val;
711 if (flags & LOOKUP_COMPLAIN)
712 error ("%q#T used where a %qT was expected", in_vtype, type);
713 return error_mark_node;
715 return fold_if_not_in_template (convert_to_vector (type, e));
717 if (code == REAL_TYPE || code == COMPLEX_TYPE)
719 if (IS_AGGR_TYPE (TREE_TYPE (e)))
721 tree rval;
722 rval = build_type_conversion (type, e);
723 if (rval)
724 return rval;
725 else
726 if (flags & LOOKUP_COMPLAIN)
727 error ("%q#T used where a floating point value was expected",
728 TREE_TYPE (e));
730 if (code == REAL_TYPE)
731 return fold_if_not_in_template (convert_to_real (type, e));
732 else if (code == COMPLEX_TYPE)
733 return fold_if_not_in_template (convert_to_complex (type, e));
736 /* New C++ semantics: since assignment is now based on
737 memberwise copying, if the rhs type is derived from the
738 lhs type, then we may still do a conversion. */
739 if (IS_AGGR_TYPE_CODE (code))
741 tree dtype = TREE_TYPE (e);
742 tree ctor = NULL_TREE;
744 dtype = TYPE_MAIN_VARIANT (dtype);
746 /* Conversion between aggregate types. New C++ semantics allow
747 objects of derived type to be cast to objects of base type.
748 Old semantics only allowed this between pointers.
750 There may be some ambiguity between using a constructor
751 vs. using a type conversion operator when both apply. */
753 ctor = e;
755 if (abstract_virtuals_error (NULL_TREE, type))
756 return error_mark_node;
758 if ((flags & LOOKUP_ONLYCONVERTING)
759 && ! (IS_AGGR_TYPE (dtype) && DERIVED_FROM_P (type, dtype)))
760 /* For copy-initialization, first we create a temp of the proper type
761 with a user-defined conversion sequence, then we direct-initialize
762 the target with the temp (see [dcl.init]). */
763 ctor = build_user_type_conversion (type, ctor, flags);
764 else
765 ctor = build_special_member_call (NULL_TREE,
766 complete_ctor_identifier,
767 build_tree_list (NULL_TREE, ctor),
768 type, flags);
769 if (ctor)
770 return build_cplus_new (type, ctor);
773 if (flags & LOOKUP_COMPLAIN)
774 error ("conversion from %qT to non-scalar type %qT requested",
775 TREE_TYPE (expr), type);
776 return error_mark_node;
779 /* When an expression is used in a void context, its value is discarded and
780 no lvalue-rvalue and similar conversions happen [expr.static.cast/4,
781 stmt.expr/1, expr.comma/1]. This permits dereferencing an incomplete type
782 in a void context. The C++ standard does not define what an `access' to an
783 object is, but there is reason to believe that it is the lvalue to rvalue
784 conversion -- if it were not, `*&*p = 1' would violate [expr]/4 in that it
785 accesses `*p' not to calculate the value to be stored. But, dcl.type.cv/8
786 indicates that volatile semantics should be the same between C and C++
787 where ever possible. C leaves it implementation defined as to what
788 constitutes an access to a volatile. So, we interpret `*vp' as a read of
789 the volatile object `vp' points to, unless that is an incomplete type. For
790 volatile references we do not do this interpretation, because that would
791 make it impossible to ignore the reference return value from functions. We
792 issue warnings in the confusing cases.
794 IMPLICIT is tells us the context of an implicit void conversion. */
796 tree
797 convert_to_void (tree expr, const char *implicit)
799 if (expr == error_mark_node
800 || TREE_TYPE (expr) == error_mark_node)
801 return error_mark_node;
802 if (!TREE_TYPE (expr))
803 return expr;
804 if (invalid_nonstatic_memfn_p (expr))
805 return error_mark_node;
806 if (TREE_CODE (expr) == PSEUDO_DTOR_EXPR)
808 error ("pseudo-destructor is not called");
809 return error_mark_node;
811 if (VOID_TYPE_P (TREE_TYPE (expr)))
812 return expr;
813 switch (TREE_CODE (expr))
815 case COND_EXPR:
817 /* The two parts of a cond expr might be separate lvalues. */
818 tree op1 = TREE_OPERAND (expr,1);
819 tree op2 = TREE_OPERAND (expr,2);
820 tree new_op1 = convert_to_void
821 (op1, (implicit && !TREE_SIDE_EFFECTS (op2)
822 ? "second operand of conditional" : NULL));
823 tree new_op2 = convert_to_void
824 (op2, (implicit && !TREE_SIDE_EFFECTS (op1)
825 ? "third operand of conditional" : NULL));
827 expr = build3 (COND_EXPR, TREE_TYPE (new_op1),
828 TREE_OPERAND (expr, 0), new_op1, new_op2);
829 break;
832 case COMPOUND_EXPR:
834 /* The second part of a compound expr contains the value. */
835 tree op1 = TREE_OPERAND (expr,1);
836 tree new_op1 = convert_to_void
837 (op1, (implicit && !TREE_NO_WARNING (expr)
838 ? "right-hand operand of comma" : NULL));
840 if (new_op1 != op1)
842 tree t = build2 (COMPOUND_EXPR, TREE_TYPE (new_op1),
843 TREE_OPERAND (expr, 0), new_op1);
844 expr = t;
847 break;
850 case NON_LVALUE_EXPR:
851 case NOP_EXPR:
852 /* These have already decayed to rvalue. */
853 break;
855 case CALL_EXPR: /* We have a special meaning for volatile void fn(). */
856 break;
858 case INDIRECT_REF:
860 tree type = TREE_TYPE (expr);
861 int is_reference = TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0)))
862 == REFERENCE_TYPE;
863 int is_volatile = TYPE_VOLATILE (type);
864 int is_complete = COMPLETE_TYPE_P (complete_type (type));
866 /* Can't load the value if we don't know the type. */
867 if (is_volatile && !is_complete)
868 warning (0, "object of incomplete type %qT will not be accessed in %s",
869 type, implicit ? implicit : "void context");
870 /* Don't load the value if this is an implicit dereference, or if
871 the type needs to be handled by ctors/dtors. */
872 else if (is_volatile && (is_reference || TREE_ADDRESSABLE (type)))
873 warning (0, "object of type %qT will not be accessed in %s",
874 TREE_TYPE (TREE_OPERAND (expr, 0)),
875 implicit ? implicit : "void context");
876 if (is_reference || !is_volatile || !is_complete || TREE_ADDRESSABLE (type))
877 expr = TREE_OPERAND (expr, 0);
879 break;
882 case VAR_DECL:
884 /* External variables might be incomplete. */
885 tree type = TREE_TYPE (expr);
886 int is_complete = COMPLETE_TYPE_P (complete_type (type));
888 if (TYPE_VOLATILE (type) && !is_complete)
889 warning (0, "object %qE of incomplete type %qT will not be accessed in %s",
890 expr, type, implicit ? implicit : "void context");
891 break;
894 default:;
897 tree probe = expr;
899 if (TREE_CODE (probe) == ADDR_EXPR)
900 probe = TREE_OPERAND (expr, 0);
901 if (type_unknown_p (probe))
903 /* [over.over] enumerates the places where we can take the address
904 of an overloaded function, and this is not one of them. */
905 pedwarn ("%s cannot resolve address of overloaded function",
906 implicit ? implicit : "void cast");
907 expr = void_zero_node;
909 else if (implicit && probe == expr && is_overloaded_fn (probe))
911 /* Only warn when there is no &. */
912 warning (0, "%s is a reference, not call, to function %qE",
913 implicit, expr);
914 if (TREE_CODE (expr) == COMPONENT_REF)
915 expr = TREE_OPERAND (expr, 0);
919 if (expr != error_mark_node && !VOID_TYPE_P (TREE_TYPE (expr)))
921 if (implicit
922 && warn_unused_value
923 && !TREE_NO_WARNING (expr)
924 && !processing_template_decl)
926 /* The middle end does not warn about expressions that have
927 been explicitly cast to void, so we must do so here. */
928 if (!TREE_SIDE_EFFECTS (expr))
929 warning (0, "%s has no effect", implicit);
930 else
932 tree e;
933 enum tree_code code;
934 enum tree_code_class class;
936 e = expr;
937 /* We might like to warn about (say) "(int) f()", as the
938 cast has no effect, but the compiler itself will
939 generate implicit conversions under some
940 circumstances. (For example a block copy will be
941 turned into a call to "__builtin_memcpy", with a
942 conversion of the return value to an appropriate
943 type.) So, to avoid false positives, we strip
944 conversions. Do not use STRIP_NOPs because it will
945 not strip conversions to "void", as that is not a
946 mode-preserving conversion. */
947 while (TREE_CODE (e) == NOP_EXPR)
948 e = TREE_OPERAND (e, 0);
950 code = TREE_CODE (e);
951 class = TREE_CODE_CLASS (code);
952 if (class == tcc_comparison
953 || class == tcc_unary
954 || (class == tcc_binary
955 && !(code == MODIFY_EXPR
956 || code == INIT_EXPR
957 || code == PREDECREMENT_EXPR
958 || code == PREINCREMENT_EXPR
959 || code == POSTDECREMENT_EXPR
960 || code == POSTINCREMENT_EXPR)))
961 warning (0, "value computed is not used");
964 expr = build1 (CONVERT_EXPR, void_type_node, expr);
966 if (! TREE_SIDE_EFFECTS (expr))
967 expr = void_zero_node;
968 return expr;
971 /* Create an expression whose value is that of EXPR,
972 converted to type TYPE. The TREE_TYPE of the value
973 is always TYPE. This function implements all reasonable
974 conversions; callers should filter out those that are
975 not permitted by the language being compiled.
977 Most of this routine is from build_reinterpret_cast.
979 The backend cannot call cp_convert (what was convert) because
980 conversions to/from basetypes may involve memory references
981 (vbases) and adding or subtracting small values (multiple
982 inheritance), but it calls convert from the constant folding code
983 on subtrees of already built trees after it has ripped them apart.
985 Also, if we ever support range variables, we'll probably also have to
986 do a little bit more work. */
988 tree
989 convert (tree type, tree expr)
991 tree intype;
993 if (type == error_mark_node || expr == error_mark_node)
994 return error_mark_node;
996 intype = TREE_TYPE (expr);
998 if (POINTER_TYPE_P (type) && POINTER_TYPE_P (intype))
999 return fold_if_not_in_template (build_nop (type, expr));
1001 return ocp_convert (type, expr, CONV_OLD_CONVERT,
1002 LOOKUP_NORMAL|LOOKUP_NO_CONVERSION);
1005 /* Like cp_convert, except permit conversions to take place which
1006 are not normally allowed due to access restrictions
1007 (such as conversion from sub-type to private super-type). */
1009 tree
1010 convert_force (tree type, tree expr, int convtype)
1012 tree e = expr;
1013 enum tree_code code = TREE_CODE (type);
1015 if (code == REFERENCE_TYPE)
1016 return (fold_if_not_in_template
1017 (convert_to_reference (type, e, CONV_C_CAST, LOOKUP_COMPLAIN,
1018 NULL_TREE)));
1020 if (code == POINTER_TYPE)
1021 return fold_if_not_in_template (convert_to_pointer_force (type, e));
1023 /* From typeck.c convert_for_assignment */
1024 if (((TREE_CODE (TREE_TYPE (e)) == POINTER_TYPE && TREE_CODE (e) == ADDR_EXPR
1025 && TREE_CODE (TREE_TYPE (e)) == POINTER_TYPE
1026 && TREE_CODE (TREE_TYPE (TREE_TYPE (e))) == METHOD_TYPE)
1027 || integer_zerop (e)
1028 || TYPE_PTRMEMFUNC_P (TREE_TYPE (e)))
1029 && TYPE_PTRMEMFUNC_P (type))
1030 /* compatible pointer to member functions. */
1031 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), e, 1,
1032 /*c_cast_p=*/1);
1034 return ocp_convert (type, e, CONV_C_CAST|convtype, LOOKUP_NORMAL);
1037 /* Convert an aggregate EXPR to type XTYPE. If a conversion
1038 exists, return the attempted conversion. This may
1039 return ERROR_MARK_NODE if the conversion is not
1040 allowed (references private members, etc).
1041 If no conversion exists, NULL_TREE is returned.
1043 FIXME: Ambiguity checking is wrong. Should choose one by the implicit
1044 object parameter, or by the second standard conversion sequence if
1045 that doesn't do it. This will probably wait for an overloading rewrite.
1046 (jason 8/9/95) */
1048 tree
1049 build_type_conversion (tree xtype, tree expr)
1051 /* C++: check to see if we can convert this aggregate type
1052 into the required type. */
1053 return build_user_type_conversion (xtype, expr, LOOKUP_NORMAL);
1056 /* Convert the given EXPR to one of a group of types suitable for use in an
1057 expression. DESIRES is a combination of various WANT_* flags (q.v.)
1058 which indicates which types are suitable. If COMPLAIN is true, complain
1059 about ambiguity; otherwise, the caller will deal with it. */
1061 tree
1062 build_expr_type_conversion (int desires, tree expr, bool complain)
1064 tree basetype = TREE_TYPE (expr);
1065 tree conv = NULL_TREE;
1066 tree winner = NULL_TREE;
1068 if (expr == null_node
1069 && (desires & WANT_INT)
1070 && !(desires & WANT_NULL))
1071 warning (0, "converting NULL to non-pointer type");
1073 basetype = TREE_TYPE (expr);
1075 if (basetype == error_mark_node)
1076 return error_mark_node;
1078 if (! IS_AGGR_TYPE (basetype))
1079 switch (TREE_CODE (basetype))
1081 case INTEGER_TYPE:
1082 if ((desires & WANT_NULL) && null_ptr_cst_p (expr))
1083 return expr;
1084 /* else fall through... */
1086 case BOOLEAN_TYPE:
1087 return (desires & WANT_INT) ? expr : NULL_TREE;
1088 case ENUMERAL_TYPE:
1089 return (desires & WANT_ENUM) ? expr : NULL_TREE;
1090 case REAL_TYPE:
1091 return (desires & WANT_FLOAT) ? expr : NULL_TREE;
1092 case POINTER_TYPE:
1093 return (desires & WANT_POINTER) ? expr : NULL_TREE;
1095 case FUNCTION_TYPE:
1096 case ARRAY_TYPE:
1097 return (desires & WANT_POINTER) ? decay_conversion (expr)
1098 : NULL_TREE;
1100 case VECTOR_TYPE:
1101 if ((desires & WANT_VECTOR) == 0)
1102 return NULL_TREE;
1103 switch (TREE_CODE (TREE_TYPE (basetype)))
1105 case INTEGER_TYPE:
1106 case BOOLEAN_TYPE:
1107 return (desires & WANT_INT) ? expr : NULL_TREE;
1108 case ENUMERAL_TYPE:
1109 return (desires & WANT_ENUM) ? expr : NULL_TREE;
1110 case REAL_TYPE:
1111 return (desires & WANT_FLOAT) ? expr : NULL_TREE;
1112 default:
1113 return NULL_TREE;
1116 default:
1117 return NULL_TREE;
1120 /* The code for conversions from class type is currently only used for
1121 delete expressions. Other expressions are handled by build_new_op. */
1122 if (!complete_type_or_else (basetype, expr))
1123 return error_mark_node;
1124 if (!TYPE_HAS_CONVERSION (basetype))
1125 return NULL_TREE;
1127 for (conv = lookup_conversions (basetype); conv; conv = TREE_CHAIN (conv))
1129 int win = 0;
1130 tree candidate;
1131 tree cand = TREE_VALUE (conv);
1133 if (winner && winner == cand)
1134 continue;
1136 candidate = non_reference (TREE_TYPE (TREE_TYPE (cand)));
1138 switch (TREE_CODE (candidate))
1140 case BOOLEAN_TYPE:
1141 case INTEGER_TYPE:
1142 win = (desires & WANT_INT); break;
1143 case ENUMERAL_TYPE:
1144 win = (desires & WANT_ENUM); break;
1145 case REAL_TYPE:
1146 win = (desires & WANT_FLOAT); break;
1147 case POINTER_TYPE:
1148 win = (desires & WANT_POINTER); break;
1150 case VECTOR_TYPE:
1151 if ((desires & WANT_VECTOR) == 0)
1152 break;
1153 switch (TREE_CODE (TREE_TYPE (candidate)))
1155 case BOOLEAN_TYPE:
1156 case INTEGER_TYPE:
1157 win = (desires & WANT_INT); break;
1158 case ENUMERAL_TYPE:
1159 win = (desires & WANT_ENUM); break;
1160 case REAL_TYPE:
1161 win = (desires & WANT_FLOAT); break;
1162 default:
1163 break;
1165 break;
1167 default:
1168 break;
1171 if (win)
1173 if (winner)
1175 if (complain)
1177 error ("ambiguous default type conversion from %qT",
1178 basetype);
1179 error (" candidate conversions include %qD and %qD",
1180 winner, cand);
1182 return error_mark_node;
1184 else
1185 winner = cand;
1189 if (winner)
1191 tree type = non_reference (TREE_TYPE (TREE_TYPE (winner)));
1192 return build_user_type_conversion (type, expr, LOOKUP_NORMAL);
1195 return NULL_TREE;
1198 /* Implements integral promotion (4.1) and float->double promotion. */
1200 tree
1201 type_promotes_to (tree type)
1203 if (type == error_mark_node)
1204 return error_mark_node;
1206 type = TYPE_MAIN_VARIANT (type);
1208 /* bool always promotes to int (not unsigned), even if it's the same
1209 size. */
1210 if (type == boolean_type_node)
1211 type = integer_type_node;
1213 /* Normally convert enums to int, but convert wide enums to something
1214 wider. */
1215 else if (TREE_CODE (type) == ENUMERAL_TYPE
1216 || type == wchar_type_node)
1218 int precision = MAX (TYPE_PRECISION (type),
1219 TYPE_PRECISION (integer_type_node));
1220 tree totype = c_common_type_for_size (precision, 0);
1221 if (TYPE_UNSIGNED (type)
1222 && ! int_fits_type_p (TYPE_MAX_VALUE (type), totype))
1223 type = c_common_type_for_size (precision, 1);
1224 else
1225 type = totype;
1227 else if (c_promoting_integer_type_p (type))
1229 /* Retain unsignedness if really not getting bigger. */
1230 if (TYPE_UNSIGNED (type)
1231 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
1232 type = unsigned_type_node;
1233 else
1234 type = integer_type_node;
1236 else if (type == float_type_node)
1237 type = double_type_node;
1239 return type;
1242 /* The routines below this point are carefully written to conform to
1243 the standard. They use the same terminology, and follow the rules
1244 closely. Although they are used only in pt.c at the moment, they
1245 should presumably be used everywhere in the future. */
1247 /* Attempt to perform qualification conversions on EXPR to convert it
1248 to TYPE. Return the resulting expression, or error_mark_node if
1249 the conversion was impossible. */
1251 tree
1252 perform_qualification_conversions (tree type, tree expr)
1254 tree expr_type;
1256 expr_type = TREE_TYPE (expr);
1258 if (same_type_p (type, expr_type))
1259 return expr;
1260 else if (TYPE_PTR_P (type) && TYPE_PTR_P (expr_type)
1261 && comp_ptr_ttypes (TREE_TYPE (type), TREE_TYPE (expr_type)))
1262 return build_nop (type, expr);
1263 else if (TYPE_PTR_TO_MEMBER_P (type)
1264 && TYPE_PTR_TO_MEMBER_P (expr_type)
1265 && same_type_p (TYPE_PTRMEM_CLASS_TYPE (type),
1266 TYPE_PTRMEM_CLASS_TYPE (expr_type))
1267 && comp_ptr_ttypes (TYPE_PTRMEM_POINTED_TO_TYPE (type),
1268 TYPE_PTRMEM_POINTED_TO_TYPE (expr_type)))
1269 return build_nop (type, expr);
1270 else
1271 return error_mark_node;