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