call.c revision 119256
1/* Functions related to invoking methods and overloaded functions. 2 Copyright (C) 1987, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 2003, 3 1999, 2000, 2001, 2002, 2003 Free Software Foundation, Inc. 4 Contributed by Michael Tiemann (tiemann@cygnus.com) and 5 modified by Brendan Kehoe (brendan@cygnus.com). 6 7This file is part of GNU CC. 8 9GNU CC is free software; you can redistribute it and/or modify 10it under the terms of the GNU General Public License as published by 11the Free Software Foundation; either version 2, or (at your option) 12any later version. 13 14GNU CC is distributed in the hope that it will be useful, 15but WITHOUT ANY WARRANTY; without even the implied warranty of 16MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17GNU General Public License for more details. 18 19You should have received a copy of the GNU General Public License 20along with GNU CC; see the file COPYING. If not, write to 21the Free Software Foundation, 59 Temple Place - Suite 330, 22Boston, MA 02111-1307, USA. */ 23 24 25/* High-level class interface. */ 26 27#include "config.h" 28#include "system.h" 29#include "tree.h" 30#include "cp-tree.h" 31#include "output.h" 32#include "flags.h" 33#include "rtl.h" 34#include "toplev.h" 35#include "expr.h" 36#include "ggc.h" 37#include "diagnostic.h" 38 39extern int inhibit_warnings; 40 41static tree build_field_call PARAMS ((tree, tree, tree)); 42static struct z_candidate * tourney PARAMS ((struct z_candidate *)); 43static int equal_functions PARAMS ((tree, tree)); 44static int joust PARAMS ((struct z_candidate *, struct z_candidate *, int)); 45static int compare_ics PARAMS ((tree, tree)); 46static tree build_over_call PARAMS ((struct z_candidate *, tree, int)); 47static tree build_java_interface_fn_ref PARAMS ((tree, tree)); 48#define convert_like(CONV, EXPR) \ 49 convert_like_real ((CONV), (EXPR), NULL_TREE, 0, 0, \ 50 /*issue_conversion_warnings=*/true) 51#define convert_like_with_context(CONV, EXPR, FN, ARGNO) \ 52 convert_like_real ((CONV), (EXPR), (FN), (ARGNO), 0, \ 53 /*issue_conversion_warnings=*/true) 54static tree convert_like_real (tree, tree, tree, int, int, bool); 55static void op_error PARAMS ((enum tree_code, enum tree_code, tree, tree, 56 tree, const char *)); 57static tree build_object_call PARAMS ((tree, tree)); 58static tree resolve_args PARAMS ((tree)); 59static struct z_candidate * build_user_type_conversion_1 60 PARAMS ((tree, tree, int)); 61static void print_z_candidates PARAMS ((struct z_candidate *)); 62static tree build_this PARAMS ((tree)); 63static struct z_candidate * splice_viable PARAMS ((struct z_candidate *)); 64static int any_viable PARAMS ((struct z_candidate *)); 65static int any_strictly_viable PARAMS ((struct z_candidate *)); 66static struct z_candidate * add_template_candidate 67 PARAMS ((struct z_candidate **, tree, tree, tree, tree, tree, 68 tree, tree, int, unification_kind_t)); 69static struct z_candidate * add_template_candidate_real 70 PARAMS ((struct z_candidate **, tree, tree, tree, tree, tree, 71 tree, tree, int, tree, unification_kind_t)); 72static struct z_candidate * add_template_conv_candidate 73 PARAMS ((struct z_candidate **, tree, tree, tree, tree, tree, tree)); 74static void add_builtin_candidates 75 PARAMS ((struct z_candidate **, enum tree_code, enum tree_code, 76 tree, tree *, int)); 77static void add_builtin_candidate 78 PARAMS ((struct z_candidate **, enum tree_code, enum tree_code, 79 tree, tree, tree, tree *, tree *, int)); 80static int is_complete PARAMS ((tree)); 81static void build_builtin_candidate 82 PARAMS ((struct z_candidate **, tree, tree, tree, tree *, tree *, 83 int)); 84static struct z_candidate * add_conv_candidate 85 PARAMS ((struct z_candidate **, tree, tree, tree, tree, tree)); 86static struct z_candidate * add_function_candidate 87 (struct z_candidate **, tree, tree, tree, tree, tree, int); 88static tree implicit_conversion PARAMS ((tree, tree, tree, int)); 89static tree standard_conversion PARAMS ((tree, tree, tree)); 90static tree reference_binding (tree, tree, tree, int); 91static tree non_reference PARAMS ((tree)); 92static tree build_conv PARAMS ((enum tree_code, tree, tree)); 93static int is_subseq PARAMS ((tree, tree)); 94static tree maybe_handle_ref_bind PARAMS ((tree*)); 95static void maybe_handle_implicit_object PARAMS ((tree*)); 96static struct z_candidate *add_candidate 97 (struct z_candidate **, tree, tree, tree, tree, int); 98static tree source_type PARAMS ((tree)); 99static void add_warning PARAMS ((struct z_candidate *, struct z_candidate *)); 100static int reference_related_p PARAMS ((tree, tree)); 101static int reference_compatible_p PARAMS ((tree, tree)); 102static tree convert_class_to_reference PARAMS ((tree, tree, tree)); 103static tree direct_reference_binding PARAMS ((tree, tree)); 104static int promoted_arithmetic_type_p PARAMS ((tree)); 105static tree conditional_conversion PARAMS ((tree, tree)); 106static tree call_builtin_trap PARAMS ((void)); 107static tree merge_conversion_sequences (tree, tree); 108 109tree 110build_vfield_ref (datum, type) 111 tree datum, type; 112{ 113 if (datum == error_mark_node) 114 return error_mark_node; 115 116 if (TREE_CODE (TREE_TYPE (datum)) == REFERENCE_TYPE) 117 datum = convert_from_reference (datum); 118 119 if (TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (type) 120 && !same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (datum), type)) 121 datum = convert_to_base (datum, type, /*check_access=*/false); 122 123 return build (COMPONENT_REF, TREE_TYPE (TYPE_VFIELD (type)), 124 datum, TYPE_VFIELD (type)); 125} 126 127/* Build a call to a member of an object. I.e., one that overloads 128 operator ()(), or is a pointer-to-function or pointer-to-method. */ 129 130static tree 131build_field_call (tree instance_ptr, tree decl, tree parms) 132{ 133 tree instance; 134 135 if (decl == error_mark_node || decl == NULL_TREE) 136 return decl; 137 138 if (TREE_CODE (decl) == FIELD_DECL || TREE_CODE (decl) == VAR_DECL) 139 { 140 /* If it's a field, try overloading operator (), 141 or calling if the field is a pointer-to-function. */ 142 instance = build_indirect_ref (instance_ptr, NULL); 143 instance = build_class_member_access_expr (instance, decl, 144 /*access_path=*/NULL_TREE, 145 /*preserve_reference=*/false); 146 147 if (instance == error_mark_node) 148 return error_mark_node; 149 150 if (IS_AGGR_TYPE (TREE_TYPE (instance))) 151 return build_opfncall (CALL_EXPR, LOOKUP_NORMAL, 152 instance, parms, NULL_TREE); 153 else if (TREE_CODE (TREE_TYPE (instance)) == FUNCTION_TYPE 154 || (TREE_CODE (TREE_TYPE (instance)) == POINTER_TYPE 155 && (TREE_CODE (TREE_TYPE (TREE_TYPE (instance))) 156 == FUNCTION_TYPE))) 157 return build_function_call (instance, parms); 158 } 159 160 return NULL_TREE; 161} 162 163/* Returns nonzero iff the destructor name specified in NAME 164 (a BIT_NOT_EXPR) matches BASETYPE. The operand of NAME can take many 165 forms... */ 166 167int 168check_dtor_name (basetype, name) 169 tree basetype, name; 170{ 171 name = TREE_OPERAND (name, 0); 172 173 /* Just accept something we've already complained about. */ 174 if (name == error_mark_node) 175 return 1; 176 177 if (TREE_CODE (name) == TYPE_DECL) 178 name = TREE_TYPE (name); 179 else if (TYPE_P (name)) 180 /* OK */; 181 else if (TREE_CODE (name) == IDENTIFIER_NODE) 182 { 183 if ((IS_AGGR_TYPE (basetype) && name == constructor_name (basetype)) 184 || (TREE_CODE (basetype) == ENUMERAL_TYPE 185 && name == TYPE_IDENTIFIER (basetype))) 186 name = basetype; 187 else 188 name = get_type_value (name); 189 } 190 /* In the case of: 191 192 template <class T> struct S { ~S(); }; 193 int i; 194 i.~S(); 195 196 NAME will be a class template. */ 197 else if (DECL_CLASS_TEMPLATE_P (name)) 198 return 0; 199 else 200 abort (); 201 202 if (name && TYPE_MAIN_VARIANT (basetype) == TYPE_MAIN_VARIANT (name)) 203 return 1; 204 return 0; 205} 206 207/* Build a method call of the form `EXP->SCOPES::NAME (PARMS)'. 208 This is how virtual function calls are avoided. */ 209 210tree 211build_scoped_method_call (exp, basetype, name, parms) 212 tree exp, basetype, name, parms; 213{ 214 /* Because this syntactic form does not allow 215 a pointer to a base class to be `stolen', 216 we need not protect the derived->base conversion 217 that happens here. 218 219 @@ But we do have to check access privileges later. */ 220 tree binfo, decl; 221 tree type = TREE_TYPE (exp); 222 223 if (type == error_mark_node 224 || basetype == error_mark_node) 225 return error_mark_node; 226 227 if (processing_template_decl) 228 { 229 if (TREE_CODE (name) == BIT_NOT_EXPR 230 && TREE_CODE (TREE_OPERAND (name, 0)) == IDENTIFIER_NODE) 231 { 232 tree type = get_aggr_from_typedef (TREE_OPERAND (name, 0), 0); 233 if (type) 234 name = build_min_nt (BIT_NOT_EXPR, type); 235 } 236 name = build_min_nt (SCOPE_REF, basetype, name); 237 return build_min_nt (METHOD_CALL_EXPR, name, exp, parms, NULL_TREE); 238 } 239 240 if (TREE_CODE (type) == REFERENCE_TYPE) 241 type = TREE_TYPE (type); 242 243 if (TREE_CODE (basetype) == TREE_VEC) 244 { 245 binfo = basetype; 246 basetype = BINFO_TYPE (binfo); 247 } 248 else 249 binfo = NULL_TREE; 250 251 /* Check the destructor call syntax. */ 252 if (TREE_CODE (name) == BIT_NOT_EXPR) 253 { 254 /* We can get here if someone writes their destructor call like 255 `obj.NS::~T()'; this isn't really a scoped method call, so hand 256 it off. */ 257 if (TREE_CODE (basetype) == NAMESPACE_DECL) 258 return build_method_call (exp, name, parms, NULL_TREE, LOOKUP_NORMAL); 259 260 if (! check_dtor_name (basetype, name)) 261 error ("qualified type `%T' does not match destructor name `~%T'", 262 basetype, TREE_OPERAND (name, 0)); 263 264 /* Destructors can be "called" for simple types; see 5.2.4 and 12.4 Note 265 that explicit ~int is caught in the parser; this deals with typedefs 266 and template parms. */ 267 if (! IS_AGGR_TYPE (basetype)) 268 { 269 if (TYPE_MAIN_VARIANT (type) != TYPE_MAIN_VARIANT (basetype)) 270 error ("type of `%E' does not match destructor type `%T' (type was `%T')", 271 exp, basetype, type); 272 273 return cp_convert (void_type_node, exp); 274 } 275 } 276 277 if (TREE_CODE (basetype) == NAMESPACE_DECL) 278 { 279 error ("`%D' is a namespace", basetype); 280 return error_mark_node; 281 } 282 if (! is_aggr_type (basetype, 1)) 283 return error_mark_node; 284 285 if (! IS_AGGR_TYPE (type)) 286 { 287 error ("base object `%E' of scoped method call is of non-aggregate type `%T'", 288 exp, type); 289 return error_mark_node; 290 } 291 292 decl = build_scoped_ref (exp, basetype, &binfo); 293 294 if (binfo) 295 { 296 /* Call to a destructor. */ 297 if (TREE_CODE (name) == BIT_NOT_EXPR) 298 { 299 if (! TYPE_HAS_DESTRUCTOR (TREE_TYPE (decl))) 300 return cp_convert (void_type_node, exp); 301 302 return build_delete (TREE_TYPE (decl), decl, 303 sfk_complete_destructor, 304 LOOKUP_NORMAL|LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR, 305 0); 306 } 307 308 /* Call to a method. */ 309 return build_method_call (decl, name, parms, binfo, 310 LOOKUP_NORMAL|LOOKUP_NONVIRTUAL); 311 } 312 return error_mark_node; 313} 314 315/* We want the address of a function or method. We avoid creating a 316 pointer-to-member function. */ 317 318tree 319build_addr_func (function) 320 tree function; 321{ 322 tree type = TREE_TYPE (function); 323 324 /* We have to do these by hand to avoid real pointer to member 325 functions. */ 326 if (TREE_CODE (type) == METHOD_TYPE) 327 { 328 tree addr; 329 330 type = build_pointer_type (type); 331 332 if (!cxx_mark_addressable (function)) 333 return error_mark_node; 334 335 addr = build1 (ADDR_EXPR, type, function); 336 337 /* Address of a static or external variable or function counts 338 as a constant */ 339 if (staticp (function)) 340 TREE_CONSTANT (addr) = 1; 341 342 function = addr; 343 } 344 else 345 function = default_conversion (function); 346 347 return function; 348} 349 350/* Build a CALL_EXPR, we can handle FUNCTION_TYPEs, METHOD_TYPEs, or 351 POINTER_TYPE to those. Note, pointer to member function types 352 (TYPE_PTRMEMFUNC_P) must be handled by our callers. */ 353 354tree 355build_call (function, parms) 356 tree function, parms; 357{ 358 int is_constructor = 0; 359 int nothrow; 360 tree tmp; 361 tree decl; 362 tree result_type; 363 tree fntype; 364 365 function = build_addr_func (function); 366 367 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function))) 368 { 369 sorry ("unable to call pointer to member function here"); 370 return error_mark_node; 371 } 372 373 fntype = TREE_TYPE (TREE_TYPE (function)); 374 result_type = TREE_TYPE (fntype); 375 376 if (TREE_CODE (function) == ADDR_EXPR 377 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL) 378 decl = TREE_OPERAND (function, 0); 379 else 380 decl = NULL_TREE; 381 382 /* We check both the decl and the type; a function may be known not to 383 throw without being declared throw(). */ 384 nothrow = ((decl && TREE_NOTHROW (decl)) 385 || TYPE_NOTHROW_P (TREE_TYPE (TREE_TYPE (function)))); 386 387 if (decl && TREE_THIS_VOLATILE (decl) && cfun) 388 current_function_returns_abnormally = 1; 389 390 if (decl && TREE_DEPRECATED (decl)) 391 warn_deprecated_use (decl); 392 require_complete_eh_spec_types (fntype, decl); 393 394 if (decl && DECL_CONSTRUCTOR_P (decl)) 395 is_constructor = 1; 396 397 if (decl && ! TREE_USED (decl)) 398 { 399 /* We invoke build_call directly for several library functions. 400 These may have been declared normally if we're building libgcc, 401 so we can't just check DECL_ARTIFICIAL. */ 402 if (DECL_ARTIFICIAL (decl) 403 || !strncmp (IDENTIFIER_POINTER (DECL_NAME (decl)), "__", 2)) 404 mark_used (decl); 405 else 406 abort (); 407 } 408 409 /* Don't pass empty class objects by value. This is useful 410 for tags in STL, which are used to control overload resolution. 411 We don't need to handle other cases of copying empty classes. */ 412 if (! decl || ! DECL_BUILT_IN (decl)) 413 for (tmp = parms; tmp; tmp = TREE_CHAIN (tmp)) 414 if (is_empty_class (TREE_TYPE (TREE_VALUE (tmp))) 415 && ! TREE_ADDRESSABLE (TREE_TYPE (TREE_VALUE (tmp)))) 416 { 417 tree t = build (EMPTY_CLASS_EXPR, TREE_TYPE (TREE_VALUE (tmp))); 418 TREE_VALUE (tmp) = build (COMPOUND_EXPR, TREE_TYPE (t), 419 TREE_VALUE (tmp), t); 420 } 421 422 function = build_nt (CALL_EXPR, function, parms, NULL_TREE); 423 TREE_HAS_CONSTRUCTOR (function) = is_constructor; 424 TREE_TYPE (function) = result_type; 425 TREE_SIDE_EFFECTS (function) = 1; 426 TREE_NOTHROW (function) = nothrow; 427 428 return function; 429} 430 431/* Build something of the form ptr->method (args) 432 or object.method (args). This can also build 433 calls to constructors, and find friends. 434 435 Member functions always take their class variable 436 as a pointer. 437 438 INSTANCE is a class instance. 439 440 NAME is the name of the method desired, usually an IDENTIFIER_NODE. 441 442 PARMS help to figure out what that NAME really refers to. 443 444 BASETYPE_PATH, if non-NULL, contains a chain from the type of INSTANCE 445 down to the real instance type to use for access checking. We need this 446 information to get protected accesses correct. This parameter is used 447 by build_member_call. 448 449 FLAGS is the logical disjunction of zero or more LOOKUP_ 450 flags. See cp-tree.h for more info. 451 452 If this is all OK, calls build_function_call with the resolved 453 member function. 454 455 This function must also handle being called to perform 456 initialization, promotion/coercion of arguments, and 457 instantiation of default parameters. 458 459 Note that NAME may refer to an instance variable name. If 460 `operator()()' is defined for the type of that field, then we return 461 that result. */ 462 463#ifdef GATHER_STATISTICS 464extern int n_build_method_call; 465#endif 466 467tree 468build_method_call (instance, name, parms, basetype_path, flags) 469 tree instance, name, parms, basetype_path; 470 int flags; 471{ 472 tree fn; 473 tree object_type; 474 tree template_args = NULL_TREE; 475 bool has_template_args = false; 476 477#ifdef GATHER_STATISTICS 478 n_build_method_call++; 479#endif 480 481 if (instance == error_mark_node 482 || name == error_mark_node 483 || parms == error_mark_node 484 || (instance && TREE_TYPE (instance) == error_mark_node)) 485 return error_mark_node; 486 487 if (processing_template_decl) 488 { 489 /* We need to process template parm names here so that tsubst catches 490 them properly. Other type names can wait. */ 491 if (TREE_CODE (name) == BIT_NOT_EXPR) 492 { 493 tree type = NULL_TREE; 494 495 if (TREE_CODE (TREE_OPERAND (name, 0)) == IDENTIFIER_NODE) 496 type = get_aggr_from_typedef (TREE_OPERAND (name, 0), 0); 497 else if (TREE_CODE (TREE_OPERAND (name, 0)) == TYPE_DECL) 498 type = TREE_TYPE (TREE_OPERAND (name, 0)); 499 500 if (type && TREE_CODE (type) == TEMPLATE_TYPE_PARM) 501 name = build_min_nt (BIT_NOT_EXPR, type); 502 } 503 504 return build_min_nt (METHOD_CALL_EXPR, name, instance, parms, NULL_TREE); 505 } 506 507 if (TREE_CODE (instance) == OFFSET_REF) 508 instance = resolve_offset_ref (instance); 509 if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE) 510 instance = convert_from_reference (instance); 511 object_type = TREE_TYPE (instance); 512 513 if (TREE_CODE (name) == BIT_NOT_EXPR) 514 { 515 tree instance_ptr; 516 517 if (parms) 518 error ("destructors take no parameters"); 519 520 if (! check_dtor_name (object_type, name)) 521 error 522 ("destructor name `~%T' does not match type `%T' of expression", 523 TREE_OPERAND (name, 0), object_type); 524 525 /* The destructor type must be complete. */ 526 object_type = complete_type_or_else (object_type, NULL_TREE); 527 if (!object_type || object_type == error_mark_node) 528 return error_mark_node; 529 530 if (! TYPE_HAS_DESTRUCTOR (object_type)) 531 return cp_convert (void_type_node, instance); 532 instance = default_conversion (instance); 533 instance_ptr = build_unary_op (ADDR_EXPR, instance, 0); 534 return build_delete (build_pointer_type (object_type), 535 instance_ptr, sfk_complete_destructor, 536 LOOKUP_NORMAL|LOOKUP_DESTRUCTOR, 0); 537 } 538 539 if (!CLASS_TYPE_P (object_type)) 540 { 541 if ((flags & LOOKUP_COMPLAIN) 542 && TREE_TYPE (instance) != error_mark_node) 543 error ("request for member `%D' in `%E', which is of non-aggregate type `%T'", 544 name, instance, object_type); 545 return error_mark_node; 546 } 547 548 if (TREE_CODE (name) == TEMPLATE_ID_EXPR) 549 { 550 template_args = TREE_OPERAND (name, 1); 551 has_template_args = true; 552 name = TREE_OPERAND (name, 0); 553 } 554 if (TREE_CODE (name) == OVERLOAD) 555 name = DECL_NAME (get_first_fn (name)); 556 else if (TREE_CODE (name) == LOOKUP_EXPR) 557 name = TREE_OPERAND (name, 0); 558 else if (DECL_P (name)) 559 name = DECL_NAME (name); 560 if (has_template_args) 561 fn = lookup_fnfields (object_type, name, /*protect=*/2); 562 else 563 fn = lookup_member (object_type, name, /*protect=*/2, /*want_type=*/0); 564 565 if (fn && TREE_CODE (fn) == TREE_LIST && !BASELINK_P (fn)) 566 { 567 error ("request for member `%D' is ambiguous", name); 568 print_candidates (fn); 569 return error_mark_node; 570 } 571 572 /* If the name could not be found, issue an error. */ 573 if (!fn) 574 { 575 unqualified_name_lookup_error (name); 576 return error_mark_node; 577 } 578 579 if (BASELINK_P (fn) && has_template_args) 580 BASELINK_FUNCTIONS (fn) 581 = build_nt (TEMPLATE_ID_EXPR, 582 BASELINK_FUNCTIONS (fn), 583 template_args); 584 if (BASELINK_P (fn) && basetype_path) 585 BASELINK_ACCESS_BINFO (fn) = basetype_path; 586 587 return build_new_method_call (instance, fn, parms, 588 /*conversion_path=*/NULL_TREE, flags); 589} 590 591/* New overloading code. */ 592 593struct z_candidate GTY(()) { 594 /* The FUNCTION_DECL that will be called if this candidate is 595 selected by overload resolution. */ 596 tree fn; 597 tree convs; 598 tree second_conv; 599 int viable; 600 /* If FN is a member function, the binfo indicating the path used to 601 qualify the name of FN at the call site. This path is used to 602 determine whether or not FN is accessible if it is selected by 603 overload resolution. The DECL_CONTEXT of FN will always be a 604 (possibly improper) base of this binfo. */ 605 tree access_path; 606 /* If FN is a non-static member function, the binfo indicating the 607 subobject to which the `this' pointer should be converted if FN 608 is selected by overload resolution. The type pointed to the by 609 the `this' pointer must correspond to the most derived class 610 indicated by the CONVERSION_PATH. */ 611 tree conversion_path; 612 tree template; 613 tree warnings; 614 struct z_candidate *next; 615}; 616 617#define IDENTITY_RANK 0 618#define EXACT_RANK 1 619#define PROMO_RANK 2 620#define STD_RANK 3 621#define PBOOL_RANK 4 622#define USER_RANK 5 623#define ELLIPSIS_RANK 6 624#define BAD_RANK 7 625 626#define ICS_RANK(NODE) \ 627 (ICS_BAD_FLAG (NODE) ? BAD_RANK \ 628 : ICS_ELLIPSIS_FLAG (NODE) ? ELLIPSIS_RANK \ 629 : ICS_USER_FLAG (NODE) ? USER_RANK \ 630 : ICS_STD_RANK (NODE)) 631 632#define ICS_STD_RANK(NODE) TREE_COMPLEXITY (NODE) 633 634#define ICS_USER_FLAG(NODE) TREE_LANG_FLAG_0 (NODE) 635#define ICS_ELLIPSIS_FLAG(NODE) TREE_LANG_FLAG_1 (NODE) 636#define ICS_THIS_FLAG(NODE) TREE_LANG_FLAG_2 (NODE) 637#define ICS_BAD_FLAG(NODE) TREE_LANG_FLAG_3 (NODE) 638 639/* In a REF_BIND or a BASE_CONV, this indicates that a temporary 640 should be created to hold the result of the conversion. */ 641#define NEED_TEMPORARY_P(NODE) TREE_LANG_FLAG_4 (NODE) 642 643#define USER_CONV_CAND(NODE) WRAPPER_ZC (TREE_OPERAND (NODE, 1)) 644#define USER_CONV_FN(NODE) (USER_CONV_CAND (NODE)->fn) 645 646int 647null_ptr_cst_p (t) 648 tree t; 649{ 650 /* [conv.ptr] 651 652 A null pointer constant is an integral constant expression 653 (_expr.const_) rvalue of integer type that evaluates to zero. */ 654 if (t == null_node 655 || (CP_INTEGRAL_TYPE_P (TREE_TYPE (t)) && integer_zerop (t))) 656 return 1; 657 return 0; 658} 659 660 661/* Returns nonzero if PARMLIST consists of only default parms and/or 662 ellipsis. */ 663 664int 665sufficient_parms_p (parmlist) 666 tree parmlist; 667{ 668 for (; parmlist && parmlist != void_list_node; 669 parmlist = TREE_CHAIN (parmlist)) 670 if (!TREE_PURPOSE (parmlist)) 671 return 0; 672 return 1; 673} 674 675static tree 676build_conv (code, type, from) 677 enum tree_code code; 678 tree type, from; 679{ 680 tree t; 681 int rank = ICS_STD_RANK (from); 682 683 /* We can't use buildl1 here because CODE could be USER_CONV, which 684 takes two arguments. In that case, the caller is responsible for 685 filling in the second argument. */ 686 t = make_node (code); 687 TREE_TYPE (t) = type; 688 TREE_OPERAND (t, 0) = from; 689 690 switch (code) 691 { 692 case PTR_CONV: 693 case PMEM_CONV: 694 case BASE_CONV: 695 case STD_CONV: 696 if (rank < STD_RANK) 697 rank = STD_RANK; 698 break; 699 700 case QUAL_CONV: 701 if (rank < EXACT_RANK) 702 rank = EXACT_RANK; 703 704 default: 705 break; 706 } 707 ICS_STD_RANK (t) = rank; 708 ICS_USER_FLAG (t) = (code == USER_CONV || ICS_USER_FLAG (from)); 709 ICS_BAD_FLAG (t) = ICS_BAD_FLAG (from); 710 return t; 711} 712 713/* If T is a REFERENCE_TYPE return the type to which T refers. 714 Otherwise, return T itself. */ 715 716static tree 717non_reference (t) 718 tree t; 719{ 720 if (TREE_CODE (t) == REFERENCE_TYPE) 721 t = TREE_TYPE (t); 722 return t; 723} 724 725tree 726strip_top_quals (t) 727 tree t; 728{ 729 if (TREE_CODE (t) == ARRAY_TYPE) 730 return t; 731 return TYPE_MAIN_VARIANT (t); 732} 733 734/* Returns the standard conversion path (see [conv]) from type FROM to type 735 TO, if any. For proper handling of null pointer constants, you must 736 also pass the expression EXPR to convert from. */ 737 738static tree 739standard_conversion (to, from, expr) 740 tree to, from, expr; 741{ 742 enum tree_code fcode, tcode; 743 tree conv; 744 int fromref = 0; 745 746 if (TREE_CODE (to) == REFERENCE_TYPE) 747 to = TREE_TYPE (to); 748 if (TREE_CODE (from) == REFERENCE_TYPE) 749 { 750 fromref = 1; 751 from = TREE_TYPE (from); 752 } 753 to = strip_top_quals (to); 754 from = strip_top_quals (from); 755 756 if ((TYPE_PTRFN_P (to) || TYPE_PTRMEMFUNC_P (to)) 757 && expr && type_unknown_p (expr)) 758 { 759 expr = instantiate_type (to, expr, tf_none); 760 if (expr == error_mark_node) 761 return NULL_TREE; 762 from = TREE_TYPE (expr); 763 } 764 765 fcode = TREE_CODE (from); 766 tcode = TREE_CODE (to); 767 768 conv = build1 (IDENTITY_CONV, from, expr); 769 770 if (fcode == FUNCTION_TYPE) 771 { 772 from = build_pointer_type (from); 773 fcode = TREE_CODE (from); 774 conv = build_conv (LVALUE_CONV, from, conv); 775 } 776 else if (fcode == ARRAY_TYPE) 777 { 778 from = build_pointer_type (TREE_TYPE (from)); 779 fcode = TREE_CODE (from); 780 conv = build_conv (LVALUE_CONV, from, conv); 781 } 782 else if (fromref || (expr && lvalue_p (expr))) 783 conv = build_conv (RVALUE_CONV, from, conv); 784 785 /* Allow conversion between `__complex__' data types */ 786 if (tcode == COMPLEX_TYPE && fcode == COMPLEX_TYPE) 787 { 788 /* The standard conversion sequence to convert FROM to TO is 789 the standard conversion sequence to perform componentwise 790 conversion. */ 791 tree part_conv = standard_conversion 792 (TREE_TYPE (to), TREE_TYPE (from), NULL_TREE); 793 794 if (part_conv) 795 { 796 conv = build_conv (TREE_CODE (part_conv), to, conv); 797 ICS_STD_RANK (conv) = ICS_STD_RANK (part_conv); 798 } 799 else 800 conv = NULL_TREE; 801 802 return conv; 803 } 804 805 if (same_type_p (from, to)) 806 return conv; 807 808 if ((tcode == POINTER_TYPE || TYPE_PTRMEMFUNC_P (to)) 809 && expr && null_ptr_cst_p (expr)) 810 { 811 conv = build_conv (STD_CONV, to, conv); 812 } 813 else if ((tcode == INTEGER_TYPE && fcode == POINTER_TYPE) 814 || (tcode == POINTER_TYPE && fcode == INTEGER_TYPE)) 815 { 816 /* For backwards brain damage compatibility, allow interconversion of 817 pointers and integers with a pedwarn. */ 818 conv = build_conv (STD_CONV, to, conv); 819 ICS_BAD_FLAG (conv) = 1; 820 } 821 else if (tcode == ENUMERAL_TYPE && fcode == INTEGER_TYPE 822 && TYPE_PRECISION (to) == TYPE_PRECISION (from)) 823 { 824 /* For backwards brain damage compatibility, allow interconversion of 825 enums and integers with a pedwarn. */ 826 conv = build_conv (STD_CONV, to, conv); 827 ICS_BAD_FLAG (conv) = 1; 828 } 829 else if (tcode == POINTER_TYPE && fcode == POINTER_TYPE) 830 { 831 enum tree_code ufcode = TREE_CODE (TREE_TYPE (from)); 832 enum tree_code utcode = TREE_CODE (TREE_TYPE (to)); 833 834 if (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (from), 835 TREE_TYPE (to))) 836 ; 837 else if (utcode == VOID_TYPE && ufcode != OFFSET_TYPE 838 && ufcode != FUNCTION_TYPE) 839 { 840 from = build_pointer_type 841 (cp_build_qualified_type (void_type_node, 842 cp_type_quals (TREE_TYPE (from)))); 843 conv = build_conv (PTR_CONV, from, conv); 844 } 845 else if (ufcode == OFFSET_TYPE && utcode == OFFSET_TYPE) 846 { 847 tree fbase = TYPE_OFFSET_BASETYPE (TREE_TYPE (from)); 848 tree tbase = TYPE_OFFSET_BASETYPE (TREE_TYPE (to)); 849 850 if (DERIVED_FROM_P (fbase, tbase) 851 && (same_type_ignoring_top_level_qualifiers_p 852 (TREE_TYPE (TREE_TYPE (from)), 853 TREE_TYPE (TREE_TYPE (to))))) 854 { 855 from = build_ptrmem_type (tbase, TREE_TYPE (TREE_TYPE (from))); 856 conv = build_conv (PMEM_CONV, from, conv); 857 } 858 } 859 else if (IS_AGGR_TYPE (TREE_TYPE (from)) 860 && IS_AGGR_TYPE (TREE_TYPE (to))) 861 { 862 if (DERIVED_FROM_P (TREE_TYPE (to), TREE_TYPE (from))) 863 { 864 from = 865 cp_build_qualified_type (TREE_TYPE (to), 866 cp_type_quals (TREE_TYPE (from))); 867 from = build_pointer_type (from); 868 conv = build_conv (PTR_CONV, from, conv); 869 } 870 } 871 872 if (same_type_p (from, to)) 873 /* OK */; 874 else if (comp_ptr_ttypes (TREE_TYPE (to), TREE_TYPE (from))) 875 conv = build_conv (QUAL_CONV, to, conv); 876 else if (expr && string_conv_p (to, expr, 0)) 877 /* converting from string constant to char *. */ 878 conv = build_conv (QUAL_CONV, to, conv); 879 else if (ptr_reasonably_similar (TREE_TYPE (to), TREE_TYPE (from))) 880 { 881 conv = build_conv (PTR_CONV, to, conv); 882 ICS_BAD_FLAG (conv) = 1; 883 } 884 else 885 return 0; 886 887 from = to; 888 } 889 else if (TYPE_PTRMEMFUNC_P (to) && TYPE_PTRMEMFUNC_P (from)) 890 { 891 tree fromfn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (from)); 892 tree tofn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (to)); 893 tree fbase = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (fromfn))); 894 tree tbase = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (tofn))); 895 896 if (!DERIVED_FROM_P (fbase, tbase) 897 || !same_type_p (TREE_TYPE (fromfn), TREE_TYPE (tofn)) 898 || !compparms (TREE_CHAIN (TYPE_ARG_TYPES (fromfn)), 899 TREE_CHAIN (TYPE_ARG_TYPES (tofn))) 900 || cp_type_quals (fbase) != cp_type_quals (tbase)) 901 return 0; 902 903 from = cp_build_qualified_type (tbase, cp_type_quals (fbase)); 904 from = build_cplus_method_type (from, TREE_TYPE (fromfn), 905 TREE_CHAIN (TYPE_ARG_TYPES (fromfn))); 906 from = build_ptrmemfunc_type (build_pointer_type (from)); 907 conv = build_conv (PMEM_CONV, from, conv); 908 } 909 else if (tcode == BOOLEAN_TYPE) 910 { 911 if (! (INTEGRAL_CODE_P (fcode) || fcode == REAL_TYPE 912 || fcode == POINTER_TYPE || TYPE_PTRMEMFUNC_P (from))) 913 return 0; 914 915 conv = build_conv (STD_CONV, to, conv); 916 if (fcode == POINTER_TYPE 917 || (TYPE_PTRMEMFUNC_P (from) && ICS_STD_RANK (conv) < PBOOL_RANK)) 918 ICS_STD_RANK (conv) = PBOOL_RANK; 919 } 920 /* We don't check for ENUMERAL_TYPE here because there are no standard 921 conversions to enum type. */ 922 else if (tcode == INTEGER_TYPE || tcode == BOOLEAN_TYPE 923 || tcode == REAL_TYPE) 924 { 925 if (! (INTEGRAL_CODE_P (fcode) || fcode == REAL_TYPE)) 926 return 0; 927 conv = build_conv (STD_CONV, to, conv); 928 929 /* Give this a better rank if it's a promotion. */ 930 if (to == type_promotes_to (from) 931 && ICS_STD_RANK (TREE_OPERAND (conv, 0)) <= PROMO_RANK) 932 ICS_STD_RANK (conv) = PROMO_RANK; 933 } 934 else if (IS_AGGR_TYPE (to) && IS_AGGR_TYPE (from) 935 && is_properly_derived_from (from, to)) 936 { 937 if (TREE_CODE (conv) == RVALUE_CONV) 938 conv = TREE_OPERAND (conv, 0); 939 conv = build_conv (BASE_CONV, to, conv); 940 /* The derived-to-base conversion indicates the initialization 941 of a parameter with base type from an object of a derived 942 type. A temporary object is created to hold the result of 943 the conversion. */ 944 NEED_TEMPORARY_P (conv) = 1; 945 } 946 else 947 return 0; 948 949 return conv; 950} 951 952/* Returns nonzero if T1 is reference-related to T2. */ 953 954static int 955reference_related_p (t1, t2) 956 tree t1; 957 tree t2; 958{ 959 t1 = TYPE_MAIN_VARIANT (t1); 960 t2 = TYPE_MAIN_VARIANT (t2); 961 962 /* [dcl.init.ref] 963 964 Given types "cv1 T1" and "cv2 T2," "cv1 T1" is reference-related 965 to "cv2 T2" if T1 is the same type as T2, or T1 is a base class 966 of T2. */ 967 return (same_type_p (t1, t2) 968 || (CLASS_TYPE_P (t1) && CLASS_TYPE_P (t2) 969 && DERIVED_FROM_P (t1, t2))); 970} 971 972/* Returns nonzero if T1 is reference-compatible with T2. */ 973 974static int 975reference_compatible_p (t1, t2) 976 tree t1; 977 tree t2; 978{ 979 /* [dcl.init.ref] 980 981 "cv1 T1" is reference compatible with "cv2 T2" if T1 is 982 reference-related to T2 and cv1 is the same cv-qualification as, 983 or greater cv-qualification than, cv2. */ 984 return (reference_related_p (t1, t2) 985 && at_least_as_qualified_p (t1, t2)); 986} 987 988/* Determine whether or not the EXPR (of class type S) can be 989 converted to T as in [over.match.ref]. */ 990 991static tree 992convert_class_to_reference (t, s, expr) 993 tree t; 994 tree s; 995 tree expr; 996{ 997 tree conversions; 998 tree arglist; 999 tree conv; 1000 tree reference_type; 1001 struct z_candidate *candidates; 1002 struct z_candidate *cand; 1003 1004 conversions = lookup_conversions (s); 1005 if (!conversions) 1006 return NULL_TREE; 1007 1008 /* [over.match.ref] 1009 1010 Assuming that "cv1 T" is the underlying type of the reference 1011 being initialized, and "cv S" is the type of the initializer 1012 expression, with S a class type, the candidate functions are 1013 selected as follows: 1014 1015 --The conversion functions of S and its base classes are 1016 considered. Those that are not hidden within S and yield type 1017 "reference to cv2 T2", where "cv1 T" is reference-compatible 1018 (_dcl.init.ref_) with "cv2 T2", are candidate functions. 1019 1020 The argument list has one argument, which is the initializer 1021 expression. */ 1022 1023 candidates = 0; 1024 1025 /* Conceptually, we should take the address of EXPR and put it in 1026 the argument list. Unfortunately, however, that can result in 1027 error messages, which we should not issue now because we are just 1028 trying to find a conversion operator. Therefore, we use NULL, 1029 cast to the appropriate type. */ 1030 arglist = build_int_2 (0, 0); 1031 TREE_TYPE (arglist) = build_pointer_type (s); 1032 arglist = build_tree_list (NULL_TREE, arglist); 1033 1034 reference_type = build_reference_type (t); 1035 1036 while (conversions) 1037 { 1038 tree fns = TREE_VALUE (conversions); 1039 1040 for (; fns; fns = OVL_NEXT (fns)) 1041 { 1042 tree f = OVL_CURRENT (fns); 1043 tree t2 = TREE_TYPE (TREE_TYPE (f)); 1044 1045 cand = NULL; 1046 1047 /* If this is a template function, try to get an exact 1048 match. */ 1049 if (TREE_CODE (f) == TEMPLATE_DECL) 1050 { 1051 cand = add_template_candidate (&candidates, 1052 f, s, 1053 NULL_TREE, 1054 arglist, 1055 reference_type, 1056 TYPE_BINFO (s), 1057 TREE_PURPOSE (conversions), 1058 LOOKUP_NORMAL, 1059 DEDUCE_CONV); 1060 1061 if (cand) 1062 { 1063 /* Now, see if the conversion function really returns 1064 an lvalue of the appropriate type. From the 1065 point of view of unification, simply returning an 1066 rvalue of the right type is good enough. */ 1067 f = cand->fn; 1068 t2 = TREE_TYPE (TREE_TYPE (f)); 1069 if (TREE_CODE (t2) != REFERENCE_TYPE 1070 || !reference_compatible_p (t, TREE_TYPE (t2))) 1071 { 1072 candidates = candidates->next; 1073 cand = NULL; 1074 } 1075 } 1076 } 1077 else if (TREE_CODE (t2) == REFERENCE_TYPE 1078 && reference_compatible_p (t, TREE_TYPE (t2))) 1079 cand = add_function_candidate (&candidates, f, s, arglist, 1080 TYPE_BINFO (s), 1081 TREE_PURPOSE (conversions), 1082 LOOKUP_NORMAL); 1083 1084 if (cand) 1085 /* Build a standard conversion sequence indicating the 1086 binding from the reference type returned by the 1087 function to the desired REFERENCE_TYPE. */ 1088 cand->second_conv 1089 = (direct_reference_binding 1090 (reference_type, 1091 build1 (IDENTITY_CONV, 1092 TREE_TYPE (TREE_TYPE (TREE_TYPE (cand->fn))), 1093 NULL_TREE))); 1094 } 1095 conversions = TREE_CHAIN (conversions); 1096 } 1097 1098 /* If none of the conversion functions worked out, let our caller 1099 know. */ 1100 if (!any_viable (candidates)) 1101 return NULL_TREE; 1102 1103 candidates = splice_viable (candidates); 1104 cand = tourney (candidates); 1105 if (!cand) 1106 return NULL_TREE; 1107 1108 /* Build a user-defined conversion sequence representing the 1109 conversion. */ 1110 conv = build_conv (USER_CONV, 1111 TREE_TYPE (TREE_TYPE (cand->fn)), 1112 build1 (IDENTITY_CONV, TREE_TYPE (expr), expr)); 1113 TREE_OPERAND (conv, 1) = build_zc_wrapper (cand); 1114 1115 /* Merge it with the standard conversion sequence from the 1116 conversion function's return type to the desired type. */ 1117 cand->second_conv = merge_conversion_sequences (conv, cand->second_conv); 1118 1119 if (cand->viable == -1) 1120 ICS_BAD_FLAG (conv) = 1; 1121 1122 return cand->second_conv; 1123} 1124 1125/* A reference of the indicated TYPE is being bound directly to the 1126 expression represented by the implicit conversion sequence CONV. 1127 Return a conversion sequence for this binding. */ 1128 1129static tree 1130direct_reference_binding (type, conv) 1131 tree type; 1132 tree conv; 1133{ 1134 tree t; 1135 1136 my_friendly_assert (TREE_CODE (type) == REFERENCE_TYPE, 20030306); 1137 my_friendly_assert (TREE_CODE (TREE_TYPE (conv)) != REFERENCE_TYPE, 1138 20030306); 1139 1140 t = TREE_TYPE (type); 1141 1142 /* [over.ics.rank] 1143 1144 When a parameter of reference type binds directly 1145 (_dcl.init.ref_) to an argument expression, the implicit 1146 conversion sequence is the identity conversion, unless the 1147 argument expression has a type that is a derived class of the 1148 parameter type, in which case the implicit conversion sequence is 1149 a derived-to-base Conversion. 1150 1151 If the parameter binds directly to the result of applying a 1152 conversion function to the argument expression, the implicit 1153 conversion sequence is a user-defined conversion sequence 1154 (_over.ics.user_), with the second standard conversion sequence 1155 either an identity conversion or, if the conversion function 1156 returns an entity of a type that is a derived class of the 1157 parameter type, a derived-to-base conversion. */ 1158 if (!same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (conv))) 1159 { 1160 /* Represent the derived-to-base conversion. */ 1161 conv = build_conv (BASE_CONV, t, conv); 1162 /* We will actually be binding to the base-class subobject in 1163 the derived class, so we mark this conversion appropriately. 1164 That way, convert_like knows not to generate a temporary. */ 1165 NEED_TEMPORARY_P (conv) = 0; 1166 } 1167 return build_conv (REF_BIND, type, conv); 1168} 1169 1170/* Returns the conversion path from type FROM to reference type TO for 1171 purposes of reference binding. For lvalue binding, either pass a 1172 reference type to FROM or an lvalue expression to EXPR. If the 1173 reference will be bound to a temporary, NEED_TEMPORARY_P is set for 1174 the conversion returned. */ 1175 1176static tree 1177reference_binding (tree rto, tree rfrom, tree expr, int flags) 1178{ 1179 tree conv = NULL_TREE; 1180 tree to = TREE_TYPE (rto); 1181 tree from = rfrom; 1182 int related_p; 1183 int compatible_p; 1184 cp_lvalue_kind lvalue_p = clk_none; 1185 1186 if (TREE_CODE (to) == FUNCTION_TYPE && expr && type_unknown_p (expr)) 1187 { 1188 expr = instantiate_type (to, expr, tf_none); 1189 if (expr == error_mark_node) 1190 return NULL_TREE; 1191 from = TREE_TYPE (expr); 1192 } 1193 1194 if (TREE_CODE (from) == REFERENCE_TYPE) 1195 { 1196 /* Anything with reference type is an lvalue. */ 1197 lvalue_p = clk_ordinary; 1198 from = TREE_TYPE (from); 1199 } 1200 else if (expr) 1201 lvalue_p = real_lvalue_p (expr); 1202 1203 /* Figure out whether or not the types are reference-related and 1204 reference compatible. We have do do this after stripping 1205 references from FROM. */ 1206 related_p = reference_related_p (to, from); 1207 compatible_p = reference_compatible_p (to, from); 1208 1209 if (lvalue_p && compatible_p) 1210 { 1211 /* [dcl.init.ref] 1212 1213 If the initializer expression 1214 1215 -- is an lvalue (but not an lvalue for a bit-field), and "cv1 T1" 1216 is reference-compatible with "cv2 T2," 1217 1218 the reference is bound directly to the initializer exprssion 1219 lvalue. */ 1220 conv = build1 (IDENTITY_CONV, from, expr); 1221 conv = direct_reference_binding (rto, conv); 1222 if ((lvalue_p & clk_bitfield) != 0 1223 && CP_TYPE_CONST_NON_VOLATILE_P (to)) 1224 /* For the purposes of overload resolution, we ignore the fact 1225 this expression is a bitfield. (In particular, 1226 [over.ics.ref] says specifically that a function with a 1227 non-const reference parameter is viable even if the 1228 argument is a bitfield.) 1229 1230 However, when we actually call the function we must create 1231 a temporary to which to bind the reference. If the 1232 reference is volatile, or isn't const, then we cannot make 1233 a temporary, so we just issue an error when the conversion 1234 actually occurs. */ 1235 NEED_TEMPORARY_P (conv) = 1; 1236 return conv; 1237 } 1238 else if (CLASS_TYPE_P (from) && !(flags & LOOKUP_NO_CONVERSION)) 1239 { 1240 /* [dcl.init.ref] 1241 1242 If the initializer exprsesion 1243 1244 -- has a class type (i.e., T2 is a class type) can be 1245 implicitly converted to an lvalue of type "cv3 T3," where 1246 "cv1 T1" is reference-compatible with "cv3 T3". (this 1247 conversion is selected by enumerating the applicable 1248 conversion functions (_over.match.ref_) and choosing the 1249 best one through overload resolution. (_over.match_). 1250 1251 the reference is bound to the lvalue result of the conversion 1252 in the second case. */ 1253 conv = convert_class_to_reference (to, from, expr); 1254 if (conv) 1255 return conv; 1256 } 1257 1258 /* From this point on, we conceptually need temporaries, even if we 1259 elide them. Only the cases above are "direct bindings". */ 1260 if (flags & LOOKUP_NO_TEMP_BIND) 1261 return NULL_TREE; 1262 1263 /* [over.ics.rank] 1264 1265 When a parameter of reference type is not bound directly to an 1266 argument expression, the conversion sequence is the one required 1267 to convert the argument expression to the underlying type of the 1268 reference according to _over.best.ics_. Conceptually, this 1269 conversion sequence corresponds to copy-initializing a temporary 1270 of the underlying type with the argument expression. Any 1271 difference in top-level cv-qualification is subsumed by the 1272 initialization itself and does not constitute a conversion. */ 1273 1274 /* [dcl.init.ref] 1275 1276 Otherwise, the reference shall be to a non-volatile const type. */ 1277 if (!CP_TYPE_CONST_NON_VOLATILE_P (to)) 1278 return NULL_TREE; 1279 1280 /* [dcl.init.ref] 1281 1282 If the initializer expression is an rvalue, with T2 a class type, 1283 and "cv1 T1" is reference-compatible with "cv2 T2", the reference 1284 is bound in one of the following ways: 1285 1286 -- The reference is bound to the object represented by the rvalue 1287 or to a sub-object within that object. 1288 1289 -- ... 1290 1291 We use the first alternative. The implicit conversion sequence 1292 is supposed to be same as we would obtain by generating a 1293 temporary. Fortunately, if the types are reference compatible, 1294 then this is either an identity conversion or the derived-to-base 1295 conversion, just as for direct binding. */ 1296 if (CLASS_TYPE_P (from) && compatible_p) 1297 { 1298 conv = build1 (IDENTITY_CONV, from, expr); 1299 return direct_reference_binding (rto, conv); 1300 } 1301 1302 /* [dcl.init.ref] 1303 1304 Otherwise, a temporary of type "cv1 T1" is created and 1305 initialized from the initializer expression using the rules for a 1306 non-reference copy initialization. If T1 is reference-related to 1307 T2, cv1 must be the same cv-qualification as, or greater 1308 cv-qualification than, cv2; otherwise, the program is ill-formed. */ 1309 if (related_p && !at_least_as_qualified_p (to, from)) 1310 return NULL_TREE; 1311 1312 conv = implicit_conversion (to, from, expr, flags); 1313 if (!conv) 1314 return NULL_TREE; 1315 1316 conv = build_conv (REF_BIND, rto, conv); 1317 /* This reference binding, unlike those above, requires the 1318 creation of a temporary. */ 1319 NEED_TEMPORARY_P (conv) = 1; 1320 1321 return conv; 1322} 1323 1324/* Returns the implicit conversion sequence (see [over.ics]) from type FROM 1325 to type TO. The optional expression EXPR may affect the conversion. 1326 FLAGS are the usual overloading flags. Only LOOKUP_NO_CONVERSION is 1327 significant. */ 1328 1329static tree 1330implicit_conversion (to, from, expr, flags) 1331 tree to, from, expr; 1332 int flags; 1333{ 1334 tree conv; 1335 1336 /* Resolve expressions like `A::p' that we thought might become 1337 pointers-to-members. */ 1338 if (expr && TREE_CODE (expr) == OFFSET_REF) 1339 { 1340 expr = resolve_offset_ref (expr); 1341 from = TREE_TYPE (expr); 1342 } 1343 1344 if (from == error_mark_node || to == error_mark_node 1345 || expr == error_mark_node) 1346 return NULL_TREE; 1347 1348 if (TREE_CODE (to) == REFERENCE_TYPE) 1349 conv = reference_binding (to, from, expr, flags); 1350 else 1351 conv = standard_conversion (to, from, expr); 1352 1353 if (conv) 1354 ; 1355 else if (expr != NULL_TREE 1356 && (IS_AGGR_TYPE (from) 1357 || IS_AGGR_TYPE (to)) 1358 && (flags & LOOKUP_NO_CONVERSION) == 0) 1359 { 1360 struct z_candidate *cand; 1361 1362 cand = build_user_type_conversion_1 1363 (to, expr, LOOKUP_ONLYCONVERTING); 1364 if (cand) 1365 conv = cand->second_conv; 1366 1367 /* We used to try to bind a reference to a temporary here, but that 1368 is now handled by the recursive call to this function at the end 1369 of reference_binding. */ 1370 } 1371 1372 return conv; 1373} 1374 1375/* Add a new entry to the list of candidates. Used by the add_*_candidate 1376 functions. */ 1377 1378static struct z_candidate * 1379add_candidate (struct z_candidate **candidates, 1380 tree fn, tree convs, tree access_path, tree 1381 conversion_path, int viable) 1382{ 1383 struct z_candidate *cand 1384 = (struct z_candidate *) ggc_alloc_cleared (sizeof (struct z_candidate)); 1385 1386 cand->fn = fn; 1387 cand->convs = convs; 1388 cand->access_path = access_path; 1389 cand->conversion_path = conversion_path; 1390 cand->viable = viable; 1391 cand->next = *candidates; 1392 *candidates = cand; 1393 1394 return cand; 1395} 1396 1397/* Create an overload candidate for the function or method FN called with 1398 the argument list ARGLIST and add it to CANDIDATES. FLAGS is passed on 1399 to implicit_conversion. 1400 1401 CTYPE, if non-NULL, is the type we want to pretend this function 1402 comes from for purposes of overload resolution. */ 1403 1404static struct z_candidate * 1405add_function_candidate (struct z_candidate **candidates, 1406 tree fn, tree ctype, tree arglist, 1407 tree access_path, tree conversion_path, 1408 int flags) 1409{ 1410 tree parmlist = TYPE_ARG_TYPES (TREE_TYPE (fn)); 1411 int i, len; 1412 tree convs; 1413 tree parmnode, argnode; 1414 int viable = 1; 1415 1416 /* Built-in functions that haven't been declared don't really 1417 exist. */ 1418 if (DECL_ANTICIPATED (fn)) 1419 return NULL; 1420 1421 /* The `this', `in_chrg' and VTT arguments to constructors are not 1422 considered in overload resolution. */ 1423 if (DECL_CONSTRUCTOR_P (fn)) 1424 { 1425 parmlist = skip_artificial_parms_for (fn, parmlist); 1426 arglist = skip_artificial_parms_for (fn, arglist); 1427 } 1428 1429 len = list_length (arglist); 1430 convs = make_tree_vec (len); 1431 1432 /* 13.3.2 - Viable functions [over.match.viable] 1433 First, to be a viable function, a candidate function shall have enough 1434 parameters to agree in number with the arguments in the list. 1435 1436 We need to check this first; otherwise, checking the ICSes might cause 1437 us to produce an ill-formed template instantiation. */ 1438 1439 parmnode = parmlist; 1440 for (i = 0; i < len; ++i) 1441 { 1442 if (parmnode == NULL_TREE || parmnode == void_list_node) 1443 break; 1444 parmnode = TREE_CHAIN (parmnode); 1445 } 1446 1447 if (i < len && parmnode) 1448 viable = 0; 1449 1450 /* Make sure there are default args for the rest of the parms. */ 1451 else if (!sufficient_parms_p (parmnode)) 1452 viable = 0; 1453 1454 if (! viable) 1455 goto out; 1456 1457 /* Second, for F to be a viable function, there shall exist for each 1458 argument an implicit conversion sequence that converts that argument 1459 to the corresponding parameter of F. */ 1460 1461 parmnode = parmlist; 1462 argnode = arglist; 1463 1464 for (i = 0; i < len; ++i) 1465 { 1466 tree arg = TREE_VALUE (argnode); 1467 tree argtype = lvalue_type (arg); 1468 tree t; 1469 int is_this; 1470 1471 if (parmnode == void_list_node) 1472 break; 1473 1474 is_this = (i == 0 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn) 1475 && ! DECL_CONSTRUCTOR_P (fn)); 1476 1477 if (parmnode) 1478 { 1479 tree parmtype = TREE_VALUE (parmnode); 1480 1481 /* The type of the implicit object parameter ('this') for 1482 overload resolution is not always the same as for the 1483 function itself; conversion functions are considered to 1484 be members of the class being converted, and functions 1485 introduced by a using-declaration are considered to be 1486 members of the class that uses them. 1487 1488 Since build_over_call ignores the ICS for the `this' 1489 parameter, we can just change the parm type. */ 1490 if (ctype && is_this) 1491 { 1492 parmtype 1493 = build_qualified_type (ctype, 1494 TYPE_QUALS (TREE_TYPE (parmtype))); 1495 parmtype = build_pointer_type (parmtype); 1496 } 1497 1498 t = implicit_conversion (parmtype, argtype, arg, flags); 1499 } 1500 else 1501 { 1502 t = build1 (IDENTITY_CONV, argtype, arg); 1503 ICS_ELLIPSIS_FLAG (t) = 1; 1504 } 1505 1506 if (t && is_this) 1507 ICS_THIS_FLAG (t) = 1; 1508 1509 TREE_VEC_ELT (convs, i) = t; 1510 if (! t) 1511 { 1512 viable = 0; 1513 break; 1514 } 1515 1516 if (ICS_BAD_FLAG (t)) 1517 viable = -1; 1518 1519 if (parmnode) 1520 parmnode = TREE_CHAIN (parmnode); 1521 argnode = TREE_CHAIN (argnode); 1522 } 1523 1524 out: 1525 return add_candidate (candidates, fn, convs, access_path, 1526 conversion_path, viable); 1527} 1528 1529/* Create an overload candidate for the conversion function FN which will 1530 be invoked for expression OBJ, producing a pointer-to-function which 1531 will in turn be called with the argument list ARGLIST, and add it to 1532 CANDIDATES. FLAGS is passed on to implicit_conversion. 1533 1534 Actually, we don't really care about FN; we care about the type it 1535 converts to. There may be multiple conversion functions that will 1536 convert to that type, and we rely on build_user_type_conversion_1 to 1537 choose the best one; so when we create our candidate, we record the type 1538 instead of the function. */ 1539 1540static struct z_candidate * 1541add_conv_candidate (candidates, fn, obj, arglist, access_path, 1542 conversion_path) 1543 struct z_candidate **candidates; 1544 tree fn, obj, arglist; 1545 tree access_path; 1546 tree conversion_path; 1547{ 1548 tree totype = TREE_TYPE (TREE_TYPE (fn)); 1549 int i, len, viable, flags; 1550 tree parmlist, convs, parmnode, argnode; 1551 1552 for (parmlist = totype; TREE_CODE (parmlist) != FUNCTION_TYPE; ) 1553 parmlist = TREE_TYPE (parmlist); 1554 parmlist = TYPE_ARG_TYPES (parmlist); 1555 1556 len = list_length (arglist) + 1; 1557 convs = make_tree_vec (len); 1558 parmnode = parmlist; 1559 argnode = arglist; 1560 viable = 1; 1561 flags = LOOKUP_NORMAL; 1562 1563 /* Don't bother looking up the same type twice. */ 1564 if (*candidates && (*candidates)->fn == totype) 1565 return NULL; 1566 1567 for (i = 0; i < len; ++i) 1568 { 1569 tree arg = i == 0 ? obj : TREE_VALUE (argnode); 1570 tree argtype = lvalue_type (arg); 1571 tree t; 1572 1573 if (i == 0) 1574 t = implicit_conversion (totype, argtype, arg, flags); 1575 else if (parmnode == void_list_node) 1576 break; 1577 else if (parmnode) 1578 t = implicit_conversion (TREE_VALUE (parmnode), argtype, arg, flags); 1579 else 1580 { 1581 t = build1 (IDENTITY_CONV, argtype, arg); 1582 ICS_ELLIPSIS_FLAG (t) = 1; 1583 } 1584 1585 TREE_VEC_ELT (convs, i) = t; 1586 if (! t) 1587 break; 1588 1589 if (ICS_BAD_FLAG (t)) 1590 viable = -1; 1591 1592 if (i == 0) 1593 continue; 1594 1595 if (parmnode) 1596 parmnode = TREE_CHAIN (parmnode); 1597 argnode = TREE_CHAIN (argnode); 1598 } 1599 1600 if (i < len) 1601 viable = 0; 1602 1603 if (!sufficient_parms_p (parmnode)) 1604 viable = 0; 1605 1606 return add_candidate (candidates, totype, convs, access_path, 1607 conversion_path, viable); 1608} 1609 1610static void 1611build_builtin_candidate (candidates, fnname, type1, type2, 1612 args, argtypes, flags) 1613 struct z_candidate **candidates; 1614 tree fnname, type1, type2, *args, *argtypes; 1615 int flags; 1616 1617{ 1618 tree t, convs; 1619 int viable = 1, i; 1620 tree types[2]; 1621 1622 types[0] = type1; 1623 types[1] = type2; 1624 1625 convs = make_tree_vec (args[2] ? 3 : (args[1] ? 2 : 1)); 1626 1627 for (i = 0; i < 2; ++i) 1628 { 1629 if (! args[i]) 1630 break; 1631 1632 t = implicit_conversion (types[i], argtypes[i], args[i], flags); 1633 if (! t) 1634 { 1635 viable = 0; 1636 /* We need something for printing the candidate. */ 1637 t = build1 (IDENTITY_CONV, types[i], NULL_TREE); 1638 } 1639 else if (ICS_BAD_FLAG (t)) 1640 viable = 0; 1641 TREE_VEC_ELT (convs, i) = t; 1642 } 1643 1644 /* For COND_EXPR we rearranged the arguments; undo that now. */ 1645 if (args[2]) 1646 { 1647 TREE_VEC_ELT (convs, 2) = TREE_VEC_ELT (convs, 1); 1648 TREE_VEC_ELT (convs, 1) = TREE_VEC_ELT (convs, 0); 1649 t = implicit_conversion (boolean_type_node, argtypes[2], args[2], flags); 1650 if (t) 1651 TREE_VEC_ELT (convs, 0) = t; 1652 else 1653 viable = 0; 1654 } 1655 1656 add_candidate (candidates, fnname, convs, 1657 /*access_path=*/NULL_TREE, 1658 /*conversion_path=*/NULL_TREE, 1659 viable); 1660} 1661 1662static int 1663is_complete (t) 1664 tree t; 1665{ 1666 return COMPLETE_TYPE_P (complete_type (t)); 1667} 1668 1669/* Returns nonzero if TYPE is a promoted arithmetic type. */ 1670 1671static int 1672promoted_arithmetic_type_p (type) 1673 tree type; 1674{ 1675 /* [over.built] 1676 1677 In this section, the term promoted integral type is used to refer 1678 to those integral types which are preserved by integral promotion 1679 (including e.g. int and long but excluding e.g. char). 1680 Similarly, the term promoted arithmetic type refers to promoted 1681 integral types plus floating types. */ 1682 return ((INTEGRAL_TYPE_P (type) 1683 && same_type_p (type_promotes_to (type), type)) 1684 || TREE_CODE (type) == REAL_TYPE); 1685} 1686 1687/* Create any builtin operator overload candidates for the operator in 1688 question given the converted operand types TYPE1 and TYPE2. The other 1689 args are passed through from add_builtin_candidates to 1690 build_builtin_candidate. 1691 1692 TYPE1 and TYPE2 may not be permissible, and we must filter them. 1693 If CODE is requires candidates operands of the same type of the kind 1694 of which TYPE1 and TYPE2 are, we add both candidates 1695 CODE (TYPE1, TYPE1) and CODE (TYPE2, TYPE2). */ 1696 1697static void 1698add_builtin_candidate (candidates, code, code2, fnname, type1, type2, 1699 args, argtypes, flags) 1700 struct z_candidate **candidates; 1701 enum tree_code code, code2; 1702 tree fnname, type1, type2, *args, *argtypes; 1703 int flags; 1704{ 1705 switch (code) 1706 { 1707 case POSTINCREMENT_EXPR: 1708 case POSTDECREMENT_EXPR: 1709 args[1] = integer_zero_node; 1710 type2 = integer_type_node; 1711 break; 1712 default: 1713 break; 1714 } 1715 1716 switch (code) 1717 { 1718 1719/* 4 For every pair T, VQ), where T is an arithmetic or enumeration type, 1720 and VQ is either volatile or empty, there exist candidate operator 1721 functions of the form 1722 VQ T& operator++(VQ T&); 1723 T operator++(VQ T&, int); 1724 5 For every pair T, VQ), where T is an enumeration type or an arithmetic 1725 type other than bool, and VQ is either volatile or empty, there exist 1726 candidate operator functions of the form 1727 VQ T& operator--(VQ T&); 1728 T operator--(VQ T&, int); 1729 6 For every pair T, VQ), where T is a cv-qualified or cv-unqualified 1730 complete object type, and VQ is either volatile or empty, there exist 1731 candidate operator functions of the form 1732 T*VQ& operator++(T*VQ&); 1733 T*VQ& operator--(T*VQ&); 1734 T* operator++(T*VQ&, int); 1735 T* operator--(T*VQ&, int); */ 1736 1737 case POSTDECREMENT_EXPR: 1738 case PREDECREMENT_EXPR: 1739 if (TREE_CODE (type1) == BOOLEAN_TYPE) 1740 return; 1741 case POSTINCREMENT_EXPR: 1742 case PREINCREMENT_EXPR: 1743 if (ARITHMETIC_TYPE_P (type1) || TYPE_PTROB_P (type1)) 1744 { 1745 type1 = build_reference_type (type1); 1746 break; 1747 } 1748 return; 1749 1750/* 7 For every cv-qualified or cv-unqualified complete object type T, there 1751 exist candidate operator functions of the form 1752 1753 T& operator*(T*); 1754 1755 8 For every function type T, there exist candidate operator functions of 1756 the form 1757 T& operator*(T*); */ 1758 1759 case INDIRECT_REF: 1760 if (TREE_CODE (type1) == POINTER_TYPE 1761 && (TYPE_PTROB_P (type1) 1762 || TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)) 1763 break; 1764 return; 1765 1766/* 9 For every type T, there exist candidate operator functions of the form 1767 T* operator+(T*); 1768 1769 10For every promoted arithmetic type T, there exist candidate operator 1770 functions of the form 1771 T operator+(T); 1772 T operator-(T); */ 1773 1774 case CONVERT_EXPR: /* unary + */ 1775 if (TREE_CODE (type1) == POINTER_TYPE 1776 && TREE_CODE (TREE_TYPE (type1)) != OFFSET_TYPE) 1777 break; 1778 case NEGATE_EXPR: 1779 if (ARITHMETIC_TYPE_P (type1)) 1780 break; 1781 return; 1782 1783/* 11For every promoted integral type T, there exist candidate operator 1784 functions of the form 1785 T operator~(T); */ 1786 1787 case BIT_NOT_EXPR: 1788 if (INTEGRAL_TYPE_P (type1)) 1789 break; 1790 return; 1791 1792/* 12For every quintuple C1, C2, T, CV1, CV2), where C2 is a class type, C1 1793 is the same type as C2 or is a derived class of C2, T is a complete 1794 object type or a function type, and CV1 and CV2 are cv-qualifier-seqs, 1795 there exist candidate operator functions of the form 1796 CV12 T& operator->*(CV1 C1*, CV2 T C2::*); 1797 where CV12 is the union of CV1 and CV2. */ 1798 1799 case MEMBER_REF: 1800 if (TREE_CODE (type1) == POINTER_TYPE 1801 && (TYPE_PTRMEMFUNC_P (type2) || TYPE_PTRMEM_P (type2))) 1802 { 1803 tree c1 = TREE_TYPE (type1); 1804 tree c2 = (TYPE_PTRMEMFUNC_P (type2) 1805 ? TYPE_METHOD_BASETYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (type2))) 1806 : TYPE_OFFSET_BASETYPE (TREE_TYPE (type2))); 1807 1808 if (IS_AGGR_TYPE (c1) && DERIVED_FROM_P (c2, c1) 1809 && (TYPE_PTRMEMFUNC_P (type2) 1810 || is_complete (TREE_TYPE (TREE_TYPE (type2))))) 1811 break; 1812 } 1813 return; 1814 1815/* 13For every pair of promoted arithmetic types L and R, there exist can- 1816 didate operator functions of the form 1817 LR operator*(L, R); 1818 LR operator/(L, R); 1819 LR operator+(L, R); 1820 LR operator-(L, R); 1821 bool operator<(L, R); 1822 bool operator>(L, R); 1823 bool operator<=(L, R); 1824 bool operator>=(L, R); 1825 bool operator==(L, R); 1826 bool operator!=(L, R); 1827 where LR is the result of the usual arithmetic conversions between 1828 types L and R. 1829 1830 14For every pair of types T and I, where T is a cv-qualified or cv- 1831 unqualified complete object type and I is a promoted integral type, 1832 there exist candidate operator functions of the form 1833 T* operator+(T*, I); 1834 T& operator[](T*, I); 1835 T* operator-(T*, I); 1836 T* operator+(I, T*); 1837 T& operator[](I, T*); 1838 1839 15For every T, where T is a pointer to complete object type, there exist 1840 candidate operator functions of the form112) 1841 ptrdiff_t operator-(T, T); 1842 1843 16For every pointer or enumeration type T, there exist candidate operator 1844 functions of the form 1845 bool operator<(T, T); 1846 bool operator>(T, T); 1847 bool operator<=(T, T); 1848 bool operator>=(T, T); 1849 bool operator==(T, T); 1850 bool operator!=(T, T); 1851 1852 17For every pointer to member type T, there exist candidate operator 1853 functions of the form 1854 bool operator==(T, T); 1855 bool operator!=(T, T); */ 1856 1857 case MINUS_EXPR: 1858 if (TYPE_PTROB_P (type1) && TYPE_PTROB_P (type2)) 1859 break; 1860 if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2)) 1861 { 1862 type2 = ptrdiff_type_node; 1863 break; 1864 } 1865 case MULT_EXPR: 1866 case TRUNC_DIV_EXPR: 1867 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2)) 1868 break; 1869 return; 1870 1871 case EQ_EXPR: 1872 case NE_EXPR: 1873 if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2)) 1874 || (TYPE_PTRMEM_P (type1) && TYPE_PTRMEM_P (type2))) 1875 break; 1876 if ((TYPE_PTRMEMFUNC_P (type1) || TYPE_PTRMEM_P (type1)) 1877 && null_ptr_cst_p (args[1])) 1878 { 1879 type2 = type1; 1880 break; 1881 } 1882 if ((TYPE_PTRMEMFUNC_P (type2) || TYPE_PTRMEM_P (type2)) 1883 && null_ptr_cst_p (args[0])) 1884 { 1885 type1 = type2; 1886 break; 1887 } 1888 /* FALLTHROUGH */ 1889 case LT_EXPR: 1890 case GT_EXPR: 1891 case LE_EXPR: 1892 case GE_EXPR: 1893 case MAX_EXPR: 1894 case MIN_EXPR: 1895 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2)) 1896 break; 1897 if (TYPE_PTR_P (type1) && TYPE_PTR_P (type2)) 1898 break; 1899 if (TREE_CODE (type1) == ENUMERAL_TYPE && TREE_CODE (type2) == ENUMERAL_TYPE) 1900 break; 1901 if (TYPE_PTR_P (type1) && null_ptr_cst_p (args[1])) 1902 { 1903 type2 = type1; 1904 break; 1905 } 1906 if (null_ptr_cst_p (args[0]) && TYPE_PTR_P (type2)) 1907 { 1908 type1 = type2; 1909 break; 1910 } 1911 return; 1912 1913 case PLUS_EXPR: 1914 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2)) 1915 break; 1916 case ARRAY_REF: 1917 if (INTEGRAL_TYPE_P (type1) && TYPE_PTROB_P (type2)) 1918 { 1919 type1 = ptrdiff_type_node; 1920 break; 1921 } 1922 if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2)) 1923 { 1924 type2 = ptrdiff_type_node; 1925 break; 1926 } 1927 return; 1928 1929/* 18For every pair of promoted integral types L and R, there exist candi- 1930 date operator functions of the form 1931 LR operator%(L, R); 1932 LR operator&(L, R); 1933 LR operator^(L, R); 1934 LR operator|(L, R); 1935 L operator<<(L, R); 1936 L operator>>(L, R); 1937 where LR is the result of the usual arithmetic conversions between 1938 types L and R. */ 1939 1940 case TRUNC_MOD_EXPR: 1941 case BIT_AND_EXPR: 1942 case BIT_IOR_EXPR: 1943 case BIT_XOR_EXPR: 1944 case LSHIFT_EXPR: 1945 case RSHIFT_EXPR: 1946 if (INTEGRAL_TYPE_P (type1) && INTEGRAL_TYPE_P (type2)) 1947 break; 1948 return; 1949 1950/* 19For every triple L, VQ, R), where L is an arithmetic or enumeration 1951 type, VQ is either volatile or empty, and R is a promoted arithmetic 1952 type, there exist candidate operator functions of the form 1953 VQ L& operator=(VQ L&, R); 1954 VQ L& operator*=(VQ L&, R); 1955 VQ L& operator/=(VQ L&, R); 1956 VQ L& operator+=(VQ L&, R); 1957 VQ L& operator-=(VQ L&, R); 1958 1959 20For every pair T, VQ), where T is any type and VQ is either volatile 1960 or empty, there exist candidate operator functions of the form 1961 T*VQ& operator=(T*VQ&, T*); 1962 1963 21For every pair T, VQ), where T is a pointer to member type and VQ is 1964 either volatile or empty, there exist candidate operator functions of 1965 the form 1966 VQ T& operator=(VQ T&, T); 1967 1968 22For every triple T, VQ, I), where T is a cv-qualified or cv- 1969 unqualified complete object type, VQ is either volatile or empty, and 1970 I is a promoted integral type, there exist candidate operator func- 1971 tions of the form 1972 T*VQ& operator+=(T*VQ&, I); 1973 T*VQ& operator-=(T*VQ&, I); 1974 1975 23For every triple L, VQ, R), where L is an integral or enumeration 1976 type, VQ is either volatile or empty, and R is a promoted integral 1977 type, there exist candidate operator functions of the form 1978 1979 VQ L& operator%=(VQ L&, R); 1980 VQ L& operator<<=(VQ L&, R); 1981 VQ L& operator>>=(VQ L&, R); 1982 VQ L& operator&=(VQ L&, R); 1983 VQ L& operator^=(VQ L&, R); 1984 VQ L& operator|=(VQ L&, R); */ 1985 1986 case MODIFY_EXPR: 1987 switch (code2) 1988 { 1989 case PLUS_EXPR: 1990 case MINUS_EXPR: 1991 if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2)) 1992 { 1993 type2 = ptrdiff_type_node; 1994 break; 1995 } 1996 case MULT_EXPR: 1997 case TRUNC_DIV_EXPR: 1998 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2)) 1999 break; 2000 return; 2001 2002 case TRUNC_MOD_EXPR: 2003 case BIT_AND_EXPR: 2004 case BIT_IOR_EXPR: 2005 case BIT_XOR_EXPR: 2006 case LSHIFT_EXPR: 2007 case RSHIFT_EXPR: 2008 if (INTEGRAL_TYPE_P (type1) && INTEGRAL_TYPE_P (type2)) 2009 break; 2010 return; 2011 2012 case NOP_EXPR: 2013 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2)) 2014 break; 2015 if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2)) 2016 || (TYPE_PTR_P (type1) && TYPE_PTR_P (type2)) 2017 || (TYPE_PTRMEM_P (type1) && TYPE_PTRMEM_P (type2)) 2018 || ((TYPE_PTRMEMFUNC_P (type1) 2019 || TREE_CODE (type1) == POINTER_TYPE) 2020 && null_ptr_cst_p (args[1]))) 2021 { 2022 type2 = type1; 2023 break; 2024 } 2025 return; 2026 2027 default: 2028 abort (); 2029 } 2030 type1 = build_reference_type (type1); 2031 break; 2032 2033 case COND_EXPR: 2034 /* [over.built] 2035 2036 For every pair of promoted arithmetic types L and R, there 2037 exist candidate operator functions of the form 2038 2039 LR operator?(bool, L, R); 2040 2041 where LR is the result of the usual arithmetic conversions 2042 between types L and R. 2043 2044 For every type T, where T is a pointer or pointer-to-member 2045 type, there exist candidate operator functions of the form T 2046 operator?(bool, T, T); */ 2047 2048 if (promoted_arithmetic_type_p (type1) 2049 && promoted_arithmetic_type_p (type2)) 2050 /* That's OK. */ 2051 break; 2052 2053 /* Otherwise, the types should be pointers. */ 2054 if (!(TREE_CODE (type1) == POINTER_TYPE 2055 || TYPE_PTRMEM_P (type1) 2056 || TYPE_PTRMEMFUNC_P (type1)) 2057 || !(TREE_CODE (type2) == POINTER_TYPE 2058 || TYPE_PTRMEM_P (type2) 2059 || TYPE_PTRMEMFUNC_P (type2))) 2060 return; 2061 2062 /* We don't check that the two types are the same; the logic 2063 below will actually create two candidates; one in which both 2064 parameter types are TYPE1, and one in which both parameter 2065 types are TYPE2. */ 2066 break; 2067 2068 default: 2069 abort (); 2070 } 2071 2072 /* If we're dealing with two pointer types or two enumeral types, 2073 we need candidates for both of them. */ 2074 if (type2 && !same_type_p (type1, type2) 2075 && TREE_CODE (type1) == TREE_CODE (type2) 2076 && (TREE_CODE (type1) == REFERENCE_TYPE 2077 || (TREE_CODE (type1) == POINTER_TYPE 2078 && TYPE_PTRMEM_P (type1) == TYPE_PTRMEM_P (type2)) 2079 || TYPE_PTRMEMFUNC_P (type1) 2080 || IS_AGGR_TYPE (type1) 2081 || TREE_CODE (type1) == ENUMERAL_TYPE)) 2082 { 2083 build_builtin_candidate 2084 (candidates, fnname, type1, type1, args, argtypes, flags); 2085 build_builtin_candidate 2086 (candidates, fnname, type2, type2, args, argtypes, flags); 2087 return; 2088 } 2089 2090 build_builtin_candidate 2091 (candidates, fnname, type1, type2, args, argtypes, flags); 2092} 2093 2094tree 2095type_decays_to (type) 2096 tree type; 2097{ 2098 if (TREE_CODE (type) == ARRAY_TYPE) 2099 return build_pointer_type (TREE_TYPE (type)); 2100 if (TREE_CODE (type) == FUNCTION_TYPE) 2101 return build_pointer_type (type); 2102 return type; 2103} 2104 2105/* There are three conditions of builtin candidates: 2106 2107 1) bool-taking candidates. These are the same regardless of the input. 2108 2) pointer-pair taking candidates. These are generated for each type 2109 one of the input types converts to. 2110 3) arithmetic candidates. According to the standard, we should generate 2111 all of these, but I'm trying not to... 2112 2113 Here we generate a superset of the possible candidates for this particular 2114 case. That is a subset of the full set the standard defines, plus some 2115 other cases which the standard disallows. add_builtin_candidate will 2116 filter out the invalid set. */ 2117 2118static void 2119add_builtin_candidates (candidates, code, code2, fnname, args, flags) 2120 struct z_candidate **candidates; 2121 enum tree_code code, code2; 2122 tree fnname, *args; 2123 int flags; 2124{ 2125 int ref1, i; 2126 int enum_p = 0; 2127 tree type, argtypes[3]; 2128 /* TYPES[i] is the set of possible builtin-operator parameter types 2129 we will consider for the Ith argument. These are represented as 2130 a TREE_LIST; the TREE_VALUE of each node is the potential 2131 parameter type. */ 2132 tree types[2]; 2133 2134 for (i = 0; i < 3; ++i) 2135 { 2136 if (args[i]) 2137 argtypes[i] = lvalue_type (args[i]); 2138 else 2139 argtypes[i] = NULL_TREE; 2140 } 2141 2142 switch (code) 2143 { 2144/* 4 For every pair T, VQ), where T is an arithmetic or enumeration type, 2145 and VQ is either volatile or empty, there exist candidate operator 2146 functions of the form 2147 VQ T& operator++(VQ T&); */ 2148 2149 case POSTINCREMENT_EXPR: 2150 case PREINCREMENT_EXPR: 2151 case POSTDECREMENT_EXPR: 2152 case PREDECREMENT_EXPR: 2153 case MODIFY_EXPR: 2154 ref1 = 1; 2155 break; 2156 2157/* 24There also exist candidate operator functions of the form 2158 bool operator!(bool); 2159 bool operator&&(bool, bool); 2160 bool operator||(bool, bool); */ 2161 2162 case TRUTH_NOT_EXPR: 2163 build_builtin_candidate 2164 (candidates, fnname, boolean_type_node, 2165 NULL_TREE, args, argtypes, flags); 2166 return; 2167 2168 case TRUTH_ORIF_EXPR: 2169 case TRUTH_ANDIF_EXPR: 2170 build_builtin_candidate 2171 (candidates, fnname, boolean_type_node, 2172 boolean_type_node, args, argtypes, flags); 2173 return; 2174 2175 case ADDR_EXPR: 2176 case COMPOUND_EXPR: 2177 case COMPONENT_REF: 2178 return; 2179 2180 case COND_EXPR: 2181 case EQ_EXPR: 2182 case NE_EXPR: 2183 case LT_EXPR: 2184 case LE_EXPR: 2185 case GT_EXPR: 2186 case GE_EXPR: 2187 enum_p = 1; 2188 /* FALLTHROUGH */ 2189 2190 default: 2191 ref1 = 0; 2192 } 2193 2194 types[0] = types[1] = NULL_TREE; 2195 2196 for (i = 0; i < 2; ++i) 2197 { 2198 if (! args[i]) 2199 ; 2200 else if (IS_AGGR_TYPE (argtypes[i])) 2201 { 2202 tree convs; 2203 2204 if (i == 0 && code == MODIFY_EXPR && code2 == NOP_EXPR) 2205 return; 2206 2207 convs = lookup_conversions (argtypes[i]); 2208 2209 if (code == COND_EXPR) 2210 { 2211 if (real_lvalue_p (args[i])) 2212 types[i] = tree_cons 2213 (NULL_TREE, build_reference_type (argtypes[i]), types[i]); 2214 2215 types[i] = tree_cons 2216 (NULL_TREE, TYPE_MAIN_VARIANT (argtypes[i]), types[i]); 2217 } 2218 2219 else if (! convs) 2220 return; 2221 2222 for (; convs; convs = TREE_CHAIN (convs)) 2223 { 2224 type = TREE_TYPE (TREE_TYPE (OVL_CURRENT (TREE_VALUE (convs)))); 2225 2226 if (i == 0 && ref1 2227 && (TREE_CODE (type) != REFERENCE_TYPE 2228 || CP_TYPE_CONST_P (TREE_TYPE (type)))) 2229 continue; 2230 2231 if (code == COND_EXPR && TREE_CODE (type) == REFERENCE_TYPE) 2232 types[i] = tree_cons (NULL_TREE, type, types[i]); 2233 2234 type = non_reference (type); 2235 if (i != 0 || ! ref1) 2236 { 2237 type = TYPE_MAIN_VARIANT (type_decays_to (type)); 2238 if (enum_p && TREE_CODE (type) == ENUMERAL_TYPE) 2239 types[i] = tree_cons (NULL_TREE, type, types[i]); 2240 if (INTEGRAL_TYPE_P (type)) 2241 type = type_promotes_to (type); 2242 } 2243 2244 if (! value_member (type, types[i])) 2245 types[i] = tree_cons (NULL_TREE, type, types[i]); 2246 } 2247 } 2248 else 2249 { 2250 if (code == COND_EXPR && real_lvalue_p (args[i])) 2251 types[i] = tree_cons 2252 (NULL_TREE, build_reference_type (argtypes[i]), types[i]); 2253 type = non_reference (argtypes[i]); 2254 if (i != 0 || ! ref1) 2255 { 2256 type = TYPE_MAIN_VARIANT (type_decays_to (type)); 2257 if (enum_p && TREE_CODE (type) == ENUMERAL_TYPE) 2258 types[i] = tree_cons (NULL_TREE, type, types[i]); 2259 if (INTEGRAL_TYPE_P (type)) 2260 type = type_promotes_to (type); 2261 } 2262 types[i] = tree_cons (NULL_TREE, type, types[i]); 2263 } 2264 } 2265 2266 /* Run through the possible parameter types of both arguments, 2267 creating candidates with those parameter types. */ 2268 for (; types[0]; types[0] = TREE_CHAIN (types[0])) 2269 { 2270 if (types[1]) 2271 for (type = types[1]; type; type = TREE_CHAIN (type)) 2272 add_builtin_candidate 2273 (candidates, code, code2, fnname, TREE_VALUE (types[0]), 2274 TREE_VALUE (type), args, argtypes, flags); 2275 else 2276 add_builtin_candidate 2277 (candidates, code, code2, fnname, TREE_VALUE (types[0]), 2278 NULL_TREE, args, argtypes, flags); 2279 } 2280 2281 return; 2282} 2283 2284 2285/* If TMPL can be successfully instantiated as indicated by 2286 EXPLICIT_TARGS and ARGLIST, adds the instantiation to CANDIDATES. 2287 2288 TMPL is the template. EXPLICIT_TARGS are any explicit template 2289 arguments. ARGLIST is the arguments provided at the call-site. 2290 The RETURN_TYPE is the desired type for conversion operators. If 2291 OBJ is NULL_TREE, FLAGS and CTYPE are as for add_function_candidate. 2292 If an OBJ is supplied, FLAGS and CTYPE are ignored, and OBJ is as for 2293 add_conv_candidate. */ 2294 2295static struct z_candidate* 2296add_template_candidate_real (candidates, tmpl, ctype, explicit_targs, 2297 arglist, return_type, access_path, 2298 conversion_path, flags, obj, strict) 2299 struct z_candidate **candidates; 2300 tree tmpl, ctype, explicit_targs, arglist, return_type; 2301 tree access_path; 2302 tree conversion_path; 2303 int flags; 2304 tree obj; 2305 unification_kind_t strict; 2306{ 2307 int ntparms = DECL_NTPARMS (tmpl); 2308 tree targs = make_tree_vec (ntparms); 2309 tree args_without_in_chrg = arglist; 2310 struct z_candidate *cand; 2311 int i; 2312 tree fn; 2313 2314 /* We don't do deduction on the in-charge parameter, the VTT 2315 parameter or 'this'. */ 2316 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (tmpl)) 2317 args_without_in_chrg = TREE_CHAIN (args_without_in_chrg); 2318 2319 if ((DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (tmpl) 2320 || DECL_BASE_CONSTRUCTOR_P (tmpl)) 2321 && TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (tmpl))) 2322 args_without_in_chrg = TREE_CHAIN (args_without_in_chrg); 2323 2324 i = fn_type_unification (tmpl, explicit_targs, targs, 2325 args_without_in_chrg, 2326 return_type, strict, -1); 2327 2328 if (i != 0) 2329 return NULL; 2330 2331 fn = instantiate_template (tmpl, targs); 2332 if (fn == error_mark_node) 2333 return NULL; 2334 2335 /* In [class.copy]: 2336 2337 A member function template is never instantiated to perform the 2338 copy of a class object to an object of its class type. 2339 2340 It's a little unclear what this means; the standard explicitly 2341 does allow a template to be used to copy a class. For example, 2342 in: 2343 2344 struct A { 2345 A(A&); 2346 template <class T> A(const T&); 2347 }; 2348 const A f (); 2349 void g () { A a (f ()); } 2350 2351 the member template will be used to make the copy. The section 2352 quoted above appears in the paragraph that forbids constructors 2353 whose only parameter is (a possibly cv-qualified variant of) the 2354 class type, and a logical interpretation is that the intent was 2355 to forbid the instantiation of member templates which would then 2356 have that form. */ 2357 if (DECL_CONSTRUCTOR_P (fn) && list_length (arglist) == 2) 2358 { 2359 tree arg_types = FUNCTION_FIRST_USER_PARMTYPE (fn); 2360 if (arg_types && same_type_p (TYPE_MAIN_VARIANT (TREE_VALUE (arg_types)), 2361 ctype)) 2362 return NULL; 2363 } 2364 2365 if (obj != NULL_TREE) 2366 /* Aha, this is a conversion function. */ 2367 cand = add_conv_candidate (candidates, fn, obj, access_path, 2368 conversion_path, arglist); 2369 else 2370 cand = add_function_candidate (candidates, fn, ctype, 2371 arglist, access_path, 2372 conversion_path, flags); 2373 if (DECL_TI_TEMPLATE (fn) != tmpl) 2374 /* This situation can occur if a member template of a template 2375 class is specialized. Then, instantiate_template might return 2376 an instantiation of the specialization, in which case the 2377 DECL_TI_TEMPLATE field will point at the original 2378 specialization. For example: 2379 2380 template <class T> struct S { template <class U> void f(U); 2381 template <> void f(int) {}; }; 2382 S<double> sd; 2383 sd.f(3); 2384 2385 Here, TMPL will be template <class U> S<double>::f(U). 2386 And, instantiate template will give us the specialization 2387 template <> S<double>::f(int). But, the DECL_TI_TEMPLATE field 2388 for this will point at template <class T> template <> S<T>::f(int), 2389 so that we can find the definition. For the purposes of 2390 overload resolution, however, we want the original TMPL. */ 2391 cand->template = tree_cons (tmpl, targs, NULL_TREE); 2392 else 2393 cand->template = DECL_TEMPLATE_INFO (fn); 2394 2395 return cand; 2396} 2397 2398 2399static struct z_candidate * 2400add_template_candidate (candidates, tmpl, ctype, explicit_targs, 2401 arglist, return_type, access_path, 2402 conversion_path, flags, strict) 2403 struct z_candidate **candidates; 2404 tree tmpl, ctype, explicit_targs, arglist, return_type; 2405 tree access_path; 2406 tree conversion_path; 2407 int flags; 2408 unification_kind_t strict; 2409{ 2410 return 2411 add_template_candidate_real (candidates, tmpl, ctype, 2412 explicit_targs, arglist, return_type, 2413 access_path, conversion_path, 2414 flags, NULL_TREE, strict); 2415} 2416 2417 2418static struct z_candidate * 2419add_template_conv_candidate (candidates, tmpl, obj, arglist, return_type, 2420 access_path, conversion_path) 2421 struct z_candidate **candidates; 2422 tree tmpl, obj, arglist, return_type; 2423 tree access_path; 2424 tree conversion_path; 2425{ 2426 return 2427 add_template_candidate_real (candidates, tmpl, NULL_TREE, NULL_TREE, 2428 arglist, return_type, access_path, 2429 conversion_path, 0, obj, DEDUCE_CONV); 2430} 2431 2432 2433static int 2434any_viable (cands) 2435 struct z_candidate *cands; 2436{ 2437 for (; cands; cands = cands->next) 2438 if (pedantic ? cands->viable == 1 : cands->viable) 2439 return 1; 2440 return 0; 2441} 2442 2443static int 2444any_strictly_viable (cands) 2445 struct z_candidate *cands; 2446{ 2447 for (; cands; cands = cands->next) 2448 if (cands->viable == 1) 2449 return 1; 2450 return 0; 2451} 2452 2453static struct z_candidate * 2454splice_viable (cands) 2455 struct z_candidate *cands; 2456{ 2457 struct z_candidate **p = &cands; 2458 2459 for (; *p; ) 2460 { 2461 if (pedantic ? (*p)->viable == 1 : (*p)->viable) 2462 p = &((*p)->next); 2463 else 2464 *p = (*p)->next; 2465 } 2466 2467 return cands; 2468} 2469 2470static tree 2471build_this (obj) 2472 tree obj; 2473{ 2474 /* Fix this to work on non-lvalues. */ 2475 return build_unary_op (ADDR_EXPR, obj, 0); 2476} 2477 2478/* Returns true iff functions are equivalent. Equivalent functions are 2479 not '==' only if one is a function-local extern function or if 2480 both are extern "C". */ 2481 2482static inline int 2483equal_functions (fn1, fn2) 2484 tree fn1; 2485 tree fn2; 2486{ 2487 if (DECL_LOCAL_FUNCTION_P (fn1) || DECL_LOCAL_FUNCTION_P (fn2) 2488 || DECL_EXTERN_C_FUNCTION_P (fn1)) 2489 return decls_match (fn1, fn2); 2490 return fn1 == fn2; 2491} 2492 2493static void 2494print_z_candidates (struct z_candidate *candidates) 2495{ 2496 const char *str; 2497 struct z_candidate *cand1; 2498 struct z_candidate **cand2; 2499 2500 /* There may be duplicates in the set of candidates. We put off 2501 checking this condition as long as possible, since we have no way 2502 to eliminate duplicates from a set of functions in less than n^2 2503 time. Now we are about to emit an error message, so it is more 2504 permissible to go slowly. */ 2505 for (cand1 = candidates; cand1; cand1 = cand1->next) 2506 { 2507 tree fn = cand1->fn; 2508 /* Skip builtin candidates and conversion functions. */ 2509 if (TREE_CODE (fn) != FUNCTION_DECL) 2510 continue; 2511 cand2 = &cand1->next; 2512 while (*cand2) 2513 { 2514 if (TREE_CODE ((*cand2)->fn) == FUNCTION_DECL 2515 && equal_functions (fn, (*cand2)->fn)) 2516 *cand2 = (*cand2)->next; 2517 else 2518 cand2 = &(*cand2)->next; 2519 } 2520 } 2521 2522 str = "candidates are:"; 2523 for (; candidates; candidates = candidates->next) 2524 { 2525 if (TREE_CODE (candidates->fn) == IDENTIFIER_NODE) 2526 { 2527 if (TREE_VEC_LENGTH (candidates->convs) == 3) 2528 error ("%s %D(%T, %T, %T) <built-in>", str, candidates->fn, 2529 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 0)), 2530 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 1)), 2531 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 2))); 2532 else if (TREE_VEC_LENGTH (candidates->convs) == 2) 2533 error ("%s %D(%T, %T) <built-in>", str, candidates->fn, 2534 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 0)), 2535 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 1))); 2536 else 2537 error ("%s %D(%T) <built-in>", str, candidates->fn, 2538 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 0))); 2539 } 2540 else if (TYPE_P (candidates->fn)) 2541 error ("%s %T <conversion>", str, candidates->fn); 2542 else 2543 cp_error_at ("%s %+#D%s", str, candidates->fn, 2544 candidates->viable == -1 ? " <near match>" : ""); 2545 str = " "; 2546 } 2547} 2548 2549/* USER_SEQ is a user-defined conversion sequence, beginning with a 2550 USER_CONV. STD_SEQ is the standard conversion sequence applied to 2551 the result of the conversion function to convert it to the final 2552 desired type. Merge the the two sequences into a single sequence, 2553 and return the merged sequence. */ 2554 2555static tree 2556merge_conversion_sequences (tree user_seq, tree std_seq) 2557{ 2558 tree *t; 2559 2560 my_friendly_assert (TREE_CODE (user_seq) == USER_CONV, 2561 20030306); 2562 2563 /* Find the end of the second conversion sequence. */ 2564 t = &(std_seq); 2565 while (TREE_CODE (*t) != IDENTITY_CONV) 2566 t = &TREE_OPERAND (*t, 0); 2567 2568 /* Replace the identity conversion with the user conversion 2569 sequence. */ 2570 *t = user_seq; 2571 2572 /* The entire sequence is a user-conversion sequence. */ 2573 ICS_USER_FLAG (std_seq) = 1; 2574 2575 return std_seq; 2576} 2577 2578/* Returns the best overload candidate to perform the requested 2579 conversion. This function is used for three the overloading situations 2580 described in [over.match.copy], [over.match.conv], and [over.match.ref]. 2581 If TOTYPE is a REFERENCE_TYPE, we're trying to find an lvalue binding as 2582 per [dcl.init.ref], so we ignore temporary bindings. */ 2583 2584static struct z_candidate * 2585build_user_type_conversion_1 (totype, expr, flags) 2586 tree totype, expr; 2587 int flags; 2588{ 2589 struct z_candidate *candidates, *cand; 2590 tree fromtype = TREE_TYPE (expr); 2591 tree ctors = NULL_TREE, convs = NULL_TREE; 2592 tree args = NULL_TREE; 2593 2594 /* We represent conversion within a hierarchy using RVALUE_CONV and 2595 BASE_CONV, as specified by [over.best.ics]; these become plain 2596 constructor calls, as specified in [dcl.init]. */ 2597 my_friendly_assert (!IS_AGGR_TYPE (fromtype) || !IS_AGGR_TYPE (totype) 2598 || !DERIVED_FROM_P (totype, fromtype), 20011226); 2599 2600 if (IS_AGGR_TYPE (totype)) 2601 ctors = lookup_fnfields (TYPE_BINFO (totype), 2602 complete_ctor_identifier, 2603 0); 2604 2605 if (IS_AGGR_TYPE (fromtype)) 2606 convs = lookup_conversions (fromtype); 2607 2608 candidates = 0; 2609 flags |= LOOKUP_NO_CONVERSION; 2610 2611 if (ctors) 2612 { 2613 tree t; 2614 2615 ctors = BASELINK_FUNCTIONS (ctors); 2616 2617 t = build_int_2 (0, 0); 2618 TREE_TYPE (t) = build_pointer_type (totype); 2619 args = build_tree_list (NULL_TREE, expr); 2620 /* We should never try to call the abstract or base constructor 2621 from here. */ 2622 my_friendly_assert (!DECL_HAS_IN_CHARGE_PARM_P (OVL_CURRENT (ctors)) 2623 && !DECL_HAS_VTT_PARM_P (OVL_CURRENT (ctors)), 2624 20011226); 2625 args = tree_cons (NULL_TREE, t, args); 2626 } 2627 for (; ctors; ctors = OVL_NEXT (ctors)) 2628 { 2629 tree ctor = OVL_CURRENT (ctors); 2630 if (DECL_NONCONVERTING_P (ctor)) 2631 continue; 2632 2633 if (TREE_CODE (ctor) == TEMPLATE_DECL) 2634 cand = add_template_candidate (&candidates, ctor, totype, 2635 NULL_TREE, args, NULL_TREE, 2636 TYPE_BINFO (totype), 2637 TYPE_BINFO (totype), 2638 flags, 2639 DEDUCE_CALL); 2640 else 2641 cand = add_function_candidate (&candidates, ctor, totype, 2642 args, TYPE_BINFO (totype), 2643 TYPE_BINFO (totype), 2644 flags); 2645 2646 if (cand) 2647 cand->second_conv = build1 (IDENTITY_CONV, totype, NULL_TREE); 2648 } 2649 2650 if (convs) 2651 args = build_tree_list (NULL_TREE, build_this (expr)); 2652 2653 for (; convs; convs = TREE_CHAIN (convs)) 2654 { 2655 tree fns; 2656 tree conversion_path = TREE_PURPOSE (convs); 2657 int convflags = LOOKUP_NO_CONVERSION; 2658 2659 /* If we are called to convert to a reference type, we are trying to 2660 find an lvalue binding, so don't even consider temporaries. If 2661 we don't find an lvalue binding, the caller will try again to 2662 look for a temporary binding. */ 2663 if (TREE_CODE (totype) == REFERENCE_TYPE) 2664 convflags |= LOOKUP_NO_TEMP_BIND; 2665 2666 for (fns = TREE_VALUE (convs); fns; fns = OVL_NEXT (fns)) 2667 { 2668 tree fn = OVL_CURRENT (fns); 2669 2670 /* [over.match.funcs] For conversion functions, the function 2671 is considered to be a member of the class of the implicit 2672 object argument for the purpose of defining the type of 2673 the implicit object parameter. 2674 2675 So we pass fromtype as CTYPE to add_*_candidate. */ 2676 2677 if (TREE_CODE (fn) == TEMPLATE_DECL) 2678 cand = add_template_candidate (&candidates, fn, fromtype, NULL_TREE, 2679 args, totype, 2680 TYPE_BINFO (fromtype), 2681 conversion_path, 2682 flags, 2683 DEDUCE_CONV); 2684 else 2685 cand = add_function_candidate (&candidates, fn, fromtype, 2686 args, 2687 TYPE_BINFO (fromtype), 2688 conversion_path, 2689 flags); 2690 2691 if (cand) 2692 { 2693 tree ics = implicit_conversion 2694 (totype, TREE_TYPE (TREE_TYPE (cand->fn)), 2695 0, convflags); 2696 2697 cand->second_conv = ics; 2698 2699 if (ics == NULL_TREE) 2700 cand->viable = 0; 2701 else if (cand->viable == 1 && ICS_BAD_FLAG (ics)) 2702 cand->viable = -1; 2703 } 2704 } 2705 } 2706 2707 if (! any_viable (candidates)) 2708 return 0; 2709 2710 candidates = splice_viable (candidates); 2711 cand = tourney (candidates); 2712 2713 if (cand == 0) 2714 { 2715 if (flags & LOOKUP_COMPLAIN) 2716 { 2717 error ("conversion from `%T' to `%T' is ambiguous", 2718 fromtype, totype); 2719 print_z_candidates (candidates); 2720 } 2721 2722 cand = candidates; /* any one will do */ 2723 cand->second_conv = build1 (AMBIG_CONV, totype, expr); 2724 ICS_USER_FLAG (cand->second_conv) = 1; 2725 if (!any_strictly_viable (candidates)) 2726 ICS_BAD_FLAG (cand->second_conv) = 1; 2727 /* If there are viable candidates, don't set ICS_BAD_FLAG; an 2728 ambiguous conversion is no worse than another user-defined 2729 conversion. */ 2730 2731 return cand; 2732 } 2733 2734 /* Build the user conversion sequence. */ 2735 convs = build_conv 2736 (USER_CONV, 2737 (DECL_CONSTRUCTOR_P (cand->fn) 2738 ? totype : non_reference (TREE_TYPE (TREE_TYPE (cand->fn)))), 2739 build1 (IDENTITY_CONV, TREE_TYPE (expr), expr)); 2740 TREE_OPERAND (convs, 1) = build_zc_wrapper (cand); 2741 2742 /* Combine it with the second conversion sequence. */ 2743 cand->second_conv = merge_conversion_sequences (convs, 2744 cand->second_conv); 2745 2746 if (cand->viable == -1) 2747 ICS_BAD_FLAG (cand->second_conv) = 1; 2748 2749 return cand; 2750} 2751 2752tree 2753build_user_type_conversion (totype, expr, flags) 2754 tree totype, expr; 2755 int flags; 2756{ 2757 struct z_candidate *cand 2758 = build_user_type_conversion_1 (totype, expr, flags); 2759 2760 if (cand) 2761 { 2762 if (TREE_CODE (cand->second_conv) == AMBIG_CONV) 2763 return error_mark_node; 2764 return convert_from_reference (convert_like (cand->second_conv, expr)); 2765 } 2766 return NULL_TREE; 2767} 2768 2769/* Find the possibly overloaded set of functions corresponding to a 2770 call of the form SCOPE::NAME (...). NAME might be a 2771 TEMPLATE_ID_EXPR, OVERLOAD, _DECL, IDENTIFIER_NODE or LOOKUP_EXPR. */ 2772 2773tree 2774resolve_scoped_fn_name (tree scope, tree name) 2775{ 2776 tree fn; 2777 tree template_args = NULL_TREE; 2778 bool is_template_id = TREE_CODE (name) == TEMPLATE_ID_EXPR; 2779 2780 if (is_template_id) 2781 { 2782 template_args = TREE_OPERAND (name, 1); 2783 name = TREE_OPERAND (name, 0); 2784 } 2785 if (TREE_CODE (name) == OVERLOAD) 2786 name = DECL_NAME (get_first_fn (name)); 2787 else if (TREE_CODE (name) == LOOKUP_EXPR) 2788 name = TREE_OPERAND (name, 0); 2789 2790 if (TREE_CODE (scope) == NAMESPACE_DECL) 2791 fn = lookup_namespace_name (scope, name); 2792 else if (!CLASS_TYPE_P (scope)) 2793 { 2794 error ("`%T' is not a class type", scope); 2795 return error_mark_node; 2796 } 2797 else 2798 { 2799 if (!TYPE_BEING_DEFINED (scope) 2800 && !COMPLETE_TYPE_P (complete_type (scope))) 2801 { 2802 error ("incomplete type '%T' cannot be used to name a scope", 2803 scope); 2804 return error_mark_node; 2805 } 2806 2807 if (BASELINK_P (name)) 2808 fn = name; 2809 else 2810 fn = lookup_member (scope, name, /*protect=*/1, /*prefer_type=*/0); 2811 if (fn && current_class_type) 2812 fn = (adjust_result_of_qualified_name_lookup 2813 (fn, scope, current_class_type)); 2814 2815 /* It might be the name of a function pointer member. */ 2816 if (fn && TREE_CODE (fn) == FIELD_DECL) 2817 fn = resolve_offset_ref (build_offset_ref (scope, fn)); 2818 } 2819 2820 if (!fn) 2821 { 2822 error ("'%D' has no member named '%E'", scope, name); 2823 return error_mark_node; 2824 } 2825 if (is_template_id) 2826 { 2827 tree fns = fn; 2828 2829 if (BASELINK_P (fn)) 2830 fns = BASELINK_FUNCTIONS (fns); 2831 fns = build_nt (TEMPLATE_ID_EXPR, fns, template_args); 2832 if (BASELINK_P (fn)) 2833 BASELINK_FUNCTIONS (fn) = fns; 2834 else 2835 fn = fns; 2836 } 2837 2838 return fn; 2839} 2840 2841/* Do any initial processing on the arguments to a function call. */ 2842 2843static tree 2844resolve_args (args) 2845 tree args; 2846{ 2847 tree t; 2848 for (t = args; t; t = TREE_CHAIN (t)) 2849 { 2850 tree arg = TREE_VALUE (t); 2851 2852 if (arg == error_mark_node) 2853 return error_mark_node; 2854 else if (VOID_TYPE_P (TREE_TYPE (arg))) 2855 { 2856 error ("invalid use of void expression"); 2857 return error_mark_node; 2858 } 2859 else if (TREE_CODE (arg) == OFFSET_REF) 2860 arg = resolve_offset_ref (arg); 2861 arg = convert_from_reference (arg); 2862 TREE_VALUE (t) = arg; 2863 } 2864 return args; 2865} 2866 2867/* Return an expression for a call to FN (a namespace-scope function) 2868 with the ARGS. */ 2869 2870tree 2871build_new_function_call (fn, args) 2872 tree fn, args; 2873{ 2874 struct z_candidate *candidates = 0, *cand; 2875 tree explicit_targs = NULL_TREE; 2876 int template_only = 0; 2877 2878 /* Check FN and ARGS. */ 2879 my_friendly_assert (TREE_CODE (fn) == FUNCTION_DECL 2880 || TREE_CODE (fn) == TEMPLATE_DECL 2881 || TREE_CODE (fn) == OVERLOAD 2882 || TREE_CODE (fn) == TEMPLATE_ID_EXPR, 2883 20020712); 2884 my_friendly_assert (!args || TREE_CODE (args) == TREE_LIST, 2885 20020712); 2886 2887 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR) 2888 { 2889 explicit_targs = TREE_OPERAND (fn, 1); 2890 fn = TREE_OPERAND (fn, 0); 2891 template_only = 1; 2892 } 2893 2894 if (really_overloaded_fn (fn) 2895 || TREE_CODE (fn) == TEMPLATE_DECL) 2896 { 2897 tree t1; 2898 2899 args = resolve_args (args); 2900 2901 if (args == error_mark_node) 2902 return error_mark_node; 2903 2904 for (t1 = fn; t1; t1 = OVL_NEXT (t1)) 2905 { 2906 tree t = OVL_CURRENT (t1); 2907 2908 if (TREE_CODE (t) == TEMPLATE_DECL) 2909 add_template_candidate 2910 (&candidates, t, NULL_TREE, explicit_targs, args, 2911 NULL_TREE, 2912 /*access_path=*/NULL_TREE, /*conversion_path=*/NULL_TREE, 2913 LOOKUP_NORMAL, DEDUCE_CALL); 2914 else if (! template_only) 2915 add_function_candidate 2916 (&candidates, t, NULL_TREE, args, /*access_path=*/NULL_TREE, 2917 /*conversion_path=*/NULL_TREE, LOOKUP_NORMAL); 2918 } 2919 2920 if (! any_viable (candidates)) 2921 { 2922 if (candidates && ! candidates->next) 2923 return build_function_call (candidates->fn, args); 2924 error ("no matching function for call to `%D(%A)'", 2925 DECL_NAME (OVL_CURRENT (fn)), args); 2926 if (candidates) 2927 print_z_candidates (candidates); 2928 return error_mark_node; 2929 } 2930 candidates = splice_viable (candidates); 2931 cand = tourney (candidates); 2932 2933 if (cand == 0) 2934 { 2935 error ("call of overloaded `%D(%A)' is ambiguous", 2936 DECL_NAME (OVL_FUNCTION (fn)), args); 2937 print_z_candidates (candidates); 2938 return error_mark_node; 2939 } 2940 2941 return build_over_call (cand, args, LOOKUP_NORMAL); 2942 } 2943 2944 /* This is not really overloaded. */ 2945 fn = OVL_CURRENT (fn); 2946 2947 return build_function_call (fn, args); 2948} 2949 2950static tree 2951build_object_call (obj, args) 2952 tree obj, args; 2953{ 2954 struct z_candidate *candidates = 0, *cand; 2955 tree fns, convs, mem_args = NULL_TREE; 2956 tree type = TREE_TYPE (obj); 2957 2958 if (TYPE_PTRMEMFUNC_P (type)) 2959 { 2960 /* It's no good looking for an overloaded operator() on a 2961 pointer-to-member-function. */ 2962 error ("pointer-to-member function %E cannot be called without an object; consider using .* or ->*", obj); 2963 return error_mark_node; 2964 } 2965 2966 fns = lookup_fnfields (TYPE_BINFO (type), ansi_opname (CALL_EXPR), 1); 2967 if (fns == error_mark_node) 2968 return error_mark_node; 2969 2970 args = resolve_args (args); 2971 2972 if (args == error_mark_node) 2973 return error_mark_node; 2974 2975 if (fns) 2976 { 2977 tree base = BINFO_TYPE (BASELINK_BINFO (fns)); 2978 mem_args = tree_cons (NULL_TREE, build_this (obj), args); 2979 2980 for (fns = BASELINK_FUNCTIONS (fns); fns; fns = OVL_NEXT (fns)) 2981 { 2982 tree fn = OVL_CURRENT (fns); 2983 if (TREE_CODE (fn) == TEMPLATE_DECL) 2984 add_template_candidate (&candidates, fn, base, NULL_TREE, 2985 mem_args, NULL_TREE, 2986 TYPE_BINFO (type), 2987 TYPE_BINFO (type), 2988 LOOKUP_NORMAL, DEDUCE_CALL); 2989 else 2990 add_function_candidate 2991 (&candidates, fn, base, mem_args, TYPE_BINFO (type), 2992 TYPE_BINFO (type), LOOKUP_NORMAL); 2993 } 2994 } 2995 2996 convs = lookup_conversions (type); 2997 2998 for (; convs; convs = TREE_CHAIN (convs)) 2999 { 3000 tree fns = TREE_VALUE (convs); 3001 tree totype = TREE_TYPE (TREE_TYPE (OVL_CURRENT (fns))); 3002 3003 if ((TREE_CODE (totype) == POINTER_TYPE 3004 && TREE_CODE (TREE_TYPE (totype)) == FUNCTION_TYPE) 3005 || (TREE_CODE (totype) == REFERENCE_TYPE 3006 && TREE_CODE (TREE_TYPE (totype)) == FUNCTION_TYPE) 3007 || (TREE_CODE (totype) == REFERENCE_TYPE 3008 && TREE_CODE (TREE_TYPE (totype)) == POINTER_TYPE 3009 && TREE_CODE (TREE_TYPE (TREE_TYPE (totype))) == FUNCTION_TYPE)) 3010 for (; fns; fns = OVL_NEXT (fns)) 3011 { 3012 tree fn = OVL_CURRENT (fns); 3013 if (TREE_CODE (fn) == TEMPLATE_DECL) 3014 add_template_conv_candidate 3015 (&candidates, fn, obj, args, totype, 3016 /*access_path=*/NULL_TREE, 3017 /*conversion_path=*/NULL_TREE); 3018 else 3019 add_conv_candidate (&candidates, fn, obj, args, 3020 /*conversion_path=*/NULL_TREE, 3021 /*access_path=*/NULL_TREE); 3022 } 3023 } 3024 3025 if (! any_viable (candidates)) 3026 { 3027 error ("no match for call to `(%T) (%A)'", TREE_TYPE (obj), args); 3028 print_z_candidates (candidates); 3029 return error_mark_node; 3030 } 3031 3032 candidates = splice_viable (candidates); 3033 cand = tourney (candidates); 3034 3035 if (cand == 0) 3036 { 3037 error ("call of `(%T) (%A)' is ambiguous", TREE_TYPE (obj), args); 3038 print_z_candidates (candidates); 3039 return error_mark_node; 3040 } 3041 3042 /* Since cand->fn will be a type, not a function, for a conversion 3043 function, we must be careful not to unconditionally look at 3044 DECL_NAME here. */ 3045 if (TREE_CODE (cand->fn) == FUNCTION_DECL 3046 && DECL_OVERLOADED_OPERATOR_P (cand->fn) == CALL_EXPR) 3047 return build_over_call (cand, mem_args, LOOKUP_NORMAL); 3048 3049 obj = convert_like_with_context 3050 (TREE_VEC_ELT (cand->convs, 0), obj, cand->fn, -1); 3051 3052 /* FIXME */ 3053 return build_function_call (obj, args); 3054} 3055 3056static void 3057op_error (code, code2, arg1, arg2, arg3, problem) 3058 enum tree_code code, code2; 3059 tree arg1, arg2, arg3; 3060 const char *problem; 3061{ 3062 const char *opname; 3063 3064 if (code == MODIFY_EXPR) 3065 opname = assignment_operator_name_info[code2].name; 3066 else 3067 opname = operator_name_info[code].name; 3068 3069 switch (code) 3070 { 3071 case COND_EXPR: 3072 error ("%s for ternary 'operator?:' in '%E ? %E : %E'", 3073 problem, arg1, arg2, arg3); 3074 break; 3075 3076 case POSTINCREMENT_EXPR: 3077 case POSTDECREMENT_EXPR: 3078 error ("%s for 'operator%s' in '%E%s'", problem, opname, arg1, opname); 3079 break; 3080 3081 case ARRAY_REF: 3082 error ("%s for 'operator[]' in '%E[%E]'", problem, arg1, arg2); 3083 break; 3084 3085 default: 3086 if (arg2) 3087 error ("%s for 'operator%s' in '%E %s %E'", 3088 problem, opname, arg1, opname, arg2); 3089 else 3090 error ("%s for 'operator%s' in '%s%E'", 3091 problem, opname, opname, arg1); 3092 break; 3093 } 3094} 3095 3096/* Return the implicit conversion sequence that could be used to 3097 convert E1 to E2 in [expr.cond]. */ 3098 3099static tree 3100conditional_conversion (e1, e2) 3101 tree e1; 3102 tree e2; 3103{ 3104 tree t1 = non_reference (TREE_TYPE (e1)); 3105 tree t2 = non_reference (TREE_TYPE (e2)); 3106 tree conv; 3107 bool good_base; 3108 3109 /* [expr.cond] 3110 3111 If E2 is an lvalue: E1 can be converted to match E2 if E1 can be 3112 implicitly converted (clause _conv_) to the type "reference to 3113 T2", subject to the constraint that in the conversion the 3114 reference must bind directly (_dcl.init.ref_) to E1. */ 3115 if (real_lvalue_p (e2)) 3116 { 3117 conv = implicit_conversion (build_reference_type (t2), 3118 t1, 3119 e1, 3120 LOOKUP_NO_TEMP_BIND); 3121 if (conv) 3122 return conv; 3123 } 3124 3125 /* [expr.cond] 3126 3127 If E1 and E2 have class type, and the underlying class types are 3128 the same or one is a base class of the other: E1 can be converted 3129 to match E2 if the class of T2 is the same type as, or a base 3130 class of, the class of T1, and the cv-qualification of T2 is the 3131 same cv-qualification as, or a greater cv-qualification than, the 3132 cv-qualification of T1. If the conversion is applied, E1 is 3133 changed to an rvalue of type T2 that still refers to the original 3134 source class object (or the appropriate subobject thereof). */ 3135 if (CLASS_TYPE_P (t1) && CLASS_TYPE_P (t2) 3136 && ((good_base = DERIVED_FROM_P (t2, t1)) || DERIVED_FROM_P (t1, t2))) 3137 { 3138 if (good_base && at_least_as_qualified_p (t2, t1)) 3139 { 3140 conv = build1 (IDENTITY_CONV, t1, e1); 3141 if (!same_type_p (TYPE_MAIN_VARIANT (t1), 3142 TYPE_MAIN_VARIANT (t2))) 3143 conv = build_conv (BASE_CONV, t2, conv); 3144 return conv; 3145 } 3146 else 3147 return NULL_TREE; 3148 } 3149 else 3150 /* [expr.cond] 3151 3152 Otherwise: E1 can be converted to match E2 if E1 can be implicitly 3153 converted to the type that expression E2 would have if E2 were 3154 converted to an rvalue (or the type it has, if E2 is an rvalue). */ 3155 return implicit_conversion (t2, t1, e1, LOOKUP_NORMAL); 3156} 3157 3158/* Implement [expr.cond]. ARG1, ARG2, and ARG3 are the three 3159 arguments to the conditional expression. */ 3160 3161tree 3162build_conditional_expr (arg1, arg2, arg3) 3163 tree arg1; 3164 tree arg2; 3165 tree arg3; 3166{ 3167 tree arg2_type; 3168 tree arg3_type; 3169 tree result; 3170 tree result_type = NULL_TREE; 3171 int lvalue_p = 1; 3172 struct z_candidate *candidates = 0; 3173 struct z_candidate *cand; 3174 3175 /* As a G++ extension, the second argument to the conditional can be 3176 omitted. (So that `a ? : c' is roughly equivalent to `a ? a : 3177 c'.) If the second operand is omitted, make sure it is 3178 calculated only once. */ 3179 if (!arg2) 3180 { 3181 if (pedantic) 3182 pedwarn ("ISO C++ forbids omitting the middle term of a ?: expression"); 3183 arg1 = arg2 = save_expr (arg1); 3184 } 3185 3186 /* [expr.cond] 3187 3188 The first expr ession is implicitly converted to bool (clause 3189 _conv_). */ 3190 arg1 = cp_convert (boolean_type_node, arg1); 3191 3192 /* If something has already gone wrong, just pass that fact up the 3193 tree. */ 3194 if (arg1 == error_mark_node 3195 || arg2 == error_mark_node 3196 || arg3 == error_mark_node 3197 || TREE_TYPE (arg1) == error_mark_node 3198 || TREE_TYPE (arg2) == error_mark_node 3199 || TREE_TYPE (arg3) == error_mark_node) 3200 return error_mark_node; 3201 3202 /* [expr.cond] 3203 3204 If either the second or the third operand has type (possibly 3205 cv-qualified) void, then the lvalue-to-rvalue (_conv.lval_), 3206 array-to-pointer (_conv.array_), and function-to-pointer 3207 (_conv.func_) standard conversions are performed on the second 3208 and third operands. */ 3209 arg2_type = TREE_TYPE (arg2); 3210 arg3_type = TREE_TYPE (arg3); 3211 if (VOID_TYPE_P (arg2_type) || VOID_TYPE_P (arg3_type)) 3212 { 3213 /* Do the conversions. We don't these for `void' type arguments 3214 since it can't have any effect and since decay_conversion 3215 does not handle that case gracefully. */ 3216 if (!VOID_TYPE_P (arg2_type)) 3217 arg2 = decay_conversion (arg2); 3218 if (!VOID_TYPE_P (arg3_type)) 3219 arg3 = decay_conversion (arg3); 3220 arg2_type = TREE_TYPE (arg2); 3221 arg3_type = TREE_TYPE (arg3); 3222 3223 /* [expr.cond] 3224 3225 One of the following shall hold: 3226 3227 --The second or the third operand (but not both) is a 3228 throw-expression (_except.throw_); the result is of the 3229 type of the other and is an rvalue. 3230 3231 --Both the second and the third operands have type void; the 3232 result is of type void and is an rvalue. */ 3233 if ((TREE_CODE (arg2) == THROW_EXPR) 3234 ^ (TREE_CODE (arg3) == THROW_EXPR)) 3235 result_type = ((TREE_CODE (arg2) == THROW_EXPR) 3236 ? arg3_type : arg2_type); 3237 else if (VOID_TYPE_P (arg2_type) && VOID_TYPE_P (arg3_type)) 3238 result_type = void_type_node; 3239 else 3240 { 3241 error ("`%E' has type `void' and is not a throw-expression", 3242 VOID_TYPE_P (arg2_type) ? arg2 : arg3); 3243 return error_mark_node; 3244 } 3245 3246 lvalue_p = 0; 3247 goto valid_operands; 3248 } 3249 /* [expr.cond] 3250 3251 Otherwise, if the second and third operand have different types, 3252 and either has (possibly cv-qualified) class type, an attempt is 3253 made to convert each of those operands to the type of the other. */ 3254 else if (!same_type_p (arg2_type, arg3_type) 3255 && (CLASS_TYPE_P (arg2_type) || CLASS_TYPE_P (arg3_type))) 3256 { 3257 tree conv2 = conditional_conversion (arg2, arg3); 3258 tree conv3 = conditional_conversion (arg3, arg2); 3259 3260 /* [expr.cond] 3261 3262 If both can be converted, or one can be converted but the 3263 conversion is ambiguous, the program is ill-formed. If 3264 neither can be converted, the operands are left unchanged and 3265 further checking is performed as described below. If exactly 3266 one conversion is possible, that conversion is applied to the 3267 chosen operand and the converted operand is used in place of 3268 the original operand for the remainder of this section. */ 3269 if ((conv2 && !ICS_BAD_FLAG (conv2) 3270 && conv3 && !ICS_BAD_FLAG (conv3)) 3271 || (conv2 && TREE_CODE (conv2) == AMBIG_CONV) 3272 || (conv3 && TREE_CODE (conv3) == AMBIG_CONV)) 3273 { 3274 error ("operands to ?: have different types"); 3275 return error_mark_node; 3276 } 3277 else if (conv2 && !ICS_BAD_FLAG (conv2)) 3278 { 3279 arg2 = convert_like (conv2, arg2); 3280 arg2 = convert_from_reference (arg2); 3281 /* That may not quite have done the trick. If the two types 3282 are cv-qualified variants of one another, we will have 3283 just used an IDENTITY_CONV. (There's no conversion from 3284 an lvalue of one class type to an lvalue of another type, 3285 even a cv-qualified variant, and we don't want to lose 3286 lvalue-ness here.) So, we manually add a NOP_EXPR here 3287 if necessary. */ 3288 if (!same_type_p (TREE_TYPE (arg2), arg3_type)) 3289 arg2 = build1 (NOP_EXPR, arg3_type, arg2); 3290 arg2_type = TREE_TYPE (arg2); 3291 } 3292 else if (conv3 && !ICS_BAD_FLAG (conv3)) 3293 { 3294 arg3 = convert_like (conv3, arg3); 3295 arg3 = convert_from_reference (arg3); 3296 if (!same_type_p (TREE_TYPE (arg3), arg2_type)) 3297 arg3 = build1 (NOP_EXPR, arg2_type, arg3); 3298 arg3_type = TREE_TYPE (arg3); 3299 } 3300 } 3301 3302 /* [expr.cond] 3303 3304 If the second and third operands are lvalues and have the same 3305 type, the result is of that type and is an lvalue. */ 3306 if (real_lvalue_p (arg2) && real_lvalue_p (arg3) && 3307 same_type_p (arg2_type, arg3_type)) 3308 { 3309 result_type = arg2_type; 3310 goto valid_operands; 3311 } 3312 3313 /* [expr.cond] 3314 3315 Otherwise, the result is an rvalue. If the second and third 3316 operand do not have the same type, and either has (possibly 3317 cv-qualified) class type, overload resolution is used to 3318 determine the conversions (if any) to be applied to the operands 3319 (_over.match.oper_, _over.built_). */ 3320 lvalue_p = 0; 3321 if (!same_type_p (arg2_type, arg3_type) 3322 && (CLASS_TYPE_P (arg2_type) || CLASS_TYPE_P (arg3_type))) 3323 { 3324 tree args[3]; 3325 tree conv; 3326 3327 /* Rearrange the arguments so that add_builtin_candidate only has 3328 to know about two args. In build_builtin_candidates, the 3329 arguments are unscrambled. */ 3330 args[0] = arg2; 3331 args[1] = arg3; 3332 args[2] = arg1; 3333 add_builtin_candidates (&candidates, 3334 COND_EXPR, 3335 NOP_EXPR, 3336 ansi_opname (COND_EXPR), 3337 args, 3338 LOOKUP_NORMAL); 3339 3340 /* [expr.cond] 3341 3342 If the overload resolution fails, the program is 3343 ill-formed. */ 3344 if (!any_viable (candidates)) 3345 { 3346 op_error (COND_EXPR, NOP_EXPR, arg1, arg2, arg3, "no match"); 3347 print_z_candidates (candidates); 3348 return error_mark_node; 3349 } 3350 candidates = splice_viable (candidates); 3351 cand = tourney (candidates); 3352 if (!cand) 3353 { 3354 op_error (COND_EXPR, NOP_EXPR, arg1, arg2, arg3, "no match"); 3355 print_z_candidates (candidates); 3356 return error_mark_node; 3357 } 3358 3359 /* [expr.cond] 3360 3361 Otherwise, the conversions thus determined are applied, and 3362 the converted operands are used in place of the original 3363 operands for the remainder of this section. */ 3364 conv = TREE_VEC_ELT (cand->convs, 0); 3365 arg1 = convert_like (conv, arg1); 3366 conv = TREE_VEC_ELT (cand->convs, 1); 3367 arg2 = convert_like (conv, arg2); 3368 conv = TREE_VEC_ELT (cand->convs, 2); 3369 arg3 = convert_like (conv, arg3); 3370 } 3371 3372 /* [expr.cond] 3373 3374 Lvalue-to-rvalue (_conv.lval_), array-to-pointer (_conv.array_), 3375 and function-to-pointer (_conv.func_) standard conversions are 3376 performed on the second and third operands. 3377 3378 We need to force the lvalue-to-rvalue conversion here for class types, 3379 so we get TARGET_EXPRs; trying to deal with a COND_EXPR of class rvalues 3380 that isn't wrapped with a TARGET_EXPR plays havoc with exception 3381 regions. 3382 3383 We use ocp_convert rather than build_user_type_conversion because the 3384 latter returns NULL_TREE on failure, while the former gives an error. */ 3385 3386 arg2 = force_rvalue (arg2); 3387 arg2_type = TREE_TYPE (arg2); 3388 3389 arg3 = force_rvalue (arg3); 3390 arg3_type = TREE_TYPE (arg3); 3391 3392 if (arg2 == error_mark_node || arg3 == error_mark_node) 3393 return error_mark_node; 3394 3395 /* [expr.cond] 3396 3397 After those conversions, one of the following shall hold: 3398 3399 --The second and third operands have the same type; the result is of 3400 that type. */ 3401 if (same_type_p (arg2_type, arg3_type)) 3402 result_type = arg2_type; 3403 /* [expr.cond] 3404 3405 --The second and third operands have arithmetic or enumeration 3406 type; the usual arithmetic conversions are performed to bring 3407 them to a common type, and the result is of that type. */ 3408 else if ((ARITHMETIC_TYPE_P (arg2_type) 3409 || TREE_CODE (arg2_type) == ENUMERAL_TYPE) 3410 && (ARITHMETIC_TYPE_P (arg3_type) 3411 || TREE_CODE (arg3_type) == ENUMERAL_TYPE)) 3412 { 3413 /* In this case, there is always a common type. */ 3414 result_type = type_after_usual_arithmetic_conversions (arg2_type, 3415 arg3_type); 3416 3417 if (TREE_CODE (arg2_type) == ENUMERAL_TYPE 3418 && TREE_CODE (arg3_type) == ENUMERAL_TYPE) 3419 warning ("enumeral mismatch in conditional expression: `%T' vs `%T'", 3420 arg2_type, arg3_type); 3421 else if (extra_warnings 3422 && ((TREE_CODE (arg2_type) == ENUMERAL_TYPE 3423 && !same_type_p (arg3_type, type_promotes_to (arg2_type))) 3424 || (TREE_CODE (arg3_type) == ENUMERAL_TYPE 3425 && !same_type_p (arg2_type, type_promotes_to (arg3_type))))) 3426 warning ("enumeral and non-enumeral type in conditional expression"); 3427 3428 arg2 = perform_implicit_conversion (result_type, arg2); 3429 arg3 = perform_implicit_conversion (result_type, arg3); 3430 } 3431 /* [expr.cond] 3432 3433 --The second and third operands have pointer type, or one has 3434 pointer type and the other is a null pointer constant; pointer 3435 conversions (_conv.ptr_) and qualification conversions 3436 (_conv.qual_) are performed to bring them to their composite 3437 pointer type (_expr.rel_). The result is of the composite 3438 pointer type. 3439 3440 --The second and third operands have pointer to member type, or 3441 one has pointer to member type and the other is a null pointer 3442 constant; pointer to member conversions (_conv.mem_) and 3443 qualification conversions (_conv.qual_) are performed to bring 3444 them to a common type, whose cv-qualification shall match the 3445 cv-qualification of either the second or the third operand. 3446 The result is of the common type. */ 3447 else if ((null_ptr_cst_p (arg2) 3448 && (TYPE_PTR_P (arg3_type) || TYPE_PTRMEM_P (arg3_type) 3449 || TYPE_PTRMEMFUNC_P (arg3_type))) 3450 || (null_ptr_cst_p (arg3) 3451 && (TYPE_PTR_P (arg2_type) || TYPE_PTRMEM_P (arg2_type) 3452 || TYPE_PTRMEMFUNC_P (arg2_type))) 3453 || (TYPE_PTR_P (arg2_type) && TYPE_PTR_P (arg3_type)) 3454 || (TYPE_PTRMEM_P (arg2_type) && TYPE_PTRMEM_P (arg3_type)) 3455 || (TYPE_PTRMEMFUNC_P (arg2_type) 3456 && TYPE_PTRMEMFUNC_P (arg3_type))) 3457 { 3458 result_type = composite_pointer_type (arg2_type, arg3_type, arg2, 3459 arg3, "conditional expression"); 3460 arg2 = perform_implicit_conversion (result_type, arg2); 3461 arg3 = perform_implicit_conversion (result_type, arg3); 3462 } 3463 3464 if (!result_type) 3465 { 3466 error ("operands to ?: have different types"); 3467 return error_mark_node; 3468 } 3469 3470 valid_operands: 3471 result = fold (build (COND_EXPR, result_type, arg1, arg2, arg3)); 3472 /* Expand both sides into the same slot, hopefully the target of the 3473 ?: expression. We used to check for TARGET_EXPRs here, but now we 3474 sometimes wrap them in NOP_EXPRs so the test would fail. */ 3475 if (!lvalue_p && IS_AGGR_TYPE (result_type)) 3476 result = build_target_expr_with_type (result, result_type); 3477 3478 /* If this expression is an rvalue, but might be mistaken for an 3479 lvalue, we must add a NON_LVALUE_EXPR. */ 3480 if (!lvalue_p && real_lvalue_p (result)) 3481 result = build1 (NON_LVALUE_EXPR, result_type, result); 3482 3483 return result; 3484} 3485 3486tree 3487build_new_op (code, flags, arg1, arg2, arg3) 3488 enum tree_code code; 3489 int flags; 3490 tree arg1, arg2, arg3; 3491{ 3492 struct z_candidate *candidates = 0, *cand; 3493 tree fns, mem_arglist = NULL_TREE, arglist, fnname; 3494 enum tree_code code2 = NOP_EXPR; 3495 tree conv; 3496 bool viable_candidates; 3497 3498 if (arg1 == error_mark_node 3499 || arg2 == error_mark_node 3500 || arg3 == error_mark_node) 3501 return error_mark_node; 3502 3503 /* This can happen if a template takes all non-type parameters, e.g. 3504 undeclared_template<1, 5, 72>a; */ 3505 if (code == LT_EXPR && TREE_CODE (arg1) == TEMPLATE_DECL) 3506 { 3507 error ("`%D' must be declared before use", arg1); 3508 return error_mark_node; 3509 } 3510 3511 if (code == MODIFY_EXPR) 3512 { 3513 code2 = TREE_CODE (arg3); 3514 arg3 = NULL_TREE; 3515 fnname = ansi_assopname (code2); 3516 } 3517 else 3518 fnname = ansi_opname (code); 3519 3520 if (TREE_CODE (arg1) == OFFSET_REF) 3521 arg1 = resolve_offset_ref (arg1); 3522 arg1 = convert_from_reference (arg1); 3523 if (CLASS_TYPE_P (TREE_TYPE (arg1)) 3524 && CLASSTYPE_TEMPLATE_INSTANTIATION (TREE_TYPE (arg1))) 3525 /* Make sure the template type is instantiated now. */ 3526 instantiate_class_template (TYPE_MAIN_VARIANT (TREE_TYPE (arg1))); 3527 3528 switch (code) 3529 { 3530 case NEW_EXPR: 3531 case VEC_NEW_EXPR: 3532 case VEC_DELETE_EXPR: 3533 case DELETE_EXPR: 3534 /* Use build_op_new_call and build_op_delete_call instead. */ 3535 abort (); 3536 3537 case CALL_EXPR: 3538 return build_object_call (arg1, arg2); 3539 3540 default: 3541 break; 3542 } 3543 3544 if (arg2) 3545 { 3546 if (TREE_CODE (arg2) == OFFSET_REF) 3547 arg2 = resolve_offset_ref (arg2); 3548 arg2 = convert_from_reference (arg2); 3549 if (CLASS_TYPE_P (TREE_TYPE (arg2)) 3550 && CLASSTYPE_TEMPLATE_INSTANTIATION (TREE_TYPE (arg2))) 3551 instantiate_class_template (TYPE_MAIN_VARIANT (TREE_TYPE (arg2))); 3552 } 3553 if (arg3) 3554 { 3555 if (TREE_CODE (arg3) == OFFSET_REF) 3556 arg3 = resolve_offset_ref (arg3); 3557 arg3 = convert_from_reference (arg3); 3558 if (CLASS_TYPE_P (TREE_TYPE (arg3)) 3559 && CLASSTYPE_TEMPLATE_INSTANTIATION (TREE_TYPE (arg3))) 3560 instantiate_class_template (TYPE_MAIN_VARIANT (TREE_TYPE (arg3))); 3561 } 3562 3563 if (code == COND_EXPR) 3564 { 3565 if (arg2 == NULL_TREE 3566 || TREE_CODE (TREE_TYPE (arg2)) == VOID_TYPE 3567 || TREE_CODE (TREE_TYPE (arg3)) == VOID_TYPE 3568 || (! IS_OVERLOAD_TYPE (TREE_TYPE (arg2)) 3569 && ! IS_OVERLOAD_TYPE (TREE_TYPE (arg3)))) 3570 goto builtin; 3571 } 3572 else if (! IS_OVERLOAD_TYPE (TREE_TYPE (arg1)) 3573 && (! arg2 || ! IS_OVERLOAD_TYPE (TREE_TYPE (arg2)))) 3574 goto builtin; 3575 3576 if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR) 3577 arg2 = integer_zero_node; 3578 3579 arglist = NULL_TREE; 3580 if (arg3) 3581 arglist = tree_cons (NULL_TREE, arg3, arglist); 3582 if (arg2) 3583 arglist = tree_cons (NULL_TREE, arg2, arglist); 3584 arglist = tree_cons (NULL_TREE, arg1, arglist); 3585 3586 fns = lookup_function_nonclass (fnname, arglist); 3587 3588 if (fns && TREE_CODE (fns) == TREE_LIST) 3589 fns = TREE_VALUE (fns); 3590 for (; fns; fns = OVL_NEXT (fns)) 3591 { 3592 tree fn = OVL_CURRENT (fns); 3593 if (TREE_CODE (fn) == TEMPLATE_DECL) 3594 add_template_candidate (&candidates, fn, NULL_TREE, NULL_TREE, 3595 arglist, TREE_TYPE (fnname), 3596 /*access_path=*/NULL_TREE, 3597 /*conversion_path=*/NULL_TREE, 3598 flags, DEDUCE_CALL); 3599 else 3600 add_function_candidate (&candidates, fn, NULL_TREE, 3601 arglist, 3602 /*access_path=*/NULL_TREE, 3603 /*conversion_path=*/NULL_TREE, 3604 flags); 3605 } 3606 3607 if (IS_AGGR_TYPE (TREE_TYPE (arg1))) 3608 { 3609 fns = lookup_fnfields (TYPE_BINFO (TREE_TYPE (arg1)), fnname, 1); 3610 if (fns == error_mark_node) 3611 return fns; 3612 } 3613 else 3614 fns = NULL_TREE; 3615 3616 if (fns) 3617 { 3618 tree conversion_path = BASELINK_BINFO (fns); 3619 3620 mem_arglist = tree_cons (NULL_TREE, build_this (arg1), TREE_CHAIN (arglist)); 3621 for (fns = BASELINK_FUNCTIONS (fns); fns; fns = OVL_NEXT (fns)) 3622 { 3623 tree fn = OVL_CURRENT (fns); 3624 tree this_arglist; 3625 tree access_path = TYPE_BINFO (TREE_TYPE (arg1)); 3626 3627 if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE) 3628 this_arglist = mem_arglist; 3629 else 3630 this_arglist = arglist; 3631 3632 if (TREE_CODE (fn) == TEMPLATE_DECL) 3633 /* A member template. */ 3634 add_template_candidate (&candidates, fn, 3635 BINFO_TYPE (conversion_path), 3636 NULL_TREE, 3637 this_arglist, TREE_TYPE (fnname), 3638 access_path, conversion_path, 3639 flags, DEDUCE_CALL); 3640 else 3641 add_function_candidate 3642 (&candidates, fn, BINFO_TYPE (conversion_path), this_arglist, 3643 access_path, conversion_path, flags); 3644 } 3645 } 3646 3647 { 3648 tree args[3]; 3649 3650 /* Rearrange the arguments for ?: so that add_builtin_candidate only has 3651 to know about two args; a builtin candidate will always have a first 3652 parameter of type bool. We'll handle that in 3653 build_builtin_candidate. */ 3654 if (code == COND_EXPR) 3655 { 3656 args[0] = arg2; 3657 args[1] = arg3; 3658 args[2] = arg1; 3659 } 3660 else 3661 { 3662 args[0] = arg1; 3663 args[1] = arg2; 3664 args[2] = NULL_TREE; 3665 } 3666 3667 add_builtin_candidates (&candidates, code, code2, fnname, args, flags); 3668 } 3669 3670 switch (code) 3671 { 3672 case COMPOUND_EXPR: 3673 case ADDR_EXPR: 3674 /* For these, the built-in candidates set is empty 3675 [over.match.oper]/3. We don't want non-strict matches 3676 because exact matches are always possible with built-in 3677 operators. The built-in candidate set for COMPONENT_REF 3678 would be empty too, but since there are no such built-in 3679 operators, we accept non-strict matches for them. */ 3680 viable_candidates = any_strictly_viable (candidates); 3681 break; 3682 3683 default: 3684 viable_candidates = any_viable (candidates); 3685 break; 3686 } 3687 3688 if (! viable_candidates) 3689 { 3690 switch (code) 3691 { 3692 case POSTINCREMENT_EXPR: 3693 case POSTDECREMENT_EXPR: 3694 /* Look for an `operator++ (int)'. If they didn't have 3695 one, then we fall back to the old way of doing things. */ 3696 if (flags & LOOKUP_COMPLAIN) 3697 pedwarn ("no `%D(int)' declared for postfix `%s', trying prefix operator instead", 3698 fnname, 3699 operator_name_info[code].name); 3700 if (code == POSTINCREMENT_EXPR) 3701 code = PREINCREMENT_EXPR; 3702 else 3703 code = PREDECREMENT_EXPR; 3704 return build_new_op (code, flags, arg1, NULL_TREE, NULL_TREE); 3705 3706 /* The caller will deal with these. */ 3707 case ADDR_EXPR: 3708 case COMPOUND_EXPR: 3709 case COMPONENT_REF: 3710 return NULL_TREE; 3711 3712 default: 3713 break; 3714 } 3715 if (flags & LOOKUP_COMPLAIN) 3716 { 3717 op_error (code, code2, arg1, arg2, arg3, "no match"); 3718 print_z_candidates (candidates); 3719 } 3720 return error_mark_node; 3721 } 3722 candidates = splice_viable (candidates); 3723 cand = tourney (candidates); 3724 3725 if (cand == 0) 3726 { 3727 if (flags & LOOKUP_COMPLAIN) 3728 { 3729 op_error (code, code2, arg1, arg2, arg3, "ambiguous overload"); 3730 print_z_candidates (candidates); 3731 } 3732 return error_mark_node; 3733 } 3734 3735 if (TREE_CODE (cand->fn) == FUNCTION_DECL) 3736 { 3737 extern int warn_synth; 3738 if (warn_synth 3739 && fnname == ansi_assopname (NOP_EXPR) 3740 && DECL_ARTIFICIAL (cand->fn) 3741 && candidates->next 3742 && ! candidates->next->next) 3743 { 3744 warning ("using synthesized `%#D' for copy assignment", 3745 cand->fn); 3746 cp_warning_at (" where cfront would use `%#D'", 3747 cand == candidates 3748 ? candidates->next->fn 3749 : candidates->fn); 3750 } 3751 3752 return build_over_call 3753 (cand, 3754 TREE_CODE (TREE_TYPE (cand->fn)) == METHOD_TYPE 3755 ? mem_arglist : arglist, 3756 LOOKUP_NORMAL); 3757 } 3758 3759 /* Check for comparison of different enum types. */ 3760 switch (code) 3761 { 3762 case GT_EXPR: 3763 case LT_EXPR: 3764 case GE_EXPR: 3765 case LE_EXPR: 3766 case EQ_EXPR: 3767 case NE_EXPR: 3768 if (TREE_CODE (TREE_TYPE (arg1)) == ENUMERAL_TYPE 3769 && TREE_CODE (TREE_TYPE (arg2)) == ENUMERAL_TYPE 3770 && (TYPE_MAIN_VARIANT (TREE_TYPE (arg1)) 3771 != TYPE_MAIN_VARIANT (TREE_TYPE (arg2)))) 3772 { 3773 warning ("comparison between `%#T' and `%#T'", 3774 TREE_TYPE (arg1), TREE_TYPE (arg2)); 3775 } 3776 break; 3777 default: 3778 break; 3779 } 3780 3781 /* We need to strip any leading REF_BIND so that bitfields don't cause 3782 errors. This should not remove any important conversions, because 3783 builtins don't apply to class objects directly. */ 3784 conv = TREE_VEC_ELT (cand->convs, 0); 3785 if (TREE_CODE (conv) == REF_BIND) 3786 conv = TREE_OPERAND (conv, 0); 3787 arg1 = convert_like (conv, arg1); 3788 if (arg2) 3789 { 3790 conv = TREE_VEC_ELT (cand->convs, 1); 3791 if (TREE_CODE (conv) == REF_BIND) 3792 conv = TREE_OPERAND (conv, 0); 3793 arg2 = convert_like (conv, arg2); 3794 } 3795 if (arg3) 3796 { 3797 conv = TREE_VEC_ELT (cand->convs, 2); 3798 if (TREE_CODE (conv) == REF_BIND) 3799 conv = TREE_OPERAND (conv, 0); 3800 arg3 = convert_like (conv, arg3); 3801 } 3802 3803builtin: 3804 switch (code) 3805 { 3806 case MODIFY_EXPR: 3807 return build_modify_expr (arg1, code2, arg2); 3808 3809 case INDIRECT_REF: 3810 return build_indirect_ref (arg1, "unary *"); 3811 3812 case PLUS_EXPR: 3813 case MINUS_EXPR: 3814 case MULT_EXPR: 3815 case TRUNC_DIV_EXPR: 3816 case GT_EXPR: 3817 case LT_EXPR: 3818 case GE_EXPR: 3819 case LE_EXPR: 3820 case EQ_EXPR: 3821 case NE_EXPR: 3822 case MAX_EXPR: 3823 case MIN_EXPR: 3824 case LSHIFT_EXPR: 3825 case RSHIFT_EXPR: 3826 case TRUNC_MOD_EXPR: 3827 case BIT_AND_EXPR: 3828 case BIT_IOR_EXPR: 3829 case BIT_XOR_EXPR: 3830 case TRUTH_ANDIF_EXPR: 3831 case TRUTH_ORIF_EXPR: 3832 return cp_build_binary_op (code, arg1, arg2); 3833 3834 case CONVERT_EXPR: 3835 case NEGATE_EXPR: 3836 case BIT_NOT_EXPR: 3837 case TRUTH_NOT_EXPR: 3838 case PREINCREMENT_EXPR: 3839 case POSTINCREMENT_EXPR: 3840 case PREDECREMENT_EXPR: 3841 case POSTDECREMENT_EXPR: 3842 case REALPART_EXPR: 3843 case IMAGPART_EXPR: 3844 return build_unary_op (code, arg1, candidates != 0); 3845 3846 case ARRAY_REF: 3847 return build_array_ref (arg1, arg2); 3848 3849 case COND_EXPR: 3850 return build_conditional_expr (arg1, arg2, arg3); 3851 3852 case MEMBER_REF: 3853 return build_m_component_ref 3854 (build_indirect_ref (arg1, NULL), arg2); 3855 3856 /* The caller will deal with these. */ 3857 case ADDR_EXPR: 3858 case COMPONENT_REF: 3859 case COMPOUND_EXPR: 3860 return NULL_TREE; 3861 3862 default: 3863 abort (); 3864 return NULL_TREE; 3865 } 3866} 3867 3868/* Build a call to operator delete. This has to be handled very specially, 3869 because the restrictions on what signatures match are different from all 3870 other call instances. For a normal delete, only a delete taking (void *) 3871 or (void *, size_t) is accepted. For a placement delete, only an exact 3872 match with the placement new is accepted. 3873 3874 CODE is either DELETE_EXPR or VEC_DELETE_EXPR. 3875 ADDR is the pointer to be deleted. 3876 SIZE is the size of the memory block to be deleted. 3877 FLAGS are the usual overloading flags. 3878 PLACEMENT is the corresponding placement new call, or NULL_TREE. */ 3879 3880tree 3881build_op_delete_call (code, addr, size, flags, placement) 3882 enum tree_code code; 3883 tree addr, size, placement; 3884 int flags; 3885{ 3886 tree fn = NULL_TREE; 3887 tree fns, fnname, fntype, argtypes, args, type; 3888 int pass; 3889 3890 if (addr == error_mark_node) 3891 return error_mark_node; 3892 3893 type = TREE_TYPE (TREE_TYPE (addr)); 3894 while (TREE_CODE (type) == ARRAY_TYPE) 3895 type = TREE_TYPE (type); 3896 3897 fnname = ansi_opname (code); 3898 3899 if (IS_AGGR_TYPE (type) && ! (flags & LOOKUP_GLOBAL)) 3900 /* In [class.free] 3901 3902 If the result of the lookup is ambiguous or inaccessible, or if 3903 the lookup selects a placement deallocation function, the 3904 program is ill-formed. 3905 3906 Therefore, we ask lookup_fnfields to complain ambout ambiguity. */ 3907 { 3908 fns = lookup_fnfields (TYPE_BINFO (type), fnname, 1); 3909 if (fns == error_mark_node) 3910 return error_mark_node; 3911 } 3912 else 3913 fns = NULL_TREE; 3914 3915 if (fns == NULL_TREE) 3916 fns = lookup_name_nonclass (fnname); 3917 3918 if (placement) 3919 { 3920 tree alloc_fn; 3921 tree call_expr; 3922 3923 /* Find the allocation function that is being called. */ 3924 call_expr = placement; 3925 /* Sometimes we have a COMPOUND_EXPR, rather than a simple 3926 CALL_EXPR. */ 3927 while (TREE_CODE (call_expr) == COMPOUND_EXPR) 3928 call_expr = TREE_OPERAND (call_expr, 1); 3929 /* Extract the function. */ 3930 alloc_fn = get_callee_fndecl (call_expr); 3931 my_friendly_assert (alloc_fn != NULL_TREE, 20020327); 3932 /* Then the second parm type. */ 3933 argtypes = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (alloc_fn))); 3934 /* Also the second argument. */ 3935 args = TREE_CHAIN (TREE_OPERAND (call_expr, 1)); 3936 } 3937 else 3938 { 3939 /* First try it without the size argument. */ 3940 argtypes = void_list_node; 3941 args = NULL_TREE; 3942 } 3943 3944 /* Strip const and volatile from addr. */ 3945 addr = cp_convert (ptr_type_node, addr); 3946 3947 /* We make two tries at finding a matching `operator delete'. On 3948 the first pass, we look for an one-operator (or placement) 3949 operator delete. If we're not doing placement delete, then on 3950 the second pass we look for a two-argument delete. */ 3951 for (pass = 0; pass < (placement ? 1 : 2); ++pass) 3952 { 3953 if (pass == 0) 3954 argtypes = tree_cons (NULL_TREE, ptr_type_node, argtypes); 3955 else 3956 /* Normal delete; now try to find a match including the size 3957 argument. */ 3958 argtypes = tree_cons (NULL_TREE, ptr_type_node, 3959 tree_cons (NULL_TREE, sizetype, 3960 void_list_node)); 3961 fntype = build_function_type (void_type_node, argtypes); 3962 3963 /* Go through the `operator delete' functions looking for one 3964 with a matching type. */ 3965 for (fn = BASELINK_P (fns) ? BASELINK_FUNCTIONS (fns) : fns; 3966 fn; 3967 fn = OVL_NEXT (fn)) 3968 { 3969 tree t; 3970 3971 /* Exception specifications on the `delete' operator do not 3972 matter. */ 3973 t = build_exception_variant (TREE_TYPE (OVL_CURRENT (fn)), 3974 NULL_TREE); 3975 /* We also don't compare attributes. We're really just 3976 trying to check the types of the first two parameters. */ 3977 if (comptypes (t, fntype, COMPARE_NO_ATTRIBUTES)) 3978 break; 3979 } 3980 3981 /* If we found a match, we're done. */ 3982 if (fn) 3983 break; 3984 } 3985 3986 /* If we have a matching function, call it. */ 3987 if (fn) 3988 { 3989 /* Make sure we have the actual function, and not an 3990 OVERLOAD. */ 3991 fn = OVL_CURRENT (fn); 3992 3993 /* If the FN is a member function, make sure that it is 3994 accessible. */ 3995 if (DECL_CLASS_SCOPE_P (fn)) 3996 enforce_access (type, fn); 3997 3998 if (pass == 0) 3999 args = tree_cons (NULL_TREE, addr, args); 4000 else 4001 args = tree_cons (NULL_TREE, addr, 4002 build_tree_list (NULL_TREE, size)); 4003 4004 return build_function_call (fn, args); 4005 } 4006 4007 /* If we are doing placement delete we do nothing if we don't find a 4008 matching op delete. */ 4009 if (placement) 4010 return NULL_TREE; 4011 4012 error ("no suitable `operator delete' for `%T'", type); 4013 return error_mark_node; 4014} 4015 4016/* If the current scope isn't allowed to access DECL along 4017 BASETYPE_PATH, give an error. The most derived class in 4018 BASETYPE_PATH is the one used to qualify DECL. */ 4019 4020int 4021enforce_access (basetype_path, decl) 4022 tree basetype_path; 4023 tree decl; 4024{ 4025 int accessible; 4026 4027 accessible = accessible_p (basetype_path, decl); 4028 if (!accessible) 4029 { 4030 if (TREE_PRIVATE (decl)) 4031 cp_error_at ("`%+#D' is private", decl); 4032 else if (TREE_PROTECTED (decl)) 4033 cp_error_at ("`%+#D' is protected", decl); 4034 else 4035 cp_error_at ("`%+#D' is inaccessible", decl); 4036 error ("within this context"); 4037 return 0; 4038 } 4039 4040 return 1; 4041} 4042 4043/* Perform the conversions in CONVS on the expression EXPR. FN and 4044 ARGNUM are used for diagnostics. ARGNUM is zero based, -1 4045 indicates the `this' argument of a method. INNER is nonzero when 4046 being called to continue a conversion chain. It is negative when a 4047 reference binding will be applied, positive otherwise. If 4048 ISSUE_CONVERSION_WARNINGS is true, warnings about suspicious 4049 conversions will be emitted if appropriate. */ 4050 4051static tree 4052convert_like_real (tree convs, tree expr, tree fn, int argnum, int inner, 4053 bool issue_conversion_warnings) 4054{ 4055 int savew, savee; 4056 4057 tree totype = TREE_TYPE (convs); 4058 4059 if (ICS_BAD_FLAG (convs) 4060 && TREE_CODE (convs) != USER_CONV 4061 && TREE_CODE (convs) != AMBIG_CONV 4062 && TREE_CODE (convs) != REF_BIND) 4063 { 4064 tree t = convs; 4065 for (; t; t = TREE_OPERAND (t, 0)) 4066 { 4067 if (TREE_CODE (t) == USER_CONV || !ICS_BAD_FLAG (t)) 4068 { 4069 expr = convert_like_real (t, expr, fn, argnum, 1, 4070 /*issue_conversion_warnings=*/false); 4071 break; 4072 } 4073 else if (TREE_CODE (t) == AMBIG_CONV) 4074 return convert_like_real (t, expr, fn, argnum, 1, 4075 /*issue_conversion_warnings=*/false); 4076 else if (TREE_CODE (t) == IDENTITY_CONV) 4077 break; 4078 } 4079 pedwarn ("invalid conversion from `%T' to `%T'", TREE_TYPE (expr), totype); 4080 if (fn) 4081 pedwarn (" initializing argument %P of `%D'", argnum, fn); 4082 return cp_convert (totype, expr); 4083 } 4084 4085 if (issue_conversion_warnings) 4086 expr = dubious_conversion_warnings 4087 (totype, expr, "argument", fn, argnum); 4088 switch (TREE_CODE (convs)) 4089 { 4090 case USER_CONV: 4091 { 4092 struct z_candidate *cand = USER_CONV_CAND (convs); 4093 tree convfn = cand->fn; 4094 tree args; 4095 4096 if (DECL_CONSTRUCTOR_P (convfn)) 4097 { 4098 tree t = build_int_2 (0, 0); 4099 TREE_TYPE (t) = build_pointer_type (DECL_CONTEXT (convfn)); 4100 4101 args = build_tree_list (NULL_TREE, expr); 4102 if (DECL_HAS_IN_CHARGE_PARM_P (convfn) 4103 || DECL_HAS_VTT_PARM_P (convfn)) 4104 /* We should never try to call the abstract or base constructor 4105 from here. */ 4106 abort (); 4107 args = tree_cons (NULL_TREE, t, args); 4108 } 4109 else 4110 args = build_this (expr); 4111 expr = build_over_call (cand, args, LOOKUP_NORMAL); 4112 4113 /* If this is a constructor or a function returning an aggr type, 4114 we need to build up a TARGET_EXPR. */ 4115 if (DECL_CONSTRUCTOR_P (convfn)) 4116 expr = build_cplus_new (totype, expr); 4117 4118 /* The result of the call is then used to direct-initialize the object 4119 that is the destination of the copy-initialization. [dcl.init] 4120 4121 Note that this step is not reflected in the conversion sequence; 4122 it affects the semantics when we actually perform the 4123 conversion, but is not considered during overload resolution. 4124 4125 If the target is a class, that means call a ctor. */ 4126 if (IS_AGGR_TYPE (totype) 4127 && (inner >= 0 || !lvalue_p (expr))) 4128 { 4129 savew = warningcount, savee = errorcount; 4130 expr = build_special_member_call 4131 (NULL_TREE, complete_ctor_identifier, 4132 build_tree_list (NULL_TREE, expr), TYPE_BINFO (totype), 4133 /* Core issue 84, now a DR, says that we don't allow UDCs 4134 for these args (which deliberately breaks copy-init of an 4135 auto_ptr<Base> from an auto_ptr<Derived>). */ 4136 LOOKUP_NORMAL|LOOKUP_ONLYCONVERTING|LOOKUP_NO_CONVERSION); 4137 4138 /* Tell the user where this failing constructor call came from. */ 4139 if (fn) 4140 { 4141 if (warningcount > savew) 4142 warning 4143 (" initializing argument %P of `%D' from result of `%D'", 4144 argnum, fn, convfn); 4145 else if (errorcount > savee) 4146 error 4147 (" initializing argument %P of `%D' from result of `%D'", 4148 argnum, fn, convfn); 4149 } 4150 else 4151 { 4152 if (warningcount > savew) 4153 warning (" initializing temporary from result of `%D'", 4154 convfn); 4155 else if (errorcount > savee) 4156 error (" initializing temporary from result of `%D'", 4157 convfn); 4158 } 4159 expr = build_cplus_new (totype, expr); 4160 } 4161 return expr; 4162 } 4163 case IDENTITY_CONV: 4164 if (type_unknown_p (expr)) 4165 expr = instantiate_type (totype, expr, tf_error | tf_warning); 4166 return expr; 4167 case AMBIG_CONV: 4168 /* Call build_user_type_conversion again for the error. */ 4169 return build_user_type_conversion 4170 (totype, TREE_OPERAND (convs, 0), LOOKUP_NORMAL); 4171 4172 default: 4173 break; 4174 }; 4175 4176 expr = convert_like_real (TREE_OPERAND (convs, 0), expr, fn, argnum, 4177 TREE_CODE (convs) == REF_BIND ? -1 : 1, 4178 /*issue_conversion_warnings=*/false); 4179 if (expr == error_mark_node) 4180 return error_mark_node; 4181 4182 /* Convert a non-array constant variable to its underlying value, unless we 4183 are about to bind it to a reference, in which case we need to 4184 leave it as an lvalue. */ 4185 if (TREE_CODE (convs) != REF_BIND 4186 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE) 4187 expr = decl_constant_value (expr); 4188 4189 switch (TREE_CODE (convs)) 4190 { 4191 case RVALUE_CONV: 4192 if (! IS_AGGR_TYPE (totype)) 4193 return expr; 4194 /* else fall through */ 4195 case BASE_CONV: 4196 if (TREE_CODE (convs) == BASE_CONV && !NEED_TEMPORARY_P (convs)) 4197 { 4198 /* We are going to bind a reference directly to a base-class 4199 subobject of EXPR. */ 4200 tree base_ptr = build_pointer_type (totype); 4201 4202 /* Build an expression for `*((base*) &expr)'. */ 4203 expr = build_unary_op (ADDR_EXPR, expr, 0); 4204 expr = perform_implicit_conversion (base_ptr, expr); 4205 expr = build_indirect_ref (expr, "implicit conversion"); 4206 return expr; 4207 } 4208 4209 /* Copy-initialization where the cv-unqualified version of the source 4210 type is the same class as, or a derived class of, the class of the 4211 destination [is treated as direct-initialization]. [dcl.init] */ 4212 savew = warningcount, savee = errorcount; 4213 expr = build_special_member_call (NULL_TREE, complete_ctor_identifier, 4214 build_tree_list (NULL_TREE, expr), 4215 TYPE_BINFO (totype), 4216 LOOKUP_NORMAL|LOOKUP_ONLYCONVERTING); 4217 if (fn) 4218 { 4219 if (warningcount > savew) 4220 warning (" initializing argument %P of `%D'", argnum, fn); 4221 else if (errorcount > savee) 4222 error (" initializing argument %P of `%D'", argnum, fn); 4223 } 4224 return build_cplus_new (totype, expr); 4225 4226 case REF_BIND: 4227 { 4228 tree ref_type = totype; 4229 4230 /* If necessary, create a temporary. */ 4231 if (NEED_TEMPORARY_P (convs) || !non_cast_lvalue_p (expr)) 4232 { 4233 tree type = TREE_TYPE (TREE_OPERAND (convs, 0)); 4234 expr = build_target_expr_with_type (expr, type); 4235 } 4236 4237 /* Take the address of the thing to which we will bind the 4238 reference. */ 4239 expr = build_unary_op (ADDR_EXPR, expr, 1); 4240 if (expr == error_mark_node) 4241 return error_mark_node; 4242 4243 /* Convert it to a pointer to the type referred to by the 4244 reference. This will adjust the pointer if a derived to 4245 base conversion is being performed. */ 4246 expr = cp_convert (build_pointer_type (TREE_TYPE (ref_type)), 4247 expr); 4248 /* Convert the pointer to the desired reference type. */ 4249 return build_nop (ref_type, expr); 4250 } 4251 4252 case LVALUE_CONV: 4253 return decay_conversion (expr); 4254 4255 case QUAL_CONV: 4256 /* Warn about deprecated conversion if appropriate. */ 4257 string_conv_p (totype, expr, 1); 4258 break; 4259 4260 default: 4261 break; 4262 } 4263 return ocp_convert (totype, expr, CONV_IMPLICIT, 4264 LOOKUP_NORMAL|LOOKUP_NO_CONVERSION); 4265} 4266 4267/* Build a call to __builtin_trap which can be used in an expression. */ 4268 4269static tree 4270call_builtin_trap () 4271{ 4272 tree fn = get_identifier ("__builtin_trap"); 4273 if (IDENTIFIER_GLOBAL_VALUE (fn)) 4274 fn = IDENTIFIER_GLOBAL_VALUE (fn); 4275 else 4276 abort (); 4277 4278 fn = build_call (fn, NULL_TREE); 4279 fn = build (COMPOUND_EXPR, integer_type_node, fn, integer_zero_node); 4280 return fn; 4281} 4282 4283/* ARG is being passed to a varargs function. Perform any conversions 4284 required. Array/function to pointer decay must have already happened. 4285 Return the converted value. */ 4286 4287tree 4288convert_arg_to_ellipsis (arg) 4289 tree arg; 4290{ 4291 if (TREE_CODE (TREE_TYPE (arg)) == REAL_TYPE 4292 && (TYPE_PRECISION (TREE_TYPE (arg)) 4293 < TYPE_PRECISION (double_type_node))) 4294 /* Convert `float' to `double'. */ 4295 arg = cp_convert (double_type_node, arg); 4296 else 4297 /* Convert `short' and `char' to full-size `int'. */ 4298 arg = default_conversion (arg); 4299 4300 arg = require_complete_type (arg); 4301 4302 if (arg != error_mark_node && ! pod_type_p (TREE_TYPE (arg))) 4303 { 4304 /* Undefined behavior [expr.call] 5.2.2/7. We used to just warn 4305 here and do a bitwise copy, but now cp_expr_size will abort if we 4306 try to do that. */ 4307 warning ("cannot pass objects of non-POD type `%#T' through `...'; \ 4308call will abort at runtime", 4309 TREE_TYPE (arg)); 4310 arg = call_builtin_trap (); 4311 } 4312 4313 return arg; 4314} 4315 4316/* va_arg (EXPR, TYPE) is a builtin. Make sure it is not abused. */ 4317 4318tree 4319build_x_va_arg (expr, type) 4320 tree expr; 4321 tree type; 4322{ 4323 if (processing_template_decl) 4324 return build_min (VA_ARG_EXPR, type, expr); 4325 4326 type = complete_type_or_else (type, NULL_TREE); 4327 4328 if (expr == error_mark_node || !type) 4329 return error_mark_node; 4330 4331 if (! pod_type_p (type)) 4332 { 4333 /* Undefined behavior [expr.call] 5.2.2/7. */ 4334 warning ("cannot receive objects of non-POD type `%#T' through `...'", 4335 type); 4336 } 4337 4338 return build_va_arg (expr, type); 4339} 4340 4341/* TYPE has been given to va_arg. Apply the default conversions which 4342 would have happened when passed via ellipsis. Return the promoted 4343 type, or the passed type if there is no change. */ 4344 4345tree 4346cxx_type_promotes_to (type) 4347 tree type; 4348{ 4349 tree promote; 4350 4351 if (TREE_CODE (type) == ARRAY_TYPE) 4352 return build_pointer_type (TREE_TYPE (type)); 4353 4354 if (TREE_CODE (type) == FUNCTION_TYPE) 4355 return build_pointer_type (type); 4356 4357 promote = type_promotes_to (type); 4358 if (same_type_p (type, promote)) 4359 promote = type; 4360 4361 return promote; 4362} 4363 4364/* ARG is a default argument expression being passed to a parameter of 4365 the indicated TYPE, which is a parameter to FN. Do any required 4366 conversions. Return the converted value. */ 4367 4368tree 4369convert_default_arg (type, arg, fn, parmnum) 4370 tree type; 4371 tree arg; 4372 tree fn; 4373 int parmnum; 4374{ 4375 if (TREE_CODE (arg) == DEFAULT_ARG) 4376 { 4377 /* When processing the default args for a class, we can find that 4378 there is an ordering constraint, and we call a function who's 4379 default args have not yet been converted. For instance, 4380 class A { 4381 A (int = 0); 4382 void Foo (A const & = A ()); 4383 }; 4384 We must process A::A before A::Foo's default arg can be converted. 4385 Remember the dependent function, so do_pending_defargs can retry, 4386 and check loops. */ 4387 unprocessed_defarg_fn (fn); 4388 4389 /* Don't return error_mark node, as we won't be able to distinguish 4390 genuine errors from this case, and that would lead to repeated 4391 diagnostics. Just make something of the right type. */ 4392 return build1 (NOP_EXPR, type, integer_zero_node); 4393 } 4394 4395 if (fn && DECL_TEMPLATE_INFO (fn)) 4396 arg = tsubst_default_argument (fn, type, arg); 4397 4398 arg = break_out_target_exprs (arg); 4399 4400 if (TREE_CODE (arg) == CONSTRUCTOR) 4401 { 4402 arg = digest_init (type, arg, 0); 4403 arg = convert_for_initialization (0, type, arg, LOOKUP_NORMAL, 4404 "default argument", fn, parmnum); 4405 } 4406 else 4407 { 4408 /* This could get clobbered by the following call. */ 4409 if (TREE_HAS_CONSTRUCTOR (arg)) 4410 arg = copy_node (arg); 4411 4412 arg = convert_for_initialization (0, type, arg, LOOKUP_NORMAL, 4413 "default argument", fn, parmnum); 4414 arg = convert_for_arg_passing (type, arg); 4415 } 4416 4417 return arg; 4418} 4419 4420/* Returns the type which will really be used for passing an argument of 4421 type TYPE. */ 4422 4423tree 4424type_passed_as (type) 4425 tree type; 4426{ 4427 /* Pass classes with copy ctors by invisible reference. */ 4428 if (TREE_ADDRESSABLE (type)) 4429 type = build_reference_type (type); 4430 else if (PROMOTE_PROTOTYPES 4431 && INTEGRAL_TYPE_P (type) 4432 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)) 4433 type = integer_type_node; 4434 4435 return type; 4436} 4437 4438/* Actually perform the appropriate conversion. */ 4439 4440tree 4441convert_for_arg_passing (type, val) 4442 tree type, val; 4443{ 4444 if (val == error_mark_node) 4445 ; 4446 /* Pass classes with copy ctors by invisible reference. */ 4447 else if (TREE_ADDRESSABLE (type)) 4448 val = build1 (ADDR_EXPR, build_reference_type (type), val); 4449 else if (PROMOTE_PROTOTYPES 4450 && INTEGRAL_TYPE_P (type) 4451 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)) 4452 val = default_conversion (val); 4453 return val; 4454} 4455 4456/* Subroutine of the various build_*_call functions. Overload resolution 4457 has chosen a winning candidate CAND; build up a CALL_EXPR accordingly. 4458 ARGS is a TREE_LIST of the unconverted arguments to the call. FLAGS is a 4459 bitmask of various LOOKUP_* flags which apply to the call itself. */ 4460 4461static tree 4462build_over_call (cand, args, flags) 4463 struct z_candidate *cand; 4464 tree args; 4465 int flags; 4466{ 4467 tree fn = cand->fn; 4468 tree convs = cand->convs; 4469 tree converted_args = NULL_TREE; 4470 tree parm = TYPE_ARG_TYPES (TREE_TYPE (fn)); 4471 tree conv, arg, val; 4472 int i = 0; 4473 int is_method = 0; 4474 4475 /* Give any warnings we noticed during overload resolution. */ 4476 if (cand->warnings) 4477 for (val = cand->warnings; val; val = TREE_CHAIN (val)) 4478 joust (cand, WRAPPER_ZC (TREE_VALUE (val)), 1); 4479 4480 if (DECL_FUNCTION_MEMBER_P (fn)) 4481 enforce_access (cand->access_path, fn); 4482 4483 if (args && TREE_CODE (args) != TREE_LIST) 4484 args = build_tree_list (NULL_TREE, args); 4485 arg = args; 4486 4487 /* The implicit parameters to a constructor are not considered by overload 4488 resolution, and must be of the proper type. */ 4489 if (DECL_CONSTRUCTOR_P (fn)) 4490 { 4491 converted_args = tree_cons (NULL_TREE, TREE_VALUE (arg), converted_args); 4492 arg = TREE_CHAIN (arg); 4493 parm = TREE_CHAIN (parm); 4494 if (DECL_HAS_IN_CHARGE_PARM_P (fn)) 4495 /* We should never try to call the abstract constructor. */ 4496 abort (); 4497 if (DECL_HAS_VTT_PARM_P (fn)) 4498 { 4499 converted_args = tree_cons 4500 (NULL_TREE, TREE_VALUE (arg), converted_args); 4501 arg = TREE_CHAIN (arg); 4502 parm = TREE_CHAIN (parm); 4503 } 4504 } 4505 /* Bypass access control for 'this' parameter. */ 4506 else if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE) 4507 { 4508 tree parmtype = TREE_VALUE (parm); 4509 tree argtype = TREE_TYPE (TREE_VALUE (arg)); 4510 tree converted_arg; 4511 tree base_binfo; 4512 4513 if (ICS_BAD_FLAG (TREE_VEC_ELT (convs, i))) 4514 pedwarn ("passing `%T' as `this' argument of `%#D' discards qualifiers", 4515 TREE_TYPE (argtype), fn); 4516 4517 /* [class.mfct.nonstatic]: If a nonstatic member function of a class 4518 X is called for an object that is not of type X, or of a type 4519 derived from X, the behavior is undefined. 4520 4521 So we can assume that anything passed as 'this' is non-null, and 4522 optimize accordingly. */ 4523 my_friendly_assert (TREE_CODE (parmtype) == POINTER_TYPE, 19990811); 4524 /* Convert to the base in which the function was declared. */ 4525 my_friendly_assert (cand->conversion_path != NULL_TREE, 20020730); 4526 converted_arg = build_base_path (PLUS_EXPR, 4527 TREE_VALUE (arg), 4528 cand->conversion_path, 4529 1); 4530 /* Check that the base class is accessible. */ 4531 if (!accessible_base_p (TREE_TYPE (argtype), 4532 BINFO_TYPE (cand->conversion_path))) 4533 error ("`%T' is not an accessible base of `%T'", 4534 BINFO_TYPE (cand->conversion_path), 4535 TREE_TYPE (argtype)); 4536 /* If fn was found by a using declaration, the conversion path 4537 will be to the derived class, not the base declaring fn. We 4538 must convert from derived to base. */ 4539 base_binfo = lookup_base (TREE_TYPE (TREE_TYPE (converted_arg)), 4540 TREE_TYPE (parmtype), ba_ignore, NULL); 4541 converted_arg = build_base_path (PLUS_EXPR, converted_arg, 4542 base_binfo, 1); 4543 4544 converted_args = tree_cons (NULL_TREE, converted_arg, converted_args); 4545 parm = TREE_CHAIN (parm); 4546 arg = TREE_CHAIN (arg); 4547 ++i; 4548 is_method = 1; 4549 } 4550 4551 for (; arg && parm; 4552 parm = TREE_CHAIN (parm), arg = TREE_CHAIN (arg), ++i) 4553 { 4554 tree type = TREE_VALUE (parm); 4555 4556 conv = TREE_VEC_ELT (convs, i); 4557 val = convert_like_with_context 4558 (conv, TREE_VALUE (arg), fn, i - is_method); 4559 4560 val = convert_for_arg_passing (type, val); 4561 converted_args = tree_cons (NULL_TREE, val, converted_args); 4562 } 4563 4564 /* Default arguments */ 4565 for (; parm && parm != void_list_node; parm = TREE_CHAIN (parm), i++) 4566 converted_args 4567 = tree_cons (NULL_TREE, 4568 convert_default_arg (TREE_VALUE (parm), 4569 TREE_PURPOSE (parm), 4570 fn, i - is_method), 4571 converted_args); 4572 4573 /* Ellipsis */ 4574 for (; arg; arg = TREE_CHAIN (arg)) 4575 converted_args 4576 = tree_cons (NULL_TREE, 4577 convert_arg_to_ellipsis (TREE_VALUE (arg)), 4578 converted_args); 4579 4580 converted_args = nreverse (converted_args); 4581 4582 if (warn_format) 4583 check_function_format (NULL, TYPE_ATTRIBUTES (TREE_TYPE (fn)), 4584 converted_args); 4585 4586 /* Avoid actually calling copy constructors and copy assignment operators, 4587 if possible. */ 4588 4589 if (! flag_elide_constructors) 4590 /* Do things the hard way. */; 4591 else if (TREE_VEC_LENGTH (convs) == 1 4592 && DECL_COPY_CONSTRUCTOR_P (fn)) 4593 { 4594 tree targ; 4595 arg = skip_artificial_parms_for (fn, converted_args); 4596 arg = TREE_VALUE (arg); 4597 4598 /* Pull out the real argument, disregarding const-correctness. */ 4599 targ = arg; 4600 while (TREE_CODE (targ) == NOP_EXPR 4601 || TREE_CODE (targ) == NON_LVALUE_EXPR 4602 || TREE_CODE (targ) == CONVERT_EXPR) 4603 targ = TREE_OPERAND (targ, 0); 4604 if (TREE_CODE (targ) == ADDR_EXPR) 4605 { 4606 targ = TREE_OPERAND (targ, 0); 4607 if (!same_type_ignoring_top_level_qualifiers_p 4608 (TREE_TYPE (TREE_TYPE (arg)), TREE_TYPE (targ))) 4609 targ = NULL_TREE; 4610 } 4611 else 4612 targ = NULL_TREE; 4613 4614 if (targ) 4615 arg = targ; 4616 else 4617 arg = build_indirect_ref (arg, 0); 4618 4619 /* [class.copy]: the copy constructor is implicitly defined even if 4620 the implementation elided its use. */ 4621 if (TYPE_HAS_COMPLEX_INIT_REF (DECL_CONTEXT (fn))) 4622 mark_used (fn); 4623 4624 /* If we're creating a temp and we already have one, don't create a 4625 new one. If we're not creating a temp but we get one, use 4626 INIT_EXPR to collapse the temp into our target. Otherwise, if the 4627 ctor is trivial, do a bitwise copy with a simple TARGET_EXPR for a 4628 temp or an INIT_EXPR otherwise. */ 4629 if (integer_zerop (TREE_VALUE (args))) 4630 { 4631 if (TREE_CODE (arg) == TARGET_EXPR) 4632 return arg; 4633 else if (TYPE_HAS_TRIVIAL_INIT_REF (DECL_CONTEXT (fn))) 4634 return build_target_expr_with_type (arg, DECL_CONTEXT (fn)); 4635 } 4636 else if (TREE_CODE (arg) == TARGET_EXPR 4637 || TYPE_HAS_TRIVIAL_INIT_REF (DECL_CONTEXT (fn))) 4638 { 4639 tree address; 4640 tree to = stabilize_reference 4641 (build_indirect_ref (TREE_VALUE (args), 0)); 4642 4643 val = build (INIT_EXPR, DECL_CONTEXT (fn), to, arg); 4644 address = build_unary_op (ADDR_EXPR, val, 0); 4645 /* Avoid a warning about this expression, if the address is 4646 never used. */ 4647 TREE_USED (address) = 1; 4648 return address; 4649 } 4650 } 4651 else if (DECL_OVERLOADED_OPERATOR_P (fn) == NOP_EXPR 4652 && copy_fn_p (fn) 4653 && TYPE_HAS_TRIVIAL_ASSIGN_REF (DECL_CONTEXT (fn))) 4654 { 4655 tree to = stabilize_reference 4656 (build_indirect_ref (TREE_VALUE (converted_args), 0)); 4657 4658 arg = build_indirect_ref (TREE_VALUE (TREE_CHAIN (converted_args)), 0); 4659 val = build (MODIFY_EXPR, TREE_TYPE (to), to, arg); 4660 return val; 4661 } 4662 4663 mark_used (fn); 4664 4665 if (DECL_VINDEX (fn) && (flags & LOOKUP_NONVIRTUAL) == 0) 4666 { 4667 tree t, *p = &TREE_VALUE (converted_args); 4668 tree binfo = lookup_base (TREE_TYPE (TREE_TYPE (*p)), 4669 DECL_CONTEXT (fn), 4670 ba_any, NULL); 4671 my_friendly_assert (binfo && binfo != error_mark_node, 20010730); 4672 4673 *p = build_base_path (PLUS_EXPR, *p, binfo, 1); 4674 if (TREE_SIDE_EFFECTS (*p)) 4675 *p = save_expr (*p); 4676 t = build_pointer_type (TREE_TYPE (fn)); 4677 if (DECL_CONTEXT (fn) && TYPE_JAVA_INTERFACE (DECL_CONTEXT (fn))) 4678 fn = build_java_interface_fn_ref (fn, *p); 4679 else 4680 fn = build_vfn_ref (build_indirect_ref (*p, 0), DECL_VINDEX (fn)); 4681 TREE_TYPE (fn) = t; 4682 } 4683 else if (DECL_INLINE (fn)) 4684 fn = inline_conversion (fn); 4685 else 4686 fn = build_addr_func (fn); 4687 4688 return build_cxx_call (fn, args, converted_args); 4689} 4690 4691/* Build and return a call to FN, using the the CONVERTED_ARGS. ARGS 4692 gives the original form of the arguments. This function performs 4693 no overload resolution, conversion, or other high-level 4694 operations. */ 4695 4696tree 4697build_cxx_call(tree fn, tree args, tree converted_args) 4698{ 4699 tree fndecl; 4700 4701 /* Recognize certain built-in functions so we can make tree-codes 4702 other than CALL_EXPR. We do this when it enables fold-const.c 4703 to do something useful. */ 4704 if (TREE_CODE (fn) == ADDR_EXPR 4705 && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL 4706 && DECL_BUILT_IN (TREE_OPERAND (fn, 0))) 4707 { 4708 tree exp; 4709 exp = expand_tree_builtin (TREE_OPERAND (fn, 0), args, converted_args); 4710 if (exp) 4711 return exp; 4712 } 4713 4714 fn = build_call (fn, converted_args); 4715 4716 /* If this call might throw an exception, note that fact. */ 4717 fndecl = get_callee_fndecl (fn); 4718 if ((!fndecl || !TREE_NOTHROW (fndecl)) 4719 && at_function_scope_p () 4720 && cfun) 4721 cp_function_chain->can_throw = 1; 4722 4723 /* Some built-in function calls will be evaluated at compile-time in 4724 fold (). */ 4725 fn = fold (fn); 4726 4727 if (VOID_TYPE_P (TREE_TYPE (fn))) 4728 return fn; 4729 4730 fn = require_complete_type (fn); 4731 if (fn == error_mark_node) 4732 return error_mark_node; 4733 4734 if (IS_AGGR_TYPE (TREE_TYPE (fn))) 4735 fn = build_cplus_new (TREE_TYPE (fn), fn); 4736 return convert_from_reference (fn); 4737} 4738 4739static GTY(()) tree java_iface_lookup_fn; 4740 4741/* Make an expression which yields the address of the Java interface 4742 method FN. This is achieved by generating a call to libjava's 4743 _Jv_LookupInterfaceMethodIdx(). */ 4744 4745static tree 4746build_java_interface_fn_ref (fn, instance) 4747 tree fn, instance; 4748{ 4749 tree lookup_args, lookup_fn, method, idx; 4750 tree klass_ref, iface, iface_ref; 4751 int i; 4752 4753 if (!java_iface_lookup_fn) 4754 { 4755 tree endlink = build_void_list_node (); 4756 tree t = tree_cons (NULL_TREE, ptr_type_node, 4757 tree_cons (NULL_TREE, ptr_type_node, 4758 tree_cons (NULL_TREE, java_int_type_node, 4759 endlink))); 4760 java_iface_lookup_fn 4761 = builtin_function ("_Jv_LookupInterfaceMethodIdx", 4762 build_function_type (ptr_type_node, t), 4763 0, NOT_BUILT_IN, NULL, NULL_TREE); 4764 } 4765 4766 /* Look up the pointer to the runtime java.lang.Class object for `instance'. 4767 This is the first entry in the vtable. */ 4768 klass_ref = build_vtbl_ref (build_indirect_ref (instance, 0), 4769 integer_zero_node); 4770 4771 /* Get the java.lang.Class pointer for the interface being called. */ 4772 iface = DECL_CONTEXT (fn); 4773 iface_ref = lookup_field (iface, get_identifier ("class$"), 0, 0); 4774 if (!iface_ref || TREE_CODE (iface_ref) != VAR_DECL 4775 || DECL_CONTEXT (iface_ref) != iface) 4776 { 4777 error ("could not find class$ field in java interface type `%T'", 4778 iface); 4779 return error_mark_node; 4780 } 4781 iface_ref = build1 (ADDR_EXPR, build_pointer_type (iface), iface_ref); 4782 4783 /* Determine the itable index of FN. */ 4784 i = 1; 4785 for (method = TYPE_METHODS (iface); method; method = TREE_CHAIN (method)) 4786 { 4787 if (!DECL_VIRTUAL_P (method)) 4788 continue; 4789 if (fn == method) 4790 break; 4791 i++; 4792 } 4793 idx = build_int_2 (i, 0); 4794 4795 lookup_args = tree_cons (NULL_TREE, klass_ref, 4796 tree_cons (NULL_TREE, iface_ref, 4797 build_tree_list (NULL_TREE, idx))); 4798 lookup_fn = build1 (ADDR_EXPR, 4799 build_pointer_type (TREE_TYPE (java_iface_lookup_fn)), 4800 java_iface_lookup_fn); 4801 return build (CALL_EXPR, ptr_type_node, lookup_fn, lookup_args, NULL_TREE); 4802} 4803 4804/* Returns the value to use for the in-charge parameter when making a 4805 call to a function with the indicated NAME. */ 4806 4807tree 4808in_charge_arg_for_name (name) 4809 tree name; 4810{ 4811 if (name == base_ctor_identifier 4812 || name == base_dtor_identifier) 4813 return integer_zero_node; 4814 else if (name == complete_ctor_identifier) 4815 return integer_one_node; 4816 else if (name == complete_dtor_identifier) 4817 return integer_two_node; 4818 else if (name == deleting_dtor_identifier) 4819 return integer_three_node; 4820 4821 /* This function should only be called with one of the names listed 4822 above. */ 4823 abort (); 4824 return NULL_TREE; 4825} 4826 4827/* Build a call to a constructor, destructor, or an assignment 4828 operator for INSTANCE, an expression with class type. NAME 4829 indicates the special member function to call; ARGS are the 4830 arguments. BINFO indicates the base of INSTANCE that is to be 4831 passed as the `this' parameter to the member function called. 4832 4833 FLAGS are the LOOKUP_* flags to use when processing the call. 4834 4835 If NAME indicates a complete object constructor, INSTANCE may be 4836 NULL_TREE. In this case, the caller will call build_cplus_new to 4837 store the newly constructed object into a VAR_DECL. */ 4838 4839tree 4840build_special_member_call (tree instance, tree name, tree args, 4841 tree binfo, int flags) 4842{ 4843 tree fns; 4844 /* The type of the subobject to be constructed or destroyed. */ 4845 tree class_type; 4846 4847 my_friendly_assert (name == complete_ctor_identifier 4848 || name == base_ctor_identifier 4849 || name == complete_dtor_identifier 4850 || name == base_dtor_identifier 4851 || name == deleting_dtor_identifier 4852 || name == ansi_assopname (NOP_EXPR), 4853 20020712); 4854 my_friendly_assert (binfo != NULL_TREE, 20020712); 4855 4856 class_type = BINFO_TYPE (binfo); 4857 4858 /* Handle the special case where INSTANCE is NULL_TREE. */ 4859 if (name == complete_ctor_identifier && !instance) 4860 { 4861 instance = build_int_2 (0, 0); 4862 TREE_TYPE (instance) = build_pointer_type (class_type); 4863 instance = build1 (INDIRECT_REF, class_type, instance); 4864 } 4865 else 4866 { 4867 if (name == complete_dtor_identifier 4868 || name == base_dtor_identifier 4869 || name == deleting_dtor_identifier) 4870 my_friendly_assert (args == NULL_TREE, 20020712); 4871 4872 /* We must perform the conversion here so that we do not 4873 subsequently check to see whether BINFO is an accessible 4874 base. (It is OK for a constructor to call a constructor in 4875 an inaccessible base as long as the constructor being called 4876 is accessible.) */ 4877 if (!same_type_ignoring_top_level_qualifiers_p 4878 (TREE_TYPE (instance), BINFO_TYPE (binfo))) 4879 instance = convert_to_base_statically (instance, binfo); 4880 } 4881 4882 my_friendly_assert (instance != NULL_TREE, 20020712); 4883 4884 /* Resolve the name. */ 4885 if (!complete_type_or_else (BINFO_TYPE (binfo), NULL_TREE)) 4886 return error_mark_node; 4887 4888 fns = lookup_fnfields (binfo, name, 1); 4889 4890 /* When making a call to a constructor or destructor for a subobject 4891 that uses virtual base classes, pass down a pointer to a VTT for 4892 the subobject. */ 4893 if ((name == base_ctor_identifier 4894 || name == base_dtor_identifier) 4895 && TYPE_USES_VIRTUAL_BASECLASSES (class_type)) 4896 { 4897 tree vtt; 4898 tree sub_vtt; 4899 4900 /* If the current function is a complete object constructor 4901 or destructor, then we fetch the VTT directly. 4902 Otherwise, we look it up using the VTT we were given. */ 4903 vtt = TREE_CHAIN (CLASSTYPE_VTABLES (current_class_type)); 4904 vtt = decay_conversion (vtt); 4905 vtt = build (COND_EXPR, TREE_TYPE (vtt), 4906 build (EQ_EXPR, boolean_type_node, 4907 current_in_charge_parm, integer_zero_node), 4908 current_vtt_parm, 4909 vtt); 4910 if (TREE_VIA_VIRTUAL (binfo)) 4911 binfo = binfo_for_vbase (class_type, current_class_type); 4912 my_friendly_assert (BINFO_SUBVTT_INDEX (binfo), 20010110); 4913 sub_vtt = build (PLUS_EXPR, TREE_TYPE (vtt), vtt, 4914 BINFO_SUBVTT_INDEX (binfo)); 4915 4916 args = tree_cons (NULL_TREE, sub_vtt, args); 4917 } 4918 4919 return build_new_method_call (instance, fns, args, 4920 TYPE_BINFO (BINFO_TYPE (binfo)), 4921 flags); 4922} 4923 4924/* Build a call to "INSTANCE.FN (ARGS)". */ 4925 4926tree 4927build_new_method_call (tree instance, tree fns, tree args, 4928 tree conversion_path, int flags) 4929{ 4930 struct z_candidate *candidates = 0, *cand; 4931 tree explicit_targs = NULL_TREE; 4932 tree basetype = NULL_TREE; 4933 tree access_binfo; 4934 tree optype; 4935 tree mem_args = NULL_TREE, instance_ptr; 4936 tree name, pretty_name; 4937 tree user_args; 4938 tree call; 4939 int template_only = 0; 4940 4941 my_friendly_assert (instance != NULL_TREE, 20020729); 4942 4943 if (instance == error_mark_node || fns == error_mark_node 4944 || args == error_mark_node) 4945 return error_mark_node; 4946 4947 /* Process the argument list. */ 4948 user_args = args; 4949 args = resolve_args (args); 4950 if (args == error_mark_node) 4951 return error_mark_node; 4952 4953 if (TREE_CODE (instance) == OFFSET_REF) 4954 instance = resolve_offset_ref (instance); 4955 if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE) 4956 instance = convert_from_reference (instance); 4957 basetype = TYPE_MAIN_VARIANT (TREE_TYPE (instance)); 4958 instance_ptr = build_this (instance); 4959 4960 if (!BASELINK_P (fns)) 4961 { 4962 call = build_field_call (instance_ptr, fns, args); 4963 if (call) 4964 return call; 4965 error ("call to non-function `%D'", fns); 4966 return error_mark_node; 4967 } 4968 4969 if (!conversion_path) 4970 conversion_path = BASELINK_BINFO (fns); 4971 access_binfo = BASELINK_ACCESS_BINFO (fns); 4972 optype = BASELINK_OPTYPE (fns); 4973 fns = BASELINK_FUNCTIONS (fns); 4974 4975 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR) 4976 { 4977 explicit_targs = TREE_OPERAND (fns, 1); 4978 fns = TREE_OPERAND (fns, 0); 4979 template_only = 1; 4980 } 4981 4982 my_friendly_assert (TREE_CODE (fns) == FUNCTION_DECL 4983 || TREE_CODE (fns) == TEMPLATE_DECL 4984 || TREE_CODE (fns) == OVERLOAD, 4985 20020712); 4986 4987 /* XXX this should be handled before we get here. */ 4988 if (! IS_AGGR_TYPE (basetype)) 4989 { 4990 if ((flags & LOOKUP_COMPLAIN) && basetype != error_mark_node) 4991 error ("request for member `%D' in `%E', which is of non-aggregate type `%T'", 4992 fns, instance, basetype); 4993 4994 return error_mark_node; 4995 } 4996 4997 name = DECL_NAME (get_first_fn (fns)); 4998 4999 if (IDENTIFIER_CTOR_OR_DTOR_P (name)) 5000 { 5001 /* Callers should explicitly indicate whether they want to construct 5002 the complete object or just the part without virtual bases. */ 5003 my_friendly_assert (name != ctor_identifier, 20000408); 5004 /* Similarly for destructors. */ 5005 my_friendly_assert (name != dtor_identifier, 20000408); 5006 5007 if (name == complete_ctor_identifier 5008 || name == base_ctor_identifier) 5009 pretty_name = constructor_name (basetype); 5010 else 5011 pretty_name = dtor_identifier; 5012 } 5013 else 5014 pretty_name = name; 5015 5016 if (fns) 5017 { 5018 tree fn; 5019 tree class_type = (conversion_path 5020 ? BINFO_TYPE (conversion_path) 5021 : NULL_TREE); 5022 5023 mem_args = tree_cons (NULL_TREE, instance_ptr, args); 5024 for (fn = fns; fn; fn = OVL_NEXT (fn)) 5025 { 5026 tree t = OVL_CURRENT (fn); 5027 tree this_arglist; 5028 5029 /* We can end up here for copy-init of same or base class. */ 5030 if ((flags & LOOKUP_ONLYCONVERTING) 5031 && DECL_NONCONVERTING_P (t)) 5032 continue; 5033 5034 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (t)) 5035 this_arglist = mem_args; 5036 else 5037 this_arglist = args; 5038 5039 if (TREE_CODE (t) == TEMPLATE_DECL) 5040 /* A member template. */ 5041 add_template_candidate (&candidates, t, 5042 class_type, 5043 explicit_targs, 5044 this_arglist, optype, 5045 access_binfo, 5046 conversion_path, 5047 flags, 5048 DEDUCE_CALL); 5049 else if (! template_only) 5050 add_function_candidate (&candidates, t, 5051 class_type, 5052 this_arglist, 5053 access_binfo, 5054 conversion_path, 5055 flags); 5056 } 5057 } 5058 5059 if (! any_viable (candidates)) 5060 { 5061 /* XXX will LOOKUP_SPECULATIVELY be needed when this is done? */ 5062 if (flags & LOOKUP_SPECULATIVELY) 5063 return NULL_TREE; 5064 if (!COMPLETE_TYPE_P (basetype)) 5065 cxx_incomplete_type_error (instance_ptr, basetype); 5066 else 5067 error ("no matching function for call to `%T::%D(%A)%#V'", 5068 basetype, pretty_name, user_args, 5069 TREE_TYPE (TREE_TYPE (instance_ptr))); 5070 print_z_candidates (candidates); 5071 return error_mark_node; 5072 } 5073 candidates = splice_viable (candidates); 5074 cand = tourney (candidates); 5075 5076 if (cand == 0) 5077 { 5078 error ("call of overloaded `%D(%A)' is ambiguous", pretty_name, 5079 user_args); 5080 print_z_candidates (candidates); 5081 return error_mark_node; 5082 } 5083 5084 if (DECL_PURE_VIRTUAL_P (cand->fn) 5085 && instance == current_class_ref 5086 && (DECL_CONSTRUCTOR_P (current_function_decl) 5087 || DECL_DESTRUCTOR_P (current_function_decl)) 5088 && ! (flags & LOOKUP_NONVIRTUAL) 5089 && value_member (cand->fn, CLASSTYPE_PURE_VIRTUALS (basetype))) 5090 error ((DECL_CONSTRUCTOR_P (current_function_decl) ? 5091 "abstract virtual `%#D' called from constructor" 5092 : "abstract virtual `%#D' called from destructor"), 5093 cand->fn); 5094 if (TREE_CODE (TREE_TYPE (cand->fn)) == METHOD_TYPE 5095 && is_dummy_object (instance_ptr)) 5096 { 5097 error ("cannot call member function `%D' without object", cand->fn); 5098 return error_mark_node; 5099 } 5100 5101 if (DECL_VINDEX (cand->fn) && ! (flags & LOOKUP_NONVIRTUAL) 5102 && resolves_to_fixed_type_p (instance, 0)) 5103 flags |= LOOKUP_NONVIRTUAL; 5104 5105 if (TREE_CODE (TREE_TYPE (cand->fn)) == METHOD_TYPE) 5106 call = build_over_call (cand, mem_args, flags); 5107 else 5108 { 5109 call = build_over_call (cand, args, flags); 5110 /* In an expression of the form `a->f()' where `f' turns out to 5111 be a static member function, `a' is none-the-less evaluated. */ 5112 if (instance && TREE_SIDE_EFFECTS (instance)) 5113 call = build (COMPOUND_EXPR, TREE_TYPE (call), instance, call); 5114 } 5115 5116 return call; 5117} 5118 5119/* Returns nonzero iff standard conversion sequence ICS1 is a proper 5120 subsequence of ICS2. */ 5121 5122static int 5123is_subseq (ics1, ics2) 5124 tree ics1, ics2; 5125{ 5126 /* We can assume that a conversion of the same code 5127 between the same types indicates a subsequence since we only get 5128 here if the types we are converting from are the same. */ 5129 5130 while (TREE_CODE (ics1) == RVALUE_CONV 5131 || TREE_CODE (ics1) == LVALUE_CONV) 5132 ics1 = TREE_OPERAND (ics1, 0); 5133 5134 while (1) 5135 { 5136 while (TREE_CODE (ics2) == RVALUE_CONV 5137 || TREE_CODE (ics2) == LVALUE_CONV) 5138 ics2 = TREE_OPERAND (ics2, 0); 5139 5140 if (TREE_CODE (ics2) == USER_CONV 5141 || TREE_CODE (ics2) == AMBIG_CONV 5142 || TREE_CODE (ics2) == IDENTITY_CONV) 5143 /* At this point, ICS1 cannot be a proper subsequence of 5144 ICS2. We can get a USER_CONV when we are comparing the 5145 second standard conversion sequence of two user conversion 5146 sequences. */ 5147 return 0; 5148 5149 ics2 = TREE_OPERAND (ics2, 0); 5150 5151 if (TREE_CODE (ics2) == TREE_CODE (ics1) 5152 && same_type_p (TREE_TYPE (ics2), TREE_TYPE (ics1)) 5153 && same_type_p (TREE_TYPE (TREE_OPERAND (ics2, 0)), 5154 TREE_TYPE (TREE_OPERAND (ics1, 0)))) 5155 return 1; 5156 } 5157} 5158 5159/* Returns nonzero iff DERIVED is derived from BASE. The inputs may 5160 be any _TYPE nodes. */ 5161 5162int 5163is_properly_derived_from (derived, base) 5164 tree derived; 5165 tree base; 5166{ 5167 if (!IS_AGGR_TYPE_CODE (TREE_CODE (derived)) 5168 || !IS_AGGR_TYPE_CODE (TREE_CODE (base))) 5169 return 0; 5170 5171 /* We only allow proper derivation here. The DERIVED_FROM_P macro 5172 considers every class derived from itself. */ 5173 return (!same_type_ignoring_top_level_qualifiers_p (derived, base) 5174 && DERIVED_FROM_P (base, derived)); 5175} 5176 5177/* We build the ICS for an implicit object parameter as a pointer 5178 conversion sequence. However, such a sequence should be compared 5179 as if it were a reference conversion sequence. If ICS is the 5180 implicit conversion sequence for an implicit object parameter, 5181 modify it accordingly. */ 5182 5183static void 5184maybe_handle_implicit_object (ics) 5185 tree* ics; 5186{ 5187 if (ICS_THIS_FLAG (*ics)) 5188 { 5189 /* [over.match.funcs] 5190 5191 For non-static member functions, the type of the 5192 implicit object parameter is "reference to cv X" 5193 where X is the class of which the function is a 5194 member and cv is the cv-qualification on the member 5195 function declaration. */ 5196 tree t = *ics; 5197 tree reference_type; 5198 5199 /* The `this' parameter is a pointer to a class type. Make the 5200 implict conversion talk about a reference to that same class 5201 type. */ 5202 reference_type = TREE_TYPE (TREE_TYPE (*ics)); 5203 reference_type = build_reference_type (reference_type); 5204 5205 if (TREE_CODE (t) == QUAL_CONV) 5206 t = TREE_OPERAND (t, 0); 5207 if (TREE_CODE (t) == PTR_CONV) 5208 t = TREE_OPERAND (t, 0); 5209 t = build1 (IDENTITY_CONV, TREE_TYPE (TREE_TYPE (t)), NULL_TREE); 5210 t = direct_reference_binding (reference_type, t); 5211 *ics = t; 5212 } 5213} 5214 5215/* If *ICS is a REF_BIND set *ICS to the remainder of the conversion, 5216 and return the type to which the reference refers. Otherwise, 5217 leave *ICS unchanged and return NULL_TREE. */ 5218 5219static tree 5220maybe_handle_ref_bind (ics) 5221 tree* ics; 5222{ 5223 if (TREE_CODE (*ics) == REF_BIND) 5224 { 5225 tree old_ics = *ics; 5226 tree type = TREE_TYPE (TREE_TYPE (old_ics)); 5227 *ics = TREE_OPERAND (old_ics, 0); 5228 ICS_USER_FLAG (*ics) = ICS_USER_FLAG (old_ics); 5229 ICS_BAD_FLAG (*ics) = ICS_BAD_FLAG (old_ics); 5230 return type; 5231 } 5232 5233 return NULL_TREE; 5234} 5235 5236/* Compare two implicit conversion sequences according to the rules set out in 5237 [over.ics.rank]. Return values: 5238 5239 1: ics1 is better than ics2 5240 -1: ics2 is better than ics1 5241 0: ics1 and ics2 are indistinguishable */ 5242 5243static int 5244compare_ics (ics1, ics2) 5245 tree ics1, ics2; 5246{ 5247 tree from_type1; 5248 tree from_type2; 5249 tree to_type1; 5250 tree to_type2; 5251 tree deref_from_type1 = NULL_TREE; 5252 tree deref_from_type2 = NULL_TREE; 5253 tree deref_to_type1 = NULL_TREE; 5254 tree deref_to_type2 = NULL_TREE; 5255 int rank1, rank2; 5256 5257 /* REF_BINDING is nonzero if the result of the conversion sequence 5258 is a reference type. In that case TARGET_TYPE is the 5259 type referred to by the reference. */ 5260 tree target_type1; 5261 tree target_type2; 5262 5263 /* Handle implicit object parameters. */ 5264 maybe_handle_implicit_object (&ics1); 5265 maybe_handle_implicit_object (&ics2); 5266 5267 /* Handle reference parameters. */ 5268 target_type1 = maybe_handle_ref_bind (&ics1); 5269 target_type2 = maybe_handle_ref_bind (&ics2); 5270 5271 /* [over.ics.rank] 5272 5273 When comparing the basic forms of implicit conversion sequences (as 5274 defined in _over.best.ics_) 5275 5276 --a standard conversion sequence (_over.ics.scs_) is a better 5277 conversion sequence than a user-defined conversion sequence 5278 or an ellipsis conversion sequence, and 5279 5280 --a user-defined conversion sequence (_over.ics.user_) is a 5281 better conversion sequence than an ellipsis conversion sequence 5282 (_over.ics.ellipsis_). */ 5283 rank1 = ICS_RANK (ics1); 5284 rank2 = ICS_RANK (ics2); 5285 5286 if (rank1 > rank2) 5287 return -1; 5288 else if (rank1 < rank2) 5289 return 1; 5290 5291 if (rank1 == BAD_RANK) 5292 { 5293 /* XXX Isn't this an extension? */ 5294 /* Both ICS are bad. We try to make a decision based on what 5295 would have happenned if they'd been good. */ 5296 if (ICS_USER_FLAG (ics1) > ICS_USER_FLAG (ics2) 5297 || ICS_STD_RANK (ics1) > ICS_STD_RANK (ics2)) 5298 return -1; 5299 else if (ICS_USER_FLAG (ics1) < ICS_USER_FLAG (ics2) 5300 || ICS_STD_RANK (ics1) < ICS_STD_RANK (ics2)) 5301 return 1; 5302 5303 /* We couldn't make up our minds; try to figure it out below. */ 5304 } 5305 5306 if (ICS_ELLIPSIS_FLAG (ics1)) 5307 /* Both conversions are ellipsis conversions. */ 5308 return 0; 5309 5310 /* User-defined conversion sequence U1 is a better conversion sequence 5311 than another user-defined conversion sequence U2 if they contain the 5312 same user-defined conversion operator or constructor and if the sec- 5313 ond standard conversion sequence of U1 is better than the second 5314 standard conversion sequence of U2. */ 5315 5316 if (ICS_USER_FLAG (ics1)) 5317 { 5318 tree t1, t2; 5319 5320 for (t1 = ics1; TREE_CODE (t1) != USER_CONV; t1 = TREE_OPERAND (t1, 0)) 5321 if (TREE_CODE (t1) == AMBIG_CONV) 5322 return 0; 5323 for (t2 = ics2; TREE_CODE (t2) != USER_CONV; t2 = TREE_OPERAND (t2, 0)) 5324 if (TREE_CODE (t2) == AMBIG_CONV) 5325 return 0; 5326 5327 if (USER_CONV_FN (t1) != USER_CONV_FN (t2)) 5328 return 0; 5329 5330 /* We can just fall through here, after setting up 5331 FROM_TYPE1 and FROM_TYPE2. */ 5332 from_type1 = TREE_TYPE (t1); 5333 from_type2 = TREE_TYPE (t2); 5334 } 5335 else 5336 { 5337 /* We're dealing with two standard conversion sequences. 5338 5339 [over.ics.rank] 5340 5341 Standard conversion sequence S1 is a better conversion 5342 sequence than standard conversion sequence S2 if 5343 5344 --S1 is a proper subsequence of S2 (comparing the conversion 5345 sequences in the canonical form defined by _over.ics.scs_, 5346 excluding any Lvalue Transformation; the identity 5347 conversion sequence is considered to be a subsequence of 5348 any non-identity conversion sequence */ 5349 5350 from_type1 = ics1; 5351 while (TREE_CODE (from_type1) != IDENTITY_CONV) 5352 from_type1 = TREE_OPERAND (from_type1, 0); 5353 from_type1 = TREE_TYPE (from_type1); 5354 5355 from_type2 = ics2; 5356 while (TREE_CODE (from_type2) != IDENTITY_CONV) 5357 from_type2 = TREE_OPERAND (from_type2, 0); 5358 from_type2 = TREE_TYPE (from_type2); 5359 } 5360 5361 if (same_type_p (from_type1, from_type2)) 5362 { 5363 if (is_subseq (ics1, ics2)) 5364 return 1; 5365 if (is_subseq (ics2, ics1)) 5366 return -1; 5367 } 5368 /* Otherwise, one sequence cannot be a subsequence of the other; they 5369 don't start with the same type. This can happen when comparing the 5370 second standard conversion sequence in two user-defined conversion 5371 sequences. */ 5372 5373 /* [over.ics.rank] 5374 5375 Or, if not that, 5376 5377 --the rank of S1 is better than the rank of S2 (by the rules 5378 defined below): 5379 5380 Standard conversion sequences are ordered by their ranks: an Exact 5381 Match is a better conversion than a Promotion, which is a better 5382 conversion than a Conversion. 5383 5384 Two conversion sequences with the same rank are indistinguishable 5385 unless one of the following rules applies: 5386 5387 --A conversion that is not a conversion of a pointer, or pointer 5388 to member, to bool is better than another conversion that is such 5389 a conversion. 5390 5391 The ICS_STD_RANK automatically handles the pointer-to-bool rule, 5392 so that we do not have to check it explicitly. */ 5393 if (ICS_STD_RANK (ics1) < ICS_STD_RANK (ics2)) 5394 return 1; 5395 else if (ICS_STD_RANK (ics2) < ICS_STD_RANK (ics1)) 5396 return -1; 5397 5398 to_type1 = TREE_TYPE (ics1); 5399 to_type2 = TREE_TYPE (ics2); 5400 5401 if (TYPE_PTR_P (from_type1) 5402 && TYPE_PTR_P (from_type2) 5403 && TYPE_PTR_P (to_type1) 5404 && TYPE_PTR_P (to_type2)) 5405 { 5406 deref_from_type1 = TREE_TYPE (from_type1); 5407 deref_from_type2 = TREE_TYPE (from_type2); 5408 deref_to_type1 = TREE_TYPE (to_type1); 5409 deref_to_type2 = TREE_TYPE (to_type2); 5410 } 5411 /* The rules for pointers to members A::* are just like the rules 5412 for pointers A*, except opposite: if B is derived from A then 5413 A::* converts to B::*, not vice versa. For that reason, we 5414 switch the from_ and to_ variables here. */ 5415 else if (TYPE_PTRMEM_P (from_type1) 5416 && TYPE_PTRMEM_P (from_type2) 5417 && TYPE_PTRMEM_P (to_type1) 5418 && TYPE_PTRMEM_P (to_type2)) 5419 { 5420 deref_to_type1 = TYPE_OFFSET_BASETYPE (TREE_TYPE (from_type1)); 5421 deref_to_type2 = TYPE_OFFSET_BASETYPE (TREE_TYPE (from_type2)); 5422 deref_from_type1 = TYPE_OFFSET_BASETYPE (TREE_TYPE (to_type1)); 5423 deref_from_type2 = TYPE_OFFSET_BASETYPE (TREE_TYPE (to_type2)); 5424 } 5425 else if (TYPE_PTRMEMFUNC_P (from_type1) 5426 && TYPE_PTRMEMFUNC_P (from_type2) 5427 && TYPE_PTRMEMFUNC_P (to_type1) 5428 && TYPE_PTRMEMFUNC_P (to_type2)) 5429 { 5430 deref_to_type1 = TYPE_PTRMEMFUNC_OBJECT_TYPE (from_type1); 5431 deref_to_type2 = TYPE_PTRMEMFUNC_OBJECT_TYPE (from_type2); 5432 deref_from_type1 = TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type1); 5433 deref_from_type2 = TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type2); 5434 } 5435 5436 if (deref_from_type1 != NULL_TREE 5437 && IS_AGGR_TYPE_CODE (TREE_CODE (deref_from_type1)) 5438 && IS_AGGR_TYPE_CODE (TREE_CODE (deref_from_type2))) 5439 { 5440 /* This was one of the pointer or pointer-like conversions. 5441 5442 [over.ics.rank] 5443 5444 --If class B is derived directly or indirectly from class A, 5445 conversion of B* to A* is better than conversion of B* to 5446 void*, and conversion of A* to void* is better than 5447 conversion of B* to void*. */ 5448 if (TREE_CODE (deref_to_type1) == VOID_TYPE 5449 && TREE_CODE (deref_to_type2) == VOID_TYPE) 5450 { 5451 if (is_properly_derived_from (deref_from_type1, 5452 deref_from_type2)) 5453 return -1; 5454 else if (is_properly_derived_from (deref_from_type2, 5455 deref_from_type1)) 5456 return 1; 5457 } 5458 else if (TREE_CODE (deref_to_type1) == VOID_TYPE 5459 || TREE_CODE (deref_to_type2) == VOID_TYPE) 5460 { 5461 if (same_type_p (deref_from_type1, deref_from_type2)) 5462 { 5463 if (TREE_CODE (deref_to_type2) == VOID_TYPE) 5464 { 5465 if (is_properly_derived_from (deref_from_type1, 5466 deref_to_type1)) 5467 return 1; 5468 } 5469 /* We know that DEREF_TO_TYPE1 is `void' here. */ 5470 else if (is_properly_derived_from (deref_from_type1, 5471 deref_to_type2)) 5472 return -1; 5473 } 5474 } 5475 else if (IS_AGGR_TYPE_CODE (TREE_CODE (deref_to_type1)) 5476 && IS_AGGR_TYPE_CODE (TREE_CODE (deref_to_type2))) 5477 { 5478 /* [over.ics.rank] 5479 5480 --If class B is derived directly or indirectly from class A 5481 and class C is derived directly or indirectly from B, 5482 5483 --conversion of C* to B* is better than conversion of C* to 5484 A*, 5485 5486 --conversion of B* to A* is better than conversion of C* to 5487 A* */ 5488 if (same_type_p (deref_from_type1, deref_from_type2)) 5489 { 5490 if (is_properly_derived_from (deref_to_type1, 5491 deref_to_type2)) 5492 return 1; 5493 else if (is_properly_derived_from (deref_to_type2, 5494 deref_to_type1)) 5495 return -1; 5496 } 5497 else if (same_type_p (deref_to_type1, deref_to_type2)) 5498 { 5499 if (is_properly_derived_from (deref_from_type2, 5500 deref_from_type1)) 5501 return 1; 5502 else if (is_properly_derived_from (deref_from_type1, 5503 deref_from_type2)) 5504 return -1; 5505 } 5506 } 5507 } 5508 else if (CLASS_TYPE_P (non_reference (from_type1)) 5509 && same_type_p (from_type1, from_type2)) 5510 { 5511 tree from = non_reference (from_type1); 5512 5513 /* [over.ics.rank] 5514 5515 --binding of an expression of type C to a reference of type 5516 B& is better than binding an expression of type C to a 5517 reference of type A& 5518 5519 --conversion of C to B is better than conversion of C to A, */ 5520 if (is_properly_derived_from (from, to_type1) 5521 && is_properly_derived_from (from, to_type2)) 5522 { 5523 if (is_properly_derived_from (to_type1, to_type2)) 5524 return 1; 5525 else if (is_properly_derived_from (to_type2, to_type1)) 5526 return -1; 5527 } 5528 } 5529 else if (CLASS_TYPE_P (non_reference (to_type1)) 5530 && same_type_p (to_type1, to_type2)) 5531 { 5532 tree to = non_reference (to_type1); 5533 5534 /* [over.ics.rank] 5535 5536 --binding of an expression of type B to a reference of type 5537 A& is better than binding an expression of type C to a 5538 reference of type A&, 5539 5540 --onversion of B to A is better than conversion of C to A */ 5541 if (is_properly_derived_from (from_type1, to) 5542 && is_properly_derived_from (from_type2, to)) 5543 { 5544 if (is_properly_derived_from (from_type2, from_type1)) 5545 return 1; 5546 else if (is_properly_derived_from (from_type1, from_type2)) 5547 return -1; 5548 } 5549 } 5550 5551 /* [over.ics.rank] 5552 5553 --S1 and S2 differ only in their qualification conversion and yield 5554 similar types T1 and T2 (_conv.qual_), respectively, and the cv- 5555 qualification signature of type T1 is a proper subset of the cv- 5556 qualification signature of type T2 */ 5557 if (TREE_CODE (ics1) == QUAL_CONV 5558 && TREE_CODE (ics2) == QUAL_CONV 5559 && same_type_p (from_type1, from_type2)) 5560 return comp_cv_qual_signature (to_type1, to_type2); 5561 5562 /* [over.ics.rank] 5563 5564 --S1 and S2 are reference bindings (_dcl.init.ref_), and the 5565 types to which the references refer are the same type except for 5566 top-level cv-qualifiers, and the type to which the reference 5567 initialized by S2 refers is more cv-qualified than the type to 5568 which the reference initialized by S1 refers */ 5569 5570 if (target_type1 && target_type2 5571 && same_type_ignoring_top_level_qualifiers_p (to_type1, to_type2)) 5572 return comp_cv_qualification (target_type2, target_type1); 5573 5574 /* Neither conversion sequence is better than the other. */ 5575 return 0; 5576} 5577 5578/* The source type for this standard conversion sequence. */ 5579 5580static tree 5581source_type (t) 5582 tree t; 5583{ 5584 for (;; t = TREE_OPERAND (t, 0)) 5585 { 5586 if (TREE_CODE (t) == USER_CONV 5587 || TREE_CODE (t) == AMBIG_CONV 5588 || TREE_CODE (t) == IDENTITY_CONV) 5589 return TREE_TYPE (t); 5590 } 5591 abort (); 5592} 5593 5594/* Note a warning about preferring WINNER to LOSER. We do this by storing 5595 a pointer to LOSER and re-running joust to produce the warning if WINNER 5596 is actually used. */ 5597 5598static void 5599add_warning (winner, loser) 5600 struct z_candidate *winner, *loser; 5601{ 5602 winner->warnings = tree_cons (NULL_TREE, 5603 build_zc_wrapper (loser), 5604 winner->warnings); 5605} 5606 5607/* Compare two candidates for overloading as described in 5608 [over.match.best]. Return values: 5609 5610 1: cand1 is better than cand2 5611 -1: cand2 is better than cand1 5612 0: cand1 and cand2 are indistinguishable */ 5613 5614static int 5615joust (cand1, cand2, warn) 5616 struct z_candidate *cand1, *cand2; 5617 int warn; 5618{ 5619 int winner = 0; 5620 int i, off1 = 0, off2 = 0, len; 5621 5622 /* Candidates that involve bad conversions are always worse than those 5623 that don't. */ 5624 if (cand1->viable > cand2->viable) 5625 return 1; 5626 if (cand1->viable < cand2->viable) 5627 return -1; 5628 5629 /* If we have two pseudo-candidates for conversions to the same type, 5630 or two candidates for the same function, arbitrarily pick one. */ 5631 if (cand1->fn == cand2->fn 5632 && (TYPE_P (cand1->fn) || DECL_P (cand1->fn))) 5633 return 1; 5634 5635 /* a viable function F1 5636 is defined to be a better function than another viable function F2 if 5637 for all arguments i, ICSi(F1) is not a worse conversion sequence than 5638 ICSi(F2), and then */ 5639 5640 /* for some argument j, ICSj(F1) is a better conversion sequence than 5641 ICSj(F2) */ 5642 5643 /* For comparing static and non-static member functions, we ignore 5644 the implicit object parameter of the non-static function. The 5645 standard says to pretend that the static function has an object 5646 parm, but that won't work with operator overloading. */ 5647 len = TREE_VEC_LENGTH (cand1->convs); 5648 if (len != TREE_VEC_LENGTH (cand2->convs)) 5649 { 5650 if (DECL_STATIC_FUNCTION_P (cand1->fn) 5651 && ! DECL_STATIC_FUNCTION_P (cand2->fn)) 5652 off2 = 1; 5653 else if (! DECL_STATIC_FUNCTION_P (cand1->fn) 5654 && DECL_STATIC_FUNCTION_P (cand2->fn)) 5655 { 5656 off1 = 1; 5657 --len; 5658 } 5659 else 5660 abort (); 5661 } 5662 5663 for (i = 0; i < len; ++i) 5664 { 5665 tree t1 = TREE_VEC_ELT (cand1->convs, i+off1); 5666 tree t2 = TREE_VEC_ELT (cand2->convs, i+off2); 5667 int comp = compare_ics (t1, t2); 5668 5669 if (comp != 0) 5670 { 5671 if (warn_sign_promo 5672 && ICS_RANK (t1) + ICS_RANK (t2) == STD_RANK + PROMO_RANK 5673 && TREE_CODE (t1) == STD_CONV 5674 && TREE_CODE (t2) == STD_CONV 5675 && TREE_CODE (TREE_TYPE (t1)) == INTEGER_TYPE 5676 && TREE_CODE (TREE_TYPE (t2)) == INTEGER_TYPE 5677 && (TYPE_PRECISION (TREE_TYPE (t1)) 5678 == TYPE_PRECISION (TREE_TYPE (t2))) 5679 && (TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (t1, 0))) 5680 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (t1, 0))) 5681 == ENUMERAL_TYPE))) 5682 { 5683 tree type = TREE_TYPE (TREE_OPERAND (t1, 0)); 5684 tree type1, type2; 5685 struct z_candidate *w, *l; 5686 if (comp > 0) 5687 type1 = TREE_TYPE (t1), type2 = TREE_TYPE (t2), 5688 w = cand1, l = cand2; 5689 else 5690 type1 = TREE_TYPE (t2), type2 = TREE_TYPE (t1), 5691 w = cand2, l = cand1; 5692 5693 if (warn) 5694 { 5695 warning ("passing `%T' chooses `%T' over `%T'", 5696 type, type1, type2); 5697 warning (" in call to `%D'", w->fn); 5698 } 5699 else 5700 add_warning (w, l); 5701 } 5702 5703 if (winner && comp != winner) 5704 { 5705 winner = 0; 5706 goto tweak; 5707 } 5708 winner = comp; 5709 } 5710 } 5711 5712 /* warn about confusing overload resolution for user-defined conversions, 5713 either between a constructor and a conversion op, or between two 5714 conversion ops. */ 5715 if (winner && cand1->second_conv 5716 && ((DECL_CONSTRUCTOR_P (cand1->fn) 5717 != DECL_CONSTRUCTOR_P (cand2->fn)) 5718 /* Don't warn if the two conv ops convert to the same type... */ 5719 || (! DECL_CONSTRUCTOR_P (cand1->fn) 5720 && ! same_type_p (TREE_TYPE (TREE_TYPE (cand1->fn)), 5721 TREE_TYPE (TREE_TYPE (cand2->fn)))))) 5722 { 5723 int comp = compare_ics (cand1->second_conv, cand2->second_conv); 5724 if (comp != winner) 5725 { 5726 struct z_candidate *w, *l; 5727 tree convn; 5728 if (winner == 1) 5729 w = cand1, l = cand2; 5730 else 5731 w = cand2, l = cand1; 5732 if (DECL_CONTEXT (cand1->fn) == DECL_CONTEXT (cand2->fn) 5733 && ! DECL_CONSTRUCTOR_P (cand1->fn) 5734 && ! DECL_CONSTRUCTOR_P (cand2->fn) 5735 && (convn = standard_conversion 5736 (TREE_TYPE (TREE_TYPE (l->fn)), 5737 TREE_TYPE (TREE_TYPE (w->fn)), NULL_TREE)) 5738 && TREE_CODE (convn) == QUAL_CONV) 5739 /* Don't complain about `operator char *()' beating 5740 `operator const char *() const'. */; 5741 else if (warn && warn_conversion) 5742 { 5743 tree source = source_type (TREE_VEC_ELT (w->convs, 0)); 5744 if (! DECL_CONSTRUCTOR_P (w->fn)) 5745 source = TREE_TYPE (source); 5746 warning ("choosing `%D' over `%D'", w->fn, l->fn); 5747 warning (" for conversion from `%T' to `%T'", 5748 source, TREE_TYPE (w->second_conv)); 5749 warning (" because conversion sequence for the argument is better"); 5750 } 5751 else 5752 add_warning (w, l); 5753 } 5754 } 5755 5756 if (winner) 5757 return winner; 5758 5759 /* or, if not that, 5760 F1 is a non-template function and F2 is a template function 5761 specialization. */ 5762 5763 if (! cand1->template && cand2->template) 5764 return 1; 5765 else if (cand1->template && ! cand2->template) 5766 return -1; 5767 5768 /* or, if not that, 5769 F1 and F2 are template functions and the function template for F1 is 5770 more specialized than the template for F2 according to the partial 5771 ordering rules. */ 5772 5773 if (cand1->template && cand2->template) 5774 { 5775 winner = more_specialized 5776 (TI_TEMPLATE (cand1->template), TI_TEMPLATE (cand2->template), 5777 DEDUCE_ORDER, 5778 /* Tell the deduction code how many real function arguments 5779 we saw, not counting the implicit 'this' argument. But, 5780 add_function_candidate() suppresses the "this" argument 5781 for constructors. 5782 5783 [temp.func.order]: The presence of unused ellipsis and default 5784 arguments has no effect on the partial ordering of function 5785 templates. */ 5786 TREE_VEC_LENGTH (cand1->convs) 5787 - (DECL_NONSTATIC_MEMBER_FUNCTION_P (cand1->fn) 5788 - DECL_CONSTRUCTOR_P (cand1->fn))); 5789 /* HERE */ 5790 if (winner) 5791 return winner; 5792 } 5793 5794 /* or, if not that, 5795 the context is an initialization by user-defined conversion (see 5796 _dcl.init_ and _over.match.user_) and the standard conversion 5797 sequence from the return type of F1 to the destination type (i.e., 5798 the type of the entity being initialized) is a better conversion 5799 sequence than the standard conversion sequence from the return type 5800 of F2 to the destination type. */ 5801 5802 if (cand1->second_conv) 5803 { 5804 winner = compare_ics (cand1->second_conv, cand2->second_conv); 5805 if (winner) 5806 return winner; 5807 } 5808 5809 /* Check whether we can discard a builtin candidate, either because we 5810 have two identical ones or matching builtin and non-builtin candidates. 5811 5812 (Pedantically in the latter case the builtin which matched the user 5813 function should not be added to the overload set, but we spot it here. 5814 5815 [over.match.oper] 5816 ... the builtin candidates include ... 5817 - do not have the same parameter type list as any non-template 5818 non-member candidate. */ 5819 5820 if (TREE_CODE (cand1->fn) == IDENTIFIER_NODE 5821 || TREE_CODE (cand2->fn) == IDENTIFIER_NODE) 5822 { 5823 for (i = 0; i < len; ++i) 5824 if (!same_type_p (TREE_TYPE (TREE_VEC_ELT (cand1->convs, i)), 5825 TREE_TYPE (TREE_VEC_ELT (cand2->convs, i)))) 5826 break; 5827 if (i == TREE_VEC_LENGTH (cand1->convs)) 5828 { 5829 if (cand1->fn == cand2->fn) 5830 /* Two built-in candidates; arbitrarily pick one. */ 5831 return 1; 5832 else if (TREE_CODE (cand1->fn) == IDENTIFIER_NODE) 5833 /* cand1 is built-in; prefer cand2. */ 5834 return -1; 5835 else 5836 /* cand2 is built-in; prefer cand1. */ 5837 return 1; 5838 } 5839 } 5840 5841 /* If the two functions are the same (this can happen with declarations 5842 in multiple scopes and arg-dependent lookup), arbitrarily choose one. */ 5843 if (DECL_P (cand1->fn) && DECL_P (cand2->fn) 5844 && equal_functions (cand1->fn, cand2->fn)) 5845 return 1; 5846 5847tweak: 5848 5849 /* Extension: If the worst conversion for one candidate is worse than the 5850 worst conversion for the other, take the first. */ 5851 if (!pedantic) 5852 { 5853 int rank1 = IDENTITY_RANK, rank2 = IDENTITY_RANK; 5854 struct z_candidate *w = 0, *l = 0; 5855 5856 for (i = 0; i < len; ++i) 5857 { 5858 if (ICS_RANK (TREE_VEC_ELT (cand1->convs, i+off1)) > rank1) 5859 rank1 = ICS_RANK (TREE_VEC_ELT (cand1->convs, i+off1)); 5860 if (ICS_RANK (TREE_VEC_ELT (cand2->convs, i+off2)) > rank2) 5861 rank2 = ICS_RANK (TREE_VEC_ELT (cand2->convs, i+off2)); 5862 } 5863 if (rank1 < rank2) 5864 winner = 1, w = cand1, l = cand2; 5865 if (rank1 > rank2) 5866 winner = -1, w = cand2, l = cand1; 5867 if (winner) 5868 { 5869 if (warn) 5870 pedwarn ("ISO C++ says that `%D' and `%D' are ambiguous \ 5871even though the worst conversion for the former is better than the worst \ 5872conversion for the latter", w->fn, l->fn); 5873 else 5874 add_warning (w, l); 5875 return winner; 5876 } 5877 } 5878 5879 my_friendly_assert (!winner, 20010121); 5880 return 0; 5881} 5882 5883/* Given a list of candidates for overloading, find the best one, if any. 5884 This algorithm has a worst case of O(2n) (winner is last), and a best 5885 case of O(n/2) (totally ambiguous); much better than a sorting 5886 algorithm. */ 5887 5888static struct z_candidate * 5889tourney (candidates) 5890 struct z_candidate *candidates; 5891{ 5892 struct z_candidate *champ = candidates, *challenger; 5893 int fate; 5894 int champ_compared_to_predecessor = 0; 5895 5896 /* Walk through the list once, comparing each current champ to the next 5897 candidate, knocking out a candidate or two with each comparison. */ 5898 5899 for (challenger = champ->next; challenger; ) 5900 { 5901 fate = joust (champ, challenger, 0); 5902 if (fate == 1) 5903 challenger = challenger->next; 5904 else 5905 { 5906 if (fate == 0) 5907 { 5908 champ = challenger->next; 5909 if (champ == 0) 5910 return 0; 5911 champ_compared_to_predecessor = 0; 5912 } 5913 else 5914 { 5915 champ = challenger; 5916 champ_compared_to_predecessor = 1; 5917 } 5918 5919 challenger = champ->next; 5920 } 5921 } 5922 5923 /* Make sure the champ is better than all the candidates it hasn't yet 5924 been compared to. */ 5925 5926 for (challenger = candidates; 5927 challenger != champ 5928 && !(champ_compared_to_predecessor && challenger->next == champ); 5929 challenger = challenger->next) 5930 { 5931 fate = joust (champ, challenger, 0); 5932 if (fate != 1) 5933 return 0; 5934 } 5935 5936 return champ; 5937} 5938 5939/* Returns nonzero if things of type FROM can be converted to TO. */ 5940 5941int 5942can_convert (to, from) 5943 tree to, from; 5944{ 5945 return can_convert_arg (to, from, NULL_TREE); 5946} 5947 5948/* Returns nonzero if ARG (of type FROM) can be converted to TO. */ 5949 5950int 5951can_convert_arg (to, from, arg) 5952 tree to, from, arg; 5953{ 5954 tree t = implicit_conversion (to, from, arg, LOOKUP_NORMAL); 5955 return (t && ! ICS_BAD_FLAG (t)); 5956} 5957 5958/* Like can_convert_arg, but allows dubious conversions as well. */ 5959 5960int 5961can_convert_arg_bad (to, from, arg) 5962 tree to, from, arg; 5963{ 5964 tree t = implicit_conversion (to, from, arg, LOOKUP_NORMAL); 5965 return !!t; 5966} 5967 5968/* Convert EXPR to TYPE. Return the converted expression. 5969 5970 Note that we allow bad conversions here because by the time we get to 5971 this point we are committed to doing the conversion. If we end up 5972 doing a bad conversion, convert_like will complain. */ 5973 5974tree 5975perform_implicit_conversion (type, expr) 5976 tree type; 5977 tree expr; 5978{ 5979 tree conv; 5980 5981 if (expr == error_mark_node) 5982 return error_mark_node; 5983 conv = implicit_conversion (type, TREE_TYPE (expr), expr, 5984 LOOKUP_NORMAL); 5985 if (!conv) 5986 { 5987 error ("could not convert `%E' to `%T'", expr, type); 5988 return error_mark_node; 5989 } 5990 5991 return convert_like (conv, expr); 5992} 5993 5994/* Convert EXPR to TYPE (as a direct-initialization) if that is 5995 permitted. If the conversion is valid, the converted expression is 5996 returned. Otherwise, NULL_TREE is returned. */ 5997 5998tree 5999perform_direct_initialization_if_possible (tree type, tree expr) 6000{ 6001 tree conv; 6002 6003 if (type == error_mark_node || error_operand_p (expr)) 6004 return error_mark_node; 6005 conv = implicit_conversion (type, TREE_TYPE (expr), expr, 6006 LOOKUP_NORMAL); 6007 if (!conv || ICS_BAD_FLAG (conv)) 6008 return NULL_TREE; 6009 return convert_like_real (conv, expr, NULL_TREE, 0, 0, 6010 /*issue_conversion_warnings=*/false); 6011} 6012 6013/* DECL is a VAR_DECL whose type is a REFERENCE_TYPE. The reference 6014 is being bound to a temporary. Create and return a new VAR_DECL 6015 with the indicated TYPE; this variable will store the value to 6016 which the reference is bound. */ 6017 6018tree 6019make_temporary_var_for_ref_to_temp (tree decl, tree type) 6020{ 6021 tree var; 6022 6023 /* Create the variable. */ 6024 var = build_decl (VAR_DECL, NULL_TREE, type); 6025 DECL_ARTIFICIAL (var) = 1; 6026 TREE_USED (var) = 1; 6027 6028 /* Register the variable. */ 6029 if (TREE_STATIC (decl)) 6030 { 6031 /* Namespace-scope or local static; give it a mangled name. */ 6032 tree name; 6033 6034 TREE_STATIC (var) = 1; 6035 name = mangle_ref_init_variable (decl); 6036 DECL_NAME (var) = name; 6037 SET_DECL_ASSEMBLER_NAME (var, name); 6038 var = pushdecl_top_level (var); 6039 } 6040 else 6041 { 6042 /* Create a new cleanup level if necessary. */ 6043 maybe_push_cleanup_level (type); 6044 /* Don't push unnamed temps. Do set DECL_CONTEXT, though. */ 6045 DECL_CONTEXT (var) = current_function_decl; 6046 } 6047 6048 return var; 6049} 6050 6051/* Convert EXPR to the indicated reference TYPE, in a way suitable for 6052 initializing a variable of that TYPE. If DECL is non-NULL, it is 6053 the VAR_DECL being initialized with the EXPR. (In that case, the 6054 type of DECL will be TYPE.) 6055 6056 Return the converted expression. */ 6057 6058tree 6059initialize_reference (type, expr, decl) 6060 tree type; 6061 tree expr; 6062 tree decl; 6063{ 6064 tree conv; 6065 6066 if (type == error_mark_node || error_operand_p (expr)) 6067 return error_mark_node; 6068 6069 conv = reference_binding (type, TREE_TYPE (expr), expr, LOOKUP_NORMAL); 6070 if (!conv || ICS_BAD_FLAG (conv)) 6071 { 6072 error ("could not convert `%E' to `%T'", expr, type); 6073 return error_mark_node; 6074 } 6075 6076 /* If DECL is non-NULL, then this special rule applies: 6077 6078 [class.temporary] 6079 6080 The temporary to which the reference is bound or the temporary 6081 that is the complete object to which the reference is bound 6082 persists for the lifetime of the reference. 6083 6084 The temporaries created during the evaluation of the expression 6085 initializing the reference, except the temporary to which the 6086 reference is bound, are destroyed at the end of the 6087 full-expression in which they are created. 6088 6089 In that case, we store the converted expression into a new 6090 VAR_DECL in a new scope. 6091 6092 However, we want to be careful not to create temporaries when 6093 they are not required. For example, given: 6094 6095 struct B {}; 6096 struct D : public B {}; 6097 D f(); 6098 const B& b = f(); 6099 6100 there is no need to copy the return value from "f"; we can just 6101 extend its lifetime. Similarly, given: 6102 6103 struct S {}; 6104 struct T { operator S(); }; 6105 T t; 6106 const S& s = t; 6107 6108 we can extend the lifetime of the return value of the conversion 6109 operator. */ 6110 my_friendly_assert (TREE_CODE (conv) == REF_BIND, 20030302); 6111 if (decl) 6112 { 6113 tree var; 6114 tree base_conv_type; 6115 6116 /* Skip over the REF_BIND. */ 6117 conv = TREE_OPERAND (conv, 0); 6118 /* If the next conversion is a BASE_CONV, skip that too -- but 6119 remember that the conversion was required. */ 6120 if (TREE_CODE (conv) == BASE_CONV && !NEED_TEMPORARY_P (conv)) 6121 { 6122 base_conv_type = TREE_TYPE (conv); 6123 conv = TREE_OPERAND (conv, 0); 6124 } 6125 else 6126 base_conv_type = NULL_TREE; 6127 /* Perform the remainder of the conversion. */ 6128 expr = convert_like (conv, expr); 6129 if (!real_non_cast_lvalue_p (expr)) 6130 { 6131 tree init; 6132 tree type; 6133 6134 /* Create the temporary variable. */ 6135 type = TREE_TYPE (expr); 6136 var = make_temporary_var_for_ref_to_temp (decl, type); 6137 layout_decl (var, 0); 6138 if (at_function_scope_p ()) 6139 { 6140 tree cleanup; 6141 6142 add_decl_stmt (var); 6143 cleanup = cxx_maybe_build_cleanup (var); 6144 if (cleanup) 6145 finish_decl_cleanup (var, cleanup); 6146 } 6147 else 6148 { 6149 rest_of_decl_compilation (var, NULL, /*toplev=*/1, at_eof); 6150 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)) 6151 static_aggregates = tree_cons (NULL_TREE, var, 6152 static_aggregates); 6153 } 6154 init = build (INIT_EXPR, type, var, expr); 6155 /* Use its address to initialize the reference variable. */ 6156 expr = build_address (var); 6157 expr = build (COMPOUND_EXPR, TREE_TYPE (expr), init, expr); 6158 } 6159 else 6160 /* Take the address of EXPR. */ 6161 expr = build_unary_op (ADDR_EXPR, expr, 0); 6162 /* If a BASE_CONV was required, perform it now. */ 6163 if (base_conv_type) 6164 expr = (perform_implicit_conversion 6165 (build_pointer_type (base_conv_type), expr)); 6166 return build_nop (type, expr); 6167 } 6168 6169 /* Perform the conversion. */ 6170 return convert_like (conv, expr); 6171} 6172 6173#include "gt-cp-call.h" 6174