]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/cp/cvt.c
gcc:
[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,
a33e06b7 3 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
471086d6 4 Hacked by Michael Tiemann (tiemann@cygnus.com)
5
6f0d25a6 6This file is part of GCC.
471086d6 7
6f0d25a6 8GCC is free software; you can redistribute it and/or modify
471086d6 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
6f0d25a6 13GCC is distributed in the hope that it will be useful,
471086d6 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
6f0d25a6 19along with GCC; see the file COPYING. If not, write to
beaeca68 20the Free Software Foundation, 51 Franklin Street, Fifth Floor,
21Boston, MA 02110-1301, USA. */
471086d6 22
23
af86cc06 24/* This file contains the functions for converting C++ expressions
471086d6 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"
7a979707 39#include "target.h"
96624a9e 40
35771a9a 41static tree cp_convert_to_pointer (tree, tree, bool);
42static tree convert_to_pointer_force (tree, tree);
ef9571e5 43static tree build_type_conversion (tree, tree);
35771a9a 44static tree build_up_reference (tree, tree, int, tree);
45static void warn_ref_binding (tree, tree, tree);
71ccdfff 46
471086d6 47/* Change of width--truncation and extension of integers or reals--
48 is represented with NOP_EXPR. Proper functioning of many things
49 assumes that no other conversions can be NOP_EXPRs.
50
51 Conversion between integer and pointer is represented with CONVERT_EXPR.
52 Converting integer to real uses FLOAT_EXPR
53 and real to integer uses FIX_TRUNC_EXPR.
54
55 Here is a list of all the functions that assume that widening and
56 narrowing is always done with a NOP_EXPR:
57 In convert.c, convert_to_integer.
58 In c-typeck.c, build_binary_op_nodefault (boolean ops),
653e5405 59 and c_common_truthvalue_conversion.
471086d6 60 In expr.c: expand_expr, for operands of a MULT_EXPR.
61 In fold-const.c: fold.
62 In tree.c: get_narrower and get_unwidened.
63
64 C++: in multiple-inheritance, converting between pointers may involve
65 adjusting them by a delta stored within the class definition. */
66\f
67/* Subroutines of `convert'. */
68
471086d6 69/* if converting pointer to pointer
70 if dealing with classes, check for derived->base or vice versa
71 else if dealing with method pointers, delegate
72 else convert blindly
73 else if converting class, pass off to build_type_conversion
045ed8f8 74 else try C-style pointer conversion. If FORCE is true then allow
75 conversions via virtual bases (these are permitted by reinterpret_cast,
76 but not static_cast). */
96624a9e 77
471086d6 78static tree
35771a9a 79cp_convert_to_pointer (tree type, tree expr, bool force)
471086d6 80{
cd16867a 81 tree intype = TREE_TYPE (expr);
82 enum tree_code form;
f82eeb87 83 tree rval;
8bf328bf 84 if (intype == error_mark_node)
85 return error_mark_node;
74002e1d 86
96624a9e 87 if (IS_AGGR_TYPE (intype))
88 {
96624a9e 89 intype = complete_type (intype);
4b72716d 90 if (!COMPLETE_TYPE_P (intype))
96624a9e 91 {
0a25aad6 92 error ("can't convert from incomplete type %qT to %qT",
653e5405 93 intype, type);
96624a9e 94 return error_mark_node;
95 }
96
8999978b 97 rval = build_type_conversion (type, expr);
96624a9e 98 if (rval)
99 {
100 if (rval == error_mark_node)
0a25aad6 101 error ("conversion of %qE from %qT to %qT is ambiguous",
653e5405 102 expr, intype, type);
96624a9e 103 return rval;
104 }
105 }
106
860740a7 107 /* Handle anachronistic conversions from (::*)() to cv void* or (*)(). */
38281c46 108 if (TREE_CODE (type) == POINTER_TYPE
109 && (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
e3cfe3ce 110 || VOID_TYPE_P (TREE_TYPE (type))))
38281c46 111 {
6ab399e8 112 if (TYPE_PTRMEMFUNC_P (intype)
113 || TREE_CODE (intype) == METHOD_TYPE)
114 return convert_member_func_to_ptr (type, expr);
a63bc44c 115 if (TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE)
116 return build_nop (type, expr);
38281c46 117 intype = TREE_TYPE (expr);
118 }
119
311e42bc 120 if (expr == error_mark_node)
121 return error_mark_node;
122
74002e1d 123 form = TREE_CODE (intype);
124
2b77484d 125 if (POINTER_TYPE_P (intype))
471086d6 126 {
127 intype = TYPE_MAIN_VARIANT (intype);
128
129 if (TYPE_MAIN_VARIANT (type) != intype
2b77484d 130 && TREE_CODE (type) == POINTER_TYPE
471086d6 131 && TREE_CODE (TREE_TYPE (type)) == RECORD_TYPE
bccffb3a 132 && IS_AGGR_TYPE (TREE_TYPE (type))
133 && IS_AGGR_TYPE (TREE_TYPE (intype))
4a2680fc 134 && TREE_CODE (TREE_TYPE (intype)) == RECORD_TYPE)
471086d6 135 {
136 enum tree_code code = PLUS_EXPR;
4a2680fc 137 tree binfo;
8999978b 138 tree intype_class;
139 tree type_class;
140 bool same_p;
4a2680fc 141
8999978b 142 intype_class = TREE_TYPE (intype);
143 type_class = TREE_TYPE (type);
144
9031d10b 145 same_p = same_type_p (TYPE_MAIN_VARIANT (intype_class),
8999978b 146 TYPE_MAIN_VARIANT (type_class));
147 binfo = NULL_TREE;
c0af329c 148 /* Try derived to base conversion. */
8999978b 149 if (!same_p)
150 binfo = lookup_base (intype_class, type_class, ba_check, NULL);
151 if (!same_p && !binfo)
471086d6 152 {
c0af329c 153 /* Try base to derived conversion. */
8999978b 154 binfo = lookup_base (type_class, intype_class, ba_check, NULL);
471086d6 155 code = MINUS_EXPR;
156 }
4a2680fc 157 if (binfo == error_mark_node)
158 return error_mark_node;
8999978b 159 if (binfo || same_p)
471086d6 160 {
8999978b 161 if (binfo)
162 expr = build_base_path (code, expr, binfo, 0);
c0af329c 163 /* Add any qualifier conversions. */
8999978b 164 return build_nop (type, expr);
471086d6 165 }
166 }
471086d6 167
1bc16cab 168 if (TYPE_PTRMEMFUNC_P (type))
bea7d742 169 {
0a25aad6 170 error ("cannot convert %qE from type %qT to type %qT",
653e5405 171 expr, intype, type);
1bc16cab 172 return error_mark_node;
173 }
a17aefa2 174
1bc16cab 175 return build_nop (type, expr);
176 }
177 else if (TYPE_PTRMEM_P (type) && TYPE_PTRMEM_P (intype))
178 {
9031d10b 179 tree b1;
1bc16cab 180 tree b2;
181 tree binfo;
182 enum tree_code code = PLUS_EXPR;
183 base_kind bk;
184
185 b1 = TYPE_PTRMEM_CLASS_TYPE (type);
186 b2 = TYPE_PTRMEM_CLASS_TYPE (intype);
187 binfo = lookup_base (b1, b2, ba_check, &bk);
188 if (!binfo)
189 {
190 binfo = lookup_base (b2, b1, ba_check, &bk);
191 code = MINUS_EXPR;
192 }
193 if (binfo == error_mark_node)
194 return error_mark_node;
a17aefa2 195
1bc16cab 196 if (bk == bk_via_virtual)
197 {
198 if (force)
c3ceba8e 199 warning (0, "pointer to member cast from %qT to %qT is via"
653e5405 200 " virtual base", intype, type);
1bc16cab 201 else
7045d67a 202 {
0a25aad6 203 error ("pointer to member cast from %qT to %qT is"
653e5405 204 " via virtual base", intype, type);
1bc16cab 205 return error_mark_node;
7045d67a 206 }
1bc16cab 207 /* This is a reinterpret cast, whose result is unspecified.
208 We choose to do nothing. */
209 return build1 (NOP_EXPR, type, expr);
ce28ee2e 210 }
211
1bc16cab 212 if (TREE_CODE (expr) == PTRMEM_CST)
213 expr = cplus_expand_constant (expr);
214
215 if (binfo && !integer_zerop (BINFO_OFFSET (binfo)))
9031d10b 216 expr = size_binop (code,
1bc16cab 217 build_nop (sizetype, expr),
218 BINFO_OFFSET (binfo));
8999978b 219 return build_nop (type, expr);
471086d6 220 }
2b77484d 221 else if (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype))
cb02169c 222 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), expr, 0,
223 /*c_cast_p=*/false);
2b77484d 224 else if (TYPE_PTRMEMFUNC_P (intype))
225 {
a63bc44c 226 if (!warn_pmf2ptr)
227 {
228 if (TREE_CODE (expr) == PTRMEM_CST)
229 return cp_convert_to_pointer (type,
230 PTRMEM_CST_MEMBER (expr),
231 force);
232 else if (TREE_CODE (expr) == OFFSET_REF)
233 {
234 tree object = TREE_OPERAND (expr, 0);
235 return get_member_function_from_ptrfunc (&object,
236 TREE_OPERAND (expr, 1));
237 }
238 }
0a25aad6 239 error ("cannot convert %qE from type %qT to type %qT",
653e5405 240 expr, intype, type);
2b77484d 241 return error_mark_node;
242 }
471086d6 243
471086d6 244 if (integer_zerop (expr))
245 {
2b77484d 246 if (TYPE_PTRMEMFUNC_P (type))
cb02169c 247 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), expr, 0,
248 /*c_cast_p=*/false);
ad63a0fc 249
606b494c 250 if (TYPE_PTRMEM_P (type))
7c446c95 251 {
252 /* A NULL pointer-to-member is represented by -1, not by
253 zero. */
7016c612 254 expr = build_int_cst (type, -1);
7c446c95 255 /* Fix up the representation of -1 if appropriate. */
256 expr = force_fit_type (expr, 0, false, false);
257 }
ad63a0fc 258 else
7016c612 259 expr = build_int_cst (type, 0);
9031d10b 260
471086d6 261 return expr;
262 }
1bc16cab 263 else if (TYPE_PTR_TO_MEMBER_P (type) && INTEGRAL_CODE_P (form))
de1f960a 264 {
0a25aad6 265 error ("invalid conversion from %qT to %qT", intype, type);
de1f960a 266 return error_mark_node;
267 }
471086d6 268
bb0726a1 269 if (INTEGRAL_CODE_P (form))
471086d6 270 {
c5aa1e92 271 if (TYPE_PRECISION (intype) == POINTER_SIZE)
471086d6 272 return build1 (CONVERT_EXPR, type, expr);
771d21fa 273 expr = cp_convert (c_common_type_for_size (POINTER_SIZE, 0), expr);
092b1d6f 274 /* Modes may be different but sizes should be the same. There
275 is supposed to be some integral type that is the same width
276 as a pointer. */
277 gcc_assert (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (expr)))
278 == GET_MODE_SIZE (TYPE_MODE (type)));
9031d10b 279
471086d6 280 return convert_to_pointer (type, expr);
281 }
282
cc4d0855 283 if (type_unknown_p (expr))
0fbca5e8 284 return instantiate_type (type, expr, tf_warning_or_error);
cc4d0855 285
0a25aad6 286 error ("cannot convert %qE from type %qT to type %qT",
653e5405 287 expr, intype, type);
471086d6 288 return error_mark_node;
289}
290
291/* Like convert, except permit conversions to take place which
292 are not normally allowed due to access restrictions
293 (such as conversion from sub-type to private super-type). */
96624a9e 294
471086d6 295static tree
35771a9a 296convert_to_pointer_force (tree type, tree expr)
471086d6 297{
cd16867a 298 tree intype = TREE_TYPE (expr);
299 enum tree_code form = TREE_CODE (intype);
9031d10b 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),
ada40935 315 ba_unique, NULL);
4a2680fc 316 if (!binfo)
471086d6 317 {
4a2680fc 318 binfo = lookup_base (TREE_TYPE (type), TREE_TYPE (intype),
ada40935 319 ba_unique, NULL);
4a2680fc 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);
653e5405 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)))
8999978b 332 expr = build_nop (type, expr);
4a2680fc 333 return expr;
471086d6 334 }
471086d6 335 }
471086d6 336 }
337
35771a9a 338 return cp_convert_to_pointer (type, expr, true);
471086d6 339}
340
341/* We are passing something to a function which requires a reference.
342 The type we are interested in is in TYPE. The initial
343 value we have to begin with is in ARG.
344
345 FLAGS controls how we manage access checking.
ca106ab1 346 DIRECT_BIND in FLAGS controls how any temporaries are generated.
347 If DIRECT_BIND is set, DECL is the reference we're binding to. */
96624a9e 348
471086d6 349static tree
35771a9a 350build_up_reference (tree type, tree arg, int flags, tree decl)
471086d6 351{
c76251c1 352 tree rval;
0543e7a9 353 tree argtype = TREE_TYPE (arg);
471086d6 354 tree target_type = TREE_TYPE (type);
471086d6 355
b4df430b 356 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
471086d6 357
c76251c1 358 if ((flags & DIRECT_BIND) && ! real_lvalue_p (arg))
471086d6 359 {
ca106ab1 360 /* Create a new temporary variable. We can't just use a TARGET_EXPR
361 here because it needs to live as long as DECL. */
c76251c1 362 tree targ = arg;
ca106ab1 363
7c09476d 364 arg = make_temporary_var_for_ref_to_temp (decl, TREE_TYPE (arg));
c23ebfdd 365
366 /* Process the initializer for the declaration. */
38281c46 367 DECL_INITIAL (arg) = targ;
d91303a6 368 cp_finish_decl (arg, targ, /*init_const_expr_p=*/false, 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
ca106ab1 374 /* If we had a way to wrap this up, and say, if we ever needed its
b0df6589 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 {
7e6960e0 386 /* We go through lookup_base for the access control. */
4a2680fc 387 tree binfo = lookup_base (argtype, target_type, ba_check, NULL);
6686e84d 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);
4a2680fc 392 rval = build_base_path (PLUS_EXPR, rval, binfo, 1);
6686e84d 393 }
c76251c1 394 else
395 rval
396 = convert_to_pointer_force (build_pointer_type (target_type), rval);
8999978b 397 return build_nop (type, rval);
471086d6 398}
399
519ba62c 400/* Subroutine of convert_to_reference. REFTYPE is the target reference type.
401 INTYPE is the original rvalue type and DECL is an optional _DECL node
402 for diagnostics.
9031d10b 403
519ba62c 404 [dcl.init.ref] says that if an rvalue is used to
405 initialize a reference, then the reference must be to a
406 non-volatile const type. */
407
408static void
35771a9a 409warn_ref_binding (tree reftype, tree intype, tree decl)
519ba62c 410{
411 tree ttl = TREE_TYPE (reftype);
9031d10b 412
519ba62c 413 if (!CP_TYPE_CONST_NON_VOLATILE_P (ttl))
414 {
415 const char *msg;
416
417 if (CP_TYPE_VOLATILE_P (ttl) && decl)
0a25aad6 418 msg = "initialization of volatile reference type %q#T from"
653e5405 419 " rvalue of type %qT";
519ba62c 420 else if (CP_TYPE_VOLATILE_P (ttl))
0a25aad6 421 msg = "conversion to volatile reference type %q#T "
653e5405 422 " from rvalue of type %qT";
519ba62c 423 else if (decl)
0a25aad6 424 msg = "initialization of non-const reference type %q#T from"
653e5405 425 " rvalue of type %qT";
519ba62c 426 else
0a25aad6 427 msg = "conversion to non-const reference type %q#T from"
653e5405 428 " rvalue of type %qT";
519ba62c 429
cf103c6c 430 pedwarn (msg, reftype, intype);
519ba62c 431 }
432}
433
471086d6 434/* For C++: Only need to do one-level references, but cannot
435 get tripped up on signed/unsigned differences.
436
d81e00a4 437 DECL is either NULL_TREE or the _DECL node for a reference that is being
438 initialized. It can be error_mark_node if we don't know the _DECL but
439 we know it's an initialization. */
471086d6 440
471086d6 441tree
35771a9a 442convert_to_reference (tree reftype, tree expr, int convtype,
653e5405 443 int flags, tree decl)
471086d6 444{
cd16867a 445 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (reftype));
446 tree intype;
471086d6 447 tree rval = NULL_TREE;
1a3f833b 448 tree rval_as_conversion = NULL_TREE;
1bc16cab 449 bool can_convert_intype_to_type;
1a3f833b 450
9031d10b 451 if (TREE_CODE (type) == FUNCTION_TYPE
d32df6a6 452 && TREE_TYPE (expr) == unknown_type_node)
9031d10b 453 expr = instantiate_type (type, expr,
0a3b29ad 454 (flags & LOOKUP_COMPLAIN)
0fbca5e8 455 ? tf_warning_or_error : tf_none);
0a3b29ad 456
457 if (expr == error_mark_node)
458 return error_mark_node;
459
460 intype = TREE_TYPE (expr);
cc4d0855 461
b4df430b 462 gcc_assert (TREE_CODE (intype) != REFERENCE_TYPE);
ab12ed55 463 gcc_assert (TREE_CODE (reftype) == REFERENCE_TYPE);
471086d6 464
471086d6 465 intype = TYPE_MAIN_VARIANT (intype);
466
1bc16cab 467 can_convert_intype_to_type = can_convert (type, intype);
468 if (!can_convert_intype_to_type
469 && (convtype & CONV_IMPLICIT) && IS_AGGR_TYPE (intype)
1a3f833b 470 && ! (flags & LOOKUP_NO_CONVERSION))
471 {
472 /* Look for a user-defined conversion to lvalue that we can use. */
473
0a4be248 474 rval_as_conversion
8999978b 475 = build_type_conversion (reftype, expr);
1a3f833b 476
477 if (rval_as_conversion && rval_as_conversion != error_mark_node
478 && real_lvalue_p (rval_as_conversion))
479 {
480 expr = rval_as_conversion;
481 rval_as_conversion = NULL_TREE;
482 intype = type;
1bc16cab 483 can_convert_intype_to_type = 1;
1a3f833b 484 }
485 }
486
1bc16cab 487 if (((convtype & CONV_STATIC) && can_convert (intype, type))
488 || ((convtype & CONV_IMPLICIT) && can_convert_intype_to_type))
471086d6 489 {
471086d6 490 if (flags & LOOKUP_COMPLAIN)
491 {
bc3887cf 492 tree ttl = TREE_TYPE (reftype);
cb0ba4ec 493 tree ttr = lvalue_type (expr);
471086d6 494
519ba62c 495 if (! real_lvalue_p (expr))
496 warn_ref_binding (reftype, intype, decl);
9031d10b 497
519ba62c 498 if (! (convtype & CONV_CONST)
3e04bd45 499 && !at_least_as_qualified_p (ttl, ttr))
0a25aad6 500 pedwarn ("conversion from %qT to %qT discards qualifiers",
653e5405 501 ttr, reftype);
0543e7a9 502 }
503
ca106ab1 504 return build_up_reference (reftype, expr, flags, decl);
471086d6 505 }
c07b1ad1 506 else if ((convtype & CONV_REINTERPRET) && lvalue_p (expr))
0543e7a9 507 {
508 /* When casting an lvalue to a reference type, just convert into
509 a pointer to the new type and deference it. This is allowed
e581f478 510 by San Diego WP section 5.2.9 paragraph 12, though perhaps it
0543e7a9 511 should be done directly (jason). (int &)ri ---> *(int*)&ri */
e581f478 512
3748625f 513 /* B* bp; A& ar = (A&)bp; is valid, but it's probably not what they
653e5405 514 meant. */
1a3f833b 515 if (TREE_CODE (intype) == POINTER_TYPE
00952d10 516 && (comptypes (TREE_TYPE (intype), type,
517 COMPARE_BASE | COMPARE_DERIVED)))
c3ceba8e 518 warning (0, "casting %qT to %qT does not dereference pointer",
00952d10 519 intype, reftype);
9031d10b 520
0543e7a9 521 rval = build_unary_op (ADDR_EXPR, expr, 0);
522 if (rval != error_mark_node)
985e9ee0 523 rval = convert_force (build_pointer_type (TREE_TYPE (reftype)),
524 rval, 0);
0543e7a9 525 if (rval != error_mark_node)
b0722fac 526 rval = build1 (NOP_EXPR, reftype, rval);
0543e7a9 527 }
0a4be248 528 else
860740a7 529 {
530 rval = convert_for_initialization (NULL_TREE, type, expr, flags,
531 "converting", 0, 0);
a66fe4cb 532 if (rval == NULL_TREE || rval == error_mark_node)
533 return rval;
519ba62c 534 warn_ref_binding (reftype, intype, decl);
ca106ab1 535 rval = build_up_reference (reftype, rval, flags, decl);
860740a7 536 }
471086d6 537
538 if (rval)
539 {
96624a9e 540 /* If we found a way to convert earlier, then use it. */
471086d6 541 return rval;
542 }
543
b248d3f7 544 if (flags & LOOKUP_COMPLAIN)
0a25aad6 545 error ("cannot convert type %qT to type %qT", intype, reftype);
b248d3f7 546
471086d6 547 return error_mark_node;
548}
549
550/* We are using a reference VAL for its value. Bash that reference all the
96624a9e 551 way down to its lowest form. */
552
471086d6 553tree
35771a9a 554convert_from_reference (tree val)
471086d6 555{
120c0017 556 if (TREE_CODE (TREE_TYPE (val)) == REFERENCE_TYPE)
729f89ff 557 {
558 tree t = canonical_type_variant (TREE_TYPE (TREE_TYPE (val)));
559 tree ref = build1 (INDIRECT_REF, t, val);
9031d10b 560
729f89ff 561 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
562 so that we get the proper error message if the result is used
563 to assign to. Also, &* is supposed to be a no-op. */
564 TREE_READONLY (ref) = CP_TYPE_CONST_P (t);
565 TREE_THIS_VOLATILE (ref) = CP_TYPE_VOLATILE_P (t);
566 TREE_SIDE_EFFECTS (ref)
567 = (TREE_THIS_VOLATILE (ref) || TREE_SIDE_EFFECTS (val));
568 REFERENCE_REF_P (ref) = 1;
569 val = ref;
570 }
9031d10b 571
471086d6 572 return val;
573}
e5dab226 574
575/* Really perform an lvalue-to-rvalue conversion, including copying an
576 argument of class type into a temporary. */
577
578tree
579force_rvalue (tree expr)
580{
581 if (IS_AGGR_TYPE (TREE_TYPE (expr)) && TREE_CODE (expr) != TARGET_EXPR)
582 expr = ocp_convert (TREE_TYPE (expr), expr,
583 CONV_IMPLICIT|CONV_FORCE_TEMP, LOOKUP_NORMAL);
584 else
585 expr = decay_conversion (expr);
586
587 return expr;
588}
471086d6 589\f
c4a8ac95 590/* C++ conversions, preference to static cast conversions. */
591
592tree
35771a9a 593cp_convert (tree type, tree expr)
c4a8ac95 594{
595 return ocp_convert (type, expr, CONV_OLD_CONVERT, LOOKUP_NORMAL);
596}
597
b248d3f7 598/* Conversion...
599
600 FLAGS indicates how we should behave. */
601
471086d6 602tree
35771a9a 603ocp_convert (tree type, tree expr, int convtype, int flags)
471086d6 604{
cd16867a 605 tree e = expr;
606 enum tree_code code = TREE_CODE (type);
7a979707 607 const char *invalid_conv_diag;
471086d6 608
1bc16cab 609 if (error_operand_p (e) || type == error_mark_node)
471086d6 610 return error_mark_node;
d81e00a4 611
5b592939 612 complete_type (type);
613 complete_type (TREE_TYPE (expr));
614
7a979707 615 if ((invalid_conv_diag
616 = targetm.invalid_conversion (TREE_TYPE (expr), type)))
617 {
618 error (invalid_conv_diag);
619 return error_mark_node;
620 }
621
13f0eb20 622 e = integral_constant_value (e);
7745052b 623
2133b374 624 if (IS_AGGR_TYPE (type) && (convtype & CONV_FORCE_TEMP)
625 /* Some internal structures (vtable_entry_type, sigtbl_ptr_type)
626 don't go through finish_struct, so they don't have the synthesized
627 constructors. So don't force a temporary. */
628 && TYPE_HAS_CONSTRUCTOR (type))
1a3f833b 629 /* We need a new temporary; don't take this shortcut. */;
630 else if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (TREE_TYPE (e)))
ca23ec64 631 {
daf9ff67 632 if (same_type_p (type, TREE_TYPE (e)))
ca23ec64 633 /* The call to fold will not always remove the NOP_EXPR as
634 might be expected, since if one of the types is a typedef;
755edffd 635 the comparison in fold is just equality of pointers, not a
f699c174 636 call to comptypes. We don't call fold in this case because
637 that can result in infinite recursion; fold will call
638 convert, which will call ocp_convert, etc. */
639 return e;
372e0e29 640 /* For complex data types, we need to perform componentwise
653e5405 641 conversion. */
372e0e29 642 else if (TREE_CODE (type) == COMPLEX_TYPE)
653e5405 643 return fold_if_not_in_template (convert_to_complex (type, e));
bdb2219e 644 else if (TREE_CODE (e) == TARGET_EXPR)
645 {
646 /* Don't build a NOP_EXPR of class type. Instead, change the
647 type of the temporary. Only allow this for cv-qual changes,
648 though. */
092b1d6f 649 gcc_assert (same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (e)),
650 TYPE_MAIN_VARIANT (type)));
bdb2219e 651 TREE_TYPE (e) = TREE_TYPE (TARGET_EXPR_SLOT (e)) = type;
652 return e;
653 }
ca23ec64 654 else
092b1d6f 655 {
656 /* We shouldn't be treating objects of ADDRESSABLE type as
657 rvalues. */
658 gcc_assert (!TREE_ADDRESSABLE (type));
5d7ed6c7 659 return fold_if_not_in_template (build_nop (type, e));
092b1d6f 660 }
ca23ec64 661 }
662
d81e00a4 663 if (code == VOID_TYPE && (convtype & CONV_STATIC))
95b2ac55 664 {
b1a8d4ce 665 e = convert_to_void (e, /*implicit=*/NULL);
aeef2be5 666 return e;
95b2ac55 667 }
d81e00a4 668
bb0726a1 669 if (INTEGRAL_CODE_P (code))
471086d6 670 {
617abf06 671 tree intype = TREE_TYPE (e);
02d7f858 672 /* enum = enum, enum = int, enum = float, (enum)pointer are all
653e5405 673 errors. */
723d9ad3 674 if (TREE_CODE (type) == ENUMERAL_TYPE
b312a686 675 && (((INTEGRAL_OR_ENUMERATION_TYPE_P (intype)
653e5405 676 || TREE_CODE (intype) == REAL_TYPE)
b312a686 677 && ! (convtype & CONV_STATIC))
678 || TREE_CODE (intype) == POINTER_TYPE))
471086d6 679 {
b312a686 680 if (flags & LOOKUP_COMPLAIN)
681 pedwarn ("conversion from %q#T to %q#T", intype, type);
471086d6 682
683 if (flag_pedantic_errors)
684 return error_mark_node;
685 }
f3ba5c6a 686 if (IS_AGGR_TYPE (intype))
471086d6 687 {
688 tree rval;
8999978b 689 rval = build_type_conversion (type, e);
6495357a 690 if (rval)
691 return rval;
b248d3f7 692 if (flags & LOOKUP_COMPLAIN)
0a25aad6 693 error ("%q#T used where a %qT was expected", intype, type);
471086d6 694 return error_mark_node;
695 }
bb0726a1 696 if (code == BOOLEAN_TYPE)
ce871053 697 return cp_truthvalue_conversion (e);
698
5d7ed6c7 699 return fold_if_not_in_template (convert_to_integer (type, e));
471086d6 700 }
1bc16cab 701 if (POINTER_TYPE_P (type) || TYPE_PTR_TO_MEMBER_P (type))
5d7ed6c7 702 return fold_if_not_in_template (cp_convert_to_pointer (type, e, false));
684b9d79 703 if (code == VECTOR_TYPE)
c37ff371 704 {
705 tree in_vtype = TREE_TYPE (e);
706 if (IS_AGGR_TYPE (in_vtype))
707 {
708 tree ret_val;
709 ret_val = build_type_conversion (type, e);
653e5405 710 if (ret_val)
711 return ret_val;
712 if (flags & LOOKUP_COMPLAIN)
713 error ("%q#T used where a %qT was expected", in_vtype, type);
714 return error_mark_node;
c37ff371 715 }
5d7ed6c7 716 return fold_if_not_in_template (convert_to_vector (type, e));
c37ff371 717 }
c4a8ac95 718 if (code == REAL_TYPE || code == COMPLEX_TYPE)
471086d6 719 {
720 if (IS_AGGR_TYPE (TREE_TYPE (e)))
721 {
722 tree rval;
8999978b 723 rval = build_type_conversion (type, e);
471086d6 724 if (rval)
725 return rval;
726 else
b248d3f7 727 if (flags & LOOKUP_COMPLAIN)
0a25aad6 728 error ("%q#T used where a floating point value was expected",
b248d3f7 729 TREE_TYPE (e));
471086d6 730 }
c4a8ac95 731 if (code == REAL_TYPE)
5d7ed6c7 732 return fold_if_not_in_template (convert_to_real (type, e));
c4a8ac95 733 else if (code == COMPLEX_TYPE)
5d7ed6c7 734 return fold_if_not_in_template (convert_to_complex (type, e));
471086d6 735 }
736
737 /* New C++ semantics: since assignment is now based on
738 memberwise copying, if the rhs type is derived from the
739 lhs type, then we may still do a conversion. */
740 if (IS_AGGR_TYPE_CODE (code))
741 {
742 tree dtype = TREE_TYPE (e);
c25194fd 743 tree ctor = NULL_TREE;
471086d6 744
471086d6 745 dtype = TYPE_MAIN_VARIANT (dtype);
746
471086d6 747 /* Conversion between aggregate types. New C++ semantics allow
748 objects of derived type to be cast to objects of base type.
749 Old semantics only allowed this between pointers.
750
751 There may be some ambiguity between using a constructor
752 vs. using a type conversion operator when both apply. */
753
0a4be248 754 ctor = e;
860740a7 755
8c18e707 756 if (abstract_virtuals_error (NULL_TREE, type))
757 return error_mark_node;
6cbb4197 758
0a4be248 759 if ((flags & LOOKUP_ONLYCONVERTING)
760 && ! (IS_AGGR_TYPE (dtype) && DERIVED_FROM_P (type, dtype)))
fce73460 761 /* For copy-initialization, first we create a temp of the proper type
762 with a user-defined conversion sequence, then we direct-initialize
763 the target with the temp (see [dcl.init]). */
764 ctor = build_user_type_conversion (type, ctor, flags);
3eb89cd8 765 else
9031d10b 766 ctor = build_special_member_call (NULL_TREE,
f70cb9e6 767 complete_ctor_identifier,
768 build_tree_list (NULL_TREE, ctor),
a6460bf1 769 type, flags);
0a4be248 770 if (ctor)
771 return build_cplus_new (type, ctor);
471086d6 772 }
773
b248d3f7 774 if (flags & LOOKUP_COMPLAIN)
0a25aad6 775 error ("conversion from %qT to non-scalar type %qT requested",
653e5405 776 TREE_TYPE (expr), type);
471086d6 777 return error_mark_node;
778}
779
b1a8d4ce 780/* When an expression is used in a void context, its value is discarded and
781 no lvalue-rvalue and similar conversions happen [expr.static.cast/4,
782 stmt.expr/1, expr.comma/1]. This permits dereferencing an incomplete type
783 in a void context. The C++ standard does not define what an `access' to an
63eff20d 784 object is, but there is reason to believe that it is the lvalue to rvalue
b1a8d4ce 785 conversion -- if it were not, `*&*p = 1' would violate [expr]/4 in that it
786 accesses `*p' not to calculate the value to be stored. But, dcl.type.cv/8
787 indicates that volatile semantics should be the same between C and C++
788 where ever possible. C leaves it implementation defined as to what
789 constitutes an access to a volatile. So, we interpret `*vp' as a read of
790 the volatile object `vp' points to, unless that is an incomplete type. For
791 volatile references we do not do this interpretation, because that would
792 make it impossible to ignore the reference return value from functions. We
793 issue warnings in the confusing cases.
9031d10b 794
b1a8d4ce 795 IMPLICIT is tells us the context of an implicit void conversion. */
796
797tree
35771a9a 798convert_to_void (tree expr, const char *implicit)
b1a8d4ce 799{
9031d10b 800 if (expr == error_mark_node
3d411d73 801 || TREE_TYPE (expr) == error_mark_node)
802 return error_mark_node;
b1a8d4ce 803 if (!TREE_TYPE (expr))
804 return expr;
334f6ce1 805 if (invalid_nonstatic_memfn_p (expr))
806 return error_mark_node;
ed36f1cf 807 if (TREE_CODE (expr) == PSEUDO_DTOR_EXPR)
808 {
809 error ("pseudo-destructor is not called");
810 return error_mark_node;
811 }
e3cfe3ce 812 if (VOID_TYPE_P (TREE_TYPE (expr)))
b1a8d4ce 813 return expr;
814 switch (TREE_CODE (expr))
815 {
816 case COND_EXPR:
817 {
653e5405 818 /* The two parts of a cond expr might be separate lvalues. */
819 tree op1 = TREE_OPERAND (expr,1);
820 tree op2 = TREE_OPERAND (expr,2);
821 tree new_op1 = convert_to_void
00fa9079 822 (op1, (implicit && !TREE_SIDE_EFFECTS (op2)
823 ? "second operand of conditional" : NULL));
653e5405 824 tree new_op2 = convert_to_void
00fa9079 825 (op2, (implicit && !TREE_SIDE_EFFECTS (op1)
826 ? "third operand of conditional" : NULL));
9031d10b 827
831d52a2 828 expr = build3 (COND_EXPR, TREE_TYPE (new_op1),
829 TREE_OPERAND (expr, 0), new_op1, new_op2);
653e5405 830 break;
b1a8d4ce 831 }
9031d10b 832
b1a8d4ce 833 case COMPOUND_EXPR:
834 {
653e5405 835 /* The second part of a compound expr contains the value. */
836 tree op1 = TREE_OPERAND (expr,1);
837 tree new_op1 = convert_to_void
4ee9c684 838 (op1, (implicit && !TREE_NO_WARNING (expr)
23ec9bd9 839 ? "right-hand operand of comma" : NULL));
9031d10b 840
653e5405 841 if (new_op1 != op1)
e907a2e9 842 {
831d52a2 843 tree t = build2 (COMPOUND_EXPR, TREE_TYPE (new_op1),
844 TREE_OPERAND (expr, 0), new_op1);
e907a2e9 845 expr = t;
846 }
847
653e5405 848 break;
b1a8d4ce 849 }
9031d10b 850
b1a8d4ce 851 case NON_LVALUE_EXPR:
852 case NOP_EXPR:
c0af329c 853 /* These have already decayed to rvalue. */
b1a8d4ce 854 break;
9031d10b 855
331bc0ad 856 case CALL_EXPR: /* We have a special meaning for volatile void fn(). */
b1a8d4ce 857 break;
9031d10b 858
b1a8d4ce 859 case INDIRECT_REF:
860 {
653e5405 861 tree type = TREE_TYPE (expr);
862 int is_reference = TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0)))
863 == REFERENCE_TYPE;
864 int is_volatile = TYPE_VOLATILE (type);
865 int is_complete = COMPLETE_TYPE_P (complete_type (type));
866
dc02da7f 867 /* Can't load the value if we don't know the type. */
653e5405 868 if (is_volatile && !is_complete)
869 warning (0, "object of incomplete type %qT will not be accessed in %s",
870 type, implicit ? implicit : "void context");
dc02da7f 871 /* Don't load the value if this is an implicit dereference, or if
872 the type needs to be handled by ctors/dtors. */
873 else if (is_volatile && (is_reference || TREE_ADDRESSABLE (type)))
653e5405 874 warning (0, "object of type %qT will not be accessed in %s",
875 TREE_TYPE (TREE_OPERAND (expr, 0)),
876 implicit ? implicit : "void context");
dc02da7f 877 if (is_reference || !is_volatile || !is_complete || TREE_ADDRESSABLE (type))
653e5405 878 expr = TREE_OPERAND (expr, 0);
879
880 break;
b1a8d4ce 881 }
9031d10b 882
b1a8d4ce 883 case VAR_DECL:
884 {
653e5405 885 /* External variables might be incomplete. */
886 tree type = TREE_TYPE (expr);
887 int is_complete = COMPLETE_TYPE_P (complete_type (type));
888
889 if (TYPE_VOLATILE (type) && !is_complete)
890 warning (0, "object %qE of incomplete type %qT will not be accessed in %s",
891 expr, type, implicit ? implicit : "void context");
892 break;
b1a8d4ce 893 }
d9f88785 894
25b3017b 895 case TARGET_EXPR:
896 /* Don't bother with the temporary object returned from a function if
897 we don't use it and don't need to destroy it. We'll still
898 allocate space for it in expand_call or declare_return_variable,
899 but we don't need to track it through all the tree phases. */
67bb1301 900 if (TARGET_EXPR_IMPLICIT_P (expr)
25b3017b 901 && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (expr)))
902 {
903 tree init = TARGET_EXPR_INITIAL (expr);
904 if (TREE_CODE (init) == AGGR_INIT_EXPR
905 && !AGGR_INIT_VIA_CTOR_P (init))
906 {
907 tree fn = TREE_OPERAND (init, 0);
908 expr = build3 (CALL_EXPR, TREE_TYPE (TREE_TYPE (TREE_TYPE (fn))),
909 fn, TREE_OPERAND (init, 1), NULL_TREE);
910 }
911 }
912 break;
913
b1a8d4ce 914 default:;
915 }
916 {
917 tree probe = expr;
9031d10b 918
b1a8d4ce 919 if (TREE_CODE (probe) == ADDR_EXPR)
920 probe = TREE_OPERAND (expr, 0);
2e0e6579 921 if (type_unknown_p (probe))
922 {
923 /* [over.over] enumerates the places where we can take the address
924 of an overloaded function, and this is not one of them. */
cf103c6c 925 pedwarn ("%s cannot resolve address of overloaded function",
2e0e6579 926 implicit ? implicit : "void cast");
ea720917 927 expr = void_zero_node;
2e0e6579 928 }
929 else if (implicit && probe == expr && is_overloaded_fn (probe))
b1a8d4ce 930 /* Only warn when there is no &. */
c3ceba8e 931 warning (0, "%s is a reference, not call, to function %qE",
2e0e6579 932 implicit, expr);
b1a8d4ce 933 }
9031d10b 934
e3cfe3ce 935 if (expr != error_mark_node && !VOID_TYPE_P (TREE_TYPE (expr)))
b1a8d4ce 936 {
5e2a4133 937 if (implicit
938 && warn_unused_value
939 && !TREE_NO_WARNING (expr)
940 && !processing_template_decl)
1fe46df1 941 {
942 /* The middle end does not warn about expressions that have
943 been explicitly cast to void, so we must do so here. */
944 if (!TREE_SIDE_EFFECTS (expr))
ced7c954 945 warning (OPT_Wunused_value, "%s has no effect", implicit);
9031d10b 946 else
947 {
1fe46df1 948 tree e;
949 enum tree_code code;
950 enum tree_code_class class;
9031d10b 951
1fe46df1 952 e = expr;
953 /* We might like to warn about (say) "(int) f()", as the
954 cast has no effect, but the compiler itself will
955 generate implicit conversions under some
324d8f2d 956 circumstances. (For example a block copy will be
1fe46df1 957 turned into a call to "__builtin_memcpy", with a
958 conversion of the return value to an appropriate
959 type.) So, to avoid false positives, we strip
416ce344 960 conversions. Do not use STRIP_NOPs because it will
961 not strip conversions to "void", as that is not a
962 mode-preserving conversion. */
963 while (TREE_CODE (e) == NOP_EXPR)
964 e = TREE_OPERAND (e, 0);
1fe46df1 965
966 code = TREE_CODE (e);
967 class = TREE_CODE_CLASS (code);
968 if (class == tcc_comparison
969 || class == tcc_unary
9031d10b 970 || (class == tcc_binary
1fe46df1 971 && !(code == MODIFY_EXPR
972 || code == INIT_EXPR
973 || code == PREDECREMENT_EXPR
974 || code == PREINCREMENT_EXPR
975 || code == POSTDECREMENT_EXPR
976 || code == POSTINCREMENT_EXPR)))
ced7c954 977 warning (OPT_Wunused_value, "value computed is not used");
1fe46df1 978 }
979 }
00fa9079 980 expr = build1 (CONVERT_EXPR, void_type_node, expr);
b1a8d4ce 981 }
de5ad4cb 982 if (! TREE_SIDE_EFFECTS (expr))
983 expr = void_zero_node;
b1a8d4ce 984 return expr;
985}
986
d81e00a4 987/* Create an expression whose value is that of EXPR,
988 converted to type TYPE. The TREE_TYPE of the value
989 is always TYPE. This function implements all reasonable
990 conversions; callers should filter out those that are
c4a8ac95 991 not permitted by the language being compiled.
992
993 Most of this routine is from build_reinterpret_cast.
994
995 The backend cannot call cp_convert (what was convert) because
996 conversions to/from basetypes may involve memory references
997 (vbases) and adding or subtracting small values (multiple
998 inheritance), but it calls convert from the constant folding code
e0aa5007 999 on subtrees of already built trees after it has ripped them apart.
c4a8ac95 1000
1001 Also, if we ever support range variables, we'll probably also have to
1002 do a little bit more work. */
d81e00a4 1003
1004tree
35771a9a 1005convert (tree type, tree expr)
d81e00a4 1006{
c4a8ac95 1007 tree intype;
1008
1009 if (type == error_mark_node || expr == error_mark_node)
1010 return error_mark_node;
1011
c4a8ac95 1012 intype = TREE_TYPE (expr);
1013
6686e84d 1014 if (POINTER_TYPE_P (type) && POINTER_TYPE_P (intype))
13f0eb20 1015 return fold_if_not_in_template (build_nop (type, expr));
c4a8ac95 1016
1017 return ocp_convert (type, expr, CONV_OLD_CONVERT,
1018 LOOKUP_NORMAL|LOOKUP_NO_CONVERSION);
d81e00a4 1019}
1020
c4a8ac95 1021/* Like cp_convert, except permit conversions to take place which
471086d6 1022 are not normally allowed due to access restrictions
1023 (such as conversion from sub-type to private super-type). */
96624a9e 1024
471086d6 1025tree
35771a9a 1026convert_force (tree type, tree expr, int convtype)
471086d6 1027{
cd16867a 1028 tree e = expr;
1029 enum tree_code code = TREE_CODE (type);
471086d6 1030
1031 if (code == REFERENCE_TYPE)
9031d10b 1032 return (fold_if_not_in_template
5d7ed6c7 1033 (convert_to_reference (type, e, CONV_C_CAST, LOOKUP_COMPLAIN,
1034 NULL_TREE)));
471086d6 1035
1036 if (code == POINTER_TYPE)
5d7ed6c7 1037 return fold_if_not_in_template (convert_to_pointer_force (type, e));
471086d6 1038
ac9386a0 1039 /* From typeck.c convert_for_assignment */
471086d6 1040 if (((TREE_CODE (TREE_TYPE (e)) == POINTER_TYPE && TREE_CODE (e) == ADDR_EXPR
1041 && TREE_CODE (TREE_TYPE (e)) == POINTER_TYPE
1042 && TREE_CODE (TREE_TYPE (TREE_TYPE (e))) == METHOD_TYPE)
ac9386a0 1043 || integer_zerop (e)
1044 || TYPE_PTRMEMFUNC_P (TREE_TYPE (e)))
471086d6 1045 && TYPE_PTRMEMFUNC_P (type))
cb02169c 1046 /* compatible pointer to member functions. */
1047 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), e, 1,
1048 /*c_cast_p=*/1);
a74e8896 1049
c4a8ac95 1050 return ocp_convert (type, e, CONV_C_CAST|convtype, LOOKUP_NORMAL);
471086d6 1051}
1052
471086d6 1053/* Convert an aggregate EXPR to type XTYPE. If a conversion
1054 exists, return the attempted conversion. This may
1055 return ERROR_MARK_NODE if the conversion is not
1056 allowed (references private members, etc).
1057 If no conversion exists, NULL_TREE is returned.
1058
ce28ee2e 1059 FIXME: Ambiguity checking is wrong. Should choose one by the implicit
1060 object parameter, or by the second standard conversion sequence if
1061 that doesn't do it. This will probably wait for an overloading rewrite.
1062 (jason 8/9/95) */
471086d6 1063
ef9571e5 1064static tree
8999978b 1065build_type_conversion (tree xtype, tree expr)
471086d6 1066{
1067 /* C++: check to see if we can convert this aggregate type
bcf789d7 1068 into the required type. */
8999978b 1069 return build_user_type_conversion (xtype, expr, LOOKUP_NORMAL);
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.)
35771a9a 1074 which indicates which types are suitable. If COMPLAIN is true, complain
1e66592c 1075 about ambiguity; otherwise, the caller will deal with it. */
471086d6 1076
1e66592c 1077tree
35771a9a 1078build_expr_type_conversion (int desires, tree expr, bool complain)
471086d6 1079{
1e66592c 1080 tree basetype = TREE_TYPE (expr);
6a44e72e 1081 tree conv = NULL_TREE;
bdd152ce 1082 tree winner = NULL_TREE;
471086d6 1083
9031d10b 1084 if (expr == null_node
1085 && (desires & WANT_INT)
954885ed 1086 && !(desires & WANT_NULL))
308d6af4 1087 warning (OPT_Wconversion, "converting NULL to non-pointer type");
9031d10b 1088
ce28ee2e 1089 basetype = TREE_TYPE (expr);
471086d6 1090
3d411d73 1091 if (basetype == error_mark_node)
1092 return error_mark_node;
1093
bdd152ce 1094 if (! IS_AGGR_TYPE (basetype))
1095 switch (TREE_CODE (basetype))
1096 {
1097 case INTEGER_TYPE:
954885ed 1098 if ((desires & WANT_NULL) && null_ptr_cst_p (expr))
bdd152ce 1099 return expr;
1100 /* else fall through... */
1101
955f5493 1102 case VECTOR_TYPE:
bdd152ce 1103 case BOOLEAN_TYPE:
1104 return (desires & WANT_INT) ? expr : NULL_TREE;
1105 case ENUMERAL_TYPE:
1106 return (desires & WANT_ENUM) ? expr : NULL_TREE;
1107 case REAL_TYPE:
1108 return (desires & WANT_FLOAT) ? expr : NULL_TREE;
1109 case POINTER_TYPE:
1110 return (desires & WANT_POINTER) ? expr : NULL_TREE;
9031d10b 1111
bdd152ce 1112 case FUNCTION_TYPE:
1113 case ARRAY_TYPE:
a681799d 1114 return (desires & WANT_POINTER) ? decay_conversion (expr)
653e5405 1115 : NULL_TREE;
bdd152ce 1116 default:
1117 return NULL_TREE;
1118 }
1119
1120 /* The code for conversions from class type is currently only used for
1121 delete expressions. Other expressions are handled by build_new_op. */
f8182e08 1122 if (!complete_type_or_else (basetype, expr))
1123 return error_mark_node;
1124 if (!TYPE_HAS_CONVERSION (basetype))
bdd152ce 1125 return NULL_TREE;
1126
1127 for (conv = lookup_conversions (basetype); conv; conv = TREE_CHAIN (conv))
1128 {
1129 int win = 0;
1130 tree candidate;
1131 tree cand = TREE_VALUE (conv);
1132
1133 if (winner && winner == cand)
1134 continue;
1135
ef4534a3 1136 candidate = non_reference (TREE_TYPE (TREE_TYPE (cand)));
bdd152ce 1137
1138 switch (TREE_CODE (candidate))
1139 {
1140 case BOOLEAN_TYPE:
1141 case INTEGER_TYPE:
1142 win = (desires & WANT_INT); break;
1143 case ENUMERAL_TYPE:
1144 win = (desires & WANT_ENUM); break;
1145 case REAL_TYPE:
1146 win = (desires & WANT_FLOAT); break;
1147 case POINTER_TYPE:
1148 win = (desires & WANT_POINTER); break;
1149
1150 default:
1151 break;
1152 }
1153
1154 if (win)
1155 {
1156 if (winner)
1157 {
1158 if (complain)
1159 {
0a25aad6 1160 error ("ambiguous default type conversion from %qT",
653e5405 1161 basetype);
0a25aad6 1162 error (" candidate conversions include %qD and %qD",
653e5405 1163 winner, cand);
bdd152ce 1164 }
1165 return error_mark_node;
1166 }
1167 else
1168 winner = cand;
1169 }
1170 }
1e66592c 1171
bdd152ce 1172 if (winner)
1173 {
ef4534a3 1174 tree type = non_reference (TREE_TYPE (TREE_TYPE (winner)));
bdd152ce 1175 return build_user_type_conversion (type, expr, LOOKUP_NORMAL);
471086d6 1176 }
1e66592c 1177
985e9ee0 1178 return NULL_TREE;
471086d6 1179}
bc3887cf 1180
96624a9e 1181/* Implements integral promotion (4.1) and float->double promotion. */
1182
bc3887cf 1183tree
35771a9a 1184type_promotes_to (tree type)
bc3887cf 1185{
ce28ee2e 1186 if (type == error_mark_node)
1187 return error_mark_node;
1188
bc3887cf 1189 type = TYPE_MAIN_VARIANT (type);
bb0726a1 1190
1191 /* bool always promotes to int (not unsigned), even if it's the same
1192 size. */
028ea8b4 1193 if (type == boolean_type_node)
bb0726a1 1194 type = integer_type_node;
1195
1196 /* Normally convert enums to int, but convert wide enums to something
1197 wider. */
1198 else if (TREE_CODE (type) == ENUMERAL_TYPE
1199 || type == wchar_type_node)
6c9497b1 1200 {
1201 int precision = MAX (TYPE_PRECISION (type),
1202 TYPE_PRECISION (integer_type_node));
771d21fa 1203 tree totype = c_common_type_for_size (precision, 0);
78a8ed03 1204 if (TYPE_UNSIGNED (type)
6c9497b1 1205 && ! int_fits_type_p (TYPE_MAX_VALUE (type), totype))
771d21fa 1206 type = c_common_type_for_size (precision, 1);
6c9497b1 1207 else
1208 type = totype;
1209 }
d7aeef06 1210 else if (c_promoting_integer_type_p (type))
bc3887cf 1211 {
d0acef9e 1212 /* Retain unsignedness if really not getting bigger. */
78a8ed03 1213 if (TYPE_UNSIGNED (type)
d0acef9e 1214 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
bc3887cf 1215 type = unsigned_type_node;
1216 else
1217 type = integer_type_node;
1218 }
1219 else if (type == float_type_node)
1220 type = double_type_node;
9031d10b 1221
a681799d 1222 return type;
bc3887cf 1223}
668ae905 1224
668ae905 1225/* The routines below this point are carefully written to conform to
1226 the standard. They use the same terminology, and follow the rules
1227 closely. Although they are used only in pt.c at the moment, they
1228 should presumably be used everywhere in the future. */
1229
1146f179 1230/* Attempt to perform qualification conversions on EXPR to convert it
1231 to TYPE. Return the resulting expression, or error_mark_node if
1232 the conversion was impossible. */
1233
9031d10b 1234tree
35771a9a 1235perform_qualification_conversions (tree type, tree expr)
668ae905 1236{
1bc16cab 1237 tree expr_type;
1238
1239 expr_type = TREE_TYPE (expr);
1240
3b087710 1241 if (same_type_p (type, expr_type))
1242 return expr;
1243 else if (TYPE_PTR_P (type) && TYPE_PTR_P (expr_type)
1244 && comp_ptr_ttypes (TREE_TYPE (type), TREE_TYPE (expr_type)))
1bc16cab 1245 return build_nop (type, expr);
1246 else if (TYPE_PTR_TO_MEMBER_P (type)
1247 && TYPE_PTR_TO_MEMBER_P (expr_type)
1248 && same_type_p (TYPE_PTRMEM_CLASS_TYPE (type),
1249 TYPE_PTRMEM_CLASS_TYPE (expr_type))
1250 && comp_ptr_ttypes (TYPE_PTRMEM_POINTED_TO_TYPE (type),
1251 TYPE_PTRMEM_POINTED_TO_TYPE (expr_type)))
1252 return build_nop (type, expr);
1146f179 1253 else
1254 return error_mark_node;
668ae905 1255}