typeck.c revision 161651
1/* Build expressions with type checking for C++ compiler. 2 Copyright (C) 1987, 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 3 1999, 2000, 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc. 4 Hacked by Michael Tiemann (tiemann@cygnus.com) 5 6This file is part of GCC. 7 8GCC is free software; you can redistribute it and/or modify 9it under the terms of the GNU General Public License as published by 10the Free Software Foundation; either version 2, or (at your option) 11any later version. 12 13GCC is distributed in the hope that it will be useful, 14but WITHOUT ANY WARRANTY; without even the implied warranty of 15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16GNU General Public License for more details. 17 18You should have received a copy of the GNU General Public License 19along with GCC; see the file COPYING. If not, write to 20the Free Software Foundation, 59 Temple Place - Suite 330, 21Boston, MA 02111-1307, USA. */ 22 23 24/* This file is part of the C++ front end. 25 It contains routines to build C++ expressions given their operands, 26 including computing the types of the result, C and C++ specific error 27 checks, and some optimization. 28 29 There are also routines to build RETURN_STMT nodes and CASE_STMT nodes, 30 and to process initializations in declarations (since they work 31 like a strange sort of assignment). */ 32 33#include "config.h" 34#include "system.h" 35#include "coretypes.h" 36#include "tm.h" 37#include "tree.h" 38#include "rtl.h" 39#include "expr.h" 40#include "cp-tree.h" 41#include "tm_p.h" 42#include "flags.h" 43#include "output.h" 44#include "toplev.h" 45#include "diagnostic.h" 46#include "target.h" 47#include "convert.h" 48 49static tree convert_for_assignment (tree, tree, const char *, tree, int); 50static tree cp_pointer_int_sum (enum tree_code, tree, tree); 51static tree rationalize_conditional_expr (enum tree_code, tree); 52static int comp_ptr_ttypes_real (tree, tree, int); 53static int comp_ptr_ttypes_const (tree, tree); 54static bool comp_except_types (tree, tree, bool); 55static bool comp_array_types (tree, tree, bool); 56static tree common_base_type (tree, tree); 57static tree pointer_diff (tree, tree, tree); 58static tree get_delta_difference (tree, tree, int); 59static void casts_away_constness_r (tree *, tree *); 60static bool casts_away_constness (tree, tree); 61static void maybe_warn_about_returning_address_of_local (tree); 62static tree lookup_destructor (tree, tree, tree); 63 64/* Return the target type of TYPE, which means return T for: 65 T*, T&, T[], T (...), and otherwise, just T. */ 66 67tree 68target_type (tree type) 69{ 70 type = non_reference (type); 71 while (TREE_CODE (type) == POINTER_TYPE 72 || TREE_CODE (type) == ARRAY_TYPE 73 || TREE_CODE (type) == FUNCTION_TYPE 74 || TREE_CODE (type) == METHOD_TYPE 75 || TYPE_PTRMEM_P (type)) 76 type = TREE_TYPE (type); 77 return type; 78} 79 80/* Do `exp = require_complete_type (exp);' to make sure exp 81 does not have an incomplete type. (That includes void types.) 82 Returns the error_mark_node if the VALUE does not have 83 complete type when this function returns. */ 84 85tree 86require_complete_type (tree value) 87{ 88 tree type; 89 90 if (processing_template_decl || value == error_mark_node) 91 return value; 92 93 if (TREE_CODE (value) == OVERLOAD) 94 type = unknown_type_node; 95 else 96 type = TREE_TYPE (value); 97 98 if (type == error_mark_node) 99 return error_mark_node; 100 101 /* First, detect a valid value with a complete type. */ 102 if (COMPLETE_TYPE_P (type)) 103 return value; 104 105 if (complete_type_or_else (type, value)) 106 return value; 107 else 108 return error_mark_node; 109} 110 111/* Try to complete TYPE, if it is incomplete. For example, if TYPE is 112 a template instantiation, do the instantiation. Returns TYPE, 113 whether or not it could be completed, unless something goes 114 horribly wrong, in which case the error_mark_node is returned. */ 115 116tree 117complete_type (tree type) 118{ 119 if (type == NULL_TREE) 120 /* Rather than crash, we return something sure to cause an error 121 at some point. */ 122 return error_mark_node; 123 124 if (type == error_mark_node || COMPLETE_TYPE_P (type)) 125 ; 126 else if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type)) 127 { 128 tree t = complete_type (TREE_TYPE (type)); 129 if (COMPLETE_TYPE_P (t) && !dependent_type_p (type)) 130 layout_type (type); 131 TYPE_NEEDS_CONSTRUCTING (type) 132 = TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (t)); 133 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type) 134 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TYPE_MAIN_VARIANT (t)); 135 } 136 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INSTANTIATION (type)) 137 instantiate_class_template (TYPE_MAIN_VARIANT (type)); 138 139 return type; 140} 141 142/* Like complete_type, but issue an error if the TYPE cannot be completed. 143 VALUE is used for informative diagnostics. DIAG_TYPE indicates the type 144 of diagnostic: 0 for an error, 1 for a warning, 2 for a pedwarn. 145 Returns NULL_TREE if the type cannot be made complete. */ 146 147tree 148complete_type_or_diagnostic (tree type, tree value, int diag_type) 149{ 150 type = complete_type (type); 151 if (type == error_mark_node) 152 /* We already issued an error. */ 153 return NULL_TREE; 154 else if (!COMPLETE_TYPE_P (type)) 155 { 156 cxx_incomplete_type_diagnostic (value, type, diag_type); 157 return NULL_TREE; 158 } 159 else 160 return type; 161} 162 163/* Return truthvalue of whether type of EXP is instantiated. */ 164 165int 166type_unknown_p (tree exp) 167{ 168 return (TREE_CODE (exp) == TREE_LIST 169 || TREE_TYPE (exp) == unknown_type_node); 170} 171 172 173/* Return the common type of two parameter lists. 174 We assume that comptypes has already been done and returned 1; 175 if that isn't so, this may crash. 176 177 As an optimization, free the space we allocate if the parameter 178 lists are already common. */ 179 180tree 181commonparms (tree p1, tree p2) 182{ 183 tree oldargs = p1, newargs, n; 184 int i, len; 185 int any_change = 0; 186 187 len = list_length (p1); 188 newargs = tree_last (p1); 189 190 if (newargs == void_list_node) 191 i = 1; 192 else 193 { 194 i = 0; 195 newargs = 0; 196 } 197 198 for (; i < len; i++) 199 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs); 200 201 n = newargs; 202 203 for (i = 0; p1; 204 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n), i++) 205 { 206 if (TREE_PURPOSE (p1) && !TREE_PURPOSE (p2)) 207 { 208 TREE_PURPOSE (n) = TREE_PURPOSE (p1); 209 any_change = 1; 210 } 211 else if (! TREE_PURPOSE (p1)) 212 { 213 if (TREE_PURPOSE (p2)) 214 { 215 TREE_PURPOSE (n) = TREE_PURPOSE (p2); 216 any_change = 1; 217 } 218 } 219 else 220 { 221 if (1 != simple_cst_equal (TREE_PURPOSE (p1), TREE_PURPOSE (p2))) 222 any_change = 1; 223 TREE_PURPOSE (n) = TREE_PURPOSE (p2); 224 } 225 if (TREE_VALUE (p1) != TREE_VALUE (p2)) 226 { 227 any_change = 1; 228 TREE_VALUE (n) = merge_types (TREE_VALUE (p1), TREE_VALUE (p2)); 229 } 230 else 231 TREE_VALUE (n) = TREE_VALUE (p1); 232 } 233 if (! any_change) 234 return oldargs; 235 236 return newargs; 237} 238 239/* Given a type, perhaps copied for a typedef, 240 find the "original" version of it. */ 241tree 242original_type (tree t) 243{ 244 while (TYPE_NAME (t) != NULL_TREE) 245 { 246 tree x = TYPE_NAME (t); 247 if (TREE_CODE (x) != TYPE_DECL) 248 break; 249 x = DECL_ORIGINAL_TYPE (x); 250 if (x == NULL_TREE) 251 break; 252 t = x; 253 } 254 return t; 255} 256 257/* T1 and T2 are arithmetic or enumeration types. Return the type 258 that will result from the "usual arithmetic conversions" on T1 and 259 T2 as described in [expr]. */ 260 261tree 262type_after_usual_arithmetic_conversions (tree t1, tree t2) 263{ 264 enum tree_code code1 = TREE_CODE (t1); 265 enum tree_code code2 = TREE_CODE (t2); 266 tree attributes; 267 268 /* FIXME: Attributes. */ 269 my_friendly_assert (ARITHMETIC_TYPE_P (t1) 270 || TREE_CODE (t1) == COMPLEX_TYPE 271 || TREE_CODE (t1) == VECTOR_TYPE 272 || TREE_CODE (t1) == ENUMERAL_TYPE, 273 19990725); 274 my_friendly_assert (ARITHMETIC_TYPE_P (t2) 275 || TREE_CODE (t2) == COMPLEX_TYPE 276 || TREE_CODE (t2) == VECTOR_TYPE 277 || TREE_CODE (t2) == ENUMERAL_TYPE, 278 19990725); 279 280 /* In what follows, we slightly generalize the rules given in [expr] so 281 as to deal with `long long' and `complex'. First, merge the 282 attributes. */ 283 attributes = (*targetm.merge_type_attributes) (t1, t2); 284 285 /* If one type is complex, form the common type of the non-complex 286 components, then make that complex. Use T1 or T2 if it is the 287 required type. */ 288 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE) 289 { 290 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1; 291 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2; 292 tree subtype 293 = type_after_usual_arithmetic_conversions (subtype1, subtype2); 294 295 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype) 296 return build_type_attribute_variant (t1, attributes); 297 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype) 298 return build_type_attribute_variant (t2, attributes); 299 else 300 return build_type_attribute_variant (build_complex_type (subtype), 301 attributes); 302 } 303 304 if (code1 == VECTOR_TYPE) 305 { 306 /* When we get here we should have two vectors of the same size. 307 Just prefer the unsigned one if present. */ 308 if (TREE_UNSIGNED (t1)) 309 return build_type_attribute_variant (t1, attributes); 310 else 311 return build_type_attribute_variant (t2, attributes); 312 } 313 314 /* If only one is real, use it as the result. */ 315 if (code1 == REAL_TYPE && code2 != REAL_TYPE) 316 return build_type_attribute_variant (t1, attributes); 317 if (code2 == REAL_TYPE && code1 != REAL_TYPE) 318 return build_type_attribute_variant (t2, attributes); 319 320 /* Perform the integral promotions. */ 321 if (code1 != REAL_TYPE) 322 { 323 t1 = type_promotes_to (t1); 324 t2 = type_promotes_to (t2); 325 } 326 327 /* Both real or both integers; use the one with greater precision. */ 328 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2)) 329 return build_type_attribute_variant (t1, attributes); 330 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1)) 331 return build_type_attribute_variant (t2, attributes); 332 333 /* The types are the same; no need to do anything fancy. */ 334 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2)) 335 return build_type_attribute_variant (t1, attributes); 336 337 if (code1 != REAL_TYPE) 338 { 339 /* If one is a sizetype, use it so size_binop doesn't blow up. */ 340 if (TYPE_IS_SIZETYPE (t1) > TYPE_IS_SIZETYPE (t2)) 341 return build_type_attribute_variant (t1, attributes); 342 if (TYPE_IS_SIZETYPE (t2) > TYPE_IS_SIZETYPE (t1)) 343 return build_type_attribute_variant (t2, attributes); 344 345 /* If one is unsigned long long, then convert the other to unsigned 346 long long. */ 347 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_unsigned_type_node) 348 || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_unsigned_type_node)) 349 return build_type_attribute_variant (long_long_unsigned_type_node, 350 attributes); 351 /* If one is a long long, and the other is an unsigned long, and 352 long long can represent all the values of an unsigned long, then 353 convert to a long long. Otherwise, convert to an unsigned long 354 long. Otherwise, if either operand is long long, convert the 355 other to long long. 356 357 Since we're here, we know the TYPE_PRECISION is the same; 358 therefore converting to long long cannot represent all the values 359 of an unsigned long, so we choose unsigned long long in that 360 case. */ 361 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_integer_type_node) 362 || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_integer_type_node)) 363 { 364 tree t = ((TREE_UNSIGNED (t1) || TREE_UNSIGNED (t2)) 365 ? long_long_unsigned_type_node 366 : long_long_integer_type_node); 367 return build_type_attribute_variant (t, attributes); 368 } 369 370 /* Go through the same procedure, but for longs. */ 371 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_unsigned_type_node) 372 || same_type_p (TYPE_MAIN_VARIANT (t2), long_unsigned_type_node)) 373 return build_type_attribute_variant (long_unsigned_type_node, 374 attributes); 375 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_integer_type_node) 376 || same_type_p (TYPE_MAIN_VARIANT (t2), long_integer_type_node)) 377 { 378 tree t = ((TREE_UNSIGNED (t1) || TREE_UNSIGNED (t2)) 379 ? long_unsigned_type_node : long_integer_type_node); 380 return build_type_attribute_variant (t, attributes); 381 } 382 /* Otherwise prefer the unsigned one. */ 383 if (TREE_UNSIGNED (t1)) 384 return build_type_attribute_variant (t1, attributes); 385 else 386 return build_type_attribute_variant (t2, attributes); 387 } 388 else 389 { 390 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_double_type_node) 391 || same_type_p (TYPE_MAIN_VARIANT (t2), long_double_type_node)) 392 return build_type_attribute_variant (long_double_type_node, 393 attributes); 394 if (same_type_p (TYPE_MAIN_VARIANT (t1), double_type_node) 395 || same_type_p (TYPE_MAIN_VARIANT (t2), double_type_node)) 396 return build_type_attribute_variant (double_type_node, 397 attributes); 398 if (same_type_p (TYPE_MAIN_VARIANT (t1), float_type_node) 399 || same_type_p (TYPE_MAIN_VARIANT (t2), float_type_node)) 400 return build_type_attribute_variant (float_type_node, 401 attributes); 402 403 /* Two floating-point types whose TYPE_MAIN_VARIANTs are none of 404 the standard C++ floating-point types. Logic earlier in this 405 function has already eliminated the possibility that 406 TYPE_PRECISION (t2) != TYPE_PRECISION (t1), so there's no 407 compelling reason to choose one or the other. */ 408 return build_type_attribute_variant (t1, attributes); 409 } 410} 411 412/* Subroutine of composite_pointer_type to implement the recursive 413 case. See that function for documentation fo the parameters. */ 414 415static tree 416composite_pointer_type_r (tree t1, tree t2, const char* location) 417{ 418 tree pointee1; 419 tree pointee2; 420 tree result_type; 421 tree attributes; 422 423 /* Determine the types pointed to by T1 and T2. */ 424 if (TREE_CODE (t1) == POINTER_TYPE) 425 { 426 pointee1 = TREE_TYPE (t1); 427 pointee2 = TREE_TYPE (t2); 428 } 429 else 430 { 431 pointee1 = TYPE_PTRMEM_POINTED_TO_TYPE (t1); 432 pointee2 = TYPE_PTRMEM_POINTED_TO_TYPE (t2); 433 } 434 435 /* [expr.rel] 436 437 Otherwise, the composite pointer type is a pointer type 438 similar (_conv.qual_) to the type of one of the operands, 439 with a cv-qualification signature (_conv.qual_) that is the 440 union of the cv-qualification signatures of the operand 441 types. */ 442 if (same_type_ignoring_top_level_qualifiers_p (pointee1, pointee2)) 443 result_type = pointee1; 444 else if ((TREE_CODE (pointee1) == POINTER_TYPE 445 && TREE_CODE (pointee2) == POINTER_TYPE) 446 || (TYPE_PTR_TO_MEMBER_P (pointee1) 447 && TYPE_PTR_TO_MEMBER_P (pointee2))) 448 result_type = composite_pointer_type_r (pointee1, pointee2, location); 449 else 450 { 451 pedwarn ("%s between distinct pointer types `%T' and `%T' " 452 "lacks a cast", 453 location, t1, t2); 454 result_type = void_type_node; 455 } 456 result_type = cp_build_qualified_type (result_type, 457 (cp_type_quals (pointee1) 458 | cp_type_quals (pointee2))); 459 /* If the original types were pointers to members, so is the 460 result. */ 461 if (TYPE_PTR_TO_MEMBER_P (t1)) 462 { 463 if (!same_type_p (TYPE_PTRMEM_CLASS_TYPE (t1), 464 TYPE_PTRMEM_CLASS_TYPE (t2))) 465 pedwarn ("%s between distinct pointer types `%T' and `%T' " 466 "lacks a cast", 467 location, t1, t2); 468 result_type = build_ptrmem_type (TYPE_PTRMEM_CLASS_TYPE (t1), 469 result_type); 470 } 471 else 472 result_type = build_pointer_type (result_type); 473 474 /* Merge the attributes. */ 475 attributes = (*targetm.merge_type_attributes) (t1, t2); 476 return build_type_attribute_variant (result_type, attributes); 477} 478 479/* Return the composite pointer type (see [expr.rel]) for T1 and T2. 480 ARG1 and ARG2 are the values with those types. The LOCATION is a 481 string describing the current location, in case an error occurs. 482 483 This routine also implements the computation of a common type for 484 pointers-to-members as per [expr.eq]. */ 485 486tree 487composite_pointer_type (tree t1, tree t2, tree arg1, tree arg2, 488 const char* location) 489{ 490 tree class1; 491 tree class2; 492 493 /* [expr.rel] 494 495 If one operand is a null pointer constant, the composite pointer 496 type is the type of the other operand. */ 497 if (null_ptr_cst_p (arg1)) 498 return t2; 499 if (null_ptr_cst_p (arg2)) 500 return t1; 501 502 /* We have: 503 504 [expr.rel] 505 506 If one of the operands has type "pointer to cv1 void*", then 507 the other has type "pointer to cv2T", and the composite pointer 508 type is "pointer to cv12 void", where cv12 is the union of cv1 509 and cv2. 510 511 If either type is a pointer to void, make sure it is T1. */ 512 if (TREE_CODE (t2) == POINTER_TYPE && VOID_TYPE_P (TREE_TYPE (t2))) 513 { 514 tree t; 515 t = t1; 516 t1 = t2; 517 t2 = t; 518 } 519 520 /* Now, if T1 is a pointer to void, merge the qualifiers. */ 521 if (TREE_CODE (t1) == POINTER_TYPE && VOID_TYPE_P (TREE_TYPE (t1))) 522 { 523 tree attributes; 524 tree result_type; 525 526 if (pedantic && TYPE_PTRFN_P (t2)) 527 pedwarn ("ISO C++ forbids %s between pointer of type `void *' and pointer-to-function", location); 528 result_type 529 = cp_build_qualified_type (void_type_node, 530 (cp_type_quals (TREE_TYPE (t1)) 531 | cp_type_quals (TREE_TYPE (t2)))); 532 result_type = build_pointer_type (result_type); 533 /* Merge the attributes. */ 534 attributes = (*targetm.merge_type_attributes) (t1, t2); 535 return build_type_attribute_variant (result_type, attributes); 536 } 537 538 /* [expr.eq] permits the application of a pointer conversion to 539 bring the pointers to a common type. */ 540 if (TREE_CODE (t1) == POINTER_TYPE && TREE_CODE (t2) == POINTER_TYPE 541 && CLASS_TYPE_P (TREE_TYPE (t1)) 542 && CLASS_TYPE_P (TREE_TYPE (t2)) 543 && !same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (t1), 544 TREE_TYPE (t2))) 545 { 546 class1 = TREE_TYPE (t1); 547 class2 = TREE_TYPE (t2); 548 549 if (DERIVED_FROM_P (class1, class2)) 550 t2 = (build_pointer_type 551 (cp_build_qualified_type (class1, TYPE_QUALS (class2)))); 552 else if (DERIVED_FROM_P (class2, class1)) 553 t1 = (build_pointer_type 554 (cp_build_qualified_type (class2, TYPE_QUALS (class1)))); 555 else 556 { 557 error ("%s between distinct pointer types `%T' and `%T' " 558 "lacks a cast", location, t1, t2); 559 return error_mark_node; 560 } 561 } 562 /* [expr.eq] permits the application of a pointer-to-member 563 conversion to change the class type of one of the types. */ 564 else if (TYPE_PTR_TO_MEMBER_P (t1) 565 && !same_type_p (TYPE_PTRMEM_CLASS_TYPE (t1), 566 TYPE_PTRMEM_CLASS_TYPE (t2))) 567 { 568 class1 = TYPE_PTRMEM_CLASS_TYPE (t1); 569 class2 = TYPE_PTRMEM_CLASS_TYPE (t2); 570 571 if (DERIVED_FROM_P (class1, class2)) 572 t1 = build_ptrmem_type (class2, TYPE_PTRMEM_POINTED_TO_TYPE (t1)); 573 else if (DERIVED_FROM_P (class2, class1)) 574 t2 = build_ptrmem_type (class1, TYPE_PTRMEM_POINTED_TO_TYPE (t2)); 575 else 576 { 577 error ("%s between distinct pointer-to-member types `%T' and `%T' " 578 "lacks a cast", location, t1, t2); 579 return error_mark_node; 580 } 581 } 582 583 return composite_pointer_type_r (t1, t2, location); 584} 585 586/* Return the merged type of two types. 587 We assume that comptypes has already been done and returned 1; 588 if that isn't so, this may crash. 589 590 This just combines attributes and default arguments; any other 591 differences would cause the two types to compare unalike. */ 592 593tree 594merge_types (tree t1, tree t2) 595{ 596 enum tree_code code1; 597 enum tree_code code2; 598 tree attributes; 599 600 /* Save time if the two types are the same. */ 601 if (t1 == t2) 602 return t1; 603 if (original_type (t1) == original_type (t2)) 604 return t1; 605 606 /* If one type is nonsense, use the other. */ 607 if (t1 == error_mark_node) 608 return t2; 609 if (t2 == error_mark_node) 610 return t1; 611 612 /* Merge the attributes. */ 613 attributes = (*targetm.merge_type_attributes) (t1, t2); 614 615 if (TYPE_PTRMEMFUNC_P (t1)) 616 t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1); 617 if (TYPE_PTRMEMFUNC_P (t2)) 618 t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2); 619 620 code1 = TREE_CODE (t1); 621 code2 = TREE_CODE (t2); 622 623 switch (code1) 624 { 625 case POINTER_TYPE: 626 case REFERENCE_TYPE: 627 /* For two pointers, do this recursively on the target type. */ 628 { 629 tree target = merge_types (TREE_TYPE (t1), TREE_TYPE (t2)); 630 int quals = cp_type_quals (t1); 631 632 if (code1 == POINTER_TYPE) 633 t1 = build_pointer_type (target); 634 else 635 t1 = build_reference_type (target); 636 t1 = build_type_attribute_variant (t1, attributes); 637 t1 = cp_build_qualified_type (t1, quals); 638 639 if (TREE_CODE (target) == METHOD_TYPE) 640 t1 = build_ptrmemfunc_type (t1); 641 642 return t1; 643 } 644 645 case OFFSET_TYPE: 646 { 647 int quals; 648 tree pointee; 649 quals = cp_type_quals (t1); 650 pointee = merge_types (TYPE_PTRMEM_POINTED_TO_TYPE (t1), 651 TYPE_PTRMEM_POINTED_TO_TYPE (t2)); 652 t1 = build_ptrmem_type (TYPE_PTRMEM_CLASS_TYPE (t1), 653 pointee); 654 t1 = cp_build_qualified_type (t1, quals); 655 break; 656 } 657 658 case ARRAY_TYPE: 659 { 660 tree elt = merge_types (TREE_TYPE (t1), TREE_TYPE (t2)); 661 /* Save space: see if the result is identical to one of the args. */ 662 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1)) 663 return build_type_attribute_variant (t1, attributes); 664 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2)) 665 return build_type_attribute_variant (t2, attributes); 666 /* Merge the element types, and have a size if either arg has one. */ 667 t1 = build_cplus_array_type 668 (elt, TYPE_DOMAIN (TYPE_DOMAIN (t1) ? t1 : t2)); 669 break; 670 } 671 672 case FUNCTION_TYPE: 673 /* Function types: prefer the one that specified arg types. 674 If both do, merge the arg types. Also merge the return types. */ 675 { 676 tree valtype = merge_types (TREE_TYPE (t1), TREE_TYPE (t2)); 677 tree p1 = TYPE_ARG_TYPES (t1); 678 tree p2 = TYPE_ARG_TYPES (t2); 679 tree rval, raises; 680 681 /* Save space: see if the result is identical to one of the args. */ 682 if (valtype == TREE_TYPE (t1) && ! p2) 683 return cp_build_type_attribute_variant (t1, attributes); 684 if (valtype == TREE_TYPE (t2) && ! p1) 685 return cp_build_type_attribute_variant (t2, attributes); 686 687 /* Simple way if one arg fails to specify argument types. */ 688 if (p1 == NULL_TREE || TREE_VALUE (p1) == void_type_node) 689 { 690 rval = build_function_type (valtype, p2); 691 if ((raises = TYPE_RAISES_EXCEPTIONS (t2))) 692 rval = build_exception_variant (rval, raises); 693 return cp_build_type_attribute_variant (rval, attributes); 694 } 695 raises = TYPE_RAISES_EXCEPTIONS (t1); 696 if (p2 == NULL_TREE || TREE_VALUE (p2) == void_type_node) 697 { 698 rval = build_function_type (valtype, p1); 699 if (raises) 700 rval = build_exception_variant (rval, raises); 701 return cp_build_type_attribute_variant (rval, attributes); 702 } 703 704 rval = build_function_type (valtype, commonparms (p1, p2)); 705 t1 = build_exception_variant (rval, raises); 706 break; 707 } 708 709 case METHOD_TYPE: 710 { 711 /* Get this value the long way, since TYPE_METHOD_BASETYPE 712 is just the main variant of this. */ 713 tree basetype = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (t2))); 714 tree raises = TYPE_RAISES_EXCEPTIONS (t1); 715 tree t3; 716 717 /* If this was a member function type, get back to the 718 original type of type member function (i.e., without 719 the class instance variable up front. */ 720 t1 = build_function_type (TREE_TYPE (t1), 721 TREE_CHAIN (TYPE_ARG_TYPES (t1))); 722 t2 = build_function_type (TREE_TYPE (t2), 723 TREE_CHAIN (TYPE_ARG_TYPES (t2))); 724 t3 = merge_types (t1, t2); 725 t3 = build_method_type_directly (basetype, TREE_TYPE (t3), 726 TYPE_ARG_TYPES (t3)); 727 t1 = build_exception_variant (t3, raises); 728 break; 729 } 730 731 case TYPENAME_TYPE: 732 /* There is no need to merge attributes into a TYPENAME_TYPE. 733 When the type is instantiated it will have whatever 734 attributes result from the instantiation. */ 735 return t1; 736 737 default:; 738 } 739 return cp_build_type_attribute_variant (t1, attributes); 740} 741 742/* Return the common type of two types. 743 We assume that comptypes has already been done and returned 1; 744 if that isn't so, this may crash. 745 746 This is the type for the result of most arithmetic operations 747 if the operands have the given two types. */ 748 749tree 750common_type (tree t1, tree t2) 751{ 752 enum tree_code code1; 753 enum tree_code code2; 754 755 /* If one type is nonsense, bail. */ 756 if (t1 == error_mark_node || t2 == error_mark_node) 757 return error_mark_node; 758 759 code1 = TREE_CODE (t1); 760 code2 = TREE_CODE (t2); 761 762 if ((ARITHMETIC_TYPE_P (t1) || code1 == ENUMERAL_TYPE 763 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE) 764 && (ARITHMETIC_TYPE_P (t2) || code2 == ENUMERAL_TYPE 765 || code2 == COMPLEX_TYPE || code2 == VECTOR_TYPE)) 766 return type_after_usual_arithmetic_conversions (t1, t2); 767 768 else if ((TYPE_PTR_P (t1) && TYPE_PTR_P (t2)) 769 || (TYPE_PTRMEM_P (t1) && TYPE_PTRMEM_P (t2)) 770 || (TYPE_PTRMEMFUNC_P (t1) && TYPE_PTRMEMFUNC_P (t2))) 771 return composite_pointer_type (t1, t2, error_mark_node, error_mark_node, 772 "conversion"); 773 else 774 abort (); 775} 776 777/* Compare two exception specifier types for exactness or subsetness, if 778 allowed. Returns false for mismatch, true for match (same, or 779 derived and !exact). 780 781 [except.spec] "If a class X ... objects of class X or any class publicly 782 and unambiguously derived from X. Similarly, if a pointer type Y * ... 783 exceptions of type Y * or that are pointers to any type publicly and 784 unambiguously derived from Y. Otherwise a function only allows exceptions 785 that have the same type ..." 786 This does not mention cv qualifiers and is different to what throw 787 [except.throw] and catch [except.catch] will do. They will ignore the 788 top level cv qualifiers, and allow qualifiers in the pointer to class 789 example. 790 791 We implement the letter of the standard. */ 792 793static bool 794comp_except_types (tree a, tree b, bool exact) 795{ 796 if (same_type_p (a, b)) 797 return true; 798 else if (!exact) 799 { 800 if (cp_type_quals (a) || cp_type_quals (b)) 801 return false; 802 803 if (TREE_CODE (a) == POINTER_TYPE 804 && TREE_CODE (b) == POINTER_TYPE) 805 { 806 a = TREE_TYPE (a); 807 b = TREE_TYPE (b); 808 if (cp_type_quals (a) || cp_type_quals (b)) 809 return false; 810 } 811 812 if (TREE_CODE (a) != RECORD_TYPE 813 || TREE_CODE (b) != RECORD_TYPE) 814 return false; 815 816 if (ACCESSIBLY_UNIQUELY_DERIVED_P (a, b)) 817 return true; 818 } 819 return false; 820} 821 822/* Return true if TYPE1 and TYPE2 are equivalent exception specifiers. 823 If EXACT is false, T2 can be stricter than T1 (according to 15.4/7), 824 otherwise it must be exact. Exception lists are unordered, but 825 we've already filtered out duplicates. Most lists will be in order, 826 we should try to make use of that. */ 827 828bool 829comp_except_specs (tree t1, tree t2, bool exact) 830{ 831 tree probe; 832 tree base; 833 int length = 0; 834 835 if (t1 == t2) 836 return true; 837 838 if (t1 == NULL_TREE) /* T1 is ... */ 839 return t2 == NULL_TREE || !exact; 840 if (!TREE_VALUE (t1)) /* t1 is EMPTY */ 841 return t2 != NULL_TREE && !TREE_VALUE (t2); 842 if (t2 == NULL_TREE) /* T2 is ... */ 843 return false; 844 if (TREE_VALUE (t1) && !TREE_VALUE (t2)) /* T2 is EMPTY, T1 is not */ 845 return !exact; 846 847 /* Neither set is ... or EMPTY, make sure each part of T2 is in T1. 848 Count how many we find, to determine exactness. For exact matching and 849 ordered T1, T2, this is an O(n) operation, otherwise its worst case is 850 O(nm). */ 851 for (base = t1; t2 != NULL_TREE; t2 = TREE_CHAIN (t2)) 852 { 853 for (probe = base; probe != NULL_TREE; probe = TREE_CHAIN (probe)) 854 { 855 tree a = TREE_VALUE (probe); 856 tree b = TREE_VALUE (t2); 857 858 if (comp_except_types (a, b, exact)) 859 { 860 if (probe == base && exact) 861 base = TREE_CHAIN (probe); 862 length++; 863 break; 864 } 865 } 866 if (probe == NULL_TREE) 867 return false; 868 } 869 return !exact || base == NULL_TREE || length == list_length (t1); 870} 871 872/* Compare the array types T1 and T2. ALLOW_REDECLARATION is true if 873 [] can match [size]. */ 874 875static bool 876comp_array_types (tree t1, tree t2, bool allow_redeclaration) 877{ 878 tree d1; 879 tree d2; 880 tree max1, max2; 881 882 if (t1 == t2) 883 return true; 884 885 /* The type of the array elements must be the same. */ 886 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2))) 887 return false; 888 889 d1 = TYPE_DOMAIN (t1); 890 d2 = TYPE_DOMAIN (t2); 891 892 if (d1 == d2) 893 return true; 894 895 /* If one of the arrays is dimensionless, and the other has a 896 dimension, they are of different types. However, it is valid to 897 write: 898 899 extern int a[]; 900 int a[3]; 901 902 by [basic.link]: 903 904 declarations for an array object can specify 905 array types that differ by the presence or absence of a major 906 array bound (_dcl.array_). */ 907 if (!d1 || !d2) 908 return allow_redeclaration; 909 910 /* Check that the dimensions are the same. */ 911 912 if (!cp_tree_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))) 913 return false; 914 max1 = TYPE_MAX_VALUE (d1); 915 max2 = TYPE_MAX_VALUE (d2); 916 if (processing_template_decl && !abi_version_at_least (2) 917 && !value_dependent_expression_p (max1) 918 && !value_dependent_expression_p (max2)) 919 { 920 /* With abi-1 we do not fold non-dependent array bounds, (and 921 consequently mangle them incorrectly). We must therefore 922 fold them here, to verify the domains have the same 923 value. */ 924 max1 = fold (max1); 925 max2 = fold (max2); 926 } 927 928 if (!cp_tree_equal (max1, max2)) 929 return false; 930 931 return true; 932} 933 934/* Return true if T1 and T2 are related as allowed by STRICT. STRICT 935 is a bitwise-or of the COMPARE_* flags. */ 936 937bool 938comptypes (tree t1, tree t2, int strict) 939{ 940 if (t1 == t2) 941 return true; 942 943 /* Suppress errors caused by previously reported errors. */ 944 if (t1 == error_mark_node || t2 == error_mark_node) 945 return false; 946 947 my_friendly_assert (TYPE_P (t1) && TYPE_P (t2), 20030623); 948 949 /* TYPENAME_TYPEs should be resolved if the qualifying scope is the 950 current instantiation. */ 951 if (TREE_CODE (t1) == TYPENAME_TYPE) 952 { 953 tree resolved = resolve_typename_type (t1, /*only_current_p=*/true); 954 955 if (resolved != error_mark_node) 956 t1 = resolved; 957 } 958 959 if (TREE_CODE (t2) == TYPENAME_TYPE) 960 { 961 tree resolved = resolve_typename_type (t2, /*only_current_p=*/true); 962 963 if (resolved != error_mark_node) 964 t2 = resolved; 965 } 966 967 /* If either type is the internal version of sizetype, use the 968 language version. */ 969 if (TREE_CODE (t1) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t1) 970 && TYPE_DOMAIN (t1)) 971 t1 = TYPE_DOMAIN (t1); 972 973 if (TREE_CODE (t2) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t2) 974 && TYPE_DOMAIN (t2)) 975 t2 = TYPE_DOMAIN (t2); 976 977 if (TYPE_PTRMEMFUNC_P (t1)) 978 t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1); 979 if (TYPE_PTRMEMFUNC_P (t2)) 980 t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2); 981 982 /* Different classes of types can't be compatible. */ 983 if (TREE_CODE (t1) != TREE_CODE (t2)) 984 return false; 985 986 /* Qualifiers must match. For array types, we will check when we 987 recur on the array element types. */ 988 if (TREE_CODE (t1) != ARRAY_TYPE 989 && TYPE_QUALS (t1) != TYPE_QUALS (t2)) 990 return false; 991 if (TYPE_FOR_JAVA (t1) != TYPE_FOR_JAVA (t2)) 992 return false; 993 994 /* Allow for two different type nodes which have essentially the same 995 definition. Note that we already checked for equality of the type 996 qualifiers (just above). */ 997 998 if (TREE_CODE (t1) != ARRAY_TYPE 999 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2)) 1000 return true; 1001 1002 if (!(*targetm.comp_type_attributes) (t1, t2)) 1003 return false; 1004 1005 switch (TREE_CODE (t1)) 1006 { 1007 case TEMPLATE_TEMPLATE_PARM: 1008 case BOUND_TEMPLATE_TEMPLATE_PARM: 1009 if (TEMPLATE_TYPE_IDX (t1) != TEMPLATE_TYPE_IDX (t2) 1010 || TEMPLATE_TYPE_LEVEL (t1) != TEMPLATE_TYPE_LEVEL (t2)) 1011 return false; 1012 if (!comp_template_parms 1013 (DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t1)), 1014 DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t2)))) 1015 return false; 1016 if (TREE_CODE (t1) == TEMPLATE_TEMPLATE_PARM) 1017 return true; 1018 /* Don't check inheritance. */ 1019 strict = COMPARE_STRICT; 1020 /* Fall through. */ 1021 1022 case RECORD_TYPE: 1023 case UNION_TYPE: 1024 if (TYPE_TEMPLATE_INFO (t1) && TYPE_TEMPLATE_INFO (t2) 1025 && (TYPE_TI_TEMPLATE (t1) == TYPE_TI_TEMPLATE (t2) 1026 || TREE_CODE (t1) == BOUND_TEMPLATE_TEMPLATE_PARM) 1027 && comp_template_args (TYPE_TI_ARGS (t1), TYPE_TI_ARGS (t2))) 1028 return true; 1029 1030 if ((strict & COMPARE_BASE) && DERIVED_FROM_P (t1, t2)) 1031 return true; 1032 else if ((strict & COMPARE_DERIVED) && DERIVED_FROM_P (t2, t1)) 1033 return true; 1034 1035 return false; 1036 1037 case OFFSET_TYPE: 1038 if (!comptypes (TYPE_OFFSET_BASETYPE (t1), TYPE_OFFSET_BASETYPE (t2), 1039 strict & ~COMPARE_REDECLARATION)) 1040 return false; 1041 /* Fall through. */ 1042 1043 case POINTER_TYPE: 1044 case REFERENCE_TYPE: 1045 return same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)); 1046 1047 case METHOD_TYPE: 1048 case FUNCTION_TYPE: 1049 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2))) 1050 return false; 1051 return compparms (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2)); 1052 1053 case ARRAY_TYPE: 1054 /* Target types must match incl. qualifiers. */ 1055 return comp_array_types (t1, t2, !!(strict & COMPARE_REDECLARATION)); 1056 1057 case TEMPLATE_TYPE_PARM: 1058 return (TEMPLATE_TYPE_IDX (t1) == TEMPLATE_TYPE_IDX (t2) 1059 && TEMPLATE_TYPE_LEVEL (t1) == TEMPLATE_TYPE_LEVEL (t2)); 1060 1061 case TYPENAME_TYPE: 1062 if (!cp_tree_equal (TYPENAME_TYPE_FULLNAME (t1), 1063 TYPENAME_TYPE_FULLNAME (t2))) 1064 return false; 1065 return same_type_p (TYPE_CONTEXT (t1), TYPE_CONTEXT (t2)); 1066 1067 case UNBOUND_CLASS_TEMPLATE: 1068 if (!cp_tree_equal (TYPE_IDENTIFIER (t1), TYPE_IDENTIFIER (t2))) 1069 return false; 1070 return same_type_p (TYPE_CONTEXT (t1), TYPE_CONTEXT (t2)); 1071 1072 case COMPLEX_TYPE: 1073 return same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)); 1074 1075 default: 1076 break; 1077 } 1078 return false; 1079} 1080 1081/* Returns 1 if TYPE1 is at least as qualified as TYPE2. */ 1082 1083bool 1084at_least_as_qualified_p (tree type1, tree type2) 1085{ 1086 int q1 = cp_type_quals (type1); 1087 int q2 = cp_type_quals (type2); 1088 1089 /* All qualifiers for TYPE2 must also appear in TYPE1. */ 1090 return (q1 & q2) == q2; 1091} 1092 1093/* Returns 1 if TYPE1 is more qualified than TYPE2. */ 1094 1095bool 1096more_qualified_p (tree type1, tree type2) 1097{ 1098 int q1 = cp_type_quals (type1); 1099 int q2 = cp_type_quals (type2); 1100 1101 return q1 != q2 && (q1 & q2) == q2; 1102} 1103 1104/* Returns 1 if TYPE1 is more cv-qualified than TYPE2, -1 if TYPE2 is 1105 more cv-qualified that TYPE1, and 0 otherwise. */ 1106 1107int 1108comp_cv_qualification (tree type1, tree type2) 1109{ 1110 int q1 = cp_type_quals (type1); 1111 int q2 = cp_type_quals (type2); 1112 1113 if (q1 == q2) 1114 return 0; 1115 1116 if ((q1 & q2) == q2) 1117 return 1; 1118 else if ((q1 & q2) == q1) 1119 return -1; 1120 1121 return 0; 1122} 1123 1124/* Returns 1 if the cv-qualification signature of TYPE1 is a proper 1125 subset of the cv-qualification signature of TYPE2, and the types 1126 are similar. Returns -1 if the other way 'round, and 0 otherwise. */ 1127 1128int 1129comp_cv_qual_signature (tree type1, tree type2) 1130{ 1131 if (comp_ptr_ttypes_real (type2, type1, -1)) 1132 return 1; 1133 else if (comp_ptr_ttypes_real (type1, type2, -1)) 1134 return -1; 1135 else 1136 return 0; 1137} 1138 1139/* If two types share a common base type, return that basetype. 1140 If there is not a unique most-derived base type, this function 1141 returns ERROR_MARK_NODE. */ 1142 1143static tree 1144common_base_type (tree tt1, tree tt2) 1145{ 1146 tree best = NULL_TREE; 1147 int i; 1148 1149 /* If one is a baseclass of another, that's good enough. */ 1150 if (UNIQUELY_DERIVED_FROM_P (tt1, tt2)) 1151 return tt1; 1152 if (UNIQUELY_DERIVED_FROM_P (tt2, tt1)) 1153 return tt2; 1154 1155 /* Otherwise, try to find a unique baseclass of TT1 1156 that is shared by TT2, and follow that down. */ 1157 for (i = CLASSTYPE_N_BASECLASSES (tt1)-1; i >= 0; i--) 1158 { 1159 tree basetype = TYPE_BINFO_BASETYPE (tt1, i); 1160 tree trial = common_base_type (basetype, tt2); 1161 if (trial) 1162 { 1163 if (trial == error_mark_node) 1164 return trial; 1165 if (best == NULL_TREE) 1166 best = trial; 1167 else if (best != trial) 1168 return error_mark_node; 1169 } 1170 } 1171 1172 /* Same for TT2. */ 1173 for (i = CLASSTYPE_N_BASECLASSES (tt2)-1; i >= 0; i--) 1174 { 1175 tree basetype = TYPE_BINFO_BASETYPE (tt2, i); 1176 tree trial = common_base_type (tt1, basetype); 1177 if (trial) 1178 { 1179 if (trial == error_mark_node) 1180 return trial; 1181 if (best == NULL_TREE) 1182 best = trial; 1183 else if (best != trial) 1184 return error_mark_node; 1185 } 1186 } 1187 return best; 1188} 1189 1190/* Subroutines of `comptypes'. */ 1191 1192/* Return true if two parameter type lists PARMS1 and PARMS2 are 1193 equivalent in the sense that functions with those parameter types 1194 can have equivalent types. The two lists must be equivalent, 1195 element by element. */ 1196 1197bool 1198compparms (tree parms1, tree parms2) 1199{ 1200 tree t1, t2; 1201 1202 /* An unspecified parmlist matches any specified parmlist 1203 whose argument types don't need default promotions. */ 1204 1205 for (t1 = parms1, t2 = parms2; 1206 t1 || t2; 1207 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2)) 1208 { 1209 /* If one parmlist is shorter than the other, 1210 they fail to match. */ 1211 if (!t1 || !t2) 1212 return false; 1213 if (!same_type_p (TREE_VALUE (t1), TREE_VALUE (t2))) 1214 return false; 1215 } 1216 return true; 1217} 1218 1219 1220/* Process a sizeof or alignof expression where the operand is a 1221 type. */ 1222 1223tree 1224cxx_sizeof_or_alignof_type (tree type, enum tree_code op, bool complain) 1225{ 1226 enum tree_code type_code; 1227 tree value; 1228 const char *op_name; 1229 1230 my_friendly_assert (op == SIZEOF_EXPR || op == ALIGNOF_EXPR, 20020720); 1231 if (type == error_mark_node) 1232 return error_mark_node; 1233 1234 if (dependent_type_p (type)) 1235 { 1236 value = build_min (op, size_type_node, type); 1237 TREE_READONLY (value) = 1; 1238 return value; 1239 } 1240 1241 op_name = operator_name_info[(int) op].name; 1242 1243 type = non_reference (type); 1244 type_code = TREE_CODE (type); 1245 1246 if (type_code == METHOD_TYPE) 1247 { 1248 if (complain && (pedantic || warn_pointer_arith)) 1249 pedwarn ("invalid application of `%s' to a member function", op_name); 1250 value = size_one_node; 1251 } 1252 else 1253 value = c_sizeof_or_alignof_type (complete_type (type), op, complain); 1254 1255 return value; 1256} 1257 1258/* Process a sizeof or alignof expression where the operand is an 1259 expression. */ 1260 1261tree 1262cxx_sizeof_or_alignof_expr (tree e, enum tree_code op) 1263{ 1264 const char *op_name = operator_name_info[(int) op].name; 1265 1266 if (e == error_mark_node) 1267 return error_mark_node; 1268 1269 if (processing_template_decl) 1270 { 1271 e = build_min (op, size_type_node, e); 1272 TREE_SIDE_EFFECTS (e) = 0; 1273 TREE_READONLY (e) = 1; 1274 1275 return e; 1276 } 1277 1278 if (TREE_CODE (e) == COMPONENT_REF 1279 && TREE_CODE (TREE_OPERAND (e, 1)) == FIELD_DECL 1280 && DECL_C_BIT_FIELD (TREE_OPERAND (e, 1))) 1281 { 1282 error ("invalid application of `%s' to a bit-field", op_name); 1283 e = char_type_node; 1284 } 1285 else if (is_overloaded_fn (e)) 1286 { 1287 pedwarn ("ISO C++ forbids applying `%s' to an expression of function type", op_name); 1288 e = char_type_node; 1289 } 1290 else if (type_unknown_p (e)) 1291 { 1292 cxx_incomplete_type_error (e, TREE_TYPE (e)); 1293 e = char_type_node; 1294 } 1295 else 1296 e = TREE_TYPE (e); 1297 1298 return cxx_sizeof_or_alignof_type (e, op, true); 1299} 1300 1301 1302/* EXPR is being used in a context that is not a function call. 1303 Enforce: 1304 1305 [expr.ref] 1306 1307 The expression can be used only as the left-hand operand of a 1308 member function call. 1309 1310 [expr.mptr.operator] 1311 1312 If the result of .* or ->* is a function, then that result can be 1313 used only as the operand for the function call operator (). 1314 1315 by issuing an error message if appropriate. Returns true iff EXPR 1316 violates these rules. */ 1317 1318bool 1319invalid_nonstatic_memfn_p (tree expr) 1320{ 1321 if (TREE_CODE (TREE_TYPE (expr)) == METHOD_TYPE) 1322 { 1323 error ("invalid use of non-static member function"); 1324 return true; 1325 } 1326 return false; 1327} 1328 1329/* Perform the conversions in [expr] that apply when an lvalue appears 1330 in an rvalue context: the lvalue-to-rvalue, array-to-pointer, and 1331 function-to-pointer conversions. 1332 1333 In addition manifest constants are replaced by their values. */ 1334 1335tree 1336decay_conversion (tree exp) 1337{ 1338 tree type; 1339 enum tree_code code; 1340 1341 type = TREE_TYPE (exp); 1342 code = TREE_CODE (type); 1343 1344 if (code == REFERENCE_TYPE) 1345 { 1346 exp = convert_from_reference (exp); 1347 type = TREE_TYPE (exp); 1348 code = TREE_CODE (type); 1349 } 1350 1351 if (type == error_mark_node) 1352 return error_mark_node; 1353 1354 if (type_unknown_p (exp)) 1355 { 1356 cxx_incomplete_type_error (exp, TREE_TYPE (exp)); 1357 return error_mark_node; 1358 } 1359 1360 /* Constants can be used directly unless they're not loadable. */ 1361 if (TREE_CODE (exp) == CONST_DECL) 1362 exp = DECL_INITIAL (exp); 1363 /* Replace a nonvolatile const static variable with its value. We 1364 don't do this for arrays, though; we want the address of the 1365 first element of the array, not the address of the first element 1366 of its initializing constant. */ 1367 else if (code != ARRAY_TYPE) 1368 { 1369 exp = decl_constant_value (exp); 1370 type = TREE_TYPE (exp); 1371 } 1372 1373 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue. 1374 Leave such NOP_EXPRs, since RHS is being used in non-lvalue context. */ 1375 1376 if (code == VOID_TYPE) 1377 { 1378 error ("void value not ignored as it ought to be"); 1379 return error_mark_node; 1380 } 1381 if (invalid_nonstatic_memfn_p (exp)) 1382 return error_mark_node; 1383 if (code == FUNCTION_TYPE || is_overloaded_fn (exp)) 1384 return build_unary_op (ADDR_EXPR, exp, 0); 1385 if (code == ARRAY_TYPE) 1386 { 1387 tree adr; 1388 tree ptrtype; 1389 1390 if (TREE_CODE (exp) == INDIRECT_REF) 1391 return build_nop (build_pointer_type (TREE_TYPE (type)), 1392 TREE_OPERAND (exp, 0)); 1393 1394 if (TREE_CODE (exp) == COMPOUND_EXPR) 1395 { 1396 tree op1 = decay_conversion (TREE_OPERAND (exp, 1)); 1397 return build (COMPOUND_EXPR, TREE_TYPE (op1), 1398 TREE_OPERAND (exp, 0), op1); 1399 } 1400 1401 if (!lvalue_p (exp) 1402 && ! (TREE_CODE (exp) == CONSTRUCTOR && TREE_STATIC (exp))) 1403 { 1404 error ("invalid use of non-lvalue array"); 1405 return error_mark_node; 1406 } 1407 1408 ptrtype = build_pointer_type (TREE_TYPE (type)); 1409 1410 if (TREE_CODE (exp) == VAR_DECL) 1411 { 1412 if (!cxx_mark_addressable (exp)) 1413 return error_mark_node; 1414 adr = build_nop (ptrtype, build_address (exp)); 1415 TREE_SIDE_EFFECTS (adr) = 0; /* Default would be, same as EXP. */ 1416 return adr; 1417 } 1418 /* This way is better for a COMPONENT_REF since it can 1419 simplify the offset for a component. */ 1420 adr = build_unary_op (ADDR_EXPR, exp, 1); 1421 return cp_convert (ptrtype, adr); 1422 } 1423 1424 /* [basic.lval]: Class rvalues can have cv-qualified types; non-class 1425 rvalues always have cv-unqualified types. */ 1426 if (! CLASS_TYPE_P (type)) 1427 exp = cp_convert (TYPE_MAIN_VARIANT (type), exp); 1428 1429 return exp; 1430} 1431 1432tree 1433default_conversion (tree exp) 1434{ 1435 exp = decay_conversion (exp); 1436 1437 if (INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (exp))) 1438 exp = perform_integral_promotions (exp); 1439 1440 return exp; 1441} 1442 1443/* EXPR is an expression with an integral or enumeration type. 1444 Perform the integral promotions in [conv.prom], and return the 1445 converted value. */ 1446 1447tree 1448perform_integral_promotions (tree expr) 1449{ 1450 tree type; 1451 tree promoted_type; 1452 1453 type = TREE_TYPE (expr); 1454 my_friendly_assert (INTEGRAL_OR_ENUMERATION_TYPE_P (type), 20030703); 1455 promoted_type = type_promotes_to (type); 1456 if (type != promoted_type) 1457 expr = cp_convert (promoted_type, expr); 1458 return expr; 1459} 1460 1461/* Take the address of an inline function without setting TREE_ADDRESSABLE 1462 or TREE_USED. */ 1463 1464tree 1465inline_conversion (tree exp) 1466{ 1467 if (TREE_CODE (exp) == FUNCTION_DECL) 1468 exp = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (exp)), exp); 1469 1470 return exp; 1471} 1472 1473/* Returns nonzero iff exp is a STRING_CST or the result of applying 1474 decay_conversion to one. */ 1475 1476int 1477string_conv_p (tree totype, tree exp, int warn) 1478{ 1479 tree t; 1480 1481 if (! flag_const_strings || TREE_CODE (totype) != POINTER_TYPE) 1482 return 0; 1483 1484 t = TREE_TYPE (totype); 1485 if (!same_type_p (t, char_type_node) 1486 && !same_type_p (t, wchar_type_node)) 1487 return 0; 1488 1489 if (TREE_CODE (exp) == STRING_CST) 1490 { 1491 /* Make sure that we don't try to convert between char and wchar_t. */ 1492 if (!same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (exp))), t)) 1493 return 0; 1494 } 1495 else 1496 { 1497 /* Is this a string constant which has decayed to 'const char *'? */ 1498 t = build_pointer_type (build_qualified_type (t, TYPE_QUAL_CONST)); 1499 if (!same_type_p (TREE_TYPE (exp), t)) 1500 return 0; 1501 STRIP_NOPS (exp); 1502 if (TREE_CODE (exp) != ADDR_EXPR 1503 || TREE_CODE (TREE_OPERAND (exp, 0)) != STRING_CST) 1504 return 0; 1505 } 1506 1507 /* This warning is not very useful, as it complains about printf. */ 1508 if (warn && warn_write_strings) 1509 warning ("deprecated conversion from string constant to `%T'", totype); 1510 1511 return 1; 1512} 1513 1514/* Given a COND_EXPR, MIN_EXPR, or MAX_EXPR in T, return it in a form that we 1515 can, for example, use as an lvalue. This code used to be in 1516 unary_complex_lvalue, but we needed it to deal with `a = (d == c) ? b : c' 1517 expressions, where we're dealing with aggregates. But now it's again only 1518 called from unary_complex_lvalue. The case (in particular) that led to 1519 this was with CODE == ADDR_EXPR, since it's not an lvalue when we'd 1520 get it there. */ 1521 1522static tree 1523rationalize_conditional_expr (enum tree_code code, tree t) 1524{ 1525 /* For MIN_EXPR or MAX_EXPR, fold-const.c has arranged things so that 1526 the first operand is always the one to be used if both operands 1527 are equal, so we know what conditional expression this used to be. */ 1528 if (TREE_CODE (t) == MIN_EXPR || TREE_CODE (t) == MAX_EXPR) 1529 { 1530 return 1531 build_conditional_expr (build_x_binary_op ((TREE_CODE (t) == MIN_EXPR 1532 ? LE_EXPR : GE_EXPR), 1533 TREE_OPERAND (t, 0), 1534 TREE_OPERAND (t, 1), 1535 /*overloaded_p=*/NULL), 1536 build_unary_op (code, TREE_OPERAND (t, 0), 0), 1537 build_unary_op (code, TREE_OPERAND (t, 1), 0)); 1538 } 1539 1540 return 1541 build_conditional_expr (TREE_OPERAND (t, 0), 1542 build_unary_op (code, TREE_OPERAND (t, 1), 0), 1543 build_unary_op (code, TREE_OPERAND (t, 2), 0)); 1544} 1545 1546/* Given the TYPE of an anonymous union field inside T, return the 1547 FIELD_DECL for the field. If not found return NULL_TREE. Because 1548 anonymous unions can nest, we must also search all anonymous unions 1549 that are directly reachable. */ 1550 1551tree 1552lookup_anon_field (tree t, tree type) 1553{ 1554 tree field; 1555 1556 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field)) 1557 { 1558 if (TREE_STATIC (field)) 1559 continue; 1560 if (TREE_CODE (field) != FIELD_DECL || DECL_ARTIFICIAL (field)) 1561 continue; 1562 1563 /* If we find it directly, return the field. */ 1564 if (DECL_NAME (field) == NULL_TREE 1565 && type == TYPE_MAIN_VARIANT (TREE_TYPE (field))) 1566 { 1567 return field; 1568 } 1569 1570 /* Otherwise, it could be nested, search harder. */ 1571 if (DECL_NAME (field) == NULL_TREE 1572 && ANON_AGGR_TYPE_P (TREE_TYPE (field))) 1573 { 1574 tree subfield = lookup_anon_field (TREE_TYPE (field), type); 1575 if (subfield) 1576 return subfield; 1577 } 1578 } 1579 return NULL_TREE; 1580} 1581 1582/* Build an expression representing OBJECT.MEMBER. OBJECT is an 1583 expression; MEMBER is a DECL or baselink. If ACCESS_PATH is 1584 non-NULL, it indicates the path to the base used to name MEMBER. 1585 If PRESERVE_REFERENCE is true, the expression returned will have 1586 REFERENCE_TYPE if the MEMBER does. Otherwise, the expression 1587 returned will have the type referred to by the reference. 1588 1589 This function does not perform access control; that is either done 1590 earlier by the parser when the name of MEMBER is resolved to MEMBER 1591 itself, or later when overload resolution selects one of the 1592 functions indicated by MEMBER. */ 1593 1594tree 1595build_class_member_access_expr (tree object, tree member, 1596 tree access_path, bool preserve_reference) 1597{ 1598 tree object_type; 1599 tree member_scope; 1600 tree result = NULL_TREE; 1601 1602 if (object == error_mark_node || member == error_mark_node) 1603 return error_mark_node; 1604 1605 if (TREE_CODE (member) == PSEUDO_DTOR_EXPR) 1606 return member; 1607 1608 my_friendly_assert (DECL_P (member) || BASELINK_P (member), 1609 20020801); 1610 1611 /* [expr.ref] 1612 1613 The type of the first expression shall be "class object" (of a 1614 complete type). */ 1615 object_type = TREE_TYPE (object); 1616 if (!currently_open_class (object_type) 1617 && !complete_type_or_else (object_type, object)) 1618 return error_mark_node; 1619 if (!CLASS_TYPE_P (object_type)) 1620 { 1621 error ("request for member `%D' in `%E', which is of non-class type `%T'", 1622 member, object, object_type); 1623 return error_mark_node; 1624 } 1625 1626 /* The standard does not seem to actually say that MEMBER must be a 1627 member of OBJECT_TYPE. However, that is clearly what is 1628 intended. */ 1629 if (DECL_P (member)) 1630 { 1631 member_scope = DECL_CLASS_CONTEXT (member); 1632 mark_used (member); 1633 if (TREE_DEPRECATED (member)) 1634 warn_deprecated_use (member); 1635 } 1636 else 1637 member_scope = BINFO_TYPE (BASELINK_BINFO (member)); 1638 /* If MEMBER is from an anonymous aggregate, MEMBER_SCOPE will 1639 presently be the anonymous union. Go outwards until we find a 1640 type related to OBJECT_TYPE. */ 1641 while (ANON_AGGR_TYPE_P (member_scope) 1642 && !same_type_ignoring_top_level_qualifiers_p (member_scope, 1643 object_type)) 1644 member_scope = TYPE_CONTEXT (member_scope); 1645 if (!member_scope || !DERIVED_FROM_P (member_scope, object_type)) 1646 { 1647 if (TREE_CODE (member) == FIELD_DECL) 1648 error ("invalid use of nonstatic data member '%E'", member); 1649 else 1650 error ("`%D' is not a member of `%T'", member, object_type); 1651 return error_mark_node; 1652 } 1653 1654 /* Transform `(a, b).x' into `(*(a, &b)).x', `(a ? b : c).x' into 1655 `(*(a ? &b : &c)).x', and so on. A COND_EXPR is only an lvalue 1656 in the frontend; only _DECLs and _REFs are lvalues in the backend. */ 1657 { 1658 tree temp = unary_complex_lvalue (ADDR_EXPR, object); 1659 if (temp) 1660 object = build_indirect_ref (temp, NULL); 1661 } 1662 1663 /* In [expr.ref], there is an explicit list of the valid choices for 1664 MEMBER. We check for each of those cases here. */ 1665 if (TREE_CODE (member) == VAR_DECL) 1666 { 1667 /* A static data member. */ 1668 result = member; 1669 /* If OBJECT has side-effects, they are supposed to occur. */ 1670 if (TREE_SIDE_EFFECTS (object)) 1671 result = build (COMPOUND_EXPR, TREE_TYPE (result), object, result); 1672 } 1673 else if (TREE_CODE (member) == FIELD_DECL) 1674 { 1675 /* A non-static data member. */ 1676 bool null_object_p; 1677 int type_quals; 1678 tree member_type; 1679 1680 null_object_p = (TREE_CODE (object) == INDIRECT_REF 1681 && integer_zerop (TREE_OPERAND (object, 0))); 1682 1683 /* Convert OBJECT to the type of MEMBER. */ 1684 if (!same_type_p (TYPE_MAIN_VARIANT (object_type), 1685 TYPE_MAIN_VARIANT (member_scope))) 1686 { 1687 tree binfo; 1688 base_kind kind; 1689 1690 binfo = lookup_base (access_path ? access_path : object_type, 1691 member_scope, ba_ignore, &kind); 1692 if (binfo == error_mark_node) 1693 return error_mark_node; 1694 1695 /* It is invalid to try to get to a virtual base of a 1696 NULL object. The most common cause is invalid use of 1697 offsetof macro. */ 1698 if (null_object_p && kind == bk_via_virtual) 1699 { 1700 error ("invalid access to non-static data member `%D' of NULL object", 1701 member); 1702 error ("(perhaps the `offsetof' macro was used incorrectly)"); 1703 return error_mark_node; 1704 } 1705 1706 /* Convert to the base. */ 1707 object = build_base_path (PLUS_EXPR, object, binfo, 1708 /*nonnull=*/1); 1709 /* If we found the base successfully then we should be able 1710 to convert to it successfully. */ 1711 my_friendly_assert (object != error_mark_node, 1712 20020801); 1713 } 1714 1715 /* Complain about other invalid uses of offsetof, even though they will 1716 give the right answer. Note that we complain whether or not they 1717 actually used the offsetof macro, since there's no way to know at this 1718 point. So we just give a warning, instead of a pedwarn. */ 1719 if (null_object_p && warn_invalid_offsetof 1720 && CLASSTYPE_NON_POD_P (object_type)) 1721 { 1722 warning ("invalid access to non-static data member `%D' of NULL object", 1723 member); 1724 warning ("(perhaps the `offsetof' macro was used incorrectly)"); 1725 } 1726 1727 /* If MEMBER is from an anonymous aggregate, we have converted 1728 OBJECT so that it refers to the class containing the 1729 anonymous union. Generate a reference to the anonymous union 1730 itself, and recur to find MEMBER. */ 1731 if (ANON_AGGR_TYPE_P (DECL_CONTEXT (member)) 1732 /* When this code is called from build_field_call, the 1733 object already has the type of the anonymous union. 1734 That is because the COMPONENT_REF was already 1735 constructed, and was then disassembled before calling 1736 build_field_call. After the function-call code is 1737 cleaned up, this waste can be eliminated. */ 1738 && (!same_type_ignoring_top_level_qualifiers_p 1739 (TREE_TYPE (object), DECL_CONTEXT (member)))) 1740 { 1741 tree anonymous_union; 1742 1743 anonymous_union = lookup_anon_field (TREE_TYPE (object), 1744 DECL_CONTEXT (member)); 1745 object = build_class_member_access_expr (object, 1746 anonymous_union, 1747 /*access_path=*/NULL_TREE, 1748 preserve_reference); 1749 } 1750 1751 /* Compute the type of the field, as described in [expr.ref]. */ 1752 type_quals = TYPE_UNQUALIFIED; 1753 member_type = TREE_TYPE (member); 1754 if (TREE_CODE (member_type) != REFERENCE_TYPE) 1755 { 1756 type_quals = (cp_type_quals (member_type) 1757 | cp_type_quals (object_type)); 1758 1759 /* A field is const (volatile) if the enclosing object, or the 1760 field itself, is const (volatile). But, a mutable field is 1761 not const, even within a const object. */ 1762 if (DECL_MUTABLE_P (member)) 1763 type_quals &= ~TYPE_QUAL_CONST; 1764 member_type = cp_build_qualified_type (member_type, type_quals); 1765 } 1766 1767 result = fold (build (COMPONENT_REF, member_type, object, member)); 1768 1769 /* Mark the expression const or volatile, as appropriate. Even 1770 though we've dealt with the type above, we still have to mark the 1771 expression itself. */ 1772 if (type_quals & TYPE_QUAL_CONST) 1773 TREE_READONLY (result) = 1; 1774 else if (type_quals & TYPE_QUAL_VOLATILE) 1775 TREE_THIS_VOLATILE (result) = 1; 1776 } 1777 else if (BASELINK_P (member)) 1778 { 1779 /* The member is a (possibly overloaded) member function. */ 1780 tree functions; 1781 tree type; 1782 1783 /* If the MEMBER is exactly one static member function, then we 1784 know the type of the expression. Otherwise, we must wait 1785 until overload resolution has been performed. */ 1786 functions = BASELINK_FUNCTIONS (member); 1787 if (TREE_CODE (functions) == FUNCTION_DECL 1788 && DECL_STATIC_FUNCTION_P (functions)) 1789 type = TREE_TYPE (functions); 1790 else 1791 type = unknown_type_node; 1792 /* Note that we do not convert OBJECT to the BASELINK_BINFO 1793 base. That will happen when the function is called. */ 1794 result = build (COMPONENT_REF, type, object, member); 1795 } 1796 else if (TREE_CODE (member) == CONST_DECL) 1797 { 1798 /* The member is an enumerator. */ 1799 result = member; 1800 /* If OBJECT has side-effects, they are supposed to occur. */ 1801 if (TREE_SIDE_EFFECTS (object)) 1802 result = build (COMPOUND_EXPR, TREE_TYPE (result), 1803 object, result); 1804 } 1805 else 1806 { 1807 error ("invalid use of `%D'", member); 1808 return error_mark_node; 1809 } 1810 1811 if (!preserve_reference) 1812 /* [expr.ref] 1813 1814 If E2 is declared to have type "reference to T", then ... the 1815 type of E1.E2 is T. */ 1816 result = convert_from_reference (result); 1817 1818 return result; 1819} 1820 1821/* Return the destructor denoted by OBJECT.SCOPE::~DTOR_NAME, or, if 1822 SCOPE is NULL, by OBJECT.~DTOR_NAME. */ 1823 1824static tree 1825lookup_destructor (tree object, tree scope, tree dtor_name) 1826{ 1827 tree object_type = TREE_TYPE (object); 1828 tree dtor_type = TREE_OPERAND (dtor_name, 0); 1829 tree expr; 1830 1831 if (scope && !check_dtor_name (scope, dtor_name)) 1832 { 1833 error ("qualified type `%T' does not match destructor name `~%T'", 1834 scope, dtor_type); 1835 return error_mark_node; 1836 } 1837 if (!DERIVED_FROM_P (dtor_type, TYPE_MAIN_VARIANT (object_type))) 1838 { 1839 error ("the type being destroyed is `%T', but the destructor refers to `%T'", 1840 TYPE_MAIN_VARIANT (object_type), dtor_type); 1841 return error_mark_node; 1842 } 1843 if (!TYPE_HAS_DESTRUCTOR (dtor_type)) 1844 return build (PSEUDO_DTOR_EXPR, void_type_node, object, scope, 1845 dtor_type); 1846 expr = lookup_member (dtor_type, complete_dtor_identifier, 1847 /*protect=*/1, /*want_type=*/false); 1848 expr = (adjust_result_of_qualified_name_lookup 1849 (expr, dtor_type, object_type)); 1850 return expr; 1851} 1852 1853/* This function is called by the parser to process a class member 1854 access expression of the form OBJECT.NAME. NAME is a node used by 1855 the parser to represent a name; it is not yet a DECL. It may, 1856 however, be a BASELINK where the BASELINK_FUNCTIONS is a 1857 TEMPLATE_ID_EXPR. Templates must be looked up by the parser, and 1858 there is no reason to do the lookup twice, so the parser keeps the 1859 BASELINK. */ 1860 1861tree 1862finish_class_member_access_expr (tree object, tree name) 1863{ 1864 tree expr; 1865 tree object_type; 1866 tree member; 1867 tree access_path = NULL_TREE; 1868 tree orig_object = object; 1869 tree orig_name = name; 1870 1871 if (object == error_mark_node || name == error_mark_node) 1872 return error_mark_node; 1873 1874 object_type = TREE_TYPE (object); 1875 1876 if (processing_template_decl) 1877 { 1878 if (/* If OBJECT_TYPE is dependent, so is OBJECT.NAME. */ 1879 dependent_type_p (object_type) 1880 /* If NAME is just an IDENTIFIER_NODE, then the expression 1881 is dependent. */ 1882 || TREE_CODE (object) == IDENTIFIER_NODE 1883 /* If NAME is "f<args>", where either 'f' or 'args' is 1884 dependent, then the expression is dependent. */ 1885 || (TREE_CODE (name) == TEMPLATE_ID_EXPR 1886 && dependent_template_id_p (TREE_OPERAND (name, 0), 1887 TREE_OPERAND (name, 1))) 1888 /* If NAME is "T::X" where "T" is dependent, then the 1889 expression is dependent. */ 1890 || (TREE_CODE (name) == SCOPE_REF 1891 && TYPE_P (TREE_OPERAND (name, 0)) 1892 && dependent_type_p (TREE_OPERAND (name, 0)))) 1893 return build_min_nt (COMPONENT_REF, object, name); 1894 object = build_non_dependent_expr (object); 1895 } 1896 1897 if (TREE_CODE (object_type) == REFERENCE_TYPE) 1898 { 1899 object = convert_from_reference (object); 1900 object_type = TREE_TYPE (object); 1901 } 1902 1903 /* [expr.ref] 1904 1905 The type of the first expression shall be "class object" (of a 1906 complete type). */ 1907 if (!currently_open_class (object_type) 1908 && !complete_type_or_else (object_type, object)) 1909 return error_mark_node; 1910 if (!CLASS_TYPE_P (object_type)) 1911 { 1912 error ("request for member `%D' in `%E', which is of non-class type `%T'", 1913 name, object, object_type); 1914 return error_mark_node; 1915 } 1916 1917 if (BASELINK_P (name)) 1918 { 1919 /* A member function that has already been looked up. */ 1920 my_friendly_assert ((TREE_CODE (BASELINK_FUNCTIONS (name)) 1921 == TEMPLATE_ID_EXPR), 1922 20020805); 1923 member = name; 1924 } 1925 else 1926 { 1927 bool is_template_id = false; 1928 tree template_args = NULL_TREE; 1929 tree scope; 1930 1931 if (TREE_CODE (name) == TEMPLATE_ID_EXPR) 1932 { 1933 is_template_id = true; 1934 template_args = TREE_OPERAND (name, 1); 1935 name = TREE_OPERAND (name, 0); 1936 1937 if (TREE_CODE (name) == OVERLOAD) 1938 name = DECL_NAME (get_first_fn (name)); 1939 else if (DECL_P (name)) 1940 name = DECL_NAME (name); 1941 } 1942 1943 if (TREE_CODE (name) == SCOPE_REF) 1944 { 1945 /* A qualified name. The qualifying class or namespace `S' has 1946 already been looked up; it is either a TYPE or a 1947 NAMESPACE_DECL. The member name is either an IDENTIFIER_NODE 1948 or a BIT_NOT_EXPR. */ 1949 scope = TREE_OPERAND (name, 0); 1950 name = TREE_OPERAND (name, 1); 1951 my_friendly_assert ((CLASS_TYPE_P (scope) 1952 || TREE_CODE (scope) == NAMESPACE_DECL), 1953 20020804); 1954 my_friendly_assert ((TREE_CODE (name) == IDENTIFIER_NODE 1955 || TREE_CODE (name) == BIT_NOT_EXPR), 1956 20020804); 1957 1958 /* If SCOPE is a namespace, then the qualified name does not 1959 name a member of OBJECT_TYPE. */ 1960 if (TREE_CODE (scope) == NAMESPACE_DECL) 1961 { 1962 error ("`%D::%D' is not a member of `%T'", 1963 scope, name, object_type); 1964 return error_mark_node; 1965 } 1966 1967 /* Find the base of OBJECT_TYPE corresponding to SCOPE. */ 1968 access_path = lookup_base (object_type, scope, ba_check, NULL); 1969 if (access_path == error_mark_node) 1970 return error_mark_node; 1971 if (!access_path) 1972 { 1973 error ("`%T' is not a base of `%T'", scope, object_type); 1974 return error_mark_node; 1975 } 1976 } 1977 else 1978 { 1979 scope = NULL_TREE; 1980 access_path = object_type; 1981 } 1982 1983 if (TREE_CODE (name) == BIT_NOT_EXPR) 1984 member = lookup_destructor (object, scope, name); 1985 else 1986 { 1987 /* Look up the member. */ 1988 member = lookup_member (access_path, name, /*protect=*/1, 1989 /*want_type=*/false); 1990 if (member == NULL_TREE) 1991 { 1992 error ("'%D' has no member named '%E'", object_type, name); 1993 return error_mark_node; 1994 } 1995 if (member == error_mark_node) 1996 return error_mark_node; 1997 } 1998 1999 if (is_template_id) 2000 { 2001 tree template = member; 2002 2003 if (BASELINK_P (template)) 2004 template = lookup_template_function (template, template_args); 2005 else 2006 { 2007 error ("`%D' is not a member template function", name); 2008 return error_mark_node; 2009 } 2010 } 2011 } 2012 2013 if (TREE_DEPRECATED (member)) 2014 warn_deprecated_use (member); 2015 2016 expr = build_class_member_access_expr (object, member, access_path, 2017 /*preserve_reference=*/false); 2018 if (processing_template_decl && expr != error_mark_node) 2019 return build_min_non_dep (COMPONENT_REF, expr, 2020 orig_object, orig_name); 2021 return expr; 2022} 2023 2024/* Return an expression for the MEMBER_NAME field in the internal 2025 representation of PTRMEM, a pointer-to-member function. (Each 2026 pointer-to-member function type gets its own RECORD_TYPE so it is 2027 more convenient to access the fields by name than by FIELD_DECL.) 2028 This routine converts the NAME to a FIELD_DECL and then creates the 2029 node for the complete expression. */ 2030 2031tree 2032build_ptrmemfunc_access_expr (tree ptrmem, tree member_name) 2033{ 2034 tree ptrmem_type; 2035 tree member; 2036 tree member_type; 2037 2038 /* This code is a stripped down version of 2039 build_class_member_access_expr. It does not work to use that 2040 routine directly because it expects the object to be of class 2041 type. */ 2042 ptrmem_type = TREE_TYPE (ptrmem); 2043 my_friendly_assert (TYPE_PTRMEMFUNC_P (ptrmem_type), 20020804); 2044 member = lookup_member (ptrmem_type, member_name, /*protect=*/0, 2045 /*want_type=*/false); 2046 member_type = cp_build_qualified_type (TREE_TYPE (member), 2047 cp_type_quals (ptrmem_type)); 2048 return fold (build (COMPONENT_REF, member_type, ptrmem, member)); 2049} 2050 2051/* Given an expression PTR for a pointer, return an expression 2052 for the value pointed to. 2053 ERRORSTRING is the name of the operator to appear in error messages. 2054 2055 This function may need to overload OPERATOR_FNNAME. 2056 Must also handle REFERENCE_TYPEs for C++. */ 2057 2058tree 2059build_x_indirect_ref (tree expr, const char *errorstring) 2060{ 2061 tree orig_expr = expr; 2062 tree rval; 2063 2064 if (processing_template_decl) 2065 { 2066 if (type_dependent_expression_p (expr)) 2067 return build_min_nt (INDIRECT_REF, expr); 2068 expr = build_non_dependent_expr (expr); 2069 } 2070 2071 rval = build_new_op (INDIRECT_REF, LOOKUP_NORMAL, expr, NULL_TREE, 2072 NULL_TREE, /*overloaded_p=*/NULL); 2073 if (!rval) 2074 rval = build_indirect_ref (expr, errorstring); 2075 2076 if (processing_template_decl && rval != error_mark_node) 2077 return build_min_non_dep (INDIRECT_REF, rval, orig_expr); 2078 else 2079 return rval; 2080} 2081 2082tree 2083build_indirect_ref (tree ptr, const char *errorstring) 2084{ 2085 tree pointer, type; 2086 2087 if (ptr == error_mark_node) 2088 return error_mark_node; 2089 2090 if (ptr == current_class_ptr) 2091 return current_class_ref; 2092 2093 pointer = (TREE_CODE (TREE_TYPE (ptr)) == REFERENCE_TYPE 2094 ? ptr : decay_conversion (ptr)); 2095 type = TREE_TYPE (pointer); 2096 2097 if (TYPE_PTR_P (type) || TREE_CODE (type) == REFERENCE_TYPE) 2098 { 2099 /* [expr.unary.op] 2100 2101 If the type of the expression is "pointer to T," the type 2102 of the result is "T." 2103 2104 We must use the canonical variant because certain parts of 2105 the back end, like fold, do pointer comparisons between 2106 types. */ 2107 tree t = canonical_type_variant (TREE_TYPE (type)); 2108 2109 if (VOID_TYPE_P (t)) 2110 { 2111 /* A pointer to incomplete type (other than cv void) can be 2112 dereferenced [expr.unary.op]/1 */ 2113 error ("`%T' is not a pointer-to-object type", type); 2114 return error_mark_node; 2115 } 2116 else if (TREE_CODE (pointer) == ADDR_EXPR 2117 && same_type_p (t, TREE_TYPE (TREE_OPERAND (pointer, 0)))) 2118 /* The POINTER was something like `&x'. We simplify `*&x' to 2119 `x'. */ 2120 return TREE_OPERAND (pointer, 0); 2121 else 2122 { 2123 tree ref = build1 (INDIRECT_REF, t, pointer); 2124 2125 /* We *must* set TREE_READONLY when dereferencing a pointer to const, 2126 so that we get the proper error message if the result is used 2127 to assign to. Also, &* is supposed to be a no-op. */ 2128 TREE_READONLY (ref) = CP_TYPE_CONST_P (t); 2129 TREE_THIS_VOLATILE (ref) = CP_TYPE_VOLATILE_P (t); 2130 TREE_SIDE_EFFECTS (ref) 2131 = (TREE_THIS_VOLATILE (ref) || TREE_SIDE_EFFECTS (pointer)); 2132 return ref; 2133 } 2134 } 2135 /* `pointer' won't be an error_mark_node if we were given a 2136 pointer to member, so it's cool to check for this here. */ 2137 else if (TYPE_PTR_TO_MEMBER_P (type)) 2138 error ("invalid use of `%s' on pointer to member", errorstring); 2139 else if (pointer != error_mark_node) 2140 { 2141 if (errorstring) 2142 error ("invalid type argument of `%s'", errorstring); 2143 else 2144 error ("invalid type argument"); 2145 } 2146 return error_mark_node; 2147} 2148 2149/* This handles expressions of the form "a[i]", which denotes 2150 an array reference. 2151 2152 This is logically equivalent in C to *(a+i), but we may do it differently. 2153 If A is a variable or a member, we generate a primitive ARRAY_REF. 2154 This avoids forcing the array out of registers, and can work on 2155 arrays that are not lvalues (for example, members of structures returned 2156 by functions). 2157 2158 If INDEX is of some user-defined type, it must be converted to 2159 integer type. Otherwise, to make a compatible PLUS_EXPR, it 2160 will inherit the type of the array, which will be some pointer type. */ 2161 2162tree 2163build_array_ref (tree array, tree idx) 2164{ 2165 if (idx == 0) 2166 { 2167 error ("subscript missing in array reference"); 2168 return error_mark_node; 2169 } 2170 2171 if (TREE_TYPE (array) == error_mark_node 2172 || TREE_TYPE (idx) == error_mark_node) 2173 return error_mark_node; 2174 2175 /* If ARRAY is a COMPOUND_EXPR or COND_EXPR, move our reference 2176 inside it. */ 2177 switch (TREE_CODE (array)) 2178 { 2179 case COMPOUND_EXPR: 2180 { 2181 tree value = build_array_ref (TREE_OPERAND (array, 1), idx); 2182 return build (COMPOUND_EXPR, TREE_TYPE (value), 2183 TREE_OPERAND (array, 0), value); 2184 } 2185 2186 case COND_EXPR: 2187 return build_conditional_expr 2188 (TREE_OPERAND (array, 0), 2189 build_array_ref (TREE_OPERAND (array, 1), idx), 2190 build_array_ref (TREE_OPERAND (array, 2), idx)); 2191 2192 default: 2193 break; 2194 } 2195 2196 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE 2197 && TREE_CODE (array) != INDIRECT_REF) 2198 { 2199 tree rval, type; 2200 2201 /* Subscripting with type char is likely to lose 2202 on a machine where chars are signed. 2203 So warn on any machine, but optionally. 2204 Don't warn for unsigned char since that type is safe. 2205 Don't warn for signed char because anyone who uses that 2206 must have done so deliberately. */ 2207 if (warn_char_subscripts 2208 && TYPE_MAIN_VARIANT (TREE_TYPE (idx)) == char_type_node) 2209 warning ("array subscript has type `char'"); 2210 2211 if (!INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (idx))) 2212 { 2213 error ("array subscript is not an integer"); 2214 return error_mark_node; 2215 } 2216 2217 /* Apply integral promotions *after* noticing character types. 2218 (It is unclear why we do these promotions -- the standard 2219 does not say that we should. In fact, the natual thing would 2220 seem to be to convert IDX to ptrdiff_t; we're performing 2221 pointer arithmetic.) */ 2222 idx = perform_integral_promotions (idx); 2223 2224 /* An array that is indexed by a non-constant 2225 cannot be stored in a register; we must be able to do 2226 address arithmetic on its address. 2227 Likewise an array of elements of variable size. */ 2228 if (TREE_CODE (idx) != INTEGER_CST 2229 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array))) 2230 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) 2231 != INTEGER_CST))) 2232 { 2233 if (!cxx_mark_addressable (array)) 2234 return error_mark_node; 2235 } 2236 2237 /* An array that is indexed by a constant value which is not within 2238 the array bounds cannot be stored in a register either; because we 2239 would get a crash in store_bit_field/extract_bit_field when trying 2240 to access a non-existent part of the register. */ 2241 if (TREE_CODE (idx) == INTEGER_CST 2242 && TYPE_VALUES (TREE_TYPE (array)) 2243 && ! int_fits_type_p (idx, TYPE_VALUES (TREE_TYPE (array)))) 2244 { 2245 if (!cxx_mark_addressable (array)) 2246 return error_mark_node; 2247 } 2248 2249 if (pedantic && !lvalue_p (array)) 2250 pedwarn ("ISO C++ forbids subscripting non-lvalue array"); 2251 2252 /* Note in C++ it is valid to subscript a `register' array, since 2253 it is valid to take the address of something with that 2254 storage specification. */ 2255 if (extra_warnings) 2256 { 2257 tree foo = array; 2258 while (TREE_CODE (foo) == COMPONENT_REF) 2259 foo = TREE_OPERAND (foo, 0); 2260 if (TREE_CODE (foo) == VAR_DECL && DECL_REGISTER (foo)) 2261 warning ("subscripting array declared `register'"); 2262 } 2263 2264 type = TREE_TYPE (TREE_TYPE (array)); 2265 rval = build (ARRAY_REF, type, array, idx); 2266 /* Array ref is const/volatile if the array elements are 2267 or if the array is.. */ 2268 TREE_READONLY (rval) 2269 |= (CP_TYPE_CONST_P (type) | TREE_READONLY (array)); 2270 TREE_SIDE_EFFECTS (rval) 2271 |= (CP_TYPE_VOLATILE_P (type) | TREE_SIDE_EFFECTS (array)); 2272 TREE_THIS_VOLATILE (rval) 2273 |= (CP_TYPE_VOLATILE_P (type) | TREE_THIS_VOLATILE (array)); 2274 return require_complete_type (fold (rval)); 2275 } 2276 2277 { 2278 tree ar = default_conversion (array); 2279 tree ind = default_conversion (idx); 2280 2281 /* Put the integer in IND to simplify error checking. */ 2282 if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE) 2283 { 2284 tree temp = ar; 2285 ar = ind; 2286 ind = temp; 2287 } 2288 2289 if (ar == error_mark_node) 2290 return ar; 2291 2292 if (TREE_CODE (TREE_TYPE (ar)) != POINTER_TYPE) 2293 { 2294 error ("subscripted value is neither array nor pointer"); 2295 return error_mark_node; 2296 } 2297 if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE) 2298 { 2299 error ("array subscript is not an integer"); 2300 return error_mark_node; 2301 } 2302 2303 return build_indirect_ref (cp_build_binary_op (PLUS_EXPR, ar, ind), 2304 "array indexing"); 2305 } 2306} 2307 2308/* Resolve a pointer to member function. INSTANCE is the object 2309 instance to use, if the member points to a virtual member. 2310 2311 This used to avoid checking for virtual functions if basetype 2312 has no virtual functions, according to an earlier ANSI draft. 2313 With the final ISO C++ rules, such an optimization is 2314 incorrect: A pointer to a derived member can be static_cast 2315 to pointer-to-base-member, as long as the dynamic object 2316 later has the right member. */ 2317 2318tree 2319get_member_function_from_ptrfunc (tree *instance_ptrptr, tree function) 2320{ 2321 if (TREE_CODE (function) == OFFSET_REF) 2322 function = TREE_OPERAND (function, 1); 2323 2324 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function))) 2325 { 2326 tree idx, delta, e1, e2, e3, vtbl, basetype; 2327 tree fntype = TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (function)); 2328 2329 tree instance_ptr = *instance_ptrptr; 2330 tree instance_save_expr = 0; 2331 if (instance_ptr == error_mark_node) 2332 { 2333 if (TREE_CODE (function) == PTRMEM_CST) 2334 { 2335 /* Extracting the function address from a pmf is only 2336 allowed with -Wno-pmf-conversions. It only works for 2337 pmf constants. */ 2338 e1 = build_addr_func (PTRMEM_CST_MEMBER (function)); 2339 e1 = convert (fntype, e1); 2340 return e1; 2341 } 2342 else 2343 { 2344 error ("object missing in use of `%E'", function); 2345 return error_mark_node; 2346 } 2347 } 2348 2349 if (TREE_SIDE_EFFECTS (instance_ptr)) 2350 instance_ptr = instance_save_expr = save_expr (instance_ptr); 2351 2352 if (TREE_SIDE_EFFECTS (function)) 2353 function = save_expr (function); 2354 2355 /* Start by extracting all the information from the PMF itself. */ 2356 e3 = PFN_FROM_PTRMEMFUNC (function); 2357 delta = build_ptrmemfunc_access_expr (function, delta_identifier); 2358 idx = build1 (NOP_EXPR, vtable_index_type, e3); 2359 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION) 2360 { 2361 case ptrmemfunc_vbit_in_pfn: 2362 e1 = cp_build_binary_op (BIT_AND_EXPR, idx, integer_one_node); 2363 idx = cp_build_binary_op (MINUS_EXPR, idx, integer_one_node); 2364 break; 2365 2366 case ptrmemfunc_vbit_in_delta: 2367 e1 = cp_build_binary_op (BIT_AND_EXPR, delta, integer_one_node); 2368 delta = cp_build_binary_op (RSHIFT_EXPR, delta, integer_one_node); 2369 break; 2370 2371 default: 2372 abort (); 2373 } 2374 2375 /* Convert down to the right base before using the instance. First 2376 use the type... */ 2377 basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (fntype)); 2378 basetype = lookup_base (TREE_TYPE (TREE_TYPE (instance_ptr)), 2379 basetype, ba_check, NULL); 2380 instance_ptr = build_base_path (PLUS_EXPR, instance_ptr, basetype, 1); 2381 if (instance_ptr == error_mark_node) 2382 return error_mark_node; 2383 /* ...and then the delta in the PMF. */ 2384 instance_ptr = build (PLUS_EXPR, TREE_TYPE (instance_ptr), 2385 instance_ptr, delta); 2386 2387 /* Hand back the adjusted 'this' argument to our caller. */ 2388 *instance_ptrptr = instance_ptr; 2389 2390 /* Next extract the vtable pointer from the object. */ 2391 vtbl = build1 (NOP_EXPR, build_pointer_type (vtbl_ptr_type_node), 2392 instance_ptr); 2393 vtbl = build_indirect_ref (vtbl, NULL); 2394 2395 /* Finally, extract the function pointer from the vtable. */ 2396 e2 = fold (build (PLUS_EXPR, TREE_TYPE (vtbl), vtbl, idx)); 2397 e2 = build_indirect_ref (e2, NULL); 2398 TREE_CONSTANT (e2) = 1; 2399 2400 /* When using function descriptors, the address of the 2401 vtable entry is treated as a function pointer. */ 2402 if (TARGET_VTABLE_USES_DESCRIPTORS) 2403 e2 = build1 (NOP_EXPR, TREE_TYPE (e2), 2404 build_unary_op (ADDR_EXPR, e2, /*noconvert=*/1)); 2405 2406 TREE_TYPE (e2) = TREE_TYPE (e3); 2407 e1 = build_conditional_expr (e1, e2, e3); 2408 2409 /* Make sure this doesn't get evaluated first inside one of the 2410 branches of the COND_EXPR. */ 2411 if (instance_save_expr) 2412 e1 = build (COMPOUND_EXPR, TREE_TYPE (e1), 2413 instance_save_expr, e1); 2414 2415 function = e1; 2416 } 2417 return function; 2418} 2419 2420tree 2421build_function_call (tree function, tree params) 2422{ 2423 tree fntype, fndecl; 2424 tree coerced_params; 2425 tree result; 2426 tree name = NULL_TREE, assembler_name = NULL_TREE; 2427 int is_method; 2428 tree original = function; 2429 2430 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue. 2431 Strip such NOP_EXPRs, since FUNCTION is used in non-lvalue context. */ 2432 if (TREE_CODE (function) == NOP_EXPR 2433 && TREE_TYPE (function) == TREE_TYPE (TREE_OPERAND (function, 0))) 2434 function = TREE_OPERAND (function, 0); 2435 2436 if (TREE_CODE (function) == FUNCTION_DECL) 2437 { 2438 name = DECL_NAME (function); 2439 assembler_name = DECL_ASSEMBLER_NAME (function); 2440 2441 mark_used (function); 2442 fndecl = function; 2443 2444 /* Convert anything with function type to a pointer-to-function. */ 2445 if (pedantic && DECL_MAIN_P (function)) 2446 pedwarn ("ISO C++ forbids calling `::main' from within program"); 2447 2448 /* Differs from default_conversion by not setting TREE_ADDRESSABLE 2449 (because calling an inline function does not mean the function 2450 needs to be separately compiled). */ 2451 2452 if (DECL_INLINE (function)) 2453 function = inline_conversion (function); 2454 else 2455 function = build_addr_func (function); 2456 } 2457 else 2458 { 2459 fndecl = NULL_TREE; 2460 2461 function = build_addr_func (function); 2462 } 2463 2464 if (function == error_mark_node) 2465 return error_mark_node; 2466 2467 fntype = TREE_TYPE (function); 2468 2469 if (TYPE_PTRMEMFUNC_P (fntype)) 2470 { 2471 error ("must use .* or ->* to call pointer-to-member function in `%E (...)'", 2472 original); 2473 return error_mark_node; 2474 } 2475 2476 is_method = (TREE_CODE (fntype) == POINTER_TYPE 2477 && TREE_CODE (TREE_TYPE (fntype)) == METHOD_TYPE); 2478 2479 if (!((TREE_CODE (fntype) == POINTER_TYPE 2480 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE) 2481 || is_method 2482 || TREE_CODE (function) == TEMPLATE_ID_EXPR)) 2483 { 2484 error ("`%E' cannot be used as a function", original); 2485 return error_mark_node; 2486 } 2487 2488 /* fntype now gets the type of function pointed to. */ 2489 fntype = TREE_TYPE (fntype); 2490 2491 /* Convert the parameters to the types declared in the 2492 function prototype, or apply default promotions. */ 2493 2494 coerced_params = convert_arguments (TYPE_ARG_TYPES (fntype), 2495 params, fndecl, LOOKUP_NORMAL); 2496 if (coerced_params == error_mark_node) 2497 return error_mark_node; 2498 2499 /* Check for errors in format strings. */ 2500 2501 if (warn_format) 2502 check_function_format (NULL, TYPE_ATTRIBUTES (fntype), coerced_params); 2503 2504 /* Recognize certain built-in functions so we can make tree-codes 2505 other than CALL_EXPR. We do this when it enables fold-const.c 2506 to do something useful. */ 2507 2508 if (TREE_CODE (function) == ADDR_EXPR 2509 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL 2510 && DECL_BUILT_IN (TREE_OPERAND (function, 0))) 2511 { 2512 result = expand_tree_builtin (TREE_OPERAND (function, 0), 2513 params, coerced_params); 2514 if (result) 2515 return result; 2516 } 2517 2518 return build_cxx_call (function, params, coerced_params); 2519} 2520 2521/* Convert the actual parameter expressions in the list VALUES 2522 to the types in the list TYPELIST. 2523 If parmdecls is exhausted, or when an element has NULL as its type, 2524 perform the default conversions. 2525 2526 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages. 2527 2528 This is also where warnings about wrong number of args are generated. 2529 2530 Return a list of expressions for the parameters as converted. 2531 2532 Both VALUES and the returned value are chains of TREE_LIST nodes 2533 with the elements of the list in the TREE_VALUE slots of those nodes. 2534 2535 In C++, unspecified trailing parameters can be filled in with their 2536 default arguments, if such were specified. Do so here. */ 2537 2538tree 2539convert_arguments (tree typelist, tree values, tree fndecl, int flags) 2540{ 2541 tree typetail, valtail; 2542 tree result = NULL_TREE; 2543 const char *called_thing = 0; 2544 int i = 0; 2545 2546 /* Argument passing is always copy-initialization. */ 2547 flags |= LOOKUP_ONLYCONVERTING; 2548 2549 if (fndecl) 2550 { 2551 if (TREE_CODE (TREE_TYPE (fndecl)) == METHOD_TYPE) 2552 { 2553 if (DECL_NAME (fndecl) == NULL_TREE 2554 || IDENTIFIER_HAS_TYPE_VALUE (DECL_NAME (fndecl))) 2555 called_thing = "constructor"; 2556 else 2557 called_thing = "member function"; 2558 } 2559 else 2560 called_thing = "function"; 2561 } 2562 2563 for (valtail = values, typetail = typelist; 2564 valtail; 2565 valtail = TREE_CHAIN (valtail), i++) 2566 { 2567 tree type = typetail ? TREE_VALUE (typetail) : 0; 2568 tree val = TREE_VALUE (valtail); 2569 2570 if (val == error_mark_node) 2571 return error_mark_node; 2572 2573 if (type == void_type_node) 2574 { 2575 if (fndecl) 2576 { 2577 cp_error_at ("too many arguments to %s `%+#D'", called_thing, 2578 fndecl); 2579 error ("at this point in file"); 2580 } 2581 else 2582 error ("too many arguments to function"); 2583 /* In case anybody wants to know if this argument 2584 list is valid. */ 2585 if (result) 2586 TREE_TYPE (tree_last (result)) = error_mark_node; 2587 break; 2588 } 2589 2590 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue. 2591 Strip such NOP_EXPRs, since VAL is used in non-lvalue context. */ 2592 if (TREE_CODE (val) == NOP_EXPR 2593 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)) 2594 && (type == 0 || TREE_CODE (type) != REFERENCE_TYPE)) 2595 val = TREE_OPERAND (val, 0); 2596 2597 if (type == 0 || TREE_CODE (type) != REFERENCE_TYPE) 2598 { 2599 if (TREE_CODE (TREE_TYPE (val)) == ARRAY_TYPE 2600 || TREE_CODE (TREE_TYPE (val)) == FUNCTION_TYPE 2601 || TREE_CODE (TREE_TYPE (val)) == METHOD_TYPE) 2602 val = decay_conversion (val); 2603 } 2604 2605 if (val == error_mark_node) 2606 return error_mark_node; 2607 2608 if (type != 0) 2609 { 2610 /* Formal parm type is specified by a function prototype. */ 2611 tree parmval; 2612 2613 if (!COMPLETE_TYPE_P (complete_type (type))) 2614 { 2615 if (fndecl) 2616 error ("parameter %P of `%D' has incomplete type `%T'", 2617 i, fndecl, type); 2618 else 2619 error ("parameter %P has incomplete type `%T'", i, type); 2620 parmval = error_mark_node; 2621 } 2622 else 2623 { 2624 parmval = convert_for_initialization 2625 (NULL_TREE, type, val, flags, 2626 "argument passing", fndecl, i); 2627 parmval = convert_for_arg_passing (type, parmval); 2628 } 2629 2630 if (parmval == error_mark_node) 2631 return error_mark_node; 2632 2633 result = tree_cons (NULL_TREE, parmval, result); 2634 } 2635 else 2636 { 2637 if (TREE_CODE (TREE_TYPE (val)) == REFERENCE_TYPE) 2638 val = convert_from_reference (val); 2639 2640 if (fndecl && DECL_BUILT_IN (fndecl) 2641 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CONSTANT_P) 2642 /* Don't do ellipsis conversion for __built_in_constant_p 2643 as this will result in spurious warnings for non-POD 2644 types. */ 2645 val = require_complete_type (val); 2646 else 2647 val = convert_arg_to_ellipsis (val); 2648 2649 result = tree_cons (NULL_TREE, val, result); 2650 } 2651 2652 if (typetail) 2653 typetail = TREE_CHAIN (typetail); 2654 } 2655 2656 if (typetail != 0 && typetail != void_list_node) 2657 { 2658 /* See if there are default arguments that can be used. */ 2659 if (TREE_PURPOSE (typetail) 2660 && TREE_CODE (TREE_PURPOSE (typetail)) != DEFAULT_ARG) 2661 { 2662 for (; typetail != void_list_node; ++i) 2663 { 2664 tree parmval 2665 = convert_default_arg (TREE_VALUE (typetail), 2666 TREE_PURPOSE (typetail), 2667 fndecl, i); 2668 2669 if (parmval == error_mark_node) 2670 return error_mark_node; 2671 2672 result = tree_cons (0, parmval, result); 2673 typetail = TREE_CHAIN (typetail); 2674 /* ends with `...'. */ 2675 if (typetail == NULL_TREE) 2676 break; 2677 } 2678 } 2679 else 2680 { 2681 if (fndecl) 2682 { 2683 cp_error_at ("too few arguments to %s `%+#D'", 2684 called_thing, fndecl); 2685 error ("at this point in file"); 2686 } 2687 else 2688 error ("too few arguments to function"); 2689 return error_mark_list; 2690 } 2691 } 2692 2693 return nreverse (result); 2694} 2695 2696/* Build a binary-operation expression, after performing default 2697 conversions on the operands. CODE is the kind of expression to build. */ 2698 2699tree 2700build_x_binary_op (enum tree_code code, tree arg1, tree arg2, 2701 bool *overloaded_p) 2702{ 2703 tree orig_arg1; 2704 tree orig_arg2; 2705 tree expr; 2706 2707 orig_arg1 = arg1; 2708 orig_arg2 = arg2; 2709 2710 if (processing_template_decl) 2711 { 2712 if (type_dependent_expression_p (arg1) 2713 || type_dependent_expression_p (arg2)) 2714 return build_min_nt (code, arg1, arg2); 2715 arg1 = build_non_dependent_expr (arg1); 2716 arg2 = build_non_dependent_expr (arg2); 2717 } 2718 2719 if (code == DOTSTAR_EXPR) 2720 expr = build_m_component_ref (arg1, arg2); 2721 else 2722 expr = build_new_op (code, LOOKUP_NORMAL, arg1, arg2, NULL_TREE, 2723 overloaded_p); 2724 2725 if (processing_template_decl && expr != error_mark_node) 2726 return build_min_non_dep (code, expr, orig_arg1, orig_arg2); 2727 2728 return expr; 2729} 2730 2731/* Build a binary-operation expression without default conversions. 2732 CODE is the kind of expression to build. 2733 This function differs from `build' in several ways: 2734 the data type of the result is computed and recorded in it, 2735 warnings are generated if arg data types are invalid, 2736 special handling for addition and subtraction of pointers is known, 2737 and some optimization is done (operations on narrow ints 2738 are done in the narrower type when that gives the same result). 2739 Constant folding is also done before the result is returned. 2740 2741 Note that the operands will never have enumeral types 2742 because either they have just had the default conversions performed 2743 or they have both just been converted to some other type in which 2744 the arithmetic is to be done. 2745 2746 C++: must do special pointer arithmetic when implementing 2747 multiple inheritance, and deal with pointer to member functions. */ 2748 2749tree 2750build_binary_op (enum tree_code code, tree orig_op0, tree orig_op1, 2751 int convert_p ATTRIBUTE_UNUSED) 2752{ 2753 tree op0, op1; 2754 enum tree_code code0, code1; 2755 tree type0, type1; 2756 2757 /* Expression code to give to the expression when it is built. 2758 Normally this is CODE, which is what the caller asked for, 2759 but in some special cases we change it. */ 2760 enum tree_code resultcode = code; 2761 2762 /* Data type in which the computation is to be performed. 2763 In the simplest cases this is the common type of the arguments. */ 2764 tree result_type = NULL; 2765 2766 /* Nonzero means operands have already been type-converted 2767 in whatever way is necessary. 2768 Zero means they need to be converted to RESULT_TYPE. */ 2769 int converted = 0; 2770 2771 /* Nonzero means create the expression with this type, rather than 2772 RESULT_TYPE. */ 2773 tree build_type = 0; 2774 2775 /* Nonzero means after finally constructing the expression 2776 convert it to this type. */ 2777 tree final_type = 0; 2778 2779 /* Nonzero if this is an operation like MIN or MAX which can 2780 safely be computed in short if both args are promoted shorts. 2781 Also implies COMMON. 2782 -1 indicates a bitwise operation; this makes a difference 2783 in the exact conditions for when it is safe to do the operation 2784 in a narrower mode. */ 2785 int shorten = 0; 2786 2787 /* Nonzero if this is a comparison operation; 2788 if both args are promoted shorts, compare the original shorts. 2789 Also implies COMMON. */ 2790 int short_compare = 0; 2791 2792 /* Nonzero if this is a right-shift operation, which can be computed on the 2793 original short and then promoted if the operand is a promoted short. */ 2794 int short_shift = 0; 2795 2796 /* Nonzero means set RESULT_TYPE to the common type of the args. */ 2797 int common = 0; 2798 2799 /* Apply default conversions. */ 2800 op0 = orig_op0; 2801 op1 = orig_op1; 2802 2803 if (code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR 2804 || code == TRUTH_OR_EXPR || code == TRUTH_ORIF_EXPR 2805 || code == TRUTH_XOR_EXPR) 2806 { 2807 if (!really_overloaded_fn (op0)) 2808 op0 = decay_conversion (op0); 2809 if (!really_overloaded_fn (op1)) 2810 op1 = decay_conversion (op1); 2811 } 2812 else 2813 { 2814 if (!really_overloaded_fn (op0)) 2815 op0 = default_conversion (op0); 2816 if (!really_overloaded_fn (op1)) 2817 op1 = default_conversion (op1); 2818 } 2819 2820 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */ 2821 STRIP_TYPE_NOPS (op0); 2822 STRIP_TYPE_NOPS (op1); 2823 2824 /* DTRT if one side is an overloaded function, but complain about it. */ 2825 if (type_unknown_p (op0)) 2826 { 2827 tree t = instantiate_type (TREE_TYPE (op1), op0, tf_none); 2828 if (t != error_mark_node) 2829 { 2830 pedwarn ("assuming cast to type `%T' from overloaded function", 2831 TREE_TYPE (t)); 2832 op0 = t; 2833 } 2834 } 2835 if (type_unknown_p (op1)) 2836 { 2837 tree t = instantiate_type (TREE_TYPE (op0), op1, tf_none); 2838 if (t != error_mark_node) 2839 { 2840 pedwarn ("assuming cast to type `%T' from overloaded function", 2841 TREE_TYPE (t)); 2842 op1 = t; 2843 } 2844 } 2845 2846 type0 = TREE_TYPE (op0); 2847 type1 = TREE_TYPE (op1); 2848 2849 /* The expression codes of the data types of the arguments tell us 2850 whether the arguments are integers, floating, pointers, etc. */ 2851 code0 = TREE_CODE (type0); 2852 code1 = TREE_CODE (type1); 2853 2854 /* If an error was already reported for one of the arguments, 2855 avoid reporting another error. */ 2856 2857 if (code0 == ERROR_MARK || code1 == ERROR_MARK) 2858 return error_mark_node; 2859 2860 switch (code) 2861 { 2862 case PLUS_EXPR: 2863 /* Handle the pointer + int case. */ 2864 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE) 2865 return cp_pointer_int_sum (PLUS_EXPR, op0, op1); 2866 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE) 2867 return cp_pointer_int_sum (PLUS_EXPR, op1, op0); 2868 else 2869 common = 1; 2870 break; 2871 2872 case MINUS_EXPR: 2873 /* Subtraction of two similar pointers. 2874 We must subtract them as integers, then divide by object size. */ 2875 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE 2876 && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0), 2877 TREE_TYPE (type1))) 2878 return pointer_diff (op0, op1, common_type (type0, type1)); 2879 /* Handle pointer minus int. Just like pointer plus int. */ 2880 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE) 2881 return cp_pointer_int_sum (MINUS_EXPR, op0, op1); 2882 else 2883 common = 1; 2884 break; 2885 2886 case MULT_EXPR: 2887 common = 1; 2888 break; 2889 2890 case TRUNC_DIV_EXPR: 2891 case CEIL_DIV_EXPR: 2892 case FLOOR_DIV_EXPR: 2893 case ROUND_DIV_EXPR: 2894 case EXACT_DIV_EXPR: 2895 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE 2896 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE) 2897 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE 2898 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)) 2899 { 2900 if (TREE_CODE (op1) == INTEGER_CST && integer_zerop (op1)) 2901 warning ("division by zero in `%E / 0'", op0); 2902 else if (TREE_CODE (op1) == REAL_CST && real_zerop (op1)) 2903 warning ("division by zero in `%E / 0.'", op0); 2904 2905 if (!(code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)) 2906 resultcode = RDIV_EXPR; 2907 else 2908 /* When dividing two signed integers, we have to promote to int. 2909 unless we divide by a constant != -1. Note that default 2910 conversion will have been performed on the operands at this 2911 point, so we have to dig out the original type to find out if 2912 it was unsigned. */ 2913 shorten = ((TREE_CODE (op0) == NOP_EXPR 2914 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0)))) 2915 || (TREE_CODE (op1) == INTEGER_CST 2916 && ! integer_all_onesp (op1))); 2917 2918 common = 1; 2919 } 2920 break; 2921 2922 case BIT_AND_EXPR: 2923 case BIT_IOR_EXPR: 2924 case BIT_XOR_EXPR: 2925 if ((code0 == INTEGER_TYPE && code1 == INTEGER_TYPE) 2926 || (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)) 2927 shorten = -1; 2928 break; 2929 2930 case TRUNC_MOD_EXPR: 2931 case FLOOR_MOD_EXPR: 2932 if (code1 == INTEGER_TYPE && integer_zerop (op1)) 2933 warning ("division by zero in `%E %% 0'", op0); 2934 else if (code1 == REAL_TYPE && real_zerop (op1)) 2935 warning ("division by zero in `%E %% 0.'", op0); 2936 2937 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE) 2938 { 2939 /* Although it would be tempting to shorten always here, that loses 2940 on some targets, since the modulo instruction is undefined if the 2941 quotient can't be represented in the computation mode. We shorten 2942 only if unsigned or if dividing by something we know != -1. */ 2943 shorten = ((TREE_CODE (op0) == NOP_EXPR 2944 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0)))) 2945 || (TREE_CODE (op1) == INTEGER_CST 2946 && ! integer_all_onesp (op1))); 2947 common = 1; 2948 } 2949 break; 2950 2951 case TRUTH_ANDIF_EXPR: 2952 case TRUTH_ORIF_EXPR: 2953 case TRUTH_AND_EXPR: 2954 case TRUTH_OR_EXPR: 2955 result_type = boolean_type_node; 2956 break; 2957 2958 /* Shift operations: result has same type as first operand; 2959 always convert second operand to int. 2960 Also set SHORT_SHIFT if shifting rightward. */ 2961 2962 case RSHIFT_EXPR: 2963 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE) 2964 { 2965 result_type = type0; 2966 if (TREE_CODE (op1) == INTEGER_CST) 2967 { 2968 if (tree_int_cst_lt (op1, integer_zero_node)) 2969 warning ("right shift count is negative"); 2970 else 2971 { 2972 if (! integer_zerop (op1)) 2973 short_shift = 1; 2974 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0) 2975 warning ("right shift count >= width of type"); 2976 } 2977 } 2978 /* Convert the shift-count to an integer, regardless of 2979 size of value being shifted. */ 2980 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node) 2981 op1 = cp_convert (integer_type_node, op1); 2982 /* Avoid converting op1 to result_type later. */ 2983 converted = 1; 2984 } 2985 break; 2986 2987 case LSHIFT_EXPR: 2988 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE) 2989 { 2990 result_type = type0; 2991 if (TREE_CODE (op1) == INTEGER_CST) 2992 { 2993 if (tree_int_cst_lt (op1, integer_zero_node)) 2994 warning ("left shift count is negative"); 2995 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0) 2996 warning ("left shift count >= width of type"); 2997 } 2998 /* Convert the shift-count to an integer, regardless of 2999 size of value being shifted. */ 3000 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node) 3001 op1 = cp_convert (integer_type_node, op1); 3002 /* Avoid converting op1 to result_type later. */ 3003 converted = 1; 3004 } 3005 break; 3006 3007 case RROTATE_EXPR: 3008 case LROTATE_EXPR: 3009 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE) 3010 { 3011 result_type = type0; 3012 if (TREE_CODE (op1) == INTEGER_CST) 3013 { 3014 if (tree_int_cst_lt (op1, integer_zero_node)) 3015 warning ("%s rotate count is negative", 3016 (code == LROTATE_EXPR) ? "left" : "right"); 3017 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0) 3018 warning ("%s rotate count >= width of type", 3019 (code == LROTATE_EXPR) ? "left" : "right"); 3020 } 3021 /* Convert the shift-count to an integer, regardless of 3022 size of value being shifted. */ 3023 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node) 3024 op1 = cp_convert (integer_type_node, op1); 3025 } 3026 break; 3027 3028 case EQ_EXPR: 3029 case NE_EXPR: 3030 if (warn_float_equal && (code0 == REAL_TYPE || code1 == REAL_TYPE)) 3031 warning ("comparing floating point with == or != is unsafe"); 3032 3033 build_type = boolean_type_node; 3034 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE 3035 || code0 == COMPLEX_TYPE) 3036 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE 3037 || code1 == COMPLEX_TYPE)) 3038 short_compare = 1; 3039 else if ((code0 == POINTER_TYPE && code1 == POINTER_TYPE) 3040 || (TYPE_PTRMEM_P (type0) && TYPE_PTRMEM_P (type1))) 3041 result_type = composite_pointer_type (type0, type1, op0, op1, 3042 "comparison"); 3043 else if ((code0 == POINTER_TYPE || TYPE_PTRMEM_P (type0)) 3044 && null_ptr_cst_p (op1)) 3045 result_type = type0; 3046 else if ((code1 == POINTER_TYPE || TYPE_PTRMEM_P (type1)) 3047 && null_ptr_cst_p (op0)) 3048 result_type = type1; 3049 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE) 3050 { 3051 result_type = type0; 3052 error ("ISO C++ forbids comparison between pointer and integer"); 3053 } 3054 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE) 3055 { 3056 result_type = type1; 3057 error ("ISO C++ forbids comparison between pointer and integer"); 3058 } 3059 else if (TYPE_PTRMEMFUNC_P (type0) && null_ptr_cst_p (op1)) 3060 { 3061 op0 = build_ptrmemfunc_access_expr (op0, pfn_identifier); 3062 op1 = cp_convert (TREE_TYPE (op0), integer_zero_node); 3063 result_type = TREE_TYPE (op0); 3064 } 3065 else if (TYPE_PTRMEMFUNC_P (type1) && null_ptr_cst_p (op0)) 3066 return cp_build_binary_op (code, op1, op0); 3067 else if (TYPE_PTRMEMFUNC_P (type0) && TYPE_PTRMEMFUNC_P (type1) 3068 && same_type_p (type0, type1)) 3069 { 3070 /* E will be the final comparison. */ 3071 tree e; 3072 /* E1 and E2 are for scratch. */ 3073 tree e1; 3074 tree e2; 3075 tree pfn0; 3076 tree pfn1; 3077 tree delta0; 3078 tree delta1; 3079 3080 if (TREE_SIDE_EFFECTS (op0)) 3081 op0 = save_expr (op0); 3082 if (TREE_SIDE_EFFECTS (op1)) 3083 op1 = save_expr (op1); 3084 3085 /* We generate: 3086 3087 (op0.pfn == op1.pfn 3088 && (!op0.pfn || op0.delta == op1.delta)) 3089 3090 The reason for the `!op0.pfn' bit is that a NULL 3091 pointer-to-member is any member with a zero PFN; the 3092 DELTA field is unspecified. */ 3093 pfn0 = pfn_from_ptrmemfunc (op0); 3094 pfn1 = pfn_from_ptrmemfunc (op1); 3095 delta0 = build_ptrmemfunc_access_expr (op0, 3096 delta_identifier); 3097 delta1 = build_ptrmemfunc_access_expr (op1, 3098 delta_identifier); 3099 e1 = cp_build_binary_op (EQ_EXPR, delta0, delta1); 3100 e2 = cp_build_binary_op (EQ_EXPR, 3101 pfn0, 3102 cp_convert (TREE_TYPE (pfn0), 3103 integer_zero_node)); 3104 e1 = cp_build_binary_op (TRUTH_ORIF_EXPR, e1, e2); 3105 e2 = build (EQ_EXPR, boolean_type_node, pfn0, pfn1); 3106 e = cp_build_binary_op (TRUTH_ANDIF_EXPR, e2, e1); 3107 if (code == EQ_EXPR) 3108 return e; 3109 return cp_build_binary_op (EQ_EXPR, e, integer_zero_node); 3110 } 3111 else if ((TYPE_PTRMEMFUNC_P (type0) 3112 && same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type0), type1)) 3113 || (TYPE_PTRMEMFUNC_P (type1) 3114 && same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type1), type0))) 3115 abort (); 3116 break; 3117 3118 case MAX_EXPR: 3119 case MIN_EXPR: 3120 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE) 3121 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE)) 3122 shorten = 1; 3123 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE) 3124 result_type = composite_pointer_type (type0, type1, op0, op1, 3125 "comparison"); 3126 break; 3127 3128 case LE_EXPR: 3129 case GE_EXPR: 3130 case LT_EXPR: 3131 case GT_EXPR: 3132 build_type = boolean_type_node; 3133 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE) 3134 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE)) 3135 short_compare = 1; 3136 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE) 3137 result_type = composite_pointer_type (type0, type1, op0, op1, 3138 "comparison"); 3139 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST 3140 && integer_zerop (op1)) 3141 result_type = type0; 3142 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST 3143 && integer_zerop (op0)) 3144 result_type = type1; 3145 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE) 3146 { 3147 result_type = type0; 3148 pedwarn ("ISO C++ forbids comparison between pointer and integer"); 3149 } 3150 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE) 3151 { 3152 result_type = type1; 3153 pedwarn ("ISO C++ forbids comparison between pointer and integer"); 3154 } 3155 break; 3156 3157 case UNORDERED_EXPR: 3158 case ORDERED_EXPR: 3159 case UNLT_EXPR: 3160 case UNLE_EXPR: 3161 case UNGT_EXPR: 3162 case UNGE_EXPR: 3163 case UNEQ_EXPR: 3164 build_type = integer_type_node; 3165 if (code0 != REAL_TYPE || code1 != REAL_TYPE) 3166 { 3167 error ("unordered comparison on non-floating point argument"); 3168 return error_mark_node; 3169 } 3170 common = 1; 3171 break; 3172 3173 default: 3174 break; 3175 } 3176 3177 if (((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE) 3178 && 3179 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE)) 3180 || (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)) 3181 { 3182 int none_complex = (code0 != COMPLEX_TYPE && code1 != COMPLEX_TYPE); 3183 3184 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE 3185 && !tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))) 3186 error ("can't convert between vector values of different size"); 3187 3188 if (shorten || common || short_compare) 3189 result_type = common_type (type0, type1); 3190 3191 /* For certain operations (which identify themselves by shorten != 0) 3192 if both args were extended from the same smaller type, 3193 do the arithmetic in that type and then extend. 3194 3195 shorten !=0 and !=1 indicates a bitwise operation. 3196 For them, this optimization is safe only if 3197 both args are zero-extended or both are sign-extended. 3198 Otherwise, we might change the result. 3199 Eg, (short)-1 | (unsigned short)-1 is (int)-1 3200 but calculated in (unsigned short) it would be (unsigned short)-1. */ 3201 3202 if (shorten && none_complex) 3203 { 3204 int unsigned0, unsigned1; 3205 tree arg0 = get_narrower (op0, &unsigned0); 3206 tree arg1 = get_narrower (op1, &unsigned1); 3207 /* UNS is 1 if the operation to be done is an unsigned one. */ 3208 int uns = TREE_UNSIGNED (result_type); 3209 tree type; 3210 3211 final_type = result_type; 3212 3213 /* Handle the case that OP0 does not *contain* a conversion 3214 but it *requires* conversion to FINAL_TYPE. */ 3215 3216 if (op0 == arg0 && TREE_TYPE (op0) != final_type) 3217 unsigned0 = TREE_UNSIGNED (TREE_TYPE (op0)); 3218 if (op1 == arg1 && TREE_TYPE (op1) != final_type) 3219 unsigned1 = TREE_UNSIGNED (TREE_TYPE (op1)); 3220 3221 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */ 3222 3223 /* For bitwise operations, signedness of nominal type 3224 does not matter. Consider only how operands were extended. */ 3225 if (shorten == -1) 3226 uns = unsigned0; 3227 3228 /* Note that in all three cases below we refrain from optimizing 3229 an unsigned operation on sign-extended args. 3230 That would not be valid. */ 3231 3232 /* Both args variable: if both extended in same way 3233 from same width, do it in that width. 3234 Do it unsigned if args were zero-extended. */ 3235 if ((TYPE_PRECISION (TREE_TYPE (arg0)) 3236 < TYPE_PRECISION (result_type)) 3237 && (TYPE_PRECISION (TREE_TYPE (arg1)) 3238 == TYPE_PRECISION (TREE_TYPE (arg0))) 3239 && unsigned0 == unsigned1 3240 && (unsigned0 || !uns)) 3241 result_type = c_common_signed_or_unsigned_type 3242 (unsigned0, common_type (TREE_TYPE (arg0), TREE_TYPE (arg1))); 3243 else if (TREE_CODE (arg0) == INTEGER_CST 3244 && (unsigned1 || !uns) 3245 && (TYPE_PRECISION (TREE_TYPE (arg1)) 3246 < TYPE_PRECISION (result_type)) 3247 && (type = c_common_signed_or_unsigned_type 3248 (unsigned1, TREE_TYPE (arg1)), 3249 int_fits_type_p (arg0, type))) 3250 result_type = type; 3251 else if (TREE_CODE (arg1) == INTEGER_CST 3252 && (unsigned0 || !uns) 3253 && (TYPE_PRECISION (TREE_TYPE (arg0)) 3254 < TYPE_PRECISION (result_type)) 3255 && (type = c_common_signed_or_unsigned_type 3256 (unsigned0, TREE_TYPE (arg0)), 3257 int_fits_type_p (arg1, type))) 3258 result_type = type; 3259 } 3260 3261 /* Shifts can be shortened if shifting right. */ 3262 3263 if (short_shift) 3264 { 3265 int unsigned_arg; 3266 tree arg0 = get_narrower (op0, &unsigned_arg); 3267 3268 final_type = result_type; 3269 3270 if (arg0 == op0 && final_type == TREE_TYPE (op0)) 3271 unsigned_arg = TREE_UNSIGNED (TREE_TYPE (op0)); 3272 3273 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type) 3274 /* We can shorten only if the shift count is less than the 3275 number of bits in the smaller type size. */ 3276 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0 3277 /* If arg is sign-extended and then unsigned-shifted, 3278 we can simulate this with a signed shift in arg's type 3279 only if the extended result is at least twice as wide 3280 as the arg. Otherwise, the shift could use up all the 3281 ones made by sign-extension and bring in zeros. 3282 We can't optimize that case at all, but in most machines 3283 it never happens because available widths are 2**N. */ 3284 && (!TREE_UNSIGNED (final_type) 3285 || unsigned_arg 3286 || (((unsigned) 2 * TYPE_PRECISION (TREE_TYPE (arg0))) 3287 <= TYPE_PRECISION (result_type)))) 3288 { 3289 /* Do an unsigned shift if the operand was zero-extended. */ 3290 result_type 3291 = c_common_signed_or_unsigned_type (unsigned_arg, 3292 TREE_TYPE (arg0)); 3293 /* Convert value-to-be-shifted to that type. */ 3294 if (TREE_TYPE (op0) != result_type) 3295 op0 = cp_convert (result_type, op0); 3296 converted = 1; 3297 } 3298 } 3299 3300 /* Comparison operations are shortened too but differently. 3301 They identify themselves by setting short_compare = 1. */ 3302 3303 if (short_compare) 3304 { 3305 /* Don't write &op0, etc., because that would prevent op0 3306 from being kept in a register. 3307 Instead, make copies of the our local variables and 3308 pass the copies by reference, then copy them back afterward. */ 3309 tree xop0 = op0, xop1 = op1, xresult_type = result_type; 3310 enum tree_code xresultcode = resultcode; 3311 tree val 3312 = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode); 3313 if (val != 0) 3314 return cp_convert (boolean_type_node, val); 3315 op0 = xop0, op1 = xop1; 3316 converted = 1; 3317 resultcode = xresultcode; 3318 } 3319 3320 if ((short_compare || code == MIN_EXPR || code == MAX_EXPR) 3321 && warn_sign_compare 3322 /* Do not warn until the template is instantiated; we cannot 3323 bound the ranges of the arguments until that point. */ 3324 && !processing_template_decl) 3325 { 3326 int op0_signed = ! TREE_UNSIGNED (TREE_TYPE (orig_op0)); 3327 int op1_signed = ! TREE_UNSIGNED (TREE_TYPE (orig_op1)); 3328 3329 int unsignedp0, unsignedp1; 3330 tree primop0 = get_narrower (op0, &unsignedp0); 3331 tree primop1 = get_narrower (op1, &unsignedp1); 3332 3333 /* Check for comparison of different enum types. */ 3334 if (TREE_CODE (TREE_TYPE (orig_op0)) == ENUMERAL_TYPE 3335 && TREE_CODE (TREE_TYPE (orig_op1)) == ENUMERAL_TYPE 3336 && TYPE_MAIN_VARIANT (TREE_TYPE (orig_op0)) 3337 != TYPE_MAIN_VARIANT (TREE_TYPE (orig_op1))) 3338 { 3339 warning ("comparison between types `%#T' and `%#T'", 3340 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1)); 3341 } 3342 3343 /* Give warnings for comparisons between signed and unsigned 3344 quantities that may fail. */ 3345 /* Do the checking based on the original operand trees, so that 3346 casts will be considered, but default promotions won't be. */ 3347 3348 /* Do not warn if the comparison is being done in a signed type, 3349 since the signed type will only be chosen if it can represent 3350 all the values of the unsigned type. */ 3351 if (! TREE_UNSIGNED (result_type)) 3352 /* OK */; 3353 /* Do not warn if both operands are unsigned. */ 3354 else if (op0_signed == op1_signed) 3355 /* OK */; 3356 /* Do not warn if the signed quantity is an unsuffixed 3357 integer literal (or some static constant expression 3358 involving such literals or a conditional expression 3359 involving such literals) and it is non-negative. */ 3360 else if ((op0_signed && tree_expr_nonnegative_p (orig_op0)) 3361 || (op1_signed && tree_expr_nonnegative_p (orig_op1))) 3362 /* OK */; 3363 /* Do not warn if the comparison is an equality operation, 3364 the unsigned quantity is an integral constant and it does 3365 not use the most significant bit of result_type. */ 3366 else if ((resultcode == EQ_EXPR || resultcode == NE_EXPR) 3367 && ((op0_signed && TREE_CODE (orig_op1) == INTEGER_CST 3368 && int_fits_type_p (orig_op1, c_common_signed_type 3369 (result_type))) 3370 || (op1_signed && TREE_CODE (orig_op0) == INTEGER_CST 3371 && int_fits_type_p (orig_op0, c_common_signed_type 3372 (result_type))))) 3373 /* OK */; 3374 else 3375 warning ("comparison between signed and unsigned integer expressions"); 3376 3377 /* Warn if two unsigned values are being compared in a size 3378 larger than their original size, and one (and only one) is the 3379 result of a `~' operator. This comparison will always fail. 3380 3381 Also warn if one operand is a constant, and the constant does not 3382 have all bits set that are set in the ~ operand when it is 3383 extended. */ 3384 3385 if ((TREE_CODE (primop0) == BIT_NOT_EXPR) 3386 ^ (TREE_CODE (primop1) == BIT_NOT_EXPR)) 3387 { 3388 if (TREE_CODE (primop0) == BIT_NOT_EXPR) 3389 primop0 = get_narrower (TREE_OPERAND (op0, 0), &unsignedp0); 3390 if (TREE_CODE (primop1) == BIT_NOT_EXPR) 3391 primop1 = get_narrower (TREE_OPERAND (op1, 0), &unsignedp1); 3392 3393 if (host_integerp (primop0, 0) || host_integerp (primop1, 0)) 3394 { 3395 tree primop; 3396 HOST_WIDE_INT constant, mask; 3397 int unsignedp; 3398 unsigned int bits; 3399 3400 if (host_integerp (primop0, 0)) 3401 { 3402 primop = primop1; 3403 unsignedp = unsignedp1; 3404 constant = tree_low_cst (primop0, 0); 3405 } 3406 else 3407 { 3408 primop = primop0; 3409 unsignedp = unsignedp0; 3410 constant = tree_low_cst (primop1, 0); 3411 } 3412 3413 bits = TYPE_PRECISION (TREE_TYPE (primop)); 3414 if (bits < TYPE_PRECISION (result_type) 3415 && bits < HOST_BITS_PER_LONG && unsignedp) 3416 { 3417 mask = (~ (HOST_WIDE_INT) 0) << bits; 3418 if ((mask & constant) != mask) 3419 warning ("comparison of promoted ~unsigned with constant"); 3420 } 3421 } 3422 else if (unsignedp0 && unsignedp1 3423 && (TYPE_PRECISION (TREE_TYPE (primop0)) 3424 < TYPE_PRECISION (result_type)) 3425 && (TYPE_PRECISION (TREE_TYPE (primop1)) 3426 < TYPE_PRECISION (result_type))) 3427 warning ("comparison of promoted ~unsigned with unsigned"); 3428 } 3429 } 3430 } 3431 3432 /* At this point, RESULT_TYPE must be nonzero to avoid an error message. 3433 If CONVERTED is zero, both args will be converted to type RESULT_TYPE. 3434 Then the expression will be built. 3435 It will be given type FINAL_TYPE if that is nonzero; 3436 otherwise, it will be given type RESULT_TYPE. */ 3437 3438 if (!result_type) 3439 { 3440 error ("invalid operands of types `%T' and `%T' to binary `%O'", 3441 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1), code); 3442 return error_mark_node; 3443 } 3444 3445 /* Issue warnings about peculiar, but valid, uses of NULL. */ 3446 if (/* It's reasonable to use pointer values as operands of && 3447 and ||, so NULL is no exception. */ 3448 !(code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR) 3449 && (/* If OP0 is NULL and OP1 is not a pointer, or vice versa. */ 3450 (orig_op0 == null_node 3451 && TREE_CODE (TREE_TYPE (op1)) != POINTER_TYPE) 3452 /* Or vice versa. */ 3453 || (orig_op1 == null_node 3454 && TREE_CODE (TREE_TYPE (op0)) != POINTER_TYPE) 3455 /* Or, both are NULL and the operation was not a comparison. */ 3456 || (orig_op0 == null_node && orig_op1 == null_node 3457 && code != EQ_EXPR && code != NE_EXPR))) 3458 /* Some sort of arithmetic operation involving NULL was 3459 performed. Note that pointer-difference and pointer-addition 3460 have already been handled above, and so we don't end up here in 3461 that case. */ 3462 warning ("NULL used in arithmetic"); 3463 3464 if (! converted) 3465 { 3466 if (TREE_TYPE (op0) != result_type) 3467 op0 = cp_convert (result_type, op0); 3468 if (TREE_TYPE (op1) != result_type) 3469 op1 = cp_convert (result_type, op1); 3470 3471 if (op0 == error_mark_node || op1 == error_mark_node) 3472 return error_mark_node; 3473 } 3474 3475 if (build_type == NULL_TREE) 3476 build_type = result_type; 3477 3478 { 3479 tree result = build (resultcode, build_type, op0, op1); 3480 tree folded; 3481 3482 folded = fold_if_not_in_template (result); 3483 if (folded == result) 3484 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1); 3485 if (final_type != 0) 3486 return cp_convert (final_type, folded); 3487 return folded; 3488 } 3489} 3490 3491/* Return a tree for the sum or difference (RESULTCODE says which) 3492 of pointer PTROP and integer INTOP. */ 3493 3494static tree 3495cp_pointer_int_sum (enum tree_code resultcode, tree ptrop, tree intop) 3496{ 3497 tree res_type = TREE_TYPE (ptrop); 3498 3499 /* pointer_int_sum() uses size_in_bytes() on the TREE_TYPE(res_type) 3500 in certain circumstance (when it's valid to do so). So we need 3501 to make sure it's complete. We don't need to check here, if we 3502 can actually complete it at all, as those checks will be done in 3503 pointer_int_sum() anyway. */ 3504 complete_type (TREE_TYPE (res_type)); 3505 3506 return pointer_int_sum (resultcode, ptrop, fold (intop)); 3507} 3508 3509/* Return a tree for the difference of pointers OP0 and OP1. 3510 The resulting tree has type int. */ 3511 3512static tree 3513pointer_diff (tree op0, tree op1, tree ptrtype) 3514{ 3515 tree result, folded; 3516 tree restype = ptrdiff_type_node; 3517 tree target_type = TREE_TYPE (ptrtype); 3518 3519 if (!complete_type_or_else (target_type, NULL_TREE)) 3520 return error_mark_node; 3521 3522 if (pedantic || warn_pointer_arith) 3523 { 3524 if (TREE_CODE (target_type) == VOID_TYPE) 3525 pedwarn ("ISO C++ forbids using pointer of type `void *' in subtraction"); 3526 if (TREE_CODE (target_type) == FUNCTION_TYPE) 3527 pedwarn ("ISO C++ forbids using pointer to a function in subtraction"); 3528 if (TREE_CODE (target_type) == METHOD_TYPE) 3529 pedwarn ("ISO C++ forbids using pointer to a method in subtraction"); 3530 } 3531 3532 /* First do the subtraction as integers; 3533 then drop through to build the divide operator. */ 3534 3535 op0 = cp_build_binary_op (MINUS_EXPR, 3536 cp_convert (restype, op0), 3537 cp_convert (restype, op1)); 3538 3539 /* This generates an error if op1 is a pointer to an incomplete type. */ 3540 if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (op1)))) 3541 error ("invalid use of a pointer to an incomplete type in pointer arithmetic"); 3542 3543 op1 = (TYPE_PTROB_P (ptrtype) 3544 ? size_in_bytes (target_type) 3545 : integer_one_node); 3546 3547 /* Do the division. */ 3548 3549 result = build (EXACT_DIV_EXPR, restype, op0, cp_convert (restype, op1)); 3550 3551 folded = fold (result); 3552 if (folded == result) 3553 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1); 3554 return folded; 3555} 3556 3557/* Construct and perhaps optimize a tree representation 3558 for a unary operation. CODE, a tree_code, specifies the operation 3559 and XARG is the operand. */ 3560 3561tree 3562build_x_unary_op (enum tree_code code, tree xarg) 3563{ 3564 tree orig_expr = xarg; 3565 tree exp; 3566 int ptrmem = 0; 3567 3568 if (processing_template_decl) 3569 { 3570 if (type_dependent_expression_p (xarg)) 3571 return build_min_nt (code, xarg, NULL_TREE); 3572 3573 xarg = build_non_dependent_expr (xarg); 3574 } 3575 3576 exp = NULL_TREE; 3577 3578 /* [expr.unary.op] says: 3579 3580 The address of an object of incomplete type can be taken. 3581 3582 (And is just the ordinary address operator, not an overloaded 3583 "operator &".) However, if the type is a template 3584 specialization, we must complete the type at this point so that 3585 an overloaded "operator &" will be available if required. */ 3586 if (code == ADDR_EXPR 3587 && TREE_CODE (xarg) != TEMPLATE_ID_EXPR 3588 && ((CLASS_TYPE_P (TREE_TYPE (xarg)) 3589 && !COMPLETE_TYPE_P (complete_type (TREE_TYPE (xarg)))) 3590 || (TREE_CODE (xarg) == OFFSET_REF))) 3591 /* Don't look for a function. */; 3592 else 3593 exp = build_new_op (code, LOOKUP_NORMAL, xarg, NULL_TREE, NULL_TREE, 3594 /*overloaded_p=*/NULL); 3595 if (!exp && code == ADDR_EXPR) 3596 { 3597 /* A pointer to member-function can be formed only by saying 3598 &X::mf. */ 3599 if (!flag_ms_extensions && TREE_CODE (TREE_TYPE (xarg)) == METHOD_TYPE 3600 && (TREE_CODE (xarg) != OFFSET_REF || !PTRMEM_OK_P (xarg))) 3601 { 3602 if (TREE_CODE (xarg) != OFFSET_REF) 3603 { 3604 error ("invalid use of '%E' to form a pointer-to-member-function. Use a qualified-id.", 3605 xarg); 3606 return error_mark_node; 3607 } 3608 else 3609 { 3610 error ("parenthesis around '%E' cannot be used to form a pointer-to-member-function", 3611 xarg); 3612 PTRMEM_OK_P (xarg) = 1; 3613 } 3614 } 3615 3616 if (TREE_CODE (xarg) == OFFSET_REF) 3617 { 3618 ptrmem = PTRMEM_OK_P (xarg); 3619 3620 if (!ptrmem && !flag_ms_extensions 3621 && TREE_CODE (TREE_TYPE (TREE_OPERAND (xarg, 1))) == METHOD_TYPE) 3622 { 3623 /* A single non-static member, make sure we don't allow a 3624 pointer-to-member. */ 3625 xarg = build (OFFSET_REF, TREE_TYPE (xarg), 3626 TREE_OPERAND (xarg, 0), 3627 ovl_cons (TREE_OPERAND (xarg, 1), NULL_TREE)); 3628 PTRMEM_OK_P (xarg) = ptrmem; 3629 } 3630 } 3631 else if (TREE_CODE (xarg) == TARGET_EXPR) 3632 warning ("taking address of temporary"); 3633 exp = build_unary_op (ADDR_EXPR, xarg, 0); 3634 } 3635 3636 if (processing_template_decl && exp != error_mark_node) 3637 exp = build_min_non_dep (code, exp, orig_expr, 3638 /*For {PRE,POST}{INC,DEC}REMENT_EXPR*/NULL_TREE); 3639 if (TREE_CODE (exp) == ADDR_EXPR) 3640 PTRMEM_OK_P (exp) = ptrmem; 3641 return exp; 3642} 3643 3644/* Like c_common_truthvalue_conversion, but handle pointer-to-member 3645 constants, where a null value is represented by an INTEGER_CST of 3646 -1. */ 3647 3648tree 3649cp_truthvalue_conversion (tree expr) 3650{ 3651 tree type = TREE_TYPE (expr); 3652 if (TYPE_PTRMEM_P (type)) 3653 return build_binary_op (NE_EXPR, expr, integer_zero_node, 1); 3654 else 3655 return c_common_truthvalue_conversion (expr); 3656} 3657 3658/* Just like cp_truthvalue_conversion, but we want a CLEANUP_POINT_EXPR. */ 3659 3660tree 3661condition_conversion (tree expr) 3662{ 3663 tree t; 3664 if (processing_template_decl) 3665 return expr; 3666 t = perform_implicit_conversion (boolean_type_node, expr); 3667 t = fold (build1 (CLEANUP_POINT_EXPR, boolean_type_node, t)); 3668 return t; 3669} 3670 3671/* Return an ADDR_EXPR giving the address of T. This function 3672 attempts no optimizations or simplifications; it is a low-level 3673 primitive. */ 3674 3675tree 3676build_address (tree t) 3677{ 3678 tree addr; 3679 3680 if (error_operand_p (t) || !cxx_mark_addressable (t)) 3681 return error_mark_node; 3682 3683 addr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (t)), t); 3684 if (staticp (t)) 3685 TREE_CONSTANT (addr) = 1; 3686 3687 return addr; 3688} 3689 3690/* Return a NOP_EXPR converting EXPR to TYPE. */ 3691 3692tree 3693build_nop (tree type, tree expr) 3694{ 3695 tree nop; 3696 3697 if (type == error_mark_node || error_operand_p (expr)) 3698 return expr; 3699 3700 nop = build1 (NOP_EXPR, type, expr); 3701 if (TREE_CONSTANT (expr)) 3702 TREE_CONSTANT (nop) = 1; 3703 3704 return nop; 3705} 3706 3707/* C++: Must handle pointers to members. 3708 3709 Perhaps type instantiation should be extended to handle conversion 3710 from aggregates to types we don't yet know we want? (Or are those 3711 cases typically errors which should be reported?) 3712 3713 NOCONVERT nonzero suppresses the default promotions 3714 (such as from short to int). */ 3715 3716tree 3717build_unary_op (enum tree_code code, tree xarg, int noconvert) 3718{ 3719 /* No default_conversion here. It causes trouble for ADDR_EXPR. */ 3720 tree arg = xarg; 3721 tree argtype = 0; 3722 const char *errstring = NULL; 3723 tree val; 3724 3725 if (arg == error_mark_node) 3726 return error_mark_node; 3727 3728 switch (code) 3729 { 3730 case CONVERT_EXPR: 3731 /* This is used for unary plus, because a CONVERT_EXPR 3732 is enough to prevent anybody from looking inside for 3733 associativity, but won't generate any code. */ 3734 if (!(arg = build_expr_type_conversion 3735 (WANT_ARITH | WANT_ENUM | WANT_POINTER, arg, true))) 3736 errstring = "wrong type argument to unary plus"; 3737 else 3738 { 3739 if (!noconvert) 3740 arg = default_conversion (arg); 3741 arg = build1 (NON_LVALUE_EXPR, TREE_TYPE (arg), arg); 3742 TREE_CONSTANT (arg) = TREE_CONSTANT (TREE_OPERAND (arg, 0)); 3743 } 3744 break; 3745 3746 case NEGATE_EXPR: 3747 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true))) 3748 errstring = "wrong type argument to unary minus"; 3749 else if (!noconvert && CP_INTEGRAL_TYPE_P (TREE_TYPE (arg))) 3750 arg = perform_integral_promotions (arg); 3751 break; 3752 3753 case BIT_NOT_EXPR: 3754 if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE) 3755 { 3756 code = CONJ_EXPR; 3757 if (!noconvert) 3758 arg = default_conversion (arg); 3759 } 3760 else if (!(arg = build_expr_type_conversion (WANT_INT | WANT_ENUM, 3761 arg, true))) 3762 errstring = "wrong type argument to bit-complement"; 3763 else if (!noconvert) 3764 arg = perform_integral_promotions (arg); 3765 break; 3766 3767 case ABS_EXPR: 3768 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true))) 3769 errstring = "wrong type argument to abs"; 3770 else if (!noconvert) 3771 arg = default_conversion (arg); 3772 break; 3773 3774 case CONJ_EXPR: 3775 /* Conjugating a real value is a no-op, but allow it anyway. */ 3776 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true))) 3777 errstring = "wrong type argument to conjugation"; 3778 else if (!noconvert) 3779 arg = default_conversion (arg); 3780 break; 3781 3782 case TRUTH_NOT_EXPR: 3783 arg = perform_implicit_conversion (boolean_type_node, arg); 3784 val = invert_truthvalue (arg); 3785 if (arg != error_mark_node) 3786 return val; 3787 errstring = "in argument to unary !"; 3788 break; 3789 3790 case NOP_EXPR: 3791 break; 3792 3793 case REALPART_EXPR: 3794 if (TREE_CODE (arg) == COMPLEX_CST) 3795 return TREE_REALPART (arg); 3796 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE) 3797 return fold (build1 (REALPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg)); 3798 else 3799 return arg; 3800 3801 case IMAGPART_EXPR: 3802 if (TREE_CODE (arg) == COMPLEX_CST) 3803 return TREE_IMAGPART (arg); 3804 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE) 3805 return fold (build1 (IMAGPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg)); 3806 else 3807 return cp_convert (TREE_TYPE (arg), integer_zero_node); 3808 3809 case PREINCREMENT_EXPR: 3810 case POSTINCREMENT_EXPR: 3811 case PREDECREMENT_EXPR: 3812 case POSTDECREMENT_EXPR: 3813 /* Handle complex lvalues (when permitted) 3814 by reduction to simpler cases. */ 3815 3816 val = unary_complex_lvalue (code, arg); 3817 if (val != 0) 3818 return val; 3819 3820 /* Increment or decrement the real part of the value, 3821 and don't change the imaginary part. */ 3822 if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE) 3823 { 3824 tree real, imag; 3825 3826 arg = stabilize_reference (arg); 3827 real = build_unary_op (REALPART_EXPR, arg, 1); 3828 imag = build_unary_op (IMAGPART_EXPR, arg, 1); 3829 return build (COMPLEX_EXPR, TREE_TYPE (arg), 3830 build_unary_op (code, real, 1), imag); 3831 } 3832 3833 /* Report invalid types. */ 3834 3835 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_POINTER, 3836 arg, true))) 3837 { 3838 if (code == PREINCREMENT_EXPR) 3839 errstring ="no pre-increment operator for type"; 3840 else if (code == POSTINCREMENT_EXPR) 3841 errstring ="no post-increment operator for type"; 3842 else if (code == PREDECREMENT_EXPR) 3843 errstring ="no pre-decrement operator for type"; 3844 else 3845 errstring ="no post-decrement operator for type"; 3846 break; 3847 } 3848 3849 /* Report something read-only. */ 3850 3851 if (CP_TYPE_CONST_P (TREE_TYPE (arg)) 3852 || TREE_READONLY (arg)) 3853 readonly_error (arg, ((code == PREINCREMENT_EXPR 3854 || code == POSTINCREMENT_EXPR) 3855 ? "increment" : "decrement"), 3856 0); 3857 3858 { 3859 tree inc; 3860 tree result_type = TREE_TYPE (arg); 3861 3862 arg = get_unwidened (arg, 0); 3863 argtype = TREE_TYPE (arg); 3864 3865 /* ARM $5.2.5 last annotation says this should be forbidden. */ 3866 if (TREE_CODE (argtype) == ENUMERAL_TYPE) 3867 pedwarn ("ISO C++ forbids %sing an enum", 3868 (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR) 3869 ? "increment" : "decrement"); 3870 3871 /* Compute the increment. */ 3872 3873 if (TREE_CODE (argtype) == POINTER_TYPE) 3874 { 3875 tree type = complete_type (TREE_TYPE (argtype)); 3876 3877 if (!COMPLETE_OR_VOID_TYPE_P (type)) 3878 error ("cannot %s a pointer to incomplete type `%T'", 3879 ((code == PREINCREMENT_EXPR 3880 || code == POSTINCREMENT_EXPR) 3881 ? "increment" : "decrement"), TREE_TYPE (argtype)); 3882 else if ((pedantic || warn_pointer_arith) 3883 && !TYPE_PTROB_P (argtype)) 3884 pedwarn ("ISO C++ forbids %sing a pointer of type `%T'", 3885 ((code == PREINCREMENT_EXPR 3886 || code == POSTINCREMENT_EXPR) 3887 ? "increment" : "decrement"), argtype); 3888 inc = cxx_sizeof_nowarn (TREE_TYPE (argtype)); 3889 } 3890 else 3891 inc = integer_one_node; 3892 3893 inc = cp_convert (argtype, inc); 3894 3895 /* Handle incrementing a cast-expression. */ 3896 3897 switch (TREE_CODE (arg)) 3898 { 3899 case NOP_EXPR: 3900 case CONVERT_EXPR: 3901 case FLOAT_EXPR: 3902 case FIX_TRUNC_EXPR: 3903 case FIX_FLOOR_EXPR: 3904 case FIX_ROUND_EXPR: 3905 case FIX_CEIL_EXPR: 3906 { 3907 tree incremented, modify, value, compound; 3908 if (! lvalue_p (arg) && pedantic) 3909 pedwarn ("cast to non-reference type used as lvalue"); 3910 arg = stabilize_reference (arg); 3911 if (code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR) 3912 value = arg; 3913 else 3914 value = save_expr (arg); 3915 incremented = build (((code == PREINCREMENT_EXPR 3916 || code == POSTINCREMENT_EXPR) 3917 ? PLUS_EXPR : MINUS_EXPR), 3918 argtype, value, inc); 3919 3920 modify = build_modify_expr (arg, NOP_EXPR, incremented); 3921 compound = build (COMPOUND_EXPR, TREE_TYPE (arg), modify, value); 3922 3923 /* Eliminate warning about unused result of + or -. */ 3924 TREE_NO_UNUSED_WARNING (compound) = 1; 3925 return compound; 3926 } 3927 3928 default: 3929 break; 3930 } 3931 3932 /* Complain about anything else that is not a true lvalue. */ 3933 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR 3934 || code == POSTINCREMENT_EXPR) 3935 ? "increment" : "decrement"))) 3936 return error_mark_node; 3937 3938 /* Forbid using -- on `bool'. */ 3939 if (TREE_TYPE (arg) == boolean_type_node) 3940 { 3941 if (code == POSTDECREMENT_EXPR || code == PREDECREMENT_EXPR) 3942 { 3943 error ("invalid use of `--' on bool variable `%D'", arg); 3944 return error_mark_node; 3945 } 3946 val = boolean_increment (code, arg); 3947 } 3948 else 3949 val = build (code, TREE_TYPE (arg), arg, inc); 3950 3951 TREE_SIDE_EFFECTS (val) = 1; 3952 return cp_convert (result_type, val); 3953 } 3954 3955 case ADDR_EXPR: 3956 /* Note that this operation never does default_conversion 3957 regardless of NOCONVERT. */ 3958 3959 argtype = lvalue_type (arg); 3960 3961 if (TREE_CODE (arg) == OFFSET_REF) 3962 goto offset_ref; 3963 3964 if (TREE_CODE (argtype) == REFERENCE_TYPE) 3965 { 3966 arg = build1 3967 (CONVERT_EXPR, 3968 build_pointer_type (TREE_TYPE (argtype)), arg); 3969 TREE_CONSTANT (arg) = TREE_CONSTANT (TREE_OPERAND (arg, 0)); 3970 return arg; 3971 } 3972 else if (pedantic && DECL_MAIN_P (arg)) 3973 /* ARM $3.4 */ 3974 pedwarn ("ISO C++ forbids taking address of function `::main'"); 3975 3976 /* Let &* cancel out to simplify resulting code. */ 3977 if (TREE_CODE (arg) == INDIRECT_REF) 3978 { 3979 /* We don't need to have `current_class_ptr' wrapped in a 3980 NON_LVALUE_EXPR node. */ 3981 if (arg == current_class_ref) 3982 return current_class_ptr; 3983 3984 arg = TREE_OPERAND (arg, 0); 3985 if (TREE_CODE (TREE_TYPE (arg)) == REFERENCE_TYPE) 3986 { 3987 arg = build1 3988 (CONVERT_EXPR, 3989 build_pointer_type (TREE_TYPE (TREE_TYPE (arg))), arg); 3990 TREE_CONSTANT (arg) = TREE_CONSTANT (TREE_OPERAND (arg, 0)); 3991 } 3992 else if (lvalue_p (arg)) 3993 /* Don't let this be an lvalue. */ 3994 return non_lvalue (arg); 3995 return arg; 3996 } 3997 3998 /* For &x[y], return x+y. But, in a template, ARG may be an 3999 ARRAY_REF representing a non-dependent expression. In that 4000 case, there may be an overloaded "operator []" that will be 4001 chosen at instantiation time; we must not try to optimize 4002 here. */ 4003 if (TREE_CODE (arg) == ARRAY_REF && !processing_template_decl) 4004 { 4005 if (!cxx_mark_addressable (TREE_OPERAND (arg, 0))) 4006 return error_mark_node; 4007 return cp_build_binary_op (PLUS_EXPR, TREE_OPERAND (arg, 0), 4008 TREE_OPERAND (arg, 1)); 4009 } 4010 4011 /* Uninstantiated types are all functions. Taking the 4012 address of a function is a no-op, so just return the 4013 argument. */ 4014 4015 if (TREE_CODE (arg) == IDENTIFIER_NODE 4016 && IDENTIFIER_OPNAME_P (arg)) 4017 { 4018 abort (); 4019 /* We don't know the type yet, so just work around the problem. 4020 We know that this will resolve to an lvalue. */ 4021 return build1 (ADDR_EXPR, unknown_type_node, arg); 4022 } 4023 4024 if (TREE_CODE (arg) == COMPONENT_REF && type_unknown_p (arg) 4025 && !really_overloaded_fn (TREE_OPERAND (arg, 1))) 4026 { 4027 /* They're trying to take the address of a unique non-static 4028 member function. This is ill-formed (except in MS-land), 4029 but let's try to DTRT. 4030 Note: We only handle unique functions here because we don't 4031 want to complain if there's a static overload; non-unique 4032 cases will be handled by instantiate_type. But we need to 4033 handle this case here to allow casts on the resulting PMF. 4034 We could defer this in non-MS mode, but it's easier to give 4035 a useful error here. */ 4036 4037 /* Inside constant member functions, the `this' pointer 4038 contains an extra const qualifier. TYPE_MAIN_VARIANT 4039 is used here to remove this const from the diagnostics 4040 and the created OFFSET_REF. */ 4041 tree base = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (arg, 0))); 4042 tree name = DECL_NAME (get_first_fn (TREE_OPERAND (arg, 1))); 4043 4044 if (! flag_ms_extensions) 4045 { 4046 if (current_class_type 4047 && TREE_OPERAND (arg, 0) == current_class_ref) 4048 /* An expression like &memfn. */ 4049 pedwarn ("ISO C++ forbids taking the address of an unqualified" 4050 " or parenthesized non-static member function to form" 4051 " a pointer to member function. Say `&%T::%D'", 4052 base, name); 4053 else 4054 pedwarn ("ISO C++ forbids taking the address of a bound member" 4055 " function to form a pointer to member function." 4056 " Say `&%T::%D'", 4057 base, name); 4058 } 4059 arg = build_offset_ref (base, name, /*address_p=*/true); 4060 } 4061 4062 offset_ref: 4063 if (type_unknown_p (arg)) 4064 return build1 (ADDR_EXPR, unknown_type_node, arg); 4065 4066 /* Handle complex lvalues (when permitted) 4067 by reduction to simpler cases. */ 4068 val = unary_complex_lvalue (code, arg); 4069 if (val != 0) 4070 return val; 4071 4072 switch (TREE_CODE (arg)) 4073 { 4074 case NOP_EXPR: 4075 case CONVERT_EXPR: 4076 case FLOAT_EXPR: 4077 case FIX_TRUNC_EXPR: 4078 case FIX_FLOOR_EXPR: 4079 case FIX_ROUND_EXPR: 4080 case FIX_CEIL_EXPR: 4081 if (! lvalue_p (arg) && pedantic) 4082 pedwarn ("ISO C++ forbids taking the address of a cast to a non-lvalue expression"); 4083 break; 4084 4085 case OVERLOAD: 4086 arg = OVL_CURRENT (arg); 4087 break; 4088 4089 default: 4090 break; 4091 } 4092 4093 /* Allow the address of a constructor if all the elements 4094 are constant. */ 4095 if (TREE_CODE (arg) == CONSTRUCTOR && TREE_HAS_CONSTRUCTOR (arg) 4096 && TREE_CONSTANT (arg)) 4097 ; 4098 /* Anything not already handled and not a true memory reference 4099 is an error. */ 4100 else if (TREE_CODE (argtype) != FUNCTION_TYPE 4101 && TREE_CODE (argtype) != METHOD_TYPE 4102 && TREE_CODE (arg) != OFFSET_REF 4103 && !lvalue_or_else (arg, "unary `&'")) 4104 return error_mark_node; 4105 4106 if (argtype != error_mark_node) 4107 argtype = build_pointer_type (argtype); 4108 4109 { 4110 tree addr; 4111 4112 if (TREE_CODE (arg) != COMPONENT_REF 4113 /* Inside a template, we are processing a non-dependent 4114 expression so we can just form an ADDR_EXPR with the 4115 correct type. */ 4116 || processing_template_decl) 4117 { 4118 addr = build_address (arg); 4119 if (TREE_CODE (arg) == OFFSET_REF) 4120 PTRMEM_OK_P (addr) = PTRMEM_OK_P (arg); 4121 } 4122 else if (TREE_CODE (TREE_OPERAND (arg, 1)) == BASELINK) 4123 { 4124 tree fn = BASELINK_FUNCTIONS (TREE_OPERAND (arg, 1)); 4125 4126 /* We can only get here with a single static member 4127 function. */ 4128 my_friendly_assert (TREE_CODE (fn) == FUNCTION_DECL 4129 && DECL_STATIC_FUNCTION_P (fn), 4130 20030906); 4131 mark_used (fn); 4132 addr = build_address (fn); 4133 if (TREE_SIDE_EFFECTS (TREE_OPERAND (arg, 0))) 4134 /* Do not lose object's side effects. */ 4135 addr = build (COMPOUND_EXPR, TREE_TYPE (addr), 4136 TREE_OPERAND (arg, 0), addr); 4137 } 4138 else if (DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1))) 4139 { 4140 error ("attempt to take address of bit-field structure member `%D'", 4141 TREE_OPERAND (arg, 1)); 4142 return error_mark_node; 4143 } 4144 else 4145 { 4146 /* Unfortunately we cannot just build an address 4147 expression here, because we would not handle 4148 address-constant-expressions or offsetof correctly. */ 4149 tree field = TREE_OPERAND (arg, 1); 4150 tree rval = build_unary_op (ADDR_EXPR, TREE_OPERAND (arg, 0), 0); 4151 tree binfo = lookup_base (TREE_TYPE (TREE_TYPE (rval)), 4152 decl_type_context (field), 4153 ba_check, NULL); 4154 4155 rval = build_base_path (PLUS_EXPR, rval, binfo, 1); 4156 rval = build_nop (argtype, rval); 4157 addr = fold (build (PLUS_EXPR, argtype, rval, 4158 cp_convert (argtype, byte_position (field)))); 4159 } 4160 4161 if (TREE_CODE (argtype) == POINTER_TYPE 4162 && TREE_CODE (TREE_TYPE (argtype)) == METHOD_TYPE) 4163 { 4164 build_ptrmemfunc_type (argtype); 4165 addr = build_ptrmemfunc (argtype, addr, 0); 4166 } 4167 4168 return addr; 4169 } 4170 4171 default: 4172 break; 4173 } 4174 4175 if (!errstring) 4176 { 4177 if (argtype == 0) 4178 argtype = TREE_TYPE (arg); 4179 return fold (build1 (code, argtype, arg)); 4180 } 4181 4182 error ("%s", errstring); 4183 return error_mark_node; 4184} 4185 4186/* Apply unary lvalue-demanding operator CODE to the expression ARG 4187 for certain kinds of expressions which are not really lvalues 4188 but which we can accept as lvalues. 4189 4190 If ARG is not a kind of expression we can handle, return 4191 NULL_TREE. */ 4192 4193tree 4194unary_complex_lvalue (enum tree_code code, tree arg) 4195{ 4196 /* Handle (a, b) used as an "lvalue". */ 4197 if (TREE_CODE (arg) == COMPOUND_EXPR) 4198 { 4199 tree real_result = build_unary_op (code, TREE_OPERAND (arg, 1), 0); 4200 return build (COMPOUND_EXPR, TREE_TYPE (real_result), 4201 TREE_OPERAND (arg, 0), real_result); 4202 } 4203 4204 /* Handle (a ? b : c) used as an "lvalue". */ 4205 if (TREE_CODE (arg) == COND_EXPR 4206 || TREE_CODE (arg) == MIN_EXPR || TREE_CODE (arg) == MAX_EXPR) 4207 return rationalize_conditional_expr (code, arg); 4208 4209 /* Handle (a = b), (++a), and (--a) used as an "lvalue". */ 4210 if (TREE_CODE (arg) == MODIFY_EXPR 4211 || TREE_CODE (arg) == PREINCREMENT_EXPR 4212 || TREE_CODE (arg) == PREDECREMENT_EXPR) 4213 { 4214 tree lvalue = TREE_OPERAND (arg, 0); 4215 if (TREE_SIDE_EFFECTS (lvalue)) 4216 { 4217 lvalue = stabilize_reference (lvalue); 4218 arg = build (TREE_CODE (arg), TREE_TYPE (arg), 4219 lvalue, TREE_OPERAND (arg, 1)); 4220 } 4221 return unary_complex_lvalue 4222 (code, build (COMPOUND_EXPR, TREE_TYPE (lvalue), arg, lvalue)); 4223 } 4224 4225 if (code != ADDR_EXPR) 4226 return 0; 4227 4228 /* Handle (a = b) used as an "lvalue" for `&'. */ 4229 if (TREE_CODE (arg) == MODIFY_EXPR 4230 || TREE_CODE (arg) == INIT_EXPR) 4231 { 4232 tree real_result = build_unary_op (code, TREE_OPERAND (arg, 0), 0); 4233 arg = build (COMPOUND_EXPR, TREE_TYPE (real_result), arg, real_result); 4234 TREE_NO_UNUSED_WARNING (arg) = 1; 4235 return arg; 4236 } 4237 4238 if (TREE_CODE (TREE_TYPE (arg)) == FUNCTION_TYPE 4239 || TREE_CODE (TREE_TYPE (arg)) == METHOD_TYPE 4240 || TREE_CODE (arg) == OFFSET_REF) 4241 { 4242 tree t; 4243 4244 my_friendly_assert (TREE_CODE (arg) != SCOPE_REF, 313); 4245 4246 if (TREE_CODE (arg) != OFFSET_REF) 4247 return 0; 4248 4249 t = TREE_OPERAND (arg, 1); 4250 4251 /* Check all this code for right semantics. */ 4252 if (TREE_CODE (t) == FUNCTION_DECL) 4253 { 4254 if (DECL_DESTRUCTOR_P (t)) 4255 error ("taking address of destructor"); 4256 return build_unary_op (ADDR_EXPR, t, 0); 4257 } 4258 if (TREE_CODE (t) == VAR_DECL) 4259 return build_unary_op (ADDR_EXPR, t, 0); 4260 else 4261 { 4262 tree type; 4263 4264 if (TREE_OPERAND (arg, 0) 4265 && ! is_dummy_object (TREE_OPERAND (arg, 0)) 4266 && TREE_CODE (t) != FIELD_DECL) 4267 { 4268 error ("taking address of bound pointer-to-member expression"); 4269 return error_mark_node; 4270 } 4271 if (!PTRMEM_OK_P (arg)) 4272 return build_unary_op (code, arg, 0); 4273 4274 if (TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE) 4275 { 4276 error ("cannot create pointer to reference member `%D'", t); 4277 return error_mark_node; 4278 } 4279 4280 type = build_ptrmem_type (context_for_name_lookup (t), 4281 TREE_TYPE (t)); 4282 t = make_ptrmem_cst (type, TREE_OPERAND (arg, 1)); 4283 return t; 4284 } 4285 } 4286 4287 4288 /* We permit compiler to make function calls returning 4289 objects of aggregate type look like lvalues. */ 4290 { 4291 tree targ = arg; 4292 4293 if (TREE_CODE (targ) == SAVE_EXPR) 4294 targ = TREE_OPERAND (targ, 0); 4295 4296 if (TREE_CODE (targ) == CALL_EXPR && IS_AGGR_TYPE (TREE_TYPE (targ))) 4297 { 4298 if (TREE_CODE (arg) == SAVE_EXPR) 4299 targ = arg; 4300 else 4301 targ = build_cplus_new (TREE_TYPE (arg), arg); 4302 return build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (arg)), targ); 4303 } 4304 4305 if (TREE_CODE (arg) == SAVE_EXPR && TREE_CODE (targ) == INDIRECT_REF) 4306 return build (SAVE_EXPR, build_pointer_type (TREE_TYPE (arg)), 4307 TREE_OPERAND (targ, 0), current_function_decl, NULL); 4308 } 4309 4310 /* Don't let anything else be handled specially. */ 4311 return 0; 4312} 4313 4314/* Mark EXP saying that we need to be able to take the 4315 address of it; it should not be allocated in a register. 4316 Value is true if successful. 4317 4318 C++: we do not allow `current_class_ptr' to be addressable. */ 4319 4320bool 4321cxx_mark_addressable (tree exp) 4322{ 4323 tree x = exp; 4324 4325 while (1) 4326 switch (TREE_CODE (x)) 4327 { 4328 case ADDR_EXPR: 4329 case COMPONENT_REF: 4330 case ARRAY_REF: 4331 case REALPART_EXPR: 4332 case IMAGPART_EXPR: 4333 x = TREE_OPERAND (x, 0); 4334 break; 4335 4336 case PARM_DECL: 4337 if (x == current_class_ptr) 4338 { 4339 error ("cannot take the address of `this', which is an rvalue expression"); 4340 TREE_ADDRESSABLE (x) = 1; /* so compiler doesn't die later. */ 4341 return true; 4342 } 4343 /* Fall through. */ 4344 4345 case VAR_DECL: 4346 /* Caller should not be trying to mark initialized 4347 constant fields addressable. */ 4348 my_friendly_assert (DECL_LANG_SPECIFIC (x) == 0 4349 || DECL_IN_AGGR_P (x) == 0 4350 || TREE_STATIC (x) 4351 || DECL_EXTERNAL (x), 314); 4352 /* Fall through. */ 4353 4354 case CONST_DECL: 4355 case RESULT_DECL: 4356 if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x) 4357 && !DECL_ARTIFICIAL (x) && extra_warnings) 4358 warning ("address requested for `%D', which is declared `register'", 4359 x); 4360 TREE_ADDRESSABLE (x) = 1; 4361 put_var_into_stack (x, /*rescan=*/true); 4362 return true; 4363 4364 case FUNCTION_DECL: 4365 TREE_ADDRESSABLE (x) = 1; 4366 TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (x)) = 1; 4367 return true; 4368 4369 case CONSTRUCTOR: 4370 TREE_ADDRESSABLE (x) = 1; 4371 return true; 4372 4373 case TARGET_EXPR: 4374 TREE_ADDRESSABLE (x) = 1; 4375 cxx_mark_addressable (TREE_OPERAND (x, 0)); 4376 return true; 4377 4378 default: 4379 return true; 4380 } 4381} 4382 4383/* Build and return a conditional expression IFEXP ? OP1 : OP2. */ 4384 4385tree 4386build_x_conditional_expr (tree ifexp, tree op1, tree op2) 4387{ 4388 tree orig_ifexp = ifexp; 4389 tree orig_op1 = op1; 4390 tree orig_op2 = op2; 4391 tree expr; 4392 4393 if (processing_template_decl) 4394 { 4395 /* The standard says that the expression is type-dependent if 4396 IFEXP is type-dependent, even though the eventual type of the 4397 expression doesn't dependent on IFEXP. */ 4398 if (type_dependent_expression_p (ifexp) 4399 /* As a GNU extension, the middle operand may be omitted. */ 4400 || (op1 && type_dependent_expression_p (op1)) 4401 || type_dependent_expression_p (op2)) 4402 return build_min_nt (COND_EXPR, ifexp, op1, op2); 4403 ifexp = build_non_dependent_expr (ifexp); 4404 if (op1) 4405 op1 = build_non_dependent_expr (op1); 4406 op2 = build_non_dependent_expr (op2); 4407 } 4408 4409 expr = build_conditional_expr (ifexp, op1, op2); 4410 if (processing_template_decl && expr != error_mark_node) 4411 return build_min_non_dep (COND_EXPR, expr, 4412 orig_ifexp, orig_op1, orig_op2); 4413 return expr; 4414} 4415 4416/* Given a list of expressions, return a compound expression 4417 that performs them all and returns the value of the last of them. */ 4418 4419tree build_x_compound_expr_from_list (tree list, const char *msg) 4420{ 4421 tree expr = TREE_VALUE (list); 4422 4423 if (TREE_CHAIN (list)) 4424 { 4425 if (msg) 4426 pedwarn ("%s expression list treated as compound expression", msg); 4427 4428 for (list = TREE_CHAIN (list); list; list = TREE_CHAIN (list)) 4429 expr = build_x_compound_expr (expr, TREE_VALUE (list)); 4430 } 4431 4432 return expr; 4433} 4434 4435/* Handle overloading of the ',' operator when needed. */ 4436 4437tree 4438build_x_compound_expr (tree op1, tree op2) 4439{ 4440 tree result; 4441 tree orig_op1 = op1; 4442 tree orig_op2 = op2; 4443 4444 if (processing_template_decl) 4445 { 4446 if (type_dependent_expression_p (op1) 4447 || type_dependent_expression_p (op2)) 4448 return build_min_nt (COMPOUND_EXPR, op1, op2); 4449 op1 = build_non_dependent_expr (op1); 4450 op2 = build_non_dependent_expr (op2); 4451 } 4452 4453 result = build_new_op (COMPOUND_EXPR, LOOKUP_NORMAL, op1, op2, NULL_TREE, 4454 /*overloaded_p=*/NULL); 4455 if (!result) 4456 result = build_compound_expr (op1, op2); 4457 4458 if (processing_template_decl && result != error_mark_node) 4459 return build_min_non_dep (COMPOUND_EXPR, result, orig_op1, orig_op2); 4460 4461 return result; 4462} 4463 4464/* Build a compound expression. */ 4465 4466tree 4467build_compound_expr (tree lhs, tree rhs) 4468{ 4469 lhs = decl_constant_value (lhs); 4470 lhs = convert_to_void (lhs, "left-hand operand of comma"); 4471 4472 if (lhs == error_mark_node || rhs == error_mark_node) 4473 return error_mark_node; 4474 4475 if (TREE_CODE (rhs) == TARGET_EXPR) 4476 { 4477 /* If the rhs is a TARGET_EXPR, then build the compound 4478 expression inside the target_expr's initializer. This 4479 helps the compiler to eliminate unnecessary temporaries. */ 4480 tree init = TREE_OPERAND (rhs, 1); 4481 4482 init = build (COMPOUND_EXPR, TREE_TYPE (init), lhs, init); 4483 TREE_OPERAND (rhs, 1) = init; 4484 4485 return rhs; 4486 } 4487 4488 return build (COMPOUND_EXPR, TREE_TYPE (rhs), lhs, rhs); 4489} 4490 4491/* Issue an error message if casting from SRC_TYPE to DEST_TYPE casts 4492 away constness. DESCRIPTION explains what operation is taking 4493 place. */ 4494 4495static void 4496check_for_casting_away_constness (tree src_type, tree dest_type, 4497 const char *description) 4498{ 4499 if (casts_away_constness (src_type, dest_type)) 4500 error ("%s from type `%T' to type `%T' casts away constness", 4501 description, src_type, dest_type); 4502} 4503 4504/* Return an expression representing static_cast<TYPE>(EXPR). */ 4505 4506tree 4507build_static_cast (tree type, tree expr) 4508{ 4509 tree intype; 4510 tree result; 4511 4512 if (type == error_mark_node || expr == error_mark_node) 4513 return error_mark_node; 4514 4515 if (processing_template_decl) 4516 { 4517 expr = build_min (STATIC_CAST_EXPR, type, expr); 4518 /* We don't know if it will or will not have side effects. */ 4519 TREE_SIDE_EFFECTS (expr) = 1; 4520 return expr; 4521 } 4522 4523 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue. 4524 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */ 4525 if (TREE_CODE (type) != REFERENCE_TYPE 4526 && TREE_CODE (expr) == NOP_EXPR 4527 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0))) 4528 expr = TREE_OPERAND (expr, 0); 4529 4530 intype = TREE_TYPE (expr); 4531 4532 /* [expr.static.cast] 4533 4534 An lvalue of type "cv1 B", where B is a class type, can be cast 4535 to type "reference to cv2 D", where D is a class derived (clause 4536 _class.derived_) from B, if a valid standard conversion from 4537 "pointer to D" to "pointer to B" exists (_conv.ptr_), cv2 is the 4538 same cv-qualification as, or greater cv-qualification than, cv1, 4539 and B is not a virtual base class of D. */ 4540 /* We check this case before checking the validity of "TYPE t = 4541 EXPR;" below because for this case: 4542 4543 struct B {}; 4544 struct D : public B { D(const B&); }; 4545 extern B& b; 4546 void f() { static_cast<const D&>(b); } 4547 4548 we want to avoid constructing a new D. The standard is not 4549 completely clear about this issue, but our interpretation is 4550 consistent with other compilers. */ 4551 if (TREE_CODE (type) == REFERENCE_TYPE 4552 && CLASS_TYPE_P (TREE_TYPE (type)) 4553 && CLASS_TYPE_P (intype) 4554 && real_lvalue_p (expr) 4555 && DERIVED_FROM_P (intype, TREE_TYPE (type)) 4556 && can_convert (build_pointer_type (TYPE_MAIN_VARIANT (intype)), 4557 build_pointer_type (TYPE_MAIN_VARIANT 4558 (TREE_TYPE (type)))) 4559 && at_least_as_qualified_p (TREE_TYPE (type), intype)) 4560 { 4561 /* There is a standard conversion from "D*" to "B*" even if "B" 4562 is ambiguous or inaccessible. Therefore, we ask lookup_base 4563 to check these conditions. */ 4564 tree base = lookup_base (TREE_TYPE (type), intype, ba_check, NULL); 4565 4566 /* Convert from "B*" to "D*". This function will check that "B" 4567 is not a virtual base of "D". */ 4568 expr = build_base_path (MINUS_EXPR, build_address (expr), 4569 base, /*nonnull=*/false); 4570 /* Convert the pointer to a reference -- but then remember that 4571 there are no expressions with reference type in C++. */ 4572 return convert_from_reference (build_nop (type, expr)); 4573 } 4574 4575 /* [expr.static.cast] 4576 4577 An expression e can be explicitly converted to a type T using a 4578 static_cast of the form static_cast<T>(e) if the declaration T 4579 t(e);" is well-formed, for some invented temporary variable 4580 t. */ 4581 result = perform_direct_initialization_if_possible (type, expr); 4582 if (result) 4583 { 4584 result = convert_from_reference (result); 4585 /* [expr.static.cast] 4586 4587 If T is a reference type, the result is an lvalue; otherwise, 4588 the result is an rvalue. */ 4589 if (TREE_CODE (type) != REFERENCE_TYPE 4590 && real_lvalue_p (result)) 4591 result = build1 (NON_LVALUE_EXPR, TREE_TYPE (result), result); 4592 return result; 4593 } 4594 4595 /* [expr.static.cast] 4596 4597 Any expression can be explicitly converted to type cv void. */ 4598 if (TREE_CODE (type) == VOID_TYPE) 4599 return convert_to_void (expr, /*implicit=*/NULL); 4600 4601 /* [expr.static.cast] 4602 4603 The inverse of any standard conversion sequence (clause _conv_), 4604 other than the lvalue-to-rvalue (_conv.lval_), array-to-pointer 4605 (_conv.array_), function-to-pointer (_conv.func_), and boolean 4606 (_conv.bool_) conversions, can be performed explicitly using 4607 static_cast subject to the restriction that the explicit 4608 conversion does not cast away constness (_expr.const.cast_), and 4609 the following additional rules for specific cases: */ 4610 /* For reference, the conversions not excluded are: integral 4611 promotions, floating point promotion, integral conversions, 4612 floating point conversions, floating-integral conversions, 4613 pointer conversions, and pointer to member conversions. */ 4614 if ((ARITHMETIC_TYPE_P (type) && ARITHMETIC_TYPE_P (intype)) 4615 /* DR 128 4616 4617 A value of integral _or enumeration_ type can be explicitly 4618 converted to an enumeration type. */ 4619 || (INTEGRAL_OR_ENUMERATION_TYPE_P (type) 4620 && INTEGRAL_OR_ENUMERATION_TYPE_P (intype))) 4621 /* Really, build_c_cast should defer to this function rather 4622 than the other way around. */ 4623 return build_c_cast (type, expr); 4624 4625 if (TYPE_PTR_P (type) && TYPE_PTR_P (intype) 4626 && CLASS_TYPE_P (TREE_TYPE (type)) 4627 && CLASS_TYPE_P (TREE_TYPE (intype)) 4628 && can_convert (build_pointer_type (TYPE_MAIN_VARIANT 4629 (TREE_TYPE (intype))), 4630 build_pointer_type (TYPE_MAIN_VARIANT 4631 (TREE_TYPE (type))))) 4632 { 4633 tree base; 4634 4635 check_for_casting_away_constness (intype, type, "static_cast"); 4636 base = lookup_base (TREE_TYPE (type), TREE_TYPE (intype), ba_check, 4637 NULL); 4638 return build_base_path (MINUS_EXPR, expr, base, /*nonnull=*/false); 4639 } 4640 4641 if ((TYPE_PTRMEM_P (type) && TYPE_PTRMEM_P (intype)) 4642 || (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype))) 4643 { 4644 tree c1; 4645 tree c2; 4646 tree t1; 4647 tree t2; 4648 4649 c1 = TYPE_PTRMEM_CLASS_TYPE (intype); 4650 c2 = TYPE_PTRMEM_CLASS_TYPE (type); 4651 4652 if (TYPE_PTRMEM_P (type)) 4653 { 4654 t1 = (build_ptrmem_type 4655 (c1, 4656 TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (intype)))); 4657 t2 = (build_ptrmem_type 4658 (c2, 4659 TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (type)))); 4660 } 4661 else 4662 { 4663 t1 = intype; 4664 t2 = type; 4665 } 4666 if (can_convert (t1, t2)) 4667 { 4668 check_for_casting_away_constness (intype, type, "static_cast"); 4669 if (TYPE_PTRMEM_P (type)) 4670 { 4671 tree delta; 4672 4673 if (TREE_CODE (expr) == PTRMEM_CST) 4674 expr = cplus_expand_constant (expr); 4675 delta = get_delta_difference (c1, c2, /*force=*/1); 4676 if (!integer_zerop (delta)) 4677 expr = cp_build_binary_op (PLUS_EXPR, 4678 build_nop (ptrdiff_type_node, expr), 4679 delta); 4680 return build_nop (type, expr); 4681 } 4682 else 4683 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), expr, 4684 /*force=*/1); 4685 } 4686 } 4687 4688 /* [expr.static.cast] 4689 4690 An rvalue of type "pointer to cv void" can be explicitly 4691 converted to a pointer to object type. A value of type pointer 4692 to object converted to "pointer to cv void" and back to the 4693 original pointer type will have its original value. */ 4694 if (TREE_CODE (intype) == POINTER_TYPE 4695 && VOID_TYPE_P (TREE_TYPE (intype)) 4696 && TYPE_PTROB_P (type)) 4697 { 4698 check_for_casting_away_constness (intype, type, "static_cast"); 4699 return build_nop (type, expr); 4700 } 4701 4702 error ("invalid static_cast from type `%T' to type `%T'", intype, type); 4703 return error_mark_node; 4704} 4705 4706tree 4707build_reinterpret_cast (tree type, tree expr) 4708{ 4709 tree intype; 4710 4711 if (type == error_mark_node || expr == error_mark_node) 4712 return error_mark_node; 4713 4714 if (processing_template_decl) 4715 { 4716 tree t = build_min (REINTERPRET_CAST_EXPR, type, expr); 4717 4718 if (!TREE_SIDE_EFFECTS (t) 4719 && type_dependent_expression_p (expr)) 4720 /* There might turn out to be side effects inside expr. */ 4721 TREE_SIDE_EFFECTS (t) = 1; 4722 return t; 4723 } 4724 4725 if (TREE_CODE (type) != REFERENCE_TYPE) 4726 { 4727 expr = decay_conversion (expr); 4728 4729 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue. 4730 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */ 4731 if (TREE_CODE (expr) == NOP_EXPR 4732 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0))) 4733 expr = TREE_OPERAND (expr, 0); 4734 } 4735 4736 intype = TREE_TYPE (expr); 4737 4738 if (TREE_CODE (type) == REFERENCE_TYPE) 4739 { 4740 if (! real_lvalue_p (expr)) 4741 { 4742 error ("invalid reinterpret_cast of an rvalue expression of type `%T' to type `%T'", intype, type); 4743 return error_mark_node; 4744 } 4745 expr = build_unary_op (ADDR_EXPR, expr, 0); 4746 if (expr != error_mark_node) 4747 expr = build_reinterpret_cast 4748 (build_pointer_type (TREE_TYPE (type)), expr); 4749 if (expr != error_mark_node) 4750 expr = build_indirect_ref (expr, 0); 4751 return expr; 4752 } 4753 else if (same_type_ignoring_top_level_qualifiers_p (intype, type)) 4754 return build_static_cast (type, expr); 4755 4756 if (TYPE_PTR_P (type) && (TREE_CODE (intype) == INTEGER_TYPE 4757 || TREE_CODE (intype) == ENUMERAL_TYPE)) 4758 /* OK */; 4759 else if (TREE_CODE (type) == INTEGER_TYPE && TYPE_PTR_P (intype)) 4760 { 4761 if (TYPE_PRECISION (type) < TYPE_PRECISION (intype)) 4762 pedwarn ("reinterpret_cast from `%T' to `%T' loses precision", 4763 intype, type); 4764 } 4765 else if ((TYPE_PTRFN_P (type) && TYPE_PTRFN_P (intype)) 4766 || (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype))) 4767 { 4768 expr = decl_constant_value (expr); 4769 return fold (build1 (NOP_EXPR, type, expr)); 4770 } 4771 else if ((TYPE_PTRMEM_P (type) && TYPE_PTRMEM_P (intype)) 4772 || (TYPE_PTROBV_P (type) && TYPE_PTROBV_P (intype))) 4773 { 4774 check_for_casting_away_constness (intype, type, "reinterpret_cast"); 4775 expr = decl_constant_value (expr); 4776 return fold (build1 (NOP_EXPR, type, expr)); 4777 } 4778 else if ((TYPE_PTRFN_P (type) && TYPE_PTROBV_P (intype)) 4779 || (TYPE_PTRFN_P (intype) && TYPE_PTROBV_P (type))) 4780 { 4781 pedwarn ("ISO C++ forbids casting between pointer-to-function and pointer-to-object"); 4782 expr = decl_constant_value (expr); 4783 return fold (build1 (NOP_EXPR, type, expr)); 4784 } 4785 else 4786 { 4787 error ("invalid reinterpret_cast from type `%T' to type `%T'", 4788 intype, type); 4789 return error_mark_node; 4790 } 4791 4792 return cp_convert (type, expr); 4793} 4794 4795tree 4796build_const_cast (tree type, tree expr) 4797{ 4798 tree intype; 4799 4800 if (type == error_mark_node || error_operand_p (expr)) 4801 return error_mark_node; 4802 4803 if (processing_template_decl) 4804 { 4805 tree t = build_min (CONST_CAST_EXPR, type, expr); 4806 4807 if (!TREE_SIDE_EFFECTS (t) 4808 && type_dependent_expression_p (expr)) 4809 /* There might turn out to be side effects inside expr. */ 4810 TREE_SIDE_EFFECTS (t) = 1; 4811 return t; 4812 } 4813 4814 if (!POINTER_TYPE_P (type) && !TYPE_PTRMEM_P (type)) 4815 error ("invalid use of const_cast with type `%T', which is not a pointer, reference, nor a pointer-to-data-member type", type); 4816 else if (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE) 4817 { 4818 error ("invalid use of const_cast with type `%T', which is a pointer or reference to a function type", type); 4819 return error_mark_node; 4820 } 4821 4822 if (TREE_CODE (type) != REFERENCE_TYPE) 4823 { 4824 expr = decay_conversion (expr); 4825 4826 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue. 4827 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */ 4828 if (TREE_CODE (expr) == NOP_EXPR 4829 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0))) 4830 expr = TREE_OPERAND (expr, 0); 4831 } 4832 4833 intype = TREE_TYPE (expr); 4834 4835 if (same_type_ignoring_top_level_qualifiers_p (intype, type)) 4836 return build_static_cast (type, expr); 4837 else if (TREE_CODE (type) == REFERENCE_TYPE) 4838 { 4839 if (! real_lvalue_p (expr)) 4840 { 4841 error ("invalid const_cast of an rvalue of type `%T' to type `%T'", intype, type); 4842 return error_mark_node; 4843 } 4844 4845 if (comp_ptr_ttypes_const (TREE_TYPE (type), intype)) 4846 { 4847 expr = build_unary_op (ADDR_EXPR, expr, 0); 4848 expr = build1 (NOP_EXPR, type, expr); 4849 return convert_from_reference (expr); 4850 } 4851 } 4852 else if (((TREE_CODE (type) == POINTER_TYPE 4853 && TREE_CODE (intype) == POINTER_TYPE) 4854 || (TYPE_PTRMEM_P (type) && TYPE_PTRMEM_P (intype))) 4855 && comp_ptr_ttypes_const (TREE_TYPE (type), TREE_TYPE (intype))) 4856 return cp_convert (type, expr); 4857 4858 error ("invalid const_cast from type `%T' to type `%T'", intype, type); 4859 return error_mark_node; 4860} 4861 4862/* Build an expression representing a cast to type TYPE of expression EXPR. 4863 4864 ALLOW_NONCONVERTING is true if we should allow non-converting constructors 4865 when doing the cast. */ 4866 4867tree 4868build_c_cast (tree type, tree expr) 4869{ 4870 tree value = expr; 4871 tree otype; 4872 4873 if (type == error_mark_node || expr == error_mark_node) 4874 return error_mark_node; 4875 4876 if (processing_template_decl) 4877 { 4878 tree t = build_min (CAST_EXPR, type, 4879 tree_cons (NULL_TREE, value, NULL_TREE)); 4880 /* We don't know if it will or will not have side effects. */ 4881 TREE_SIDE_EFFECTS (t) = 1; 4882 return t; 4883 } 4884 4885 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue. 4886 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */ 4887 if (TREE_CODE (type) != REFERENCE_TYPE 4888 && TREE_CODE (value) == NOP_EXPR 4889 && TREE_TYPE (value) == TREE_TYPE (TREE_OPERAND (value, 0))) 4890 value = TREE_OPERAND (value, 0); 4891 4892 if (TREE_CODE (type) == ARRAY_TYPE) 4893 { 4894 /* Allow casting from T1* to T2[] because Cfront allows it. 4895 NIHCL uses it. It is not valid ISO C++ however. */ 4896 if (TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE) 4897 { 4898 pedwarn ("ISO C++ forbids casting to an array type `%T'", type); 4899 type = build_pointer_type (TREE_TYPE (type)); 4900 } 4901 else 4902 { 4903 error ("ISO C++ forbids casting to an array type `%T'", type); 4904 return error_mark_node; 4905 } 4906 } 4907 4908 if (TREE_CODE (type) == FUNCTION_TYPE 4909 || TREE_CODE (type) == METHOD_TYPE) 4910 { 4911 error ("invalid cast to function type `%T'", type); 4912 return error_mark_node; 4913 } 4914 4915 if (TREE_CODE (type) == VOID_TYPE) 4916 { 4917 /* Conversion to void does not cause any of the normal function to 4918 * pointer, array to pointer and lvalue to rvalue decays. */ 4919 4920 value = convert_to_void (value, /*implicit=*/NULL); 4921 return value; 4922 } 4923 4924 if (!complete_type_or_else (type, NULL_TREE)) 4925 return error_mark_node; 4926 4927 /* Convert functions and arrays to pointers and 4928 convert references to their expanded types, 4929 but don't convert any other types. If, however, we are 4930 casting to a class type, there's no reason to do this: the 4931 cast will only succeed if there is a converting constructor, 4932 and the default conversions will be done at that point. In 4933 fact, doing the default conversion here is actually harmful 4934 in cases like this: 4935 4936 typedef int A[2]; 4937 struct S { S(const A&); }; 4938 4939 since we don't want the array-to-pointer conversion done. */ 4940 if (!IS_AGGR_TYPE (type)) 4941 { 4942 if (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE 4943 || (TREE_CODE (TREE_TYPE (value)) == METHOD_TYPE 4944 /* Don't do the default conversion on a ->* expression. */ 4945 && ! (TREE_CODE (type) == POINTER_TYPE 4946 && bound_pmf_p (value))) 4947 || TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE 4948 || TREE_CODE (TREE_TYPE (value)) == REFERENCE_TYPE) 4949 value = decay_conversion (value); 4950 } 4951 else if (TREE_CODE (TREE_TYPE (value)) == REFERENCE_TYPE) 4952 /* However, even for class types, we still need to strip away 4953 the reference type, since the call to convert_force below 4954 does not expect the input expression to be of reference 4955 type. */ 4956 value = convert_from_reference (value); 4957 4958 otype = TREE_TYPE (value); 4959 4960 /* Optionally warn about potentially worrisome casts. */ 4961 4962 if (warn_cast_qual 4963 && TREE_CODE (type) == POINTER_TYPE 4964 && TREE_CODE (otype) == POINTER_TYPE 4965 && !at_least_as_qualified_p (TREE_TYPE (type), 4966 TREE_TYPE (otype))) 4967 warning ("cast from `%T' to `%T' discards qualifiers from pointer target type", 4968 otype, type); 4969 4970 if (TREE_CODE (type) == INTEGER_TYPE 4971 && TYPE_PTR_P (otype) 4972 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)) 4973 warning ("cast from pointer to integer of different size"); 4974 4975 if (TYPE_PTR_P (type) 4976 && TREE_CODE (otype) == INTEGER_TYPE 4977 && TYPE_PRECISION (type) != TYPE_PRECISION (otype) 4978 /* Don't warn about converting any constant. */ 4979 && !TREE_CONSTANT (value)) 4980 warning ("cast to pointer from integer of different size"); 4981 4982 if (TREE_CODE (type) == REFERENCE_TYPE) 4983 value = (convert_from_reference 4984 (convert_to_reference (type, value, CONV_C_CAST, 4985 LOOKUP_COMPLAIN, NULL_TREE))); 4986 else 4987 { 4988 tree ovalue; 4989 4990 value = decl_constant_value (value); 4991 4992 ovalue = value; 4993 value = convert_force (type, value, CONV_C_CAST); 4994 4995 /* Ignore any integer overflow caused by the cast. */ 4996 if (TREE_CODE (value) == INTEGER_CST) 4997 { 4998 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue); 4999 TREE_CONSTANT_OVERFLOW (value) = TREE_CONSTANT_OVERFLOW (ovalue); 5000 } 5001 } 5002 5003 /* Warn about possible alignment problems. Do this here when we will have 5004 instantiated any necessary template types. */ 5005 if (STRICT_ALIGNMENT && warn_cast_align 5006 && TREE_CODE (type) == POINTER_TYPE 5007 && TREE_CODE (otype) == POINTER_TYPE 5008 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE 5009 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE 5010 && COMPLETE_TYPE_P (TREE_TYPE (otype)) 5011 && COMPLETE_TYPE_P (TREE_TYPE (type)) 5012 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype))) 5013 warning ("cast from `%T' to `%T' increases required alignment of target type", 5014 otype, type); 5015 5016 /* Always produce some operator for an explicit cast, 5017 so we can tell (for -pedantic) that the cast is no lvalue. */ 5018 if (TREE_CODE (type) != REFERENCE_TYPE && value == expr 5019 && real_lvalue_p (value)) 5020 value = non_lvalue (value); 5021 5022 return value; 5023} 5024 5025/* Build an assignment expression of lvalue LHS from value RHS. 5026 MODIFYCODE is the code for a binary operator that we use 5027 to combine the old value of LHS with RHS to get the new value. 5028 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment. 5029 5030 C++: If MODIFYCODE is INIT_EXPR, then leave references unbashed. */ 5031 5032tree 5033build_modify_expr (tree lhs, enum tree_code modifycode, tree rhs) 5034{ 5035 tree result; 5036 tree newrhs = rhs; 5037 tree lhstype = TREE_TYPE (lhs); 5038 tree olhstype = lhstype; 5039 tree olhs = NULL_TREE; 5040 5041 /* Avoid duplicate error messages from operands that had errors. */ 5042 if (lhs == error_mark_node || rhs == error_mark_node) 5043 return error_mark_node; 5044 5045 /* Handle control structure constructs used as "lvalues". */ 5046 switch (TREE_CODE (lhs)) 5047 { 5048 /* Handle --foo = 5; as these are valid constructs in C++. */ 5049 case PREDECREMENT_EXPR: 5050 case PREINCREMENT_EXPR: 5051 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0))) 5052 lhs = build (TREE_CODE (lhs), TREE_TYPE (lhs), 5053 stabilize_reference (TREE_OPERAND (lhs, 0)), 5054 TREE_OPERAND (lhs, 1)); 5055 return build (COMPOUND_EXPR, lhstype, 5056 lhs, 5057 build_modify_expr (TREE_OPERAND (lhs, 0), 5058 modifycode, rhs)); 5059 5060 /* Handle (a, b) used as an "lvalue". */ 5061 case COMPOUND_EXPR: 5062 newrhs = build_modify_expr (TREE_OPERAND (lhs, 1), 5063 modifycode, rhs); 5064 if (newrhs == error_mark_node) 5065 return error_mark_node; 5066 return build (COMPOUND_EXPR, lhstype, 5067 TREE_OPERAND (lhs, 0), newrhs); 5068 5069 case MODIFY_EXPR: 5070 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0))) 5071 lhs = build (TREE_CODE (lhs), TREE_TYPE (lhs), 5072 stabilize_reference (TREE_OPERAND (lhs, 0)), 5073 TREE_OPERAND (lhs, 1)); 5074 newrhs = build_modify_expr (TREE_OPERAND (lhs, 0), modifycode, rhs); 5075 if (newrhs == error_mark_node) 5076 return error_mark_node; 5077 return build (COMPOUND_EXPR, lhstype, lhs, newrhs); 5078 5079 /* Handle (a ? b : c) used as an "lvalue". */ 5080 case COND_EXPR: 5081 { 5082 /* Produce (a ? (b = rhs) : (c = rhs)) 5083 except that the RHS goes through a save-expr 5084 so the code to compute it is only emitted once. */ 5085 tree cond; 5086 tree preeval = NULL_TREE; 5087 5088 rhs = stabilize_expr (rhs, &preeval); 5089 5090 /* Check this here to avoid odd errors when trying to convert 5091 a throw to the type of the COND_EXPR. */ 5092 if (!lvalue_or_else (lhs, "assignment")) 5093 return error_mark_node; 5094 5095 cond = build_conditional_expr 5096 (TREE_OPERAND (lhs, 0), 5097 build_modify_expr (cp_convert (TREE_TYPE (lhs), 5098 TREE_OPERAND (lhs, 1)), 5099 modifycode, rhs), 5100 build_modify_expr (cp_convert (TREE_TYPE (lhs), 5101 TREE_OPERAND (lhs, 2)), 5102 modifycode, rhs)); 5103 5104 if (cond == error_mark_node) 5105 return cond; 5106 /* Make sure the code to compute the rhs comes out 5107 before the split. */ 5108 if (preeval) 5109 cond = build (COMPOUND_EXPR, TREE_TYPE (lhs), preeval, cond); 5110 return cond; 5111 } 5112 5113 default: 5114 break; 5115 } 5116 5117 if (modifycode == INIT_EXPR) 5118 { 5119 if (TREE_CODE (rhs) == CONSTRUCTOR) 5120 { 5121 if (! same_type_p (TREE_TYPE (rhs), lhstype)) 5122 /* Call convert to generate an error; see PR 11063. */ 5123 rhs = convert (lhstype, rhs); 5124 result = build (INIT_EXPR, lhstype, lhs, rhs); 5125 TREE_SIDE_EFFECTS (result) = 1; 5126 return result; 5127 } 5128 else if (! IS_AGGR_TYPE (lhstype)) 5129 /* Do the default thing. */; 5130 else 5131 { 5132 result = build_special_member_call (lhs, complete_ctor_identifier, 5133 build_tree_list (NULL_TREE, rhs), 5134 TYPE_BINFO (lhstype), 5135 LOOKUP_NORMAL); 5136 if (result == NULL_TREE) 5137 return error_mark_node; 5138 return result; 5139 } 5140 } 5141 else 5142 { 5143 if (TREE_CODE (lhstype) == REFERENCE_TYPE) 5144 { 5145 lhs = convert_from_reference (lhs); 5146 olhstype = lhstype = TREE_TYPE (lhs); 5147 } 5148 lhs = require_complete_type (lhs); 5149 if (lhs == error_mark_node) 5150 return error_mark_node; 5151 5152 if (modifycode == NOP_EXPR) 5153 { 5154 /* `operator=' is not an inheritable operator. */ 5155 if (! IS_AGGR_TYPE (lhstype)) 5156 /* Do the default thing. */; 5157 else 5158 { 5159 result = build_new_op (MODIFY_EXPR, LOOKUP_NORMAL, 5160 lhs, rhs, make_node (NOP_EXPR), 5161 /*overloaded_p=*/NULL); 5162 if (result == NULL_TREE) 5163 return error_mark_node; 5164 return result; 5165 } 5166 lhstype = olhstype; 5167 } 5168 else 5169 { 5170 /* A binary op has been requested. Combine the old LHS 5171 value with the RHS producing the value we should actually 5172 store into the LHS. */ 5173 5174 my_friendly_assert (!PROMOTES_TO_AGGR_TYPE (lhstype, REFERENCE_TYPE), 5175 978652); 5176 lhs = stabilize_reference (lhs); 5177 newrhs = cp_build_binary_op (modifycode, lhs, rhs); 5178 if (newrhs == error_mark_node) 5179 { 5180 error (" in evaluation of `%Q(%#T, %#T)'", modifycode, 5181 TREE_TYPE (lhs), TREE_TYPE (rhs)); 5182 return error_mark_node; 5183 } 5184 5185 /* Now it looks like a plain assignment. */ 5186 modifycode = NOP_EXPR; 5187 } 5188 my_friendly_assert (TREE_CODE (lhstype) != REFERENCE_TYPE, 20011220); 5189 my_friendly_assert (TREE_CODE (TREE_TYPE (newrhs)) != REFERENCE_TYPE, 5190 20011220); 5191 } 5192 5193 /* Handle a cast used as an "lvalue". 5194 We have already performed any binary operator using the value as cast. 5195 Now convert the result to the cast type of the lhs, 5196 and then true type of the lhs and store it there; 5197 then convert result back to the cast type to be the value 5198 of the assignment. */ 5199 5200 switch (TREE_CODE (lhs)) 5201 { 5202 case NOP_EXPR: 5203 case CONVERT_EXPR: 5204 case FLOAT_EXPR: 5205 case FIX_TRUNC_EXPR: 5206 case FIX_FLOOR_EXPR: 5207 case FIX_ROUND_EXPR: 5208 case FIX_CEIL_EXPR: 5209 { 5210 tree inner_lhs = TREE_OPERAND (lhs, 0); 5211 tree result; 5212 5213 if (TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE 5214 || TREE_CODE (TREE_TYPE (newrhs)) == FUNCTION_TYPE 5215 || TREE_CODE (TREE_TYPE (newrhs)) == METHOD_TYPE 5216 || TREE_CODE (TREE_TYPE (newrhs)) == OFFSET_TYPE) 5217 newrhs = decay_conversion (newrhs); 5218 5219 /* ISO C++ 5.4/1: The result is an lvalue if T is a reference 5220 type, otherwise the result is an rvalue. */ 5221 if (! lvalue_p (lhs)) 5222 pedwarn ("ISO C++ forbids cast to non-reference type used as lvalue"); 5223 5224 result = build_modify_expr (inner_lhs, NOP_EXPR, 5225 cp_convert (TREE_TYPE (inner_lhs), 5226 cp_convert (lhstype, newrhs))); 5227 if (result == error_mark_node) 5228 return result; 5229 return cp_convert (TREE_TYPE (lhs), result); 5230 } 5231 5232 default: 5233 break; 5234 } 5235 5236 /* Now we have handled acceptable kinds of LHS that are not truly lvalues. 5237 Reject anything strange now. */ 5238 5239 if (!lvalue_or_else (lhs, "assignment")) 5240 return error_mark_node; 5241 5242 /* Warn about modifying something that is `const'. Don't warn if 5243 this is initialization. */ 5244 if (modifycode != INIT_EXPR 5245 && (TREE_READONLY (lhs) || CP_TYPE_CONST_P (lhstype) 5246 /* Functions are not modifiable, even though they are 5247 lvalues. */ 5248 || TREE_CODE (TREE_TYPE (lhs)) == FUNCTION_TYPE 5249 || TREE_CODE (TREE_TYPE (lhs)) == METHOD_TYPE 5250 /* If it's an aggregate and any field is const, then it is 5251 effectively const. */ 5252 || (CLASS_TYPE_P (lhstype) 5253 && C_TYPE_FIELDS_READONLY (lhstype)))) 5254 readonly_error (lhs, "assignment", 0); 5255 5256 /* If storing into a structure or union member, it has probably been 5257 given type `int'. Compute the type that would go with the actual 5258 amount of storage the member occupies. */ 5259 5260 if (TREE_CODE (lhs) == COMPONENT_REF 5261 && (TREE_CODE (lhstype) == INTEGER_TYPE 5262 || TREE_CODE (lhstype) == REAL_TYPE 5263 || TREE_CODE (lhstype) == ENUMERAL_TYPE)) 5264 { 5265 lhstype = TREE_TYPE (get_unwidened (lhs, 0)); 5266 5267 /* If storing in a field that is in actuality a short or narrower 5268 than one, we must store in the field in its actual type. */ 5269 5270 if (lhstype != TREE_TYPE (lhs)) 5271 { 5272 /* Avoid warnings converting integral types back into enums for 5273 enum bit fields. */ 5274 if (TREE_CODE (lhstype) == INTEGER_TYPE 5275 && TREE_CODE (olhstype) == ENUMERAL_TYPE) 5276 { 5277 if (TREE_SIDE_EFFECTS (lhs)) 5278 lhs = stabilize_reference (lhs); 5279 olhs = lhs; 5280 } 5281 lhs = copy_node (lhs); 5282 TREE_TYPE (lhs) = lhstype; 5283 } 5284 } 5285 5286 /* Convert new value to destination type. */ 5287 5288 if (TREE_CODE (lhstype) == ARRAY_TYPE) 5289 { 5290 int from_array; 5291 5292 if (!same_or_base_type_p (TYPE_MAIN_VARIANT (lhstype), 5293 TYPE_MAIN_VARIANT (TREE_TYPE (rhs)))) 5294 { 5295 error ("incompatible types in assignment of `%T' to `%T'", 5296 TREE_TYPE (rhs), lhstype); 5297 return error_mark_node; 5298 } 5299 5300 /* Allow array assignment in compiler-generated code. */ 5301 if (! DECL_ARTIFICIAL (current_function_decl)) 5302 pedwarn ("ISO C++ forbids assignment of arrays"); 5303 5304 from_array = TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE 5305 ? 1 + (modifycode != INIT_EXPR): 0; 5306 return build_vec_init (lhs, NULL_TREE, newrhs, from_array); 5307 } 5308 5309 if (modifycode == INIT_EXPR) 5310 newrhs = convert_for_initialization (lhs, lhstype, newrhs, LOOKUP_NORMAL, 5311 "initialization", NULL_TREE, 0); 5312 else 5313 { 5314 /* Avoid warnings on enum bit fields. */ 5315 if (TREE_CODE (olhstype) == ENUMERAL_TYPE 5316 && TREE_CODE (lhstype) == INTEGER_TYPE) 5317 { 5318 newrhs = convert_for_assignment (olhstype, newrhs, "assignment", 5319 NULL_TREE, 0); 5320 newrhs = convert_force (lhstype, newrhs, 0); 5321 } 5322 else 5323 newrhs = convert_for_assignment (lhstype, newrhs, "assignment", 5324 NULL_TREE, 0); 5325 if (TREE_CODE (newrhs) == CALL_EXPR 5326 && TYPE_NEEDS_CONSTRUCTING (lhstype)) 5327 newrhs = build_cplus_new (lhstype, newrhs); 5328 5329 /* Can't initialize directly from a TARGET_EXPR, since that would 5330 cause the lhs to be constructed twice, and possibly result in 5331 accidental self-initialization. So we force the TARGET_EXPR to be 5332 expanded without a target. */ 5333 if (TREE_CODE (newrhs) == TARGET_EXPR) 5334 newrhs = build (COMPOUND_EXPR, TREE_TYPE (newrhs), newrhs, 5335 TREE_OPERAND (newrhs, 0)); 5336 } 5337 5338 if (newrhs == error_mark_node) 5339 return error_mark_node; 5340 5341 result = build (modifycode == NOP_EXPR ? MODIFY_EXPR : INIT_EXPR, 5342 lhstype, lhs, newrhs); 5343 5344 TREE_SIDE_EFFECTS (result) = 1; 5345 5346 /* If we got the LHS in a different type for storing in, 5347 convert the result back to the nominal type of LHS 5348 so that the value we return always has the same type 5349 as the LHS argument. */ 5350 5351 if (olhstype == TREE_TYPE (result)) 5352 return result; 5353 if (olhs) 5354 { 5355 result = build (COMPOUND_EXPR, olhstype, result, olhs); 5356 TREE_NO_UNUSED_WARNING (result) = 1; 5357 return result; 5358 } 5359 return convert_for_assignment (olhstype, result, "assignment", 5360 NULL_TREE, 0); 5361} 5362 5363tree 5364build_x_modify_expr (tree lhs, enum tree_code modifycode, tree rhs) 5365{ 5366 if (processing_template_decl) 5367 return build_min_nt (MODOP_EXPR, lhs, 5368 build_min_nt (modifycode, NULL_TREE, NULL_TREE), rhs); 5369 5370 if (modifycode != NOP_EXPR) 5371 { 5372 tree rval = build_new_op (MODIFY_EXPR, LOOKUP_NORMAL, lhs, rhs, 5373 make_node (modifycode), 5374 /*overloaded_p=*/NULL); 5375 if (rval) 5376 return rval; 5377 } 5378 return build_modify_expr (lhs, modifycode, rhs); 5379} 5380 5381 5382/* Get difference in deltas for different pointer to member function 5383 types. Returns an integer constant of type PTRDIFF_TYPE_NODE. If 5384 the conversion is invalid, the constant is zero. If FORCE is true, 5385 then allow reverse conversions as well. 5386 5387 Note that the naming of FROM and TO is kind of backwards; the return 5388 value is what we add to a TO in order to get a FROM. They are named 5389 this way because we call this function to find out how to convert from 5390 a pointer to member of FROM to a pointer to member of TO. */ 5391 5392static tree 5393get_delta_difference (tree from, tree to, int force) 5394{ 5395 tree binfo; 5396 tree virt_binfo; 5397 base_kind kind; 5398 5399 binfo = lookup_base (to, from, ba_check, &kind); 5400 if (kind == bk_inaccessible || kind == bk_ambig) 5401 { 5402 error (" in pointer to member function conversion"); 5403 goto error; 5404 } 5405 if (!binfo) 5406 { 5407 if (!force) 5408 { 5409 error_not_base_type (from, to); 5410 error (" in pointer to member conversion"); 5411 goto error; 5412 } 5413 binfo = lookup_base (from, to, ba_check, &kind); 5414 if (!binfo) 5415 goto error; 5416 virt_binfo = binfo_from_vbase (binfo); 5417 if (virt_binfo) 5418 { 5419 /* This is a reinterpret cast, we choose to do nothing. */ 5420 warning ("pointer to member cast via virtual base `%T'", 5421 BINFO_TYPE (virt_binfo)); 5422 goto error; 5423 } 5424 return fold (convert_to_integer (ptrdiff_type_node, 5425 size_diffop (size_zero_node, 5426 BINFO_OFFSET (binfo)))); 5427 } 5428 5429 virt_binfo = binfo_from_vbase (binfo); 5430 if (!virt_binfo) 5431 return fold (convert_to_integer (ptrdiff_type_node, BINFO_OFFSET (binfo))); 5432 5433 /* This is a reinterpret cast, we choose to do nothing. */ 5434 if (force) 5435 warning ("pointer to member cast via virtual base `%T'", 5436 BINFO_TYPE (virt_binfo)); 5437 else 5438 error ("pointer to member conversion via virtual base `%T'", 5439 BINFO_TYPE (virt_binfo)); 5440 5441 error: 5442 return fold (convert_to_integer(ptrdiff_type_node, integer_zero_node)); 5443} 5444 5445/* Return a constructor for the pointer-to-member-function TYPE using 5446 the other components as specified. */ 5447 5448tree 5449build_ptrmemfunc1 (tree type, tree delta, tree pfn) 5450{ 5451 tree u = NULL_TREE; 5452 tree delta_field; 5453 tree pfn_field; 5454 5455 /* Pull the FIELD_DECLs out of the type. */ 5456 pfn_field = TYPE_FIELDS (type); 5457 delta_field = TREE_CHAIN (pfn_field); 5458 5459 /* Make sure DELTA has the type we want. */ 5460 delta = convert_and_check (delta_type_node, delta); 5461 5462 /* Finish creating the initializer. */ 5463 u = tree_cons (pfn_field, pfn, 5464 build_tree_list (delta_field, delta)); 5465 u = build_constructor (type, u); 5466 TREE_CONSTANT (u) = TREE_CONSTANT (pfn) && TREE_CONSTANT (delta); 5467 TREE_STATIC (u) = (TREE_CONSTANT (u) 5468 && (initializer_constant_valid_p (pfn, TREE_TYPE (pfn)) 5469 != NULL_TREE) 5470 && (initializer_constant_valid_p (delta, TREE_TYPE (delta)) 5471 != NULL_TREE)); 5472 return u; 5473} 5474 5475/* Build a constructor for a pointer to member function. It can be 5476 used to initialize global variables, local variable, or used 5477 as a value in expressions. TYPE is the POINTER to METHOD_TYPE we 5478 want to be. 5479 5480 If FORCE is nonzero, then force this conversion, even if 5481 we would rather not do it. Usually set when using an explicit 5482 cast. 5483 5484 Return error_mark_node, if something goes wrong. */ 5485 5486tree 5487build_ptrmemfunc (tree type, tree pfn, int force) 5488{ 5489 tree fn; 5490 tree pfn_type; 5491 tree to_type; 5492 5493 if (error_operand_p (pfn)) 5494 return error_mark_node; 5495 5496 pfn_type = TREE_TYPE (pfn); 5497 to_type = build_ptrmemfunc_type (type); 5498 5499 /* Handle multiple conversions of pointer to member functions. */ 5500 if (TYPE_PTRMEMFUNC_P (pfn_type)) 5501 { 5502 tree delta = NULL_TREE; 5503 tree npfn = NULL_TREE; 5504 tree n; 5505 5506 if (!force 5507 && !can_convert_arg (to_type, TREE_TYPE (pfn), pfn)) 5508 error ("invalid conversion to type `%T' from type `%T'", 5509 to_type, pfn_type); 5510 5511 n = get_delta_difference (TYPE_PTRMEMFUNC_OBJECT_TYPE (pfn_type), 5512 TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type), 5513 force); 5514 5515 /* We don't have to do any conversion to convert a 5516 pointer-to-member to its own type. But, we don't want to 5517 just return a PTRMEM_CST if there's an explicit cast; that 5518 cast should make the expression an invalid template argument. */ 5519 if (TREE_CODE (pfn) != PTRMEM_CST) 5520 { 5521 if (same_type_p (to_type, pfn_type)) 5522 return pfn; 5523 else if (integer_zerop (n)) 5524 return build_reinterpret_cast (to_type, pfn); 5525 } 5526 5527 if (TREE_SIDE_EFFECTS (pfn)) 5528 pfn = save_expr (pfn); 5529 5530 /* Obtain the function pointer and the current DELTA. */ 5531 if (TREE_CODE (pfn) == PTRMEM_CST) 5532 expand_ptrmemfunc_cst (pfn, &delta, &npfn); 5533 else 5534 { 5535 npfn = build_ptrmemfunc_access_expr (pfn, pfn_identifier); 5536 delta = build_ptrmemfunc_access_expr (pfn, delta_identifier); 5537 } 5538 5539 /* Just adjust the DELTA field. */ 5540 my_friendly_assert 5541 (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (delta), 5542 ptrdiff_type_node), 5543 20030727); 5544 if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_delta) 5545 n = cp_build_binary_op (LSHIFT_EXPR, n, integer_one_node); 5546 delta = cp_build_binary_op (PLUS_EXPR, delta, n); 5547 return build_ptrmemfunc1 (to_type, delta, npfn); 5548 } 5549 5550 /* Handle null pointer to member function conversions. */ 5551 if (integer_zerop (pfn)) 5552 { 5553 pfn = build_c_cast (type, integer_zero_node); 5554 return build_ptrmemfunc1 (to_type, 5555 integer_zero_node, 5556 pfn); 5557 } 5558 5559 if (type_unknown_p (pfn)) 5560 return instantiate_type (type, pfn, tf_error | tf_warning); 5561 5562 fn = TREE_OPERAND (pfn, 0); 5563 my_friendly_assert (TREE_CODE (fn) == FUNCTION_DECL, 0); 5564 return make_ptrmem_cst (to_type, fn); 5565} 5566 5567/* Return the DELTA, IDX, PFN, and DELTA2 values for the PTRMEM_CST 5568 given by CST. 5569 5570 ??? There is no consistency as to the types returned for the above 5571 values. Some code acts as if its a sizetype and some as if its 5572 integer_type_node. */ 5573 5574void 5575expand_ptrmemfunc_cst (tree cst, tree *delta, tree *pfn) 5576{ 5577 tree type = TREE_TYPE (cst); 5578 tree fn = PTRMEM_CST_MEMBER (cst); 5579 tree ptr_class, fn_class; 5580 5581 my_friendly_assert (TREE_CODE (fn) == FUNCTION_DECL, 0); 5582 5583 /* The class that the function belongs to. */ 5584 fn_class = DECL_CONTEXT (fn); 5585 5586 /* The class that we're creating a pointer to member of. */ 5587 ptr_class = TYPE_PTRMEMFUNC_OBJECT_TYPE (type); 5588 5589 /* First, calculate the adjustment to the function's class. */ 5590 *delta = get_delta_difference (fn_class, ptr_class, /*force=*/0); 5591 5592 if (!DECL_VIRTUAL_P (fn)) 5593 *pfn = convert (TYPE_PTRMEMFUNC_FN_TYPE (type), build_addr_func (fn)); 5594 else 5595 { 5596 /* If we're dealing with a virtual function, we have to adjust 'this' 5597 again, to point to the base which provides the vtable entry for 5598 fn; the call will do the opposite adjustment. */ 5599 tree orig_class = DECL_CONTEXT (fn); 5600 tree binfo = binfo_or_else (orig_class, fn_class); 5601 *delta = fold (build (PLUS_EXPR, TREE_TYPE (*delta), 5602 *delta, BINFO_OFFSET (binfo))); 5603 5604 /* We set PFN to the vtable offset at which the function can be 5605 found, plus one (unless ptrmemfunc_vbit_in_delta, in which 5606 case delta is shifted left, and then incremented). */ 5607 *pfn = DECL_VINDEX (fn); 5608 *pfn = fold (build (MULT_EXPR, integer_type_node, *pfn, 5609 TYPE_SIZE_UNIT (vtable_entry_type))); 5610 5611 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION) 5612 { 5613 case ptrmemfunc_vbit_in_pfn: 5614 *pfn = fold (build (PLUS_EXPR, integer_type_node, *pfn, 5615 integer_one_node)); 5616 break; 5617 5618 case ptrmemfunc_vbit_in_delta: 5619 *delta = fold (build (LSHIFT_EXPR, TREE_TYPE (*delta), 5620 *delta, integer_one_node)); 5621 *delta = fold (build (PLUS_EXPR, TREE_TYPE (*delta), 5622 *delta, integer_one_node)); 5623 break; 5624 5625 default: 5626 abort (); 5627 } 5628 5629 *pfn = fold (build1 (NOP_EXPR, TYPE_PTRMEMFUNC_FN_TYPE (type), 5630 *pfn)); 5631 } 5632} 5633 5634/* Return an expression for PFN from the pointer-to-member function 5635 given by T. */ 5636 5637tree 5638pfn_from_ptrmemfunc (tree t) 5639{ 5640 if (TREE_CODE (t) == PTRMEM_CST) 5641 { 5642 tree delta; 5643 tree pfn; 5644 5645 expand_ptrmemfunc_cst (t, &delta, &pfn); 5646 if (pfn) 5647 return pfn; 5648 } 5649 5650 return build_ptrmemfunc_access_expr (t, pfn_identifier); 5651} 5652 5653/* Expression EXPR is about to be implicitly converted to TYPE. Warn 5654 if this is a potentially dangerous thing to do. Returns a possibly 5655 marked EXPR. */ 5656 5657tree 5658dubious_conversion_warnings (tree type, tree expr, 5659 const char *errtype, tree fndecl, int parmnum) 5660{ 5661 type = non_reference (type); 5662 5663 /* Issue warnings about peculiar, but valid, uses of NULL. */ 5664 if (ARITHMETIC_TYPE_P (type) && expr == null_node) 5665 { 5666 if (fndecl) 5667 warning ("passing NULL used for non-pointer %s %P of `%D'", 5668 errtype, parmnum, fndecl); 5669 else 5670 warning ("%s to non-pointer type `%T' from NULL", errtype, type); 5671 } 5672 5673 /* Warn about assigning a floating-point type to an integer type. */ 5674 if (TREE_CODE (TREE_TYPE (expr)) == REAL_TYPE 5675 && TREE_CODE (type) == INTEGER_TYPE) 5676 { 5677 if (fndecl) 5678 warning ("passing `%T' for %s %P of `%D'", 5679 TREE_TYPE (expr), errtype, parmnum, fndecl); 5680 else 5681 warning ("%s to `%T' from `%T'", errtype, type, TREE_TYPE (expr)); 5682 } 5683 /* And warn about assigning a negative value to an unsigned 5684 variable. */ 5685 else if (TREE_UNSIGNED (type) && TREE_CODE (type) != BOOLEAN_TYPE) 5686 { 5687 if (TREE_CODE (expr) == INTEGER_CST 5688 && TREE_NEGATED_INT (expr)) 5689 { 5690 if (fndecl) 5691 warning ("passing negative value `%E' for %s %P of `%D'", 5692 expr, errtype, parmnum, fndecl); 5693 else 5694 warning ("%s of negative value `%E' to `%T'", 5695 errtype, expr, type); 5696 } 5697 5698 overflow_warning (expr); 5699 5700 if (TREE_CONSTANT (expr)) 5701 expr = fold (expr); 5702 } 5703 return expr; 5704} 5705 5706/* Convert value RHS to type TYPE as preparation for an assignment to 5707 an lvalue of type TYPE. ERRTYPE is a string to use in error 5708 messages: "assignment", "return", etc. If FNDECL is non-NULL, we 5709 are doing the conversion in order to pass the PARMNUMth argument of 5710 FNDECL. */ 5711 5712static tree 5713convert_for_assignment (tree type, tree rhs, 5714 const char *errtype, tree fndecl, int parmnum) 5715{ 5716 tree rhstype; 5717 enum tree_code coder; 5718 5719 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */ 5720 if (TREE_CODE (rhs) == NON_LVALUE_EXPR) 5721 rhs = TREE_OPERAND (rhs, 0); 5722 5723 rhstype = TREE_TYPE (rhs); 5724 coder = TREE_CODE (rhstype); 5725 5726 if (TREE_CODE (type) == VECTOR_TYPE && coder == VECTOR_TYPE 5727 && ((*targetm.vector_opaque_p) (type) 5728 || (*targetm.vector_opaque_p) (rhstype))) 5729 return convert (type, rhs); 5730 5731 if (rhs == error_mark_node || rhstype == error_mark_node) 5732 return error_mark_node; 5733 if (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node) 5734 return error_mark_node; 5735 5736 /* The RHS of an assignment cannot have void type. */ 5737 if (coder == VOID_TYPE) 5738 { 5739 error ("void value not ignored as it ought to be"); 5740 return error_mark_node; 5741 } 5742 5743 /* Simplify the RHS if possible. */ 5744 if (TREE_CODE (rhs) == CONST_DECL) 5745 rhs = DECL_INITIAL (rhs); 5746 5747 /* We do not use decl_constant_value here because of this case: 5748 5749 const char* const s = "s"; 5750 5751 The conversion rules for a string literal are more lax than for a 5752 variable; in particular, a string literal can be converted to a 5753 "char *" but the variable "s" cannot be converted in the same 5754 way. If the conversion is allowed, the optimization should be 5755 performed while creating the converted expression. */ 5756 5757 /* [expr.ass] 5758 5759 The expression is implicitly converted (clause _conv_) to the 5760 cv-unqualified type of the left operand. 5761 5762 We allow bad conversions here because by the time we get to this point 5763 we are committed to doing the conversion. If we end up doing a bad 5764 conversion, convert_like will complain. */ 5765 if (!can_convert_arg_bad (type, rhstype, rhs)) 5766 { 5767 /* When -Wno-pmf-conversions is use, we just silently allow 5768 conversions from pointers-to-members to plain pointers. If 5769 the conversion doesn't work, cp_convert will complain. */ 5770 if (!warn_pmf2ptr 5771 && TYPE_PTR_P (type) 5772 && TYPE_PTRMEMFUNC_P (rhstype)) 5773 rhs = cp_convert (strip_top_quals (type), rhs); 5774 else 5775 { 5776 /* If the right-hand side has unknown type, then it is an 5777 overloaded function. Call instantiate_type to get error 5778 messages. */ 5779 if (rhstype == unknown_type_node) 5780 instantiate_type (type, rhs, tf_error | tf_warning); 5781 else if (fndecl) 5782 error ("cannot convert `%T' to `%T' for argument `%P' to `%D'", 5783 rhstype, type, parmnum, fndecl); 5784 else 5785 error ("cannot convert `%T' to `%T' in %s", rhstype, type, 5786 errtype); 5787 return error_mark_node; 5788 } 5789 } 5790 return perform_implicit_conversion (strip_top_quals (type), rhs); 5791} 5792 5793/* Convert RHS to be of type TYPE. 5794 If EXP is nonzero, it is the target of the initialization. 5795 ERRTYPE is a string to use in error messages. 5796 5797 Two major differences between the behavior of 5798 `convert_for_assignment' and `convert_for_initialization' 5799 are that references are bashed in the former, while 5800 copied in the latter, and aggregates are assigned in 5801 the former (operator=) while initialized in the 5802 latter (X(X&)). 5803 5804 If using constructor make sure no conversion operator exists, if one does 5805 exist, an ambiguity exists. 5806 5807 If flags doesn't include LOOKUP_COMPLAIN, don't complain about anything. */ 5808 5809tree 5810convert_for_initialization (tree exp, tree type, tree rhs, int flags, 5811 const char *errtype, tree fndecl, int parmnum) 5812{ 5813 enum tree_code codel = TREE_CODE (type); 5814 tree rhstype; 5815 enum tree_code coder; 5816 5817 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue. 5818 Strip such NOP_EXPRs, since RHS is used in non-lvalue context. */ 5819 if (TREE_CODE (rhs) == NOP_EXPR 5820 && TREE_TYPE (rhs) == TREE_TYPE (TREE_OPERAND (rhs, 0)) 5821 && codel != REFERENCE_TYPE) 5822 rhs = TREE_OPERAND (rhs, 0); 5823 5824 if (rhs == error_mark_node 5825 || (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node)) 5826 return error_mark_node; 5827 5828 if (TREE_CODE (TREE_TYPE (rhs)) == REFERENCE_TYPE) 5829 rhs = convert_from_reference (rhs); 5830 5831 if ((TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE 5832 && TREE_CODE (type) != ARRAY_TYPE 5833 && (TREE_CODE (type) != REFERENCE_TYPE 5834 || TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE)) 5835 || (TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE 5836 && (TREE_CODE (type) != REFERENCE_TYPE 5837 || TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)) 5838 || TREE_CODE (TREE_TYPE (rhs)) == METHOD_TYPE) 5839 rhs = decay_conversion (rhs); 5840 5841 rhstype = TREE_TYPE (rhs); 5842 coder = TREE_CODE (rhstype); 5843 5844 if (coder == ERROR_MARK) 5845 return error_mark_node; 5846 5847 /* We accept references to incomplete types, so we can 5848 return here before checking if RHS is of complete type. */ 5849 5850 if (codel == REFERENCE_TYPE) 5851 { 5852 /* This should eventually happen in convert_arguments. */ 5853 int savew = 0, savee = 0; 5854 5855 if (fndecl) 5856 savew = warningcount, savee = errorcount; 5857 rhs = initialize_reference (type, rhs, /*decl=*/NULL_TREE, 5858 /*cleanup=*/NULL); 5859 if (fndecl) 5860 { 5861 if (warningcount > savew) 5862 cp_warning_at ("in passing argument %P of `%+D'", parmnum, fndecl); 5863 else if (errorcount > savee) 5864 cp_error_at ("in passing argument %P of `%+D'", parmnum, fndecl); 5865 } 5866 return rhs; 5867 } 5868 5869 if (exp != 0) 5870 exp = require_complete_type (exp); 5871 if (exp == error_mark_node) 5872 return error_mark_node; 5873 5874 rhstype = non_reference (rhstype); 5875 5876 type = complete_type (type); 5877 5878 if (IS_AGGR_TYPE (type)) 5879 return ocp_convert (type, rhs, CONV_IMPLICIT|CONV_FORCE_TEMP, flags); 5880 5881 return convert_for_assignment (type, rhs, errtype, fndecl, parmnum); 5882} 5883 5884/* Expand an ASM statement with operands, handling output operands 5885 that are not variables or INDIRECT_REFS by transforming such 5886 cases into cases that expand_asm_operands can handle. 5887 5888 Arguments are same as for expand_asm_operands. 5889 5890 We don't do default conversions on all inputs, because it can screw 5891 up operands that are expected to be in memory. */ 5892 5893void 5894c_expand_asm_operands (tree string, tree outputs, tree inputs, tree clobbers, 5895 int vol, location_t locus) 5896{ 5897 int noutputs = list_length (outputs); 5898 int i; 5899 /* o[I] is the place that output number I should be written. */ 5900 tree *o = alloca (noutputs * sizeof (tree)); 5901 tree tail; 5902 5903 /* Record the contents of OUTPUTS before it is modified. */ 5904 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++) 5905 o[i] = TREE_VALUE (tail); 5906 5907 /* Generate the ASM_OPERANDS insn; 5908 store into the TREE_VALUEs of OUTPUTS some trees for 5909 where the values were actually stored. */ 5910 expand_asm_operands (string, outputs, inputs, clobbers, vol, locus); 5911 5912 /* Copy all the intermediate outputs into the specified outputs. */ 5913 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++) 5914 { 5915 if (o[i] != TREE_VALUE (tail)) 5916 { 5917 expand_expr (build_modify_expr (o[i], NOP_EXPR, TREE_VALUE (tail)), 5918 const0_rtx, VOIDmode, EXPAND_NORMAL); 5919 free_temp_slots (); 5920 5921 /* Restore the original value so that it's correct the next 5922 time we expand this function. */ 5923 TREE_VALUE (tail) = o[i]; 5924 } 5925 /* Detect modification of read-only values. 5926 (Otherwise done by build_modify_expr.) */ 5927 else 5928 { 5929 tree type = TREE_TYPE (o[i]); 5930 if (type != error_mark_node 5931 && (CP_TYPE_CONST_P (type) 5932 || (CLASS_TYPE_P (type) && C_TYPE_FIELDS_READONLY (type)))) 5933 readonly_error (o[i], "modification by `asm'", 1); 5934 } 5935 } 5936 5937 /* Those MODIFY_EXPRs could do autoincrements. */ 5938 emit_queue (); 5939} 5940 5941/* If RETVAL is the address of, or a reference to, a local variable or 5942 temporary give an appropriate warning. */ 5943 5944static void 5945maybe_warn_about_returning_address_of_local (tree retval) 5946{ 5947 tree valtype = TREE_TYPE (DECL_RESULT (current_function_decl)); 5948 tree whats_returned = retval; 5949 5950 for (;;) 5951 { 5952 if (TREE_CODE (whats_returned) == COMPOUND_EXPR) 5953 whats_returned = TREE_OPERAND (whats_returned, 1); 5954 else if (TREE_CODE (whats_returned) == CONVERT_EXPR 5955 || TREE_CODE (whats_returned) == NON_LVALUE_EXPR 5956 || TREE_CODE (whats_returned) == NOP_EXPR) 5957 whats_returned = TREE_OPERAND (whats_returned, 0); 5958 else 5959 break; 5960 } 5961 5962 if (TREE_CODE (whats_returned) != ADDR_EXPR) 5963 return; 5964 whats_returned = TREE_OPERAND (whats_returned, 0); 5965 5966 if (TREE_CODE (valtype) == REFERENCE_TYPE) 5967 { 5968 if (TREE_CODE (whats_returned) == AGGR_INIT_EXPR 5969 || TREE_CODE (whats_returned) == TARGET_EXPR) 5970 { 5971 warning ("returning reference to temporary"); 5972 return; 5973 } 5974 if (TREE_CODE (whats_returned) == VAR_DECL 5975 && DECL_NAME (whats_returned) 5976 && TEMP_NAME_P (DECL_NAME (whats_returned))) 5977 { 5978 warning ("reference to non-lvalue returned"); 5979 return; 5980 } 5981 } 5982 5983 if (TREE_CODE (whats_returned) == VAR_DECL 5984 && DECL_NAME (whats_returned) 5985 && DECL_FUNCTION_SCOPE_P (whats_returned) 5986 && !(TREE_STATIC (whats_returned) 5987 || TREE_PUBLIC (whats_returned))) 5988 { 5989 if (TREE_CODE (valtype) == REFERENCE_TYPE) 5990 cp_warning_at ("reference to local variable `%D' returned", 5991 whats_returned); 5992 else 5993 cp_warning_at ("address of local variable `%D' returned", 5994 whats_returned); 5995 return; 5996 } 5997} 5998 5999/* Check that returning RETVAL from the current function is valid. 6000 Return an expression explicitly showing all conversions required to 6001 change RETVAL into the function return type, and to assign it to 6002 the DECL_RESULT for the function. */ 6003 6004tree 6005check_return_expr (tree retval) 6006{ 6007 tree result; 6008 /* The type actually returned by the function, after any 6009 promotions. */ 6010 tree valtype; 6011 int fn_returns_value_p; 6012 6013 /* A `volatile' function is one that isn't supposed to return, ever. 6014 (This is a G++ extension, used to get better code for functions 6015 that call the `volatile' function.) */ 6016 if (TREE_THIS_VOLATILE (current_function_decl)) 6017 warning ("function declared `noreturn' has a `return' statement"); 6018 6019 /* Check for various simple errors. */ 6020 if (DECL_DESTRUCTOR_P (current_function_decl)) 6021 { 6022 if (retval) 6023 error ("returning a value from a destructor"); 6024 return NULL_TREE; 6025 } 6026 else if (DECL_CONSTRUCTOR_P (current_function_decl)) 6027 { 6028 if (in_function_try_handler) 6029 /* If a return statement appears in a handler of the 6030 function-try-block of a constructor, the program is ill-formed. */ 6031 error ("cannot return from a handler of a function-try-block of a constructor"); 6032 else if (retval) 6033 /* You can't return a value from a constructor. */ 6034 error ("returning a value from a constructor"); 6035 return NULL_TREE; 6036 } 6037 6038 if (processing_template_decl) 6039 { 6040 current_function_returns_value = 1; 6041 return retval; 6042 } 6043 6044 /* When no explicit return-value is given in a function with a named 6045 return value, the named return value is used. */ 6046 result = DECL_RESULT (current_function_decl); 6047 valtype = TREE_TYPE (result); 6048 my_friendly_assert (valtype != NULL_TREE, 19990924); 6049 fn_returns_value_p = !VOID_TYPE_P (valtype); 6050 if (!retval && DECL_NAME (result) && fn_returns_value_p) 6051 retval = result; 6052 6053 /* Check for a return statement with no return value in a function 6054 that's supposed to return a value. */ 6055 if (!retval && fn_returns_value_p) 6056 { 6057 pedwarn ("return-statement with no value, in function returning '%T'", 6058 valtype); 6059 /* Clear this, so finish_function won't say that we reach the 6060 end of a non-void function (which we don't, we gave a 6061 return!). */ 6062 current_function_returns_null = 0; 6063 } 6064 /* Check for a return statement with a value in a function that 6065 isn't supposed to return a value. */ 6066 else if (retval && !fn_returns_value_p) 6067 { 6068 if (VOID_TYPE_P (TREE_TYPE (retval))) 6069 /* You can return a `void' value from a function of `void' 6070 type. In that case, we have to evaluate the expression for 6071 its side-effects. */ 6072 finish_expr_stmt (retval); 6073 else 6074 pedwarn ("return-statement with a value, in function " 6075 "returning 'void'"); 6076 6077 current_function_returns_null = 1; 6078 6079 /* There's really no value to return, after all. */ 6080 return NULL_TREE; 6081 } 6082 else if (!retval) 6083 /* Remember that this function can sometimes return without a 6084 value. */ 6085 current_function_returns_null = 1; 6086 else 6087 /* Remember that this function did return a value. */ 6088 current_function_returns_value = 1; 6089 6090 /* Check for erroneous operands -- but after giving ourselves a 6091 chance to provide an error about returning a value from a void 6092 function. */ 6093 if (error_operand_p (retval)) 6094 { 6095 current_function_return_value = error_mark_node; 6096 return error_mark_node; 6097 } 6098 6099 /* Only operator new(...) throw(), can return NULL [expr.new/13]. */ 6100 if ((DECL_OVERLOADED_OPERATOR_P (current_function_decl) == NEW_EXPR 6101 || DECL_OVERLOADED_OPERATOR_P (current_function_decl) == VEC_NEW_EXPR) 6102 && !TYPE_NOTHROW_P (TREE_TYPE (current_function_decl)) 6103 && ! flag_check_new 6104 && null_ptr_cst_p (retval)) 6105 warning ("`operator new' must not return NULL unless it is declared `throw()' (or -fcheck-new is in effect)"); 6106 6107 /* Effective C++ rule 15. See also start_function. */ 6108 if (warn_ecpp 6109 && DECL_NAME (current_function_decl) == ansi_assopname(NOP_EXPR) 6110 && retval != current_class_ref) 6111 warning ("`operator=' should return a reference to `*this'"); 6112 6113 /* The fabled Named Return Value optimization, as per [class.copy]/15: 6114 6115 [...] For a function with a class return type, if the expression 6116 in the return statement is the name of a local object, and the cv- 6117 unqualified type of the local object is the same as the function 6118 return type, an implementation is permitted to omit creating the tem- 6119 porary object to hold the function return value [...] 6120 6121 So, if this is a value-returning function that always returns the same 6122 local variable, remember it. 6123 6124 It might be nice to be more flexible, and choose the first suitable 6125 variable even if the function sometimes returns something else, but 6126 then we run the risk of clobbering the variable we chose if the other 6127 returned expression uses the chosen variable somehow. And people expect 6128 this restriction, anyway. (jason 2000-11-19) 6129 6130 See finish_function, cxx_expand_function_start, and 6131 cp_copy_res_decl_for_inlining for other pieces of this 6132 optimization. */ 6133 6134 if (fn_returns_value_p && flag_elide_constructors) 6135 { 6136 if (retval != NULL_TREE 6137 && (current_function_return_value == NULL_TREE 6138 || current_function_return_value == retval) 6139 && TREE_CODE (retval) == VAR_DECL 6140 && DECL_CONTEXT (retval) == current_function_decl 6141 && ! TREE_STATIC (retval) 6142 && (DECL_ALIGN (retval) 6143 >= DECL_ALIGN (DECL_RESULT (current_function_decl))) 6144 && same_type_p ((TYPE_MAIN_VARIANT 6145 (TREE_TYPE (retval))), 6146 (TYPE_MAIN_VARIANT 6147 (TREE_TYPE (TREE_TYPE (current_function_decl)))))) 6148 current_function_return_value = retval; 6149 else 6150 current_function_return_value = error_mark_node; 6151 } 6152 6153 /* We don't need to do any conversions when there's nothing being 6154 returned. */ 6155 if (!retval) 6156 return NULL_TREE; 6157 6158 /* Do any required conversions. */ 6159 if (retval == result || DECL_CONSTRUCTOR_P (current_function_decl)) 6160 /* No conversions are required. */ 6161 ; 6162 else 6163 { 6164 /* The type the function is declared to return. */ 6165 tree functype = TREE_TYPE (TREE_TYPE (current_function_decl)); 6166 6167 /* First convert the value to the function's return type, then 6168 to the type of return value's location to handle the 6169 case that functype is smaller than the valtype. */ 6170 retval = convert_for_initialization 6171 (NULL_TREE, functype, retval, LOOKUP_NORMAL|LOOKUP_ONLYCONVERTING, 6172 "return", NULL_TREE, 0); 6173 retval = convert (valtype, retval); 6174 6175 /* If the conversion failed, treat this just like `return;'. */ 6176 if (retval == error_mark_node) 6177 return retval; 6178 /* We can't initialize a register from a AGGR_INIT_EXPR. */ 6179 else if (! current_function_returns_struct 6180 && TREE_CODE (retval) == TARGET_EXPR 6181 && TREE_CODE (TREE_OPERAND (retval, 1)) == AGGR_INIT_EXPR) 6182 retval = build (COMPOUND_EXPR, TREE_TYPE (retval), retval, 6183 TREE_OPERAND (retval, 0)); 6184 else 6185 maybe_warn_about_returning_address_of_local (retval); 6186 } 6187 6188 /* Actually copy the value returned into the appropriate location. */ 6189 if (retval && retval != result) 6190 retval = build (INIT_EXPR, TREE_TYPE (result), result, retval); 6191 6192 return retval; 6193} 6194 6195 6196/* Returns nonzero if the pointer-type FROM can be converted to the 6197 pointer-type TO via a qualification conversion. If CONSTP is -1, 6198 then we return nonzero if the pointers are similar, and the 6199 cv-qualification signature of FROM is a proper subset of that of TO. 6200 6201 If CONSTP is positive, then all outer pointers have been 6202 const-qualified. */ 6203 6204static int 6205comp_ptr_ttypes_real (tree to, tree from, int constp) 6206{ 6207 bool to_more_cv_qualified = false; 6208 6209 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from)) 6210 { 6211 if (TREE_CODE (to) != TREE_CODE (from)) 6212 return 0; 6213 6214 if (TREE_CODE (from) == OFFSET_TYPE 6215 && !same_type_p (TYPE_OFFSET_BASETYPE (from), 6216 TYPE_OFFSET_BASETYPE (to))) 6217 return 0; 6218 6219 /* Const and volatile mean something different for function types, 6220 so the usual checks are not appropriate. */ 6221 if (TREE_CODE (to) != FUNCTION_TYPE && TREE_CODE (to) != METHOD_TYPE) 6222 { 6223 if (!at_least_as_qualified_p (to, from)) 6224 return 0; 6225 6226 if (!at_least_as_qualified_p (from, to)) 6227 { 6228 if (constp == 0) 6229 return 0; 6230 to_more_cv_qualified = true; 6231 } 6232 6233 if (constp > 0) 6234 constp &= TYPE_READONLY (to); 6235 } 6236 6237 if (TREE_CODE (to) != POINTER_TYPE && !TYPE_PTRMEM_P (to)) 6238 return ((constp >= 0 || to_more_cv_qualified) 6239 && same_type_ignoring_top_level_qualifiers_p (to, from)); 6240 } 6241} 6242 6243/* When comparing, say, char ** to char const **, this function takes 6244 the 'char *' and 'char const *'. Do not pass non-pointer/reference 6245 types to this function. */ 6246 6247int 6248comp_ptr_ttypes (tree to, tree from) 6249{ 6250 return comp_ptr_ttypes_real (to, from, 1); 6251} 6252 6253/* Returns 1 if to and from are (possibly multi-level) pointers to the same 6254 type or inheritance-related types, regardless of cv-quals. */ 6255 6256int 6257ptr_reasonably_similar (tree to, tree from) 6258{ 6259 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from)) 6260 { 6261 /* Any target type is similar enough to void. */ 6262 if (TREE_CODE (to) == VOID_TYPE 6263 || TREE_CODE (from) == VOID_TYPE) 6264 return 1; 6265 6266 if (TREE_CODE (to) != TREE_CODE (from)) 6267 return 0; 6268 6269 if (TREE_CODE (from) == OFFSET_TYPE 6270 && comptypes (TYPE_OFFSET_BASETYPE (to), 6271 TYPE_OFFSET_BASETYPE (from), 6272 COMPARE_BASE | COMPARE_DERIVED)) 6273 continue; 6274 6275 if (TREE_CODE (to) == INTEGER_TYPE 6276 && TYPE_PRECISION (to) == TYPE_PRECISION (from)) 6277 return 1; 6278 6279 if (TREE_CODE (to) == FUNCTION_TYPE) 6280 return 1; 6281 6282 if (TREE_CODE (to) != POINTER_TYPE) 6283 return comptypes 6284 (TYPE_MAIN_VARIANT (to), TYPE_MAIN_VARIANT (from), 6285 COMPARE_BASE | COMPARE_DERIVED); 6286 } 6287} 6288 6289/* Like comp_ptr_ttypes, for const_cast. */ 6290 6291static int 6292comp_ptr_ttypes_const (tree to, tree from) 6293{ 6294 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from)) 6295 { 6296 if (TREE_CODE (to) != TREE_CODE (from)) 6297 return 0; 6298 6299 if (TREE_CODE (from) == OFFSET_TYPE 6300 && same_type_p (TYPE_OFFSET_BASETYPE (from), 6301 TYPE_OFFSET_BASETYPE (to))) 6302 continue; 6303 6304 if (TREE_CODE (to) != POINTER_TYPE) 6305 return same_type_ignoring_top_level_qualifiers_p (to, from); 6306 } 6307} 6308 6309/* Returns the type qualifiers for this type, including the qualifiers on the 6310 elements for an array type. */ 6311 6312int 6313cp_type_quals (tree type) 6314{ 6315 type = strip_array_types (type); 6316 if (type == error_mark_node) 6317 return TYPE_UNQUALIFIED; 6318 return TYPE_QUALS (type); 6319} 6320 6321/* Returns nonzero if the TYPE contains a mutable member. */ 6322 6323bool 6324cp_has_mutable_p (tree type) 6325{ 6326 type = strip_array_types (type); 6327 6328 return CLASS_TYPE_P (type) && CLASSTYPE_HAS_MUTABLE (type); 6329} 6330 6331/* Subroutine of casts_away_constness. Make T1 and T2 point at 6332 exemplar types such that casting T1 to T2 is casting away castness 6333 if and only if there is no implicit conversion from T1 to T2. */ 6334 6335static void 6336casts_away_constness_r (tree *t1, tree *t2) 6337{ 6338 int quals1; 6339 int quals2; 6340 6341 /* [expr.const.cast] 6342 6343 For multi-level pointer to members and multi-level mixed pointers 6344 and pointers to members (conv.qual), the "member" aspect of a 6345 pointer to member level is ignored when determining if a const 6346 cv-qualifier has been cast away. */ 6347 /* [expr.const.cast] 6348 6349 For two pointer types: 6350 6351 X1 is T1cv1,1 * ... cv1,N * where T1 is not a pointer type 6352 X2 is T2cv2,1 * ... cv2,M * where T2 is not a pointer type 6353 K is min(N,M) 6354 6355 casting from X1 to X2 casts away constness if, for a non-pointer 6356 type T there does not exist an implicit conversion (clause 6357 _conv_) from: 6358 6359 Tcv1,(N-K+1) * cv1,(N-K+2) * ... cv1,N * 6360 6361 to 6362 6363 Tcv2,(M-K+1) * cv2,(M-K+2) * ... cv2,M *. */ 6364 if ((!TYPE_PTR_P (*t1) && !TYPE_PTRMEM_P (*t1)) 6365 || (!TYPE_PTR_P (*t2) && !TYPE_PTRMEM_P (*t2))) 6366 { 6367 *t1 = cp_build_qualified_type (void_type_node, 6368 cp_type_quals (*t1)); 6369 *t2 = cp_build_qualified_type (void_type_node, 6370 cp_type_quals (*t2)); 6371 return; 6372 } 6373 6374 quals1 = cp_type_quals (*t1); 6375 quals2 = cp_type_quals (*t2); 6376 6377 if (TYPE_PTRMEM_P (*t1)) 6378 *t1 = TYPE_PTRMEM_POINTED_TO_TYPE (*t1); 6379 else 6380 *t1 = TREE_TYPE (*t1); 6381 if (TYPE_PTRMEM_P (*t2)) 6382 *t2 = TYPE_PTRMEM_POINTED_TO_TYPE (*t2); 6383 else 6384 *t2 = TREE_TYPE (*t2); 6385 6386 casts_away_constness_r (t1, t2); 6387 *t1 = build_pointer_type (*t1); 6388 *t2 = build_pointer_type (*t2); 6389 *t1 = cp_build_qualified_type (*t1, quals1); 6390 *t2 = cp_build_qualified_type (*t2, quals2); 6391} 6392 6393/* Returns nonzero if casting from TYPE1 to TYPE2 casts away 6394 constness. */ 6395 6396static bool 6397casts_away_constness (tree t1, tree t2) 6398{ 6399 if (TREE_CODE (t2) == REFERENCE_TYPE) 6400 { 6401 /* [expr.const.cast] 6402 6403 Casting from an lvalue of type T1 to an lvalue of type T2 6404 using a reference cast casts away constness if a cast from an 6405 rvalue of type "pointer to T1" to the type "pointer to T2" 6406 casts away constness. */ 6407 t1 = (TREE_CODE (t1) == REFERENCE_TYPE ? TREE_TYPE (t1) : t1); 6408 return casts_away_constness (build_pointer_type (t1), 6409 build_pointer_type (TREE_TYPE (t2))); 6410 } 6411 6412 if (TYPE_PTRMEM_P (t1) && TYPE_PTRMEM_P (t2)) 6413 /* [expr.const.cast] 6414 6415 Casting from an rvalue of type "pointer to data member of X 6416 of type T1" to the type "pointer to data member of Y of type 6417 T2" casts away constness if a cast from an rvalue of type 6418 "pointer to T1" to the type "pointer to T2" casts away 6419 constness. */ 6420 return casts_away_constness 6421 (build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t1)), 6422 build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t2))); 6423 6424 /* Casting away constness is only something that makes sense for 6425 pointer or reference types. */ 6426 if (TREE_CODE (t1) != POINTER_TYPE 6427 || TREE_CODE (t2) != POINTER_TYPE) 6428 return false; 6429 6430 /* Top-level qualifiers don't matter. */ 6431 t1 = TYPE_MAIN_VARIANT (t1); 6432 t2 = TYPE_MAIN_VARIANT (t2); 6433 casts_away_constness_r (&t1, &t2); 6434 if (!can_convert (t2, t1)) 6435 return true; 6436 6437 return false; 6438} 6439 6440/* If T is a REFERENCE_TYPE return the type to which T refers. 6441 Otherwise, return T itself. */ 6442 6443tree 6444non_reference (tree t) 6445{ 6446 if (TREE_CODE (t) == REFERENCE_TYPE) 6447 t = TREE_TYPE (t); 6448 return t; 6449} 6450