PR c++/9844, PR c++/13989
[official-gcc.git] / gcc / cp / cvt.c
blob5db41468c903e5cf46246d0c473acf121171b78d
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)
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, 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. */
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"
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
70 else convert blindly
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). */
76 static tree
77 cp_convert_to_pointer (tree type, tree expr, bool force)
79 tree intype = TREE_TYPE (expr);
80 enum tree_code form;
81 tree rval;
83 if (IS_AGGR_TYPE (intype))
85 intype = complete_type (intype);
86 if (!COMPLETE_TYPE_P (intype))
88 error ("can't convert from incomplete type `%T' to `%T'",
89 intype, type);
90 return error_mark_node;
93 rval = build_type_conversion (type, expr);
94 if (rval)
96 if (rval == error_mark_node)
97 error ("conversion of `%E' from `%T' to `%T' is ambiguous",
98 expr, intype, type);
99 return rval;
103 /* Handle anachronistic conversions from (::*)() to cv void* or (*)(). */
104 if (TREE_CODE (type) == POINTER_TYPE
105 && (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
106 || VOID_TYPE_P (TREE_TYPE (type))))
108 /* Allow an implicit this pointer for pointer to member
109 functions. */
110 if (TYPE_PTRMEMFUNC_P (intype))
112 if (pedantic || warn_pmf2ptr)
113 pedwarn ("converting from `%T' to `%T'", intype, type);
114 if (TREE_CODE (expr) == PTRMEM_CST)
115 expr = build_address (PTRMEM_CST_MEMBER (expr));
116 else
118 tree decl = maybe_dummy_object (TYPE_PTRMEM_CLASS_TYPE (intype),
120 decl = build_address (decl);
121 expr = get_member_function_from_ptrfunc (&decl, expr);
124 else if (TREE_CODE (TREE_TYPE (expr)) == METHOD_TYPE)
126 if (pedantic || warn_pmf2ptr)
127 pedwarn ("converting from `%T' to `%T'", intype, type);
128 expr = build_addr_func (expr);
130 if (TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE)
131 return build_nop (type, expr);
132 intype = TREE_TYPE (expr);
135 if (expr == error_mark_node)
136 return error_mark_node;
138 form = TREE_CODE (intype);
140 if (POINTER_TYPE_P (intype))
142 intype = TYPE_MAIN_VARIANT (intype);
144 if (TYPE_MAIN_VARIANT (type) != intype
145 && TREE_CODE (type) == POINTER_TYPE
146 && TREE_CODE (TREE_TYPE (type)) == RECORD_TYPE
147 && IS_AGGR_TYPE (TREE_TYPE (type))
148 && IS_AGGR_TYPE (TREE_TYPE (intype))
149 && TREE_CODE (TREE_TYPE (intype)) == RECORD_TYPE)
151 enum tree_code code = PLUS_EXPR;
152 tree binfo;
153 tree intype_class;
154 tree type_class;
155 bool same_p;
157 intype_class = TREE_TYPE (intype);
158 type_class = TREE_TYPE (type);
160 same_p = same_type_p (TYPE_MAIN_VARIANT (intype_class),
161 TYPE_MAIN_VARIANT (type_class));
162 binfo = NULL_TREE;
163 /* Try derived to base conversion. */
164 if (!same_p)
165 binfo = lookup_base (intype_class, type_class, ba_check, NULL);
166 if (!same_p && !binfo)
168 /* Try base to derived conversion. */
169 binfo = lookup_base (type_class, intype_class, ba_check, NULL);
170 code = MINUS_EXPR;
172 if (binfo == error_mark_node)
173 return error_mark_node;
174 if (binfo || same_p)
176 if (binfo)
177 expr = build_base_path (code, expr, binfo, 0);
178 /* Add any qualifier conversions. */
179 return build_nop (type, expr);
183 if (TYPE_PTRMEMFUNC_P (type))
185 error ("cannot convert `%E' from type `%T' to type `%T'",
186 expr, intype, type);
187 return error_mark_node;
190 return build_nop (type, expr);
192 else if (TYPE_PTRMEM_P (type) && TYPE_PTRMEM_P (intype))
194 tree b1;
195 tree b2;
196 tree binfo;
197 enum tree_code code = PLUS_EXPR;
198 base_kind bk;
200 b1 = TYPE_PTRMEM_CLASS_TYPE (type);
201 b2 = TYPE_PTRMEM_CLASS_TYPE (intype);
202 binfo = lookup_base (b1, b2, ba_check, &bk);
203 if (!binfo)
205 binfo = lookup_base (b2, b1, ba_check, &bk);
206 code = MINUS_EXPR;
208 if (binfo == error_mark_node)
209 return error_mark_node;
211 if (bk == bk_via_virtual)
213 if (force)
214 warning ("pointer to member cast from `%T' to `%T' is via virtual base",
215 intype, type);
216 else
218 error ("pointer to member cast from `%T' to `%T' is via virtual base",
219 intype, type);
220 return error_mark_node;
222 /* This is a reinterpret cast, whose result is unspecified.
223 We choose to do nothing. */
224 return build1 (NOP_EXPR, type, expr);
227 if (TREE_CODE (expr) == PTRMEM_CST)
228 expr = cplus_expand_constant (expr);
230 if (binfo && !integer_zerop (BINFO_OFFSET (binfo)))
231 expr = size_binop (code,
232 build_nop (sizetype, expr),
233 BINFO_OFFSET (binfo));
234 return build_nop (type, expr);
236 else if (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype))
237 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), expr, 0);
238 else if (TYPE_PTRMEMFUNC_P (intype))
240 if (!warn_pmf2ptr)
242 if (TREE_CODE (expr) == PTRMEM_CST)
243 return cp_convert_to_pointer (type,
244 PTRMEM_CST_MEMBER (expr),
245 force);
246 else if (TREE_CODE (expr) == OFFSET_REF)
248 tree object = TREE_OPERAND (expr, 0);
249 return get_member_function_from_ptrfunc (&object,
250 TREE_OPERAND (expr, 1));
253 error ("cannot convert `%E' from type `%T' to type `%T'",
254 expr, intype, type);
255 return error_mark_node;
258 if (integer_zerop (expr))
260 if (TYPE_PTRMEMFUNC_P (type))
261 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), expr, 0);
263 if (TYPE_PTRMEM_P (type))
265 /* A NULL pointer-to-member is represented by -1, not by
266 zero. */
267 expr = build_int_cst (type, -1);
268 /* Fix up the representation of -1 if appropriate. */
269 expr = force_fit_type (expr, 0, false, false);
271 else
272 expr = build_int_cst (type, 0);
274 return expr;
276 else if (TYPE_PTR_TO_MEMBER_P (type) && INTEGRAL_CODE_P (form))
278 error ("invalid conversion from '%T' to '%T'", intype, type);
279 return error_mark_node;
282 if (INTEGRAL_CODE_P (form))
284 if (TYPE_PRECISION (intype) == POINTER_SIZE)
285 return build1 (CONVERT_EXPR, type, expr);
286 expr = cp_convert (c_common_type_for_size (POINTER_SIZE, 0), expr);
287 /* Modes may be different but sizes should be the same. There
288 is supposed to be some integral type that is the same width
289 as a pointer. */
290 gcc_assert (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (expr)))
291 == GET_MODE_SIZE (TYPE_MODE (type)));
293 return convert_to_pointer (type, expr);
296 if (type_unknown_p (expr))
297 return instantiate_type (type, expr, tf_error | tf_warning);
299 error ("cannot convert `%E' from type `%T' to type `%T'",
300 expr, intype, type);
301 return error_mark_node;
304 /* Like convert, except permit conversions to take place which
305 are not normally allowed due to access restrictions
306 (such as conversion from sub-type to private super-type). */
308 static tree
309 convert_to_pointer_force (tree type, tree expr)
311 tree intype = TREE_TYPE (expr);
312 enum tree_code form = TREE_CODE (intype);
314 if (form == POINTER_TYPE)
316 intype = TYPE_MAIN_VARIANT (intype);
318 if (TYPE_MAIN_VARIANT (type) != intype
319 && TREE_CODE (TREE_TYPE (type)) == RECORD_TYPE
320 && IS_AGGR_TYPE (TREE_TYPE (type))
321 && IS_AGGR_TYPE (TREE_TYPE (intype))
322 && TREE_CODE (TREE_TYPE (intype)) == RECORD_TYPE)
324 enum tree_code code = PLUS_EXPR;
325 tree binfo;
327 binfo = lookup_base (TREE_TYPE (intype), TREE_TYPE (type),
328 ba_ignore, NULL);
329 if (!binfo)
331 binfo = lookup_base (TREE_TYPE (type), TREE_TYPE (intype),
332 ba_ignore, NULL);
333 code = MINUS_EXPR;
335 if (binfo == error_mark_node)
336 return error_mark_node;
337 if (binfo)
339 expr = build_base_path (code, expr, binfo, 0);
340 if (expr == error_mark_node)
341 return error_mark_node;
342 /* Add any qualifier conversions. */
343 if (!same_type_p (TREE_TYPE (TREE_TYPE (expr)),
344 TREE_TYPE (type)))
345 expr = build_nop (type, expr);
346 return expr;
351 return cp_convert_to_pointer (type, expr, true);
354 /* We are passing something to a function which requires a reference.
355 The type we are interested in is in TYPE. The initial
356 value we have to begin with is in ARG.
358 FLAGS controls how we manage access checking.
359 DIRECT_BIND in FLAGS controls how any temporaries are generated.
360 If DIRECT_BIND is set, DECL is the reference we're binding to. */
362 static tree
363 build_up_reference (tree type, tree arg, int flags, tree decl)
365 tree rval;
366 tree argtype = TREE_TYPE (arg);
367 tree target_type = TREE_TYPE (type);
369 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
371 if ((flags & DIRECT_BIND) && ! real_lvalue_p (arg))
373 /* Create a new temporary variable. We can't just use a TARGET_EXPR
374 here because it needs to live as long as DECL. */
375 tree targ = arg;
377 arg = make_temporary_var_for_ref_to_temp (decl, TREE_TYPE (arg));
379 /* Process the initializer for the declaration. */
380 DECL_INITIAL (arg) = targ;
381 cp_finish_decl (arg, targ, NULL_TREE,
382 LOOKUP_ONLYCONVERTING|DIRECT_BIND);
384 else if (!(flags & DIRECT_BIND) && ! lvalue_p (arg))
385 return get_target_expr (arg);
387 /* If we had a way to wrap this up, and say, if we ever needed its
388 address, transform all occurrences of the register, into a memory
389 reference we could win better. */
390 rval = build_unary_op (ADDR_EXPR, arg, 1);
391 if (rval == error_mark_node)
392 return error_mark_node;
394 if ((flags & LOOKUP_PROTECT)
395 && TYPE_MAIN_VARIANT (argtype) != TYPE_MAIN_VARIANT (target_type)
396 && IS_AGGR_TYPE (argtype)
397 && IS_AGGR_TYPE (target_type))
399 /* We go through lookup_base for the access control. */
400 tree binfo = lookup_base (argtype, target_type, ba_check, NULL);
401 if (binfo == error_mark_node)
402 return error_mark_node;
403 if (binfo == NULL_TREE)
404 return error_not_base_type (target_type, argtype);
405 rval = build_base_path (PLUS_EXPR, rval, binfo, 1);
407 else
408 rval
409 = convert_to_pointer_force (build_pointer_type (target_type), rval);
410 return build_nop (type, rval);
413 /* Subroutine of convert_to_reference. REFTYPE is the target reference type.
414 INTYPE is the original rvalue type and DECL is an optional _DECL node
415 for diagnostics.
417 [dcl.init.ref] says that if an rvalue is used to
418 initialize a reference, then the reference must be to a
419 non-volatile const type. */
421 static void
422 warn_ref_binding (tree reftype, tree intype, tree decl)
424 tree ttl = TREE_TYPE (reftype);
426 if (!CP_TYPE_CONST_NON_VOLATILE_P (ttl))
428 const char *msg;
430 if (CP_TYPE_VOLATILE_P (ttl) && decl)
431 msg = "initialization of volatile reference type `%#T' from rvalue of type `%T'";
432 else if (CP_TYPE_VOLATILE_P (ttl))
433 msg = "conversion to volatile reference type `%#T' from rvalue of type `%T'";
434 else if (decl)
435 msg = "initialization of non-const reference type `%#T' from rvalue of type `%T'";
436 else
437 msg = "conversion to non-const reference type `%#T' from rvalue of type `%T'";
439 pedwarn (msg, reftype, intype);
443 /* For C++: Only need to do one-level references, but cannot
444 get tripped up on signed/unsigned differences.
446 DECL is either NULL_TREE or the _DECL node for a reference that is being
447 initialized. It can be error_mark_node if we don't know the _DECL but
448 we know it's an initialization. */
450 tree
451 convert_to_reference (tree reftype, tree expr, int convtype,
452 int flags, tree decl)
454 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (reftype));
455 tree intype;
456 tree rval = NULL_TREE;
457 tree rval_as_conversion = NULL_TREE;
458 bool can_convert_intype_to_type;
460 if (TREE_CODE (type) == FUNCTION_TYPE
461 && TREE_TYPE (expr) == unknown_type_node)
462 expr = instantiate_type (type, expr,
463 (flags & LOOKUP_COMPLAIN)
464 ? tf_error | tf_warning : tf_none);
465 else
466 expr = convert_from_reference (expr);
468 if (expr == error_mark_node)
469 return error_mark_node;
471 intype = TREE_TYPE (expr);
473 gcc_assert (TREE_CODE (intype) != REFERENCE_TYPE);
475 intype = TYPE_MAIN_VARIANT (intype);
477 can_convert_intype_to_type = can_convert (type, intype);
478 if (!can_convert_intype_to_type
479 && (convtype & CONV_IMPLICIT) && IS_AGGR_TYPE (intype)
480 && ! (flags & LOOKUP_NO_CONVERSION))
482 /* Look for a user-defined conversion to lvalue that we can use. */
484 rval_as_conversion
485 = build_type_conversion (reftype, expr);
487 if (rval_as_conversion && rval_as_conversion != error_mark_node
488 && real_lvalue_p (rval_as_conversion))
490 expr = rval_as_conversion;
491 rval_as_conversion = NULL_TREE;
492 intype = type;
493 can_convert_intype_to_type = 1;
497 if (((convtype & CONV_STATIC) && can_convert (intype, type))
498 || ((convtype & CONV_IMPLICIT) && can_convert_intype_to_type))
500 if (flags & LOOKUP_COMPLAIN)
502 tree ttl = TREE_TYPE (reftype);
503 tree ttr = lvalue_type (expr);
505 if (! real_lvalue_p (expr))
506 warn_ref_binding (reftype, intype, decl);
508 if (! (convtype & CONV_CONST)
509 && !at_least_as_qualified_p (ttl, ttr))
510 pedwarn ("conversion from `%T' to `%T' discards qualifiers",
511 ttr, reftype);
514 return build_up_reference (reftype, expr, flags, decl);
516 else if ((convtype & CONV_REINTERPRET) && lvalue_p (expr))
518 /* When casting an lvalue to a reference type, just convert into
519 a pointer to the new type and deference it. This is allowed
520 by San Diego WP section 5.2.9 paragraph 12, though perhaps it
521 should be done directly (jason). (int &)ri ---> *(int*)&ri */
523 /* B* bp; A& ar = (A&)bp; is valid, but it's probably not what they
524 meant. */
525 if (TREE_CODE (intype) == POINTER_TYPE
526 && (comptypes (TREE_TYPE (intype), type,
527 COMPARE_BASE | COMPARE_DERIVED)))
528 warning ("casting `%T' to `%T' does not dereference pointer",
529 intype, reftype);
531 rval = build_unary_op (ADDR_EXPR, expr, 0);
532 if (rval != error_mark_node)
533 rval = convert_force (build_pointer_type (TREE_TYPE (reftype)),
534 rval, 0);
535 if (rval != error_mark_node)
536 rval = build1 (NOP_EXPR, reftype, rval);
538 else
540 rval = convert_for_initialization (NULL_TREE, type, expr, flags,
541 "converting", 0, 0);
542 if (rval == NULL_TREE || rval == error_mark_node)
543 return rval;
544 warn_ref_binding (reftype, intype, decl);
545 rval = build_up_reference (reftype, rval, flags, decl);
548 if (rval)
550 /* If we found a way to convert earlier, then use it. */
551 return rval;
554 if (flags & LOOKUP_COMPLAIN)
555 error ("cannot convert type `%T' to type `%T'", intype, reftype);
557 return error_mark_node;
560 /* We are using a reference VAL for its value. Bash that reference all the
561 way down to its lowest form. */
563 tree
564 convert_from_reference (tree val)
566 if (TREE_CODE (TREE_TYPE (val)) == REFERENCE_TYPE)
567 return build_indirect_ref (val, NULL);
568 return val;
571 /* Implicitly convert the lvalue EXPR to another lvalue of type TOTYPE,
572 preserving cv-qualification. */
574 tree
575 convert_lvalue (tree totype, tree expr)
577 totype = cp_build_qualified_type (totype, TYPE_QUALS (TREE_TYPE (expr)));
578 totype = build_reference_type (totype);
579 expr = convert_to_reference (totype, expr, CONV_IMPLICIT, LOOKUP_NORMAL,
580 NULL_TREE);
581 return convert_from_reference (expr);
584 /* Really perform an lvalue-to-rvalue conversion, including copying an
585 argument of class type into a temporary. */
587 tree
588 force_rvalue (tree expr)
590 if (IS_AGGR_TYPE (TREE_TYPE (expr)) && TREE_CODE (expr) != TARGET_EXPR)
591 expr = ocp_convert (TREE_TYPE (expr), expr,
592 CONV_IMPLICIT|CONV_FORCE_TEMP, LOOKUP_NORMAL);
593 else
594 expr = decay_conversion (expr);
596 return expr;
599 /* C++ conversions, preference to static cast conversions. */
601 tree
602 cp_convert (tree type, tree expr)
604 return ocp_convert (type, expr, CONV_OLD_CONVERT, LOOKUP_NORMAL);
607 /* Conversion...
609 FLAGS indicates how we should behave. */
611 tree
612 ocp_convert (tree type, tree expr, int convtype, int flags)
614 tree e = expr;
615 enum tree_code code = TREE_CODE (type);
617 if (error_operand_p (e) || type == error_mark_node)
618 return error_mark_node;
620 complete_type (type);
621 complete_type (TREE_TYPE (expr));
623 e = decl_constant_value (e);
625 if (IS_AGGR_TYPE (type) && (convtype & CONV_FORCE_TEMP)
626 /* Some internal structures (vtable_entry_type, sigtbl_ptr_type)
627 don't go through finish_struct, so they don't have the synthesized
628 constructors. So don't force a temporary. */
629 && TYPE_HAS_CONSTRUCTOR (type))
630 /* We need a new temporary; don't take this shortcut. */;
631 else if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (TREE_TYPE (e)))
633 if (same_type_p (type, TREE_TYPE (e)))
634 /* The call to fold will not always remove the NOP_EXPR as
635 might be expected, since if one of the types is a typedef;
636 the comparison in fold is just equality of pointers, not a
637 call to comptypes. We don't call fold in this case because
638 that can result in infinite recursion; fold will call
639 convert, which will call ocp_convert, etc. */
640 return e;
641 /* For complex data types, we need to perform componentwise
642 conversion. */
643 else if (TREE_CODE (type) == COMPLEX_TYPE)
644 return fold (convert_to_complex (type, e));
645 else if (TREE_CODE (e) == TARGET_EXPR)
647 /* Don't build a NOP_EXPR of class type. Instead, change the
648 type of the temporary. Only allow this for cv-qual changes,
649 though. */
650 gcc_assert (same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (e)),
651 TYPE_MAIN_VARIANT (type)));
652 TREE_TYPE (e) = TREE_TYPE (TARGET_EXPR_SLOT (e)) = type;
653 return e;
655 else
657 /* We shouldn't be treating objects of ADDRESSABLE type as
658 rvalues. */
659 gcc_assert (!TREE_ADDRESSABLE (type));
660 return fold (build1 (NOP_EXPR, type, e));
664 if (code == VOID_TYPE && (convtype & CONV_STATIC))
666 e = convert_to_void (e, /*implicit=*/NULL);
667 return e;
670 if (INTEGRAL_CODE_P (code))
672 tree intype = TREE_TYPE (e);
673 /* enum = enum, enum = int, enum = float, (enum)pointer are all
674 errors. */
675 if (TREE_CODE (type) == ENUMERAL_TYPE
676 && ((ARITHMETIC_TYPE_P (intype) && ! (convtype & CONV_STATIC))
677 || (TREE_CODE (intype) == POINTER_TYPE)))
679 pedwarn ("conversion from `%#T' to `%#T'", intype, type);
681 if (flag_pedantic_errors)
682 return error_mark_node;
684 if (IS_AGGR_TYPE (intype))
686 tree rval;
687 rval = build_type_conversion (type, e);
688 if (rval)
689 return rval;
690 if (flags & LOOKUP_COMPLAIN)
691 error ("`%#T' used where a `%T' was expected", intype, type);
692 return error_mark_node;
694 if (code == BOOLEAN_TYPE)
695 return cp_truthvalue_conversion (e);
697 return fold (convert_to_integer (type, e));
699 if (POINTER_TYPE_P (type) || TYPE_PTR_TO_MEMBER_P (type))
700 return fold (cp_convert_to_pointer (type, e, false));
701 if (code == VECTOR_TYPE)
703 tree in_vtype = TREE_TYPE (e);
704 if (IS_AGGR_TYPE (in_vtype))
706 tree ret_val;
707 ret_val = build_type_conversion (type, e);
708 if (ret_val)
709 return ret_val;
710 if (flags & LOOKUP_COMPLAIN)
711 error ("`%#T' used where a `%T' was expected", in_vtype, type);
712 return error_mark_node;
714 return fold (convert_to_vector (type, e));
716 if (code == REAL_TYPE || code == COMPLEX_TYPE)
718 if (IS_AGGR_TYPE (TREE_TYPE (e)))
720 tree rval;
721 rval = build_type_conversion (type, e);
722 if (rval)
723 return rval;
724 else
725 if (flags & LOOKUP_COMPLAIN)
726 error ("`%#T' used where a floating point value was expected",
727 TREE_TYPE (e));
729 if (code == REAL_TYPE)
730 return fold (convert_to_real (type, e));
731 else if (code == COMPLEX_TYPE)
732 return fold (convert_to_complex (type, e));
735 /* New C++ semantics: since assignment is now based on
736 memberwise copying, if the rhs type is derived from the
737 lhs type, then we may still do a conversion. */
738 if (IS_AGGR_TYPE_CODE (code))
740 tree dtype = TREE_TYPE (e);
741 tree ctor = NULL_TREE;
743 dtype = TYPE_MAIN_VARIANT (dtype);
745 /* Conversion between aggregate types. New C++ semantics allow
746 objects of derived type to be cast to objects of base type.
747 Old semantics only allowed this between pointers.
749 There may be some ambiguity between using a constructor
750 vs. using a type conversion operator when both apply. */
752 ctor = e;
754 if (abstract_virtuals_error (NULL_TREE, type))
755 return error_mark_node;
757 if ((flags & LOOKUP_ONLYCONVERTING)
758 && ! (IS_AGGR_TYPE (dtype) && DERIVED_FROM_P (type, dtype)))
759 /* For copy-initialization, first we create a temp of the proper type
760 with a user-defined conversion sequence, then we direct-initialize
761 the target with the temp (see [dcl.init]). */
762 ctor = build_user_type_conversion (type, ctor, flags);
763 else
764 ctor = build_special_member_call (NULL_TREE,
765 complete_ctor_identifier,
766 build_tree_list (NULL_TREE, ctor),
767 type, flags);
768 if (ctor)
769 return build_cplus_new (type, ctor);
772 if (flags & LOOKUP_COMPLAIN)
773 error ("conversion from `%T' to non-scalar type `%T' requested",
774 TREE_TYPE (expr), type);
775 return error_mark_node;
778 /* When an expression is used in a void context, its value is discarded and
779 no lvalue-rvalue and similar conversions happen [expr.static.cast/4,
780 stmt.expr/1, expr.comma/1]. This permits dereferencing an incomplete type
781 in a void context. The C++ standard does not define what an `access' to an
782 object is, but there is reason to believe that it is the lvalue to rvalue
783 conversion -- if it were not, `*&*p = 1' would violate [expr]/4 in that it
784 accesses `*p' not to calculate the value to be stored. But, dcl.type.cv/8
785 indicates that volatile semantics should be the same between C and C++
786 where ever possible. C leaves it implementation defined as to what
787 constitutes an access to a volatile. So, we interpret `*vp' as a read of
788 the volatile object `vp' points to, unless that is an incomplete type. For
789 volatile references we do not do this interpretation, because that would
790 make it impossible to ignore the reference return value from functions. We
791 issue warnings in the confusing cases.
793 IMPLICIT is tells us the context of an implicit void conversion. */
795 tree
796 convert_to_void (tree expr, const char *implicit)
798 if (expr == error_mark_node
799 || TREE_TYPE (expr) == error_mark_node)
800 return error_mark_node;
801 if (!TREE_TYPE (expr))
802 return expr;
803 if (invalid_nonstatic_memfn_p (expr))
804 return error_mark_node;
805 if (VOID_TYPE_P (TREE_TYPE (expr)))
806 return expr;
807 switch (TREE_CODE (expr))
809 case COND_EXPR:
811 /* The two parts of a cond expr might be separate lvalues. */
812 tree op1 = TREE_OPERAND (expr,1);
813 tree op2 = TREE_OPERAND (expr,2);
814 tree new_op1 = convert_to_void
815 (op1, (implicit && !TREE_SIDE_EFFECTS (op2)
816 ? "second operand of conditional" : NULL));
817 tree new_op2 = convert_to_void
818 (op2, (implicit && !TREE_SIDE_EFFECTS (op1)
819 ? "third operand of conditional" : NULL));
821 expr = build3 (COND_EXPR, TREE_TYPE (new_op1),
822 TREE_OPERAND (expr, 0), new_op1, new_op2);
823 break;
826 case COMPOUND_EXPR:
828 /* The second part of a compound expr contains the value. */
829 tree op1 = TREE_OPERAND (expr,1);
830 tree new_op1 = convert_to_void
831 (op1, (implicit && !TREE_NO_WARNING (expr)
832 ? "right-hand operand of comma" : NULL));
834 if (new_op1 != op1)
836 tree t = build2 (COMPOUND_EXPR, TREE_TYPE (new_op1),
837 TREE_OPERAND (expr, 0), new_op1);
838 expr = t;
841 break;
844 case NON_LVALUE_EXPR:
845 case NOP_EXPR:
846 /* These have already decayed to rvalue. */
847 break;
849 case CALL_EXPR: /* We have a special meaning for volatile void fn(). */
850 break;
852 case INDIRECT_REF:
854 tree type = TREE_TYPE (expr);
855 int is_reference = TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0)))
856 == REFERENCE_TYPE;
857 int is_volatile = TYPE_VOLATILE (type);
858 int is_complete = COMPLETE_TYPE_P (complete_type (type));
860 if (is_volatile && !is_complete)
861 warning ("object of incomplete type `%T' will not be accessed in %s",
862 type, implicit ? implicit : "void context");
863 else if (is_reference && is_volatile)
864 warning ("object of type `%T' will not be accessed in %s",
865 TREE_TYPE (TREE_OPERAND (expr, 0)),
866 implicit ? implicit : "void context");
867 if (is_reference || !is_volatile || !is_complete)
868 expr = TREE_OPERAND (expr, 0);
870 break;
873 case VAR_DECL:
875 /* External variables might be incomplete. */
876 tree type = TREE_TYPE (expr);
877 int is_complete = COMPLETE_TYPE_P (complete_type (type));
879 if (TYPE_VOLATILE (type) && !is_complete)
880 warning ("object `%E' of incomplete type `%T' will not be accessed in %s",
881 expr, type, implicit ? implicit : "void context");
882 break;
885 default:;
888 tree probe = expr;
890 if (TREE_CODE (probe) == ADDR_EXPR)
891 probe = TREE_OPERAND (expr, 0);
892 if (type_unknown_p (probe))
894 /* [over.over] enumerates the places where we can take the address
895 of an overloaded function, and this is not one of them. */
896 pedwarn ("%s cannot resolve address of overloaded function",
897 implicit ? implicit : "void cast");
898 expr = void_zero_node;
900 else if (implicit && probe == expr && is_overloaded_fn (probe))
901 /* Only warn when there is no &. */
902 warning ("%s is a reference, not call, to function `%E'",
903 implicit, expr);
906 if (expr != error_mark_node && !VOID_TYPE_P (TREE_TYPE (expr)))
908 if (implicit && warn_unused_value
909 && !TREE_SIDE_EFFECTS (expr) && !TREE_NO_WARNING (expr))
910 warning ("%s has no effect", implicit);
911 expr = build1 (CONVERT_EXPR, void_type_node, expr);
913 return expr;
916 /* Create an expression whose value is that of EXPR,
917 converted to type TYPE. The TREE_TYPE of the value
918 is always TYPE. This function implements all reasonable
919 conversions; callers should filter out those that are
920 not permitted by the language being compiled.
922 Most of this routine is from build_reinterpret_cast.
924 The backend cannot call cp_convert (what was convert) because
925 conversions to/from basetypes may involve memory references
926 (vbases) and adding or subtracting small values (multiple
927 inheritance), but it calls convert from the constant folding code
928 on subtrees of already built trees after it has ripped them apart.
930 Also, if we ever support range variables, we'll probably also have to
931 do a little bit more work. */
933 tree
934 convert (tree type, tree expr)
936 tree intype;
938 if (type == error_mark_node || expr == error_mark_node)
939 return error_mark_node;
941 intype = TREE_TYPE (expr);
943 if (POINTER_TYPE_P (type) && POINTER_TYPE_P (intype))
945 expr = decl_constant_value (expr);
946 return fold (build1 (NOP_EXPR, type, expr));
949 return ocp_convert (type, expr, CONV_OLD_CONVERT,
950 LOOKUP_NORMAL|LOOKUP_NO_CONVERSION);
953 /* Like cp_convert, except permit conversions to take place which
954 are not normally allowed due to access restrictions
955 (such as conversion from sub-type to private super-type). */
957 tree
958 convert_force (tree type, tree expr, int convtype)
960 tree e = expr;
961 enum tree_code code = TREE_CODE (type);
963 if (code == REFERENCE_TYPE)
964 return fold (convert_to_reference (type, e, CONV_C_CAST, LOOKUP_COMPLAIN,
965 NULL_TREE));
966 else if (TREE_CODE (TREE_TYPE (e)) == REFERENCE_TYPE)
967 e = convert_from_reference (e);
969 if (code == POINTER_TYPE)
970 return fold (convert_to_pointer_force (type, e));
972 /* From typeck.c convert_for_assignment */
973 if (((TREE_CODE (TREE_TYPE (e)) == POINTER_TYPE && TREE_CODE (e) == ADDR_EXPR
974 && TREE_CODE (TREE_TYPE (e)) == POINTER_TYPE
975 && TREE_CODE (TREE_TYPE (TREE_TYPE (e))) == METHOD_TYPE)
976 || integer_zerop (e)
977 || TYPE_PTRMEMFUNC_P (TREE_TYPE (e)))
978 && TYPE_PTRMEMFUNC_P (type))
980 /* compatible pointer to member functions. */
981 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), e, 1);
984 return ocp_convert (type, e, CONV_C_CAST|convtype, LOOKUP_NORMAL);
987 /* Convert an aggregate EXPR to type XTYPE. If a conversion
988 exists, return the attempted conversion. This may
989 return ERROR_MARK_NODE if the conversion is not
990 allowed (references private members, etc).
991 If no conversion exists, NULL_TREE is returned.
993 FIXME: Ambiguity checking is wrong. Should choose one by the implicit
994 object parameter, or by the second standard conversion sequence if
995 that doesn't do it. This will probably wait for an overloading rewrite.
996 (jason 8/9/95) */
998 tree
999 build_type_conversion (tree xtype, tree expr)
1001 /* C++: check to see if we can convert this aggregate type
1002 into the required type. */
1003 return build_user_type_conversion (xtype, expr, LOOKUP_NORMAL);
1006 /* Convert the given EXPR to one of a group of types suitable for use in an
1007 expression. DESIRES is a combination of various WANT_* flags (q.v.)
1008 which indicates which types are suitable. If COMPLAIN is true, complain
1009 about ambiguity; otherwise, the caller will deal with it. */
1011 tree
1012 build_expr_type_conversion (int desires, tree expr, bool complain)
1014 tree basetype = TREE_TYPE (expr);
1015 tree conv = NULL_TREE;
1016 tree winner = NULL_TREE;
1018 if (expr == null_node
1019 && (desires & WANT_INT)
1020 && !(desires & WANT_NULL))
1021 warning ("converting NULL to non-pointer type");
1023 expr = convert_from_reference (expr);
1024 basetype = TREE_TYPE (expr);
1026 if (basetype == error_mark_node)
1027 return error_mark_node;
1029 if (! IS_AGGR_TYPE (basetype))
1030 switch (TREE_CODE (basetype))
1032 case INTEGER_TYPE:
1033 if ((desires & WANT_NULL) && null_ptr_cst_p (expr))
1034 return expr;
1035 /* else fall through... */
1037 case BOOLEAN_TYPE:
1038 return (desires & WANT_INT) ? expr : NULL_TREE;
1039 case ENUMERAL_TYPE:
1040 return (desires & WANT_ENUM) ? expr : NULL_TREE;
1041 case REAL_TYPE:
1042 return (desires & WANT_FLOAT) ? expr : NULL_TREE;
1043 case POINTER_TYPE:
1044 return (desires & WANT_POINTER) ? expr : NULL_TREE;
1046 case FUNCTION_TYPE:
1047 case ARRAY_TYPE:
1048 return (desires & WANT_POINTER) ? decay_conversion (expr)
1049 : NULL_TREE;
1050 default:
1051 return NULL_TREE;
1054 /* The code for conversions from class type is currently only used for
1055 delete expressions. Other expressions are handled by build_new_op. */
1056 if (!complete_type_or_else (basetype, expr))
1057 return error_mark_node;
1058 if (!TYPE_HAS_CONVERSION (basetype))
1059 return NULL_TREE;
1061 for (conv = lookup_conversions (basetype); conv; conv = TREE_CHAIN (conv))
1063 int win = 0;
1064 tree candidate;
1065 tree cand = TREE_VALUE (conv);
1067 if (winner && winner == cand)
1068 continue;
1070 candidate = non_reference (TREE_TYPE (TREE_TYPE (cand)));
1072 switch (TREE_CODE (candidate))
1074 case BOOLEAN_TYPE:
1075 case INTEGER_TYPE:
1076 win = (desires & WANT_INT); break;
1077 case ENUMERAL_TYPE:
1078 win = (desires & WANT_ENUM); break;
1079 case REAL_TYPE:
1080 win = (desires & WANT_FLOAT); break;
1081 case POINTER_TYPE:
1082 win = (desires & WANT_POINTER); break;
1084 default:
1085 break;
1088 if (win)
1090 if (winner)
1092 if (complain)
1094 error ("ambiguous default type conversion from `%T'",
1095 basetype);
1096 error (" candidate conversions include `%D' and `%D'",
1097 winner, cand);
1099 return error_mark_node;
1101 else
1102 winner = cand;
1106 if (winner)
1108 tree type = non_reference (TREE_TYPE (TREE_TYPE (winner)));
1109 return build_user_type_conversion (type, expr, LOOKUP_NORMAL);
1112 return NULL_TREE;
1115 /* Implements integral promotion (4.1) and float->double promotion. */
1117 tree
1118 type_promotes_to (tree type)
1120 if (type == error_mark_node)
1121 return error_mark_node;
1123 type = TYPE_MAIN_VARIANT (type);
1125 /* bool always promotes to int (not unsigned), even if it's the same
1126 size. */
1127 if (type == boolean_type_node)
1128 type = integer_type_node;
1130 /* Normally convert enums to int, but convert wide enums to something
1131 wider. */
1132 else if (TREE_CODE (type) == ENUMERAL_TYPE
1133 || type == wchar_type_node)
1135 int precision = MAX (TYPE_PRECISION (type),
1136 TYPE_PRECISION (integer_type_node));
1137 tree totype = c_common_type_for_size (precision, 0);
1138 if (TYPE_UNSIGNED (type)
1139 && ! int_fits_type_p (TYPE_MAX_VALUE (type), totype))
1140 type = c_common_type_for_size (precision, 1);
1141 else
1142 type = totype;
1144 else if (c_promoting_integer_type_p (type))
1146 /* Retain unsignedness if really not getting bigger. */
1147 if (TYPE_UNSIGNED (type)
1148 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
1149 type = unsigned_type_node;
1150 else
1151 type = integer_type_node;
1153 else if (type == float_type_node)
1154 type = double_type_node;
1156 return type;
1159 /* The routines below this point are carefully written to conform to
1160 the standard. They use the same terminology, and follow the rules
1161 closely. Although they are used only in pt.c at the moment, they
1162 should presumably be used everywhere in the future. */
1164 /* Attempt to perform qualification conversions on EXPR to convert it
1165 to TYPE. Return the resulting expression, or error_mark_node if
1166 the conversion was impossible. */
1168 tree
1169 perform_qualification_conversions (tree type, tree expr)
1171 tree expr_type;
1173 expr_type = TREE_TYPE (expr);
1175 if (TYPE_PTR_P (type) && TYPE_PTR_P (expr_type)
1176 && comp_ptr_ttypes (TREE_TYPE (type), TREE_TYPE (expr_type)))
1177 return build_nop (type, expr);
1178 else if (TYPE_PTR_TO_MEMBER_P (type)
1179 && TYPE_PTR_TO_MEMBER_P (expr_type)
1180 && same_type_p (TYPE_PTRMEM_CLASS_TYPE (type),
1181 TYPE_PTRMEM_CLASS_TYPE (expr_type))
1182 && comp_ptr_ttypes (TYPE_PTRMEM_POINTED_TO_TYPE (type),
1183 TYPE_PTRMEM_POINTED_TO_TYPE (expr_type)))
1184 return build_nop (type, expr);
1185 else
1186 return error_mark_node;