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