]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/cp/cvt.c
* typeck.c (decay_conversion): Any expression with type nullptr_t
[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,
b2af2a93 3 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
aa139c3f 4 Free Software Foundation, Inc.
471086d6 5 Hacked by Michael Tiemann (tiemann@cygnus.com)
6
6f0d25a6 7This file is part of GCC.
471086d6 8
6f0d25a6 9GCC is free software; you can redistribute it and/or modify
471086d6 10it under the terms of the GNU General Public License as published by
aa139c3f 11the Free Software Foundation; either version 3, or (at your option)
471086d6 12any later version.
13
6f0d25a6 14GCC is distributed in the hope that it will be useful,
471086d6 15but WITHOUT ANY WARRANTY; without even the implied warranty of
16MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17GNU General Public License for more details.
18
19You should have received a copy of the GNU General Public License
aa139c3f 20along with GCC; see the file COPYING3. If not see
21<http://www.gnu.org/licenses/>. */
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"
ca82e026 36#include "intl.h"
471086d6 37#include "convert.h"
f0eaeecd 38#include "toplev.h"
2e8226f7 39#include "decl.h"
7a979707 40#include "target.h"
96624a9e 41
e5ead12a 42static tree cp_convert_to_pointer (tree, tree);
35771a9a 43static tree convert_to_pointer_force (tree, tree);
ef9571e5 44static tree build_type_conversion (tree, tree);
35771a9a 45static tree build_up_reference (tree, tree, int, tree);
46static void warn_ref_binding (tree, tree, tree);
71ccdfff 47
471086d6 48/* Change of width--truncation and extension of integers or reals--
49 is represented with NOP_EXPR. Proper functioning of many things
50 assumes that no other conversions can be NOP_EXPRs.
51
52 Conversion between integer and pointer is represented with CONVERT_EXPR.
53 Converting integer to real uses FLOAT_EXPR
54 and real to integer uses FIX_TRUNC_EXPR.
55
56 Here is a list of all the functions that assume that widening and
57 narrowing is always done with a NOP_EXPR:
58 In convert.c, convert_to_integer.
59 In c-typeck.c, build_binary_op_nodefault (boolean ops),
653e5405 60 and c_common_truthvalue_conversion.
471086d6 61 In expr.c: expand_expr, for operands of a MULT_EXPR.
62 In fold-const.c: fold.
63 In tree.c: get_narrower and get_unwidened.
64
65 C++: in multiple-inheritance, converting between pointers may involve
66 adjusting them by a delta stored within the class definition. */
67\f
68/* Subroutines of `convert'. */
69
471086d6 70/* if converting pointer to pointer
71 if dealing with classes, check for derived->base or vice versa
72 else if dealing with method pointers, delegate
73 else convert blindly
74 else if converting class, pass off to build_type_conversion
e5ead12a 75 else try C-style pointer conversion. */
96624a9e 76
471086d6 77static tree
e5ead12a 78cp_convert_to_pointer (tree type, tree expr)
471086d6 79{
cd16867a 80 tree intype = TREE_TYPE (expr);
81 enum tree_code form;
f82eeb87 82 tree rval;
8bf328bf 83 if (intype == error_mark_node)
84 return error_mark_node;
74002e1d 85
95397ff9 86 if (MAYBE_CLASS_TYPE_P (intype))
96624a9e 87 {
96624a9e 88 intype = complete_type (intype);
4b72716d 89 if (!COMPLETE_TYPE_P (intype))
96624a9e 90 {
0a25aad6 91 error ("can't convert from incomplete type %qT to %qT",
653e5405 92 intype, type);
96624a9e 93 return error_mark_node;
94 }
95
8999978b 96 rval = build_type_conversion (type, expr);
96624a9e 97 if (rval)
98 {
99 if (rval == error_mark_node)
0a25aad6 100 error ("conversion of %qE from %qT to %qT is ambiguous",
653e5405 101 expr, intype, type);
96624a9e 102 return rval;
103 }
104 }
105
860740a7 106 /* Handle anachronistic conversions from (::*)() to cv void* or (*)(). */
38281c46 107 if (TREE_CODE (type) == POINTER_TYPE
108 && (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
e3cfe3ce 109 || VOID_TYPE_P (TREE_TYPE (type))))
38281c46 110 {
6ab399e8 111 if (TYPE_PTRMEMFUNC_P (intype)
112 || TREE_CODE (intype) == METHOD_TYPE)
113 return convert_member_func_to_ptr (type, expr);
a63bc44c 114 if (TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE)
115 return build_nop (type, expr);
38281c46 116 intype = TREE_TYPE (expr);
117 }
118
311e42bc 119 if (expr == error_mark_node)
120 return error_mark_node;
121
74002e1d 122 form = TREE_CODE (intype);
123
2b77484d 124 if (POINTER_TYPE_P (intype))
471086d6 125 {
126 intype = TYPE_MAIN_VARIANT (intype);
127
128 if (TYPE_MAIN_VARIANT (type) != intype
2b77484d 129 && TREE_CODE (type) == POINTER_TYPE
471086d6 130 && TREE_CODE (TREE_TYPE (type)) == RECORD_TYPE
95397ff9 131 && MAYBE_CLASS_TYPE_P (TREE_TYPE (type))
132 && MAYBE_CLASS_TYPE_P (TREE_TYPE (intype))
4a2680fc 133 && TREE_CODE (TREE_TYPE (intype)) == RECORD_TYPE)
471086d6 134 {
135 enum tree_code code = PLUS_EXPR;
4a2680fc 136 tree binfo;
8999978b 137 tree intype_class;
138 tree type_class;
139 bool same_p;
4a2680fc 140
8999978b 141 intype_class = TREE_TYPE (intype);
142 type_class = TREE_TYPE (type);
143
9031d10b 144 same_p = same_type_p (TYPE_MAIN_VARIANT (intype_class),
8999978b 145 TYPE_MAIN_VARIANT (type_class));
146 binfo = NULL_TREE;
c0af329c 147 /* Try derived to base conversion. */
8999978b 148 if (!same_p)
149 binfo = lookup_base (intype_class, type_class, ba_check, NULL);
150 if (!same_p && !binfo)
471086d6 151 {
c0af329c 152 /* Try base to derived conversion. */
8999978b 153 binfo = lookup_base (type_class, intype_class, ba_check, NULL);
471086d6 154 code = MINUS_EXPR;
155 }
4a2680fc 156 if (binfo == error_mark_node)
157 return error_mark_node;
8999978b 158 if (binfo || same_p)
471086d6 159 {
8999978b 160 if (binfo)
161 expr = build_base_path (code, expr, binfo, 0);
c0af329c 162 /* Add any qualifier conversions. */
8999978b 163 return build_nop (type, expr);
471086d6 164 }
165 }
471086d6 166
1bc16cab 167 if (TYPE_PTRMEMFUNC_P (type))
bea7d742 168 {
0a25aad6 169 error ("cannot convert %qE from type %qT to type %qT",
653e5405 170 expr, intype, type);
1bc16cab 171 return error_mark_node;
172 }
a17aefa2 173
1bc16cab 174 return build_nop (type, expr);
175 }
e5ead12a 176 else if ((TYPE_PTRMEM_P (type) && TYPE_PTRMEM_P (intype))
177 || (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype)))
178 return convert_ptrmem (type, expr, /*allow_inverse_p=*/false,
85132186 179 /*c_cast_p=*/false, tf_warning_or_error);
2b77484d 180 else if (TYPE_PTRMEMFUNC_P (intype))
181 {
a63bc44c 182 if (!warn_pmf2ptr)
183 {
184 if (TREE_CODE (expr) == PTRMEM_CST)
185 return cp_convert_to_pointer (type,
e5ead12a 186 PTRMEM_CST_MEMBER (expr));
a63bc44c 187 else if (TREE_CODE (expr) == OFFSET_REF)
188 {
189 tree object = TREE_OPERAND (expr, 0);
190 return get_member_function_from_ptrfunc (&object,
191 TREE_OPERAND (expr, 1));
192 }
193 }
0a25aad6 194 error ("cannot convert %qE from type %qT to type %qT",
653e5405 195 expr, intype, type);
2b77484d 196 return error_mark_node;
197 }
471086d6 198
ef434176 199 if (null_ptr_cst_p (expr))
471086d6 200 {
2b77484d 201 if (TYPE_PTRMEMFUNC_P (type))
cb02169c 202 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), expr, 0,
85132186 203 /*c_cast_p=*/false, tf_warning_or_error);
ad63a0fc 204
606b494c 205 if (TYPE_PTRMEM_P (type))
7c446c95 206 {
207 /* A NULL pointer-to-member is represented by -1, not by
208 zero. */
697bbc3f 209 expr = build_int_cst_type (type, -1);
7c446c95 210 }
ad63a0fc 211 else
7016c612 212 expr = build_int_cst (type, 0);
9031d10b 213
471086d6 214 return expr;
215 }
1bc16cab 216 else if (TYPE_PTR_TO_MEMBER_P (type) && INTEGRAL_CODE_P (form))
de1f960a 217 {
0a25aad6 218 error ("invalid conversion from %qT to %qT", intype, type);
de1f960a 219 return error_mark_node;
220 }
471086d6 221
bb0726a1 222 if (INTEGRAL_CODE_P (form))
471086d6 223 {
c5aa1e92 224 if (TYPE_PRECISION (intype) == POINTER_SIZE)
471086d6 225 return build1 (CONVERT_EXPR, type, expr);
771d21fa 226 expr = cp_convert (c_common_type_for_size (POINTER_SIZE, 0), expr);
092b1d6f 227 /* Modes may be different but sizes should be the same. There
228 is supposed to be some integral type that is the same width
229 as a pointer. */
230 gcc_assert (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (expr)))
231 == GET_MODE_SIZE (TYPE_MODE (type)));
9031d10b 232
471086d6 233 return convert_to_pointer (type, expr);
234 }
235
cc4d0855 236 if (type_unknown_p (expr))
0fbca5e8 237 return instantiate_type (type, expr, tf_warning_or_error);
cc4d0855 238
0a25aad6 239 error ("cannot convert %qE from type %qT to type %qT",
653e5405 240 expr, intype, type);
471086d6 241 return error_mark_node;
242}
243
244/* Like convert, except permit conversions to take place which
245 are not normally allowed due to access restrictions
246 (such as conversion from sub-type to private super-type). */
96624a9e 247
471086d6 248static tree
35771a9a 249convert_to_pointer_force (tree type, tree expr)
471086d6 250{
cd16867a 251 tree intype = TREE_TYPE (expr);
252 enum tree_code form = TREE_CODE (intype);
9031d10b 253
471086d6 254 if (form == POINTER_TYPE)
255 {
256 intype = TYPE_MAIN_VARIANT (intype);
257
258 if (TYPE_MAIN_VARIANT (type) != intype
259 && TREE_CODE (TREE_TYPE (type)) == RECORD_TYPE
95397ff9 260 && MAYBE_CLASS_TYPE_P (TREE_TYPE (type))
261 && MAYBE_CLASS_TYPE_P (TREE_TYPE (intype))
471086d6 262 && TREE_CODE (TREE_TYPE (intype)) == RECORD_TYPE)
263 {
264 enum tree_code code = PLUS_EXPR;
4a2680fc 265 tree binfo;
266
267 binfo = lookup_base (TREE_TYPE (intype), TREE_TYPE (type),
ada40935 268 ba_unique, NULL);
4a2680fc 269 if (!binfo)
471086d6 270 {
4a2680fc 271 binfo = lookup_base (TREE_TYPE (type), TREE_TYPE (intype),
ada40935 272 ba_unique, NULL);
4a2680fc 273 code = MINUS_EXPR;
471086d6 274 }
4a2680fc 275 if (binfo == error_mark_node)
276 return error_mark_node;
277 if (binfo)
471086d6 278 {
4a2680fc 279 expr = build_base_path (code, expr, binfo, 0);
653e5405 280 if (expr == error_mark_node)
281 return error_mark_node;
c0af329c 282 /* Add any qualifier conversions. */
4a2680fc 283 if (!same_type_p (TREE_TYPE (TREE_TYPE (expr)),
284 TREE_TYPE (type)))
8999978b 285 expr = build_nop (type, expr);
4a2680fc 286 return expr;
471086d6 287 }
471086d6 288 }
471086d6 289 }
290
e5ead12a 291 return cp_convert_to_pointer (type, expr);
471086d6 292}
293
294/* We are passing something to a function which requires a reference.
295 The type we are interested in is in TYPE. The initial
296 value we have to begin with is in ARG.
297
298 FLAGS controls how we manage access checking.
ca106ab1 299 DIRECT_BIND in FLAGS controls how any temporaries are generated.
300 If DIRECT_BIND is set, DECL is the reference we're binding to. */
96624a9e 301
471086d6 302static tree
35771a9a 303build_up_reference (tree type, tree arg, int flags, tree decl)
471086d6 304{
c76251c1 305 tree rval;
0543e7a9 306 tree argtype = TREE_TYPE (arg);
471086d6 307 tree target_type = TREE_TYPE (type);
471086d6 308
b4df430b 309 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
471086d6 310
c76251c1 311 if ((flags & DIRECT_BIND) && ! real_lvalue_p (arg))
471086d6 312 {
ca106ab1 313 /* Create a new temporary variable. We can't just use a TARGET_EXPR
314 here because it needs to live as long as DECL. */
c76251c1 315 tree targ = arg;
ca106ab1 316
7c09476d 317 arg = make_temporary_var_for_ref_to_temp (decl, TREE_TYPE (arg));
c23ebfdd 318
319 /* Process the initializer for the declaration. */
38281c46 320 DECL_INITIAL (arg) = targ;
d91303a6 321 cp_finish_decl (arg, targ, /*init_const_expr_p=*/false, NULL_TREE,
011310f7 322 LOOKUP_ONLYCONVERTING|DIRECT_BIND);
1ad432f2 323 }
c76251c1 324 else if (!(flags & DIRECT_BIND) && ! lvalue_p (arg))
1d8e4310 325 return get_target_expr (arg);
1ad432f2 326
ca106ab1 327 /* If we had a way to wrap this up, and say, if we ever needed its
b0df6589 328 address, transform all occurrences of the register, into a memory
329 reference we could win better. */
ebd21de4 330 rval = cp_build_unary_op (ADDR_EXPR, arg, 1, tf_warning_or_error);
15e55420 331 if (rval == error_mark_node)
332 return error_mark_node;
333
6686e84d 334 if ((flags & LOOKUP_PROTECT)
335 && TYPE_MAIN_VARIANT (argtype) != TYPE_MAIN_VARIANT (target_type)
95397ff9 336 && MAYBE_CLASS_TYPE_P (argtype)
337 && MAYBE_CLASS_TYPE_P (target_type))
6686e84d 338 {
7e6960e0 339 /* We go through lookup_base for the access control. */
4a2680fc 340 tree binfo = lookup_base (argtype, target_type, ba_check, NULL);
6686e84d 341 if (binfo == error_mark_node)
342 return error_mark_node;
343 if (binfo == NULL_TREE)
344 return error_not_base_type (target_type, argtype);
4a2680fc 345 rval = build_base_path (PLUS_EXPR, rval, binfo, 1);
6686e84d 346 }
c76251c1 347 else
348 rval
349 = convert_to_pointer_force (build_pointer_type (target_type), rval);
8999978b 350 return build_nop (type, rval);
471086d6 351}
352
519ba62c 353/* Subroutine of convert_to_reference. REFTYPE is the target reference type.
354 INTYPE is the original rvalue type and DECL is an optional _DECL node
355 for diagnostics.
9031d10b 356
519ba62c 357 [dcl.init.ref] says that if an rvalue is used to
358 initialize a reference, then the reference must be to a
359 non-volatile const type. */
360
361static void
35771a9a 362warn_ref_binding (tree reftype, tree intype, tree decl)
519ba62c 363{
364 tree ttl = TREE_TYPE (reftype);
9031d10b 365
519ba62c 366 if (!CP_TYPE_CONST_NON_VOLATILE_P (ttl))
367 {
368 const char *msg;
369
370 if (CP_TYPE_VOLATILE_P (ttl) && decl)
ca82e026 371 msg = G_("initialization of volatile reference type %q#T from "
372 "rvalue of type %qT");
519ba62c 373 else if (CP_TYPE_VOLATILE_P (ttl))
ca82e026 374 msg = G_("conversion to volatile reference type %q#T "
375 "from rvalue of type %qT");
519ba62c 376 else if (decl)
ca82e026 377 msg = G_("initialization of non-const reference type %q#T from "
378 "rvalue of type %qT");
519ba62c 379 else
ca82e026 380 msg = G_("conversion to non-const reference type %q#T from "
381 "rvalue of type %qT");
519ba62c 382
2b9e3597 383 permerror (input_location, msg, reftype, intype);
519ba62c 384 }
385}
386
471086d6 387/* For C++: Only need to do one-level references, but cannot
388 get tripped up on signed/unsigned differences.
389
d81e00a4 390 DECL is either NULL_TREE or the _DECL node for a reference that is being
391 initialized. It can be error_mark_node if we don't know the _DECL but
392 we know it's an initialization. */
471086d6 393
471086d6 394tree
35771a9a 395convert_to_reference (tree reftype, tree expr, int convtype,
653e5405 396 int flags, tree decl)
471086d6 397{
cd16867a 398 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (reftype));
399 tree intype;
471086d6 400 tree rval = NULL_TREE;
1a3f833b 401 tree rval_as_conversion = NULL_TREE;
1bc16cab 402 bool can_convert_intype_to_type;
1a3f833b 403
9031d10b 404 if (TREE_CODE (type) == FUNCTION_TYPE
d32df6a6 405 && TREE_TYPE (expr) == unknown_type_node)
9031d10b 406 expr = instantiate_type (type, expr,
0a3b29ad 407 (flags & LOOKUP_COMPLAIN)
0fbca5e8 408 ? tf_warning_or_error : tf_none);
0a3b29ad 409
410 if (expr == error_mark_node)
411 return error_mark_node;
412
413 intype = TREE_TYPE (expr);
cc4d0855 414
b4df430b 415 gcc_assert (TREE_CODE (intype) != REFERENCE_TYPE);
ab12ed55 416 gcc_assert (TREE_CODE (reftype) == REFERENCE_TYPE);
471086d6 417
471086d6 418 intype = TYPE_MAIN_VARIANT (intype);
419
1bc16cab 420 can_convert_intype_to_type = can_convert (type, intype);
421 if (!can_convert_intype_to_type
95397ff9 422 && (convtype & CONV_IMPLICIT) && MAYBE_CLASS_TYPE_P (intype)
1a3f833b 423 && ! (flags & LOOKUP_NO_CONVERSION))
424 {
425 /* Look for a user-defined conversion to lvalue that we can use. */
426
0a4be248 427 rval_as_conversion
8999978b 428 = build_type_conversion (reftype, expr);
1a3f833b 429
430 if (rval_as_conversion && rval_as_conversion != error_mark_node
431 && real_lvalue_p (rval_as_conversion))
432 {
433 expr = rval_as_conversion;
434 rval_as_conversion = NULL_TREE;
435 intype = type;
1bc16cab 436 can_convert_intype_to_type = 1;
1a3f833b 437 }
438 }
439
1bc16cab 440 if (((convtype & CONV_STATIC) && can_convert (intype, type))
441 || ((convtype & CONV_IMPLICIT) && can_convert_intype_to_type))
471086d6 442 {
471086d6 443 if (flags & LOOKUP_COMPLAIN)
444 {
bc3887cf 445 tree ttl = TREE_TYPE (reftype);
cb0ba4ec 446 tree ttr = lvalue_type (expr);
471086d6 447
519ba62c 448 if (! real_lvalue_p (expr))
449 warn_ref_binding (reftype, intype, decl);
9031d10b 450
519ba62c 451 if (! (convtype & CONV_CONST)
3e04bd45 452 && !at_least_as_qualified_p (ttl, ttr))
2b9e3597 453 permerror (input_location, "conversion from %qT to %qT discards qualifiers",
07317e69 454 ttr, reftype);
0543e7a9 455 }
456
ca106ab1 457 return build_up_reference (reftype, expr, flags, decl);
471086d6 458 }
c07b1ad1 459 else if ((convtype & CONV_REINTERPRET) && lvalue_p (expr))
0543e7a9 460 {
461 /* When casting an lvalue to a reference type, just convert into
462 a pointer to the new type and deference it. This is allowed
e581f478 463 by San Diego WP section 5.2.9 paragraph 12, though perhaps it
0543e7a9 464 should be done directly (jason). (int &)ri ---> *(int*)&ri */
e581f478 465
3748625f 466 /* B* bp; A& ar = (A&)bp; is valid, but it's probably not what they
653e5405 467 meant. */
1a3f833b 468 if (TREE_CODE (intype) == POINTER_TYPE
a09db423 469 && (comptypes (TREE_TYPE (intype), type,
470 COMPARE_BASE | COMPARE_DERIVED)))
c3ceba8e 471 warning (0, "casting %qT to %qT does not dereference pointer",
00952d10 472 intype, reftype);
9031d10b 473
ebd21de4 474 rval = cp_build_unary_op (ADDR_EXPR, expr, 0, tf_warning_or_error);
0543e7a9 475 if (rval != error_mark_node)
985e9ee0 476 rval = convert_force (build_pointer_type (TREE_TYPE (reftype)),
477 rval, 0);
0543e7a9 478 if (rval != error_mark_node)
b0722fac 479 rval = build1 (NOP_EXPR, reftype, rval);
0543e7a9 480 }
0a4be248 481 else
860740a7 482 {
483 rval = convert_for_initialization (NULL_TREE, type, expr, flags,
183407ee 484 ICR_CONVERTING, 0, 0,
ebd21de4 485 tf_warning_or_error);
a66fe4cb 486 if (rval == NULL_TREE || rval == error_mark_node)
487 return rval;
519ba62c 488 warn_ref_binding (reftype, intype, decl);
ca106ab1 489 rval = build_up_reference (reftype, rval, flags, decl);
860740a7 490 }
471086d6 491
492 if (rval)
493 {
96624a9e 494 /* If we found a way to convert earlier, then use it. */
471086d6 495 return rval;
496 }
497
b248d3f7 498 if (flags & LOOKUP_COMPLAIN)
0a25aad6 499 error ("cannot convert type %qT to type %qT", intype, reftype);
b248d3f7 500
471086d6 501 return error_mark_node;
502}
503
504/* We are using a reference VAL for its value. Bash that reference all the
96624a9e 505 way down to its lowest form. */
506
471086d6 507tree
35771a9a 508convert_from_reference (tree val)
471086d6 509{
73bdcca2 510 if (TREE_TYPE (val)
511 && TREE_CODE (TREE_TYPE (val)) == REFERENCE_TYPE)
729f89ff 512 {
2fb61329 513 tree t = TREE_TYPE (TREE_TYPE (val));
729f89ff 514 tree ref = build1 (INDIRECT_REF, t, val);
9031d10b 515
729f89ff 516 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
517 so that we get the proper error message if the result is used
518 to assign to. Also, &* is supposed to be a no-op. */
519 TREE_READONLY (ref) = CP_TYPE_CONST_P (t);
520 TREE_THIS_VOLATILE (ref) = CP_TYPE_VOLATILE_P (t);
521 TREE_SIDE_EFFECTS (ref)
522 = (TREE_THIS_VOLATILE (ref) || TREE_SIDE_EFFECTS (val));
523 REFERENCE_REF_P (ref) = 1;
524 val = ref;
525 }
9031d10b 526
471086d6 527 return val;
528}
e5dab226 529
530/* Really perform an lvalue-to-rvalue conversion, including copying an
531 argument of class type into a temporary. */
532
533tree
534force_rvalue (tree expr)
535{
95397ff9 536 if (MAYBE_CLASS_TYPE_P (TREE_TYPE (expr)) && TREE_CODE (expr) != TARGET_EXPR)
e5dab226 537 expr = ocp_convert (TREE_TYPE (expr), expr,
538 CONV_IMPLICIT|CONV_FORCE_TEMP, LOOKUP_NORMAL);
539 else
540 expr = decay_conversion (expr);
541
542 return expr;
543}
f5220369 544
471086d6 545\f
f5220369 546/* Fold away simple conversions, but make sure the result is an rvalue. */
547
548tree
549cp_fold_convert (tree type, tree expr)
550{
551 return rvalue (fold_convert (type, expr));
552}
553
c4a8ac95 554/* C++ conversions, preference to static cast conversions. */
555
556tree
35771a9a 557cp_convert (tree type, tree expr)
c4a8ac95 558{
559 return ocp_convert (type, expr, CONV_OLD_CONVERT, LOOKUP_NORMAL);
560}
561
59dd8856 562/* C++ equivalent of convert_and_check but using cp_convert as the
563 conversion function.
564
565 Convert EXPR to TYPE, warning about conversion problems with constants.
566 Invoke this function on every expression that is converted implicitly,
567 i.e. because of language rules and not because of an explicit cast. */
568
569tree
570cp_convert_and_check (tree type, tree expr)
571{
572 tree result;
573
574 if (TREE_TYPE (expr) == type)
575 return expr;
576
577 result = cp_convert (type, expr);
578
48d94ede 579 if (c_inhibit_evaluation_warnings == 0
580 && !TREE_OVERFLOW_P (expr)
581 && result != error_mark_node)
59dd8856 582 warnings_for_convert_and_check (type, expr, result);
583
584 return result;
585}
586
b248d3f7 587/* Conversion...
588
589 FLAGS indicates how we should behave. */
590
471086d6 591tree
35771a9a 592ocp_convert (tree type, tree expr, int convtype, int flags)
471086d6 593{
cd16867a 594 tree e = expr;
595 enum tree_code code = TREE_CODE (type);
7a979707 596 const char *invalid_conv_diag;
2b3c93a3 597 tree e1;
471086d6 598
1bc16cab 599 if (error_operand_p (e) || type == error_mark_node)
471086d6 600 return error_mark_node;
d81e00a4 601
5b592939 602 complete_type (type);
603 complete_type (TREE_TYPE (expr));
604
7a979707 605 if ((invalid_conv_diag
606 = targetm.invalid_conversion (TREE_TYPE (expr), type)))
607 {
608 error (invalid_conv_diag);
609 return error_mark_node;
610 }
611
13f0eb20 612 e = integral_constant_value (e);
7a00f939 613 if (error_operand_p (e))
614 return error_mark_node;
7745052b 615
95397ff9 616 if (MAYBE_CLASS_TYPE_P (type) && (convtype & CONV_FORCE_TEMP))
1a3f833b 617 /* We need a new temporary; don't take this shortcut. */;
930e8175 618 else if (same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (e)))
ca23ec64 619 {
daf9ff67 620 if (same_type_p (type, TREE_TYPE (e)))
ca23ec64 621 /* The call to fold will not always remove the NOP_EXPR as
622 might be expected, since if one of the types is a typedef;
755edffd 623 the comparison in fold is just equality of pointers, not a
a09db423 624 call to comptypes. We don't call fold in this case because
f699c174 625 that can result in infinite recursion; fold will call
626 convert, which will call ocp_convert, etc. */
627 return e;
372e0e29 628 /* For complex data types, we need to perform componentwise
653e5405 629 conversion. */
372e0e29 630 else if (TREE_CODE (type) == COMPLEX_TYPE)
653e5405 631 return fold_if_not_in_template (convert_to_complex (type, e));
bdb2219e 632 else if (TREE_CODE (e) == TARGET_EXPR)
633 {
634 /* Don't build a NOP_EXPR of class type. Instead, change the
930e8175 635 type of the temporary. */
bdb2219e 636 TREE_TYPE (e) = TREE_TYPE (TARGET_EXPR_SLOT (e)) = type;
637 return e;
638 }
ca23ec64 639 else
092b1d6f 640 {
641 /* We shouldn't be treating objects of ADDRESSABLE type as
642 rvalues. */
643 gcc_assert (!TREE_ADDRESSABLE (type));
5d7ed6c7 644 return fold_if_not_in_template (build_nop (type, e));
092b1d6f 645 }
ca23ec64 646 }
647
2b3c93a3 648 e1 = targetm.convert_to_type (type, e);
649 if (e1)
650 return e1;
651
d81e00a4 652 if (code == VOID_TYPE && (convtype & CONV_STATIC))
95b2ac55 653 {
dab3247a 654 e = convert_to_void (e, ICV_CAST, tf_warning_or_error);
aeef2be5 655 return e;
95b2ac55 656 }
d81e00a4 657
bb0726a1 658 if (INTEGRAL_CODE_P (code))
471086d6 659 {
617abf06 660 tree intype = TREE_TYPE (e);
384590d4 661
662 if (TREE_CODE (type) == ENUMERAL_TYPE)
663 {
664 /* enum = enum, enum = int, enum = float, (enum)pointer are all
665 errors. */
666 if (((INTEGRAL_OR_ENUMERATION_TYPE_P (intype)
653e5405 667 || TREE_CODE (intype) == REAL_TYPE)
b312a686 668 && ! (convtype & CONV_STATIC))
384590d4 669 || TREE_CODE (intype) == POINTER_TYPE)
670 {
671 if (flags & LOOKUP_COMPLAIN)
2b9e3597 672 permerror (input_location, "conversion from %q#T to %q#T", intype, type);
471086d6 673
384590d4 674 if (!flag_permissive)
675 return error_mark_node;
676 }
677
678 /* [expr.static.cast]
679
680 8. A value of integral or enumeration type can be explicitly
681 converted to an enumeration type. The value is unchanged if
682 the original value is within the range of the enumeration
683 values. Otherwise, the resulting enumeration value is
684 unspecified. */
bb9c1a09 685 if (TREE_CODE (expr) == INTEGER_CST
686 && !int_fits_type_p (expr, ENUM_UNDERLYING_TYPE (type)))
384590d4 687 warning (OPT_Wconversion,
688 "the result of the conversion is unspecified because "
689 "%qE is outside the range of type %qT",
690 expr, type);
471086d6 691 }
95397ff9 692 if (MAYBE_CLASS_TYPE_P (intype))
471086d6 693 {
694 tree rval;
8999978b 695 rval = build_type_conversion (type, e);
6495357a 696 if (rval)
697 return rval;
b248d3f7 698 if (flags & LOOKUP_COMPLAIN)
0a25aad6 699 error ("%q#T used where a %qT was expected", intype, type);
471086d6 700 return error_mark_node;
701 }
bb0726a1 702 if (code == BOOLEAN_TYPE)
ce871053 703 return cp_truthvalue_conversion (e);
704
5d7ed6c7 705 return fold_if_not_in_template (convert_to_integer (type, e));
471086d6 706 }
c7ca48ea 707 if (NULLPTR_TYPE_P (type) && e && null_ptr_cst_p (e))
6fe11077 708 return nullptr_node;
1bc16cab 709 if (POINTER_TYPE_P (type) || TYPE_PTR_TO_MEMBER_P (type))
e5ead12a 710 return fold_if_not_in_template (cp_convert_to_pointer (type, e));
684b9d79 711 if (code == VECTOR_TYPE)
c37ff371 712 {
713 tree in_vtype = TREE_TYPE (e);
95397ff9 714 if (MAYBE_CLASS_TYPE_P (in_vtype))
c37ff371 715 {
716 tree ret_val;
717 ret_val = build_type_conversion (type, e);
653e5405 718 if (ret_val)
719 return ret_val;
720 if (flags & LOOKUP_COMPLAIN)
721 error ("%q#T used where a %qT was expected", in_vtype, type);
722 return error_mark_node;
c37ff371 723 }
5d7ed6c7 724 return fold_if_not_in_template (convert_to_vector (type, e));
c37ff371 725 }
c4a8ac95 726 if (code == REAL_TYPE || code == COMPLEX_TYPE)
471086d6 727 {
95397ff9 728 if (MAYBE_CLASS_TYPE_P (TREE_TYPE (e)))
471086d6 729 {
730 tree rval;
8999978b 731 rval = build_type_conversion (type, e);
471086d6 732 if (rval)
733 return rval;
734 else
b248d3f7 735 if (flags & LOOKUP_COMPLAIN)
0a25aad6 736 error ("%q#T used where a floating point value was expected",
b248d3f7 737 TREE_TYPE (e));
471086d6 738 }
c4a8ac95 739 if (code == REAL_TYPE)
5d7ed6c7 740 return fold_if_not_in_template (convert_to_real (type, e));
c4a8ac95 741 else if (code == COMPLEX_TYPE)
5d7ed6c7 742 return fold_if_not_in_template (convert_to_complex (type, e));
471086d6 743 }
744
745 /* New C++ semantics: since assignment is now based on
746 memberwise copying, if the rhs type is derived from the
747 lhs type, then we may still do a conversion. */
95397ff9 748 if (RECORD_OR_UNION_CODE_P (code))
471086d6 749 {
750 tree dtype = TREE_TYPE (e);
c25194fd 751 tree ctor = NULL_TREE;
471086d6 752
471086d6 753 dtype = TYPE_MAIN_VARIANT (dtype);
754
471086d6 755 /* Conversion between aggregate types. New C++ semantics allow
756 objects of derived type to be cast to objects of base type.
757 Old semantics only allowed this between pointers.
758
759 There may be some ambiguity between using a constructor
760 vs. using a type conversion operator when both apply. */
761
0a4be248 762 ctor = e;
860740a7 763
8c18e707 764 if (abstract_virtuals_error (NULL_TREE, type))
765 return error_mark_node;
6cbb4197 766
f82f1250 767 if (BRACE_ENCLOSED_INITIALIZER_P (ctor))
768 ctor = perform_implicit_conversion (type, ctor, tf_warning_or_error);
769 else if ((flags & LOOKUP_ONLYCONVERTING)
770 && ! (CLASS_TYPE_P (dtype) && DERIVED_FROM_P (type, dtype)))
fce73460 771 /* For copy-initialization, first we create a temp of the proper type
772 with a user-defined conversion sequence, then we direct-initialize
773 the target with the temp (see [dcl.init]). */
774 ctor = build_user_type_conversion (type, ctor, flags);
3eb89cd8 775 else
f352a3fb 776 {
777 VEC(tree,gc) *ctor_vec = make_tree_vector_single (ctor);
778 ctor = build_special_member_call (NULL_TREE,
779 complete_ctor_identifier,
780 &ctor_vec,
781 type, flags,
782 tf_warning_or_error);
783 release_tree_vector (ctor_vec);
784 }
0a4be248 785 if (ctor)
786 return build_cplus_new (type, ctor);
471086d6 787 }
788
b248d3f7 789 if (flags & LOOKUP_COMPLAIN)
a7cc1549 790 {
791 /* If the conversion failed and expr was an invalid use of pointer to
792 member function, try to report a meaningful error. */
793 if (invalid_nonstatic_memfn_p (expr, tf_warning_or_error))
794 /* We displayed the error message. */;
795 else
796 error ("conversion from %qT to non-scalar type %qT requested",
797 TREE_TYPE (expr), type);
798 }
471086d6 799 return error_mark_node;
800}
801
b1a8d4ce 802/* When an expression is used in a void context, its value is discarded and
803 no lvalue-rvalue and similar conversions happen [expr.static.cast/4,
804 stmt.expr/1, expr.comma/1]. This permits dereferencing an incomplete type
805 in a void context. The C++ standard does not define what an `access' to an
63eff20d 806 object is, but there is reason to believe that it is the lvalue to rvalue
b1a8d4ce 807 conversion -- if it were not, `*&*p = 1' would violate [expr]/4 in that it
808 accesses `*p' not to calculate the value to be stored. But, dcl.type.cv/8
809 indicates that volatile semantics should be the same between C and C++
810 where ever possible. C leaves it implementation defined as to what
811 constitutes an access to a volatile. So, we interpret `*vp' as a read of
812 the volatile object `vp' points to, unless that is an incomplete type. For
813 volatile references we do not do this interpretation, because that would
814 make it impossible to ignore the reference return value from functions. We
815 issue warnings in the confusing cases.
9031d10b 816
dab3247a 817 The IMPLICIT is ICV_CAST when the user is explicitly converting an expression
818 to void via a cast. If an expression is being implicitly converted, IMPLICIT
819 indicates the context of the implicit conversion. */
b1a8d4ce 820
821tree
dab3247a 822convert_to_void (tree expr, impl_conv_void implicit, tsubst_flags_t complain)
b1a8d4ce 823{
9031d10b 824 if (expr == error_mark_node
3d411d73 825 || TREE_TYPE (expr) == error_mark_node)
826 return error_mark_node;
b2af2a93 827
dab3247a 828 if (implicit == ICV_CAST)
f94341bb 829 mark_exp_read (expr);
830 else
831 {
832 tree exprv = expr;
833
834 while (TREE_CODE (exprv) == COMPOUND_EXPR)
835 exprv = TREE_OPERAND (exprv, 1);
0388391d 836 if (DECL_P (exprv)
837 || handled_component_p (exprv)
838 || TREE_CODE (exprv) == INDIRECT_REF)
f94341bb 839 /* Expr is not being 'used' here, otherwise we whould have
840 called mark_{rl}value_use use here, which would have in turn
841 called mark_exp_read. Rather, we call mark_exp_read directly
842 to avoid some warnings when
843 -Wunused-but-set-{variable,parameter} is in effect. */
844 mark_exp_read (exprv);
845 }
b2af2a93 846
b1a8d4ce 847 if (!TREE_TYPE (expr))
848 return expr;
ebd21de4 849 if (invalid_nonstatic_memfn_p (expr, complain))
334f6ce1 850 return error_mark_node;
ed36f1cf 851 if (TREE_CODE (expr) == PSEUDO_DTOR_EXPR)
852 {
ebd21de4 853 if (complain & tf_error)
854 error ("pseudo-destructor is not called");
ed36f1cf 855 return error_mark_node;
856 }
e3cfe3ce 857 if (VOID_TYPE_P (TREE_TYPE (expr)))
b1a8d4ce 858 return expr;
859 switch (TREE_CODE (expr))
860 {
861 case COND_EXPR:
862 {
653e5405 863 /* The two parts of a cond expr might be separate lvalues. */
864 tree op1 = TREE_OPERAND (expr,1);
865 tree op2 = TREE_OPERAND (expr,2);
7e34c19d 866 bool side_effects = TREE_SIDE_EFFECTS (op1) || TREE_SIDE_EFFECTS (op2);
dab3247a 867 tree new_op1, new_op2;
868 if (implicit != ICV_CAST && !side_effects)
869 {
870 new_op1 = convert_to_void (op1, ICV_SECOND_OF_COND, complain);
871 new_op2 = convert_to_void (op2, ICV_THIRD_OF_COND, complain);
872 }
873 else
874 {
875 new_op1 = convert_to_void (op1, ICV_CAST, complain);
876 new_op2 = convert_to_void (op2, ICV_CAST, complain);
877 }
9031d10b 878
831d52a2 879 expr = build3 (COND_EXPR, TREE_TYPE (new_op1),
880 TREE_OPERAND (expr, 0), new_op1, new_op2);
653e5405 881 break;
b1a8d4ce 882 }
9031d10b 883
b1a8d4ce 884 case COMPOUND_EXPR:
885 {
653e5405 886 /* The second part of a compound expr contains the value. */
887 tree op1 = TREE_OPERAND (expr,1);
dab3247a 888 tree new_op1;
889 if (implicit != ICV_CAST && !TREE_NO_WARNING (expr))
890 new_op1 = convert_to_void (op1, ICV_RIGHT_OF_COMMA, complain);
891 else
892 new_op1 = convert_to_void (op1, ICV_CAST, complain);
9031d10b 893
653e5405 894 if (new_op1 != op1)
e907a2e9 895 {
831d52a2 896 tree t = build2 (COMPOUND_EXPR, TREE_TYPE (new_op1),
897 TREE_OPERAND (expr, 0), new_op1);
e907a2e9 898 expr = t;
899 }
900
653e5405 901 break;
b1a8d4ce 902 }
9031d10b 903
b1a8d4ce 904 case NON_LVALUE_EXPR:
905 case NOP_EXPR:
c0af329c 906 /* These have already decayed to rvalue. */
b1a8d4ce 907 break;
9031d10b 908
331bc0ad 909 case CALL_EXPR: /* We have a special meaning for volatile void fn(). */
b1a8d4ce 910 break;
9031d10b 911
b1a8d4ce 912 case INDIRECT_REF:
913 {
653e5405 914 tree type = TREE_TYPE (expr);
915 int is_reference = TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0)))
916 == REFERENCE_TYPE;
917 int is_volatile = TYPE_VOLATILE (type);
918 int is_complete = COMPLETE_TYPE_P (complete_type (type));
919
dc02da7f 920 /* Can't load the value if we don't know the type. */
653e5405 921 if (is_volatile && !is_complete)
ebd21de4 922 {
923 if (complain & tf_warning)
dab3247a 924 switch (implicit)
925 {
926 case ICV_CAST:
927 warning (0, "conversion to void will not access "
928 "object of incomplete type %qT", type);
929 break;
930 case ICV_SECOND_OF_COND:
931 warning (0, "indirection will not access object of "
932 "incomplete type %qT in second operand "
933 "of conditional expression", type);
934 break;
935 case ICV_THIRD_OF_COND:
936 warning (0, "indirection will not access object of "
937 "incomplete type %qT in third operand "
938 "of conditional expression", type);
939 break;
940 case ICV_RIGHT_OF_COMMA:
941 warning (0, "indirection will not access object of "
942 "incomplete type %qT in right operand of "
943 "comma operator", type);
944 break;
945 case ICV_LEFT_OF_COMMA:
946 warning (0, "indirection will not access object of "
947 "incomplete type %qT in left operand of "
948 "comma operator", type);
949 break;
950 case ICV_STATEMENT:
951 warning (0, "indirection will not access object of "
952 "incomplete type %qT in statement", type);
953 break;
954 case ICV_THIRD_IN_FOR:
955 warning (0, "indirection will not access object of "
956 "incomplete type %qT in for increment "
957 "expression", type);
958 break;
959 default:
960 gcc_unreachable ();
961 }
ebd21de4 962 }
dc02da7f 963 /* Don't load the value if this is an implicit dereference, or if
964 the type needs to be handled by ctors/dtors. */
dab3247a 965 else if (is_volatile && is_reference)
ebd21de4 966 {
967 if (complain & tf_warning)
dab3247a 968 switch (implicit)
969 {
970 case ICV_CAST:
971 warning (0, "conversion to void will not access "
972 "object of type %qT", type);
973 break;
974 case ICV_SECOND_OF_COND:
975 warning (0, "implicit dereference will not access object "
976 "of type %qT in second operand of "
977 "conditional expression", type);
978 break;
979 case ICV_THIRD_OF_COND:
980 warning (0, "implicit dereference will not access object "
981 "of type %qT in third operand of "
982 "conditional expression", type);
983 break;
984 case ICV_RIGHT_OF_COMMA:
985 warning (0, "implicit dereference will not access object "
986 "of type %qT in right operand of "
987 "comma operator", type);
988 break;
989 case ICV_LEFT_OF_COMMA:
990 warning (0, "implicit dereference will not access object "
991 "of type %qT in left operand of comma operator",
992 type);
993 break;
994 case ICV_STATEMENT:
995 warning (0, "implicit dereference will not access object "
996 "of type %qT in statement", type);
997 break;
998 case ICV_THIRD_IN_FOR:
999 warning (0, "implicit dereference will not access object "
1000 "of type %qT in for increment expression",
1001 type);
1002 break;
1003 default:
1004 gcc_unreachable ();
1005 }
ebd21de4 1006 }
dab3247a 1007 else if (is_volatile && TREE_ADDRESSABLE (type))
1008 {
1009 if (complain & tf_warning)
1010 switch (implicit)
1011 {
1012 case ICV_CAST:
1013 warning (0, "conversion to void will not access "
1014 "object of non-trivially-copyable type %qT",
1015 type);
1016 break;
1017 case ICV_SECOND_OF_COND:
1018 warning (0, "indirection will not access object of "
1019 "non-trivially-copyable type %qT in second "
1020 "operand of conditional expression", type);
1021 break;
1022 case ICV_THIRD_OF_COND:
1023 warning (0, "indirection will not access object of "
1024 "non-trivially-copyable type %qT in third "
1025 "operand of conditional expression", type);
1026 break;
1027 case ICV_RIGHT_OF_COMMA:
1028 warning (0, "indirection will not access object of "
1029 "non-trivially-copyable type %qT in right "
1030 "operand of comma operator", type);
1031 break;
1032 case ICV_LEFT_OF_COMMA:
1033 warning (0, "indirection will not access object of "
1034 "non-trivially-copyable type %qT in left "
1035 "operand of comma operator", type);
1036 break;
1037 case ICV_STATEMENT:
1038 warning (0, "indirection will not access object of "
1039 "non-trivially-copyable type %qT in statement",
1040 type);
1041 break;
1042 case ICV_THIRD_IN_FOR:
1043 warning (0, "indirection will not access object of "
1044 "non-trivially-copyable type %qT in for "
1045 "increment expression", type);
1046 break;
1047 default:
1048 gcc_unreachable ();
1049 }
1050 }
dc02da7f 1051 if (is_reference || !is_volatile || !is_complete || TREE_ADDRESSABLE (type))
a4d4030b 1052 {
1053 /* Emit a warning (if enabled) when the "effect-less" INDIRECT_REF
1054 operation is stripped off. Note that we don't warn about
1055 - an expression with TREE_NO_WARNING set. (For an example of
1056 such expressions, see build_over_call in call.c.)
1057 - automatic dereferencing of references, since the user cannot
1058 control it. (See also warn_if_unused_value() in stmt.c.) */
1059 if (warn_unused_value
dab3247a 1060 && implicit != ICV_CAST
a4d4030b 1061 && (complain & tf_warning)
1062 && !TREE_NO_WARNING (expr)
1063 && !is_reference)
1064 warning (OPT_Wunused_value, "value computed is not used");
1065 expr = TREE_OPERAND (expr, 0);
1066 }
653e5405 1067
1068 break;
b1a8d4ce 1069 }
9031d10b 1070
b1a8d4ce 1071 case VAR_DECL:
1072 {
653e5405 1073 /* External variables might be incomplete. */
1074 tree type = TREE_TYPE (expr);
1075 int is_complete = COMPLETE_TYPE_P (complete_type (type));
1076
ebd21de4 1077 if (TYPE_VOLATILE (type) && !is_complete && (complain & tf_warning))
dab3247a 1078 switch (implicit)
1079 {
1080 case ICV_CAST:
1081 warning (0, "conversion to void will not access "
1082 "object %qE of incomplete type %qT", expr, type);
1083 break;
1084 case ICV_SECOND_OF_COND:
1085 warning (0, "variable %qE of incomplete type %qT will not "
1086 "be accessed in second operand of "
1087 "conditional expression", expr, type);
1088 break;
1089 case ICV_THIRD_OF_COND:
1090 warning (0, "variable %qE of incomplete type %qT will not "
1091 "be accessed in third operand of "
1092 "conditional expression", expr, type);
1093 break;
1094 case ICV_RIGHT_OF_COMMA:
1095 warning (0, "variable %qE of incomplete type %qT will not "
1096 "be accessed in right operand of comma operator",
1097 expr, type);
1098 break;
1099 case ICV_LEFT_OF_COMMA:
1100 warning (0, "variable %qE of incomplete type %qT will not "
1101 "be accessed in left operand of comma operator",
1102 expr, type);
1103 break;
1104 case ICV_STATEMENT:
1105 warning (0, "variable %qE of incomplete type %qT will not "
1106 "be accessed in statement", expr, type);
1107 break;
1108 case ICV_THIRD_IN_FOR:
1109 warning (0, "variable %qE of incomplete type %qT will not "
1110 "be accessed in for increment expression",
1111 expr, type);
1112 break;
1113 default:
1114 gcc_unreachable ();
1115 }
1116
653e5405 1117 break;
b1a8d4ce 1118 }
d9f88785 1119
25b3017b 1120 case TARGET_EXPR:
1121 /* Don't bother with the temporary object returned from a function if
1122 we don't use it and don't need to destroy it. We'll still
1123 allocate space for it in expand_call or declare_return_variable,
1124 but we don't need to track it through all the tree phases. */
67bb1301 1125 if (TARGET_EXPR_IMPLICIT_P (expr)
25b3017b 1126 && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (expr)))
1127 {
1128 tree init = TARGET_EXPR_INITIAL (expr);
1129 if (TREE_CODE (init) == AGGR_INIT_EXPR
1130 && !AGGR_INIT_VIA_CTOR_P (init))
1131 {
c2f47e15 1132 tree fn = AGGR_INIT_EXPR_FN (init);
389dd41b 1133 expr = build_call_array_loc (input_location,
1134 TREE_TYPE (TREE_TYPE (TREE_TYPE (fn))),
1135 fn,
1136 aggr_init_expr_nargs (init),
1137 AGGR_INIT_EXPR_ARGP (init));
25b3017b 1138 }
1139 }
1140 break;
1141
b1a8d4ce 1142 default:;
1143 }
eebd67e7 1144 expr = resolve_nondeduced_context (expr);
b1a8d4ce 1145 {
1146 tree probe = expr;
9031d10b 1147
b1a8d4ce 1148 if (TREE_CODE (probe) == ADDR_EXPR)
1149 probe = TREE_OPERAND (expr, 0);
2e0e6579 1150 if (type_unknown_p (probe))
1151 {
1152 /* [over.over] enumerates the places where we can take the address
1153 of an overloaded function, and this is not one of them. */
ebd21de4 1154 if (complain & tf_error)
dab3247a 1155 switch (implicit)
1156 {
1157 case ICV_CAST:
1158 error ("conversion to void "
1159 "cannot resolve address of overloaded function");
1160 break;
1161 case ICV_SECOND_OF_COND:
1162 error ("second operand of conditional expression "
1163 "cannot resolve address of overloaded function");
1164 break;
1165 case ICV_THIRD_OF_COND:
1166 error ("third operand of conditional expression "
1167 "cannot resolve address of overloaded function");
1168 break;
1169 case ICV_RIGHT_OF_COMMA:
1170 error ("right operand of comma operator "
1171 "cannot resolve address of overloaded function");
1172 break;
1173 case ICV_LEFT_OF_COMMA:
1174 error ("left operand of comma operator "
1175 "cannot resolve address of overloaded function");
1176 break;
1177 case ICV_STATEMENT:
1178 error ("statement "
1179 "cannot resolve address of overloaded function");
1180 break;
1181 case ICV_THIRD_IN_FOR:
1182 error ("for increment expression "
1183 "cannot resolve address of overloaded function");
1184 break;
1185 }
ebd21de4 1186 else
1187 return error_mark_node;
ea720917 1188 expr = void_zero_node;
2e0e6579 1189 }
dab3247a 1190 else if (implicit != ICV_CAST && probe == expr && is_overloaded_fn (probe))
2056769e 1191 {
1192 /* Only warn when there is no &. */
ebd21de4 1193 if (complain & tf_warning)
dab3247a 1194 switch (implicit)
1195 {
1196 case ICV_SECOND_OF_COND:
1197 warning (OPT_Waddress,
1198 "second operand of conditional expression "
1199 "is a reference, not call, to function %qE", expr);
1200 break;
1201 case ICV_THIRD_OF_COND:
1202 warning (OPT_Waddress,
1203 "third operand of conditional expression "
1204 "is a reference, not call, to function %qE", expr);
1205 break;
1206 case ICV_RIGHT_OF_COMMA:
1207 warning (OPT_Waddress,
1208 "right operand of comma operator "
1209 "is a reference, not call, to function %qE", expr);
1210 break;
1211 case ICV_LEFT_OF_COMMA:
1212 warning (OPT_Waddress,
1213 "left operand of comma operator "
1214 "is a reference, not call, to function %qE", expr);
1215 break;
1216 case ICV_STATEMENT:
1217 warning (OPT_Waddress,
1218 "statement is a reference, not call, to function %qE",
1219 expr);
1220 break;
1221 case ICV_THIRD_IN_FOR:
1222 warning (OPT_Waddress,
1223 "for increment expression "
1224 "is a reference, not call, to function %qE", expr);
1225 break;
1226 default:
1227 gcc_unreachable ();
1228 }
1229
2056769e 1230 if (TREE_CODE (expr) == COMPONENT_REF)
1231 expr = TREE_OPERAND (expr, 0);
1232 }
b1a8d4ce 1233 }
9031d10b 1234
e3cfe3ce 1235 if (expr != error_mark_node && !VOID_TYPE_P (TREE_TYPE (expr)))
b1a8d4ce 1236 {
dab3247a 1237 if (implicit != ICV_CAST
5e2a4133 1238 && warn_unused_value
1239 && !TREE_NO_WARNING (expr)
1240 && !processing_template_decl)
1fe46df1 1241 {
1242 /* The middle end does not warn about expressions that have
1243 been explicitly cast to void, so we must do so here. */
ebd21de4 1244 if (!TREE_SIDE_EFFECTS (expr)) {
1245 if (complain & tf_warning)
dab3247a 1246 switch (implicit)
1247 {
1248 case ICV_SECOND_OF_COND:
1249 warning (OPT_Wunused_value,
1250 "second operand of conditional expression has no effect");
1251 break;
1252 case ICV_THIRD_OF_COND:
1253 warning (OPT_Wunused_value,
1254 "third operand of conditional expression has no effect");
1255 break;
1256 case ICV_RIGHT_OF_COMMA:
1257 warning (OPT_Wunused_value,
1258 "right operand of comma operator has no effect");
1259 break;
1260 case ICV_LEFT_OF_COMMA:
1261 warning (OPT_Wunused_value,
1262 "left operand of comma operator has no effect");
1263 break;
1264 case ICV_STATEMENT:
1265 warning (OPT_Wunused_value,
1266 "statement has no effect");
1267 break;
1268 case ICV_THIRD_IN_FOR:
1269 warning (OPT_Wunused_value,
1270 "for increment expression has no effect");
1271 break;
1272 default:
1273 gcc_unreachable ();
1274 }
ebd21de4 1275 }
9031d10b 1276 else
1277 {
1fe46df1 1278 tree e;
1279 enum tree_code code;
607a5d68 1280 enum tree_code_class tclass;
9031d10b 1281
1fe46df1 1282 e = expr;
1283 /* We might like to warn about (say) "(int) f()", as the
1284 cast has no effect, but the compiler itself will
1285 generate implicit conversions under some
324d8f2d 1286 circumstances. (For example a block copy will be
1fe46df1 1287 turned into a call to "__builtin_memcpy", with a
1288 conversion of the return value to an appropriate
1289 type.) So, to avoid false positives, we strip
416ce344 1290 conversions. Do not use STRIP_NOPs because it will
1291 not strip conversions to "void", as that is not a
1292 mode-preserving conversion. */
1293 while (TREE_CODE (e) == NOP_EXPR)
1294 e = TREE_OPERAND (e, 0);
1fe46df1 1295
1296 code = TREE_CODE (e);
607a5d68 1297 tclass = TREE_CODE_CLASS (code);
1298 if ((tclass == tcc_comparison
1299 || tclass == tcc_unary
1300 || (tclass == tcc_binary
1fe46df1 1301 && !(code == MODIFY_EXPR
1302 || code == INIT_EXPR
1303 || code == PREDECREMENT_EXPR
1304 || code == PREINCREMENT_EXPR
1305 || code == POSTDECREMENT_EXPR
1306 || code == POSTINCREMENT_EXPR)))
ebd21de4 1307 && (complain & tf_warning))
ced7c954 1308 warning (OPT_Wunused_value, "value computed is not used");
1fe46df1 1309 }
1310 }
00fa9079 1311 expr = build1 (CONVERT_EXPR, void_type_node, expr);
b1a8d4ce 1312 }
de5ad4cb 1313 if (! TREE_SIDE_EFFECTS (expr))
1314 expr = void_zero_node;
b1a8d4ce 1315 return expr;
1316}
1317
d81e00a4 1318/* Create an expression whose value is that of EXPR,
1319 converted to type TYPE. The TREE_TYPE of the value
1320 is always TYPE. This function implements all reasonable
1321 conversions; callers should filter out those that are
c4a8ac95 1322 not permitted by the language being compiled.
1323
1324 Most of this routine is from build_reinterpret_cast.
1325
a17c2a3a 1326 The back end cannot call cp_convert (what was convert) because
c4a8ac95 1327 conversions to/from basetypes may involve memory references
1328 (vbases) and adding or subtracting small values (multiple
1329 inheritance), but it calls convert from the constant folding code
e0aa5007 1330 on subtrees of already built trees after it has ripped them apart.
c4a8ac95 1331
1332 Also, if we ever support range variables, we'll probably also have to
1333 do a little bit more work. */
d81e00a4 1334
1335tree
35771a9a 1336convert (tree type, tree expr)
d81e00a4 1337{
c4a8ac95 1338 tree intype;
1339
1340 if (type == error_mark_node || expr == error_mark_node)
1341 return error_mark_node;
1342
c4a8ac95 1343 intype = TREE_TYPE (expr);
1344
6686e84d 1345 if (POINTER_TYPE_P (type) && POINTER_TYPE_P (intype))
13f0eb20 1346 return fold_if_not_in_template (build_nop (type, expr));
c4a8ac95 1347
1348 return ocp_convert (type, expr, CONV_OLD_CONVERT,
1349 LOOKUP_NORMAL|LOOKUP_NO_CONVERSION);
d81e00a4 1350}
1351
c4a8ac95 1352/* Like cp_convert, except permit conversions to take place which
471086d6 1353 are not normally allowed due to access restrictions
1354 (such as conversion from sub-type to private super-type). */
96624a9e 1355
471086d6 1356tree
35771a9a 1357convert_force (tree type, tree expr, int convtype)
471086d6 1358{
cd16867a 1359 tree e = expr;
1360 enum tree_code code = TREE_CODE (type);
471086d6 1361
1362 if (code == REFERENCE_TYPE)
9031d10b 1363 return (fold_if_not_in_template
5d7ed6c7 1364 (convert_to_reference (type, e, CONV_C_CAST, LOOKUP_COMPLAIN,
1365 NULL_TREE)));
471086d6 1366
1367 if (code == POINTER_TYPE)
5d7ed6c7 1368 return fold_if_not_in_template (convert_to_pointer_force (type, e));
471086d6 1369
ac9386a0 1370 /* From typeck.c convert_for_assignment */
471086d6 1371 if (((TREE_CODE (TREE_TYPE (e)) == POINTER_TYPE && TREE_CODE (e) == ADDR_EXPR
1372 && TREE_CODE (TREE_TYPE (e)) == POINTER_TYPE
1373 && TREE_CODE (TREE_TYPE (TREE_TYPE (e))) == METHOD_TYPE)
ac9386a0 1374 || integer_zerop (e)
1375 || TYPE_PTRMEMFUNC_P (TREE_TYPE (e)))
471086d6 1376 && TYPE_PTRMEMFUNC_P (type))
cb02169c 1377 /* compatible pointer to member functions. */
1378 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), e, 1,
85132186 1379 /*c_cast_p=*/1, tf_warning_or_error);
a74e8896 1380
c4a8ac95 1381 return ocp_convert (type, e, CONV_C_CAST|convtype, LOOKUP_NORMAL);
471086d6 1382}
1383
471086d6 1384/* Convert an aggregate EXPR to type XTYPE. If a conversion
1385 exists, return the attempted conversion. This may
1386 return ERROR_MARK_NODE if the conversion is not
1387 allowed (references private members, etc).
1388 If no conversion exists, NULL_TREE is returned.
1389
ce28ee2e 1390 FIXME: Ambiguity checking is wrong. Should choose one by the implicit
1391 object parameter, or by the second standard conversion sequence if
1392 that doesn't do it. This will probably wait for an overloading rewrite.
1393 (jason 8/9/95) */
471086d6 1394
ef9571e5 1395static tree
8999978b 1396build_type_conversion (tree xtype, tree expr)
471086d6 1397{
1398 /* C++: check to see if we can convert this aggregate type
bcf789d7 1399 into the required type. */
8999978b 1400 return build_user_type_conversion (xtype, expr, LOOKUP_NORMAL);
471086d6 1401}
1402
1e66592c 1403/* Convert the given EXPR to one of a group of types suitable for use in an
1404 expression. DESIRES is a combination of various WANT_* flags (q.v.)
35771a9a 1405 which indicates which types are suitable. If COMPLAIN is true, complain
1e66592c 1406 about ambiguity; otherwise, the caller will deal with it. */
471086d6 1407
1e66592c 1408tree
35771a9a 1409build_expr_type_conversion (int desires, tree expr, bool complain)
471086d6 1410{
1e66592c 1411 tree basetype = TREE_TYPE (expr);
6a44e72e 1412 tree conv = NULL_TREE;
bdd152ce 1413 tree winner = NULL_TREE;
471086d6 1414
9031d10b 1415 if (expr == null_node
1416 && (desires & WANT_INT)
954885ed 1417 && !(desires & WANT_NULL))
8296fdaa 1418 warning_at (input_location, OPT_Wconversion_null,
1419 "converting NULL to non-pointer type");
9031d10b 1420
ce28ee2e 1421 basetype = TREE_TYPE (expr);
471086d6 1422
3d411d73 1423 if (basetype == error_mark_node)
1424 return error_mark_node;
1425
95397ff9 1426 if (! MAYBE_CLASS_TYPE_P (basetype))
bdd152ce 1427 switch (TREE_CODE (basetype))
1428 {
1429 case INTEGER_TYPE:
954885ed 1430 if ((desires & WANT_NULL) && null_ptr_cst_p (expr))
bdd152ce 1431 return expr;
1432 /* else fall through... */
1433
1434 case BOOLEAN_TYPE:
1435 return (desires & WANT_INT) ? expr : NULL_TREE;
1436 case ENUMERAL_TYPE:
1437 return (desires & WANT_ENUM) ? expr : NULL_TREE;
1438 case REAL_TYPE:
1439 return (desires & WANT_FLOAT) ? expr : NULL_TREE;
1440 case POINTER_TYPE:
1441 return (desires & WANT_POINTER) ? expr : NULL_TREE;
9031d10b 1442
bdd152ce 1443 case FUNCTION_TYPE:
1444 case ARRAY_TYPE:
a681799d 1445 return (desires & WANT_POINTER) ? decay_conversion (expr)
653e5405 1446 : NULL_TREE;
96ec9e30 1447
64ec1ad6 1448 case COMPLEX_TYPE:
96ec9e30 1449 case VECTOR_TYPE:
64ec1ad6 1450 if ((desires & WANT_VECTOR_OR_COMPLEX) == 0)
96ec9e30 1451 return NULL_TREE;
1452 switch (TREE_CODE (TREE_TYPE (basetype)))
1453 {
1454 case INTEGER_TYPE:
1455 case BOOLEAN_TYPE:
1456 return (desires & WANT_INT) ? expr : NULL_TREE;
1457 case ENUMERAL_TYPE:
1458 return (desires & WANT_ENUM) ? expr : NULL_TREE;
1459 case REAL_TYPE:
1460 return (desires & WANT_FLOAT) ? expr : NULL_TREE;
1461 default:
1462 return NULL_TREE;
1463 }
1464
bdd152ce 1465 default:
1466 return NULL_TREE;
1467 }
1468
1469 /* The code for conversions from class type is currently only used for
1470 delete expressions. Other expressions are handled by build_new_op. */
f8182e08 1471 if (!complete_type_or_else (basetype, expr))
1472 return error_mark_node;
1473 if (!TYPE_HAS_CONVERSION (basetype))
bdd152ce 1474 return NULL_TREE;
1475
8972df28 1476 for (conv = lookup_conversions (basetype, /*lookup_template_convs_p=*/true);
1477 conv;
1478 conv = TREE_CHAIN (conv))
bdd152ce 1479 {
1480 int win = 0;
1481 tree candidate;
1482 tree cand = TREE_VALUE (conv);
c1ca2f12 1483 cand = OVL_CURRENT (cand);
bdd152ce 1484
1485 if (winner && winner == cand)
1486 continue;
1487
cf7aa2e5 1488 if (DECL_NONCONVERTING_P (cand))
1489 continue;
1490
ef4534a3 1491 candidate = non_reference (TREE_TYPE (TREE_TYPE (cand)));
bdd152ce 1492
1493 switch (TREE_CODE (candidate))
1494 {
1495 case BOOLEAN_TYPE:
1496 case INTEGER_TYPE:
1497 win = (desires & WANT_INT); break;
1498 case ENUMERAL_TYPE:
1499 win = (desires & WANT_ENUM); break;
1500 case REAL_TYPE:
1501 win = (desires & WANT_FLOAT); break;
1502 case POINTER_TYPE:
1503 win = (desires & WANT_POINTER); break;
1504
64ec1ad6 1505 case COMPLEX_TYPE:
96ec9e30 1506 case VECTOR_TYPE:
64ec1ad6 1507 if ((desires & WANT_VECTOR_OR_COMPLEX) == 0)
96ec9e30 1508 break;
1509 switch (TREE_CODE (TREE_TYPE (candidate)))
1510 {
1511 case BOOLEAN_TYPE:
1512 case INTEGER_TYPE:
1513 win = (desires & WANT_INT); break;
1514 case ENUMERAL_TYPE:
1515 win = (desires & WANT_ENUM); break;
1516 case REAL_TYPE:
1517 win = (desires & WANT_FLOAT); break;
1518 default:
1519 break;
1520 }
1521 break;
1522
bdd152ce 1523 default:
1524 break;
1525 }
1526
1527 if (win)
1528 {
1529 if (winner)
1530 {
1531 if (complain)
1532 {
0a25aad6 1533 error ("ambiguous default type conversion from %qT",
653e5405 1534 basetype);
0a25aad6 1535 error (" candidate conversions include %qD and %qD",
653e5405 1536 winner, cand);
bdd152ce 1537 }
1538 return error_mark_node;
1539 }
1540 else
1541 winner = cand;
1542 }
1543 }
1e66592c 1544
bdd152ce 1545 if (winner)
1546 {
ef4534a3 1547 tree type = non_reference (TREE_TYPE (TREE_TYPE (winner)));
bdd152ce 1548 return build_user_type_conversion (type, expr, LOOKUP_NORMAL);
471086d6 1549 }
1e66592c 1550
985e9ee0 1551 return NULL_TREE;
471086d6 1552}
bc3887cf 1553
96624a9e 1554/* Implements integral promotion (4.1) and float->double promotion. */
1555
bc3887cf 1556tree
35771a9a 1557type_promotes_to (tree type)
bc3887cf 1558{
2b3c93a3 1559 tree promoted_type;
1560
ce28ee2e 1561 if (type == error_mark_node)
1562 return error_mark_node;
1563
bc3887cf 1564 type = TYPE_MAIN_VARIANT (type);
bb0726a1 1565
2b3c93a3 1566 /* Check for promotions of target-defined types first. */
1567 promoted_type = targetm.promoted_type (type);
1568 if (promoted_type)
1569 return promoted_type;
1570
bb0726a1 1571 /* bool always promotes to int (not unsigned), even if it's the same
1572 size. */
9e1a8234 1573 if (TREE_CODE (type) == BOOLEAN_TYPE)
bb0726a1 1574 type = integer_type_node;
1575
1576 /* Normally convert enums to int, but convert wide enums to something
1577 wider. */
1578 else if (TREE_CODE (type) == ENUMERAL_TYPE
924bbf02 1579 || type == char16_type_node
1580 || type == char32_type_node
bb0726a1 1581 || type == wchar_type_node)
6c9497b1 1582 {
1583 int precision = MAX (TYPE_PRECISION (type),
1584 TYPE_PRECISION (integer_type_node));
771d21fa 1585 tree totype = c_common_type_for_size (precision, 0);
bb9c1a09 1586 if (TREE_CODE (type) == ENUMERAL_TYPE)
1587 type = ENUM_UNDERLYING_TYPE (type);
78a8ed03 1588 if (TYPE_UNSIGNED (type)
6c9497b1 1589 && ! int_fits_type_p (TYPE_MAX_VALUE (type), totype))
771d21fa 1590 type = c_common_type_for_size (precision, 1);
6c9497b1 1591 else
1592 type = totype;
1593 }
d7aeef06 1594 else if (c_promoting_integer_type_p (type))
bc3887cf 1595 {
d0acef9e 1596 /* Retain unsignedness if really not getting bigger. */
78a8ed03 1597 if (TYPE_UNSIGNED (type)
d0acef9e 1598 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
bc3887cf 1599 type = unsigned_type_node;
1600 else
1601 type = integer_type_node;
1602 }
1603 else if (type == float_type_node)
1604 type = double_type_node;
9031d10b 1605
a681799d 1606 return type;
bc3887cf 1607}
668ae905 1608
668ae905 1609/* The routines below this point are carefully written to conform to
1610 the standard. They use the same terminology, and follow the rules
1611 closely. Although they are used only in pt.c at the moment, they
1612 should presumably be used everywhere in the future. */
1613
1146f179 1614/* Attempt to perform qualification conversions on EXPR to convert it
1615 to TYPE. Return the resulting expression, or error_mark_node if
1616 the conversion was impossible. */
1617
9031d10b 1618tree
35771a9a 1619perform_qualification_conversions (tree type, tree expr)
668ae905 1620{
1bc16cab 1621 tree expr_type;
1622
1623 expr_type = TREE_TYPE (expr);
1624
3b087710 1625 if (same_type_p (type, expr_type))
1626 return expr;
1627 else if (TYPE_PTR_P (type) && TYPE_PTR_P (expr_type)
1628 && comp_ptr_ttypes (TREE_TYPE (type), TREE_TYPE (expr_type)))
1bc16cab 1629 return build_nop (type, expr);
1630 else if (TYPE_PTR_TO_MEMBER_P (type)
1631 && TYPE_PTR_TO_MEMBER_P (expr_type)
1632 && same_type_p (TYPE_PTRMEM_CLASS_TYPE (type),
1633 TYPE_PTRMEM_CLASS_TYPE (expr_type))
1634 && comp_ptr_ttypes (TYPE_PTRMEM_POINTED_TO_TYPE (type),
1635 TYPE_PTRMEM_POINTED_TO_TYPE (expr_type)))
1636 return build_nop (type, expr);
1146f179 1637 else
1638 return error_mark_node;
668ae905 1639}