1/* Language-level data type conversion for GNU C++. 2 Copyright (C) 1987, 88, 92-97, 1998 Free Software Foundation, Inc. 3 Hacked by Michael Tiemann (tiemann@cygnus.com) 4 5This file is part of GNU CC. 6 7GNU CC 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 2, or (at your option) 10any later version. 11 12GNU CC 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 GNU CC; see the file COPYING. If not, write to 19the Free Software Foundation, 59 Temple Place - Suite 330, 20Boston, MA 02111-1307, USA. */ 21 22 23/* This file contains the functions for converting C expressions 24 to different data types. The only entry point is `convert'. 25 Every language front end must have a `convert' function 26 but what kind of conversions it does will depend on the language. */ 27 28#include "config.h" 29#include "system.h" 30#include "tree.h" 31#include "flags.h" 32#include "cp-tree.h" 33#include "convert.h" 34#include "toplev.h" 35#include "decl.h" 36 37static tree cp_convert_to_pointer PROTO((tree, tree)); 38static tree convert_to_pointer_force PROTO((tree, tree)); 39static tree build_up_reference PROTO((tree, tree, int)); 40 41/* Change of width--truncation and extension of integers or reals-- 42 is represented with NOP_EXPR. Proper functioning of many things 43 assumes that no other conversions can be NOP_EXPRs. 44 45 Conversion between integer and pointer is represented with CONVERT_EXPR. 46 Converting integer to real uses FLOAT_EXPR 47 and real to integer uses FIX_TRUNC_EXPR. 48 49 Here is a list of all the functions that assume that widening and 50 narrowing is always done with a NOP_EXPR: 51 In convert.c, convert_to_integer. 52 In c-typeck.c, build_binary_op_nodefault (boolean ops), 53 and truthvalue_conversion. 54 In expr.c: expand_expr, for operands of a MULT_EXPR. 55 In fold-const.c: fold. 56 In tree.c: get_narrower and get_unwidened. 57 58 C++: in multiple-inheritance, converting between pointers may involve 59 adjusting them by a delta stored within the class definition. */ 60 61/* Subroutines of `convert'. */ 62 63/* if converting pointer to pointer 64 if dealing with classes, check for derived->base or vice versa 65 else if dealing with method pointers, delegate 66 else convert blindly 67 else if converting class, pass off to build_type_conversion 68 else try C-style pointer conversion */ 69 70static tree 71cp_convert_to_pointer (type, expr) 72 tree type, expr; 73{ 74 register tree intype = TREE_TYPE (expr); 75 register enum tree_code form; 76 tree rval; 77 78 if (IS_AGGR_TYPE (intype)) 79 { 80 intype = complete_type (intype); 81 if (TYPE_SIZE (intype) == NULL_TREE) 82 { 83 cp_error ("can't convert from incomplete type `%T' to `%T'", 84 intype, type); 85 return error_mark_node; 86 } 87 88 rval = build_type_conversion (type, expr, 1); 89 if (rval) 90 { 91 if (rval == error_mark_node) 92 cp_error ("conversion of `%E' from `%T' to `%T' is ambiguous", 93 expr, intype, type); 94 return rval; 95 } 96 } 97 98 /* Handle anachronistic conversions from (::*)() to cv void* or (*)(). */ 99 if (TREE_CODE (type) == POINTER_TYPE 100 && (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE 101 || TYPE_MAIN_VARIANT (TREE_TYPE (type)) == void_type_node)) 102 { 103 /* Allow an implicit this pointer for pointer to member 104 functions. */ 105 if (TYPE_PTRMEMFUNC_P (intype)) 106 { 107 tree fntype = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (intype)); 108 tree decl = maybe_dummy_object (TYPE_METHOD_BASETYPE (fntype), 0); 109 expr = build (OFFSET_REF, fntype, decl, expr); 110 } 111 112 if (TREE_CODE (expr) == OFFSET_REF 113 && TREE_CODE (TREE_TYPE (expr)) == METHOD_TYPE) 114 expr = resolve_offset_ref (expr); 115 if (TREE_CODE (TREE_TYPE (expr)) == METHOD_TYPE) 116 expr = build_addr_func (expr); 117 if (TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE) 118 { 119 if (TREE_CODE (TREE_TYPE (TREE_TYPE (expr))) == METHOD_TYPE) 120 if (pedantic || warn_pmf2ptr) 121 cp_pedwarn ("converting from `%T' to `%T'", TREE_TYPE (expr), 122 type); 123 return build1 (NOP_EXPR, type, expr); 124 } 125 intype = TREE_TYPE (expr); 126 } 127 128 form = TREE_CODE (intype); 129 130 if (POINTER_TYPE_P (intype)) 131 { 132 intype = TYPE_MAIN_VARIANT (intype); 133 134 if (TYPE_MAIN_VARIANT (type) != intype 135 && TREE_CODE (type) == POINTER_TYPE 136 && TREE_CODE (TREE_TYPE (type)) == RECORD_TYPE 137 && IS_AGGR_TYPE (TREE_TYPE (type)) 138 && IS_AGGR_TYPE (TREE_TYPE (intype)) 139 && TREE_CODE (TREE_TYPE (intype)) == RECORD_TYPE 140 /* If EXPR is NULL, then we don't need to do any arithmetic 141 to convert it: 142 143 [conv.ptr] 144 145 The null pointer value is converted to the null pointer 146 value of the destination type. */ 147 && !integer_zerop (expr)) 148 { 149 enum tree_code code = PLUS_EXPR; 150 tree binfo = get_binfo (TREE_TYPE (type), TREE_TYPE (intype), 1); 151 if (binfo == error_mark_node) 152 return error_mark_node; 153 if (binfo == NULL_TREE) 154 { 155 binfo = get_binfo (TREE_TYPE (intype), TREE_TYPE (type), 1); 156 if (binfo == error_mark_node) 157 return error_mark_node; 158 code = MINUS_EXPR; 159 } 160 if (binfo) 161 { 162 if (TYPE_USES_VIRTUAL_BASECLASSES (TREE_TYPE (type)) 163 || TYPE_USES_VIRTUAL_BASECLASSES (TREE_TYPE (intype)) 164 || ! BINFO_OFFSET_ZEROP (binfo)) 165 { 166 /* Need to get the path we took. */ 167 tree path; 168 169 if (code == PLUS_EXPR) 170 get_base_distance (TREE_TYPE (type), TREE_TYPE (intype), 171 0, &path); 172 else 173 get_base_distance (TREE_TYPE (intype), TREE_TYPE (type), 174 0, &path); 175 return build_vbase_path (code, type, expr, path, 0); 176 } 177 } 178 } 179 180 if (TYPE_PTRMEM_P (type) && TYPE_PTRMEM_P (intype)) 181 { 182 tree b1 = TYPE_OFFSET_BASETYPE (TREE_TYPE (type)); 183 tree b2 = TYPE_OFFSET_BASETYPE (TREE_TYPE (intype)); 184 tree binfo = get_binfo (b2, b1, 1); 185 enum tree_code code = PLUS_EXPR; 186 187 if (binfo == NULL_TREE) 188 { 189 binfo = get_binfo (b1, b2, 1); 190 code = MINUS_EXPR; 191 } 192 193 if (binfo == error_mark_node) 194 return error_mark_node; 195 if (binfo && ! TREE_VIA_VIRTUAL (binfo)) 196 expr = size_binop (code, expr, BINFO_OFFSET (binfo)); 197 } 198 else if (TYPE_PTRMEMFUNC_P (type)) 199 { 200 cp_error ("cannot convert `%E' from type `%T' to type `%T'", 201 expr, intype, type); 202 return error_mark_node; 203 } 204 205 rval = build1 (NOP_EXPR, type, expr); 206 TREE_CONSTANT (rval) = TREE_CONSTANT (expr); 207 return rval; 208 } 209 else if (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype)) 210 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), expr, 1); 211 else if (TYPE_PTRMEMFUNC_P (intype)) 212 { 213 cp_error ("cannot convert `%E' from type `%T' to type `%T'", 214 expr, intype, type); 215 return error_mark_node; 216 } 217 218 my_friendly_assert (form != OFFSET_TYPE, 186); 219 220 if (TYPE_LANG_SPECIFIC (intype) 221 && (IS_SIGNATURE_POINTER (intype) || IS_SIGNATURE_REFERENCE (intype))) 222 return convert_to_pointer (type, build_optr_ref (expr)); 223 224 if (integer_zerop (expr)) 225 { 226 if (TYPE_PTRMEMFUNC_P (type)) 227 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), expr, 0); 228 expr = build_int_2 (0, 0); 229 TREE_TYPE (expr) = type; 230 return expr; 231 } 232 233 if (INTEGRAL_CODE_P (form)) 234 { 235 if (TYPE_PRECISION (intype) == POINTER_SIZE) 236 return build1 (CONVERT_EXPR, type, expr); 237 expr = cp_convert (type_for_size (POINTER_SIZE, 0), expr); 238 /* Modes may be different but sizes should be the same. */ 239 if (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (expr))) 240 != GET_MODE_SIZE (TYPE_MODE (type))) 241 /* There is supposed to be some integral type 242 that is the same width as a pointer. */ 243 abort (); 244 return convert_to_pointer (type, expr); 245 } 246 247 if (type_unknown_p (expr)) 248 return instantiate_type (type, expr, 1); 249 250 cp_error ("cannot convert `%E' from type `%T' to type `%T'", 251 expr, intype, type); 252 return error_mark_node; 253} 254 255/* Like convert, except permit conversions to take place which 256 are not normally allowed due to access restrictions 257 (such as conversion from sub-type to private super-type). */ 258 259static tree 260convert_to_pointer_force (type, expr) 261 tree type, expr; 262{ 263 register tree intype = TREE_TYPE (expr); 264 register enum tree_code form = TREE_CODE (intype); 265 266 if (integer_zerop (expr)) 267 { 268 expr = build_int_2 (0, 0); 269 TREE_TYPE (expr) = type; 270 return expr; 271 } 272 273 /* Convert signature pointer/reference to `void *' first. */ 274 if (form == RECORD_TYPE 275 && (IS_SIGNATURE_POINTER (intype) || IS_SIGNATURE_REFERENCE (intype))) 276 { 277 expr = build_optr_ref (expr); 278 intype = TREE_TYPE (expr); 279 form = TREE_CODE (intype); 280 } 281 282 if (form == POINTER_TYPE) 283 { 284 intype = TYPE_MAIN_VARIANT (intype); 285 286 if (TYPE_MAIN_VARIANT (type) != intype 287 && TREE_CODE (TREE_TYPE (type)) == RECORD_TYPE 288 && IS_AGGR_TYPE (TREE_TYPE (type)) 289 && IS_AGGR_TYPE (TREE_TYPE (intype)) 290 && TREE_CODE (TREE_TYPE (intype)) == RECORD_TYPE) 291 { 292 enum tree_code code = PLUS_EXPR; 293 tree path; 294 int distance = get_base_distance (TREE_TYPE (type), 295 TREE_TYPE (intype), 0, &path); 296 if (distance == -2) 297 { 298 ambig: 299 cp_error ("type `%T' is ambiguous baseclass of `%s'", 300 TREE_TYPE (type), 301 TYPE_NAME_STRING (TREE_TYPE (intype))); 302 return error_mark_node; 303 } 304 if (distance == -1) 305 { 306 distance = get_base_distance (TREE_TYPE (intype), 307 TREE_TYPE (type), 0, &path); 308 if (distance == -2) 309 goto ambig; 310 if (distance < 0) 311 /* Doesn't need any special help from us. */ 312 return build1 (NOP_EXPR, type, expr); 313 314 code = MINUS_EXPR; 315 } 316 return build_vbase_path (code, type, expr, path, 0); 317 } 318 } 319 320 return cp_convert_to_pointer (type, expr); 321} 322 323/* We are passing something to a function which requires a reference. 324 The type we are interested in is in TYPE. The initial 325 value we have to begin with is in ARG. 326 327 FLAGS controls how we manage access checking. 328 DIRECT_BIND in FLAGS controls how any temporaries are generated. */ 329 330static tree 331build_up_reference (type, arg, flags) 332 tree type, arg; 333 int flags; 334{ 335 tree rval; 336 tree argtype = TREE_TYPE (arg); 337 tree target_type = TREE_TYPE (type); 338 339 my_friendly_assert (TREE_CODE (type) == REFERENCE_TYPE, 187); 340 341 if ((flags & DIRECT_BIND) && ! real_lvalue_p (arg)) 342 { 343 tree targ = arg; 344 if (toplevel_bindings_p ()) 345 arg = get_temp_name (argtype, 1); 346 else 347 { 348 arg = pushdecl (build_decl (VAR_DECL, NULL_TREE, argtype)); 349 DECL_ARTIFICIAL (arg) = 1; 350 } 351 DECL_INITIAL (arg) = targ; 352 cp_finish_decl (arg, targ, NULL_TREE, 0, 353 LOOKUP_ONLYCONVERTING|DIRECT_BIND); 354 } 355 else if (!(flags & DIRECT_BIND) && ! lvalue_p (arg)) 356 { 357 tree slot = build_decl (VAR_DECL, NULL_TREE, argtype); 358 DECL_ARTIFICIAL (slot) = 1; 359 arg = build (TARGET_EXPR, argtype, slot, arg, NULL_TREE, NULL_TREE); 360 TREE_SIDE_EFFECTS (arg) = 1; 361 } 362 363 /* If we had a way to wrap this up, and say, if we ever needed it's 364 address, transform all occurrences of the register, into a memory 365 reference we could win better. */ 366 rval = build_unary_op (ADDR_EXPR, arg, 1); 367 if (rval == error_mark_node) 368 return error_mark_node; 369 370 if ((flags & LOOKUP_PROTECT) 371 && TYPE_MAIN_VARIANT (argtype) != TYPE_MAIN_VARIANT (target_type) 372 && IS_AGGR_TYPE (argtype) 373 && IS_AGGR_TYPE (target_type)) 374 { 375 /* We go through get_binfo for the access control. */ 376 tree binfo = get_binfo (target_type, argtype, 1); 377 if (binfo == error_mark_node) 378 return error_mark_node; 379 if (binfo == NULL_TREE) 380 return error_not_base_type (target_type, argtype); 381 rval = convert_pointer_to_real (binfo, rval); 382 } 383 else 384 rval 385 = convert_to_pointer_force (build_pointer_type (target_type), rval); 386 rval = build1 (NOP_EXPR, type, rval); 387 TREE_CONSTANT (rval) = TREE_CONSTANT (TREE_OPERAND (rval, 0)); 388 return rval; 389} 390 391/* For C++: Only need to do one-level references, but cannot 392 get tripped up on signed/unsigned differences. 393 394 DECL is either NULL_TREE or the _DECL node for a reference that is being 395 initialized. It can be error_mark_node if we don't know the _DECL but 396 we know it's an initialization. */ 397 398tree 399convert_to_reference (reftype, expr, convtype, flags, decl) 400 tree reftype, expr; 401 int convtype, flags; 402 tree decl; 403{ 404 register tree type = TYPE_MAIN_VARIANT (TREE_TYPE (reftype)); 405 register tree intype = TREE_TYPE (expr); 406 tree rval = NULL_TREE; 407 tree rval_as_conversion = NULL_TREE; 408 int i; 409 410 if (TREE_CODE (type) == FUNCTION_TYPE && intype == unknown_type_node) 411 { 412 expr = instantiate_type (type, expr, 413 (flags & LOOKUP_COMPLAIN) != 0); 414 if (expr == error_mark_node) 415 return error_mark_node; 416 417 intype = TREE_TYPE (expr); 418 } 419 420 if (TREE_CODE (intype) == REFERENCE_TYPE) 421 my_friendly_abort (364); 422 423 intype = TYPE_MAIN_VARIANT (intype); 424 425 i = comp_target_types (type, intype, 0); 426 427 if (i <= 0 && (convtype & CONV_IMPLICIT) && IS_AGGR_TYPE (intype) 428 && ! (flags & LOOKUP_NO_CONVERSION)) 429 { 430 /* Look for a user-defined conversion to lvalue that we can use. */ 431 432 rval_as_conversion 433 = build_type_conversion (reftype, expr, 1); 434 435 if (rval_as_conversion && rval_as_conversion != error_mark_node 436 && real_lvalue_p (rval_as_conversion)) 437 { 438 expr = rval_as_conversion; 439 rval_as_conversion = NULL_TREE; 440 intype = type; 441 i = 1; 442 } 443 } 444 445 if (((convtype & CONV_STATIC) && i == -1) 446 || ((convtype & CONV_IMPLICIT) && i == 1)) 447 { 448 if (flags & LOOKUP_COMPLAIN) 449 { 450 tree ttl = TREE_TYPE (reftype); 451 tree ttr = lvalue_type (expr); 452 453 /* [dcl.init.ref] says that if an rvalue is used to 454 initialize a reference, then the reference must be to a 455 non-volatile const type. */ 456 if (! real_lvalue_p (expr) 457 && !CP_TYPE_CONST_NON_VOLATILE_P (ttl)) 458 { 459 const char *msg; 460 461 if (CP_TYPE_VOLATILE_P (ttl) && decl) 462 msg = "initialization of volatile reference type `%#T'"; 463 else if (CP_TYPE_VOLATILE_P (ttl)) 464 msg = "conversion to volatile reference type `%#T'"; 465 else if (decl) 466 msg = "initialization of non-const reference type `%#T'"; 467 else 468 msg = "conversion to non-const reference type `%#T'"; 469 470 cp_pedwarn (msg, reftype); 471 cp_pedwarn ("from rvalue of type `%T'", intype); 472 } 473 else if (! (convtype & CONV_CONST) 474 && !at_least_as_qualified_p (ttl, ttr)) 475 cp_pedwarn ("conversion from `%T' to `%T' discards qualifiers", 476 ttr, reftype); 477 } 478 479 return build_up_reference (reftype, expr, flags); 480 } 481 else if ((convtype & CONV_REINTERPRET) && lvalue_p (expr)) 482 { 483 /* When casting an lvalue to a reference type, just convert into 484 a pointer to the new type and deference it. This is allowed 485 by San Diego WP section 5.2.9 paragraph 12, though perhaps it 486 should be done directly (jason). (int &)ri ---> *(int*)&ri */ 487 488 /* B* bp; A& ar = (A&)bp; is valid, but it's probably not what they 489 meant. */ 490 if (TREE_CODE (intype) == POINTER_TYPE 491 && (comptypes (TREE_TYPE (intype), type, 492 COMPARE_BASE | COMPARE_RELAXED ))) 493 cp_warning ("casting `%T' to `%T' does not dereference pointer", 494 intype, reftype); 495 496 rval = build_unary_op (ADDR_EXPR, expr, 0); 497 if (rval != error_mark_node) 498 rval = convert_force (build_pointer_type (TREE_TYPE (reftype)), 499 rval, 0); 500 if (rval != error_mark_node) 501 rval = build1 (NOP_EXPR, reftype, rval); 502 } 503 else 504 { 505 rval = convert_for_initialization (NULL_TREE, type, expr, flags, 506 "converting", 0, 0); 507 if (rval == NULL_TREE || rval == error_mark_node) 508 return rval; 509 rval = build_up_reference (reftype, rval, flags); 510 511 if (rval && ! CP_TYPE_CONST_P (TREE_TYPE (reftype))) 512 cp_pedwarn ("initializing non-const `%T' with `%T' will use a temporary", 513 reftype, intype); 514 } 515 516 if (rval) 517 { 518 /* If we found a way to convert earlier, then use it. */ 519 return rval; 520 } 521 522 my_friendly_assert (TREE_CODE (intype) != OFFSET_TYPE, 189); 523 524 if (flags & LOOKUP_COMPLAIN) 525 cp_error ("cannot convert type `%T' to type `%T'", intype, reftype); 526 527 if (flags & LOOKUP_SPECULATIVELY) 528 return NULL_TREE; 529 530 return error_mark_node; 531} 532 533/* We are using a reference VAL for its value. Bash that reference all the 534 way down to its lowest form. */ 535 536tree 537convert_from_reference (val) 538 tree val; 539{ 540 tree type = TREE_TYPE (val); 541 542 if (TREE_CODE (type) == OFFSET_TYPE) 543 type = TREE_TYPE (type); 544 if (TREE_CODE (type) == REFERENCE_TYPE) 545 return build_indirect_ref (val, NULL_PTR); 546 return val; 547} 548 549/* Call this when we know (for any reason) that expr is not, in fact, 550 zero. This routine is like convert_pointer_to, but it pays 551 attention to which specific instance of what type we want to 552 convert to. This routine should eventually become 553 convert_to_pointer after all references to convert_to_pointer 554 are removed. */ 555 556tree 557convert_pointer_to_real (binfo, expr) 558 tree binfo, expr; 559{ 560 register tree intype = TREE_TYPE (expr); 561 tree ptr_type; 562 tree type, rval; 563 564 if (intype == error_mark_node) 565 return error_mark_node; 566 567 if (TREE_CODE (binfo) == TREE_VEC) 568 type = BINFO_TYPE (binfo); 569 else if (IS_AGGR_TYPE (binfo)) 570 { 571 type = binfo; 572 } 573 else 574 { 575 type = binfo; 576 binfo = NULL_TREE; 577 } 578 579 ptr_type = cp_build_qualified_type (type, 580 CP_TYPE_QUALS (TREE_TYPE (intype))); 581 ptr_type = build_pointer_type (ptr_type); 582 if (same_type_p (ptr_type, TYPE_MAIN_VARIANT (intype))) 583 return expr; 584 585 my_friendly_assert (!integer_zerop (expr), 191); 586 587 intype = TYPE_MAIN_VARIANT (TREE_TYPE (intype)); 588 if (TREE_CODE (type) == RECORD_TYPE 589 && TREE_CODE (intype) == RECORD_TYPE 590 && type != intype) 591 { 592 tree path; 593 int distance 594 = get_base_distance (binfo, intype, 0, &path); 595 596 /* This function shouldn't be called with unqualified arguments 597 but if it is, give them an error message that they can read. */ 598 if (distance < 0) 599 { 600 cp_error ("cannot convert a pointer of type `%T' to a pointer of type `%T'", 601 intype, type); 602 603 if (distance == -2) 604 cp_error ("because `%T' is an ambiguous base class", type); 605 return error_mark_node; 606 } 607 608 return build_vbase_path (PLUS_EXPR, ptr_type, expr, path, 1); 609 } 610 rval = build1 (NOP_EXPR, ptr_type, 611 TREE_CODE (expr) == NOP_EXPR ? TREE_OPERAND (expr, 0) : expr); 612 TREE_CONSTANT (rval) = TREE_CONSTANT (expr); 613 return rval; 614} 615 616/* Call this when we know (for any reason) that expr is 617 not, in fact, zero. This routine gets a type out of the first 618 argument and uses it to search for the type to convert to. If there 619 is more than one instance of that type in the expr, the conversion is 620 ambiguous. This routine should eventually go away, and all 621 callers should use convert_to_pointer_real. */ 622 623tree 624convert_pointer_to (binfo, expr) 625 tree binfo, expr; 626{ 627 tree type; 628 629 if (TREE_CODE (binfo) == TREE_VEC) 630 type = BINFO_TYPE (binfo); 631 else if (IS_AGGR_TYPE (binfo)) 632 type = binfo; 633 else 634 type = binfo; 635 return convert_pointer_to_real (type, expr); 636} 637 638/* C++ conversions, preference to static cast conversions. */ 639 640tree 641cp_convert (type, expr) 642 tree type, expr; 643{ 644 return ocp_convert (type, expr, CONV_OLD_CONVERT, LOOKUP_NORMAL); 645} 646 647/* Conversion... 648 649 FLAGS indicates how we should behave. */ 650 651tree 652ocp_convert (type, expr, convtype, flags) 653 tree type, expr; 654 int convtype, flags; 655{ 656 register tree e = expr; 657 register enum tree_code code = TREE_CODE (type); 658 659 if (e == error_mark_node 660 || TREE_TYPE (e) == error_mark_node) 661 return error_mark_node; 662 663 if (TREE_READONLY_DECL_P (e)) 664 e = decl_constant_value (e); 665 666 if (IS_AGGR_TYPE (type) && (convtype & CONV_FORCE_TEMP) 667 /* Some internal structures (vtable_entry_type, sigtbl_ptr_type) 668 don't go through finish_struct, so they don't have the synthesized 669 constructors. So don't force a temporary. */ 670 && TYPE_HAS_CONSTRUCTOR (type)) 671 /* We need a new temporary; don't take this shortcut. */; 672 else if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (TREE_TYPE (e))) 673 { 674 if (same_type_p (type, TREE_TYPE (e))) 675 /* The call to fold will not always remove the NOP_EXPR as 676 might be expected, since if one of the types is a typedef; 677 the comparsion in fold is just equality of pointers, not a 678 call to comptypes. We don't call fold in this case because 679 that can result in infinite recursion; fold will call 680 convert, which will call ocp_convert, etc. */ 681 return e; 682 else 683 return fold (build1 (NOP_EXPR, type, e)); 684 } 685 686 if (code == VOID_TYPE && (convtype & CONV_STATIC)) 687 { 688 e = require_complete_type_in_void (e); 689 if (e != error_mark_node) 690 e = build1 (CONVERT_EXPR, void_type_node, e); 691 692 return e; 693 } 694 695#if 0 696 /* This is incorrect. A truncation can't be stripped this way. 697 Extensions will be stripped by the use of get_unwidened. */ 698 if (TREE_CODE (e) == NOP_EXPR) 699 return cp_convert (type, TREE_OPERAND (e, 0)); 700#endif 701 702 /* Just convert to the type of the member. */ 703 if (code == OFFSET_TYPE) 704 { 705 type = TREE_TYPE (type); 706 code = TREE_CODE (type); 707 } 708 709#if 0 710 if (code == REFERENCE_TYPE) 711 return fold (convert_to_reference (type, e, convtype, flags, NULL_TREE)); 712 else if (TREE_CODE (TREE_TYPE (e)) == REFERENCE_TYPE) 713 e = convert_from_reference (e); 714#endif 715 716 if (TREE_CODE (e) == OFFSET_REF) 717 e = resolve_offset_ref (e); 718 719 if (INTEGRAL_CODE_P (code)) 720 { 721 tree intype = TREE_TYPE (e); 722 /* enum = enum, enum = int, enum = float, (enum)pointer are all 723 errors. */ 724 if (TREE_CODE (type) == ENUMERAL_TYPE 725 && ((ARITHMETIC_TYPE_P (intype) && ! (convtype & CONV_STATIC)) 726 || (TREE_CODE (intype) == POINTER_TYPE))) 727 { 728 cp_pedwarn ("conversion from `%#T' to `%#T'", intype, type); 729 730 if (flag_pedantic_errors) 731 return error_mark_node; 732 } 733 if (IS_AGGR_TYPE (intype)) 734 { 735 tree rval; 736 rval = build_type_conversion (type, e, 1); 737 if (rval) 738 return rval; 739 if (flags & LOOKUP_COMPLAIN) 740 cp_error ("`%#T' used where a `%T' was expected", intype, type); 741 if (flags & LOOKUP_SPECULATIVELY) 742 return NULL_TREE; 743 return error_mark_node; 744 } 745 if (code == BOOLEAN_TYPE) 746 { 747 /* Common Ada/Pascal programmer's mistake. We always warn 748 about this since it is so bad. */ 749 if (TREE_CODE (expr) == FUNCTION_DECL) 750 cp_warning ("the address of `%D', will always be `true'", expr); 751 return truthvalue_conversion (e); 752 } 753 return fold (convert_to_integer (type, e)); 754 } 755 if (code == POINTER_TYPE || code == REFERENCE_TYPE 756 || TYPE_PTRMEMFUNC_P (type)) 757 return fold (cp_convert_to_pointer (type, e)); 758 if (code == REAL_TYPE || code == COMPLEX_TYPE) 759 { 760 if (IS_AGGR_TYPE (TREE_TYPE (e))) 761 { 762 tree rval; 763 rval = build_type_conversion (type, e, 1); 764 if (rval) 765 return rval; 766 else 767 if (flags & LOOKUP_COMPLAIN) 768 cp_error ("`%#T' used where a floating point value was expected", 769 TREE_TYPE (e)); 770 } 771 if (code == REAL_TYPE) 772 return fold (convert_to_real (type, e)); 773 else if (code == COMPLEX_TYPE) 774 return fold (convert_to_complex (type, e)); 775 } 776 777 /* New C++ semantics: since assignment is now based on 778 memberwise copying, if the rhs type is derived from the 779 lhs type, then we may still do a conversion. */ 780 if (IS_AGGR_TYPE_CODE (code)) 781 { 782 tree dtype = TREE_TYPE (e); 783 tree ctor = NULL_TREE; 784 785 dtype = TYPE_MAIN_VARIANT (dtype); 786 787 /* Conversion of object pointers or signature pointers/references 788 to signature pointers/references. */ 789 790 if (TYPE_LANG_SPECIFIC (type) 791 && (IS_SIGNATURE_POINTER (type) || IS_SIGNATURE_REFERENCE (type))) 792 { 793 tree constructor = build_signature_pointer_constructor (type, expr); 794 tree sig_ty = SIGNATURE_TYPE (type); 795 tree sig_ptr; 796 797 if (constructor == error_mark_node) 798 return error_mark_node; 799 800 sig_ptr = get_temp_name (type, 1); 801 DECL_INITIAL (sig_ptr) = constructor; 802 CLEAR_SIGNATURE (sig_ty); 803 cp_finish_decl (sig_ptr, constructor, NULL_TREE, 0, 0); 804 SET_SIGNATURE (sig_ty); 805 TREE_READONLY (sig_ptr) = 1; 806 807 return sig_ptr; 808 } 809 810 /* Conversion between aggregate types. New C++ semantics allow 811 objects of derived type to be cast to objects of base type. 812 Old semantics only allowed this between pointers. 813 814 There may be some ambiguity between using a constructor 815 vs. using a type conversion operator when both apply. */ 816 817 ctor = e; 818 819 if (IS_AGGR_TYPE (type) && CLASSTYPE_ABSTRACT_VIRTUALS (type)) 820 { 821 abstract_virtuals_error (NULL_TREE, type); 822 return error_mark_node; 823 } 824 825 if ((flags & LOOKUP_ONLYCONVERTING) 826 && ! (IS_AGGR_TYPE (dtype) && DERIVED_FROM_P (type, dtype))) 827 /* For copy-initialization, first we create a temp of the proper type 828 with a user-defined conversion sequence, then we direct-initialize 829 the target with the temp (see [dcl.init]). */ 830 ctor = build_user_type_conversion (type, ctor, flags); 831 if (ctor) 832 ctor = build_method_call (NULL_TREE, ctor_identifier, 833 build_expr_list (NULL_TREE, ctor), 834 TYPE_BINFO (type), flags); 835 if (ctor) 836 return build_cplus_new (type, ctor); 837 } 838 839 /* If TYPE or TREE_TYPE (E) is not on the permanent_obstack, 840 then it won't be hashed and hence compare as not equal, 841 even when it is. */ 842 if (code == ARRAY_TYPE 843 && TREE_TYPE (TREE_TYPE (e)) == TREE_TYPE (type) 844 && index_type_equal (TYPE_DOMAIN (TREE_TYPE (e)), TYPE_DOMAIN (type))) 845 return e; 846 847 if (flags & LOOKUP_COMPLAIN) 848 cp_error ("conversion from `%T' to non-scalar type `%T' requested", 849 TREE_TYPE (expr), type); 850 if (flags & LOOKUP_SPECULATIVELY) 851 return NULL_TREE; 852 return error_mark_node; 853} 854 855/* Create an expression whose value is that of EXPR, 856 converted to type TYPE. The TREE_TYPE of the value 857 is always TYPE. This function implements all reasonable 858 conversions; callers should filter out those that are 859 not permitted by the language being compiled. 860 861 Most of this routine is from build_reinterpret_cast. 862 863 The backend cannot call cp_convert (what was convert) because 864 conversions to/from basetypes may involve memory references 865 (vbases) and adding or subtracting small values (multiple 866 inheritance), but it calls convert from the constant folding code 867 on subtrees of already build trees after it has ripped them apart. 868 869 Also, if we ever support range variables, we'll probably also have to 870 do a little bit more work. */ 871 872tree 873convert (type, expr) 874 tree type, expr; 875{ 876 tree intype; 877 878 if (type == error_mark_node || expr == error_mark_node) 879 return error_mark_node; 880 881 intype = TREE_TYPE (expr); 882 883 if (POINTER_TYPE_P (type) && POINTER_TYPE_P (intype)) 884 { 885 if (TREE_READONLY_DECL_P (expr)) 886 expr = decl_constant_value (expr); 887 return fold (build1 (NOP_EXPR, type, expr)); 888 } 889 890 return ocp_convert (type, expr, CONV_OLD_CONVERT, 891 LOOKUP_NORMAL|LOOKUP_NO_CONVERSION); 892} 893 894/* Like cp_convert, except permit conversions to take place which 895 are not normally allowed due to access restrictions 896 (such as conversion from sub-type to private super-type). */ 897 898tree 899convert_force (type, expr, convtype) 900 tree type; 901 tree expr; 902 int convtype; 903{ 904 register tree e = expr; 905 register enum tree_code code = TREE_CODE (type); 906 907 if (code == REFERENCE_TYPE) 908 return fold (convert_to_reference (type, e, CONV_C_CAST, LOOKUP_COMPLAIN, 909 NULL_TREE)); 910 else if (TREE_CODE (TREE_TYPE (e)) == REFERENCE_TYPE) 911 e = convert_from_reference (e); 912 913 if (code == POINTER_TYPE) 914 return fold (convert_to_pointer_force (type, e)); 915 916 /* From typeck.c convert_for_assignment */ 917 if (((TREE_CODE (TREE_TYPE (e)) == POINTER_TYPE && TREE_CODE (e) == ADDR_EXPR 918 && TREE_CODE (TREE_TYPE (e)) == POINTER_TYPE 919 && TREE_CODE (TREE_TYPE (TREE_TYPE (e))) == METHOD_TYPE) 920 || integer_zerop (e) 921 || TYPE_PTRMEMFUNC_P (TREE_TYPE (e))) 922 && TYPE_PTRMEMFUNC_P (type)) 923 { 924 /* compatible pointer to member functions. */ 925 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), e, 1); 926 } 927 928 return ocp_convert (type, e, CONV_C_CAST|convtype, LOOKUP_NORMAL); 929} 930 931/* Convert an aggregate EXPR to type XTYPE. If a conversion 932 exists, return the attempted conversion. This may 933 return ERROR_MARK_NODE if the conversion is not 934 allowed (references private members, etc). 935 If no conversion exists, NULL_TREE is returned. 936 937 If (FOR_SURE & 1) is non-zero, then we allow this type conversion 938 to take place immediately. Otherwise, we build a SAVE_EXPR 939 which can be evaluated if the results are ever needed. 940 941 Changes to this functions should be mirrored in user_harshness. 942 943 FIXME: Ambiguity checking is wrong. Should choose one by the implicit 944 object parameter, or by the second standard conversion sequence if 945 that doesn't do it. This will probably wait for an overloading rewrite. 946 (jason 8/9/95) */ 947 948tree 949build_type_conversion (xtype, expr, for_sure) 950 tree xtype, expr; 951 int for_sure; 952{ 953 /* C++: check to see if we can convert this aggregate type 954 into the required type. */ 955 return build_user_type_conversion 956 (xtype, expr, for_sure ? LOOKUP_NORMAL : 0); 957} 958 959/* Convert the given EXPR to one of a group of types suitable for use in an 960 expression. DESIRES is a combination of various WANT_* flags (q.v.) 961 which indicates which types are suitable. If COMPLAIN is 1, complain 962 about ambiguity; otherwise, the caller will deal with it. */ 963 964tree 965build_expr_type_conversion (desires, expr, complain) 966 int desires; 967 tree expr; 968 int complain; 969{ 970 tree basetype = TREE_TYPE (expr); 971 tree conv = NULL_TREE; 972 tree winner = NULL_TREE; 973 974 if (expr == null_node 975 && (desires & WANT_INT) 976 && !(desires & WANT_NULL)) 977 cp_warning ("converting NULL to non-pointer type"); 978 979 if (TREE_CODE (expr) == OFFSET_REF) 980 expr = resolve_offset_ref (expr); 981 expr = convert_from_reference (expr); 982 basetype = TREE_TYPE (expr); 983 984 if (! IS_AGGR_TYPE (basetype)) 985 switch (TREE_CODE (basetype)) 986 { 987 case INTEGER_TYPE: 988 if ((desires & WANT_NULL) && null_ptr_cst_p (expr)) 989 return expr; 990 /* else fall through... */ 991 992 case BOOLEAN_TYPE: 993 return (desires & WANT_INT) ? expr : NULL_TREE; 994 case ENUMERAL_TYPE: 995 return (desires & WANT_ENUM) ? expr : NULL_TREE; 996 case REAL_TYPE: 997 return (desires & WANT_FLOAT) ? expr : NULL_TREE; 998 case POINTER_TYPE: 999 return (desires & WANT_POINTER) ? expr : NULL_TREE; 1000 1001 case FUNCTION_TYPE: 1002 case ARRAY_TYPE: 1003 return (desires & WANT_POINTER) ? default_conversion (expr) 1004 : NULL_TREE; 1005 default: 1006 return NULL_TREE; 1007 } 1008 1009 /* The code for conversions from class type is currently only used for 1010 delete expressions. Other expressions are handled by build_new_op. */ 1011 1012 if (! TYPE_HAS_CONVERSION (basetype)) 1013 return NULL_TREE; 1014 1015 for (conv = lookup_conversions (basetype); conv; conv = TREE_CHAIN (conv)) 1016 { 1017 int win = 0; 1018 tree candidate; 1019 tree cand = TREE_VALUE (conv); 1020 1021 if (winner && winner == cand) 1022 continue; 1023 1024 candidate = TREE_TYPE (TREE_TYPE (cand)); 1025 if (TREE_CODE (candidate) == REFERENCE_TYPE) 1026 candidate = TREE_TYPE (candidate); 1027 1028 switch (TREE_CODE (candidate)) 1029 { 1030 case BOOLEAN_TYPE: 1031 case INTEGER_TYPE: 1032 win = (desires & WANT_INT); break; 1033 case ENUMERAL_TYPE: 1034 win = (desires & WANT_ENUM); break; 1035 case REAL_TYPE: 1036 win = (desires & WANT_FLOAT); break; 1037 case POINTER_TYPE: 1038 win = (desires & WANT_POINTER); break; 1039 1040 default: 1041 break; 1042 } 1043 1044 if (win) 1045 { 1046 if (winner) 1047 { 1048 if (complain) 1049 { 1050 cp_error ("ambiguous default type conversion from `%T'", 1051 basetype); 1052 cp_error (" candidate conversions include `%D' and `%D'", 1053 winner, cand); 1054 } 1055 return error_mark_node; 1056 } 1057 else 1058 winner = cand; 1059 } 1060 } 1061 1062 if (winner) 1063 { 1064 tree type = TREE_TYPE (TREE_TYPE (winner)); 1065 if (TREE_CODE (type) == REFERENCE_TYPE) 1066 type = TREE_TYPE (type); 1067 return build_user_type_conversion (type, expr, LOOKUP_NORMAL); 1068 } 1069 1070 return NULL_TREE; 1071} 1072 1073/* Implements integral promotion (4.1) and float->double promotion. */ 1074 1075tree 1076type_promotes_to (type) 1077 tree type; 1078{ 1079 int type_quals; 1080 1081 if (type == error_mark_node) 1082 return error_mark_node; 1083 1084 type_quals = CP_TYPE_QUALS (type); 1085 type = TYPE_MAIN_VARIANT (type); 1086 1087 /* bool always promotes to int (not unsigned), even if it's the same 1088 size. */ 1089 if (type == boolean_type_node) 1090 type = integer_type_node; 1091 1092 /* Normally convert enums to int, but convert wide enums to something 1093 wider. */ 1094 else if (TREE_CODE (type) == ENUMERAL_TYPE 1095 || type == wchar_type_node) 1096 { 1097 int precision = MAX (TYPE_PRECISION (type), 1098 TYPE_PRECISION (integer_type_node)); 1099 tree totype = type_for_size (precision, 0); 1100 if (TREE_UNSIGNED (type) 1101 && ! int_fits_type_p (TYPE_MAX_VALUE (type), totype)) 1102 type = type_for_size (precision, 1); 1103 else 1104 type = totype; 1105 } 1106 else if (C_PROMOTING_INTEGER_TYPE_P (type)) 1107 { 1108 /* Retain unsignedness if really not getting bigger. */ 1109 if (TREE_UNSIGNED (type) 1110 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)) 1111 type = unsigned_type_node; 1112 else 1113 type = integer_type_node; 1114 } 1115 else if (type == float_type_node) 1116 type = double_type_node; 1117 1118 return cp_build_qualified_type (type, type_quals); 1119} 1120 1121/* The routines below this point are carefully written to conform to 1122 the standard. They use the same terminology, and follow the rules 1123 closely. Although they are used only in pt.c at the moment, they 1124 should presumably be used everywhere in the future. */ 1125 1126/* Attempt to perform qualification conversions on EXPR to convert it 1127 to TYPE. Return the resulting expression, or error_mark_node if 1128 the conversion was impossible. */ 1129 1130tree 1131perform_qualification_conversions (type, expr) 1132 tree type; 1133 tree expr; 1134{ 1135 if (TREE_CODE (type) == POINTER_TYPE 1136 && TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE 1137 && comp_ptr_ttypes (TREE_TYPE (type), TREE_TYPE (TREE_TYPE (expr)))) 1138 return build1 (NOP_EXPR, type, expr); 1139 else 1140 return error_mark_node; 1141} 1142