* cvt.c (convert_to_void): Preserve TREE_SIDE_EFFECTS
[official-gcc.git] / gcc / cp / cvt.c
blobc03090c16777b3449e05a158cad24bd9590f8163
1 /* Language-level data type conversion for GNU C++.
2 Copyright (C) 1987, 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000 Free Software Foundation, Inc.
4 Hacked by Michael Tiemann (tiemann@cygnus.com)
6 This file is part of GNU CC.
8 GNU CC 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 GNU CC 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 GNU CC; 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 "tree.h"
32 #include "flags.h"
33 #include "cp-tree.h"
34 #include "convert.h"
35 #include "toplev.h"
36 #include "decl.h"
38 static tree cp_convert_to_pointer PARAMS ((tree, tree, int));
39 static tree convert_to_pointer_force PARAMS ((tree, tree));
40 static tree build_up_reference PARAMS ((tree, tree, int));
41 static void warn_ref_binding PARAMS ((tree, tree, tree));
43 /* Change of width--truncation and extension of integers or reals--
44 is represented with NOP_EXPR. Proper functioning of many things
45 assumes that no other conversions can be NOP_EXPRs.
47 Conversion between integer and pointer is represented with CONVERT_EXPR.
48 Converting integer to real uses FLOAT_EXPR
49 and real to integer uses FIX_TRUNC_EXPR.
51 Here is a list of all the functions that assume that widening and
52 narrowing is always done with a NOP_EXPR:
53 In convert.c, convert_to_integer.
54 In c-typeck.c, build_binary_op_nodefault (boolean ops),
55 and truthvalue_conversion.
56 In expr.c: expand_expr, for operands of a MULT_EXPR.
57 In fold-const.c: fold.
58 In tree.c: get_narrower and get_unwidened.
60 C++: in multiple-inheritance, converting between pointers may involve
61 adjusting them by a delta stored within the class definition. */
63 /* Subroutines of `convert'. */
65 /* if converting pointer to pointer
66 if dealing with classes, check for derived->base or vice versa
67 else if dealing with method pointers, delegate
68 else convert blindly
69 else if converting class, pass off to build_type_conversion
70 else try C-style pointer conversion. If FORCE is true then allow
71 conversions via virtual bases (these are permitted by reinterpret_cast,
72 but not static_cast). */
74 static tree
75 cp_convert_to_pointer (type, expr, force)
76 tree type, expr;
77 int force;
79 register tree intype = TREE_TYPE (expr);
80 register 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 cp_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, 1);
94 if (rval)
96 if (rval == error_mark_node)
97 cp_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 tree fntype = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (intype));
113 tree decl = maybe_dummy_object (TYPE_METHOD_BASETYPE (fntype), 0);
114 expr = build (OFFSET_REF, fntype, decl, expr);
117 if (TREE_CODE (expr) == OFFSET_REF
118 && TREE_CODE (TREE_TYPE (expr)) == METHOD_TYPE)
119 expr = resolve_offset_ref (expr);
120 if (TREE_CODE (TREE_TYPE (expr)) == METHOD_TYPE)
121 expr = build_addr_func (expr);
122 if (TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE)
124 if (TREE_CODE (TREE_TYPE (TREE_TYPE (expr))) == METHOD_TYPE)
125 if (pedantic || warn_pmf2ptr)
126 cp_pedwarn ("converting from `%T' to `%T'", TREE_TYPE (expr),
127 type);
128 return build1 (NOP_EXPR, type, expr);
130 intype = TREE_TYPE (expr);
133 form = TREE_CODE (intype);
135 if (POINTER_TYPE_P (intype))
137 intype = TYPE_MAIN_VARIANT (intype);
139 if (TYPE_MAIN_VARIANT (type) != intype
140 && TREE_CODE (type) == POINTER_TYPE
141 && TREE_CODE (TREE_TYPE (type)) == RECORD_TYPE
142 && IS_AGGR_TYPE (TREE_TYPE (type))
143 && IS_AGGR_TYPE (TREE_TYPE (intype))
144 && TREE_CODE (TREE_TYPE (intype)) == RECORD_TYPE
145 /* If EXPR is NULL, then we don't need to do any arithmetic
146 to convert it:
148 [conv.ptr]
150 The null pointer value is converted to the null pointer
151 value of the destination type. */
152 && !integer_zerop (expr))
154 enum tree_code code = PLUS_EXPR;
155 tree binfo = get_binfo (TREE_TYPE (type), TREE_TYPE (intype), 1);
156 if (binfo == error_mark_node)
157 return error_mark_node;
158 if (binfo == NULL_TREE)
160 binfo = get_binfo (TREE_TYPE (intype), TREE_TYPE (type), 1);
161 if (binfo == error_mark_node)
162 return error_mark_node;
163 code = MINUS_EXPR;
165 if (binfo)
167 if (TYPE_USES_VIRTUAL_BASECLASSES (TREE_TYPE (type))
168 || TYPE_USES_VIRTUAL_BASECLASSES (TREE_TYPE (intype))
169 || ! BINFO_OFFSET_ZEROP (binfo))
171 /* Need to get the path we took. */
172 tree path;
174 if (code == PLUS_EXPR)
175 get_base_distance (TREE_TYPE (type), TREE_TYPE (intype),
176 0, &path);
177 else
178 get_base_distance (TREE_TYPE (intype), TREE_TYPE (type),
179 0, &path);
180 return build_vbase_path (code, type, expr, path, 0);
185 if (TYPE_PTRMEM_P (type) && TYPE_PTRMEM_P (intype))
187 tree b1;
188 tree b2;
189 tree binfo;
190 tree virt_binfo;
191 enum tree_code code;
193 b1 = TYPE_OFFSET_BASETYPE (TREE_TYPE (type));
194 b2 = TYPE_OFFSET_BASETYPE (TREE_TYPE (intype));
195 binfo = get_binfo (b2, b1, 1);
197 if (binfo == NULL_TREE)
199 binfo = get_binfo (b1, b2, 1);
200 code = MINUS_EXPR;
202 else
203 code = PLUS_EXPR;
205 if (binfo == error_mark_node)
206 return error_mark_node;
208 virt_binfo = binfo_from_vbase (binfo);
209 if (virt_binfo)
211 if (force)
212 cp_warning ("pointer to member cast via virtual base `%T' of `%T'",
213 BINFO_TYPE (virt_binfo),
214 BINFO_TYPE (BINFO_INHERITANCE_CHAIN (virt_binfo)));
215 else
217 cp_error ("pointer to member cast via virtual base `%T' of `%T'",
218 BINFO_TYPE (virt_binfo),
219 BINFO_TYPE (BINFO_INHERITANCE_CHAIN (virt_binfo)));
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)
231 expr = size_binop (code, convert (sizetype, expr),
232 BINFO_OFFSET (binfo));
234 else if (TYPE_PTRMEMFUNC_P (type))
236 cp_error ("cannot convert `%E' from type `%T' to type `%T'",
237 expr, intype, type);
238 return error_mark_node;
241 rval = build1 (NOP_EXPR, type, expr);
242 TREE_CONSTANT (rval) = TREE_CONSTANT (expr);
243 return rval;
245 else if (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype))
246 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), expr, 0);
247 else if (TYPE_PTRMEMFUNC_P (intype))
249 cp_error ("cannot convert `%E' from type `%T' to type `%T'",
250 expr, intype, type);
251 return error_mark_node;
254 my_friendly_assert (form != OFFSET_TYPE, 186);
256 if (integer_zerop (expr))
258 if (TYPE_PTRMEMFUNC_P (type))
259 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), expr, 0);
261 if (TYPE_PTRMEM_P (type))
262 /* Under the new ABI, a NULL pointer-to-member is represented
263 by -1, not by zero. */
264 expr = build_int_2 (-1, -1);
265 else
266 expr = build_int_2 (0, 0);
267 TREE_TYPE (expr) = type;
268 return expr;
271 if (INTEGRAL_CODE_P (form))
273 if (TYPE_PRECISION (intype) == POINTER_SIZE)
274 return build1 (CONVERT_EXPR, type, expr);
275 expr = cp_convert (type_for_size (POINTER_SIZE, 0), expr);
276 /* Modes may be different but sizes should be the same. */
277 if (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (expr)))
278 != GET_MODE_SIZE (TYPE_MODE (type)))
279 /* There is supposed to be some integral type
280 that is the same width as a pointer. */
281 abort ();
282 return convert_to_pointer (type, expr);
285 if (type_unknown_p (expr))
286 return instantiate_type (type, expr, itf_complain);
288 cp_error ("cannot convert `%E' from type `%T' to type `%T'",
289 expr, intype, type);
290 return error_mark_node;
293 /* Like convert, except permit conversions to take place which
294 are not normally allowed due to access restrictions
295 (such as conversion from sub-type to private super-type). */
297 static tree
298 convert_to_pointer_force (type, expr)
299 tree type, expr;
301 register tree intype = TREE_TYPE (expr);
302 register enum tree_code form = TREE_CODE (intype);
304 if (integer_zerop (expr))
306 expr = build_int_2 (0, 0);
307 TREE_TYPE (expr) = type;
308 return expr;
311 if (form == POINTER_TYPE)
313 intype = TYPE_MAIN_VARIANT (intype);
315 if (TYPE_MAIN_VARIANT (type) != intype
316 && TREE_CODE (TREE_TYPE (type)) == RECORD_TYPE
317 && IS_AGGR_TYPE (TREE_TYPE (type))
318 && IS_AGGR_TYPE (TREE_TYPE (intype))
319 && TREE_CODE (TREE_TYPE (intype)) == RECORD_TYPE)
321 enum tree_code code = PLUS_EXPR;
322 tree path;
323 int distance = get_base_distance (TREE_TYPE (type),
324 TREE_TYPE (intype), 0, &path);
325 if (distance == -2)
327 cp_error ("type `%T' is ambiguous base of `%T'",
328 TREE_TYPE (type),
329 TREE_TYPE (intype));
330 return error_mark_node;
332 if (distance == -1)
334 distance = get_base_distance (TREE_TYPE (intype),
335 TREE_TYPE (type), 0, &path);
336 if (distance == -2)
338 cp_error ("type `%T' is ambiguous base of `%T'",
339 TREE_TYPE (intype),
340 TREE_TYPE (type));
341 return error_mark_node;
343 if (distance < 0)
344 /* Doesn't need any special help from us. */
345 return build1 (NOP_EXPR, type, expr);
347 code = MINUS_EXPR;
349 return build_vbase_path (code, type, expr, path, 0);
353 return cp_convert_to_pointer (type, expr, 1);
356 /* We are passing something to a function which requires a reference.
357 The type we are interested in is in TYPE. The initial
358 value we have to begin with is in ARG.
360 FLAGS controls how we manage access checking.
361 DIRECT_BIND in FLAGS controls how any temporaries are generated. */
363 static tree
364 build_up_reference (type, arg, flags)
365 tree type, arg;
366 int flags;
368 tree rval;
369 tree argtype = TREE_TYPE (arg);
370 tree target_type = TREE_TYPE (type);
371 tree stmt_expr = NULL_TREE;
373 my_friendly_assert (TREE_CODE (type) == REFERENCE_TYPE, 187);
375 if ((flags & DIRECT_BIND) && ! real_lvalue_p (arg))
377 /* Create a new temporary variable. */
378 tree targ = arg;
379 if (toplevel_bindings_p ())
380 arg = get_temp_name (argtype);
381 else
383 maybe_push_cleanup_level (argtype);
384 arg = pushdecl (build_decl (VAR_DECL, NULL_TREE, argtype));
385 DECL_ARTIFICIAL (arg) = 1;
388 /* Process the initializer for the declaration. */
389 DECL_INITIAL (arg) = targ;
390 cp_finish_decl (arg, targ, NULL_TREE,
391 LOOKUP_ONLYCONVERTING|DIRECT_BIND);
393 else if (!(flags & DIRECT_BIND) && ! lvalue_p (arg))
394 return get_target_expr (arg);
396 /* If we had a way to wrap this up, and say, if we ever needed it's
397 address, transform all occurrences of the register, into a memory
398 reference we could win better. */
399 rval = build_unary_op (ADDR_EXPR, arg, 1);
400 if (rval == error_mark_node)
401 return error_mark_node;
403 if ((flags & LOOKUP_PROTECT)
404 && TYPE_MAIN_VARIANT (argtype) != TYPE_MAIN_VARIANT (target_type)
405 && IS_AGGR_TYPE (argtype)
406 && IS_AGGR_TYPE (target_type))
408 /* We go through get_binfo for the access control. */
409 tree binfo = get_binfo (target_type, argtype, 1);
410 if (binfo == error_mark_node)
411 return error_mark_node;
412 if (binfo == NULL_TREE)
413 return error_not_base_type (target_type, argtype);
414 rval = convert_pointer_to_real (binfo, rval);
416 else
417 rval
418 = convert_to_pointer_force (build_pointer_type (target_type), rval);
419 rval = build1 (NOP_EXPR, type, rval);
420 TREE_CONSTANT (rval) = TREE_CONSTANT (TREE_OPERAND (rval, 0));
422 /* If we created and initialized a new temporary variable, add the
423 representation of that initialization to the RVAL. */
424 if (stmt_expr)
425 rval = build (COMPOUND_EXPR, TREE_TYPE (rval), stmt_expr, rval);
427 /* And return the result. */
428 return rval;
431 /* Subroutine of convert_to_reference. REFTYPE is the target reference type.
432 INTYPE is the original rvalue type and DECL is an optional _DECL node
433 for diagnostics.
435 [dcl.init.ref] says that if an rvalue is used to
436 initialize a reference, then the reference must be to a
437 non-volatile const type. */
439 static void
440 warn_ref_binding (reftype, intype, decl)
441 tree reftype, intype, decl;
443 tree ttl = TREE_TYPE (reftype);
445 if (!CP_TYPE_CONST_NON_VOLATILE_P (ttl))
447 const char *msg;
449 if (CP_TYPE_VOLATILE_P (ttl) && decl)
450 msg = "initialization of volatile reference type `%#T' from rvalue of type `%T'";
451 else if (CP_TYPE_VOLATILE_P (ttl))
452 msg = "conversion to volatile reference type `%#T' from rvalue of type `%T'";
453 else if (decl)
454 msg = "initialization of non-const reference type `%#T' from rvalue of type `%T'";
455 else
456 msg = "conversion to non-const reference type `%#T' from rvalue of type `%T'";
458 cp_pedwarn (msg, reftype, intype);
462 /* For C++: Only need to do one-level references, but cannot
463 get tripped up on signed/unsigned differences.
465 DECL is either NULL_TREE or the _DECL node for a reference that is being
466 initialized. It can be error_mark_node if we don't know the _DECL but
467 we know it's an initialization. */
469 tree
470 convert_to_reference (reftype, expr, convtype, flags, decl)
471 tree reftype, expr;
472 int convtype, flags;
473 tree decl;
475 register tree type = TYPE_MAIN_VARIANT (TREE_TYPE (reftype));
476 register tree intype = TREE_TYPE (expr);
477 tree rval = NULL_TREE;
478 tree rval_as_conversion = NULL_TREE;
479 int i;
481 if (TREE_CODE (type) == FUNCTION_TYPE && intype == unknown_type_node)
483 expr = instantiate_type (type, expr,
484 (flags & LOOKUP_COMPLAIN)
485 ? itf_complain : itf_none);
486 if (expr == error_mark_node)
487 return error_mark_node;
489 intype = TREE_TYPE (expr);
492 if (TREE_CODE (intype) == REFERENCE_TYPE)
493 my_friendly_abort (364);
495 intype = TYPE_MAIN_VARIANT (intype);
497 i = comp_target_types (type, intype, 0);
499 if (i <= 0 && (convtype & CONV_IMPLICIT) && IS_AGGR_TYPE (intype)
500 && ! (flags & LOOKUP_NO_CONVERSION))
502 /* Look for a user-defined conversion to lvalue that we can use. */
504 rval_as_conversion
505 = build_type_conversion (reftype, expr, 1);
507 if (rval_as_conversion && rval_as_conversion != error_mark_node
508 && real_lvalue_p (rval_as_conversion))
510 expr = rval_as_conversion;
511 rval_as_conversion = NULL_TREE;
512 intype = type;
513 i = 1;
517 if (((convtype & CONV_STATIC) && i == -1)
518 || ((convtype & CONV_IMPLICIT) && i == 1))
520 if (flags & LOOKUP_COMPLAIN)
522 tree ttl = TREE_TYPE (reftype);
523 tree ttr = lvalue_type (expr);
525 if (! real_lvalue_p (expr))
526 warn_ref_binding (reftype, intype, decl);
528 if (! (convtype & CONV_CONST)
529 && !at_least_as_qualified_p (ttl, ttr))
530 cp_pedwarn ("conversion from `%T' to `%T' discards qualifiers",
531 ttr, reftype);
534 return build_up_reference (reftype, expr, flags);
536 else if ((convtype & CONV_REINTERPRET) && lvalue_p (expr))
538 /* When casting an lvalue to a reference type, just convert into
539 a pointer to the new type and deference it. This is allowed
540 by San Diego WP section 5.2.9 paragraph 12, though perhaps it
541 should be done directly (jason). (int &)ri ---> *(int*)&ri */
543 /* B* bp; A& ar = (A&)bp; is valid, but it's probably not what they
544 meant. */
545 if (TREE_CODE (intype) == POINTER_TYPE
546 && (comptypes (TREE_TYPE (intype), type,
547 COMPARE_BASE | COMPARE_RELAXED )))
548 cp_warning ("casting `%T' to `%T' does not dereference pointer",
549 intype, reftype);
551 rval = build_unary_op (ADDR_EXPR, expr, 0);
552 if (rval != error_mark_node)
553 rval = convert_force (build_pointer_type (TREE_TYPE (reftype)),
554 rval, 0);
555 if (rval != error_mark_node)
556 rval = build1 (NOP_EXPR, reftype, rval);
558 else
560 rval = convert_for_initialization (NULL_TREE, type, expr, flags,
561 "converting", 0, 0);
562 if (rval == NULL_TREE || rval == error_mark_node)
563 return rval;
564 warn_ref_binding (reftype, intype, decl);
565 rval = build_up_reference (reftype, rval, flags);
568 if (rval)
570 /* If we found a way to convert earlier, then use it. */
571 return rval;
574 my_friendly_assert (TREE_CODE (intype) != OFFSET_TYPE, 189);
576 if (flags & LOOKUP_COMPLAIN)
577 cp_error ("cannot convert type `%T' to type `%T'", intype, reftype);
579 if (flags & LOOKUP_SPECULATIVELY)
580 return NULL_TREE;
582 return error_mark_node;
585 /* We are using a reference VAL for its value. Bash that reference all the
586 way down to its lowest form. */
588 tree
589 convert_from_reference (val)
590 tree val;
592 tree type = TREE_TYPE (val);
594 if (TREE_CODE (type) == OFFSET_TYPE)
595 type = TREE_TYPE (type);
596 if (TREE_CODE (type) == REFERENCE_TYPE)
597 return build_indirect_ref (val, NULL_PTR);
598 return val;
601 /* Implicitly convert the lvalue EXPR to another lvalue of type TOTYPE,
602 preserving cv-qualification. */
604 tree
605 convert_lvalue (totype, expr)
606 tree totype, expr;
608 totype = cp_build_qualified_type (totype, TYPE_QUALS (TREE_TYPE (expr)));
609 totype = build_reference_type (totype);
610 expr = convert_to_reference (totype, expr, CONV_IMPLICIT, LOOKUP_NORMAL,
611 NULL_TREE);
612 return convert_from_reference (expr);
615 /* Call this when we know (for any reason) that expr is not, in fact,
616 zero. This routine is like convert_pointer_to, but it pays
617 attention to which specific instance of what type we want to
618 convert to. This routine should eventually become
619 convert_to_pointer after all references to convert_to_pointer
620 are removed. */
622 tree
623 convert_pointer_to_real (binfo, expr)
624 tree binfo, expr;
626 register tree intype = TREE_TYPE (expr);
627 tree ptr_type;
628 tree type, rval;
630 if (intype == error_mark_node)
631 return error_mark_node;
633 if (TREE_CODE (binfo) == TREE_VEC)
634 type = BINFO_TYPE (binfo);
635 else if (IS_AGGR_TYPE (binfo))
637 type = binfo;
639 else
641 type = binfo;
642 binfo = NULL_TREE;
645 ptr_type = cp_build_qualified_type (type,
646 CP_TYPE_QUALS (TREE_TYPE (intype)));
647 ptr_type = build_pointer_type (ptr_type);
648 if (same_type_p (ptr_type, TYPE_MAIN_VARIANT (intype)))
649 return expr;
651 my_friendly_assert (!integer_zerop (expr), 191);
653 intype = TYPE_MAIN_VARIANT (TREE_TYPE (intype));
654 if (TREE_CODE (type) == RECORD_TYPE
655 && TREE_CODE (intype) == RECORD_TYPE
656 && type != intype)
658 tree path;
659 int distance
660 = get_base_distance (binfo, intype, 0, &path);
662 /* This function shouldn't be called with unqualified arguments
663 but if it is, give them an error message that they can read. */
664 if (distance < 0)
666 cp_error ("cannot convert a pointer of type `%T' to a pointer of type `%T'",
667 intype, type);
669 if (distance == -2)
670 cp_error ("because `%T' is an ambiguous base class", type);
671 return error_mark_node;
674 return build_vbase_path (PLUS_EXPR, ptr_type, expr, path, 1);
676 rval = build1 (NOP_EXPR, ptr_type,
677 TREE_CODE (expr) == NOP_EXPR ? TREE_OPERAND (expr, 0) : expr);
678 TREE_CONSTANT (rval) = TREE_CONSTANT (expr);
679 return rval;
682 /* Call this when we know (for any reason) that expr is
683 not, in fact, zero. This routine gets a type out of the first
684 argument and uses it to search for the type to convert to. If there
685 is more than one instance of that type in the expr, the conversion is
686 ambiguous. This routine should eventually go away, and all
687 callers should use convert_to_pointer_real. */
689 tree
690 convert_pointer_to (binfo, expr)
691 tree binfo, expr;
693 return convert_pointer_to_real (binfo, expr);
696 /* C++ conversions, preference to static cast conversions. */
698 tree
699 cp_convert (type, expr)
700 tree type, expr;
702 return ocp_convert (type, expr, CONV_OLD_CONVERT, LOOKUP_NORMAL);
705 /* Conversion...
707 FLAGS indicates how we should behave. */
709 tree
710 ocp_convert (type, expr, convtype, flags)
711 tree type, expr;
712 int convtype, flags;
714 register tree e = expr;
715 register enum tree_code code = TREE_CODE (type);
717 if (e == error_mark_node
718 || TREE_TYPE (e) == error_mark_node)
719 return error_mark_node;
721 complete_type (type);
722 complete_type (TREE_TYPE (expr));
724 e = decl_constant_value (e);
726 if (IS_AGGR_TYPE (type) && (convtype & CONV_FORCE_TEMP)
727 /* Some internal structures (vtable_entry_type, sigtbl_ptr_type)
728 don't go through finish_struct, so they don't have the synthesized
729 constructors. So don't force a temporary. */
730 && TYPE_HAS_CONSTRUCTOR (type))
731 /* We need a new temporary; don't take this shortcut. */;
732 else if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (TREE_TYPE (e)))
734 if (same_type_p (type, TREE_TYPE (e)))
735 /* The call to fold will not always remove the NOP_EXPR as
736 might be expected, since if one of the types is a typedef;
737 the comparsion in fold is just equality of pointers, not a
738 call to comptypes. We don't call fold in this case because
739 that can result in infinite recursion; fold will call
740 convert, which will call ocp_convert, etc. */
741 return e;
742 /* For complex data types, we need to perform componentwise
743 conversion. */
744 else if (TREE_CODE (type) == COMPLEX_TYPE)
745 return fold (convert_to_complex (type, e));
746 else
747 return fold (build1 (NOP_EXPR, type, e));
750 if (code == VOID_TYPE && (convtype & CONV_STATIC))
752 e = convert_to_void (e, /*implicit=*/NULL);
753 return e;
756 /* Just convert to the type of the member. */
757 if (code == OFFSET_TYPE)
759 type = TREE_TYPE (type);
760 code = TREE_CODE (type);
763 if (TREE_CODE (e) == OFFSET_REF)
764 e = resolve_offset_ref (e);
766 if (INTEGRAL_CODE_P (code))
768 tree intype = TREE_TYPE (e);
769 /* enum = enum, enum = int, enum = float, (enum)pointer are all
770 errors. */
771 if (TREE_CODE (type) == ENUMERAL_TYPE
772 && ((ARITHMETIC_TYPE_P (intype) && ! (convtype & CONV_STATIC))
773 || (TREE_CODE (intype) == POINTER_TYPE)))
775 cp_pedwarn ("conversion from `%#T' to `%#T'", intype, type);
777 if (flag_pedantic_errors)
778 return error_mark_node;
780 if (IS_AGGR_TYPE (intype))
782 tree rval;
783 rval = build_type_conversion (type, e, 1);
784 if (rval)
785 return rval;
786 if (flags & LOOKUP_COMPLAIN)
787 cp_error ("`%#T' used where a `%T' was expected", intype, type);
788 if (flags & LOOKUP_SPECULATIVELY)
789 return NULL_TREE;
790 return error_mark_node;
792 if (code == BOOLEAN_TYPE)
794 tree fn = NULL_TREE;
796 /* Common Ada/Pascal programmer's mistake. We always warn
797 about this since it is so bad. */
798 if (TREE_CODE (expr) == FUNCTION_DECL)
799 fn = expr;
800 else if (TREE_CODE (expr) == ADDR_EXPR
801 && TREE_CODE (TREE_OPERAND (expr, 0)) == FUNCTION_DECL)
802 fn = TREE_OPERAND (expr, 0);
803 if (fn)
804 cp_warning ("the address of `%D', will always be `true'", fn);
805 return cp_truthvalue_conversion (e);
807 return fold (convert_to_integer (type, e));
809 if (code == POINTER_TYPE || code == REFERENCE_TYPE
810 || TYPE_PTRMEMFUNC_P (type))
811 return fold (cp_convert_to_pointer (type, e, 0));
812 if (code == VECTOR_TYPE)
813 return fold (convert_to_vector (type, e));
814 if (code == REAL_TYPE || code == COMPLEX_TYPE)
816 if (IS_AGGR_TYPE (TREE_TYPE (e)))
818 tree rval;
819 rval = build_type_conversion (type, e, 1);
820 if (rval)
821 return rval;
822 else
823 if (flags & LOOKUP_COMPLAIN)
824 cp_error ("`%#T' used where a floating point value was expected",
825 TREE_TYPE (e));
827 if (code == REAL_TYPE)
828 return fold (convert_to_real (type, e));
829 else if (code == COMPLEX_TYPE)
830 return fold (convert_to_complex (type, e));
833 /* New C++ semantics: since assignment is now based on
834 memberwise copying, if the rhs type is derived from the
835 lhs type, then we may still do a conversion. */
836 if (IS_AGGR_TYPE_CODE (code))
838 tree dtype = TREE_TYPE (e);
839 tree ctor = NULL_TREE;
841 dtype = TYPE_MAIN_VARIANT (dtype);
843 /* Conversion between aggregate types. New C++ semantics allow
844 objects of derived type to be cast to objects of base type.
845 Old semantics only allowed this between pointers.
847 There may be some ambiguity between using a constructor
848 vs. using a type conversion operator when both apply. */
850 ctor = e;
852 if (abstract_virtuals_error (NULL_TREE, type))
853 return error_mark_node;
855 if ((flags & LOOKUP_ONLYCONVERTING)
856 && ! (IS_AGGR_TYPE (dtype) && DERIVED_FROM_P (type, dtype)))
857 /* For copy-initialization, first we create a temp of the proper type
858 with a user-defined conversion sequence, then we direct-initialize
859 the target with the temp (see [dcl.init]). */
860 ctor = build_user_type_conversion (type, ctor, flags);
861 else
862 ctor = build_method_call (NULL_TREE,
863 complete_ctor_identifier,
864 build_tree_list (NULL_TREE, ctor),
865 TYPE_BINFO (type), flags);
866 if (ctor)
867 return build_cplus_new (type, ctor);
870 /* If TYPE or TREE_TYPE (E) is not on the permanent_obstack,
871 then it won't be hashed and hence compare as not equal,
872 even when it is. */
873 if (code == ARRAY_TYPE
874 && TREE_TYPE (TREE_TYPE (e)) == TREE_TYPE (type)
875 && index_type_equal (TYPE_DOMAIN (TREE_TYPE (e)), TYPE_DOMAIN (type)))
876 return e;
878 if (flags & LOOKUP_COMPLAIN)
879 cp_error ("conversion from `%T' to non-scalar type `%T' requested",
880 TREE_TYPE (expr), type);
881 if (flags & LOOKUP_SPECULATIVELY)
882 return NULL_TREE;
883 return error_mark_node;
886 /* When an expression is used in a void context, its value is discarded and
887 no lvalue-rvalue and similar conversions happen [expr.static.cast/4,
888 stmt.expr/1, expr.comma/1]. This permits dereferencing an incomplete type
889 in a void context. The C++ standard does not define what an `access' to an
890 object is, but there is reason to beleive that it is the lvalue to rvalue
891 conversion -- if it were not, `*&*p = 1' would violate [expr]/4 in that it
892 accesses `*p' not to calculate the value to be stored. But, dcl.type.cv/8
893 indicates that volatile semantics should be the same between C and C++
894 where ever possible. C leaves it implementation defined as to what
895 constitutes an access to a volatile. So, we interpret `*vp' as a read of
896 the volatile object `vp' points to, unless that is an incomplete type. For
897 volatile references we do not do this interpretation, because that would
898 make it impossible to ignore the reference return value from functions. We
899 issue warnings in the confusing cases.
901 IMPLICIT is tells us the context of an implicit void conversion. */
903 tree
904 convert_to_void (expr, implicit)
905 tree expr;
906 const char *implicit;
908 if (expr == error_mark_node
909 || TREE_TYPE (expr) == error_mark_node)
910 return error_mark_node;
911 if (!TREE_TYPE (expr))
912 return expr;
913 if (VOID_TYPE_P (TREE_TYPE (expr)))
914 return expr;
915 switch (TREE_CODE (expr))
917 case COND_EXPR:
919 /* The two parts of a cond expr might be separate lvalues. */
920 tree op1 = TREE_OPERAND (expr,1);
921 tree op2 = TREE_OPERAND (expr,2);
922 tree new_op1 = convert_to_void (op1, implicit);
923 tree new_op2 = convert_to_void (op2, implicit);
925 if (new_op1 != op1 || new_op2 != op2)
926 expr = build (COND_EXPR,
927 implicit ? TREE_TYPE (expr) : void_type_node,
928 TREE_OPERAND (expr, 0), new_op1, new_op2);
929 break;
932 case COMPOUND_EXPR:
934 /* The second part of a compound expr contains the value. */
935 tree op1 = TREE_OPERAND (expr,1);
936 tree new_op1 = convert_to_void (op1, implicit);
938 if (new_op1 != op1)
940 tree t = build (COMPOUND_EXPR, TREE_TYPE (new_op1),
941 TREE_OPERAND (expr, 0), new_op1);
942 TREE_SIDE_EFFECTS (t) = TREE_SIDE_EFFECTS (expr);
943 expr = t;
946 break;
949 case NON_LVALUE_EXPR:
950 case NOP_EXPR:
951 /* These have already decayed to rvalue. */
952 break;
954 case CALL_EXPR: /* we have a special meaning for volatile void fn() */
955 break;
957 case INDIRECT_REF:
959 tree type = TREE_TYPE (expr);
960 int is_reference = TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0)))
961 == REFERENCE_TYPE;
962 int is_volatile = TYPE_VOLATILE (type);
963 int is_complete = COMPLETE_TYPE_P (complete_type (type));
965 if (is_volatile && !is_complete)
966 cp_warning ("object of incomplete type `%T' will not be accessed in %s",
967 type, implicit ? implicit : "void context");
968 else if (is_reference && is_volatile)
969 cp_warning ("object of type `%T' will not be accessed in %s",
970 TREE_TYPE (TREE_OPERAND (expr, 0)),
971 implicit ? implicit : "void context");
972 if (is_reference || !is_volatile || !is_complete)
973 expr = TREE_OPERAND (expr, 0);
975 break;
978 case VAR_DECL:
980 /* External variables might be incomplete. */
981 tree type = TREE_TYPE (expr);
982 int is_complete = COMPLETE_TYPE_P (complete_type (type));
984 if (TYPE_VOLATILE (type) && !is_complete)
985 cp_warning ("object `%E' of incomplete type `%T' will not be accessed in %s",
986 expr, type, implicit ? implicit : "void context");
987 break;
990 case OFFSET_REF:
991 expr = resolve_offset_ref (expr);
992 break;
994 default:;
997 tree probe = expr;
999 if (TREE_CODE (probe) == ADDR_EXPR)
1000 probe = TREE_OPERAND (expr, 0);
1001 if (type_unknown_p (probe))
1003 /* [over.over] enumerates the places where we can take the address
1004 of an overloaded function, and this is not one of them. */
1005 cp_pedwarn ("%s cannot resolve address of overloaded function",
1006 implicit ? implicit : "void cast");
1008 else if (implicit && probe == expr && is_overloaded_fn (probe))
1009 /* Only warn when there is no &. */
1010 cp_warning ("%s is a reference, not call, to function `%E'",
1011 implicit, expr);
1014 if (expr != error_mark_node && !VOID_TYPE_P (TREE_TYPE (expr)))
1016 /* FIXME: This is where we should check for expressions with no
1017 effects. At the moment we do that in both build_x_component_expr
1018 and expand_expr_stmt -- inconsistently too. For the moment
1019 leave implicit void conversions unadorned so that expand_expr_stmt
1020 has a chance of detecting some of the cases. */
1021 if (!implicit)
1022 expr = build1 (CONVERT_EXPR, void_type_node, expr);
1024 return expr;
1027 /* Create an expression whose value is that of EXPR,
1028 converted to type TYPE. The TREE_TYPE of the value
1029 is always TYPE. This function implements all reasonable
1030 conversions; callers should filter out those that are
1031 not permitted by the language being compiled.
1033 Most of this routine is from build_reinterpret_cast.
1035 The backend cannot call cp_convert (what was convert) because
1036 conversions to/from basetypes may involve memory references
1037 (vbases) and adding or subtracting small values (multiple
1038 inheritance), but it calls convert from the constant folding code
1039 on subtrees of already built trees after it has ripped them apart.
1041 Also, if we ever support range variables, we'll probably also have to
1042 do a little bit more work. */
1044 tree
1045 convert (type, expr)
1046 tree type, expr;
1048 tree intype;
1050 if (type == error_mark_node || expr == error_mark_node)
1051 return error_mark_node;
1053 intype = TREE_TYPE (expr);
1055 if (POINTER_TYPE_P (type) && POINTER_TYPE_P (intype))
1057 expr = decl_constant_value (expr);
1058 return fold (build1 (NOP_EXPR, type, expr));
1061 return ocp_convert (type, expr, CONV_OLD_CONVERT,
1062 LOOKUP_NORMAL|LOOKUP_NO_CONVERSION);
1065 /* Like cp_convert, except permit conversions to take place which
1066 are not normally allowed due to access restrictions
1067 (such as conversion from sub-type to private super-type). */
1069 tree
1070 convert_force (type, expr, convtype)
1071 tree type;
1072 tree expr;
1073 int convtype;
1075 register tree e = expr;
1076 register enum tree_code code = TREE_CODE (type);
1078 if (code == REFERENCE_TYPE)
1079 return fold (convert_to_reference (type, e, CONV_C_CAST, LOOKUP_COMPLAIN,
1080 NULL_TREE));
1081 else if (TREE_CODE (TREE_TYPE (e)) == REFERENCE_TYPE)
1082 e = convert_from_reference (e);
1084 if (code == POINTER_TYPE)
1085 return fold (convert_to_pointer_force (type, e));
1087 /* From typeck.c convert_for_assignment */
1088 if (((TREE_CODE (TREE_TYPE (e)) == POINTER_TYPE && TREE_CODE (e) == ADDR_EXPR
1089 && TREE_CODE (TREE_TYPE (e)) == POINTER_TYPE
1090 && TREE_CODE (TREE_TYPE (TREE_TYPE (e))) == METHOD_TYPE)
1091 || integer_zerop (e)
1092 || TYPE_PTRMEMFUNC_P (TREE_TYPE (e)))
1093 && TYPE_PTRMEMFUNC_P (type))
1095 /* compatible pointer to member functions. */
1096 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), e, 1);
1099 return ocp_convert (type, e, CONV_C_CAST|convtype, LOOKUP_NORMAL);
1102 /* Convert an aggregate EXPR to type XTYPE. If a conversion
1103 exists, return the attempted conversion. This may
1104 return ERROR_MARK_NODE if the conversion is not
1105 allowed (references private members, etc).
1106 If no conversion exists, NULL_TREE is returned.
1108 If (FOR_SURE & 1) is non-zero, then we allow this type conversion
1109 to take place immediately. Otherwise, we build a SAVE_EXPR
1110 which can be evaluated if the results are ever needed.
1112 Changes to this functions should be mirrored in user_harshness.
1114 FIXME: Ambiguity checking is wrong. Should choose one by the implicit
1115 object parameter, or by the second standard conversion sequence if
1116 that doesn't do it. This will probably wait for an overloading rewrite.
1117 (jason 8/9/95) */
1119 tree
1120 build_type_conversion (xtype, expr, for_sure)
1121 tree xtype, expr;
1122 int for_sure;
1124 /* C++: check to see if we can convert this aggregate type
1125 into the required type. */
1126 return build_user_type_conversion
1127 (xtype, expr, for_sure ? LOOKUP_NORMAL : 0);
1130 /* Convert the given EXPR to one of a group of types suitable for use in an
1131 expression. DESIRES is a combination of various WANT_* flags (q.v.)
1132 which indicates which types are suitable. If COMPLAIN is 1, complain
1133 about ambiguity; otherwise, the caller will deal with it. */
1135 tree
1136 build_expr_type_conversion (desires, expr, complain)
1137 int desires;
1138 tree expr;
1139 int complain;
1141 tree basetype = TREE_TYPE (expr);
1142 tree conv = NULL_TREE;
1143 tree winner = NULL_TREE;
1145 if (expr == null_node
1146 && (desires & WANT_INT)
1147 && !(desires & WANT_NULL))
1148 cp_warning ("converting NULL to non-pointer type");
1150 if (TREE_CODE (expr) == OFFSET_REF)
1151 expr = resolve_offset_ref (expr);
1152 expr = convert_from_reference (expr);
1153 basetype = TREE_TYPE (expr);
1155 if (basetype == error_mark_node)
1156 return error_mark_node;
1158 if (! IS_AGGR_TYPE (basetype))
1159 switch (TREE_CODE (basetype))
1161 case INTEGER_TYPE:
1162 if ((desires & WANT_NULL) && null_ptr_cst_p (expr))
1163 return expr;
1164 /* else fall through... */
1166 case BOOLEAN_TYPE:
1167 return (desires & WANT_INT) ? expr : NULL_TREE;
1168 case ENUMERAL_TYPE:
1169 return (desires & WANT_ENUM) ? expr : NULL_TREE;
1170 case REAL_TYPE:
1171 return (desires & WANT_FLOAT) ? expr : NULL_TREE;
1172 case POINTER_TYPE:
1173 return (desires & WANT_POINTER) ? expr : NULL_TREE;
1175 case FUNCTION_TYPE:
1176 case ARRAY_TYPE:
1177 return (desires & WANT_POINTER) ? default_conversion (expr)
1178 : NULL_TREE;
1179 default:
1180 return NULL_TREE;
1183 /* The code for conversions from class type is currently only used for
1184 delete expressions. Other expressions are handled by build_new_op. */
1186 if (! TYPE_HAS_CONVERSION (basetype))
1187 return NULL_TREE;
1189 for (conv = lookup_conversions (basetype); conv; conv = TREE_CHAIN (conv))
1191 int win = 0;
1192 tree candidate;
1193 tree cand = TREE_VALUE (conv);
1195 if (winner && winner == cand)
1196 continue;
1198 candidate = TREE_TYPE (TREE_TYPE (cand));
1199 if (TREE_CODE (candidate) == REFERENCE_TYPE)
1200 candidate = TREE_TYPE (candidate);
1202 switch (TREE_CODE (candidate))
1204 case BOOLEAN_TYPE:
1205 case INTEGER_TYPE:
1206 win = (desires & WANT_INT); break;
1207 case ENUMERAL_TYPE:
1208 win = (desires & WANT_ENUM); break;
1209 case REAL_TYPE:
1210 win = (desires & WANT_FLOAT); break;
1211 case POINTER_TYPE:
1212 win = (desires & WANT_POINTER); break;
1214 default:
1215 break;
1218 if (win)
1220 if (winner)
1222 if (complain)
1224 cp_error ("ambiguous default type conversion from `%T'",
1225 basetype);
1226 cp_error (" candidate conversions include `%D' and `%D'",
1227 winner, cand);
1229 return error_mark_node;
1231 else
1232 winner = cand;
1236 if (winner)
1238 tree type = TREE_TYPE (TREE_TYPE (winner));
1239 if (TREE_CODE (type) == REFERENCE_TYPE)
1240 type = TREE_TYPE (type);
1241 return build_user_type_conversion (type, expr, LOOKUP_NORMAL);
1244 return NULL_TREE;
1247 /* Implements integral promotion (4.1) and float->double promotion. */
1249 tree
1250 type_promotes_to (type)
1251 tree type;
1253 int type_quals;
1255 if (type == error_mark_node)
1256 return error_mark_node;
1258 type_quals = CP_TYPE_QUALS (type);
1259 type = TYPE_MAIN_VARIANT (type);
1261 /* bool always promotes to int (not unsigned), even if it's the same
1262 size. */
1263 if (type == boolean_type_node)
1264 type = integer_type_node;
1266 /* Normally convert enums to int, but convert wide enums to something
1267 wider. */
1268 else if (TREE_CODE (type) == ENUMERAL_TYPE
1269 || type == wchar_type_node)
1271 int precision = MAX (TYPE_PRECISION (type),
1272 TYPE_PRECISION (integer_type_node));
1273 tree totype = type_for_size (precision, 0);
1274 if (TREE_UNSIGNED (type)
1275 && ! int_fits_type_p (TYPE_MAX_VALUE (type), totype))
1276 type = type_for_size (precision, 1);
1277 else
1278 type = totype;
1280 else if (c_promoting_integer_type_p (type))
1282 /* Retain unsignedness if really not getting bigger. */
1283 if (TREE_UNSIGNED (type)
1284 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
1285 type = unsigned_type_node;
1286 else
1287 type = integer_type_node;
1289 else if (type == float_type_node)
1290 type = double_type_node;
1292 return cp_build_qualified_type (type, type_quals);
1295 /* The routines below this point are carefully written to conform to
1296 the standard. They use the same terminology, and follow the rules
1297 closely. Although they are used only in pt.c at the moment, they
1298 should presumably be used everywhere in the future. */
1300 /* Attempt to perform qualification conversions on EXPR to convert it
1301 to TYPE. Return the resulting expression, or error_mark_node if
1302 the conversion was impossible. */
1304 tree
1305 perform_qualification_conversions (type, expr)
1306 tree type;
1307 tree expr;
1309 if (TREE_CODE (type) == POINTER_TYPE
1310 && TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE
1311 && comp_ptr_ttypes (TREE_TYPE (type), TREE_TYPE (TREE_TYPE (expr))))
1312 return build1 (NOP_EXPR, type, expr);
1313 else
1314 return error_mark_node;