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