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