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