118334Speter/* Functions related to invoking methods and overloaded functions. 2169689Skan Copyright (C) 1987, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 3169689Skan 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006 Free Software Foundation, Inc. 418334Speter Contributed by Michael Tiemann (tiemann@cygnus.com) and 550397Sobrien modified by Brendan Kehoe (brendan@cygnus.com). 618334Speter 7132718SkanThis file is part of GCC. 818334Speter 9132718SkanGCC is free software; you can redistribute it and/or modify 1018334Speterit under the terms of the GNU General Public License as published by 1118334Speterthe Free Software Foundation; either version 2, or (at your option) 1218334Speterany later version. 1318334Speter 14132718SkanGCC is distributed in the hope that it will be useful, 1518334Speterbut WITHOUT ANY WARRANTY; without even the implied warranty of 1618334SpeterMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 1718334SpeterGNU General Public License for more details. 1818334Speter 1918334SpeterYou should have received a copy of the GNU General Public License 20132718Skanalong with GCC; see the file COPYING. If not, write to 21169689Skanthe Free Software Foundation, 51 Franklin Street, Fifth Floor, 22169689SkanBoston, MA 02110-1301, USA. */ 2318334Speter 2418334Speter 2550397Sobrien/* High-level class interface. */ 2618334Speter 2718334Speter#include "config.h" 2850397Sobrien#include "system.h" 29132718Skan#include "coretypes.h" 30132718Skan#include "tm.h" 3118334Speter#include "tree.h" 3218334Speter#include "cp-tree.h" 3318334Speter#include "output.h" 3418334Speter#include "flags.h" 3550397Sobrien#include "rtl.h" 3650397Sobrien#include "toplev.h" 3790075Sobrien#include "expr.h" 3890075Sobrien#include "diagnostic.h" 39132718Skan#include "intl.h" 40132718Skan#include "target.h" 41132718Skan#include "convert.h" 4218334Speter 43169689Skan/* The various kinds of conversion. */ 44169689Skan 45169689Skantypedef enum conversion_kind { 46169689Skan ck_identity, 47169689Skan ck_lvalue, 48169689Skan ck_qual, 49169689Skan ck_std, 50169689Skan ck_ptr, 51169689Skan ck_pmem, 52169689Skan ck_base, 53169689Skan ck_ref_bind, 54169689Skan ck_user, 55169689Skan ck_ambig, 56169689Skan ck_rvalue 57169689Skan} conversion_kind; 58169689Skan 59169689Skan/* The rank of the conversion. Order of the enumerals matters; better 60169689Skan conversions should come earlier in the list. */ 61169689Skan 62169689Skantypedef enum conversion_rank { 63169689Skan cr_identity, 64169689Skan cr_exact, 65169689Skan cr_promotion, 66169689Skan cr_std, 67169689Skan cr_pbool, 68169689Skan cr_user, 69169689Skan cr_ellipsis, 70169689Skan cr_bad 71169689Skan} conversion_rank; 72169689Skan 73169689Skan/* An implicit conversion sequence, in the sense of [over.best.ics]. 74169689Skan The first conversion to be performed is at the end of the chain. 75169689Skan That conversion is always a cr_identity conversion. */ 76169689Skan 77169689Skantypedef struct conversion conversion; 78169689Skanstruct conversion { 79169689Skan /* The kind of conversion represented by this step. */ 80169689Skan conversion_kind kind; 81169689Skan /* The rank of this conversion. */ 82169689Skan conversion_rank rank; 83169689Skan BOOL_BITFIELD user_conv_p : 1; 84169689Skan BOOL_BITFIELD ellipsis_p : 1; 85169689Skan BOOL_BITFIELD this_p : 1; 86169689Skan BOOL_BITFIELD bad_p : 1; 87169689Skan /* If KIND is ck_ref_bind ck_base_conv, true to indicate that a 88169689Skan temporary should be created to hold the result of the 89169689Skan conversion. */ 90169689Skan BOOL_BITFIELD need_temporary_p : 1; 91169689Skan /* If KIND is ck_identity or ck_base_conv, true to indicate that the 92169689Skan copy constructor must be accessible, even though it is not being 93169689Skan used. */ 94169689Skan BOOL_BITFIELD check_copy_constructor_p : 1; 95169689Skan /* If KIND is ck_ptr or ck_pmem, true to indicate that a conversion 96169689Skan from a pointer-to-derived to pointer-to-base is being performed. */ 97169689Skan BOOL_BITFIELD base_p : 1; 98169689Skan /* The type of the expression resulting from the conversion. */ 99169689Skan tree type; 100169689Skan union { 101169689Skan /* The next conversion in the chain. Since the conversions are 102169689Skan arranged from outermost to innermost, the NEXT conversion will 103169689Skan actually be performed before this conversion. This variant is 104169689Skan used only when KIND is neither ck_identity nor ck_ambig. */ 105169689Skan conversion *next; 106169689Skan /* The expression at the beginning of the conversion chain. This 107169689Skan variant is used only if KIND is ck_identity or ck_ambig. */ 108169689Skan tree expr; 109169689Skan } u; 110169689Skan /* The function candidate corresponding to this conversion 111169689Skan sequence. This field is only used if KIND is ck_user. */ 112169689Skan struct z_candidate *cand; 113169689Skan}; 114169689Skan 115169689Skan#define CONVERSION_RANK(NODE) \ 116169689Skan ((NODE)->bad_p ? cr_bad \ 117169689Skan : (NODE)->ellipsis_p ? cr_ellipsis \ 118169689Skan : (NODE)->user_conv_p ? cr_user \ 119169689Skan : (NODE)->rank) 120169689Skan 121169689Skanstatic struct obstack conversion_obstack; 122169689Skanstatic bool conversion_obstack_initialized; 123169689Skan 124132718Skanstatic struct z_candidate * tourney (struct z_candidate *); 125132718Skanstatic int equal_functions (tree, tree); 126132718Skanstatic int joust (struct z_candidate *, struct z_candidate *, bool); 127169689Skanstatic int compare_ics (conversion *, conversion *); 128132718Skanstatic tree build_over_call (struct z_candidate *, int); 129132718Skanstatic tree build_java_interface_fn_ref (tree, tree); 130117395Skan#define convert_like(CONV, EXPR) \ 131169689Skan convert_like_real ((CONV), (EXPR), NULL_TREE, 0, 0, \ 132169689Skan /*issue_conversion_warnings=*/true, \ 133169689Skan /*c_cast_p=*/false) 134117395Skan#define convert_like_with_context(CONV, EXPR, FN, ARGNO) \ 135169689Skan convert_like_real ((CONV), (EXPR), (FN), (ARGNO), 0, \ 136169689Skan /*issue_conversion_warnings=*/true, \ 137169689Skan /*c_cast_p=*/false) 138169689Skanstatic tree convert_like_real (conversion *, tree, tree, int, int, bool, 139169689Skan bool); 140132718Skanstatic void op_error (enum tree_code, enum tree_code, tree, tree, 141169689Skan tree, const char *); 142132718Skanstatic tree build_object_call (tree, tree); 143132718Skanstatic tree resolve_args (tree); 144132718Skanstatic struct z_candidate *build_user_type_conversion_1 (tree, tree, int); 145132718Skanstatic void print_z_candidate (const char *, struct z_candidate *); 146132718Skanstatic void print_z_candidates (struct z_candidate *); 147132718Skanstatic tree build_this (tree); 148132718Skanstatic struct z_candidate *splice_viable (struct z_candidate *, bool, bool *); 149132718Skanstatic bool any_strictly_viable (struct z_candidate *); 150132718Skanstatic struct z_candidate *add_template_candidate 151169689Skan (struct z_candidate **, tree, tree, tree, tree, tree, 152169689Skan tree, tree, int, unification_kind_t); 153132718Skanstatic struct z_candidate *add_template_candidate_real 154169689Skan (struct z_candidate **, tree, tree, tree, tree, tree, 155169689Skan tree, tree, int, tree, unification_kind_t); 156169689Skanstatic struct z_candidate *add_template_conv_candidate 157169689Skan (struct z_candidate **, tree, tree, tree, tree, tree, tree); 158117395Skanstatic void add_builtin_candidates 159132718Skan (struct z_candidate **, enum tree_code, enum tree_code, 160169689Skan tree, tree *, int); 161117395Skanstatic void add_builtin_candidate 162132718Skan (struct z_candidate **, enum tree_code, enum tree_code, 163169689Skan tree, tree, tree, tree *, tree *, int); 164132718Skanstatic bool is_complete (tree); 165169689Skanstatic void build_builtin_candidate 166132718Skan (struct z_candidate **, tree, tree, tree, tree *, tree *, 167169689Skan int); 168169689Skanstatic struct z_candidate *add_conv_candidate 169132718Skan (struct z_candidate **, tree, tree, tree, tree, tree); 170169689Skanstatic struct z_candidate *add_function_candidate 171117395Skan (struct z_candidate **, tree, tree, tree, tree, tree, int); 172169689Skanstatic conversion *implicit_conversion (tree, tree, tree, bool, int); 173169689Skanstatic conversion *standard_conversion (tree, tree, tree, bool, int); 174169689Skanstatic conversion *reference_binding (tree, tree, tree, bool, int); 175169689Skanstatic conversion *build_conv (conversion_kind, tree, conversion *); 176169689Skanstatic bool is_subseq (conversion *, conversion *); 177169689Skanstatic tree maybe_handle_ref_bind (conversion **); 178169689Skanstatic void maybe_handle_implicit_object (conversion **); 179169689Skanstatic struct z_candidate *add_candidate 180169689Skan (struct z_candidate **, tree, tree, size_t, 181169689Skan conversion **, tree, tree, int); 182169689Skanstatic tree source_type (conversion *); 183132718Skanstatic void add_warning (struct z_candidate *, struct z_candidate *); 184132718Skanstatic bool reference_related_p (tree, tree); 185132718Skanstatic bool reference_compatible_p (tree, tree); 186169689Skanstatic conversion *convert_class_to_reference (tree, tree, tree); 187169689Skanstatic conversion *direct_reference_binding (tree, conversion *); 188132718Skanstatic bool promoted_arithmetic_type_p (tree); 189169689Skanstatic conversion *conditional_conversion (tree, tree); 190132718Skanstatic char *name_as_c_string (tree, tree, bool *); 191132718Skanstatic tree call_builtin_trap (void); 192132718Skanstatic tree prep_operand (tree); 193132718Skanstatic void add_candidates (tree, tree, tree, bool, tree, tree, 194132718Skan int, struct z_candidate **); 195169689Skanstatic conversion *merge_conversion_sequences (conversion *, conversion *); 196132718Skanstatic bool magic_varargs_p (tree); 197169689Skantypedef void (*diagnostic_fn_t) (const char *, ...) ATTRIBUTE_GCC_CXXDIAG(1,2); 198169689Skanstatic tree build_temp (tree, tree, int, diagnostic_fn_t *); 199132718Skanstatic void check_constructor_callable (tree, tree); 20018334Speter 201169689Skan/* Returns nonzero iff the destructor name specified in NAME matches BASETYPE. 202169689Skan NAME can take many forms... */ 20318334Speter 204132718Skanbool 205132718Skancheck_dtor_name (tree basetype, tree name) 20650397Sobrien{ 20752284Sobrien /* Just accept something we've already complained about. */ 20852284Sobrien if (name == error_mark_node) 209132718Skan return true; 21052284Sobrien 21150397Sobrien if (TREE_CODE (name) == TYPE_DECL) 21250397Sobrien name = TREE_TYPE (name); 21390075Sobrien else if (TYPE_P (name)) 21450397Sobrien /* OK */; 21550397Sobrien else if (TREE_CODE (name) == IDENTIFIER_NODE) 21650397Sobrien { 21750397Sobrien if ((IS_AGGR_TYPE (basetype) && name == constructor_name (basetype)) 21850397Sobrien || (TREE_CODE (basetype) == ENUMERAL_TYPE 21950397Sobrien && name == TYPE_IDENTIFIER (basetype))) 220169689Skan return true; 22150397Sobrien else 22250397Sobrien name = get_type_value (name); 22350397Sobrien } 224169689Skan else 225169689Skan { 226169689Skan /* In the case of: 22790075Sobrien 228169689Skan template <class T> struct S { ~S(); }; 229169689Skan int i; 230169689Skan i.~S(); 231169689Skan 232169689Skan NAME will be a class template. */ 233169689Skan gcc_assert (DECL_CLASS_TEMPLATE_P (name)); 234169689Skan return false; 235169689Skan } 236169689Skan 237169689Skan if (!name) 238132718Skan return false; 239169689Skan return same_type_p (TYPE_MAIN_VARIANT (basetype), TYPE_MAIN_VARIANT (name)); 24050397Sobrien} 24150397Sobrien 24250397Sobrien/* We want the address of a function or method. We avoid creating a 24350397Sobrien pointer-to-member function. */ 24450397Sobrien 24550397Sobrientree 246132718Skanbuild_addr_func (tree function) 24718334Speter{ 24850397Sobrien tree type = TREE_TYPE (function); 24918334Speter 25050397Sobrien /* We have to do these by hand to avoid real pointer to member 25150397Sobrien functions. */ 25250397Sobrien if (TREE_CODE (type) == METHOD_TYPE) 25318334Speter { 254132718Skan if (TREE_CODE (function) == OFFSET_REF) 255132718Skan { 256132718Skan tree object = build_address (TREE_OPERAND (function, 0)); 257132718Skan return get_member_function_from_ptrfunc (&object, 258132718Skan TREE_OPERAND (function, 1)); 259132718Skan } 260132718Skan function = build_address (function); 26118334Speter } 26250397Sobrien else 263132718Skan function = decay_conversion (function); 26450397Sobrien 26550397Sobrien return function; 26618334Speter} 26718334Speter 26850397Sobrien/* Build a CALL_EXPR, we can handle FUNCTION_TYPEs, METHOD_TYPEs, or 26950397Sobrien POINTER_TYPE to those. Note, pointer to member function types 27050397Sobrien (TYPE_PTRMEMFUNC_P) must be handled by our callers. */ 27150397Sobrien 27250397Sobrientree 273132718Skanbuild_call (tree function, tree parms) 27418334Speter{ 27550397Sobrien int is_constructor = 0; 27690075Sobrien int nothrow; 27750397Sobrien tree tmp; 27850397Sobrien tree decl; 27990075Sobrien tree result_type; 280117395Skan tree fntype; 28118334Speter 28250397Sobrien function = build_addr_func (function); 28350397Sobrien 284169689Skan gcc_assert (TYPE_PTR_P (TREE_TYPE (function))); 285117395Skan fntype = TREE_TYPE (TREE_TYPE (function)); 286169689Skan gcc_assert (TREE_CODE (fntype) == FUNCTION_TYPE 287169689Skan || TREE_CODE (fntype) == METHOD_TYPE); 288117395Skan result_type = TREE_TYPE (fntype); 28990075Sobrien 29050397Sobrien if (TREE_CODE (function) == ADDR_EXPR 29150397Sobrien && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL) 292169689Skan { 293169689Skan decl = TREE_OPERAND (function, 0); 294169689Skan if (!TREE_USED (decl)) 295169689Skan { 296169689Skan /* We invoke build_call directly for several library 297169689Skan functions. These may have been declared normally if 298169689Skan we're building libgcc, so we can't just check 299169689Skan DECL_ARTIFICIAL. */ 300169689Skan gcc_assert (DECL_ARTIFICIAL (decl) 301169689Skan || !strncmp (IDENTIFIER_POINTER (DECL_NAME (decl)), 302169689Skan "__", 2)); 303169689Skan mark_used (decl); 304169689Skan } 305169689Skan } 30650397Sobrien else 30750397Sobrien decl = NULL_TREE; 30850397Sobrien 30990075Sobrien /* We check both the decl and the type; a function may be known not to 31090075Sobrien throw without being declared throw(). */ 31190075Sobrien nothrow = ((decl && TREE_NOTHROW (decl)) 31290075Sobrien || TYPE_NOTHROW_P (TREE_TYPE (TREE_TYPE (function)))); 31390075Sobrien 314117395Skan if (decl && TREE_THIS_VOLATILE (decl) && cfun) 31596263Sobrien current_function_returns_abnormally = 1; 31696263Sobrien 31790075Sobrien if (decl && TREE_DEPRECATED (decl)) 31890075Sobrien warn_deprecated_use (decl); 319117395Skan require_complete_eh_spec_types (fntype, decl); 32090075Sobrien 32150397Sobrien if (decl && DECL_CONSTRUCTOR_P (decl)) 32250397Sobrien is_constructor = 1; 32350397Sobrien 32450397Sobrien /* Don't pass empty class objects by value. This is useful 32550397Sobrien for tags in STL, which are used to control overload resolution. 32650397Sobrien We don't need to handle other cases of copying empty classes. */ 32750397Sobrien if (! decl || ! DECL_BUILT_IN (decl)) 32850397Sobrien for (tmp = parms; tmp; tmp = TREE_CHAIN (tmp)) 32950397Sobrien if (is_empty_class (TREE_TYPE (TREE_VALUE (tmp))) 33050397Sobrien && ! TREE_ADDRESSABLE (TREE_TYPE (TREE_VALUE (tmp)))) 33150397Sobrien { 332169689Skan tree t = build0 (EMPTY_CLASS_EXPR, TREE_TYPE (TREE_VALUE (tmp))); 333169689Skan TREE_VALUE (tmp) = build2 (COMPOUND_EXPR, TREE_TYPE (t), 334169689Skan TREE_VALUE (tmp), t); 33550397Sobrien } 33650397Sobrien 337169689Skan function = build3 (CALL_EXPR, result_type, function, parms, NULL_TREE); 33850397Sobrien TREE_HAS_CONSTRUCTOR (function) = is_constructor; 33990075Sobrien TREE_NOTHROW (function) = nothrow; 340169689Skan 34150397Sobrien return function; 34218334Speter} 34318334Speter 34418334Speter/* Build something of the form ptr->method (args) 34518334Speter or object.method (args). This can also build 34618334Speter calls to constructors, and find friends. 34718334Speter 34818334Speter Member functions always take their class variable 34918334Speter as a pointer. 35018334Speter 35118334Speter INSTANCE is a class instance. 35218334Speter 35318334Speter NAME is the name of the method desired, usually an IDENTIFIER_NODE. 35418334Speter 35518334Speter PARMS help to figure out what that NAME really refers to. 35618334Speter 35718334Speter BASETYPE_PATH, if non-NULL, contains a chain from the type of INSTANCE 35818334Speter down to the real instance type to use for access checking. We need this 359132718Skan information to get protected accesses correct. 36018334Speter 36118334Speter FLAGS is the logical disjunction of zero or more LOOKUP_ 36218334Speter flags. See cp-tree.h for more info. 36318334Speter 36418334Speter If this is all OK, calls build_function_call with the resolved 36518334Speter member function. 36618334Speter 36718334Speter This function must also handle being called to perform 36818334Speter initialization, promotion/coercion of arguments, and 36918334Speter instantiation of default parameters. 37018334Speter 37118334Speter Note that NAME may refer to an instance variable name. If 37218334Speter `operator()()' is defined for the type of that field, then we return 37318334Speter that result. */ 37450397Sobrien 37550397Sobrien/* New overloading code. */ 37618334Speter 377169689Skantypedef struct z_candidate z_candidate; 378169689Skan 379169689Skantypedef struct candidate_warning candidate_warning; 380169689Skanstruct candidate_warning { 381169689Skan z_candidate *loser; 382169689Skan candidate_warning *next; 383169689Skan}; 384169689Skan 385169689Skanstruct z_candidate { 386117395Skan /* The FUNCTION_DECL that will be called if this candidate is 387117395Skan selected by overload resolution. */ 38850397Sobrien tree fn; 389132718Skan /* The arguments to use when calling this function. */ 390132718Skan tree args; 391132718Skan /* The implicit conversion sequences for each of the arguments to 392132718Skan FN. */ 393169689Skan conversion **convs; 394169689Skan /* The number of implicit conversion sequences. */ 395169689Skan size_t num_convs; 396132718Skan /* If FN is a user-defined conversion, the standard conversion 397132718Skan sequence from the type returned by FN to the desired destination 398132718Skan type. */ 399169689Skan conversion *second_conv; 40050397Sobrien int viable; 401117395Skan /* If FN is a member function, the binfo indicating the path used to 402117395Skan qualify the name of FN at the call site. This path is used to 403117395Skan determine whether or not FN is accessible if it is selected by 404117395Skan overload resolution. The DECL_CONTEXT of FN will always be a 405117395Skan (possibly improper) base of this binfo. */ 406117395Skan tree access_path; 407117395Skan /* If FN is a non-static member function, the binfo indicating the 408117395Skan subobject to which the `this' pointer should be converted if FN 409117395Skan is selected by overload resolution. The type pointed to the by 410117395Skan the `this' pointer must correspond to the most derived class 411117395Skan indicated by the CONVERSION_PATH. */ 412117395Skan tree conversion_path; 413169689Skan tree template_decl; 414169689Skan candidate_warning *warnings; 415169689Skan z_candidate *next; 41650397Sobrien}; 41750397Sobrien 418146895Skan/* Returns true iff T is a null pointer constant in the sense of 419146895Skan [conv.ptr]. */ 420146895Skan 421132718Skanbool 422132718Skannull_ptr_cst_p (tree t) 42350397Sobrien{ 42490075Sobrien /* [conv.ptr] 42590075Sobrien 42690075Sobrien A null pointer constant is an integral constant expression 42790075Sobrien (_expr.const_) rvalue of integer type that evaluates to zero. */ 428169689Skan t = integral_constant_value (t); 429169689Skan if (t == null_node) 430132718Skan return true; 431169689Skan if (CP_INTEGRAL_TYPE_P (TREE_TYPE (t)) && integer_zerop (t)) 432169689Skan { 433169689Skan STRIP_NOPS (t); 434169689Skan if (!TREE_CONSTANT_OVERFLOW (t)) 435169689Skan return true; 436169689Skan } 437132718Skan return false; 43850397Sobrien} 43950397Sobrien 440117395Skan/* Returns nonzero if PARMLIST consists of only default parms and/or 441117395Skan ellipsis. */ 44290075Sobrien 443132718Skanbool 444132718Skansufficient_parms_p (tree parmlist) 44590075Sobrien{ 44690075Sobrien for (; parmlist && parmlist != void_list_node; 44790075Sobrien parmlist = TREE_CHAIN (parmlist)) 44890075Sobrien if (!TREE_PURPOSE (parmlist)) 449132718Skan return false; 450132718Skan return true; 45190075Sobrien} 45290075Sobrien 453169689Skan/* Allocate N bytes of memory from the conversion obstack. The memory 454169689Skan is zeroed before being returned. */ 455169689Skan 456169689Skanstatic void * 457169689Skanconversion_obstack_alloc (size_t n) 45850397Sobrien{ 459169689Skan void *p; 460169689Skan if (!conversion_obstack_initialized) 461169689Skan { 462169689Skan gcc_obstack_init (&conversion_obstack); 463169689Skan conversion_obstack_initialized = true; 464169689Skan } 465169689Skan p = obstack_alloc (&conversion_obstack, n); 466169689Skan memset (p, 0, n); 467169689Skan return p; 468169689Skan} 46990075Sobrien 470169689Skan/* Dynamically allocate a conversion. */ 471169689Skan 472169689Skanstatic conversion * 473169689Skanalloc_conversion (conversion_kind kind) 474169689Skan{ 475169689Skan conversion *c; 476169689Skan c = (conversion *) conversion_obstack_alloc (sizeof (conversion)); 477169689Skan c->kind = kind; 478169689Skan return c; 479169689Skan} 480169689Skan 481169689Skan#ifdef ENABLE_CHECKING 482169689Skan 483169689Skan/* Make sure that all memory on the conversion obstack has been 484169689Skan freed. */ 485169689Skan 486169689Skanvoid 487169689Skanvalidate_conversion_obstack (void) 488169689Skan{ 489169689Skan if (conversion_obstack_initialized) 490169689Skan gcc_assert ((obstack_next_free (&conversion_obstack) 491169689Skan == obstack_base (&conversion_obstack))); 492169689Skan} 493169689Skan 494169689Skan#endif /* ENABLE_CHECKING */ 495169689Skan 496169689Skan/* Dynamically allocate an array of N conversions. */ 497169689Skan 498169689Skanstatic conversion ** 499169689Skanalloc_conversions (size_t n) 500169689Skan{ 501169689Skan return (conversion **) conversion_obstack_alloc (n * sizeof (conversion *)); 502169689Skan} 503169689Skan 504169689Skanstatic conversion * 505169689Skanbuild_conv (conversion_kind code, tree type, conversion *from) 506169689Skan{ 507169689Skan conversion *t; 508169689Skan conversion_rank rank = CONVERSION_RANK (from); 509169689Skan 51090075Sobrien /* We can't use buildl1 here because CODE could be USER_CONV, which 51190075Sobrien takes two arguments. In that case, the caller is responsible for 51290075Sobrien filling in the second argument. */ 513169689Skan t = alloc_conversion (code); 514169689Skan t->type = type; 515169689Skan t->u.next = from; 51690075Sobrien 51750397Sobrien switch (code) 51818334Speter { 519169689Skan case ck_ptr: 520169689Skan case ck_pmem: 521169689Skan case ck_base: 522169689Skan case ck_std: 523169689Skan if (rank < cr_std) 524169689Skan rank = cr_std; 52550397Sobrien break; 52650397Sobrien 527169689Skan case ck_qual: 528169689Skan if (rank < cr_exact) 529169689Skan rank = cr_exact; 530169689Skan break; 53150397Sobrien 53250397Sobrien default: 53350397Sobrien break; 53450397Sobrien } 535169689Skan t->rank = rank; 536169689Skan t->user_conv_p = (code == ck_user || from->user_conv_p); 537169689Skan t->bad_p = from->bad_p; 538169689Skan t->base_p = false; 53950397Sobrien return t; 54050397Sobrien} 54150397Sobrien 542169689Skan/* Build a representation of the identity conversion from EXPR to 543169689Skan itself. The TYPE should match the type of EXPR, if EXPR is non-NULL. */ 544169689Skan 545169689Skanstatic conversion * 546169689Skanbuild_identity_conv (tree type, tree expr) 547169689Skan{ 548169689Skan conversion *c; 549169689Skan 550169689Skan c = alloc_conversion (ck_identity); 551169689Skan c->type = type; 552169689Skan c->u.expr = expr; 553169689Skan 554169689Skan return c; 555169689Skan} 556169689Skan 557169689Skan/* Converting from EXPR to TYPE was ambiguous in the sense that there 558169689Skan were multiple user-defined conversions to accomplish the job. 559169689Skan Build a conversion that indicates that ambiguity. */ 560169689Skan 561169689Skanstatic conversion * 562169689Skanbuild_ambiguous_conv (tree type, tree expr) 563169689Skan{ 564169689Skan conversion *c; 565169689Skan 566169689Skan c = alloc_conversion (ck_ambig); 567169689Skan c->type = type; 568169689Skan c->u.expr = expr; 569169689Skan 570169689Skan return c; 571169689Skan} 572169689Skan 57390075Sobrientree 574132718Skanstrip_top_quals (tree t) 57550397Sobrien{ 57650397Sobrien if (TREE_CODE (t) == ARRAY_TYPE) 57750397Sobrien return t; 578132718Skan return cp_build_qualified_type (t, 0); 57950397Sobrien} 58050397Sobrien 58150397Sobrien/* Returns the standard conversion path (see [conv]) from type FROM to type 58250397Sobrien TO, if any. For proper handling of null pointer constants, you must 583169689Skan also pass the expression EXPR to convert from. If C_CAST_P is true, 584169689Skan this conversion is coming from a C-style cast. */ 58550397Sobrien 586169689Skanstatic conversion * 587169689Skanstandard_conversion (tree to, tree from, tree expr, bool c_cast_p, 588169689Skan int flags) 58950397Sobrien{ 59050397Sobrien enum tree_code fcode, tcode; 591169689Skan conversion *conv; 592132718Skan bool fromref = false; 59350397Sobrien 594132718Skan to = non_reference (to); 59550397Sobrien if (TREE_CODE (from) == REFERENCE_TYPE) 59650397Sobrien { 597132718Skan fromref = true; 59850397Sobrien from = TREE_TYPE (from); 59950397Sobrien } 60050397Sobrien to = strip_top_quals (to); 60150397Sobrien from = strip_top_quals (from); 60250397Sobrien 60352284Sobrien if ((TYPE_PTRFN_P (to) || TYPE_PTRMEMFUNC_P (to)) 60452284Sobrien && expr && type_unknown_p (expr)) 60552284Sobrien { 606122180Skan expr = instantiate_type (to, expr, tf_conv); 60752284Sobrien if (expr == error_mark_node) 608169689Skan return NULL; 60952284Sobrien from = TREE_TYPE (expr); 61052284Sobrien } 61152284Sobrien 61250397Sobrien fcode = TREE_CODE (from); 61350397Sobrien tcode = TREE_CODE (to); 61450397Sobrien 615169689Skan conv = build_identity_conv (from, expr); 616169689Skan if (fcode == FUNCTION_TYPE || fcode == ARRAY_TYPE) 61750397Sobrien { 618169689Skan from = type_decays_to (from); 61950397Sobrien fcode = TREE_CODE (from); 620169689Skan conv = build_conv (ck_lvalue, from, conv); 62150397Sobrien } 622169689Skan else if (fromref || (expr && lvalue_p (expr))) 62350397Sobrien { 624169689Skan if (expr) 625169689Skan { 626169689Skan tree bitfield_type; 627169689Skan bitfield_type = is_bitfield_expr_with_lowered_type (expr); 628169689Skan if (bitfield_type) 629169689Skan { 630169689Skan from = strip_top_quals (bitfield_type); 631169689Skan fcode = TREE_CODE (from); 632169689Skan } 633169689Skan } 634169689Skan conv = build_conv (ck_rvalue, from, conv); 63550397Sobrien } 63650397Sobrien 637132718Skan /* Allow conversion between `__complex__' data types. */ 63890075Sobrien if (tcode == COMPLEX_TYPE && fcode == COMPLEX_TYPE) 63990075Sobrien { 64090075Sobrien /* The standard conversion sequence to convert FROM to TO is 641169689Skan the standard conversion sequence to perform componentwise 642169689Skan conversion. */ 643169689Skan conversion *part_conv = standard_conversion 644169689Skan (TREE_TYPE (to), TREE_TYPE (from), NULL_TREE, c_cast_p, flags); 645169689Skan 64690075Sobrien if (part_conv) 647169689Skan { 648169689Skan conv = build_conv (part_conv->kind, to, conv); 649169689Skan conv->rank = part_conv->rank; 650169689Skan } 65190075Sobrien else 652169689Skan conv = NULL; 65390075Sobrien 65490075Sobrien return conv; 65590075Sobrien } 65690075Sobrien 65790075Sobrien if (same_type_p (from, to)) 65850397Sobrien return conv; 65950397Sobrien 660132718Skan if ((tcode == POINTER_TYPE || TYPE_PTR_TO_MEMBER_P (to)) 66150397Sobrien && expr && null_ptr_cst_p (expr)) 662169689Skan conv = build_conv (ck_std, to, conv); 66390075Sobrien else if ((tcode == INTEGER_TYPE && fcode == POINTER_TYPE) 66490075Sobrien || (tcode == POINTER_TYPE && fcode == INTEGER_TYPE)) 66590075Sobrien { 66690075Sobrien /* For backwards brain damage compatibility, allow interconversion of 66790075Sobrien pointers and integers with a pedwarn. */ 668169689Skan conv = build_conv (ck_std, to, conv); 669169689Skan conv->bad_p = true; 67090075Sobrien } 671132718Skan else if (tcode == ENUMERAL_TYPE && fcode == INTEGER_TYPE) 67290075Sobrien { 67390075Sobrien /* For backwards brain damage compatibility, allow interconversion of 67490075Sobrien enums and integers with a pedwarn. */ 675169689Skan conv = build_conv (ck_std, to, conv); 676169689Skan conv->bad_p = true; 67790075Sobrien } 678132718Skan else if ((tcode == POINTER_TYPE && fcode == POINTER_TYPE) 679132718Skan || (TYPE_PTRMEM_P (to) && TYPE_PTRMEM_P (from))) 68050397Sobrien { 681132718Skan tree to_pointee; 682132718Skan tree from_pointee; 68350397Sobrien 684132718Skan if (tcode == POINTER_TYPE 685132718Skan && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (from), 686132718Skan TREE_TYPE (to))) 68750397Sobrien ; 688132718Skan else if (VOID_TYPE_P (TREE_TYPE (to)) 689132718Skan && !TYPE_PTRMEM_P (from) 690132718Skan && TREE_CODE (TREE_TYPE (from)) != FUNCTION_TYPE) 69118334Speter { 69250397Sobrien from = build_pointer_type 693169689Skan (cp_build_qualified_type (void_type_node, 69490075Sobrien cp_type_quals (TREE_TYPE (from)))); 695169689Skan conv = build_conv (ck_ptr, from, conv); 69618334Speter } 697132718Skan else if (TYPE_PTRMEM_P (from)) 69818334Speter { 699132718Skan tree fbase = TYPE_PTRMEM_CLASS_TYPE (from); 700132718Skan tree tbase = TYPE_PTRMEM_CLASS_TYPE (to); 70150397Sobrien 70296263Sobrien if (DERIVED_FROM_P (fbase, tbase) 70390075Sobrien && (same_type_ignoring_top_level_qualifiers_p 704132718Skan (TYPE_PTRMEM_POINTED_TO_TYPE (from), 705132718Skan TYPE_PTRMEM_POINTED_TO_TYPE (to)))) 70650397Sobrien { 707169689Skan from = build_ptrmem_type (tbase, 708132718Skan TYPE_PTRMEM_POINTED_TO_TYPE (from)); 709169689Skan conv = build_conv (ck_pmem, from, conv); 71050397Sobrien } 711146895Skan else if (!same_type_p (fbase, tbase)) 712146895Skan return NULL; 71318334Speter } 71450397Sobrien else if (IS_AGGR_TYPE (TREE_TYPE (from)) 715122180Skan && IS_AGGR_TYPE (TREE_TYPE (to)) 716122180Skan /* [conv.ptr] 717169689Skan 718169689Skan An rvalue of type "pointer to cv D," where D is a 719122180Skan class type, can be converted to an rvalue of type 720122180Skan "pointer to cv B," where B is a base class (clause 721122180Skan _class.derived_) of D. If B is an inaccessible 722122180Skan (clause _class.access_) or ambiguous 723122180Skan (_class.member.lookup_) base class of D, a program 724169689Skan that necessitates this conversion is ill-formed. 725169689Skan Therefore, we use DERIVED_FROM_P, and do not check 726169689Skan access or uniqueness. */ 727169689Skan && DERIVED_FROM_P (TREE_TYPE (to), TREE_TYPE (from)) 728169689Skan /* If FROM is not yet complete, then we must be parsing 729169689Skan the body of a class. We know what's derived from 730169689Skan what, but we can't actually perform a 731169689Skan derived-to-base conversion. For example, in: 732169689Skan 733169689Skan struct D : public B { 734169689Skan static const int i = sizeof((B*)(D*)0); 735169689Skan }; 736169689Skan 737169689Skan the D*-to-B* conversion is a reinterpret_cast, not a 738169689Skan static_cast. */ 739169689Skan && COMPLETE_TYPE_P (TREE_TYPE (from))) 74050397Sobrien { 741169689Skan from = 742122180Skan cp_build_qualified_type (TREE_TYPE (to), 743122180Skan cp_type_quals (TREE_TYPE (from))); 744122180Skan from = build_pointer_type (from); 745169689Skan conv = build_conv (ck_ptr, from, conv); 746169689Skan conv->base_p = true; 74750397Sobrien } 74818334Speter 749132718Skan if (tcode == POINTER_TYPE) 750132718Skan { 751132718Skan to_pointee = TREE_TYPE (to); 752132718Skan from_pointee = TREE_TYPE (from); 753132718Skan } 754132718Skan else 755132718Skan { 756132718Skan to_pointee = TYPE_PTRMEM_POINTED_TO_TYPE (to); 757132718Skan from_pointee = TYPE_PTRMEM_POINTED_TO_TYPE (from); 758132718Skan } 759132718Skan 76052284Sobrien if (same_type_p (from, to)) 76150397Sobrien /* OK */; 762169689Skan else if (c_cast_p && comp_ptr_ttypes_const (to, from)) 763169689Skan /* In a C-style cast, we ignore CV-qualification because we 764169689Skan are allowed to perform a static_cast followed by a 765169689Skan const_cast. */ 766169689Skan conv = build_conv (ck_qual, to, conv); 767169689Skan else if (!c_cast_p && comp_ptr_ttypes (to_pointee, from_pointee)) 768169689Skan conv = build_conv (ck_qual, to, conv); 76952284Sobrien else if (expr && string_conv_p (to, expr, 0)) 77052284Sobrien /* converting from string constant to char *. */ 771169689Skan conv = build_conv (ck_qual, to, conv); 772132718Skan else if (ptr_reasonably_similar (to_pointee, from_pointee)) 77318334Speter { 774169689Skan conv = build_conv (ck_ptr, to, conv); 775169689Skan conv->bad_p = true; 77618334Speter } 77718334Speter else 778169689Skan return NULL; 77950397Sobrien 78050397Sobrien from = to; 78150397Sobrien } 78250397Sobrien else if (TYPE_PTRMEMFUNC_P (to) && TYPE_PTRMEMFUNC_P (from)) 78350397Sobrien { 78450397Sobrien tree fromfn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (from)); 78550397Sobrien tree tofn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (to)); 78650397Sobrien tree fbase = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (fromfn))); 78750397Sobrien tree tbase = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (tofn))); 78850397Sobrien 78996263Sobrien if (!DERIVED_FROM_P (fbase, tbase) 79090075Sobrien || !same_type_p (TREE_TYPE (fromfn), TREE_TYPE (tofn)) 79190075Sobrien || !compparms (TREE_CHAIN (TYPE_ARG_TYPES (fromfn)), 79290075Sobrien TREE_CHAIN (TYPE_ARG_TYPES (tofn))) 79390075Sobrien || cp_type_quals (fbase) != cp_type_quals (tbase)) 794169689Skan return NULL; 79550397Sobrien 79690075Sobrien from = cp_build_qualified_type (tbase, cp_type_quals (fbase)); 797169689Skan from = build_method_type_directly (from, 798132718Skan TREE_TYPE (fromfn), 799132718Skan TREE_CHAIN (TYPE_ARG_TYPES (fromfn))); 80050397Sobrien from = build_ptrmemfunc_type (build_pointer_type (from)); 801169689Skan conv = build_conv (ck_pmem, from, conv); 802169689Skan conv->base_p = true; 80350397Sobrien } 80450397Sobrien else if (tcode == BOOLEAN_TYPE) 80550397Sobrien { 806132718Skan /* [conv.bool] 80750397Sobrien 808169689Skan An rvalue of arithmetic, enumeration, pointer, or pointer to 809132718Skan member type can be converted to an rvalue of type bool. */ 810132718Skan if (ARITHMETIC_TYPE_P (from) 811132718Skan || fcode == ENUMERAL_TYPE 812132718Skan || fcode == POINTER_TYPE 813132718Skan || TYPE_PTR_TO_MEMBER_P (from)) 814132718Skan { 815169689Skan conv = build_conv (ck_std, to, conv); 816132718Skan if (fcode == POINTER_TYPE 817132718Skan || TYPE_PTRMEM_P (from) 818169689Skan || (TYPE_PTRMEMFUNC_P (from) 819169689Skan && conv->rank < cr_pbool)) 820169689Skan conv->rank = cr_pbool; 821132718Skan return conv; 822132718Skan } 823169689Skan 824169689Skan return NULL; 82550397Sobrien } 82650397Sobrien /* We don't check for ENUMERAL_TYPE here because there are no standard 82750397Sobrien conversions to enum type. */ 82850397Sobrien else if (tcode == INTEGER_TYPE || tcode == BOOLEAN_TYPE 82950397Sobrien || tcode == REAL_TYPE) 83050397Sobrien { 83150397Sobrien if (! (INTEGRAL_CODE_P (fcode) || fcode == REAL_TYPE)) 832169689Skan return NULL; 833169689Skan conv = build_conv (ck_std, to, conv); 83450397Sobrien 83550397Sobrien /* Give this a better rank if it's a promotion. */ 836132718Skan if (same_type_p (to, type_promotes_to (from)) 837169689Skan && conv->u.next->rank <= cr_promotion) 838169689Skan conv->rank = cr_promotion; 83950397Sobrien } 840132718Skan else if (fcode == VECTOR_TYPE && tcode == VECTOR_TYPE 841260075Spfg && vector_types_convertible_p (from, to, false)) 842169689Skan return build_conv (ck_std, to, conv); 843146895Skan else if (!(flags & LOOKUP_CONSTRUCTOR_CALLABLE) 844146895Skan && IS_AGGR_TYPE (to) && IS_AGGR_TYPE (from) 84590075Sobrien && is_properly_derived_from (from, to)) 84650397Sobrien { 847169689Skan if (conv->kind == ck_rvalue) 848169689Skan conv = conv->u.next; 849169689Skan conv = build_conv (ck_base, to, conv); 85090075Sobrien /* The derived-to-base conversion indicates the initialization 85190075Sobrien of a parameter with base type from an object of a derived 85290075Sobrien type. A temporary object is created to hold the result of 85390075Sobrien the conversion. */ 854169689Skan conv->need_temporary_p = true; 85550397Sobrien } 85650397Sobrien else 857169689Skan return NULL; 85850397Sobrien 85950397Sobrien return conv; 86050397Sobrien} 86150397Sobrien 862117395Skan/* Returns nonzero if T1 is reference-related to T2. */ 86390075Sobrien 864132718Skanstatic bool 865132718Skanreference_related_p (tree t1, tree t2) 86690075Sobrien{ 86790075Sobrien t1 = TYPE_MAIN_VARIANT (t1); 86890075Sobrien t2 = TYPE_MAIN_VARIANT (t2); 86990075Sobrien 87090075Sobrien /* [dcl.init.ref] 87190075Sobrien 87290075Sobrien Given types "cv1 T1" and "cv2 T2," "cv1 T1" is reference-related 87390075Sobrien to "cv2 T2" if T1 is the same type as T2, or T1 is a base class 87490075Sobrien of T2. */ 87590075Sobrien return (same_type_p (t1, t2) 87690075Sobrien || (CLASS_TYPE_P (t1) && CLASS_TYPE_P (t2) 87790075Sobrien && DERIVED_FROM_P (t1, t2))); 87890075Sobrien} 87990075Sobrien 880117395Skan/* Returns nonzero if T1 is reference-compatible with T2. */ 88190075Sobrien 882132718Skanstatic bool 883132718Skanreference_compatible_p (tree t1, tree t2) 88490075Sobrien{ 88590075Sobrien /* [dcl.init.ref] 88690075Sobrien 88790075Sobrien "cv1 T1" is reference compatible with "cv2 T2" if T1 is 88890075Sobrien reference-related to T2 and cv1 is the same cv-qualification as, 88990075Sobrien or greater cv-qualification than, cv2. */ 89090075Sobrien return (reference_related_p (t1, t2) 89190075Sobrien && at_least_as_qualified_p (t1, t2)); 89290075Sobrien} 89390075Sobrien 89490075Sobrien/* Determine whether or not the EXPR (of class type S) can be 89590075Sobrien converted to T as in [over.match.ref]. */ 89690075Sobrien 897169689Skanstatic conversion * 898132718Skanconvert_class_to_reference (tree t, tree s, tree expr) 89990075Sobrien{ 90090075Sobrien tree conversions; 90190075Sobrien tree arglist; 902169689Skan conversion *conv; 903117395Skan tree reference_type; 90490075Sobrien struct z_candidate *candidates; 90590075Sobrien struct z_candidate *cand; 906132718Skan bool any_viable_p; 90790075Sobrien 908117395Skan conversions = lookup_conversions (s); 909117395Skan if (!conversions) 910169689Skan return NULL; 911117395Skan 91290075Sobrien /* [over.match.ref] 91390075Sobrien 91490075Sobrien Assuming that "cv1 T" is the underlying type of the reference 91590075Sobrien being initialized, and "cv S" is the type of the initializer 91690075Sobrien expression, with S a class type, the candidate functions are 91790075Sobrien selected as follows: 91890075Sobrien 91990075Sobrien --The conversion functions of S and its base classes are 92090075Sobrien considered. Those that are not hidden within S and yield type 92190075Sobrien "reference to cv2 T2", where "cv1 T" is reference-compatible 92290075Sobrien (_dcl.init.ref_) with "cv2 T2", are candidate functions. 92390075Sobrien 92490075Sobrien The argument list has one argument, which is the initializer 92590075Sobrien expression. */ 92690075Sobrien 92790075Sobrien candidates = 0; 92890075Sobrien 92990075Sobrien /* Conceptually, we should take the address of EXPR and put it in 93090075Sobrien the argument list. Unfortunately, however, that can result in 93190075Sobrien error messages, which we should not issue now because we are just 93290075Sobrien trying to find a conversion operator. Therefore, we use NULL, 93390075Sobrien cast to the appropriate type. */ 934169689Skan arglist = build_int_cst (build_pointer_type (s), 0); 93590075Sobrien arglist = build_tree_list (NULL_TREE, arglist); 936117395Skan 937117395Skan reference_type = build_reference_type (t); 938117395Skan 939117395Skan while (conversions) 94090075Sobrien { 94190075Sobrien tree fns = TREE_VALUE (conversions); 94290075Sobrien 94390075Sobrien for (; fns; fns = OVL_NEXT (fns)) 94490075Sobrien { 94590075Sobrien tree f = OVL_CURRENT (fns); 94690075Sobrien tree t2 = TREE_TYPE (TREE_TYPE (f)); 947169689Skan 948117395Skan cand = NULL; 94990075Sobrien 95090075Sobrien /* If this is a template function, try to get an exact 951169689Skan match. */ 95290075Sobrien if (TREE_CODE (f) == TEMPLATE_DECL) 95390075Sobrien { 954117395Skan cand = add_template_candidate (&candidates, 955117395Skan f, s, 956117395Skan NULL_TREE, 957117395Skan arglist, 958117395Skan reference_type, 959117395Skan TYPE_BINFO (s), 960117395Skan TREE_PURPOSE (conversions), 961117395Skan LOOKUP_NORMAL, 962117395Skan DEDUCE_CONV); 963169689Skan 964117395Skan if (cand) 96590075Sobrien { 96690075Sobrien /* Now, see if the conversion function really returns 96790075Sobrien an lvalue of the appropriate type. From the 96890075Sobrien point of view of unification, simply returning an 96990075Sobrien rvalue of the right type is good enough. */ 970117395Skan f = cand->fn; 97190075Sobrien t2 = TREE_TYPE (TREE_TYPE (f)); 97290075Sobrien if (TREE_CODE (t2) != REFERENCE_TYPE 97390075Sobrien || !reference_compatible_p (t, TREE_TYPE (t2))) 974117395Skan { 975117395Skan candidates = candidates->next; 976117395Skan cand = NULL; 977117395Skan } 97890075Sobrien } 97990075Sobrien } 98090075Sobrien else if (TREE_CODE (t2) == REFERENCE_TYPE 98190075Sobrien && reference_compatible_p (t, TREE_TYPE (t2))) 982169689Skan cand = add_function_candidate (&candidates, f, s, arglist, 983169689Skan TYPE_BINFO (s), 984117395Skan TREE_PURPOSE (conversions), 985117395Skan LOOKUP_NORMAL); 986169689Skan 987117395Skan if (cand) 988132718Skan { 989169689Skan conversion *identity_conv; 990132718Skan /* Build a standard conversion sequence indicating the 991132718Skan binding from the reference type returned by the 992132718Skan function to the desired REFERENCE_TYPE. */ 993169689Skan identity_conv 994169689Skan = build_identity_conv (TREE_TYPE (TREE_TYPE 995169689Skan (TREE_TYPE (cand->fn))), 996169689Skan NULL_TREE); 997132718Skan cand->second_conv 998169689Skan = (direct_reference_binding 999169689Skan (reference_type, identity_conv)); 1000169689Skan cand->second_conv->bad_p |= cand->convs[0]->bad_p; 1001132718Skan } 100290075Sobrien } 1003117395Skan conversions = TREE_CHAIN (conversions); 100490075Sobrien } 100590075Sobrien 1006132718Skan candidates = splice_viable (candidates, pedantic, &any_viable_p); 100790075Sobrien /* If none of the conversion functions worked out, let our caller 100890075Sobrien know. */ 1009132718Skan if (!any_viable_p) 1010169689Skan return NULL; 1011132718Skan 101290075Sobrien cand = tourney (candidates); 101390075Sobrien if (!cand) 1014169689Skan return NULL; 101590075Sobrien 1016132718Skan /* Now that we know that this is the function we're going to use fix 1017132718Skan the dummy first argument. */ 1018132718Skan cand->args = tree_cons (NULL_TREE, 1019132718Skan build_this (expr), 1020132718Skan TREE_CHAIN (cand->args)); 1021132718Skan 1022117395Skan /* Build a user-defined conversion sequence representing the 1023117395Skan conversion. */ 1024169689Skan conv = build_conv (ck_user, 1025117395Skan TREE_TYPE (TREE_TYPE (cand->fn)), 1026169689Skan build_identity_conv (TREE_TYPE (expr), expr)); 1027169689Skan conv->cand = cand; 1028117395Skan 1029117395Skan /* Merge it with the standard conversion sequence from the 1030117395Skan conversion function's return type to the desired type. */ 1031117395Skan cand->second_conv = merge_conversion_sequences (conv, cand->second_conv); 1032117395Skan 103390075Sobrien if (cand->viable == -1) 1034169689Skan conv->bad_p = true; 1035169689Skan 1036117395Skan return cand->second_conv; 103790075Sobrien} 103890075Sobrien 103990075Sobrien/* A reference of the indicated TYPE is being bound directly to the 104090075Sobrien expression represented by the implicit conversion sequence CONV. 104190075Sobrien Return a conversion sequence for this binding. */ 104290075Sobrien 1043169689Skanstatic conversion * 1044169689Skandirect_reference_binding (tree type, conversion *conv) 104590075Sobrien{ 1046117395Skan tree t; 104790075Sobrien 1048169689Skan gcc_assert (TREE_CODE (type) == REFERENCE_TYPE); 1049169689Skan gcc_assert (TREE_CODE (conv->type) != REFERENCE_TYPE); 1050132718Skan 1051117395Skan t = TREE_TYPE (type); 1052117395Skan 1053169689Skan /* [over.ics.rank] 1054169689Skan 105590075Sobrien When a parameter of reference type binds directly 105690075Sobrien (_dcl.init.ref_) to an argument expression, the implicit 105790075Sobrien conversion sequence is the identity conversion, unless the 105890075Sobrien argument expression has a type that is a derived class of the 105990075Sobrien parameter type, in which case the implicit conversion sequence is 106090075Sobrien a derived-to-base Conversion. 1061169689Skan 106290075Sobrien If the parameter binds directly to the result of applying a 106390075Sobrien conversion function to the argument expression, the implicit 106490075Sobrien conversion sequence is a user-defined conversion sequence 106590075Sobrien (_over.ics.user_), with the second standard conversion sequence 106690075Sobrien either an identity conversion or, if the conversion function 106790075Sobrien returns an entity of a type that is a derived class of the 106890075Sobrien parameter type, a derived-to-base conversion. */ 1069169689Skan if (!same_type_ignoring_top_level_qualifiers_p (t, conv->type)) 107090075Sobrien { 107190075Sobrien /* Represent the derived-to-base conversion. */ 1072169689Skan conv = build_conv (ck_base, t, conv); 107390075Sobrien /* We will actually be binding to the base-class subobject in 107490075Sobrien the derived class, so we mark this conversion appropriately. 107590075Sobrien That way, convert_like knows not to generate a temporary. */ 1076169689Skan conv->need_temporary_p = false; 107790075Sobrien } 1078169689Skan return build_conv (ck_ref_bind, type, conv); 107990075Sobrien} 108090075Sobrien 108150397Sobrien/* Returns the conversion path from type FROM to reference type TO for 108250397Sobrien purposes of reference binding. For lvalue binding, either pass a 108390075Sobrien reference type to FROM or an lvalue expression to EXPR. If the 108490075Sobrien reference will be bound to a temporary, NEED_TEMPORARY_P is set for 1085169689Skan the conversion returned. If C_CAST_P is true, this 1086169689Skan conversion is coming from a C-style cast. */ 108750397Sobrien 1088169689Skanstatic conversion * 1089169689Skanreference_binding (tree rto, tree rfrom, tree expr, bool c_cast_p, int flags) 109050397Sobrien{ 1091169689Skan conversion *conv = NULL; 109250397Sobrien tree to = TREE_TYPE (rto); 109350397Sobrien tree from = rfrom; 1094132718Skan bool related_p; 1095132718Skan bool compatible_p; 109690075Sobrien cp_lvalue_kind lvalue_p = clk_none; 109750397Sobrien 109852284Sobrien if (TREE_CODE (to) == FUNCTION_TYPE && expr && type_unknown_p (expr)) 109952284Sobrien { 110096263Sobrien expr = instantiate_type (to, expr, tf_none); 110152284Sobrien if (expr == error_mark_node) 1102169689Skan return NULL; 110352284Sobrien from = TREE_TYPE (expr); 110452284Sobrien } 110552284Sobrien 110650397Sobrien if (TREE_CODE (from) == REFERENCE_TYPE) 110790075Sobrien { 110890075Sobrien /* Anything with reference type is an lvalue. */ 110990075Sobrien lvalue_p = clk_ordinary; 111090075Sobrien from = TREE_TYPE (from); 111190075Sobrien } 111290075Sobrien else if (expr) 111390075Sobrien lvalue_p = real_lvalue_p (expr); 111450397Sobrien 111590075Sobrien /* Figure out whether or not the types are reference-related and 111690075Sobrien reference compatible. We have do do this after stripping 111790075Sobrien references from FROM. */ 111890075Sobrien related_p = reference_related_p (to, from); 1119169689Skan /* If this is a C cast, first convert to an appropriately qualified 1120169689Skan type, so that we can later do a const_cast to the desired type. */ 1121169689Skan if (related_p && c_cast_p 1122169689Skan && !at_least_as_qualified_p (to, from)) 1123169689Skan to = build_qualified_type (to, cp_type_quals (from)); 112490075Sobrien compatible_p = reference_compatible_p (to, from); 112550397Sobrien 112690075Sobrien if (lvalue_p && compatible_p) 112750397Sobrien { 112890075Sobrien /* [dcl.init.ref] 112990075Sobrien 1130169689Skan If the initializer expression 1131169689Skan 113290075Sobrien -- is an lvalue (but not an lvalue for a bit-field), and "cv1 T1" 113390075Sobrien is reference-compatible with "cv2 T2," 1134169689Skan 1135132718Skan the reference is bound directly to the initializer expression 113690075Sobrien lvalue. */ 1137169689Skan conv = build_identity_conv (from, expr); 113890075Sobrien conv = direct_reference_binding (rto, conv); 1139132718Skan if ((lvalue_p & clk_bitfield) != 0 1140132718Skan || ((lvalue_p & clk_packed) != 0 && !TYPE_PACKED (to))) 114190075Sobrien /* For the purposes of overload resolution, we ignore the fact 1142132718Skan this expression is a bitfield or packed field. (In particular, 114390075Sobrien [over.ics.ref] says specifically that a function with a 114490075Sobrien non-const reference parameter is viable even if the 114590075Sobrien argument is a bitfield.) 114650397Sobrien 114790075Sobrien However, when we actually call the function we must create 114890075Sobrien a temporary to which to bind the reference. If the 114990075Sobrien reference is volatile, or isn't const, then we cannot make 115090075Sobrien a temporary, so we just issue an error when the conversion 115190075Sobrien actually occurs. */ 1152169689Skan conv->need_temporary_p = true; 1153169689Skan 115490075Sobrien return conv; 115550397Sobrien } 115690075Sobrien else if (CLASS_TYPE_P (from) && !(flags & LOOKUP_NO_CONVERSION)) 115790075Sobrien { 115890075Sobrien /* [dcl.init.ref] 115950397Sobrien 1160132718Skan If the initializer expression 116190075Sobrien 116290075Sobrien -- has a class type (i.e., T2 is a class type) can be 116390075Sobrien implicitly converted to an lvalue of type "cv3 T3," where 116490075Sobrien "cv1 T1" is reference-compatible with "cv3 T3". (this 116590075Sobrien conversion is selected by enumerating the applicable 116690075Sobrien conversion functions (_over.match.ref_) and choosing the 1167169689Skan best one through overload resolution. (_over.match_). 116890075Sobrien 1169169689Skan the reference is bound to the lvalue result of the conversion 117090075Sobrien in the second case. */ 117190075Sobrien conv = convert_class_to_reference (to, from, expr); 117250397Sobrien if (conv) 1173117395Skan return conv; 117490075Sobrien } 117550397Sobrien 117690075Sobrien /* From this point on, we conceptually need temporaries, even if we 117790075Sobrien elide them. Only the cases above are "direct bindings". */ 117890075Sobrien if (flags & LOOKUP_NO_TEMP_BIND) 1179169689Skan return NULL; 118090075Sobrien 118190075Sobrien /* [over.ics.rank] 1182169689Skan 118390075Sobrien When a parameter of reference type is not bound directly to an 118490075Sobrien argument expression, the conversion sequence is the one required 118590075Sobrien to convert the argument expression to the underlying type of the 118690075Sobrien reference according to _over.best.ics_. Conceptually, this 118790075Sobrien conversion sequence corresponds to copy-initializing a temporary 118890075Sobrien of the underlying type with the argument expression. Any 118990075Sobrien difference in top-level cv-qualification is subsumed by the 119090075Sobrien initialization itself and does not constitute a conversion. */ 119190075Sobrien 119290075Sobrien /* [dcl.init.ref] 119390075Sobrien 119490075Sobrien Otherwise, the reference shall be to a non-volatile const type. */ 119590075Sobrien if (!CP_TYPE_CONST_NON_VOLATILE_P (to)) 1196169689Skan return NULL; 119790075Sobrien 119890075Sobrien /* [dcl.init.ref] 1199169689Skan 120090075Sobrien If the initializer expression is an rvalue, with T2 a class type, 120190075Sobrien and "cv1 T1" is reference-compatible with "cv2 T2", the reference 120290075Sobrien is bound in one of the following ways: 1203169689Skan 120490075Sobrien -- The reference is bound to the object represented by the rvalue 1205169689Skan or to a sub-object within that object. 120690075Sobrien 1207117395Skan -- ... 1208169689Skan 1209117395Skan We use the first alternative. The implicit conversion sequence 1210117395Skan is supposed to be same as we would obtain by generating a 1211117395Skan temporary. Fortunately, if the types are reference compatible, 1212117395Skan then this is either an identity conversion or the derived-to-base 1213117395Skan conversion, just as for direct binding. */ 121490075Sobrien if (CLASS_TYPE_P (from) && compatible_p) 121590075Sobrien { 1216169689Skan conv = build_identity_conv (from, expr); 1217132718Skan conv = direct_reference_binding (rto, conv); 1218132718Skan if (!(flags & LOOKUP_CONSTRUCTOR_CALLABLE)) 1219169689Skan conv->u.next->check_copy_constructor_p = true; 1220132718Skan return conv; 122150397Sobrien } 122250397Sobrien 122390075Sobrien /* [dcl.init.ref] 122490075Sobrien 122590075Sobrien Otherwise, a temporary of type "cv1 T1" is created and 122690075Sobrien initialized from the initializer expression using the rules for a 122790075Sobrien non-reference copy initialization. If T1 is reference-related to 122890075Sobrien T2, cv1 must be the same cv-qualification as, or greater 122990075Sobrien cv-qualification than, cv2; otherwise, the program is ill-formed. */ 123090075Sobrien if (related_p && !at_least_as_qualified_p (to, from)) 1231169689Skan return NULL; 123290075Sobrien 1233169689Skan conv = implicit_conversion (to, from, expr, c_cast_p, 1234169689Skan flags); 123590075Sobrien if (!conv) 1236169689Skan return NULL; 123790075Sobrien 1238169689Skan conv = build_conv (ck_ref_bind, rto, conv); 123990075Sobrien /* This reference binding, unlike those above, requires the 124090075Sobrien creation of a temporary. */ 1241169689Skan conv->need_temporary_p = true; 124290075Sobrien 124350397Sobrien return conv; 124450397Sobrien} 124550397Sobrien 1246169689Skan/* Returns the implicit conversion sequence (see [over.ics]) from type 1247169689Skan FROM to type TO. The optional expression EXPR may affect the 1248169689Skan conversion. FLAGS are the usual overloading flags. Only 1249169689Skan LOOKUP_NO_CONVERSION is significant. If C_CAST_P is true, this 1250169689Skan conversion is coming from a C-style cast. */ 125150397Sobrien 1252169689Skanstatic conversion * 1253169689Skanimplicit_conversion (tree to, tree from, tree expr, bool c_cast_p, 1254169689Skan int flags) 125550397Sobrien{ 1256169689Skan conversion *conv; 125750397Sobrien 125890075Sobrien if (from == error_mark_node || to == error_mark_node 125990075Sobrien || expr == error_mark_node) 1260169689Skan return NULL; 126190075Sobrien 126250397Sobrien if (TREE_CODE (to) == REFERENCE_TYPE) 1263169689Skan conv = reference_binding (to, from, expr, c_cast_p, flags); 126450397Sobrien else 1265169689Skan conv = standard_conversion (to, from, expr, c_cast_p, flags); 126650397Sobrien 126750397Sobrien if (conv) 1268132718Skan return conv; 1269132718Skan 1270132718Skan if (expr != NULL_TREE 1271132718Skan && (IS_AGGR_TYPE (from) 1272132718Skan || IS_AGGR_TYPE (to)) 1273132718Skan && (flags & LOOKUP_NO_CONVERSION) == 0) 127450397Sobrien { 1275117395Skan struct z_candidate *cand; 1276117395Skan 127750397Sobrien cand = build_user_type_conversion_1 127850397Sobrien (to, expr, LOOKUP_ONLYCONVERTING); 127950397Sobrien if (cand) 128050397Sobrien conv = cand->second_conv; 128190075Sobrien 128290075Sobrien /* We used to try to bind a reference to a temporary here, but that 128390075Sobrien is now handled by the recursive call to this function at the end 128490075Sobrien of reference_binding. */ 1285132718Skan return conv; 128650397Sobrien } 128718334Speter 1288169689Skan return NULL; 128950397Sobrien} 129050397Sobrien 129150397Sobrien/* Add a new entry to the list of candidates. Used by the add_*_candidate 129250397Sobrien functions. */ 129350397Sobrien 129450397Sobrienstatic struct z_candidate * 1295169689Skanadd_candidate (struct z_candidate **candidates, 1296169689Skan tree fn, tree args, 1297169689Skan size_t num_convs, conversion **convs, 1298169689Skan tree access_path, tree conversion_path, 1299169689Skan int viable) 130050397Sobrien{ 1301169689Skan struct z_candidate *cand = (struct z_candidate *) 1302169689Skan conversion_obstack_alloc (sizeof (struct z_candidate)); 130350397Sobrien 130450397Sobrien cand->fn = fn; 1305132718Skan cand->args = args; 130650397Sobrien cand->convs = convs; 1307169689Skan cand->num_convs = num_convs; 1308117395Skan cand->access_path = access_path; 1309117395Skan cand->conversion_path = conversion_path; 131050397Sobrien cand->viable = viable; 1311117395Skan cand->next = *candidates; 1312117395Skan *candidates = cand; 131350397Sobrien 131450397Sobrien return cand; 131550397Sobrien} 131650397Sobrien 131750397Sobrien/* Create an overload candidate for the function or method FN called with 131850397Sobrien the argument list ARGLIST and add it to CANDIDATES. FLAGS is passed on 131990075Sobrien to implicit_conversion. 132050397Sobrien 132190075Sobrien CTYPE, if non-NULL, is the type we want to pretend this function 132290075Sobrien comes from for purposes of overload resolution. */ 132390075Sobrien 132450397Sobrienstatic struct z_candidate * 1325169689Skanadd_function_candidate (struct z_candidate **candidates, 1326169689Skan tree fn, tree ctype, tree arglist, 1327117395Skan tree access_path, tree conversion_path, 1328117395Skan int flags) 132950397Sobrien{ 133050397Sobrien tree parmlist = TYPE_ARG_TYPES (TREE_TYPE (fn)); 133150397Sobrien int i, len; 1332169689Skan conversion **convs; 133352284Sobrien tree parmnode, argnode; 1334132718Skan tree orig_arglist; 133550397Sobrien int viable = 1; 133650397Sobrien 1337169689Skan /* At this point we should not see any functions which haven't been 1338169689Skan explicitly declared, except for friend functions which will have 1339169689Skan been found using argument dependent lookup. */ 1340169689Skan gcc_assert (!DECL_ANTICIPATED (fn) || DECL_HIDDEN_FRIEND_P (fn)); 1341117395Skan 134290075Sobrien /* The `this', `in_chrg' and VTT arguments to constructors are not 134390075Sobrien considered in overload resolution. */ 134450397Sobrien if (DECL_CONSTRUCTOR_P (fn)) 134550397Sobrien { 134690075Sobrien parmlist = skip_artificial_parms_for (fn, parmlist); 1347132718Skan orig_arglist = arglist; 134890075Sobrien arglist = skip_artificial_parms_for (fn, arglist); 134918334Speter } 1350169689Skan else 1351132718Skan orig_arglist = arglist; 135250397Sobrien 135352284Sobrien len = list_length (arglist); 1354169689Skan convs = alloc_conversions (len); 135550397Sobrien 135652284Sobrien /* 13.3.2 - Viable functions [over.match.viable] 135752284Sobrien First, to be a viable function, a candidate function shall have enough 135852284Sobrien parameters to agree in number with the arguments in the list. 135952284Sobrien 136052284Sobrien We need to check this first; otherwise, checking the ICSes might cause 136152284Sobrien us to produce an ill-formed template instantiation. */ 136252284Sobrien 136352284Sobrien parmnode = parmlist; 136450397Sobrien for (i = 0; i < len; ++i) 136518334Speter { 136652284Sobrien if (parmnode == NULL_TREE || parmnode == void_list_node) 136752284Sobrien break; 136852284Sobrien parmnode = TREE_CHAIN (parmnode); 136952284Sobrien } 137052284Sobrien 137152284Sobrien if (i < len && parmnode) 137252284Sobrien viable = 0; 137352284Sobrien 137452284Sobrien /* Make sure there are default args for the rest of the parms. */ 137590075Sobrien else if (!sufficient_parms_p (parmnode)) 137690075Sobrien viable = 0; 137752284Sobrien 137852284Sobrien if (! viable) 137952284Sobrien goto out; 138052284Sobrien 138152284Sobrien /* Second, for F to be a viable function, there shall exist for each 138252284Sobrien argument an implicit conversion sequence that converts that argument 138352284Sobrien to the corresponding parameter of F. */ 138452284Sobrien 138552284Sobrien parmnode = parmlist; 138652284Sobrien argnode = arglist; 138752284Sobrien 138852284Sobrien for (i = 0; i < len; ++i) 138952284Sobrien { 139050397Sobrien tree arg = TREE_VALUE (argnode); 139152284Sobrien tree argtype = lvalue_type (arg); 1392169689Skan conversion *t; 139390075Sobrien int is_this; 139418334Speter 139550397Sobrien if (parmnode == void_list_node) 139650397Sobrien break; 139752284Sobrien 139890075Sobrien is_this = (i == 0 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn) 139990075Sobrien && ! DECL_CONSTRUCTOR_P (fn)); 140090075Sobrien 140152284Sobrien if (parmnode) 140252284Sobrien { 140352284Sobrien tree parmtype = TREE_VALUE (parmnode); 140452284Sobrien 140590075Sobrien /* The type of the implicit object parameter ('this') for 140690075Sobrien overload resolution is not always the same as for the 140790075Sobrien function itself; conversion functions are considered to 140890075Sobrien be members of the class being converted, and functions 140990075Sobrien introduced by a using-declaration are considered to be 141090075Sobrien members of the class that uses them. 141152284Sobrien 141290075Sobrien Since build_over_call ignores the ICS for the `this' 141390075Sobrien parameter, we can just change the parm type. */ 141490075Sobrien if (ctype && is_this) 141552284Sobrien { 141652284Sobrien parmtype 141790075Sobrien = build_qualified_type (ctype, 141852284Sobrien TYPE_QUALS (TREE_TYPE (parmtype))); 141952284Sobrien parmtype = build_pointer_type (parmtype); 142052284Sobrien } 142152284Sobrien 1422169689Skan t = implicit_conversion (parmtype, argtype, arg, 1423169689Skan /*c_cast_p=*/false, flags); 142452284Sobrien } 142550397Sobrien else 142618334Speter { 1427169689Skan t = build_identity_conv (argtype, arg); 1428169689Skan t->ellipsis_p = true; 142918334Speter } 143050397Sobrien 143190075Sobrien if (t && is_this) 1432169689Skan t->this_p = true; 143350397Sobrien 1434169689Skan convs[i] = t; 143550397Sobrien if (! t) 143652284Sobrien { 143752284Sobrien viable = 0; 143852284Sobrien break; 143952284Sobrien } 144050397Sobrien 1441169689Skan if (t->bad_p) 144250397Sobrien viable = -1; 144350397Sobrien 144450397Sobrien if (parmnode) 144550397Sobrien parmnode = TREE_CHAIN (parmnode); 144650397Sobrien argnode = TREE_CHAIN (argnode); 144750397Sobrien } 144850397Sobrien 144952284Sobrien out: 1450169689Skan return add_candidate (candidates, fn, orig_arglist, len, convs, 1451169689Skan access_path, conversion_path, viable); 145250397Sobrien} 145350397Sobrien 145450397Sobrien/* Create an overload candidate for the conversion function FN which will 145550397Sobrien be invoked for expression OBJ, producing a pointer-to-function which 145650397Sobrien will in turn be called with the argument list ARGLIST, and add it to 145752284Sobrien CANDIDATES. FLAGS is passed on to implicit_conversion. 145850397Sobrien 145952284Sobrien Actually, we don't really care about FN; we care about the type it 146052284Sobrien converts to. There may be multiple conversion functions that will 146152284Sobrien convert to that type, and we rely on build_user_type_conversion_1 to 146252284Sobrien choose the best one; so when we create our candidate, we record the type 146352284Sobrien instead of the function. */ 146452284Sobrien 146550397Sobrienstatic struct z_candidate * 1466132718Skanadd_conv_candidate (struct z_candidate **candidates, tree fn, tree obj, 1467169689Skan tree arglist, tree access_path, tree conversion_path) 146850397Sobrien{ 146950397Sobrien tree totype = TREE_TYPE (TREE_TYPE (fn)); 147090075Sobrien int i, len, viable, flags; 1471169689Skan tree parmlist, parmnode, argnode; 1472169689Skan conversion **convs; 147350397Sobrien 147490075Sobrien for (parmlist = totype; TREE_CODE (parmlist) != FUNCTION_TYPE; ) 147590075Sobrien parmlist = TREE_TYPE (parmlist); 147690075Sobrien parmlist = TYPE_ARG_TYPES (parmlist); 147790075Sobrien 147890075Sobrien len = list_length (arglist) + 1; 1479169689Skan convs = alloc_conversions (len); 148090075Sobrien parmnode = parmlist; 148190075Sobrien argnode = arglist; 148290075Sobrien viable = 1; 148390075Sobrien flags = LOOKUP_NORMAL; 148490075Sobrien 148552284Sobrien /* Don't bother looking up the same type twice. */ 1486117395Skan if (*candidates && (*candidates)->fn == totype) 1487117395Skan return NULL; 148852284Sobrien 148950397Sobrien for (i = 0; i < len; ++i) 149050397Sobrien { 149150397Sobrien tree arg = i == 0 ? obj : TREE_VALUE (argnode); 149250397Sobrien tree argtype = lvalue_type (arg); 1493169689Skan conversion *t; 149450397Sobrien 149550397Sobrien if (i == 0) 1496169689Skan t = implicit_conversion (totype, argtype, arg, /*c_cast_p=*/false, 1497169689Skan flags); 149850397Sobrien else if (parmnode == void_list_node) 149950397Sobrien break; 150050397Sobrien else if (parmnode) 1501169689Skan t = implicit_conversion (TREE_VALUE (parmnode), argtype, arg, 1502169689Skan /*c_cast_p=*/false, flags); 150318334Speter else 150418334Speter { 1505169689Skan t = build_identity_conv (argtype, arg); 1506169689Skan t->ellipsis_p = true; 150718334Speter } 150818334Speter 1509169689Skan convs[i] = t; 151050397Sobrien if (! t) 151150397Sobrien break; 151250397Sobrien 1513169689Skan if (t->bad_p) 151450397Sobrien viable = -1; 151550397Sobrien 151650397Sobrien if (i == 0) 151750397Sobrien continue; 151850397Sobrien 151950397Sobrien if (parmnode) 152050397Sobrien parmnode = TREE_CHAIN (parmnode); 152150397Sobrien argnode = TREE_CHAIN (argnode); 152218334Speter } 152350397Sobrien 152450397Sobrien if (i < len) 152550397Sobrien viable = 0; 152650397Sobrien 152790075Sobrien if (!sufficient_parms_p (parmnode)) 152890075Sobrien viable = 0; 152950397Sobrien 1530169689Skan return add_candidate (candidates, totype, arglist, len, convs, 1531169689Skan access_path, conversion_path, viable); 153250397Sobrien} 153350397Sobrien 1534117395Skanstatic void 1535132718Skanbuild_builtin_candidate (struct z_candidate **candidates, tree fnname, 1536169689Skan tree type1, tree type2, tree *args, tree *argtypes, 1537169689Skan int flags) 153850397Sobrien{ 1539169689Skan conversion *t; 1540169689Skan conversion **convs; 1541169689Skan size_t num_convs; 154250397Sobrien int viable = 1, i; 154350397Sobrien tree types[2]; 154450397Sobrien 154550397Sobrien types[0] = type1; 154650397Sobrien types[1] = type2; 154750397Sobrien 1548169689Skan num_convs = args[2] ? 3 : (args[1] ? 2 : 1); 1549169689Skan convs = alloc_conversions (num_convs); 155050397Sobrien 155150397Sobrien for (i = 0; i < 2; ++i) 155218334Speter { 155350397Sobrien if (! args[i]) 155450397Sobrien break; 155550397Sobrien 1556169689Skan t = implicit_conversion (types[i], argtypes[i], args[i], 1557169689Skan /*c_cast_p=*/false, flags); 155850397Sobrien if (! t) 155950397Sobrien { 156050397Sobrien viable = 0; 156150397Sobrien /* We need something for printing the candidate. */ 1562169689Skan t = build_identity_conv (types[i], NULL_TREE); 156350397Sobrien } 1564169689Skan else if (t->bad_p) 156550397Sobrien viable = 0; 1566169689Skan convs[i] = t; 156718334Speter } 156850397Sobrien 156950397Sobrien /* For COND_EXPR we rearranged the arguments; undo that now. */ 157050397Sobrien if (args[2]) 157118334Speter { 1572169689Skan convs[2] = convs[1]; 1573169689Skan convs[1] = convs[0]; 1574169689Skan t = implicit_conversion (boolean_type_node, argtypes[2], args[2], 1575169689Skan /*c_cast_p=*/false, flags); 157650397Sobrien if (t) 1577169689Skan convs[0] = t; 157850397Sobrien else 157950397Sobrien viable = 0; 1580169689Skan } 158118334Speter 1582169689Skan add_candidate (candidates, fnname, /*args=*/NULL_TREE, 1583169689Skan num_convs, convs, 1584117395Skan /*access_path=*/NULL_TREE, 1585117395Skan /*conversion_path=*/NULL_TREE, 1586117395Skan viable); 158750397Sobrien} 158818334Speter 1589132718Skanstatic bool 1590132718Skanis_complete (tree t) 159150397Sobrien{ 159290075Sobrien return COMPLETE_TYPE_P (complete_type (t)); 159350397Sobrien} 159418334Speter 1595117395Skan/* Returns nonzero if TYPE is a promoted arithmetic type. */ 159690075Sobrien 1597132718Skanstatic bool 1598132718Skanpromoted_arithmetic_type_p (tree type) 159990075Sobrien{ 160090075Sobrien /* [over.built] 160190075Sobrien 160290075Sobrien In this section, the term promoted integral type is used to refer 160390075Sobrien to those integral types which are preserved by integral promotion 160490075Sobrien (including e.g. int and long but excluding e.g. char). 160590075Sobrien Similarly, the term promoted arithmetic type refers to promoted 160690075Sobrien integral types plus floating types. */ 160790075Sobrien return ((INTEGRAL_TYPE_P (type) 160890075Sobrien && same_type_p (type_promotes_to (type), type)) 160990075Sobrien || TREE_CODE (type) == REAL_TYPE); 161090075Sobrien} 161190075Sobrien 161250397Sobrien/* Create any builtin operator overload candidates for the operator in 161350397Sobrien question given the converted operand types TYPE1 and TYPE2. The other 161450397Sobrien args are passed through from add_builtin_candidates to 1615169689Skan build_builtin_candidate. 1616169689Skan 1617169689Skan TYPE1 and TYPE2 may not be permissible, and we must filter them. 161890075Sobrien If CODE is requires candidates operands of the same type of the kind 161990075Sobrien of which TYPE1 and TYPE2 are, we add both candidates 162090075Sobrien CODE (TYPE1, TYPE1) and CODE (TYPE2, TYPE2). */ 162118334Speter 1622117395Skanstatic void 1623132718Skanadd_builtin_candidate (struct z_candidate **candidates, enum tree_code code, 1624169689Skan enum tree_code code2, tree fnname, tree type1, 1625169689Skan tree type2, tree *args, tree *argtypes, int flags) 162650397Sobrien{ 162750397Sobrien switch (code) 162850397Sobrien { 162950397Sobrien case POSTINCREMENT_EXPR: 163050397Sobrien case POSTDECREMENT_EXPR: 163150397Sobrien args[1] = integer_zero_node; 163250397Sobrien type2 = integer_type_node; 163350397Sobrien break; 163450397Sobrien default: 163550397Sobrien break; 163650397Sobrien } 163750397Sobrien 163850397Sobrien switch (code) 163950397Sobrien { 164050397Sobrien 164150397Sobrien/* 4 For every pair T, VQ), where T is an arithmetic or enumeration type, 164250397Sobrien and VQ is either volatile or empty, there exist candidate operator 164350397Sobrien functions of the form 164450397Sobrien VQ T& operator++(VQ T&); 164550397Sobrien T operator++(VQ T&, int); 164650397Sobrien 5 For every pair T, VQ), where T is an enumeration type or an arithmetic 164750397Sobrien type other than bool, and VQ is either volatile or empty, there exist 164850397Sobrien candidate operator functions of the form 164950397Sobrien VQ T& operator--(VQ T&); 165050397Sobrien T operator--(VQ T&, int); 165150397Sobrien 6 For every pair T, VQ), where T is a cv-qualified or cv-unqualified 165250397Sobrien complete object type, and VQ is either volatile or empty, there exist 165350397Sobrien candidate operator functions of the form 165450397Sobrien T*VQ& operator++(T*VQ&); 165550397Sobrien T*VQ& operator--(T*VQ&); 165650397Sobrien T* operator++(T*VQ&, int); 165750397Sobrien T* operator--(T*VQ&, int); */ 165850397Sobrien 165950397Sobrien case POSTDECREMENT_EXPR: 166050397Sobrien case PREDECREMENT_EXPR: 166150397Sobrien if (TREE_CODE (type1) == BOOLEAN_TYPE) 1662117395Skan return; 166350397Sobrien case POSTINCREMENT_EXPR: 166450397Sobrien case PREINCREMENT_EXPR: 166590075Sobrien if (ARITHMETIC_TYPE_P (type1) || TYPE_PTROB_P (type1)) 166618334Speter { 166750397Sobrien type1 = build_reference_type (type1); 166850397Sobrien break; 166918334Speter } 1670117395Skan return; 167150397Sobrien 167250397Sobrien/* 7 For every cv-qualified or cv-unqualified complete object type T, there 167350397Sobrien exist candidate operator functions of the form 167450397Sobrien 167550397Sobrien T& operator*(T*); 167650397Sobrien 167750397Sobrien 8 For every function type T, there exist candidate operator functions of 167850397Sobrien the form 167950397Sobrien T& operator*(T*); */ 168050397Sobrien 168150397Sobrien case INDIRECT_REF: 168250397Sobrien if (TREE_CODE (type1) == POINTER_TYPE 168350397Sobrien && (TYPE_PTROB_P (type1) 168450397Sobrien || TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)) 168550397Sobrien break; 1686169689Skan return; 168750397Sobrien 168850397Sobrien/* 9 For every type T, there exist candidate operator functions of the form 168950397Sobrien T* operator+(T*); 169050397Sobrien 169150397Sobrien 10For every promoted arithmetic type T, there exist candidate operator 169250397Sobrien functions of the form 169350397Sobrien T operator+(T); 169450397Sobrien T operator-(T); */ 169550397Sobrien 1696169689Skan case UNARY_PLUS_EXPR: /* unary + */ 1697132718Skan if (TREE_CODE (type1) == POINTER_TYPE) 169850397Sobrien break; 169950397Sobrien case NEGATE_EXPR: 170050397Sobrien if (ARITHMETIC_TYPE_P (type1)) 170150397Sobrien break; 1702117395Skan return; 170350397Sobrien 170450397Sobrien/* 11For every promoted integral type T, there exist candidate operator 170550397Sobrien functions of the form 170650397Sobrien T operator~(T); */ 170750397Sobrien 170850397Sobrien case BIT_NOT_EXPR: 170950397Sobrien if (INTEGRAL_TYPE_P (type1)) 171050397Sobrien break; 1711117395Skan return; 171250397Sobrien 171350397Sobrien/* 12For every quintuple C1, C2, T, CV1, CV2), where C2 is a class type, C1 171450397Sobrien is the same type as C2 or is a derived class of C2, T is a complete 171550397Sobrien object type or a function type, and CV1 and CV2 are cv-qualifier-seqs, 171650397Sobrien there exist candidate operator functions of the form 171750397Sobrien CV12 T& operator->*(CV1 C1*, CV2 T C2::*); 171850397Sobrien where CV12 is the union of CV1 and CV2. */ 171950397Sobrien 172050397Sobrien case MEMBER_REF: 172150397Sobrien if (TREE_CODE (type1) == POINTER_TYPE 1722132718Skan && TYPE_PTR_TO_MEMBER_P (type2)) 172318334Speter { 172450397Sobrien tree c1 = TREE_TYPE (type1); 1725132718Skan tree c2 = TYPE_PTRMEM_CLASS_TYPE (type2); 172618334Speter 172750397Sobrien if (IS_AGGR_TYPE (c1) && DERIVED_FROM_P (c2, c1) 172850397Sobrien && (TYPE_PTRMEMFUNC_P (type2) 1729161651Skan || is_complete (TYPE_PTRMEM_POINTED_TO_TYPE (type2)))) 173050397Sobrien break; 173150397Sobrien } 1732117395Skan return; 173318334Speter 173450397Sobrien/* 13For every pair of promoted arithmetic types L and R, there exist can- 173550397Sobrien didate operator functions of the form 173650397Sobrien LR operator*(L, R); 173750397Sobrien LR operator/(L, R); 173850397Sobrien LR operator+(L, R); 173950397Sobrien LR operator-(L, R); 174050397Sobrien bool operator<(L, R); 174150397Sobrien bool operator>(L, R); 174250397Sobrien bool operator<=(L, R); 174350397Sobrien bool operator>=(L, R); 174450397Sobrien bool operator==(L, R); 174550397Sobrien bool operator!=(L, R); 174650397Sobrien where LR is the result of the usual arithmetic conversions between 174750397Sobrien types L and R. 174850397Sobrien 174950397Sobrien 14For every pair of types T and I, where T is a cv-qualified or cv- 175050397Sobrien unqualified complete object type and I is a promoted integral type, 175150397Sobrien there exist candidate operator functions of the form 175250397Sobrien T* operator+(T*, I); 175350397Sobrien T& operator[](T*, I); 175450397Sobrien T* operator-(T*, I); 175550397Sobrien T* operator+(I, T*); 175650397Sobrien T& operator[](I, T*); 175750397Sobrien 175850397Sobrien 15For every T, where T is a pointer to complete object type, there exist 175950397Sobrien candidate operator functions of the form112) 176050397Sobrien ptrdiff_t operator-(T, T); 176150397Sobrien 176290075Sobrien 16For every pointer or enumeration type T, there exist candidate operator 176390075Sobrien functions of the form 176450397Sobrien bool operator<(T, T); 176550397Sobrien bool operator>(T, T); 176650397Sobrien bool operator<=(T, T); 176750397Sobrien bool operator>=(T, T); 176850397Sobrien bool operator==(T, T); 176950397Sobrien bool operator!=(T, T); 177050397Sobrien 177150397Sobrien 17For every pointer to member type T, there exist candidate operator 177250397Sobrien functions of the form 177350397Sobrien bool operator==(T, T); 177450397Sobrien bool operator!=(T, T); */ 177550397Sobrien 177650397Sobrien case MINUS_EXPR: 177750397Sobrien if (TYPE_PTROB_P (type1) && TYPE_PTROB_P (type2)) 177850397Sobrien break; 177950397Sobrien if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2)) 178050397Sobrien { 178150397Sobrien type2 = ptrdiff_type_node; 178250397Sobrien break; 178350397Sobrien } 178450397Sobrien case MULT_EXPR: 178550397Sobrien case TRUNC_DIV_EXPR: 178650397Sobrien if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2)) 178750397Sobrien break; 1788117395Skan return; 178950397Sobrien 179050397Sobrien case EQ_EXPR: 179150397Sobrien case NE_EXPR: 179250397Sobrien if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2)) 179350397Sobrien || (TYPE_PTRMEM_P (type1) && TYPE_PTRMEM_P (type2))) 179450397Sobrien break; 1795132718Skan if (TYPE_PTR_TO_MEMBER_P (type1) && null_ptr_cst_p (args[1])) 179650397Sobrien { 179750397Sobrien type2 = type1; 179850397Sobrien break; 179950397Sobrien } 1800132718Skan if (TYPE_PTR_TO_MEMBER_P (type2) && null_ptr_cst_p (args[0])) 180150397Sobrien { 180250397Sobrien type1 = type2; 180350397Sobrien break; 180450397Sobrien } 1805132718Skan /* Fall through. */ 180650397Sobrien case LT_EXPR: 180750397Sobrien case GT_EXPR: 180850397Sobrien case LE_EXPR: 180950397Sobrien case GE_EXPR: 181050397Sobrien case MAX_EXPR: 181150397Sobrien case MIN_EXPR: 181290075Sobrien if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2)) 1813169689Skan break; 181490075Sobrien if (TYPE_PTR_P (type1) && TYPE_PTR_P (type2)) 181550397Sobrien break; 1816169689Skan if (TREE_CODE (type1) == ENUMERAL_TYPE 1817169689Skan && TREE_CODE (type2) == ENUMERAL_TYPE) 1818169689Skan break; 1819169689Skan if (TYPE_PTR_P (type1) 1820169689Skan && null_ptr_cst_p (args[1]) 1821169689Skan && !uses_template_parms (type1)) 182250397Sobrien { 182350397Sobrien type2 = type1; 182450397Sobrien break; 182550397Sobrien } 1826169689Skan if (null_ptr_cst_p (args[0]) 1827169689Skan && TYPE_PTR_P (type2) 1828169689Skan && !uses_template_parms (type2)) 182950397Sobrien { 183050397Sobrien type1 = type2; 183150397Sobrien break; 183250397Sobrien } 1833117395Skan return; 183450397Sobrien 183550397Sobrien case PLUS_EXPR: 183650397Sobrien if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2)) 183750397Sobrien break; 183850397Sobrien case ARRAY_REF: 183950397Sobrien if (INTEGRAL_TYPE_P (type1) && TYPE_PTROB_P (type2)) 184050397Sobrien { 184150397Sobrien type1 = ptrdiff_type_node; 184250397Sobrien break; 184350397Sobrien } 184450397Sobrien if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2)) 184550397Sobrien { 184650397Sobrien type2 = ptrdiff_type_node; 184750397Sobrien break; 184850397Sobrien } 1849117395Skan return; 185050397Sobrien 185150397Sobrien/* 18For every pair of promoted integral types L and R, there exist candi- 185250397Sobrien date operator functions of the form 185350397Sobrien LR operator%(L, R); 185450397Sobrien LR operator&(L, R); 185550397Sobrien LR operator^(L, R); 185650397Sobrien LR operator|(L, R); 185750397Sobrien L operator<<(L, R); 185850397Sobrien L operator>>(L, R); 185950397Sobrien where LR is the result of the usual arithmetic conversions between 186050397Sobrien types L and R. */ 186150397Sobrien 186250397Sobrien case TRUNC_MOD_EXPR: 186350397Sobrien case BIT_AND_EXPR: 186450397Sobrien case BIT_IOR_EXPR: 186550397Sobrien case BIT_XOR_EXPR: 186650397Sobrien case LSHIFT_EXPR: 186750397Sobrien case RSHIFT_EXPR: 186850397Sobrien if (INTEGRAL_TYPE_P (type1) && INTEGRAL_TYPE_P (type2)) 186950397Sobrien break; 1870117395Skan return; 187150397Sobrien 187250397Sobrien/* 19For every triple L, VQ, R), where L is an arithmetic or enumeration 187350397Sobrien type, VQ is either volatile or empty, and R is a promoted arithmetic 187450397Sobrien type, there exist candidate operator functions of the form 187550397Sobrien VQ L& operator=(VQ L&, R); 187650397Sobrien VQ L& operator*=(VQ L&, R); 187750397Sobrien VQ L& operator/=(VQ L&, R); 187850397Sobrien VQ L& operator+=(VQ L&, R); 187950397Sobrien VQ L& operator-=(VQ L&, R); 188050397Sobrien 188150397Sobrien 20For every pair T, VQ), where T is any type and VQ is either volatile 188250397Sobrien or empty, there exist candidate operator functions of the form 188350397Sobrien T*VQ& operator=(T*VQ&, T*); 188450397Sobrien 188550397Sobrien 21For every pair T, VQ), where T is a pointer to member type and VQ is 188650397Sobrien either volatile or empty, there exist candidate operator functions of 188750397Sobrien the form 188850397Sobrien VQ T& operator=(VQ T&, T); 188950397Sobrien 189050397Sobrien 22For every triple T, VQ, I), where T is a cv-qualified or cv- 189150397Sobrien unqualified complete object type, VQ is either volatile or empty, and 189250397Sobrien I is a promoted integral type, there exist candidate operator func- 189350397Sobrien tions of the form 189450397Sobrien T*VQ& operator+=(T*VQ&, I); 189550397Sobrien T*VQ& operator-=(T*VQ&, I); 189650397Sobrien 189750397Sobrien 23For every triple L, VQ, R), where L is an integral or enumeration 189850397Sobrien type, VQ is either volatile or empty, and R is a promoted integral 189950397Sobrien type, there exist candidate operator functions of the form 190050397Sobrien 190150397Sobrien VQ L& operator%=(VQ L&, R); 190250397Sobrien VQ L& operator<<=(VQ L&, R); 190350397Sobrien VQ L& operator>>=(VQ L&, R); 190450397Sobrien VQ L& operator&=(VQ L&, R); 190550397Sobrien VQ L& operator^=(VQ L&, R); 190650397Sobrien VQ L& operator|=(VQ L&, R); */ 190750397Sobrien 190850397Sobrien case MODIFY_EXPR: 190950397Sobrien switch (code2) 191050397Sobrien { 191150397Sobrien case PLUS_EXPR: 191250397Sobrien case MINUS_EXPR: 191350397Sobrien if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2)) 191418334Speter { 191550397Sobrien type2 = ptrdiff_type_node; 191650397Sobrien break; 191718334Speter } 191850397Sobrien case MULT_EXPR: 191950397Sobrien case TRUNC_DIV_EXPR: 192050397Sobrien if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2)) 192150397Sobrien break; 1922117395Skan return; 192350397Sobrien 192450397Sobrien case TRUNC_MOD_EXPR: 192550397Sobrien case BIT_AND_EXPR: 192650397Sobrien case BIT_IOR_EXPR: 192750397Sobrien case BIT_XOR_EXPR: 192850397Sobrien case LSHIFT_EXPR: 192950397Sobrien case RSHIFT_EXPR: 193050397Sobrien if (INTEGRAL_TYPE_P (type1) && INTEGRAL_TYPE_P (type2)) 193150397Sobrien break; 1932117395Skan return; 193350397Sobrien 193450397Sobrien case NOP_EXPR: 193550397Sobrien if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2)) 193650397Sobrien break; 193750397Sobrien if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2)) 193850397Sobrien || (TYPE_PTR_P (type1) && TYPE_PTR_P (type2)) 193950397Sobrien || (TYPE_PTRMEM_P (type1) && TYPE_PTRMEM_P (type2)) 194050397Sobrien || ((TYPE_PTRMEMFUNC_P (type1) 194150397Sobrien || TREE_CODE (type1) == POINTER_TYPE) 194250397Sobrien && null_ptr_cst_p (args[1]))) 194318334Speter { 194450397Sobrien type2 = type1; 194550397Sobrien break; 194618334Speter } 1947117395Skan return; 194850397Sobrien 194950397Sobrien default: 1950169689Skan gcc_unreachable (); 195118334Speter } 195250397Sobrien type1 = build_reference_type (type1); 195350397Sobrien break; 195418334Speter 195550397Sobrien case COND_EXPR: 195690075Sobrien /* [over.built] 195718334Speter 195890075Sobrien For every pair of promoted arithmetic types L and R, there 1959169689Skan exist candidate operator functions of the form 196090075Sobrien 1961169689Skan LR operator?(bool, L, R); 196290075Sobrien 196390075Sobrien where LR is the result of the usual arithmetic conversions 196490075Sobrien between types L and R. 196590075Sobrien 196690075Sobrien For every type T, where T is a pointer or pointer-to-member 196790075Sobrien type, there exist candidate operator functions of the form T 196890075Sobrien operator?(bool, T, T); */ 196990075Sobrien 197090075Sobrien if (promoted_arithmetic_type_p (type1) 197190075Sobrien && promoted_arithmetic_type_p (type2)) 197290075Sobrien /* That's OK. */ 197350397Sobrien break; 197490075Sobrien 197590075Sobrien /* Otherwise, the types should be pointers. */ 1976132718Skan if (!(TYPE_PTR_P (type1) || TYPE_PTR_TO_MEMBER_P (type1)) 1977132718Skan || !(TYPE_PTR_P (type2) || TYPE_PTR_TO_MEMBER_P (type2))) 1978117395Skan return; 1979169689Skan 198090075Sobrien /* We don't check that the two types are the same; the logic 198190075Sobrien below will actually create two candidates; one in which both 198290075Sobrien parameter types are TYPE1, and one in which both parameter 198390075Sobrien types are TYPE2. */ 1984117395Skan break; 198590075Sobrien 198650397Sobrien default: 1987169689Skan gcc_unreachable (); 198850397Sobrien } 198950397Sobrien 199090075Sobrien /* If we're dealing with two pointer types or two enumeral types, 199190075Sobrien we need candidates for both of them. */ 199290075Sobrien if (type2 && !same_type_p (type1, type2) 199350397Sobrien && TREE_CODE (type1) == TREE_CODE (type2) 199450397Sobrien && (TREE_CODE (type1) == REFERENCE_TYPE 1995132718Skan || (TYPE_PTR_P (type1) && TYPE_PTR_P (type2)) 1996132718Skan || (TYPE_PTRMEM_P (type1) && TYPE_PTRMEM_P (type2)) 199750397Sobrien || TYPE_PTRMEMFUNC_P (type1) 199890075Sobrien || IS_AGGR_TYPE (type1) 199990075Sobrien || TREE_CODE (type1) == ENUMERAL_TYPE)) 200050397Sobrien { 2001117395Skan build_builtin_candidate 200250397Sobrien (candidates, fnname, type1, type1, args, argtypes, flags); 2003117395Skan build_builtin_candidate 200450397Sobrien (candidates, fnname, type2, type2, args, argtypes, flags); 2005117395Skan return; 200650397Sobrien } 200750397Sobrien 2008117395Skan build_builtin_candidate 200950397Sobrien (candidates, fnname, type1, type2, args, argtypes, flags); 201050397Sobrien} 201150397Sobrien 201250397Sobrientree 2013132718Skantype_decays_to (tree type) 201450397Sobrien{ 201550397Sobrien if (TREE_CODE (type) == ARRAY_TYPE) 201650397Sobrien return build_pointer_type (TREE_TYPE (type)); 201750397Sobrien if (TREE_CODE (type) == FUNCTION_TYPE) 201850397Sobrien return build_pointer_type (type); 201950397Sobrien return type; 202050397Sobrien} 202150397Sobrien 202250397Sobrien/* There are three conditions of builtin candidates: 202350397Sobrien 202450397Sobrien 1) bool-taking candidates. These are the same regardless of the input. 202550397Sobrien 2) pointer-pair taking candidates. These are generated for each type 202650397Sobrien one of the input types converts to. 202790075Sobrien 3) arithmetic candidates. According to the standard, we should generate 202890075Sobrien all of these, but I'm trying not to... 2029169689Skan 203090075Sobrien Here we generate a superset of the possible candidates for this particular 203190075Sobrien case. That is a subset of the full set the standard defines, plus some 203290075Sobrien other cases which the standard disallows. add_builtin_candidate will 2033117395Skan filter out the invalid set. */ 203450397Sobrien 2035117395Skanstatic void 2036132718Skanadd_builtin_candidates (struct z_candidate **candidates, enum tree_code code, 2037169689Skan enum tree_code code2, tree fnname, tree *args, 2038169689Skan int flags) 203950397Sobrien{ 204050397Sobrien int ref1, i; 204190075Sobrien int enum_p = 0; 204290075Sobrien tree type, argtypes[3]; 204390075Sobrien /* TYPES[i] is the set of possible builtin-operator parameter types 204490075Sobrien we will consider for the Ith argument. These are represented as 204590075Sobrien a TREE_LIST; the TREE_VALUE of each node is the potential 204690075Sobrien parameter type. */ 204790075Sobrien tree types[2]; 204850397Sobrien 204950397Sobrien for (i = 0; i < 3; ++i) 205050397Sobrien { 205150397Sobrien if (args[i]) 205250397Sobrien argtypes[i] = lvalue_type (args[i]); 205350397Sobrien else 205450397Sobrien argtypes[i] = NULL_TREE; 205550397Sobrien } 205650397Sobrien 205750397Sobrien switch (code) 205850397Sobrien { 205950397Sobrien/* 4 For every pair T, VQ), where T is an arithmetic or enumeration type, 206050397Sobrien and VQ is either volatile or empty, there exist candidate operator 206150397Sobrien functions of the form 206250397Sobrien VQ T& operator++(VQ T&); */ 206350397Sobrien 206450397Sobrien case POSTINCREMENT_EXPR: 206550397Sobrien case PREINCREMENT_EXPR: 206650397Sobrien case POSTDECREMENT_EXPR: 206750397Sobrien case PREDECREMENT_EXPR: 206850397Sobrien case MODIFY_EXPR: 206950397Sobrien ref1 = 1; 207050397Sobrien break; 207150397Sobrien 207250397Sobrien/* 24There also exist candidate operator functions of the form 207350397Sobrien bool operator!(bool); 207450397Sobrien bool operator&&(bool, bool); 207550397Sobrien bool operator||(bool, bool); */ 207650397Sobrien 207750397Sobrien case TRUTH_NOT_EXPR: 2078117395Skan build_builtin_candidate 207950397Sobrien (candidates, fnname, boolean_type_node, 208050397Sobrien NULL_TREE, args, argtypes, flags); 2081117395Skan return; 208250397Sobrien 208350397Sobrien case TRUTH_ORIF_EXPR: 208450397Sobrien case TRUTH_ANDIF_EXPR: 2085117395Skan build_builtin_candidate 208650397Sobrien (candidates, fnname, boolean_type_node, 208750397Sobrien boolean_type_node, args, argtypes, flags); 2088117395Skan return; 208950397Sobrien 209050397Sobrien case ADDR_EXPR: 209150397Sobrien case COMPOUND_EXPR: 209250397Sobrien case COMPONENT_REF: 2093117395Skan return; 209450397Sobrien 209590075Sobrien case COND_EXPR: 209690075Sobrien case EQ_EXPR: 209790075Sobrien case NE_EXPR: 209890075Sobrien case LT_EXPR: 209990075Sobrien case LE_EXPR: 210090075Sobrien case GT_EXPR: 210190075Sobrien case GE_EXPR: 210290075Sobrien enum_p = 1; 2103132718Skan /* Fall through. */ 2104169689Skan 210550397Sobrien default: 210650397Sobrien ref1 = 0; 210750397Sobrien } 210850397Sobrien 210950397Sobrien types[0] = types[1] = NULL_TREE; 211050397Sobrien 211150397Sobrien for (i = 0; i < 2; ++i) 211250397Sobrien { 211350397Sobrien if (! args[i]) 211450397Sobrien ; 211550397Sobrien else if (IS_AGGR_TYPE (argtypes[i])) 211618334Speter { 211752284Sobrien tree convs; 211850397Sobrien 211950397Sobrien if (i == 0 && code == MODIFY_EXPR && code2 == NOP_EXPR) 2120117395Skan return; 212150397Sobrien 212250397Sobrien convs = lookup_conversions (argtypes[i]); 212350397Sobrien 212450397Sobrien if (code == COND_EXPR) 212518334Speter { 212650397Sobrien if (real_lvalue_p (args[i])) 212790075Sobrien types[i] = tree_cons 212850397Sobrien (NULL_TREE, build_reference_type (argtypes[i]), types[i]); 212950397Sobrien 213090075Sobrien types[i] = tree_cons 213150397Sobrien (NULL_TREE, TYPE_MAIN_VARIANT (argtypes[i]), types[i]); 213218334Speter } 213350397Sobrien 213450397Sobrien else if (! convs) 2135117395Skan return; 213650397Sobrien 213750397Sobrien for (; convs; convs = TREE_CHAIN (convs)) 213818334Speter { 213952284Sobrien type = TREE_TYPE (TREE_TYPE (OVL_CURRENT (TREE_VALUE (convs)))); 214050397Sobrien 214150397Sobrien if (i == 0 && ref1 214250397Sobrien && (TREE_CODE (type) != REFERENCE_TYPE 214352284Sobrien || CP_TYPE_CONST_P (TREE_TYPE (type)))) 214450397Sobrien continue; 214550397Sobrien 214650397Sobrien if (code == COND_EXPR && TREE_CODE (type) == REFERENCE_TYPE) 214790075Sobrien types[i] = tree_cons (NULL_TREE, type, types[i]); 214850397Sobrien 214950397Sobrien type = non_reference (type); 215050397Sobrien if (i != 0 || ! ref1) 215150397Sobrien { 215250397Sobrien type = TYPE_MAIN_VARIANT (type_decays_to (type)); 2153169689Skan if (enum_p && TREE_CODE (type) == ENUMERAL_TYPE) 2154169689Skan types[i] = tree_cons (NULL_TREE, type, types[i]); 215550397Sobrien if (INTEGRAL_TYPE_P (type)) 215650397Sobrien type = type_promotes_to (type); 215750397Sobrien } 215850397Sobrien 215950397Sobrien if (! value_member (type, types[i])) 216090075Sobrien types[i] = tree_cons (NULL_TREE, type, types[i]); 216118334Speter } 216218334Speter } 216350397Sobrien else 216450397Sobrien { 216550397Sobrien if (code == COND_EXPR && real_lvalue_p (args[i])) 216690075Sobrien types[i] = tree_cons 216750397Sobrien (NULL_TREE, build_reference_type (argtypes[i]), types[i]); 216850397Sobrien type = non_reference (argtypes[i]); 216950397Sobrien if (i != 0 || ! ref1) 217050397Sobrien { 217150397Sobrien type = TYPE_MAIN_VARIANT (type_decays_to (type)); 217290075Sobrien if (enum_p && TREE_CODE (type) == ENUMERAL_TYPE) 2173169689Skan types[i] = tree_cons (NULL_TREE, type, types[i]); 217450397Sobrien if (INTEGRAL_TYPE_P (type)) 217550397Sobrien type = type_promotes_to (type); 217650397Sobrien } 217790075Sobrien types[i] = tree_cons (NULL_TREE, type, types[i]); 217850397Sobrien } 217918334Speter } 218018334Speter 218190075Sobrien /* Run through the possible parameter types of both arguments, 218290075Sobrien creating candidates with those parameter types. */ 218350397Sobrien for (; types[0]; types[0] = TREE_CHAIN (types[0])) 218418334Speter { 218550397Sobrien if (types[1]) 218650397Sobrien for (type = types[1]; type; type = TREE_CHAIN (type)) 2187117395Skan add_builtin_candidate 218850397Sobrien (candidates, code, code2, fnname, TREE_VALUE (types[0]), 218950397Sobrien TREE_VALUE (type), args, argtypes, flags); 219050397Sobrien else 2191117395Skan add_builtin_candidate 219250397Sobrien (candidates, code, code2, fnname, TREE_VALUE (types[0]), 219350397Sobrien NULL_TREE, args, argtypes, flags); 219418334Speter } 219550397Sobrien} 219618334Speter 219718334Speter 219850397Sobrien/* If TMPL can be successfully instantiated as indicated by 219950397Sobrien EXPLICIT_TARGS and ARGLIST, adds the instantiation to CANDIDATES. 220050397Sobrien 220150397Sobrien TMPL is the template. EXPLICIT_TARGS are any explicit template 220250397Sobrien arguments. ARGLIST is the arguments provided at the call-site. 220350397Sobrien The RETURN_TYPE is the desired type for conversion operators. If 220490075Sobrien OBJ is NULL_TREE, FLAGS and CTYPE are as for add_function_candidate. 220590075Sobrien If an OBJ is supplied, FLAGS and CTYPE are ignored, and OBJ is as for 220650397Sobrien add_conv_candidate. */ 220750397Sobrien 220850397Sobrienstatic struct z_candidate* 2209132718Skanadd_template_candidate_real (struct z_candidate **candidates, tree tmpl, 2210169689Skan tree ctype, tree explicit_targs, tree arglist, 2211169689Skan tree return_type, tree access_path, 2212132718Skan tree conversion_path, int flags, tree obj, 2213169689Skan unification_kind_t strict) 221450397Sobrien{ 221550397Sobrien int ntparms = DECL_NTPARMS (tmpl); 221690075Sobrien tree targs = make_tree_vec (ntparms); 221790075Sobrien tree args_without_in_chrg = arglist; 221850397Sobrien struct z_candidate *cand; 221950397Sobrien int i; 222050397Sobrien tree fn; 222150397Sobrien 222290075Sobrien /* We don't do deduction on the in-charge parameter, the VTT 222390075Sobrien parameter or 'this'. */ 222490075Sobrien if (DECL_NONSTATIC_MEMBER_FUNCTION_P (tmpl)) 222590075Sobrien args_without_in_chrg = TREE_CHAIN (args_without_in_chrg); 222650397Sobrien 222790075Sobrien if ((DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (tmpl) 222890075Sobrien || DECL_BASE_CONSTRUCTOR_P (tmpl)) 2229169689Skan && CLASSTYPE_VBASECLASSES (DECL_CONTEXT (tmpl))) 223090075Sobrien args_without_in_chrg = TREE_CHAIN (args_without_in_chrg); 223190075Sobrien 223290075Sobrien i = fn_type_unification (tmpl, explicit_targs, targs, 223390075Sobrien args_without_in_chrg, 2234169689Skan return_type, strict, flags); 223590075Sobrien 223650397Sobrien if (i != 0) 2237117395Skan return NULL; 223850397Sobrien 2239132718Skan fn = instantiate_template (tmpl, targs, tf_none); 224050397Sobrien if (fn == error_mark_node) 2241117395Skan return NULL; 224250397Sobrien 224396263Sobrien /* In [class.copy]: 224496263Sobrien 224596263Sobrien A member function template is never instantiated to perform the 2246169689Skan copy of a class object to an object of its class type. 224796263Sobrien 224896263Sobrien It's a little unclear what this means; the standard explicitly 224996263Sobrien does allow a template to be used to copy a class. For example, 225096263Sobrien in: 225196263Sobrien 225296263Sobrien struct A { 2253169689Skan A(A&); 225496263Sobrien template <class T> A(const T&); 225596263Sobrien }; 225696263Sobrien const A f (); 225796263Sobrien void g () { A a (f ()); } 2258169689Skan 225996263Sobrien the member template will be used to make the copy. The section 226096263Sobrien quoted above appears in the paragraph that forbids constructors 226196263Sobrien whose only parameter is (a possibly cv-qualified variant of) the 226296263Sobrien class type, and a logical interpretation is that the intent was 226396263Sobrien to forbid the instantiation of member templates which would then 226496263Sobrien have that form. */ 2265169689Skan if (DECL_CONSTRUCTOR_P (fn) && list_length (arglist) == 2) 226696263Sobrien { 226796263Sobrien tree arg_types = FUNCTION_FIRST_USER_PARMTYPE (fn); 226896263Sobrien if (arg_types && same_type_p (TYPE_MAIN_VARIANT (TREE_VALUE (arg_types)), 226996263Sobrien ctype)) 2270117395Skan return NULL; 227196263Sobrien } 227296263Sobrien 227350397Sobrien if (obj != NULL_TREE) 227450397Sobrien /* Aha, this is a conversion function. */ 2275117395Skan cand = add_conv_candidate (candidates, fn, obj, access_path, 2276117395Skan conversion_path, arglist); 227750397Sobrien else 227890075Sobrien cand = add_function_candidate (candidates, fn, ctype, 2279169689Skan arglist, access_path, 2280117395Skan conversion_path, flags); 228150397Sobrien if (DECL_TI_TEMPLATE (fn) != tmpl) 228250397Sobrien /* This situation can occur if a member template of a template 228350397Sobrien class is specialized. Then, instantiate_template might return 228450397Sobrien an instantiation of the specialization, in which case the 228550397Sobrien DECL_TI_TEMPLATE field will point at the original 228650397Sobrien specialization. For example: 228750397Sobrien 228850397Sobrien template <class T> struct S { template <class U> void f(U); 228950397Sobrien template <> void f(int) {}; }; 229050397Sobrien S<double> sd; 229150397Sobrien sd.f(3); 229250397Sobrien 229350397Sobrien Here, TMPL will be template <class U> S<double>::f(U). 229450397Sobrien And, instantiate template will give us the specialization 229550397Sobrien template <> S<double>::f(int). But, the DECL_TI_TEMPLATE field 229650397Sobrien for this will point at template <class T> template <> S<T>::f(int), 229750397Sobrien so that we can find the definition. For the purposes of 229850397Sobrien overload resolution, however, we want the original TMPL. */ 2299169689Skan cand->template_decl = tree_cons (tmpl, targs, NULL_TREE); 230050397Sobrien else 2301169689Skan cand->template_decl = DECL_TEMPLATE_INFO (fn); 230250397Sobrien 230350397Sobrien return cand; 230450397Sobrien} 230550397Sobrien 230650397Sobrien 230750397Sobrienstatic struct z_candidate * 2308132718Skanadd_template_candidate (struct z_candidate **candidates, tree tmpl, tree ctype, 2309169689Skan tree explicit_targs, tree arglist, tree return_type, 2310169689Skan tree access_path, tree conversion_path, int flags, 2311169689Skan unification_kind_t strict) 231250397Sobrien{ 2313169689Skan return 231490075Sobrien add_template_candidate_real (candidates, tmpl, ctype, 2315169689Skan explicit_targs, arglist, return_type, 2316117395Skan access_path, conversion_path, 2317117395Skan flags, NULL_TREE, strict); 231850397Sobrien} 231950397Sobrien 232050397Sobrien 232150397Sobrienstatic struct z_candidate * 2322132718Skanadd_template_conv_candidate (struct z_candidate **candidates, tree tmpl, 2323169689Skan tree obj, tree arglist, tree return_type, 2324132718Skan tree access_path, tree conversion_path) 232550397Sobrien{ 2326169689Skan return 232790075Sobrien add_template_candidate_real (candidates, tmpl, NULL_TREE, NULL_TREE, 2328117395Skan arglist, return_type, access_path, 2329117395Skan conversion_path, 0, obj, DEDUCE_CONV); 233050397Sobrien} 233150397Sobrien 2332132718Skan/* The CANDS are the set of candidates that were considered for 2333132718Skan overload resolution. Return the set of viable candidates. If none 2334132718Skan of the candidates were viable, set *ANY_VIABLE_P to true. STRICT_P 2335132718Skan is true if a candidate should be considered viable only if it is 2336132718Skan strictly viable. */ 233750397Sobrien 2338132718Skanstatic struct z_candidate* 2339132718Skansplice_viable (struct z_candidate *cands, 2340132718Skan bool strict_p, 2341132718Skan bool *any_viable_p) 234250397Sobrien{ 2343132718Skan struct z_candidate *viable; 2344132718Skan struct z_candidate **last_viable; 2345132718Skan struct z_candidate **cand; 234650397Sobrien 2347132718Skan viable = NULL; 2348132718Skan last_viable = &viable; 2349132718Skan *any_viable_p = false; 2350117395Skan 2351169689Skan cand = &cands; 2352169689Skan while (*cand) 235318334Speter { 2354132718Skan struct z_candidate *c = *cand; 2355132718Skan if (strict_p ? c->viable == 1 : c->viable) 2356132718Skan { 2357132718Skan *last_viable = c; 2358132718Skan *cand = c->next; 2359132718Skan c->next = NULL; 2360132718Skan last_viable = &c->next; 2361132718Skan *any_viable_p = true; 2362132718Skan } 236350397Sobrien else 2364132718Skan cand = &c->next; 236550397Sobrien } 236650397Sobrien 2367132718Skan return viable ? viable : cands; 236850397Sobrien} 236950397Sobrien 2370132718Skanstatic bool 2371132718Skanany_strictly_viable (struct z_candidate *cands) 2372132718Skan{ 2373132718Skan for (; cands; cands = cands->next) 2374132718Skan if (cands->viable == 1) 2375132718Skan return true; 2376132718Skan return false; 2377132718Skan} 2378132718Skan 2379146895Skan/* OBJ is being used in an expression like "OBJ.f (...)". In other 2380146895Skan words, it is about to become the "this" pointer for a member 2381146895Skan function call. Take the address of the object. */ 2382146895Skan 238350397Sobrienstatic tree 2384132718Skanbuild_this (tree obj) 238550397Sobrien{ 2386146895Skan /* In a template, we are only concerned about the type of the 2387146895Skan expression, so we can take a shortcut. */ 2388146895Skan if (processing_template_decl) 2389146895Skan return build_address (obj); 2390146895Skan 239190075Sobrien return build_unary_op (ADDR_EXPR, obj, 0); 239250397Sobrien} 239350397Sobrien 2394117395Skan/* Returns true iff functions are equivalent. Equivalent functions are 2395117395Skan not '==' only if one is a function-local extern function or if 2396117395Skan both are extern "C". */ 2397117395Skan 2398117395Skanstatic inline int 2399132718Skanequal_functions (tree fn1, tree fn2) 2400117395Skan{ 2401117395Skan if (DECL_LOCAL_FUNCTION_P (fn1) || DECL_LOCAL_FUNCTION_P (fn2) 2402117395Skan || DECL_EXTERN_C_FUNCTION_P (fn1)) 2403117395Skan return decls_match (fn1, fn2); 2404117395Skan return fn1 == fn2; 2405117395Skan} 2406117395Skan 2407132718Skan/* Print information about one overload candidate CANDIDATE. MSGSTR 2408132718Skan is the text to print before the candidate itself. 2409132718Skan 2410132718Skan NOTE: Unlike most diagnostic functions in GCC, MSGSTR is expected 2411132718Skan to have been run through gettext by the caller. This wart makes 2412132718Skan life simpler in print_z_candidates and for the translators. */ 2413132718Skan 241450397Sobrienstatic void 2415132718Skanprint_z_candidate (const char *msgstr, struct z_candidate *candidate) 2416132718Skan{ 2417132718Skan if (TREE_CODE (candidate->fn) == IDENTIFIER_NODE) 2418132718Skan { 2419169689Skan if (candidate->num_convs == 3) 2420132718Skan inform ("%s %D(%T, %T, %T) <built-in>", msgstr, candidate->fn, 2421169689Skan candidate->convs[0]->type, 2422169689Skan candidate->convs[1]->type, 2423169689Skan candidate->convs[2]->type); 2424169689Skan else if (candidate->num_convs == 2) 2425132718Skan inform ("%s %D(%T, %T) <built-in>", msgstr, candidate->fn, 2426169689Skan candidate->convs[0]->type, 2427169689Skan candidate->convs[1]->type); 2428132718Skan else 2429132718Skan inform ("%s %D(%T) <built-in>", msgstr, candidate->fn, 2430169689Skan candidate->convs[0]->type); 2431132718Skan } 2432132718Skan else if (TYPE_P (candidate->fn)) 2433132718Skan inform ("%s %T <conversion>", msgstr, candidate->fn); 2434132718Skan else if (candidate->viable == -1) 2435169689Skan inform ("%s %+#D <near match>", msgstr, candidate->fn); 2436132718Skan else 2437169689Skan inform ("%s %+#D", msgstr, candidate->fn); 2438132718Skan} 2439132718Skan 2440132718Skanstatic void 2441117395Skanprint_z_candidates (struct z_candidate *candidates) 244250397Sobrien{ 2443117395Skan const char *str; 2444117395Skan struct z_candidate *cand1; 2445117395Skan struct z_candidate **cand2; 2446117395Skan 2447117395Skan /* There may be duplicates in the set of candidates. We put off 2448117395Skan checking this condition as long as possible, since we have no way 2449117395Skan to eliminate duplicates from a set of functions in less than n^2 2450117395Skan time. Now we are about to emit an error message, so it is more 2451117395Skan permissible to go slowly. */ 2452117395Skan for (cand1 = candidates; cand1; cand1 = cand1->next) 2453117395Skan { 2454117395Skan tree fn = cand1->fn; 2455117395Skan /* Skip builtin candidates and conversion functions. */ 2456117395Skan if (TREE_CODE (fn) != FUNCTION_DECL) 2457117395Skan continue; 2458117395Skan cand2 = &cand1->next; 2459117395Skan while (*cand2) 2460117395Skan { 2461117395Skan if (TREE_CODE ((*cand2)->fn) == FUNCTION_DECL 2462117395Skan && equal_functions (fn, (*cand2)->fn)) 2463117395Skan *cand2 = (*cand2)->next; 2464117395Skan else 2465117395Skan cand2 = &(*cand2)->next; 2466117395Skan } 2467117395Skan } 2468117395Skan 2469132718Skan if (!candidates) 2470132718Skan return; 2471132718Skan 2472132718Skan str = _("candidates are:"); 2473132718Skan print_z_candidate (str, candidates); 2474132718Skan if (candidates->next) 247550397Sobrien { 2476132718Skan /* Indent successive candidates by the width of the translation 2477132718Skan of the above string. */ 2478132718Skan size_t len = gcc_gettext_width (str) + 1; 2479169689Skan char *spaces = (char *) alloca (len); 2480132718Skan memset (spaces, ' ', len-1); 2481132718Skan spaces[len - 1] = '\0'; 2482132718Skan 2483132718Skan candidates = candidates->next; 2484132718Skan do 248518334Speter { 2486132718Skan print_z_candidate (spaces, candidates); 2487132718Skan candidates = candidates->next; 248818334Speter } 2489132718Skan while (candidates); 249050397Sobrien } 249150397Sobrien} 249250397Sobrien 2493117395Skan/* USER_SEQ is a user-defined conversion sequence, beginning with a 2494117395Skan USER_CONV. STD_SEQ is the standard conversion sequence applied to 2495117395Skan the result of the conversion function to convert it to the final 2496169689Skan desired type. Merge the two sequences into a single sequence, 2497117395Skan and return the merged sequence. */ 2498117395Skan 2499169689Skanstatic conversion * 2500169689Skanmerge_conversion_sequences (conversion *user_seq, conversion *std_seq) 2501117395Skan{ 2502169689Skan conversion **t; 2503117395Skan 2504169689Skan gcc_assert (user_seq->kind == ck_user); 2505117395Skan 2506117395Skan /* Find the end of the second conversion sequence. */ 2507169689Skan t = &(std_seq); 2508169689Skan while ((*t)->kind != ck_identity) 2509169689Skan t = &((*t)->u.next); 2510117395Skan 2511117395Skan /* Replace the identity conversion with the user conversion 2512117395Skan sequence. */ 2513117395Skan *t = user_seq; 2514117395Skan 2515117395Skan /* The entire sequence is a user-conversion sequence. */ 2516169689Skan std_seq->user_conv_p = true; 2517117395Skan 2518117395Skan return std_seq; 2519117395Skan} 2520117395Skan 252150397Sobrien/* Returns the best overload candidate to perform the requested 252250397Sobrien conversion. This function is used for three the overloading situations 252350397Sobrien described in [over.match.copy], [over.match.conv], and [over.match.ref]. 252450397Sobrien If TOTYPE is a REFERENCE_TYPE, we're trying to find an lvalue binding as 252550397Sobrien per [dcl.init.ref], so we ignore temporary bindings. */ 252650397Sobrien 252750397Sobrienstatic struct z_candidate * 2528132718Skanbuild_user_type_conversion_1 (tree totype, tree expr, int flags) 252950397Sobrien{ 253050397Sobrien struct z_candidate *candidates, *cand; 253150397Sobrien tree fromtype = TREE_TYPE (expr); 2532169689Skan tree ctors = NULL_TREE; 2533169689Skan tree conv_fns = NULL_TREE; 2534169689Skan conversion *conv = NULL; 253550397Sobrien tree args = NULL_TREE; 2536132718Skan bool any_viable_p; 253750397Sobrien 253890075Sobrien /* We represent conversion within a hierarchy using RVALUE_CONV and 253990075Sobrien BASE_CONV, as specified by [over.best.ics]; these become plain 254090075Sobrien constructor calls, as specified in [dcl.init]. */ 2541169689Skan gcc_assert (!IS_AGGR_TYPE (fromtype) || !IS_AGGR_TYPE (totype) 2542169689Skan || !DERIVED_FROM_P (totype, fromtype)); 254390075Sobrien 254450397Sobrien if (IS_AGGR_TYPE (totype)) 2545169689Skan ctors = lookup_fnfields (totype, complete_ctor_identifier, 0); 254690075Sobrien 254790075Sobrien if (IS_AGGR_TYPE (fromtype)) 2548169689Skan conv_fns = lookup_conversions (fromtype); 254950397Sobrien 255050397Sobrien candidates = 0; 255150397Sobrien flags |= LOOKUP_NO_CONVERSION; 255250397Sobrien 255350397Sobrien if (ctors) 255450397Sobrien { 255590075Sobrien tree t; 255650397Sobrien 2557117395Skan ctors = BASELINK_FUNCTIONS (ctors); 255890075Sobrien 2559169689Skan t = build_int_cst (build_pointer_type (totype), 0); 256090075Sobrien args = build_tree_list (NULL_TREE, expr); 256190075Sobrien /* We should never try to call the abstract or base constructor 256290075Sobrien from here. */ 2563169689Skan gcc_assert (!DECL_HAS_IN_CHARGE_PARM_P (OVL_CURRENT (ctors)) 2564169689Skan && !DECL_HAS_VTT_PARM_P (OVL_CURRENT (ctors))); 256590075Sobrien args = tree_cons (NULL_TREE, t, args); 256650397Sobrien } 256750397Sobrien for (; ctors; ctors = OVL_NEXT (ctors)) 256850397Sobrien { 256950397Sobrien tree ctor = OVL_CURRENT (ctors); 257050397Sobrien if (DECL_NONCONVERTING_P (ctor)) 257150397Sobrien continue; 257250397Sobrien 2573169689Skan if (TREE_CODE (ctor) == TEMPLATE_DECL) 2574117395Skan cand = add_template_candidate (&candidates, ctor, totype, 2575169689Skan NULL_TREE, args, NULL_TREE, 2576117395Skan TYPE_BINFO (totype), 2577117395Skan TYPE_BINFO (totype), 2578117395Skan flags, 2579117395Skan DEDUCE_CALL); 2580169689Skan else 2581117395Skan cand = add_function_candidate (&candidates, ctor, totype, 2582169689Skan args, TYPE_BINFO (totype), 2583117395Skan TYPE_BINFO (totype), 2584169689Skan flags); 258550397Sobrien 2586117395Skan if (cand) 2587169689Skan cand->second_conv = build_identity_conv (totype, NULL_TREE); 258850397Sobrien } 258950397Sobrien 2590169689Skan if (conv_fns) 259190075Sobrien args = build_tree_list (NULL_TREE, build_this (expr)); 259250397Sobrien 2593169689Skan for (; conv_fns; conv_fns = TREE_CHAIN (conv_fns)) 259450397Sobrien { 2595117395Skan tree fns; 2596169689Skan tree conversion_path = TREE_PURPOSE (conv_fns); 259750397Sobrien int convflags = LOOKUP_NO_CONVERSION; 259850397Sobrien 259950397Sobrien /* If we are called to convert to a reference type, we are trying to 260050397Sobrien find an lvalue binding, so don't even consider temporaries. If 260150397Sobrien we don't find an lvalue binding, the caller will try again to 260250397Sobrien look for a temporary binding. */ 260350397Sobrien if (TREE_CODE (totype) == REFERENCE_TYPE) 260450397Sobrien convflags |= LOOKUP_NO_TEMP_BIND; 2605169689Skan 2606169689Skan for (fns = TREE_VALUE (conv_fns); fns; fns = OVL_NEXT (fns)) 2607117395Skan { 2608117395Skan tree fn = OVL_CURRENT (fns); 2609169689Skan 2610117395Skan /* [over.match.funcs] For conversion functions, the function 2611117395Skan is considered to be a member of the class of the implicit 2612117395Skan object argument for the purpose of defining the type of 2613117395Skan the implicit object parameter. 261450397Sobrien 2615117395Skan So we pass fromtype as CTYPE to add_*_candidate. */ 261650397Sobrien 2617117395Skan if (TREE_CODE (fn) == TEMPLATE_DECL) 2618169689Skan cand = add_template_candidate (&candidates, fn, fromtype, 2619132718Skan NULL_TREE, 2620169689Skan args, totype, 2621169689Skan TYPE_BINFO (fromtype), 2622117395Skan conversion_path, 2623117395Skan flags, 2624117395Skan DEDUCE_CONV); 2625169689Skan else 2626117395Skan cand = add_function_candidate (&candidates, fn, fromtype, 2627117395Skan args, 2628117395Skan TYPE_BINFO (fromtype), 2629117395Skan conversion_path, 2630169689Skan flags); 263150397Sobrien 2632117395Skan if (cand) 2633117395Skan { 2634169689Skan conversion *ics 2635169689Skan = implicit_conversion (totype, 2636169689Skan TREE_TYPE (TREE_TYPE (cand->fn)), 2637169689Skan 0, 2638169689Skan /*c_cast_p=*/false, convflags); 263990075Sobrien 2640117395Skan cand->second_conv = ics; 2641169689Skan 2642169689Skan if (!ics) 2643117395Skan cand->viable = 0; 2644169689Skan else if (candidates->viable == 1 && ics->bad_p) 2645117395Skan cand->viable = -1; 2646117395Skan } 2647117395Skan } 264850397Sobrien } 264950397Sobrien 2650132718Skan candidates = splice_viable (candidates, pedantic, &any_viable_p); 2651132718Skan if (!any_viable_p) 2652169689Skan return NULL; 265350397Sobrien 265450397Sobrien cand = tourney (candidates); 265550397Sobrien if (cand == 0) 265650397Sobrien { 265750397Sobrien if (flags & LOOKUP_COMPLAIN) 265850397Sobrien { 2659169689Skan error ("conversion from %qT to %qT is ambiguous", 266050397Sobrien fromtype, totype); 266150397Sobrien print_z_candidates (candidates); 266250397Sobrien } 266350397Sobrien 266450397Sobrien cand = candidates; /* any one will do */ 2665169689Skan cand->second_conv = build_ambiguous_conv (totype, expr); 2666169689Skan cand->second_conv->user_conv_p = true; 2667117395Skan if (!any_strictly_viable (candidates)) 2668169689Skan cand->second_conv->bad_p = true; 2669117395Skan /* If there are viable candidates, don't set ICS_BAD_FLAG; an 2670117395Skan ambiguous conversion is no worse than another user-defined 2671117395Skan conversion. */ 267250397Sobrien 267350397Sobrien return cand; 267450397Sobrien } 267550397Sobrien 2676117395Skan /* Build the user conversion sequence. */ 2677169689Skan conv = build_conv 2678169689Skan (ck_user, 267950397Sobrien (DECL_CONSTRUCTOR_P (cand->fn) 268050397Sobrien ? totype : non_reference (TREE_TYPE (TREE_TYPE (cand->fn)))), 2681169689Skan build_identity_conv (TREE_TYPE (expr), expr)); 2682169689Skan conv->cand = cand; 2683117395Skan 2684117395Skan /* Combine it with the second conversion sequence. */ 2685169689Skan cand->second_conv = merge_conversion_sequences (conv, 2686117395Skan cand->second_conv); 2687117395Skan 268850397Sobrien if (cand->viable == -1) 2689169689Skan cand->second_conv->bad_p = true; 269050397Sobrien 269150397Sobrien return cand; 269250397Sobrien} 269350397Sobrien 269450397Sobrientree 2695132718Skanbuild_user_type_conversion (tree totype, tree expr, int flags) 269650397Sobrien{ 269750397Sobrien struct z_candidate *cand 269850397Sobrien = build_user_type_conversion_1 (totype, expr, flags); 269950397Sobrien 270050397Sobrien if (cand) 270150397Sobrien { 2702169689Skan if (cand->second_conv->kind == ck_ambig) 270318334Speter return error_mark_node; 2704169689Skan expr = convert_like (cand->second_conv, expr); 2705169689Skan return convert_from_reference (expr); 270618334Speter } 270750397Sobrien return NULL_TREE; 270850397Sobrien} 270918334Speter 271050397Sobrien/* Do any initial processing on the arguments to a function call. */ 271150397Sobrien 271250397Sobrienstatic tree 2713132718Skanresolve_args (tree args) 271450397Sobrien{ 271550397Sobrien tree t; 271650397Sobrien for (t = args; t; t = TREE_CHAIN (t)) 271718334Speter { 271890075Sobrien tree arg = TREE_VALUE (t); 2719169689Skan 2720161651Skan if (error_operand_p (arg)) 272150397Sobrien return error_mark_node; 272290075Sobrien else if (VOID_TYPE_P (TREE_TYPE (arg))) 272350397Sobrien { 272450397Sobrien error ("invalid use of void expression"); 272550397Sobrien return error_mark_node; 272650397Sobrien } 2727169689Skan else if (invalid_nonstatic_memfn_p (arg)) 2728169689Skan return error_mark_node; 272918334Speter } 273050397Sobrien return args; 273150397Sobrien} 2732117395Skan 2733132718Skan/* Perform overload resolution on FN, which is called with the ARGS. 2734132718Skan 2735132718Skan Return the candidate function selected by overload resolution, or 2736132718Skan NULL if the event that overload resolution failed. In the case 2737132718Skan that overload resolution fails, *CANDIDATES will be the set of 2738132718Skan candidates considered, and ANY_VIABLE_P will be set to true or 2739132718Skan false to indicate whether or not any of the candidates were 2740169689Skan viable. 2741132718Skan 2742132718Skan The ARGS should already have gone through RESOLVE_ARGS before this 2743132718Skan function is called. */ 2744132718Skan 2745132718Skanstatic struct z_candidate * 2746169689Skanperform_overload_resolution (tree fn, 2747169689Skan tree args, 2748132718Skan struct z_candidate **candidates, 2749132718Skan bool *any_viable_p) 275050397Sobrien{ 2751132718Skan struct z_candidate *cand; 275250397Sobrien tree explicit_targs = NULL_TREE; 275350397Sobrien int template_only = 0; 275450397Sobrien 2755132718Skan *candidates = NULL; 2756132718Skan *any_viable_p = true; 2757132718Skan 2758117395Skan /* Check FN and ARGS. */ 2759169689Skan gcc_assert (TREE_CODE (fn) == FUNCTION_DECL 2760169689Skan || TREE_CODE (fn) == TEMPLATE_DECL 2761169689Skan || TREE_CODE (fn) == OVERLOAD 2762169689Skan || TREE_CODE (fn) == TEMPLATE_ID_EXPR); 2763169689Skan gcc_assert (!args || TREE_CODE (args) == TREE_LIST); 2764117395Skan 276550397Sobrien if (TREE_CODE (fn) == TEMPLATE_ID_EXPR) 276618334Speter { 276750397Sobrien explicit_targs = TREE_OPERAND (fn, 1); 276850397Sobrien fn = TREE_OPERAND (fn, 0); 276950397Sobrien template_only = 1; 277018334Speter } 277150397Sobrien 2772132718Skan /* Add the various candidate functions. */ 2773132718Skan add_candidates (fn, args, explicit_targs, template_only, 2774132718Skan /*conversion_path=*/NULL_TREE, 2775132718Skan /*access_path=*/NULL_TREE, 2776132718Skan LOOKUP_NORMAL, 2777132718Skan candidates); 2778132718Skan 2779132718Skan *candidates = splice_viable (*candidates, pedantic, any_viable_p); 2780132718Skan if (!*any_viable_p) 2781132718Skan return NULL; 2782132718Skan 2783132718Skan cand = tourney (*candidates); 2784132718Skan return cand; 2785132718Skan} 2786132718Skan 2787132718Skan/* Return an expression for a call to FN (a namespace-scope function, 2788132718Skan or a static member function) with the ARGS. */ 2789169689Skan 2790132718Skantree 2791169689Skanbuild_new_function_call (tree fn, tree args, bool koenig_p) 2792132718Skan{ 2793132718Skan struct z_candidate *candidates, *cand; 2794132718Skan bool any_viable_p; 2795169689Skan void *p; 2796169689Skan tree result; 2797132718Skan 2798132718Skan args = resolve_args (args); 2799132718Skan if (args == error_mark_node) 2800132718Skan return error_mark_node; 2801132718Skan 2802169689Skan /* If this function was found without using argument dependent 2803169689Skan lookup, then we want to ignore any undeclared friend 2804169689Skan functions. */ 2805169689Skan if (!koenig_p) 2806169689Skan { 2807169689Skan tree orig_fn = fn; 2808169689Skan 2809169689Skan fn = remove_hidden_names (fn); 2810169689Skan if (!fn) 2811169689Skan { 2812169689Skan error ("no matching function for call to %<%D(%A)%>", 2813169689Skan DECL_NAME (OVL_CURRENT (orig_fn)), args); 2814169689Skan return error_mark_node; 2815169689Skan } 2816169689Skan } 2817169689Skan 2818169689Skan /* Get the high-water mark for the CONVERSION_OBSTACK. */ 2819169689Skan p = conversion_obstack_alloc (0); 2820169689Skan 2821132718Skan cand = perform_overload_resolution (fn, args, &candidates, &any_viable_p); 2822132718Skan 2823132718Skan if (!cand) 282418334Speter { 2825132718Skan if (!any_viable_p && candidates && ! candidates->next) 2826132718Skan return build_function_call (candidates->fn, args); 2827132718Skan if (TREE_CODE (fn) == TEMPLATE_ID_EXPR) 2828132718Skan fn = TREE_OPERAND (fn, 0); 2829132718Skan if (!any_viable_p) 2830169689Skan error ("no matching function for call to %<%D(%A)%>", 2831132718Skan DECL_NAME (OVL_CURRENT (fn)), args); 2832132718Skan else 2833169689Skan error ("call of overloaded %<%D(%A)%> is ambiguous", 2834132718Skan DECL_NAME (OVL_CURRENT (fn)), args); 2835132718Skan if (candidates) 2836132718Skan print_z_candidates (candidates); 2837169689Skan result = error_mark_node; 2838132718Skan } 2839169689Skan else 2840169689Skan result = build_over_call (cand, LOOKUP_NORMAL); 284150397Sobrien 2842169689Skan /* Free all the conversions we allocated. */ 2843169689Skan obstack_free (&conversion_obstack, p); 2844169689Skan 2845169689Skan return result; 2846132718Skan} 284750397Sobrien 2848132718Skan/* Build a call to a global operator new. FNNAME is the name of the 2849132718Skan operator (either "operator new" or "operator new[]") and ARGS are 2850132718Skan the arguments provided. *SIZE points to the total number of bytes 2851132718Skan required by the allocation, and is updated if that is changed here. 2852132718Skan *COOKIE_SIZE is non-NULL if a cookie should be used. If this 2853132718Skan function determines that no cookie should be used, after all, 2854169689Skan *COOKIE_SIZE is set to NULL_TREE. If FN is non-NULL, it will be 2855169689Skan set, upon return, to the allocation function called. */ 285650397Sobrien 2857132718Skantree 2858169689Skanbuild_operator_new_call (tree fnname, tree args, 2859169689Skan tree *size, tree *cookie_size, 2860169689Skan tree *fn) 2861132718Skan{ 2862132718Skan tree fns; 2863132718Skan struct z_candidate *candidates; 2864132718Skan struct z_candidate *cand; 2865132718Skan bool any_viable_p; 286652284Sobrien 2867169689Skan if (fn) 2868169689Skan *fn = NULL_TREE; 2869132718Skan args = tree_cons (NULL_TREE, *size, args); 2870132718Skan args = resolve_args (args); 2871132718Skan if (args == error_mark_node) 2872132718Skan return args; 287318334Speter 2874169689Skan /* Based on: 287550397Sobrien 2876169689Skan [expr.new] 2877169689Skan 2878169689Skan If this lookup fails to find the name, or if the allocated type 2879169689Skan is not a class type, the allocation function's name is looked 2880169689Skan up in the global scope. 2881169689Skan 2882169689Skan we disregard block-scope declarations of "operator new". */ 2883169689Skan fns = lookup_function_nonclass (fnname, args, /*block_p=*/false); 2884169689Skan 2885132718Skan /* Figure out what function is being called. */ 2886132718Skan cand = perform_overload_resolution (fns, args, &candidates, &any_viable_p); 2887169689Skan 2888132718Skan /* If no suitable function could be found, issue an error message 2889132718Skan and give up. */ 2890132718Skan if (!cand) 2891132718Skan { 2892132718Skan if (!any_viable_p) 2893169689Skan error ("no matching function for call to %<%D(%A)%>", 2894132718Skan DECL_NAME (OVL_CURRENT (fns)), args); 2895132718Skan else 2896169689Skan error ("call of overloaded %<%D(%A)%> is ambiguous", 2897132718Skan DECL_NAME (OVL_CURRENT (fns)), args); 2898132718Skan if (candidates) 2899132718Skan print_z_candidates (candidates); 2900132718Skan return error_mark_node; 290118334Speter } 290218334Speter 2903132718Skan /* If a cookie is required, add some extra space. Whether 2904132718Skan or not a cookie is required cannot be determined until 2905132718Skan after we know which function was called. */ 2906132718Skan if (*cookie_size) 2907132718Skan { 2908132718Skan bool use_cookie = true; 2909132718Skan if (!abi_version_at_least (2)) 2910132718Skan { 2911132718Skan tree placement = TREE_CHAIN (args); 2912132718Skan /* In G++ 3.2, the check was implemented incorrectly; it 2913132718Skan looked at the placement expression, rather than the 2914132718Skan type of the function. */ 2915132718Skan if (placement && !TREE_CHAIN (placement) 2916132718Skan && same_type_p (TREE_TYPE (TREE_VALUE (placement)), 2917132718Skan ptr_type_node)) 2918132718Skan use_cookie = false; 2919132718Skan } 2920132718Skan else 2921132718Skan { 2922132718Skan tree arg_types; 292318334Speter 2924132718Skan arg_types = TYPE_ARG_TYPES (TREE_TYPE (cand->fn)); 2925132718Skan /* Skip the size_t parameter. */ 2926132718Skan arg_types = TREE_CHAIN (arg_types); 2927132718Skan /* Check the remaining parameters (if any). */ 2928169689Skan if (arg_types 2929132718Skan && TREE_CHAIN (arg_types) == void_list_node 2930132718Skan && same_type_p (TREE_VALUE (arg_types), 2931132718Skan ptr_type_node)) 2932132718Skan use_cookie = false; 2933132718Skan } 2934132718Skan /* If we need a cookie, adjust the number of bytes allocated. */ 2935132718Skan if (use_cookie) 2936132718Skan { 2937132718Skan /* Update the total size. */ 2938132718Skan *size = size_binop (PLUS_EXPR, *size, *cookie_size); 2939132718Skan /* Update the argument list to reflect the adjusted size. */ 2940132718Skan TREE_VALUE (args) = *size; 2941132718Skan } 2942132718Skan else 2943132718Skan *cookie_size = NULL_TREE; 2944132718Skan } 2945132718Skan 2946169689Skan /* Tell our caller which function we decided to call. */ 2947169689Skan if (fn) 2948169689Skan *fn = cand->fn; 2949169689Skan 2950132718Skan /* Build the CALL_EXPR. */ 2951132718Skan return build_over_call (cand, LOOKUP_NORMAL); 295250397Sobrien} 295318334Speter 295450397Sobrienstatic tree 2955132718Skanbuild_object_call (tree obj, tree args) 295650397Sobrien{ 295750397Sobrien struct z_candidate *candidates = 0, *cand; 295850397Sobrien tree fns, convs, mem_args = NULL_TREE; 295950397Sobrien tree type = TREE_TYPE (obj); 2960132718Skan bool any_viable_p; 2961169689Skan tree result = NULL_TREE; 2962169689Skan void *p; 296318334Speter 296450397Sobrien if (TYPE_PTRMEMFUNC_P (type)) 296518334Speter { 296650397Sobrien /* It's no good looking for an overloaded operator() on a 296750397Sobrien pointer-to-member-function. */ 296890075Sobrien error ("pointer-to-member function %E cannot be called without an object; consider using .* or ->*", obj); 296950397Sobrien return error_mark_node; 297050397Sobrien } 297150397Sobrien 2972169689Skan if (TYPE_BINFO (type)) 2973169689Skan { 2974169689Skan fns = lookup_fnfields (TYPE_BINFO (type), ansi_opname (CALL_EXPR), 1); 2975169689Skan if (fns == error_mark_node) 2976169689Skan return error_mark_node; 2977169689Skan } 2978169689Skan else 2979169689Skan fns = NULL_TREE; 298050397Sobrien 298150397Sobrien args = resolve_args (args); 298250397Sobrien 298350397Sobrien if (args == error_mark_node) 298450397Sobrien return error_mark_node; 298550397Sobrien 2986169689Skan /* Get the high-water mark for the CONVERSION_OBSTACK. */ 2987169689Skan p = conversion_obstack_alloc (0); 2988169689Skan 298950397Sobrien if (fns) 299050397Sobrien { 2991117395Skan tree base = BINFO_TYPE (BASELINK_BINFO (fns)); 299290075Sobrien mem_args = tree_cons (NULL_TREE, build_this (obj), args); 299350397Sobrien 2994117395Skan for (fns = BASELINK_FUNCTIONS (fns); fns; fns = OVL_NEXT (fns)) 299518334Speter { 299650397Sobrien tree fn = OVL_CURRENT (fns); 299750397Sobrien if (TREE_CODE (fn) == TEMPLATE_DECL) 2998117395Skan add_template_candidate (&candidates, fn, base, NULL_TREE, 2999169689Skan mem_args, NULL_TREE, 3000117395Skan TYPE_BINFO (type), 3001117395Skan TYPE_BINFO (type), 3002117395Skan LOOKUP_NORMAL, DEDUCE_CALL); 300350397Sobrien else 3004117395Skan add_function_candidate 3005117395Skan (&candidates, fn, base, mem_args, TYPE_BINFO (type), 3006117395Skan TYPE_BINFO (type), LOOKUP_NORMAL); 300718334Speter } 300818334Speter } 300918334Speter 301050397Sobrien convs = lookup_conversions (type); 301118334Speter 301250397Sobrien for (; convs; convs = TREE_CHAIN (convs)) 301350397Sobrien { 301450397Sobrien tree fns = TREE_VALUE (convs); 301550397Sobrien tree totype = TREE_TYPE (TREE_TYPE (OVL_CURRENT (fns))); 301618334Speter 301752284Sobrien if ((TREE_CODE (totype) == POINTER_TYPE 301890075Sobrien && TREE_CODE (TREE_TYPE (totype)) == FUNCTION_TYPE) 301990075Sobrien || (TREE_CODE (totype) == REFERENCE_TYPE 302090075Sobrien && TREE_CODE (TREE_TYPE (totype)) == FUNCTION_TYPE) 302190075Sobrien || (TREE_CODE (totype) == REFERENCE_TYPE 302290075Sobrien && TREE_CODE (TREE_TYPE (totype)) == POINTER_TYPE 302390075Sobrien && TREE_CODE (TREE_TYPE (TREE_TYPE (totype))) == FUNCTION_TYPE)) 302452284Sobrien for (; fns; fns = OVL_NEXT (fns)) 302550397Sobrien { 302652284Sobrien tree fn = OVL_CURRENT (fns); 3027169689Skan if (TREE_CODE (fn) == TEMPLATE_DECL) 3028169689Skan add_template_conv_candidate 3029117395Skan (&candidates, fn, obj, args, totype, 3030117395Skan /*access_path=*/NULL_TREE, 3031117395Skan /*conversion_path=*/NULL_TREE); 303250397Sobrien else 3033117395Skan add_conv_candidate (&candidates, fn, obj, args, 3034117395Skan /*conversion_path=*/NULL_TREE, 3035117395Skan /*access_path=*/NULL_TREE); 303650397Sobrien } 303750397Sobrien } 303850397Sobrien 3039132718Skan candidates = splice_viable (candidates, pedantic, &any_viable_p); 3040132718Skan if (!any_viable_p) 304118334Speter { 3042169689Skan error ("no match for call to %<(%T) (%A)%>", TREE_TYPE (obj), args); 304350397Sobrien print_z_candidates (candidates); 3044169689Skan result = error_mark_node; 304550397Sobrien } 3046169689Skan else 304750397Sobrien { 3048169689Skan cand = tourney (candidates); 3049169689Skan if (cand == 0) 3050169689Skan { 3051169689Skan error ("call of %<(%T) (%A)%> is ambiguous", TREE_TYPE (obj), args); 3052169689Skan print_z_candidates (candidates); 3053169689Skan result = error_mark_node; 3054169689Skan } 3055169689Skan /* Since cand->fn will be a type, not a function, for a conversion 3056169689Skan function, we must be careful not to unconditionally look at 3057169689Skan DECL_NAME here. */ 3058169689Skan else if (TREE_CODE (cand->fn) == FUNCTION_DECL 3059169689Skan && DECL_OVERLOADED_OPERATOR_P (cand->fn) == CALL_EXPR) 3060169689Skan result = build_over_call (cand, LOOKUP_NORMAL); 3061169689Skan else 3062169689Skan { 3063169689Skan obj = convert_like_with_context (cand->convs[0], obj, cand->fn, -1); 3064169689Skan obj = convert_from_reference (obj); 3065169689Skan result = build_function_call (obj, args); 3066169689Skan } 306750397Sobrien } 306818334Speter 3069169689Skan /* Free all the conversions we allocated. */ 3070169689Skan obstack_free (&conversion_obstack, p); 307118334Speter 3072169689Skan return result; 307350397Sobrien} 307418334Speter 307550397Sobrienstatic void 3076132718Skanop_error (enum tree_code code, enum tree_code code2, 3077169689Skan tree arg1, tree arg2, tree arg3, const char *problem) 307850397Sobrien{ 307990075Sobrien const char *opname; 308018334Speter 308190075Sobrien if (code == MODIFY_EXPR) 308290075Sobrien opname = assignment_operator_name_info[code2].name; 308390075Sobrien else 308490075Sobrien opname = operator_name_info[code].name; 308590075Sobrien 308650397Sobrien switch (code) 308750397Sobrien { 308850397Sobrien case COND_EXPR: 3089169689Skan error ("%s for ternary %<operator?:%> in %<%E ? %E : %E%>", 3090169689Skan problem, arg1, arg2, arg3); 309150397Sobrien break; 3092169689Skan 309350397Sobrien case POSTINCREMENT_EXPR: 309450397Sobrien case POSTDECREMENT_EXPR: 3095169689Skan error ("%s for %<operator%s%> in %<%E%s%>", problem, opname, arg1, opname); 309650397Sobrien break; 3097169689Skan 309850397Sobrien case ARRAY_REF: 3099169689Skan error ("%s for %<operator[]%> in %<%E[%E]%>", problem, arg1, arg2); 310050397Sobrien break; 3101132718Skan 3102132718Skan case REALPART_EXPR: 3103132718Skan case IMAGPART_EXPR: 3104169689Skan error ("%s for %qs in %<%s %E%>", problem, opname, opname, arg1); 3105132718Skan break; 3106169689Skan 310750397Sobrien default: 310850397Sobrien if (arg2) 3109169689Skan error ("%s for %<operator%s%> in %<%E %s %E%>", 3110169689Skan problem, opname, arg1, opname, arg2); 311150397Sobrien else 3112169689Skan error ("%s for %<operator%s%> in %<%s%E%>", 3113169689Skan problem, opname, opname, arg1); 3114117395Skan break; 311550397Sobrien } 311650397Sobrien} 311718334Speter 311890075Sobrien/* Return the implicit conversion sequence that could be used to 311990075Sobrien convert E1 to E2 in [expr.cond]. */ 312090075Sobrien 3121169689Skanstatic conversion * 3122132718Skanconditional_conversion (tree e1, tree e2) 312390075Sobrien{ 312490075Sobrien tree t1 = non_reference (TREE_TYPE (e1)); 312590075Sobrien tree t2 = non_reference (TREE_TYPE (e2)); 3126169689Skan conversion *conv; 3127117395Skan bool good_base; 312890075Sobrien 312990075Sobrien /* [expr.cond] 313090075Sobrien 313190075Sobrien If E2 is an lvalue: E1 can be converted to match E2 if E1 can be 313290075Sobrien implicitly converted (clause _conv_) to the type "reference to 313390075Sobrien T2", subject to the constraint that in the conversion the 313490075Sobrien reference must bind directly (_dcl.init.ref_) to E1. */ 313590075Sobrien if (real_lvalue_p (e2)) 313690075Sobrien { 3137169689Skan conv = implicit_conversion (build_reference_type (t2), 313890075Sobrien t1, 313990075Sobrien e1, 3140169689Skan /*c_cast_p=*/false, 314190075Sobrien LOOKUP_NO_TEMP_BIND); 314290075Sobrien if (conv) 314390075Sobrien return conv; 314490075Sobrien } 314590075Sobrien 314690075Sobrien /* [expr.cond] 314790075Sobrien 314890075Sobrien If E1 and E2 have class type, and the underlying class types are 314990075Sobrien the same or one is a base class of the other: E1 can be converted 315090075Sobrien to match E2 if the class of T2 is the same type as, or a base 315190075Sobrien class of, the class of T1, and the cv-qualification of T2 is the 315290075Sobrien same cv-qualification as, or a greater cv-qualification than, the 315390075Sobrien cv-qualification of T1. If the conversion is applied, E1 is 315490075Sobrien changed to an rvalue of type T2 that still refers to the original 315590075Sobrien source class object (or the appropriate subobject thereof). */ 315690075Sobrien if (CLASS_TYPE_P (t1) && CLASS_TYPE_P (t2) 3157117395Skan && ((good_base = DERIVED_FROM_P (t2, t1)) || DERIVED_FROM_P (t1, t2))) 315890075Sobrien { 3159117395Skan if (good_base && at_least_as_qualified_p (t2, t1)) 316090075Sobrien { 3161169689Skan conv = build_identity_conv (t1, e1); 3162169689Skan if (!same_type_p (TYPE_MAIN_VARIANT (t1), 316390075Sobrien TYPE_MAIN_VARIANT (t2))) 3164169689Skan conv = build_conv (ck_base, t2, conv); 3165132718Skan else 3166169689Skan conv = build_conv (ck_rvalue, t2, conv); 316790075Sobrien return conv; 316890075Sobrien } 316990075Sobrien else 3170169689Skan return NULL; 317190075Sobrien } 3172117395Skan else 3173117395Skan /* [expr.cond] 317490075Sobrien 3175117395Skan Otherwise: E1 can be converted to match E2 if E1 can be implicitly 3176117395Skan converted to the type that expression E2 would have if E2 were 3177117395Skan converted to an rvalue (or the type it has, if E2 is an rvalue). */ 3178169689Skan return implicit_conversion (t2, t1, e1, /*c_cast_p=*/false, 3179169689Skan LOOKUP_NORMAL); 318090075Sobrien} 318190075Sobrien 318290075Sobrien/* Implement [expr.cond]. ARG1, ARG2, and ARG3 are the three 3183117395Skan arguments to the conditional expression. */ 318490075Sobrien 318550397Sobrientree 3186132718Skanbuild_conditional_expr (tree arg1, tree arg2, tree arg3) 318790075Sobrien{ 318890075Sobrien tree arg2_type; 318990075Sobrien tree arg3_type; 3190169689Skan tree result = NULL_TREE; 319190075Sobrien tree result_type = NULL_TREE; 3192132718Skan bool lvalue_p = true; 319390075Sobrien struct z_candidate *candidates = 0; 319490075Sobrien struct z_candidate *cand; 3195169689Skan void *p; 319690075Sobrien 319790075Sobrien /* As a G++ extension, the second argument to the conditional can be 319890075Sobrien omitted. (So that `a ? : c' is roughly equivalent to `a ? a : 319990075Sobrien c'.) If the second operand is omitted, make sure it is 320090075Sobrien calculated only once. */ 320190075Sobrien if (!arg2) 320290075Sobrien { 320390075Sobrien if (pedantic) 320490075Sobrien pedwarn ("ISO C++ forbids omitting the middle term of a ?: expression"); 3205132718Skan 3206132718Skan /* Make sure that lvalues remain lvalues. See g++.oliva/ext1.C. */ 3207132718Skan if (real_lvalue_p (arg1)) 3208132718Skan arg2 = arg1 = stabilize_reference (arg1); 3209132718Skan else 3210132718Skan arg2 = arg1 = save_expr (arg1); 321190075Sobrien } 321290075Sobrien 321390075Sobrien /* [expr.cond] 3214169689Skan 321590075Sobrien The first expr ession is implicitly converted to bool (clause 321690075Sobrien _conv_). */ 3217132718Skan arg1 = perform_implicit_conversion (boolean_type_node, arg1); 321890075Sobrien 321990075Sobrien /* If something has already gone wrong, just pass that fact up the 322090075Sobrien tree. */ 3221132718Skan if (error_operand_p (arg1) 3222132718Skan || error_operand_p (arg2) 3223132718Skan || error_operand_p (arg3)) 322490075Sobrien return error_mark_node; 322590075Sobrien 322690075Sobrien /* [expr.cond] 322790075Sobrien 322890075Sobrien If either the second or the third operand has type (possibly 322990075Sobrien cv-qualified) void, then the lvalue-to-rvalue (_conv.lval_), 323090075Sobrien array-to-pointer (_conv.array_), and function-to-pointer 323190075Sobrien (_conv.func_) standard conversions are performed on the second 323290075Sobrien and third operands. */ 3233169689Skan arg2_type = unlowered_expr_type (arg2); 3234169689Skan arg3_type = unlowered_expr_type (arg3); 323590075Sobrien if (VOID_TYPE_P (arg2_type) || VOID_TYPE_P (arg3_type)) 323690075Sobrien { 323790075Sobrien /* Do the conversions. We don't these for `void' type arguments 323890075Sobrien since it can't have any effect and since decay_conversion 323990075Sobrien does not handle that case gracefully. */ 324090075Sobrien if (!VOID_TYPE_P (arg2_type)) 324190075Sobrien arg2 = decay_conversion (arg2); 324290075Sobrien if (!VOID_TYPE_P (arg3_type)) 324390075Sobrien arg3 = decay_conversion (arg3); 324490075Sobrien arg2_type = TREE_TYPE (arg2); 324590075Sobrien arg3_type = TREE_TYPE (arg3); 324690075Sobrien 324790075Sobrien /* [expr.cond] 324890075Sobrien 324990075Sobrien One of the following shall hold: 325090075Sobrien 325190075Sobrien --The second or the third operand (but not both) is a 325290075Sobrien throw-expression (_except.throw_); the result is of the 325390075Sobrien type of the other and is an rvalue. 325490075Sobrien 325590075Sobrien --Both the second and the third operands have type void; the 3256169689Skan result is of type void and is an rvalue. 3257132718Skan 3258169689Skan We must avoid calling force_rvalue for expressions of type 3259132718Skan "void" because it will complain that their value is being 3260169689Skan used. */ 3261169689Skan if (TREE_CODE (arg2) == THROW_EXPR 3262132718Skan && TREE_CODE (arg3) != THROW_EXPR) 3263132718Skan { 3264132718Skan if (!VOID_TYPE_P (arg3_type)) 3265132718Skan arg3 = force_rvalue (arg3); 3266132718Skan arg3_type = TREE_TYPE (arg3); 3267132718Skan result_type = arg3_type; 3268132718Skan } 3269169689Skan else if (TREE_CODE (arg2) != THROW_EXPR 3270132718Skan && TREE_CODE (arg3) == THROW_EXPR) 3271132718Skan { 3272132718Skan if (!VOID_TYPE_P (arg2_type)) 3273132718Skan arg2 = force_rvalue (arg2); 3274132718Skan arg2_type = TREE_TYPE (arg2); 3275132718Skan result_type = arg2_type; 3276132718Skan } 327790075Sobrien else if (VOID_TYPE_P (arg2_type) && VOID_TYPE_P (arg3_type)) 327890075Sobrien result_type = void_type_node; 327990075Sobrien else 328090075Sobrien { 3281169689Skan error ("%qE has type %<void%> and is not a throw-expression", 328290075Sobrien VOID_TYPE_P (arg2_type) ? arg2 : arg3); 328390075Sobrien return error_mark_node; 328490075Sobrien } 328590075Sobrien 3286132718Skan lvalue_p = false; 328790075Sobrien goto valid_operands; 328890075Sobrien } 328990075Sobrien /* [expr.cond] 329090075Sobrien 329190075Sobrien Otherwise, if the second and third operand have different types, 329290075Sobrien and either has (possibly cv-qualified) class type, an attempt is 329390075Sobrien made to convert each of those operands to the type of the other. */ 329490075Sobrien else if (!same_type_p (arg2_type, arg3_type) 329590075Sobrien && (CLASS_TYPE_P (arg2_type) || CLASS_TYPE_P (arg3_type))) 329690075Sobrien { 3297169689Skan conversion *conv2; 3298169689Skan conversion *conv3; 3299169689Skan 3300169689Skan /* Get the high-water mark for the CONVERSION_OBSTACK. */ 3301169689Skan p = conversion_obstack_alloc (0); 3302169689Skan 3303169689Skan conv2 = conditional_conversion (arg2, arg3); 3304169689Skan conv3 = conditional_conversion (arg3, arg2); 3305169689Skan 330690075Sobrien /* [expr.cond] 330790075Sobrien 330890075Sobrien If both can be converted, or one can be converted but the 330990075Sobrien conversion is ambiguous, the program is ill-formed. If 331090075Sobrien neither can be converted, the operands are left unchanged and 331190075Sobrien further checking is performed as described below. If exactly 331290075Sobrien one conversion is possible, that conversion is applied to the 331390075Sobrien chosen operand and the converted operand is used in place of 331490075Sobrien the original operand for the remainder of this section. */ 3315169689Skan if ((conv2 && !conv2->bad_p 3316169689Skan && conv3 && !conv3->bad_p) 3317169689Skan || (conv2 && conv2->kind == ck_ambig) 3318169689Skan || (conv3 && conv3->kind == ck_ambig)) 331990075Sobrien { 3320169689Skan error ("operands to ?: have different types %qT and %qT", 3321169689Skan arg2_type, arg3_type); 3322169689Skan result = error_mark_node; 332390075Sobrien } 3324169689Skan else if (conv2 && (!conv2->bad_p || !conv3)) 332590075Sobrien { 332690075Sobrien arg2 = convert_like (conv2, arg2); 332790075Sobrien arg2 = convert_from_reference (arg2); 332890075Sobrien arg2_type = TREE_TYPE (arg2); 3329169689Skan /* Even if CONV2 is a valid conversion, the result of the 3330169689Skan conversion may be invalid. For example, if ARG3 has type 3331169689Skan "volatile X", and X does not have a copy constructor 3332169689Skan accepting a "volatile X&", then even if ARG2 can be 3333169689Skan converted to X, the conversion will fail. */ 3334169689Skan if (error_operand_p (arg2)) 3335169689Skan result = error_mark_node; 333690075Sobrien } 3337169689Skan else if (conv3 && (!conv3->bad_p || !conv2)) 333890075Sobrien { 333990075Sobrien arg3 = convert_like (conv3, arg3); 334090075Sobrien arg3 = convert_from_reference (arg3); 334190075Sobrien arg3_type = TREE_TYPE (arg3); 3342169689Skan if (error_operand_p (arg3)) 3343169689Skan result = error_mark_node; 334490075Sobrien } 3345132718Skan 3346169689Skan /* Free all the conversions we allocated. */ 3347169689Skan obstack_free (&conversion_obstack, p); 3348169689Skan 3349169689Skan if (result) 3350169689Skan return result; 3351169689Skan 3352132718Skan /* If, after the conversion, both operands have class type, 3353132718Skan treat the cv-qualification of both operands as if it were the 3354169689Skan union of the cv-qualification of the operands. 3355132718Skan 3356132718Skan The standard is not clear about what to do in this 3357132718Skan circumstance. For example, if the first operand has type 3358132718Skan "const X" and the second operand has a user-defined 3359132718Skan conversion to "volatile X", what is the type of the second 3360132718Skan operand after this step? Making it be "const X" (matching 3361132718Skan the first operand) seems wrong, as that discards the 3362169689Skan qualification without actually performing a copy. Leaving it 3363132718Skan as "volatile X" seems wrong as that will result in the 3364132718Skan conditional expression failing altogether, even though, 3365132718Skan according to this step, the one operand could be converted to 3366132718Skan the type of the other. */ 3367132718Skan if ((conv2 || conv3) 3368132718Skan && CLASS_TYPE_P (arg2_type) 3369132718Skan && TYPE_QUALS (arg2_type) != TYPE_QUALS (arg3_type)) 3370169689Skan arg2_type = arg3_type = 3371132718Skan cp_build_qualified_type (arg2_type, 3372132718Skan TYPE_QUALS (arg2_type) 3373132718Skan | TYPE_QUALS (arg3_type)); 337490075Sobrien } 337590075Sobrien 337690075Sobrien /* [expr.cond] 337790075Sobrien 337890075Sobrien If the second and third operands are lvalues and have the same 337990075Sobrien type, the result is of that type and is an lvalue. */ 3380169689Skan if (real_lvalue_p (arg2) 3381169689Skan && real_lvalue_p (arg3) 3382132718Skan && same_type_p (arg2_type, arg3_type)) 338390075Sobrien { 338490075Sobrien result_type = arg2_type; 338590075Sobrien goto valid_operands; 338690075Sobrien } 338790075Sobrien 338890075Sobrien /* [expr.cond] 338990075Sobrien 339090075Sobrien Otherwise, the result is an rvalue. If the second and third 339190075Sobrien operand do not have the same type, and either has (possibly 339290075Sobrien cv-qualified) class type, overload resolution is used to 339390075Sobrien determine the conversions (if any) to be applied to the operands 339490075Sobrien (_over.match.oper_, _over.built_). */ 3395132718Skan lvalue_p = false; 339690075Sobrien if (!same_type_p (arg2_type, arg3_type) 339790075Sobrien && (CLASS_TYPE_P (arg2_type) || CLASS_TYPE_P (arg3_type))) 339890075Sobrien { 339990075Sobrien tree args[3]; 3400169689Skan conversion *conv; 3401132718Skan bool any_viable_p; 340290075Sobrien 340390075Sobrien /* Rearrange the arguments so that add_builtin_candidate only has 340490075Sobrien to know about two args. In build_builtin_candidates, the 340590075Sobrien arguments are unscrambled. */ 340690075Sobrien args[0] = arg2; 340790075Sobrien args[1] = arg3; 340890075Sobrien args[2] = arg1; 3409169689Skan add_builtin_candidates (&candidates, 3410169689Skan COND_EXPR, 3411117395Skan NOP_EXPR, 3412117395Skan ansi_opname (COND_EXPR), 3413117395Skan args, 3414117395Skan LOOKUP_NORMAL); 341590075Sobrien 341690075Sobrien /* [expr.cond] 341790075Sobrien 341890075Sobrien If the overload resolution fails, the program is 341990075Sobrien ill-formed. */ 3420132718Skan candidates = splice_viable (candidates, pedantic, &any_viable_p); 3421132718Skan if (!any_viable_p) 342290075Sobrien { 342390075Sobrien op_error (COND_EXPR, NOP_EXPR, arg1, arg2, arg3, "no match"); 342490075Sobrien print_z_candidates (candidates); 342590075Sobrien return error_mark_node; 342690075Sobrien } 342790075Sobrien cand = tourney (candidates); 342890075Sobrien if (!cand) 342990075Sobrien { 343090075Sobrien op_error (COND_EXPR, NOP_EXPR, arg1, arg2, arg3, "no match"); 343190075Sobrien print_z_candidates (candidates); 343290075Sobrien return error_mark_node; 343390075Sobrien } 343490075Sobrien 343590075Sobrien /* [expr.cond] 343690075Sobrien 343790075Sobrien Otherwise, the conversions thus determined are applied, and 343890075Sobrien the converted operands are used in place of the original 343990075Sobrien operands for the remainder of this section. */ 3440169689Skan conv = cand->convs[0]; 344190075Sobrien arg1 = convert_like (conv, arg1); 3442169689Skan conv = cand->convs[1]; 344390075Sobrien arg2 = convert_like (conv, arg2); 3444169689Skan conv = cand->convs[2]; 344590075Sobrien arg3 = convert_like (conv, arg3); 344690075Sobrien } 344790075Sobrien 344890075Sobrien /* [expr.cond] 344990075Sobrien 345090075Sobrien Lvalue-to-rvalue (_conv.lval_), array-to-pointer (_conv.array_), 345190075Sobrien and function-to-pointer (_conv.func_) standard conversions are 345290075Sobrien performed on the second and third operands. 345390075Sobrien 345490075Sobrien We need to force the lvalue-to-rvalue conversion here for class types, 345590075Sobrien so we get TARGET_EXPRs; trying to deal with a COND_EXPR of class rvalues 345690075Sobrien that isn't wrapped with a TARGET_EXPR plays havoc with exception 3457132718Skan regions. */ 345890075Sobrien 3459117395Skan arg2 = force_rvalue (arg2); 3460132718Skan if (!CLASS_TYPE_P (arg2_type)) 3461132718Skan arg2_type = TREE_TYPE (arg2); 346290075Sobrien 3463117395Skan arg3 = force_rvalue (arg3); 3464132718Skan if (!CLASS_TYPE_P (arg2_type)) 3465132718Skan arg3_type = TREE_TYPE (arg3); 346690075Sobrien 346790075Sobrien if (arg2 == error_mark_node || arg3 == error_mark_node) 346890075Sobrien return error_mark_node; 3469169689Skan 347090075Sobrien /* [expr.cond] 3471169689Skan 347290075Sobrien After those conversions, one of the following shall hold: 347390075Sobrien 347490075Sobrien --The second and third operands have the same type; the result is of 347590075Sobrien that type. */ 347690075Sobrien if (same_type_p (arg2_type, arg3_type)) 347790075Sobrien result_type = arg2_type; 347890075Sobrien /* [expr.cond] 347990075Sobrien 348090075Sobrien --The second and third operands have arithmetic or enumeration 348190075Sobrien type; the usual arithmetic conversions are performed to bring 348290075Sobrien them to a common type, and the result is of that type. */ 3483169689Skan else if ((ARITHMETIC_TYPE_P (arg2_type) 348490075Sobrien || TREE_CODE (arg2_type) == ENUMERAL_TYPE) 348590075Sobrien && (ARITHMETIC_TYPE_P (arg3_type) 348690075Sobrien || TREE_CODE (arg3_type) == ENUMERAL_TYPE)) 348790075Sobrien { 348890075Sobrien /* In this case, there is always a common type. */ 3489169689Skan result_type = type_after_usual_arithmetic_conversions (arg2_type, 349090075Sobrien arg3_type); 3491169689Skan 349290075Sobrien if (TREE_CODE (arg2_type) == ENUMERAL_TYPE 3493169689Skan && TREE_CODE (arg3_type) == ENUMERAL_TYPE) 3494169689Skan warning (0, "enumeral mismatch in conditional expression: %qT vs %qT", 3495169689Skan arg2_type, arg3_type); 349690075Sobrien else if (extra_warnings 3497169689Skan && ((TREE_CODE (arg2_type) == ENUMERAL_TYPE 3498169689Skan && !same_type_p (arg3_type, type_promotes_to (arg2_type))) 3499169689Skan || (TREE_CODE (arg3_type) == ENUMERAL_TYPE 3500169689Skan && !same_type_p (arg2_type, type_promotes_to (arg3_type))))) 3501169689Skan warning (0, "enumeral and non-enumeral type in conditional expression"); 3502169689Skan 350390075Sobrien arg2 = perform_implicit_conversion (result_type, arg2); 350490075Sobrien arg3 = perform_implicit_conversion (result_type, arg3); 350590075Sobrien } 350690075Sobrien /* [expr.cond] 350790075Sobrien 350890075Sobrien --The second and third operands have pointer type, or one has 350990075Sobrien pointer type and the other is a null pointer constant; pointer 351090075Sobrien conversions (_conv.ptr_) and qualification conversions 351190075Sobrien (_conv.qual_) are performed to bring them to their composite 351290075Sobrien pointer type (_expr.rel_). The result is of the composite 351390075Sobrien pointer type. 351490075Sobrien 351590075Sobrien --The second and third operands have pointer to member type, or 351690075Sobrien one has pointer to member type and the other is a null pointer 351790075Sobrien constant; pointer to member conversions (_conv.mem_) and 351890075Sobrien qualification conversions (_conv.qual_) are performed to bring 351990075Sobrien them to a common type, whose cv-qualification shall match the 352090075Sobrien cv-qualification of either the second or the third operand. 3521117395Skan The result is of the common type. */ 3522169689Skan else if ((null_ptr_cst_p (arg2) 3523132718Skan && (TYPE_PTR_P (arg3_type) || TYPE_PTR_TO_MEMBER_P (arg3_type))) 3524169689Skan || (null_ptr_cst_p (arg3) 3525132718Skan && (TYPE_PTR_P (arg2_type) || TYPE_PTR_TO_MEMBER_P (arg2_type))) 352690075Sobrien || (TYPE_PTR_P (arg2_type) && TYPE_PTR_P (arg3_type)) 352790075Sobrien || (TYPE_PTRMEM_P (arg2_type) && TYPE_PTRMEM_P (arg3_type)) 3528132718Skan || (TYPE_PTRMEMFUNC_P (arg2_type) && TYPE_PTRMEMFUNC_P (arg3_type))) 352990075Sobrien { 353090075Sobrien result_type = composite_pointer_type (arg2_type, arg3_type, arg2, 353190075Sobrien arg3, "conditional expression"); 3532132718Skan if (result_type == error_mark_node) 3533132718Skan return error_mark_node; 353490075Sobrien arg2 = perform_implicit_conversion (result_type, arg2); 353590075Sobrien arg3 = perform_implicit_conversion (result_type, arg3); 353690075Sobrien } 353790075Sobrien 353890075Sobrien if (!result_type) 353990075Sobrien { 3540169689Skan error ("operands to ?: have different types %qT and %qT", 3541169689Skan arg2_type, arg3_type); 354290075Sobrien return error_mark_node; 354390075Sobrien } 354490075Sobrien 354590075Sobrien valid_operands: 3546169689Skan result = fold_if_not_in_template (build3 (COND_EXPR, result_type, arg1, 3547169689Skan arg2, arg3)); 3548132718Skan /* We can't use result_type below, as fold might have returned a 3549132718Skan throw_expr. */ 3550132718Skan 3551169689Skan if (!lvalue_p) 3552169689Skan { 3553169689Skan /* Expand both sides into the same slot, hopefully the target of 3554169689Skan the ?: expression. We used to check for TARGET_EXPRs here, 3555169689Skan but now we sometimes wrap them in NOP_EXPRs so the test would 3556169689Skan fail. */ 3557169689Skan if (CLASS_TYPE_P (TREE_TYPE (result))) 3558169689Skan result = get_target_expr (result); 3559169689Skan /* If this expression is an rvalue, but might be mistaken for an 3560169689Skan lvalue, we must add a NON_LVALUE_EXPR. */ 3561169689Skan result = rvalue (result); 3562169689Skan } 356390075Sobrien 356490075Sobrien return result; 356590075Sobrien} 356690075Sobrien 3567132718Skan/* OPERAND is an operand to an expression. Perform necessary steps 3568132718Skan required before using it. If OPERAND is NULL_TREE, NULL_TREE is 3569132718Skan returned. */ 3570132718Skan 3571132718Skanstatic tree 3572132718Skanprep_operand (tree operand) 3573132718Skan{ 3574132718Skan if (operand) 3575132718Skan { 3576132718Skan if (CLASS_TYPE_P (TREE_TYPE (operand)) 3577132718Skan && CLASSTYPE_TEMPLATE_INSTANTIATION (TREE_TYPE (operand))) 3578132718Skan /* Make sure the template type is instantiated now. */ 3579132718Skan instantiate_class_template (TYPE_MAIN_VARIANT (TREE_TYPE (operand))); 3580132718Skan } 3581132718Skan 3582132718Skan return operand; 3583132718Skan} 3584132718Skan 3585132718Skan/* Add each of the viable functions in FNS (a FUNCTION_DECL or 3586132718Skan OVERLOAD) to the CANDIDATES, returning an updated list of 3587132718Skan CANDIDATES. The ARGS are the arguments provided to the call, 3588132718Skan without any implicit object parameter. The EXPLICIT_TARGS are 3589132718Skan explicit template arguments provided. TEMPLATE_ONLY is true if 3590132718Skan only template functions should be considered. CONVERSION_PATH, 3591132718Skan ACCESS_PATH, and FLAGS are as for add_function_candidate. */ 3592132718Skan 3593132718Skanstatic void 3594169689Skanadd_candidates (tree fns, tree args, 3595132718Skan tree explicit_targs, bool template_only, 3596132718Skan tree conversion_path, tree access_path, 3597132718Skan int flags, 3598132718Skan struct z_candidate **candidates) 3599132718Skan{ 3600132718Skan tree ctype; 3601132718Skan tree non_static_args; 3602132718Skan 3603132718Skan ctype = conversion_path ? BINFO_TYPE (conversion_path) : NULL_TREE; 3604132718Skan /* Delay creating the implicit this parameter until it is needed. */ 3605132718Skan non_static_args = NULL_TREE; 3606132718Skan 3607169689Skan while (fns) 3608132718Skan { 3609132718Skan tree fn; 3610132718Skan tree fn_args; 3611132718Skan 3612132718Skan fn = OVL_CURRENT (fns); 3613132718Skan /* Figure out which set of arguments to use. */ 3614132718Skan if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)) 3615132718Skan { 3616132718Skan /* If this function is a non-static member, prepend the implicit 3617132718Skan object parameter. */ 3618132718Skan if (!non_static_args) 3619132718Skan non_static_args = tree_cons (NULL_TREE, 3620132718Skan build_this (TREE_VALUE (args)), 3621132718Skan TREE_CHAIN (args)); 3622132718Skan fn_args = non_static_args; 3623132718Skan } 3624132718Skan else 3625132718Skan /* Otherwise, just use the list of arguments provided. */ 3626132718Skan fn_args = args; 3627132718Skan 3628132718Skan if (TREE_CODE (fn) == TEMPLATE_DECL) 3629169689Skan add_template_candidate (candidates, 3630169689Skan fn, 3631132718Skan ctype, 3632132718Skan explicit_targs, 3633132718Skan fn_args, 3634132718Skan NULL_TREE, 3635132718Skan access_path, 3636132718Skan conversion_path, 3637132718Skan flags, 3638132718Skan DEDUCE_CALL); 3639132718Skan else if (!template_only) 3640132718Skan add_function_candidate (candidates, 3641132718Skan fn, 3642132718Skan ctype, 3643132718Skan fn_args, 3644132718Skan access_path, 3645132718Skan conversion_path, 3646132718Skan flags); 3647132718Skan fns = OVL_NEXT (fns); 3648132718Skan } 3649132718Skan} 3650132718Skan 365190075Sobrientree 3652132718Skanbuild_new_op (enum tree_code code, int flags, tree arg1, tree arg2, tree arg3, 3653132718Skan bool *overloaded_p) 365450397Sobrien{ 365550397Sobrien struct z_candidate *candidates = 0, *cand; 3656132718Skan tree arglist, fnname; 3657132718Skan tree args[3]; 3658169689Skan tree result = NULL_TREE; 3659169689Skan bool result_valid_p = false; 366050397Sobrien enum tree_code code2 = NOP_EXPR; 3661169689Skan conversion *conv; 3662169689Skan void *p; 3663132718Skan bool strict_p; 3664132718Skan bool any_viable_p; 366518334Speter 3666169689Skan if (error_operand_p (arg1) 3667169689Skan || error_operand_p (arg2) 3668132718Skan || error_operand_p (arg3)) 366950397Sobrien return error_mark_node; 367018334Speter 367150397Sobrien if (code == MODIFY_EXPR) 367250397Sobrien { 367350397Sobrien code2 = TREE_CODE (arg3); 367450397Sobrien arg3 = NULL_TREE; 367590075Sobrien fnname = ansi_assopname (code2); 367650397Sobrien } 367750397Sobrien else 367890075Sobrien fnname = ansi_opname (code); 367918334Speter 3680132718Skan arg1 = prep_operand (arg1); 3681169689Skan 368250397Sobrien switch (code) 368350397Sobrien { 368450397Sobrien case NEW_EXPR: 368550397Sobrien case VEC_NEW_EXPR: 368650397Sobrien case VEC_DELETE_EXPR: 368750397Sobrien case DELETE_EXPR: 3688117395Skan /* Use build_op_new_call and build_op_delete_call instead. */ 3689169689Skan gcc_unreachable (); 369018334Speter 369150397Sobrien case CALL_EXPR: 369250397Sobrien return build_object_call (arg1, arg2); 369318334Speter 369450397Sobrien default: 369550397Sobrien break; 369650397Sobrien } 369718334Speter 3698132718Skan arg2 = prep_operand (arg2); 3699132718Skan arg3 = prep_operand (arg3); 3700169689Skan 370150397Sobrien if (code == COND_EXPR) 370250397Sobrien { 370350397Sobrien if (arg2 == NULL_TREE 370450397Sobrien || TREE_CODE (TREE_TYPE (arg2)) == VOID_TYPE 370550397Sobrien || TREE_CODE (TREE_TYPE (arg3)) == VOID_TYPE 370650397Sobrien || (! IS_OVERLOAD_TYPE (TREE_TYPE (arg2)) 370750397Sobrien && ! IS_OVERLOAD_TYPE (TREE_TYPE (arg3)))) 370850397Sobrien goto builtin; 370950397Sobrien } 371050397Sobrien else if (! IS_OVERLOAD_TYPE (TREE_TYPE (arg1)) 371150397Sobrien && (! arg2 || ! IS_OVERLOAD_TYPE (TREE_TYPE (arg2)))) 371250397Sobrien goto builtin; 371318334Speter 371450397Sobrien if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR) 371550397Sobrien arg2 = integer_zero_node; 371618334Speter 371790075Sobrien arglist = NULL_TREE; 371890075Sobrien if (arg3) 371990075Sobrien arglist = tree_cons (NULL_TREE, arg3, arglist); 372090075Sobrien if (arg2) 372190075Sobrien arglist = tree_cons (NULL_TREE, arg2, arglist); 372290075Sobrien arglist = tree_cons (NULL_TREE, arg1, arglist); 372318334Speter 3724169689Skan /* Get the high-water mark for the CONVERSION_OBSTACK. */ 3725169689Skan p = conversion_obstack_alloc (0); 3726169689Skan 3727132718Skan /* Add namespace-scope operators to the list of functions to 3728132718Skan consider. */ 3729169689Skan add_candidates (lookup_function_nonclass (fnname, arglist, /*block_p=*/true), 3730132718Skan arglist, NULL_TREE, false, NULL_TREE, NULL_TREE, 3731132718Skan flags, &candidates); 3732132718Skan /* Add class-member operators to the candidate set. */ 3733132718Skan if (CLASS_TYPE_P (TREE_TYPE (arg1))) 373450397Sobrien { 3735132718Skan tree fns; 373618334Speter 3737169689Skan fns = lookup_fnfields (TREE_TYPE (arg1), fnname, 1); 373850397Sobrien if (fns == error_mark_node) 3739169689Skan { 3740169689Skan result = error_mark_node; 3741169689Skan goto user_defined_result_ready; 3742169689Skan } 3743132718Skan if (fns) 3744169689Skan add_candidates (BASELINK_FUNCTIONS (fns), arglist, 3745132718Skan NULL_TREE, false, 3746132718Skan BASELINK_BINFO (fns), 3747132718Skan TYPE_BINFO (TREE_TYPE (arg1)), 3748132718Skan flags, &candidates); 374950397Sobrien } 375018334Speter 3751132718Skan /* Rearrange the arguments for ?: so that add_builtin_candidate only has 3752132718Skan to know about two args; a builtin candidate will always have a first 3753132718Skan parameter of type bool. We'll handle that in 3754132718Skan build_builtin_candidate. */ 3755132718Skan if (code == COND_EXPR) 375650397Sobrien { 3757132718Skan args[0] = arg2; 3758132718Skan args[1] = arg3; 3759132718Skan args[2] = arg1; 376050397Sobrien } 3761132718Skan else 3762132718Skan { 3763132718Skan args[0] = arg1; 3764132718Skan args[1] = arg2; 3765132718Skan args[2] = NULL_TREE; 3766132718Skan } 376718334Speter 3768132718Skan add_builtin_candidates (&candidates, code, code2, fnname, args, flags); 376918334Speter 3770117395Skan switch (code) 377150397Sobrien { 3772117395Skan case COMPOUND_EXPR: 3773117395Skan case ADDR_EXPR: 3774117395Skan /* For these, the built-in candidates set is empty 3775117395Skan [over.match.oper]/3. We don't want non-strict matches 3776117395Skan because exact matches are always possible with built-in 3777117395Skan operators. The built-in candidate set for COMPONENT_REF 3778117395Skan would be empty too, but since there are no such built-in 3779117395Skan operators, we accept non-strict matches for them. */ 3780132718Skan strict_p = true; 3781117395Skan break; 3782117395Skan 3783117395Skan default: 3784132718Skan strict_p = pedantic; 3785117395Skan break; 3786169689Skan } 3787117395Skan 3788132718Skan candidates = splice_viable (candidates, strict_p, &any_viable_p); 3789132718Skan if (!any_viable_p) 3790117395Skan { 379150397Sobrien switch (code) 379218334Speter { 379350397Sobrien case POSTINCREMENT_EXPR: 379450397Sobrien case POSTDECREMENT_EXPR: 379550397Sobrien /* Look for an `operator++ (int)'. If they didn't have 379650397Sobrien one, then we fall back to the old way of doing things. */ 379718334Speter if (flags & LOOKUP_COMPLAIN) 3798169689Skan pedwarn ("no %<%D(int)%> declared for postfix %qs, " 3799169689Skan "trying prefix operator instead", 3800169689Skan fnname, 3801169689Skan operator_name_info[code].name); 380250397Sobrien if (code == POSTINCREMENT_EXPR) 380350397Sobrien code = PREINCREMENT_EXPR; 380418334Speter else 3805169689Skan code = PREDECREMENT_EXPR; 3806169689Skan result = build_new_op (code, flags, arg1, NULL_TREE, NULL_TREE, 3807169689Skan overloaded_p); 3808169689Skan break; 3809169689Skan 381050397Sobrien /* The caller will deal with these. */ 381150397Sobrien case ADDR_EXPR: 381250397Sobrien case COMPOUND_EXPR: 381350397Sobrien case COMPONENT_REF: 3814169689Skan result = NULL_TREE; 3815169689Skan result_valid_p = true; 3816169689Skan break; 381750397Sobrien 381850397Sobrien default: 3819169689Skan if (flags & LOOKUP_COMPLAIN) 3820169689Skan { 3821169689Skan op_error (code, code2, arg1, arg2, arg3, "no match"); 3822169689Skan print_z_candidates (candidates); 3823169689Skan } 3824169689Skan result = error_mark_node; 382550397Sobrien break; 382618334Speter } 382718334Speter } 3828169689Skan else 382918334Speter { 3830169689Skan cand = tourney (candidates); 3831169689Skan if (cand == 0) 383218334Speter { 3833169689Skan if (flags & LOOKUP_COMPLAIN) 3834169689Skan { 3835169689Skan op_error (code, code2, arg1, arg2, arg3, "ambiguous overload"); 3836169689Skan print_z_candidates (candidates); 3837169689Skan } 3838169689Skan result = error_mark_node; 383918334Speter } 3840169689Skan else if (TREE_CODE (cand->fn) == FUNCTION_DECL) 3841169689Skan { 3842169689Skan if (overloaded_p) 3843169689Skan *overloaded_p = true; 384418334Speter 3845169689Skan result = build_over_call (cand, LOOKUP_NORMAL); 3846169689Skan } 3847169689Skan else 3848169689Skan { 3849169689Skan /* Give any warnings we noticed during overload resolution. */ 3850169689Skan if (cand->warnings) 3851169689Skan { 3852169689Skan struct candidate_warning *w; 3853169689Skan for (w = cand->warnings; w; w = w->next) 3854169689Skan joust (cand, w->loser, 1); 3855169689Skan } 3856132718Skan 3857169689Skan /* Check for comparison of different enum types. */ 3858169689Skan switch (code) 3859169689Skan { 3860169689Skan case GT_EXPR: 3861169689Skan case LT_EXPR: 3862169689Skan case GE_EXPR: 3863169689Skan case LE_EXPR: 3864169689Skan case EQ_EXPR: 3865169689Skan case NE_EXPR: 3866169689Skan if (TREE_CODE (TREE_TYPE (arg1)) == ENUMERAL_TYPE 3867169689Skan && TREE_CODE (TREE_TYPE (arg2)) == ENUMERAL_TYPE 3868169689Skan && (TYPE_MAIN_VARIANT (TREE_TYPE (arg1)) 3869169689Skan != TYPE_MAIN_VARIANT (TREE_TYPE (arg2)))) 3870169689Skan { 3871169689Skan warning (0, "comparison between %q#T and %q#T", 3872169689Skan TREE_TYPE (arg1), TREE_TYPE (arg2)); 3873169689Skan } 3874169689Skan break; 3875169689Skan default: 3876169689Skan break; 3877169689Skan } 387850397Sobrien 3879169689Skan /* We need to strip any leading REF_BIND so that bitfields 3880169689Skan don't cause errors. This should not remove any important 3881169689Skan conversions, because builtins don't apply to class 3882169689Skan objects directly. */ 3883169689Skan conv = cand->convs[0]; 3884169689Skan if (conv->kind == ck_ref_bind) 3885169689Skan conv = conv->u.next; 3886169689Skan arg1 = convert_like (conv, arg1); 3887169689Skan if (arg2) 3888169689Skan { 3889169689Skan conv = cand->convs[1]; 3890169689Skan if (conv->kind == ck_ref_bind) 3891169689Skan conv = conv->u.next; 3892169689Skan arg2 = convert_like (conv, arg2); 3893169689Skan } 3894169689Skan if (arg3) 3895169689Skan { 3896169689Skan conv = cand->convs[2]; 3897169689Skan if (conv->kind == ck_ref_bind) 3898169689Skan conv = conv->u.next; 3899169689Skan arg3 = convert_like (conv, arg3); 3900169689Skan } 390118334Speter } 390250397Sobrien } 390318334Speter 3904169689Skan user_defined_result_ready: 390518334Speter 3906169689Skan /* Free all the conversions we allocated. */ 3907169689Skan obstack_free (&conversion_obstack, p); 3908169689Skan 3909169689Skan if (result || result_valid_p) 3910169689Skan return result; 3911169689Skan 3912169689Skan builtin: 391350397Sobrien switch (code) 391450397Sobrien { 391550397Sobrien case MODIFY_EXPR: 391650397Sobrien return build_modify_expr (arg1, code2, arg2); 391718334Speter 391850397Sobrien case INDIRECT_REF: 391950397Sobrien return build_indirect_ref (arg1, "unary *"); 392018334Speter 392150397Sobrien case PLUS_EXPR: 392250397Sobrien case MINUS_EXPR: 392350397Sobrien case MULT_EXPR: 392450397Sobrien case TRUNC_DIV_EXPR: 392550397Sobrien case GT_EXPR: 392650397Sobrien case LT_EXPR: 392750397Sobrien case GE_EXPR: 392850397Sobrien case LE_EXPR: 392950397Sobrien case EQ_EXPR: 393050397Sobrien case NE_EXPR: 393150397Sobrien case MAX_EXPR: 393250397Sobrien case MIN_EXPR: 393350397Sobrien case LSHIFT_EXPR: 393450397Sobrien case RSHIFT_EXPR: 393550397Sobrien case TRUNC_MOD_EXPR: 393650397Sobrien case BIT_AND_EXPR: 393750397Sobrien case BIT_IOR_EXPR: 393850397Sobrien case BIT_XOR_EXPR: 393950397Sobrien case TRUTH_ANDIF_EXPR: 394050397Sobrien case TRUTH_ORIF_EXPR: 394190075Sobrien return cp_build_binary_op (code, arg1, arg2); 394218334Speter 3943169689Skan case UNARY_PLUS_EXPR: 394450397Sobrien case NEGATE_EXPR: 394550397Sobrien case BIT_NOT_EXPR: 394650397Sobrien case TRUTH_NOT_EXPR: 394750397Sobrien case PREINCREMENT_EXPR: 394850397Sobrien case POSTINCREMENT_EXPR: 394950397Sobrien case PREDECREMENT_EXPR: 395050397Sobrien case POSTDECREMENT_EXPR: 395150397Sobrien case REALPART_EXPR: 395250397Sobrien case IMAGPART_EXPR: 395350397Sobrien return build_unary_op (code, arg1, candidates != 0); 395450397Sobrien 395550397Sobrien case ARRAY_REF: 395650397Sobrien return build_array_ref (arg1, arg2); 395750397Sobrien 395850397Sobrien case COND_EXPR: 395950397Sobrien return build_conditional_expr (arg1, arg2, arg3); 396050397Sobrien 396150397Sobrien case MEMBER_REF: 3962169689Skan return build_m_component_ref (build_indirect_ref (arg1, NULL), arg2); 396350397Sobrien 396450397Sobrien /* The caller will deal with these. */ 396550397Sobrien case ADDR_EXPR: 396650397Sobrien case COMPONENT_REF: 396750397Sobrien case COMPOUND_EXPR: 396850397Sobrien return NULL_TREE; 396950397Sobrien 397050397Sobrien default: 3971169689Skan gcc_unreachable (); 397250397Sobrien } 3973169689Skan return NULL_TREE; 397450397Sobrien} 397550397Sobrien 397650397Sobrien/* Build a call to operator delete. This has to be handled very specially, 397750397Sobrien because the restrictions on what signatures match are different from all 397850397Sobrien other call instances. For a normal delete, only a delete taking (void *) 397950397Sobrien or (void *, size_t) is accepted. For a placement delete, only an exact 398050397Sobrien match with the placement new is accepted. 398150397Sobrien 398250397Sobrien CODE is either DELETE_EXPR or VEC_DELETE_EXPR. 398396263Sobrien ADDR is the pointer to be deleted. 398450397Sobrien SIZE is the size of the memory block to be deleted. 3985169689Skan GLOBAL_P is true if the delete-expression should not consider 3986169689Skan class-specific delete operators. 3987169689Skan PLACEMENT is the corresponding placement new call, or NULL_TREE. 398850397Sobrien 3989171825Skan If this call to "operator delete" is being generated as part to 3990171825Skan deallocate memory allocated via a new-expression (as per [expr.new] 3991171825Skan which requires that if the initialization throws an exception then 3992171825Skan we call a deallocation function), then ALLOC_FN is the allocation 3993171825Skan function. */ 3994171825Skan 399550397Sobrientree 3996132718Skanbuild_op_delete_call (enum tree_code code, tree addr, tree size, 3997169689Skan bool global_p, tree placement, 3998169689Skan tree alloc_fn) 399950397Sobrien{ 400090075Sobrien tree fn = NULL_TREE; 4001132718Skan tree fns, fnname, argtypes, args, type; 400290075Sobrien int pass; 400350397Sobrien 400450397Sobrien if (addr == error_mark_node) 400550397Sobrien return error_mark_node; 400650397Sobrien 4007132718Skan type = strip_array_types (TREE_TYPE (TREE_TYPE (addr))); 400850397Sobrien 400990075Sobrien fnname = ansi_opname (code); 401090075Sobrien 4011169689Skan if (CLASS_TYPE_P (type) 4012169689Skan && COMPLETE_TYPE_P (complete_type (type)) 4013169689Skan && !global_p) 401450397Sobrien /* In [class.free] 401550397Sobrien 401650397Sobrien If the result of the lookup is ambiguous or inaccessible, or if 401750397Sobrien the lookup selects a placement deallocation function, the 401850397Sobrien program is ill-formed. 4019169689Skan 4020132718Skan Therefore, we ask lookup_fnfields to complain about ambiguity. */ 402150397Sobrien { 402250397Sobrien fns = lookup_fnfields (TYPE_BINFO (type), fnname, 1); 402350397Sobrien if (fns == error_mark_node) 402450397Sobrien return error_mark_node; 402518334Speter } 402650397Sobrien else 402750397Sobrien fns = NULL_TREE; 402818334Speter 402950397Sobrien if (fns == NULL_TREE) 403050397Sobrien fns = lookup_name_nonclass (fnname); 403150397Sobrien 403250397Sobrien if (placement) 403318334Speter { 4034169689Skan /* Get the parameter types for the allocation function that is 4035169689Skan being called. */ 4036169689Skan gcc_assert (alloc_fn != NULL_TREE); 403796263Sobrien argtypes = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (alloc_fn))); 403850397Sobrien /* Also the second argument. */ 4039169689Skan args = TREE_CHAIN (TREE_OPERAND (placement, 1)); 404050397Sobrien } 404150397Sobrien else 404250397Sobrien { 404350397Sobrien /* First try it without the size argument. */ 404450397Sobrien argtypes = void_list_node; 404550397Sobrien args = NULL_TREE; 404650397Sobrien } 404750397Sobrien 404850397Sobrien /* Strip const and volatile from addr. */ 404990075Sobrien addr = cp_convert (ptr_type_node, addr); 405050397Sobrien 405190075Sobrien /* We make two tries at finding a matching `operator delete'. On 4052132718Skan the first pass, we look for a one-operator (or placement) 405390075Sobrien operator delete. If we're not doing placement delete, then on 405490075Sobrien the second pass we look for a two-argument delete. */ 4055169689Skan for (pass = 0; pass < (placement ? 1 : 2); ++pass) 405690075Sobrien { 405790075Sobrien /* Go through the `operator delete' functions looking for one 405890075Sobrien with a matching type. */ 4059169689Skan for (fn = BASELINK_P (fns) ? BASELINK_FUNCTIONS (fns) : fns; 4060169689Skan fn; 406190075Sobrien fn = OVL_NEXT (fn)) 406290075Sobrien { 406390075Sobrien tree t; 406490075Sobrien 4065132718Skan /* The first argument must be "void *". */ 4066132718Skan t = TYPE_ARG_TYPES (TREE_TYPE (OVL_CURRENT (fn))); 4067132718Skan if (!same_type_p (TREE_VALUE (t), ptr_type_node)) 4068132718Skan continue; 4069132718Skan t = TREE_CHAIN (t); 4070132718Skan /* On the first pass, check the rest of the arguments. */ 4071132718Skan if (pass == 0) 4072132718Skan { 4073132718Skan tree a = argtypes; 4074132718Skan while (a && t) 4075132718Skan { 4076132718Skan if (!same_type_p (TREE_VALUE (a), TREE_VALUE (t))) 4077132718Skan break; 4078132718Skan a = TREE_CHAIN (a); 4079132718Skan t = TREE_CHAIN (t); 4080132718Skan } 4081132718Skan if (!a && !t) 4082132718Skan break; 4083132718Skan } 4084171825Skan /* On the second pass, look for a function with exactly two 4085171825Skan arguments: "void *" and "size_t". */ 4086132718Skan else if (pass == 1 4087171825Skan /* For "operator delete(void *, ...)" there will be 4088171825Skan no second argument, but we will not get an exact 4089171825Skan match above. */ 4090171825Skan && t 4091132718Skan && same_type_p (TREE_VALUE (t), sizetype) 4092132718Skan && TREE_CHAIN (t) == void_list_node) 409390075Sobrien break; 409490075Sobrien } 409590075Sobrien 409690075Sobrien /* If we found a match, we're done. */ 409790075Sobrien if (fn) 409890075Sobrien break; 409950397Sobrien } 410050397Sobrien 410190075Sobrien /* If we have a matching function, call it. */ 410290075Sobrien if (fn) 410390075Sobrien { 410490075Sobrien /* Make sure we have the actual function, and not an 410590075Sobrien OVERLOAD. */ 410690075Sobrien fn = OVL_CURRENT (fn); 410750397Sobrien 410890075Sobrien /* If the FN is a member function, make sure that it is 410990075Sobrien accessible. */ 411090075Sobrien if (DECL_CLASS_SCOPE_P (fn)) 4111169689Skan perform_or_defer_access_check (TYPE_BINFO (type), fn, fn); 411250397Sobrien 411390075Sobrien if (pass == 0) 411490075Sobrien args = tree_cons (NULL_TREE, addr, args); 411590075Sobrien else 4116169689Skan args = tree_cons (NULL_TREE, addr, 411790075Sobrien build_tree_list (NULL_TREE, size)); 411850397Sobrien 4119132718Skan if (placement) 4120132718Skan { 4121132718Skan /* The placement args might not be suitable for overload 4122132718Skan resolution at this point, so build the call directly. */ 4123132718Skan mark_used (fn); 4124169689Skan return build_cxx_call (fn, args); 4125132718Skan } 4126132718Skan else 4127132718Skan return build_function_call (fn, args); 412850397Sobrien } 412950397Sobrien 4130171825Skan /* [expr.new] 413150397Sobrien 4132171825Skan If no unambiguous matching deallocation function can be found, 4133171825Skan propagating the exception does not cause the object's memory to 4134171825Skan be freed. */ 4135171825Skan if (alloc_fn) 4136171825Skan { 4137171825Skan if (!placement) 4138171825Skan warning (0, "no corresponding deallocation function for `%D'", 4139171825Skan alloc_fn); 4140171825Skan return NULL_TREE; 4141171825Skan } 4142171825Skan 4143169689Skan error ("no suitable %<operator %s%> for %qT", 4144132718Skan operator_name_info[(int)code].name, type); 414550397Sobrien return error_mark_node; 414650397Sobrien} 414750397Sobrien 414850397Sobrien/* If the current scope isn't allowed to access DECL along 414952284Sobrien BASETYPE_PATH, give an error. The most derived class in 4150169689Skan BASETYPE_PATH is the one used to qualify DECL. DIAG_DECL is 4151169689Skan the declaration to use in the error diagnostic. */ 415250397Sobrien 4153132718Skanbool 4154169689Skanenforce_access (tree basetype_path, tree decl, tree diag_decl) 415550397Sobrien{ 4156169689Skan gcc_assert (TREE_CODE (basetype_path) == TREE_BINFO); 4157169689Skan 4158169689Skan if (!accessible_p (basetype_path, decl, true)) 415950397Sobrien { 416052284Sobrien if (TREE_PRIVATE (decl)) 4161169689Skan error ("%q+#D is private", diag_decl); 416252284Sobrien else if (TREE_PROTECTED (decl)) 4163169689Skan error ("%q+#D is protected", diag_decl); 416452284Sobrien else 4165169689Skan error ("%q+#D is inaccessible", diag_decl); 416690075Sobrien error ("within this context"); 4167132718Skan return false; 416850397Sobrien } 416952284Sobrien 4170132718Skan return true; 417150397Sobrien} 417250397Sobrien 4173132718Skan/* Check that a callable constructor to initialize a temporary of 4174132718Skan TYPE from an EXPR exists. */ 4175132718Skan 4176132718Skanstatic void 4177132718Skancheck_constructor_callable (tree type, tree expr) 4178132718Skan{ 4179132718Skan build_special_member_call (NULL_TREE, 4180132718Skan complete_ctor_identifier, 4181169689Skan build_tree_list (NULL_TREE, expr), 4182169689Skan type, 4183132718Skan LOOKUP_NORMAL | LOOKUP_ONLYCONVERTING 4184146895Skan | LOOKUP_NO_CONVERSION 4185132718Skan | LOOKUP_CONSTRUCTOR_CALLABLE); 4186132718Skan} 4187132718Skan 4188132718Skan/* Initialize a temporary of type TYPE with EXPR. The FLAGS are a 4189132718Skan bitwise or of LOOKUP_* values. If any errors are warnings are 4190132718Skan generated, set *DIAGNOSTIC_FN to "error" or "warning", 4191132718Skan respectively. If no diagnostics are generated, set *DIAGNOSTIC_FN 4192132718Skan to NULL. */ 4193132718Skan 4194132718Skanstatic tree 4195169689Skanbuild_temp (tree expr, tree type, int flags, 4196169689Skan diagnostic_fn_t *diagnostic_fn) 4197132718Skan{ 4198132718Skan int savew, savee; 4199169689Skan 4200132718Skan savew = warningcount, savee = errorcount; 4201132718Skan expr = build_special_member_call (NULL_TREE, 4202132718Skan complete_ctor_identifier, 4203169689Skan build_tree_list (NULL_TREE, expr), 4204169689Skan type, flags); 4205132718Skan if (warningcount > savew) 4206169689Skan *diagnostic_fn = warning0; 4207132718Skan else if (errorcount > savee) 4208132718Skan *diagnostic_fn = error; 4209132718Skan else 4210132718Skan *diagnostic_fn = NULL; 4211132718Skan return expr; 4212132718Skan} 4213132718Skan 4214169689Skan 4215117395Skan/* Perform the conversions in CONVS on the expression EXPR. FN and 4216117395Skan ARGNUM are used for diagnostics. ARGNUM is zero based, -1 4217117395Skan indicates the `this' argument of a method. INNER is nonzero when 421890075Sobrien being called to continue a conversion chain. It is negative when a 4219117395Skan reference binding will be applied, positive otherwise. If 4220117395Skan ISSUE_CONVERSION_WARNINGS is true, warnings about suspicious 4221169689Skan conversions will be emitted if appropriate. If C_CAST_P is true, 4222169689Skan this conversion is coming from a C-style cast; in that case, 4223169689Skan conversions to inaccessible bases are permitted. */ 422450397Sobrien 422550397Sobrienstatic tree 4226169689Skanconvert_like_real (conversion *convs, tree expr, tree fn, int argnum, 4227169689Skan int inner, bool issue_conversion_warnings, 4228169689Skan bool c_cast_p) 422950397Sobrien{ 4230169689Skan tree totype = convs->type; 4231169689Skan diagnostic_fn_t diagnostic_fn; 423290075Sobrien 4233169689Skan if (convs->bad_p 4234169689Skan && convs->kind != ck_user 4235169689Skan && convs->kind != ck_ambig 4236169689Skan && convs->kind != ck_ref_bind) 423750397Sobrien { 4238169689Skan conversion *t = convs; 4239169689Skan for (; t; t = convs->u.next) 424018334Speter { 4241169689Skan if (t->kind == ck_user || !t->bad_p) 424218334Speter { 4243117395Skan expr = convert_like_real (t, expr, fn, argnum, 1, 4244169689Skan /*issue_conversion_warnings=*/false, 4245169689Skan /*c_cast_p=*/false); 424650397Sobrien break; 424718334Speter } 4248169689Skan else if (t->kind == ck_ambig) 4249117395Skan return convert_like_real (t, expr, fn, argnum, 1, 4250169689Skan /*issue_conversion_warnings=*/false, 4251169689Skan /*c_cast_p=*/false); 4252169689Skan else if (t->kind == ck_identity) 425350397Sobrien break; 425418334Speter } 4255169689Skan pedwarn ("invalid conversion from %qT to %qT", TREE_TYPE (expr), totype); 425690075Sobrien if (fn) 4257169689Skan pedwarn (" initializing argument %P of %qD", argnum, fn); 425890075Sobrien return cp_convert (totype, expr); 425918334Speter } 4260169689Skan 4261117395Skan if (issue_conversion_warnings) 426250397Sobrien { 4263169689Skan tree t = non_reference (totype); 4264169689Skan 4265169689Skan /* Issue warnings about peculiar, but valid, uses of NULL. */ 4266169689Skan if (ARITHMETIC_TYPE_P (t) && expr == null_node) 4267169689Skan { 4268169689Skan if (fn) 4269169689Skan warning (OPT_Wconversion, "passing NULL to non-pointer argument %P of %qD", 4270169689Skan argnum, fn); 4271169689Skan else 4272169689Skan warning (OPT_Wconversion, "converting to non-pointer type %qT from NULL", t); 4273169689Skan } 4274169689Skan 4275169689Skan /* Warn about assigning a floating-point type to an integer type. */ 4276169689Skan if (TREE_CODE (TREE_TYPE (expr)) == REAL_TYPE 4277169689Skan && TREE_CODE (t) == INTEGER_TYPE) 4278169689Skan { 4279169689Skan if (fn) 4280169689Skan warning (OPT_Wconversion, "passing %qT for argument %P to %qD", 4281169689Skan TREE_TYPE (expr), argnum, fn); 4282169689Skan else 4283169689Skan warning (OPT_Wconversion, "converting to %qT from %qT", t, TREE_TYPE (expr)); 4284169689Skan } 4285169689Skan } 4286169689Skan 4287169689Skan switch (convs->kind) 4288169689Skan { 4289169689Skan case ck_user: 429050397Sobrien { 4291169689Skan struct z_candidate *cand = convs->cand; 429290075Sobrien tree convfn = cand->fn; 429350397Sobrien tree args; 429418334Speter 429590075Sobrien if (DECL_CONSTRUCTOR_P (convfn)) 429650397Sobrien { 4297169689Skan tree t = build_int_cst (build_pointer_type (DECL_CONTEXT (convfn)), 4298169689Skan 0); 429950397Sobrien 430090075Sobrien args = build_tree_list (NULL_TREE, expr); 4301169689Skan /* We should never try to call the abstract or base constructor 4302169689Skan from here. */ 4303169689Skan gcc_assert (!DECL_HAS_IN_CHARGE_PARM_P (convfn) 4304169689Skan && !DECL_HAS_VTT_PARM_P (convfn)); 430590075Sobrien args = tree_cons (NULL_TREE, t, args); 430650397Sobrien } 430750397Sobrien else 430850397Sobrien args = build_this (expr); 4309132718Skan expr = build_over_call (cand, LOOKUP_NORMAL); 431050397Sobrien 431150397Sobrien /* If this is a constructor or a function returning an aggr type, 431250397Sobrien we need to build up a TARGET_EXPR. */ 431390075Sobrien if (DECL_CONSTRUCTOR_P (convfn)) 431490075Sobrien expr = build_cplus_new (totype, expr); 431550397Sobrien 431690075Sobrien /* The result of the call is then used to direct-initialize the object 431790075Sobrien that is the destination of the copy-initialization. [dcl.init] 431890075Sobrien 431990075Sobrien Note that this step is not reflected in the conversion sequence; 432090075Sobrien it affects the semantics when we actually perform the 432190075Sobrien conversion, but is not considered during overload resolution. 432290075Sobrien 432390075Sobrien If the target is a class, that means call a ctor. */ 432490075Sobrien if (IS_AGGR_TYPE (totype) 432590075Sobrien && (inner >= 0 || !lvalue_p (expr))) 432690075Sobrien { 4327169689Skan expr = (build_temp 4328169689Skan (expr, totype, 4329132718Skan /* Core issue 84, now a DR, says that we don't 4330132718Skan allow UDCs for these args (which deliberately 4331132718Skan breaks copy-init of an auto_ptr<Base> from an 4332132718Skan auto_ptr<Derived>). */ 4333132718Skan LOOKUP_NORMAL|LOOKUP_ONLYCONVERTING|LOOKUP_NO_CONVERSION, 4334132718Skan &diagnostic_fn)); 4335169689Skan 4336132718Skan if (diagnostic_fn) 433790075Sobrien { 4338132718Skan if (fn) 4339169689Skan diagnostic_fn 4340169689Skan (" initializing argument %P of %qD from result of %qD", 434190075Sobrien argnum, fn, convfn); 4342132718Skan else 4343169689Skan diagnostic_fn 4344169689Skan (" initializing temporary from result of %qD", convfn); 434590075Sobrien } 434690075Sobrien expr = build_cplus_new (totype, expr); 434790075Sobrien } 434850397Sobrien return expr; 434950397Sobrien } 4350169689Skan case ck_identity: 435150397Sobrien if (type_unknown_p (expr)) 4352169689Skan expr = instantiate_type (totype, expr, tf_warning_or_error); 4353169689Skan /* Convert a constant to its underlying value, unless we are 4354169689Skan about to bind it to a reference, in which case we need to 4355169689Skan leave it as an lvalue. */ 4356169689Skan if (inner >= 0) 4357132718Skan expr = decl_constant_value (expr); 4358169689Skan if (convs->check_copy_constructor_p) 4359132718Skan check_constructor_callable (totype, expr); 436050397Sobrien return expr; 4361169689Skan case ck_ambig: 436250397Sobrien /* Call build_user_type_conversion again for the error. */ 436350397Sobrien return build_user_type_conversion 4364169689Skan (totype, convs->u.expr, LOOKUP_NORMAL); 436550397Sobrien 436650397Sobrien default: 436750397Sobrien break; 436850397Sobrien }; 436950397Sobrien 4370169689Skan expr = convert_like_real (convs->u.next, expr, fn, argnum, 4371169689Skan convs->kind == ck_ref_bind ? -1 : 1, 4372169689Skan /*issue_conversion_warnings=*/false, 4373169689Skan c_cast_p); 437450397Sobrien if (expr == error_mark_node) 437550397Sobrien return error_mark_node; 437650397Sobrien 4377169689Skan switch (convs->kind) 437818334Speter { 4379169689Skan case ck_rvalue: 4380169689Skan expr = convert_bitfield_to_declared_type (expr); 438190075Sobrien if (! IS_AGGR_TYPE (totype)) 438250397Sobrien return expr; 4383132718Skan /* Else fall through. */ 4384169689Skan case ck_base: 4385169689Skan if (convs->kind == ck_base && !convs->need_temporary_p) 438690075Sobrien { 438790075Sobrien /* We are going to bind a reference directly to a base-class 438890075Sobrien subobject of EXPR. */ 4389169689Skan if (convs->check_copy_constructor_p) 4390132718Skan check_constructor_callable (TREE_TYPE (expr), expr); 439190075Sobrien /* Build an expression for `*((base*) &expr)'. */ 439290075Sobrien expr = build_unary_op (ADDR_EXPR, expr, 0); 4393169689Skan expr = convert_to_base (expr, build_pointer_type (totype), 4394169689Skan !c_cast_p, /*nonnull=*/true); 439590075Sobrien expr = build_indirect_ref (expr, "implicit conversion"); 439690075Sobrien return expr; 439790075Sobrien } 439890075Sobrien 439990075Sobrien /* Copy-initialization where the cv-unqualified version of the source 440090075Sobrien type is the same class as, or a derived class of, the class of the 440190075Sobrien destination [is treated as direct-initialization]. [dcl.init] */ 4402132718Skan expr = build_temp (expr, totype, LOOKUP_NORMAL|LOOKUP_ONLYCONVERTING, 4403132718Skan &diagnostic_fn); 4404132718Skan if (diagnostic_fn && fn) 4405169689Skan diagnostic_fn (" initializing argument %P of %qD", argnum, fn); 440690075Sobrien return build_cplus_new (totype, expr); 440790075Sobrien 4408169689Skan case ck_ref_bind: 440950397Sobrien { 441090075Sobrien tree ref_type = totype; 441190075Sobrien 441290075Sobrien /* If necessary, create a temporary. */ 4413169689Skan if (convs->need_temporary_p || !lvalue_p (expr)) 441450397Sobrien { 4415169689Skan tree type = convs->u.next->type; 4416161651Skan cp_lvalue_kind lvalue = real_lvalue_p (expr); 4417132718Skan 4418132718Skan if (!CP_TYPE_CONST_NON_VOLATILE_P (TREE_TYPE (ref_type))) 4419132718Skan { 4420132718Skan /* If the reference is volatile or non-const, we 4421132718Skan cannot create a temporary. */ 4422132718Skan if (lvalue & clk_bitfield) 4423169689Skan error ("cannot bind bitfield %qE to %qT", 4424132718Skan expr, ref_type); 4425132718Skan else if (lvalue & clk_packed) 4426169689Skan error ("cannot bind packed field %qE to %qT", 4427132718Skan expr, ref_type); 4428132718Skan else 4429169689Skan error ("cannot bind rvalue %qE to %qT", expr, ref_type); 4430132718Skan return error_mark_node; 4431132718Skan } 4432161651Skan /* If the source is a packed field, and we must use a copy 4433161651Skan constructor, then building the target expr will require 4434161651Skan binding the field to the reference parameter to the 4435161651Skan copy constructor, and we'll end up with an infinite 4436161651Skan loop. If we can use a bitwise copy, then we'll be 4437161651Skan OK. */ 4438169689Skan if ((lvalue & clk_packed) 4439169689Skan && CLASS_TYPE_P (type) 4440161651Skan && !TYPE_HAS_TRIVIAL_INIT_REF (type)) 4441161651Skan { 4442169689Skan error ("cannot bind packed field %qE to %qT", 4443161651Skan expr, ref_type); 4444161651Skan return error_mark_node; 4445161651Skan } 444690075Sobrien expr = build_target_expr_with_type (expr, type); 444750397Sobrien } 444890075Sobrien 444990075Sobrien /* Take the address of the thing to which we will bind the 445090075Sobrien reference. */ 445190075Sobrien expr = build_unary_op (ADDR_EXPR, expr, 1); 445290075Sobrien if (expr == error_mark_node) 445390075Sobrien return error_mark_node; 445490075Sobrien 445590075Sobrien /* Convert it to a pointer to the type referred to by the 445690075Sobrien reference. This will adjust the pointer if a derived to 445790075Sobrien base conversion is being performed. */ 4458169689Skan expr = cp_convert (build_pointer_type (TREE_TYPE (ref_type)), 445990075Sobrien expr); 446090075Sobrien /* Convert the pointer to the desired reference type. */ 4461117395Skan return build_nop (ref_type, expr); 446250397Sobrien } 446350397Sobrien 4464169689Skan case ck_lvalue: 446550397Sobrien return decay_conversion (expr); 446650397Sobrien 4467169689Skan case ck_qual: 446852284Sobrien /* Warn about deprecated conversion if appropriate. */ 446990075Sobrien string_conv_p (totype, expr, 1); 447052284Sobrien break; 4471169689Skan 4472169689Skan case ck_ptr: 4473169689Skan if (convs->base_p) 4474169689Skan expr = convert_to_base (expr, totype, !c_cast_p, 4475169689Skan /*nonnull=*/false); 4476169689Skan return build_nop (totype, expr); 4477169689Skan 4478169689Skan case ck_pmem: 4479169689Skan return convert_ptrmem (totype, expr, /*allow_inverse_p=*/false, 4480169689Skan c_cast_p); 4481169689Skan 448250397Sobrien default: 448350397Sobrien break; 448418334Speter } 4485169689Skan 4486169689Skan if (issue_conversion_warnings) 4487169689Skan expr = convert_and_check (totype, expr); 4488169689Skan else 4489169689Skan expr = convert (totype, expr); 4490169689Skan 4491169689Skan return expr; 449250397Sobrien} 449318334Speter 4494132718Skan/* Build a call to __builtin_trap. */ 4495117395Skan 4496117395Skanstatic tree 4497132718Skancall_builtin_trap (void) 4498117395Skan{ 4499169689Skan tree fn = implicit_built_in_decls[BUILT_IN_TRAP]; 4500117395Skan 4501169689Skan gcc_assert (fn != NULL); 4502117395Skan fn = build_call (fn, NULL_TREE); 4503117395Skan return fn; 4504117395Skan} 4505117395Skan 450650397Sobrien/* ARG is being passed to a varargs function. Perform any conversions 4507132718Skan required. Return the converted value. */ 450850397Sobrien 450950397Sobrientree 4510132718Skanconvert_arg_to_ellipsis (tree arg) 451150397Sobrien{ 4512132718Skan /* [expr.call] 4513132718Skan 4514132718Skan The lvalue-to-rvalue, array-to-pointer, and function-to-pointer 4515132718Skan standard conversions are performed. */ 4516132718Skan arg = decay_conversion (arg); 4517132718Skan /* [expr.call] 4518132718Skan 4519132718Skan If the argument has integral or enumeration type that is subject 4520132718Skan to the integral promotions (_conv.prom_), or a floating point 4521132718Skan type that is subject to the floating point promotion 4522132718Skan (_conv.fpprom_), the value of the argument is converted to the 4523132718Skan promoted type before the call. */ 452450397Sobrien if (TREE_CODE (TREE_TYPE (arg)) == REAL_TYPE 452550397Sobrien && (TYPE_PRECISION (TREE_TYPE (arg)) 452650397Sobrien < TYPE_PRECISION (double_type_node))) 4527132718Skan arg = convert_to_real (double_type_node, arg); 4528132718Skan else if (INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (arg))) 4529132718Skan arg = perform_integral_promotions (arg); 453050397Sobrien 453152284Sobrien arg = require_complete_type (arg); 4532169689Skan 4533132718Skan if (arg != error_mark_node 4534132718Skan && !pod_type_p (TREE_TYPE (arg))) 453590075Sobrien { 4536117395Skan /* Undefined behavior [expr.call] 5.2.2/7. We used to just warn 4537102780Skan here and do a bitwise copy, but now cp_expr_size will abort if we 4538169689Skan try to do that. 4539169689Skan If the call appears in the context of a sizeof expression, 4540169689Skan there is no need to emit a warning, since the expression won't be 4541132718Skan evaluated. We keep the builtin_trap just as a safety check. */ 4542132718Skan if (!skip_evaluation) 4543169689Skan warning (0, "cannot pass objects of non-POD type %q#T through %<...%>; " 4544169689Skan "call will abort at runtime", TREE_TYPE (arg)); 4545117395Skan arg = call_builtin_trap (); 4546169689Skan arg = build2 (COMPOUND_EXPR, integer_type_node, arg, 4547169689Skan integer_zero_node); 454890075Sobrien } 454990075Sobrien 455050397Sobrien return arg; 455150397Sobrien} 455250397Sobrien 455390075Sobrien/* va_arg (EXPR, TYPE) is a builtin. Make sure it is not abused. */ 455490075Sobrien 455590075Sobrientree 4556132718Skanbuild_x_va_arg (tree expr, tree type) 455790075Sobrien{ 455890075Sobrien if (processing_template_decl) 455990075Sobrien return build_min (VA_ARG_EXPR, type, expr); 4560169689Skan 456190075Sobrien type = complete_type_or_else (type, NULL_TREE); 456290075Sobrien 456390075Sobrien if (expr == error_mark_node || !type) 456490075Sobrien return error_mark_node; 4565169689Skan 456690075Sobrien if (! pod_type_p (type)) 456790075Sobrien { 4568169689Skan /* Remove reference types so we don't ICE later on. */ 4569169689Skan tree type1 = non_reference (type); 4570117395Skan /* Undefined behavior [expr.call] 5.2.2/7. */ 4571169689Skan warning (0, "cannot receive objects of non-POD type %q#T through %<...%>; " 4572169689Skan "call will abort at runtime", type); 4573169689Skan expr = convert (build_pointer_type (type1), null_node); 4574169689Skan expr = build2 (COMPOUND_EXPR, TREE_TYPE (expr), 4575169689Skan call_builtin_trap (), expr); 4576132718Skan expr = build_indirect_ref (expr, NULL); 4577132718Skan return expr; 457890075Sobrien } 4579169689Skan 458090075Sobrien return build_va_arg (expr, type); 458190075Sobrien} 458290075Sobrien 4583117395Skan/* TYPE has been given to va_arg. Apply the default conversions which 4584117395Skan would have happened when passed via ellipsis. Return the promoted 4585117395Skan type, or the passed type if there is no change. */ 458690075Sobrien 458790075Sobrientree 4588132718Skancxx_type_promotes_to (tree type) 458990075Sobrien{ 459090075Sobrien tree promote; 4591117395Skan 4592132718Skan /* Perform the array-to-pointer and function-to-pointer 4593132718Skan conversions. */ 4594132718Skan type = type_decays_to (type); 4595117395Skan 4596117395Skan promote = type_promotes_to (type); 4597117395Skan if (same_type_p (type, promote)) 4598117395Skan promote = type; 4599169689Skan 4600117395Skan return promote; 460190075Sobrien} 460290075Sobrien 460350397Sobrien/* ARG is a default argument expression being passed to a parameter of 460452284Sobrien the indicated TYPE, which is a parameter to FN. Do any required 460552284Sobrien conversions. Return the converted value. */ 460650397Sobrien 460750397Sobrientree 4608132718Skanconvert_default_arg (tree type, tree arg, tree fn, int parmnum) 460950397Sobrien{ 4610132718Skan /* If the ARG is an unparsed default argument expression, the 4611132718Skan conversion cannot be performed. */ 461290075Sobrien if (TREE_CODE (arg) == DEFAULT_ARG) 461352284Sobrien { 4614169689Skan error ("the default argument for parameter %d of %qD has " 4615132718Skan "not yet been parsed", 4616132718Skan parmnum, fn); 4617132718Skan return error_mark_node; 461890075Sobrien } 461952284Sobrien 462090075Sobrien if (fn && DECL_TEMPLATE_INFO (fn)) 462190075Sobrien arg = tsubst_default_argument (fn, type, arg); 462252284Sobrien 462350397Sobrien arg = break_out_target_exprs (arg); 462450397Sobrien 462550397Sobrien if (TREE_CODE (arg) == CONSTRUCTOR) 462618334Speter { 4627169689Skan arg = digest_init (type, arg); 462850397Sobrien arg = convert_for_initialization (0, type, arg, LOOKUP_NORMAL, 462990075Sobrien "default argument", fn, parmnum); 463050397Sobrien } 463150397Sobrien else 463250397Sobrien { 4633169689Skan /* We must make a copy of ARG, in case subsequent processing 4634169689Skan alters any part of it. For example, during gimplification a 4635169689Skan cast of the form (T) &X::f (where "f" is a member function) 4636169689Skan will lead to replacing the PTRMEM_CST for &X::f with a 4637169689Skan VAR_DECL. We can avoid the copy for constants, since they 4638169689Skan are never modified in place. */ 4639169689Skan if (!CONSTANT_CLASS_P (arg)) 4640169689Skan arg = unshare_expr (arg); 464150397Sobrien arg = convert_for_initialization (0, type, arg, LOOKUP_NORMAL, 464290075Sobrien "default argument", fn, parmnum); 4643117395Skan arg = convert_for_arg_passing (type, arg); 464450397Sobrien } 464550397Sobrien 464650397Sobrien return arg; 464750397Sobrien} 464850397Sobrien 4649117395Skan/* Returns the type which will really be used for passing an argument of 4650117395Skan type TYPE. */ 4651117395Skan 4652117395Skantree 4653132718Skantype_passed_as (tree type) 4654117395Skan{ 4655117395Skan /* Pass classes with copy ctors by invisible reference. */ 4656117395Skan if (TREE_ADDRESSABLE (type)) 4657169689Skan { 4658169689Skan type = build_reference_type (type); 4659169689Skan /* There are no other pointers to this temporary. */ 4660169689Skan type = build_qualified_type (type, TYPE_QUAL_RESTRICT); 4661169689Skan } 4662169689Skan else if (targetm.calls.promote_prototypes (type) 4663117395Skan && INTEGRAL_TYPE_P (type) 4664132718Skan && COMPLETE_TYPE_P (type) 4665132718Skan && INT_CST_LT_UNSIGNED (TYPE_SIZE (type), 4666132718Skan TYPE_SIZE (integer_type_node))) 4667117395Skan type = integer_type_node; 4668117395Skan 4669117395Skan return type; 4670117395Skan} 4671117395Skan 4672117395Skan/* Actually perform the appropriate conversion. */ 4673117395Skan 4674117395Skantree 4675132718Skanconvert_for_arg_passing (tree type, tree val) 4676117395Skan{ 4677220150Smm tree bitfield_type; 4678220150Smm 4679220150Smm /* If VAL is a bitfield, then -- since it has already been converted 4680220150Smm to TYPE -- it cannot have a precision greater than TYPE. 4681220150Smm 4682220150Smm If it has a smaller precision, we must widen it here. For 4683220150Smm example, passing "int f:3;" to a function expecting an "int" will 4684220150Smm not result in any conversion before this point. 4685220150Smm 4686220150Smm If the precision is the same we must not risk widening. For 4687220150Smm example, the COMPONENT_REF for a 32-bit "long long" bitfield will 4688220150Smm often have type "int", even though the C++ type for the field is 4689220150Smm "long long". If the value is being passed to a function 4690220150Smm expecting an "int", then no conversions will be required. But, 4691220150Smm if we call convert_bitfield_to_declared_type, the bitfield will 4692220150Smm be converted to "long long". */ 4693220150Smm bitfield_type = is_bitfield_expr_with_lowered_type (val); 4694220150Smm if (bitfield_type 4695220150Smm && TYPE_PRECISION (TREE_TYPE (val)) < TYPE_PRECISION (type)) 4696220150Smm val = convert_to_integer (TYPE_MAIN_VARIANT (bitfield_type), val); 4697220150Smm 4698117395Skan if (val == error_mark_node) 4699117395Skan ; 4700117395Skan /* Pass classes with copy ctors by invisible reference. */ 4701117395Skan else if (TREE_ADDRESSABLE (type)) 4702117395Skan val = build1 (ADDR_EXPR, build_reference_type (type), val); 4703169689Skan else if (targetm.calls.promote_prototypes (type) 4704117395Skan && INTEGRAL_TYPE_P (type) 4705132718Skan && COMPLETE_TYPE_P (type) 4706132718Skan && INT_CST_LT_UNSIGNED (TYPE_SIZE (type), 4707132718Skan TYPE_SIZE (integer_type_node))) 4708132718Skan val = perform_integral_promotions (val); 4709169689Skan if (warn_missing_format_attribute) 4710169689Skan { 4711169689Skan tree rhstype = TREE_TYPE (val); 4712169689Skan const enum tree_code coder = TREE_CODE (rhstype); 4713169689Skan const enum tree_code codel = TREE_CODE (type); 4714169689Skan if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE) 4715169689Skan && coder == codel 4716169689Skan && check_missing_format_attribute (type, rhstype)) 4717169689Skan warning (OPT_Wmissing_format_attribute, 4718169689Skan "argument of function call might be a candidate for a format attribute"); 4719169689Skan } 4720117395Skan return val; 4721117395Skan} 4722117395Skan 4723132718Skan/* Returns true iff FN is a function with magic varargs, i.e. ones for 4724132718Skan which no conversions at all should be done. This is true for some 4725132718Skan builtins which don't act like normal functions. */ 4726132718Skan 4727132718Skanstatic bool 4728132718Skanmagic_varargs_p (tree fn) 4729132718Skan{ 4730132718Skan if (DECL_BUILT_IN (fn)) 4731132718Skan switch (DECL_FUNCTION_CODE (fn)) 4732132718Skan { 4733132718Skan case BUILT_IN_CLASSIFY_TYPE: 4734132718Skan case BUILT_IN_CONSTANT_P: 4735132718Skan case BUILT_IN_NEXT_ARG: 4736132718Skan case BUILT_IN_STDARG_START: 4737132718Skan case BUILT_IN_VA_START: 4738132718Skan return true; 4739132718Skan 4740132718Skan default:; 4741132718Skan } 4742132718Skan 4743132718Skan return false; 4744132718Skan} 4745132718Skan 474690075Sobrien/* Subroutine of the various build_*_call functions. Overload resolution 474790075Sobrien has chosen a winning candidate CAND; build up a CALL_EXPR accordingly. 474890075Sobrien ARGS is a TREE_LIST of the unconverted arguments to the call. FLAGS is a 474990075Sobrien bitmask of various LOOKUP_* flags which apply to the call itself. */ 475090075Sobrien 475150397Sobrienstatic tree 4752132718Skanbuild_over_call (struct z_candidate *cand, int flags) 475350397Sobrien{ 475450397Sobrien tree fn = cand->fn; 4755132718Skan tree args = cand->args; 4756169689Skan conversion **convs = cand->convs; 4757169689Skan conversion *conv; 475850397Sobrien tree converted_args = NULL_TREE; 475950397Sobrien tree parm = TYPE_ARG_TYPES (TREE_TYPE (fn)); 4760169689Skan tree arg, val; 476150397Sobrien int i = 0; 476250397Sobrien int is_method = 0; 476350397Sobrien 4764132718Skan /* In a template, there is no need to perform all of the work that 4765132718Skan is normally done. We are only interested in the type of the call 4766132718Skan expression, i.e., the return type of the function. Any semantic 4767132718Skan errors will be deferred until the template is instantiated. */ 4768132718Skan if (processing_template_decl) 4769132718Skan { 4770132718Skan tree expr; 4771132718Skan tree return_type; 4772132718Skan return_type = TREE_TYPE (TREE_TYPE (fn)); 4773169689Skan expr = build3 (CALL_EXPR, return_type, fn, args, NULL_TREE); 4774132718Skan if (TREE_THIS_VOLATILE (fn) && cfun) 4775132718Skan current_function_returns_abnormally = 1; 4776132718Skan if (!VOID_TYPE_P (return_type)) 4777132718Skan require_complete_type (return_type); 4778132718Skan return convert_from_reference (expr); 4779132718Skan } 4780132718Skan 478150397Sobrien /* Give any warnings we noticed during overload resolution. */ 478250397Sobrien if (cand->warnings) 4783169689Skan { 4784169689Skan struct candidate_warning *w; 4785169689Skan for (w = cand->warnings; w; w = w->next) 4786169689Skan joust (cand, w->loser, 1); 4787169689Skan } 478850397Sobrien 478950397Sobrien if (DECL_FUNCTION_MEMBER_P (fn)) 4790132718Skan { 4791132718Skan /* If FN is a template function, two cases must be considered. 4792132718Skan For example: 479350397Sobrien 4794132718Skan struct A { 4795132718Skan protected: 4796132718Skan template <class T> void f(); 4797132718Skan }; 4798132718Skan template <class T> struct B { 4799132718Skan protected: 4800132718Skan void g(); 4801132718Skan }; 4802132718Skan struct C : A, B<int> { 4803132718Skan using A::f; // #1 4804132718Skan using B<int>::g; // #2 4805132718Skan }; 4806132718Skan 4807132718Skan In case #1 where `A::f' is a member template, DECL_ACCESS is 4808132718Skan recorded in the primary template but not in its specialization. 4809132718Skan We check access of FN using its primary template. 4810132718Skan 4811132718Skan In case #2, where `B<int>::g' has a DECL_TEMPLATE_INFO simply 4812132718Skan because it is a member of class template B, DECL_ACCESS is 4813132718Skan recorded in the specialization `B<int>::g'. We cannot use its 4814132718Skan primary template because `B<T>::g' and `B<int>::g' may have 4815132718Skan different access. */ 4816132718Skan if (DECL_TEMPLATE_INFO (fn) 4817169689Skan && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (fn))) 4818132718Skan perform_or_defer_access_check (cand->access_path, 4819169689Skan DECL_TI_TEMPLATE (fn), fn); 4820132718Skan else 4821169689Skan perform_or_defer_access_check (cand->access_path, fn, fn); 4822132718Skan } 4823132718Skan 482450397Sobrien if (args && TREE_CODE (args) != TREE_LIST) 482590075Sobrien args = build_tree_list (NULL_TREE, args); 482650397Sobrien arg = args; 482750397Sobrien 482850397Sobrien /* The implicit parameters to a constructor are not considered by overload 482950397Sobrien resolution, and must be of the proper type. */ 483050397Sobrien if (DECL_CONSTRUCTOR_P (fn)) 483150397Sobrien { 483290075Sobrien converted_args = tree_cons (NULL_TREE, TREE_VALUE (arg), converted_args); 483350397Sobrien arg = TREE_CHAIN (arg); 483450397Sobrien parm = TREE_CHAIN (parm); 4835169689Skan /* We should never try to call the abstract constructor. */ 4836169689Skan gcc_assert (!DECL_HAS_IN_CHARGE_PARM_P (fn)); 4837169689Skan 483890075Sobrien if (DECL_HAS_VTT_PARM_P (fn)) 483918334Speter { 484090075Sobrien converted_args = tree_cons 484150397Sobrien (NULL_TREE, TREE_VALUE (arg), converted_args); 484250397Sobrien arg = TREE_CHAIN (arg); 484350397Sobrien parm = TREE_CHAIN (parm); 484418334Speter } 4845169689Skan } 484650397Sobrien /* Bypass access control for 'this' parameter. */ 484750397Sobrien else if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE) 484850397Sobrien { 484950397Sobrien tree parmtype = TREE_VALUE (parm); 485050397Sobrien tree argtype = TREE_TYPE (TREE_VALUE (arg)); 4851117395Skan tree converted_arg; 4852117395Skan tree base_binfo; 485350397Sobrien 4854169689Skan if (convs[i]->bad_p) 4855169689Skan pedwarn ("passing %qT as %<this%> argument of %q#D discards qualifiers", 4856169689Skan TREE_TYPE (argtype), fn); 4857169689Skan 485852284Sobrien /* [class.mfct.nonstatic]: If a nonstatic member function of a class 485952284Sobrien X is called for an object that is not of type X, or of a type 486052284Sobrien derived from X, the behavior is undefined. 486152284Sobrien 4862169689Skan So we can assume that anything passed as 'this' is non-null, and 486352284Sobrien optimize accordingly. */ 4864169689Skan gcc_assert (TREE_CODE (parmtype) == POINTER_TYPE); 4865117395Skan /* Convert to the base in which the function was declared. */ 4866169689Skan gcc_assert (cand->conversion_path != NULL_TREE); 4867117395Skan converted_arg = build_base_path (PLUS_EXPR, 4868117395Skan TREE_VALUE (arg), 4869117395Skan cand->conversion_path, 4870117395Skan 1); 4871119256Skan /* Check that the base class is accessible. */ 4872169689Skan if (!accessible_base_p (TREE_TYPE (argtype), 4873169689Skan BINFO_TYPE (cand->conversion_path), true)) 4874169689Skan error ("%qT is not an accessible base of %qT", 4875119256Skan BINFO_TYPE (cand->conversion_path), 4876119256Skan TREE_TYPE (argtype)); 4877117395Skan /* If fn was found by a using declaration, the conversion path 4878169689Skan will be to the derived class, not the base declaring fn. We 4879169689Skan must convert from derived to base. */ 4880117395Skan base_binfo = lookup_base (TREE_TYPE (TREE_TYPE (converted_arg)), 4881169689Skan TREE_TYPE (parmtype), ba_unique, NULL); 4882117395Skan converted_arg = build_base_path (PLUS_EXPR, converted_arg, 4883117395Skan base_binfo, 1); 4884169689Skan 4885117395Skan converted_args = tree_cons (NULL_TREE, converted_arg, converted_args); 488650397Sobrien parm = TREE_CHAIN (parm); 488750397Sobrien arg = TREE_CHAIN (arg); 488850397Sobrien ++i; 488950397Sobrien is_method = 1; 489018334Speter } 489150397Sobrien 489250397Sobrien for (; arg && parm; 489350397Sobrien parm = TREE_CHAIN (parm), arg = TREE_CHAIN (arg), ++i) 489418334Speter { 489550397Sobrien tree type = TREE_VALUE (parm); 489618334Speter 4897169689Skan conv = convs[i]; 4898169689Skan 4899169689Skan /* Don't make a copy here if build_call is going to. */ 4900169689Skan if (conv->kind == ck_rvalue 4901169689Skan && !TREE_ADDRESSABLE (complete_type (type))) 4902169689Skan conv = conv->u.next; 4903169689Skan 490490075Sobrien val = convert_like_with_context 490590075Sobrien (conv, TREE_VALUE (arg), fn, i - is_method); 490618334Speter 4907117395Skan val = convert_for_arg_passing (type, val); 490890075Sobrien converted_args = tree_cons (NULL_TREE, val, converted_args); 490918334Speter } 491018334Speter 491150397Sobrien /* Default arguments */ 491290075Sobrien for (; parm && parm != void_list_node; parm = TREE_CHAIN (parm), i++) 4913169689Skan converted_args 4914169689Skan = tree_cons (NULL_TREE, 4915169689Skan convert_default_arg (TREE_VALUE (parm), 491690075Sobrien TREE_PURPOSE (parm), 491790075Sobrien fn, i - is_method), 491890075Sobrien converted_args); 491918334Speter 492050397Sobrien /* Ellipsis */ 492150397Sobrien for (; arg; arg = TREE_CHAIN (arg)) 4922132718Skan { 4923132718Skan tree a = TREE_VALUE (arg); 4924132718Skan if (magic_varargs_p (fn)) 4925132718Skan /* Do no conversions for magic varargs. */; 4926132718Skan else 4927132718Skan a = convert_arg_to_ellipsis (a); 4928132718Skan converted_args = tree_cons (NULL_TREE, a, converted_args); 4929132718Skan } 493050397Sobrien 493150397Sobrien converted_args = nreverse (converted_args); 493250397Sobrien 4933169689Skan check_function_arguments (TYPE_ATTRIBUTES (TREE_TYPE (fn)), 4934169689Skan converted_args, TYPE_ARG_TYPES (TREE_TYPE (fn))); 493552284Sobrien 493650397Sobrien /* Avoid actually calling copy constructors and copy assignment operators, 493750397Sobrien if possible. */ 493852284Sobrien 493952284Sobrien if (! flag_elide_constructors) 494052284Sobrien /* Do things the hard way. */; 4941169689Skan else if (cand->num_convs == 1 && DECL_COPY_CONSTRUCTOR_P (fn)) 494250397Sobrien { 494350397Sobrien tree targ; 494490075Sobrien arg = skip_artificial_parms_for (fn, converted_args); 494550397Sobrien arg = TREE_VALUE (arg); 494650397Sobrien 494750397Sobrien /* Pull out the real argument, disregarding const-correctness. */ 494850397Sobrien targ = arg; 494950397Sobrien while (TREE_CODE (targ) == NOP_EXPR 495050397Sobrien || TREE_CODE (targ) == NON_LVALUE_EXPR 495150397Sobrien || TREE_CODE (targ) == CONVERT_EXPR) 495250397Sobrien targ = TREE_OPERAND (targ, 0); 495350397Sobrien if (TREE_CODE (targ) == ADDR_EXPR) 495450397Sobrien { 495550397Sobrien targ = TREE_OPERAND (targ, 0); 4956169689Skan if (!same_type_ignoring_top_level_qualifiers_p 495790075Sobrien (TREE_TYPE (TREE_TYPE (arg)), TREE_TYPE (targ))) 495850397Sobrien targ = NULL_TREE; 495950397Sobrien } 496018334Speter else 496150397Sobrien targ = NULL_TREE; 496218334Speter 496350397Sobrien if (targ) 496450397Sobrien arg = targ; 496550397Sobrien else 496650397Sobrien arg = build_indirect_ref (arg, 0); 496718334Speter 496850397Sobrien /* [class.copy]: the copy constructor is implicitly defined even if 496950397Sobrien the implementation elided its use. */ 497050397Sobrien if (TYPE_HAS_COMPLEX_INIT_REF (DECL_CONTEXT (fn))) 497150397Sobrien mark_used (fn); 497250397Sobrien 497350397Sobrien /* If we're creating a temp and we already have one, don't create a 4974169689Skan new one. If we're not creating a temp but we get one, use 4975169689Skan INIT_EXPR to collapse the temp into our target. Otherwise, if the 4976169689Skan ctor is trivial, do a bitwise copy with a simple TARGET_EXPR for a 4977169689Skan temp or an INIT_EXPR otherwise. */ 497850397Sobrien if (integer_zerop (TREE_VALUE (args))) 497918334Speter { 4980117395Skan if (TREE_CODE (arg) == TARGET_EXPR) 498150397Sobrien return arg; 498250397Sobrien else if (TYPE_HAS_TRIVIAL_INIT_REF (DECL_CONTEXT (fn))) 498390075Sobrien return build_target_expr_with_type (arg, DECL_CONTEXT (fn)); 498418334Speter } 4985117395Skan else if (TREE_CODE (arg) == TARGET_EXPR 4986117395Skan || TYPE_HAS_TRIVIAL_INIT_REF (DECL_CONTEXT (fn))) 498750397Sobrien { 498850397Sobrien tree to = stabilize_reference 498950397Sobrien (build_indirect_ref (TREE_VALUE (args), 0)); 499018334Speter 4991169689Skan val = build2 (INIT_EXPR, DECL_CONTEXT (fn), to, arg); 4992132718Skan return val; 499350397Sobrien } 499418334Speter } 499590075Sobrien else if (DECL_OVERLOADED_OPERATOR_P (fn) == NOP_EXPR 499690075Sobrien && copy_fn_p (fn) 499790075Sobrien && TYPE_HAS_TRIVIAL_ASSIGN_REF (DECL_CONTEXT (fn))) 499850397Sobrien { 499950397Sobrien tree to = stabilize_reference 500050397Sobrien (build_indirect_ref (TREE_VALUE (converted_args), 0)); 5001132718Skan tree type = TREE_TYPE (to); 5002132718Skan tree as_base = CLASSTYPE_AS_BASE (type); 500318334Speter 5004169689Skan arg = TREE_VALUE (TREE_CHAIN (converted_args)); 5005132718Skan if (tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (as_base))) 5006169689Skan { 5007169689Skan arg = build_indirect_ref (arg, 0); 5008169689Skan val = build2 (MODIFY_EXPR, TREE_TYPE (to), to, arg); 5009169689Skan } 5010132718Skan else 5011132718Skan { 5012169689Skan /* We must only copy the non-tail padding parts. 5013169689Skan Use __builtin_memcpy for the bitwise copy. */ 5014132718Skan 5015169689Skan tree args, t; 5016132718Skan 5017169689Skan args = tree_cons (NULL, TYPE_SIZE_UNIT (as_base), NULL); 5018169689Skan args = tree_cons (NULL, arg, args); 5019169689Skan t = build_unary_op (ADDR_EXPR, to, 0); 5020169689Skan args = tree_cons (NULL, t, args); 5021169689Skan t = implicit_built_in_decls[BUILT_IN_MEMCPY]; 5022169689Skan t = build_call (t, args); 5023132718Skan 5024169689Skan t = convert (TREE_TYPE (TREE_VALUE (args)), t); 5025169689Skan val = build_indirect_ref (t, 0); 5026169689Skan } 5027132718Skan 502850397Sobrien return val; 502950397Sobrien } 503050397Sobrien 503150397Sobrien mark_used (fn); 503250397Sobrien 503390075Sobrien if (DECL_VINDEX (fn) && (flags & LOOKUP_NONVIRTUAL) == 0) 503418334Speter { 503550397Sobrien tree t, *p = &TREE_VALUE (converted_args); 503690075Sobrien tree binfo = lookup_base (TREE_TYPE (TREE_TYPE (*p)), 5037117395Skan DECL_CONTEXT (fn), 503890075Sobrien ba_any, NULL); 5039169689Skan gcc_assert (binfo && binfo != error_mark_node); 5040169689Skan 504190075Sobrien *p = build_base_path (PLUS_EXPR, *p, binfo, 1); 504250397Sobrien if (TREE_SIDE_EFFECTS (*p)) 504350397Sobrien *p = save_expr (*p); 504450397Sobrien t = build_pointer_type (TREE_TYPE (fn)); 504590075Sobrien if (DECL_CONTEXT (fn) && TYPE_JAVA_INTERFACE (DECL_CONTEXT (fn))) 504690075Sobrien fn = build_java_interface_fn_ref (fn, *p); 504790075Sobrien else 5048169689Skan fn = build_vfn_ref (*p, DECL_VINDEX (fn)); 504950397Sobrien TREE_TYPE (fn) = t; 505018334Speter } 505150397Sobrien else if (DECL_INLINE (fn)) 505250397Sobrien fn = inline_conversion (fn); 505350397Sobrien else 505450397Sobrien fn = build_addr_func (fn); 505518334Speter 5056169689Skan return build_cxx_call (fn, converted_args); 5057117395Skan} 5058117395Skan 5059169689Skan/* Build and return a call to FN, using ARGS. This function performs 5060117395Skan no overload resolution, conversion, or other high-level 5061117395Skan operations. */ 5062117395Skan 5063117395Skantree 5064169689Skanbuild_cxx_call (tree fn, tree args) 5065117395Skan{ 5066117395Skan tree fndecl; 5067117395Skan 5068169689Skan fn = build_call (fn, args); 506918334Speter 5070117395Skan /* If this call might throw an exception, note that fact. */ 5071117395Skan fndecl = get_callee_fndecl (fn); 5072169689Skan if ((!fndecl || !TREE_NOTHROW (fndecl)) 5073117395Skan && at_function_scope_p () 5074117395Skan && cfun) 5075117395Skan cp_function_chain->can_throw = 1; 5076117395Skan 5077117395Skan /* Some built-in function calls will be evaluated at compile-time in 5078117395Skan fold (). */ 5079169689Skan fn = fold_if_not_in_template (fn); 5080117395Skan 508190075Sobrien if (VOID_TYPE_P (TREE_TYPE (fn))) 508250397Sobrien return fn; 5083117395Skan 508450397Sobrien fn = require_complete_type (fn); 508590075Sobrien if (fn == error_mark_node) 508690075Sobrien return error_mark_node; 5087117395Skan 508850397Sobrien if (IS_AGGR_TYPE (TREE_TYPE (fn))) 508950397Sobrien fn = build_cplus_new (TREE_TYPE (fn), fn); 509050397Sobrien return convert_from_reference (fn); 509118334Speter} 509218334Speter 5093117395Skanstatic GTY(()) tree java_iface_lookup_fn; 509490075Sobrien 509590075Sobrien/* Make an expression which yields the address of the Java interface 509690075Sobrien method FN. This is achieved by generating a call to libjava's 509790075Sobrien _Jv_LookupInterfaceMethodIdx(). */ 509890075Sobrien 509950397Sobrienstatic tree 5100132718Skanbuild_java_interface_fn_ref (tree fn, tree instance) 510190075Sobrien{ 510290075Sobrien tree lookup_args, lookup_fn, method, idx; 510390075Sobrien tree klass_ref, iface, iface_ref; 510490075Sobrien int i; 5105169689Skan 510690075Sobrien if (!java_iface_lookup_fn) 510790075Sobrien { 510890075Sobrien tree endlink = build_void_list_node (); 510990075Sobrien tree t = tree_cons (NULL_TREE, ptr_type_node, 511090075Sobrien tree_cons (NULL_TREE, ptr_type_node, 511190075Sobrien tree_cons (NULL_TREE, java_int_type_node, 511290075Sobrien endlink))); 5113169689Skan java_iface_lookup_fn 511490075Sobrien = builtin_function ("_Jv_LookupInterfaceMethodIdx", 511590075Sobrien build_function_type (ptr_type_node, t), 5116117395Skan 0, NOT_BUILT_IN, NULL, NULL_TREE); 511790075Sobrien } 511890075Sobrien 5119169689Skan /* Look up the pointer to the runtime java.lang.Class object for `instance'. 5120117395Skan This is the first entry in the vtable. */ 5121169689Skan klass_ref = build_vtbl_ref (build_indirect_ref (instance, 0), 512290075Sobrien integer_zero_node); 512390075Sobrien 5124117395Skan /* Get the java.lang.Class pointer for the interface being called. */ 512590075Sobrien iface = DECL_CONTEXT (fn); 5126132718Skan iface_ref = lookup_field (iface, get_identifier ("class$"), 0, false); 512790075Sobrien if (!iface_ref || TREE_CODE (iface_ref) != VAR_DECL 512890075Sobrien || DECL_CONTEXT (iface_ref) != iface) 512990075Sobrien { 5130169689Skan error ("could not find class$ field in java interface type %qT", 513190075Sobrien iface); 513290075Sobrien return error_mark_node; 513390075Sobrien } 5134169689Skan iface_ref = build_address (iface_ref); 5135169689Skan iface_ref = convert (build_pointer_type (iface), iface_ref); 5136169689Skan 5137117395Skan /* Determine the itable index of FN. */ 513890075Sobrien i = 1; 513990075Sobrien for (method = TYPE_METHODS (iface); method; method = TREE_CHAIN (method)) 514090075Sobrien { 514190075Sobrien if (!DECL_VIRTUAL_P (method)) 5142169689Skan continue; 514390075Sobrien if (fn == method) 5144169689Skan break; 514590075Sobrien i++; 514690075Sobrien } 5147169689Skan idx = build_int_cst (NULL_TREE, i); 514890075Sobrien 5149169689Skan lookup_args = tree_cons (NULL_TREE, klass_ref, 515090075Sobrien tree_cons (NULL_TREE, iface_ref, 515190075Sobrien build_tree_list (NULL_TREE, idx))); 5152169689Skan lookup_fn = build1 (ADDR_EXPR, 515390075Sobrien build_pointer_type (TREE_TYPE (java_iface_lookup_fn)), 515490075Sobrien java_iface_lookup_fn); 5155169689Skan return build3 (CALL_EXPR, ptr_type_node, lookup_fn, lookup_args, NULL_TREE); 515690075Sobrien} 515790075Sobrien 515890075Sobrien/* Returns the value to use for the in-charge parameter when making a 5159169689Skan call to a function with the indicated NAME. 516090075Sobrien 5161169689Skan FIXME:Can't we find a neater way to do this mapping? */ 5162169689Skan 516390075Sobrientree 5164132718Skanin_charge_arg_for_name (tree name) 516590075Sobrien{ 5166169689Skan if (name == base_ctor_identifier 516790075Sobrien || name == base_dtor_identifier) 516890075Sobrien return integer_zero_node; 516990075Sobrien else if (name == complete_ctor_identifier) 517090075Sobrien return integer_one_node; 517190075Sobrien else if (name == complete_dtor_identifier) 517290075Sobrien return integer_two_node; 517390075Sobrien else if (name == deleting_dtor_identifier) 517490075Sobrien return integer_three_node; 517590075Sobrien 517690075Sobrien /* This function should only be called with one of the names listed 517790075Sobrien above. */ 5178169689Skan gcc_unreachable (); 517990075Sobrien return NULL_TREE; 518090075Sobrien} 518190075Sobrien 5182117395Skan/* Build a call to a constructor, destructor, or an assignment 5183117395Skan operator for INSTANCE, an expression with class type. NAME 5184117395Skan indicates the special member function to call; ARGS are the 5185117395Skan arguments. BINFO indicates the base of INSTANCE that is to be 5186117395Skan passed as the `this' parameter to the member function called. 5187117395Skan 5188117395Skan FLAGS are the LOOKUP_* flags to use when processing the call. 5189117395Skan 5190117395Skan If NAME indicates a complete object constructor, INSTANCE may be 5191117395Skan NULL_TREE. In this case, the caller will call build_cplus_new to 5192117395Skan store the newly constructed object into a VAR_DECL. */ 5193117395Skan 5194117395Skantree 5195169689Skanbuild_special_member_call (tree instance, tree name, tree args, 5196117395Skan tree binfo, int flags) 519750397Sobrien{ 5198117395Skan tree fns; 5199117395Skan /* The type of the subobject to be constructed or destroyed. */ 5200117395Skan tree class_type; 5201117395Skan 5202169689Skan gcc_assert (name == complete_ctor_identifier 5203169689Skan || name == base_ctor_identifier 5204169689Skan || name == complete_dtor_identifier 5205169689Skan || name == base_dtor_identifier 5206169689Skan || name == deleting_dtor_identifier 5207169689Skan || name == ansi_assopname (NOP_EXPR)); 5208169689Skan if (TYPE_P (binfo)) 5209169689Skan { 5210169689Skan /* Resolve the name. */ 5211169689Skan if (!complete_type_or_else (binfo, NULL_TREE)) 5212169689Skan return error_mark_node; 5213117395Skan 5214169689Skan binfo = TYPE_BINFO (binfo); 5215169689Skan } 5216169689Skan 5217169689Skan gcc_assert (binfo != NULL_TREE); 5218169689Skan 5219117395Skan class_type = BINFO_TYPE (binfo); 5220117395Skan 5221117395Skan /* Handle the special case where INSTANCE is NULL_TREE. */ 5222117395Skan if (name == complete_ctor_identifier && !instance) 5223117395Skan { 5224169689Skan instance = build_int_cst (build_pointer_type (class_type), 0); 5225117395Skan instance = build1 (INDIRECT_REF, class_type, instance); 5226117395Skan } 5227119256Skan else 5228119256Skan { 5229169689Skan if (name == complete_dtor_identifier 5230119256Skan || name == base_dtor_identifier 5231119256Skan || name == deleting_dtor_identifier) 5232169689Skan gcc_assert (args == NULL_TREE); 5233117395Skan 5234132718Skan /* Convert to the base class, if necessary. */ 5235169689Skan if (!same_type_ignoring_top_level_qualifiers_p 5236119256Skan (TREE_TYPE (instance), BINFO_TYPE (binfo))) 5237132718Skan { 5238132718Skan if (name != ansi_assopname (NOP_EXPR)) 5239132718Skan /* For constructors and destructors, either the base is 5240132718Skan non-virtual, or it is virtual but we are doing the 5241132718Skan conversion from a constructor or destructor for the 5242132718Skan complete object. In either case, we can convert 5243132718Skan statically. */ 5244132718Skan instance = convert_to_base_statically (instance, binfo); 5245132718Skan else 5246132718Skan /* However, for assignment operators, we must convert 5247132718Skan dynamically if the base is virtual. */ 5248132718Skan instance = build_base_path (PLUS_EXPR, instance, 5249132718Skan binfo, /*nonnull=*/1); 5250132718Skan } 5251119256Skan } 5252117395Skan 5253169689Skan gcc_assert (instance != NULL_TREE); 5254117395Skan 5255117395Skan fns = lookup_fnfields (binfo, name, 1); 5256169689Skan 5257117395Skan /* When making a call to a constructor or destructor for a subobject 5258117395Skan that uses virtual base classes, pass down a pointer to a VTT for 5259117395Skan the subobject. */ 5260117395Skan if ((name == base_ctor_identifier 5261117395Skan || name == base_dtor_identifier) 5262169689Skan && CLASSTYPE_VBASECLASSES (class_type)) 5263117395Skan { 5264117395Skan tree vtt; 5265117395Skan tree sub_vtt; 5266117395Skan 5267117395Skan /* If the current function is a complete object constructor 5268117395Skan or destructor, then we fetch the VTT directly. 5269117395Skan Otherwise, we look it up using the VTT we were given. */ 5270117395Skan vtt = TREE_CHAIN (CLASSTYPE_VTABLES (current_class_type)); 5271117395Skan vtt = decay_conversion (vtt); 5272169689Skan vtt = build3 (COND_EXPR, TREE_TYPE (vtt), 5273169689Skan build2 (EQ_EXPR, boolean_type_node, 5274169689Skan current_in_charge_parm, integer_zero_node), 5275169689Skan current_vtt_parm, 5276169689Skan vtt); 5277169689Skan gcc_assert (BINFO_SUBVTT_INDEX (binfo)); 5278169689Skan sub_vtt = build2 (PLUS_EXPR, TREE_TYPE (vtt), vtt, 5279169689Skan BINFO_SUBVTT_INDEX (binfo)); 5280117395Skan 5281117395Skan args = tree_cons (NULL_TREE, sub_vtt, args); 5282117395Skan } 5283117395Skan 5284169689Skan return build_new_method_call (instance, fns, args, 5285169689Skan TYPE_BINFO (BINFO_TYPE (binfo)), 5286169689Skan flags, /*fn=*/NULL); 5287117395Skan} 5288117395Skan 5289132718Skan/* Return the NAME, as a C string. The NAME indicates a function that 5290132718Skan is a member of TYPE. *FREE_P is set to true if the caller must 5291169689Skan free the memory returned. 5292132718Skan 5293132718Skan Rather than go through all of this, we should simply set the names 5294132718Skan of constructors and destructors appropriately, and dispense with 5295132718Skan ctor_identifier, dtor_identifier, etc. */ 5296132718Skan 5297132718Skanstatic char * 5298132718Skanname_as_c_string (tree name, tree type, bool *free_p) 5299132718Skan{ 5300132718Skan char *pretty_name; 5301132718Skan 5302132718Skan /* Assume that we will not allocate memory. */ 5303132718Skan *free_p = false; 5304132718Skan /* Constructors and destructors are special. */ 5305132718Skan if (IDENTIFIER_CTOR_OR_DTOR_P (name)) 5306132718Skan { 5307169689Skan pretty_name 5308132718Skan = (char *) IDENTIFIER_POINTER (constructor_name (type)); 5309132718Skan /* For a destructor, add the '~'. */ 5310132718Skan if (name == complete_dtor_identifier 5311132718Skan || name == base_dtor_identifier 5312132718Skan || name == deleting_dtor_identifier) 5313132718Skan { 5314132718Skan pretty_name = concat ("~", pretty_name, NULL); 5315132718Skan /* Remember that we need to free the memory allocated. */ 5316132718Skan *free_p = true; 5317132718Skan } 5318132718Skan } 5319132718Skan else if (IDENTIFIER_TYPENAME_P (name)) 5320132718Skan { 5321132718Skan pretty_name = concat ("operator ", 5322132718Skan type_as_string (TREE_TYPE (name), 5323132718Skan TFF_PLAIN_IDENTIFIER), 5324132718Skan NULL); 5325132718Skan /* Remember that we need to free the memory allocated. */ 5326132718Skan *free_p = true; 5327132718Skan } 5328132718Skan else 5329132718Skan pretty_name = (char *) IDENTIFIER_POINTER (name); 5330132718Skan 5331132718Skan return pretty_name; 5332132718Skan} 5333132718Skan 5334169689Skan/* Build a call to "INSTANCE.FN (ARGS)". If FN_P is non-NULL, it will 5335169689Skan be set, upon return, to the function called. */ 5336117395Skan 5337117395Skantree 5338169689Skanbuild_new_method_call (tree instance, tree fns, tree args, 5339169689Skan tree conversion_path, int flags, 5340169689Skan tree *fn_p) 5341117395Skan{ 534250397Sobrien struct z_candidate *candidates = 0, *cand; 534350397Sobrien tree explicit_targs = NULL_TREE; 5344117395Skan tree basetype = NULL_TREE; 5345117395Skan tree access_binfo; 5346117395Skan tree optype; 5347117395Skan tree mem_args = NULL_TREE, instance_ptr; 5348132718Skan tree name; 534990075Sobrien tree user_args; 535090075Sobrien tree call; 5351132718Skan tree fn; 5352132718Skan tree class_type; 535350397Sobrien int template_only = 0; 5354132718Skan bool any_viable_p; 5355132718Skan tree orig_instance; 5356132718Skan tree orig_fns; 5357132718Skan tree orig_args; 5358169689Skan void *p; 535918334Speter 5360169689Skan gcc_assert (instance != NULL_TREE); 536152284Sobrien 5362169689Skan /* We don't know what function we're going to call, yet. */ 5363169689Skan if (fn_p) 5364169689Skan *fn_p = NULL_TREE; 5365169689Skan 5366169689Skan if (error_operand_p (instance) 5367132718Skan || error_operand_p (fns) 5368117395Skan || args == error_mark_node) 5369117395Skan return error_mark_node; 537018334Speter 5371117395Skan if (!BASELINK_P (fns)) 537250397Sobrien { 5373169689Skan error ("call to non-function %qD", fns); 5374117395Skan return error_mark_node; 5375117395Skan } 537618334Speter 5377169689Skan orig_instance = instance; 5378169689Skan orig_fns = fns; 5379169689Skan orig_args = args; 5380169689Skan 5381169689Skan /* Dismantle the baselink to collect all the information we need. */ 5382117395Skan if (!conversion_path) 5383117395Skan conversion_path = BASELINK_BINFO (fns); 5384117395Skan access_binfo = BASELINK_ACCESS_BINFO (fns); 5385117395Skan optype = BASELINK_OPTYPE (fns); 5386117395Skan fns = BASELINK_FUNCTIONS (fns); 5387117395Skan if (TREE_CODE (fns) == TEMPLATE_ID_EXPR) 5388117395Skan { 5389117395Skan explicit_targs = TREE_OPERAND (fns, 1); 5390117395Skan fns = TREE_OPERAND (fns, 0); 5391117395Skan template_only = 1; 539218334Speter } 5393169689Skan gcc_assert (TREE_CODE (fns) == FUNCTION_DECL 5394169689Skan || TREE_CODE (fns) == TEMPLATE_DECL 5395169689Skan || TREE_CODE (fns) == OVERLOAD); 5396169689Skan fn = get_first_fn (fns); 5397169689Skan name = DECL_NAME (fn); 539818334Speter 5399169689Skan basetype = TYPE_MAIN_VARIANT (TREE_TYPE (instance)); 5400169689Skan gcc_assert (CLASS_TYPE_P (basetype)); 540150397Sobrien 5402169689Skan if (processing_template_decl) 540318334Speter { 5404169689Skan instance = build_non_dependent_expr (instance); 5405169689Skan args = build_non_dependent_args (orig_args); 540618334Speter } 540718334Speter 5408169689Skan /* The USER_ARGS are the arguments we will display to users if an 5409169689Skan error occurs. The USER_ARGS should not include any 5410169689Skan compiler-generated arguments. The "this" pointer hasn't been 5411169689Skan added yet. However, we must remove the VTT pointer if this is a 5412169689Skan call to a base-class constructor or destructor. */ 5413169689Skan user_args = args; 541490075Sobrien if (IDENTIFIER_CTOR_OR_DTOR_P (name)) 541590075Sobrien { 5416117395Skan /* Callers should explicitly indicate whether they want to construct 5417117395Skan the complete object or just the part without virtual bases. */ 5418169689Skan gcc_assert (name != ctor_identifier); 5419117395Skan /* Similarly for destructors. */ 5420169689Skan gcc_assert (name != dtor_identifier); 5421169689Skan /* Remove the VTT pointer, if present. */ 5422169689Skan if ((name == base_ctor_identifier || name == base_dtor_identifier) 5423169689Skan && CLASSTYPE_VBASECLASSES (basetype)) 5424169689Skan user_args = TREE_CHAIN (user_args); 542590075Sobrien } 542690075Sobrien 5427169689Skan /* Process the argument list. */ 5428169689Skan args = resolve_args (args); 5429169689Skan if (args == error_mark_node) 5430169689Skan return error_mark_node; 5431169689Skan 5432169689Skan instance_ptr = build_this (instance); 5433169689Skan 5434132718Skan /* It's OK to call destructors on cv-qualified objects. Therefore, 5435132718Skan convert the INSTANCE_PTR to the unqualified type, if necessary. */ 5436132718Skan if (DECL_DESTRUCTOR_P (fn)) 543718334Speter { 5438132718Skan tree type = build_pointer_type (basetype); 5439132718Skan if (!same_type_p (type, TREE_TYPE (instance_ptr))) 5440132718Skan instance_ptr = build_nop (type, instance_ptr); 5441169689Skan name = complete_dtor_identifier; 5442132718Skan } 5443117395Skan 5444132718Skan class_type = (conversion_path ? BINFO_TYPE (conversion_path) : NULL_TREE); 5445132718Skan mem_args = tree_cons (NULL_TREE, instance_ptr, args); 544650397Sobrien 5447169689Skan /* Get the high-water mark for the CONVERSION_OBSTACK. */ 5448169689Skan p = conversion_obstack_alloc (0); 5449169689Skan 5450132718Skan for (fn = fns; fn; fn = OVL_NEXT (fn)) 5451132718Skan { 5452132718Skan tree t = OVL_CURRENT (fn); 5453132718Skan tree this_arglist; 545490075Sobrien 5455132718Skan /* We can end up here for copy-init of same or base class. */ 5456132718Skan if ((flags & LOOKUP_ONLYCONVERTING) 5457132718Skan && DECL_NONCONVERTING_P (t)) 5458132718Skan continue; 545950397Sobrien 5460132718Skan if (DECL_NONSTATIC_MEMBER_FUNCTION_P (t)) 5461132718Skan this_arglist = mem_args; 5462132718Skan else 5463132718Skan this_arglist = args; 5464132718Skan 5465132718Skan if (TREE_CODE (t) == TEMPLATE_DECL) 5466132718Skan /* A member template. */ 5467169689Skan add_template_candidate (&candidates, t, 5468132718Skan class_type, 5469132718Skan explicit_targs, 5470132718Skan this_arglist, optype, 5471169689Skan access_binfo, 5472132718Skan conversion_path, 5473132718Skan flags, 5474132718Skan DEDUCE_CALL); 5475132718Skan else if (! template_only) 5476169689Skan add_function_candidate (&candidates, t, 5477132718Skan class_type, 5478132718Skan this_arglist, 5479132718Skan access_binfo, 5480132718Skan conversion_path, 5481132718Skan flags); 548218334Speter } 548318334Speter 5484132718Skan candidates = splice_viable (candidates, pedantic, &any_viable_p); 5485132718Skan if (!any_viable_p) 548618334Speter { 548790075Sobrien if (!COMPLETE_TYPE_P (basetype)) 5488117395Skan cxx_incomplete_type_error (instance_ptr, basetype); 548952284Sobrien else 5490132718Skan { 5491132718Skan char *pretty_name; 5492132718Skan bool free_p; 5493132718Skan 5494132718Skan pretty_name = name_as_c_string (name, basetype, &free_p); 5495169689Skan error ("no matching function for call to %<%T::%s(%A)%#V%>", 5496132718Skan basetype, pretty_name, user_args, 5497132718Skan TREE_TYPE (TREE_TYPE (instance_ptr))); 5498132718Skan if (free_p) 5499132718Skan free (pretty_name); 5500132718Skan } 550150397Sobrien print_z_candidates (candidates); 5502169689Skan call = error_mark_node; 550318334Speter } 5504169689Skan else 550518334Speter { 5506169689Skan cand = tourney (candidates); 5507169689Skan if (cand == 0) 5508169689Skan { 5509169689Skan char *pretty_name; 5510169689Skan bool free_p; 5511132718Skan 5512169689Skan pretty_name = name_as_c_string (name, basetype, &free_p); 5513169689Skan error ("call of overloaded %<%s(%A)%> is ambiguous", pretty_name, 5514169689Skan user_args); 5515169689Skan print_z_candidates (candidates); 5516169689Skan if (free_p) 5517169689Skan free (pretty_name); 5518169689Skan call = error_mark_node; 5519169689Skan } 5520169689Skan else 5521169689Skan { 5522169689Skan fn = cand->fn; 552318334Speter 5524169689Skan if (!(flags & LOOKUP_NONVIRTUAL) 5525169689Skan && DECL_PURE_VIRTUAL_P (fn) 5526169689Skan && instance == current_class_ref 5527169689Skan && (DECL_CONSTRUCTOR_P (current_function_decl) 5528169689Skan || DECL_DESTRUCTOR_P (current_function_decl))) 5529169689Skan /* This is not an error, it is runtime undefined 5530169689Skan behavior. */ 5531169689Skan warning (0, (DECL_CONSTRUCTOR_P (current_function_decl) ? 5532169689Skan "abstract virtual %q#D called from constructor" 5533169689Skan : "abstract virtual %q#D called from destructor"), 5534169689Skan fn); 5535169689Skan 5536169689Skan if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE 5537169689Skan && is_dummy_object (instance_ptr)) 5538169689Skan { 5539169689Skan error ("cannot call member function %qD without object", 5540169689Skan fn); 5541169689Skan call = error_mark_node; 5542169689Skan } 5543169689Skan else 5544169689Skan { 5545169689Skan if (DECL_VINDEX (fn) && ! (flags & LOOKUP_NONVIRTUAL) 5546169689Skan && resolves_to_fixed_type_p (instance, 0)) 5547169689Skan flags |= LOOKUP_NONVIRTUAL; 5548169689Skan /* Now we know what function is being called. */ 5549169689Skan if (fn_p) 5550169689Skan *fn_p = fn; 5551169689Skan /* Build the actual CALL_EXPR. */ 5552169689Skan call = build_over_call (cand, flags); 5553169689Skan /* In an expression of the form `a->f()' where `f' turns 5554169689Skan out to be a static member function, `a' is 5555169689Skan none-the-less evaluated. */ 5556169689Skan if (TREE_CODE (TREE_TYPE (fn)) != METHOD_TYPE 5557169689Skan && !is_dummy_object (instance_ptr) 5558169689Skan && TREE_SIDE_EFFECTS (instance_ptr)) 5559169689Skan call = build2 (COMPOUND_EXPR, TREE_TYPE (call), 5560169689Skan instance_ptr, call); 5561169689Skan else if (call != error_mark_node 5562169689Skan && DECL_DESTRUCTOR_P (cand->fn) 5563169689Skan && !VOID_TYPE_P (TREE_TYPE (call))) 5564169689Skan /* An explicit call of the form "x->~X()" has type 5565169689Skan "void". However, on platforms where destructors 5566169689Skan return "this" (i.e., those where 5567169689Skan targetm.cxx.cdtor_returns_this is true), such calls 5568169689Skan will appear to have a return value of pointer type 5569169689Skan to the low-level call machinery. We do not want to 5570169689Skan change the low-level machinery, since we want to be 5571169689Skan able to optimize "delete f()" on such platforms as 5572169689Skan "operator delete(~X(f()))" (rather than generating 5573169689Skan "t = f(), ~X(t), operator delete (t)"). */ 5574169689Skan call = build_nop (void_type_node, call); 5575169689Skan } 5576169689Skan } 557752284Sobrien } 557850397Sobrien 5579169689Skan if (processing_template_decl && call != error_mark_node) 5580169689Skan call = (build_min_non_dep 5581169689Skan (CALL_EXPR, call, 5582169689Skan build_min_nt (COMPONENT_REF, orig_instance, orig_fns, NULL_TREE), 5583169689Skan orig_args, NULL_TREE)); 558450397Sobrien 5585169689Skan /* Free all the conversions we allocated. */ 5586169689Skan obstack_free (&conversion_obstack, p); 5587169689Skan 558890075Sobrien return call; 558950397Sobrien} 559050397Sobrien 5591132718Skan/* Returns true iff standard conversion sequence ICS1 is a proper 559250397Sobrien subsequence of ICS2. */ 559350397Sobrien 5594132718Skanstatic bool 5595169689Skanis_subseq (conversion *ics1, conversion *ics2) 559650397Sobrien{ 559750397Sobrien /* We can assume that a conversion of the same code 559850397Sobrien between the same types indicates a subsequence since we only get 559950397Sobrien here if the types we are converting from are the same. */ 560050397Sobrien 5601169689Skan while (ics1->kind == ck_rvalue 5602169689Skan || ics1->kind == ck_lvalue) 5603169689Skan ics1 = ics1->u.next; 560450397Sobrien 560550397Sobrien while (1) 560618334Speter { 5607169689Skan while (ics2->kind == ck_rvalue 5608169689Skan || ics2->kind == ck_lvalue) 5609169689Skan ics2 = ics2->u.next; 561050397Sobrien 5611169689Skan if (ics2->kind == ck_user 5612169689Skan || ics2->kind == ck_ambig 5613169689Skan || ics2->kind == ck_identity) 561450397Sobrien /* At this point, ICS1 cannot be a proper subsequence of 561550397Sobrien ICS2. We can get a USER_CONV when we are comparing the 561650397Sobrien second standard conversion sequence of two user conversion 561750397Sobrien sequences. */ 5618132718Skan return false; 561950397Sobrien 5620169689Skan ics2 = ics2->u.next; 562150397Sobrien 5622169689Skan if (ics2->kind == ics1->kind 5623169689Skan && same_type_p (ics2->type, ics1->type) 5624169689Skan && same_type_p (ics2->u.next->type, 5625169689Skan ics1->u.next->type)) 5626132718Skan return true; 562750397Sobrien } 562850397Sobrien} 562950397Sobrien 5630117395Skan/* Returns nonzero iff DERIVED is derived from BASE. The inputs may 563150397Sobrien be any _TYPE nodes. */ 563250397Sobrien 5633132718Skanbool 5634132718Skanis_properly_derived_from (tree derived, tree base) 563550397Sobrien{ 563650397Sobrien if (!IS_AGGR_TYPE_CODE (TREE_CODE (derived)) 563750397Sobrien || !IS_AGGR_TYPE_CODE (TREE_CODE (base))) 5638132718Skan return false; 563950397Sobrien 564050397Sobrien /* We only allow proper derivation here. The DERIVED_FROM_P macro 564150397Sobrien considers every class derived from itself. */ 564290075Sobrien return (!same_type_ignoring_top_level_qualifiers_p (derived, base) 564350397Sobrien && DERIVED_FROM_P (base, derived)); 564450397Sobrien} 564550397Sobrien 564650397Sobrien/* We build the ICS for an implicit object parameter as a pointer 564750397Sobrien conversion sequence. However, such a sequence should be compared 564850397Sobrien as if it were a reference conversion sequence. If ICS is the 564950397Sobrien implicit conversion sequence for an implicit object parameter, 565050397Sobrien modify it accordingly. */ 565150397Sobrien 565250397Sobrienstatic void 5653169689Skanmaybe_handle_implicit_object (conversion **ics) 565450397Sobrien{ 5655169689Skan if ((*ics)->this_p) 565650397Sobrien { 565750397Sobrien /* [over.match.funcs] 5658169689Skan 565950397Sobrien For non-static member functions, the type of the 566050397Sobrien implicit object parameter is "reference to cv X" 566150397Sobrien where X is the class of which the function is a 566250397Sobrien member and cv is the cv-qualification on the member 566350397Sobrien function declaration. */ 5664169689Skan conversion *t = *ics; 566590075Sobrien tree reference_type; 566690075Sobrien 566790075Sobrien /* The `this' parameter is a pointer to a class type. Make the 5668132718Skan implicit conversion talk about a reference to that same class 566990075Sobrien type. */ 5670169689Skan reference_type = TREE_TYPE (t->type); 567190075Sobrien reference_type = build_reference_type (reference_type); 567290075Sobrien 5673169689Skan if (t->kind == ck_qual) 5674169689Skan t = t->u.next; 5675169689Skan if (t->kind == ck_ptr) 5676169689Skan t = t->u.next; 5677169689Skan t = build_identity_conv (TREE_TYPE (t->type), NULL_TREE); 5678169689Skan t = direct_reference_binding (reference_type, t); 567950397Sobrien *ics = t; 568050397Sobrien } 568150397Sobrien} 568250397Sobrien 568390075Sobrien/* If *ICS is a REF_BIND set *ICS to the remainder of the conversion, 568490075Sobrien and return the type to which the reference refers. Otherwise, 568590075Sobrien leave *ICS unchanged and return NULL_TREE. */ 568650397Sobrien 568790075Sobrienstatic tree 5688169689Skanmaybe_handle_ref_bind (conversion **ics) 568950397Sobrien{ 5690169689Skan if ((*ics)->kind == ck_ref_bind) 569150397Sobrien { 5692169689Skan conversion *old_ics = *ics; 5693169689Skan tree type = TREE_TYPE (old_ics->type); 5694169689Skan *ics = old_ics->u.next; 5695169689Skan (*ics)->user_conv_p = old_ics->user_conv_p; 5696169689Skan (*ics)->bad_p = old_ics->bad_p; 569790075Sobrien return type; 569818334Speter } 569990075Sobrien 570090075Sobrien return NULL_TREE; 570150397Sobrien} 570218334Speter 570350397Sobrien/* Compare two implicit conversion sequences according to the rules set out in 570450397Sobrien [over.ics.rank]. Return values: 570550397Sobrien 570650397Sobrien 1: ics1 is better than ics2 570750397Sobrien -1: ics2 is better than ics1 570850397Sobrien 0: ics1 and ics2 are indistinguishable */ 570950397Sobrien 571050397Sobrienstatic int 5711169689Skancompare_ics (conversion *ics1, conversion *ics2) 571250397Sobrien{ 571350397Sobrien tree from_type1; 571450397Sobrien tree from_type2; 571550397Sobrien tree to_type1; 571650397Sobrien tree to_type2; 571750397Sobrien tree deref_from_type1 = NULL_TREE; 571852284Sobrien tree deref_from_type2 = NULL_TREE; 571952284Sobrien tree deref_to_type1 = NULL_TREE; 572052284Sobrien tree deref_to_type2 = NULL_TREE; 5721169689Skan conversion_rank rank1, rank2; 572250397Sobrien 5723117395Skan /* REF_BINDING is nonzero if the result of the conversion sequence 572450397Sobrien is a reference type. In that case TARGET_TYPE is the 572550397Sobrien type referred to by the reference. */ 572650397Sobrien tree target_type1; 572750397Sobrien tree target_type2; 572850397Sobrien 572950397Sobrien /* Handle implicit object parameters. */ 573050397Sobrien maybe_handle_implicit_object (&ics1); 573150397Sobrien maybe_handle_implicit_object (&ics2); 573250397Sobrien 573350397Sobrien /* Handle reference parameters. */ 573490075Sobrien target_type1 = maybe_handle_ref_bind (&ics1); 573590075Sobrien target_type2 = maybe_handle_ref_bind (&ics2); 573650397Sobrien 573750397Sobrien /* [over.ics.rank] 573850397Sobrien 573950397Sobrien When comparing the basic forms of implicit conversion sequences (as 574050397Sobrien defined in _over.best.ics_) 574150397Sobrien 574250397Sobrien --a standard conversion sequence (_over.ics.scs_) is a better 574350397Sobrien conversion sequence than a user-defined conversion sequence 574450397Sobrien or an ellipsis conversion sequence, and 5745169689Skan 574650397Sobrien --a user-defined conversion sequence (_over.ics.user_) is a 574750397Sobrien better conversion sequence than an ellipsis conversion sequence 574850397Sobrien (_over.ics.ellipsis_). */ 5749169689Skan rank1 = CONVERSION_RANK (ics1); 5750169689Skan rank2 = CONVERSION_RANK (ics2); 5751169689Skan 575290075Sobrien if (rank1 > rank2) 575350397Sobrien return -1; 575490075Sobrien else if (rank1 < rank2) 575550397Sobrien return 1; 575650397Sobrien 5757169689Skan if (rank1 == cr_bad) 575850397Sobrien { 575990075Sobrien /* XXX Isn't this an extension? */ 576050397Sobrien /* Both ICS are bad. We try to make a decision based on what 5761132718Skan would have happened if they'd been good. */ 5762169689Skan if (ics1->user_conv_p > ics2->user_conv_p 5763169689Skan || ics1->rank > ics2->rank) 576450397Sobrien return -1; 5765169689Skan else if (ics1->user_conv_p < ics2->user_conv_p 5766169689Skan || ics1->rank < ics2->rank) 576750397Sobrien return 1; 576850397Sobrien 576950397Sobrien /* We couldn't make up our minds; try to figure it out below. */ 577050397Sobrien } 577150397Sobrien 5772169689Skan if (ics1->ellipsis_p) 577350397Sobrien /* Both conversions are ellipsis conversions. */ 577450397Sobrien return 0; 577550397Sobrien 577650397Sobrien /* User-defined conversion sequence U1 is a better conversion sequence 577750397Sobrien than another user-defined conversion sequence U2 if they contain the 577850397Sobrien same user-defined conversion operator or constructor and if the sec- 577950397Sobrien ond standard conversion sequence of U1 is better than the second 578050397Sobrien standard conversion sequence of U2. */ 578150397Sobrien 5782169689Skan if (ics1->user_conv_p) 578350397Sobrien { 5784169689Skan conversion *t1; 5785169689Skan conversion *t2; 578650397Sobrien 5787169689Skan for (t1 = ics1; t1->kind != ck_user; t1 = t1->u.next) 5788169689Skan if (t1->kind == ck_ambig) 578950397Sobrien return 0; 5790169689Skan for (t2 = ics2; t2->kind != ck_user; t2 = t2->u.next) 5791169689Skan if (t2->kind == ck_ambig) 579250397Sobrien return 0; 579350397Sobrien 5794169689Skan if (t1->cand->fn != t2->cand->fn) 579550397Sobrien return 0; 579650397Sobrien 579750397Sobrien /* We can just fall through here, after setting up 579850397Sobrien FROM_TYPE1 and FROM_TYPE2. */ 5799169689Skan from_type1 = t1->type; 5800169689Skan from_type2 = t2->type; 580150397Sobrien } 580218334Speter else 580318334Speter { 5804169689Skan conversion *t1; 5805169689Skan conversion *t2; 580650397Sobrien 5807169689Skan /* We're dealing with two standard conversion sequences. 5808169689Skan 580950397Sobrien [over.ics.rank] 5810169689Skan 581150397Sobrien Standard conversion sequence S1 is a better conversion 581250397Sobrien sequence than standard conversion sequence S2 if 5813169689Skan 581450397Sobrien --S1 is a proper subsequence of S2 (comparing the conversion 581550397Sobrien sequences in the canonical form defined by _over.ics.scs_, 581650397Sobrien excluding any Lvalue Transformation; the identity 581750397Sobrien conversion sequence is considered to be a subsequence of 581850397Sobrien any non-identity conversion sequence */ 5819169689Skan 5820169689Skan t1 = ics1; 5821169689Skan while (t1->kind != ck_identity) 5822169689Skan t1 = t1->u.next; 5823169689Skan from_type1 = t1->type; 5824169689Skan 5825169689Skan t2 = ics2; 5826169689Skan while (t2->kind != ck_identity) 5827169689Skan t2 = t2->u.next; 5828169689Skan from_type2 = t2->type; 582918334Speter } 583018334Speter 583152284Sobrien if (same_type_p (from_type1, from_type2)) 583250397Sobrien { 583350397Sobrien if (is_subseq (ics1, ics2)) 583450397Sobrien return 1; 583550397Sobrien if (is_subseq (ics2, ics1)) 583650397Sobrien return -1; 583750397Sobrien } 583850397Sobrien /* Otherwise, one sequence cannot be a subsequence of the other; they 583950397Sobrien don't start with the same type. This can happen when comparing the 584050397Sobrien second standard conversion sequence in two user-defined conversion 584150397Sobrien sequences. */ 584218334Speter 584350397Sobrien /* [over.ics.rank] 584418334Speter 584550397Sobrien Or, if not that, 584618334Speter 584750397Sobrien --the rank of S1 is better than the rank of S2 (by the rules 584850397Sobrien defined below): 584950397Sobrien 585050397Sobrien Standard conversion sequences are ordered by their ranks: an Exact 585150397Sobrien Match is a better conversion than a Promotion, which is a better 585250397Sobrien conversion than a Conversion. 585350397Sobrien 585450397Sobrien Two conversion sequences with the same rank are indistinguishable 585550397Sobrien unless one of the following rules applies: 585650397Sobrien 585750397Sobrien --A conversion that is not a conversion of a pointer, or pointer 585850397Sobrien to member, to bool is better than another conversion that is such 5859169689Skan a conversion. 586050397Sobrien 586150397Sobrien The ICS_STD_RANK automatically handles the pointer-to-bool rule, 586250397Sobrien so that we do not have to check it explicitly. */ 5863169689Skan if (ics1->rank < ics2->rank) 586450397Sobrien return 1; 5865169689Skan else if (ics2->rank < ics1->rank) 586650397Sobrien return -1; 586750397Sobrien 5868169689Skan to_type1 = ics1->type; 5869169689Skan to_type2 = ics2->type; 587050397Sobrien 587150397Sobrien if (TYPE_PTR_P (from_type1) 587250397Sobrien && TYPE_PTR_P (from_type2) 587350397Sobrien && TYPE_PTR_P (to_type1) 587450397Sobrien && TYPE_PTR_P (to_type2)) 587518334Speter { 587650397Sobrien deref_from_type1 = TREE_TYPE (from_type1); 587750397Sobrien deref_from_type2 = TREE_TYPE (from_type2); 587850397Sobrien deref_to_type1 = TREE_TYPE (to_type1); 587950397Sobrien deref_to_type2 = TREE_TYPE (to_type2); 588050397Sobrien } 588150397Sobrien /* The rules for pointers to members A::* are just like the rules 588250397Sobrien for pointers A*, except opposite: if B is derived from A then 588350397Sobrien A::* converts to B::*, not vice versa. For that reason, we 588450397Sobrien switch the from_ and to_ variables here. */ 5885132718Skan else if ((TYPE_PTRMEM_P (from_type1) && TYPE_PTRMEM_P (from_type2) 5886132718Skan && TYPE_PTRMEM_P (to_type1) && TYPE_PTRMEM_P (to_type2)) 5887132718Skan || (TYPE_PTRMEMFUNC_P (from_type1) 5888132718Skan && TYPE_PTRMEMFUNC_P (from_type2) 5889132718Skan && TYPE_PTRMEMFUNC_P (to_type1) 5890132718Skan && TYPE_PTRMEMFUNC_P (to_type2))) 589150397Sobrien { 5892132718Skan deref_to_type1 = TYPE_PTRMEM_CLASS_TYPE (from_type1); 5893132718Skan deref_to_type2 = TYPE_PTRMEM_CLASS_TYPE (from_type2); 5894132718Skan deref_from_type1 = TYPE_PTRMEM_CLASS_TYPE (to_type1); 5895132718Skan deref_from_type2 = TYPE_PTRMEM_CLASS_TYPE (to_type2); 589650397Sobrien } 589750397Sobrien 589850397Sobrien if (deref_from_type1 != NULL_TREE 589950397Sobrien && IS_AGGR_TYPE_CODE (TREE_CODE (deref_from_type1)) 590050397Sobrien && IS_AGGR_TYPE_CODE (TREE_CODE (deref_from_type2))) 590150397Sobrien { 5902169689Skan /* This was one of the pointer or pointer-like conversions. 590350397Sobrien 590450397Sobrien [over.ics.rank] 5905169689Skan 590650397Sobrien --If class B is derived directly or indirectly from class A, 590750397Sobrien conversion of B* to A* is better than conversion of B* to 590850397Sobrien void*, and conversion of A* to void* is better than 590950397Sobrien conversion of B* to void*. */ 591050397Sobrien if (TREE_CODE (deref_to_type1) == VOID_TYPE 591150397Sobrien && TREE_CODE (deref_to_type2) == VOID_TYPE) 591218334Speter { 591350397Sobrien if (is_properly_derived_from (deref_from_type1, 591450397Sobrien deref_from_type2)) 591550397Sobrien return -1; 591650397Sobrien else if (is_properly_derived_from (deref_from_type2, 591750397Sobrien deref_from_type1)) 591850397Sobrien return 1; 591950397Sobrien } 592050397Sobrien else if (TREE_CODE (deref_to_type1) == VOID_TYPE 592150397Sobrien || TREE_CODE (deref_to_type2) == VOID_TYPE) 592250397Sobrien { 592352284Sobrien if (same_type_p (deref_from_type1, deref_from_type2)) 592418334Speter { 592550397Sobrien if (TREE_CODE (deref_to_type2) == VOID_TYPE) 592650397Sobrien { 592750397Sobrien if (is_properly_derived_from (deref_from_type1, 592850397Sobrien deref_to_type1)) 592950397Sobrien return 1; 593050397Sobrien } 593150397Sobrien /* We know that DEREF_TO_TYPE1 is `void' here. */ 593250397Sobrien else if (is_properly_derived_from (deref_from_type1, 593350397Sobrien deref_to_type2)) 593450397Sobrien return -1; 593518334Speter } 593618334Speter } 593750397Sobrien else if (IS_AGGR_TYPE_CODE (TREE_CODE (deref_to_type1)) 593850397Sobrien && IS_AGGR_TYPE_CODE (TREE_CODE (deref_to_type2))) 593918334Speter { 594050397Sobrien /* [over.ics.rank] 594118334Speter 594250397Sobrien --If class B is derived directly or indirectly from class A 594350397Sobrien and class C is derived directly or indirectly from B, 5944169689Skan 594550397Sobrien --conversion of C* to B* is better than conversion of C* to 5946169689Skan A*, 5947169689Skan 594850397Sobrien --conversion of B* to A* is better than conversion of C* to 594950397Sobrien A* */ 595052284Sobrien if (same_type_p (deref_from_type1, deref_from_type2)) 595150397Sobrien { 595250397Sobrien if (is_properly_derived_from (deref_to_type1, 595350397Sobrien deref_to_type2)) 595450397Sobrien return 1; 595550397Sobrien else if (is_properly_derived_from (deref_to_type2, 595650397Sobrien deref_to_type1)) 595750397Sobrien return -1; 595850397Sobrien } 595952284Sobrien else if (same_type_p (deref_to_type1, deref_to_type2)) 596050397Sobrien { 596150397Sobrien if (is_properly_derived_from (deref_from_type2, 596250397Sobrien deref_from_type1)) 596350397Sobrien return 1; 596450397Sobrien else if (is_properly_derived_from (deref_from_type1, 596550397Sobrien deref_from_type2)) 596650397Sobrien return -1; 596750397Sobrien } 596818334Speter } 596950397Sobrien } 597090075Sobrien else if (CLASS_TYPE_P (non_reference (from_type1)) 597152284Sobrien && same_type_p (from_type1, from_type2)) 597250397Sobrien { 597390075Sobrien tree from = non_reference (from_type1); 597490075Sobrien 597550397Sobrien /* [over.ics.rank] 5976169689Skan 597750397Sobrien --binding of an expression of type C to a reference of type 597850397Sobrien B& is better than binding an expression of type C to a 597950397Sobrien reference of type A& 598018334Speter 598150397Sobrien --conversion of C to B is better than conversion of C to A, */ 598290075Sobrien if (is_properly_derived_from (from, to_type1) 598390075Sobrien && is_properly_derived_from (from, to_type2)) 598418334Speter { 598550397Sobrien if (is_properly_derived_from (to_type1, to_type2)) 598650397Sobrien return 1; 598750397Sobrien else if (is_properly_derived_from (to_type2, to_type1)) 598850397Sobrien return -1; 598918334Speter } 599050397Sobrien } 599190075Sobrien else if (CLASS_TYPE_P (non_reference (to_type1)) 599252284Sobrien && same_type_p (to_type1, to_type2)) 599350397Sobrien { 599490075Sobrien tree to = non_reference (to_type1); 599590075Sobrien 599650397Sobrien /* [over.ics.rank] 599750397Sobrien 599850397Sobrien --binding of an expression of type B to a reference of type 599950397Sobrien A& is better than binding an expression of type C to a 6000169689Skan reference of type A&, 600150397Sobrien 6002169689Skan --conversion of B to A is better than conversion of C to A */ 600390075Sobrien if (is_properly_derived_from (from_type1, to) 600490075Sobrien && is_properly_derived_from (from_type2, to)) 600518334Speter { 600650397Sobrien if (is_properly_derived_from (from_type2, from_type1)) 600750397Sobrien return 1; 600850397Sobrien else if (is_properly_derived_from (from_type1, from_type2)) 600950397Sobrien return -1; 601050397Sobrien } 601150397Sobrien } 601218334Speter 601350397Sobrien /* [over.ics.rank] 601418334Speter 601550397Sobrien --S1 and S2 differ only in their qualification conversion and yield 601650397Sobrien similar types T1 and T2 (_conv.qual_), respectively, and the cv- 601750397Sobrien qualification signature of type T1 is a proper subset of the cv- 601850397Sobrien qualification signature of type T2 */ 6019169689Skan if (ics1->kind == ck_qual 6020169689Skan && ics2->kind == ck_qual 602152284Sobrien && same_type_p (from_type1, from_type2)) 602250397Sobrien return comp_cv_qual_signature (to_type1, to_type2); 602318334Speter 602450397Sobrien /* [over.ics.rank] 6025169689Skan 602650397Sobrien --S1 and S2 are reference bindings (_dcl.init.ref_), and the 602750397Sobrien types to which the references refer are the same type except for 602850397Sobrien top-level cv-qualifiers, and the type to which the reference 602950397Sobrien initialized by S2 refers is more cv-qualified than the type to 603050397Sobrien which the reference initialized by S1 refers */ 6031169689Skan 603290075Sobrien if (target_type1 && target_type2 603390075Sobrien && same_type_ignoring_top_level_qualifiers_p (to_type1, to_type2)) 603450397Sobrien return comp_cv_qualification (target_type2, target_type1); 603518334Speter 603650397Sobrien /* Neither conversion sequence is better than the other. */ 603750397Sobrien return 0; 603850397Sobrien} 603918334Speter 604050397Sobrien/* The source type for this standard conversion sequence. */ 604118334Speter 604250397Sobrienstatic tree 6043169689Skansource_type (conversion *t) 604450397Sobrien{ 6045169689Skan for (;; t = t->u.next) 604650397Sobrien { 6047169689Skan if (t->kind == ck_user 6048169689Skan || t->kind == ck_ambig 6049169689Skan || t->kind == ck_identity) 6050169689Skan return t->type; 605150397Sobrien } 6052169689Skan gcc_unreachable (); 605350397Sobrien} 605450397Sobrien 605550397Sobrien/* Note a warning about preferring WINNER to LOSER. We do this by storing 605650397Sobrien a pointer to LOSER and re-running joust to produce the warning if WINNER 605750397Sobrien is actually used. */ 605850397Sobrien 605950397Sobrienstatic void 6060132718Skanadd_warning (struct z_candidate *winner, struct z_candidate *loser) 606150397Sobrien{ 6062169689Skan candidate_warning *cw = (candidate_warning *) 6063169689Skan conversion_obstack_alloc (sizeof (candidate_warning)); 6064169689Skan cw->loser = loser; 6065169689Skan cw->next = winner->warnings; 6066169689Skan winner->warnings = cw; 606750397Sobrien} 606850397Sobrien 606950397Sobrien/* Compare two candidates for overloading as described in 607050397Sobrien [over.match.best]. Return values: 607150397Sobrien 607250397Sobrien 1: cand1 is better than cand2 607350397Sobrien -1: cand2 is better than cand1 607450397Sobrien 0: cand1 and cand2 are indistinguishable */ 607550397Sobrien 607650397Sobrienstatic int 6077132718Skanjoust (struct z_candidate *cand1, struct z_candidate *cand2, bool warn) 607850397Sobrien{ 607950397Sobrien int winner = 0; 6080169689Skan int off1 = 0, off2 = 0; 6081169689Skan size_t i; 6082169689Skan size_t len; 608350397Sobrien 608450397Sobrien /* Candidates that involve bad conversions are always worse than those 608550397Sobrien that don't. */ 608650397Sobrien if (cand1->viable > cand2->viable) 608750397Sobrien return 1; 608850397Sobrien if (cand1->viable < cand2->viable) 608950397Sobrien return -1; 609050397Sobrien 609152284Sobrien /* If we have two pseudo-candidates for conversions to the same type, 609290075Sobrien or two candidates for the same function, arbitrarily pick one. */ 609390075Sobrien if (cand1->fn == cand2->fn 6094169689Skan && (IS_TYPE_OR_DECL_P (cand1->fn))) 609552284Sobrien return 1; 609652284Sobrien 609750397Sobrien /* a viable function F1 609850397Sobrien is defined to be a better function than another viable function F2 if 609950397Sobrien for all arguments i, ICSi(F1) is not a worse conversion sequence than 610050397Sobrien ICSi(F2), and then */ 610150397Sobrien 610250397Sobrien /* for some argument j, ICSj(F1) is a better conversion sequence than 610350397Sobrien ICSj(F2) */ 610450397Sobrien 610590075Sobrien /* For comparing static and non-static member functions, we ignore 610690075Sobrien the implicit object parameter of the non-static function. The 610790075Sobrien standard says to pretend that the static function has an object 610890075Sobrien parm, but that won't work with operator overloading. */ 6109169689Skan len = cand1->num_convs; 6110169689Skan if (len != cand2->num_convs) 611150397Sobrien { 6112169689Skan int static_1 = DECL_STATIC_FUNCTION_P (cand1->fn); 6113169689Skan int static_2 = DECL_STATIC_FUNCTION_P (cand2->fn); 6114169689Skan 6115169689Skan gcc_assert (static_1 != static_2); 6116169689Skan 6117169689Skan if (static_1) 611850397Sobrien off2 = 1; 6119169689Skan else 612050397Sobrien { 612150397Sobrien off1 = 1; 612250397Sobrien --len; 612318334Speter } 612418334Speter } 612518334Speter 612650397Sobrien for (i = 0; i < len; ++i) 612718334Speter { 6128169689Skan conversion *t1 = cand1->convs[i + off1]; 6129169689Skan conversion *t2 = cand2->convs[i + off2]; 613050397Sobrien int comp = compare_ics (t1, t2); 613118334Speter 613250397Sobrien if (comp != 0) 613318334Speter { 613450397Sobrien if (warn_sign_promo 6135169689Skan && (CONVERSION_RANK (t1) + CONVERSION_RANK (t2) 6136169689Skan == cr_std + cr_promotion) 6137169689Skan && t1->kind == ck_std 6138169689Skan && t2->kind == ck_std 6139169689Skan && TREE_CODE (t1->type) == INTEGER_TYPE 6140169689Skan && TREE_CODE (t2->type) == INTEGER_TYPE 6141169689Skan && (TYPE_PRECISION (t1->type) 6142169689Skan == TYPE_PRECISION (t2->type)) 6143169689Skan && (TYPE_UNSIGNED (t1->u.next->type) 6144169689Skan || (TREE_CODE (t1->u.next->type) 614550397Sobrien == ENUMERAL_TYPE))) 614618334Speter { 6147169689Skan tree type = t1->u.next->type; 614850397Sobrien tree type1, type2; 614950397Sobrien struct z_candidate *w, *l; 615050397Sobrien if (comp > 0) 6151169689Skan type1 = t1->type, type2 = t2->type, 615250397Sobrien w = cand1, l = cand2; 615350397Sobrien else 6154169689Skan type1 = t2->type, type2 = t1->type, 615550397Sobrien w = cand2, l = cand1; 615650397Sobrien 615750397Sobrien if (warn) 615818334Speter { 6159169689Skan warning (OPT_Wsign_promo, "passing %qT chooses %qT over %qT", 6160169689Skan type, type1, type2); 6161169689Skan warning (OPT_Wsign_promo, " in call to %qD", w->fn); 616218334Speter } 616350397Sobrien else 616450397Sobrien add_warning (w, l); 616518334Speter } 616650397Sobrien 616750397Sobrien if (winner && comp != winner) 616850397Sobrien { 616950397Sobrien winner = 0; 617050397Sobrien goto tweak; 617150397Sobrien } 617250397Sobrien winner = comp; 617350397Sobrien } 617450397Sobrien } 617550397Sobrien 617650397Sobrien /* warn about confusing overload resolution for user-defined conversions, 617750397Sobrien either between a constructor and a conversion op, or between two 617850397Sobrien conversion ops. */ 6179132718Skan if (winner && warn_conversion && cand1->second_conv 6180132718Skan && (!DECL_CONSTRUCTOR_P (cand1->fn) || !DECL_CONSTRUCTOR_P (cand2->fn)) 6181132718Skan && winner != compare_ics (cand1->second_conv, cand2->second_conv)) 618250397Sobrien { 6183132718Skan struct z_candidate *w, *l; 6184132718Skan bool give_warning = false; 6185169689Skan 6186132718Skan if (winner == 1) 6187132718Skan w = cand1, l = cand2; 6188132718Skan else 6189132718Skan w = cand2, l = cand1; 6190169689Skan 6191132718Skan /* We don't want to complain about `X::operator T1 ()' 6192132718Skan beating `X::operator T2 () const', when T2 is a no less 6193132718Skan cv-qualified version of T1. */ 6194132718Skan if (DECL_CONTEXT (w->fn) == DECL_CONTEXT (l->fn) 6195132718Skan && !DECL_CONSTRUCTOR_P (w->fn) && !DECL_CONSTRUCTOR_P (l->fn)) 619650397Sobrien { 6197132718Skan tree t = TREE_TYPE (TREE_TYPE (l->fn)); 6198132718Skan tree f = TREE_TYPE (TREE_TYPE (w->fn)); 6199169689Skan 6200132718Skan if (TREE_CODE (t) == TREE_CODE (f) && POINTER_TYPE_P (t)) 620150397Sobrien { 6202132718Skan t = TREE_TYPE (t); 6203132718Skan f = TREE_TYPE (f); 620450397Sobrien } 6205132718Skan if (!comp_ptr_ttypes (t, f)) 6206132718Skan give_warning = true; 620718334Speter } 6208132718Skan else 6209132718Skan give_warning = true; 6210169689Skan 6211132718Skan if (!give_warning) 6212132718Skan /*NOP*/; 6213132718Skan else if (warn) 6214132718Skan { 6215169689Skan tree source = source_type (w->convs[0]); 6216132718Skan if (! DECL_CONSTRUCTOR_P (w->fn)) 6217132718Skan source = TREE_TYPE (source); 6218169689Skan warning (OPT_Wconversion, "choosing %qD over %qD", w->fn, l->fn); 6219169689Skan warning (OPT_Wconversion, " for conversion from %qT to %qT", 6220169689Skan source, w->second_conv->type); 6221169689Skan inform (" because conversion sequence for the argument is better"); 6222132718Skan } 6223132718Skan else 6224132718Skan add_warning (w, l); 622550397Sobrien } 622650397Sobrien 622750397Sobrien if (winner) 622850397Sobrien return winner; 622950397Sobrien 623050397Sobrien /* or, if not that, 623190075Sobrien F1 is a non-template function and F2 is a template function 623290075Sobrien specialization. */ 6233169689Skan 6234169689Skan if (!cand1->template_decl && cand2->template_decl) 623550397Sobrien return 1; 6236169689Skan else if (cand1->template_decl && !cand2->template_decl) 623750397Sobrien return -1; 6238169689Skan 623990075Sobrien /* or, if not that, 624090075Sobrien F1 and F2 are template functions and the function template for F1 is 624190075Sobrien more specialized than the template for F2 according to the partial 624290075Sobrien ordering rules. */ 6243169689Skan 6244169689Skan if (cand1->template_decl && cand2->template_decl) 624590075Sobrien { 6246169689Skan winner = more_specialized_fn 6247169689Skan (TI_TEMPLATE (cand1->template_decl), 6248169689Skan TI_TEMPLATE (cand2->template_decl), 6249169689Skan /* [temp.func.order]: The presence of unused ellipsis and default 625090075Sobrien arguments has no effect on the partial ordering of function 6251169689Skan templates. add_function_candidate() will not have 6252169689Skan counted the "this" argument for constructors. */ 6253169689Skan cand1->num_convs + DECL_CONSTRUCTOR_P (cand1->fn)); 625490075Sobrien if (winner) 6255169689Skan return winner; 625690075Sobrien } 625790075Sobrien 625850397Sobrien /* or, if not that, 625950397Sobrien the context is an initialization by user-defined conversion (see 626050397Sobrien _dcl.init_ and _over.match.user_) and the standard conversion 626150397Sobrien sequence from the return type of F1 to the destination type (i.e., 626250397Sobrien the type of the entity being initialized) is a better conversion 626350397Sobrien sequence than the standard conversion sequence from the return type 626450397Sobrien of F2 to the destination type. */ 626550397Sobrien 626690075Sobrien if (cand1->second_conv) 626790075Sobrien { 626890075Sobrien winner = compare_ics (cand1->second_conv, cand2->second_conv); 626990075Sobrien if (winner) 6270169689Skan return winner; 627190075Sobrien } 6272169689Skan 627390075Sobrien /* Check whether we can discard a builtin candidate, either because we 627490075Sobrien have two identical ones or matching builtin and non-builtin candidates. 627550397Sobrien 627690075Sobrien (Pedantically in the latter case the builtin which matched the user 627790075Sobrien function should not be added to the overload set, but we spot it here. 6278169689Skan 627990075Sobrien [over.match.oper] 628090075Sobrien ... the builtin candidates include ... 628190075Sobrien - do not have the same parameter type list as any non-template 628290075Sobrien non-member candidate. */ 6283169689Skan 628490075Sobrien if (TREE_CODE (cand1->fn) == IDENTIFIER_NODE 628590075Sobrien || TREE_CODE (cand2->fn) == IDENTIFIER_NODE) 628650397Sobrien { 628750397Sobrien for (i = 0; i < len; ++i) 6288169689Skan if (!same_type_p (cand1->convs[i]->type, 6289169689Skan cand2->convs[i]->type)) 629050397Sobrien break; 6291169689Skan if (i == cand1->num_convs) 629250397Sobrien { 629390075Sobrien if (cand1->fn == cand2->fn) 629490075Sobrien /* Two built-in candidates; arbitrarily pick one. */ 629590075Sobrien return 1; 629690075Sobrien else if (TREE_CODE (cand1->fn) == IDENTIFIER_NODE) 629790075Sobrien /* cand1 is built-in; prefer cand2. */ 629890075Sobrien return -1; 629990075Sobrien else 630090075Sobrien /* cand2 is built-in; prefer cand1. */ 630190075Sobrien return 1; 630250397Sobrien } 630350397Sobrien } 630450397Sobrien 630590075Sobrien /* If the two functions are the same (this can happen with declarations 630690075Sobrien in multiple scopes and arg-dependent lookup), arbitrarily choose one. */ 630790075Sobrien if (DECL_P (cand1->fn) && DECL_P (cand2->fn) 630890075Sobrien && equal_functions (cand1->fn, cand2->fn)) 630990075Sobrien return 1; 6310169689Skan 631150397Sobrientweak: 631250397Sobrien 631350397Sobrien /* Extension: If the worst conversion for one candidate is worse than the 631450397Sobrien worst conversion for the other, take the first. */ 631590075Sobrien if (!pedantic) 631650397Sobrien { 6317169689Skan conversion_rank rank1 = cr_identity, rank2 = cr_identity; 631890075Sobrien struct z_candidate *w = 0, *l = 0; 631950397Sobrien 632050397Sobrien for (i = 0; i < len; ++i) 632150397Sobrien { 6322169689Skan if (CONVERSION_RANK (cand1->convs[i+off1]) > rank1) 6323169689Skan rank1 = CONVERSION_RANK (cand1->convs[i+off1]); 6324169689Skan if (CONVERSION_RANK (cand2->convs[i + off2]) > rank2) 6325169689Skan rank2 = CONVERSION_RANK (cand2->convs[i + off2]); 632650397Sobrien } 632750397Sobrien if (rank1 < rank2) 632890075Sobrien winner = 1, w = cand1, l = cand2; 632950397Sobrien if (rank1 > rank2) 633090075Sobrien winner = -1, w = cand2, l = cand1; 633190075Sobrien if (winner) 6332169689Skan { 633390075Sobrien if (warn) 6334132718Skan { 6335132718Skan pedwarn ("\ 6336132718SkanISO C++ says that these are ambiguous, even \ 6337132718Skanthough the worst conversion for the first is better than \ 6338132718Skanthe worst conversion for the second:"); 6339132718Skan print_z_candidate (_("candidate 1:"), w); 6340132718Skan print_z_candidate (_("candidate 2:"), l); 6341132718Skan } 634290075Sobrien else 634390075Sobrien add_warning (w, l); 6344169689Skan return winner; 6345169689Skan } 634650397Sobrien } 634750397Sobrien 6348169689Skan gcc_assert (!winner); 634990075Sobrien return 0; 635050397Sobrien} 635150397Sobrien 635250397Sobrien/* Given a list of candidates for overloading, find the best one, if any. 635350397Sobrien This algorithm has a worst case of O(2n) (winner is last), and a best 635450397Sobrien case of O(n/2) (totally ambiguous); much better than a sorting 635550397Sobrien algorithm. */ 635650397Sobrien 635750397Sobrienstatic struct z_candidate * 6358132718Skantourney (struct z_candidate *candidates) 635950397Sobrien{ 636050397Sobrien struct z_candidate *champ = candidates, *challenger; 636150397Sobrien int fate; 636250397Sobrien int champ_compared_to_predecessor = 0; 636350397Sobrien 636450397Sobrien /* Walk through the list once, comparing each current champ to the next 636550397Sobrien candidate, knocking out a candidate or two with each comparison. */ 636650397Sobrien 636750397Sobrien for (challenger = champ->next; challenger; ) 636850397Sobrien { 636950397Sobrien fate = joust (champ, challenger, 0); 637050397Sobrien if (fate == 1) 637150397Sobrien challenger = challenger->next; 637218334Speter else 637318334Speter { 637450397Sobrien if (fate == 0) 637518334Speter { 637650397Sobrien champ = challenger->next; 637750397Sobrien if (champ == 0) 6378169689Skan return NULL; 637950397Sobrien champ_compared_to_predecessor = 0; 638018334Speter } 638118334Speter else 638250397Sobrien { 638350397Sobrien champ = challenger; 638450397Sobrien champ_compared_to_predecessor = 1; 638550397Sobrien } 638650397Sobrien 638750397Sobrien challenger = champ->next; 638818334Speter } 638950397Sobrien } 639018334Speter 639150397Sobrien /* Make sure the champ is better than all the candidates it hasn't yet 639250397Sobrien been compared to. */ 639318334Speter 6394169689Skan for (challenger = candidates; 6395169689Skan challenger != champ 639650397Sobrien && !(champ_compared_to_predecessor && challenger->next == champ); 639750397Sobrien challenger = challenger->next) 639850397Sobrien { 639950397Sobrien fate = joust (champ, challenger, 0); 640050397Sobrien if (fate != 1) 6401169689Skan return NULL; 640218334Speter } 640318334Speter 640450397Sobrien return champ; 640518334Speter} 640618334Speter 6407117395Skan/* Returns nonzero if things of type FROM can be converted to TO. */ 640890075Sobrien 6409132718Skanbool 6410132718Skancan_convert (tree to, tree from) 641118334Speter{ 6412169689Skan return can_convert_arg (to, from, NULL_TREE, LOOKUP_NORMAL); 641318334Speter} 641418334Speter 6415117395Skan/* Returns nonzero if ARG (of type FROM) can be converted to TO. */ 641690075Sobrien 6417132718Skanbool 6418169689Skancan_convert_arg (tree to, tree from, tree arg, int flags) 641918334Speter{ 6420169689Skan conversion *t; 6421169689Skan void *p; 6422169689Skan bool ok_p; 6423169689Skan 6424169689Skan /* Get the high-water mark for the CONVERSION_OBSTACK. */ 6425169689Skan p = conversion_obstack_alloc (0); 6426169689Skan 6427169689Skan t = implicit_conversion (to, from, arg, /*c_cast_p=*/false, 6428169689Skan flags); 6429169689Skan ok_p = (t && !t->bad_p); 6430169689Skan 6431169689Skan /* Free all the conversions we allocated. */ 6432169689Skan obstack_free (&conversion_obstack, p); 6433169689Skan 6434169689Skan return ok_p; 643518334Speter} 643690075Sobrien 643790075Sobrien/* Like can_convert_arg, but allows dubious conversions as well. */ 643890075Sobrien 6439132718Skanbool 6440132718Skancan_convert_arg_bad (tree to, tree from, tree arg) 644190075Sobrien{ 6442169689Skan conversion *t; 6443169689Skan void *p; 6444169689Skan 6445169689Skan /* Get the high-water mark for the CONVERSION_OBSTACK. */ 6446169689Skan p = conversion_obstack_alloc (0); 6447169689Skan /* Try to perform the conversion. */ 6448169689Skan t = implicit_conversion (to, from, arg, /*c_cast_p=*/false, 6449169689Skan LOOKUP_NORMAL); 6450169689Skan /* Free all the conversions we allocated. */ 6451169689Skan obstack_free (&conversion_obstack, p); 6452169689Skan 6453169689Skan return t != NULL; 645490075Sobrien} 645590075Sobrien 645690075Sobrien/* Convert EXPR to TYPE. Return the converted expression. 645790075Sobrien 645890075Sobrien Note that we allow bad conversions here because by the time we get to 645990075Sobrien this point we are committed to doing the conversion. If we end up 646090075Sobrien doing a bad conversion, convert_like will complain. */ 646190075Sobrien 646290075Sobrientree 6463132718Skanperform_implicit_conversion (tree type, tree expr) 646490075Sobrien{ 6465169689Skan conversion *conv; 6466169689Skan void *p; 6467169689Skan 6468132718Skan if (error_operand_p (expr)) 646990075Sobrien return error_mark_node; 6470169689Skan 6471169689Skan /* Get the high-water mark for the CONVERSION_OBSTACK. */ 6472169689Skan p = conversion_obstack_alloc (0); 6473169689Skan 647490075Sobrien conv = implicit_conversion (type, TREE_TYPE (expr), expr, 6475169689Skan /*c_cast_p=*/false, 647690075Sobrien LOOKUP_NORMAL); 647790075Sobrien if (!conv) 647890075Sobrien { 6479169689Skan error ("could not convert %qE to %qT", expr, type); 6480169689Skan expr = error_mark_node; 648190075Sobrien } 6482169689Skan else if (processing_template_decl) 6483169689Skan { 6484169689Skan /* In a template, we are only concerned about determining the 6485169689Skan type of non-dependent expressions, so we do not have to 6486169689Skan perform the actual conversion. */ 6487169689Skan if (TREE_TYPE (expr) != type) 6488169689Skan expr = build_nop (type, expr); 6489169689Skan } 6490169689Skan else 6491169689Skan expr = convert_like (conv, expr); 649290075Sobrien 6493169689Skan /* Free all the conversions we allocated. */ 6494169689Skan obstack_free (&conversion_obstack, p); 6495169689Skan 6496169689Skan return expr; 649790075Sobrien} 649890075Sobrien 6499117395Skan/* Convert EXPR to TYPE (as a direct-initialization) if that is 6500117395Skan permitted. If the conversion is valid, the converted expression is 6501122180Skan returned. Otherwise, NULL_TREE is returned, except in the case 6502169689Skan that TYPE is a class type; in that case, an error is issued. If 6503169689Skan C_CAST_P is true, then this direction initialization is taking 6504169689Skan place as part of a static_cast being attempted as part of a C-style 6505169689Skan cast. */ 6506117395Skan 6507117395Skantree 6508169689Skanperform_direct_initialization_if_possible (tree type, 6509169689Skan tree expr, 6510169689Skan bool c_cast_p) 6511117395Skan{ 6512169689Skan conversion *conv; 6513169689Skan void *p; 6514169689Skan 6515117395Skan if (type == error_mark_node || error_operand_p (expr)) 6516117395Skan return error_mark_node; 6517122180Skan /* [dcl.init] 6518122180Skan 6519122180Skan If the destination type is a (possibly cv-qualified) class type: 6520122180Skan 6521122180Skan -- If the initialization is direct-initialization ..., 6522122180Skan constructors are considered. ... If no constructor applies, or 6523122180Skan the overload resolution is ambiguous, the initialization is 6524122180Skan ill-formed. */ 6525122180Skan if (CLASS_TYPE_P (type)) 6526122180Skan { 6527122180Skan expr = build_special_member_call (NULL_TREE, complete_ctor_identifier, 6528122180Skan build_tree_list (NULL_TREE, expr), 6529169689Skan type, LOOKUP_NORMAL); 6530122180Skan return build_cplus_new (type, expr); 6531122180Skan } 6532169689Skan 6533169689Skan /* Get the high-water mark for the CONVERSION_OBSTACK. */ 6534169689Skan p = conversion_obstack_alloc (0); 6535169689Skan 6536117395Skan conv = implicit_conversion (type, TREE_TYPE (expr), expr, 6537169689Skan c_cast_p, 6538117395Skan LOOKUP_NORMAL); 6539169689Skan if (!conv || conv->bad_p) 6540169689Skan expr = NULL_TREE; 6541169689Skan else 6542169689Skan expr = convert_like_real (conv, expr, NULL_TREE, 0, 0, 6543169689Skan /*issue_conversion_warnings=*/false, 6544169689Skan c_cast_p); 6545169689Skan 6546169689Skan /* Free all the conversions we allocated. */ 6547169689Skan obstack_free (&conversion_obstack, p); 6548169689Skan 6549169689Skan return expr; 6550117395Skan} 6551117395Skan 6552117395Skan/* DECL is a VAR_DECL whose type is a REFERENCE_TYPE. The reference 6553117395Skan is being bound to a temporary. Create and return a new VAR_DECL 6554117395Skan with the indicated TYPE; this variable will store the value to 6555117395Skan which the reference is bound. */ 6556117395Skan 6557169689Skantree 6558117395Skanmake_temporary_var_for_ref_to_temp (tree decl, tree type) 6559117395Skan{ 6560117395Skan tree var; 6561117395Skan 6562117395Skan /* Create the variable. */ 6563169689Skan var = create_temporary_var (type); 6564117395Skan 6565117395Skan /* Register the variable. */ 6566117395Skan if (TREE_STATIC (decl)) 6567117395Skan { 6568117395Skan /* Namespace-scope or local static; give it a mangled name. */ 6569117395Skan tree name; 6570117395Skan 6571117395Skan TREE_STATIC (var) = 1; 6572117395Skan name = mangle_ref_init_variable (decl); 6573117395Skan DECL_NAME (var) = name; 6574117395Skan SET_DECL_ASSEMBLER_NAME (var, name); 6575117395Skan var = pushdecl_top_level (var); 6576117395Skan } 6577117395Skan else 6578169689Skan /* Create a new cleanup level if necessary. */ 6579169689Skan maybe_push_cleanup_level (type); 6580117395Skan 6581117395Skan return var; 6582117395Skan} 6583117395Skan 6584132718Skan/* Convert EXPR to the indicated reference TYPE, in a way suitable for 6585132718Skan initializing a variable of that TYPE. If DECL is non-NULL, it is 6586132718Skan the VAR_DECL being initialized with the EXPR. (In that case, the 6587132718Skan type of DECL will be TYPE.) If DECL is non-NULL, then CLEANUP must 6588132718Skan also be non-NULL, and with *CLEANUP initialized to NULL. Upon 6589169689Skan return, if *CLEANUP is no longer NULL, it will be an expression 6590169689Skan that should be pushed as a cleanup after the returned expression 6591169689Skan is used to initialize DECL. 659290075Sobrien 6593132718Skan Return the converted expression. */ 6594117395Skan 659590075Sobrientree 6596132718Skaninitialize_reference (tree type, tree expr, tree decl, tree *cleanup) 659790075Sobrien{ 6598169689Skan conversion *conv; 6599169689Skan void *p; 660090075Sobrien 6601117395Skan if (type == error_mark_node || error_operand_p (expr)) 6602117395Skan return error_mark_node; 6603117395Skan 6604169689Skan /* Get the high-water mark for the CONVERSION_OBSTACK. */ 6605169689Skan p = conversion_obstack_alloc (0); 6606169689Skan 6607169689Skan conv = reference_binding (type, TREE_TYPE (expr), expr, /*c_cast_p=*/false, 6608169689Skan LOOKUP_NORMAL); 6609169689Skan if (!conv || conv->bad_p) 661090075Sobrien { 6611122180Skan if (!(TYPE_QUALS (TREE_TYPE (type)) & TYPE_QUAL_CONST) 6612169689Skan && !real_lvalue_p (expr)) 6613169689Skan error ("invalid initialization of non-const reference of " 6614169689Skan "type %qT from a temporary of type %qT", 6615169689Skan type, TREE_TYPE (expr)); 6616122180Skan else 6617169689Skan error ("invalid initialization of reference of type " 6618169689Skan "%qT from expression of type %qT", type, 6619122180Skan TREE_TYPE (expr)); 662090075Sobrien return error_mark_node; 662190075Sobrien } 662290075Sobrien 6623117395Skan /* If DECL is non-NULL, then this special rule applies: 6624117395Skan 6625117395Skan [class.temporary] 6626117395Skan 6627117395Skan The temporary to which the reference is bound or the temporary 6628117395Skan that is the complete object to which the reference is bound 6629117395Skan persists for the lifetime of the reference. 6630117395Skan 6631117395Skan The temporaries created during the evaluation of the expression 6632117395Skan initializing the reference, except the temporary to which the 6633117395Skan reference is bound, are destroyed at the end of the 6634117395Skan full-expression in which they are created. 6635117395Skan 6636117395Skan In that case, we store the converted expression into a new 6637169689Skan VAR_DECL in a new scope. 6638117395Skan 6639117395Skan However, we want to be careful not to create temporaries when 6640117395Skan they are not required. For example, given: 6641117395Skan 6642169689Skan struct B {}; 6643117395Skan struct D : public B {}; 6644117395Skan D f(); 6645117395Skan const B& b = f(); 6646117395Skan 6647117395Skan there is no need to copy the return value from "f"; we can just 6648117395Skan extend its lifetime. Similarly, given: 6649117395Skan 6650117395Skan struct S {}; 6651117395Skan struct T { operator S(); }; 6652117395Skan T t; 6653117395Skan const S& s = t; 6654117395Skan 6655117395Skan we can extend the lifetime of the return value of the conversion 6656117395Skan operator. */ 6657169689Skan gcc_assert (conv->kind == ck_ref_bind); 6658117395Skan if (decl) 6659117395Skan { 6660117395Skan tree var; 6661117395Skan tree base_conv_type; 6662117395Skan 6663117395Skan /* Skip over the REF_BIND. */ 6664169689Skan conv = conv->u.next; 6665117395Skan /* If the next conversion is a BASE_CONV, skip that too -- but 6666117395Skan remember that the conversion was required. */ 6667169689Skan if (conv->kind == ck_base) 6668117395Skan { 6669169689Skan if (conv->check_copy_constructor_p) 6670132718Skan check_constructor_callable (TREE_TYPE (expr), expr); 6671169689Skan base_conv_type = conv->type; 6672169689Skan conv = conv->u.next; 6673117395Skan } 6674117395Skan else 6675117395Skan base_conv_type = NULL_TREE; 6676117395Skan /* Perform the remainder of the conversion. */ 6677132718Skan expr = convert_like_real (conv, expr, 6678132718Skan /*fn=*/NULL_TREE, /*argnum=*/0, 6679132718Skan /*inner=*/-1, 6680169689Skan /*issue_conversion_warnings=*/true, 6681169689Skan /*c_cast_p=*/false); 6682146895Skan if (error_operand_p (expr)) 6683169689Skan expr = error_mark_node; 6684169689Skan else 6685117395Skan { 6686169689Skan if (!real_lvalue_p (expr)) 6687117395Skan { 6688169689Skan tree init; 6689169689Skan tree type; 6690117395Skan 6691169689Skan /* Create the temporary variable. */ 6692169689Skan type = TREE_TYPE (expr); 6693169689Skan var = make_temporary_var_for_ref_to_temp (decl, type); 6694169689Skan layout_decl (var, 0); 6695169689Skan /* If the rvalue is the result of a function call it will be 6696169689Skan a TARGET_EXPR. If it is some other construct (such as a 6697169689Skan member access expression where the underlying object is 6698169689Skan itself the result of a function call), turn it into a 6699169689Skan TARGET_EXPR here. It is important that EXPR be a 6700169689Skan TARGET_EXPR below since otherwise the INIT_EXPR will 6701169689Skan attempt to make a bitwise copy of EXPR to initialize 6702169689Skan VAR. */ 6703169689Skan if (TREE_CODE (expr) != TARGET_EXPR) 6704169689Skan expr = get_target_expr (expr); 6705169689Skan /* Create the INIT_EXPR that will initialize the temporary 6706169689Skan variable. */ 6707169689Skan init = build2 (INIT_EXPR, type, var, expr); 6708169689Skan if (at_function_scope_p ()) 6709169689Skan { 6710169689Skan add_decl_expr (var); 6711169689Skan *cleanup = cxx_maybe_build_cleanup (var); 6712122180Skan 6713169689Skan /* We must be careful to destroy the temporary only 6714169689Skan after its initialization has taken place. If the 6715169689Skan initialization throws an exception, then the 6716169689Skan destructor should not be run. We cannot simply 6717169689Skan transform INIT into something like: 6718169689Skan 6719169689Skan (INIT, ({ CLEANUP_STMT; })) 6720169689Skan 6721169689Skan because emit_local_var always treats the 6722169689Skan initializer as a full-expression. Thus, the 6723169689Skan destructor would run too early; it would run at the 6724169689Skan end of initializing the reference variable, rather 6725169689Skan than at the end of the block enclosing the 6726169689Skan reference variable. 6727169689Skan 6728169689Skan The solution is to pass back a cleanup expression 6729169689Skan which the caller is responsible for attaching to 6730169689Skan the statement tree. */ 6731169689Skan } 6732169689Skan else 6733169689Skan { 6734169689Skan rest_of_decl_compilation (var, /*toplev=*/1, at_eof); 6735169689Skan if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)) 6736169689Skan static_aggregates = tree_cons (NULL_TREE, var, 6737169689Skan static_aggregates); 6738169689Skan } 6739169689Skan /* Use its address to initialize the reference variable. */ 6740169689Skan expr = build_address (var); 6741169689Skan if (base_conv_type) 6742169689Skan expr = convert_to_base (expr, 6743169689Skan build_pointer_type (base_conv_type), 6744169689Skan /*check_access=*/true, 6745169689Skan /*nonnull=*/true); 6746169689Skan expr = build2 (COMPOUND_EXPR, TREE_TYPE (expr), init, expr); 6747117395Skan } 6748117395Skan else 6749169689Skan /* Take the address of EXPR. */ 6750169689Skan expr = build_unary_op (ADDR_EXPR, expr, 0); 6751169689Skan /* If a BASE_CONV was required, perform it now. */ 6752169689Skan if (base_conv_type) 6753169689Skan expr = (perform_implicit_conversion 6754169689Skan (build_pointer_type (base_conv_type), expr)); 6755169689Skan expr = build_nop (type, expr); 6756117395Skan } 6757117395Skan } 6758169689Skan else 6759169689Skan /* Perform the conversion. */ 6760169689Skan expr = convert_like (conv, expr); 6761117395Skan 6762169689Skan /* Free all the conversions we allocated. */ 6763169689Skan obstack_free (&conversion_obstack, p); 6764169689Skan 6765169689Skan return expr; 676690075Sobrien} 6767117395Skan 6768117395Skan#include "gt-cp-call.h" 6769