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