* config/arm/elf.h (ASM_OUTPUT_ALIGNED_COMMON): Remove definition.
[official-gcc.git] / gcc / cp / cvt.c
blobc62edf09cb85486e6097307d86a1a5b268e1c272
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 (TREE_ADDRESSABLE (type))
651 /* We shouldn't be treating objects of ADDRESSABLE type as rvalues. */
652 abort ();
653 else
654 return fold (build1 (NOP_EXPR, type, e));
657 if (code == VOID_TYPE && (convtype & CONV_STATIC))
659 e = convert_to_void (e, /*implicit=*/NULL);
660 return e;
663 /* Just convert to the type of the member. */
664 if (code == OFFSET_TYPE)
666 type = TREE_TYPE (type);
667 code = TREE_CODE (type);
670 if (TREE_CODE (e) == OFFSET_REF)
671 e = resolve_offset_ref (e);
673 if (INTEGRAL_CODE_P (code))
675 tree intype = TREE_TYPE (e);
676 /* enum = enum, enum = int, enum = float, (enum)pointer are all
677 errors. */
678 if (TREE_CODE (type) == ENUMERAL_TYPE
679 && ((ARITHMETIC_TYPE_P (intype) && ! (convtype & CONV_STATIC))
680 || (TREE_CODE (intype) == POINTER_TYPE)))
682 pedwarn ("conversion from `%#T' to `%#T'", intype, type);
684 if (flag_pedantic_errors)
685 return error_mark_node;
687 if (IS_AGGR_TYPE (intype))
689 tree rval;
690 rval = build_type_conversion (type, e);
691 if (rval)
692 return rval;
693 if (flags & LOOKUP_COMPLAIN)
694 error ("`%#T' used where a `%T' was expected", intype, type);
695 if (flags & LOOKUP_SPECULATIVELY)
696 return NULL_TREE;
697 return error_mark_node;
699 if (code == BOOLEAN_TYPE)
701 tree fn = NULL_TREE;
703 /* Common Ada/Pascal programmer's mistake. We always warn
704 about this since it is so bad. */
705 if (TREE_CODE (expr) == FUNCTION_DECL)
706 fn = expr;
707 else if (TREE_CODE (expr) == ADDR_EXPR
708 && TREE_CODE (TREE_OPERAND (expr, 0)) == FUNCTION_DECL)
709 fn = TREE_OPERAND (expr, 0);
710 if (fn && !DECL_WEAK (fn))
711 warning ("the address of `%D', will always be `true'", fn);
712 return cp_truthvalue_conversion (e);
714 return fold (convert_to_integer (type, e));
716 if (code == POINTER_TYPE || code == REFERENCE_TYPE
717 || TYPE_PTRMEMFUNC_P (type))
718 return fold (cp_convert_to_pointer (type, e, false));
719 if (code == VECTOR_TYPE)
720 return fold (convert_to_vector (type, e));
721 if (code == REAL_TYPE || code == COMPLEX_TYPE)
723 if (IS_AGGR_TYPE (TREE_TYPE (e)))
725 tree rval;
726 rval = build_type_conversion (type, e);
727 if (rval)
728 return rval;
729 else
730 if (flags & LOOKUP_COMPLAIN)
731 error ("`%#T' used where a floating point value was expected",
732 TREE_TYPE (e));
734 if (code == REAL_TYPE)
735 return fold (convert_to_real (type, e));
736 else if (code == COMPLEX_TYPE)
737 return fold (convert_to_complex (type, e));
740 /* New C++ semantics: since assignment is now based on
741 memberwise copying, if the rhs type is derived from the
742 lhs type, then we may still do a conversion. */
743 if (IS_AGGR_TYPE_CODE (code))
745 tree dtype = TREE_TYPE (e);
746 tree ctor = NULL_TREE;
748 dtype = TYPE_MAIN_VARIANT (dtype);
750 /* Conversion between aggregate types. New C++ semantics allow
751 objects of derived type to be cast to objects of base type.
752 Old semantics only allowed this between pointers.
754 There may be some ambiguity between using a constructor
755 vs. using a type conversion operator when both apply. */
757 ctor = e;
759 if (abstract_virtuals_error (NULL_TREE, type))
760 return error_mark_node;
762 if ((flags & LOOKUP_ONLYCONVERTING)
763 && ! (IS_AGGR_TYPE (dtype) && DERIVED_FROM_P (type, dtype)))
764 /* For copy-initialization, first we create a temp of the proper type
765 with a user-defined conversion sequence, then we direct-initialize
766 the target with the temp (see [dcl.init]). */
767 ctor = build_user_type_conversion (type, ctor, flags);
768 else
769 ctor = build_special_member_call (NULL_TREE,
770 complete_ctor_identifier,
771 build_tree_list (NULL_TREE, ctor),
772 TYPE_BINFO (type), flags);
773 if (ctor)
774 return build_cplus_new (type, ctor);
777 if (flags & LOOKUP_COMPLAIN)
778 error ("conversion from `%T' to non-scalar type `%T' requested",
779 TREE_TYPE (expr), type);
780 if (flags & LOOKUP_SPECULATIVELY)
781 return NULL_TREE;
782 return error_mark_node;
785 /* When an expression is used in a void context, its value is discarded and
786 no lvalue-rvalue and similar conversions happen [expr.static.cast/4,
787 stmt.expr/1, expr.comma/1]. This permits dereferencing an incomplete type
788 in a void context. The C++ standard does not define what an `access' to an
789 object is, but there is reason to beleive that it is the lvalue to rvalue
790 conversion -- if it were not, `*&*p = 1' would violate [expr]/4 in that it
791 accesses `*p' not to calculate the value to be stored. But, dcl.type.cv/8
792 indicates that volatile semantics should be the same between C and C++
793 where ever possible. C leaves it implementation defined as to what
794 constitutes an access to a volatile. So, we interpret `*vp' as a read of
795 the volatile object `vp' points to, unless that is an incomplete type. For
796 volatile references we do not do this interpretation, because that would
797 make it impossible to ignore the reference return value from functions. We
798 issue warnings in the confusing cases.
800 IMPLICIT is tells us the context of an implicit void conversion. */
802 tree
803 convert_to_void (tree expr, const char *implicit)
805 if (expr == error_mark_node
806 || TREE_TYPE (expr) == error_mark_node)
807 return error_mark_node;
808 if (!TREE_TYPE (expr))
809 return expr;
810 if (VOID_TYPE_P (TREE_TYPE (expr)))
811 return expr;
812 switch (TREE_CODE (expr))
814 case COND_EXPR:
816 /* The two parts of a cond expr might be separate lvalues. */
817 tree op1 = TREE_OPERAND (expr,1);
818 tree op2 = TREE_OPERAND (expr,2);
819 tree new_op1 = convert_to_void (op1, implicit);
820 tree new_op2 = convert_to_void (op2, implicit);
822 expr = build (COND_EXPR, TREE_TYPE (new_op1),
823 TREE_OPERAND (expr, 0), new_op1, new_op2);
824 break;
827 case COMPOUND_EXPR:
829 /* The second part of a compound expr contains the value. */
830 tree op1 = TREE_OPERAND (expr,1);
831 tree new_op1 = convert_to_void (op1, implicit);
833 if (new_op1 != op1)
835 tree t = build (COMPOUND_EXPR, TREE_TYPE (new_op1),
836 TREE_OPERAND (expr, 0), new_op1);
837 TREE_SIDE_EFFECTS (t) = TREE_SIDE_EFFECTS (expr);
838 TREE_NO_UNUSED_WARNING (t) = TREE_NO_UNUSED_WARNING (expr);
839 expr = t;
842 break;
845 case NON_LVALUE_EXPR:
846 case NOP_EXPR:
847 /* These have already decayed to rvalue. */
848 break;
850 case CALL_EXPR: /* we have a special meaning for volatile void fn() */
851 break;
853 case INDIRECT_REF:
855 tree type = TREE_TYPE (expr);
856 int is_reference = TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0)))
857 == REFERENCE_TYPE;
858 int is_volatile = TYPE_VOLATILE (type);
859 int is_complete = COMPLETE_TYPE_P (complete_type (type));
861 if (is_volatile && !is_complete)
862 warning ("object of incomplete type `%T' will not be accessed in %s",
863 type, implicit ? implicit : "void context");
864 else if (is_reference && is_volatile)
865 warning ("object of type `%T' will not be accessed in %s",
866 TREE_TYPE (TREE_OPERAND (expr, 0)),
867 implicit ? implicit : "void context");
868 if (is_reference || !is_volatile || !is_complete)
869 expr = TREE_OPERAND (expr, 0);
871 break;
874 case VAR_DECL:
876 /* External variables might be incomplete. */
877 tree type = TREE_TYPE (expr);
878 int is_complete = COMPLETE_TYPE_P (complete_type (type));
880 if (TYPE_VOLATILE (type) && !is_complete)
881 warning ("object `%E' of incomplete type `%T' will not be accessed in %s",
882 expr, type, implicit ? implicit : "void context");
883 break;
886 case OFFSET_REF:
887 expr = resolve_offset_ref (expr);
888 break;
890 default:;
893 tree probe = expr;
895 if (TREE_CODE (probe) == ADDR_EXPR)
896 probe = TREE_OPERAND (expr, 0);
897 if (type_unknown_p (probe))
899 /* [over.over] enumerates the places where we can take the address
900 of an overloaded function, and this is not one of them. */
901 pedwarn ("%s cannot resolve address of overloaded function",
902 implicit ? implicit : "void cast");
904 else if (implicit && probe == expr && is_overloaded_fn (probe))
905 /* Only warn when there is no &. */
906 warning ("%s is a reference, not call, to function `%E'",
907 implicit, expr);
910 if (expr != error_mark_node && !VOID_TYPE_P (TREE_TYPE (expr)))
912 /* FIXME: This is where we should check for expressions with no
913 effects. At the moment we do that in both build_x_component_expr
914 and expand_expr_stmt -- inconsistently too. For the moment
915 leave implicit void conversions unadorned so that expand_expr_stmt
916 has a chance of detecting some of the cases. */
917 if (!implicit)
918 expr = build1 (CONVERT_EXPR, void_type_node, expr);
920 return expr;
923 /* Create an expression whose value is that of EXPR,
924 converted to type TYPE. The TREE_TYPE of the value
925 is always TYPE. This function implements all reasonable
926 conversions; callers should filter out those that are
927 not permitted by the language being compiled.
929 Most of this routine is from build_reinterpret_cast.
931 The backend cannot call cp_convert (what was convert) because
932 conversions to/from basetypes may involve memory references
933 (vbases) and adding or subtracting small values (multiple
934 inheritance), but it calls convert from the constant folding code
935 on subtrees of already built trees after it has ripped them apart.
937 Also, if we ever support range variables, we'll probably also have to
938 do a little bit more work. */
940 tree
941 convert (tree type, tree expr)
943 tree intype;
945 if (type == error_mark_node || expr == error_mark_node)
946 return error_mark_node;
948 intype = TREE_TYPE (expr);
950 if (POINTER_TYPE_P (type) && POINTER_TYPE_P (intype))
952 expr = decl_constant_value (expr);
953 return fold (build1 (NOP_EXPR, type, expr));
956 return ocp_convert (type, expr, CONV_OLD_CONVERT,
957 LOOKUP_NORMAL|LOOKUP_NO_CONVERSION);
960 /* Like cp_convert, except permit conversions to take place which
961 are not normally allowed due to access restrictions
962 (such as conversion from sub-type to private super-type). */
964 tree
965 convert_force (tree type, tree expr, int convtype)
967 register tree e = expr;
968 register enum tree_code code = TREE_CODE (type);
970 if (code == REFERENCE_TYPE)
971 return fold (convert_to_reference (type, e, CONV_C_CAST, LOOKUP_COMPLAIN,
972 NULL_TREE));
973 else if (TREE_CODE (TREE_TYPE (e)) == REFERENCE_TYPE)
974 e = convert_from_reference (e);
976 if (code == POINTER_TYPE)
977 return fold (convert_to_pointer_force (type, e));
979 /* From typeck.c convert_for_assignment */
980 if (((TREE_CODE (TREE_TYPE (e)) == POINTER_TYPE && TREE_CODE (e) == ADDR_EXPR
981 && TREE_CODE (TREE_TYPE (e)) == POINTER_TYPE
982 && TREE_CODE (TREE_TYPE (TREE_TYPE (e))) == METHOD_TYPE)
983 || integer_zerop (e)
984 || TYPE_PTRMEMFUNC_P (TREE_TYPE (e)))
985 && TYPE_PTRMEMFUNC_P (type))
987 /* compatible pointer to member functions. */
988 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), e, 1);
991 return ocp_convert (type, e, CONV_C_CAST|convtype, LOOKUP_NORMAL);
994 /* Convert an aggregate EXPR to type XTYPE. If a conversion
995 exists, return the attempted conversion. This may
996 return ERROR_MARK_NODE if the conversion is not
997 allowed (references private members, etc).
998 If no conversion exists, NULL_TREE is returned.
1000 FIXME: Ambiguity checking is wrong. Should choose one by the implicit
1001 object parameter, or by the second standard conversion sequence if
1002 that doesn't do it. This will probably wait for an overloading rewrite.
1003 (jason 8/9/95) */
1005 tree
1006 build_type_conversion (tree xtype, tree expr)
1008 /* C++: check to see if we can convert this aggregate type
1009 into the required type. */
1010 return build_user_type_conversion (xtype, expr, LOOKUP_NORMAL);
1013 /* Convert the given EXPR to one of a group of types suitable for use in an
1014 expression. DESIRES is a combination of various WANT_* flags (q.v.)
1015 which indicates which types are suitable. If COMPLAIN is true, complain
1016 about ambiguity; otherwise, the caller will deal with it. */
1018 tree
1019 build_expr_type_conversion (int desires, tree expr, bool complain)
1021 tree basetype = TREE_TYPE (expr);
1022 tree conv = NULL_TREE;
1023 tree winner = NULL_TREE;
1025 if (expr == null_node
1026 && (desires & WANT_INT)
1027 && !(desires & WANT_NULL))
1028 warning ("converting NULL to non-pointer type");
1030 if (TREE_CODE (expr) == OFFSET_REF)
1031 expr = resolve_offset_ref (expr);
1032 expr = convert_from_reference (expr);
1033 basetype = TREE_TYPE (expr);
1035 if (basetype == error_mark_node)
1036 return error_mark_node;
1038 if (! IS_AGGR_TYPE (basetype))
1039 switch (TREE_CODE (basetype))
1041 case INTEGER_TYPE:
1042 if ((desires & WANT_NULL) && null_ptr_cst_p (expr))
1043 return expr;
1044 /* else fall through... */
1046 case BOOLEAN_TYPE:
1047 return (desires & WANT_INT) ? expr : NULL_TREE;
1048 case ENUMERAL_TYPE:
1049 return (desires & WANT_ENUM) ? expr : NULL_TREE;
1050 case REAL_TYPE:
1051 return (desires & WANT_FLOAT) ? expr : NULL_TREE;
1052 case POINTER_TYPE:
1053 return (desires & WANT_POINTER) ? expr : NULL_TREE;
1055 case FUNCTION_TYPE:
1056 case ARRAY_TYPE:
1057 return (desires & WANT_POINTER) ? default_conversion (expr)
1058 : NULL_TREE;
1059 default:
1060 return NULL_TREE;
1063 /* The code for conversions from class type is currently only used for
1064 delete expressions. Other expressions are handled by build_new_op. */
1066 if (! TYPE_HAS_CONVERSION (basetype))
1067 return NULL_TREE;
1069 for (conv = lookup_conversions (basetype); conv; conv = TREE_CHAIN (conv))
1071 int win = 0;
1072 tree candidate;
1073 tree cand = TREE_VALUE (conv);
1075 if (winner && winner == cand)
1076 continue;
1078 candidate = TREE_TYPE (TREE_TYPE (cand));
1079 if (TREE_CODE (candidate) == REFERENCE_TYPE)
1080 candidate = TREE_TYPE (candidate);
1082 switch (TREE_CODE (candidate))
1084 case BOOLEAN_TYPE:
1085 case INTEGER_TYPE:
1086 win = (desires & WANT_INT); break;
1087 case ENUMERAL_TYPE:
1088 win = (desires & WANT_ENUM); break;
1089 case REAL_TYPE:
1090 win = (desires & WANT_FLOAT); break;
1091 case POINTER_TYPE:
1092 win = (desires & WANT_POINTER); break;
1094 default:
1095 break;
1098 if (win)
1100 if (winner)
1102 if (complain)
1104 error ("ambiguous default type conversion from `%T'",
1105 basetype);
1106 error (" candidate conversions include `%D' and `%D'",
1107 winner, cand);
1109 return error_mark_node;
1111 else
1112 winner = cand;
1116 if (winner)
1118 tree type = TREE_TYPE (TREE_TYPE (winner));
1119 if (TREE_CODE (type) == REFERENCE_TYPE)
1120 type = TREE_TYPE (type);
1121 return build_user_type_conversion (type, expr, LOOKUP_NORMAL);
1124 return NULL_TREE;
1127 /* Implements integral promotion (4.1) and float->double promotion. */
1129 tree
1130 type_promotes_to (tree type)
1132 int type_quals;
1134 if (type == error_mark_node)
1135 return error_mark_node;
1137 type_quals = cp_type_quals (type);
1138 type = TYPE_MAIN_VARIANT (type);
1140 /* bool always promotes to int (not unsigned), even if it's the same
1141 size. */
1142 if (type == boolean_type_node)
1143 type = integer_type_node;
1145 /* Normally convert enums to int, but convert wide enums to something
1146 wider. */
1147 else if (TREE_CODE (type) == ENUMERAL_TYPE
1148 || type == wchar_type_node)
1150 int precision = MAX (TYPE_PRECISION (type),
1151 TYPE_PRECISION (integer_type_node));
1152 tree totype = c_common_type_for_size (precision, 0);
1153 if (TREE_UNSIGNED (type)
1154 && ! int_fits_type_p (TYPE_MAX_VALUE (type), totype))
1155 type = c_common_type_for_size (precision, 1);
1156 else
1157 type = totype;
1159 else if (c_promoting_integer_type_p (type))
1161 /* Retain unsignedness if really not getting bigger. */
1162 if (TREE_UNSIGNED (type)
1163 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
1164 type = unsigned_type_node;
1165 else
1166 type = integer_type_node;
1168 else if (type == float_type_node)
1169 type = double_type_node;
1171 return cp_build_qualified_type (type, type_quals);
1174 /* The routines below this point are carefully written to conform to
1175 the standard. They use the same terminology, and follow the rules
1176 closely. Although they are used only in pt.c at the moment, they
1177 should presumably be used everywhere in the future. */
1179 /* Attempt to perform qualification conversions on EXPR to convert it
1180 to TYPE. Return the resulting expression, or error_mark_node if
1181 the conversion was impossible. */
1183 tree
1184 perform_qualification_conversions (tree type, tree expr)
1186 if (TREE_CODE (type) == POINTER_TYPE
1187 && TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE
1188 && comp_ptr_ttypes (TREE_TYPE (type), TREE_TYPE (TREE_TYPE (expr))))
1189 return build1 (NOP_EXPR, type, expr);
1190 else
1191 return error_mark_node;