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