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