]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/cp/cvt.c
* doc/install.texi (Installing GCC): Refer to buildstat.html,
[thirdparty/gcc.git] / gcc / cp / cvt.c
CommitLineData
471086d6 1/* Language-level data type conversion for GNU C++.
107c7f39 2 Copyright (C) 1987, 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
e63bd8ae 3 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
471086d6 4 Hacked by Michael Tiemann (tiemann@cygnus.com)
5
6This file is part of GNU CC.
7
8GNU CC is free software; you can redistribute it and/or modify
9it under the terms of the GNU General Public License as published by
10the Free Software Foundation; either version 2, or (at your option)
11any later version.
12
13GNU CC is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16GNU General Public License for more details.
17
18You should have received a copy of the GNU General Public License
19along with GNU CC; see the file COPYING. If not, write to
c58d4270 20the Free Software Foundation, 59 Temple Place - Suite 330,
21Boston, MA 02111-1307, USA. */
471086d6 22
23
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. */
28
29#include "config.h"
b3ef7553 30#include "system.h"
471086d6 31#include "tree.h"
32#include "flags.h"
33#include "cp-tree.h"
471086d6 34#include "convert.h"
f0eaeecd 35#include "toplev.h"
2e8226f7 36#include "decl.h"
96624a9e 37
045ed8f8 38static tree cp_convert_to_pointer PARAMS ((tree, tree, int));
24054144 39static tree convert_to_pointer_force PARAMS ((tree, tree));
ca106ab1 40static tree build_up_reference PARAMS ((tree, tree, int, tree));
519ba62c 41static void warn_ref_binding PARAMS ((tree, tree, tree));
71ccdfff 42
471086d6 43/* Change of width--truncation and extension of integers or reals--
44 is represented with NOP_EXPR. Proper functioning of many things
45 assumes that no other conversions can be NOP_EXPRs.
46
47 Conversion between integer and pointer is represented with CONVERT_EXPR.
48 Converting integer to real uses FLOAT_EXPR
49 and real to integer uses FIX_TRUNC_EXPR.
50
51 Here is a list of all the functions that assume that widening and
52 narrowing is always done with a NOP_EXPR:
53 In convert.c, convert_to_integer.
54 In c-typeck.c, build_binary_op_nodefault (boolean ops),
aff9e656 55 and c_common_truthvalue_conversion.
471086d6 56 In expr.c: expand_expr, for operands of a MULT_EXPR.
57 In fold-const.c: fold.
58 In tree.c: get_narrower and get_unwidened.
59
60 C++: in multiple-inheritance, converting between pointers may involve
61 adjusting them by a delta stored within the class definition. */
62\f
63/* Subroutines of `convert'. */
64
471086d6 65/* if converting pointer to pointer
66 if dealing with classes, check for derived->base or vice versa
67 else if dealing with method pointers, delegate
68 else convert blindly
69 else if converting class, pass off to build_type_conversion
045ed8f8 70 else try C-style pointer conversion. If FORCE is true then allow
71 conversions via virtual bases (these are permitted by reinterpret_cast,
72 but not static_cast). */
96624a9e 73
471086d6 74static tree
045ed8f8 75cp_convert_to_pointer (type, expr, force)
471086d6 76 tree type, expr;
045ed8f8 77 int force;
471086d6 78{
79 register tree intype = TREE_TYPE (expr);
74002e1d 80 register enum tree_code form;
f82eeb87 81 tree rval;
74002e1d 82
96624a9e 83 if (IS_AGGR_TYPE (intype))
84 {
96624a9e 85 intype = complete_type (intype);
4b72716d 86 if (!COMPLETE_TYPE_P (intype))
96624a9e 87 {
cf103c6c 88 error ("can't convert from incomplete type `%T' to `%T'",
96624a9e 89 intype, type);
90 return error_mark_node;
91 }
92
1c16607c 93 rval = build_type_conversion (type, expr, 1);
96624a9e 94 if (rval)
95 {
96 if (rval == error_mark_node)
cf103c6c 97 error ("conversion of `%E' from `%T' to `%T' is ambiguous",
96624a9e 98 expr, intype, type);
99 return rval;
100 }
101 }
102
860740a7 103 /* Handle anachronistic conversions from (::*)() to cv void* or (*)(). */
38281c46 104 if (TREE_CODE (type) == POINTER_TYPE
105 && (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
e3cfe3ce 106 || VOID_TYPE_P (TREE_TYPE (type))))
38281c46 107 {
108 /* Allow an implicit this pointer for pointer to member
109 functions. */
860740a7 110 if (TYPE_PTRMEMFUNC_P (intype))
38281c46 111 {
860740a7 112 tree fntype = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (intype));
2917b86f 113 tree decl = maybe_dummy_object (TYPE_METHOD_BASETYPE (fntype), 0);
38281c46 114 expr = build (OFFSET_REF, fntype, decl, expr);
115 }
116
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)
123 {
124 if (TREE_CODE (TREE_TYPE (TREE_TYPE (expr))) == METHOD_TYPE)
125 if (pedantic || warn_pmf2ptr)
cf103c6c 126 pedwarn ("converting from `%T' to `%T'", TREE_TYPE (expr),
38281c46 127 type);
128 return build1 (NOP_EXPR, type, expr);
129 }
130 intype = TREE_TYPE (expr);
131 }
132
311e42bc 133 if (expr == error_mark_node)
134 return error_mark_node;
135
74002e1d 136 form = TREE_CODE (intype);
137
2b77484d 138 if (POINTER_TYPE_P (intype))
471086d6 139 {
140 intype = TYPE_MAIN_VARIANT (intype);
141
142 if (TYPE_MAIN_VARIANT (type) != intype
2b77484d 143 && TREE_CODE (type) == POINTER_TYPE
471086d6 144 && TREE_CODE (TREE_TYPE (type)) == RECORD_TYPE
bccffb3a 145 && IS_AGGR_TYPE (TREE_TYPE (type))
146 && IS_AGGR_TYPE (TREE_TYPE (intype))
4a2680fc 147 && TREE_CODE (TREE_TYPE (intype)) == RECORD_TYPE)
471086d6 148 {
149 enum tree_code code = PLUS_EXPR;
4a2680fc 150 tree binfo;
151
152 /* Try derived to base conversion. */
153 binfo = lookup_base (TREE_TYPE (intype), TREE_TYPE (type),
154 ba_check, NULL);
155 if (!binfo)
471086d6 156 {
4a2680fc 157 /* Try base to derived conversion. */
158 binfo = lookup_base (TREE_TYPE (type), TREE_TYPE (intype),
159 ba_check, NULL);
471086d6 160 code = MINUS_EXPR;
161 }
4a2680fc 162 if (binfo == error_mark_node)
163 return error_mark_node;
471086d6 164 if (binfo)
165 {
4a2680fc 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)))
471086d6 170 {
4a2680fc 171 expr = build1 (NOP_EXPR, type, expr);
172 TREE_CONSTANT (expr) =
173 TREE_CONSTANT (TREE_OPERAND (expr, 0));
471086d6 174 }
4a2680fc 175 return expr;
471086d6 176 }
177 }
471086d6 178
1c16607c 179 if (TYPE_PTRMEM_P (type) && TYPE_PTRMEM_P (intype))
bea7d742 180 {
a17aefa2 181 tree b1;
182 tree b2;
183 tree binfo;
4a2680fc 184 enum tree_code code = PLUS_EXPR;
185 base_kind bk;
a17aefa2 186
187 b1 = TYPE_OFFSET_BASETYPE (TREE_TYPE (type));
188 b2 = TYPE_OFFSET_BASETYPE (TREE_TYPE (intype));
4a2680fc 189 binfo = lookup_base (b1, b2, ba_check, &bk);
190 if (!binfo)
fc9fc8d8 191 {
4a2680fc 192 binfo = lookup_base (b2, b1, ba_check, &bk);
fc9fc8d8 193 code = MINUS_EXPR;
194 }
bea7d742 195 if (binfo == error_mark_node)
196 return error_mark_node;
a17aefa2 197
4a2680fc 198 if (bk == bk_via_virtual)
7045d67a 199 {
045ed8f8 200 if (force)
cf103c6c 201 warning ("pointer to member cast from `%T' to `%T' is via virtual base",
4a2680fc 202 TREE_TYPE (intype), TREE_TYPE (type));
045ed8f8 203 else
204 {
cf103c6c 205 error ("pointer to member cast from `%T' to `%T' is via virtual base",
4a2680fc 206 TREE_TYPE (intype), TREE_TYPE (type));
045ed8f8 207 return error_mark_node;
208 }
1bbb5947 209 /* This is a reinterpret cast, whose result is unspecified.
210 We choose to do nothing. */
211 return build1 (NOP_EXPR, type, expr);
7045d67a 212 }
213
a17aefa2 214 if (TREE_CODE (expr) == PTRMEM_CST)
215 expr = cplus_expand_constant (expr);
216
1bbb5947 217 if (binfo)
218 expr = size_binop (code, convert (sizetype, expr),
902de8ed 219 BINFO_OFFSET (binfo));
bea7d742 220 }
2b77484d 221 else if (TYPE_PTRMEMFUNC_P (type))
ce28ee2e 222 {
cf103c6c 223 error ("cannot convert `%E' from type `%T' to type `%T'",
ce28ee2e 224 expr, intype, type);
225 return error_mark_node;
226 }
227
f82eeb87 228 rval = build1 (NOP_EXPR, type, expr);
229 TREE_CONSTANT (rval) = TREE_CONSTANT (expr);
230 return rval;
471086d6 231 }
2b77484d 232 else if (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype))
7045d67a 233 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), expr, 0);
2b77484d 234 else if (TYPE_PTRMEMFUNC_P (intype))
235 {
cf103c6c 236 error ("cannot convert `%E' from type `%T' to type `%T'",
2b77484d 237 expr, intype, type);
238 return error_mark_node;
239 }
471086d6 240
241 my_friendly_assert (form != OFFSET_TYPE, 186);
242
471086d6 243 if (integer_zerop (expr))
244 {
2b77484d 245 if (TYPE_PTRMEMFUNC_P (type))
f05d5d03 246 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), expr, 0);
ad63a0fc 247
606b494c 248 if (TYPE_PTRMEM_P (type))
5ad590ad 249 /* A NULL pointer-to-member is represented by -1, not by
250 zero. */
ad63a0fc 251 expr = build_int_2 (-1, -1);
252 else
253 expr = build_int_2 (0, 0);
471086d6 254 TREE_TYPE (expr) = type;
8f5d16f2 255 /* Fix up the representation of -1 if appropriate. */
256 force_fit_type (expr, 0);
471086d6 257 return expr;
258 }
259
bb0726a1 260 if (INTEGRAL_CODE_P (form))
471086d6 261 {
c5aa1e92 262 if (TYPE_PRECISION (intype) == POINTER_SIZE)
471086d6 263 return build1 (CONVERT_EXPR, type, expr);
771d21fa 264 expr = cp_convert (c_common_type_for_size (POINTER_SIZE, 0), expr);
471086d6 265 /* Modes may be different but sizes should be the same. */
266 if (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (expr)))
267 != GET_MODE_SIZE (TYPE_MODE (type)))
268 /* There is supposed to be some integral type
269 that is the same width as a pointer. */
270 abort ();
271 return convert_to_pointer (type, expr);
272 }
273
cc4d0855 274 if (type_unknown_p (expr))
4b471722 275 return instantiate_type (type, expr, tf_error | tf_warning);
cc4d0855 276
cf103c6c 277 error ("cannot convert `%E' from type `%T' to type `%T'",
471086d6 278 expr, intype, type);
279 return error_mark_node;
280}
281
282/* Like convert, except permit conversions to take place which
283 are not normally allowed due to access restrictions
284 (such as conversion from sub-type to private super-type). */
96624a9e 285
471086d6 286static tree
287convert_to_pointer_force (type, expr)
288 tree type, expr;
289{
290 register tree intype = TREE_TYPE (expr);
291 register enum tree_code form = TREE_CODE (intype);
292
471086d6 293 if (form == POINTER_TYPE)
294 {
295 intype = TYPE_MAIN_VARIANT (intype);
296
297 if (TYPE_MAIN_VARIANT (type) != intype
298 && TREE_CODE (TREE_TYPE (type)) == RECORD_TYPE
bccffb3a 299 && IS_AGGR_TYPE (TREE_TYPE (type))
300 && IS_AGGR_TYPE (TREE_TYPE (intype))
471086d6 301 && TREE_CODE (TREE_TYPE (intype)) == RECORD_TYPE)
302 {
303 enum tree_code code = PLUS_EXPR;
4a2680fc 304 tree binfo;
305
306 binfo = lookup_base (TREE_TYPE (intype), TREE_TYPE (type),
307 ba_ignore, NULL);
308 if (!binfo)
471086d6 309 {
4a2680fc 310 binfo = lookup_base (TREE_TYPE (type), TREE_TYPE (intype),
311 ba_ignore, NULL);
312 code = MINUS_EXPR;
471086d6 313 }
4a2680fc 314 if (binfo == error_mark_node)
315 return error_mark_node;
316 if (binfo)
471086d6 317 {
4a2680fc 318 expr = build_base_path (code, expr, binfo, 0);
319 /* Add any qualifier conversions. */
320 if (!same_type_p (TREE_TYPE (TREE_TYPE (expr)),
321 TREE_TYPE (type)))
322 {
323 expr = build1 (NOP_EXPR, type, expr);
324 TREE_CONSTANT (expr) =
325 TREE_CONSTANT (TREE_OPERAND (expr, 0));
326 }
327 return expr;
471086d6 328 }
4a2680fc 329
471086d6 330 }
471086d6 331 }
332
045ed8f8 333 return cp_convert_to_pointer (type, expr, 1);
471086d6 334}
335
336/* We are passing something to a function which requires a reference.
337 The type we are interested in is in TYPE. The initial
338 value we have to begin with is in ARG.
339
340 FLAGS controls how we manage access checking.
ca106ab1 341 DIRECT_BIND in FLAGS controls how any temporaries are generated.
342 If DIRECT_BIND is set, DECL is the reference we're binding to. */
96624a9e 343
471086d6 344static tree
ca106ab1 345build_up_reference (type, arg, flags, decl)
346 tree type, arg, decl;
6a44e72e 347 int flags;
471086d6 348{
c76251c1 349 tree rval;
0543e7a9 350 tree argtype = TREE_TYPE (arg);
471086d6 351 tree target_type = TREE_TYPE (type);
c23ebfdd 352 tree stmt_expr = NULL_TREE;
471086d6 353
354 my_friendly_assert (TREE_CODE (type) == REFERENCE_TYPE, 187);
471086d6 355
c76251c1 356 if ((flags & DIRECT_BIND) && ! real_lvalue_p (arg))
471086d6 357 {
ca106ab1 358 /* Create a new temporary variable. We can't just use a TARGET_EXPR
359 here because it needs to live as long as DECL. */
c76251c1 360 tree targ = arg;
ca106ab1 361
362 arg = build_decl (VAR_DECL, NULL_TREE, argtype);
363 DECL_ARTIFICIAL (arg) = 1;
364 TREE_USED (arg) = 1;
365 TREE_STATIC (arg) = TREE_STATIC (decl);
366
367 if (TREE_STATIC (decl))
368 {
369 /* Namespace-scope or local static; give it a mangled name. */
370 tree name = mangle_ref_init_variable (decl);
371 DECL_NAME (arg) = name;
372 SET_DECL_ASSEMBLER_NAME (arg, name);
373 arg = pushdecl_top_level (arg);
374 }
471086d6 375 else
376 {
243adba6 377 /* Automatic; make sure we handle the cleanup properly. */
614cc70d 378 maybe_push_cleanup_level (argtype);
ca106ab1 379 arg = pushdecl (arg);
1ad432f2 380 }
c23ebfdd 381
382 /* Process the initializer for the declaration. */
38281c46 383 DECL_INITIAL (arg) = targ;
ce23987e 384 cp_finish_decl (arg, targ, NULL_TREE,
011310f7 385 LOOKUP_ONLYCONVERTING|DIRECT_BIND);
1ad432f2 386 }
c76251c1 387 else if (!(flags & DIRECT_BIND) && ! lvalue_p (arg))
1d8e4310 388 return get_target_expr (arg);
1ad432f2 389
ca106ab1 390 /* If we had a way to wrap this up, and say, if we ever needed its
b0df6589 391 address, transform all occurrences of the register, into a memory
392 reference we could win better. */
c76251c1 393 rval = build_unary_op (ADDR_EXPR, arg, 1);
15e55420 394 if (rval == error_mark_node)
395 return error_mark_node;
396
6686e84d 397 if ((flags & LOOKUP_PROTECT)
398 && TYPE_MAIN_VARIANT (argtype) != TYPE_MAIN_VARIANT (target_type)
399 && IS_AGGR_TYPE (argtype)
400 && IS_AGGR_TYPE (target_type))
401 {
7e6960e0 402 /* We go through lookup_base for the access control. */
4a2680fc 403 tree binfo = lookup_base (argtype, target_type, ba_check, NULL);
6686e84d 404 if (binfo == error_mark_node)
405 return error_mark_node;
406 if (binfo == NULL_TREE)
407 return error_not_base_type (target_type, argtype);
4a2680fc 408 rval = build_base_path (PLUS_EXPR, rval, binfo, 1);
6686e84d 409 }
c76251c1 410 else
411 rval
412 = convert_to_pointer_force (build_pointer_type (target_type), rval);
6686e84d 413 rval = build1 (NOP_EXPR, type, rval);
c76251c1 414 TREE_CONSTANT (rval) = TREE_CONSTANT (TREE_OPERAND (rval, 0));
c23ebfdd 415
416 /* If we created and initialized a new temporary variable, add the
417 representation of that initialization to the RVAL. */
418 if (stmt_expr)
419 rval = build (COMPOUND_EXPR, TREE_TYPE (rval), stmt_expr, rval);
420
421 /* And return the result. */
471086d6 422 return rval;
423}
424
519ba62c 425/* Subroutine of convert_to_reference. REFTYPE is the target reference type.
426 INTYPE is the original rvalue type and DECL is an optional _DECL node
427 for diagnostics.
428
429 [dcl.init.ref] says that if an rvalue is used to
430 initialize a reference, then the reference must be to a
431 non-volatile const type. */
432
433static void
434warn_ref_binding (reftype, intype, decl)
435 tree reftype, intype, decl;
436{
437 tree ttl = TREE_TYPE (reftype);
438
439 if (!CP_TYPE_CONST_NON_VOLATILE_P (ttl))
440 {
441 const char *msg;
442
443 if (CP_TYPE_VOLATILE_P (ttl) && decl)
444 msg = "initialization of volatile reference type `%#T' from rvalue of type `%T'";
445 else if (CP_TYPE_VOLATILE_P (ttl))
446 msg = "conversion to volatile reference type `%#T' from rvalue of type `%T'";
447 else if (decl)
448 msg = "initialization of non-const reference type `%#T' from rvalue of type `%T'";
449 else
450 msg = "conversion to non-const reference type `%#T' from rvalue of type `%T'";
451
cf103c6c 452 pedwarn (msg, reftype, intype);
519ba62c 453 }
454}
455
471086d6 456/* For C++: Only need to do one-level references, but cannot
457 get tripped up on signed/unsigned differences.
458
d81e00a4 459 DECL is either NULL_TREE or the _DECL node for a reference that is being
460 initialized. It can be error_mark_node if we don't know the _DECL but
461 we know it's an initialization. */
471086d6 462
471086d6 463tree
d81e00a4 464convert_to_reference (reftype, expr, convtype, flags, decl)
471086d6 465 tree reftype, expr;
d81e00a4 466 int convtype, flags;
467 tree decl;
471086d6 468{
469 register tree type = TYPE_MAIN_VARIANT (TREE_TYPE (reftype));
470 register tree intype = TREE_TYPE (expr);
471086d6 471 tree rval = NULL_TREE;
1a3f833b 472 tree rval_as_conversion = NULL_TREE;
473 int i;
474
cc4d0855 475 if (TREE_CODE (type) == FUNCTION_TYPE && intype == unknown_type_node)
476 {
3effa7a7 477 expr = instantiate_type (type, expr,
ffe5bd74 478 (flags & LOOKUP_COMPLAIN)
4b471722 479 ? tf_error | tf_warning : tf_none);
3effa7a7 480 if (expr == error_mark_node)
481 return error_mark_node;
482
cc4d0855 483 intype = TREE_TYPE (expr);
484 }
485
8bc57e28 486 my_friendly_assert (TREE_CODE (intype) != REFERENCE_TYPE, 364);
471086d6 487
471086d6 488 intype = TYPE_MAIN_VARIANT (intype);
489
1a3f833b 490 i = comp_target_types (type, intype, 0);
491
492 if (i <= 0 && (convtype & CONV_IMPLICIT) && IS_AGGR_TYPE (intype)
493 && ! (flags & LOOKUP_NO_CONVERSION))
494 {
495 /* Look for a user-defined conversion to lvalue that we can use. */
496
0a4be248 497 rval_as_conversion
1c16607c 498 = build_type_conversion (reftype, expr, 1);
1a3f833b 499
500 if (rval_as_conversion && rval_as_conversion != error_mark_node
501 && real_lvalue_p (rval_as_conversion))
502 {
503 expr = rval_as_conversion;
504 rval_as_conversion = NULL_TREE;
505 intype = type;
506 i = 1;
507 }
508 }
509
510 if (((convtype & CONV_STATIC) && i == -1)
511 || ((convtype & CONV_IMPLICIT) && i == 1))
471086d6 512 {
471086d6 513 if (flags & LOOKUP_COMPLAIN)
514 {
bc3887cf 515 tree ttl = TREE_TYPE (reftype);
cb0ba4ec 516 tree ttr = lvalue_type (expr);
471086d6 517
519ba62c 518 if (! real_lvalue_p (expr))
519 warn_ref_binding (reftype, intype, decl);
520
521 if (! (convtype & CONV_CONST)
3e04bd45 522 && !at_least_as_qualified_p (ttl, ttr))
cf103c6c 523 pedwarn ("conversion from `%T' to `%T' discards qualifiers",
caa99b15 524 ttr, reftype);
0543e7a9 525 }
526
ca106ab1 527 return build_up_reference (reftype, expr, flags, decl);
471086d6 528 }
c07b1ad1 529 else if ((convtype & CONV_REINTERPRET) && lvalue_p (expr))
0543e7a9 530 {
531 /* When casting an lvalue to a reference type, just convert into
532 a pointer to the new type and deference it. This is allowed
e581f478 533 by San Diego WP section 5.2.9 paragraph 12, though perhaps it
0543e7a9 534 should be done directly (jason). (int &)ri ---> *(int*)&ri */
e581f478 535
3748625f 536 /* B* bp; A& ar = (A&)bp; is valid, but it's probably not what they
e581f478 537 meant. */
1a3f833b 538 if (TREE_CODE (intype) == POINTER_TYPE
daf9ff67 539 && (comptypes (TREE_TYPE (intype), type,
540 COMPARE_BASE | COMPARE_RELAXED )))
cf103c6c 541 warning ("casting `%T' to `%T' does not dereference pointer",
e581f478 542 intype, reftype);
543
0543e7a9 544 rval = build_unary_op (ADDR_EXPR, expr, 0);
545 if (rval != error_mark_node)
985e9ee0 546 rval = convert_force (build_pointer_type (TREE_TYPE (reftype)),
547 rval, 0);
0543e7a9 548 if (rval != error_mark_node)
b0722fac 549 rval = build1 (NOP_EXPR, reftype, rval);
0543e7a9 550 }
0a4be248 551 else
860740a7 552 {
553 rval = convert_for_initialization (NULL_TREE, type, expr, flags,
554 "converting", 0, 0);
a66fe4cb 555 if (rval == NULL_TREE || rval == error_mark_node)
556 return rval;
519ba62c 557 warn_ref_binding (reftype, intype, decl);
ca106ab1 558 rval = build_up_reference (reftype, rval, flags, decl);
860740a7 559 }
471086d6 560
561 if (rval)
562 {
96624a9e 563 /* If we found a way to convert earlier, then use it. */
471086d6 564 return rval;
565 }
566
1a3f833b 567 my_friendly_assert (TREE_CODE (intype) != OFFSET_TYPE, 189);
471086d6 568
b248d3f7 569 if (flags & LOOKUP_COMPLAIN)
cf103c6c 570 error ("cannot convert type `%T' to type `%T'", intype, reftype);
b248d3f7 571
471086d6 572 if (flags & LOOKUP_SPECULATIVELY)
573 return NULL_TREE;
574
575 return error_mark_node;
576}
577
578/* We are using a reference VAL for its value. Bash that reference all the
96624a9e 579 way down to its lowest form. */
580
471086d6 581tree
582convert_from_reference (val)
583 tree val;
584{
585 tree type = TREE_TYPE (val);
586
587 if (TREE_CODE (type) == OFFSET_TYPE)
588 type = TREE_TYPE (type);
8c5c575d 589 if (TREE_CODE (type) == REFERENCE_TYPE)
4d698345 590 return build_indirect_ref (val, NULL);
471086d6 591 return val;
592}
dbacd3bd 593
594/* Implicitly convert the lvalue EXPR to another lvalue of type TOTYPE,
595 preserving cv-qualification. */
596
597tree
598convert_lvalue (totype, expr)
599 tree totype, expr;
600{
601 totype = cp_build_qualified_type (totype, TYPE_QUALS (TREE_TYPE (expr)));
602 totype = build_reference_type (totype);
603 expr = convert_to_reference (totype, expr, CONV_IMPLICIT, LOOKUP_NORMAL,
604 NULL_TREE);
605 return convert_from_reference (expr);
606}
471086d6 607\f
c4a8ac95 608/* C++ conversions, preference to static cast conversions. */
609
610tree
611cp_convert (type, expr)
612 tree type, expr;
613{
614 return ocp_convert (type, expr, CONV_OLD_CONVERT, LOOKUP_NORMAL);
615}
616
b248d3f7 617/* Conversion...
618
619 FLAGS indicates how we should behave. */
620
471086d6 621tree
c4a8ac95 622ocp_convert (type, expr, convtype, flags)
471086d6 623 tree type, expr;
d81e00a4 624 int convtype, flags;
471086d6 625{
626 register tree e = expr;
627 register enum tree_code code = TREE_CODE (type);
628
b465397d 629 if (e == error_mark_node
630 || TREE_TYPE (e) == error_mark_node)
471086d6 631 return error_mark_node;
d81e00a4 632
5b592939 633 complete_type (type);
634 complete_type (TREE_TYPE (expr));
635
1202e392 636 e = decl_constant_value (e);
7745052b 637
2133b374 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))
1a3f833b 643 /* We need a new temporary; don't take this shortcut. */;
644 else if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (TREE_TYPE (e)))
ca23ec64 645 {
daf9ff67 646 if (same_type_p (type, TREE_TYPE (e)))
ca23ec64 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
f699c174 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;
372e0e29 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));
ca23ec64 658 else
f699c174 659 return fold (build1 (NOP_EXPR, type, e));
ca23ec64 660 }
661
d81e00a4 662 if (code == VOID_TYPE && (convtype & CONV_STATIC))
95b2ac55 663 {
b1a8d4ce 664 e = convert_to_void (e, /*implicit=*/NULL);
aeef2be5 665 return e;
95b2ac55 666 }
d81e00a4 667
471086d6 668 /* Just convert to the type of the member. */
669 if (code == OFFSET_TYPE)
670 {
671 type = TREE_TYPE (type);
672 code = TREE_CODE (type);
673 }
674
f3ba5c6a 675 if (TREE_CODE (e) == OFFSET_REF)
676 e = resolve_offset_ref (e);
677
bb0726a1 678 if (INTEGRAL_CODE_P (code))
471086d6 679 {
617abf06 680 tree intype = TREE_TYPE (e);
02d7f858 681 /* enum = enum, enum = int, enum = float, (enum)pointer are all
682 errors. */
723d9ad3 683 if (TREE_CODE (type) == ENUMERAL_TYPE
02d7f858 684 && ((ARITHMETIC_TYPE_P (intype) && ! (convtype & CONV_STATIC))
685 || (TREE_CODE (intype) == POINTER_TYPE)))
471086d6 686 {
cf103c6c 687 pedwarn ("conversion from `%#T' to `%#T'", intype, type);
471086d6 688
689 if (flag_pedantic_errors)
690 return error_mark_node;
691 }
f3ba5c6a 692 if (IS_AGGR_TYPE (intype))
471086d6 693 {
694 tree rval;
1c16607c 695 rval = build_type_conversion (type, e, 1);
6495357a 696 if (rval)
697 return rval;
b248d3f7 698 if (flags & LOOKUP_COMPLAIN)
cf103c6c 699 error ("`%#T' used where a `%T' was expected", intype, type);
b248d3f7 700 if (flags & LOOKUP_SPECULATIVELY)
701 return NULL_TREE;
471086d6 702 return error_mark_node;
703 }
bb0726a1 704 if (code == BOOLEAN_TYPE)
e4ce2dc4 705 {
5b592939 706 tree fn = NULL_TREE;
707
e4ce2dc4 708 /* Common Ada/Pascal programmer's mistake. We always warn
709 about this since it is so bad. */
710 if (TREE_CODE (expr) == FUNCTION_DECL)
5b592939 711 fn = expr;
712 else if (TREE_CODE (expr) == ADDR_EXPR
713 && TREE_CODE (TREE_OPERAND (expr, 0)) == FUNCTION_DECL)
714 fn = TREE_OPERAND (expr, 0);
1d2ca10c 715 if (fn && !DECL_WEAK (fn))
cf103c6c 716 warning ("the address of `%D', will always be `true'", fn);
2e0e6579 717 return cp_truthvalue_conversion (e);
e4ce2dc4 718 }
471086d6 719 return fold (convert_to_integer (type, e));
720 }
74002e1d 721 if (code == POINTER_TYPE || code == REFERENCE_TYPE
722 || TYPE_PTRMEMFUNC_P (type))
045ed8f8 723 return fold (cp_convert_to_pointer (type, e, 0));
684b9d79 724 if (code == VECTOR_TYPE)
725 return fold (convert_to_vector (type, e));
c4a8ac95 726 if (code == REAL_TYPE || code == COMPLEX_TYPE)
471086d6 727 {
728 if (IS_AGGR_TYPE (TREE_TYPE (e)))
729 {
730 tree rval;
1c16607c 731 rval = build_type_conversion (type, e, 1);
471086d6 732 if (rval)
733 return rval;
734 else
b248d3f7 735 if (flags & LOOKUP_COMPLAIN)
cf103c6c 736 error ("`%#T' used where a floating point value was expected",
b248d3f7 737 TREE_TYPE (e));
471086d6 738 }
c4a8ac95 739 if (code == REAL_TYPE)
740 return fold (convert_to_real (type, e));
741 else if (code == COMPLEX_TYPE)
742 return fold (convert_to_complex (type, e));
471086d6 743 }
744
745 /* New C++ semantics: since assignment is now based on
746 memberwise copying, if the rhs type is derived from the
747 lhs type, then we may still do a conversion. */
748 if (IS_AGGR_TYPE_CODE (code))
749 {
750 tree dtype = TREE_TYPE (e);
c25194fd 751 tree ctor = NULL_TREE;
471086d6 752
471086d6 753 dtype = TYPE_MAIN_VARIANT (dtype);
754
471086d6 755 /* Conversion between aggregate types. New C++ semantics allow
756 objects of derived type to be cast to objects of base type.
757 Old semantics only allowed this between pointers.
758
759 There may be some ambiguity between using a constructor
760 vs. using a type conversion operator when both apply. */
761
0a4be248 762 ctor = e;
860740a7 763
8c18e707 764 if (abstract_virtuals_error (NULL_TREE, type))
765 return error_mark_node;
6cbb4197 766
0a4be248 767 if ((flags & LOOKUP_ONLYCONVERTING)
768 && ! (IS_AGGR_TYPE (dtype) && DERIVED_FROM_P (type, dtype)))
fce73460 769 /* For copy-initialization, first we create a temp of the proper type
770 with a user-defined conversion sequence, then we direct-initialize
771 the target with the temp (see [dcl.init]). */
772 ctor = build_user_type_conversion (type, ctor, flags);
3eb89cd8 773 else
f70cb9e6 774 ctor = build_special_member_call (NULL_TREE,
775 complete_ctor_identifier,
776 build_tree_list (NULL_TREE, ctor),
777 TYPE_BINFO (type), flags);
0a4be248 778 if (ctor)
779 return build_cplus_new (type, ctor);
471086d6 780 }
781
b248d3f7 782 if (flags & LOOKUP_COMPLAIN)
cf103c6c 783 error ("conversion from `%T' to non-scalar type `%T' requested",
b248d3f7 784 TREE_TYPE (expr), type);
785 if (flags & LOOKUP_SPECULATIVELY)
786 return NULL_TREE;
471086d6 787 return error_mark_node;
788}
789
b1a8d4ce 790/* When an expression is used in a void context, its value is discarded and
791 no lvalue-rvalue and similar conversions happen [expr.static.cast/4,
792 stmt.expr/1, expr.comma/1]. This permits dereferencing an incomplete type
793 in a void context. The C++ standard does not define what an `access' to an
794 object is, but there is reason to beleive that it is the lvalue to rvalue
795 conversion -- if it were not, `*&*p = 1' would violate [expr]/4 in that it
796 accesses `*p' not to calculate the value to be stored. But, dcl.type.cv/8
797 indicates that volatile semantics should be the same between C and C++
798 where ever possible. C leaves it implementation defined as to what
799 constitutes an access to a volatile. So, we interpret `*vp' as a read of
800 the volatile object `vp' points to, unless that is an incomplete type. For
801 volatile references we do not do this interpretation, because that would
802 make it impossible to ignore the reference return value from functions. We
803 issue warnings in the confusing cases.
804
805 IMPLICIT is tells us the context of an implicit void conversion. */
806
807tree
808convert_to_void (expr, implicit)
809 tree expr;
810 const char *implicit;
811{
3d411d73 812 if (expr == error_mark_node
813 || TREE_TYPE (expr) == error_mark_node)
814 return error_mark_node;
b1a8d4ce 815 if (!TREE_TYPE (expr))
816 return expr;
e3cfe3ce 817 if (VOID_TYPE_P (TREE_TYPE (expr)))
b1a8d4ce 818 return expr;
819 switch (TREE_CODE (expr))
820 {
821 case COND_EXPR:
822 {
823 /* The two parts of a cond expr might be separate lvalues. */
824 tree op1 = TREE_OPERAND (expr,1);
825 tree op2 = TREE_OPERAND (expr,2);
826 tree new_op1 = convert_to_void (op1, implicit);
827 tree new_op2 = convert_to_void (op2, implicit);
828
1b9a507b 829 expr = build (COND_EXPR, TREE_TYPE (new_op1),
69773da0 830 TREE_OPERAND (expr, 0), new_op1, new_op2);
b1a8d4ce 831 break;
832 }
833
834 case COMPOUND_EXPR:
835 {
836 /* The second part of a compound expr contains the value. */
837 tree op1 = TREE_OPERAND (expr,1);
838 tree new_op1 = convert_to_void (op1, implicit);
839
840 if (new_op1 != op1)
e907a2e9 841 {
842 tree t = build (COMPOUND_EXPR, TREE_TYPE (new_op1),
843 TREE_OPERAND (expr, 0), new_op1);
844 TREE_SIDE_EFFECTS (t) = TREE_SIDE_EFFECTS (expr);
8061c68f 845 TREE_NO_UNUSED_WARNING (t) = TREE_NO_UNUSED_WARNING (expr);
e907a2e9 846 expr = t;
847 }
848
b1a8d4ce 849 break;
850 }
851
852 case NON_LVALUE_EXPR:
853 case NOP_EXPR:
854 /* These have already decayed to rvalue. */
855 break;
856
857 case CALL_EXPR: /* we have a special meaning for volatile void fn() */
858 break;
859
860 case INDIRECT_REF:
861 {
862 tree type = TREE_TYPE (expr);
863 int is_reference = TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0)))
864 == REFERENCE_TYPE;
865 int is_volatile = TYPE_VOLATILE (type);
4b72716d 866 int is_complete = COMPLETE_TYPE_P (complete_type (type));
b1a8d4ce 867
868 if (is_volatile && !is_complete)
cf103c6c 869 warning ("object of incomplete type `%T' will not be accessed in %s",
b1a8d4ce 870 type, implicit ? implicit : "void context");
871 else if (is_reference && is_volatile)
cf103c6c 872 warning ("object of type `%T' will not be accessed in %s",
b1a8d4ce 873 TREE_TYPE (TREE_OPERAND (expr, 0)),
874 implicit ? implicit : "void context");
875 if (is_reference || !is_volatile || !is_complete)
876 expr = TREE_OPERAND (expr, 0);
877
878 break;
879 }
880
881 case VAR_DECL:
882 {
883 /* External variables might be incomplete. */
884 tree type = TREE_TYPE (expr);
4b72716d 885 int is_complete = COMPLETE_TYPE_P (complete_type (type));
b1a8d4ce 886
887 if (TYPE_VOLATILE (type) && !is_complete)
cf103c6c 888 warning ("object `%E' of incomplete type `%T' will not be accessed in %s",
b1a8d4ce 889 expr, type, implicit ? implicit : "void context");
890 break;
891 }
d9f88785 892
893 case OFFSET_REF:
894 expr = resolve_offset_ref (expr);
895 break;
896
b1a8d4ce 897 default:;
898 }
899 {
900 tree probe = expr;
901
902 if (TREE_CODE (probe) == ADDR_EXPR)
903 probe = TREE_OPERAND (expr, 0);
2e0e6579 904 if (type_unknown_p (probe))
905 {
906 /* [over.over] enumerates the places where we can take the address
907 of an overloaded function, and this is not one of them. */
cf103c6c 908 pedwarn ("%s cannot resolve address of overloaded function",
2e0e6579 909 implicit ? implicit : "void cast");
910 }
911 else if (implicit && probe == expr && is_overloaded_fn (probe))
b1a8d4ce 912 /* Only warn when there is no &. */
cf103c6c 913 warning ("%s is a reference, not call, to function `%E'",
2e0e6579 914 implicit, expr);
b1a8d4ce 915 }
916
e3cfe3ce 917 if (expr != error_mark_node && !VOID_TYPE_P (TREE_TYPE (expr)))
b1a8d4ce 918 {
919 /* FIXME: This is where we should check for expressions with no
920 effects. At the moment we do that in both build_x_component_expr
921 and expand_expr_stmt -- inconsistently too. For the moment
922 leave implicit void conversions unadorned so that expand_expr_stmt
923 has a chance of detecting some of the cases. */
924 if (!implicit)
925 expr = build1 (CONVERT_EXPR, void_type_node, expr);
926 }
927 return expr;
928}
929
d81e00a4 930/* Create an expression whose value is that of EXPR,
931 converted to type TYPE. The TREE_TYPE of the value
932 is always TYPE. This function implements all reasonable
933 conversions; callers should filter out those that are
c4a8ac95 934 not permitted by the language being compiled.
935
936 Most of this routine is from build_reinterpret_cast.
937
938 The backend cannot call cp_convert (what was convert) because
939 conversions to/from basetypes may involve memory references
940 (vbases) and adding or subtracting small values (multiple
941 inheritance), but it calls convert from the constant folding code
e0aa5007 942 on subtrees of already built trees after it has ripped them apart.
c4a8ac95 943
944 Also, if we ever support range variables, we'll probably also have to
945 do a little bit more work. */
d81e00a4 946
947tree
948convert (type, expr)
949 tree type, expr;
950{
c4a8ac95 951 tree intype;
952
953 if (type == error_mark_node || expr == error_mark_node)
954 return error_mark_node;
955
c4a8ac95 956 intype = TREE_TYPE (expr);
957
6686e84d 958 if (POINTER_TYPE_P (type) && POINTER_TYPE_P (intype))
c4a8ac95 959 {
1202e392 960 expr = decl_constant_value (expr);
c4a8ac95 961 return fold (build1 (NOP_EXPR, type, expr));
962 }
963
964 return ocp_convert (type, expr, CONV_OLD_CONVERT,
965 LOOKUP_NORMAL|LOOKUP_NO_CONVERSION);
d81e00a4 966}
967
c4a8ac95 968/* Like cp_convert, except permit conversions to take place which
471086d6 969 are not normally allowed due to access restrictions
970 (such as conversion from sub-type to private super-type). */
96624a9e 971
471086d6 972tree
a74e8896 973convert_force (type, expr, convtype)
471086d6 974 tree type;
975 tree expr;
a74e8896 976 int convtype;
471086d6 977{
978 register tree e = expr;
979 register enum tree_code code = TREE_CODE (type);
980
981 if (code == REFERENCE_TYPE)
d81e00a4 982 return fold (convert_to_reference (type, e, CONV_C_CAST, LOOKUP_COMPLAIN,
983 NULL_TREE));
471086d6 984 else if (TREE_CODE (TREE_TYPE (e)) == REFERENCE_TYPE)
985 e = convert_from_reference (e);
986
987 if (code == POINTER_TYPE)
988 return fold (convert_to_pointer_force (type, e));
989
ac9386a0 990 /* From typeck.c convert_for_assignment */
471086d6 991 if (((TREE_CODE (TREE_TYPE (e)) == POINTER_TYPE && TREE_CODE (e) == ADDR_EXPR
992 && TREE_CODE (TREE_TYPE (e)) == POINTER_TYPE
993 && TREE_CODE (TREE_TYPE (TREE_TYPE (e))) == METHOD_TYPE)
ac9386a0 994 || integer_zerop (e)
995 || TYPE_PTRMEMFUNC_P (TREE_TYPE (e)))
471086d6 996 && TYPE_PTRMEMFUNC_P (type))
997 {
96624a9e 998 /* compatible pointer to member functions. */
ac9386a0 999 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), e, 1);
471086d6 1000 }
a74e8896 1001
c4a8ac95 1002 return ocp_convert (type, e, CONV_C_CAST|convtype, LOOKUP_NORMAL);
471086d6 1003}
1004
471086d6 1005/* Convert an aggregate EXPR to type XTYPE. If a conversion
1006 exists, return the attempted conversion. This may
1007 return ERROR_MARK_NODE if the conversion is not
1008 allowed (references private members, etc).
1009 If no conversion exists, NULL_TREE is returned.
1010
1011 If (FOR_SURE & 1) is non-zero, then we allow this type conversion
1012 to take place immediately. Otherwise, we build a SAVE_EXPR
ce28ee2e 1013 which can be evaluated if the results are ever needed.
1014
1015 Changes to this functions should be mirrored in user_harshness.
1016
1017 FIXME: Ambiguity checking is wrong. Should choose one by the implicit
1018 object parameter, or by the second standard conversion sequence if
1019 that doesn't do it. This will probably wait for an overloading rewrite.
1020 (jason 8/9/95) */
471086d6 1021
471086d6 1022tree
1c16607c 1023build_type_conversion (xtype, expr, for_sure)
471086d6 1024 tree xtype, expr;
1025 int for_sure;
1026{
1027 /* C++: check to see if we can convert this aggregate type
bcf789d7 1028 into the required type. */
0a4be248 1029 return build_user_type_conversion
1030 (xtype, expr, for_sure ? LOOKUP_NORMAL : 0);
471086d6 1031}
1032
1e66592c 1033/* Convert the given EXPR to one of a group of types suitable for use in an
1034 expression. DESIRES is a combination of various WANT_* flags (q.v.)
1035 which indicates which types are suitable. If COMPLAIN is 1, complain
1036 about ambiguity; otherwise, the caller will deal with it. */
471086d6 1037
1e66592c 1038tree
1039build_expr_type_conversion (desires, expr, complain)
1040 int desires;
1041 tree expr;
1042 int complain;
471086d6 1043{
1e66592c 1044 tree basetype = TREE_TYPE (expr);
6a44e72e 1045 tree conv = NULL_TREE;
bdd152ce 1046 tree winner = NULL_TREE;
471086d6 1047
954885ed 1048 if (expr == null_node
1049 && (desires & WANT_INT)
1050 && !(desires & WANT_NULL))
cf103c6c 1051 warning ("converting NULL to non-pointer type");
954885ed 1052
42b9ec6a 1053 if (TREE_CODE (expr) == OFFSET_REF)
ce28ee2e 1054 expr = resolve_offset_ref (expr);
1055 expr = convert_from_reference (expr);
1056 basetype = TREE_TYPE (expr);
471086d6 1057
3d411d73 1058 if (basetype == error_mark_node)
1059 return error_mark_node;
1060
bdd152ce 1061 if (! IS_AGGR_TYPE (basetype))
1062 switch (TREE_CODE (basetype))
1063 {
1064 case INTEGER_TYPE:
954885ed 1065 if ((desires & WANT_NULL) && null_ptr_cst_p (expr))
bdd152ce 1066 return expr;
1067 /* else fall through... */
1068
1069 case BOOLEAN_TYPE:
1070 return (desires & WANT_INT) ? expr : NULL_TREE;
1071 case ENUMERAL_TYPE:
1072 return (desires & WANT_ENUM) ? expr : NULL_TREE;
1073 case REAL_TYPE:
1074 return (desires & WANT_FLOAT) ? expr : NULL_TREE;
1075 case POINTER_TYPE:
1076 return (desires & WANT_POINTER) ? expr : NULL_TREE;
985e9ee0 1077
bdd152ce 1078 case FUNCTION_TYPE:
1079 case ARRAY_TYPE:
1080 return (desires & WANT_POINTER) ? default_conversion (expr)
1081 : NULL_TREE;
1082 default:
1083 return NULL_TREE;
1084 }
1085
1086 /* The code for conversions from class type is currently only used for
1087 delete expressions. Other expressions are handled by build_new_op. */
1088
1089 if (! TYPE_HAS_CONVERSION (basetype))
1090 return NULL_TREE;
1091
1092 for (conv = lookup_conversions (basetype); conv; conv = TREE_CHAIN (conv))
1093 {
1094 int win = 0;
1095 tree candidate;
1096 tree cand = TREE_VALUE (conv);
1097
1098 if (winner && winner == cand)
1099 continue;
1100
1101 candidate = TREE_TYPE (TREE_TYPE (cand));
1102 if (TREE_CODE (candidate) == REFERENCE_TYPE)
1103 candidate = TREE_TYPE (candidate);
1104
1105 switch (TREE_CODE (candidate))
1106 {
1107 case BOOLEAN_TYPE:
1108 case INTEGER_TYPE:
1109 win = (desires & WANT_INT); break;
1110 case ENUMERAL_TYPE:
1111 win = (desires & WANT_ENUM); break;
1112 case REAL_TYPE:
1113 win = (desires & WANT_FLOAT); break;
1114 case POINTER_TYPE:
1115 win = (desires & WANT_POINTER); break;
1116
1117 default:
1118 break;
1119 }
1120
1121 if (win)
1122 {
1123 if (winner)
1124 {
1125 if (complain)
1126 {
cf103c6c 1127 error ("ambiguous default type conversion from `%T'",
bdd152ce 1128 basetype);
cf103c6c 1129 error (" candidate conversions include `%D' and `%D'",
bdd152ce 1130 winner, cand);
1131 }
1132 return error_mark_node;
1133 }
1134 else
1135 winner = cand;
1136 }
1137 }
1e66592c 1138
bdd152ce 1139 if (winner)
1140 {
1141 tree type = TREE_TYPE (TREE_TYPE (winner));
1142 if (TREE_CODE (type) == REFERENCE_TYPE)
1143 type = TREE_TYPE (type);
1144 return build_user_type_conversion (type, expr, LOOKUP_NORMAL);
471086d6 1145 }
1e66592c 1146
985e9ee0 1147 return NULL_TREE;
471086d6 1148}
bc3887cf 1149
96624a9e 1150/* Implements integral promotion (4.1) and float->double promotion. */
1151
bc3887cf 1152tree
1153type_promotes_to (type)
1154 tree type;
1155{
3e04bd45 1156 int type_quals;
ce28ee2e 1157
1158 if (type == error_mark_node)
1159 return error_mark_node;
1160
3119c950 1161 type_quals = cp_type_quals (type);
bc3887cf 1162 type = TYPE_MAIN_VARIANT (type);
bb0726a1 1163
1164 /* bool always promotes to int (not unsigned), even if it's the same
1165 size. */
028ea8b4 1166 if (type == boolean_type_node)
bb0726a1 1167 type = integer_type_node;
1168
1169 /* Normally convert enums to int, but convert wide enums to something
1170 wider. */
1171 else if (TREE_CODE (type) == ENUMERAL_TYPE
1172 || type == wchar_type_node)
6c9497b1 1173 {
1174 int precision = MAX (TYPE_PRECISION (type),
1175 TYPE_PRECISION (integer_type_node));
771d21fa 1176 tree totype = c_common_type_for_size (precision, 0);
6c9497b1 1177 if (TREE_UNSIGNED (type)
1178 && ! int_fits_type_p (TYPE_MAX_VALUE (type), totype))
771d21fa 1179 type = c_common_type_for_size (precision, 1);
6c9497b1 1180 else
1181 type = totype;
1182 }
d7aeef06 1183 else if (c_promoting_integer_type_p (type))
bc3887cf 1184 {
d0acef9e 1185 /* Retain unsignedness if really not getting bigger. */
bc3887cf 1186 if (TREE_UNSIGNED (type)
d0acef9e 1187 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
bc3887cf 1188 type = unsigned_type_node;
1189 else
1190 type = integer_type_node;
1191 }
1192 else if (type == float_type_node)
1193 type = double_type_node;
1194
3e04bd45 1195 return cp_build_qualified_type (type, type_quals);
bc3887cf 1196}
668ae905 1197
668ae905 1198/* The routines below this point are carefully written to conform to
1199 the standard. They use the same terminology, and follow the rules
1200 closely. Although they are used only in pt.c at the moment, they
1201 should presumably be used everywhere in the future. */
1202
1146f179 1203/* Attempt to perform qualification conversions on EXPR to convert it
1204 to TYPE. Return the resulting expression, or error_mark_node if
1205 the conversion was impossible. */
1206
668ae905 1207tree
1208perform_qualification_conversions (type, expr)
1209 tree type;
1210 tree expr;
1211{
213d2076 1212 if (TREE_CODE (type) == POINTER_TYPE
1213 && TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE
1214 && comp_ptr_ttypes (TREE_TYPE (type), TREE_TYPE (TREE_TYPE (expr))))
1146f179 1215 return build1 (NOP_EXPR, type, expr);
1216 else
1217 return error_mark_node;
668ae905 1218}