FSF GCC merge 02/23/03
[official-gcc.git] / gcc / cp / cvt.c
blob73b17e8f054a1814f507fbc83b3b8c5c8920730a
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, true);
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;
152 /* Try derived to base conversion. */
153 binfo = lookup_base (TREE_TYPE (intype), TREE_TYPE (type),
154 ba_check, NULL);
155 if (!binfo)
157 /* Try base to derived conversion. */
158 binfo = lookup_base (TREE_TYPE (type), TREE_TYPE (intype),
159 ba_check, NULL);
160 code = MINUS_EXPR;
162 if (binfo == error_mark_node)
163 return error_mark_node;
164 if (binfo)
166 expr = build_base_path (code, expr, binfo, 0);
167 /* Add any qualifier conversions. */
168 if (!same_type_p (TREE_TYPE (TREE_TYPE (expr)),
169 TREE_TYPE (type)))
171 expr = build1 (NOP_EXPR, type, expr);
172 TREE_CONSTANT (expr) =
173 TREE_CONSTANT (TREE_OPERAND (expr, 0));
175 return expr;
179 if (TYPE_PTRMEM_P (type) && TYPE_PTRMEM_P (intype))
181 tree b1;
182 tree b2;
183 tree binfo;
184 enum tree_code code = PLUS_EXPR;
185 base_kind bk;
187 b1 = TYPE_OFFSET_BASETYPE (TREE_TYPE (type));
188 b2 = TYPE_OFFSET_BASETYPE (TREE_TYPE (intype));
189 binfo = lookup_base (b1, b2, ba_check, &bk);
190 if (!binfo)
192 binfo = lookup_base (b2, b1, ba_check, &bk);
193 code = MINUS_EXPR;
195 if (binfo == error_mark_node)
196 return error_mark_node;
198 if (bk == bk_via_virtual)
200 if (force)
201 warning ("pointer to member cast from `%T' to `%T' is via virtual base",
202 TREE_TYPE (intype), TREE_TYPE (type));
203 else
205 error ("pointer to member cast from `%T' to `%T' is via virtual base",
206 TREE_TYPE (intype), TREE_TYPE (type));
207 return error_mark_node;
209 /* This is a reinterpret cast, whose result is unspecified.
210 We choose to do nothing. */
211 return build1 (NOP_EXPR, type, expr);
214 if (TREE_CODE (expr) == PTRMEM_CST)
215 expr = cplus_expand_constant (expr);
217 if (binfo)
218 expr = size_binop (code, convert (sizetype, expr),
219 BINFO_OFFSET (binfo));
221 else if (TYPE_PTRMEMFUNC_P (type))
223 error ("cannot convert `%E' from type `%T' to type `%T'",
224 expr, intype, type);
225 return error_mark_node;
228 rval = build1 (NOP_EXPR, type, expr);
229 TREE_CONSTANT (rval) = TREE_CONSTANT (expr);
230 return rval;
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)))
330 expr = build1 (NOP_EXPR, type, expr);
331 TREE_CONSTANT (expr) =
332 TREE_CONSTANT (TREE_OPERAND (expr, 0));
334 return expr;
340 return cp_convert_to_pointer (type, expr, true);
343 /* We are passing something to a function which requires a reference.
344 The type we are interested in is in TYPE. The initial
345 value we have to begin with is in ARG.
347 FLAGS controls how we manage access checking.
348 DIRECT_BIND in FLAGS controls how any temporaries are generated.
349 If DIRECT_BIND is set, DECL is the reference we're binding to. */
351 static tree
352 build_up_reference (tree type, tree arg, int flags, tree decl)
354 tree rval;
355 tree argtype = TREE_TYPE (arg);
356 tree target_type = TREE_TYPE (type);
357 tree stmt_expr = NULL_TREE;
359 my_friendly_assert (TREE_CODE (type) == REFERENCE_TYPE, 187);
361 if ((flags & DIRECT_BIND) && ! real_lvalue_p (arg))
363 /* Create a new temporary variable. We can't just use a TARGET_EXPR
364 here because it needs to live as long as DECL. */
365 tree targ = arg;
367 arg = build_decl (VAR_DECL, NULL_TREE, argtype);
368 DECL_ARTIFICIAL (arg) = 1;
369 TREE_USED (arg) = 1;
370 TREE_STATIC (arg) = TREE_STATIC (decl);
372 if (TREE_STATIC (decl))
374 /* Namespace-scope or local static; give it a mangled name. */
375 tree name = mangle_ref_init_variable (decl);
376 DECL_NAME (arg) = name;
377 SET_DECL_ASSEMBLER_NAME (arg, name);
378 arg = pushdecl_top_level (arg);
380 else
382 /* Automatic; make sure we handle the cleanup properly. */
383 maybe_push_cleanup_level (argtype);
384 /* Don't push unnamed temps. Do set DECL_CONTEXT, though. */
385 DECL_CONTEXT (arg) = current_function_decl;
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 its
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 lookup_base for the access control. */
409 tree binfo = lookup_base (argtype, target_type, ba_check, NULL);
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 = build_base_path (PLUS_EXPR, rval, binfo, 1);
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 (tree reftype, tree intype, tree decl)
442 tree ttl = TREE_TYPE (reftype);
444 if (!CP_TYPE_CONST_NON_VOLATILE_P (ttl))
446 const char *msg;
448 if (CP_TYPE_VOLATILE_P (ttl) && decl)
449 msg = "initialization of volatile reference type `%#T' from rvalue of type `%T'";
450 else if (CP_TYPE_VOLATILE_P (ttl))
451 msg = "conversion to volatile reference type `%#T' from rvalue of type `%T'";
452 else if (decl)
453 msg = "initialization of non-const reference type `%#T' from rvalue of type `%T'";
454 else
455 msg = "conversion to non-const reference type `%#T' from rvalue of type `%T'";
457 pedwarn (msg, reftype, intype);
461 /* For C++: Only need to do one-level references, but cannot
462 get tripped up on signed/unsigned differences.
464 DECL is either NULL_TREE or the _DECL node for a reference that is being
465 initialized. It can be error_mark_node if we don't know the _DECL but
466 we know it's an initialization. */
468 tree
469 convert_to_reference (tree reftype, tree expr, int convtype,
470 int flags, tree decl)
472 register tree type = TYPE_MAIN_VARIANT (TREE_TYPE (reftype));
473 register tree intype;
474 tree rval = NULL_TREE;
475 tree rval_as_conversion = NULL_TREE;
476 int i;
478 if (TREE_CODE (type) == FUNCTION_TYPE
479 && TREE_TYPE (expr) == unknown_type_node)
480 expr = instantiate_type (type, expr,
481 (flags & LOOKUP_COMPLAIN)
482 ? tf_error | tf_warning : tf_none);
483 else
484 expr = convert_from_reference (expr);
486 if (expr == error_mark_node)
487 return error_mark_node;
489 intype = TREE_TYPE (expr);
491 my_friendly_assert (TREE_CODE (intype) != REFERENCE_TYPE, 364);
493 intype = TYPE_MAIN_VARIANT (intype);
495 i = comp_target_types (type, intype, 0);
497 if (i <= 0 && (convtype & CONV_IMPLICIT) && IS_AGGR_TYPE (intype)
498 && ! (flags & LOOKUP_NO_CONVERSION))
500 /* Look for a user-defined conversion to lvalue that we can use. */
502 rval_as_conversion
503 = build_type_conversion (reftype, expr, 1);
505 if (rval_as_conversion && rval_as_conversion != error_mark_node
506 && real_lvalue_p (rval_as_conversion))
508 expr = rval_as_conversion;
509 rval_as_conversion = NULL_TREE;
510 intype = type;
511 i = 1;
515 if (((convtype & CONV_STATIC) && i == -1)
516 || ((convtype & CONV_IMPLICIT) && i == 1))
518 if (flags & LOOKUP_COMPLAIN)
520 tree ttl = TREE_TYPE (reftype);
521 tree ttr = lvalue_type (expr);
523 if (! real_lvalue_p (expr))
524 warn_ref_binding (reftype, intype, decl);
526 if (! (convtype & CONV_CONST)
527 && !at_least_as_qualified_p (ttl, ttr))
528 pedwarn ("conversion from `%T' to `%T' discards qualifiers",
529 ttr, reftype);
532 return build_up_reference (reftype, expr, flags, decl);
534 else if ((convtype & CONV_REINTERPRET) && lvalue_p (expr))
536 /* When casting an lvalue to a reference type, just convert into
537 a pointer to the new type and deference it. This is allowed
538 by San Diego WP section 5.2.9 paragraph 12, though perhaps it
539 should be done directly (jason). (int &)ri ---> *(int*)&ri */
541 /* B* bp; A& ar = (A&)bp; is valid, but it's probably not what they
542 meant. */
543 if (TREE_CODE (intype) == POINTER_TYPE
544 && (comptypes (TREE_TYPE (intype), type,
545 COMPARE_BASE | COMPARE_RELAXED )))
546 warning ("casting `%T' to `%T' does not dereference pointer",
547 intype, reftype);
549 rval = build_unary_op (ADDR_EXPR, expr, 0);
550 if (rval != error_mark_node)
551 rval = convert_force (build_pointer_type (TREE_TYPE (reftype)),
552 rval, 0);
553 if (rval != error_mark_node)
554 rval = build1 (NOP_EXPR, reftype, rval);
556 else
558 rval = convert_for_initialization (NULL_TREE, type, expr, flags,
559 "converting", 0, 0);
560 if (rval == NULL_TREE || rval == error_mark_node)
561 return rval;
562 warn_ref_binding (reftype, intype, decl);
563 rval = build_up_reference (reftype, rval, flags, decl);
566 if (rval)
568 /* If we found a way to convert earlier, then use it. */
569 return rval;
572 my_friendly_assert (TREE_CODE (intype) != OFFSET_TYPE, 189);
574 if (flags & LOOKUP_COMPLAIN)
575 error ("cannot convert type `%T' to type `%T'", intype, reftype);
577 if (flags & LOOKUP_SPECULATIVELY)
578 return NULL_TREE;
580 return error_mark_node;
583 /* We are using a reference VAL for its value. Bash that reference all the
584 way down to its lowest form. */
586 tree
587 convert_from_reference (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);
595 return val;
598 /* Implicitly convert the lvalue EXPR to another lvalue of type TOTYPE,
599 preserving cv-qualification. */
601 tree
602 convert_lvalue (tree totype, tree expr)
604 totype = cp_build_qualified_type (totype, TYPE_QUALS (TREE_TYPE (expr)));
605 totype = build_reference_type (totype);
606 expr = convert_to_reference (totype, expr, CONV_IMPLICIT, LOOKUP_NORMAL,
607 NULL_TREE);
608 return convert_from_reference (expr);
611 /* C++ conversions, preference to static cast conversions. */
613 tree
614 cp_convert (tree type, tree expr)
616 return ocp_convert (type, expr, CONV_OLD_CONVERT, LOOKUP_NORMAL);
619 /* Conversion...
621 FLAGS indicates how we should behave. */
623 tree
624 ocp_convert (tree type, tree expr, int convtype, int flags)
626 register tree e = expr;
627 register enum tree_code code = TREE_CODE (type);
629 if (e == error_mark_node
630 || TREE_TYPE (e) == error_mark_node)
631 return error_mark_node;
633 complete_type (type);
634 complete_type (TREE_TYPE (expr));
636 e = decl_constant_value (e);
638 if (IS_AGGR_TYPE (type) && (convtype & CONV_FORCE_TEMP)
639 /* Some internal structures (vtable_entry_type, sigtbl_ptr_type)
640 don't go through finish_struct, so they don't have the synthesized
641 constructors. So don't force a temporary. */
642 && TYPE_HAS_CONSTRUCTOR (type))
643 /* We need a new temporary; don't take this shortcut. */;
644 else if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (TREE_TYPE (e)))
646 if (same_type_p (type, TREE_TYPE (e)))
647 /* The call to fold will not always remove the NOP_EXPR as
648 might be expected, since if one of the types is a typedef;
649 the comparsion in fold is just equality of pointers, not a
650 call to comptypes. We don't call fold in this case because
651 that can result in infinite recursion; fold will call
652 convert, which will call ocp_convert, etc. */
653 return e;
654 /* For complex data types, we need to perform componentwise
655 conversion. */
656 else if (TREE_CODE (type) == COMPLEX_TYPE)
657 return fold (convert_to_complex (type, e));
658 else if (TREE_CODE (e) == TARGET_EXPR)
660 /* Don't build a NOP_EXPR of class type. Instead, change the
661 type of the temporary. Only allow this for cv-qual changes,
662 though. */
663 if (!same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (e)),
664 TYPE_MAIN_VARIANT (type)))
665 abort ();
666 TREE_TYPE (e) = TREE_TYPE (TARGET_EXPR_SLOT (e)) = type;
667 return e;
669 else if (CLASS_TYPE_P (type))
670 abort ();
671 else
672 return fold (build1 (NOP_EXPR, type, e));
675 if (code == VOID_TYPE && (convtype & CONV_STATIC))
677 e = convert_to_void (e, /*implicit=*/NULL);
678 return e;
681 /* Just convert to the type of the member. */
682 if (code == OFFSET_TYPE)
684 type = TREE_TYPE (type);
685 code = TREE_CODE (type);
688 if (TREE_CODE (e) == OFFSET_REF)
689 e = resolve_offset_ref (e);
691 if (INTEGRAL_CODE_P (code))
693 tree intype = TREE_TYPE (e);
694 /* enum = enum, enum = int, enum = float, (enum)pointer are all
695 errors. */
696 if (TREE_CODE (type) == ENUMERAL_TYPE
697 && ((ARITHMETIC_TYPE_P (intype) && ! (convtype & CONV_STATIC))
698 || (TREE_CODE (intype) == POINTER_TYPE)))
700 pedwarn ("conversion from `%#T' to `%#T'", intype, type);
702 if (flag_pedantic_errors)
703 return error_mark_node;
705 if (IS_AGGR_TYPE (intype))
707 tree rval;
708 rval = build_type_conversion (type, e, 1);
709 if (rval)
710 return rval;
711 if (flags & LOOKUP_COMPLAIN)
712 error ("`%#T' used where a `%T' was expected", intype, type);
713 if (flags & LOOKUP_SPECULATIVELY)
714 return NULL_TREE;
715 return error_mark_node;
717 if (code == BOOLEAN_TYPE)
719 tree fn = NULL_TREE;
721 /* Common Ada/Pascal programmer's mistake. We always warn
722 about this since it is so bad. */
723 if (TREE_CODE (expr) == FUNCTION_DECL)
724 fn = expr;
725 else if (TREE_CODE (expr) == ADDR_EXPR
726 && TREE_CODE (TREE_OPERAND (expr, 0)) == FUNCTION_DECL)
727 fn = TREE_OPERAND (expr, 0);
728 if (fn && !DECL_WEAK (fn))
729 warning ("the address of `%D', will always be `true'", fn);
730 return cp_truthvalue_conversion (e);
732 return fold (convert_to_integer (type, e));
734 if (code == POINTER_TYPE || code == REFERENCE_TYPE
735 || TYPE_PTRMEMFUNC_P (type))
736 return fold (cp_convert_to_pointer (type, e, false));
737 if (code == VECTOR_TYPE)
738 return fold (convert_to_vector (type, e));
739 if (code == REAL_TYPE || code == COMPLEX_TYPE)
741 if (IS_AGGR_TYPE (TREE_TYPE (e)))
743 tree rval;
744 rval = build_type_conversion (type, e, 1);
745 if (rval)
746 return rval;
747 else
748 if (flags & LOOKUP_COMPLAIN)
749 error ("`%#T' used where a floating point value was expected",
750 TREE_TYPE (e));
752 if (code == REAL_TYPE)
753 return fold (convert_to_real (type, e));
754 else if (code == COMPLEX_TYPE)
755 return fold (convert_to_complex (type, e));
758 /* New C++ semantics: since assignment is now based on
759 memberwise copying, if the rhs type is derived from the
760 lhs type, then we may still do a conversion. */
761 if (IS_AGGR_TYPE_CODE (code))
763 tree dtype = TREE_TYPE (e);
764 tree ctor = NULL_TREE;
766 dtype = TYPE_MAIN_VARIANT (dtype);
768 /* Conversion between aggregate types. New C++ semantics allow
769 objects of derived type to be cast to objects of base type.
770 Old semantics only allowed this between pointers.
772 There may be some ambiguity between using a constructor
773 vs. using a type conversion operator when both apply. */
775 ctor = e;
777 if (abstract_virtuals_error (NULL_TREE, type))
778 return error_mark_node;
780 if ((flags & LOOKUP_ONLYCONVERTING)
781 && ! (IS_AGGR_TYPE (dtype) && DERIVED_FROM_P (type, dtype)))
782 /* For copy-initialization, first we create a temp of the proper type
783 with a user-defined conversion sequence, then we direct-initialize
784 the target with the temp (see [dcl.init]). */
785 ctor = build_user_type_conversion (type, ctor, flags);
786 else
787 ctor = build_special_member_call (NULL_TREE,
788 complete_ctor_identifier,
789 build_tree_list (NULL_TREE, ctor),
790 TYPE_BINFO (type), flags);
791 if (ctor)
792 return build_cplus_new (type, ctor);
795 if (flags & LOOKUP_COMPLAIN)
796 error ("conversion from `%T' to non-scalar type `%T' requested",
797 TREE_TYPE (expr), type);
798 if (flags & LOOKUP_SPECULATIVELY)
799 return NULL_TREE;
800 return error_mark_node;
803 /* When an expression is used in a void context, its value is discarded and
804 no lvalue-rvalue and similar conversions happen [expr.static.cast/4,
805 stmt.expr/1, expr.comma/1]. This permits dereferencing an incomplete type
806 in a void context. The C++ standard does not define what an `access' to an
807 object is, but there is reason to beleive that it is the lvalue to rvalue
808 conversion -- if it were not, `*&*p = 1' would violate [expr]/4 in that it
809 accesses `*p' not to calculate the value to be stored. But, dcl.type.cv/8
810 indicates that volatile semantics should be the same between C and C++
811 where ever possible. C leaves it implementation defined as to what
812 constitutes an access to a volatile. So, we interpret `*vp' as a read of
813 the volatile object `vp' points to, unless that is an incomplete type. For
814 volatile references we do not do this interpretation, because that would
815 make it impossible to ignore the reference return value from functions. We
816 issue warnings in the confusing cases.
818 IMPLICIT is tells us the context of an implicit void conversion. */
820 tree
821 convert_to_void (tree expr, const char *implicit)
823 if (expr == error_mark_node
824 || TREE_TYPE (expr) == error_mark_node)
825 return error_mark_node;
826 if (!TREE_TYPE (expr))
827 return expr;
828 if (VOID_TYPE_P (TREE_TYPE (expr)))
829 return expr;
830 switch (TREE_CODE (expr))
832 case COND_EXPR:
834 /* The two parts of a cond expr might be separate lvalues. */
835 tree op1 = TREE_OPERAND (expr,1);
836 tree op2 = TREE_OPERAND (expr,2);
837 tree new_op1 = convert_to_void (op1, implicit);
838 tree new_op2 = convert_to_void (op2, implicit);
840 expr = build (COND_EXPR, TREE_TYPE (new_op1),
841 TREE_OPERAND (expr, 0), new_op1, new_op2);
842 break;
845 case COMPOUND_EXPR:
847 /* The second part of a compound expr contains the value. */
848 tree op1 = TREE_OPERAND (expr,1);
849 tree new_op1 = convert_to_void (op1, implicit);
851 if (new_op1 != op1)
853 tree t = build (COMPOUND_EXPR, TREE_TYPE (new_op1),
854 TREE_OPERAND (expr, 0), new_op1);
855 TREE_SIDE_EFFECTS (t) = TREE_SIDE_EFFECTS (expr);
856 TREE_NO_UNUSED_WARNING (t) = TREE_NO_UNUSED_WARNING (expr);
857 expr = t;
860 break;
863 case NON_LVALUE_EXPR:
864 case NOP_EXPR:
865 /* These have already decayed to rvalue. */
866 break;
868 case CALL_EXPR: /* we have a special meaning for volatile void fn() */
869 break;
871 case INDIRECT_REF:
873 tree type = TREE_TYPE (expr);
874 int is_reference = TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0)))
875 == REFERENCE_TYPE;
876 int is_volatile = TYPE_VOLATILE (type);
877 int is_complete = COMPLETE_TYPE_P (complete_type (type));
879 if (is_volatile && !is_complete)
880 warning ("object of incomplete type `%T' will not be accessed in %s",
881 type, implicit ? implicit : "void context");
882 else if (is_reference && is_volatile)
883 warning ("object of type `%T' will not be accessed in %s",
884 TREE_TYPE (TREE_OPERAND (expr, 0)),
885 implicit ? implicit : "void context");
886 if (is_reference || !is_volatile || !is_complete)
887 expr = TREE_OPERAND (expr, 0);
889 break;
892 case VAR_DECL:
894 /* External variables might be incomplete. */
895 tree type = TREE_TYPE (expr);
896 int is_complete = COMPLETE_TYPE_P (complete_type (type));
898 if (TYPE_VOLATILE (type) && !is_complete)
899 warning ("object `%E' of incomplete type `%T' will not be accessed in %s",
900 expr, type, implicit ? implicit : "void context");
901 break;
904 case OFFSET_REF:
905 expr = resolve_offset_ref (expr);
906 break;
908 default:;
911 tree probe = expr;
913 if (TREE_CODE (probe) == ADDR_EXPR)
914 probe = TREE_OPERAND (expr, 0);
915 if (type_unknown_p (probe))
917 /* [over.over] enumerates the places where we can take the address
918 of an overloaded function, and this is not one of them. */
919 pedwarn ("%s cannot resolve address of overloaded function",
920 implicit ? implicit : "void cast");
922 else if (implicit && probe == expr && is_overloaded_fn (probe))
923 /* Only warn when there is no &. */
924 warning ("%s is a reference, not call, to function `%E'",
925 implicit, expr);
928 if (expr != error_mark_node && !VOID_TYPE_P (TREE_TYPE (expr)))
930 /* FIXME: This is where we should check for expressions with no
931 effects. At the moment we do that in both build_x_component_expr
932 and expand_expr_stmt -- inconsistently too. For the moment
933 leave implicit void conversions unadorned so that expand_expr_stmt
934 has a chance of detecting some of the cases. */
935 if (!implicit)
936 expr = build1 (CONVERT_EXPR, void_type_node, expr);
938 return expr;
941 /* Create an expression whose value is that of EXPR,
942 converted to type TYPE. The TREE_TYPE of the value
943 is always TYPE. This function implements all reasonable
944 conversions; callers should filter out those that are
945 not permitted by the language being compiled.
947 Most of this routine is from build_reinterpret_cast.
949 The backend cannot call cp_convert (what was convert) because
950 conversions to/from basetypes may involve memory references
951 (vbases) and adding or subtracting small values (multiple
952 inheritance), but it calls convert from the constant folding code
953 on subtrees of already built trees after it has ripped them apart.
955 Also, if we ever support range variables, we'll probably also have to
956 do a little bit more work. */
958 tree
959 convert (tree type, tree expr)
961 tree intype;
963 if (type == error_mark_node || expr == error_mark_node)
964 return error_mark_node;
966 intype = TREE_TYPE (expr);
968 if (POINTER_TYPE_P (type) && POINTER_TYPE_P (intype))
970 expr = decl_constant_value (expr);
971 return fold (build1 (NOP_EXPR, type, expr));
974 return ocp_convert (type, expr, CONV_OLD_CONVERT,
975 LOOKUP_NORMAL|LOOKUP_NO_CONVERSION);
978 /* Like cp_convert, except permit conversions to take place which
979 are not normally allowed due to access restrictions
980 (such as conversion from sub-type to private super-type). */
982 tree
983 convert_force (tree type, tree expr, int convtype)
985 register tree e = expr;
986 register enum tree_code code = TREE_CODE (type);
988 if (code == REFERENCE_TYPE)
989 return fold (convert_to_reference (type, e, CONV_C_CAST, LOOKUP_COMPLAIN,
990 NULL_TREE));
991 else if (TREE_CODE (TREE_TYPE (e)) == REFERENCE_TYPE)
992 e = convert_from_reference (e);
994 if (code == POINTER_TYPE)
995 return fold (convert_to_pointer_force (type, e));
997 /* From typeck.c convert_for_assignment */
998 if (((TREE_CODE (TREE_TYPE (e)) == POINTER_TYPE && TREE_CODE (e) == ADDR_EXPR
999 && TREE_CODE (TREE_TYPE (e)) == POINTER_TYPE
1000 && TREE_CODE (TREE_TYPE (TREE_TYPE (e))) == METHOD_TYPE)
1001 || integer_zerop (e)
1002 || TYPE_PTRMEMFUNC_P (TREE_TYPE (e)))
1003 && TYPE_PTRMEMFUNC_P (type))
1005 /* compatible pointer to member functions. */
1006 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), e, 1);
1009 return ocp_convert (type, e, CONV_C_CAST|convtype, LOOKUP_NORMAL);
1012 /* Convert an aggregate EXPR to type XTYPE. If a conversion
1013 exists, return the attempted conversion. This may
1014 return ERROR_MARK_NODE if the conversion is not
1015 allowed (references private members, etc).
1016 If no conversion exists, NULL_TREE is returned.
1018 If (FOR_SURE & 1) is nonzero, then we allow this type conversion
1019 to take place immediately. Otherwise, we build a SAVE_EXPR
1020 which can be evaluated if the results are ever needed.
1022 Changes to this functions should be mirrored in user_harshness.
1024 FIXME: Ambiguity checking is wrong. Should choose one by the implicit
1025 object parameter, or by the second standard conversion sequence if
1026 that doesn't do it. This will probably wait for an overloading rewrite.
1027 (jason 8/9/95) */
1029 tree
1030 build_type_conversion (tree xtype, tree expr, int for_sure)
1032 /* C++: check to see if we can convert this aggregate type
1033 into the required type. */
1034 return build_user_type_conversion
1035 (xtype, expr, for_sure ? LOOKUP_NORMAL : 0);
1038 /* Convert the given EXPR to one of a group of types suitable for use in an
1039 expression. DESIRES is a combination of various WANT_* flags (q.v.)
1040 which indicates which types are suitable. If COMPLAIN is true, complain
1041 about ambiguity; otherwise, the caller will deal with it. */
1043 tree
1044 build_expr_type_conversion (int desires, tree expr, bool complain)
1046 tree basetype = TREE_TYPE (expr);
1047 tree conv = NULL_TREE;
1048 tree winner = NULL_TREE;
1050 if (expr == null_node
1051 && (desires & WANT_INT)
1052 && !(desires & WANT_NULL))
1053 warning ("converting NULL to non-pointer type");
1055 if (TREE_CODE (expr) == OFFSET_REF)
1056 expr = resolve_offset_ref (expr);
1057 expr = convert_from_reference (expr);
1058 basetype = TREE_TYPE (expr);
1060 if (basetype == error_mark_node)
1061 return error_mark_node;
1063 if (! IS_AGGR_TYPE (basetype))
1064 switch (TREE_CODE (basetype))
1066 case INTEGER_TYPE:
1067 if ((desires & WANT_NULL) && null_ptr_cst_p (expr))
1068 return expr;
1069 /* else fall through... */
1071 case BOOLEAN_TYPE:
1072 return (desires & WANT_INT) ? expr : NULL_TREE;
1073 case ENUMERAL_TYPE:
1074 return (desires & WANT_ENUM) ? expr : NULL_TREE;
1075 case REAL_TYPE:
1076 return (desires & WANT_FLOAT) ? expr : NULL_TREE;
1077 case POINTER_TYPE:
1078 return (desires & WANT_POINTER) ? expr : NULL_TREE;
1080 case FUNCTION_TYPE:
1081 case ARRAY_TYPE:
1082 return (desires & WANT_POINTER) ? default_conversion (expr)
1083 : NULL_TREE;
1084 default:
1085 return NULL_TREE;
1088 /* The code for conversions from class type is currently only used for
1089 delete expressions. Other expressions are handled by build_new_op. */
1091 if (! TYPE_HAS_CONVERSION (basetype))
1092 return NULL_TREE;
1094 for (conv = lookup_conversions (basetype); conv; conv = TREE_CHAIN (conv))
1096 int win = 0;
1097 tree candidate;
1098 tree cand = TREE_VALUE (conv);
1100 if (winner && winner == cand)
1101 continue;
1103 candidate = TREE_TYPE (TREE_TYPE (cand));
1104 if (TREE_CODE (candidate) == REFERENCE_TYPE)
1105 candidate = TREE_TYPE (candidate);
1107 switch (TREE_CODE (candidate))
1109 case BOOLEAN_TYPE:
1110 case INTEGER_TYPE:
1111 win = (desires & WANT_INT); break;
1112 case ENUMERAL_TYPE:
1113 win = (desires & WANT_ENUM); break;
1114 case REAL_TYPE:
1115 win = (desires & WANT_FLOAT); break;
1116 case POINTER_TYPE:
1117 win = (desires & WANT_POINTER); break;
1119 default:
1120 break;
1123 if (win)
1125 if (winner)
1127 if (complain)
1129 error ("ambiguous default type conversion from `%T'",
1130 basetype);
1131 error (" candidate conversions include `%D' and `%D'",
1132 winner, cand);
1134 return error_mark_node;
1136 else
1137 winner = cand;
1141 if (winner)
1143 tree type = TREE_TYPE (TREE_TYPE (winner));
1144 if (TREE_CODE (type) == REFERENCE_TYPE)
1145 type = TREE_TYPE (type);
1146 return build_user_type_conversion (type, expr, LOOKUP_NORMAL);
1149 return NULL_TREE;
1152 /* Implements integral promotion (4.1) and float->double promotion. */
1154 tree
1155 type_promotes_to (tree type)
1157 int type_quals;
1159 if (type == error_mark_node)
1160 return error_mark_node;
1162 type_quals = cp_type_quals (type);
1163 type = TYPE_MAIN_VARIANT (type);
1165 /* bool always promotes to int (not unsigned), even if it's the same
1166 size. */
1167 if (type == boolean_type_node)
1168 type = integer_type_node;
1170 /* Normally convert enums to int, but convert wide enums to something
1171 wider. */
1172 else if (TREE_CODE (type) == ENUMERAL_TYPE
1173 || type == wchar_type_node)
1175 int precision = MAX (TYPE_PRECISION (type),
1176 TYPE_PRECISION (integer_type_node));
1177 tree totype = c_common_type_for_size (precision, 0);
1178 if (TREE_UNSIGNED (type)
1179 && ! int_fits_type_p (TYPE_MAX_VALUE (type), totype))
1180 type = c_common_type_for_size (precision, 1);
1181 else
1182 type = totype;
1184 else if (c_promoting_integer_type_p (type))
1186 /* Retain unsignedness if really not getting bigger. */
1187 if (TREE_UNSIGNED (type)
1188 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
1189 type = unsigned_type_node;
1190 else
1191 type = integer_type_node;
1193 else if (type == float_type_node)
1194 type = double_type_node;
1196 return cp_build_qualified_type (type, type_quals);
1199 /* The routines below this point are carefully written to conform to
1200 the standard. They use the same terminology, and follow the rules
1201 closely. Although they are used only in pt.c at the moment, they
1202 should presumably be used everywhere in the future. */
1204 /* Attempt to perform qualification conversions on EXPR to convert it
1205 to TYPE. Return the resulting expression, or error_mark_node if
1206 the conversion was impossible. */
1208 tree
1209 perform_qualification_conversions (tree type, tree expr)
1211 if (TREE_CODE (type) == POINTER_TYPE
1212 && TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE
1213 && comp_ptr_ttypes (TREE_TYPE (type), TREE_TYPE (TREE_TYPE (expr))))
1214 return build1 (NOP_EXPR, type, expr);
1215 else
1216 return error_mark_node;