Home | History | Annotate | Line # | Download | only in cp
      1 /* Language-level data type conversion for GNU C++.
      2    Copyright (C) 1987-2024 Free Software Foundation, Inc.
      3    Hacked by Michael Tiemann (tiemann (at) cygnus.com)
      4 
      5 This file is part of GCC.
      6 
      7 GCC is free software; you can redistribute it and/or modify
      8 it under the terms of the GNU General Public License as published by
      9 the Free Software Foundation; either version 3, or (at your option)
     10 any later version.
     11 
     12 GCC is distributed in the hope that it will be useful,
     13 but WITHOUT ANY WARRANTY; without even the implied warranty of
     14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     15 GNU General Public License for more details.
     16 
     17 You should have received a copy of the GNU General Public License
     18 along with GCC; see the file COPYING3.  If not see
     19 <http://www.gnu.org/licenses/>.  */
     20 
     21 
     22 /* This file contains the functions for converting C++ expressions
     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"
     28 #include "system.h"
     29 #include "coretypes.h"
     30 #include "target.h"
     31 #include "cp-tree.h"
     32 #include "stor-layout.h"
     33 #include "flags.h"
     34 #include "intl.h"
     35 #include "convert.h"
     36 #include "stringpool.h"
     37 #include "attribs.h"
     38 #include "escaped_string.h"
     39 
     40 static tree convert_to_pointer_force (tree, tree, tsubst_flags_t);
     41 static tree build_type_conversion (tree, tree);
     42 static tree build_up_reference (tree, tree, int, tree, tsubst_flags_t);
     43 static void diagnose_ref_binding (location_t, tree, tree, tree);
     44 
     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.cc, convert_to_integer[_maybe_fold].
     56      In c-typeck.cc, build_binary_op_nodefault (boolean ops),
     57 	and c_common_truthvalue_conversion.
     58      In expr.cc: expand_expr, for operands of a MULT_EXPR.
     59      In fold-const.cc: fold.
     60      In tree.cc: 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 
     65 /* Subroutines of `convert'.  */
     67 
     68 /* if converting pointer to pointer
     69      if dealing with classes, check for derived->base or vice versa
     70      else if dealing with method pointers, delegate
     71      else convert blindly
     72    else if converting class, pass off to build_type_conversion
     73    else try C-style pointer conversion.  */
     74 
     75 static tree
     76 cp_convert_to_pointer (tree type, tree expr, bool dofold,
     77 		       tsubst_flags_t complain)
     78 {
     79   tree intype = TREE_TYPE (expr);
     80   enum tree_code form;
     81   tree rval;
     82   location_t loc = cp_expr_loc_or_input_loc (expr);
     83 
     84   if (intype == error_mark_node)
     85     return error_mark_node;
     86 
     87   if (MAYBE_CLASS_TYPE_P (intype))
     88     {
     89       intype = complete_type (intype);
     90       if (!COMPLETE_TYPE_P (intype))
     91 	{
     92 	  if (complain & tf_error)
     93 	    error_at (loc, "cannot convert from incomplete type %qH to %qI",
     94 		      intype, type);
     95 	  return error_mark_node;
     96 	}
     97 
     98       rval = build_type_conversion (type, expr);
     99       if (rval)
    100 	{
    101 	  if ((complain & tf_error)
    102 	      && rval == error_mark_node)
    103 	    error_at (loc, "conversion of %qE from %qH to %qI is ambiguous",
    104 		      expr, intype, type);
    105 	  return rval;
    106 	}
    107     }
    108 
    109   /* Handle anachronistic conversions from (::*)() to cv void* or (*)().  */
    110   if (TYPE_PTR_P (type)
    111       && (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
    112 	  || VOID_TYPE_P (TREE_TYPE (type))))
    113     {
    114       if (TYPE_PTRMEMFUNC_P (intype)
    115 	  || TREE_CODE (intype) == METHOD_TYPE)
    116 	return convert_member_func_to_ptr (type, expr, complain);
    117       if (TYPE_PTR_P (TREE_TYPE (expr)))
    118 	return build_nop (type, expr);
    119       intype = TREE_TYPE (expr);
    120     }
    121 
    122   if (expr == error_mark_node)
    123     return error_mark_node;
    124 
    125   form = TREE_CODE (intype);
    126 
    127   if (INDIRECT_TYPE_P (intype))
    128     {
    129       intype = TYPE_MAIN_VARIANT (intype);
    130 
    131       if (TYPE_MAIN_VARIANT (type) != intype
    132 	  && TYPE_PTR_P (type)
    133 	  && TREE_CODE (TREE_TYPE (type)) == RECORD_TYPE
    134 	  && MAYBE_CLASS_TYPE_P (TREE_TYPE (type))
    135 	  && MAYBE_CLASS_TYPE_P (TREE_TYPE (intype))
    136 	  && TREE_CODE (TREE_TYPE (intype)) == RECORD_TYPE)
    137 	{
    138 	  enum tree_code code = PLUS_EXPR;
    139 	  tree binfo;
    140 	  tree intype_class;
    141 	  tree type_class;
    142 	  bool same_p;
    143 
    144 	  intype_class = TREE_TYPE (intype);
    145 	  type_class = TREE_TYPE (type);
    146 
    147 	  same_p = same_type_p (TYPE_MAIN_VARIANT (intype_class),
    148 				TYPE_MAIN_VARIANT (type_class));
    149 	  binfo = NULL_TREE;
    150 	  /* Try derived to base conversion.  */
    151 	  if (!same_p)
    152 	    binfo = lookup_base (intype_class, type_class, ba_check,
    153 				 NULL, complain);
    154 	  if (!same_p && !binfo)
    155 	    {
    156 	      /* Try base to derived conversion.  */
    157 	      binfo = lookup_base (type_class, intype_class, ba_check,
    158 				   NULL, complain);
    159 	      code = MINUS_EXPR;
    160 	    }
    161 	  if (binfo == error_mark_node)
    162 	    return error_mark_node;
    163 	  if (binfo || same_p)
    164 	    {
    165 	      if (binfo)
    166 		expr = build_base_path (code, expr, binfo, 0, complain);
    167 	      /* Add any qualifier conversions.  */
    168 	      return build_nop (type, expr);
    169 	    }
    170 	}
    171 
    172       if (TYPE_PTRMEMFUNC_P (type))
    173 	{
    174 	  if (complain & tf_error)
    175 	    error_at (loc, "cannot convert %qE from type %qH to type %qI",
    176 		      expr, intype, type);
    177 	  return error_mark_node;
    178 	}
    179 
    180       return build_nop (type, expr);
    181     }
    182   else if ((TYPE_PTRDATAMEM_P (type) && TYPE_PTRDATAMEM_P (intype))
    183 	   || (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype)))
    184     return convert_ptrmem (type, expr, /*allow_inverse_p=*/false,
    185 			   /*c_cast_p=*/false, complain);
    186   else if (TYPE_PTRMEMFUNC_P (intype))
    187     {
    188       if (!warn_pmf2ptr)
    189 	{
    190 	  if (TREE_CODE (expr) == PTRMEM_CST)
    191 	    return cp_convert_to_pointer (type, PTRMEM_CST_MEMBER (expr),
    192 					  dofold, complain);
    193 	  else if (TREE_CODE (expr) == OFFSET_REF)
    194 	    {
    195 	      tree object = TREE_OPERAND (expr, 0);
    196 	      return get_member_function_from_ptrfunc (&object,
    197 						       TREE_OPERAND (expr, 1),
    198 						       complain);
    199 	    }
    200 	}
    201       if (complain & tf_error)
    202 	error_at (loc, "cannot convert %qE from type %qH to type %qI",
    203 		  expr, intype, type);
    204       return error_mark_node;
    205     }
    206 
    207   if (null_ptr_cst_p (expr))
    208     {
    209       if (TYPE_PTRMEMFUNC_P (type))
    210 	return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), expr, 0,
    211 				 /*c_cast_p=*/false, complain);
    212 
    213       if (complain & tf_warning)
    214 	maybe_warn_zero_as_null_pointer_constant (expr, loc);
    215 
    216       /* A NULL pointer-to-data-member is represented by -1, not by
    217 	 zero.  */
    218       tree val = (TYPE_PTRDATAMEM_P (type)
    219 		  ? build_int_cst_type (type, -1)
    220 		  : build_int_cst (type, 0));
    221 
    222       return (TREE_SIDE_EFFECTS (expr)
    223 	      ? build2 (COMPOUND_EXPR, type, expr, val) : val);
    224     }
    225   else if (TYPE_PTRMEM_P (type) && INTEGRAL_CODE_P (form))
    226     {
    227       if (complain & tf_error)
    228 	error_at (loc, "invalid conversion from %qH to %qI", intype, type);
    229       return error_mark_node;
    230     }
    231 
    232   if (INTEGRAL_CODE_P (form))
    233     {
    234       if (TYPE_PRECISION (intype) == POINTER_SIZE)
    235 	return build1 (CONVERT_EXPR, type, expr);
    236       expr = cp_convert (c_common_type_for_size (TYPE_PRECISION (type), 0), expr,
    237 			 complain);
    238       /* Modes may be different but sizes should be the same.  There
    239 	 is supposed to be some integral type that is the same width
    240 	 as a pointer.  */
    241       gcc_assert (GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (TREE_TYPE (expr)))
    242 		  == GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type)));
    243 
    244       /* FIXME needed because convert_to_pointer_maybe_fold still folds
    245 	 conversion of constants.  */
    246       if (!dofold)
    247 	return build1 (CONVERT_EXPR, type, expr);
    248 
    249       return convert_to_pointer_maybe_fold (type, expr, dofold);
    250     }
    251 
    252   if (type_unknown_p (expr))
    253     return instantiate_type (type, expr, complain);
    254 
    255   if (complain & tf_error)
    256     error_at (loc, "cannot convert %qE from type %qH to type %qI",
    257 	      expr, intype, type);
    258   return error_mark_node;
    259 }
    260 
    261 /* Like convert, except permit conversions to take place which
    262    are not normally allowed due to access restrictions
    263    (such as conversion from sub-type to private super-type).  */
    264 
    265 static tree
    266 convert_to_pointer_force (tree type, tree expr, tsubst_flags_t complain)
    267 {
    268   tree intype = TREE_TYPE (expr);
    269   enum tree_code form = TREE_CODE (intype);
    270 
    271   if (form == POINTER_TYPE)
    272     {
    273       intype = TYPE_MAIN_VARIANT (intype);
    274 
    275       if (TYPE_MAIN_VARIANT (type) != intype
    276 	  && TREE_CODE (TREE_TYPE (type)) == RECORD_TYPE
    277 	  && MAYBE_CLASS_TYPE_P (TREE_TYPE (type))
    278 	  && MAYBE_CLASS_TYPE_P (TREE_TYPE (intype))
    279 	  && TREE_CODE (TREE_TYPE (intype)) == RECORD_TYPE)
    280 	{
    281 	  enum tree_code code = PLUS_EXPR;
    282 	  tree binfo;
    283 
    284 	  binfo = lookup_base (TREE_TYPE (intype), TREE_TYPE (type),
    285 			       ba_unique, NULL, complain);
    286 	  if (!binfo)
    287 	    {
    288 	      binfo = lookup_base (TREE_TYPE (type), TREE_TYPE (intype),
    289 				   ba_unique, NULL, complain);
    290 	      code = MINUS_EXPR;
    291 	    }
    292 	  if (binfo == error_mark_node)
    293 	    return error_mark_node;
    294 	  if (binfo)
    295 	    {
    296 	      expr = build_base_path (code, expr, binfo, 0, complain);
    297 	      if (expr == error_mark_node)
    298 		 return error_mark_node;
    299 	      /* Add any qualifier conversions.  */
    300 	      if (!same_type_p (TREE_TYPE (TREE_TYPE (expr)),
    301 				TREE_TYPE (type)))
    302 		expr = build_nop (type, expr);
    303 	      return expr;
    304 	    }
    305 	}
    306     }
    307 
    308   return cp_convert_to_pointer (type, expr, /*fold*/false, complain);
    309 }
    310 
    311 /* We are passing something to a function which requires a reference.
    312    The type we are interested in is in TYPE. The initial
    313    value we have to begin with is in ARG.
    314 
    315    FLAGS controls how we manage access checking.
    316    DIRECT_BIND in FLAGS controls how any temporaries are generated.
    317      If DIRECT_BIND is set, DECL is the reference we're binding to.  */
    318 
    319 static tree
    320 build_up_reference (tree type, tree arg, int flags, tree decl,
    321 		    tsubst_flags_t complain)
    322 {
    323   tree rval;
    324   tree argtype = TREE_TYPE (arg);
    325   tree target_type = TREE_TYPE (type);
    326 
    327   gcc_assert (TYPE_REF_P (type));
    328 
    329   if ((flags & DIRECT_BIND) && ! lvalue_p (arg))
    330     {
    331       /* Create a new temporary variable.  We can't just use a TARGET_EXPR
    332 	 here because it needs to live as long as DECL.  */
    333       tree targ = arg;
    334 
    335       arg = make_temporary_var_for_ref_to_temp (decl, target_type);
    336 
    337       /* Process the initializer for the declaration.  */
    338       DECL_INITIAL (arg) = targ;
    339       cp_finish_decl (arg, targ, /*init_const_expr_p=*/false, NULL_TREE,
    340 		      LOOKUP_ONLYCONVERTING|DIRECT_BIND);
    341     }
    342   else if (!(flags & DIRECT_BIND) && ! obvalue_p (arg))
    343     return get_target_expr (arg, complain);
    344 
    345   /* If we had a way to wrap this up, and say, if we ever needed its
    346      address, transform all occurrences of the register, into a memory
    347      reference we could win better.  */
    348   rval = cp_build_addr_expr (arg, complain);
    349   if (rval == error_mark_node)
    350     return error_mark_node;
    351 
    352   if ((flags & LOOKUP_PROTECT)
    353       && TYPE_MAIN_VARIANT (argtype) != TYPE_MAIN_VARIANT (target_type)
    354       && MAYBE_CLASS_TYPE_P (argtype)
    355       && MAYBE_CLASS_TYPE_P (target_type))
    356     {
    357       /* We go through lookup_base for the access control.  */
    358       tree binfo = lookup_base (argtype, target_type, ba_check,
    359 				NULL, complain);
    360       if (binfo == error_mark_node)
    361 	return error_mark_node;
    362       if (binfo == NULL_TREE)
    363 	return error_not_base_type (target_type, argtype);
    364       rval = build_base_path (PLUS_EXPR, rval, binfo, 1, complain);
    365     }
    366   else
    367     rval
    368       = convert_to_pointer_force (build_pointer_type (target_type),
    369 				  rval, complain);
    370   return build_nop (type, rval);
    371 }
    372 
    373 /* Subroutine of convert_to_reference. REFTYPE is the target reference type.
    374    INTYPE is the original rvalue type and DECL is an optional _DECL node
    375    for diagnostics.
    376 
    377    [dcl.init.ref] says that if an rvalue is used to
    378    initialize a reference, then the reference must be to a
    379    non-volatile const type.  */
    380 
    381 static void
    382 diagnose_ref_binding (location_t loc, tree reftype, tree intype, tree decl)
    383 {
    384   tree ttl = TREE_TYPE (reftype);
    385 
    386   if (!TYPE_REF_IS_RVALUE (reftype)
    387       && !CP_TYPE_CONST_NON_VOLATILE_P (ttl))
    388     {
    389       const char *msg;
    390 
    391       if (CP_TYPE_VOLATILE_P (ttl) && decl)
    392 	msg = G_("initialization of volatile reference type %q#T from "
    393 	         "rvalue of type %qT");
    394       else if (CP_TYPE_VOLATILE_P (ttl))
    395 	msg = G_("conversion to volatile reference type %q#T "
    396 	         "from rvalue of type %qT");
    397       else if (decl)
    398 	msg = G_("initialization of non-const reference type %q#T from "
    399 	         "rvalue of type %qT");
    400       else
    401 	msg = G_("conversion to non-const reference type %q#T from "
    402 	         "rvalue of type %qT");
    403 
    404       permerror (loc, msg, reftype, intype);
    405     }
    406 }
    407 
    408 /* For C++: Only need to do one-level references, but cannot
    409    get tripped up on signed/unsigned differences.
    410 
    411    DECL is either NULL_TREE or the _DECL node for a reference that is being
    412    initialized.  It can be error_mark_node if we don't know the _DECL but
    413    we know it's an initialization.  */
    414 
    415 tree
    416 convert_to_reference (tree reftype, tree expr, int convtype,
    417 		      int flags, tree decl, tsubst_flags_t complain)
    418 {
    419   tree type = TYPE_MAIN_VARIANT (TREE_TYPE (reftype));
    420   tree intype;
    421   tree rval = NULL_TREE;
    422   tree rval_as_conversion = NULL_TREE;
    423   bool can_convert_intype_to_type;
    424   location_t loc = cp_expr_loc_or_input_loc (expr);
    425 
    426   if (TREE_CODE (type) == FUNCTION_TYPE
    427       && TREE_TYPE (expr) == unknown_type_node)
    428     expr = instantiate_type (type, expr, complain);
    429 
    430   if (expr == error_mark_node)
    431     return error_mark_node;
    432 
    433   intype = TREE_TYPE (expr);
    434 
    435   gcc_assert (!TYPE_REF_P (intype));
    436   gcc_assert (TYPE_REF_P (reftype));
    437 
    438   intype = TYPE_MAIN_VARIANT (intype);
    439 
    440   can_convert_intype_to_type = can_convert_standard (type, intype, complain);
    441 
    442   if (!can_convert_intype_to_type
    443       && (convtype & CONV_IMPLICIT) && MAYBE_CLASS_TYPE_P (intype)
    444       && ! (flags & LOOKUP_NO_CONVERSION))
    445     {
    446       /* Look for a user-defined conversion to lvalue that we can use.  */
    447 
    448       rval_as_conversion
    449 	= build_type_conversion (reftype, expr);
    450 
    451       if (rval_as_conversion && rval_as_conversion != error_mark_node
    452 	  && lvalue_p (rval_as_conversion))
    453 	{
    454 	  expr = rval_as_conversion;
    455 	  rval_as_conversion = NULL_TREE;
    456 	  intype = type;
    457 	  can_convert_intype_to_type = 1;
    458 	}
    459     }
    460 
    461   if (((convtype & CONV_STATIC)
    462        && can_convert_standard (intype, type, complain))
    463       || ((convtype & CONV_IMPLICIT) && can_convert_intype_to_type))
    464     {
    465       {
    466 	tree ttl = TREE_TYPE (reftype);
    467 	tree ttr = lvalue_type (expr);
    468 
    469 	if ((complain & tf_error)
    470 	    && ! lvalue_p (expr))
    471 	  diagnose_ref_binding (loc, reftype, intype, decl);
    472 
    473 	if (! (convtype & CONV_CONST)
    474 	    && !at_least_as_qualified_p (ttl, ttr))
    475 	  {
    476 	    if (complain & tf_error)
    477 	      permerror (loc, "conversion from %qH to %qI discards qualifiers",
    478 			 ttr, reftype);
    479 	    else
    480 	      return error_mark_node;
    481 	  }
    482       }
    483 
    484       return build_up_reference (reftype, expr, flags, decl, complain);
    485     }
    486   else if ((convtype & CONV_REINTERPRET) && obvalue_p (expr))
    487     {
    488       /* When casting an lvalue to a reference type, just convert into
    489 	 a pointer to the new type and deference it.  This is allowed
    490 	 by San Diego WP section 5.2.9 paragraph 12, though perhaps it
    491 	 should be done directly (jason).  (int &)ri ---> *(int*)&ri */
    492 
    493       /* B* bp; A& ar = (A&)bp; is valid, but it's probably not what they
    494 	 meant.  */
    495       if ((complain & tf_warning)
    496 	  && TYPE_PTR_P (intype)
    497 	  && (comptypes (TREE_TYPE (intype), type,
    498 			 COMPARE_BASE | COMPARE_DERIVED)))
    499 	warning_at (loc, 0, "casting %qT to %qT does not dereference pointer",
    500 		    intype, reftype);
    501 
    502       rval = cp_build_addr_expr (expr, complain);
    503       if (rval != error_mark_node)
    504 	rval = convert_force (build_pointer_type (TREE_TYPE (reftype)),
    505 			      rval, 0, complain);
    506       if (rval != error_mark_node)
    507 	rval = build1 (NOP_EXPR, reftype, rval);
    508     }
    509   else
    510     {
    511       rval = convert_for_initialization (NULL_TREE, type, expr, flags,
    512 					 ICR_CONVERTING, 0, 0, complain);
    513       if (rval == NULL_TREE || rval == error_mark_node)
    514 	return rval;
    515       if (complain & tf_error)
    516 	diagnose_ref_binding (loc, reftype, intype, decl);
    517       rval = build_up_reference (reftype, rval, flags, decl, complain);
    518     }
    519 
    520   if (rval)
    521     {
    522       /* If we found a way to convert earlier, then use it.  */
    523       return rval;
    524     }
    525 
    526   if (complain & tf_error)
    527     error_at (loc, "cannot convert type %qH to type %qI", intype, reftype);
    528 
    529   return error_mark_node;
    530 }
    531 
    532 /* We are using a reference VAL for its value. Bash that reference all the
    533    way down to its lowest form.  */
    534 
    535 tree
    536 convert_from_reference (tree val)
    537 {
    538   if (TREE_TYPE (val)
    539       && TYPE_REF_P (TREE_TYPE (val)))
    540     {
    541       tree t = TREE_TYPE (TREE_TYPE (val));
    542       tree ref = build1 (INDIRECT_REF, t, val);
    543 
    544       mark_exp_read (val);
    545        /* We *must* set TREE_READONLY when dereferencing a pointer to const,
    546 	  so that we get the proper error message if the result is used
    547 	  to assign to.  Also, &* is supposed to be a no-op.  */
    548       TREE_READONLY (ref) = CP_TYPE_CONST_P (t);
    549       TREE_THIS_VOLATILE (ref) = CP_TYPE_VOLATILE_P (t);
    550       TREE_SIDE_EFFECTS (ref)
    551 	= (TREE_THIS_VOLATILE (ref) || TREE_SIDE_EFFECTS (val));
    552       val = ref;
    553     }
    554 
    555   return val;
    556 }
    557 
    558 /* Really perform an lvalue-to-rvalue conversion, including copying an
    559    argument of class type into a temporary.  */
    560 
    561 tree
    562 force_rvalue (tree expr, tsubst_flags_t complain)
    563 {
    564   tree type = TREE_TYPE (expr);
    565   if (MAYBE_CLASS_TYPE_P (type) && TREE_CODE (expr) != TARGET_EXPR)
    566     {
    567       releasing_vec args (make_tree_vector_single (expr));
    568       expr = build_special_member_call (NULL_TREE, complete_ctor_identifier,
    569 					&args, type, LOOKUP_NORMAL, complain);
    570       expr = build_cplus_new (type, expr, complain);
    571     }
    572   else
    573     expr = decay_conversion (expr, complain);
    574 
    575   return expr;
    576 }
    577 
    578 /* Force EXPR to be an lvalue, if it isn't already.  */
    579 
    580 tree
    581 force_lvalue (tree expr, tsubst_flags_t complain)
    582 {
    583   if (!lvalue_p (expr))
    584     {
    585       expr = cp_build_addr_expr (expr, complain);
    586       expr = cp_build_indirect_ref (input_location, expr, RO_ARROW, complain);
    587     }
    588   return expr;
    589 }
    590 
    591 
    592 /* If EXPR and ORIG are INTEGER_CSTs, return a version of EXPR that has
    594    TREE_OVERFLOW set only if it is set in ORIG.  Otherwise, return EXPR
    595    unchanged.  */
    596 
    597 static tree
    598 ignore_overflows (tree expr, tree orig)
    599 {
    600   tree stripped_expr = tree_strip_any_location_wrapper (expr);
    601   tree stripped_orig = tree_strip_any_location_wrapper (orig);
    602 
    603   if (TREE_CODE (stripped_expr) == INTEGER_CST
    604       && TREE_CODE (stripped_orig) == INTEGER_CST
    605       && TREE_OVERFLOW (stripped_expr) != TREE_OVERFLOW (stripped_orig))
    606     {
    607       gcc_assert (!TREE_OVERFLOW (stripped_orig));
    608       /* Ensure constant sharing.  */
    609       stripped_expr = wide_int_to_tree (TREE_TYPE (stripped_expr),
    610 					wi::to_wide (stripped_expr));
    611     }
    612 
    613   return preserve_any_location_wrapper (stripped_expr, expr);
    614 }
    615 
    616 /* Fold away simple conversions, but make sure TREE_OVERFLOW is set
    617    properly and propagate TREE_NO_WARNING if folding EXPR results
    618    in the same expression code.  */
    619 
    620 tree
    621 cp_fold_convert (tree type, tree expr)
    622 {
    623   tree conv;
    624   if (TREE_TYPE (expr) == type)
    625     conv = expr;
    626   else if (TREE_CODE (expr) == PTRMEM_CST
    627 	   && same_type_p (TYPE_PTRMEM_CLASS_TYPE (type),
    628 			   PTRMEM_CST_CLASS (expr)))
    629     {
    630       /* Avoid wrapping a PTRMEM_CST in NOP_EXPR.  */
    631       conv = copy_node (expr);
    632       TREE_TYPE (conv) = type;
    633     }
    634   else if (TYPE_PTRMEM_P (type))
    635     {
    636       conv = convert_ptrmem (type, expr, true, false,
    637 			     tf_warning_or_error);
    638       conv = cp_fully_fold (conv);
    639     }
    640   else
    641     {
    642       conv = fold_convert (type, expr);
    643       conv = ignore_overflows (conv, expr);
    644     }
    645 
    646   if (TREE_CODE (expr) == TREE_CODE (conv))
    647     copy_warning (conv, expr);
    648 
    649   return conv;
    650 }
    651 
    652 /* C++ conversions, preference to static cast conversions.  */
    653 
    654 tree
    655 cp_convert (tree type, tree expr, tsubst_flags_t complain)
    656 {
    657   return ocp_convert (type, expr, CONV_OLD_CONVERT, LOOKUP_NORMAL, complain);
    658 }
    659 
    660 /* C++ equivalent of convert_and_check but using cp_convert as the
    661    conversion function.
    662 
    663    Convert EXPR to TYPE, warning about conversion problems with constants.
    664    Invoke this function on every expression that is converted implicitly,
    665    i.e. because of language rules and not because of an explicit cast.  */
    666 
    667 tree
    668 cp_convert_and_check (tree type, tree expr, tsubst_flags_t complain)
    669 {
    670   tree result, expr_for_warning = expr;
    671 
    672   if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
    673     expr = TREE_OPERAND (expr, 0);
    674   if (TREE_TYPE (expr) == type)
    675     return expr;
    676   if (expr == error_mark_node)
    677     return expr;
    678   result = cp_convert (type, expr, complain);
    679 
    680   if ((complain & tf_warning)
    681       && c_inhibit_evaluation_warnings == 0)
    682     {
    683       tree folded = cp_fully_fold (expr_for_warning);
    684       tree folded_result;
    685       if (folded == expr)
    686 	folded_result = result;
    687       else
    688 	{
    689 	  /* Avoid bogus -Wparentheses warnings.  */
    690 	  warning_sentinel w (warn_parentheses);
    691 	  warning_sentinel c (warn_int_in_bool_context);
    692 	  folded_result = cp_convert (type, folded, tf_none);
    693 	}
    694       folded_result = fold_simple (folded_result);
    695       if (!TREE_OVERFLOW_P (folded)
    696 	  && folded_result != error_mark_node)
    697 	warnings_for_convert_and_check (cp_expr_loc_or_input_loc (expr),
    698 					type, folded, folded_result);
    699     }
    700 
    701   return result;
    702 }
    703 
    704 /* Conversion...
    705 
    706    FLAGS indicates how we should behave.  */
    707 
    708 tree
    709 ocp_convert (tree type, tree expr, int convtype, int flags,
    710 	     tsubst_flags_t complain)
    711 {
    712   tree e = expr;
    713   enum tree_code code = TREE_CODE (type);
    714   const char *invalid_conv_diag;
    715   tree e1;
    716   location_t loc = cp_expr_loc_or_input_loc (expr);
    717   bool dofold = (convtype & CONV_FOLD);
    718 
    719   if (error_operand_p (e) || type == error_mark_node)
    720     return error_mark_node;
    721 
    722   if (TREE_CODE (e) == COMPOUND_EXPR)
    723     {
    724       e = ocp_convert (type, TREE_OPERAND (e, 1), convtype, flags, complain);
    725       if (e == error_mark_node)
    726 	return error_mark_node;
    727       if (e == TREE_OPERAND (expr, 1))
    728 	return expr;
    729       e = build2_loc (EXPR_LOCATION (expr), COMPOUND_EXPR, TREE_TYPE (e),
    730 		      TREE_OPERAND (expr, 0), e);
    731       copy_warning (e, expr);
    732       return e;
    733     }
    734 
    735   complete_type (type);
    736   complete_type (TREE_TYPE (expr));
    737 
    738   if ((invalid_conv_diag
    739        = targetm.invalid_conversion (TREE_TYPE (expr), type)))
    740     {
    741       if (complain & tf_error)
    742 	error (invalid_conv_diag);
    743       return error_mark_node;
    744     }
    745 
    746   /* FIXME remove when moving to c_fully_fold model.  */
    747   if (!CLASS_TYPE_P (type))
    748     {
    749       e = mark_rvalue_use (e);
    750       tree v = scalar_constant_value (e);
    751       if (!error_operand_p (v))
    752 	e = v;
    753     }
    754   if (error_operand_p (e))
    755     return error_mark_node;
    756 
    757   if (NULLPTR_TYPE_P (type) && null_ptr_cst_p (e))
    758     {
    759       if (complain & tf_warning)
    760 	maybe_warn_zero_as_null_pointer_constant (e, loc);
    761 
    762       if (!TREE_SIDE_EFFECTS (e))
    763 	return nullptr_node;
    764     }
    765 
    766   if (MAYBE_CLASS_TYPE_P (type) && (convtype & CONV_FORCE_TEMP))
    767     /* We need a new temporary; don't take this shortcut.  */;
    768   else if (same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (e)))
    769     {
    770       tree etype = TREE_TYPE (e);
    771       if (same_type_p (type, etype))
    772 	/* The call to fold will not always remove the NOP_EXPR as
    773 	   might be expected, since if one of the types is a typedef;
    774 	   the comparison in fold is just equality of pointers, not a
    775 	   call to comptypes.  We don't call fold in this case because
    776 	   that can result in infinite recursion; fold will call
    777 	   convert, which will call ocp_convert, etc.  */
    778 	return e;
    779       /* For complex data types, we need to perform componentwise
    780 	 conversion.  */
    781       else if (TREE_CODE (type) == COMPLEX_TYPE)
    782 	return convert_to_complex_maybe_fold (type, e, dofold);
    783       else if (VECTOR_TYPE_P (type))
    784 	return convert_to_vector (type, rvalue (e));
    785       else if (TREE_CODE (e) == TARGET_EXPR)
    786 	{
    787 	  /* Don't build a NOP_EXPR of class type.  Instead, change the
    788 	     type of the temporary.  */
    789 	  gcc_assert (same_type_ignoring_top_level_qualifiers_p (type, etype));
    790 	  TREE_TYPE (e) = TREE_TYPE (TARGET_EXPR_SLOT (e)) = type;
    791 	  return e;
    792 	}
    793       else if (TREE_CODE (e) == CONSTRUCTOR)
    794 	{
    795 	  gcc_assert (same_type_ignoring_top_level_qualifiers_p (type, etype));
    796 	  TREE_TYPE (e) = type;
    797 	  return e;
    798 	}
    799       else
    800 	{
    801 	  /* We shouldn't be treating objects of ADDRESSABLE type as
    802 	     rvalues.  */
    803 	  gcc_assert (!TREE_ADDRESSABLE (type));
    804 	  return build_nop (type, e);
    805 	}
    806     }
    807 
    808   e1 = targetm.convert_to_type (type, e);
    809   if (e1)
    810     return e1;
    811 
    812   if (code == VOID_TYPE && (convtype & CONV_STATIC))
    813     {
    814       e = convert_to_void (e, ICV_CAST, complain);
    815       return e;
    816     }
    817 
    818   if (INTEGRAL_CODE_P (code))
    819     {
    820       tree intype = TREE_TYPE (e);
    821       tree converted;
    822 
    823       if (TREE_CODE (type) == ENUMERAL_TYPE)
    824 	{
    825 	  /* enum = enum, enum = int, enum = float, (enum)pointer are all
    826 	     errors.  */
    827 	  if (((INTEGRAL_OR_ENUMERATION_TYPE_P (intype)
    828 		|| SCALAR_FLOAT_TYPE_P (intype))
    829 	       && ! (convtype & CONV_STATIC))
    830 	      || TYPE_PTR_P (intype))
    831 	    {
    832 	      if (complain & tf_error)
    833 		permerror (loc, "conversion from %q#T to %q#T", intype, type);
    834 	      else
    835 		return error_mark_node;
    836 	    }
    837 
    838 	  /* [expr.static.cast]
    839 
    840 	     8. A value of integral or enumeration type can be explicitly
    841 	     converted to an enumeration type. The value is unchanged if
    842 	     the original value is within the range of the enumeration
    843 	     values. Otherwise, the resulting enumeration value is
    844 	     unspecified.  */
    845 	  tree val = fold_for_warn (e);
    846 	  if ((complain & tf_warning)
    847 	      && TREE_CODE (val) == INTEGER_CST
    848 	      && ENUM_UNDERLYING_TYPE (type)
    849 	      && !int_fits_type_p (val, ENUM_UNDERLYING_TYPE (type)))
    850 	    warning_at (loc, OPT_Wconversion,
    851 			"the result of the conversion is unspecified because "
    852 			"%qE is outside the range of type %qT",
    853 			expr, type);
    854 	}
    855       if (MAYBE_CLASS_TYPE_P (intype))
    856 	{
    857 	  tree rval;
    858 	  rval = build_type_conversion (type, e);
    859 	  if (rval)
    860 	    return rval;
    861 	  if (complain & tf_error)
    862 	    error_at (loc, "%q#T used where a %qT was expected", intype, type);
    863 	  return error_mark_node;
    864 	}
    865       if (code == BOOLEAN_TYPE)
    866 	{
    867 	  if (VOID_TYPE_P (intype))
    868 	    {
    869 	      if (complain & tf_error)
    870 		error_at (loc,
    871 			  "could not convert %qE from %<void%> to %<bool%>",
    872 			  expr);
    873 	      return error_mark_node;
    874 	    }
    875 
    876 	  if (VECTOR_TYPE_P (intype) && !gnu_vector_type_p (intype))
    877 	    {
    878 	      if (complain & tf_error)
    879 		error_at (loc, "could not convert %qE from %qH to %qI", expr,
    880 			  TREE_TYPE (expr), type);
    881 	      return error_mark_node;
    882 	    }
    883 
    884 	  /* We can't implicitly convert a scoped enum to bool, so convert
    885 	     to the underlying type first.  */
    886 	  if (SCOPED_ENUM_P (intype) && (convtype & CONV_STATIC))
    887 	    e = build_nop (ENUM_UNDERLYING_TYPE (intype), e);
    888 	  if (complain & tf_warning)
    889 	    return cp_truthvalue_conversion (e, complain);
    890 	  else
    891 	    {
    892 	      /* Prevent bogus -Wint-in-bool-context warnings coming
    893 		 from c_common_truthvalue_conversion down the line.  */
    894 	      warning_sentinel w (warn_int_in_bool_context);
    895 	      warning_sentinel c (warn_sign_compare);
    896 	      return cp_truthvalue_conversion (e, complain);
    897 	    }
    898 	}
    899 
    900       converted = convert_to_integer_maybe_fold (type, e, dofold);
    901 
    902       /* Ignore any integer overflow caused by the conversion.  */
    903       return ignore_overflows (converted, e);
    904     }
    905   if (INDIRECT_TYPE_P (type) || TYPE_PTRMEM_P (type))
    906     return cp_convert_to_pointer (type, e, dofold, complain);
    907   if (code == VECTOR_TYPE)
    908     {
    909       tree in_vtype = TREE_TYPE (e);
    910       if (MAYBE_CLASS_TYPE_P (in_vtype))
    911 	{
    912 	  tree ret_val;
    913 	  ret_val = build_type_conversion (type, e);
    914 	  if (ret_val)
    915 	    return ret_val;
    916 	  if (complain & tf_error)
    917 	    error_at (loc, "%q#T used where a %qT was expected",
    918 		      in_vtype, type);
    919 	  return error_mark_node;
    920 	}
    921       return convert_to_vector (type, rvalue (e));
    922     }
    923   if (code == REAL_TYPE || code == COMPLEX_TYPE)
    924     {
    925       if (MAYBE_CLASS_TYPE_P (TREE_TYPE (e)))
    926 	{
    927 	  tree rval;
    928 	  rval = build_type_conversion (type, e);
    929 	  if (rval)
    930 	    return rval;
    931 	  else if (complain & tf_error)
    932 	    error_at (loc,
    933 		      "%q#T used where a floating-point value was expected",
    934 		      TREE_TYPE (e));
    935 	}
    936       if (code == REAL_TYPE)
    937 	return convert_to_real_maybe_fold (type, e, dofold);
    938       else if (code == COMPLEX_TYPE)
    939 	return convert_to_complex_maybe_fold (type, e, dofold);
    940     }
    941 
    942   /* New C++ semantics:  since assignment is now based on
    943      memberwise copying,  if the rhs type is derived from the
    944      lhs type, then we may still do a conversion.  */
    945   if (RECORD_OR_UNION_CODE_P (code))
    946     {
    947       tree dtype = TREE_TYPE (e);
    948       tree ctor = NULL_TREE;
    949 
    950       dtype = TYPE_MAIN_VARIANT (dtype);
    951 
    952       /* Conversion between aggregate types.  New C++ semantics allow
    953 	 objects of derived type to be cast to objects of base type.
    954 	 Old semantics only allowed this between pointers.
    955 
    956 	 There may be some ambiguity between using a constructor
    957 	 vs. using a type conversion operator when both apply.  */
    958 
    959       ctor = e;
    960 
    961       if (abstract_virtuals_error (NULL_TREE, type, complain))
    962 	return error_mark_node;
    963 
    964       if (BRACE_ENCLOSED_INITIALIZER_P (ctor))
    965 	ctor = perform_implicit_conversion (type, ctor, complain);
    966       else if ((flags & LOOKUP_ONLYCONVERTING)
    967 	       && ! (CLASS_TYPE_P (dtype) && DERIVED_FROM_P (type, dtype)))
    968 	/* For copy-initialization, first we create a temp of the proper type
    969 	   with a user-defined conversion sequence, then we direct-initialize
    970 	   the target with the temp (see [dcl.init]).  */
    971 	ctor = build_user_type_conversion (type, ctor, flags, complain);
    972       else
    973 	{
    974 	  releasing_vec ctor_vec (make_tree_vector_single (ctor));
    975 	  ctor = build_special_member_call (NULL_TREE,
    976 					    complete_ctor_identifier,
    977 					    &ctor_vec,
    978 					    type, flags, complain);
    979 	}
    980       if (ctor)
    981 	return build_cplus_new (type, ctor, complain);
    982     }
    983 
    984   if (complain & tf_error)
    985     {
    986       /* If the conversion failed and expr was an invalid use of pointer to
    987 	 member function, try to report a meaningful error.  */
    988       if (invalid_nonstatic_memfn_p (loc, expr, complain))
    989 	/* We displayed the error message.  */;
    990       else
    991 	error_at (loc, "conversion from %qH to non-scalar type %qI requested",
    992 		  TREE_TYPE (expr), type);
    993     }
    994   return error_mark_node;
    995 }
    996 
    997 /* If CALL is a call, return the callee; otherwise null.  */
    998 
    999 tree
   1000 cp_get_callee (tree call)
   1001 {
   1002   if (call == NULL_TREE)
   1003     return call;
   1004   else if (TREE_CODE (call) == CALL_EXPR)
   1005     return CALL_EXPR_FN (call);
   1006   else if (TREE_CODE (call) == AGGR_INIT_EXPR)
   1007     return AGGR_INIT_EXPR_FN (call);
   1008   return NULL_TREE;
   1009 }
   1010 
   1011 /* FN is the callee of a CALL_EXPR or AGGR_INIT_EXPR; return the FUNCTION_DECL
   1012    if we can.  */
   1013 
   1014 tree
   1015 cp_get_fndecl_from_callee (tree fn, bool fold /* = true */)
   1016 {
   1017   if (fn == NULL_TREE)
   1018     return fn;
   1019 
   1020   /* We evaluate constexpr functions on the original, pre-genericization
   1021      bodies.  So block-scope extern declarations have not been mapped to
   1022      declarations in outer scopes.  Use the namespace-scope declaration,
   1023      if any, so that retrieve_constexpr_fundef can find it (PR111132).  */
   1024   auto fn_or_local_alias = [] (tree f)
   1025     {
   1026       if (DECL_LOCAL_DECL_P (f))
   1027 	if (tree alias = DECL_LOCAL_DECL_ALIAS (f))
   1028 	  if (alias != error_mark_node)
   1029 	    return alias;
   1030       return f;
   1031     };
   1032 
   1033   if (TREE_CODE (fn) == FUNCTION_DECL)
   1034     return fn_or_local_alias (fn);
   1035   tree type = TREE_TYPE (fn);
   1036   if (type == NULL_TREE || !INDIRECT_TYPE_P (type))
   1037     return NULL_TREE;
   1038   if (fold)
   1039     fn = maybe_constant_init (fn);
   1040   STRIP_NOPS (fn);
   1041   if (TREE_CODE (fn) == ADDR_EXPR
   1042       || TREE_CODE (fn) == FDESC_EXPR)
   1043     fn = TREE_OPERAND (fn, 0);
   1044   if (TREE_CODE (fn) == FUNCTION_DECL)
   1045     return fn_or_local_alias (fn);
   1046   return NULL_TREE;
   1047 }
   1048 
   1049 /* Like get_callee_fndecl, but handles AGGR_INIT_EXPR as well and uses the
   1050    constexpr machinery.  */
   1051 
   1052 tree
   1053 cp_get_callee_fndecl (tree call)
   1054 {
   1055   return cp_get_fndecl_from_callee (cp_get_callee (call));
   1056 }
   1057 
   1058 /* As above, but not using the constexpr machinery.  */
   1059 
   1060 tree
   1061 cp_get_callee_fndecl_nofold (tree call)
   1062 {
   1063   return cp_get_fndecl_from_callee (cp_get_callee (call), false);
   1064 }
   1065 
   1066 /* Subroutine of convert_to_void.  Warn if we're discarding something with
   1067    attribute [[nodiscard]].  */
   1068 
   1069 static void
   1070 maybe_warn_nodiscard (tree expr, impl_conv_void implicit)
   1071 {
   1072   if (!warn_unused_result || c_inhibit_evaluation_warnings)
   1073     return;
   1074 
   1075   tree call = expr;
   1076   if (TREE_CODE (expr) == TARGET_EXPR)
   1077     call = TARGET_EXPR_INITIAL (expr);
   1078   location_t loc = cp_expr_loc_or_input_loc (call);
   1079   tree callee = cp_get_callee (call);
   1080   if (!callee || !TREE_TYPE (callee))
   1081     return;
   1082 
   1083   tree type = TREE_TYPE (callee);
   1084   if (TYPE_PTRMEMFUNC_P (type))
   1085     type = TYPE_PTRMEMFUNC_FN_TYPE (type);
   1086   if (INDIRECT_TYPE_P (type))
   1087     type = TREE_TYPE (type);
   1088   if (!FUNC_OR_METHOD_TYPE_P (type))
   1089     return;
   1090 
   1091   tree rettype = TREE_TYPE (type);
   1092   tree fn = cp_get_fndecl_from_callee (callee);
   1093   tree attr;
   1094   if (implicit != ICV_CAST && fn
   1095       && (attr = lookup_attribute ("nodiscard", DECL_ATTRIBUTES (fn))))
   1096     {
   1097       escaped_string msg;
   1098       tree args = TREE_VALUE (attr);
   1099       if (args)
   1100 	msg.escape (TREE_STRING_POINTER (TREE_VALUE (args)));
   1101       const char *format
   1102 	= (msg
   1103 	   ? G_("ignoring return value of %qD, "
   1104 		"declared with attribute %<nodiscard%>: %<%s%>")
   1105 	   : G_("ignoring return value of %qD, "
   1106 		"declared with attribute %<nodiscard%>%s"));
   1107       const char *raw_msg = msg ? (const char *) msg : "";
   1108       auto_diagnostic_group d;
   1109       if (warning_at (loc, OPT_Wunused_result, format, fn, raw_msg))
   1110 	inform (DECL_SOURCE_LOCATION (fn), "declared here");
   1111     }
   1112   else if (implicit != ICV_CAST
   1113 	   && (attr = lookup_attribute ("nodiscard", TYPE_ATTRIBUTES (rettype))))
   1114     {
   1115       escaped_string msg;
   1116       tree args = TREE_VALUE (attr);
   1117       if (args)
   1118 	msg.escape (TREE_STRING_POINTER (TREE_VALUE (args)));
   1119       const char *format
   1120 	= (msg
   1121 	   ? G_("ignoring returned value of type %qT, "
   1122 		"declared with attribute %<nodiscard%>: %<%s%>")
   1123 	   : G_("ignoring returned value of type %qT, "
   1124 		"declared with attribute %<nodiscard%>%s"));
   1125       const char *raw_msg = msg ? (const char *) msg : "";
   1126       auto_diagnostic_group d;
   1127       if (warning_at (loc, OPT_Wunused_result, format, rettype, raw_msg))
   1128 	{
   1129 	  if (fn)
   1130 	    inform (DECL_SOURCE_LOCATION (fn),
   1131 		    "in call to %qD, declared here", fn);
   1132 	  inform (DECL_SOURCE_LOCATION (TYPE_NAME (rettype)),
   1133 		  "%qT declared here", rettype);
   1134 	}
   1135     }
   1136   else if (TREE_CODE (expr) == TARGET_EXPR
   1137 	   && lookup_attribute ("warn_unused_result", TYPE_ATTRIBUTES (type)))
   1138     {
   1139       /* The TARGET_EXPR confuses do_warn_unused_result into thinking that the
   1140 	 result is used, so handle that case here.  */
   1141       if (fn)
   1142 	{
   1143 	  auto_diagnostic_group d;
   1144 	  if (warning_at (loc, OPT_Wunused_result,
   1145 			  "ignoring return value of %qD, "
   1146 			  "declared with attribute %<warn_unused_result%>",
   1147 			  fn))
   1148 	    inform (DECL_SOURCE_LOCATION (fn), "declared here");
   1149 	}
   1150       else
   1151 	warning_at (loc, OPT_Wunused_result,
   1152 		    "ignoring return value of function "
   1153 		    "declared with attribute %<warn_unused_result%>");
   1154     }
   1155 }
   1156 
   1157 /* When an expression is used in a void context, its value is discarded and
   1158    no lvalue-rvalue and similar conversions happen [expr.static.cast/4,
   1159    stmt.expr/1, expr.comma/1].  This permits dereferencing an incomplete type
   1160    in a void context. The C++ standard does not define what an `access' to an
   1161    object is, but there is reason to believe that it is the lvalue to rvalue
   1162    conversion -- if it were not, `*&*p = 1' would violate [expr]/4 in that it
   1163    accesses `*p' not to calculate the value to be stored. But, dcl.type.cv/8
   1164    indicates that volatile semantics should be the same between C and C++
   1165    where ever possible. C leaves it implementation defined as to what
   1166    constitutes an access to a volatile. So, we interpret `*vp' as a read of
   1167    the volatile object `vp' points to, unless that is an incomplete type. For
   1168    volatile references we do not do this interpretation, because that would
   1169    make it impossible to ignore the reference return value from functions. We
   1170    issue warnings in the confusing cases.
   1171 
   1172    The IMPLICIT is ICV_CAST when the user is explicitly converting an expression
   1173    to void via a cast. If an expression is being implicitly converted, IMPLICIT
   1174    indicates the context of the implicit conversion.  */
   1175 
   1176 tree
   1177 convert_to_void (tree expr, impl_conv_void implicit, tsubst_flags_t complain)
   1178 {
   1179   location_t loc = cp_expr_loc_or_input_loc (expr);
   1180 
   1181   if (expr == error_mark_node
   1182       || TREE_TYPE (expr) == error_mark_node)
   1183     return error_mark_node;
   1184 
   1185   expr = maybe_undo_parenthesized_ref (expr);
   1186 
   1187   expr = mark_discarded_use (expr);
   1188   if (implicit == ICV_CAST)
   1189     /* An explicit cast to void avoids all -Wunused-but-set* warnings.  */
   1190     mark_exp_read (expr);
   1191 
   1192   if (!TREE_TYPE (expr))
   1193     return expr;
   1194   if (invalid_nonstatic_memfn_p (loc, expr, complain))
   1195     return error_mark_node;
   1196   if (TREE_CODE (expr) == PSEUDO_DTOR_EXPR)
   1197     {
   1198       if (complain & tf_error)
   1199         error_at (loc, "pseudo-destructor is not called");
   1200       return error_mark_node;
   1201     }
   1202 
   1203   /* Explicitly evaluate void-converted concept checks since their
   1204      satisfaction may produce ill-formed programs.  */
   1205    if (concept_check_p (expr))
   1206      expr = evaluate_concept_check (expr);
   1207 
   1208   if (VOID_TYPE_P (TREE_TYPE (expr)))
   1209     return expr;
   1210   switch (TREE_CODE (expr))
   1211     {
   1212     case COND_EXPR:
   1213       {
   1214 	/* The two parts of a cond expr might be separate lvalues.  */
   1215 	tree op1 = TREE_OPERAND (expr,1);
   1216 	tree op2 = TREE_OPERAND (expr,2);
   1217 	bool side_effects = ((op1 && TREE_SIDE_EFFECTS (op1))
   1218 			     || TREE_SIDE_EFFECTS (op2));
   1219 	tree new_op1, new_op2;
   1220 	new_op1 = NULL_TREE;
   1221 	if (implicit != ICV_CAST && !side_effects)
   1222 	  {
   1223 	    if (op1)
   1224 	      new_op1 = convert_to_void (op1, ICV_SECOND_OF_COND, complain);
   1225 	    new_op2 = convert_to_void (op2, ICV_THIRD_OF_COND, complain);
   1226 	  }
   1227 	else
   1228 	  {
   1229 	    if (op1)
   1230 	      new_op1 = convert_to_void (op1, ICV_CAST, complain);
   1231 	    new_op2 = convert_to_void (op2, ICV_CAST, complain);
   1232 	  }
   1233 
   1234 	expr = build3_loc (loc, COND_EXPR, TREE_TYPE (new_op2),
   1235 			   TREE_OPERAND (expr, 0), new_op1, new_op2);
   1236 	break;
   1237       }
   1238 
   1239     case COMPOUND_EXPR:
   1240       {
   1241 	/* The second part of a compound expr contains the value.  */
   1242 	tree op1 = TREE_OPERAND (expr,1);
   1243 	tree new_op1;
   1244 	if (implicit != ICV_CAST && !warning_suppressed_p (expr /* What warning? */))
   1245 	  new_op1 = convert_to_void (op1, ICV_RIGHT_OF_COMMA, complain);
   1246 	else
   1247 	  new_op1 = convert_to_void (op1, ICV_CAST, complain);
   1248 
   1249 	if (new_op1 != op1)
   1250 	  {
   1251 	    tree t = build2_loc (loc, COMPOUND_EXPR, TREE_TYPE (new_op1),
   1252 				 TREE_OPERAND (expr, 0), new_op1);
   1253 	    expr = t;
   1254 	  }
   1255 
   1256 	break;
   1257       }
   1258 
   1259     case NON_LVALUE_EXPR:
   1260     case NOP_EXPR:
   1261       /* These have already decayed to rvalue.  */
   1262       break;
   1263 
   1264     case CALL_EXPR:   /* We have a special meaning for volatile void fn().  */
   1265       /* cdtors may return this or void, depending on
   1266 	 targetm.cxx.cdtor_returns_this, but this shouldn't affect our
   1267 	 decisions here: neither nodiscard warnings (nodiscard dtors
   1268 	 are nonsensical and ctors have a different behavior with that
   1269 	 attribute that is handled in the TARGET_EXPR case), nor should
   1270 	 any constexpr or template instantiations be affected by an ABI
   1271 	 property that is, or at least ought to be transparent to the
   1272 	 language.  */
   1273       if (tree fn = cp_get_callee_fndecl_nofold (expr))
   1274 	if (DECL_CONSTRUCTOR_P (fn) || DECL_DESTRUCTOR_P (fn))
   1275 	  return expr;
   1276 
   1277       if (complain & tf_warning)
   1278 	maybe_warn_nodiscard (expr, implicit);
   1279       break;
   1280 
   1281     case INDIRECT_REF:
   1282       {
   1283 	tree type = TREE_TYPE (expr);
   1284 	int is_reference = TYPE_REF_P (TREE_TYPE (TREE_OPERAND (expr, 0)));
   1285 	int is_volatile = TYPE_VOLATILE (type);
   1286 	if (is_volatile)
   1287 	  complete_type (type);
   1288 	int is_complete = COMPLETE_TYPE_P (type);
   1289 
   1290 	/* Don't load the value if this is an implicit dereference, or if
   1291 	   the type needs to be handled by ctors/dtors.  */
   1292 	if (is_reference)
   1293           {
   1294             if (is_volatile && (complain & tf_warning)
   1295 		/* A co_await expression, in its await_resume expression, also
   1296 		   contains an implicit dereference.  As a result, we don't
   1297 		   need to warn about them here.  */
   1298 		&& TREE_CODE (TREE_OPERAND (expr, 0)) != CO_AWAIT_EXPR)
   1299 	      switch (implicit)
   1300 		{
   1301 	      	  case ICV_CAST:
   1302 		    warning_at (loc, 0, "conversion to void will not access "
   1303 				"object of type %qT", type);
   1304 		    break;
   1305 		  case ICV_SECOND_OF_COND:
   1306 		    warning_at (loc, 0, "implicit dereference will not access "
   1307 				"object of type %qT in second operand of "
   1308 				"conditional expression", type);
   1309 		    break;
   1310 		  case ICV_THIRD_OF_COND:
   1311 		    warning_at (loc, 0, "implicit dereference will not access "
   1312 				"object of type %qT in third operand of "
   1313 				"conditional expression", type);
   1314 		    break;
   1315 		  case ICV_RIGHT_OF_COMMA:
   1316 		    warning_at (loc, 0, "implicit dereference will not access "
   1317 				"object of type %qT in right operand of "
   1318 				"comma operator", type);
   1319 		    break;
   1320 		  case ICV_LEFT_OF_COMMA:
   1321 		    warning_at (loc, 0, "implicit dereference will not access "
   1322 				"object of type %qT in left operand of comma "
   1323 				"operator", type);
   1324 		    break;
   1325 		  case ICV_STATEMENT:
   1326 		    warning_at (loc, 0, "implicit dereference will not access "
   1327 				"object of type %qT in statement",  type);
   1328 		     break;
   1329 		  case ICV_THIRD_IN_FOR:
   1330 		    warning_at (loc, 0, "implicit dereference will not access "
   1331 				"object of type %qT in for increment expression",
   1332 				type);
   1333 		    break;
   1334 		  default:
   1335 		    gcc_unreachable ();
   1336 		}
   1337 
   1338 	    /* Since this was an implicit dereference, we should also act as if
   1339 	       it was never there.  */
   1340 	    return convert_to_void (TREE_OPERAND (expr, 0), implicit, complain);
   1341           }
   1342 	/* Can't load the value if we don't know the type.  */
   1343 	else if (is_volatile && !is_complete)
   1344           {
   1345             if (complain & tf_warning)
   1346 	      switch (implicit)
   1347 		{
   1348 	      	  case ICV_CAST:
   1349 		    warning_at (loc, 0, "conversion to void will not access "
   1350 				"object of incomplete type %qT", type);
   1351 		    break;
   1352 		  case ICV_SECOND_OF_COND:
   1353 		    warning_at (loc, 0, "indirection will not access object of "
   1354 				"incomplete type %qT in second operand "
   1355 				"of conditional expression", type);
   1356 		    break;
   1357 		  case ICV_THIRD_OF_COND:
   1358 		    warning_at (loc, 0, "indirection will not access object of "
   1359 				"incomplete type %qT in third operand "
   1360 				"of conditional expression", type);
   1361 		    break;
   1362 		  case ICV_RIGHT_OF_COMMA:
   1363 		    warning_at (loc, 0, "indirection will not access object of "
   1364 				"incomplete type %qT in right operand of "
   1365 				"comma operator", type);
   1366 		    break;
   1367 		  case ICV_LEFT_OF_COMMA:
   1368 		    warning_at (loc, 0, "indirection will not access object of "
   1369 				"incomplete type %qT in left operand of "
   1370 				"comma operator", type);
   1371 		    break;
   1372 		  case ICV_STATEMENT:
   1373 		    warning_at (loc, 0, "indirection will not access object of "
   1374 				"incomplete type %qT in statement", type);
   1375 		     break;
   1376 		  case ICV_THIRD_IN_FOR:
   1377 		    warning_at (loc, 0, "indirection will not access object of "
   1378 				"incomplete type %qT in for increment "
   1379 				"expression", type);
   1380 		    break;
   1381 		  default:
   1382 		    gcc_unreachable ();
   1383 		}
   1384           }
   1385 	else if (is_volatile && TREE_ADDRESSABLE (type))
   1386 	  {
   1387 	    if (complain & tf_warning)
   1388 	      switch (implicit)
   1389 		{
   1390 	      	  case ICV_CAST:
   1391 		    warning_at (loc, 0, "conversion to void will not access "
   1392 				"object of non-trivially-copyable type %qT",
   1393 				type);
   1394 		    break;
   1395 		  case ICV_SECOND_OF_COND:
   1396 		    warning_at (loc, 0, "indirection will not access object of "
   1397 				"non-trivially-copyable type %qT in second "
   1398 				"operand of conditional expression", type);
   1399 		    break;
   1400 		  case ICV_THIRD_OF_COND:
   1401 		    warning_at (loc, 0, "indirection will not access object of "
   1402 		  	      	"non-trivially-copyable type %qT in third "
   1403 				"operand of conditional expression", type);
   1404 		    break;
   1405 		  case ICV_RIGHT_OF_COMMA:
   1406 		    warning_at (loc, 0, "indirection will not access object of "
   1407 		    		"non-trivially-copyable type %qT in right "
   1408 				"operand of comma operator", type);
   1409 		    break;
   1410 		  case ICV_LEFT_OF_COMMA:
   1411 		    warning_at (loc, 0, "indirection will not access object of "
   1412 		    		"non-trivially-copyable type %qT in left "
   1413 				"operand of comma operator", type);
   1414 		    break;
   1415 		  case ICV_STATEMENT:
   1416 		    warning_at (loc, 0, "indirection will not access object of "
   1417 		     		"non-trivially-copyable type %qT in statement",
   1418 				type);
   1419 		     break;
   1420 		  case ICV_THIRD_IN_FOR:
   1421 		    warning_at (loc, 0, "indirection will not access object of "
   1422 		    		"non-trivially-copyable type %qT in for "
   1423 				"increment expression", type);
   1424 		    break;
   1425 		  default:
   1426 		    gcc_unreachable ();
   1427 		}
   1428 	  }
   1429 	if (!is_volatile || !is_complete || TREE_ADDRESSABLE (type))
   1430           {
   1431             /* Emit a warning (if enabled) when the "effect-less" INDIRECT_REF
   1432                operation is stripped off. Note that we don't warn about
   1433                - an expression with TREE_NO_WARNING set. (For an example of
   1434                  such expressions, see build_over_call in call.cc.)
   1435                - automatic dereferencing of references, since the user cannot
   1436                  control it. (See also warn_if_unused_value() in c-common.cc.)  */
   1437             if (warn_unused_value
   1438 		&& implicit != ICV_CAST
   1439                 && (complain & tf_warning)
   1440                 && !warning_suppressed_p (expr, OPT_Wunused_value)
   1441                 && !is_reference)
   1442               warning_at (loc, OPT_Wunused_value, "value computed is not used");
   1443             expr = TREE_OPERAND (expr, 0);
   1444           }
   1445 
   1446 	break;
   1447       }
   1448 
   1449     case VAR_DECL:
   1450       {
   1451 	/* External variables might be incomplete.  */
   1452 	tree type = TREE_TYPE (expr);
   1453 
   1454 	if (TYPE_VOLATILE (type)
   1455 	    && !COMPLETE_TYPE_P (complete_type (type))
   1456 	    && (complain & tf_warning))
   1457 	  switch (implicit)
   1458 	    {
   1459 	      case ICV_CAST:
   1460 		warning_at (loc, 0, "conversion to void will not access "
   1461 			    "object %qE of incomplete type %qT", expr, type);
   1462 		break;
   1463 	      case ICV_SECOND_OF_COND:
   1464 	        warning_at (loc, 0, "variable %qE of incomplete type %qT will "
   1465 			    "not be accessed in second operand of "
   1466 			    "conditional expression", expr, type);
   1467 		break;
   1468 	      case ICV_THIRD_OF_COND:
   1469 	        warning_at (loc, 0, "variable %qE of incomplete type %qT will "
   1470 			    "not be accessed in third operand of "
   1471 			    "conditional expression", expr, type);
   1472 		break;
   1473 	      case ICV_RIGHT_OF_COMMA:
   1474 	        warning_at (loc, 0, "variable %qE of incomplete type %qT will "
   1475 			    "not be accessed in right operand of comma operator",
   1476 			    expr, type);
   1477 		break;
   1478 	      case ICV_LEFT_OF_COMMA:
   1479 	        warning_at (loc, 0, "variable %qE of incomplete type %qT will "
   1480 			    "not be accessed in left operand of comma operator",
   1481 			    expr, type);
   1482 		break;
   1483 	      case ICV_STATEMENT:
   1484 	        warning_at (loc, 0, "variable %qE of incomplete type %qT will "
   1485 			    "not be accessed in statement", expr, type);
   1486 		break;
   1487 	      case ICV_THIRD_IN_FOR:
   1488 	        warning_at (loc, 0, "variable %qE of incomplete type %qT will "
   1489 			    "not be accessed in for increment expression",
   1490 			    expr, type);
   1491 		break;
   1492 	      default:
   1493 	        gcc_unreachable ();
   1494 	    }
   1495 
   1496 	break;
   1497       }
   1498 
   1499     case TARGET_EXPR:
   1500       /* Don't bother with the temporary object returned from a function if
   1501 	 we don't use it, don't need to destroy it, and won't abort in
   1502 	 assign_temp.  We'll still
   1503 	 allocate space for it in expand_call or declare_return_variable,
   1504 	 but we don't need to track it through all the tree phases.  */
   1505       if (TARGET_EXPR_IMPLICIT_P (expr)
   1506 	  && !TREE_ADDRESSABLE (TREE_TYPE (expr)))
   1507 	{
   1508 	  tree init = TARGET_EXPR_INITIAL (expr);
   1509 	  if (TREE_CODE (init) == AGGR_INIT_EXPR
   1510 	      && !AGGR_INIT_VIA_CTOR_P (init))
   1511 	    {
   1512 	      tree fn = AGGR_INIT_EXPR_FN (init);
   1513 	      expr = build_call_array_loc (input_location,
   1514 					   TREE_TYPE (TREE_TYPE
   1515 						      (TREE_TYPE (fn))),
   1516 					   fn,
   1517 					   aggr_init_expr_nargs (init),
   1518 					   AGGR_INIT_EXPR_ARGP (init));
   1519 	    }
   1520 	}
   1521       if (complain & tf_warning)
   1522 	maybe_warn_nodiscard (expr, implicit);
   1523       break;
   1524 
   1525     case CO_AWAIT_EXPR:
   1526       if (auto awr = co_await_get_resume_call (expr))
   1527 	convert_to_void (awr, implicit, complain);
   1528       break;
   1529 
   1530     default:;
   1531     }
   1532   expr = resolve_nondeduced_context (expr, complain);
   1533   if (!mark_single_function (expr, complain))
   1534     return error_mark_node;
   1535 
   1536   {
   1537     tree probe = expr;
   1538 
   1539     if (TREE_CODE (probe) == ADDR_EXPR)
   1540       probe = TREE_OPERAND (expr, 0);
   1541     if (type_unknown_p (probe))
   1542       {
   1543 	/* [over.over] enumerates the places where we can take the address
   1544 	   of an overloaded function, and this is not one of them.  */
   1545 	if (complain & tf_error)
   1546 	  switch (implicit)
   1547 	    {
   1548 	      case ICV_CAST:
   1549 		error_at (loc, "conversion to void "
   1550 			  "cannot resolve address of overloaded function");
   1551 		break;
   1552 	      case ICV_SECOND_OF_COND:
   1553 		error_at (loc, "second operand of conditional expression "
   1554 			  "cannot resolve address of overloaded function");
   1555 		break;
   1556 	      case ICV_THIRD_OF_COND:
   1557 		error_at (loc, "third operand of conditional expression "
   1558 			  "cannot resolve address of overloaded function");
   1559 		break;
   1560 	      case ICV_RIGHT_OF_COMMA:
   1561 		error_at (loc, "right operand of comma operator "
   1562 			  "cannot resolve address of overloaded function");
   1563 		break;
   1564 	      case ICV_LEFT_OF_COMMA:
   1565 		error_at (loc, "left operand of comma operator "
   1566 			  "cannot resolve address of overloaded function");
   1567 		break;
   1568 	      case ICV_STATEMENT:
   1569 		error_at (loc, "statement "
   1570 			  "cannot resolve address of overloaded function");
   1571 		break;
   1572 	      case ICV_THIRD_IN_FOR:
   1573 		error_at (loc, "for increment expression "
   1574 			  "cannot resolve address of overloaded function");
   1575 		break;
   1576 	    }
   1577 	else
   1578 	  return error_mark_node;
   1579 	expr = void_node;
   1580       }
   1581     else if (implicit != ICV_CAST && probe == expr && is_overloaded_fn (probe))
   1582       {
   1583 	/* Only warn when there is no &.  */
   1584 	if (complain & tf_warning)
   1585 	  switch (implicit)
   1586 	    {
   1587 	      case ICV_SECOND_OF_COND:
   1588 	        warning_at (loc, OPT_Waddress,
   1589 			    "second operand of conditional expression "
   1590 			    "is a reference, not call, to function %qE", expr);
   1591 		break;
   1592 	      case ICV_THIRD_OF_COND:
   1593 	        warning_at (loc, OPT_Waddress,
   1594 			    "third operand of conditional expression "
   1595 			    "is a reference, not call, to function %qE", expr);
   1596 		break;
   1597 	      case ICV_RIGHT_OF_COMMA:
   1598 		warning_at (loc, OPT_Waddress,
   1599 			    "right operand of comma operator "
   1600 			    "is a reference, not call, to function %qE", expr);
   1601 		break;
   1602 	      case ICV_LEFT_OF_COMMA:
   1603 	        warning_at (loc, OPT_Waddress,
   1604 			    "left operand of comma operator "
   1605 			    "is a reference, not call, to function %qE", expr);
   1606 		break;
   1607 	      case ICV_STATEMENT:
   1608 	        warning_at (loc, OPT_Waddress,
   1609 			    "statement is a reference, not call, to function %qE",
   1610 			    expr);
   1611 		break;
   1612 	      case ICV_THIRD_IN_FOR:
   1613 	        warning_at (loc, OPT_Waddress,
   1614 			    "for increment expression "
   1615 			    "is a reference, not call, to function %qE", expr);
   1616 		break;
   1617 	      default:
   1618 	        gcc_unreachable ();
   1619 	    }
   1620 
   1621 	if (TREE_CODE (expr) == COMPONENT_REF)
   1622 	  expr = TREE_OPERAND (expr, 0);
   1623       }
   1624   }
   1625 
   1626   if (expr != error_mark_node && !VOID_TYPE_P (TREE_TYPE (expr)))
   1627     {
   1628       if (implicit != ICV_CAST
   1629 	  && warn_unused_value
   1630 	  && !warning_suppressed_p (expr, OPT_Wunused_value)
   1631 	  && !processing_template_decl
   1632 	  && !cp_unevaluated_operand
   1633 	  && (complain & tf_warning))
   1634 	{
   1635 	  /* The middle end does not warn about expressions that have
   1636 	     been explicitly cast to void, so we must do so here.  */
   1637 	  if (!TREE_SIDE_EFFECTS (expr))
   1638 	    {
   1639 	      switch (implicit)
   1640 		{
   1641 		  case ICV_SECOND_OF_COND:
   1642 		    warning_at (loc, OPT_Wunused_value,
   1643 				"second operand of conditional expression "
   1644 				"has no effect");
   1645 		    break;
   1646 		  case ICV_THIRD_OF_COND:
   1647 		    warning_at (loc, OPT_Wunused_value,
   1648 				"third operand of conditional expression "
   1649 				"has no effect");
   1650 		    break;
   1651 		  case ICV_RIGHT_OF_COMMA:
   1652 		    warning_at (loc, OPT_Wunused_value,
   1653 				"right operand of comma operator has no effect");
   1654 		    break;
   1655 		  case ICV_LEFT_OF_COMMA:
   1656 		    warning_at (loc, OPT_Wunused_value,
   1657 				"left operand of comma operator has no effect");
   1658 		    break;
   1659 		  case ICV_STATEMENT:
   1660 		    warning_at (loc, OPT_Wunused_value,
   1661 				"statement has no effect");
   1662 		    break;
   1663 		  case ICV_THIRD_IN_FOR:
   1664 		    warning_at (loc, OPT_Wunused_value,
   1665 				"for increment expression has no effect");
   1666 		    break;
   1667 		  default:
   1668 		    gcc_unreachable ();
   1669 		}
   1670 	    }
   1671 	  else
   1672 	    {
   1673 	      tree e = expr;
   1674 	      /* We might like to warn about (say) "(int) f()", as the
   1675 		 cast has no effect, but the compiler itself will
   1676 		 generate implicit conversions under some
   1677 		 circumstances.  (For example a block copy will be
   1678 		 turned into a call to "__builtin_memcpy", with a
   1679 		 conversion of the return value to an appropriate
   1680 		 type.)  So, to avoid false positives, we strip
   1681 		 conversions.  Do not use STRIP_NOPs because it will
   1682 		 not strip conversions to "void", as that is not a
   1683 		 mode-preserving conversion.  */
   1684 	      while (TREE_CODE (e) == NOP_EXPR)
   1685 		e = TREE_OPERAND (e, 0);
   1686 
   1687 	      enum tree_code code = TREE_CODE (e);
   1688 	      enum tree_code_class tclass = TREE_CODE_CLASS (code);
   1689 	      if (tclass == tcc_comparison
   1690 		  || tclass == tcc_unary
   1691 		  || tclass == tcc_binary
   1692 		  || code == VEC_PERM_EXPR
   1693 		  || code == VEC_COND_EXPR)
   1694 		warn_if_unused_value (e, loc);
   1695 	    }
   1696 	}
   1697       expr = build1 (CONVERT_EXPR, void_type_node, expr);
   1698     }
   1699   if (! TREE_SIDE_EFFECTS (expr))
   1700     expr = void_node;
   1701   return expr;
   1702 }
   1703 
   1704 /* Create an expression whose value is that of EXPR,
   1705    converted to type TYPE.  The TREE_TYPE of the value
   1706    is always TYPE.  This function implements all reasonable
   1707    conversions; callers should filter out those that are
   1708    not permitted by the language being compiled.
   1709 
   1710    Most of this routine is from build_reinterpret_cast.
   1711 
   1712    The back end cannot call cp_convert (what was convert) because
   1713    conversions to/from basetypes may involve memory references
   1714    (vbases) and adding or subtracting small values (multiple
   1715    inheritance), but it calls convert from the constant folding code
   1716    on subtrees of already built trees after it has ripped them apart.
   1717 
   1718    Also, if we ever support range variables, we'll probably also have to
   1719    do a little bit more work.  */
   1720 
   1721 tree
   1722 convert (tree type, tree expr)
   1723 {
   1724   tree intype;
   1725 
   1726   if (type == error_mark_node || expr == error_mark_node)
   1727     return error_mark_node;
   1728 
   1729   intype = TREE_TYPE (expr);
   1730 
   1731   if (INDIRECT_TYPE_P (type) && INDIRECT_TYPE_P (intype))
   1732     return build_nop (type, expr);
   1733 
   1734   return ocp_convert (type, expr, CONV_BACKEND_CONVERT,
   1735 		      LOOKUP_NORMAL|LOOKUP_NO_CONVERSION,
   1736 		      tf_warning_or_error);
   1737 }
   1738 
   1739 /* Like convert, but in a static initializer (called from
   1740    convert_and_check).  */
   1741 
   1742 tree
   1743 convert_init (tree type, tree expr)
   1744 {
   1745   return convert (type, expr);
   1746 }
   1747 
   1748 /* Like cp_convert, except permit conversions to take place which
   1749    are not normally allowed due to access restrictions
   1750    (such as conversion from sub-type to private super-type).  */
   1751 
   1752 tree
   1753 convert_force (tree type, tree expr, int convtype, tsubst_flags_t complain)
   1754 {
   1755   tree e = expr;
   1756   enum tree_code code = TREE_CODE (type);
   1757 
   1758   if (code == REFERENCE_TYPE)
   1759     return convert_to_reference (type, e, CONV_C_CAST, 0,
   1760 				 NULL_TREE, complain);
   1761 
   1762   if (code == POINTER_TYPE)
   1763     return convert_to_pointer_force (type, e, complain);
   1764 
   1765   /* From typeck.cc convert_for_assignment */
   1766   if (((TYPE_PTR_P (TREE_TYPE (e)) && TREE_CODE (e) == ADDR_EXPR
   1767 	&& TREE_CODE (TREE_TYPE (TREE_TYPE (e))) == METHOD_TYPE)
   1768        || integer_zerop (e)
   1769        || TYPE_PTRMEMFUNC_P (TREE_TYPE (e)))
   1770       && TYPE_PTRMEMFUNC_P (type))
   1771     /* compatible pointer to member functions.  */
   1772     return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), e, 1,
   1773 			     /*c_cast_p=*/1, complain);
   1774 
   1775   return ocp_convert (type, e, CONV_C_CAST|convtype, LOOKUP_NORMAL, complain);
   1776 }
   1777 
   1778 /* Convert an aggregate EXPR to type XTYPE.  If a conversion
   1779    exists, return the attempted conversion.  This may
   1780    return ERROR_MARK_NODE if the conversion is not
   1781    allowed (references private members, etc).
   1782    If no conversion exists, NULL_TREE is returned.
   1783 
   1784    FIXME: Ambiguity checking is wrong.  Should choose one by the implicit
   1785    object parameter, or by the second standard conversion sequence if
   1786    that doesn't do it.  This will probably wait for an overloading rewrite.
   1787    (jason 8/9/95)  */
   1788 
   1789 static tree
   1790 build_type_conversion (tree xtype, tree expr)
   1791 {
   1792   /* C++: check to see if we can convert this aggregate type
   1793      into the required type.  */
   1794   return build_user_type_conversion (xtype, expr, LOOKUP_NORMAL,
   1795 				     tf_warning_or_error);
   1796 }
   1797 
   1798 /* Convert the given EXPR to one of a group of types suitable for use in an
   1799    expression.  DESIRES is a combination of various WANT_* flags (q.v.)
   1800    which indicates which types are suitable.  If COMPLAIN is true, complain
   1801    about ambiguity; otherwise, the caller will deal with it.  */
   1802 
   1803 tree
   1804 build_expr_type_conversion (int desires, tree expr, bool complain)
   1805 {
   1806   tree basetype = TREE_TYPE (expr);
   1807   tree conv = NULL_TREE;
   1808   tree winner = NULL_TREE;
   1809 
   1810   if (null_node_p (expr)
   1811       && (desires & WANT_INT)
   1812       && !(desires & WANT_NULL))
   1813     {
   1814       location_t loc =
   1815 	expansion_point_location_if_in_system_header (input_location);
   1816 
   1817       warning_at (loc, OPT_Wconversion_null,
   1818 		  "converting NULL to non-pointer type");
   1819     }
   1820 
   1821   if (basetype == error_mark_node)
   1822     return error_mark_node;
   1823 
   1824   if (! MAYBE_CLASS_TYPE_P (basetype))
   1825     switch (TREE_CODE (basetype))
   1826       {
   1827       case INTEGER_TYPE:
   1828 	if ((desires & WANT_NULL) && null_ptr_cst_p (expr))
   1829 	  return expr;
   1830 	/* fall through.  */
   1831 
   1832       case BOOLEAN_TYPE:
   1833 	return (desires & WANT_INT) ? expr : NULL_TREE;
   1834       case ENUMERAL_TYPE:
   1835 	return (desires & WANT_ENUM) ? expr : NULL_TREE;
   1836       case REAL_TYPE:
   1837 	return (desires & WANT_FLOAT) ? expr : NULL_TREE;
   1838       case POINTER_TYPE:
   1839 	return (desires & WANT_POINTER) ? expr : NULL_TREE;
   1840 
   1841       case FUNCTION_TYPE:
   1842       case ARRAY_TYPE:
   1843 	return (desires & WANT_POINTER) ? decay_conversion (expr,
   1844 							    tf_warning_or_error)
   1845 					: NULL_TREE;
   1846 
   1847       case VECTOR_TYPE:
   1848 	if (!gnu_vector_type_p (basetype))
   1849 	  return NULL_TREE;
   1850 	/* FALLTHROUGH */
   1851       case COMPLEX_TYPE:
   1852 	if ((desires & WANT_VECTOR_OR_COMPLEX) == 0)
   1853 	  return NULL_TREE;
   1854 	switch (TREE_CODE (TREE_TYPE (basetype)))
   1855 	  {
   1856 	  case INTEGER_TYPE:
   1857 	  case BOOLEAN_TYPE:
   1858 	    return (desires & WANT_INT) ? expr : NULL_TREE;
   1859 	  case ENUMERAL_TYPE:
   1860 	    return (desires & WANT_ENUM) ? expr : NULL_TREE;
   1861 	  case REAL_TYPE:
   1862 	    return (desires & WANT_FLOAT) ? expr : NULL_TREE;
   1863 	  default:
   1864 	    return NULL_TREE;
   1865 	  }
   1866 
   1867       default:
   1868 	return NULL_TREE;
   1869       }
   1870 
   1871   /* The code for conversions from class type is currently only used for
   1872      delete expressions.  Other expressions are handled by build_new_op.  */
   1873   if (!complete_type_or_maybe_complain (basetype, expr, complain))
   1874     return error_mark_node;
   1875   if (!TYPE_HAS_CONVERSION (basetype))
   1876     return NULL_TREE;
   1877 
   1878   for (conv = lookup_conversions (basetype); conv; conv = TREE_CHAIN (conv))
   1879     {
   1880       int win = 0;
   1881       tree candidate;
   1882       tree cand = TREE_VALUE (conv);
   1883       cand = OVL_FIRST (cand);
   1884 
   1885       if (winner && winner == cand)
   1886 	continue;
   1887 
   1888       if (DECL_NONCONVERTING_P (cand))
   1889 	continue;
   1890 
   1891       candidate = non_reference (TREE_TYPE (TREE_TYPE (cand)));
   1892 
   1893       switch (TREE_CODE (candidate))
   1894 	{
   1895 	case BOOLEAN_TYPE:
   1896 	case INTEGER_TYPE:
   1897 	  win = (desires & WANT_INT); break;
   1898 	case ENUMERAL_TYPE:
   1899 	  win = (desires & WANT_ENUM); break;
   1900 	case REAL_TYPE:
   1901 	  win = (desires & WANT_FLOAT); break;
   1902 	case POINTER_TYPE:
   1903 	  win = (desires & WANT_POINTER); break;
   1904 
   1905 	case COMPLEX_TYPE:
   1906 	case VECTOR_TYPE:
   1907 	  if ((desires & WANT_VECTOR_OR_COMPLEX) == 0)
   1908 	    break;
   1909 	  switch (TREE_CODE (TREE_TYPE (candidate)))
   1910 	    {
   1911 	    case BOOLEAN_TYPE:
   1912 	    case INTEGER_TYPE:
   1913 	      win = (desires & WANT_INT); break;
   1914 	    case ENUMERAL_TYPE:
   1915 	      win = (desires & WANT_ENUM); break;
   1916 	    case REAL_TYPE:
   1917 	      win = (desires & WANT_FLOAT); break;
   1918 	    default:
   1919 	      break;
   1920 	    }
   1921 	  break;
   1922 
   1923 	default:
   1924 	  /* A wildcard could be instantiated to match any desired
   1925 	     type, but we can't deduce the template argument.  */
   1926 	  if (WILDCARD_TYPE_P (candidate))
   1927 	    win = true;
   1928 	  break;
   1929 	}
   1930 
   1931       if (win)
   1932 	{
   1933 	  if (TREE_CODE (cand) == TEMPLATE_DECL)
   1934 	    {
   1935 	      if (complain)
   1936 		error ("default type conversion cannot deduce template"
   1937 		       " argument for %qD", cand);
   1938 	      return error_mark_node;
   1939 	    }
   1940 
   1941 	  if (winner)
   1942 	    {
   1943 	      tree winner_type
   1944 		= non_reference (TREE_TYPE (TREE_TYPE (winner)));
   1945 
   1946 	      if (!same_type_ignoring_top_level_qualifiers_p (winner_type,
   1947 							      candidate))
   1948 		{
   1949 		  if (complain)
   1950 		    {
   1951 		      error ("ambiguous default type conversion from %qT",
   1952 			     basetype);
   1953 		      inform (input_location,
   1954 			      "  candidate conversions include %qD and %qD",
   1955 			      winner, cand);
   1956 		    }
   1957 		  return error_mark_node;
   1958 		}
   1959 	    }
   1960 
   1961 	  winner = cand;
   1962 	}
   1963     }
   1964 
   1965   if (winner)
   1966     {
   1967       tree type = non_reference (TREE_TYPE (TREE_TYPE (winner)));
   1968       return build_user_type_conversion (type, expr, LOOKUP_NORMAL,
   1969 					 tf_warning_or_error);
   1970     }
   1971 
   1972   return NULL_TREE;
   1973 }
   1974 
   1975 /* Implements integral promotion (4.1) and float->double promotion.  */
   1976 
   1977 tree
   1978 type_promotes_to (tree type)
   1979 {
   1980   tree promoted_type;
   1981 
   1982   if (type == error_mark_node)
   1983     return error_mark_node;
   1984 
   1985   type = TYPE_MAIN_VARIANT (type);
   1986 
   1987   /* Check for promotions of target-defined types first.  */
   1988   promoted_type = targetm.promoted_type (type);
   1989   if (promoted_type)
   1990     return promoted_type;
   1991 
   1992   /* bool always promotes to int (not unsigned), even if it's the same
   1993      size.  */
   1994   if (TREE_CODE (type) == BOOLEAN_TYPE)
   1995     type = integer_type_node;
   1996 
   1997   /* Normally convert enums to int, but convert wide enums to something
   1998      wider.  Scoped enums don't promote, but pretend they do for backward
   1999      ABI bug compatibility wrt varargs.  */
   2000   else if (TREE_CODE (type) == ENUMERAL_TYPE
   2001 	   || type == char8_type_node
   2002 	   || type == char16_type_node
   2003 	   || type == char32_type_node
   2004 	   || type == wchar_type_node)
   2005     {
   2006       tree prom = type;
   2007 
   2008       if (TREE_CODE (type) == ENUMERAL_TYPE)
   2009 	{
   2010 	  prom = ENUM_UNDERLYING_TYPE (prom);
   2011 	  if (!ENUM_IS_SCOPED (type)
   2012 	      && ENUM_FIXED_UNDERLYING_TYPE_P (type))
   2013 	    {
   2014 	      /* ISO C++17, 7.6/4.  A prvalue of an unscoped enumeration type
   2015 		 whose underlying type is fixed (10.2) can be converted to a
   2016 		 prvalue of its underlying type. Moreover, if integral promotion
   2017 		 can be applied to its underlying type, a prvalue of an unscoped
   2018 		 enumeration type whose underlying type is fixed can also be
   2019 		 converted to a prvalue of the promoted underlying type.  */
   2020 	      return type_promotes_to (prom);
   2021 	    }
   2022 	}
   2023 
   2024       int precision = MAX (TYPE_PRECISION (type),
   2025 			   TYPE_PRECISION (integer_type_node));
   2026       tree totype = c_common_type_for_size (precision, 0);
   2027       if (TYPE_UNSIGNED (prom)
   2028 	  && ! int_fits_type_p (TYPE_MAX_VALUE (prom), totype))
   2029 	prom = c_common_type_for_size (precision, 1);
   2030       else
   2031 	prom = totype;
   2032       if (SCOPED_ENUM_P (type))
   2033 	{
   2034 	  if (abi_version_crosses (6)
   2035 	      && TYPE_MODE (prom) != TYPE_MODE (type))
   2036 	    warning (OPT_Wabi, "scoped enum %qT passed through %<...%> as "
   2037 		     "%qT before %<-fabi-version=6%>, %qT after",
   2038 		     type, prom, ENUM_UNDERLYING_TYPE (type));
   2039 	  if (!abi_version_at_least (6))
   2040 	    type = prom;
   2041 	}
   2042       else
   2043 	type = prom;
   2044     }
   2045   else if (c_promoting_integer_type_p (type))
   2046     {
   2047       /* Retain unsignedness if really not getting bigger.  */
   2048       if (TYPE_UNSIGNED (type)
   2049 	  && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
   2050 	type = unsigned_type_node;
   2051       else
   2052 	type = integer_type_node;
   2053     }
   2054   else if (type == float_type_node)
   2055     type = double_type_node;
   2056 
   2057   return type;
   2058 }
   2059 
   2060 /* The routines below this point are carefully written to conform to
   2061    the standard.  They use the same terminology, and follow the rules
   2062    closely.  Although they are used only in pt.cc at the moment, they
   2063    should presumably be used everywhere in the future.  */
   2064 
   2065 /* True iff EXPR can be converted to TYPE via a qualification conversion.
   2066    Callers should check for identical types before calling this function.  */
   2067 
   2068 bool
   2069 can_convert_qual (tree type, tree expr)
   2070 {
   2071   tree expr_type = TREE_TYPE (expr);
   2072   gcc_assert (!same_type_p (type, expr_type));
   2073 
   2074   /* A function pointer conversion also counts as a Qualification Adjustment
   2075      under [over.ics.scs].  */
   2076   if (fnptr_conv_p (type, expr_type))
   2077     return true;
   2078 
   2079   if (TYPE_PTR_P (type) && TYPE_PTR_P (expr_type))
   2080     return comp_ptr_ttypes (TREE_TYPE (type), TREE_TYPE (expr_type));
   2081   else if (TYPE_PTRMEM_P (type) && TYPE_PTRMEM_P (expr_type))
   2082     return (same_type_p (TYPE_PTRMEM_CLASS_TYPE (type),
   2083 			 TYPE_PTRMEM_CLASS_TYPE (expr_type))
   2084 	    && comp_ptr_ttypes (TYPE_PTRMEM_POINTED_TO_TYPE (type),
   2085 				TYPE_PTRMEM_POINTED_TO_TYPE (expr_type)));
   2086   else
   2087     return false;
   2088 }
   2089 
   2090 /* Attempt to perform qualification conversions on EXPR to convert it
   2091    to TYPE.  Return the resulting expression, or error_mark_node if
   2092    the conversion was impossible.  Since this is only used by
   2093    convert_nontype_argument, we fold the conversion.  */
   2094 
   2095 tree
   2096 perform_qualification_conversions (tree type, tree expr)
   2097 {
   2098   tree expr_type;
   2099 
   2100   expr_type = TREE_TYPE (expr);
   2101 
   2102   if (same_type_p (type, expr_type))
   2103     return expr;
   2104   else if (can_convert_qual (type, expr))
   2105     return cp_fold_convert (type, expr);
   2106   else
   2107     return error_mark_node;
   2108 }
   2109 
   2110 /* True iff T is a transaction-safe function type.  */
   2111 
   2112 bool
   2113 tx_safe_fn_type_p (tree t)
   2114 {
   2115   if (!FUNC_OR_METHOD_TYPE_P (t))
   2116     return false;
   2117   return !!lookup_attribute ("transaction_safe", TYPE_ATTRIBUTES (t));
   2118 }
   2119 
   2120 /* Return the transaction-unsafe variant of transaction-safe function type
   2121    T.  */
   2122 
   2123 tree
   2124 tx_unsafe_fn_variant (tree t)
   2125 {
   2126   gcc_assert (tx_safe_fn_type_p (t));
   2127   tree attrs = remove_attribute ("transaction_safe",
   2128 				 TYPE_ATTRIBUTES (t));
   2129   return cp_build_type_attribute_variant (t, attrs);
   2130 }
   2131 
   2132 /* Return true iff FROM can convert to TO by a transaction-safety
   2133    conversion.  */
   2134 
   2135 static bool
   2136 can_convert_tx_safety (tree to, tree from)
   2137 {
   2138   return (flag_tm && tx_safe_fn_type_p (from)
   2139 	  && same_type_p (to, tx_unsafe_fn_variant (from)));
   2140 }
   2141 
   2142 /* Return true iff FROM can convert to TO by dropping noexcept.
   2143    This is just a subroutine of fnptr_conv_p.  */
   2144 
   2145 static bool
   2146 noexcept_conv_p (tree to, tree from)
   2147 {
   2148   if (!flag_noexcept_type)
   2149     return false;
   2150 
   2151   if (TREE_CODE (to) != TREE_CODE (from))
   2152     return false;
   2153   if (!FUNC_OR_METHOD_TYPE_P (from))
   2154     return false;
   2155   if (!type_throw_all_p (to)
   2156       || type_throw_all_p (from))
   2157     return false;
   2158   tree v = build_exception_variant (from, NULL_TREE);
   2159   return same_type_p (to, v);
   2160 }
   2161 
   2162 /* Return true iff FROM can convert to TO by a function pointer conversion.  */
   2163 
   2164 bool
   2165 fnptr_conv_p (tree to, tree from)
   2166 {
   2167   tree t = to;
   2168   tree f = from;
   2169   if (TYPE_PTRMEMFUNC_P (t)
   2170       && TYPE_PTRMEMFUNC_P (f))
   2171     {
   2172       t = TYPE_PTRMEMFUNC_FN_TYPE (t);
   2173       f = TYPE_PTRMEMFUNC_FN_TYPE (f);
   2174     }
   2175   if (INDIRECT_TYPE_P (t)
   2176       && INDIRECT_TYPE_P (f))
   2177     {
   2178       t = TREE_TYPE (t);
   2179       f = TREE_TYPE (f);
   2180     }
   2181 
   2182   return (noexcept_conv_p (t, f)
   2183 	  || can_convert_tx_safety (t, f));
   2184 }
   2185 
   2186 /* Return FN with any NOP_EXPRs stripped that represent function pointer
   2187    conversions or conversions to the same type.  */
   2188 
   2189 tree
   2190 strip_fnptr_conv (tree fn)
   2191 {
   2192   while (TREE_CODE (fn) == NOP_EXPR)
   2193     {
   2194       tree op = TREE_OPERAND (fn, 0);
   2195       tree ft = TREE_TYPE (fn);
   2196       tree ot = TREE_TYPE (op);
   2197       if (same_type_p (ft, ot)
   2198 	  || fnptr_conv_p (ft, ot))
   2199 	fn = op;
   2200       else
   2201 	break;
   2202     }
   2203   return fn;
   2204 }
   2205