* pt.c (tinst_for_decl): Remove.
[official-gcc.git] / gcc / cp / cvt.c
blob262970707e78304d9c9b77d65f2510d4c199ee51
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;
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 %qT to %qT",
91 intype, type);
92 return error_mark_node;
95 rval = build_type_conversion (type, expr);
96 if (rval)
98 if (rval == error_mark_node)
99 error ("conversion of %qE from %qT to %qT is ambiguous",
100 expr, intype, type);
101 return rval;
105 /* Handle anachronistic conversions from (::*)() to cv void* or (*)(). */
106 if (TREE_CODE (type) == POINTER_TYPE
107 && (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
108 || VOID_TYPE_P (TREE_TYPE (type))))
110 if (TYPE_PTRMEMFUNC_P (intype)
111 || TREE_CODE (intype) == METHOD_TYPE)
112 return convert_member_func_to_ptr (type, expr);
113 if (TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE)
114 return build_nop (type, expr);
115 intype = TREE_TYPE (expr);
118 if (expr == error_mark_node)
119 return error_mark_node;
121 form = TREE_CODE (intype);
123 if (POINTER_TYPE_P (intype))
125 intype = TYPE_MAIN_VARIANT (intype);
127 if (TYPE_MAIN_VARIANT (type) != intype
128 && TREE_CODE (type) == POINTER_TYPE
129 && TREE_CODE (TREE_TYPE (type)) == RECORD_TYPE
130 && 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;
135 tree binfo;
136 tree intype_class;
137 tree type_class;
138 bool same_p;
140 intype_class = TREE_TYPE (intype);
141 type_class = TREE_TYPE (type);
143 same_p = same_type_p (TYPE_MAIN_VARIANT (intype_class),
144 TYPE_MAIN_VARIANT (type_class));
145 binfo = NULL_TREE;
146 /* Try derived to base conversion. */
147 if (!same_p)
148 binfo = lookup_base (intype_class, type_class, ba_check, NULL);
149 if (!same_p && !binfo)
151 /* Try base to derived conversion. */
152 binfo = lookup_base (type_class, intype_class, ba_check, NULL);
153 code = MINUS_EXPR;
155 if (binfo == error_mark_node)
156 return error_mark_node;
157 if (binfo || same_p)
159 if (binfo)
160 expr = build_base_path (code, expr, binfo, 0);
161 /* Add any qualifier conversions. */
162 return build_nop (type, expr);
166 if (TYPE_PTRMEMFUNC_P (type))
168 error ("cannot convert %qE from type %qT to type %qT",
169 expr, intype, type);
170 return error_mark_node;
173 return build_nop (type, expr);
175 else if (TYPE_PTRMEM_P (type) && TYPE_PTRMEM_P (intype))
177 tree b1;
178 tree b2;
179 tree binfo;
180 enum tree_code code = PLUS_EXPR;
181 base_kind bk;
183 b1 = TYPE_PTRMEM_CLASS_TYPE (type);
184 b2 = TYPE_PTRMEM_CLASS_TYPE (intype);
185 binfo = lookup_base (b1, b2, ba_check, &bk);
186 if (!binfo)
188 binfo = lookup_base (b2, b1, ba_check, &bk);
189 code = MINUS_EXPR;
191 if (binfo == error_mark_node)
192 return error_mark_node;
194 if (bk == bk_via_virtual)
196 if (force)
197 warning ("pointer to member cast from %qT to %qT is via"
198 " virtual base", intype, type);
199 else
201 error ("pointer to member cast from %qT to %qT is"
202 " via virtual base", intype, type);
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 /*c_cast_p=*/false);
222 else if (TYPE_PTRMEMFUNC_P (intype))
224 if (!warn_pmf2ptr)
226 if (TREE_CODE (expr) == PTRMEM_CST)
227 return cp_convert_to_pointer (type,
228 PTRMEM_CST_MEMBER (expr),
229 force);
230 else if (TREE_CODE (expr) == OFFSET_REF)
232 tree object = TREE_OPERAND (expr, 0);
233 return get_member_function_from_ptrfunc (&object,
234 TREE_OPERAND (expr, 1));
237 error ("cannot convert %qE from type %qT to type %qT",
238 expr, intype, type);
239 return error_mark_node;
242 if (integer_zerop (expr))
244 if (TYPE_PTRMEMFUNC_P (type))
245 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), expr, 0,
246 /*c_cast_p=*/false);
248 if (TYPE_PTRMEM_P (type))
250 /* A NULL pointer-to-member is represented by -1, not by
251 zero. */
252 expr = build_int_cst (type, -1);
253 /* Fix up the representation of -1 if appropriate. */
254 expr = force_fit_type (expr, 0, false, false);
256 else
257 expr = build_int_cst (type, 0);
259 return expr;
261 else if (TYPE_PTR_TO_MEMBER_P (type) && INTEGRAL_CODE_P (form))
263 error ("invalid conversion from %qT to %qT", intype, type);
264 return error_mark_node;
267 if (INTEGRAL_CODE_P (form))
269 if (TYPE_PRECISION (intype) == POINTER_SIZE)
270 return build1 (CONVERT_EXPR, type, expr);
271 expr = cp_convert (c_common_type_for_size (POINTER_SIZE, 0), expr);
272 /* Modes may be different but sizes should be the same. There
273 is supposed to be some integral type that is the same width
274 as a pointer. */
275 gcc_assert (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (expr)))
276 == GET_MODE_SIZE (TYPE_MODE (type)));
278 return convert_to_pointer (type, expr);
281 if (type_unknown_p (expr))
282 return instantiate_type (type, expr, tf_error | tf_warning);
284 error ("cannot convert %qE from type %qT to type %qT",
285 expr, intype, type);
286 return error_mark_node;
289 /* Like convert, except permit conversions to take place which
290 are not normally allowed due to access restrictions
291 (such as conversion from sub-type to private super-type). */
293 static tree
294 convert_to_pointer_force (tree type, tree expr)
296 tree intype = TREE_TYPE (expr);
297 enum tree_code form = TREE_CODE (intype);
299 if (form == POINTER_TYPE)
301 intype = TYPE_MAIN_VARIANT (intype);
303 if (TYPE_MAIN_VARIANT (type) != intype
304 && TREE_CODE (TREE_TYPE (type)) == RECORD_TYPE
305 && IS_AGGR_TYPE (TREE_TYPE (type))
306 && IS_AGGR_TYPE (TREE_TYPE (intype))
307 && TREE_CODE (TREE_TYPE (intype)) == RECORD_TYPE)
309 enum tree_code code = PLUS_EXPR;
310 tree binfo;
312 binfo = lookup_base (TREE_TYPE (intype), TREE_TYPE (type),
313 ba_unique, NULL);
314 if (!binfo)
316 binfo = lookup_base (TREE_TYPE (type), TREE_TYPE (intype),
317 ba_unique, NULL);
318 code = MINUS_EXPR;
320 if (binfo == error_mark_node)
321 return error_mark_node;
322 if (binfo)
324 expr = build_base_path (code, expr, binfo, 0);
325 if (expr == error_mark_node)
326 return error_mark_node;
327 /* Add any qualifier conversions. */
328 if (!same_type_p (TREE_TYPE (TREE_TYPE (expr)),
329 TREE_TYPE (type)))
330 expr = build_nop (type, expr);
331 return expr;
336 return cp_convert_to_pointer (type, expr, true);
339 /* We are passing something to a function which requires a reference.
340 The type we are interested in is in TYPE. The initial
341 value we have to begin with is in ARG.
343 FLAGS controls how we manage access checking.
344 DIRECT_BIND in FLAGS controls how any temporaries are generated.
345 If DIRECT_BIND is set, DECL is the reference we're binding to. */
347 static tree
348 build_up_reference (tree type, tree arg, int flags, tree decl)
350 tree rval;
351 tree argtype = TREE_TYPE (arg);
352 tree target_type = TREE_TYPE (type);
354 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
356 if ((flags & DIRECT_BIND) && ! real_lvalue_p (arg))
358 /* Create a new temporary variable. We can't just use a TARGET_EXPR
359 here because it needs to live as long as DECL. */
360 tree targ = arg;
362 arg = make_temporary_var_for_ref_to_temp (decl, TREE_TYPE (arg));
364 /* Process the initializer for the declaration. */
365 DECL_INITIAL (arg) = targ;
366 cp_finish_decl (arg, targ, NULL_TREE,
367 LOOKUP_ONLYCONVERTING|DIRECT_BIND);
369 else if (!(flags & DIRECT_BIND) && ! lvalue_p (arg))
370 return get_target_expr (arg);
372 /* If we had a way to wrap this up, and say, if we ever needed its
373 address, transform all occurrences of the register, into a memory
374 reference we could win better. */
375 rval = build_unary_op (ADDR_EXPR, arg, 1);
376 if (rval == error_mark_node)
377 return error_mark_node;
379 if ((flags & LOOKUP_PROTECT)
380 && TYPE_MAIN_VARIANT (argtype) != TYPE_MAIN_VARIANT (target_type)
381 && IS_AGGR_TYPE (argtype)
382 && IS_AGGR_TYPE (target_type))
384 /* We go through lookup_base for the access control. */
385 tree binfo = lookup_base (argtype, target_type, ba_check, NULL);
386 if (binfo == error_mark_node)
387 return error_mark_node;
388 if (binfo == NULL_TREE)
389 return error_not_base_type (target_type, argtype);
390 rval = build_base_path (PLUS_EXPR, rval, binfo, 1);
392 else
393 rval
394 = convert_to_pointer_force (build_pointer_type (target_type), rval);
395 return build_nop (type, rval);
398 /* Subroutine of convert_to_reference. REFTYPE is the target reference type.
399 INTYPE is the original rvalue type and DECL is an optional _DECL node
400 for diagnostics.
402 [dcl.init.ref] says that if an rvalue is used to
403 initialize a reference, then the reference must be to a
404 non-volatile const type. */
406 static void
407 warn_ref_binding (tree reftype, tree intype, tree decl)
409 tree ttl = TREE_TYPE (reftype);
411 if (!CP_TYPE_CONST_NON_VOLATILE_P (ttl))
413 const char *msg;
415 if (CP_TYPE_VOLATILE_P (ttl) && decl)
416 msg = "initialization of volatile reference type %q#T from"
417 " rvalue of type %qT";
418 else if (CP_TYPE_VOLATILE_P (ttl))
419 msg = "conversion to volatile reference type %q#T "
420 " from rvalue of type %qT";
421 else if (decl)
422 msg = "initialization of non-const reference type %q#T from"
423 " rvalue of type %qT";
424 else
425 msg = "conversion to non-const reference type %q#T from"
426 " rvalue of type %qT";
428 pedwarn (msg, reftype, intype);
432 /* For C++: Only need to do one-level references, but cannot
433 get tripped up on signed/unsigned differences.
435 DECL is either NULL_TREE or the _DECL node for a reference that is being
436 initialized. It can be error_mark_node if we don't know the _DECL but
437 we know it's an initialization. */
439 tree
440 convert_to_reference (tree reftype, tree expr, int convtype,
441 int flags, tree decl)
443 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (reftype));
444 tree intype;
445 tree rval = NULL_TREE;
446 tree rval_as_conversion = NULL_TREE;
447 bool can_convert_intype_to_type;
449 if (TREE_CODE (type) == FUNCTION_TYPE
450 && TREE_TYPE (expr) == unknown_type_node)
451 expr = instantiate_type (type, expr,
452 (flags & LOOKUP_COMPLAIN)
453 ? tf_error | tf_warning : tf_none);
455 if (expr == error_mark_node)
456 return error_mark_node;
458 intype = TREE_TYPE (expr);
460 gcc_assert (TREE_CODE (intype) != REFERENCE_TYPE);
462 intype = TYPE_MAIN_VARIANT (intype);
464 can_convert_intype_to_type = can_convert (type, intype);
465 if (!can_convert_intype_to_type
466 && (convtype & CONV_IMPLICIT) && IS_AGGR_TYPE (intype)
467 && ! (flags & LOOKUP_NO_CONVERSION))
469 /* Look for a user-defined conversion to lvalue that we can use. */
471 rval_as_conversion
472 = build_type_conversion (reftype, expr);
474 if (rval_as_conversion && rval_as_conversion != error_mark_node
475 && real_lvalue_p (rval_as_conversion))
477 expr = rval_as_conversion;
478 rval_as_conversion = NULL_TREE;
479 intype = type;
480 can_convert_intype_to_type = 1;
484 if (((convtype & CONV_STATIC) && can_convert (intype, type))
485 || ((convtype & CONV_IMPLICIT) && can_convert_intype_to_type))
487 if (flags & LOOKUP_COMPLAIN)
489 tree ttl = TREE_TYPE (reftype);
490 tree ttr = lvalue_type (expr);
492 if (! real_lvalue_p (expr))
493 warn_ref_binding (reftype, intype, decl);
495 if (! (convtype & CONV_CONST)
496 && !at_least_as_qualified_p (ttl, ttr))
497 pedwarn ("conversion from %qT to %qT discards qualifiers",
498 ttr, reftype);
501 return build_up_reference (reftype, expr, flags, decl);
503 else if ((convtype & CONV_REINTERPRET) && lvalue_p (expr))
505 /* When casting an lvalue to a reference type, just convert into
506 a pointer to the new type and deference it. This is allowed
507 by San Diego WP section 5.2.9 paragraph 12, though perhaps it
508 should be done directly (jason). (int &)ri ---> *(int*)&ri */
510 /* B* bp; A& ar = (A&)bp; is valid, but it's probably not what they
511 meant. */
512 if (TREE_CODE (intype) == POINTER_TYPE
513 && (comptypes (TREE_TYPE (intype), type,
514 COMPARE_BASE | COMPARE_DERIVED)))
515 warning ("casting %qT to %qT does not dereference pointer",
516 intype, reftype);
518 rval = build_unary_op (ADDR_EXPR, expr, 0);
519 if (rval != error_mark_node)
520 rval = convert_force (build_pointer_type (TREE_TYPE (reftype)),
521 rval, 0);
522 if (rval != error_mark_node)
523 rval = build1 (NOP_EXPR, reftype, rval);
525 else
527 rval = convert_for_initialization (NULL_TREE, type, expr, flags,
528 "converting", 0, 0);
529 if (rval == NULL_TREE || rval == error_mark_node)
530 return rval;
531 warn_ref_binding (reftype, intype, decl);
532 rval = build_up_reference (reftype, rval, flags, decl);
535 if (rval)
537 /* If we found a way to convert earlier, then use it. */
538 return rval;
541 if (flags & LOOKUP_COMPLAIN)
542 error ("cannot convert type %qT to type %qT", intype, reftype);
544 return error_mark_node;
547 /* We are using a reference VAL for its value. Bash that reference all the
548 way down to its lowest form. */
550 tree
551 convert_from_reference (tree val)
553 if (TREE_CODE (TREE_TYPE (val)) == REFERENCE_TYPE)
555 tree t = canonical_type_variant (TREE_TYPE (TREE_TYPE (val)));
556 tree ref = build1 (INDIRECT_REF, t, val);
558 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
559 so that we get the proper error message if the result is used
560 to assign to. Also, &* is supposed to be a no-op. */
561 TREE_READONLY (ref) = CP_TYPE_CONST_P (t);
562 TREE_THIS_VOLATILE (ref) = CP_TYPE_VOLATILE_P (t);
563 TREE_SIDE_EFFECTS (ref)
564 = (TREE_THIS_VOLATILE (ref) || TREE_SIDE_EFFECTS (val));
565 REFERENCE_REF_P (ref) = 1;
566 val = ref;
569 return val;
572 /* Really perform an lvalue-to-rvalue conversion, including copying an
573 argument of class type into a temporary. */
575 tree
576 force_rvalue (tree expr)
578 if (IS_AGGR_TYPE (TREE_TYPE (expr)) && TREE_CODE (expr) != TARGET_EXPR)
579 expr = ocp_convert (TREE_TYPE (expr), expr,
580 CONV_IMPLICIT|CONV_FORCE_TEMP, LOOKUP_NORMAL);
581 else
582 expr = decay_conversion (expr);
584 return expr;
587 /* C++ conversions, preference to static cast conversions. */
589 tree
590 cp_convert (tree type, tree expr)
592 return ocp_convert (type, expr, CONV_OLD_CONVERT, LOOKUP_NORMAL);
595 /* Conversion...
597 FLAGS indicates how we should behave. */
599 tree
600 ocp_convert (tree type, tree expr, int convtype, int flags)
602 tree e = expr;
603 enum tree_code code = TREE_CODE (type);
605 if (error_operand_p (e) || type == error_mark_node)
606 return error_mark_node;
608 complete_type (type);
609 complete_type (TREE_TYPE (expr));
611 e = integral_constant_value (e);
613 if (IS_AGGR_TYPE (type) && (convtype & CONV_FORCE_TEMP)
614 /* Some internal structures (vtable_entry_type, sigtbl_ptr_type)
615 don't go through finish_struct, so they don't have the synthesized
616 constructors. So don't force a temporary. */
617 && TYPE_HAS_CONSTRUCTOR (type))
618 /* We need a new temporary; don't take this shortcut. */;
619 else if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (TREE_TYPE (e)))
621 if (same_type_p (type, TREE_TYPE (e)))
622 /* The call to fold will not always remove the NOP_EXPR as
623 might be expected, since if one of the types is a typedef;
624 the comparison in fold is just equality of pointers, not a
625 call to comptypes. We don't call fold in this case because
626 that can result in infinite recursion; fold will call
627 convert, which will call ocp_convert, etc. */
628 return e;
629 /* For complex data types, we need to perform componentwise
630 conversion. */
631 else if (TREE_CODE (type) == COMPLEX_TYPE)
632 return fold_if_not_in_template (convert_to_complex (type, e));
633 else if (TREE_CODE (e) == TARGET_EXPR)
635 /* Don't build a NOP_EXPR of class type. Instead, change the
636 type of the temporary. Only allow this for cv-qual changes,
637 though. */
638 gcc_assert (same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (e)),
639 TYPE_MAIN_VARIANT (type)));
640 TREE_TYPE (e) = TREE_TYPE (TARGET_EXPR_SLOT (e)) = type;
641 return e;
643 else
645 /* We shouldn't be treating objects of ADDRESSABLE type as
646 rvalues. */
647 gcc_assert (!TREE_ADDRESSABLE (type));
648 return fold_if_not_in_template (build_nop (type, e));
652 if (code == VOID_TYPE && (convtype & CONV_STATIC))
654 e = convert_to_void (e, /*implicit=*/NULL);
655 return e;
658 if (INTEGRAL_CODE_P (code))
660 tree intype = TREE_TYPE (e);
661 /* enum = enum, enum = int, enum = float, (enum)pointer are all
662 errors. */
663 if (TREE_CODE (type) == ENUMERAL_TYPE
664 && (((INTEGRAL_OR_ENUMERATION_TYPE_P (intype)
665 || TREE_CODE (intype) == REAL_TYPE)
666 && ! (convtype & CONV_STATIC))
667 || TREE_CODE (intype) == POINTER_TYPE))
669 if (flags & LOOKUP_COMPLAIN)
670 pedwarn ("conversion from %q#T to %q#T", intype, type);
672 if (flag_pedantic_errors)
673 return error_mark_node;
675 if (IS_AGGR_TYPE (intype))
677 tree rval;
678 rval = build_type_conversion (type, e);
679 if (rval)
680 return rval;
681 if (flags & LOOKUP_COMPLAIN)
682 error ("%q#T used where a %qT was expected", intype, type);
683 return error_mark_node;
685 if (code == BOOLEAN_TYPE)
686 return cp_truthvalue_conversion (e);
688 return fold_if_not_in_template (convert_to_integer (type, e));
690 if (POINTER_TYPE_P (type) || TYPE_PTR_TO_MEMBER_P (type))
691 return fold_if_not_in_template (cp_convert_to_pointer (type, e, false));
692 if (code == VECTOR_TYPE)
694 tree in_vtype = TREE_TYPE (e);
695 if (IS_AGGR_TYPE (in_vtype))
697 tree ret_val;
698 ret_val = build_type_conversion (type, e);
699 if (ret_val)
700 return ret_val;
701 if (flags & LOOKUP_COMPLAIN)
702 error ("%q#T used where a %qT was expected", in_vtype, type);
703 return error_mark_node;
705 return fold_if_not_in_template (convert_to_vector (type, e));
707 if (code == REAL_TYPE || code == COMPLEX_TYPE)
709 if (IS_AGGR_TYPE (TREE_TYPE (e)))
711 tree rval;
712 rval = build_type_conversion (type, e);
713 if (rval)
714 return rval;
715 else
716 if (flags & LOOKUP_COMPLAIN)
717 error ("%q#T used where a floating point value was expected",
718 TREE_TYPE (e));
720 if (code == REAL_TYPE)
721 return fold_if_not_in_template (convert_to_real (type, e));
722 else if (code == COMPLEX_TYPE)
723 return fold_if_not_in_template (convert_to_complex (type, e));
726 /* New C++ semantics: since assignment is now based on
727 memberwise copying, if the rhs type is derived from the
728 lhs type, then we may still do a conversion. */
729 if (IS_AGGR_TYPE_CODE (code))
731 tree dtype = TREE_TYPE (e);
732 tree ctor = NULL_TREE;
734 dtype = TYPE_MAIN_VARIANT (dtype);
736 /* Conversion between aggregate types. New C++ semantics allow
737 objects of derived type to be cast to objects of base type.
738 Old semantics only allowed this between pointers.
740 There may be some ambiguity between using a constructor
741 vs. using a type conversion operator when both apply. */
743 ctor = e;
745 if (abstract_virtuals_error (NULL_TREE, type))
746 return error_mark_node;
748 if ((flags & LOOKUP_ONLYCONVERTING)
749 && ! (IS_AGGR_TYPE (dtype) && DERIVED_FROM_P (type, dtype)))
750 /* For copy-initialization, first we create a temp of the proper type
751 with a user-defined conversion sequence, then we direct-initialize
752 the target with the temp (see [dcl.init]). */
753 ctor = build_user_type_conversion (type, ctor, flags);
754 else
755 ctor = build_special_member_call (NULL_TREE,
756 complete_ctor_identifier,
757 build_tree_list (NULL_TREE, ctor),
758 type, flags);
759 if (ctor)
760 return build_cplus_new (type, ctor);
763 if (flags & LOOKUP_COMPLAIN)
764 error ("conversion from %qT to non-scalar type %qT requested",
765 TREE_TYPE (expr), type);
766 return error_mark_node;
769 /* When an expression is used in a void context, its value is discarded and
770 no lvalue-rvalue and similar conversions happen [expr.static.cast/4,
771 stmt.expr/1, expr.comma/1]. This permits dereferencing an incomplete type
772 in a void context. The C++ standard does not define what an `access' to an
773 object is, but there is reason to believe that it is the lvalue to rvalue
774 conversion -- if it were not, `*&*p = 1' would violate [expr]/4 in that it
775 accesses `*p' not to calculate the value to be stored. But, dcl.type.cv/8
776 indicates that volatile semantics should be the same between C and C++
777 where ever possible. C leaves it implementation defined as to what
778 constitutes an access to a volatile. So, we interpret `*vp' as a read of
779 the volatile object `vp' points to, unless that is an incomplete type. For
780 volatile references we do not do this interpretation, because that would
781 make it impossible to ignore the reference return value from functions. We
782 issue warnings in the confusing cases.
784 IMPLICIT is tells us the context of an implicit void conversion. */
786 tree
787 convert_to_void (tree expr, const char *implicit)
789 if (expr == error_mark_node
790 || TREE_TYPE (expr) == error_mark_node)
791 return error_mark_node;
792 if (!TREE_TYPE (expr))
793 return expr;
794 if (invalid_nonstatic_memfn_p (expr))
795 return error_mark_node;
796 if (VOID_TYPE_P (TREE_TYPE (expr)))
797 return expr;
798 switch (TREE_CODE (expr))
800 case COND_EXPR:
802 /* The two parts of a cond expr might be separate lvalues. */
803 tree op1 = TREE_OPERAND (expr,1);
804 tree op2 = TREE_OPERAND (expr,2);
805 tree new_op1 = convert_to_void
806 (op1, (implicit && !TREE_SIDE_EFFECTS (op2)
807 ? "second operand of conditional" : NULL));
808 tree new_op2 = convert_to_void
809 (op2, (implicit && !TREE_SIDE_EFFECTS (op1)
810 ? "third operand of conditional" : NULL));
812 expr = build3 (COND_EXPR, TREE_TYPE (new_op1),
813 TREE_OPERAND (expr, 0), new_op1, new_op2);
814 break;
817 case COMPOUND_EXPR:
819 /* The second part of a compound expr contains the value. */
820 tree op1 = TREE_OPERAND (expr,1);
821 tree new_op1 = convert_to_void
822 (op1, (implicit && !TREE_NO_WARNING (expr)
823 ? "right-hand operand of comma" : NULL));
825 if (new_op1 != op1)
827 tree t = build2 (COMPOUND_EXPR, TREE_TYPE (new_op1),
828 TREE_OPERAND (expr, 0), new_op1);
829 expr = t;
832 break;
835 case NON_LVALUE_EXPR:
836 case NOP_EXPR:
837 /* These have already decayed to rvalue. */
838 break;
840 case CALL_EXPR: /* We have a special meaning for volatile void fn(). */
841 break;
843 case INDIRECT_REF:
845 tree type = TREE_TYPE (expr);
846 int is_reference = TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0)))
847 == REFERENCE_TYPE;
848 int is_volatile = TYPE_VOLATILE (type);
849 int is_complete = COMPLETE_TYPE_P (complete_type (type));
851 if (is_volatile && !is_complete)
852 warning ("object of incomplete type %qT will not be accessed in %s",
853 type, implicit ? implicit : "void context");
854 else if (is_reference && is_volatile)
855 warning ("object of type %qT will not be accessed in %s",
856 TREE_TYPE (TREE_OPERAND (expr, 0)),
857 implicit ? implicit : "void context");
858 if (is_reference || !is_volatile || !is_complete)
859 expr = TREE_OPERAND (expr, 0);
861 break;
864 case VAR_DECL:
866 /* External variables might be incomplete. */
867 tree type = TREE_TYPE (expr);
868 int is_complete = COMPLETE_TYPE_P (complete_type (type));
870 if (TYPE_VOLATILE (type) && !is_complete)
871 warning ("object %qE of incomplete type %qT will not be accessed in %s",
872 expr, type, implicit ? implicit : "void context");
873 break;
876 default:;
879 tree probe = expr;
881 if (TREE_CODE (probe) == ADDR_EXPR)
882 probe = TREE_OPERAND (expr, 0);
883 if (type_unknown_p (probe))
885 /* [over.over] enumerates the places where we can take the address
886 of an overloaded function, and this is not one of them. */
887 pedwarn ("%s cannot resolve address of overloaded function",
888 implicit ? implicit : "void cast");
889 expr = void_zero_node;
891 else if (implicit && probe == expr && is_overloaded_fn (probe))
892 /* Only warn when there is no &. */
893 warning ("%s is a reference, not call, to function %qE",
894 implicit, expr);
897 if (expr != error_mark_node && !VOID_TYPE_P (TREE_TYPE (expr)))
899 if (implicit && warn_unused_value && !TREE_NO_WARNING (expr))
901 /* The middle end does not warn about expressions that have
902 been explicitly cast to void, so we must do so here. */
903 if (!TREE_SIDE_EFFECTS (expr))
904 warning ("%s has no effect", implicit);
905 else
907 tree e;
908 enum tree_code code;
909 enum tree_code_class class;
911 e = expr;
912 /* We might like to warn about (say) "(int) f()", as the
913 cast has no effect, but the compiler itself will
914 generate implicit conversions under some
915 circumstances. (For example a block copy will be
916 turned into a call to "__builtin_memcpy", with a
917 conversion of the return value to an appropriate
918 type.) So, to avoid false positives, we strip
919 conversions. Do not use STRIP_NOPs because it will
920 not strip conversions to "void", as that is not a
921 mode-preserving conversion. */
922 while (TREE_CODE (e) == NOP_EXPR)
923 e = TREE_OPERAND (e, 0);
925 code = TREE_CODE (e);
926 class = TREE_CODE_CLASS (code);
927 if (class == tcc_comparison
928 || class == tcc_unary
929 || (class == tcc_binary
930 && !(code == MODIFY_EXPR
931 || code == INIT_EXPR
932 || code == PREDECREMENT_EXPR
933 || code == PREINCREMENT_EXPR
934 || code == POSTDECREMENT_EXPR
935 || code == POSTINCREMENT_EXPR)))
936 warning ("value computed is not used");
939 expr = build1 (CONVERT_EXPR, void_type_node, expr);
941 return expr;
944 /* Create an expression whose value is that of EXPR,
945 converted to type TYPE. The TREE_TYPE of the value
946 is always TYPE. This function implements all reasonable
947 conversions; callers should filter out those that are
948 not permitted by the language being compiled.
950 Most of this routine is from build_reinterpret_cast.
952 The backend cannot call cp_convert (what was convert) because
953 conversions to/from basetypes may involve memory references
954 (vbases) and adding or subtracting small values (multiple
955 inheritance), but it calls convert from the constant folding code
956 on subtrees of already built trees after it has ripped them apart.
958 Also, if we ever support range variables, we'll probably also have to
959 do a little bit more work. */
961 tree
962 convert (tree type, tree expr)
964 tree intype;
966 if (type == error_mark_node || expr == error_mark_node)
967 return error_mark_node;
969 intype = TREE_TYPE (expr);
971 if (POINTER_TYPE_P (type) && POINTER_TYPE_P (intype))
972 return fold_if_not_in_template (build_nop (type, expr));
974 return ocp_convert (type, expr, CONV_OLD_CONVERT,
975 LOOKUP_NORMAL|LOOKUP_NO_CONVERSION);
978 /* Like cp_convert, except permit conversions to take place which
979 are not normally allowed due to access restrictions
980 (such as conversion from sub-type to private super-type). */
982 tree
983 convert_force (tree type, tree expr, int convtype)
985 tree e = expr;
986 enum tree_code code = TREE_CODE (type);
988 if (code == REFERENCE_TYPE)
989 return (fold_if_not_in_template
990 (convert_to_reference (type, e, CONV_C_CAST, LOOKUP_COMPLAIN,
991 NULL_TREE)));
993 if (code == POINTER_TYPE)
994 return fold_if_not_in_template (convert_to_pointer_force (type, e));
996 /* From typeck.c convert_for_assignment */
997 if (((TREE_CODE (TREE_TYPE (e)) == POINTER_TYPE && TREE_CODE (e) == ADDR_EXPR
998 && TREE_CODE (TREE_TYPE (e)) == POINTER_TYPE
999 && TREE_CODE (TREE_TYPE (TREE_TYPE (e))) == METHOD_TYPE)
1000 || integer_zerop (e)
1001 || TYPE_PTRMEMFUNC_P (TREE_TYPE (e)))
1002 && TYPE_PTRMEMFUNC_P (type))
1003 /* compatible pointer to member functions. */
1004 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), e, 1,
1005 /*c_cast_p=*/1);
1007 return ocp_convert (type, e, CONV_C_CAST|convtype, LOOKUP_NORMAL);
1010 /* Convert an aggregate EXPR to type XTYPE. If a conversion
1011 exists, return the attempted conversion. This may
1012 return ERROR_MARK_NODE if the conversion is not
1013 allowed (references private members, etc).
1014 If no conversion exists, NULL_TREE is returned.
1016 FIXME: Ambiguity checking is wrong. Should choose one by the implicit
1017 object parameter, or by the second standard conversion sequence if
1018 that doesn't do it. This will probably wait for an overloading rewrite.
1019 (jason 8/9/95) */
1021 tree
1022 build_type_conversion (tree xtype, tree expr)
1024 /* C++: check to see if we can convert this aggregate type
1025 into the required type. */
1026 return build_user_type_conversion (xtype, expr, LOOKUP_NORMAL);
1029 /* Convert the given EXPR to one of a group of types suitable for use in an
1030 expression. DESIRES is a combination of various WANT_* flags (q.v.)
1031 which indicates which types are suitable. If COMPLAIN is true, complain
1032 about ambiguity; otherwise, the caller will deal with it. */
1034 tree
1035 build_expr_type_conversion (int desires, tree expr, bool complain)
1037 tree basetype = TREE_TYPE (expr);
1038 tree conv = NULL_TREE;
1039 tree winner = NULL_TREE;
1041 if (expr == null_node
1042 && (desires & WANT_INT)
1043 && !(desires & WANT_NULL))
1044 warning ("converting NULL to non-pointer type");
1046 basetype = TREE_TYPE (expr);
1048 if (basetype == error_mark_node)
1049 return error_mark_node;
1051 if (! IS_AGGR_TYPE (basetype))
1052 switch (TREE_CODE (basetype))
1054 case INTEGER_TYPE:
1055 if ((desires & WANT_NULL) && null_ptr_cst_p (expr))
1056 return expr;
1057 /* else fall through... */
1059 case BOOLEAN_TYPE:
1060 return (desires & WANT_INT) ? expr : NULL_TREE;
1061 case ENUMERAL_TYPE:
1062 return (desires & WANT_ENUM) ? expr : NULL_TREE;
1063 case REAL_TYPE:
1064 return (desires & WANT_FLOAT) ? expr : NULL_TREE;
1065 case POINTER_TYPE:
1066 return (desires & WANT_POINTER) ? expr : NULL_TREE;
1068 case FUNCTION_TYPE:
1069 case ARRAY_TYPE:
1070 return (desires & WANT_POINTER) ? decay_conversion (expr)
1071 : NULL_TREE;
1072 default:
1073 return NULL_TREE;
1076 /* The code for conversions from class type is currently only used for
1077 delete expressions. Other expressions are handled by build_new_op. */
1078 if (!complete_type_or_else (basetype, expr))
1079 return error_mark_node;
1080 if (!TYPE_HAS_CONVERSION (basetype))
1081 return NULL_TREE;
1083 for (conv = lookup_conversions (basetype); conv; conv = TREE_CHAIN (conv))
1085 int win = 0;
1086 tree candidate;
1087 tree cand = TREE_VALUE (conv);
1089 if (winner && winner == cand)
1090 continue;
1092 candidate = non_reference (TREE_TYPE (TREE_TYPE (cand)));
1094 switch (TREE_CODE (candidate))
1096 case BOOLEAN_TYPE:
1097 case INTEGER_TYPE:
1098 win = (desires & WANT_INT); break;
1099 case ENUMERAL_TYPE:
1100 win = (desires & WANT_ENUM); break;
1101 case REAL_TYPE:
1102 win = (desires & WANT_FLOAT); break;
1103 case POINTER_TYPE:
1104 win = (desires & WANT_POINTER); break;
1106 default:
1107 break;
1110 if (win)
1112 if (winner)
1114 if (complain)
1116 error ("ambiguous default type conversion from %qT",
1117 basetype);
1118 error (" candidate conversions include %qD and %qD",
1119 winner, cand);
1121 return error_mark_node;
1123 else
1124 winner = cand;
1128 if (winner)
1130 tree type = non_reference (TREE_TYPE (TREE_TYPE (winner)));
1131 return build_user_type_conversion (type, expr, LOOKUP_NORMAL);
1134 return NULL_TREE;
1137 /* Implements integral promotion (4.1) and float->double promotion. */
1139 tree
1140 type_promotes_to (tree type)
1142 if (type == error_mark_node)
1143 return error_mark_node;
1145 type = TYPE_MAIN_VARIANT (type);
1147 /* bool always promotes to int (not unsigned), even if it's the same
1148 size. */
1149 if (type == boolean_type_node)
1150 type = integer_type_node;
1152 /* Normally convert enums to int, but convert wide enums to something
1153 wider. */
1154 else if (TREE_CODE (type) == ENUMERAL_TYPE
1155 || type == wchar_type_node)
1157 int precision = MAX (TYPE_PRECISION (type),
1158 TYPE_PRECISION (integer_type_node));
1159 tree totype = c_common_type_for_size (precision, 0);
1160 if (TYPE_UNSIGNED (type)
1161 && ! int_fits_type_p (TYPE_MAX_VALUE (type), totype))
1162 type = c_common_type_for_size (precision, 1);
1163 else
1164 type = totype;
1166 else if (c_promoting_integer_type_p (type))
1168 /* Retain unsignedness if really not getting bigger. */
1169 if (TYPE_UNSIGNED (type)
1170 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
1171 type = unsigned_type_node;
1172 else
1173 type = integer_type_node;
1175 else if (type == float_type_node)
1176 type = double_type_node;
1178 return type;
1181 /* The routines below this point are carefully written to conform to
1182 the standard. They use the same terminology, and follow the rules
1183 closely. Although they are used only in pt.c at the moment, they
1184 should presumably be used everywhere in the future. */
1186 /* Attempt to perform qualification conversions on EXPR to convert it
1187 to TYPE. Return the resulting expression, or error_mark_node if
1188 the conversion was impossible. */
1190 tree
1191 perform_qualification_conversions (tree type, tree expr)
1193 tree expr_type;
1195 expr_type = TREE_TYPE (expr);
1197 if (TYPE_PTR_P (type) && TYPE_PTR_P (expr_type)
1198 && comp_ptr_ttypes (TREE_TYPE (type), TREE_TYPE (expr_type)))
1199 return build_nop (type, expr);
1200 else if (TYPE_PTR_TO_MEMBER_P (type)
1201 && TYPE_PTR_TO_MEMBER_P (expr_type)
1202 && same_type_p (TYPE_PTRMEM_CLASS_TYPE (type),
1203 TYPE_PTRMEM_CLASS_TYPE (expr_type))
1204 && comp_ptr_ttypes (TYPE_PTRMEM_POINTED_TO_TYPE (type),
1205 TYPE_PTRMEM_POINTED_TO_TYPE (expr_type)))
1206 return build_nop (type, expr);
1207 else
1208 return error_mark_node;