2002-03-31 Segher Boessenkool <segher@koffie.nl>
[official-gcc.git] / gcc / cp / cvt.c
blob19bca82f07603d3e2c11d57d7d9db46097426e22
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 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 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 error ("can't convert from incomplete type `%T' to `%T'",
89 intype, type);
90 return error_mark_node;
93 rval = build_type_conversion (type, expr);
94 if (rval)
96 if (rval == error_mark_node)
97 error ("conversion of `%E' from `%T' to `%T' is ambiguous",
98 expr, intype, type);
99 return rval;
103 /* Handle anachronistic conversions from (::*)() to cv void* or (*)(). */
104 if (TREE_CODE (type) == POINTER_TYPE
105 && (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
106 || VOID_TYPE_P (TREE_TYPE (type))))
108 /* Allow an implicit this pointer for pointer to member
109 functions. */
110 if (TYPE_PTRMEMFUNC_P (intype))
112 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 pedwarn ("converting from `%T' to `%T'", TREE_TYPE (expr),
127 type);
128 return build1 (NOP_EXPR, type, expr);
130 intype = TREE_TYPE (expr);
133 if (expr == error_mark_node)
134 return error_mark_node;
136 form = TREE_CODE (intype);
138 if (POINTER_TYPE_P (intype))
140 intype = TYPE_MAIN_VARIANT (intype);
142 if (TYPE_MAIN_VARIANT (type) != intype
143 && TREE_CODE (type) == POINTER_TYPE
144 && TREE_CODE (TREE_TYPE (type)) == RECORD_TYPE
145 && IS_AGGR_TYPE (TREE_TYPE (type))
146 && IS_AGGR_TYPE (TREE_TYPE (intype))
147 && TREE_CODE (TREE_TYPE (intype)) == RECORD_TYPE)
149 enum tree_code code = PLUS_EXPR;
150 tree binfo;
151 tree intype_class;
152 tree type_class;
153 bool same_p;
155 intype_class = TREE_TYPE (intype);
156 type_class = TREE_TYPE (type);
158 same_p = same_type_p (TYPE_MAIN_VARIANT (intype_class),
159 TYPE_MAIN_VARIANT (type_class));
160 binfo = NULL_TREE;
161 /* Try derived to base conversion. */
162 if (!same_p)
163 binfo = lookup_base (intype_class, type_class, ba_check, NULL);
164 if (!same_p && !binfo)
166 /* Try base to derived conversion. */
167 binfo = lookup_base (type_class, intype_class, ba_check, NULL);
168 code = MINUS_EXPR;
170 if (binfo == error_mark_node)
171 return error_mark_node;
172 if (binfo || same_p)
174 if (binfo)
175 expr = build_base_path (code, expr, binfo, 0);
176 /* Add any qualifier conversions. */
177 return build_nop (type, expr);
181 if (TYPE_PTRMEM_P (type) && TYPE_PTRMEM_P (intype))
183 tree b1;
184 tree b2;
185 tree binfo;
186 enum tree_code code = PLUS_EXPR;
187 base_kind bk;
189 b1 = TYPE_OFFSET_BASETYPE (TREE_TYPE (type));
190 b2 = TYPE_OFFSET_BASETYPE (TREE_TYPE (intype));
191 binfo = lookup_base (b1, b2, ba_check, &bk);
192 if (!binfo)
194 binfo = lookup_base (b2, b1, ba_check, &bk);
195 code = MINUS_EXPR;
197 if (binfo == error_mark_node)
198 return error_mark_node;
200 if (bk == bk_via_virtual)
202 if (force)
203 warning ("pointer to member cast from `%T' to `%T' is via virtual base",
204 TREE_TYPE (intype), TREE_TYPE (type));
205 else
207 error ("pointer to member cast from `%T' to `%T' is via virtual base",
208 TREE_TYPE (intype), TREE_TYPE (type));
209 return error_mark_node;
211 /* This is a reinterpret cast, whose result is unspecified.
212 We choose to do nothing. */
213 return build1 (NOP_EXPR, type, expr);
216 if (TREE_CODE (expr) == PTRMEM_CST)
217 expr = cplus_expand_constant (expr);
219 if (binfo)
220 expr = size_binop (code, convert (sizetype, expr),
221 BINFO_OFFSET (binfo));
223 else if (TYPE_PTRMEMFUNC_P (type))
225 error ("cannot convert `%E' from type `%T' to type `%T'",
226 expr, intype, type);
227 return error_mark_node;
230 return build_nop (type, expr);
232 else if (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype))
233 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), expr, 0);
234 else if (TYPE_PTRMEMFUNC_P (intype))
236 error ("cannot convert `%E' from type `%T' to type `%T'",
237 expr, intype, type);
238 return error_mark_node;
241 my_friendly_assert (form != OFFSET_TYPE, 186);
243 if (integer_zerop (expr))
245 if (TYPE_PTRMEMFUNC_P (type))
246 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), expr, 0);
248 if (TYPE_PTRMEM_P (type))
249 /* A NULL pointer-to-member is represented by -1, not by
250 zero. */
251 expr = build_int_2 (-1, -1);
252 else
253 expr = build_int_2 (0, 0);
254 TREE_TYPE (expr) = type;
255 /* Fix up the representation of -1 if appropriate. */
256 force_fit_type (expr, 0);
257 return expr;
259 else if ((TYPE_PTRMEM_P (type) || TYPE_PTRMEMFUNC_P (type))
260 && INTEGRAL_CODE_P (form))
262 error ("invalid conversion from '%T' to '%T'", intype, type);
263 return error_mark_node;
266 if (INTEGRAL_CODE_P (form))
268 if (TYPE_PRECISION (intype) == POINTER_SIZE)
269 return build1 (CONVERT_EXPR, type, expr);
270 expr = cp_convert (c_common_type_for_size (POINTER_SIZE, 0), expr);
271 /* Modes may be different but sizes should be the same. */
272 if (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (expr)))
273 != GET_MODE_SIZE (TYPE_MODE (type)))
274 /* There is supposed to be some integral type
275 that is the same width as a pointer. */
276 abort ();
277 return convert_to_pointer (type, expr);
280 if (type_unknown_p (expr))
281 return instantiate_type (type, expr, tf_error | tf_warning);
283 error ("cannot convert `%E' from type `%T' to type `%T'",
284 expr, intype, type);
285 return error_mark_node;
288 /* Like convert, except permit conversions to take place which
289 are not normally allowed due to access restrictions
290 (such as conversion from sub-type to private super-type). */
292 static tree
293 convert_to_pointer_force (tree type, tree expr)
295 register tree intype = TREE_TYPE (expr);
296 register enum tree_code form = TREE_CODE (intype);
298 if (form == POINTER_TYPE)
300 intype = TYPE_MAIN_VARIANT (intype);
302 if (TYPE_MAIN_VARIANT (type) != intype
303 && TREE_CODE (TREE_TYPE (type)) == RECORD_TYPE
304 && IS_AGGR_TYPE (TREE_TYPE (type))
305 && IS_AGGR_TYPE (TREE_TYPE (intype))
306 && TREE_CODE (TREE_TYPE (intype)) == RECORD_TYPE)
308 enum tree_code code = PLUS_EXPR;
309 tree binfo;
311 binfo = lookup_base (TREE_TYPE (intype), TREE_TYPE (type),
312 ba_ignore, NULL);
313 if (!binfo)
315 binfo = lookup_base (TREE_TYPE (type), TREE_TYPE (intype),
316 ba_ignore, NULL);
317 code = MINUS_EXPR;
319 if (binfo == error_mark_node)
320 return error_mark_node;
321 if (binfo)
323 expr = build_base_path (code, expr, binfo, 0);
324 if (expr == error_mark_node)
325 return error_mark_node;
326 /* Add any qualifier conversions. */
327 if (!same_type_p (TREE_TYPE (TREE_TYPE (expr)),
328 TREE_TYPE (type)))
329 expr = build_nop (type, expr);
330 return expr;
335 return cp_convert_to_pointer (type, expr, true);
338 /* We are passing something to a function which requires a reference.
339 The type we are interested in is in TYPE. The initial
340 value we have to begin with is in ARG.
342 FLAGS controls how we manage access checking.
343 DIRECT_BIND in FLAGS controls how any temporaries are generated.
344 If DIRECT_BIND is set, DECL is the reference we're binding to. */
346 static tree
347 build_up_reference (tree type, tree arg, int flags, tree decl)
349 tree rval;
350 tree argtype = TREE_TYPE (arg);
351 tree target_type = TREE_TYPE (type);
353 my_friendly_assert (TREE_CODE (type) == REFERENCE_TYPE, 187);
355 if ((flags & DIRECT_BIND) && ! real_lvalue_p (arg))
357 /* Create a new temporary variable. We can't just use a TARGET_EXPR
358 here because it needs to live as long as DECL. */
359 tree targ = arg;
361 arg = make_temporary_var_for_ref_to_temp (decl, TREE_TYPE (arg));
363 /* Process the initializer for the declaration. */
364 DECL_INITIAL (arg) = targ;
365 cp_finish_decl (arg, targ, NULL_TREE,
366 LOOKUP_ONLYCONVERTING|DIRECT_BIND);
368 else if (!(flags & DIRECT_BIND) && ! lvalue_p (arg))
369 return get_target_expr (arg);
371 /* If we had a way to wrap this up, and say, if we ever needed its
372 address, transform all occurrences of the register, into a memory
373 reference we could win better. */
374 rval = build_unary_op (ADDR_EXPR, arg, 1);
375 if (rval == error_mark_node)
376 return error_mark_node;
378 if ((flags & LOOKUP_PROTECT)
379 && TYPE_MAIN_VARIANT (argtype) != TYPE_MAIN_VARIANT (target_type)
380 && IS_AGGR_TYPE (argtype)
381 && IS_AGGR_TYPE (target_type))
383 /* We go through lookup_base for the access control. */
384 tree binfo = lookup_base (argtype, target_type, ba_check, NULL);
385 if (binfo == error_mark_node)
386 return error_mark_node;
387 if (binfo == NULL_TREE)
388 return error_not_base_type (target_type, argtype);
389 rval = build_base_path (PLUS_EXPR, rval, binfo, 1);
391 else
392 rval
393 = convert_to_pointer_force (build_pointer_type (target_type), rval);
394 return build_nop (type, rval);
397 /* Subroutine of convert_to_reference. REFTYPE is the target reference type.
398 INTYPE is the original rvalue type and DECL is an optional _DECL node
399 for diagnostics.
401 [dcl.init.ref] says that if an rvalue is used to
402 initialize a reference, then the reference must be to a
403 non-volatile const type. */
405 static void
406 warn_ref_binding (tree reftype, tree intype, tree decl)
408 tree ttl = TREE_TYPE (reftype);
410 if (!CP_TYPE_CONST_NON_VOLATILE_P (ttl))
412 const char *msg;
414 if (CP_TYPE_VOLATILE_P (ttl) && decl)
415 msg = "initialization of volatile reference type `%#T' from rvalue of type `%T'";
416 else if (CP_TYPE_VOLATILE_P (ttl))
417 msg = "conversion to volatile reference type `%#T' from rvalue of type `%T'";
418 else if (decl)
419 msg = "initialization of non-const reference type `%#T' from rvalue of type `%T'";
420 else
421 msg = "conversion to non-const reference type `%#T' from rvalue of type `%T'";
423 pedwarn (msg, reftype, intype);
427 /* For C++: Only need to do one-level references, but cannot
428 get tripped up on signed/unsigned differences.
430 DECL is either NULL_TREE or the _DECL node for a reference that is being
431 initialized. It can be error_mark_node if we don't know the _DECL but
432 we know it's an initialization. */
434 tree
435 convert_to_reference (tree reftype, tree expr, int convtype,
436 int flags, tree decl)
438 register tree type = TYPE_MAIN_VARIANT (TREE_TYPE (reftype));
439 register tree intype;
440 tree rval = NULL_TREE;
441 tree rval_as_conversion = NULL_TREE;
442 int i;
444 if (TREE_CODE (type) == FUNCTION_TYPE
445 && TREE_TYPE (expr) == unknown_type_node)
446 expr = instantiate_type (type, expr,
447 (flags & LOOKUP_COMPLAIN)
448 ? tf_error | tf_warning : tf_none);
449 else
450 expr = convert_from_reference (expr);
452 if (expr == error_mark_node)
453 return error_mark_node;
455 intype = TREE_TYPE (expr);
457 my_friendly_assert (TREE_CODE (intype) != REFERENCE_TYPE, 364);
459 intype = TYPE_MAIN_VARIANT (intype);
461 i = comp_target_types (type, intype, 0);
463 if (i <= 0 && (convtype & CONV_IMPLICIT) && IS_AGGR_TYPE (intype)
464 && ! (flags & LOOKUP_NO_CONVERSION))
466 /* Look for a user-defined conversion to lvalue that we can use. */
468 rval_as_conversion
469 = build_type_conversion (reftype, expr);
471 if (rval_as_conversion && rval_as_conversion != error_mark_node
472 && real_lvalue_p (rval_as_conversion))
474 expr = rval_as_conversion;
475 rval_as_conversion = NULL_TREE;
476 intype = type;
477 i = 1;
481 if (((convtype & CONV_STATIC) && i == -1)
482 || ((convtype & CONV_IMPLICIT) && i == 1))
484 if (flags & LOOKUP_COMPLAIN)
486 tree ttl = TREE_TYPE (reftype);
487 tree ttr = lvalue_type (expr);
489 if (! real_lvalue_p (expr))
490 warn_ref_binding (reftype, intype, decl);
492 if (! (convtype & CONV_CONST)
493 && !at_least_as_qualified_p (ttl, ttr))
494 pedwarn ("conversion from `%T' to `%T' discards qualifiers",
495 ttr, reftype);
498 return build_up_reference (reftype, expr, flags, decl);
500 else if ((convtype & CONV_REINTERPRET) && lvalue_p (expr))
502 /* When casting an lvalue to a reference type, just convert into
503 a pointer to the new type and deference it. This is allowed
504 by San Diego WP section 5.2.9 paragraph 12, though perhaps it
505 should be done directly (jason). (int &)ri ---> *(int*)&ri */
507 /* B* bp; A& ar = (A&)bp; is valid, but it's probably not what they
508 meant. */
509 if (TREE_CODE (intype) == POINTER_TYPE
510 && (comptypes (TREE_TYPE (intype), type,
511 COMPARE_BASE | COMPARE_RELAXED )))
512 warning ("casting `%T' to `%T' does not dereference pointer",
513 intype, reftype);
515 rval = build_unary_op (ADDR_EXPR, expr, 0);
516 if (rval != error_mark_node)
517 rval = convert_force (build_pointer_type (TREE_TYPE (reftype)),
518 rval, 0);
519 if (rval != error_mark_node)
520 rval = build1 (NOP_EXPR, reftype, rval);
522 else
524 rval = convert_for_initialization (NULL_TREE, type, expr, flags,
525 "converting", 0, 0);
526 if (rval == NULL_TREE || rval == error_mark_node)
527 return rval;
528 warn_ref_binding (reftype, intype, decl);
529 rval = build_up_reference (reftype, rval, flags, decl);
532 if (rval)
534 /* If we found a way to convert earlier, then use it. */
535 return rval;
538 my_friendly_assert (TREE_CODE (intype) != OFFSET_TYPE, 189);
540 if (flags & LOOKUP_COMPLAIN)
541 error ("cannot convert type `%T' to type `%T'", intype, reftype);
543 if (flags & LOOKUP_SPECULATIVELY)
544 return NULL_TREE;
546 return error_mark_node;
549 /* We are using a reference VAL for its value. Bash that reference all the
550 way down to its lowest form. */
552 tree
553 convert_from_reference (tree val)
555 tree type = TREE_TYPE (val);
557 if (TREE_CODE (type) == OFFSET_TYPE)
558 type = TREE_TYPE (type);
559 if (TREE_CODE (type) == REFERENCE_TYPE)
560 return build_indirect_ref (val, NULL);
561 return val;
564 /* Implicitly convert the lvalue EXPR to another lvalue of type TOTYPE,
565 preserving cv-qualification. */
567 tree
568 convert_lvalue (tree totype, tree expr)
570 totype = cp_build_qualified_type (totype, TYPE_QUALS (TREE_TYPE (expr)));
571 totype = build_reference_type (totype);
572 expr = convert_to_reference (totype, expr, CONV_IMPLICIT, LOOKUP_NORMAL,
573 NULL_TREE);
574 return convert_from_reference (expr);
577 /* Really perform an lvalue-to-rvalue conversion, including copying an
578 argument of class type into a temporary. */
580 tree
581 force_rvalue (tree expr)
583 if (IS_AGGR_TYPE (TREE_TYPE (expr)) && TREE_CODE (expr) != TARGET_EXPR)
584 expr = ocp_convert (TREE_TYPE (expr), expr,
585 CONV_IMPLICIT|CONV_FORCE_TEMP, LOOKUP_NORMAL);
586 else
587 expr = decay_conversion (expr);
589 return expr;
592 /* C++ conversions, preference to static cast conversions. */
594 tree
595 cp_convert (tree type, tree expr)
597 return ocp_convert (type, expr, CONV_OLD_CONVERT, LOOKUP_NORMAL);
600 /* Conversion...
602 FLAGS indicates how we should behave. */
604 tree
605 ocp_convert (tree type, tree expr, int convtype, int flags)
607 register tree e = expr;
608 register enum tree_code code = TREE_CODE (type);
610 if (e == error_mark_node
611 || TREE_TYPE (e) == error_mark_node)
612 return error_mark_node;
614 complete_type (type);
615 complete_type (TREE_TYPE (expr));
617 e = decl_constant_value (e);
619 if (IS_AGGR_TYPE (type) && (convtype & CONV_FORCE_TEMP)
620 /* Some internal structures (vtable_entry_type, sigtbl_ptr_type)
621 don't go through finish_struct, so they don't have the synthesized
622 constructors. So don't force a temporary. */
623 && TYPE_HAS_CONSTRUCTOR (type))
624 /* We need a new temporary; don't take this shortcut. */;
625 else if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (TREE_TYPE (e)))
627 if (same_type_p (type, TREE_TYPE (e)))
628 /* The call to fold will not always remove the NOP_EXPR as
629 might be expected, since if one of the types is a typedef;
630 the comparsion in fold is just equality of pointers, not a
631 call to comptypes. We don't call fold in this case because
632 that can result in infinite recursion; fold will call
633 convert, which will call ocp_convert, etc. */
634 return e;
635 /* For complex data types, we need to perform componentwise
636 conversion. */
637 else if (TREE_CODE (type) == COMPLEX_TYPE)
638 return fold (convert_to_complex (type, e));
639 else if (TREE_CODE (e) == TARGET_EXPR)
641 /* Don't build a NOP_EXPR of class type. Instead, change the
642 type of the temporary. Only allow this for cv-qual changes,
643 though. */
644 if (!same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (e)),
645 TYPE_MAIN_VARIANT (type)))
646 abort ();
647 TREE_TYPE (e) = TREE_TYPE (TARGET_EXPR_SLOT (e)) = type;
648 return e;
650 else if (CLASS_TYPE_P (type))
651 abort ();
652 else
653 return fold (build1 (NOP_EXPR, type, e));
656 if (code == VOID_TYPE && (convtype & CONV_STATIC))
658 e = convert_to_void (e, /*implicit=*/NULL);
659 return e;
662 /* Just convert to the type of the member. */
663 if (code == OFFSET_TYPE)
665 type = TREE_TYPE (type);
666 code = TREE_CODE (type);
669 if (TREE_CODE (e) == OFFSET_REF)
670 e = resolve_offset_ref (e);
672 if (INTEGRAL_CODE_P (code))
674 tree intype = TREE_TYPE (e);
675 /* enum = enum, enum = int, enum = float, (enum)pointer are all
676 errors. */
677 if (TREE_CODE (type) == ENUMERAL_TYPE
678 && ((ARITHMETIC_TYPE_P (intype) && ! (convtype & CONV_STATIC))
679 || (TREE_CODE (intype) == POINTER_TYPE)))
681 pedwarn ("conversion from `%#T' to `%#T'", intype, type);
683 if (flag_pedantic_errors)
684 return error_mark_node;
686 if (IS_AGGR_TYPE (intype))
688 tree rval;
689 rval = build_type_conversion (type, e);
690 if (rval)
691 return rval;
692 if (flags & LOOKUP_COMPLAIN)
693 error ("`%#T' used where a `%T' was expected", intype, type);
694 if (flags & LOOKUP_SPECULATIVELY)
695 return NULL_TREE;
696 return error_mark_node;
698 if (code == BOOLEAN_TYPE)
700 tree fn = NULL_TREE;
702 /* Common Ada/Pascal programmer's mistake. We always warn
703 about this since it is so bad. */
704 if (TREE_CODE (expr) == FUNCTION_DECL)
705 fn = expr;
706 else if (TREE_CODE (expr) == ADDR_EXPR
707 && TREE_CODE (TREE_OPERAND (expr, 0)) == FUNCTION_DECL)
708 fn = TREE_OPERAND (expr, 0);
709 if (fn && !DECL_WEAK (fn))
710 warning ("the address of `%D', will always be `true'", fn);
711 return cp_truthvalue_conversion (e);
713 return fold (convert_to_integer (type, e));
715 if (code == POINTER_TYPE || code == REFERENCE_TYPE
716 || TYPE_PTRMEMFUNC_P (type))
717 return fold (cp_convert_to_pointer (type, e, false));
718 if (code == VECTOR_TYPE)
719 return fold (convert_to_vector (type, e));
720 if (code == REAL_TYPE || code == COMPLEX_TYPE)
722 if (IS_AGGR_TYPE (TREE_TYPE (e)))
724 tree rval;
725 rval = build_type_conversion (type, e);
726 if (rval)
727 return rval;
728 else
729 if (flags & LOOKUP_COMPLAIN)
730 error ("`%#T' used where a floating point value was expected",
731 TREE_TYPE (e));
733 if (code == REAL_TYPE)
734 return fold (convert_to_real (type, e));
735 else if (code == COMPLEX_TYPE)
736 return fold (convert_to_complex (type, e));
739 /* New C++ semantics: since assignment is now based on
740 memberwise copying, if the rhs type is derived from the
741 lhs type, then we may still do a conversion. */
742 if (IS_AGGR_TYPE_CODE (code))
744 tree dtype = TREE_TYPE (e);
745 tree ctor = NULL_TREE;
747 dtype = TYPE_MAIN_VARIANT (dtype);
749 /* Conversion between aggregate types. New C++ semantics allow
750 objects of derived type to be cast to objects of base type.
751 Old semantics only allowed this between pointers.
753 There may be some ambiguity between using a constructor
754 vs. using a type conversion operator when both apply. */
756 ctor = e;
758 if (abstract_virtuals_error (NULL_TREE, type))
759 return error_mark_node;
761 if ((flags & LOOKUP_ONLYCONVERTING)
762 && ! (IS_AGGR_TYPE (dtype) && DERIVED_FROM_P (type, dtype)))
763 /* For copy-initialization, first we create a temp of the proper type
764 with a user-defined conversion sequence, then we direct-initialize
765 the target with the temp (see [dcl.init]). */
766 ctor = build_user_type_conversion (type, ctor, flags);
767 else
768 ctor = build_special_member_call (NULL_TREE,
769 complete_ctor_identifier,
770 build_tree_list (NULL_TREE, ctor),
771 TYPE_BINFO (type), flags);
772 if (ctor)
773 return build_cplus_new (type, ctor);
776 if (flags & LOOKUP_COMPLAIN)
777 error ("conversion from `%T' to non-scalar type `%T' requested",
778 TREE_TYPE (expr), type);
779 if (flags & LOOKUP_SPECULATIVELY)
780 return NULL_TREE;
781 return error_mark_node;
784 /* When an expression is used in a void context, its value is discarded and
785 no lvalue-rvalue and similar conversions happen [expr.static.cast/4,
786 stmt.expr/1, expr.comma/1]. This permits dereferencing an incomplete type
787 in a void context. The C++ standard does not define what an `access' to an
788 object is, but there is reason to beleive that it is the lvalue to rvalue
789 conversion -- if it were not, `*&*p = 1' would violate [expr]/4 in that it
790 accesses `*p' not to calculate the value to be stored. But, dcl.type.cv/8
791 indicates that volatile semantics should be the same between C and C++
792 where ever possible. C leaves it implementation defined as to what
793 constitutes an access to a volatile. So, we interpret `*vp' as a read of
794 the volatile object `vp' points to, unless that is an incomplete type. For
795 volatile references we do not do this interpretation, because that would
796 make it impossible to ignore the reference return value from functions. We
797 issue warnings in the confusing cases.
799 IMPLICIT is tells us the context of an implicit void conversion. */
801 tree
802 convert_to_void (tree expr, const char *implicit)
804 if (expr == error_mark_node
805 || TREE_TYPE (expr) == error_mark_node)
806 return error_mark_node;
807 if (!TREE_TYPE (expr))
808 return expr;
809 if (VOID_TYPE_P (TREE_TYPE (expr)))
810 return expr;
811 switch (TREE_CODE (expr))
813 case COND_EXPR:
815 /* The two parts of a cond expr might be separate lvalues. */
816 tree op1 = TREE_OPERAND (expr,1);
817 tree op2 = TREE_OPERAND (expr,2);
818 tree new_op1 = convert_to_void (op1, implicit);
819 tree new_op2 = convert_to_void (op2, implicit);
821 expr = build (COND_EXPR, TREE_TYPE (new_op1),
822 TREE_OPERAND (expr, 0), new_op1, new_op2);
823 break;
826 case COMPOUND_EXPR:
828 /* The second part of a compound expr contains the value. */
829 tree op1 = TREE_OPERAND (expr,1);
830 tree new_op1 = convert_to_void (op1, implicit);
832 if (new_op1 != op1)
834 tree t = build (COMPOUND_EXPR, TREE_TYPE (new_op1),
835 TREE_OPERAND (expr, 0), new_op1);
836 TREE_SIDE_EFFECTS (t) = TREE_SIDE_EFFECTS (expr);
837 TREE_NO_UNUSED_WARNING (t) = TREE_NO_UNUSED_WARNING (expr);
838 expr = t;
841 break;
844 case NON_LVALUE_EXPR:
845 case NOP_EXPR:
846 /* These have already decayed to rvalue. */
847 break;
849 case CALL_EXPR: /* we have a special meaning for volatile void fn() */
850 break;
852 case INDIRECT_REF:
854 tree type = TREE_TYPE (expr);
855 int is_reference = TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0)))
856 == REFERENCE_TYPE;
857 int is_volatile = TYPE_VOLATILE (type);
858 int is_complete = COMPLETE_TYPE_P (complete_type (type));
860 if (is_volatile && !is_complete)
861 warning ("object of incomplete type `%T' will not be accessed in %s",
862 type, implicit ? implicit : "void context");
863 else if (is_reference && is_volatile)
864 warning ("object of type `%T' will not be accessed in %s",
865 TREE_TYPE (TREE_OPERAND (expr, 0)),
866 implicit ? implicit : "void context");
867 if (is_reference || !is_volatile || !is_complete)
868 expr = TREE_OPERAND (expr, 0);
870 break;
873 case VAR_DECL:
875 /* External variables might be incomplete. */
876 tree type = TREE_TYPE (expr);
877 int is_complete = COMPLETE_TYPE_P (complete_type (type));
879 if (TYPE_VOLATILE (type) && !is_complete)
880 warning ("object `%E' of incomplete type `%T' will not be accessed in %s",
881 expr, type, implicit ? implicit : "void context");
882 break;
885 case OFFSET_REF:
886 expr = resolve_offset_ref (expr);
887 break;
889 default:;
892 tree probe = expr;
894 if (TREE_CODE (probe) == ADDR_EXPR)
895 probe = TREE_OPERAND (expr, 0);
896 if (type_unknown_p (probe))
898 /* [over.over] enumerates the places where we can take the address
899 of an overloaded function, and this is not one of them. */
900 pedwarn ("%s cannot resolve address of overloaded function",
901 implicit ? implicit : "void cast");
903 else if (implicit && probe == expr && is_overloaded_fn (probe))
904 /* Only warn when there is no &. */
905 warning ("%s is a reference, not call, to function `%E'",
906 implicit, expr);
909 if (expr != error_mark_node && !VOID_TYPE_P (TREE_TYPE (expr)))
911 /* FIXME: This is where we should check for expressions with no
912 effects. At the moment we do that in both build_x_component_expr
913 and expand_expr_stmt -- inconsistently too. For the moment
914 leave implicit void conversions unadorned so that expand_expr_stmt
915 has a chance of detecting some of the cases. */
916 if (!implicit)
917 expr = build1 (CONVERT_EXPR, void_type_node, expr);
919 return expr;
922 /* Create an expression whose value is that of EXPR,
923 converted to type TYPE. The TREE_TYPE of the value
924 is always TYPE. This function implements all reasonable
925 conversions; callers should filter out those that are
926 not permitted by the language being compiled.
928 Most of this routine is from build_reinterpret_cast.
930 The backend cannot call cp_convert (what was convert) because
931 conversions to/from basetypes may involve memory references
932 (vbases) and adding or subtracting small values (multiple
933 inheritance), but it calls convert from the constant folding code
934 on subtrees of already built trees after it has ripped them apart.
936 Also, if we ever support range variables, we'll probably also have to
937 do a little bit more work. */
939 tree
940 convert (tree type, tree expr)
942 tree intype;
944 if (type == error_mark_node || expr == error_mark_node)
945 return error_mark_node;
947 intype = TREE_TYPE (expr);
949 if (POINTER_TYPE_P (type) && POINTER_TYPE_P (intype))
951 expr = decl_constant_value (expr);
952 return fold (build1 (NOP_EXPR, type, expr));
955 return ocp_convert (type, expr, CONV_OLD_CONVERT,
956 LOOKUP_NORMAL|LOOKUP_NO_CONVERSION);
959 /* Like cp_convert, except permit conversions to take place which
960 are not normally allowed due to access restrictions
961 (such as conversion from sub-type to private super-type). */
963 tree
964 convert_force (tree type, tree expr, int convtype)
966 register tree e = expr;
967 register enum tree_code code = TREE_CODE (type);
969 if (code == REFERENCE_TYPE)
970 return fold (convert_to_reference (type, e, CONV_C_CAST, LOOKUP_COMPLAIN,
971 NULL_TREE));
972 else if (TREE_CODE (TREE_TYPE (e)) == REFERENCE_TYPE)
973 e = convert_from_reference (e);
975 if (code == POINTER_TYPE)
976 return fold (convert_to_pointer_force (type, e));
978 /* From typeck.c convert_for_assignment */
979 if (((TREE_CODE (TREE_TYPE (e)) == POINTER_TYPE && TREE_CODE (e) == ADDR_EXPR
980 && TREE_CODE (TREE_TYPE (e)) == POINTER_TYPE
981 && TREE_CODE (TREE_TYPE (TREE_TYPE (e))) == METHOD_TYPE)
982 || integer_zerop (e)
983 || TYPE_PTRMEMFUNC_P (TREE_TYPE (e)))
984 && TYPE_PTRMEMFUNC_P (type))
986 /* compatible pointer to member functions. */
987 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), e, 1);
990 return ocp_convert (type, e, CONV_C_CAST|convtype, LOOKUP_NORMAL);
993 /* Convert an aggregate EXPR to type XTYPE. If a conversion
994 exists, return the attempted conversion. This may
995 return ERROR_MARK_NODE if the conversion is not
996 allowed (references private members, etc).
997 If no conversion exists, NULL_TREE is returned.
999 FIXME: Ambiguity checking is wrong. Should choose one by the implicit
1000 object parameter, or by the second standard conversion sequence if
1001 that doesn't do it. This will probably wait for an overloading rewrite.
1002 (jason 8/9/95) */
1004 tree
1005 build_type_conversion (tree xtype, tree expr)
1007 /* C++: check to see if we can convert this aggregate type
1008 into the required type. */
1009 return build_user_type_conversion (xtype, expr, LOOKUP_NORMAL);
1012 /* Convert the given EXPR to one of a group of types suitable for use in an
1013 expression. DESIRES is a combination of various WANT_* flags (q.v.)
1014 which indicates which types are suitable. If COMPLAIN is true, complain
1015 about ambiguity; otherwise, the caller will deal with it. */
1017 tree
1018 build_expr_type_conversion (int desires, tree expr, bool complain)
1020 tree basetype = TREE_TYPE (expr);
1021 tree conv = NULL_TREE;
1022 tree winner = NULL_TREE;
1024 if (expr == null_node
1025 && (desires & WANT_INT)
1026 && !(desires & WANT_NULL))
1027 warning ("converting NULL to non-pointer type");
1029 if (TREE_CODE (expr) == OFFSET_REF)
1030 expr = resolve_offset_ref (expr);
1031 expr = convert_from_reference (expr);
1032 basetype = TREE_TYPE (expr);
1034 if (basetype == error_mark_node)
1035 return error_mark_node;
1037 if (! IS_AGGR_TYPE (basetype))
1038 switch (TREE_CODE (basetype))
1040 case INTEGER_TYPE:
1041 if ((desires & WANT_NULL) && null_ptr_cst_p (expr))
1042 return expr;
1043 /* else fall through... */
1045 case BOOLEAN_TYPE:
1046 return (desires & WANT_INT) ? expr : NULL_TREE;
1047 case ENUMERAL_TYPE:
1048 return (desires & WANT_ENUM) ? expr : NULL_TREE;
1049 case REAL_TYPE:
1050 return (desires & WANT_FLOAT) ? expr : NULL_TREE;
1051 case POINTER_TYPE:
1052 return (desires & WANT_POINTER) ? expr : NULL_TREE;
1054 case FUNCTION_TYPE:
1055 case ARRAY_TYPE:
1056 return (desires & WANT_POINTER) ? default_conversion (expr)
1057 : NULL_TREE;
1058 default:
1059 return NULL_TREE;
1062 /* The code for conversions from class type is currently only used for
1063 delete expressions. Other expressions are handled by build_new_op. */
1065 if (! TYPE_HAS_CONVERSION (basetype))
1066 return NULL_TREE;
1068 for (conv = lookup_conversions (basetype); conv; conv = TREE_CHAIN (conv))
1070 int win = 0;
1071 tree candidate;
1072 tree cand = TREE_VALUE (conv);
1074 if (winner && winner == cand)
1075 continue;
1077 candidate = TREE_TYPE (TREE_TYPE (cand));
1078 if (TREE_CODE (candidate) == REFERENCE_TYPE)
1079 candidate = TREE_TYPE (candidate);
1081 switch (TREE_CODE (candidate))
1083 case BOOLEAN_TYPE:
1084 case INTEGER_TYPE:
1085 win = (desires & WANT_INT); break;
1086 case ENUMERAL_TYPE:
1087 win = (desires & WANT_ENUM); break;
1088 case REAL_TYPE:
1089 win = (desires & WANT_FLOAT); break;
1090 case POINTER_TYPE:
1091 win = (desires & WANT_POINTER); break;
1093 default:
1094 break;
1097 if (win)
1099 if (winner)
1101 if (complain)
1103 error ("ambiguous default type conversion from `%T'",
1104 basetype);
1105 error (" candidate conversions include `%D' and `%D'",
1106 winner, cand);
1108 return error_mark_node;
1110 else
1111 winner = cand;
1115 if (winner)
1117 tree type = TREE_TYPE (TREE_TYPE (winner));
1118 if (TREE_CODE (type) == REFERENCE_TYPE)
1119 type = TREE_TYPE (type);
1120 return build_user_type_conversion (type, expr, LOOKUP_NORMAL);
1123 return NULL_TREE;
1126 /* Implements integral promotion (4.1) and float->double promotion. */
1128 tree
1129 type_promotes_to (tree type)
1131 int type_quals;
1133 if (type == error_mark_node)
1134 return error_mark_node;
1136 type_quals = cp_type_quals (type);
1137 type = TYPE_MAIN_VARIANT (type);
1139 /* bool always promotes to int (not unsigned), even if it's the same
1140 size. */
1141 if (type == boolean_type_node)
1142 type = integer_type_node;
1144 /* Normally convert enums to int, but convert wide enums to something
1145 wider. */
1146 else if (TREE_CODE (type) == ENUMERAL_TYPE
1147 || type == wchar_type_node)
1149 int precision = MAX (TYPE_PRECISION (type),
1150 TYPE_PRECISION (integer_type_node));
1151 tree totype = c_common_type_for_size (precision, 0);
1152 if (TREE_UNSIGNED (type)
1153 && ! int_fits_type_p (TYPE_MAX_VALUE (type), totype))
1154 type = c_common_type_for_size (precision, 1);
1155 else
1156 type = totype;
1158 else if (c_promoting_integer_type_p (type))
1160 /* Retain unsignedness if really not getting bigger. */
1161 if (TREE_UNSIGNED (type)
1162 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
1163 type = unsigned_type_node;
1164 else
1165 type = integer_type_node;
1167 else if (type == float_type_node)
1168 type = double_type_node;
1170 return cp_build_qualified_type (type, type_quals);
1173 /* The routines below this point are carefully written to conform to
1174 the standard. They use the same terminology, and follow the rules
1175 closely. Although they are used only in pt.c at the moment, they
1176 should presumably be used everywhere in the future. */
1178 /* Attempt to perform qualification conversions on EXPR to convert it
1179 to TYPE. Return the resulting expression, or error_mark_node if
1180 the conversion was impossible. */
1182 tree
1183 perform_qualification_conversions (tree type, tree expr)
1185 if (TREE_CODE (type) == POINTER_TYPE
1186 && TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE
1187 && comp_ptr_ttypes (TREE_TYPE (type), TREE_TYPE (TREE_TYPE (expr))))
1188 return build1 (NOP_EXPR, type, expr);
1189 else
1190 return error_mark_node;