cp:
[official-gcc.git] / gcc / cp / cvt.c
blobfe99c8f566e914ac8316cd14ae69ce4e0bf7c577
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' will only work for objects of dynamic type `%T'",
213 BINFO_TYPE (virt_binfo),
214 BINFO_TYPE (BINFO_INHERITANCE_CHAIN (virt_binfo)),
215 code == MINUS_EXPR ? b2 : b1);
216 else
218 cp_error ("pointer to member cast via virtual base `%T' of `%T'",
219 BINFO_TYPE (virt_binfo),
220 BINFO_TYPE (BINFO_INHERITANCE_CHAIN (virt_binfo)));
221 return error_mark_node;
225 if (TREE_CODE (expr) == PTRMEM_CST)
226 expr = cplus_expand_constant (expr);
228 if (binfo && ! TREE_VIA_VIRTUAL (binfo))
229 expr = size_binop (code, convert (sizetype,expr),
230 BINFO_OFFSET (binfo));
232 else if (TYPE_PTRMEMFUNC_P (type))
234 cp_error ("cannot convert `%E' from type `%T' to type `%T'",
235 expr, intype, type);
236 return error_mark_node;
239 rval = build1 (NOP_EXPR, type, expr);
240 TREE_CONSTANT (rval) = TREE_CONSTANT (expr);
241 return rval;
243 else if (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype))
244 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), expr, 0);
245 else if (TYPE_PTRMEMFUNC_P (intype))
247 cp_error ("cannot convert `%E' from type `%T' to type `%T'",
248 expr, intype, type);
249 return error_mark_node;
252 my_friendly_assert (form != OFFSET_TYPE, 186);
254 if (integer_zerop (expr))
256 if (TYPE_PTRMEMFUNC_P (type))
257 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), expr, 0);
259 if (flag_new_abi && TYPE_PTRMEM_P (type))
260 /* Under the new ABI, a NULL pointer-to-member is represented
261 by -1, not by zero. */
262 expr = build_int_2 (-1, -1);
263 else
264 expr = build_int_2 (0, 0);
265 TREE_TYPE (expr) = type;
266 return expr;
269 if (INTEGRAL_CODE_P (form))
271 if (TYPE_PRECISION (intype) == POINTER_SIZE)
272 return build1 (CONVERT_EXPR, type, expr);
273 expr = cp_convert (type_for_size (POINTER_SIZE, 0), expr);
274 /* Modes may be different but sizes should be the same. */
275 if (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (expr)))
276 != GET_MODE_SIZE (TYPE_MODE (type)))
277 /* There is supposed to be some integral type
278 that is the same width as a pointer. */
279 abort ();
280 return convert_to_pointer (type, expr);
283 if (type_unknown_p (expr))
284 return instantiate_type (type, expr, itf_complain);
286 cp_error ("cannot convert `%E' from type `%T' to type `%T'",
287 expr, intype, type);
288 return error_mark_node;
291 /* Like convert, except permit conversions to take place which
292 are not normally allowed due to access restrictions
293 (such as conversion from sub-type to private super-type). */
295 static tree
296 convert_to_pointer_force (type, expr)
297 tree type, expr;
299 register tree intype = TREE_TYPE (expr);
300 register enum tree_code form = TREE_CODE (intype);
302 if (integer_zerop (expr))
304 expr = build_int_2 (0, 0);
305 TREE_TYPE (expr) = type;
306 return expr;
309 if (form == POINTER_TYPE)
311 intype = TYPE_MAIN_VARIANT (intype);
313 if (TYPE_MAIN_VARIANT (type) != intype
314 && TREE_CODE (TREE_TYPE (type)) == RECORD_TYPE
315 && IS_AGGR_TYPE (TREE_TYPE (type))
316 && IS_AGGR_TYPE (TREE_TYPE (intype))
317 && TREE_CODE (TREE_TYPE (intype)) == RECORD_TYPE)
319 enum tree_code code = PLUS_EXPR;
320 tree path;
321 int distance = get_base_distance (TREE_TYPE (type),
322 TREE_TYPE (intype), 0, &path);
323 if (distance == -2)
325 cp_error ("type `%T' is ambiguous base of `%T'",
326 TREE_TYPE (type),
327 TREE_TYPE (intype));
328 return error_mark_node;
330 if (distance == -1)
332 distance = get_base_distance (TREE_TYPE (intype),
333 TREE_TYPE (type), 0, &path);
334 if (distance == -2)
336 cp_error ("type `%T' is ambiguous base of `%T'",
337 TREE_TYPE (intype),
338 TREE_TYPE (type));
339 return error_mark_node;
341 if (distance < 0)
342 /* Doesn't need any special help from us. */
343 return build1 (NOP_EXPR, type, expr);
345 code = MINUS_EXPR;
347 return build_vbase_path (code, type, expr, path, 0);
351 return cp_convert_to_pointer (type, expr, 1);
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. */
361 static tree
362 build_up_reference (type, arg, flags)
363 tree type, arg;
364 int flags;
366 tree rval;
367 tree argtype = TREE_TYPE (arg);
368 tree target_type = TREE_TYPE (type);
369 tree stmt_expr = NULL_TREE;
371 my_friendly_assert (TREE_CODE (type) == REFERENCE_TYPE, 187);
373 if ((flags & DIRECT_BIND) && ! real_lvalue_p (arg))
375 /* Create a new temporary variable. */
376 tree targ = arg;
377 if (toplevel_bindings_p ())
378 arg = get_temp_name (argtype);
379 else
381 arg = pushdecl (build_decl (VAR_DECL, NULL_TREE, argtype));
382 DECL_ARTIFICIAL (arg) = 1;
385 /* Process the initializer for the declaration. */
386 DECL_INITIAL (arg) = targ;
387 cp_finish_decl (arg, targ, NULL_TREE,
388 LOOKUP_ONLYCONVERTING|DIRECT_BIND);
390 else if (!(flags & DIRECT_BIND) && ! lvalue_p (arg))
391 return get_target_expr (arg);
393 /* If we had a way to wrap this up, and say, if we ever needed it's
394 address, transform all occurrences of the register, into a memory
395 reference we could win better. */
396 rval = build_unary_op (ADDR_EXPR, arg, 1);
397 if (rval == error_mark_node)
398 return error_mark_node;
400 if ((flags & LOOKUP_PROTECT)
401 && TYPE_MAIN_VARIANT (argtype) != TYPE_MAIN_VARIANT (target_type)
402 && IS_AGGR_TYPE (argtype)
403 && IS_AGGR_TYPE (target_type))
405 /* We go through get_binfo for the access control. */
406 tree binfo = get_binfo (target_type, argtype, 1);
407 if (binfo == error_mark_node)
408 return error_mark_node;
409 if (binfo == NULL_TREE)
410 return error_not_base_type (target_type, argtype);
411 rval = convert_pointer_to_real (binfo, rval);
413 else
414 rval
415 = convert_to_pointer_force (build_pointer_type (target_type), rval);
416 rval = build1 (NOP_EXPR, type, rval);
417 TREE_CONSTANT (rval) = TREE_CONSTANT (TREE_OPERAND (rval, 0));
419 /* If we created and initialized a new temporary variable, add the
420 representation of that initialization to the RVAL. */
421 if (stmt_expr)
422 rval = build (COMPOUND_EXPR, TREE_TYPE (rval), stmt_expr, rval);
424 /* And return the result. */
425 return rval;
428 /* Subroutine of convert_to_reference. REFTYPE is the target reference type.
429 INTYPE is the original rvalue type and DECL is an optional _DECL node
430 for diagnostics.
432 [dcl.init.ref] says that if an rvalue is used to
433 initialize a reference, then the reference must be to a
434 non-volatile const type. */
436 static void
437 warn_ref_binding (reftype, intype, decl)
438 tree reftype, intype, decl;
440 tree ttl = TREE_TYPE (reftype);
442 if (!CP_TYPE_CONST_NON_VOLATILE_P (ttl))
444 const char *msg;
446 if (CP_TYPE_VOLATILE_P (ttl) && decl)
447 msg = "initialization of volatile reference type `%#T' from rvalue of type `%T'";
448 else if (CP_TYPE_VOLATILE_P (ttl))
449 msg = "conversion to volatile reference type `%#T' from rvalue of type `%T'";
450 else if (decl)
451 msg = "initialization of non-const reference type `%#T' from rvalue of type `%T'";
452 else
453 msg = "conversion to non-const reference type `%#T' from rvalue of type `%T'";
455 cp_pedwarn (msg, reftype, intype);
459 /* For C++: Only need to do one-level references, but cannot
460 get tripped up on signed/unsigned differences.
462 DECL is either NULL_TREE or the _DECL node for a reference that is being
463 initialized. It can be error_mark_node if we don't know the _DECL but
464 we know it's an initialization. */
466 tree
467 convert_to_reference (reftype, expr, convtype, flags, decl)
468 tree reftype, expr;
469 int convtype, flags;
470 tree decl;
472 register tree type = TYPE_MAIN_VARIANT (TREE_TYPE (reftype));
473 register tree intype = TREE_TYPE (expr);
474 tree rval = NULL_TREE;
475 tree rval_as_conversion = NULL_TREE;
476 int i;
478 if (TREE_CODE (type) == FUNCTION_TYPE && intype == unknown_type_node)
480 expr = instantiate_type (type, expr,
481 (flags & LOOKUP_COMPLAIN)
482 ? itf_complain : itf_none);
483 if (expr == error_mark_node)
484 return error_mark_node;
486 intype = TREE_TYPE (expr);
489 if (TREE_CODE (intype) == REFERENCE_TYPE)
490 my_friendly_abort (364);
492 intype = TYPE_MAIN_VARIANT (intype);
494 i = comp_target_types (type, intype, 0);
496 if (i <= 0 && (convtype & CONV_IMPLICIT) && IS_AGGR_TYPE (intype)
497 && ! (flags & LOOKUP_NO_CONVERSION))
499 /* Look for a user-defined conversion to lvalue that we can use. */
501 rval_as_conversion
502 = build_type_conversion (reftype, expr, 1);
504 if (rval_as_conversion && rval_as_conversion != error_mark_node
505 && real_lvalue_p (rval_as_conversion))
507 expr = rval_as_conversion;
508 rval_as_conversion = NULL_TREE;
509 intype = type;
510 i = 1;
514 if (((convtype & CONV_STATIC) && i == -1)
515 || ((convtype & CONV_IMPLICIT) && i == 1))
517 if (flags & LOOKUP_COMPLAIN)
519 tree ttl = TREE_TYPE (reftype);
520 tree ttr = lvalue_type (expr);
522 if (! real_lvalue_p (expr))
523 warn_ref_binding (reftype, intype, decl);
525 if (! (convtype & CONV_CONST)
526 && !at_least_as_qualified_p (ttl, ttr))
527 cp_pedwarn ("conversion from `%T' to `%T' discards qualifiers",
528 ttr, reftype);
531 return build_up_reference (reftype, expr, flags);
533 else if ((convtype & CONV_REINTERPRET) && lvalue_p (expr))
535 /* When casting an lvalue to a reference type, just convert into
536 a pointer to the new type and deference it. This is allowed
537 by San Diego WP section 5.2.9 paragraph 12, though perhaps it
538 should be done directly (jason). (int &)ri ---> *(int*)&ri */
540 /* B* bp; A& ar = (A&)bp; is valid, but it's probably not what they
541 meant. */
542 if (TREE_CODE (intype) == POINTER_TYPE
543 && (comptypes (TREE_TYPE (intype), type,
544 COMPARE_BASE | COMPARE_RELAXED )))
545 cp_warning ("casting `%T' to `%T' does not dereference pointer",
546 intype, reftype);
548 rval = build_unary_op (ADDR_EXPR, expr, 0);
549 if (rval != error_mark_node)
550 rval = convert_force (build_pointer_type (TREE_TYPE (reftype)),
551 rval, 0);
552 if (rval != error_mark_node)
553 rval = build1 (NOP_EXPR, reftype, rval);
555 else
557 rval = convert_for_initialization (NULL_TREE, type, expr, flags,
558 "converting", 0, 0);
559 if (rval == NULL_TREE || rval == error_mark_node)
560 return rval;
561 warn_ref_binding (reftype, intype, decl);
562 rval = build_up_reference (reftype, rval, flags);
565 if (rval)
567 /* If we found a way to convert earlier, then use it. */
568 return rval;
571 my_friendly_assert (TREE_CODE (intype) != OFFSET_TYPE, 189);
573 if (flags & LOOKUP_COMPLAIN)
574 cp_error ("cannot convert type `%T' to type `%T'", intype, reftype);
576 if (flags & LOOKUP_SPECULATIVELY)
577 return NULL_TREE;
579 return error_mark_node;
582 /* We are using a reference VAL for its value. Bash that reference all the
583 way down to its lowest form. */
585 tree
586 convert_from_reference (val)
587 tree val;
589 tree type = TREE_TYPE (val);
591 if (TREE_CODE (type) == OFFSET_TYPE)
592 type = TREE_TYPE (type);
593 if (TREE_CODE (type) == REFERENCE_TYPE)
594 return build_indirect_ref (val, NULL_PTR);
595 return val;
598 /* Call this when we know (for any reason) that expr is not, in fact,
599 zero. This routine is like convert_pointer_to, but it pays
600 attention to which specific instance of what type we want to
601 convert to. This routine should eventually become
602 convert_to_pointer after all references to convert_to_pointer
603 are removed. */
605 tree
606 convert_pointer_to_real (binfo, expr)
607 tree binfo, expr;
609 register tree intype = TREE_TYPE (expr);
610 tree ptr_type;
611 tree type, rval;
613 if (intype == error_mark_node)
614 return error_mark_node;
616 if (TREE_CODE (binfo) == TREE_VEC)
617 type = BINFO_TYPE (binfo);
618 else if (IS_AGGR_TYPE (binfo))
620 type = binfo;
622 else
624 type = binfo;
625 binfo = NULL_TREE;
628 ptr_type = cp_build_qualified_type (type,
629 CP_TYPE_QUALS (TREE_TYPE (intype)));
630 ptr_type = build_pointer_type (ptr_type);
631 if (same_type_p (ptr_type, TYPE_MAIN_VARIANT (intype)))
632 return expr;
634 my_friendly_assert (!integer_zerop (expr), 191);
636 intype = TYPE_MAIN_VARIANT (TREE_TYPE (intype));
637 if (TREE_CODE (type) == RECORD_TYPE
638 && TREE_CODE (intype) == RECORD_TYPE
639 && type != intype)
641 tree path;
642 int distance
643 = get_base_distance (binfo, intype, 0, &path);
645 /* This function shouldn't be called with unqualified arguments
646 but if it is, give them an error message that they can read. */
647 if (distance < 0)
649 cp_error ("cannot convert a pointer of type `%T' to a pointer of type `%T'",
650 intype, type);
652 if (distance == -2)
653 cp_error ("because `%T' is an ambiguous base class", type);
654 return error_mark_node;
657 return build_vbase_path (PLUS_EXPR, ptr_type, expr, path, 1);
659 rval = build1 (NOP_EXPR, ptr_type,
660 TREE_CODE (expr) == NOP_EXPR ? TREE_OPERAND (expr, 0) : expr);
661 TREE_CONSTANT (rval) = TREE_CONSTANT (expr);
662 return rval;
665 /* Call this when we know (for any reason) that expr is
666 not, in fact, zero. This routine gets a type out of the first
667 argument and uses it to search for the type to convert to. If there
668 is more than one instance of that type in the expr, the conversion is
669 ambiguous. This routine should eventually go away, and all
670 callers should use convert_to_pointer_real. */
672 tree
673 convert_pointer_to (binfo, expr)
674 tree binfo, expr;
676 return convert_pointer_to_real (binfo, expr);
679 /* C++ conversions, preference to static cast conversions. */
681 tree
682 cp_convert (type, expr)
683 tree type, expr;
685 return ocp_convert (type, expr, CONV_OLD_CONVERT, LOOKUP_NORMAL);
688 /* Conversion...
690 FLAGS indicates how we should behave. */
692 tree
693 ocp_convert (type, expr, convtype, flags)
694 tree type, expr;
695 int convtype, flags;
697 register tree e = expr;
698 register enum tree_code code = TREE_CODE (type);
700 if (e == error_mark_node
701 || TREE_TYPE (e) == error_mark_node)
702 return error_mark_node;
704 complete_type (type);
705 complete_type (TREE_TYPE (expr));
707 e = decl_constant_value (e);
709 if (IS_AGGR_TYPE (type) && (convtype & CONV_FORCE_TEMP)
710 /* Some internal structures (vtable_entry_type, sigtbl_ptr_type)
711 don't go through finish_struct, so they don't have the synthesized
712 constructors. So don't force a temporary. */
713 && TYPE_HAS_CONSTRUCTOR (type))
714 /* We need a new temporary; don't take this shortcut. */;
715 else if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (TREE_TYPE (e)))
717 if (same_type_p (type, TREE_TYPE (e)))
718 /* The call to fold will not always remove the NOP_EXPR as
719 might be expected, since if one of the types is a typedef;
720 the comparsion in fold is just equality of pointers, not a
721 call to comptypes. We don't call fold in this case because
722 that can result in infinite recursion; fold will call
723 convert, which will call ocp_convert, etc. */
724 return e;
725 /* For complex data types, we need to perform componentwise
726 conversion. */
727 else if (TREE_CODE (type) == COMPLEX_TYPE)
728 return fold (convert_to_complex (type, e));
729 else
730 return fold (build1 (NOP_EXPR, type, e));
733 if (code == VOID_TYPE && (convtype & CONV_STATIC))
735 e = convert_to_void (e, /*implicit=*/NULL);
736 return e;
739 /* Just convert to the type of the member. */
740 if (code == OFFSET_TYPE)
742 type = TREE_TYPE (type);
743 code = TREE_CODE (type);
746 if (TREE_CODE (e) == OFFSET_REF)
747 e = resolve_offset_ref (e);
749 if (INTEGRAL_CODE_P (code))
751 tree intype = TREE_TYPE (e);
752 /* enum = enum, enum = int, enum = float, (enum)pointer are all
753 errors. */
754 if (TREE_CODE (type) == ENUMERAL_TYPE
755 && ((ARITHMETIC_TYPE_P (intype) && ! (convtype & CONV_STATIC))
756 || (TREE_CODE (intype) == POINTER_TYPE)))
758 cp_pedwarn ("conversion from `%#T' to `%#T'", intype, type);
760 if (flag_pedantic_errors)
761 return error_mark_node;
763 if (IS_AGGR_TYPE (intype))
765 tree rval;
766 rval = build_type_conversion (type, e, 1);
767 if (rval)
768 return rval;
769 if (flags & LOOKUP_COMPLAIN)
770 cp_error ("`%#T' used where a `%T' was expected", intype, type);
771 if (flags & LOOKUP_SPECULATIVELY)
772 return NULL_TREE;
773 return error_mark_node;
775 if (code == BOOLEAN_TYPE)
777 tree fn = NULL_TREE;
779 /* Common Ada/Pascal programmer's mistake. We always warn
780 about this since it is so bad. */
781 if (TREE_CODE (expr) == FUNCTION_DECL)
782 fn = expr;
783 else if (TREE_CODE (expr) == ADDR_EXPR
784 && TREE_CODE (TREE_OPERAND (expr, 0)) == FUNCTION_DECL)
785 fn = TREE_OPERAND (expr, 0);
786 if (fn)
787 cp_warning ("the address of `%D', will always be `true'", fn);
788 return truthvalue_conversion (e);
790 return fold (convert_to_integer (type, e));
792 if (code == POINTER_TYPE || code == REFERENCE_TYPE
793 || TYPE_PTRMEMFUNC_P (type))
794 return fold (cp_convert_to_pointer (type, e, 0));
795 if (code == REAL_TYPE || code == COMPLEX_TYPE)
797 if (IS_AGGR_TYPE (TREE_TYPE (e)))
799 tree rval;
800 rval = build_type_conversion (type, e, 1);
801 if (rval)
802 return rval;
803 else
804 if (flags & LOOKUP_COMPLAIN)
805 cp_error ("`%#T' used where a floating point value was expected",
806 TREE_TYPE (e));
808 if (code == REAL_TYPE)
809 return fold (convert_to_real (type, e));
810 else if (code == COMPLEX_TYPE)
811 return fold (convert_to_complex (type, e));
814 /* New C++ semantics: since assignment is now based on
815 memberwise copying, if the rhs type is derived from the
816 lhs type, then we may still do a conversion. */
817 if (IS_AGGR_TYPE_CODE (code))
819 tree dtype = TREE_TYPE (e);
820 tree ctor = NULL_TREE;
822 dtype = TYPE_MAIN_VARIANT (dtype);
824 /* Conversion between aggregate types. New C++ semantics allow
825 objects of derived type to be cast to objects of base type.
826 Old semantics only allowed this between pointers.
828 There may be some ambiguity between using a constructor
829 vs. using a type conversion operator when both apply. */
831 ctor = e;
833 if (abstract_virtuals_error (NULL_TREE, type))
834 return error_mark_node;
836 if ((flags & LOOKUP_ONLYCONVERTING)
837 && ! (IS_AGGR_TYPE (dtype) && DERIVED_FROM_P (type, dtype)))
838 /* For copy-initialization, first we create a temp of the proper type
839 with a user-defined conversion sequence, then we direct-initialize
840 the target with the temp (see [dcl.init]). */
841 ctor = build_user_type_conversion (type, ctor, flags);
842 if (ctor)
843 ctor = build_method_call (NULL_TREE,
844 complete_ctor_identifier,
845 build_tree_list (NULL_TREE, ctor),
846 TYPE_BINFO (type), flags);
847 if (ctor)
848 return build_cplus_new (type, ctor);
851 /* If TYPE or TREE_TYPE (E) is not on the permanent_obstack,
852 then it won't be hashed and hence compare as not equal,
853 even when it is. */
854 if (code == ARRAY_TYPE
855 && TREE_TYPE (TREE_TYPE (e)) == TREE_TYPE (type)
856 && index_type_equal (TYPE_DOMAIN (TREE_TYPE (e)), TYPE_DOMAIN (type)))
857 return e;
859 if (flags & LOOKUP_COMPLAIN)
860 cp_error ("conversion from `%T' to non-scalar type `%T' requested",
861 TREE_TYPE (expr), type);
862 if (flags & LOOKUP_SPECULATIVELY)
863 return NULL_TREE;
864 return error_mark_node;
867 /* When an expression is used in a void context, its value is discarded and
868 no lvalue-rvalue and similar conversions happen [expr.static.cast/4,
869 stmt.expr/1, expr.comma/1]. This permits dereferencing an incomplete type
870 in a void context. The C++ standard does not define what an `access' to an
871 object is, but there is reason to beleive that it is the lvalue to rvalue
872 conversion -- if it were not, `*&*p = 1' would violate [expr]/4 in that it
873 accesses `*p' not to calculate the value to be stored. But, dcl.type.cv/8
874 indicates that volatile semantics should be the same between C and C++
875 where ever possible. C leaves it implementation defined as to what
876 constitutes an access to a volatile. So, we interpret `*vp' as a read of
877 the volatile object `vp' points to, unless that is an incomplete type. For
878 volatile references we do not do this interpretation, because that would
879 make it impossible to ignore the reference return value from functions. We
880 issue warnings in the confusing cases.
882 IMPLICIT is tells us the context of an implicit void conversion. */
884 tree
885 convert_to_void (expr, implicit)
886 tree expr;
887 const char *implicit;
889 if (expr == error_mark_node
890 || TREE_TYPE (expr) == error_mark_node)
891 return error_mark_node;
892 if (!TREE_TYPE (expr))
893 return expr;
894 if (VOID_TYPE_P (TREE_TYPE (expr)))
895 return expr;
896 switch (TREE_CODE (expr))
898 case COND_EXPR:
900 /* The two parts of a cond expr might be separate lvalues. */
901 tree op1 = TREE_OPERAND (expr,1);
902 tree op2 = TREE_OPERAND (expr,2);
903 tree new_op1 = convert_to_void (op1, implicit);
904 tree new_op2 = convert_to_void (op2, implicit);
906 if (new_op1 != op1 || new_op2 != op2)
907 expr = build (COND_EXPR,
908 implicit ? TREE_TYPE (expr) : void_type_node,
909 TREE_OPERAND (expr, 0), new_op1, new_op2);
910 break;
913 case COMPOUND_EXPR:
915 /* The second part of a compound expr contains the value. */
916 tree op1 = TREE_OPERAND (expr,1);
917 tree new_op1 = convert_to_void (op1, implicit);
919 if (new_op1 != op1)
920 expr = build (COMPOUND_EXPR, TREE_TYPE (new_op1),
921 TREE_OPERAND (expr, 0), new_op1);
922 break;
925 case NON_LVALUE_EXPR:
926 case NOP_EXPR:
927 /* These have already decayed to rvalue. */
928 break;
930 case CALL_EXPR: /* we have a special meaning for volatile void fn() */
931 break;
933 case INDIRECT_REF:
935 tree type = TREE_TYPE (expr);
936 int is_reference = TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0)))
937 == REFERENCE_TYPE;
938 int is_volatile = TYPE_VOLATILE (type);
939 int is_complete = COMPLETE_TYPE_P (complete_type (type));
941 if (is_volatile && !is_complete)
942 cp_warning ("object of incomplete type `%T' will not be accessed in %s",
943 type, implicit ? implicit : "void context");
944 else if (is_reference && is_volatile)
945 cp_warning ("object of type `%T' will not be accessed in %s",
946 TREE_TYPE (TREE_OPERAND (expr, 0)),
947 implicit ? implicit : "void context");
948 if (is_reference || !is_volatile || !is_complete)
949 expr = TREE_OPERAND (expr, 0);
951 break;
954 case VAR_DECL:
956 /* External variables might be incomplete. */
957 tree type = TREE_TYPE (expr);
958 int is_complete = COMPLETE_TYPE_P (complete_type (type));
960 if (TYPE_VOLATILE (type) && !is_complete)
961 cp_warning ("object `%E' of incomplete type `%T' will not be accessed in %s",
962 expr, type, implicit ? implicit : "void context");
963 break;
966 case OFFSET_REF:
967 expr = resolve_offset_ref (expr);
968 break;
970 default:;
973 tree probe = expr;
975 if (TREE_CODE (probe) == ADDR_EXPR)
976 probe = TREE_OPERAND (expr, 0);
977 if (!is_overloaded_fn (probe))
978 ;/* OK */
979 else if (really_overloaded_fn (probe))
981 /* [over.over] enumerates the places where we can take the address
982 of an overloaded function, and this is not one of them. */
983 cp_pedwarn ("%s has no context for overloaded function name `%E'",
984 implicit ? implicit : "void cast", expr);
986 else if (implicit && probe == expr)
987 /* Only warn when there is no &. */
988 cp_warning ("%s is a reference, not call, to function `%E'",
989 implicit, expr);
992 if (expr != error_mark_node && !VOID_TYPE_P (TREE_TYPE (expr)))
994 /* FIXME: This is where we should check for expressions with no
995 effects. At the moment we do that in both build_x_component_expr
996 and expand_expr_stmt -- inconsistently too. For the moment
997 leave implicit void conversions unadorned so that expand_expr_stmt
998 has a chance of detecting some of the cases. */
999 if (!implicit)
1000 expr = build1 (CONVERT_EXPR, void_type_node, expr);
1002 return expr;
1005 /* Create an expression whose value is that of EXPR,
1006 converted to type TYPE. The TREE_TYPE of the value
1007 is always TYPE. This function implements all reasonable
1008 conversions; callers should filter out those that are
1009 not permitted by the language being compiled.
1011 Most of this routine is from build_reinterpret_cast.
1013 The backend cannot call cp_convert (what was convert) because
1014 conversions to/from basetypes may involve memory references
1015 (vbases) and adding or subtracting small values (multiple
1016 inheritance), but it calls convert from the constant folding code
1017 on subtrees of already build trees after it has ripped them apart.
1019 Also, if we ever support range variables, we'll probably also have to
1020 do a little bit more work. */
1022 tree
1023 convert (type, expr)
1024 tree type, expr;
1026 tree intype;
1028 if (type == error_mark_node || expr == error_mark_node)
1029 return error_mark_node;
1031 intype = TREE_TYPE (expr);
1033 if (POINTER_TYPE_P (type) && POINTER_TYPE_P (intype))
1035 expr = decl_constant_value (expr);
1036 return fold (build1 (NOP_EXPR, type, expr));
1039 return ocp_convert (type, expr, CONV_OLD_CONVERT,
1040 LOOKUP_NORMAL|LOOKUP_NO_CONVERSION);
1043 /* Like cp_convert, except permit conversions to take place which
1044 are not normally allowed due to access restrictions
1045 (such as conversion from sub-type to private super-type). */
1047 tree
1048 convert_force (type, expr, convtype)
1049 tree type;
1050 tree expr;
1051 int convtype;
1053 register tree e = expr;
1054 register enum tree_code code = TREE_CODE (type);
1056 if (code == REFERENCE_TYPE)
1057 return fold (convert_to_reference (type, e, CONV_C_CAST, LOOKUP_COMPLAIN,
1058 NULL_TREE));
1059 else if (TREE_CODE (TREE_TYPE (e)) == REFERENCE_TYPE)
1060 e = convert_from_reference (e);
1062 if (code == POINTER_TYPE)
1063 return fold (convert_to_pointer_force (type, e));
1065 /* From typeck.c convert_for_assignment */
1066 if (((TREE_CODE (TREE_TYPE (e)) == POINTER_TYPE && TREE_CODE (e) == ADDR_EXPR
1067 && TREE_CODE (TREE_TYPE (e)) == POINTER_TYPE
1068 && TREE_CODE (TREE_TYPE (TREE_TYPE (e))) == METHOD_TYPE)
1069 || integer_zerop (e)
1070 || TYPE_PTRMEMFUNC_P (TREE_TYPE (e)))
1071 && TYPE_PTRMEMFUNC_P (type))
1073 /* compatible pointer to member functions. */
1074 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), e, 1);
1077 return ocp_convert (type, e, CONV_C_CAST|convtype, LOOKUP_NORMAL);
1080 /* Convert an aggregate EXPR to type XTYPE. If a conversion
1081 exists, return the attempted conversion. This may
1082 return ERROR_MARK_NODE if the conversion is not
1083 allowed (references private members, etc).
1084 If no conversion exists, NULL_TREE is returned.
1086 If (FOR_SURE & 1) is non-zero, then we allow this type conversion
1087 to take place immediately. Otherwise, we build a SAVE_EXPR
1088 which can be evaluated if the results are ever needed.
1090 Changes to this functions should be mirrored in user_harshness.
1092 FIXME: Ambiguity checking is wrong. Should choose one by the implicit
1093 object parameter, or by the second standard conversion sequence if
1094 that doesn't do it. This will probably wait for an overloading rewrite.
1095 (jason 8/9/95) */
1097 tree
1098 build_type_conversion (xtype, expr, for_sure)
1099 tree xtype, expr;
1100 int for_sure;
1102 /* C++: check to see if we can convert this aggregate type
1103 into the required type. */
1104 return build_user_type_conversion
1105 (xtype, expr, for_sure ? LOOKUP_NORMAL : 0);
1108 /* Convert the given EXPR to one of a group of types suitable for use in an
1109 expression. DESIRES is a combination of various WANT_* flags (q.v.)
1110 which indicates which types are suitable. If COMPLAIN is 1, complain
1111 about ambiguity; otherwise, the caller will deal with it. */
1113 tree
1114 build_expr_type_conversion (desires, expr, complain)
1115 int desires;
1116 tree expr;
1117 int complain;
1119 tree basetype = TREE_TYPE (expr);
1120 tree conv = NULL_TREE;
1121 tree winner = NULL_TREE;
1123 if (expr == null_node
1124 && (desires & WANT_INT)
1125 && !(desires & WANT_NULL))
1126 cp_warning ("converting NULL to non-pointer type");
1128 if (TREE_CODE (expr) == OFFSET_REF)
1129 expr = resolve_offset_ref (expr);
1130 expr = convert_from_reference (expr);
1131 basetype = TREE_TYPE (expr);
1133 if (basetype == error_mark_node)
1134 return error_mark_node;
1136 if (! IS_AGGR_TYPE (basetype))
1137 switch (TREE_CODE (basetype))
1139 case INTEGER_TYPE:
1140 if ((desires & WANT_NULL) && null_ptr_cst_p (expr))
1141 return expr;
1142 /* else fall through... */
1144 case BOOLEAN_TYPE:
1145 return (desires & WANT_INT) ? expr : NULL_TREE;
1146 case ENUMERAL_TYPE:
1147 return (desires & WANT_ENUM) ? expr : NULL_TREE;
1148 case REAL_TYPE:
1149 return (desires & WANT_FLOAT) ? expr : NULL_TREE;
1150 case POINTER_TYPE:
1151 return (desires & WANT_POINTER) ? expr : NULL_TREE;
1153 case FUNCTION_TYPE:
1154 case ARRAY_TYPE:
1155 return (desires & WANT_POINTER) ? default_conversion (expr)
1156 : NULL_TREE;
1157 default:
1158 return NULL_TREE;
1161 /* The code for conversions from class type is currently only used for
1162 delete expressions. Other expressions are handled by build_new_op. */
1164 if (! TYPE_HAS_CONVERSION (basetype))
1165 return NULL_TREE;
1167 for (conv = lookup_conversions (basetype); conv; conv = TREE_CHAIN (conv))
1169 int win = 0;
1170 tree candidate;
1171 tree cand = TREE_VALUE (conv);
1173 if (winner && winner == cand)
1174 continue;
1176 candidate = TREE_TYPE (TREE_TYPE (cand));
1177 if (TREE_CODE (candidate) == REFERENCE_TYPE)
1178 candidate = TREE_TYPE (candidate);
1180 switch (TREE_CODE (candidate))
1182 case BOOLEAN_TYPE:
1183 case INTEGER_TYPE:
1184 win = (desires & WANT_INT); break;
1185 case ENUMERAL_TYPE:
1186 win = (desires & WANT_ENUM); break;
1187 case REAL_TYPE:
1188 win = (desires & WANT_FLOAT); break;
1189 case POINTER_TYPE:
1190 win = (desires & WANT_POINTER); break;
1192 default:
1193 break;
1196 if (win)
1198 if (winner)
1200 if (complain)
1202 cp_error ("ambiguous default type conversion from `%T'",
1203 basetype);
1204 cp_error (" candidate conversions include `%D' and `%D'",
1205 winner, cand);
1207 return error_mark_node;
1209 else
1210 winner = cand;
1214 if (winner)
1216 tree type = TREE_TYPE (TREE_TYPE (winner));
1217 if (TREE_CODE (type) == REFERENCE_TYPE)
1218 type = TREE_TYPE (type);
1219 return build_user_type_conversion (type, expr, LOOKUP_NORMAL);
1222 return NULL_TREE;
1225 /* Implements integral promotion (4.1) and float->double promotion. */
1227 tree
1228 type_promotes_to (type)
1229 tree type;
1231 int type_quals;
1233 if (type == error_mark_node)
1234 return error_mark_node;
1236 type_quals = CP_TYPE_QUALS (type);
1237 type = TYPE_MAIN_VARIANT (type);
1239 /* bool always promotes to int (not unsigned), even if it's the same
1240 size. */
1241 if (type == boolean_type_node)
1242 type = integer_type_node;
1244 /* Normally convert enums to int, but convert wide enums to something
1245 wider. */
1246 else if (TREE_CODE (type) == ENUMERAL_TYPE
1247 || type == wchar_type_node)
1249 int precision = MAX (TYPE_PRECISION (type),
1250 TYPE_PRECISION (integer_type_node));
1251 tree totype = type_for_size (precision, 0);
1252 if (TREE_UNSIGNED (type)
1253 && ! int_fits_type_p (TYPE_MAX_VALUE (type), totype))
1254 type = type_for_size (precision, 1);
1255 else
1256 type = totype;
1258 else if (C_PROMOTING_INTEGER_TYPE_P (type))
1260 /* Retain unsignedness if really not getting bigger. */
1261 if (TREE_UNSIGNED (type)
1262 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
1263 type = unsigned_type_node;
1264 else
1265 type = integer_type_node;
1267 else if (type == float_type_node)
1268 type = double_type_node;
1270 return cp_build_qualified_type (type, type_quals);
1273 /* The routines below this point are carefully written to conform to
1274 the standard. They use the same terminology, and follow the rules
1275 closely. Although they are used only in pt.c at the moment, they
1276 should presumably be used everywhere in the future. */
1278 /* Attempt to perform qualification conversions on EXPR to convert it
1279 to TYPE. Return the resulting expression, or error_mark_node if
1280 the conversion was impossible. */
1282 tree
1283 perform_qualification_conversions (type, expr)
1284 tree type;
1285 tree expr;
1287 if (TREE_CODE (type) == POINTER_TYPE
1288 && TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE
1289 && comp_ptr_ttypes (TREE_TYPE (type), TREE_TYPE (TREE_TYPE (expr))))
1290 return build1 (NOP_EXPR, type, expr);
1291 else
1292 return error_mark_node;