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