call.c revision 161651
118334Speter/* Functions related to invoking methods and overloaded functions.
2132718Skan   Copyright (C) 1987, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3146895Skan   1999, 2000, 2001, 2002, 2003, 2004, 2005 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
2118334Speterthe Free Software Foundation, 59 Temple Place - Suite 330,
2218334SpeterBoston, MA 02111-1307, 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
43132718Skanstatic struct z_candidate * tourney (struct z_candidate *);
44132718Skanstatic int equal_functions (tree, tree);
45132718Skanstatic int joust (struct z_candidate *, struct z_candidate *, bool);
46132718Skanstatic int compare_ics (tree, tree);
47132718Skanstatic tree build_over_call (struct z_candidate *, int);
48132718Skanstatic tree build_java_interface_fn_ref (tree, tree);
49117395Skan#define convert_like(CONV, EXPR)				\
50117395Skan  convert_like_real ((CONV), (EXPR), NULL_TREE, 0, 0, 		\
51117395Skan		     /*issue_conversion_warnings=*/true)
52117395Skan#define convert_like_with_context(CONV, EXPR, FN, ARGNO)	\
53117395Skan  convert_like_real ((CONV), (EXPR), (FN), (ARGNO), 0, 		\
54117395Skan		     /*issue_conversion_warnings=*/true)
55117395Skanstatic tree convert_like_real (tree, tree, tree, int, int, bool);
56132718Skanstatic void op_error (enum tree_code, enum tree_code, tree, tree,
57132718Skan			    tree, const char *);
58132718Skanstatic tree build_object_call (tree, tree);
59132718Skanstatic tree resolve_args (tree);
60132718Skanstatic struct z_candidate *build_user_type_conversion_1 (tree, tree, int);
61132718Skanstatic void print_z_candidate (const char *, struct z_candidate *);
62132718Skanstatic void print_z_candidates (struct z_candidate *);
63132718Skanstatic tree build_this (tree);
64132718Skanstatic struct z_candidate *splice_viable (struct z_candidate *, bool, bool *);
65132718Skanstatic bool any_strictly_viable (struct z_candidate *);
66132718Skanstatic struct z_candidate *add_template_candidate
67132718Skan        (struct z_candidate **, tree, tree, tree, tree, tree,
68132718Skan         tree, tree, int, unification_kind_t);
69132718Skanstatic struct z_candidate *add_template_candidate_real
70132718Skan	(struct z_candidate **, tree, tree, tree, tree, tree,
71132718Skan           tree, tree, int, tree, unification_kind_t);
72132718Skanstatic struct z_candidate *add_template_conv_candidate
73132718Skan        (struct z_candidate **, tree, tree, tree, tree, tree, tree);
74117395Skanstatic void add_builtin_candidates
75132718Skan	(struct z_candidate **, enum tree_code, enum tree_code,
76132718Skan	       tree, tree *, int);
77117395Skanstatic void add_builtin_candidate
78132718Skan	(struct z_candidate **, enum tree_code, enum tree_code,
79132718Skan	       tree, tree, tree, tree *, tree *, int);
80132718Skanstatic bool is_complete (tree);
81117395Skanstatic void build_builtin_candidate
82132718Skan	(struct z_candidate **, tree, tree, tree, tree *, tree *,
83132718Skan	       int);
84132718Skanstatic struct z_candidate *add_conv_candidate
85132718Skan	(struct z_candidate **, tree, tree, tree, tree, tree);
86132718Skanstatic struct z_candidate *add_function_candidate
87117395Skan	(struct z_candidate **, tree, tree, tree, tree, tree, int);
88132718Skanstatic tree implicit_conversion (tree, tree, tree, int);
89146895Skanstatic tree standard_conversion (tree, tree, tree, int);
90117395Skanstatic tree reference_binding (tree, tree, tree, int);
91132718Skanstatic tree build_conv (enum tree_code, tree, tree);
92132718Skanstatic bool is_subseq (tree, tree);
93132718Skanstatic tree maybe_handle_ref_bind (tree *);
94132718Skanstatic void maybe_handle_implicit_object (tree *);
95117395Skanstatic struct z_candidate *add_candidate
96132718Skan        (struct z_candidate **, tree, tree, tree, tree, tree, int);
97132718Skanstatic tree source_type (tree);
98132718Skanstatic void add_warning (struct z_candidate *, struct z_candidate *);
99132718Skanstatic bool reference_related_p (tree, tree);
100132718Skanstatic bool reference_compatible_p (tree, tree);
101132718Skanstatic tree convert_class_to_reference (tree, tree, tree);
102132718Skanstatic tree direct_reference_binding (tree, tree);
103132718Skanstatic bool promoted_arithmetic_type_p (tree);
104132718Skanstatic tree conditional_conversion (tree, tree);
105132718Skanstatic char *name_as_c_string (tree, tree, bool *);
106132718Skanstatic tree call_builtin_trap (void);
107132718Skanstatic tree prep_operand (tree);
108132718Skanstatic void add_candidates (tree, tree, tree, bool, tree, tree,
109132718Skan			    int, struct z_candidate **);
110117395Skanstatic tree merge_conversion_sequences (tree, tree);
111132718Skanstatic bool magic_varargs_p (tree);
112132718Skanstatic tree build_temp (tree, tree, int, void (**)(const char *, ...));
113132718Skanstatic void check_constructor_callable (tree, tree);
11418334Speter
11518334Spetertree
116132718Skanbuild_vfield_ref (tree datum, tree type)
11718334Speter{
11818334Speter  if (datum == error_mark_node)
11918334Speter    return error_mark_node;
12018334Speter
12118334Speter  if (TREE_CODE (TREE_TYPE (datum)) == REFERENCE_TYPE)
12218334Speter    datum = convert_from_reference (datum);
12318334Speter
124117395Skan  if (TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (type)
125117395Skan      && !same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (datum), type))
126117395Skan    datum = convert_to_base (datum, type, /*check_access=*/false);
12718334Speter
128117395Skan  return build (COMPONENT_REF, TREE_TYPE (TYPE_VFIELD (type)),
129117395Skan		datum, TYPE_VFIELD (type));
13018334Speter}
13118334Speter
13250397Sobrien/* Returns nonzero iff the destructor name specified in NAME
13350397Sobrien   (a BIT_NOT_EXPR) matches BASETYPE.  The operand of NAME can take many
13450397Sobrien   forms...  */
13550397Sobrien
136132718Skanbool
137132718Skancheck_dtor_name (tree basetype, tree name)
13850397Sobrien{
13950397Sobrien  name = TREE_OPERAND (name, 0);
14050397Sobrien
14152284Sobrien  /* Just accept something we've already complained about.  */
14252284Sobrien  if (name == error_mark_node)
143132718Skan    return true;
14452284Sobrien
14550397Sobrien  if (TREE_CODE (name) == TYPE_DECL)
14650397Sobrien    name = TREE_TYPE (name);
14790075Sobrien  else if (TYPE_P (name))
14850397Sobrien    /* OK */;
14950397Sobrien  else if (TREE_CODE (name) == IDENTIFIER_NODE)
15050397Sobrien    {
15150397Sobrien      if ((IS_AGGR_TYPE (basetype) && name == constructor_name (basetype))
15250397Sobrien	  || (TREE_CODE (basetype) == ENUMERAL_TYPE
15350397Sobrien	      && name == TYPE_IDENTIFIER (basetype)))
15450397Sobrien	name = basetype;
15550397Sobrien      else
15650397Sobrien	name = get_type_value (name);
15750397Sobrien    }
15890075Sobrien  /* In the case of:
15990075Sobrien
16090075Sobrien       template <class T> struct S { ~S(); };
16190075Sobrien       int i;
16290075Sobrien       i.~S();
16390075Sobrien
16490075Sobrien     NAME will be a class template.  */
16590075Sobrien  else if (DECL_CLASS_TEMPLATE_P (name))
166132718Skan    return false;
16750397Sobrien  else
16890075Sobrien    abort ();
16950397Sobrien
17050397Sobrien  if (name && TYPE_MAIN_VARIANT (basetype) == TYPE_MAIN_VARIANT (name))
171132718Skan    return true;
172132718Skan  return false;
17350397Sobrien}
17450397Sobrien
17550397Sobrien/* We want the address of a function or method.  We avoid creating a
17650397Sobrien   pointer-to-member function.  */
17750397Sobrien
17850397Sobrientree
179132718Skanbuild_addr_func (tree function)
18018334Speter{
18150397Sobrien  tree type = TREE_TYPE (function);
18218334Speter
18350397Sobrien  /* We have to do these by hand to avoid real pointer to member
18450397Sobrien     functions.  */
18550397Sobrien  if (TREE_CODE (type) == METHOD_TYPE)
18618334Speter    {
187132718Skan      if (TREE_CODE (function) == OFFSET_REF)
188132718Skan	{
189132718Skan	  tree object = build_address (TREE_OPERAND (function, 0));
190132718Skan	  return get_member_function_from_ptrfunc (&object,
191132718Skan						   TREE_OPERAND (function, 1));
192132718Skan	}
193132718Skan      function = build_address (function);
19418334Speter    }
19550397Sobrien  else
196132718Skan    function = decay_conversion (function);
19750397Sobrien
19850397Sobrien  return function;
19918334Speter}
20018334Speter
20150397Sobrien/* Build a CALL_EXPR, we can handle FUNCTION_TYPEs, METHOD_TYPEs, or
20250397Sobrien   POINTER_TYPE to those.  Note, pointer to member function types
20350397Sobrien   (TYPE_PTRMEMFUNC_P) must be handled by our callers.  */
20450397Sobrien
20550397Sobrientree
206132718Skanbuild_call (tree function, tree parms)
20718334Speter{
20850397Sobrien  int is_constructor = 0;
20990075Sobrien  int nothrow;
21050397Sobrien  tree tmp;
21150397Sobrien  tree decl;
21290075Sobrien  tree result_type;
213117395Skan  tree fntype;
21418334Speter
21550397Sobrien  function = build_addr_func (function);
21650397Sobrien
21750397Sobrien  if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function)))
21850397Sobrien    {
21950397Sobrien      sorry ("unable to call pointer to member function here");
22050397Sobrien      return error_mark_node;
22150397Sobrien    }
22250397Sobrien
223117395Skan  fntype = TREE_TYPE (TREE_TYPE (function));
224117395Skan  result_type = TREE_TYPE (fntype);
22590075Sobrien
22650397Sobrien  if (TREE_CODE (function) == ADDR_EXPR
22750397Sobrien      && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
22850397Sobrien    decl = TREE_OPERAND (function, 0);
22950397Sobrien  else
23050397Sobrien    decl = NULL_TREE;
23150397Sobrien
23290075Sobrien  /* We check both the decl and the type; a function may be known not to
23390075Sobrien     throw without being declared throw().  */
23490075Sobrien  nothrow = ((decl && TREE_NOTHROW (decl))
23590075Sobrien	     || TYPE_NOTHROW_P (TREE_TYPE (TREE_TYPE (function))));
23690075Sobrien
237117395Skan  if (decl && TREE_THIS_VOLATILE (decl) && cfun)
23896263Sobrien    current_function_returns_abnormally = 1;
23996263Sobrien
24090075Sobrien  if (decl && TREE_DEPRECATED (decl))
24190075Sobrien    warn_deprecated_use (decl);
242117395Skan  require_complete_eh_spec_types (fntype, decl);
24390075Sobrien
24450397Sobrien  if (decl && DECL_CONSTRUCTOR_P (decl))
24550397Sobrien    is_constructor = 1;
24650397Sobrien
24790075Sobrien  if (decl && ! TREE_USED (decl))
24890075Sobrien    {
24990075Sobrien      /* We invoke build_call directly for several library functions.
25090075Sobrien	 These may have been declared normally if we're building libgcc,
25190075Sobrien	 so we can't just check DECL_ARTIFICIAL.  */
25290075Sobrien      if (DECL_ARTIFICIAL (decl)
25390075Sobrien	  || !strncmp (IDENTIFIER_POINTER (DECL_NAME (decl)), "__", 2))
25490075Sobrien	mark_used (decl);
25590075Sobrien      else
25690075Sobrien	abort ();
25790075Sobrien    }
25852284Sobrien
25950397Sobrien  /* Don't pass empty class objects by value.  This is useful
26050397Sobrien     for tags in STL, which are used to control overload resolution.
26150397Sobrien     We don't need to handle other cases of copying empty classes.  */
26250397Sobrien  if (! decl || ! DECL_BUILT_IN (decl))
26350397Sobrien    for (tmp = parms; tmp; tmp = TREE_CHAIN (tmp))
26450397Sobrien      if (is_empty_class (TREE_TYPE (TREE_VALUE (tmp)))
26550397Sobrien	  && ! TREE_ADDRESSABLE (TREE_TYPE (TREE_VALUE (tmp))))
26650397Sobrien	{
26790075Sobrien	  tree t = build (EMPTY_CLASS_EXPR, TREE_TYPE (TREE_VALUE (tmp)));
26850397Sobrien	  TREE_VALUE (tmp) = build (COMPOUND_EXPR, TREE_TYPE (t),
26950397Sobrien				    TREE_VALUE (tmp), t);
27050397Sobrien	}
27150397Sobrien
272132718Skan  function = build (CALL_EXPR, result_type, function, parms);
27350397Sobrien  TREE_HAS_CONSTRUCTOR (function) = is_constructor;
27490075Sobrien  TREE_NOTHROW (function) = nothrow;
27550397Sobrien
27650397Sobrien  return function;
27718334Speter}
27818334Speter
27918334Speter/* Build something of the form ptr->method (args)
28018334Speter   or object.method (args).  This can also build
28118334Speter   calls to constructors, and find friends.
28218334Speter
28318334Speter   Member functions always take their class variable
28418334Speter   as a pointer.
28518334Speter
28618334Speter   INSTANCE is a class instance.
28718334Speter
28818334Speter   NAME is the name of the method desired, usually an IDENTIFIER_NODE.
28918334Speter
29018334Speter   PARMS help to figure out what that NAME really refers to.
29118334Speter
29218334Speter   BASETYPE_PATH, if non-NULL, contains a chain from the type of INSTANCE
29318334Speter   down to the real instance type to use for access checking.  We need this
294132718Skan   information to get protected accesses correct.
29518334Speter
29618334Speter   FLAGS is the logical disjunction of zero or more LOOKUP_
29718334Speter   flags.  See cp-tree.h for more info.
29818334Speter
29918334Speter   If this is all OK, calls build_function_call with the resolved
30018334Speter   member function.
30118334Speter
30218334Speter   This function must also handle being called to perform
30318334Speter   initialization, promotion/coercion of arguments, and
30418334Speter   instantiation of default parameters.
30518334Speter
30618334Speter   Note that NAME may refer to an instance variable name.  If
30718334Speter   `operator()()' is defined for the type of that field, then we return
30818334Speter   that result.  */
30950397Sobrien
31050397Sobrien/* New overloading code.  */
31118334Speter
312117395Skanstruct z_candidate GTY(()) {
313117395Skan  /* The FUNCTION_DECL that will be called if this candidate is
314117395Skan     selected by overload resolution.  */
31550397Sobrien  tree fn;
316132718Skan  /* The arguments to use when calling this function.  */
317132718Skan  tree args;
318132718Skan  /* The implicit conversion sequences for each of the arguments to
319132718Skan     FN.  */
32050397Sobrien  tree convs;
321132718Skan  /* If FN is a user-defined conversion, the standard conversion
322132718Skan     sequence from the type returned by FN to the desired destination
323132718Skan     type.  */
32450397Sobrien  tree second_conv;
32550397Sobrien  int viable;
326117395Skan  /* If FN is a member function, the binfo indicating the path used to
327117395Skan     qualify the name of FN at the call site.  This path is used to
328117395Skan     determine whether or not FN is accessible if it is selected by
329117395Skan     overload resolution.  The DECL_CONTEXT of FN will always be a
330117395Skan     (possibly improper) base of this binfo.  */
331117395Skan  tree access_path;
332117395Skan  /* If FN is a non-static member function, the binfo indicating the
333117395Skan     subobject to which the `this' pointer should be converted if FN
334117395Skan     is selected by overload resolution.  The type pointed to the by
335117395Skan     the `this' pointer must correspond to the most derived class
336117395Skan     indicated by the CONVERSION_PATH.  */
337117395Skan  tree conversion_path;
33850397Sobrien  tree template;
33950397Sobrien  tree warnings;
34050397Sobrien  struct z_candidate *next;
34150397Sobrien};
34250397Sobrien
34350397Sobrien#define IDENTITY_RANK 0
34450397Sobrien#define EXACT_RANK 1
34550397Sobrien#define PROMO_RANK 2
34650397Sobrien#define STD_RANK 3
34750397Sobrien#define PBOOL_RANK 4
34850397Sobrien#define USER_RANK 5
34950397Sobrien#define ELLIPSIS_RANK 6
35050397Sobrien#define BAD_RANK 7
35150397Sobrien
35250397Sobrien#define ICS_RANK(NODE)				\
35390075Sobrien  (ICS_BAD_FLAG (NODE) ? BAD_RANK   		\
35450397Sobrien   : ICS_ELLIPSIS_FLAG (NODE) ? ELLIPSIS_RANK	\
35550397Sobrien   : ICS_USER_FLAG (NODE) ? USER_RANK		\
35650397Sobrien   : ICS_STD_RANK (NODE))
35750397Sobrien
35850397Sobrien#define ICS_STD_RANK(NODE) TREE_COMPLEXITY (NODE)
35950397Sobrien
36050397Sobrien#define ICS_USER_FLAG(NODE) TREE_LANG_FLAG_0 (NODE)
36150397Sobrien#define ICS_ELLIPSIS_FLAG(NODE) TREE_LANG_FLAG_1 (NODE)
36250397Sobrien#define ICS_THIS_FLAG(NODE) TREE_LANG_FLAG_2 (NODE)
36350397Sobrien#define ICS_BAD_FLAG(NODE) TREE_LANG_FLAG_3 (NODE)
36450397Sobrien
36590075Sobrien/* In a REF_BIND or a BASE_CONV, this indicates that a temporary
36690075Sobrien   should be created to hold the result of the conversion.  */
36790075Sobrien#define NEED_TEMPORARY_P(NODE) TREE_LANG_FLAG_4 (NODE)
36890075Sobrien
369132718Skan/* TRUE in an IDENTITY_CONV or BASE_CONV if the copy constructor must
370132718Skan   be accessible, even though it is not being used.  */
371132718Skan#define CHECK_COPY_CONSTRUCTOR_P(NODE) TREE_LANG_FLAG_5 (NODE)
372132718Skan
373117395Skan#define USER_CONV_CAND(NODE) WRAPPER_ZC (TREE_OPERAND (NODE, 1))
37450397Sobrien#define USER_CONV_FN(NODE) (USER_CONV_CAND (NODE)->fn)
37550397Sobrien
376146895Skan/* Returns true iff T is a null pointer constant in the sense of
377146895Skan   [conv.ptr].  */
378146895Skan
379132718Skanbool
380132718Skannull_ptr_cst_p (tree t)
38150397Sobrien{
38290075Sobrien  /* [conv.ptr]
38390075Sobrien
38490075Sobrien     A null pointer constant is an integral constant expression
38590075Sobrien     (_expr.const_) rvalue of integer type that evaluates to zero.  */
386146895Skan  if (DECL_INTEGRAL_CONSTANT_VAR_P (t))
387146895Skan    t = decl_constant_value (t);
38850397Sobrien  if (t == null_node
38990075Sobrien      || (CP_INTEGRAL_TYPE_P (TREE_TYPE (t)) && integer_zerop (t)))
390132718Skan    return true;
391132718Skan  return false;
39250397Sobrien}
39350397Sobrien
39490075Sobrien
395117395Skan/* Returns nonzero if PARMLIST consists of only default parms and/or
396117395Skan   ellipsis.  */
39790075Sobrien
398132718Skanbool
399132718Skansufficient_parms_p (tree parmlist)
40090075Sobrien{
40190075Sobrien  for (; parmlist && parmlist != void_list_node;
40290075Sobrien       parmlist = TREE_CHAIN (parmlist))
40390075Sobrien    if (!TREE_PURPOSE (parmlist))
404132718Skan      return false;
405132718Skan  return true;
40690075Sobrien}
40790075Sobrien
40850397Sobrienstatic tree
409132718Skanbuild_conv (enum tree_code code, tree type, tree from)
41050397Sobrien{
41190075Sobrien  tree t;
41250397Sobrien  int rank = ICS_STD_RANK (from);
41390075Sobrien
41490075Sobrien  /* We can't use buildl1 here because CODE could be USER_CONV, which
41590075Sobrien     takes two arguments.  In that case, the caller is responsible for
41690075Sobrien     filling in the second argument.  */
41790075Sobrien  t = make_node (code);
41890075Sobrien  TREE_TYPE (t) = type;
41990075Sobrien  TREE_OPERAND (t, 0) = from;
42090075Sobrien
42150397Sobrien  switch (code)
42218334Speter    {
42350397Sobrien    case PTR_CONV:
42450397Sobrien    case PMEM_CONV:
42550397Sobrien    case BASE_CONV:
42650397Sobrien    case STD_CONV:
42750397Sobrien      if (rank < STD_RANK)
42850397Sobrien	rank = STD_RANK;
42950397Sobrien      break;
43050397Sobrien
43150397Sobrien    case QUAL_CONV:
43250397Sobrien      if (rank < EXACT_RANK)
43350397Sobrien	rank = EXACT_RANK;
43450397Sobrien
43550397Sobrien    default:
43650397Sobrien      break;
43750397Sobrien    }
43850397Sobrien  ICS_STD_RANK (t) = rank;
439117395Skan  ICS_USER_FLAG (t) = (code == USER_CONV || ICS_USER_FLAG (from));
44050397Sobrien  ICS_BAD_FLAG (t) = ICS_BAD_FLAG (from);
44150397Sobrien  return t;
44250397Sobrien}
44350397Sobrien
44490075Sobrientree
445132718Skanstrip_top_quals (tree t)
44650397Sobrien{
44750397Sobrien  if (TREE_CODE (t) == ARRAY_TYPE)
44850397Sobrien    return t;
449132718Skan  return cp_build_qualified_type (t, 0);
45050397Sobrien}
45150397Sobrien
45250397Sobrien/* Returns the standard conversion path (see [conv]) from type FROM to type
45350397Sobrien   TO, if any.  For proper handling of null pointer constants, you must
45450397Sobrien   also pass the expression EXPR to convert from.  */
45550397Sobrien
45650397Sobrienstatic tree
457146895Skanstandard_conversion (tree to, tree from, tree expr, int flags)
45850397Sobrien{
45950397Sobrien  enum tree_code fcode, tcode;
46050397Sobrien  tree conv;
461132718Skan  bool fromref = false;
46250397Sobrien
463132718Skan  to = non_reference (to);
46450397Sobrien  if (TREE_CODE (from) == REFERENCE_TYPE)
46550397Sobrien    {
466132718Skan      fromref = true;
46750397Sobrien      from = TREE_TYPE (from);
46850397Sobrien    }
46950397Sobrien  to = strip_top_quals (to);
47050397Sobrien  from = strip_top_quals (from);
47150397Sobrien
47252284Sobrien  if ((TYPE_PTRFN_P (to) || TYPE_PTRMEMFUNC_P (to))
47352284Sobrien      && expr && type_unknown_p (expr))
47452284Sobrien    {
475122180Skan      expr = instantiate_type (to, expr, tf_conv);
47652284Sobrien      if (expr == error_mark_node)
47752284Sobrien	return NULL_TREE;
47852284Sobrien      from = TREE_TYPE (expr);
47952284Sobrien    }
48052284Sobrien
48150397Sobrien  fcode = TREE_CODE (from);
48250397Sobrien  tcode = TREE_CODE (to);
48350397Sobrien
48450397Sobrien  conv = build1 (IDENTITY_CONV, from, expr);
48550397Sobrien
48650397Sobrien  if (fcode == FUNCTION_TYPE)
48750397Sobrien    {
48850397Sobrien      from = build_pointer_type (from);
48950397Sobrien      fcode = TREE_CODE (from);
49050397Sobrien      conv = build_conv (LVALUE_CONV, from, conv);
49150397Sobrien    }
49250397Sobrien  else if (fcode == ARRAY_TYPE)
49350397Sobrien    {
49450397Sobrien      from = build_pointer_type (TREE_TYPE (from));
49550397Sobrien      fcode = TREE_CODE (from);
49650397Sobrien      conv = build_conv (LVALUE_CONV, from, conv);
49750397Sobrien    }
49890075Sobrien  else if (fromref || (expr && lvalue_p (expr)))
49950397Sobrien    conv = build_conv (RVALUE_CONV, from, conv);
50050397Sobrien
501132718Skan   /* Allow conversion between `__complex__' data types.  */
50290075Sobrien  if (tcode == COMPLEX_TYPE && fcode == COMPLEX_TYPE)
50390075Sobrien    {
50490075Sobrien      /* The standard conversion sequence to convert FROM to TO is
50590075Sobrien         the standard conversion sequence to perform componentwise
50690075Sobrien         conversion.  */
50790075Sobrien      tree part_conv = standard_conversion
508146895Skan        (TREE_TYPE (to), TREE_TYPE (from), NULL_TREE, flags);
50990075Sobrien
51090075Sobrien      if (part_conv)
51190075Sobrien        {
51290075Sobrien          conv = build_conv (TREE_CODE (part_conv), to, conv);
51390075Sobrien          ICS_STD_RANK (conv) = ICS_STD_RANK (part_conv);
51490075Sobrien        }
51590075Sobrien      else
51690075Sobrien        conv = NULL_TREE;
51790075Sobrien
51890075Sobrien      return conv;
51990075Sobrien    }
52090075Sobrien
52190075Sobrien  if (same_type_p (from, to))
52250397Sobrien    return conv;
52350397Sobrien
524132718Skan  if ((tcode == POINTER_TYPE || TYPE_PTR_TO_MEMBER_P (to))
52550397Sobrien      && expr && null_ptr_cst_p (expr))
526132718Skan    conv = build_conv (STD_CONV, to, conv);
527132718Skan  else if (tcode == POINTER_TYPE && fcode == POINTER_TYPE
528132718Skan	   && TREE_CODE (TREE_TYPE (to)) == VECTOR_TYPE
529132718Skan	   && TREE_CODE (TREE_TYPE (from)) == VECTOR_TYPE
530132718Skan	   && ((*targetm.vector_opaque_p) (TREE_TYPE (to))
531132718Skan	       || (*targetm.vector_opaque_p) (TREE_TYPE (from))))
532132718Skan    conv = build_conv (STD_CONV, to, conv);
53390075Sobrien  else if ((tcode == INTEGER_TYPE && fcode == POINTER_TYPE)
53490075Sobrien	   || (tcode == POINTER_TYPE && fcode == INTEGER_TYPE))
53590075Sobrien    {
53690075Sobrien      /* For backwards brain damage compatibility, allow interconversion of
53790075Sobrien	 pointers and integers with a pedwarn.  */
53890075Sobrien      conv = build_conv (STD_CONV, to, conv);
53990075Sobrien      ICS_BAD_FLAG (conv) = 1;
54090075Sobrien    }
541132718Skan  else if (tcode == ENUMERAL_TYPE && fcode == INTEGER_TYPE)
54290075Sobrien    {
54390075Sobrien      /* For backwards brain damage compatibility, allow interconversion of
54490075Sobrien	 enums and integers with a pedwarn.  */
54590075Sobrien      conv = build_conv (STD_CONV, to, conv);
54690075Sobrien      ICS_BAD_FLAG (conv) = 1;
54790075Sobrien    }
548132718Skan  else if ((tcode == POINTER_TYPE && fcode == POINTER_TYPE)
549132718Skan	   || (TYPE_PTRMEM_P (to) && TYPE_PTRMEM_P (from)))
55050397Sobrien    {
551132718Skan      tree to_pointee;
552132718Skan      tree from_pointee;
55350397Sobrien
554132718Skan      if (tcode == POINTER_TYPE
555132718Skan	  && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (from),
556132718Skan							TREE_TYPE (to)))
55750397Sobrien	;
558132718Skan      else if (VOID_TYPE_P (TREE_TYPE (to))
559132718Skan	       && !TYPE_PTRMEM_P (from)
560132718Skan	       && TREE_CODE (TREE_TYPE (from)) != FUNCTION_TYPE)
56118334Speter	{
56250397Sobrien	  from = build_pointer_type
56352284Sobrien	    (cp_build_qualified_type (void_type_node,
56490075Sobrien				      cp_type_quals (TREE_TYPE (from))));
56550397Sobrien	  conv = build_conv (PTR_CONV, from, conv);
56618334Speter	}
567132718Skan      else if (TYPE_PTRMEM_P (from))
56818334Speter	{
569132718Skan	  tree fbase = TYPE_PTRMEM_CLASS_TYPE (from);
570132718Skan	  tree tbase = TYPE_PTRMEM_CLASS_TYPE (to);
57150397Sobrien
57296263Sobrien	  if (DERIVED_FROM_P (fbase, tbase)
57390075Sobrien	      && (same_type_ignoring_top_level_qualifiers_p
574132718Skan		  (TYPE_PTRMEM_POINTED_TO_TYPE (from),
575132718Skan		   TYPE_PTRMEM_POINTED_TO_TYPE (to))))
57650397Sobrien	    {
577132718Skan	      from = build_ptrmem_type (tbase,
578132718Skan					TYPE_PTRMEM_POINTED_TO_TYPE (from));
57950397Sobrien	      conv = build_conv (PMEM_CONV, from, conv);
58050397Sobrien	    }
581146895Skan	  else if (!same_type_p (fbase, tbase))
582146895Skan	    return NULL;
58318334Speter	}
58450397Sobrien      else if (IS_AGGR_TYPE (TREE_TYPE (from))
585122180Skan	       && IS_AGGR_TYPE (TREE_TYPE (to))
586122180Skan	       /* [conv.ptr]
587122180Skan
588122180Skan	          An rvalue of type "pointer to cv D," where D is a
589122180Skan		  class type, can be converted to an rvalue of type
590122180Skan		  "pointer to cv B," where B is a base class (clause
591122180Skan		  _class.derived_) of D.  If B is an inaccessible
592122180Skan		  (clause _class.access_) or ambiguous
593122180Skan		  (_class.member.lookup_) base class of D, a program
594122180Skan		  that necessitates this conversion is ill-formed.  */
595122180Skan	       /* Therefore, we use DERIVED_FROM_P, and not
596122180Skan		  ACESSIBLY_UNIQUELY_DERIVED_FROM_P, in this test.  */
597122180Skan	       && DERIVED_FROM_P (TREE_TYPE (to), TREE_TYPE (from)))
59850397Sobrien	{
599122180Skan	  from =
600122180Skan	    cp_build_qualified_type (TREE_TYPE (to),
601122180Skan				     cp_type_quals (TREE_TYPE (from)));
602122180Skan	  from = build_pointer_type (from);
603122180Skan	  conv = build_conv (PTR_CONV, from, conv);
60450397Sobrien	}
60518334Speter
606132718Skan      if (tcode == POINTER_TYPE)
607132718Skan	{
608132718Skan	  to_pointee = TREE_TYPE (to);
609132718Skan	  from_pointee = TREE_TYPE (from);
610132718Skan	}
611132718Skan      else
612132718Skan	{
613132718Skan	  to_pointee = TYPE_PTRMEM_POINTED_TO_TYPE (to);
614132718Skan	  from_pointee = TYPE_PTRMEM_POINTED_TO_TYPE (from);
615132718Skan	}
616132718Skan
61752284Sobrien      if (same_type_p (from, to))
61850397Sobrien	/* OK */;
619132718Skan      else if (comp_ptr_ttypes (to_pointee, from_pointee))
62050397Sobrien	conv = build_conv (QUAL_CONV, to, conv);
62152284Sobrien      else if (expr && string_conv_p (to, expr, 0))
62252284Sobrien	/* converting from string constant to char *.  */
62352284Sobrien	conv = build_conv (QUAL_CONV, to, conv);
624132718Skan      else if (ptr_reasonably_similar (to_pointee, from_pointee))
62518334Speter	{
62650397Sobrien	  conv = build_conv (PTR_CONV, to, conv);
62750397Sobrien	  ICS_BAD_FLAG (conv) = 1;
62818334Speter	}
62918334Speter      else
63050397Sobrien	return 0;
63150397Sobrien
63250397Sobrien      from = to;
63350397Sobrien    }
63450397Sobrien  else if (TYPE_PTRMEMFUNC_P (to) && TYPE_PTRMEMFUNC_P (from))
63550397Sobrien    {
63650397Sobrien      tree fromfn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (from));
63750397Sobrien      tree tofn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (to));
63850397Sobrien      tree fbase = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (fromfn)));
63950397Sobrien      tree tbase = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (tofn)));
64050397Sobrien
64196263Sobrien      if (!DERIVED_FROM_P (fbase, tbase)
64290075Sobrien	  || !same_type_p (TREE_TYPE (fromfn), TREE_TYPE (tofn))
64390075Sobrien	  || !compparms (TREE_CHAIN (TYPE_ARG_TYPES (fromfn)),
64490075Sobrien			 TREE_CHAIN (TYPE_ARG_TYPES (tofn)))
64590075Sobrien	  || cp_type_quals (fbase) != cp_type_quals (tbase))
64650397Sobrien	return 0;
64750397Sobrien
64890075Sobrien      from = cp_build_qualified_type (tbase, cp_type_quals (fbase));
649132718Skan      from = build_method_type_directly (from,
650132718Skan					 TREE_TYPE (fromfn),
651132718Skan					 TREE_CHAIN (TYPE_ARG_TYPES (fromfn)));
65250397Sobrien      from = build_ptrmemfunc_type (build_pointer_type (from));
65350397Sobrien      conv = build_conv (PMEM_CONV, from, conv);
65450397Sobrien    }
65550397Sobrien  else if (tcode == BOOLEAN_TYPE)
65650397Sobrien    {
657132718Skan      /* [conv.bool]
65850397Sobrien
659132718Skan          An rvalue of arithmetic, enumeration, pointer, or pointer to
660132718Skan	  member type can be converted to an rvalue of type bool.  */
661132718Skan      if (ARITHMETIC_TYPE_P (from)
662132718Skan	  || fcode == ENUMERAL_TYPE
663132718Skan	  || fcode == POINTER_TYPE
664132718Skan	  || TYPE_PTR_TO_MEMBER_P (from))
665132718Skan	{
666132718Skan	  conv = build_conv (STD_CONV, to, conv);
667132718Skan	  if (fcode == POINTER_TYPE
668132718Skan	      || TYPE_PTRMEM_P (from)
669132718Skan	      || (TYPE_PTRMEMFUNC_P (from)
670132718Skan		  && ICS_STD_RANK (conv) < PBOOL_RANK))
671132718Skan	    ICS_STD_RANK (conv) = PBOOL_RANK;
672132718Skan	  return conv;
673132718Skan	}
674132718Skan
675132718Skan      return NULL_TREE;
67650397Sobrien    }
67750397Sobrien  /* We don't check for ENUMERAL_TYPE here because there are no standard
67850397Sobrien     conversions to enum type.  */
67950397Sobrien  else if (tcode == INTEGER_TYPE || tcode == BOOLEAN_TYPE
68050397Sobrien	   || tcode == REAL_TYPE)
68150397Sobrien    {
68250397Sobrien      if (! (INTEGRAL_CODE_P (fcode) || fcode == REAL_TYPE))
68350397Sobrien	return 0;
68450397Sobrien      conv = build_conv (STD_CONV, to, conv);
68550397Sobrien
68650397Sobrien      /* Give this a better rank if it's a promotion.  */
687132718Skan      if (same_type_p (to, type_promotes_to (from))
68850397Sobrien	  && ICS_STD_RANK (TREE_OPERAND (conv, 0)) <= PROMO_RANK)
68950397Sobrien	ICS_STD_RANK (conv) = PROMO_RANK;
69050397Sobrien    }
691132718Skan  else if (fcode == VECTOR_TYPE && tcode == VECTOR_TYPE
692132718Skan      && ((*targetm.vector_opaque_p) (from)
693132718Skan	  || (*targetm.vector_opaque_p) (to)))
694132718Skan    return build_conv (STD_CONV, to, conv);
695146895Skan  else if (!(flags & LOOKUP_CONSTRUCTOR_CALLABLE)
696146895Skan	   && IS_AGGR_TYPE (to) && IS_AGGR_TYPE (from)
69790075Sobrien	   && is_properly_derived_from (from, to))
69850397Sobrien    {
69950397Sobrien      if (TREE_CODE (conv) == RVALUE_CONV)
70050397Sobrien	conv = TREE_OPERAND (conv, 0);
70150397Sobrien      conv = build_conv (BASE_CONV, to, conv);
70290075Sobrien      /* The derived-to-base conversion indicates the initialization
70390075Sobrien	 of a parameter with base type from an object of a derived
70490075Sobrien	 type.  A temporary object is created to hold the result of
70590075Sobrien	 the conversion.  */
70690075Sobrien      NEED_TEMPORARY_P (conv) = 1;
70750397Sobrien    }
70850397Sobrien  else
70950397Sobrien    return 0;
71050397Sobrien
71150397Sobrien  return conv;
71250397Sobrien}
71350397Sobrien
714117395Skan/* Returns nonzero if T1 is reference-related to T2.  */
71590075Sobrien
716132718Skanstatic bool
717132718Skanreference_related_p (tree t1, tree t2)
71890075Sobrien{
71990075Sobrien  t1 = TYPE_MAIN_VARIANT (t1);
72090075Sobrien  t2 = TYPE_MAIN_VARIANT (t2);
72190075Sobrien
72290075Sobrien  /* [dcl.init.ref]
72390075Sobrien
72490075Sobrien     Given types "cv1 T1" and "cv2 T2," "cv1 T1" is reference-related
72590075Sobrien     to "cv2 T2" if T1 is the same type as T2, or T1 is a base class
72690075Sobrien     of T2.  */
72790075Sobrien  return (same_type_p (t1, t2)
72890075Sobrien	  || (CLASS_TYPE_P (t1) && CLASS_TYPE_P (t2)
72990075Sobrien	      && DERIVED_FROM_P (t1, t2)));
73090075Sobrien}
73190075Sobrien
732117395Skan/* Returns nonzero if T1 is reference-compatible with T2.  */
73390075Sobrien
734132718Skanstatic bool
735132718Skanreference_compatible_p (tree t1, tree t2)
73690075Sobrien{
73790075Sobrien  /* [dcl.init.ref]
73890075Sobrien
73990075Sobrien     "cv1 T1" is reference compatible with "cv2 T2" if T1 is
74090075Sobrien     reference-related to T2 and cv1 is the same cv-qualification as,
74190075Sobrien     or greater cv-qualification than, cv2.  */
74290075Sobrien  return (reference_related_p (t1, t2)
74390075Sobrien	  && at_least_as_qualified_p (t1, t2));
74490075Sobrien}
74590075Sobrien
74690075Sobrien/* Determine whether or not the EXPR (of class type S) can be
74790075Sobrien   converted to T as in [over.match.ref].  */
74890075Sobrien
74990075Sobrienstatic tree
750132718Skanconvert_class_to_reference (tree t, tree s, tree expr)
75190075Sobrien{
75290075Sobrien  tree conversions;
75390075Sobrien  tree arglist;
75490075Sobrien  tree conv;
755117395Skan  tree reference_type;
75690075Sobrien  struct z_candidate *candidates;
75790075Sobrien  struct z_candidate *cand;
758132718Skan  bool any_viable_p;
75990075Sobrien
760117395Skan  conversions = lookup_conversions (s);
761117395Skan  if (!conversions)
762117395Skan    return NULL_TREE;
763117395Skan
76490075Sobrien  /* [over.match.ref]
76590075Sobrien
76690075Sobrien     Assuming that "cv1 T" is the underlying type of the reference
76790075Sobrien     being initialized, and "cv S" is the type of the initializer
76890075Sobrien     expression, with S a class type, the candidate functions are
76990075Sobrien     selected as follows:
77090075Sobrien
77190075Sobrien     --The conversion functions of S and its base classes are
77290075Sobrien       considered.  Those that are not hidden within S and yield type
77390075Sobrien       "reference to cv2 T2", where "cv1 T" is reference-compatible
77490075Sobrien       (_dcl.init.ref_) with "cv2 T2", are candidate functions.
77590075Sobrien
77690075Sobrien     The argument list has one argument, which is the initializer
77790075Sobrien     expression.  */
77890075Sobrien
77990075Sobrien  candidates = 0;
78090075Sobrien
78190075Sobrien  /* Conceptually, we should take the address of EXPR and put it in
78290075Sobrien     the argument list.  Unfortunately, however, that can result in
78390075Sobrien     error messages, which we should not issue now because we are just
78490075Sobrien     trying to find a conversion operator.  Therefore, we use NULL,
78590075Sobrien     cast to the appropriate type.  */
78690075Sobrien  arglist = build_int_2 (0, 0);
78790075Sobrien  TREE_TYPE (arglist) = build_pointer_type (s);
78890075Sobrien  arglist = build_tree_list (NULL_TREE, arglist);
789117395Skan
790117395Skan  reference_type = build_reference_type (t);
791117395Skan
792117395Skan  while (conversions)
79390075Sobrien    {
79490075Sobrien      tree fns = TREE_VALUE (conversions);
79590075Sobrien
79690075Sobrien      for (; fns; fns = OVL_NEXT (fns))
79790075Sobrien	{
79890075Sobrien	  tree f = OVL_CURRENT (fns);
79990075Sobrien	  tree t2 = TREE_TYPE (TREE_TYPE (f));
800117395Skan
801117395Skan	  cand = NULL;
80290075Sobrien
80390075Sobrien	  /* If this is a template function, try to get an exact
80490075Sobrien             match.  */
80590075Sobrien	  if (TREE_CODE (f) == TEMPLATE_DECL)
80690075Sobrien	    {
807117395Skan	      cand = add_template_candidate (&candidates,
808117395Skan					     f, s,
809117395Skan					     NULL_TREE,
810117395Skan					     arglist,
811117395Skan					     reference_type,
812117395Skan					     TYPE_BINFO (s),
813117395Skan					     TREE_PURPOSE (conversions),
814117395Skan					     LOOKUP_NORMAL,
815117395Skan					     DEDUCE_CONV);
81690075Sobrien
817117395Skan	      if (cand)
81890075Sobrien		{
81990075Sobrien		  /* Now, see if the conversion function really returns
82090075Sobrien		     an lvalue of the appropriate type.  From the
82190075Sobrien		     point of view of unification, simply returning an
82290075Sobrien		     rvalue of the right type is good enough.  */
823117395Skan		  f = cand->fn;
82490075Sobrien		  t2 = TREE_TYPE (TREE_TYPE (f));
82590075Sobrien		  if (TREE_CODE (t2) != REFERENCE_TYPE
82690075Sobrien		      || !reference_compatible_p (t, TREE_TYPE (t2)))
827117395Skan		    {
828117395Skan		      candidates = candidates->next;
829117395Skan		      cand = NULL;
830117395Skan		    }
83190075Sobrien		}
83290075Sobrien	    }
83390075Sobrien	  else if (TREE_CODE (t2) == REFERENCE_TYPE
83490075Sobrien		   && reference_compatible_p (t, TREE_TYPE (t2)))
835117395Skan	    cand = add_function_candidate (&candidates, f, s, arglist,
836117395Skan					   TYPE_BINFO (s),
837117395Skan					   TREE_PURPOSE (conversions),
838117395Skan					   LOOKUP_NORMAL);
839117395Skan
840117395Skan	  if (cand)
841132718Skan	    {
842132718Skan	      /* Build a standard conversion sequence indicating the
843132718Skan		 binding from the reference type returned by the
844132718Skan		 function to the desired REFERENCE_TYPE.  */
845132718Skan	      cand->second_conv
846132718Skan		= (direct_reference_binding
847132718Skan		   (reference_type,
848132718Skan		    build1 (IDENTITY_CONV,
849132718Skan			    TREE_TYPE (TREE_TYPE (TREE_TYPE (cand->fn))),
850132718Skan			    NULL_TREE)));
851132718Skan	      ICS_BAD_FLAG (cand->second_conv)
852132718Skan		|= ICS_BAD_FLAG (TREE_VEC_ELT (cand->convs, 0));
853132718Skan	    }
85490075Sobrien	}
855117395Skan      conversions = TREE_CHAIN (conversions);
85690075Sobrien    }
85790075Sobrien
858132718Skan  candidates = splice_viable (candidates, pedantic, &any_viable_p);
85990075Sobrien  /* If none of the conversion functions worked out, let our caller
86090075Sobrien     know.  */
861132718Skan  if (!any_viable_p)
86290075Sobrien    return NULL_TREE;
863132718Skan
86490075Sobrien  cand = tourney (candidates);
86590075Sobrien  if (!cand)
86690075Sobrien    return NULL_TREE;
86790075Sobrien
868132718Skan  /* Now that we know that this is the function we're going to use fix
869132718Skan     the dummy first argument.  */
870132718Skan  cand->args = tree_cons (NULL_TREE,
871132718Skan			  build_this (expr),
872132718Skan			  TREE_CHAIN (cand->args));
873132718Skan
874117395Skan  /* Build a user-defined conversion sequence representing the
875117395Skan     conversion.  */
876117395Skan  conv = build_conv (USER_CONV,
877117395Skan		     TREE_TYPE (TREE_TYPE (cand->fn)),
878117395Skan		     build1 (IDENTITY_CONV, TREE_TYPE (expr), expr));
879117395Skan  TREE_OPERAND (conv, 1) = build_zc_wrapper (cand);
880117395Skan
881117395Skan  /* Merge it with the standard conversion sequence from the
882117395Skan     conversion function's return type to the desired type.  */
883117395Skan  cand->second_conv = merge_conversion_sequences (conv, cand->second_conv);
884117395Skan
88590075Sobrien  if (cand->viable == -1)
88690075Sobrien    ICS_BAD_FLAG (conv) = 1;
887132718Skan
888117395Skan  return cand->second_conv;
88990075Sobrien}
89090075Sobrien
89190075Sobrien/* A reference of the indicated TYPE is being bound directly to the
89290075Sobrien   expression represented by the implicit conversion sequence CONV.
89390075Sobrien   Return a conversion sequence for this binding.  */
89490075Sobrien
89590075Sobrienstatic tree
896132718Skandirect_reference_binding (tree type, tree conv)
89790075Sobrien{
898117395Skan  tree t;
89990075Sobrien
900117395Skan  my_friendly_assert (TREE_CODE (type) == REFERENCE_TYPE, 20030306);
901117395Skan  my_friendly_assert (TREE_CODE (TREE_TYPE (conv)) != REFERENCE_TYPE,
902117395Skan		      20030306);
903132718Skan
904117395Skan  t = TREE_TYPE (type);
905117395Skan
90690075Sobrien  /* [over.ics.rank]
90790075Sobrien
90890075Sobrien     When a parameter of reference type binds directly
90990075Sobrien     (_dcl.init.ref_) to an argument expression, the implicit
91090075Sobrien     conversion sequence is the identity conversion, unless the
91190075Sobrien     argument expression has a type that is a derived class of the
91290075Sobrien     parameter type, in which case the implicit conversion sequence is
91390075Sobrien     a derived-to-base Conversion.
91490075Sobrien
91590075Sobrien     If the parameter binds directly to the result of applying a
91690075Sobrien     conversion function to the argument expression, the implicit
91790075Sobrien     conversion sequence is a user-defined conversion sequence
91890075Sobrien     (_over.ics.user_), with the second standard conversion sequence
91990075Sobrien     either an identity conversion or, if the conversion function
92090075Sobrien     returns an entity of a type that is a derived class of the
92190075Sobrien     parameter type, a derived-to-base conversion.  */
92290075Sobrien  if (!same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (conv)))
92390075Sobrien    {
92490075Sobrien      /* Represent the derived-to-base conversion.  */
92590075Sobrien      conv = build_conv (BASE_CONV, t, conv);
92690075Sobrien      /* We will actually be binding to the base-class subobject in
92790075Sobrien	 the derived class, so we mark this conversion appropriately.
92890075Sobrien	 That way, convert_like knows not to generate a temporary.  */
92990075Sobrien      NEED_TEMPORARY_P (conv) = 0;
93090075Sobrien    }
93190075Sobrien  return build_conv (REF_BIND, type, conv);
93290075Sobrien}
93390075Sobrien
93450397Sobrien/* Returns the conversion path from type FROM to reference type TO for
93550397Sobrien   purposes of reference binding.  For lvalue binding, either pass a
93690075Sobrien   reference type to FROM or an lvalue expression to EXPR.  If the
93790075Sobrien   reference will be bound to a temporary, NEED_TEMPORARY_P is set for
93890075Sobrien   the conversion returned.  */
93950397Sobrien
94050397Sobrienstatic tree
941117395Skanreference_binding (tree rto, tree rfrom, tree expr, int flags)
94250397Sobrien{
94390075Sobrien  tree conv = NULL_TREE;
94450397Sobrien  tree to = TREE_TYPE (rto);
94550397Sobrien  tree from = rfrom;
946132718Skan  bool related_p;
947132718Skan  bool compatible_p;
94890075Sobrien  cp_lvalue_kind lvalue_p = clk_none;
94950397Sobrien
95052284Sobrien  if (TREE_CODE (to) == FUNCTION_TYPE && expr && type_unknown_p (expr))
95152284Sobrien    {
95296263Sobrien      expr = instantiate_type (to, expr, tf_none);
95352284Sobrien      if (expr == error_mark_node)
95452284Sobrien	return NULL_TREE;
95552284Sobrien      from = TREE_TYPE (expr);
95652284Sobrien    }
95752284Sobrien
95850397Sobrien  if (TREE_CODE (from) == REFERENCE_TYPE)
95990075Sobrien    {
96090075Sobrien      /* Anything with reference type is an lvalue.  */
96190075Sobrien      lvalue_p = clk_ordinary;
96290075Sobrien      from = TREE_TYPE (from);
96390075Sobrien    }
96490075Sobrien  else if (expr)
96590075Sobrien    lvalue_p = real_lvalue_p (expr);
96650397Sobrien
96790075Sobrien  /* Figure out whether or not the types are reference-related and
96890075Sobrien     reference compatible.  We have do do this after stripping
96990075Sobrien     references from FROM.  */
97090075Sobrien  related_p = reference_related_p (to, from);
97190075Sobrien  compatible_p = reference_compatible_p (to, from);
97250397Sobrien
97390075Sobrien  if (lvalue_p && compatible_p)
97450397Sobrien    {
97590075Sobrien      /* [dcl.init.ref]
97690075Sobrien
97790075Sobrien	 If the initializer expression
97890075Sobrien
97990075Sobrien	 -- is an lvalue (but not an lvalue for a bit-field), and "cv1 T1"
98090075Sobrien	    is reference-compatible with "cv2 T2,"
98190075Sobrien
982132718Skan	 the reference is bound directly to the initializer expression
98390075Sobrien	 lvalue.  */
98450397Sobrien      conv = build1 (IDENTITY_CONV, from, expr);
98590075Sobrien      conv = direct_reference_binding (rto, conv);
986132718Skan      if ((lvalue_p & clk_bitfield) != 0
987132718Skan	  || ((lvalue_p & clk_packed) != 0 && !TYPE_PACKED (to)))
98890075Sobrien	/* For the purposes of overload resolution, we ignore the fact
989132718Skan	   this expression is a bitfield or packed field. (In particular,
99090075Sobrien	   [over.ics.ref] says specifically that a function with a
99190075Sobrien	   non-const reference parameter is viable even if the
99290075Sobrien	   argument is a bitfield.)
99350397Sobrien
99490075Sobrien	   However, when we actually call the function we must create
99590075Sobrien	   a temporary to which to bind the reference.  If the
99690075Sobrien	   reference is volatile, or isn't const, then we cannot make
99790075Sobrien	   a temporary, so we just issue an error when the conversion
99890075Sobrien	   actually occurs.  */
99990075Sobrien	NEED_TEMPORARY_P (conv) = 1;
1000132718Skan
100190075Sobrien      return conv;
100250397Sobrien    }
100390075Sobrien  else if (CLASS_TYPE_P (from) && !(flags & LOOKUP_NO_CONVERSION))
100490075Sobrien    {
100590075Sobrien      /* [dcl.init.ref]
100650397Sobrien
1007132718Skan	 If the initializer expression
100890075Sobrien
100990075Sobrien	 -- has a class type (i.e., T2 is a class type) can be
101090075Sobrien	    implicitly converted to an lvalue of type "cv3 T3," where
101190075Sobrien	    "cv1 T1" is reference-compatible with "cv3 T3".  (this
101290075Sobrien	    conversion is selected by enumerating the applicable
101390075Sobrien	    conversion functions (_over.match.ref_) and choosing the
101490075Sobrien	    best one through overload resolution.  (_over.match_).
101590075Sobrien
101690075Sobrien        the reference is bound to the lvalue result of the conversion
101790075Sobrien	in the second case.  */
101890075Sobrien      conv = convert_class_to_reference (to, from, expr);
101950397Sobrien      if (conv)
1020117395Skan	return conv;
102190075Sobrien    }
102250397Sobrien
102390075Sobrien  /* From this point on, we conceptually need temporaries, even if we
102490075Sobrien     elide them.  Only the cases above are "direct bindings".  */
102590075Sobrien  if (flags & LOOKUP_NO_TEMP_BIND)
102690075Sobrien    return NULL_TREE;
102790075Sobrien
102890075Sobrien  /* [over.ics.rank]
102990075Sobrien
103090075Sobrien     When a parameter of reference type is not bound directly to an
103190075Sobrien     argument expression, the conversion sequence is the one required
103290075Sobrien     to convert the argument expression to the underlying type of the
103390075Sobrien     reference according to _over.best.ics_.  Conceptually, this
103490075Sobrien     conversion sequence corresponds to copy-initializing a temporary
103590075Sobrien     of the underlying type with the argument expression.  Any
103690075Sobrien     difference in top-level cv-qualification is subsumed by the
103790075Sobrien     initialization itself and does not constitute a conversion.  */
103890075Sobrien
103990075Sobrien  /* [dcl.init.ref]
104090075Sobrien
104190075Sobrien     Otherwise, the reference shall be to a non-volatile const type.  */
104290075Sobrien  if (!CP_TYPE_CONST_NON_VOLATILE_P (to))
104390075Sobrien    return NULL_TREE;
104490075Sobrien
104590075Sobrien  /* [dcl.init.ref]
104690075Sobrien
104790075Sobrien     If the initializer expression is an rvalue, with T2 a class type,
104890075Sobrien     and "cv1 T1" is reference-compatible with "cv2 T2", the reference
104990075Sobrien     is bound in one of the following ways:
105090075Sobrien
105190075Sobrien     -- The reference is bound to the object represented by the rvalue
105290075Sobrien        or to a sub-object within that object.
105390075Sobrien
1054117395Skan     -- ...
1055132718Skan
1056117395Skan     We use the first alternative.  The implicit conversion sequence
1057117395Skan     is supposed to be same as we would obtain by generating a
1058117395Skan     temporary.  Fortunately, if the types are reference compatible,
1059117395Skan     then this is either an identity conversion or the derived-to-base
1060117395Skan     conversion, just as for direct binding.  */
106190075Sobrien  if (CLASS_TYPE_P (from) && compatible_p)
106290075Sobrien    {
106390075Sobrien      conv = build1 (IDENTITY_CONV, from, expr);
1064132718Skan      conv = direct_reference_binding (rto, conv);
1065132718Skan      if (!(flags & LOOKUP_CONSTRUCTOR_CALLABLE))
1066132718Skan	CHECK_COPY_CONSTRUCTOR_P (TREE_OPERAND (conv, 0)) = 1;
1067132718Skan      return conv;
106850397Sobrien    }
106950397Sobrien
107090075Sobrien  /* [dcl.init.ref]
107190075Sobrien
107290075Sobrien     Otherwise, a temporary of type "cv1 T1" is created and
107390075Sobrien     initialized from the initializer expression using the rules for a
107490075Sobrien     non-reference copy initialization.  If T1 is reference-related to
107590075Sobrien     T2, cv1 must be the same cv-qualification as, or greater
107690075Sobrien     cv-qualification than, cv2; otherwise, the program is ill-formed.  */
107790075Sobrien  if (related_p && !at_least_as_qualified_p (to, from))
107890075Sobrien    return NULL_TREE;
107990075Sobrien
108090075Sobrien  conv = implicit_conversion (to, from, expr, flags);
108190075Sobrien  if (!conv)
108290075Sobrien    return NULL_TREE;
108390075Sobrien
108490075Sobrien  conv = build_conv (REF_BIND, rto, conv);
108590075Sobrien  /* This reference binding, unlike those above, requires the
108690075Sobrien     creation of a temporary.  */
108790075Sobrien  NEED_TEMPORARY_P (conv) = 1;
108890075Sobrien
108950397Sobrien  return conv;
109050397Sobrien}
109150397Sobrien
109250397Sobrien/* Returns the implicit conversion sequence (see [over.ics]) from type FROM
109350397Sobrien   to type TO.  The optional expression EXPR may affect the conversion.
109450397Sobrien   FLAGS are the usual overloading flags.  Only LOOKUP_NO_CONVERSION is
109550397Sobrien   significant.  */
109650397Sobrien
109750397Sobrienstatic tree
1098132718Skanimplicit_conversion (tree to, tree from, tree expr, int flags)
109950397Sobrien{
110050397Sobrien  tree conv;
110150397Sobrien
110290075Sobrien  if (from == error_mark_node || to == error_mark_node
110390075Sobrien      || expr == error_mark_node)
110490075Sobrien    return NULL_TREE;
110590075Sobrien
110650397Sobrien  if (TREE_CODE (to) == REFERENCE_TYPE)
110750397Sobrien    conv = reference_binding (to, from, expr, flags);
110850397Sobrien  else
1109146895Skan    conv = standard_conversion (to, from, expr, flags);
111050397Sobrien
111150397Sobrien  if (conv)
1112132718Skan    return conv;
1113132718Skan
1114132718Skan  if (expr != NULL_TREE
1115132718Skan      && (IS_AGGR_TYPE (from)
1116132718Skan	  || IS_AGGR_TYPE (to))
1117132718Skan      && (flags & LOOKUP_NO_CONVERSION) == 0)
111850397Sobrien    {
1119117395Skan      struct z_candidate *cand;
1120117395Skan
112150397Sobrien      cand = build_user_type_conversion_1
112250397Sobrien	(to, expr, LOOKUP_ONLYCONVERTING);
112350397Sobrien      if (cand)
112450397Sobrien	conv = cand->second_conv;
112590075Sobrien
112690075Sobrien      /* We used to try to bind a reference to a temporary here, but that
112790075Sobrien	 is now handled by the recursive call to this function at the end
112890075Sobrien	 of reference_binding.  */
1129132718Skan      return conv;
113050397Sobrien    }
113118334Speter
1132132718Skan  return NULL_TREE;
113350397Sobrien}
113450397Sobrien
113550397Sobrien/* Add a new entry to the list of candidates.  Used by the add_*_candidate
113650397Sobrien   functions.  */
113750397Sobrien
113850397Sobrienstatic struct z_candidate *
1139117395Skanadd_candidate (struct z_candidate **candidates,
1140132718Skan	       tree fn, tree args, tree convs, tree access_path,
1141132718Skan	       tree conversion_path, int viable)
114250397Sobrien{
1143132718Skan  struct z_candidate *cand = ggc_alloc_cleared (sizeof (struct z_candidate));
114450397Sobrien
114550397Sobrien  cand->fn = fn;
1146132718Skan  cand->args = args;
114750397Sobrien  cand->convs = convs;
1148117395Skan  cand->access_path = access_path;
1149117395Skan  cand->conversion_path = conversion_path;
115050397Sobrien  cand->viable = viable;
1151117395Skan  cand->next = *candidates;
1152117395Skan  *candidates = cand;
115350397Sobrien
115450397Sobrien  return cand;
115550397Sobrien}
115650397Sobrien
115750397Sobrien/* Create an overload candidate for the function or method FN called with
115850397Sobrien   the argument list ARGLIST and add it to CANDIDATES.  FLAGS is passed on
115990075Sobrien   to implicit_conversion.
116050397Sobrien
116190075Sobrien   CTYPE, if non-NULL, is the type we want to pretend this function
116290075Sobrien   comes from for purposes of overload resolution.  */
116390075Sobrien
116450397Sobrienstatic struct z_candidate *
1165117395Skanadd_function_candidate (struct z_candidate **candidates,
1166117395Skan			tree fn, tree ctype, tree arglist,
1167117395Skan			tree access_path, tree conversion_path,
1168117395Skan			int flags)
116950397Sobrien{
117050397Sobrien  tree parmlist = TYPE_ARG_TYPES (TREE_TYPE (fn));
117150397Sobrien  int i, len;
117250397Sobrien  tree convs;
117352284Sobrien  tree parmnode, argnode;
1174132718Skan  tree orig_arglist;
117550397Sobrien  int viable = 1;
117650397Sobrien
1177117395Skan  /* Built-in functions that haven't been declared don't really
1178117395Skan     exist.  */
1179117395Skan  if (DECL_ANTICIPATED (fn))
1180117395Skan    return NULL;
1181117395Skan
118290075Sobrien  /* The `this', `in_chrg' and VTT arguments to constructors are not
118390075Sobrien     considered in overload resolution.  */
118450397Sobrien  if (DECL_CONSTRUCTOR_P (fn))
118550397Sobrien    {
118690075Sobrien      parmlist = skip_artificial_parms_for (fn, parmlist);
1187132718Skan      orig_arglist = arglist;
118890075Sobrien      arglist = skip_artificial_parms_for (fn, arglist);
118918334Speter    }
1190132718Skan  else
1191132718Skan    orig_arglist = arglist;
119250397Sobrien
119352284Sobrien  len = list_length (arglist);
119490075Sobrien  convs = make_tree_vec (len);
119550397Sobrien
119652284Sobrien  /* 13.3.2 - Viable functions [over.match.viable]
119752284Sobrien     First, to be a viable function, a candidate function shall have enough
119852284Sobrien     parameters to agree in number with the arguments in the list.
119952284Sobrien
120052284Sobrien     We need to check this first; otherwise, checking the ICSes might cause
120152284Sobrien     us to produce an ill-formed template instantiation.  */
120252284Sobrien
120352284Sobrien  parmnode = parmlist;
120450397Sobrien  for (i = 0; i < len; ++i)
120518334Speter    {
120652284Sobrien      if (parmnode == NULL_TREE || parmnode == void_list_node)
120752284Sobrien	break;
120852284Sobrien      parmnode = TREE_CHAIN (parmnode);
120952284Sobrien    }
121052284Sobrien
121152284Sobrien  if (i < len && parmnode)
121252284Sobrien    viable = 0;
121352284Sobrien
121452284Sobrien  /* Make sure there are default args for the rest of the parms.  */
121590075Sobrien  else if (!sufficient_parms_p (parmnode))
121690075Sobrien    viable = 0;
121752284Sobrien
121852284Sobrien  if (! viable)
121952284Sobrien    goto out;
122052284Sobrien
122152284Sobrien  /* Second, for F to be a viable function, there shall exist for each
122252284Sobrien     argument an implicit conversion sequence that converts that argument
122352284Sobrien     to the corresponding parameter of F.  */
122452284Sobrien
122552284Sobrien  parmnode = parmlist;
122652284Sobrien  argnode = arglist;
122752284Sobrien
122852284Sobrien  for (i = 0; i < len; ++i)
122952284Sobrien    {
123050397Sobrien      tree arg = TREE_VALUE (argnode);
123152284Sobrien      tree argtype = lvalue_type (arg);
123250397Sobrien      tree t;
123390075Sobrien      int is_this;
123418334Speter
123550397Sobrien      if (parmnode == void_list_node)
123650397Sobrien	break;
123752284Sobrien
123890075Sobrien      is_this = (i == 0 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
123990075Sobrien		 && ! DECL_CONSTRUCTOR_P (fn));
124090075Sobrien
124152284Sobrien      if (parmnode)
124252284Sobrien	{
124352284Sobrien	  tree parmtype = TREE_VALUE (parmnode);
124452284Sobrien
124590075Sobrien	  /* The type of the implicit object parameter ('this') for
124690075Sobrien	     overload resolution is not always the same as for the
124790075Sobrien	     function itself; conversion functions are considered to
124890075Sobrien	     be members of the class being converted, and functions
124990075Sobrien	     introduced by a using-declaration are considered to be
125090075Sobrien	     members of the class that uses them.
125152284Sobrien
125290075Sobrien	     Since build_over_call ignores the ICS for the `this'
125390075Sobrien	     parameter, we can just change the parm type.  */
125490075Sobrien	  if (ctype && is_this)
125552284Sobrien	    {
125652284Sobrien	      parmtype
125790075Sobrien		= build_qualified_type (ctype,
125852284Sobrien					TYPE_QUALS (TREE_TYPE (parmtype)));
125952284Sobrien	      parmtype = build_pointer_type (parmtype);
126052284Sobrien	    }
126152284Sobrien
126252284Sobrien	  t = implicit_conversion (parmtype, argtype, arg, flags);
126352284Sobrien	}
126450397Sobrien      else
126518334Speter	{
126650397Sobrien	  t = build1 (IDENTITY_CONV, argtype, arg);
126750397Sobrien	  ICS_ELLIPSIS_FLAG (t) = 1;
126818334Speter	}
126950397Sobrien
127090075Sobrien      if (t && is_this)
127150397Sobrien	ICS_THIS_FLAG (t) = 1;
127250397Sobrien
127350397Sobrien      TREE_VEC_ELT (convs, i) = t;
127450397Sobrien      if (! t)
127552284Sobrien	{
127652284Sobrien	  viable = 0;
127752284Sobrien	  break;
127852284Sobrien	}
127950397Sobrien
128050397Sobrien      if (ICS_BAD_FLAG (t))
128150397Sobrien	viable = -1;
128250397Sobrien
128350397Sobrien      if (parmnode)
128450397Sobrien	parmnode = TREE_CHAIN (parmnode);
128550397Sobrien      argnode = TREE_CHAIN (argnode);
128650397Sobrien    }
128750397Sobrien
128852284Sobrien out:
1289132718Skan  return add_candidate (candidates, fn, orig_arglist, convs, access_path,
1290117395Skan			conversion_path, viable);
129150397Sobrien}
129250397Sobrien
129350397Sobrien/* Create an overload candidate for the conversion function FN which will
129450397Sobrien   be invoked for expression OBJ, producing a pointer-to-function which
129550397Sobrien   will in turn be called with the argument list ARGLIST, and add it to
129652284Sobrien   CANDIDATES.  FLAGS is passed on to implicit_conversion.
129750397Sobrien
129852284Sobrien   Actually, we don't really care about FN; we care about the type it
129952284Sobrien   converts to.  There may be multiple conversion functions that will
130052284Sobrien   convert to that type, and we rely on build_user_type_conversion_1 to
130152284Sobrien   choose the best one; so when we create our candidate, we record the type
130252284Sobrien   instead of the function.  */
130352284Sobrien
130450397Sobrienstatic struct z_candidate *
1305132718Skanadd_conv_candidate (struct z_candidate **candidates, tree fn, tree obj,
1306132718Skan                    tree arglist, tree access_path, tree conversion_path)
130750397Sobrien{
130850397Sobrien  tree totype = TREE_TYPE (TREE_TYPE (fn));
130990075Sobrien  int i, len, viable, flags;
131090075Sobrien  tree parmlist, convs, parmnode, argnode;
131150397Sobrien
131290075Sobrien  for (parmlist = totype; TREE_CODE (parmlist) != FUNCTION_TYPE; )
131390075Sobrien    parmlist = TREE_TYPE (parmlist);
131490075Sobrien  parmlist = TYPE_ARG_TYPES (parmlist);
131590075Sobrien
131690075Sobrien  len = list_length (arglist) + 1;
131790075Sobrien  convs = make_tree_vec (len);
131890075Sobrien  parmnode = parmlist;
131990075Sobrien  argnode = arglist;
132090075Sobrien  viable = 1;
132190075Sobrien  flags = LOOKUP_NORMAL;
132290075Sobrien
132352284Sobrien  /* Don't bother looking up the same type twice.  */
1324117395Skan  if (*candidates && (*candidates)->fn == totype)
1325117395Skan    return NULL;
132652284Sobrien
132750397Sobrien  for (i = 0; i < len; ++i)
132850397Sobrien    {
132950397Sobrien      tree arg = i == 0 ? obj : TREE_VALUE (argnode);
133050397Sobrien      tree argtype = lvalue_type (arg);
133150397Sobrien      tree t;
133250397Sobrien
133350397Sobrien      if (i == 0)
133450397Sobrien	t = implicit_conversion (totype, argtype, arg, flags);
133550397Sobrien      else if (parmnode == void_list_node)
133650397Sobrien	break;
133750397Sobrien      else if (parmnode)
133850397Sobrien	t = implicit_conversion (TREE_VALUE (parmnode), argtype, arg, flags);
133918334Speter      else
134018334Speter	{
134150397Sobrien	  t = build1 (IDENTITY_CONV, argtype, arg);
134250397Sobrien	  ICS_ELLIPSIS_FLAG (t) = 1;
134318334Speter	}
134418334Speter
134550397Sobrien      TREE_VEC_ELT (convs, i) = t;
134650397Sobrien      if (! t)
134750397Sobrien	break;
134850397Sobrien
134950397Sobrien      if (ICS_BAD_FLAG (t))
135050397Sobrien	viable = -1;
135150397Sobrien
135250397Sobrien      if (i == 0)
135350397Sobrien	continue;
135450397Sobrien
135550397Sobrien      if (parmnode)
135650397Sobrien	parmnode = TREE_CHAIN (parmnode);
135750397Sobrien      argnode = TREE_CHAIN (argnode);
135818334Speter    }
135950397Sobrien
136050397Sobrien  if (i < len)
136150397Sobrien    viable = 0;
136250397Sobrien
136390075Sobrien  if (!sufficient_parms_p (parmnode))
136490075Sobrien    viable = 0;
136550397Sobrien
1366132718Skan  return add_candidate (candidates, totype, arglist, convs, access_path,
1367117395Skan			conversion_path, viable);
136850397Sobrien}
136950397Sobrien
1370117395Skanstatic void
1371132718Skanbuild_builtin_candidate (struct z_candidate **candidates, tree fnname,
1372132718Skan                         tree type1, tree type2, tree *args, tree *argtypes,
1373132718Skan                         int flags)
137450397Sobrien{
137550397Sobrien  tree t, convs;
137650397Sobrien  int viable = 1, i;
137750397Sobrien  tree types[2];
137850397Sobrien
137950397Sobrien  types[0] = type1;
138050397Sobrien  types[1] = type2;
138150397Sobrien
138290075Sobrien  convs = make_tree_vec (args[2] ? 3 : (args[1] ? 2 : 1));
138350397Sobrien
138450397Sobrien  for (i = 0; i < 2; ++i)
138518334Speter    {
138650397Sobrien      if (! args[i])
138750397Sobrien	break;
138850397Sobrien
138950397Sobrien      t = implicit_conversion (types[i], argtypes[i], args[i], flags);
139050397Sobrien      if (! t)
139150397Sobrien	{
139250397Sobrien	  viable = 0;
139350397Sobrien	  /* We need something for printing the candidate.  */
139450397Sobrien	  t = build1 (IDENTITY_CONV, types[i], NULL_TREE);
139550397Sobrien	}
139650397Sobrien      else if (ICS_BAD_FLAG (t))
139750397Sobrien	viable = 0;
139850397Sobrien      TREE_VEC_ELT (convs, i) = t;
139918334Speter    }
140050397Sobrien
140150397Sobrien  /* For COND_EXPR we rearranged the arguments; undo that now.  */
140250397Sobrien  if (args[2])
140318334Speter    {
140450397Sobrien      TREE_VEC_ELT (convs, 2) = TREE_VEC_ELT (convs, 1);
140550397Sobrien      TREE_VEC_ELT (convs, 1) = TREE_VEC_ELT (convs, 0);
140650397Sobrien      t = implicit_conversion (boolean_type_node, argtypes[2], args[2], flags);
140750397Sobrien      if (t)
140850397Sobrien	TREE_VEC_ELT (convs, 0) = t;
140950397Sobrien      else
141050397Sobrien	viable = 0;
141150397Sobrien    }
141218334Speter
1413132718Skan  add_candidate (candidates, fnname, /*args=*/NULL_TREE, convs,
1414117395Skan		 /*access_path=*/NULL_TREE,
1415117395Skan		 /*conversion_path=*/NULL_TREE,
1416117395Skan		 viable);
141750397Sobrien}
141818334Speter
1419132718Skanstatic bool
1420132718Skanis_complete (tree t)
142150397Sobrien{
142290075Sobrien  return COMPLETE_TYPE_P (complete_type (t));
142350397Sobrien}
142418334Speter
1425117395Skan/* Returns nonzero if TYPE is a promoted arithmetic type.  */
142690075Sobrien
1427132718Skanstatic bool
1428132718Skanpromoted_arithmetic_type_p (tree type)
142990075Sobrien{
143090075Sobrien  /* [over.built]
143190075Sobrien
143290075Sobrien     In this section, the term promoted integral type is used to refer
143390075Sobrien     to those integral types which are preserved by integral promotion
143490075Sobrien     (including e.g.  int and long but excluding e.g.  char).
143590075Sobrien     Similarly, the term promoted arithmetic type refers to promoted
143690075Sobrien     integral types plus floating types.  */
143790075Sobrien  return ((INTEGRAL_TYPE_P (type)
143890075Sobrien	   && same_type_p (type_promotes_to (type), type))
143990075Sobrien	  || TREE_CODE (type) == REAL_TYPE);
144090075Sobrien}
144190075Sobrien
144250397Sobrien/* Create any builtin operator overload candidates for the operator in
144350397Sobrien   question given the converted operand types TYPE1 and TYPE2.  The other
144450397Sobrien   args are passed through from add_builtin_candidates to
144590075Sobrien   build_builtin_candidate.
144690075Sobrien
144790075Sobrien   TYPE1 and TYPE2 may not be permissible, and we must filter them.
144890075Sobrien   If CODE is requires candidates operands of the same type of the kind
144990075Sobrien   of which TYPE1 and TYPE2 are, we add both candidates
145090075Sobrien   CODE (TYPE1, TYPE1) and CODE (TYPE2, TYPE2).  */
145118334Speter
1452117395Skanstatic void
1453132718Skanadd_builtin_candidate (struct z_candidate **candidates, enum tree_code code,
1454132718Skan                       enum tree_code code2, tree fnname, tree type1,
1455132718Skan                       tree type2, tree *args, tree *argtypes, int flags)
145650397Sobrien{
145750397Sobrien  switch (code)
145850397Sobrien    {
145950397Sobrien    case POSTINCREMENT_EXPR:
146050397Sobrien    case POSTDECREMENT_EXPR:
146150397Sobrien      args[1] = integer_zero_node;
146250397Sobrien      type2 = integer_type_node;
146350397Sobrien      break;
146450397Sobrien    default:
146550397Sobrien      break;
146650397Sobrien    }
146750397Sobrien
146850397Sobrien  switch (code)
146950397Sobrien    {
147050397Sobrien
147150397Sobrien/* 4 For every pair T, VQ), where T is an arithmetic or  enumeration  type,
147250397Sobrien     and  VQ  is  either  volatile or empty, there exist candidate operator
147350397Sobrien     functions of the form
147450397Sobrien	     VQ T&   operator++(VQ T&);
147550397Sobrien	     T       operator++(VQ T&, int);
147650397Sobrien   5 For every pair T, VQ), where T is an enumeration type or an arithmetic
147750397Sobrien     type  other than bool, and VQ is either volatile or empty, there exist
147850397Sobrien     candidate operator functions of the form
147950397Sobrien	     VQ T&   operator--(VQ T&);
148050397Sobrien	     T       operator--(VQ T&, int);
148150397Sobrien   6 For every pair T, VQ), where T is  a  cv-qualified  or  cv-unqualified
148250397Sobrien     complete  object type, and VQ is either volatile or empty, there exist
148350397Sobrien     candidate operator functions of the form
148450397Sobrien	     T*VQ&   operator++(T*VQ&);
148550397Sobrien	     T*VQ&   operator--(T*VQ&);
148650397Sobrien	     T*      operator++(T*VQ&, int);
148750397Sobrien	     T*      operator--(T*VQ&, int);  */
148850397Sobrien
148950397Sobrien    case POSTDECREMENT_EXPR:
149050397Sobrien    case PREDECREMENT_EXPR:
149150397Sobrien      if (TREE_CODE (type1) == BOOLEAN_TYPE)
1492117395Skan	return;
149350397Sobrien    case POSTINCREMENT_EXPR:
149450397Sobrien    case PREINCREMENT_EXPR:
149590075Sobrien      if (ARITHMETIC_TYPE_P (type1) || TYPE_PTROB_P (type1))
149618334Speter	{
149750397Sobrien	  type1 = build_reference_type (type1);
149850397Sobrien	  break;
149918334Speter	}
1500117395Skan      return;
150150397Sobrien
150250397Sobrien/* 7 For every cv-qualified or cv-unqualified complete object type T, there
150350397Sobrien     exist candidate operator functions of the form
150450397Sobrien
150550397Sobrien	     T&      operator*(T*);
150650397Sobrien
150750397Sobrien   8 For every function type T, there exist candidate operator functions of
150850397Sobrien     the form
150950397Sobrien	     T&      operator*(T*);  */
151050397Sobrien
151150397Sobrien    case INDIRECT_REF:
151250397Sobrien      if (TREE_CODE (type1) == POINTER_TYPE
151350397Sobrien	  && (TYPE_PTROB_P (type1)
151450397Sobrien	      || TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE))
151550397Sobrien	break;
1516117395Skan      return;
151750397Sobrien
151850397Sobrien/* 9 For every type T, there exist candidate operator functions of the form
151950397Sobrien	     T*      operator+(T*);
152050397Sobrien
152150397Sobrien   10For  every  promoted arithmetic type T, there exist candidate operator
152250397Sobrien     functions of the form
152350397Sobrien	     T       operator+(T);
152450397Sobrien	     T       operator-(T);  */
152550397Sobrien
152650397Sobrien    case CONVERT_EXPR: /* unary + */
1527132718Skan      if (TREE_CODE (type1) == POINTER_TYPE)
152850397Sobrien	break;
152950397Sobrien    case NEGATE_EXPR:
153050397Sobrien      if (ARITHMETIC_TYPE_P (type1))
153150397Sobrien	break;
1532117395Skan      return;
153350397Sobrien
153450397Sobrien/* 11For every promoted integral type T,  there  exist  candidate  operator
153550397Sobrien     functions of the form
153650397Sobrien	     T       operator~(T);  */
153750397Sobrien
153850397Sobrien    case BIT_NOT_EXPR:
153950397Sobrien      if (INTEGRAL_TYPE_P (type1))
154050397Sobrien	break;
1541117395Skan      return;
154250397Sobrien
154350397Sobrien/* 12For every quintuple C1, C2, T, CV1, CV2), where C2 is a class type, C1
154450397Sobrien     is the same type as C2 or is a derived class of C2, T  is  a  complete
154550397Sobrien     object type or a function type, and CV1 and CV2 are cv-qualifier-seqs,
154650397Sobrien     there exist candidate operator functions of the form
154750397Sobrien	     CV12 T& operator->*(CV1 C1*, CV2 T C2::*);
154850397Sobrien     where CV12 is the union of CV1 and CV2.  */
154950397Sobrien
155050397Sobrien    case MEMBER_REF:
155150397Sobrien      if (TREE_CODE (type1) == POINTER_TYPE
1552132718Skan	  && TYPE_PTR_TO_MEMBER_P (type2))
155318334Speter	{
155450397Sobrien	  tree c1 = TREE_TYPE (type1);
1555132718Skan	  tree c2 = TYPE_PTRMEM_CLASS_TYPE (type2);
155618334Speter
155750397Sobrien	  if (IS_AGGR_TYPE (c1) && DERIVED_FROM_P (c2, c1)
155850397Sobrien	      && (TYPE_PTRMEMFUNC_P (type2)
1559161651Skan		  || is_complete (TYPE_PTRMEM_POINTED_TO_TYPE (type2))))
156050397Sobrien	    break;
156150397Sobrien	}
1562117395Skan      return;
156318334Speter
156450397Sobrien/* 13For every pair of promoted arithmetic types L and R, there exist  can-
156550397Sobrien     didate operator functions of the form
156650397Sobrien	     LR      operator*(L, R);
156750397Sobrien	     LR      operator/(L, R);
156850397Sobrien	     LR      operator+(L, R);
156950397Sobrien	     LR      operator-(L, R);
157050397Sobrien	     bool    operator<(L, R);
157150397Sobrien	     bool    operator>(L, R);
157250397Sobrien	     bool    operator<=(L, R);
157350397Sobrien	     bool    operator>=(L, R);
157450397Sobrien	     bool    operator==(L, R);
157550397Sobrien	     bool    operator!=(L, R);
157650397Sobrien     where  LR  is  the  result of the usual arithmetic conversions between
157750397Sobrien     types L and R.
157850397Sobrien
157950397Sobrien   14For every pair of types T and I, where T  is  a  cv-qualified  or  cv-
158050397Sobrien     unqualified  complete  object  type and I is a promoted integral type,
158150397Sobrien     there exist candidate operator functions of the form
158250397Sobrien	     T*      operator+(T*, I);
158350397Sobrien	     T&      operator[](T*, I);
158450397Sobrien	     T*      operator-(T*, I);
158550397Sobrien	     T*      operator+(I, T*);
158650397Sobrien	     T&      operator[](I, T*);
158750397Sobrien
158850397Sobrien   15For every T, where T is a pointer to complete object type, there exist
158950397Sobrien     candidate operator functions of the form112)
159050397Sobrien	     ptrdiff_t operator-(T, T);
159150397Sobrien
159290075Sobrien   16For every pointer or enumeration type T, there exist candidate operator
159390075Sobrien     functions of the form
159450397Sobrien	     bool    operator<(T, T);
159550397Sobrien	     bool    operator>(T, T);
159650397Sobrien	     bool    operator<=(T, T);
159750397Sobrien	     bool    operator>=(T, T);
159850397Sobrien	     bool    operator==(T, T);
159950397Sobrien	     bool    operator!=(T, T);
160050397Sobrien
160150397Sobrien   17For every pointer to member type T,  there  exist  candidate  operator
160250397Sobrien     functions of the form
160350397Sobrien	     bool    operator==(T, T);
160450397Sobrien	     bool    operator!=(T, T);  */
160550397Sobrien
160650397Sobrien    case MINUS_EXPR:
160750397Sobrien      if (TYPE_PTROB_P (type1) && TYPE_PTROB_P (type2))
160850397Sobrien	break;
160950397Sobrien      if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2))
161050397Sobrien	{
161150397Sobrien	  type2 = ptrdiff_type_node;
161250397Sobrien	  break;
161350397Sobrien	}
161450397Sobrien    case MULT_EXPR:
161550397Sobrien    case TRUNC_DIV_EXPR:
161650397Sobrien      if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
161750397Sobrien	break;
1618117395Skan      return;
161950397Sobrien
162050397Sobrien    case EQ_EXPR:
162150397Sobrien    case NE_EXPR:
162250397Sobrien      if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2))
162350397Sobrien	  || (TYPE_PTRMEM_P (type1) && TYPE_PTRMEM_P (type2)))
162450397Sobrien	break;
1625132718Skan      if (TYPE_PTR_TO_MEMBER_P (type1) && null_ptr_cst_p (args[1]))
162650397Sobrien	{
162750397Sobrien	  type2 = type1;
162850397Sobrien	  break;
162950397Sobrien	}
1630132718Skan      if (TYPE_PTR_TO_MEMBER_P (type2) && null_ptr_cst_p (args[0]))
163150397Sobrien	{
163250397Sobrien	  type1 = type2;
163350397Sobrien	  break;
163450397Sobrien	}
1635132718Skan      /* Fall through.  */
163650397Sobrien    case LT_EXPR:
163750397Sobrien    case GT_EXPR:
163850397Sobrien    case LE_EXPR:
163950397Sobrien    case GE_EXPR:
164050397Sobrien    case MAX_EXPR:
164150397Sobrien    case MIN_EXPR:
164290075Sobrien      if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
164390075Sobrien        break;
164490075Sobrien      if (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
164550397Sobrien	break;
164690075Sobrien      if (TREE_CODE (type1) == ENUMERAL_TYPE && TREE_CODE (type2) == ENUMERAL_TYPE)
164790075Sobrien        break;
164850397Sobrien      if (TYPE_PTR_P (type1) && null_ptr_cst_p (args[1]))
164950397Sobrien	{
165050397Sobrien	  type2 = type1;
165150397Sobrien	  break;
165250397Sobrien	}
165350397Sobrien      if (null_ptr_cst_p (args[0]) && TYPE_PTR_P (type2))
165450397Sobrien	{
165550397Sobrien	  type1 = type2;
165650397Sobrien	  break;
165750397Sobrien	}
1658117395Skan      return;
165950397Sobrien
166050397Sobrien    case PLUS_EXPR:
166150397Sobrien      if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
166250397Sobrien	break;
166350397Sobrien    case ARRAY_REF:
166450397Sobrien      if (INTEGRAL_TYPE_P (type1) && TYPE_PTROB_P (type2))
166550397Sobrien	{
166650397Sobrien	  type1 = ptrdiff_type_node;
166750397Sobrien	  break;
166850397Sobrien	}
166950397Sobrien      if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2))
167050397Sobrien	{
167150397Sobrien	  type2 = ptrdiff_type_node;
167250397Sobrien	  break;
167350397Sobrien	}
1674117395Skan      return;
167550397Sobrien
167650397Sobrien/* 18For  every pair of promoted integral types L and R, there exist candi-
167750397Sobrien     date operator functions of the form
167850397Sobrien	     LR      operator%(L, R);
167950397Sobrien	     LR      operator&(L, R);
168050397Sobrien	     LR      operator^(L, R);
168150397Sobrien	     LR      operator|(L, R);
168250397Sobrien	     L       operator<<(L, R);
168350397Sobrien	     L       operator>>(L, R);
168450397Sobrien     where LR is the result of the  usual  arithmetic  conversions  between
168550397Sobrien     types L and R.  */
168650397Sobrien
168750397Sobrien    case TRUNC_MOD_EXPR:
168850397Sobrien    case BIT_AND_EXPR:
168950397Sobrien    case BIT_IOR_EXPR:
169050397Sobrien    case BIT_XOR_EXPR:
169150397Sobrien    case LSHIFT_EXPR:
169250397Sobrien    case RSHIFT_EXPR:
169350397Sobrien      if (INTEGRAL_TYPE_P (type1) && INTEGRAL_TYPE_P (type2))
169450397Sobrien	break;
1695117395Skan      return;
169650397Sobrien
169750397Sobrien/* 19For  every  triple  L, VQ, R), where L is an arithmetic or enumeration
169850397Sobrien     type, VQ is either volatile or empty, and R is a  promoted  arithmetic
169950397Sobrien     type, there exist candidate operator functions of the form
170050397Sobrien	     VQ L&   operator=(VQ L&, R);
170150397Sobrien	     VQ L&   operator*=(VQ L&, R);
170250397Sobrien	     VQ L&   operator/=(VQ L&, R);
170350397Sobrien	     VQ L&   operator+=(VQ L&, R);
170450397Sobrien	     VQ L&   operator-=(VQ L&, R);
170550397Sobrien
170650397Sobrien   20For  every  pair T, VQ), where T is any type and VQ is either volatile
170750397Sobrien     or empty, there exist candidate operator functions of the form
170850397Sobrien	     T*VQ&   operator=(T*VQ&, T*);
170950397Sobrien
171050397Sobrien   21For every pair T, VQ), where T is a pointer to member type and  VQ  is
171150397Sobrien     either  volatile or empty, there exist candidate operator functions of
171250397Sobrien     the form
171350397Sobrien	     VQ T&   operator=(VQ T&, T);
171450397Sobrien
171550397Sobrien   22For every triple  T,  VQ,  I),  where  T  is  a  cv-qualified  or  cv-
171650397Sobrien     unqualified  complete object type, VQ is either volatile or empty, and
171750397Sobrien     I is a promoted integral type, there exist  candidate  operator  func-
171850397Sobrien     tions of the form
171950397Sobrien	     T*VQ&   operator+=(T*VQ&, I);
172050397Sobrien	     T*VQ&   operator-=(T*VQ&, I);
172150397Sobrien
172250397Sobrien   23For  every  triple  L,  VQ,  R), where L is an integral or enumeration
172350397Sobrien     type, VQ is either volatile or empty, and R  is  a  promoted  integral
172450397Sobrien     type, there exist candidate operator functions of the form
172550397Sobrien
172650397Sobrien	     VQ L&   operator%=(VQ L&, R);
172750397Sobrien	     VQ L&   operator<<=(VQ L&, R);
172850397Sobrien	     VQ L&   operator>>=(VQ L&, R);
172950397Sobrien	     VQ L&   operator&=(VQ L&, R);
173050397Sobrien	     VQ L&   operator^=(VQ L&, R);
173150397Sobrien	     VQ L&   operator|=(VQ L&, R);  */
173250397Sobrien
173350397Sobrien    case MODIFY_EXPR:
173450397Sobrien      switch (code2)
173550397Sobrien	{
173650397Sobrien	case PLUS_EXPR:
173750397Sobrien	case MINUS_EXPR:
173850397Sobrien	  if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2))
173918334Speter	    {
174050397Sobrien	      type2 = ptrdiff_type_node;
174150397Sobrien	      break;
174218334Speter	    }
174350397Sobrien	case MULT_EXPR:
174450397Sobrien	case TRUNC_DIV_EXPR:
174550397Sobrien	  if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
174650397Sobrien	    break;
1747117395Skan	  return;
174850397Sobrien
174950397Sobrien	case TRUNC_MOD_EXPR:
175050397Sobrien	case BIT_AND_EXPR:
175150397Sobrien	case BIT_IOR_EXPR:
175250397Sobrien	case BIT_XOR_EXPR:
175350397Sobrien	case LSHIFT_EXPR:
175450397Sobrien	case RSHIFT_EXPR:
175550397Sobrien	  if (INTEGRAL_TYPE_P (type1) && INTEGRAL_TYPE_P (type2))
175650397Sobrien	    break;
1757117395Skan	  return;
175850397Sobrien
175950397Sobrien	case NOP_EXPR:
176050397Sobrien	  if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
176150397Sobrien	    break;
176250397Sobrien	  if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2))
176350397Sobrien	      || (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
176450397Sobrien	      || (TYPE_PTRMEM_P (type1) && TYPE_PTRMEM_P (type2))
176550397Sobrien	      || ((TYPE_PTRMEMFUNC_P (type1)
176650397Sobrien		   || TREE_CODE (type1) == POINTER_TYPE)
176750397Sobrien		  && null_ptr_cst_p (args[1])))
176818334Speter	    {
176950397Sobrien	      type2 = type1;
177050397Sobrien	      break;
177118334Speter	    }
1772117395Skan	  return;
177350397Sobrien
177450397Sobrien	default:
177590075Sobrien	  abort ();
177618334Speter	}
177750397Sobrien      type1 = build_reference_type (type1);
177850397Sobrien      break;
177918334Speter
178050397Sobrien    case COND_EXPR:
178190075Sobrien      /* [over.built]
178218334Speter
178390075Sobrien	 For every pair of promoted arithmetic types L and R, there
178490075Sobrien	 exist candidate operator functions of the form
178590075Sobrien
178690075Sobrien	 LR operator?(bool, L, R);
178790075Sobrien
178890075Sobrien	 where LR is the result of the usual arithmetic conversions
178990075Sobrien	 between types L and R.
179090075Sobrien
179190075Sobrien	 For every type T, where T is a pointer or pointer-to-member
179290075Sobrien	 type, there exist candidate operator functions of the form T
179390075Sobrien	 operator?(bool, T, T);  */
179490075Sobrien
179590075Sobrien      if (promoted_arithmetic_type_p (type1)
179690075Sobrien	  && promoted_arithmetic_type_p (type2))
179790075Sobrien	/* That's OK.  */
179850397Sobrien	break;
179990075Sobrien
180090075Sobrien      /* Otherwise, the types should be pointers.  */
1801132718Skan      if (!(TYPE_PTR_P (type1) || TYPE_PTR_TO_MEMBER_P (type1))
1802132718Skan	  || !(TYPE_PTR_P (type2) || TYPE_PTR_TO_MEMBER_P (type2)))
1803117395Skan	return;
180490075Sobrien
180590075Sobrien      /* We don't check that the two types are the same; the logic
180690075Sobrien	 below will actually create two candidates; one in which both
180790075Sobrien	 parameter types are TYPE1, and one in which both parameter
180890075Sobrien	 types are TYPE2.  */
1809117395Skan      break;
181090075Sobrien
181150397Sobrien    default:
181290075Sobrien      abort ();
181350397Sobrien    }
181450397Sobrien
181590075Sobrien  /* If we're dealing with two pointer types or two enumeral types,
181690075Sobrien     we need candidates for both of them.  */
181790075Sobrien  if (type2 && !same_type_p (type1, type2)
181850397Sobrien      && TREE_CODE (type1) == TREE_CODE (type2)
181950397Sobrien      && (TREE_CODE (type1) == REFERENCE_TYPE
1820132718Skan	  || (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
1821132718Skan	  || (TYPE_PTRMEM_P (type1) && TYPE_PTRMEM_P (type2))
182250397Sobrien	  || TYPE_PTRMEMFUNC_P (type1)
182390075Sobrien	  || IS_AGGR_TYPE (type1)
182490075Sobrien	  || TREE_CODE (type1) == ENUMERAL_TYPE))
182550397Sobrien    {
1826117395Skan      build_builtin_candidate
182750397Sobrien	(candidates, fnname, type1, type1, args, argtypes, flags);
1828117395Skan      build_builtin_candidate
182950397Sobrien	(candidates, fnname, type2, type2, args, argtypes, flags);
1830117395Skan      return;
183150397Sobrien    }
183250397Sobrien
1833117395Skan  build_builtin_candidate
183450397Sobrien    (candidates, fnname, type1, type2, args, argtypes, flags);
183550397Sobrien}
183650397Sobrien
183750397Sobrientree
1838132718Skantype_decays_to (tree type)
183950397Sobrien{
184050397Sobrien  if (TREE_CODE (type) == ARRAY_TYPE)
184150397Sobrien    return build_pointer_type (TREE_TYPE (type));
184250397Sobrien  if (TREE_CODE (type) == FUNCTION_TYPE)
184350397Sobrien    return build_pointer_type (type);
184450397Sobrien  return type;
184550397Sobrien}
184650397Sobrien
184750397Sobrien/* There are three conditions of builtin candidates:
184850397Sobrien
184950397Sobrien   1) bool-taking candidates.  These are the same regardless of the input.
185050397Sobrien   2) pointer-pair taking candidates.  These are generated for each type
185150397Sobrien      one of the input types converts to.
185290075Sobrien   3) arithmetic candidates.  According to the standard, we should generate
185390075Sobrien      all of these, but I'm trying not to...
185490075Sobrien
185590075Sobrien   Here we generate a superset of the possible candidates for this particular
185690075Sobrien   case.  That is a subset of the full set the standard defines, plus some
185790075Sobrien   other cases which the standard disallows. add_builtin_candidate will
1858117395Skan   filter out the invalid set.  */
185950397Sobrien
1860117395Skanstatic void
1861132718Skanadd_builtin_candidates (struct z_candidate **candidates, enum tree_code code,
1862132718Skan                        enum tree_code code2, tree fnname, tree *args,
1863132718Skan                        int flags)
186450397Sobrien{
186550397Sobrien  int ref1, i;
186690075Sobrien  int enum_p = 0;
186790075Sobrien  tree type, argtypes[3];
186890075Sobrien  /* TYPES[i] is the set of possible builtin-operator parameter types
186990075Sobrien     we will consider for the Ith argument.  These are represented as
187090075Sobrien     a TREE_LIST; the TREE_VALUE of each node is the potential
187190075Sobrien     parameter type.  */
187290075Sobrien  tree types[2];
187350397Sobrien
187450397Sobrien  for (i = 0; i < 3; ++i)
187550397Sobrien    {
187650397Sobrien      if (args[i])
187750397Sobrien	argtypes[i]  = lvalue_type (args[i]);
187850397Sobrien      else
187950397Sobrien	argtypes[i] = NULL_TREE;
188050397Sobrien    }
188150397Sobrien
188250397Sobrien  switch (code)
188350397Sobrien    {
188450397Sobrien/* 4 For every pair T, VQ), where T is an arithmetic or  enumeration  type,
188550397Sobrien     and  VQ  is  either  volatile or empty, there exist candidate operator
188650397Sobrien     functions of the form
188750397Sobrien		 VQ T&   operator++(VQ T&);  */
188850397Sobrien
188950397Sobrien    case POSTINCREMENT_EXPR:
189050397Sobrien    case PREINCREMENT_EXPR:
189150397Sobrien    case POSTDECREMENT_EXPR:
189250397Sobrien    case PREDECREMENT_EXPR:
189350397Sobrien    case MODIFY_EXPR:
189450397Sobrien      ref1 = 1;
189550397Sobrien      break;
189650397Sobrien
189750397Sobrien/* 24There also exist candidate operator functions of the form
189850397Sobrien	     bool    operator!(bool);
189950397Sobrien	     bool    operator&&(bool, bool);
190050397Sobrien	     bool    operator||(bool, bool);  */
190150397Sobrien
190250397Sobrien    case TRUTH_NOT_EXPR:
1903117395Skan      build_builtin_candidate
190450397Sobrien	(candidates, fnname, boolean_type_node,
190550397Sobrien	 NULL_TREE, args, argtypes, flags);
1906117395Skan      return;
190750397Sobrien
190850397Sobrien    case TRUTH_ORIF_EXPR:
190950397Sobrien    case TRUTH_ANDIF_EXPR:
1910117395Skan      build_builtin_candidate
191150397Sobrien	(candidates, fnname, boolean_type_node,
191250397Sobrien	 boolean_type_node, args, argtypes, flags);
1913117395Skan      return;
191450397Sobrien
191550397Sobrien    case ADDR_EXPR:
191650397Sobrien    case COMPOUND_EXPR:
191750397Sobrien    case COMPONENT_REF:
1918117395Skan      return;
191950397Sobrien
192090075Sobrien    case COND_EXPR:
192190075Sobrien    case EQ_EXPR:
192290075Sobrien    case NE_EXPR:
192390075Sobrien    case LT_EXPR:
192490075Sobrien    case LE_EXPR:
192590075Sobrien    case GT_EXPR:
192690075Sobrien    case GE_EXPR:
192790075Sobrien      enum_p = 1;
1928132718Skan      /* Fall through.  */
192990075Sobrien
193050397Sobrien    default:
193150397Sobrien      ref1 = 0;
193250397Sobrien    }
193350397Sobrien
193450397Sobrien  types[0] = types[1] = NULL_TREE;
193550397Sobrien
193650397Sobrien  for (i = 0; i < 2; ++i)
193750397Sobrien    {
193850397Sobrien      if (! args[i])
193950397Sobrien	;
194050397Sobrien      else if (IS_AGGR_TYPE (argtypes[i]))
194118334Speter	{
194252284Sobrien	  tree convs;
194350397Sobrien
194450397Sobrien	  if (i == 0 && code == MODIFY_EXPR && code2 == NOP_EXPR)
1945117395Skan	    return;
194650397Sobrien
194750397Sobrien	  convs = lookup_conversions (argtypes[i]);
194850397Sobrien
194950397Sobrien	  if (code == COND_EXPR)
195018334Speter	    {
195150397Sobrien	      if (real_lvalue_p (args[i]))
195290075Sobrien		types[i] = tree_cons
195350397Sobrien		  (NULL_TREE, build_reference_type (argtypes[i]), types[i]);
195450397Sobrien
195590075Sobrien	      types[i] = tree_cons
195650397Sobrien		(NULL_TREE, TYPE_MAIN_VARIANT (argtypes[i]), types[i]);
195718334Speter	    }
195850397Sobrien
195950397Sobrien	  else if (! convs)
1960117395Skan	    return;
196150397Sobrien
196250397Sobrien	  for (; convs; convs = TREE_CHAIN (convs))
196318334Speter	    {
196452284Sobrien	      type = TREE_TYPE (TREE_TYPE (OVL_CURRENT (TREE_VALUE (convs))));
196550397Sobrien
196650397Sobrien	      if (i == 0 && ref1
196750397Sobrien		  && (TREE_CODE (type) != REFERENCE_TYPE
196852284Sobrien		      || CP_TYPE_CONST_P (TREE_TYPE (type))))
196950397Sobrien		continue;
197050397Sobrien
197150397Sobrien	      if (code == COND_EXPR && TREE_CODE (type) == REFERENCE_TYPE)
197290075Sobrien		types[i] = tree_cons (NULL_TREE, type, types[i]);
197350397Sobrien
197450397Sobrien	      type = non_reference (type);
197550397Sobrien	      if (i != 0 || ! ref1)
197650397Sobrien		{
197750397Sobrien		  type = TYPE_MAIN_VARIANT (type_decays_to (type));
197890075Sobrien	          if (enum_p && TREE_CODE (type) == ENUMERAL_TYPE)
197990075Sobrien	            types[i] = tree_cons (NULL_TREE, type, types[i]);
198050397Sobrien		  if (INTEGRAL_TYPE_P (type))
198150397Sobrien		    type = type_promotes_to (type);
198250397Sobrien		}
198350397Sobrien
198450397Sobrien	      if (! value_member (type, types[i]))
198590075Sobrien		types[i] = tree_cons (NULL_TREE, type, types[i]);
198618334Speter	    }
198718334Speter	}
198850397Sobrien      else
198950397Sobrien	{
199050397Sobrien	  if (code == COND_EXPR && real_lvalue_p (args[i]))
199190075Sobrien	    types[i] = tree_cons
199250397Sobrien	      (NULL_TREE, build_reference_type (argtypes[i]), types[i]);
199350397Sobrien	  type = non_reference (argtypes[i]);
199450397Sobrien	  if (i != 0 || ! ref1)
199550397Sobrien	    {
199650397Sobrien	      type = TYPE_MAIN_VARIANT (type_decays_to (type));
199790075Sobrien	      if (enum_p && TREE_CODE (type) == ENUMERAL_TYPE)
199890075Sobrien	        types[i] = tree_cons (NULL_TREE, type, types[i]);
199950397Sobrien	      if (INTEGRAL_TYPE_P (type))
200050397Sobrien		type = type_promotes_to (type);
200150397Sobrien	    }
200290075Sobrien	  types[i] = tree_cons (NULL_TREE, type, types[i]);
200350397Sobrien	}
200418334Speter    }
200518334Speter
200690075Sobrien  /* Run through the possible parameter types of both arguments,
200790075Sobrien     creating candidates with those parameter types.  */
200850397Sobrien  for (; types[0]; types[0] = TREE_CHAIN (types[0]))
200918334Speter    {
201050397Sobrien      if (types[1])
201150397Sobrien	for (type = types[1]; type; type = TREE_CHAIN (type))
2012117395Skan	  add_builtin_candidate
201350397Sobrien	    (candidates, code, code2, fnname, TREE_VALUE (types[0]),
201450397Sobrien	     TREE_VALUE (type), args, argtypes, flags);
201550397Sobrien      else
2016117395Skan	add_builtin_candidate
201750397Sobrien	  (candidates, code, code2, fnname, TREE_VALUE (types[0]),
201850397Sobrien	   NULL_TREE, args, argtypes, flags);
201918334Speter    }
202018334Speter
2021117395Skan  return;
202250397Sobrien}
202318334Speter
202418334Speter
202550397Sobrien/* If TMPL can be successfully instantiated as indicated by
202650397Sobrien   EXPLICIT_TARGS and ARGLIST, adds the instantiation to CANDIDATES.
202750397Sobrien
202850397Sobrien   TMPL is the template.  EXPLICIT_TARGS are any explicit template
202950397Sobrien   arguments.  ARGLIST is the arguments provided at the call-site.
203050397Sobrien   The RETURN_TYPE is the desired type for conversion operators.  If
203190075Sobrien   OBJ is NULL_TREE, FLAGS and CTYPE are as for add_function_candidate.
203290075Sobrien   If an OBJ is supplied, FLAGS and CTYPE are ignored, and OBJ is as for
203350397Sobrien   add_conv_candidate.  */
203450397Sobrien
203550397Sobrienstatic struct z_candidate*
2036132718Skanadd_template_candidate_real (struct z_candidate **candidates, tree tmpl,
2037132718Skan                             tree ctype, tree explicit_targs, tree arglist,
2038132718Skan                             tree return_type, tree access_path,
2039132718Skan			     tree conversion_path, int flags, tree obj,
2040132718Skan                             unification_kind_t strict)
204150397Sobrien{
204250397Sobrien  int ntparms = DECL_NTPARMS (tmpl);
204390075Sobrien  tree targs = make_tree_vec (ntparms);
204490075Sobrien  tree args_without_in_chrg = arglist;
204550397Sobrien  struct z_candidate *cand;
204650397Sobrien  int i;
204750397Sobrien  tree fn;
204850397Sobrien
204990075Sobrien  /* We don't do deduction on the in-charge parameter, the VTT
205090075Sobrien     parameter or 'this'.  */
205190075Sobrien  if (DECL_NONSTATIC_MEMBER_FUNCTION_P (tmpl))
205290075Sobrien    args_without_in_chrg = TREE_CHAIN (args_without_in_chrg);
205350397Sobrien
205490075Sobrien  if ((DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (tmpl)
205590075Sobrien       || DECL_BASE_CONSTRUCTOR_P (tmpl))
205690075Sobrien      && TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (tmpl)))
205790075Sobrien    args_without_in_chrg = TREE_CHAIN (args_without_in_chrg);
205890075Sobrien
205990075Sobrien  i = fn_type_unification (tmpl, explicit_targs, targs,
206090075Sobrien			   args_without_in_chrg,
206190075Sobrien			   return_type, strict, -1);
206290075Sobrien
206350397Sobrien  if (i != 0)
2064117395Skan    return NULL;
206550397Sobrien
2066132718Skan  fn = instantiate_template (tmpl, targs, tf_none);
206750397Sobrien  if (fn == error_mark_node)
2068117395Skan    return NULL;
206950397Sobrien
207096263Sobrien  /* In [class.copy]:
207196263Sobrien
207296263Sobrien       A member function template is never instantiated to perform the
207396263Sobrien       copy of a class object to an object of its class type.
207496263Sobrien
207596263Sobrien     It's a little unclear what this means; the standard explicitly
207696263Sobrien     does allow a template to be used to copy a class.  For example,
207796263Sobrien     in:
207896263Sobrien
207996263Sobrien       struct A {
208096263Sobrien         A(A&);
208196263Sobrien	 template <class T> A(const T&);
208296263Sobrien       };
208396263Sobrien       const A f ();
208496263Sobrien       void g () { A a (f ()); }
208596263Sobrien
208696263Sobrien     the member template will be used to make the copy.  The section
208796263Sobrien     quoted above appears in the paragraph that forbids constructors
208896263Sobrien     whose only parameter is (a possibly cv-qualified variant of) the
208996263Sobrien     class type, and a logical interpretation is that the intent was
209096263Sobrien     to forbid the instantiation of member templates which would then
209196263Sobrien     have that form.  */
209296263Sobrien  if (DECL_CONSTRUCTOR_P (fn) && list_length (arglist) == 2)
209396263Sobrien    {
209496263Sobrien      tree arg_types = FUNCTION_FIRST_USER_PARMTYPE (fn);
209596263Sobrien      if (arg_types && same_type_p (TYPE_MAIN_VARIANT (TREE_VALUE (arg_types)),
209696263Sobrien				    ctype))
2097117395Skan	return NULL;
209896263Sobrien    }
209996263Sobrien
210050397Sobrien  if (obj != NULL_TREE)
210150397Sobrien    /* Aha, this is a conversion function.  */
2102117395Skan    cand = add_conv_candidate (candidates, fn, obj, access_path,
2103117395Skan			       conversion_path, arglist);
210450397Sobrien  else
210590075Sobrien    cand = add_function_candidate (candidates, fn, ctype,
2106117395Skan				   arglist, access_path,
2107117395Skan				   conversion_path, flags);
210850397Sobrien  if (DECL_TI_TEMPLATE (fn) != tmpl)
210950397Sobrien    /* This situation can occur if a member template of a template
211050397Sobrien       class is specialized.  Then, instantiate_template might return
211150397Sobrien       an instantiation of the specialization, in which case the
211250397Sobrien       DECL_TI_TEMPLATE field will point at the original
211350397Sobrien       specialization.  For example:
211450397Sobrien
211550397Sobrien	 template <class T> struct S { template <class U> void f(U);
211650397Sobrien				       template <> void f(int) {}; };
211750397Sobrien	 S<double> sd;
211850397Sobrien	 sd.f(3);
211950397Sobrien
212050397Sobrien       Here, TMPL will be template <class U> S<double>::f(U).
212150397Sobrien       And, instantiate template will give us the specialization
212250397Sobrien       template <> S<double>::f(int).  But, the DECL_TI_TEMPLATE field
212350397Sobrien       for this will point at template <class T> template <> S<T>::f(int),
212450397Sobrien       so that we can find the definition.  For the purposes of
212550397Sobrien       overload resolution, however, we want the original TMPL.  */
212650397Sobrien    cand->template = tree_cons (tmpl, targs, NULL_TREE);
212750397Sobrien  else
212850397Sobrien    cand->template = DECL_TEMPLATE_INFO (fn);
212950397Sobrien
213050397Sobrien  return cand;
213150397Sobrien}
213250397Sobrien
213350397Sobrien
213450397Sobrienstatic struct z_candidate *
2135132718Skanadd_template_candidate (struct z_candidate **candidates, tree tmpl, tree ctype,
2136132718Skan                        tree explicit_targs, tree arglist, tree return_type,
2137132718Skan                        tree access_path, tree conversion_path, int flags,
2138132718Skan                        unification_kind_t strict)
213950397Sobrien{
214050397Sobrien  return
214190075Sobrien    add_template_candidate_real (candidates, tmpl, ctype,
2142117395Skan				 explicit_targs, arglist, return_type,
2143117395Skan				 access_path, conversion_path,
2144117395Skan				 flags, NULL_TREE, strict);
214550397Sobrien}
214650397Sobrien
214750397Sobrien
214850397Sobrienstatic struct z_candidate *
2149132718Skanadd_template_conv_candidate (struct z_candidate **candidates, tree tmpl,
2150132718Skan                             tree obj, tree arglist, tree return_type,
2151132718Skan			     tree access_path, tree conversion_path)
215250397Sobrien{
215350397Sobrien  return
215490075Sobrien    add_template_candidate_real (candidates, tmpl, NULL_TREE, NULL_TREE,
2155117395Skan				 arglist, return_type, access_path,
2156117395Skan				 conversion_path, 0, obj, DEDUCE_CONV);
215750397Sobrien}
215850397Sobrien
2159132718Skan/* The CANDS are the set of candidates that were considered for
2160132718Skan   overload resolution.  Return the set of viable candidates.  If none
2161132718Skan   of the candidates were viable, set *ANY_VIABLE_P to true.  STRICT_P
2162132718Skan   is true if a candidate should be considered viable only if it is
2163132718Skan   strictly viable.  */
216450397Sobrien
2165132718Skanstatic struct z_candidate*
2166132718Skansplice_viable (struct z_candidate *cands,
2167132718Skan	       bool strict_p,
2168132718Skan	       bool *any_viable_p)
216950397Sobrien{
2170132718Skan  struct z_candidate *viable;
2171132718Skan  struct z_candidate **last_viable;
2172132718Skan  struct z_candidate **cand;
217350397Sobrien
2174132718Skan  viable = NULL;
2175132718Skan  last_viable = &viable;
2176132718Skan  *any_viable_p = false;
2177117395Skan
2178132718Skan  cand = &cands;
2179132718Skan  while (*cand)
218018334Speter    {
2181132718Skan      struct z_candidate *c = *cand;
2182132718Skan      if (strict_p ? c->viable == 1 : c->viable)
2183132718Skan	{
2184132718Skan	  *last_viable = c;
2185132718Skan	  *cand = c->next;
2186132718Skan	  c->next = NULL;
2187132718Skan	  last_viable = &c->next;
2188132718Skan	  *any_viable_p = true;
2189132718Skan	}
219050397Sobrien      else
2191132718Skan	cand = &c->next;
219250397Sobrien    }
219350397Sobrien
2194132718Skan  return viable ? viable : cands;
219550397Sobrien}
219650397Sobrien
2197132718Skanstatic bool
2198132718Skanany_strictly_viable (struct z_candidate *cands)
2199132718Skan{
2200132718Skan  for (; cands; cands = cands->next)
2201132718Skan    if (cands->viable == 1)
2202132718Skan      return true;
2203132718Skan  return false;
2204132718Skan}
2205132718Skan
2206146895Skan/* OBJ is being used in an expression like "OBJ.f (...)".  In other
2207146895Skan   words, it is about to become the "this" pointer for a member
2208146895Skan   function call.  Take the address of the object.  */
2209146895Skan
221050397Sobrienstatic tree
2211132718Skanbuild_this (tree obj)
221250397Sobrien{
2213146895Skan  /* In a template, we are only concerned about the type of the
2214146895Skan     expression, so we can take a shortcut.  */
2215146895Skan  if (processing_template_decl)
2216146895Skan    return build_address (obj);
2217146895Skan
221890075Sobrien  return build_unary_op (ADDR_EXPR, obj, 0);
221950397Sobrien}
222050397Sobrien
2221117395Skan/* Returns true iff functions are equivalent. Equivalent functions are
2222117395Skan   not '==' only if one is a function-local extern function or if
2223117395Skan   both are extern "C".  */
2224117395Skan
2225117395Skanstatic inline int
2226132718Skanequal_functions (tree fn1, tree fn2)
2227117395Skan{
2228117395Skan  if (DECL_LOCAL_FUNCTION_P (fn1) || DECL_LOCAL_FUNCTION_P (fn2)
2229117395Skan      || DECL_EXTERN_C_FUNCTION_P (fn1))
2230117395Skan    return decls_match (fn1, fn2);
2231117395Skan  return fn1 == fn2;
2232117395Skan}
2233117395Skan
2234132718Skan/* Print information about one overload candidate CANDIDATE.  MSGSTR
2235132718Skan   is the text to print before the candidate itself.
2236132718Skan
2237132718Skan   NOTE: Unlike most diagnostic functions in GCC, MSGSTR is expected
2238132718Skan   to have been run through gettext by the caller.  This wart makes
2239132718Skan   life simpler in print_z_candidates and for the translators.  */
2240132718Skan
224150397Sobrienstatic void
2242132718Skanprint_z_candidate (const char *msgstr, struct z_candidate *candidate)
2243132718Skan{
2244132718Skan  if (TREE_CODE (candidate->fn) == IDENTIFIER_NODE)
2245132718Skan    {
2246132718Skan      if (TREE_VEC_LENGTH (candidate->convs) == 3)
2247132718Skan	inform ("%s %D(%T, %T, %T) <built-in>", msgstr, candidate->fn,
2248132718Skan		TREE_TYPE (TREE_VEC_ELT (candidate->convs, 0)),
2249132718Skan		TREE_TYPE (TREE_VEC_ELT (candidate->convs, 1)),
2250132718Skan		TREE_TYPE (TREE_VEC_ELT (candidate->convs, 2)));
2251132718Skan      else if (TREE_VEC_LENGTH (candidate->convs) == 2)
2252132718Skan	inform ("%s %D(%T, %T) <built-in>", msgstr, candidate->fn,
2253132718Skan		TREE_TYPE (TREE_VEC_ELT (candidate->convs, 0)),
2254132718Skan		TREE_TYPE (TREE_VEC_ELT (candidate->convs, 1)));
2255132718Skan      else
2256132718Skan	inform ("%s %D(%T) <built-in>", msgstr, candidate->fn,
2257132718Skan		TREE_TYPE (TREE_VEC_ELT (candidate->convs, 0)));
2258132718Skan    }
2259132718Skan  else if (TYPE_P (candidate->fn))
2260132718Skan    inform ("%s %T <conversion>", msgstr, candidate->fn);
2261132718Skan  else if (candidate->viable == -1)
2262132718Skan    inform ("%J%s %+#D <near match>", candidate->fn, msgstr, candidate->fn);
2263132718Skan  else
2264132718Skan    inform ("%J%s %+#D", candidate->fn, msgstr, candidate->fn);
2265132718Skan}
2266132718Skan
2267132718Skanstatic void
2268117395Skanprint_z_candidates (struct z_candidate *candidates)
226950397Sobrien{
2270117395Skan  const char *str;
2271117395Skan  struct z_candidate *cand1;
2272117395Skan  struct z_candidate **cand2;
2273117395Skan
2274117395Skan  /* There may be duplicates in the set of candidates.  We put off
2275117395Skan     checking this condition as long as possible, since we have no way
2276117395Skan     to eliminate duplicates from a set of functions in less than n^2
2277117395Skan     time.  Now we are about to emit an error message, so it is more
2278117395Skan     permissible to go slowly.  */
2279117395Skan  for (cand1 = candidates; cand1; cand1 = cand1->next)
2280117395Skan    {
2281117395Skan      tree fn = cand1->fn;
2282117395Skan      /* Skip builtin candidates and conversion functions.  */
2283117395Skan      if (TREE_CODE (fn) != FUNCTION_DECL)
2284117395Skan	continue;
2285117395Skan      cand2 = &cand1->next;
2286117395Skan      while (*cand2)
2287117395Skan	{
2288117395Skan	  if (TREE_CODE ((*cand2)->fn) == FUNCTION_DECL
2289117395Skan	      && equal_functions (fn, (*cand2)->fn))
2290117395Skan	    *cand2 = (*cand2)->next;
2291117395Skan	  else
2292117395Skan	    cand2 = &(*cand2)->next;
2293117395Skan	}
2294117395Skan    }
2295117395Skan
2296132718Skan  if (!candidates)
2297132718Skan    return;
2298132718Skan
2299132718Skan  str = _("candidates are:");
2300132718Skan  print_z_candidate (str, candidates);
2301132718Skan  if (candidates->next)
230250397Sobrien    {
2303132718Skan      /* Indent successive candidates by the width of the translation
2304132718Skan	 of the above string.  */
2305132718Skan      size_t len = gcc_gettext_width (str) + 1;
2306132718Skan      char *spaces = alloca (len);
2307132718Skan      memset (spaces, ' ', len-1);
2308132718Skan      spaces[len - 1] = '\0';
2309132718Skan
2310132718Skan      candidates = candidates->next;
2311132718Skan      do
231218334Speter	{
2313132718Skan	  print_z_candidate (spaces, candidates);
2314132718Skan	  candidates = candidates->next;
231518334Speter	}
2316132718Skan      while (candidates);
231750397Sobrien    }
231850397Sobrien}
231950397Sobrien
2320117395Skan/* USER_SEQ is a user-defined conversion sequence, beginning with a
2321117395Skan   USER_CONV.  STD_SEQ is the standard conversion sequence applied to
2322117395Skan   the result of the conversion function to convert it to the final
2323117395Skan   desired type.  Merge the the two sequences into a single sequence,
2324117395Skan   and return the merged sequence.  */
2325117395Skan
2326117395Skanstatic tree
2327117395Skanmerge_conversion_sequences (tree user_seq, tree std_seq)
2328117395Skan{
2329117395Skan  tree *t;
2330117395Skan
2331117395Skan  my_friendly_assert (TREE_CODE (user_seq) == USER_CONV,
2332117395Skan		      20030306);
2333117395Skan
2334117395Skan  /* Find the end of the second conversion sequence.  */
2335117395Skan  t = &(std_seq);
2336117395Skan  while (TREE_CODE (*t) != IDENTITY_CONV)
2337117395Skan    t = &TREE_OPERAND (*t, 0);
2338117395Skan
2339117395Skan  /* Replace the identity conversion with the user conversion
2340117395Skan     sequence.  */
2341117395Skan  *t = user_seq;
2342117395Skan
2343117395Skan  /* The entire sequence is a user-conversion sequence.  */
2344117395Skan  ICS_USER_FLAG (std_seq) = 1;
2345117395Skan
2346117395Skan  return std_seq;
2347117395Skan}
2348117395Skan
234950397Sobrien/* Returns the best overload candidate to perform the requested
235050397Sobrien   conversion.  This function is used for three the overloading situations
235150397Sobrien   described in [over.match.copy], [over.match.conv], and [over.match.ref].
235250397Sobrien   If TOTYPE is a REFERENCE_TYPE, we're trying to find an lvalue binding as
235350397Sobrien   per [dcl.init.ref], so we ignore temporary bindings.  */
235450397Sobrien
235550397Sobrienstatic struct z_candidate *
2356132718Skanbuild_user_type_conversion_1 (tree totype, tree expr, int flags)
235750397Sobrien{
235850397Sobrien  struct z_candidate *candidates, *cand;
235950397Sobrien  tree fromtype = TREE_TYPE (expr);
2360117395Skan  tree ctors = NULL_TREE, convs = NULL_TREE;
236150397Sobrien  tree args = NULL_TREE;
2362132718Skan  bool any_viable_p;
236350397Sobrien
236490075Sobrien  /* We represent conversion within a hierarchy using RVALUE_CONV and
236590075Sobrien     BASE_CONV, as specified by [over.best.ics]; these become plain
236690075Sobrien     constructor calls, as specified in [dcl.init].  */
236790075Sobrien  my_friendly_assert (!IS_AGGR_TYPE (fromtype) || !IS_AGGR_TYPE (totype)
236890075Sobrien		      || !DERIVED_FROM_P (totype, fromtype), 20011226);
236990075Sobrien
237050397Sobrien  if (IS_AGGR_TYPE (totype))
237190075Sobrien    ctors = lookup_fnfields (TYPE_BINFO (totype),
237290075Sobrien			     complete_ctor_identifier,
237390075Sobrien			     0);
237490075Sobrien
237590075Sobrien  if (IS_AGGR_TYPE (fromtype))
237650397Sobrien    convs = lookup_conversions (fromtype);
237750397Sobrien
237850397Sobrien  candidates = 0;
237950397Sobrien  flags |= LOOKUP_NO_CONVERSION;
238050397Sobrien
238150397Sobrien  if (ctors)
238250397Sobrien    {
238390075Sobrien      tree t;
238450397Sobrien
2385117395Skan      ctors = BASELINK_FUNCTIONS (ctors);
238690075Sobrien
238790075Sobrien      t = build_int_2 (0, 0);
238890075Sobrien      TREE_TYPE (t) = build_pointer_type (totype);
238990075Sobrien      args = build_tree_list (NULL_TREE, expr);
239090075Sobrien      /* We should never try to call the abstract or base constructor
239190075Sobrien	 from here.  */
239290075Sobrien      my_friendly_assert (!DECL_HAS_IN_CHARGE_PARM_P (OVL_CURRENT (ctors))
239390075Sobrien			  && !DECL_HAS_VTT_PARM_P (OVL_CURRENT (ctors)),
239490075Sobrien			  20011226);
239590075Sobrien      args = tree_cons (NULL_TREE, t, args);
239650397Sobrien    }
239750397Sobrien  for (; ctors; ctors = OVL_NEXT (ctors))
239850397Sobrien    {
239950397Sobrien      tree ctor = OVL_CURRENT (ctors);
240050397Sobrien      if (DECL_NONCONVERTING_P (ctor))
240150397Sobrien	continue;
240250397Sobrien
240350397Sobrien      if (TREE_CODE (ctor) == TEMPLATE_DECL)
2404117395Skan	cand = add_template_candidate (&candidates, ctor, totype,
2405117395Skan				       NULL_TREE, args, NULL_TREE,
2406117395Skan				       TYPE_BINFO (totype),
2407117395Skan				       TYPE_BINFO (totype),
2408117395Skan				       flags,
2409117395Skan				       DEDUCE_CALL);
241050397Sobrien      else
2411117395Skan	cand = add_function_candidate (&candidates, ctor, totype,
2412117395Skan				       args, TYPE_BINFO (totype),
2413117395Skan				       TYPE_BINFO (totype),
2414117395Skan				       flags);
241550397Sobrien
2416117395Skan      if (cand)
2417117395Skan	cand->second_conv = build1 (IDENTITY_CONV, totype, NULL_TREE);
241850397Sobrien    }
241950397Sobrien
242050397Sobrien  if (convs)
242190075Sobrien    args = build_tree_list (NULL_TREE, build_this (expr));
242250397Sobrien
242350397Sobrien  for (; convs; convs = TREE_CHAIN (convs))
242450397Sobrien    {
2425117395Skan      tree fns;
2426117395Skan      tree conversion_path = TREE_PURPOSE (convs);
242750397Sobrien      int convflags = LOOKUP_NO_CONVERSION;
242850397Sobrien
242950397Sobrien      /* If we are called to convert to a reference type, we are trying to
243050397Sobrien	 find an lvalue binding, so don't even consider temporaries.  If
243150397Sobrien	 we don't find an lvalue binding, the caller will try again to
243250397Sobrien	 look for a temporary binding.  */
243350397Sobrien      if (TREE_CODE (totype) == REFERENCE_TYPE)
243450397Sobrien	convflags |= LOOKUP_NO_TEMP_BIND;
2435117395Skan
2436117395Skan      for (fns = TREE_VALUE (convs); fns; fns = OVL_NEXT (fns))
2437117395Skan	{
2438117395Skan	  tree fn = OVL_CURRENT (fns);
2439117395Skan
2440117395Skan	  /* [over.match.funcs] For conversion functions, the function
2441117395Skan	     is considered to be a member of the class of the implicit
2442117395Skan	     object argument for the purpose of defining the type of
2443117395Skan	     the implicit object parameter.
244450397Sobrien
2445117395Skan	     So we pass fromtype as CTYPE to add_*_candidate.  */
244650397Sobrien
2447117395Skan	  if (TREE_CODE (fn) == TEMPLATE_DECL)
2448132718Skan	    cand = add_template_candidate (&candidates, fn, fromtype,
2449132718Skan					   NULL_TREE,
2450117395Skan					   args, totype,
2451117395Skan					   TYPE_BINFO (fromtype),
2452117395Skan					   conversion_path,
2453117395Skan					   flags,
2454117395Skan					   DEDUCE_CONV);
2455117395Skan	  else
2456117395Skan	    cand = add_function_candidate (&candidates, fn, fromtype,
2457117395Skan					   args,
2458117395Skan					   TYPE_BINFO (fromtype),
2459117395Skan					   conversion_path,
2460117395Skan					   flags);
246150397Sobrien
2462117395Skan	  if (cand)
2463117395Skan	    {
2464132718Skan	      tree ics = implicit_conversion (totype,
2465132718Skan					      TREE_TYPE (TREE_TYPE (cand->fn)),
2466132718Skan					      0, convflags);
246790075Sobrien
2468117395Skan	      cand->second_conv = ics;
2469117395Skan
2470117395Skan	      if (ics == NULL_TREE)
2471117395Skan		cand->viable = 0;
2472132718Skan	      else if (candidates->viable == 1 && ICS_BAD_FLAG (ics))
2473117395Skan		cand->viable = -1;
2474117395Skan	    }
2475117395Skan	}
247650397Sobrien    }
247750397Sobrien
2478132718Skan  candidates = splice_viable (candidates, pedantic, &any_viable_p);
2479132718Skan  if (!any_viable_p)
2480117395Skan    return 0;
248150397Sobrien
248250397Sobrien  cand = tourney (candidates);
248350397Sobrien  if (cand == 0)
248450397Sobrien    {
248550397Sobrien      if (flags & LOOKUP_COMPLAIN)
248650397Sobrien	{
248790075Sobrien	  error ("conversion from `%T' to `%T' is ambiguous",
248850397Sobrien		    fromtype, totype);
248950397Sobrien	  print_z_candidates (candidates);
249050397Sobrien	}
249150397Sobrien
249250397Sobrien      cand = candidates;	/* any one will do */
249350397Sobrien      cand->second_conv = build1 (AMBIG_CONV, totype, expr);
249450397Sobrien      ICS_USER_FLAG (cand->second_conv) = 1;
2495117395Skan      if (!any_strictly_viable (candidates))
2496117395Skan	ICS_BAD_FLAG (cand->second_conv) = 1;
2497117395Skan      /* If there are viable candidates, don't set ICS_BAD_FLAG; an
2498117395Skan	 ambiguous conversion is no worse than another user-defined
2499117395Skan	 conversion.  */
250050397Sobrien
250150397Sobrien      return cand;
250250397Sobrien    }
250350397Sobrien
2504117395Skan  /* Build the user conversion sequence.  */
2505117395Skan  convs = build_conv
250650397Sobrien    (USER_CONV,
250750397Sobrien     (DECL_CONSTRUCTOR_P (cand->fn)
250850397Sobrien      ? totype : non_reference (TREE_TYPE (TREE_TYPE (cand->fn)))),
2509117395Skan     build1 (IDENTITY_CONV, TREE_TYPE (expr), expr));
2510117395Skan  TREE_OPERAND (convs, 1) = build_zc_wrapper (cand);
2511117395Skan
2512117395Skan  /* Combine it with the second conversion sequence.  */
2513117395Skan  cand->second_conv = merge_conversion_sequences (convs,
2514117395Skan						  cand->second_conv);
2515117395Skan
251650397Sobrien  if (cand->viable == -1)
2517117395Skan    ICS_BAD_FLAG (cand->second_conv) = 1;
251850397Sobrien
251950397Sobrien  return cand;
252050397Sobrien}
252150397Sobrien
252250397Sobrientree
2523132718Skanbuild_user_type_conversion (tree totype, tree expr, int flags)
252450397Sobrien{
252550397Sobrien  struct z_candidate *cand
252650397Sobrien    = build_user_type_conversion_1 (totype, expr, flags);
252750397Sobrien
252850397Sobrien  if (cand)
252950397Sobrien    {
253050397Sobrien      if (TREE_CODE (cand->second_conv) == AMBIG_CONV)
253118334Speter	return error_mark_node;
253250397Sobrien      return convert_from_reference (convert_like (cand->second_conv, expr));
253318334Speter    }
253450397Sobrien  return NULL_TREE;
253550397Sobrien}
253618334Speter
253750397Sobrien/* Do any initial processing on the arguments to a function call.  */
253850397Sobrien
253950397Sobrienstatic tree
2540132718Skanresolve_args (tree args)
254150397Sobrien{
254250397Sobrien  tree t;
254350397Sobrien  for (t = args; t; t = TREE_CHAIN (t))
254418334Speter    {
254590075Sobrien      tree arg = TREE_VALUE (t);
254690075Sobrien
2547161651Skan      if (error_operand_p (arg))
254850397Sobrien	return error_mark_node;
254990075Sobrien      else if (VOID_TYPE_P (TREE_TYPE (arg)))
255050397Sobrien	{
255150397Sobrien	  error ("invalid use of void expression");
255250397Sobrien	  return error_mark_node;
255350397Sobrien	}
255490075Sobrien      arg = convert_from_reference (arg);
255590075Sobrien      TREE_VALUE (t) = arg;
255618334Speter    }
255750397Sobrien  return args;
255850397Sobrien}
2559117395Skan
2560132718Skan/* Perform overload resolution on FN, which is called with the ARGS.
2561132718Skan
2562132718Skan   Return the candidate function selected by overload resolution, or
2563132718Skan   NULL if the event that overload resolution failed.  In the case
2564132718Skan   that overload resolution fails, *CANDIDATES will be the set of
2565132718Skan   candidates considered, and ANY_VIABLE_P will be set to true or
2566132718Skan   false to indicate whether or not any of the candidates were
2567132718Skan   viable.
2568132718Skan
2569132718Skan   The ARGS should already have gone through RESOLVE_ARGS before this
2570132718Skan   function is called.  */
2571132718Skan
2572132718Skanstatic struct z_candidate *
2573132718Skanperform_overload_resolution (tree fn,
2574132718Skan			     tree args,
2575132718Skan			     struct z_candidate **candidates,
2576132718Skan			     bool *any_viable_p)
257750397Sobrien{
2578132718Skan  struct z_candidate *cand;
257950397Sobrien  tree explicit_targs = NULL_TREE;
258050397Sobrien  int template_only = 0;
258150397Sobrien
2582132718Skan  *candidates = NULL;
2583132718Skan  *any_viable_p = true;
2584132718Skan
2585117395Skan  /* Check FN and ARGS.  */
2586117395Skan  my_friendly_assert (TREE_CODE (fn) == FUNCTION_DECL
2587117395Skan		      || TREE_CODE (fn) == TEMPLATE_DECL
2588117395Skan		      || TREE_CODE (fn) == OVERLOAD
2589117395Skan		      || TREE_CODE (fn) == TEMPLATE_ID_EXPR,
2590117395Skan		      20020712);
2591117395Skan  my_friendly_assert (!args || TREE_CODE (args) == TREE_LIST,
2592117395Skan		      20020712);
2593117395Skan
259450397Sobrien  if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
259518334Speter    {
259650397Sobrien      explicit_targs = TREE_OPERAND (fn, 1);
259750397Sobrien      fn = TREE_OPERAND (fn, 0);
259850397Sobrien      template_only = 1;
259918334Speter    }
260050397Sobrien
2601132718Skan  /* Add the various candidate functions.  */
2602132718Skan  add_candidates (fn, args, explicit_targs, template_only,
2603132718Skan		  /*conversion_path=*/NULL_TREE,
2604132718Skan		  /*access_path=*/NULL_TREE,
2605132718Skan		  LOOKUP_NORMAL,
2606132718Skan		  candidates);
2607132718Skan
2608132718Skan  *candidates = splice_viable (*candidates, pedantic, any_viable_p);
2609132718Skan  if (!*any_viable_p)
2610132718Skan    return NULL;
2611132718Skan
2612132718Skan  cand = tourney (*candidates);
2613132718Skan  return cand;
2614132718Skan}
2615132718Skan
2616132718Skan/* Return an expression for a call to FN (a namespace-scope function,
2617132718Skan   or a static member function) with the ARGS.  */
2618132718Skan
2619132718Skantree
2620132718Skanbuild_new_function_call (tree fn, tree args)
2621132718Skan{
2622132718Skan  struct z_candidate *candidates, *cand;
2623132718Skan  bool any_viable_p;
2624132718Skan
2625132718Skan  args = resolve_args (args);
2626132718Skan  if (args == error_mark_node)
2627132718Skan    return error_mark_node;
2628132718Skan
2629132718Skan  cand = perform_overload_resolution (fn, args, &candidates, &any_viable_p);
2630132718Skan
2631132718Skan  if (!cand)
263218334Speter    {
2633132718Skan      if (!any_viable_p && candidates && ! candidates->next)
2634132718Skan	return build_function_call (candidates->fn, args);
2635132718Skan      if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
2636132718Skan	fn = TREE_OPERAND (fn, 0);
2637132718Skan      if (!any_viable_p)
2638132718Skan	error ("no matching function for call to `%D(%A)'",
2639132718Skan	       DECL_NAME (OVL_CURRENT (fn)), args);
2640132718Skan      else
2641132718Skan	error ("call of overloaded `%D(%A)' is ambiguous",
2642132718Skan	       DECL_NAME (OVL_CURRENT (fn)), args);
2643132718Skan      if (candidates)
2644132718Skan	print_z_candidates (candidates);
2645132718Skan      return error_mark_node;
2646132718Skan    }
264750397Sobrien
2648132718Skan  return build_over_call (cand, LOOKUP_NORMAL);
2649132718Skan}
265050397Sobrien
2651132718Skan/* Build a call to a global operator new.  FNNAME is the name of the
2652132718Skan   operator (either "operator new" or "operator new[]") and ARGS are
2653132718Skan   the arguments provided.  *SIZE points to the total number of bytes
2654132718Skan   required by the allocation, and is updated if that is changed here.
2655132718Skan   *COOKIE_SIZE is non-NULL if a cookie should be used.  If this
2656132718Skan   function determines that no cookie should be used, after all,
2657132718Skan   *COOKIE_SIZE is set to NULL_TREE.  */
265850397Sobrien
2659132718Skantree
2660132718Skanbuild_operator_new_call (tree fnname, tree args, tree *size, tree *cookie_size)
2661132718Skan{
2662132718Skan  tree fns;
2663132718Skan  struct z_candidate *candidates;
2664132718Skan  struct z_candidate *cand;
2665132718Skan  bool any_viable_p;
266652284Sobrien
2667132718Skan  args = tree_cons (NULL_TREE, *size, args);
2668132718Skan  args = resolve_args (args);
2669132718Skan  if (args == error_mark_node)
2670132718Skan    return args;
267118334Speter
2672132718Skan  fns = lookup_function_nonclass (fnname, args);
267350397Sobrien
2674132718Skan  /* Figure out what function is being called.  */
2675132718Skan  cand = perform_overload_resolution (fns, args, &candidates, &any_viable_p);
2676132718Skan
2677132718Skan  /* If no suitable function could be found, issue an error message
2678132718Skan     and give up.  */
2679132718Skan  if (!cand)
2680132718Skan    {
2681132718Skan      if (!any_viable_p)
2682132718Skan	error ("no matching function for call to `%D(%A)'",
2683132718Skan	       DECL_NAME (OVL_CURRENT (fns)), args);
2684132718Skan      else
2685132718Skan	error ("call of overloaded `%D(%A)' is ambiguous",
2686132718Skan	       DECL_NAME (OVL_CURRENT (fns)), args);
2687132718Skan      if (candidates)
2688132718Skan	print_z_candidates (candidates);
2689132718Skan      return error_mark_node;
269018334Speter    }
269118334Speter
2692132718Skan   /* If a cookie is required, add some extra space.  Whether
2693132718Skan      or not a cookie is required cannot be determined until
2694132718Skan      after we know which function was called.  */
2695132718Skan   if (*cookie_size)
2696132718Skan     {
2697132718Skan       bool use_cookie = true;
2698132718Skan       if (!abi_version_at_least (2))
2699132718Skan	 {
2700132718Skan	   tree placement = TREE_CHAIN (args);
2701132718Skan	   /* In G++ 3.2, the check was implemented incorrectly; it
2702132718Skan	      looked at the placement expression, rather than the
2703132718Skan	      type of the function.  */
2704132718Skan	   if (placement && !TREE_CHAIN (placement)
2705132718Skan	       && same_type_p (TREE_TYPE (TREE_VALUE (placement)),
2706132718Skan			       ptr_type_node))
2707132718Skan	     use_cookie = false;
2708132718Skan	 }
2709132718Skan       else
2710132718Skan	 {
2711132718Skan	   tree arg_types;
271218334Speter
2713132718Skan	   arg_types = TYPE_ARG_TYPES (TREE_TYPE (cand->fn));
2714132718Skan	   /* Skip the size_t parameter.  */
2715132718Skan	   arg_types = TREE_CHAIN (arg_types);
2716132718Skan	   /* Check the remaining parameters (if any).  */
2717132718Skan	   if (arg_types
2718132718Skan	       && TREE_CHAIN (arg_types) == void_list_node
2719132718Skan	       && same_type_p (TREE_VALUE (arg_types),
2720132718Skan			       ptr_type_node))
2721132718Skan	     use_cookie = false;
2722132718Skan	 }
2723132718Skan       /* If we need a cookie, adjust the number of bytes allocated.  */
2724132718Skan       if (use_cookie)
2725132718Skan	 {
2726132718Skan	   /* Update the total size.  */
2727132718Skan	   *size = size_binop (PLUS_EXPR, *size, *cookie_size);
2728132718Skan	   /* Update the argument list to reflect the adjusted size.  */
2729132718Skan	   TREE_VALUE (args) = *size;
2730132718Skan	 }
2731132718Skan       else
2732132718Skan	 *cookie_size = NULL_TREE;
2733132718Skan     }
2734132718Skan
2735132718Skan   /* Build the CALL_EXPR.  */
2736132718Skan   return build_over_call (cand, LOOKUP_NORMAL);
273750397Sobrien}
273818334Speter
273950397Sobrienstatic tree
2740132718Skanbuild_object_call (tree obj, tree args)
274150397Sobrien{
274250397Sobrien  struct z_candidate *candidates = 0, *cand;
274350397Sobrien  tree fns, convs, mem_args = NULL_TREE;
274450397Sobrien  tree type = TREE_TYPE (obj);
2745132718Skan  bool any_viable_p;
274618334Speter
274750397Sobrien  if (TYPE_PTRMEMFUNC_P (type))
274818334Speter    {
274950397Sobrien      /* It's no good looking for an overloaded operator() on a
275050397Sobrien	 pointer-to-member-function.  */
275190075Sobrien      error ("pointer-to-member function %E cannot be called without an object; consider using .* or ->*", obj);
275250397Sobrien      return error_mark_node;
275350397Sobrien    }
275450397Sobrien
275590075Sobrien  fns = lookup_fnfields (TYPE_BINFO (type), ansi_opname (CALL_EXPR), 1);
275650397Sobrien  if (fns == error_mark_node)
275750397Sobrien    return error_mark_node;
275850397Sobrien
275950397Sobrien  args = resolve_args (args);
276050397Sobrien
276150397Sobrien  if (args == error_mark_node)
276250397Sobrien    return error_mark_node;
276350397Sobrien
276450397Sobrien  if (fns)
276550397Sobrien    {
2766117395Skan      tree base = BINFO_TYPE (BASELINK_BINFO (fns));
276790075Sobrien      mem_args = tree_cons (NULL_TREE, build_this (obj), args);
276850397Sobrien
2769117395Skan      for (fns = BASELINK_FUNCTIONS (fns); fns; fns = OVL_NEXT (fns))
277018334Speter	{
277150397Sobrien	  tree fn = OVL_CURRENT (fns);
277250397Sobrien	  if (TREE_CODE (fn) == TEMPLATE_DECL)
2773117395Skan	    add_template_candidate (&candidates, fn, base, NULL_TREE,
2774117395Skan				    mem_args, NULL_TREE,
2775117395Skan				    TYPE_BINFO (type),
2776117395Skan				    TYPE_BINFO (type),
2777117395Skan				    LOOKUP_NORMAL, DEDUCE_CALL);
277850397Sobrien	  else
2779117395Skan	    add_function_candidate
2780117395Skan	      (&candidates, fn, base, mem_args, TYPE_BINFO (type),
2781117395Skan	       TYPE_BINFO (type), LOOKUP_NORMAL);
278218334Speter	}
278318334Speter    }
278418334Speter
278550397Sobrien  convs = lookup_conversions (type);
278618334Speter
278750397Sobrien  for (; convs; convs = TREE_CHAIN (convs))
278850397Sobrien    {
278950397Sobrien      tree fns = TREE_VALUE (convs);
279050397Sobrien      tree totype = TREE_TYPE (TREE_TYPE (OVL_CURRENT (fns)));
279118334Speter
279252284Sobrien      if ((TREE_CODE (totype) == POINTER_TYPE
279390075Sobrien	   && TREE_CODE (TREE_TYPE (totype)) == FUNCTION_TYPE)
279490075Sobrien	  || (TREE_CODE (totype) == REFERENCE_TYPE
279590075Sobrien	      && TREE_CODE (TREE_TYPE (totype)) == FUNCTION_TYPE)
279690075Sobrien	  || (TREE_CODE (totype) == REFERENCE_TYPE
279790075Sobrien	      && TREE_CODE (TREE_TYPE (totype)) == POINTER_TYPE
279890075Sobrien	      && TREE_CODE (TREE_TYPE (TREE_TYPE (totype))) == FUNCTION_TYPE))
279952284Sobrien	for (; fns; fns = OVL_NEXT (fns))
280050397Sobrien	  {
280152284Sobrien	    tree fn = OVL_CURRENT (fns);
280250397Sobrien	    if (TREE_CODE (fn) == TEMPLATE_DECL)
2803117395Skan	      add_template_conv_candidate
2804117395Skan		(&candidates, fn, obj, args, totype,
2805117395Skan		 /*access_path=*/NULL_TREE,
2806117395Skan		 /*conversion_path=*/NULL_TREE);
280750397Sobrien	    else
2808117395Skan	      add_conv_candidate (&candidates, fn, obj, args,
2809117395Skan				  /*conversion_path=*/NULL_TREE,
2810117395Skan				  /*access_path=*/NULL_TREE);
281150397Sobrien	  }
281250397Sobrien    }
281350397Sobrien
2814132718Skan  candidates = splice_viable (candidates, pedantic, &any_viable_p);
2815132718Skan  if (!any_viable_p)
281618334Speter    {
281790075Sobrien      error ("no match for call to `(%T) (%A)'", TREE_TYPE (obj), args);
281850397Sobrien      print_z_candidates (candidates);
281950397Sobrien      return error_mark_node;
282050397Sobrien    }
282118334Speter
282250397Sobrien  cand = tourney (candidates);
282350397Sobrien  if (cand == 0)
282450397Sobrien    {
282590075Sobrien      error ("call of `(%T) (%A)' is ambiguous", TREE_TYPE (obj), args);
282650397Sobrien      print_z_candidates (candidates);
282750397Sobrien      return error_mark_node;
282850397Sobrien    }
282918334Speter
283052284Sobrien  /* Since cand->fn will be a type, not a function, for a conversion
283152284Sobrien     function, we must be careful not to unconditionally look at
283252284Sobrien     DECL_NAME here.  */
283352284Sobrien  if (TREE_CODE (cand->fn) == FUNCTION_DECL
283490075Sobrien      && DECL_OVERLOADED_OPERATOR_P (cand->fn) == CALL_EXPR)
2835132718Skan    return build_over_call (cand, LOOKUP_NORMAL);
283618334Speter
283790075Sobrien  obj = convert_like_with_context
283890075Sobrien          (TREE_VEC_ELT (cand->convs, 0), obj, cand->fn, -1);
283918334Speter
284050397Sobrien  /* FIXME */
284150397Sobrien  return build_function_call (obj, args);
284250397Sobrien}
284318334Speter
284450397Sobrienstatic void
2845132718Skanop_error (enum tree_code code, enum tree_code code2,
2846132718Skan          tree arg1, tree arg2, tree arg3, const char *problem)
284750397Sobrien{
284890075Sobrien  const char *opname;
284918334Speter
285090075Sobrien  if (code == MODIFY_EXPR)
285190075Sobrien    opname = assignment_operator_name_info[code2].name;
285290075Sobrien  else
285390075Sobrien    opname = operator_name_info[code].name;
285490075Sobrien
285550397Sobrien  switch (code)
285650397Sobrien    {
285750397Sobrien    case COND_EXPR:
2858117395Skan      error ("%s for ternary 'operator?:' in '%E ? %E : %E'",
2859117395Skan             problem, arg1, arg2, arg3);
286050397Sobrien      break;
2861117395Skan
286250397Sobrien    case POSTINCREMENT_EXPR:
286350397Sobrien    case POSTDECREMENT_EXPR:
2864117395Skan      error ("%s for 'operator%s' in '%E%s'", problem, opname, arg1, opname);
286550397Sobrien      break;
2866117395Skan
286750397Sobrien    case ARRAY_REF:
2868117395Skan      error ("%s for 'operator[]' in '%E[%E]'", problem, arg1, arg2);
286950397Sobrien      break;
2870132718Skan
2871132718Skan    case REALPART_EXPR:
2872132718Skan    case IMAGPART_EXPR:
2873132718Skan      error ("%s for '%s' in '%s %E'", problem, opname, opname, arg1);
2874132718Skan      break;
2875117395Skan
287650397Sobrien    default:
287750397Sobrien      if (arg2)
2878117395Skan	error ("%s for 'operator%s' in '%E %s %E'",
2879117395Skan               problem, opname, arg1, opname, arg2);
288050397Sobrien      else
2881117395Skan	error ("%s for 'operator%s' in '%s%E'",
2882117395Skan               problem, opname, opname, arg1);
2883117395Skan      break;
288450397Sobrien    }
288550397Sobrien}
288618334Speter
288790075Sobrien/* Return the implicit conversion sequence that could be used to
288890075Sobrien   convert E1 to E2 in [expr.cond].  */
288990075Sobrien
289090075Sobrienstatic tree
2891132718Skanconditional_conversion (tree e1, tree e2)
289290075Sobrien{
289390075Sobrien  tree t1 = non_reference (TREE_TYPE (e1));
289490075Sobrien  tree t2 = non_reference (TREE_TYPE (e2));
289590075Sobrien  tree conv;
2896117395Skan  bool good_base;
289790075Sobrien
289890075Sobrien  /* [expr.cond]
289990075Sobrien
290090075Sobrien     If E2 is an lvalue: E1 can be converted to match E2 if E1 can be
290190075Sobrien     implicitly converted (clause _conv_) to the type "reference to
290290075Sobrien     T2", subject to the constraint that in the conversion the
290390075Sobrien     reference must bind directly (_dcl.init.ref_) to E1.  */
290490075Sobrien  if (real_lvalue_p (e2))
290590075Sobrien    {
290690075Sobrien      conv = implicit_conversion (build_reference_type (t2),
290790075Sobrien				  t1,
290890075Sobrien				  e1,
290990075Sobrien				  LOOKUP_NO_TEMP_BIND);
291090075Sobrien      if (conv)
291190075Sobrien	return conv;
291290075Sobrien    }
291390075Sobrien
291490075Sobrien  /* [expr.cond]
291590075Sobrien
291690075Sobrien     If E1 and E2 have class type, and the underlying class types are
291790075Sobrien     the same or one is a base class of the other: E1 can be converted
291890075Sobrien     to match E2 if the class of T2 is the same type as, or a base
291990075Sobrien     class of, the class of T1, and the cv-qualification of T2 is the
292090075Sobrien     same cv-qualification as, or a greater cv-qualification than, the
292190075Sobrien     cv-qualification of T1.  If the conversion is applied, E1 is
292290075Sobrien     changed to an rvalue of type T2 that still refers to the original
292390075Sobrien     source class object (or the appropriate subobject thereof).  */
292490075Sobrien  if (CLASS_TYPE_P (t1) && CLASS_TYPE_P (t2)
2925117395Skan      && ((good_base = DERIVED_FROM_P (t2, t1)) || DERIVED_FROM_P (t1, t2)))
292690075Sobrien    {
2927117395Skan      if (good_base && at_least_as_qualified_p (t2, t1))
292890075Sobrien	{
292990075Sobrien	  conv = build1 (IDENTITY_CONV, t1, e1);
293090075Sobrien	  if (!same_type_p (TYPE_MAIN_VARIANT (t1),
293190075Sobrien			    TYPE_MAIN_VARIANT (t2)))
293290075Sobrien	    conv = build_conv (BASE_CONV, t2, conv);
2933132718Skan	  else
2934132718Skan	    conv = build_conv (RVALUE_CONV, t2, conv);
293590075Sobrien	  return conv;
293690075Sobrien	}
293790075Sobrien      else
293890075Sobrien	return NULL_TREE;
293990075Sobrien    }
2940117395Skan  else
2941117395Skan    /* [expr.cond]
294290075Sobrien
2943117395Skan       Otherwise: E1 can be converted to match E2 if E1 can be implicitly
2944117395Skan       converted to the type that expression E2 would have if E2 were
2945117395Skan       converted to an rvalue (or the type it has, if E2 is an rvalue).  */
2946117395Skan    return implicit_conversion (t2, t1, e1, LOOKUP_NORMAL);
294790075Sobrien}
294890075Sobrien
294990075Sobrien/* Implement [expr.cond].  ARG1, ARG2, and ARG3 are the three
2950117395Skan   arguments to the conditional expression.  */
295190075Sobrien
295250397Sobrientree
2953132718Skanbuild_conditional_expr (tree arg1, tree arg2, tree arg3)
295490075Sobrien{
295590075Sobrien  tree arg2_type;
295690075Sobrien  tree arg3_type;
295790075Sobrien  tree result;
295890075Sobrien  tree result_type = NULL_TREE;
2959132718Skan  bool lvalue_p = true;
296090075Sobrien  struct z_candidate *candidates = 0;
296190075Sobrien  struct z_candidate *cand;
296290075Sobrien
296390075Sobrien  /* As a G++ extension, the second argument to the conditional can be
296490075Sobrien     omitted.  (So that `a ? : c' is roughly equivalent to `a ? a :
296590075Sobrien     c'.)  If the second operand is omitted, make sure it is
296690075Sobrien     calculated only once.  */
296790075Sobrien  if (!arg2)
296890075Sobrien    {
296990075Sobrien      if (pedantic)
297090075Sobrien	pedwarn ("ISO C++ forbids omitting the middle term of a ?: expression");
2971132718Skan
2972132718Skan      /* Make sure that lvalues remain lvalues.  See g++.oliva/ext1.C.  */
2973132718Skan      if (real_lvalue_p (arg1))
2974132718Skan	arg2 = arg1 = stabilize_reference (arg1);
2975132718Skan      else
2976132718Skan	arg2 = arg1 = save_expr (arg1);
297790075Sobrien    }
297890075Sobrien
297990075Sobrien  /* [expr.cond]
298090075Sobrien
298190075Sobrien     The first expr ession is implicitly converted to bool (clause
298290075Sobrien     _conv_).  */
2983132718Skan  arg1 = perform_implicit_conversion (boolean_type_node, arg1);
298490075Sobrien
298590075Sobrien  /* If something has already gone wrong, just pass that fact up the
298690075Sobrien     tree.  */
2987132718Skan  if (error_operand_p (arg1)
2988132718Skan      || error_operand_p (arg2)
2989132718Skan      || error_operand_p (arg3))
299090075Sobrien    return error_mark_node;
299190075Sobrien
299290075Sobrien  /* [expr.cond]
299390075Sobrien
299490075Sobrien     If either the second or the third operand has type (possibly
299590075Sobrien     cv-qualified) void, then the lvalue-to-rvalue (_conv.lval_),
299690075Sobrien     array-to-pointer (_conv.array_), and function-to-pointer
299790075Sobrien     (_conv.func_) standard conversions are performed on the second
299890075Sobrien     and third operands.  */
299990075Sobrien  arg2_type = TREE_TYPE (arg2);
300090075Sobrien  arg3_type = TREE_TYPE (arg3);
300190075Sobrien  if (VOID_TYPE_P (arg2_type) || VOID_TYPE_P (arg3_type))
300290075Sobrien    {
300390075Sobrien      /* Do the conversions.  We don't these for `void' type arguments
300490075Sobrien	 since it can't have any effect and since decay_conversion
300590075Sobrien	 does not handle that case gracefully.  */
300690075Sobrien      if (!VOID_TYPE_P (arg2_type))
300790075Sobrien	arg2 = decay_conversion (arg2);
300890075Sobrien      if (!VOID_TYPE_P (arg3_type))
300990075Sobrien	arg3 = decay_conversion (arg3);
301090075Sobrien      arg2_type = TREE_TYPE (arg2);
301190075Sobrien      arg3_type = TREE_TYPE (arg3);
301290075Sobrien
301390075Sobrien      /* [expr.cond]
301490075Sobrien
301590075Sobrien	 One of the following shall hold:
301690075Sobrien
301790075Sobrien	 --The second or the third operand (but not both) is a
301890075Sobrien	   throw-expression (_except.throw_); the result is of the
301990075Sobrien	   type of the other and is an rvalue.
302090075Sobrien
302190075Sobrien	 --Both the second and the third operands have type void; the
3022132718Skan	   result is of type void and is an rvalue.
3023132718Skan
3024132718Skan         We must avoid calling force_rvalue for expressions of type
3025132718Skan	 "void" because it will complain that their value is being
3026132718Skan	 used.   */
3027132718Skan      if (TREE_CODE (arg2) == THROW_EXPR
3028132718Skan	  && TREE_CODE (arg3) != THROW_EXPR)
3029132718Skan	{
3030132718Skan	  if (!VOID_TYPE_P (arg3_type))
3031132718Skan	    arg3 = force_rvalue (arg3);
3032132718Skan	  arg3_type = TREE_TYPE (arg3);
3033132718Skan	  result_type = arg3_type;
3034132718Skan	}
3035132718Skan      else if (TREE_CODE (arg2) != THROW_EXPR
3036132718Skan	       && TREE_CODE (arg3) == THROW_EXPR)
3037132718Skan	{
3038132718Skan	  if (!VOID_TYPE_P (arg2_type))
3039132718Skan	    arg2 = force_rvalue (arg2);
3040132718Skan	  arg2_type = TREE_TYPE (arg2);
3041132718Skan	  result_type = arg2_type;
3042132718Skan	}
304390075Sobrien      else if (VOID_TYPE_P (arg2_type) && VOID_TYPE_P (arg3_type))
304490075Sobrien	result_type = void_type_node;
304590075Sobrien      else
304690075Sobrien	{
304790075Sobrien	  error ("`%E' has type `void' and is not a throw-expression",
304890075Sobrien		    VOID_TYPE_P (arg2_type) ? arg2 : arg3);
304990075Sobrien	  return error_mark_node;
305090075Sobrien	}
305190075Sobrien
3052132718Skan      lvalue_p = false;
305390075Sobrien      goto valid_operands;
305490075Sobrien    }
305590075Sobrien  /* [expr.cond]
305690075Sobrien
305790075Sobrien     Otherwise, if the second and third operand have different types,
305890075Sobrien     and either has (possibly cv-qualified) class type, an attempt is
305990075Sobrien     made to convert each of those operands to the type of the other.  */
306090075Sobrien  else if (!same_type_p (arg2_type, arg3_type)
306190075Sobrien	   && (CLASS_TYPE_P (arg2_type) || CLASS_TYPE_P (arg3_type)))
306290075Sobrien    {
306390075Sobrien      tree conv2 = conditional_conversion (arg2, arg3);
306490075Sobrien      tree conv3 = conditional_conversion (arg3, arg2);
306590075Sobrien
306690075Sobrien      /* [expr.cond]
306790075Sobrien
306890075Sobrien	 If both can be converted, or one can be converted but the
306990075Sobrien	 conversion is ambiguous, the program is ill-formed.  If
307090075Sobrien	 neither can be converted, the operands are left unchanged and
307190075Sobrien	 further checking is performed as described below.  If exactly
307290075Sobrien	 one conversion is possible, that conversion is applied to the
307390075Sobrien	 chosen operand and the converted operand is used in place of
307490075Sobrien	 the original operand for the remainder of this section.  */
307590075Sobrien      if ((conv2 && !ICS_BAD_FLAG (conv2)
307690075Sobrien	   && conv3 && !ICS_BAD_FLAG (conv3))
307790075Sobrien	  || (conv2 && TREE_CODE (conv2) == AMBIG_CONV)
307890075Sobrien	  || (conv3 && TREE_CODE (conv3) == AMBIG_CONV))
307990075Sobrien	{
308090075Sobrien	  error ("operands to ?: have different types");
308190075Sobrien	  return error_mark_node;
308290075Sobrien	}
308390075Sobrien      else if (conv2 && !ICS_BAD_FLAG (conv2))
308490075Sobrien	{
308590075Sobrien	  arg2 = convert_like (conv2, arg2);
308690075Sobrien	  arg2 = convert_from_reference (arg2);
308790075Sobrien	  arg2_type = TREE_TYPE (arg2);
308890075Sobrien	}
308990075Sobrien      else if (conv3 && !ICS_BAD_FLAG (conv3))
309090075Sobrien	{
309190075Sobrien	  arg3 = convert_like (conv3, arg3);
309290075Sobrien	  arg3 = convert_from_reference (arg3);
309390075Sobrien	  arg3_type = TREE_TYPE (arg3);
309490075Sobrien	}
3095132718Skan
3096132718Skan      /* If, after the conversion, both operands have class type,
3097132718Skan	 treat the cv-qualification of both operands as if it were the
3098132718Skan	 union of the cv-qualification of the operands.
3099132718Skan
3100132718Skan	 The standard is not clear about what to do in this
3101132718Skan	 circumstance.  For example, if the first operand has type
3102132718Skan	 "const X" and the second operand has a user-defined
3103132718Skan	 conversion to "volatile X", what is the type of the second
3104132718Skan	 operand after this step?  Making it be "const X" (matching
3105132718Skan	 the first operand) seems wrong, as that discards the
3106132718Skan	 qualification without actuall performing a copy.  Leaving it
3107132718Skan	 as "volatile X" seems wrong as that will result in the
3108132718Skan	 conditional expression failing altogether, even though,
3109132718Skan	 according to this step, the one operand could be converted to
3110132718Skan	 the type of the other.  */
3111132718Skan      if ((conv2 || conv3)
3112132718Skan	  && CLASS_TYPE_P (arg2_type)
3113132718Skan	  && TYPE_QUALS (arg2_type) != TYPE_QUALS (arg3_type))
3114132718Skan	arg2_type = arg3_type =
3115132718Skan	  cp_build_qualified_type (arg2_type,
3116132718Skan				   TYPE_QUALS (arg2_type)
3117132718Skan				   | TYPE_QUALS (arg3_type));
311890075Sobrien    }
311990075Sobrien
312090075Sobrien  /* [expr.cond]
312190075Sobrien
312290075Sobrien     If the second and third operands are lvalues and have the same
312390075Sobrien     type, the result is of that type and is an lvalue.  */
3124132718Skan  if (real_lvalue_p (arg2)
3125132718Skan      && real_lvalue_p (arg3)
3126132718Skan      && same_type_p (arg2_type, arg3_type))
312790075Sobrien    {
312890075Sobrien      result_type = arg2_type;
312990075Sobrien      goto valid_operands;
313090075Sobrien    }
313190075Sobrien
313290075Sobrien  /* [expr.cond]
313390075Sobrien
313490075Sobrien     Otherwise, the result is an rvalue.  If the second and third
313590075Sobrien     operand do not have the same type, and either has (possibly
313690075Sobrien     cv-qualified) class type, overload resolution is used to
313790075Sobrien     determine the conversions (if any) to be applied to the operands
313890075Sobrien     (_over.match.oper_, _over.built_).  */
3139132718Skan  lvalue_p = false;
314090075Sobrien  if (!same_type_p (arg2_type, arg3_type)
314190075Sobrien      && (CLASS_TYPE_P (arg2_type) || CLASS_TYPE_P (arg3_type)))
314290075Sobrien    {
314390075Sobrien      tree args[3];
314490075Sobrien      tree conv;
3145132718Skan      bool any_viable_p;
314690075Sobrien
314790075Sobrien      /* Rearrange the arguments so that add_builtin_candidate only has
314890075Sobrien	 to know about two args.  In build_builtin_candidates, the
314990075Sobrien	 arguments are unscrambled.  */
315090075Sobrien      args[0] = arg2;
315190075Sobrien      args[1] = arg3;
315290075Sobrien      args[2] = arg1;
3153117395Skan      add_builtin_candidates (&candidates,
3154117395Skan			      COND_EXPR,
3155117395Skan			      NOP_EXPR,
3156117395Skan			      ansi_opname (COND_EXPR),
3157117395Skan			      args,
3158117395Skan			      LOOKUP_NORMAL);
315990075Sobrien
316090075Sobrien      /* [expr.cond]
316190075Sobrien
316290075Sobrien	 If the overload resolution fails, the program is
316390075Sobrien	 ill-formed.  */
3164132718Skan      candidates = splice_viable (candidates, pedantic, &any_viable_p);
3165132718Skan      if (!any_viable_p)
316690075Sobrien	{
316790075Sobrien	  op_error (COND_EXPR, NOP_EXPR, arg1, arg2, arg3, "no match");
316890075Sobrien	  print_z_candidates (candidates);
316990075Sobrien	  return error_mark_node;
317090075Sobrien	}
317190075Sobrien      cand = tourney (candidates);
317290075Sobrien      if (!cand)
317390075Sobrien	{
317490075Sobrien	  op_error (COND_EXPR, NOP_EXPR, arg1, arg2, arg3, "no match");
317590075Sobrien	  print_z_candidates (candidates);
317690075Sobrien	  return error_mark_node;
317790075Sobrien	}
317890075Sobrien
317990075Sobrien      /* [expr.cond]
318090075Sobrien
318190075Sobrien	 Otherwise, the conversions thus determined are applied, and
318290075Sobrien	 the converted operands are used in place of the original
318390075Sobrien	 operands for the remainder of this section.  */
318490075Sobrien      conv = TREE_VEC_ELT (cand->convs, 0);
318590075Sobrien      arg1 = convert_like (conv, arg1);
318690075Sobrien      conv = TREE_VEC_ELT (cand->convs, 1);
318790075Sobrien      arg2 = convert_like (conv, arg2);
318890075Sobrien      conv = TREE_VEC_ELT (cand->convs, 2);
318990075Sobrien      arg3 = convert_like (conv, arg3);
319090075Sobrien    }
319190075Sobrien
319290075Sobrien  /* [expr.cond]
319390075Sobrien
319490075Sobrien     Lvalue-to-rvalue (_conv.lval_), array-to-pointer (_conv.array_),
319590075Sobrien     and function-to-pointer (_conv.func_) standard conversions are
319690075Sobrien     performed on the second and third operands.
319790075Sobrien
319890075Sobrien     We need to force the lvalue-to-rvalue conversion here for class types,
319990075Sobrien     so we get TARGET_EXPRs; trying to deal with a COND_EXPR of class rvalues
320090075Sobrien     that isn't wrapped with a TARGET_EXPR plays havoc with exception
3201132718Skan     regions.  */
320290075Sobrien
3203117395Skan  arg2 = force_rvalue (arg2);
3204132718Skan  if (!CLASS_TYPE_P (arg2_type))
3205132718Skan    arg2_type = TREE_TYPE (arg2);
320690075Sobrien
3207117395Skan  arg3 = force_rvalue (arg3);
3208132718Skan  if (!CLASS_TYPE_P (arg2_type))
3209132718Skan    arg3_type = TREE_TYPE (arg3);
321090075Sobrien
321190075Sobrien  if (arg2 == error_mark_node || arg3 == error_mark_node)
321290075Sobrien    return error_mark_node;
321390075Sobrien
321490075Sobrien  /* [expr.cond]
321590075Sobrien
321690075Sobrien     After those conversions, one of the following shall hold:
321790075Sobrien
321890075Sobrien     --The second and third operands have the same type; the result  is  of
321990075Sobrien       that type.  */
322090075Sobrien  if (same_type_p (arg2_type, arg3_type))
322190075Sobrien    result_type = arg2_type;
322290075Sobrien  /* [expr.cond]
322390075Sobrien
322490075Sobrien     --The second and third operands have arithmetic or enumeration
322590075Sobrien       type; the usual arithmetic conversions are performed to bring
322690075Sobrien       them to a common type, and the result is of that type.  */
322790075Sobrien  else if ((ARITHMETIC_TYPE_P (arg2_type)
322890075Sobrien	    || TREE_CODE (arg2_type) == ENUMERAL_TYPE)
322990075Sobrien	   && (ARITHMETIC_TYPE_P (arg3_type)
323090075Sobrien	       || TREE_CODE (arg3_type) == ENUMERAL_TYPE))
323190075Sobrien    {
323290075Sobrien      /* In this case, there is always a common type.  */
323390075Sobrien      result_type = type_after_usual_arithmetic_conversions (arg2_type,
323490075Sobrien							     arg3_type);
323590075Sobrien
323690075Sobrien      if (TREE_CODE (arg2_type) == ENUMERAL_TYPE
323790075Sobrien          && TREE_CODE (arg3_type) == ENUMERAL_TYPE)
323890075Sobrien         warning ("enumeral mismatch in conditional expression: `%T' vs `%T'",
323990075Sobrien                   arg2_type, arg3_type);
324090075Sobrien      else if (extra_warnings
324190075Sobrien               && ((TREE_CODE (arg2_type) == ENUMERAL_TYPE
324290075Sobrien                    && !same_type_p (arg3_type, type_promotes_to (arg2_type)))
324390075Sobrien                   || (TREE_CODE (arg3_type) == ENUMERAL_TYPE
324490075Sobrien                       && !same_type_p (arg2_type, type_promotes_to (arg3_type)))))
324590075Sobrien        warning ("enumeral and non-enumeral type in conditional expression");
324690075Sobrien
324790075Sobrien      arg2 = perform_implicit_conversion (result_type, arg2);
324890075Sobrien      arg3 = perform_implicit_conversion (result_type, arg3);
324990075Sobrien    }
325090075Sobrien  /* [expr.cond]
325190075Sobrien
325290075Sobrien     --The second and third operands have pointer type, or one has
325390075Sobrien       pointer type and the other is a null pointer constant; pointer
325490075Sobrien       conversions (_conv.ptr_) and qualification conversions
325590075Sobrien       (_conv.qual_) are performed to bring them to their composite
325690075Sobrien       pointer type (_expr.rel_).  The result is of the composite
325790075Sobrien       pointer type.
325890075Sobrien
325990075Sobrien     --The second and third operands have pointer to member type, or
326090075Sobrien       one has pointer to member type and the other is a null pointer
326190075Sobrien       constant; pointer to member conversions (_conv.mem_) and
326290075Sobrien       qualification conversions (_conv.qual_) are performed to bring
326390075Sobrien       them to a common type, whose cv-qualification shall match the
326490075Sobrien       cv-qualification of either the second or the third operand.
3265117395Skan       The result is of the common type.  */
326690075Sobrien  else if ((null_ptr_cst_p (arg2)
3267132718Skan	    && (TYPE_PTR_P (arg3_type) || TYPE_PTR_TO_MEMBER_P (arg3_type)))
326890075Sobrien	   || (null_ptr_cst_p (arg3)
3269132718Skan	       && (TYPE_PTR_P (arg2_type) || TYPE_PTR_TO_MEMBER_P (arg2_type)))
327090075Sobrien	   || (TYPE_PTR_P (arg2_type) && TYPE_PTR_P (arg3_type))
327190075Sobrien	   || (TYPE_PTRMEM_P (arg2_type) && TYPE_PTRMEM_P (arg3_type))
3272132718Skan	   || (TYPE_PTRMEMFUNC_P (arg2_type) && TYPE_PTRMEMFUNC_P (arg3_type)))
327390075Sobrien    {
327490075Sobrien      result_type = composite_pointer_type (arg2_type, arg3_type, arg2,
327590075Sobrien					    arg3, "conditional expression");
3276132718Skan      if (result_type == error_mark_node)
3277132718Skan	return error_mark_node;
327890075Sobrien      arg2 = perform_implicit_conversion (result_type, arg2);
327990075Sobrien      arg3 = perform_implicit_conversion (result_type, arg3);
328090075Sobrien    }
328190075Sobrien
328290075Sobrien  if (!result_type)
328390075Sobrien    {
328490075Sobrien      error ("operands to ?: have different types");
328590075Sobrien      return error_mark_node;
328690075Sobrien    }
328790075Sobrien
328890075Sobrien valid_operands:
3289146895Skan  result = fold_if_not_in_template (build (COND_EXPR, result_type,
3290146895Skan					   arg1, arg2, arg3));
3291132718Skan  /* We can't use result_type below, as fold might have returned a
3292132718Skan     throw_expr.  */
3293132718Skan
329490075Sobrien  /* Expand both sides into the same slot, hopefully the target of the
329590075Sobrien     ?: expression.  We used to check for TARGET_EXPRs here, but now we
329690075Sobrien     sometimes wrap them in NOP_EXPRs so the test would fail.  */
3297132718Skan  if (!lvalue_p && CLASS_TYPE_P (TREE_TYPE (result)))
3298132718Skan    result = get_target_expr (result);
329990075Sobrien
330090075Sobrien  /* If this expression is an rvalue, but might be mistaken for an
330190075Sobrien     lvalue, we must add a NON_LVALUE_EXPR.  */
330290075Sobrien  if (!lvalue_p && real_lvalue_p (result))
3303132718Skan    result = build1 (NON_LVALUE_EXPR, TREE_TYPE (result), result);
330490075Sobrien
330590075Sobrien  return result;
330690075Sobrien}
330790075Sobrien
3308132718Skan/* OPERAND is an operand to an expression.  Perform necessary steps
3309132718Skan   required before using it.  If OPERAND is NULL_TREE, NULL_TREE is
3310132718Skan   returned.  */
3311132718Skan
3312132718Skanstatic tree
3313132718Skanprep_operand (tree operand)
3314132718Skan{
3315132718Skan  if (operand)
3316132718Skan    {
3317132718Skan      operand = convert_from_reference (operand);
3318132718Skan      if (CLASS_TYPE_P (TREE_TYPE (operand))
3319132718Skan	  && CLASSTYPE_TEMPLATE_INSTANTIATION (TREE_TYPE (operand)))
3320132718Skan	/* Make sure the template type is instantiated now.  */
3321132718Skan	instantiate_class_template (TYPE_MAIN_VARIANT (TREE_TYPE (operand)));
3322132718Skan    }
3323132718Skan
3324132718Skan  return operand;
3325132718Skan}
3326132718Skan
3327132718Skan/* Add each of the viable functions in FNS (a FUNCTION_DECL or
3328132718Skan   OVERLOAD) to the CANDIDATES, returning an updated list of
3329132718Skan   CANDIDATES.  The ARGS are the arguments provided to the call,
3330132718Skan   without any implicit object parameter.  The EXPLICIT_TARGS are
3331132718Skan   explicit template arguments provided.  TEMPLATE_ONLY is true if
3332132718Skan   only template functions should be considered.  CONVERSION_PATH,
3333132718Skan   ACCESS_PATH, and FLAGS are as for add_function_candidate.  */
3334132718Skan
3335132718Skanstatic void
3336132718Skanadd_candidates (tree fns, tree args,
3337132718Skan		tree explicit_targs, bool template_only,
3338132718Skan		tree conversion_path, tree access_path,
3339132718Skan		int flags,
3340132718Skan		struct z_candidate **candidates)
3341132718Skan{
3342132718Skan  tree ctype;
3343132718Skan  tree non_static_args;
3344132718Skan
3345132718Skan  ctype = conversion_path ? BINFO_TYPE (conversion_path) : NULL_TREE;
3346132718Skan  /* Delay creating the implicit this parameter until it is needed.  */
3347132718Skan  non_static_args = NULL_TREE;
3348132718Skan
3349132718Skan  while (fns)
3350132718Skan    {
3351132718Skan      tree fn;
3352132718Skan      tree fn_args;
3353132718Skan
3354132718Skan      fn = OVL_CURRENT (fns);
3355132718Skan      /* Figure out which set of arguments to use.  */
3356132718Skan      if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
3357132718Skan	{
3358132718Skan	  /* If this function is a non-static member, prepend the implicit
3359132718Skan	     object parameter.  */
3360132718Skan	  if (!non_static_args)
3361132718Skan	    non_static_args = tree_cons (NULL_TREE,
3362132718Skan					 build_this (TREE_VALUE (args)),
3363132718Skan					 TREE_CHAIN (args));
3364132718Skan	  fn_args = non_static_args;
3365132718Skan	}
3366132718Skan      else
3367132718Skan	/* Otherwise, just use the list of arguments provided.  */
3368132718Skan	fn_args = args;
3369132718Skan
3370132718Skan      if (TREE_CODE (fn) == TEMPLATE_DECL)
3371132718Skan	add_template_candidate (candidates,
3372132718Skan				fn,
3373132718Skan				ctype,
3374132718Skan				explicit_targs,
3375132718Skan				fn_args,
3376132718Skan				NULL_TREE,
3377132718Skan				access_path,
3378132718Skan				conversion_path,
3379132718Skan				flags,
3380132718Skan				DEDUCE_CALL);
3381132718Skan      else if (!template_only)
3382132718Skan	add_function_candidate (candidates,
3383132718Skan				fn,
3384132718Skan				ctype,
3385132718Skan				fn_args,
3386132718Skan				access_path,
3387132718Skan				conversion_path,
3388132718Skan				flags);
3389132718Skan      fns = OVL_NEXT (fns);
3390132718Skan    }
3391132718Skan}
3392132718Skan
339390075Sobrientree
3394132718Skanbuild_new_op (enum tree_code code, int flags, tree arg1, tree arg2, tree arg3,
3395132718Skan	      bool *overloaded_p)
339650397Sobrien{
339750397Sobrien  struct z_candidate *candidates = 0, *cand;
3398132718Skan  tree arglist, fnname;
3399132718Skan  tree args[3];
340050397Sobrien  enum tree_code code2 = NOP_EXPR;
340150397Sobrien  tree conv;
3402132718Skan  bool strict_p;
3403132718Skan  bool any_viable_p;
340418334Speter
3405132718Skan  if (error_operand_p (arg1)
3406132718Skan      || error_operand_p (arg2)
3407132718Skan      || error_operand_p (arg3))
340850397Sobrien    return error_mark_node;
340918334Speter
341050397Sobrien  if (code == MODIFY_EXPR)
341150397Sobrien    {
341250397Sobrien      code2 = TREE_CODE (arg3);
341350397Sobrien      arg3 = NULL_TREE;
341490075Sobrien      fnname = ansi_assopname (code2);
341550397Sobrien    }
341650397Sobrien  else
341790075Sobrien    fnname = ansi_opname (code);
341818334Speter
3419132718Skan  arg1 = prep_operand (arg1);
342090075Sobrien
342150397Sobrien  switch (code)
342250397Sobrien    {
342350397Sobrien    case NEW_EXPR:
342450397Sobrien    case VEC_NEW_EXPR:
342550397Sobrien    case VEC_DELETE_EXPR:
342650397Sobrien    case DELETE_EXPR:
3427117395Skan      /* Use build_op_new_call and build_op_delete_call instead.  */
342890075Sobrien      abort ();
342918334Speter
343050397Sobrien    case CALL_EXPR:
343150397Sobrien      return build_object_call (arg1, arg2);
343218334Speter
343350397Sobrien    default:
343450397Sobrien      break;
343550397Sobrien    }
343618334Speter
3437132718Skan  arg2 = prep_operand (arg2);
3438132718Skan  arg3 = prep_operand (arg3);
343990075Sobrien
344050397Sobrien  if (code == COND_EXPR)
344150397Sobrien    {
344250397Sobrien      if (arg2 == NULL_TREE
344350397Sobrien	  || TREE_CODE (TREE_TYPE (arg2)) == VOID_TYPE
344450397Sobrien	  || TREE_CODE (TREE_TYPE (arg3)) == VOID_TYPE
344550397Sobrien	  || (! IS_OVERLOAD_TYPE (TREE_TYPE (arg2))
344650397Sobrien	      && ! IS_OVERLOAD_TYPE (TREE_TYPE (arg3))))
344750397Sobrien	goto builtin;
344850397Sobrien    }
344950397Sobrien  else if (! IS_OVERLOAD_TYPE (TREE_TYPE (arg1))
345050397Sobrien	   && (! arg2 || ! IS_OVERLOAD_TYPE (TREE_TYPE (arg2))))
345150397Sobrien    goto builtin;
345218334Speter
345350397Sobrien  if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
345450397Sobrien    arg2 = integer_zero_node;
345518334Speter
345690075Sobrien  arglist = NULL_TREE;
345790075Sobrien  if (arg3)
345890075Sobrien    arglist = tree_cons (NULL_TREE, arg3, arglist);
345990075Sobrien  if (arg2)
346090075Sobrien    arglist = tree_cons (NULL_TREE, arg2, arglist);
346190075Sobrien  arglist = tree_cons (NULL_TREE, arg1, arglist);
346218334Speter
3463132718Skan  /* Add namespace-scope operators to the list of functions to
3464132718Skan     consider.  */
3465132718Skan  add_candidates (lookup_function_nonclass (fnname, arglist),
3466132718Skan		  arglist, NULL_TREE, false, NULL_TREE, NULL_TREE,
3467132718Skan		  flags, &candidates);
3468132718Skan  /* Add class-member operators to the candidate set.  */
3469132718Skan  if (CLASS_TYPE_P (TREE_TYPE (arg1)))
347050397Sobrien    {
3471132718Skan      tree fns;
347218334Speter
347350397Sobrien      fns = lookup_fnfields (TYPE_BINFO (TREE_TYPE (arg1)), fnname, 1);
347450397Sobrien      if (fns == error_mark_node)
347550397Sobrien	return fns;
3476132718Skan      if (fns)
3477132718Skan	add_candidates (BASELINK_FUNCTIONS (fns), arglist,
3478132718Skan			NULL_TREE, false,
3479132718Skan			BASELINK_BINFO (fns),
3480132718Skan			TYPE_BINFO (TREE_TYPE (arg1)),
3481132718Skan			flags, &candidates);
348250397Sobrien    }
348318334Speter
3484132718Skan  /* Rearrange the arguments for ?: so that add_builtin_candidate only has
3485132718Skan     to know about two args; a builtin candidate will always have a first
3486132718Skan     parameter of type bool.  We'll handle that in
3487132718Skan     build_builtin_candidate.  */
3488132718Skan  if (code == COND_EXPR)
348950397Sobrien    {
3490132718Skan      args[0] = arg2;
3491132718Skan      args[1] = arg3;
3492132718Skan      args[2] = arg1;
349350397Sobrien    }
3494132718Skan  else
3495132718Skan    {
3496132718Skan      args[0] = arg1;
3497132718Skan      args[1] = arg2;
3498132718Skan      args[2] = NULL_TREE;
3499132718Skan    }
350018334Speter
3501132718Skan  add_builtin_candidates (&candidates, code, code2, fnname, args, flags);
350218334Speter
3503117395Skan  switch (code)
350450397Sobrien    {
3505117395Skan    case COMPOUND_EXPR:
3506117395Skan    case ADDR_EXPR:
3507117395Skan      /* For these, the built-in candidates set is empty
3508117395Skan	 [over.match.oper]/3.  We don't want non-strict matches
3509117395Skan	 because exact matches are always possible with built-in
3510117395Skan	 operators.  The built-in candidate set for COMPONENT_REF
3511117395Skan	 would be empty too, but since there are no such built-in
3512117395Skan	 operators, we accept non-strict matches for them.  */
3513132718Skan      strict_p = true;
3514117395Skan      break;
3515117395Skan
3516117395Skan    default:
3517132718Skan      strict_p = pedantic;
3518117395Skan      break;
3519117395Skan    }
3520117395Skan
3521132718Skan  candidates = splice_viable (candidates, strict_p, &any_viable_p);
3522132718Skan  if (!any_viable_p)
3523117395Skan    {
352450397Sobrien      switch (code)
352518334Speter	{
352650397Sobrien	case POSTINCREMENT_EXPR:
352750397Sobrien	case POSTDECREMENT_EXPR:
352850397Sobrien	  /* Look for an `operator++ (int)'.  If they didn't have
352950397Sobrien	     one, then we fall back to the old way of doing things.  */
353018334Speter	  if (flags & LOOKUP_COMPLAIN)
353190075Sobrien	    pedwarn ("no `%D(int)' declared for postfix `%s', trying prefix operator instead",
353290075Sobrien			fnname,
353390075Sobrien			operator_name_info[code].name);
353450397Sobrien	  if (code == POSTINCREMENT_EXPR)
353550397Sobrien	    code = PREINCREMENT_EXPR;
353618334Speter	  else
353750397Sobrien	    code = PREDECREMENT_EXPR;
3538132718Skan	  return build_new_op (code, flags, arg1, NULL_TREE, NULL_TREE,
3539132718Skan			       overloaded_p);
354050397Sobrien
354150397Sobrien	  /* The caller will deal with these.  */
354250397Sobrien	case ADDR_EXPR:
354350397Sobrien	case COMPOUND_EXPR:
354450397Sobrien	case COMPONENT_REF:
354550397Sobrien	  return NULL_TREE;
354650397Sobrien
354750397Sobrien	default:
354850397Sobrien	  break;
354918334Speter	}
355018334Speter      if (flags & LOOKUP_COMPLAIN)
355118334Speter	{
355250397Sobrien	  op_error (code, code2, arg1, arg2, arg3, "no match");
355350397Sobrien	  print_z_candidates (candidates);
355418334Speter	}
355518334Speter      return error_mark_node;
355618334Speter    }
3557132718Skan
355850397Sobrien  cand = tourney (candidates);
355950397Sobrien  if (cand == 0)
356018334Speter    {
356118334Speter      if (flags & LOOKUP_COMPLAIN)
356218334Speter	{
356350397Sobrien	  op_error (code, code2, arg1, arg2, arg3, "ambiguous overload");
356450397Sobrien	  print_z_candidates (candidates);
356518334Speter	}
356618334Speter      return error_mark_node;
356718334Speter    }
356818334Speter
356950397Sobrien  if (TREE_CODE (cand->fn) == FUNCTION_DECL)
357050397Sobrien    {
3571132718Skan      if (overloaded_p)
3572132718Skan	*overloaded_p = true;
3573132718Skan
3574132718Skan      return build_over_call (cand, LOOKUP_NORMAL);
357550397Sobrien    }
357650397Sobrien
357750397Sobrien  /* Check for comparison of different enum types.  */
357850397Sobrien  switch (code)
357918334Speter    {
358050397Sobrien    case GT_EXPR:
358150397Sobrien    case LT_EXPR:
358250397Sobrien    case GE_EXPR:
358350397Sobrien    case LE_EXPR:
358450397Sobrien    case EQ_EXPR:
358550397Sobrien    case NE_EXPR:
358652284Sobrien      if (TREE_CODE (TREE_TYPE (arg1)) == ENUMERAL_TYPE
358750397Sobrien	  && TREE_CODE (TREE_TYPE (arg2)) == ENUMERAL_TYPE
358850397Sobrien	  && (TYPE_MAIN_VARIANT (TREE_TYPE (arg1))
358950397Sobrien	      != TYPE_MAIN_VARIANT (TREE_TYPE (arg2))))
359050397Sobrien	{
359190075Sobrien	  warning ("comparison between `%#T' and `%#T'",
359250397Sobrien		      TREE_TYPE (arg1), TREE_TYPE (arg2));
359318334Speter	}
359450397Sobrien      break;
359550397Sobrien    default:
359650397Sobrien      break;
359750397Sobrien    }
359818334Speter
359950397Sobrien  /* We need to strip any leading REF_BIND so that bitfields don't cause
360050397Sobrien     errors.  This should not remove any important conversions, because
360150397Sobrien     builtins don't apply to class objects directly.  */
360250397Sobrien  conv = TREE_VEC_ELT (cand->convs, 0);
360350397Sobrien  if (TREE_CODE (conv) == REF_BIND)
360450397Sobrien    conv = TREE_OPERAND (conv, 0);
360550397Sobrien  arg1 = convert_like (conv, arg1);
360650397Sobrien  if (arg2)
360752284Sobrien    {
360852284Sobrien      conv = TREE_VEC_ELT (cand->convs, 1);
360952284Sobrien      if (TREE_CODE (conv) == REF_BIND)
361052284Sobrien        conv = TREE_OPERAND (conv, 0);
361152284Sobrien      arg2 = convert_like (conv, arg2);
361252284Sobrien    }
361350397Sobrien  if (arg3)
361452284Sobrien    {
361552284Sobrien      conv = TREE_VEC_ELT (cand->convs, 2);
361652284Sobrien      if (TREE_CODE (conv) == REF_BIND)
361752284Sobrien        conv = TREE_OPERAND (conv, 0);
361852284Sobrien      arg3 = convert_like (conv, arg3);
361952284Sobrien    }
362018334Speter
362150397Sobrienbuiltin:
362250397Sobrien  switch (code)
362350397Sobrien    {
362450397Sobrien    case MODIFY_EXPR:
362550397Sobrien      return build_modify_expr (arg1, code2, arg2);
362618334Speter
362750397Sobrien    case INDIRECT_REF:
362850397Sobrien      return build_indirect_ref (arg1, "unary *");
362918334Speter
363050397Sobrien    case PLUS_EXPR:
363150397Sobrien    case MINUS_EXPR:
363250397Sobrien    case MULT_EXPR:
363350397Sobrien    case TRUNC_DIV_EXPR:
363450397Sobrien    case GT_EXPR:
363550397Sobrien    case LT_EXPR:
363650397Sobrien    case GE_EXPR:
363750397Sobrien    case LE_EXPR:
363850397Sobrien    case EQ_EXPR:
363950397Sobrien    case NE_EXPR:
364050397Sobrien    case MAX_EXPR:
364150397Sobrien    case MIN_EXPR:
364250397Sobrien    case LSHIFT_EXPR:
364350397Sobrien    case RSHIFT_EXPR:
364450397Sobrien    case TRUNC_MOD_EXPR:
364550397Sobrien    case BIT_AND_EXPR:
364650397Sobrien    case BIT_IOR_EXPR:
364750397Sobrien    case BIT_XOR_EXPR:
364850397Sobrien    case TRUTH_ANDIF_EXPR:
364950397Sobrien    case TRUTH_ORIF_EXPR:
365090075Sobrien      return cp_build_binary_op (code, arg1, arg2);
365118334Speter
365250397Sobrien    case CONVERT_EXPR:
365350397Sobrien    case NEGATE_EXPR:
365450397Sobrien    case BIT_NOT_EXPR:
365550397Sobrien    case TRUTH_NOT_EXPR:
365650397Sobrien    case PREINCREMENT_EXPR:
365750397Sobrien    case POSTINCREMENT_EXPR:
365850397Sobrien    case PREDECREMENT_EXPR:
365950397Sobrien    case POSTDECREMENT_EXPR:
366050397Sobrien    case REALPART_EXPR:
366150397Sobrien    case IMAGPART_EXPR:
366250397Sobrien      return build_unary_op (code, arg1, candidates != 0);
366350397Sobrien
366450397Sobrien    case ARRAY_REF:
366550397Sobrien      return build_array_ref (arg1, arg2);
366650397Sobrien
366750397Sobrien    case COND_EXPR:
366850397Sobrien      return build_conditional_expr (arg1, arg2, arg3);
366950397Sobrien
367050397Sobrien    case MEMBER_REF:
367150397Sobrien      return build_m_component_ref
367290075Sobrien	(build_indirect_ref (arg1, NULL), arg2);
367350397Sobrien
367450397Sobrien      /* The caller will deal with these.  */
367550397Sobrien    case ADDR_EXPR:
367650397Sobrien    case COMPONENT_REF:
367750397Sobrien    case COMPOUND_EXPR:
367850397Sobrien      return NULL_TREE;
367950397Sobrien
368050397Sobrien    default:
368190075Sobrien      abort ();
368250397Sobrien      return NULL_TREE;
368350397Sobrien    }
368450397Sobrien}
368550397Sobrien
368650397Sobrien/* Build a call to operator delete.  This has to be handled very specially,
368750397Sobrien   because the restrictions on what signatures match are different from all
368850397Sobrien   other call instances.  For a normal delete, only a delete taking (void *)
368950397Sobrien   or (void *, size_t) is accepted.  For a placement delete, only an exact
369050397Sobrien   match with the placement new is accepted.
369150397Sobrien
369250397Sobrien   CODE is either DELETE_EXPR or VEC_DELETE_EXPR.
369396263Sobrien   ADDR is the pointer to be deleted.
369450397Sobrien   SIZE is the size of the memory block to be deleted.
369550397Sobrien   FLAGS are the usual overloading flags.
369690075Sobrien   PLACEMENT is the corresponding placement new call, or NULL_TREE.  */
369750397Sobrien
369850397Sobrientree
3699132718Skanbuild_op_delete_call (enum tree_code code, tree addr, tree size,
3700132718Skan                      int flags, tree placement)
370150397Sobrien{
370290075Sobrien  tree fn = NULL_TREE;
3703132718Skan  tree fns, fnname, argtypes, args, type;
370490075Sobrien  int pass;
370550397Sobrien
370650397Sobrien  if (addr == error_mark_node)
370750397Sobrien    return error_mark_node;
370850397Sobrien
3709132718Skan  type = strip_array_types (TREE_TYPE (TREE_TYPE (addr)));
371050397Sobrien
371190075Sobrien  fnname = ansi_opname (code);
371290075Sobrien
371350397Sobrien  if (IS_AGGR_TYPE (type) && ! (flags & LOOKUP_GLOBAL))
371450397Sobrien    /* In [class.free]
371550397Sobrien
371650397Sobrien       If the result of the lookup is ambiguous or inaccessible, or if
371750397Sobrien       the lookup selects a placement deallocation function, the
371850397Sobrien       program is ill-formed.
371950397Sobrien
3720132718Skan       Therefore, we ask lookup_fnfields to complain about ambiguity.  */
372150397Sobrien    {
372250397Sobrien      fns = lookup_fnfields (TYPE_BINFO (type), fnname, 1);
372350397Sobrien      if (fns == error_mark_node)
372450397Sobrien	return error_mark_node;
372518334Speter    }
372650397Sobrien  else
372750397Sobrien    fns = NULL_TREE;
372818334Speter
372950397Sobrien  if (fns == NULL_TREE)
373050397Sobrien    fns = lookup_name_nonclass (fnname);
373150397Sobrien
373250397Sobrien  if (placement)
373318334Speter    {
373496263Sobrien      tree alloc_fn;
373596263Sobrien      tree call_expr;
373650397Sobrien
3737117395Skan      /* Find the allocation function that is being called.  */
373896263Sobrien      call_expr = placement;
373950397Sobrien      /* Extract the function.  */
374096263Sobrien      alloc_fn = get_callee_fndecl (call_expr);
374196263Sobrien      my_friendly_assert (alloc_fn != NULL_TREE, 20020327);
374250397Sobrien      /* Then the second parm type.  */
374396263Sobrien      argtypes = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (alloc_fn)));
374450397Sobrien      /* Also the second argument.  */
374596263Sobrien      args = TREE_CHAIN (TREE_OPERAND (call_expr, 1));
374650397Sobrien    }
374750397Sobrien  else
374850397Sobrien    {
374950397Sobrien      /* First try it without the size argument.  */
375050397Sobrien      argtypes = void_list_node;
375150397Sobrien      args = NULL_TREE;
375250397Sobrien    }
375350397Sobrien
375450397Sobrien  /* Strip const and volatile from addr.  */
375590075Sobrien  addr = cp_convert (ptr_type_node, addr);
375650397Sobrien
375790075Sobrien  /* We make two tries at finding a matching `operator delete'.  On
3758132718Skan     the first pass, we look for a one-operator (or placement)
375990075Sobrien     operator delete.  If we're not doing placement delete, then on
376090075Sobrien     the second pass we look for a two-argument delete.  */
376190075Sobrien  for (pass = 0; pass < (placement ? 1 : 2); ++pass)
376290075Sobrien    {
376390075Sobrien      /* Go through the `operator delete' functions looking for one
376490075Sobrien	 with a matching type.  */
3765117395Skan      for (fn = BASELINK_P (fns) ? BASELINK_FUNCTIONS (fns) : fns;
376690075Sobrien	   fn;
376790075Sobrien	   fn = OVL_NEXT (fn))
376890075Sobrien	{
376990075Sobrien	  tree t;
377090075Sobrien
3771132718Skan	  /* The first argument must be "void *".  */
3772132718Skan	  t = TYPE_ARG_TYPES (TREE_TYPE (OVL_CURRENT (fn)));
3773132718Skan	  if (!same_type_p (TREE_VALUE (t), ptr_type_node))
3774132718Skan	    continue;
3775132718Skan	  t = TREE_CHAIN (t);
3776132718Skan	  /* On the first pass, check the rest of the arguments.  */
3777132718Skan	  if (pass == 0)
3778132718Skan	    {
3779132718Skan	      tree a = argtypes;
3780132718Skan	      while (a && t)
3781132718Skan		{
3782132718Skan		  if (!same_type_p (TREE_VALUE (a), TREE_VALUE (t)))
3783132718Skan		    break;
3784132718Skan		  a = TREE_CHAIN (a);
3785132718Skan		  t = TREE_CHAIN (t);
3786132718Skan		}
3787132718Skan	      if (!a && !t)
3788132718Skan		break;
3789132718Skan	    }
3790132718Skan	  /* On the second pass, the second argument must be
3791132718Skan	     "size_t".  */
3792132718Skan	  else if (pass == 1
3793132718Skan		   && same_type_p (TREE_VALUE (t), sizetype)
3794132718Skan		   && TREE_CHAIN (t) == void_list_node)
379590075Sobrien	    break;
379690075Sobrien	}
379790075Sobrien
379890075Sobrien      /* If we found a match, we're done.  */
379990075Sobrien      if (fn)
380090075Sobrien	break;
380150397Sobrien    }
380250397Sobrien
380390075Sobrien  /* If we have a matching function, call it.  */
380490075Sobrien  if (fn)
380590075Sobrien    {
380690075Sobrien      /* Make sure we have the actual function, and not an
380790075Sobrien	 OVERLOAD.  */
380890075Sobrien      fn = OVL_CURRENT (fn);
380950397Sobrien
381090075Sobrien      /* If the FN is a member function, make sure that it is
381190075Sobrien	 accessible.  */
381290075Sobrien      if (DECL_CLASS_SCOPE_P (fn))
3813132718Skan	perform_or_defer_access_check (TYPE_BINFO (type), fn);
381450397Sobrien
381590075Sobrien      if (pass == 0)
381690075Sobrien	args = tree_cons (NULL_TREE, addr, args);
381790075Sobrien      else
381890075Sobrien	args = tree_cons (NULL_TREE, addr,
381990075Sobrien			  build_tree_list (NULL_TREE, size));
382050397Sobrien
3821132718Skan      if (placement)
3822132718Skan	{
3823132718Skan	  /* The placement args might not be suitable for overload
3824132718Skan	     resolution at this point, so build the call directly.  */
3825132718Skan	  mark_used (fn);
3826132718Skan	  return build_cxx_call (fn, args, args);
3827132718Skan	}
3828132718Skan      else
3829132718Skan	return build_function_call (fn, args);
383050397Sobrien    }
383150397Sobrien
383290075Sobrien  /* If we are doing placement delete we do nothing if we don't find a
383390075Sobrien     matching op delete.  */
383490075Sobrien  if (placement)
383550397Sobrien    return NULL_TREE;
383650397Sobrien
3837132718Skan  error ("no suitable `operator %s' for `%T'",
3838132718Skan	 operator_name_info[(int)code].name, type);
383950397Sobrien  return error_mark_node;
384050397Sobrien}
384150397Sobrien
384250397Sobrien/* If the current scope isn't allowed to access DECL along
384352284Sobrien   BASETYPE_PATH, give an error.  The most derived class in
384452284Sobrien   BASETYPE_PATH is the one used to qualify DECL.  */
384550397Sobrien
3846132718Skanbool
3847132718Skanenforce_access (tree basetype_path, tree decl)
384850397Sobrien{
3849132718Skan  my_friendly_assert (TREE_CODE (basetype_path) == TREE_VEC, 20030624);
3850132718Skan
3851132718Skan  if (!accessible_p (basetype_path, decl))
385250397Sobrien    {
385352284Sobrien      if (TREE_PRIVATE (decl))
385452284Sobrien	cp_error_at ("`%+#D' is private", decl);
385552284Sobrien      else if (TREE_PROTECTED (decl))
385652284Sobrien	cp_error_at ("`%+#D' is protected", decl);
385752284Sobrien      else
385852284Sobrien	cp_error_at ("`%+#D' is inaccessible", decl);
385990075Sobrien      error ("within this context");
3860132718Skan      return false;
386150397Sobrien    }
386252284Sobrien
3863132718Skan  return true;
386450397Sobrien}
386550397Sobrien
3866132718Skan/* Check that a callable constructor to initialize a temporary of
3867132718Skan   TYPE from an EXPR exists.  */
3868132718Skan
3869132718Skanstatic void
3870132718Skancheck_constructor_callable (tree type, tree expr)
3871132718Skan{
3872132718Skan  build_special_member_call (NULL_TREE,
3873132718Skan			     complete_ctor_identifier,
3874132718Skan			     build_tree_list (NULL_TREE, expr),
3875132718Skan			     TYPE_BINFO (type),
3876132718Skan			     LOOKUP_NORMAL | LOOKUP_ONLYCONVERTING
3877146895Skan			     | LOOKUP_NO_CONVERSION
3878132718Skan			     | LOOKUP_CONSTRUCTOR_CALLABLE);
3879132718Skan}
3880132718Skan
3881132718Skan/* Initialize a temporary of type TYPE with EXPR.  The FLAGS are a
3882132718Skan   bitwise or of LOOKUP_* values.  If any errors are warnings are
3883132718Skan   generated, set *DIAGNOSTIC_FN to "error" or "warning",
3884132718Skan   respectively.  If no diagnostics are generated, set *DIAGNOSTIC_FN
3885132718Skan   to NULL.  */
3886132718Skan
3887132718Skanstatic tree
3888132718Skanbuild_temp (tree expr, tree type, int flags,
3889132718Skan	    void (**diagnostic_fn)(const char *, ...))
3890132718Skan{
3891132718Skan  int savew, savee;
3892132718Skan
3893132718Skan  savew = warningcount, savee = errorcount;
3894132718Skan  expr = build_special_member_call (NULL_TREE,
3895132718Skan				    complete_ctor_identifier,
3896132718Skan				    build_tree_list (NULL_TREE, expr),
3897132718Skan				    TYPE_BINFO (type),
3898132718Skan				    flags);
3899132718Skan  if (warningcount > savew)
3900132718Skan    *diagnostic_fn = warning;
3901132718Skan  else if (errorcount > savee)
3902132718Skan    *diagnostic_fn = error;
3903132718Skan  else
3904132718Skan    *diagnostic_fn = NULL;
3905132718Skan  return expr;
3906132718Skan}
3907132718Skan
3908132718Skan
3909117395Skan/* Perform the conversions in CONVS on the expression EXPR.  FN and
3910117395Skan   ARGNUM are used for diagnostics.  ARGNUM is zero based, -1
3911117395Skan   indicates the `this' argument of a method.  INNER is nonzero when
391290075Sobrien   being called to continue a conversion chain. It is negative when a
3913117395Skan   reference binding will be applied, positive otherwise.  If
3914117395Skan   ISSUE_CONVERSION_WARNINGS is true, warnings about suspicious
3915117395Skan   conversions will be emitted if appropriate.  */
391650397Sobrien
391750397Sobrienstatic tree
3918117395Skanconvert_like_real (tree convs, tree expr, tree fn, int argnum, int inner,
3919117395Skan		   bool issue_conversion_warnings)
392050397Sobrien{
392190075Sobrien  tree totype = TREE_TYPE (convs);
3922132718Skan  void (*diagnostic_fn)(const char *, ...);
392390075Sobrien
392450397Sobrien  if (ICS_BAD_FLAG (convs)
392550397Sobrien      && TREE_CODE (convs) != USER_CONV
392690075Sobrien      && TREE_CODE (convs) != AMBIG_CONV
392790075Sobrien      && TREE_CODE (convs) != REF_BIND)
392850397Sobrien    {
392950397Sobrien      tree t = convs;
393050397Sobrien      for (; t; t = TREE_OPERAND (t, 0))
393118334Speter	{
393290075Sobrien	  if (TREE_CODE (t) == USER_CONV || !ICS_BAD_FLAG (t))
393318334Speter	    {
3934117395Skan	      expr = convert_like_real (t, expr, fn, argnum, 1,
3935117395Skan					/*issue_conversion_warnings=*/false);
393650397Sobrien	      break;
393718334Speter	    }
393850397Sobrien	  else if (TREE_CODE (t) == AMBIG_CONV)
3939117395Skan	    return convert_like_real (t, expr, fn, argnum, 1,
3940117395Skan				      /*issue_conversion_warnings=*/false);
394150397Sobrien	  else if (TREE_CODE (t) == IDENTITY_CONV)
394250397Sobrien	    break;
394318334Speter	}
394490075Sobrien      pedwarn ("invalid conversion from `%T' to `%T'", TREE_TYPE (expr), totype);
394590075Sobrien      if (fn)
394690075Sobrien	pedwarn ("  initializing argument %P of `%D'", argnum, fn);
394790075Sobrien      return cp_convert (totype, expr);
394818334Speter    }
394990075Sobrien
3950117395Skan  if (issue_conversion_warnings)
395190075Sobrien    expr = dubious_conversion_warnings
3952132718Skan             (totype, expr, "converting", fn, argnum);
395350397Sobrien  switch (TREE_CODE (convs))
395450397Sobrien    {
395550397Sobrien    case USER_CONV:
395650397Sobrien      {
3957117395Skan	struct z_candidate *cand = USER_CONV_CAND (convs);
395890075Sobrien	tree convfn = cand->fn;
395950397Sobrien	tree args;
396018334Speter
396190075Sobrien	if (DECL_CONSTRUCTOR_P (convfn))
396250397Sobrien	  {
396350397Sobrien	    tree t = build_int_2 (0, 0);
396490075Sobrien	    TREE_TYPE (t) = build_pointer_type (DECL_CONTEXT (convfn));
396550397Sobrien
396690075Sobrien	    args = build_tree_list (NULL_TREE, expr);
396790075Sobrien	    if (DECL_HAS_IN_CHARGE_PARM_P (convfn)
396890075Sobrien		|| DECL_HAS_VTT_PARM_P (convfn))
396990075Sobrien	      /* We should never try to call the abstract or base constructor
397090075Sobrien		 from here.  */
397190075Sobrien	      abort ();
397290075Sobrien	    args = tree_cons (NULL_TREE, t, args);
397350397Sobrien	  }
397450397Sobrien	else
397550397Sobrien	  args = build_this (expr);
3976132718Skan	expr = build_over_call (cand, LOOKUP_NORMAL);
397750397Sobrien
397850397Sobrien	/* If this is a constructor or a function returning an aggr type,
397950397Sobrien	   we need to build up a TARGET_EXPR.  */
398090075Sobrien	if (DECL_CONSTRUCTOR_P (convfn))
398190075Sobrien	  expr = build_cplus_new (totype, expr);
398250397Sobrien
398390075Sobrien	/* The result of the call is then used to direct-initialize the object
398490075Sobrien	   that is the destination of the copy-initialization.  [dcl.init]
398590075Sobrien
398690075Sobrien	   Note that this step is not reflected in the conversion sequence;
398790075Sobrien	   it affects the semantics when we actually perform the
398890075Sobrien	   conversion, but is not considered during overload resolution.
398990075Sobrien
399090075Sobrien	   If the target is a class, that means call a ctor.  */
399190075Sobrien	if (IS_AGGR_TYPE (totype)
399290075Sobrien	    && (inner >= 0 || !lvalue_p (expr)))
399390075Sobrien	  {
3994132718Skan	    expr = (build_temp
3995132718Skan		    (expr, totype,
3996132718Skan		     /* Core issue 84, now a DR, says that we don't
3997132718Skan			allow UDCs for these args (which deliberately
3998132718Skan			breaks copy-init of an auto_ptr<Base> from an
3999132718Skan			auto_ptr<Derived>).  */
4000132718Skan		     LOOKUP_NORMAL|LOOKUP_ONLYCONVERTING|LOOKUP_NO_CONVERSION,
4001132718Skan		     &diagnostic_fn));
4002132718Skan
4003132718Skan	    if (diagnostic_fn)
400490075Sobrien	      {
4005132718Skan		if (fn)
4006132718Skan		  diagnostic_fn
400790075Sobrien		    ("  initializing argument %P of `%D' from result of `%D'",
400890075Sobrien		     argnum, fn, convfn);
4009132718Skan		else
4010132718Skan		 diagnostic_fn
4011132718Skan		   ("  initializing temporary from result of `%D'",  convfn);
401290075Sobrien	      }
401390075Sobrien	    expr = build_cplus_new (totype, expr);
401490075Sobrien	  }
401550397Sobrien	return expr;
401650397Sobrien      }
401750397Sobrien    case IDENTITY_CONV:
401850397Sobrien      if (type_unknown_p (expr))
401996263Sobrien	expr = instantiate_type (totype, expr, tf_error | tf_warning);
4020132718Skan      /* Convert a non-array constant variable to its underlying
4021132718Skan	 value, unless we are about to bind it to a reference, in
4022132718Skan	 which case we need to leave it as an lvalue.  */
4023132718Skan      if (inner >= 0
4024132718Skan	  && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
4025132718Skan	expr = decl_constant_value (expr);
4026132718Skan      if (CHECK_COPY_CONSTRUCTOR_P (convs))
4027132718Skan	check_constructor_callable (totype, expr);
4028132718Skan
402950397Sobrien      return expr;
403050397Sobrien    case AMBIG_CONV:
403150397Sobrien      /* Call build_user_type_conversion again for the error.  */
403250397Sobrien      return build_user_type_conversion
403390075Sobrien	(totype, TREE_OPERAND (convs, 0), LOOKUP_NORMAL);
403450397Sobrien
403550397Sobrien    default:
403650397Sobrien      break;
403750397Sobrien    };
403850397Sobrien
403990075Sobrien  expr = convert_like_real (TREE_OPERAND (convs, 0), expr, fn, argnum,
4040117395Skan                            TREE_CODE (convs) == REF_BIND ? -1 : 1,
4041117395Skan			    /*issue_conversion_warnings=*/false);
404250397Sobrien  if (expr == error_mark_node)
404350397Sobrien    return error_mark_node;
404450397Sobrien
404550397Sobrien  switch (TREE_CODE (convs))
404618334Speter    {
404750397Sobrien    case RVALUE_CONV:
404890075Sobrien      if (! IS_AGGR_TYPE (totype))
404950397Sobrien	return expr;
4050132718Skan      /* Else fall through.  */
405150397Sobrien    case BASE_CONV:
405290075Sobrien      if (TREE_CODE (convs) == BASE_CONV && !NEED_TEMPORARY_P (convs))
405390075Sobrien	{
405490075Sobrien	  /* We are going to bind a reference directly to a base-class
405590075Sobrien	     subobject of EXPR.  */
4056132718Skan	  if (CHECK_COPY_CONSTRUCTOR_P (convs))
4057132718Skan	    check_constructor_callable (TREE_TYPE (expr), expr);
405890075Sobrien	  /* Build an expression for `*((base*) &expr)'.  */
405990075Sobrien	  expr = build_unary_op (ADDR_EXPR, expr, 0);
4060132718Skan	  expr = perform_implicit_conversion (build_pointer_type (totype),
4061132718Skan					      expr);
406290075Sobrien	  expr = build_indirect_ref (expr, "implicit conversion");
406390075Sobrien	  return expr;
406490075Sobrien	}
406590075Sobrien
406690075Sobrien      /* Copy-initialization where the cv-unqualified version of the source
406790075Sobrien	 type is the same class as, or a derived class of, the class of the
406890075Sobrien	 destination [is treated as direct-initialization].  [dcl.init] */
4069132718Skan      expr = build_temp (expr, totype, LOOKUP_NORMAL|LOOKUP_ONLYCONVERTING,
4070132718Skan			 &diagnostic_fn);
4071132718Skan      if (diagnostic_fn && fn)
4072132718Skan	diagnostic_fn ("  initializing argument %P of `%D'", argnum, fn);
407390075Sobrien      return build_cplus_new (totype, expr);
407490075Sobrien
407590075Sobrien    case REF_BIND:
407650397Sobrien      {
407790075Sobrien	tree ref_type = totype;
407890075Sobrien
407990075Sobrien	/* If necessary, create a temporary.  */
4080132718Skan	if (NEED_TEMPORARY_P (convs) || !lvalue_p (expr))
408150397Sobrien	  {
408290075Sobrien	    tree type = TREE_TYPE (TREE_OPERAND (convs, 0));
4083161651Skan	    cp_lvalue_kind lvalue = real_lvalue_p (expr);
4084132718Skan
4085132718Skan	    if (!CP_TYPE_CONST_NON_VOLATILE_P (TREE_TYPE (ref_type)))
4086132718Skan	      {
4087132718Skan		/* If the reference is volatile or non-const, we
4088132718Skan		   cannot create a temporary.  */
4089132718Skan		if (lvalue & clk_bitfield)
4090132718Skan		  error ("cannot bind bitfield `%E' to `%T'",
4091132718Skan			 expr, ref_type);
4092132718Skan		else if (lvalue & clk_packed)
4093132718Skan		  error ("cannot bind packed field `%E' to `%T'",
4094132718Skan			 expr, ref_type);
4095132718Skan		else
4096132718Skan		  error ("cannot bind rvalue `%E' to `%T'", expr, ref_type);
4097132718Skan		return error_mark_node;
4098132718Skan	      }
4099161651Skan	    /* If the source is a packed field, and we must use a copy
4100161651Skan	       constructor, then building the target expr will require
4101161651Skan	       binding the field to the reference parameter to the
4102161651Skan	       copy constructor, and we'll end up with an infinite
4103161651Skan	       loop.  If we can use a bitwise copy, then we'll be
4104161651Skan	       OK.  */
4105161651Skan	    if ((lvalue & clk_packed)
4106161651Skan		&& CLASS_TYPE_P (type)
4107161651Skan		&& !TYPE_HAS_TRIVIAL_INIT_REF (type))
4108161651Skan	      {
4109161651Skan		error ("cannot bind packed field `%E' to `%T'",
4110161651Skan		       expr, ref_type);
4111161651Skan		return error_mark_node;
4112161651Skan	      }
411390075Sobrien	    expr = build_target_expr_with_type (expr, type);
411450397Sobrien	  }
411590075Sobrien
411690075Sobrien	/* Take the address of the thing to which we will bind the
411790075Sobrien	   reference.  */
411890075Sobrien	expr = build_unary_op (ADDR_EXPR, expr, 1);
411990075Sobrien	if (expr == error_mark_node)
412090075Sobrien	  return error_mark_node;
412190075Sobrien
412290075Sobrien	/* Convert it to a pointer to the type referred to by the
412390075Sobrien	   reference.  This will adjust the pointer if a derived to
412490075Sobrien	   base conversion is being performed.  */
412590075Sobrien	expr = cp_convert (build_pointer_type (TREE_TYPE (ref_type)),
412690075Sobrien			   expr);
412790075Sobrien	/* Convert the pointer to the desired reference type.  */
4128117395Skan	return build_nop (ref_type, expr);
412950397Sobrien      }
413050397Sobrien
413150397Sobrien    case LVALUE_CONV:
413250397Sobrien      return decay_conversion (expr);
413350397Sobrien
413452284Sobrien    case QUAL_CONV:
413552284Sobrien      /* Warn about deprecated conversion if appropriate.  */
413690075Sobrien      string_conv_p (totype, expr, 1);
413752284Sobrien      break;
413852284Sobrien
413950397Sobrien    default:
414050397Sobrien      break;
414118334Speter    }
414290075Sobrien  return ocp_convert (totype, expr, CONV_IMPLICIT,
414350397Sobrien		      LOOKUP_NORMAL|LOOKUP_NO_CONVERSION);
414450397Sobrien}
414518334Speter
4146132718Skan/* Build a call to __builtin_trap.  */
4147117395Skan
4148117395Skanstatic tree
4149132718Skancall_builtin_trap (void)
4150117395Skan{
4151132718Skan  tree fn = IDENTIFIER_GLOBAL_VALUE (get_identifier ("__builtin_trap"));
4152117395Skan
4153132718Skan  my_friendly_assert (fn != NULL, 20030927);
4154117395Skan  fn = build_call (fn, NULL_TREE);
4155117395Skan  return fn;
4156117395Skan}
4157117395Skan
415850397Sobrien/* ARG is being passed to a varargs function.  Perform any conversions
4159132718Skan   required.  Return the converted value.  */
416050397Sobrien
416150397Sobrientree
4162132718Skanconvert_arg_to_ellipsis (tree arg)
416350397Sobrien{
4164132718Skan  /* [expr.call]
4165132718Skan
4166132718Skan     The lvalue-to-rvalue, array-to-pointer, and function-to-pointer
4167132718Skan     standard conversions are performed.  */
4168132718Skan  arg = decay_conversion (arg);
4169132718Skan  /* [expr.call]
4170132718Skan
4171132718Skan     If the argument has integral or enumeration type that is subject
4172132718Skan     to the integral promotions (_conv.prom_), or a floating point
4173132718Skan     type that is subject to the floating point promotion
4174132718Skan     (_conv.fpprom_), the value of the argument is converted to the
4175132718Skan     promoted type before the call.  */
417650397Sobrien  if (TREE_CODE (TREE_TYPE (arg)) == REAL_TYPE
417750397Sobrien      && (TYPE_PRECISION (TREE_TYPE (arg))
417850397Sobrien	  < TYPE_PRECISION (double_type_node)))
4179132718Skan    arg = convert_to_real (double_type_node, arg);
4180132718Skan  else if (INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (arg)))
4181132718Skan    arg = perform_integral_promotions (arg);
418250397Sobrien
418352284Sobrien  arg = require_complete_type (arg);
418452284Sobrien
4185132718Skan  if (arg != error_mark_node
4186132718Skan      && !pod_type_p (TREE_TYPE (arg)))
418790075Sobrien    {
4188117395Skan      /* Undefined behavior [expr.call] 5.2.2/7.  We used to just warn
4189102780Skan	 here and do a bitwise copy, but now cp_expr_size will abort if we
4190132718Skan	 try to do that.
4191132718Skan	 If the call appears in the context of a sizeof expression,
4192132718Skan	 there is no need to emit a warning, since the expression won't be
4193132718Skan	 evaluated. We keep the builtin_trap just as a safety check.  */
4194132718Skan      if (!skip_evaluation)
4195132718Skan	warning ("cannot pass objects of non-POD type `%#T' through `...'; "
4196132718Skan	         "call will abort at runtime", TREE_TYPE (arg));
4197117395Skan      arg = call_builtin_trap ();
4198132718Skan      arg = build (COMPOUND_EXPR, integer_type_node, arg,
4199132718Skan		   integer_zero_node);
420090075Sobrien    }
420190075Sobrien
420250397Sobrien  return arg;
420350397Sobrien}
420450397Sobrien
420590075Sobrien/* va_arg (EXPR, TYPE) is a builtin. Make sure it is not abused.  */
420690075Sobrien
420790075Sobrientree
4208132718Skanbuild_x_va_arg (tree expr, tree type)
420990075Sobrien{
421090075Sobrien  if (processing_template_decl)
421190075Sobrien    return build_min (VA_ARG_EXPR, type, expr);
421290075Sobrien
421390075Sobrien  type = complete_type_or_else (type, NULL_TREE);
421490075Sobrien
421590075Sobrien  if (expr == error_mark_node || !type)
421690075Sobrien    return error_mark_node;
421790075Sobrien
421890075Sobrien  if (! pod_type_p (type))
421990075Sobrien    {
4220117395Skan      /* Undefined behavior [expr.call] 5.2.2/7.  */
4221132718Skan      warning ("cannot receive objects of non-POD type `%#T' through `...'; \
4222132718Skancall will abort at runtime",
4223132718Skan	       type);
4224132718Skan      expr = convert (build_pointer_type (type), null_node);
4225132718Skan      expr = build (COMPOUND_EXPR, TREE_TYPE (expr),
4226132718Skan		    call_builtin_trap (), expr);
4227132718Skan      expr = build_indirect_ref (expr, NULL);
4228132718Skan      return expr;
422990075Sobrien    }
423090075Sobrien
423190075Sobrien  return build_va_arg (expr, type);
423290075Sobrien}
423390075Sobrien
4234117395Skan/* TYPE has been given to va_arg.  Apply the default conversions which
4235117395Skan   would have happened when passed via ellipsis.  Return the promoted
4236117395Skan   type, or the passed type if there is no change.  */
423790075Sobrien
423890075Sobrientree
4239132718Skancxx_type_promotes_to (tree type)
424090075Sobrien{
424190075Sobrien  tree promote;
4242117395Skan
4243132718Skan  /* Perform the array-to-pointer and function-to-pointer
4244132718Skan     conversions.  */
4245132718Skan  type = type_decays_to (type);
4246117395Skan
4247117395Skan  promote = type_promotes_to (type);
4248117395Skan  if (same_type_p (type, promote))
4249117395Skan    promote = type;
425090075Sobrien
4251117395Skan  return promote;
425290075Sobrien}
425390075Sobrien
425450397Sobrien/* ARG is a default argument expression being passed to a parameter of
425552284Sobrien   the indicated TYPE, which is a parameter to FN.  Do any required
425652284Sobrien   conversions.  Return the converted value.  */
425750397Sobrien
425850397Sobrientree
4259132718Skanconvert_default_arg (tree type, tree arg, tree fn, int parmnum)
426050397Sobrien{
4261132718Skan  /* If the ARG is an unparsed default argument expression, the
4262132718Skan     conversion cannot be performed.  */
426390075Sobrien  if (TREE_CODE (arg) == DEFAULT_ARG)
426452284Sobrien    {
4265132718Skan      error ("the default argument for parameter %d of `%D' has "
4266132718Skan	     "not yet been parsed",
4267132718Skan	     parmnum, fn);
4268132718Skan      return error_mark_node;
426990075Sobrien    }
427052284Sobrien
427190075Sobrien  if (fn && DECL_TEMPLATE_INFO (fn))
427290075Sobrien    arg = tsubst_default_argument (fn, type, arg);
427352284Sobrien
427450397Sobrien  arg = break_out_target_exprs (arg);
427550397Sobrien
427650397Sobrien  if (TREE_CODE (arg) == CONSTRUCTOR)
427718334Speter    {
427850397Sobrien      arg = digest_init (type, arg, 0);
427950397Sobrien      arg = convert_for_initialization (0, type, arg, LOOKUP_NORMAL,
428090075Sobrien					"default argument", fn, parmnum);
428150397Sobrien    }
428250397Sobrien  else
428350397Sobrien    {
428450397Sobrien      /* This could get clobbered by the following call.  */
428550397Sobrien      if (TREE_HAS_CONSTRUCTOR (arg))
428650397Sobrien	arg = copy_node (arg);
428750397Sobrien
428850397Sobrien      arg = convert_for_initialization (0, type, arg, LOOKUP_NORMAL,
428990075Sobrien					"default argument", fn, parmnum);
4290117395Skan      arg = convert_for_arg_passing (type, arg);
429150397Sobrien    }
429250397Sobrien
429350397Sobrien  return arg;
429450397Sobrien}
429550397Sobrien
4296117395Skan/* Returns the type which will really be used for passing an argument of
4297117395Skan   type TYPE.  */
4298117395Skan
4299117395Skantree
4300132718Skantype_passed_as (tree type)
4301117395Skan{
4302117395Skan  /* Pass classes with copy ctors by invisible reference.  */
4303117395Skan  if (TREE_ADDRESSABLE (type))
4304117395Skan    type = build_reference_type (type);
4305117395Skan  else if (PROMOTE_PROTOTYPES
4306117395Skan	   && INTEGRAL_TYPE_P (type)
4307132718Skan	   && COMPLETE_TYPE_P (type)
4308132718Skan	   && INT_CST_LT_UNSIGNED (TYPE_SIZE (type),
4309132718Skan				   TYPE_SIZE (integer_type_node)))
4310117395Skan    type = integer_type_node;
4311117395Skan
4312117395Skan  return type;
4313117395Skan}
4314117395Skan
4315117395Skan/* Actually perform the appropriate conversion.  */
4316117395Skan
4317117395Skantree
4318132718Skanconvert_for_arg_passing (tree type, tree val)
4319117395Skan{
4320117395Skan  if (val == error_mark_node)
4321117395Skan    ;
4322117395Skan  /* Pass classes with copy ctors by invisible reference.  */
4323117395Skan  else if (TREE_ADDRESSABLE (type))
4324117395Skan    val = build1 (ADDR_EXPR, build_reference_type (type), val);
4325117395Skan  else if (PROMOTE_PROTOTYPES
4326117395Skan	   && INTEGRAL_TYPE_P (type)
4327132718Skan	   && COMPLETE_TYPE_P (type)
4328132718Skan	   && INT_CST_LT_UNSIGNED (TYPE_SIZE (type),
4329132718Skan				   TYPE_SIZE (integer_type_node)))
4330132718Skan    val = perform_integral_promotions (val);
4331117395Skan  return val;
4332117395Skan}
4333117395Skan
4334132718Skan/* Returns true iff FN is a function with magic varargs, i.e. ones for
4335132718Skan   which no conversions at all should be done.  This is true for some
4336132718Skan   builtins which don't act like normal functions.  */
4337132718Skan
4338132718Skanstatic bool
4339132718Skanmagic_varargs_p (tree fn)
4340132718Skan{
4341132718Skan  if (DECL_BUILT_IN (fn))
4342132718Skan    switch (DECL_FUNCTION_CODE (fn))
4343132718Skan      {
4344132718Skan      case BUILT_IN_CLASSIFY_TYPE:
4345132718Skan      case BUILT_IN_CONSTANT_P:
4346132718Skan      case BUILT_IN_NEXT_ARG:
4347132718Skan      case BUILT_IN_STDARG_START:
4348132718Skan      case BUILT_IN_VA_START:
4349132718Skan	return true;
4350132718Skan
4351132718Skan      default:;
4352132718Skan      }
4353132718Skan
4354132718Skan  return false;
4355132718Skan}
4356132718Skan
435790075Sobrien/* Subroutine of the various build_*_call functions.  Overload resolution
435890075Sobrien   has chosen a winning candidate CAND; build up a CALL_EXPR accordingly.
435990075Sobrien   ARGS is a TREE_LIST of the unconverted arguments to the call.  FLAGS is a
436090075Sobrien   bitmask of various LOOKUP_* flags which apply to the call itself.  */
436190075Sobrien
436250397Sobrienstatic tree
4363132718Skanbuild_over_call (struct z_candidate *cand, int flags)
436450397Sobrien{
436550397Sobrien  tree fn = cand->fn;
4366132718Skan  tree args = cand->args;
436750397Sobrien  tree convs = cand->convs;
436850397Sobrien  tree converted_args = NULL_TREE;
436950397Sobrien  tree parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
437050397Sobrien  tree conv, arg, val;
437150397Sobrien  int i = 0;
437250397Sobrien  int is_method = 0;
437350397Sobrien
4374132718Skan  /* In a template, there is no need to perform all of the work that
4375132718Skan     is normally done.  We are only interested in the type of the call
4376132718Skan     expression, i.e., the return type of the function.  Any semantic
4377132718Skan     errors will be deferred until the template is instantiated.  */
4378132718Skan  if (processing_template_decl)
4379132718Skan    {
4380132718Skan      tree expr;
4381132718Skan      tree return_type;
4382132718Skan      return_type = TREE_TYPE (TREE_TYPE (fn));
4383132718Skan      expr = build (CALL_EXPR, return_type, fn, args);
4384132718Skan      if (TREE_THIS_VOLATILE (fn) && cfun)
4385132718Skan	current_function_returns_abnormally = 1;
4386132718Skan      if (!VOID_TYPE_P (return_type))
4387132718Skan	require_complete_type (return_type);
4388132718Skan      return convert_from_reference (expr);
4389132718Skan    }
4390132718Skan
439150397Sobrien  /* Give any warnings we noticed during overload resolution.  */
439250397Sobrien  if (cand->warnings)
439350397Sobrien    for (val = cand->warnings; val; val = TREE_CHAIN (val))
4394117395Skan      joust (cand, WRAPPER_ZC (TREE_VALUE (val)), 1);
439550397Sobrien
439650397Sobrien  if (DECL_FUNCTION_MEMBER_P (fn))
4397132718Skan    {
4398132718Skan      /* If FN is a template function, two cases must be considered.
4399132718Skan	 For example:
440050397Sobrien
4401132718Skan	   struct A {
4402132718Skan	     protected:
4403132718Skan	       template <class T> void f();
4404132718Skan	   };
4405132718Skan	   template <class T> struct B {
4406132718Skan	     protected:
4407132718Skan	       void g();
4408132718Skan	   };
4409132718Skan	   struct C : A, B<int> {
4410132718Skan	     using A::f;	// #1
4411132718Skan	     using B<int>::g;	// #2
4412132718Skan	   };
4413132718Skan
4414132718Skan	 In case #1 where `A::f' is a member template, DECL_ACCESS is
4415132718Skan	 recorded in the primary template but not in its specialization.
4416132718Skan	 We check access of FN using its primary template.
4417132718Skan
4418132718Skan	 In case #2, where `B<int>::g' has a DECL_TEMPLATE_INFO simply
4419132718Skan	 because it is a member of class template B, DECL_ACCESS is
4420132718Skan	 recorded in the specialization `B<int>::g'.  We cannot use its
4421132718Skan	 primary template because `B<T>::g' and `B<int>::g' may have
4422132718Skan	 different access.  */
4423132718Skan      if (DECL_TEMPLATE_INFO (fn)
4424132718Skan	  && is_member_template (DECL_TI_TEMPLATE (fn)))
4425132718Skan	perform_or_defer_access_check (cand->access_path,
4426132718Skan				       DECL_TI_TEMPLATE (fn));
4427132718Skan      else
4428132718Skan	perform_or_defer_access_check (cand->access_path, fn);
4429132718Skan    }
4430132718Skan
443150397Sobrien  if (args && TREE_CODE (args) != TREE_LIST)
443290075Sobrien    args = build_tree_list (NULL_TREE, args);
443350397Sobrien  arg = args;
443450397Sobrien
443550397Sobrien  /* The implicit parameters to a constructor are not considered by overload
443650397Sobrien     resolution, and must be of the proper type.  */
443750397Sobrien  if (DECL_CONSTRUCTOR_P (fn))
443850397Sobrien    {
443990075Sobrien      converted_args = tree_cons (NULL_TREE, TREE_VALUE (arg), converted_args);
444050397Sobrien      arg = TREE_CHAIN (arg);
444150397Sobrien      parm = TREE_CHAIN (parm);
444290075Sobrien      if (DECL_HAS_IN_CHARGE_PARM_P (fn))
444390075Sobrien	/* We should never try to call the abstract constructor.  */
444490075Sobrien	abort ();
444590075Sobrien      if (DECL_HAS_VTT_PARM_P (fn))
444618334Speter	{
444790075Sobrien	  converted_args = tree_cons
444850397Sobrien	    (NULL_TREE, TREE_VALUE (arg), converted_args);
444950397Sobrien	  arg = TREE_CHAIN (arg);
445050397Sobrien	  parm = TREE_CHAIN (parm);
445118334Speter	}
445250397Sobrien    }
445350397Sobrien  /* Bypass access control for 'this' parameter.  */
445450397Sobrien  else if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
445550397Sobrien    {
445650397Sobrien      tree parmtype = TREE_VALUE (parm);
445750397Sobrien      tree argtype = TREE_TYPE (TREE_VALUE (arg));
4458117395Skan      tree converted_arg;
4459117395Skan      tree base_binfo;
4460117395Skan
446150397Sobrien      if (ICS_BAD_FLAG (TREE_VEC_ELT (convs, i)))
446290075Sobrien	pedwarn ("passing `%T' as `this' argument of `%#D' discards qualifiers",
446352284Sobrien		    TREE_TYPE (argtype), fn);
446450397Sobrien
446552284Sobrien      /* [class.mfct.nonstatic]: If a nonstatic member function of a class
446652284Sobrien	 X is called for an object that is not of type X, or of a type
446752284Sobrien	 derived from X, the behavior is undefined.
446852284Sobrien
446952284Sobrien         So we can assume that anything passed as 'this' is non-null, and
447052284Sobrien	 optimize accordingly.  */
447190075Sobrien      my_friendly_assert (TREE_CODE (parmtype) == POINTER_TYPE, 19990811);
4472117395Skan      /* Convert to the base in which the function was declared.  */
4473117395Skan      my_friendly_assert (cand->conversion_path != NULL_TREE, 20020730);
4474117395Skan      converted_arg = build_base_path (PLUS_EXPR,
4475117395Skan				       TREE_VALUE (arg),
4476117395Skan				       cand->conversion_path,
4477117395Skan				       1);
4478119256Skan      /* Check that the base class is accessible.  */
4479119256Skan      if (!accessible_base_p (TREE_TYPE (argtype),
4480119256Skan			      BINFO_TYPE (cand->conversion_path)))
4481119256Skan	error ("`%T' is not an accessible base of `%T'",
4482119256Skan	       BINFO_TYPE (cand->conversion_path),
4483119256Skan	       TREE_TYPE (argtype));
4484117395Skan      /* If fn was found by a using declaration, the conversion path
4485117395Skan         will be to the derived class, not the base declaring fn. We
4486117395Skan         must convert from derived to base.  */
4487117395Skan      base_binfo = lookup_base (TREE_TYPE (TREE_TYPE (converted_arg)),
4488117395Skan				TREE_TYPE (parmtype), ba_ignore, NULL);
4489117395Skan      converted_arg = build_base_path (PLUS_EXPR, converted_arg,
4490117395Skan				       base_binfo, 1);
4491117395Skan
4492117395Skan      converted_args = tree_cons (NULL_TREE, converted_arg, converted_args);
449350397Sobrien      parm = TREE_CHAIN (parm);
449450397Sobrien      arg = TREE_CHAIN (arg);
449550397Sobrien      ++i;
449650397Sobrien      is_method = 1;
449718334Speter    }
449850397Sobrien
449950397Sobrien  for (; arg && parm;
450050397Sobrien       parm = TREE_CHAIN (parm), arg = TREE_CHAIN (arg), ++i)
450118334Speter    {
450250397Sobrien      tree type = TREE_VALUE (parm);
450318334Speter
450450397Sobrien      conv = TREE_VEC_ELT (convs, i);
450590075Sobrien      val = convert_like_with_context
450690075Sobrien	(conv, TREE_VALUE (arg), fn, i - is_method);
450718334Speter
4508117395Skan      val = convert_for_arg_passing (type, val);
450990075Sobrien      converted_args = tree_cons (NULL_TREE, val, converted_args);
451018334Speter    }
451118334Speter
451250397Sobrien  /* Default arguments */
451390075Sobrien  for (; parm && parm != void_list_node; parm = TREE_CHAIN (parm), i++)
451452284Sobrien    converted_args
451590075Sobrien      = tree_cons (NULL_TREE,
451690075Sobrien		   convert_default_arg (TREE_VALUE (parm),
451790075Sobrien					TREE_PURPOSE (parm),
451890075Sobrien					fn, i - is_method),
451990075Sobrien		   converted_args);
452018334Speter
452150397Sobrien  /* Ellipsis */
452250397Sobrien  for (; arg; arg = TREE_CHAIN (arg))
4523132718Skan    {
4524132718Skan      tree a = TREE_VALUE (arg);
4525132718Skan      if (magic_varargs_p (fn))
4526132718Skan	/* Do no conversions for magic varargs.  */;
4527132718Skan      else
4528132718Skan	a = convert_arg_to_ellipsis (a);
4529132718Skan      converted_args = tree_cons (NULL_TREE, a, converted_args);
4530132718Skan    }
453150397Sobrien
453250397Sobrien  converted_args = nreverse (converted_args);
453350397Sobrien
453490075Sobrien  if (warn_format)
453590075Sobrien    check_function_format (NULL, TYPE_ATTRIBUTES (TREE_TYPE (fn)),
453690075Sobrien			   converted_args);
453752284Sobrien
453850397Sobrien  /* Avoid actually calling copy constructors and copy assignment operators,
453950397Sobrien     if possible.  */
454052284Sobrien
454152284Sobrien  if (! flag_elide_constructors)
454252284Sobrien    /* Do things the hard way.  */;
454390075Sobrien  else if (TREE_VEC_LENGTH (convs) == 1
454490075Sobrien	   && DECL_COPY_CONSTRUCTOR_P (fn))
454550397Sobrien    {
454650397Sobrien      tree targ;
454790075Sobrien      arg = skip_artificial_parms_for (fn, converted_args);
454850397Sobrien      arg = TREE_VALUE (arg);
454950397Sobrien
455050397Sobrien      /* Pull out the real argument, disregarding const-correctness.  */
455150397Sobrien      targ = arg;
455250397Sobrien      while (TREE_CODE (targ) == NOP_EXPR
455350397Sobrien	     || TREE_CODE (targ) == NON_LVALUE_EXPR
455450397Sobrien	     || TREE_CODE (targ) == CONVERT_EXPR)
455550397Sobrien	targ = TREE_OPERAND (targ, 0);
455650397Sobrien      if (TREE_CODE (targ) == ADDR_EXPR)
455750397Sobrien	{
455850397Sobrien	  targ = TREE_OPERAND (targ, 0);
455990075Sobrien	  if (!same_type_ignoring_top_level_qualifiers_p
456090075Sobrien	      (TREE_TYPE (TREE_TYPE (arg)), TREE_TYPE (targ)))
456150397Sobrien	    targ = NULL_TREE;
456250397Sobrien	}
456318334Speter      else
456450397Sobrien	targ = NULL_TREE;
456518334Speter
456650397Sobrien      if (targ)
456750397Sobrien	arg = targ;
456850397Sobrien      else
456950397Sobrien	arg = build_indirect_ref (arg, 0);
457018334Speter
457150397Sobrien      /* [class.copy]: the copy constructor is implicitly defined even if
457250397Sobrien	 the implementation elided its use.  */
457350397Sobrien      if (TYPE_HAS_COMPLEX_INIT_REF (DECL_CONTEXT (fn)))
457450397Sobrien	mark_used (fn);
457550397Sobrien
457650397Sobrien      /* If we're creating a temp and we already have one, don't create a
457750397Sobrien         new one.  If we're not creating a temp but we get one, use
457850397Sobrien         INIT_EXPR to collapse the temp into our target.  Otherwise, if the
457950397Sobrien         ctor is trivial, do a bitwise copy with a simple TARGET_EXPR for a
458050397Sobrien         temp or an INIT_EXPR otherwise.  */
458150397Sobrien      if (integer_zerop (TREE_VALUE (args)))
458218334Speter	{
4583117395Skan	  if (TREE_CODE (arg) == TARGET_EXPR)
458450397Sobrien	    return arg;
458550397Sobrien	  else if (TYPE_HAS_TRIVIAL_INIT_REF (DECL_CONTEXT (fn)))
458690075Sobrien	    return build_target_expr_with_type (arg, DECL_CONTEXT (fn));
458718334Speter	}
4588117395Skan      else if (TREE_CODE (arg) == TARGET_EXPR
4589117395Skan	       || TYPE_HAS_TRIVIAL_INIT_REF (DECL_CONTEXT (fn)))
459050397Sobrien	{
459150397Sobrien	  tree to = stabilize_reference
459250397Sobrien	    (build_indirect_ref (TREE_VALUE (args), 0));
459318334Speter
459490075Sobrien	  val = build (INIT_EXPR, DECL_CONTEXT (fn), to, arg);
4595132718Skan	  return val;
459650397Sobrien	}
459718334Speter    }
459890075Sobrien  else if (DECL_OVERLOADED_OPERATOR_P (fn) == NOP_EXPR
459990075Sobrien	   && copy_fn_p (fn)
460090075Sobrien	   && TYPE_HAS_TRIVIAL_ASSIGN_REF (DECL_CONTEXT (fn)))
460150397Sobrien    {
460250397Sobrien      tree to = stabilize_reference
460350397Sobrien	(build_indirect_ref (TREE_VALUE (converted_args), 0));
4604132718Skan      tree type = TREE_TYPE (to);
4605132718Skan      tree as_base = CLASSTYPE_AS_BASE (type);
460618334Speter
460750397Sobrien      arg = build_indirect_ref (TREE_VALUE (TREE_CHAIN (converted_args)), 0);
4608132718Skan      if (tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (as_base)))
4609132718Skan	val = build (MODIFY_EXPR, TREE_TYPE (to), to, arg);
4610132718Skan      else
4611132718Skan	{
4612132718Skan	  /* We must only copy the non-tail padding parts. Use
4613132718Skan	     CLASSTYPE_AS_BASE for the bitwise copy.  */
4614132718Skan	  tree to_ptr, arg_ptr, to_as_base, arg_as_base, base_ptr_type;
4615132718Skan	  tree save_to;
4616132718Skan
4617132718Skan	  to_ptr = save_expr (build_unary_op (ADDR_EXPR, to, 0));
4618132718Skan	  arg_ptr = build_unary_op (ADDR_EXPR, arg, 0);
4619132718Skan
4620132718Skan	  base_ptr_type = build_pointer_type (as_base);
4621132718Skan	  to_as_base = build_nop (base_ptr_type, to_ptr);
4622132718Skan	  to_as_base = build_indirect_ref (to_as_base, 0);
4623132718Skan	  arg_as_base = build_nop (base_ptr_type, arg_ptr);
4624132718Skan	  arg_as_base = build_indirect_ref (arg_as_base, 0);
4625132718Skan
4626132718Skan	  save_to = build_indirect_ref (to_ptr, 0);
4627132718Skan
4628132718Skan	  val = build (MODIFY_EXPR, as_base, to_as_base, arg_as_base);
4629132718Skan	  val = convert_to_void (val, NULL);
4630132718Skan	  val = build (COMPOUND_EXPR, type, val, save_to);
4631132718Skan	  TREE_NO_UNUSED_WARNING (val) = 1;
4632132718Skan	}
4633132718Skan
463450397Sobrien      return val;
463550397Sobrien    }
463650397Sobrien
463750397Sobrien  mark_used (fn);
463850397Sobrien
463990075Sobrien  if (DECL_VINDEX (fn) && (flags & LOOKUP_NONVIRTUAL) == 0)
464018334Speter    {
464150397Sobrien      tree t, *p = &TREE_VALUE (converted_args);
464290075Sobrien      tree binfo = lookup_base (TREE_TYPE (TREE_TYPE (*p)),
4643117395Skan				DECL_CONTEXT (fn),
464490075Sobrien				ba_any, NULL);
464590075Sobrien      my_friendly_assert (binfo && binfo != error_mark_node, 20010730);
464690075Sobrien
464790075Sobrien      *p = build_base_path (PLUS_EXPR, *p, binfo, 1);
464850397Sobrien      if (TREE_SIDE_EFFECTS (*p))
464950397Sobrien	*p = save_expr (*p);
465050397Sobrien      t = build_pointer_type (TREE_TYPE (fn));
465190075Sobrien      if (DECL_CONTEXT (fn) && TYPE_JAVA_INTERFACE (DECL_CONTEXT (fn)))
465290075Sobrien	fn = build_java_interface_fn_ref (fn, *p);
465390075Sobrien      else
465490075Sobrien	fn = build_vfn_ref (build_indirect_ref (*p, 0), DECL_VINDEX (fn));
465550397Sobrien      TREE_TYPE (fn) = t;
465618334Speter    }
465750397Sobrien  else if (DECL_INLINE (fn))
465850397Sobrien    fn = inline_conversion (fn);
465950397Sobrien  else
466050397Sobrien    fn = build_addr_func (fn);
466118334Speter
4662117395Skan  return build_cxx_call (fn, args, converted_args);
4663117395Skan}
4664117395Skan
4665117395Skan/* Build and return a call to FN, using the the CONVERTED_ARGS.  ARGS
4666117395Skan   gives the original form of the arguments.  This function performs
4667117395Skan   no overload resolution, conversion, or other high-level
4668117395Skan   operations.  */
4669117395Skan
4670117395Skantree
4671117395Skanbuild_cxx_call(tree fn, tree args, tree converted_args)
4672117395Skan{
4673117395Skan  tree fndecl;
4674117395Skan
467550397Sobrien  /* Recognize certain built-in functions so we can make tree-codes
467650397Sobrien     other than CALL_EXPR.  We do this when it enables fold-const.c
467750397Sobrien     to do something useful.  */
467850397Sobrien  if (TREE_CODE (fn) == ADDR_EXPR
467950397Sobrien      && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL
468050397Sobrien      && DECL_BUILT_IN (TREE_OPERAND (fn, 0)))
468190075Sobrien    {
468290075Sobrien      tree exp;
468390075Sobrien      exp = expand_tree_builtin (TREE_OPERAND (fn, 0), args, converted_args);
468490075Sobrien      if (exp)
468590075Sobrien	return exp;
468690075Sobrien    }
468718334Speter
4688117395Skan  fn = build_call (fn, converted_args);
4689117395Skan
4690117395Skan  /* If this call might throw an exception, note that fact.  */
4691117395Skan  fndecl = get_callee_fndecl (fn);
4692117395Skan  if ((!fndecl || !TREE_NOTHROW (fndecl))
4693117395Skan      && at_function_scope_p ()
4694117395Skan      && cfun)
4695117395Skan    cp_function_chain->can_throw = 1;
4696117395Skan
4697117395Skan  /* Some built-in function calls will be evaluated at compile-time in
4698117395Skan     fold ().  */
4699117395Skan  fn = fold (fn);
4700117395Skan
470190075Sobrien  if (VOID_TYPE_P (TREE_TYPE (fn)))
470250397Sobrien    return fn;
4703117395Skan
470450397Sobrien  fn = require_complete_type (fn);
470590075Sobrien  if (fn == error_mark_node)
470690075Sobrien    return error_mark_node;
4707117395Skan
470850397Sobrien  if (IS_AGGR_TYPE (TREE_TYPE (fn)))
470950397Sobrien    fn = build_cplus_new (TREE_TYPE (fn), fn);
471050397Sobrien  return convert_from_reference (fn);
471118334Speter}
471218334Speter
4713117395Skanstatic GTY(()) tree java_iface_lookup_fn;
471490075Sobrien
471590075Sobrien/* Make an expression which yields the address of the Java interface
471690075Sobrien   method FN.  This is achieved by generating a call to libjava's
471790075Sobrien   _Jv_LookupInterfaceMethodIdx().  */
471890075Sobrien
471950397Sobrienstatic tree
4720132718Skanbuild_java_interface_fn_ref (tree fn, tree instance)
472190075Sobrien{
472290075Sobrien  tree lookup_args, lookup_fn, method, idx;
472390075Sobrien  tree klass_ref, iface, iface_ref;
472490075Sobrien  int i;
472590075Sobrien
472690075Sobrien  if (!java_iface_lookup_fn)
472790075Sobrien    {
472890075Sobrien      tree endlink = build_void_list_node ();
472990075Sobrien      tree t = tree_cons (NULL_TREE, ptr_type_node,
473090075Sobrien			  tree_cons (NULL_TREE, ptr_type_node,
473190075Sobrien				     tree_cons (NULL_TREE, java_int_type_node,
473290075Sobrien						endlink)));
473390075Sobrien      java_iface_lookup_fn
473490075Sobrien	= builtin_function ("_Jv_LookupInterfaceMethodIdx",
473590075Sobrien			    build_function_type (ptr_type_node, t),
4736117395Skan			    0, NOT_BUILT_IN, NULL, NULL_TREE);
473790075Sobrien    }
473890075Sobrien
473990075Sobrien  /* Look up the pointer to the runtime java.lang.Class object for `instance'.
4740117395Skan     This is the first entry in the vtable.  */
474190075Sobrien  klass_ref = build_vtbl_ref (build_indirect_ref (instance, 0),
474290075Sobrien			      integer_zero_node);
474390075Sobrien
4744117395Skan  /* Get the java.lang.Class pointer for the interface being called.  */
474590075Sobrien  iface = DECL_CONTEXT (fn);
4746132718Skan  iface_ref = lookup_field (iface, get_identifier ("class$"), 0, false);
474790075Sobrien  if (!iface_ref || TREE_CODE (iface_ref) != VAR_DECL
474890075Sobrien      || DECL_CONTEXT (iface_ref) != iface)
474990075Sobrien    {
475090075Sobrien      error ("could not find class$ field in java interface type `%T'",
475190075Sobrien		iface);
475290075Sobrien      return error_mark_node;
475390075Sobrien    }
475490075Sobrien  iface_ref = build1 (ADDR_EXPR, build_pointer_type (iface), iface_ref);
475590075Sobrien
4756117395Skan  /* Determine the itable index of FN.  */
475790075Sobrien  i = 1;
475890075Sobrien  for (method = TYPE_METHODS (iface); method; method = TREE_CHAIN (method))
475990075Sobrien    {
476090075Sobrien      if (!DECL_VIRTUAL_P (method))
476190075Sobrien        continue;
476290075Sobrien      if (fn == method)
476390075Sobrien        break;
476490075Sobrien      i++;
476590075Sobrien    }
476690075Sobrien  idx = build_int_2 (i, 0);
476790075Sobrien
476890075Sobrien  lookup_args = tree_cons (NULL_TREE, klass_ref,
476990075Sobrien			   tree_cons (NULL_TREE, iface_ref,
477090075Sobrien				      build_tree_list (NULL_TREE, idx)));
477190075Sobrien  lookup_fn = build1 (ADDR_EXPR,
477290075Sobrien		      build_pointer_type (TREE_TYPE (java_iface_lookup_fn)),
477390075Sobrien		      java_iface_lookup_fn);
477490075Sobrien  return build (CALL_EXPR, ptr_type_node, lookup_fn, lookup_args, NULL_TREE);
477590075Sobrien}
477690075Sobrien
477790075Sobrien/* Returns the value to use for the in-charge parameter when making a
477890075Sobrien   call to a function with the indicated NAME.  */
477990075Sobrien
478090075Sobrientree
4781132718Skanin_charge_arg_for_name (tree name)
478290075Sobrien{
478390075Sobrien  if (name == base_ctor_identifier
478490075Sobrien      || name == base_dtor_identifier)
478590075Sobrien    return integer_zero_node;
478690075Sobrien  else if (name == complete_ctor_identifier)
478790075Sobrien    return integer_one_node;
478890075Sobrien  else if (name == complete_dtor_identifier)
478990075Sobrien    return integer_two_node;
479090075Sobrien  else if (name == deleting_dtor_identifier)
479190075Sobrien    return integer_three_node;
479290075Sobrien
479390075Sobrien  /* This function should only be called with one of the names listed
479490075Sobrien     above.  */
479590075Sobrien  abort ();
479690075Sobrien  return NULL_TREE;
479790075Sobrien}
479890075Sobrien
4799117395Skan/* Build a call to a constructor, destructor, or an assignment
4800117395Skan   operator for INSTANCE, an expression with class type.  NAME
4801117395Skan   indicates the special member function to call; ARGS are the
4802117395Skan   arguments.  BINFO indicates the base of INSTANCE that is to be
4803117395Skan   passed as the `this' parameter to the member function called.
4804117395Skan
4805117395Skan   FLAGS are the LOOKUP_* flags to use when processing the call.
4806117395Skan
4807117395Skan   If NAME indicates a complete object constructor, INSTANCE may be
4808117395Skan   NULL_TREE.  In this case, the caller will call build_cplus_new to
4809117395Skan   store the newly constructed object into a VAR_DECL.  */
4810117395Skan
4811117395Skantree
4812117395Skanbuild_special_member_call (tree instance, tree name, tree args,
4813117395Skan			   tree binfo, int flags)
481450397Sobrien{
4815117395Skan  tree fns;
4816117395Skan  /* The type of the subobject to be constructed or destroyed.  */
4817117395Skan  tree class_type;
4818117395Skan
4819117395Skan  my_friendly_assert (name == complete_ctor_identifier
4820117395Skan		      || name == base_ctor_identifier
4821117395Skan		      || name == complete_dtor_identifier
4822117395Skan		      || name == base_dtor_identifier
4823117395Skan		      || name == deleting_dtor_identifier
4824117395Skan		      || name == ansi_assopname (NOP_EXPR),
4825117395Skan		      20020712);
4826117395Skan  my_friendly_assert (binfo != NULL_TREE, 20020712);
4827117395Skan
4828117395Skan  class_type = BINFO_TYPE (binfo);
4829117395Skan
4830117395Skan  /* Handle the special case where INSTANCE is NULL_TREE.  */
4831117395Skan  if (name == complete_ctor_identifier && !instance)
4832117395Skan    {
4833117395Skan      instance = build_int_2 (0, 0);
4834117395Skan      TREE_TYPE (instance) = build_pointer_type (class_type);
4835117395Skan      instance = build1 (INDIRECT_REF, class_type, instance);
4836117395Skan    }
4837119256Skan  else
4838119256Skan    {
4839119256Skan      if (name == complete_dtor_identifier
4840119256Skan	  || name == base_dtor_identifier
4841119256Skan	  || name == deleting_dtor_identifier)
4842119256Skan	my_friendly_assert (args == NULL_TREE, 20020712);
4843117395Skan
4844132718Skan      /* Convert to the base class, if necessary.  */
4845119256Skan      if (!same_type_ignoring_top_level_qualifiers_p
4846119256Skan	  (TREE_TYPE (instance), BINFO_TYPE (binfo)))
4847132718Skan	{
4848132718Skan	  if (name != ansi_assopname (NOP_EXPR))
4849132718Skan	    /* For constructors and destructors, either the base is
4850132718Skan	       non-virtual, or it is virtual but we are doing the
4851132718Skan	       conversion from a constructor or destructor for the
4852132718Skan	       complete object.  In either case, we can convert
4853132718Skan	       statically.  */
4854132718Skan	    instance = convert_to_base_statically (instance, binfo);
4855132718Skan	  else
4856132718Skan	    /* However, for assignment operators, we must convert
4857132718Skan	       dynamically if the base is virtual.  */
4858132718Skan	    instance = build_base_path (PLUS_EXPR, instance,
4859132718Skan					binfo, /*nonnull=*/1);
4860132718Skan	}
4861119256Skan    }
4862119256Skan
4863117395Skan  my_friendly_assert (instance != NULL_TREE, 20020712);
4864117395Skan
4865117395Skan  /* Resolve the name.  */
4866117395Skan  if (!complete_type_or_else (BINFO_TYPE (binfo), NULL_TREE))
4867117395Skan    return error_mark_node;
4868117395Skan
4869117395Skan  fns = lookup_fnfields (binfo, name, 1);
4870117395Skan
4871117395Skan  /* When making a call to a constructor or destructor for a subobject
4872117395Skan     that uses virtual base classes, pass down a pointer to a VTT for
4873117395Skan     the subobject.  */
4874117395Skan  if ((name == base_ctor_identifier
4875117395Skan       || name == base_dtor_identifier)
4876117395Skan      && TYPE_USES_VIRTUAL_BASECLASSES (class_type))
4877117395Skan    {
4878117395Skan      tree vtt;
4879117395Skan      tree sub_vtt;
4880117395Skan
4881117395Skan      /* If the current function is a complete object constructor
4882117395Skan	 or destructor, then we fetch the VTT directly.
4883117395Skan	 Otherwise, we look it up using the VTT we were given.  */
4884117395Skan      vtt = TREE_CHAIN (CLASSTYPE_VTABLES (current_class_type));
4885117395Skan      vtt = decay_conversion (vtt);
4886117395Skan      vtt = build (COND_EXPR, TREE_TYPE (vtt),
4887117395Skan		   build (EQ_EXPR, boolean_type_node,
4888117395Skan			  current_in_charge_parm, integer_zero_node),
4889117395Skan		   current_vtt_parm,
4890117395Skan		   vtt);
4891117395Skan      my_friendly_assert (BINFO_SUBVTT_INDEX (binfo), 20010110);
4892117395Skan      sub_vtt = build (PLUS_EXPR, TREE_TYPE (vtt), vtt,
4893117395Skan		       BINFO_SUBVTT_INDEX (binfo));
4894117395Skan
4895117395Skan      args = tree_cons (NULL_TREE, sub_vtt, args);
4896117395Skan    }
4897117395Skan
4898119256Skan  return build_new_method_call (instance, fns, args,
4899119256Skan				TYPE_BINFO (BINFO_TYPE (binfo)),
4900119256Skan				flags);
4901117395Skan}
4902117395Skan
4903132718Skan/* Return the NAME, as a C string.  The NAME indicates a function that
4904132718Skan   is a member of TYPE.  *FREE_P is set to true if the caller must
4905132718Skan   free the memory returned.
4906132718Skan
4907132718Skan   Rather than go through all of this, we should simply set the names
4908132718Skan   of constructors and destructors appropriately, and dispense with
4909132718Skan   ctor_identifier, dtor_identifier, etc.  */
4910132718Skan
4911132718Skanstatic char *
4912132718Skanname_as_c_string (tree name, tree type, bool *free_p)
4913132718Skan{
4914132718Skan  char *pretty_name;
4915132718Skan
4916132718Skan  /* Assume that we will not allocate memory.  */
4917132718Skan  *free_p = false;
4918132718Skan  /* Constructors and destructors are special.  */
4919132718Skan  if (IDENTIFIER_CTOR_OR_DTOR_P (name))
4920132718Skan    {
4921132718Skan      pretty_name
4922132718Skan	= (char *) IDENTIFIER_POINTER (constructor_name (type));
4923132718Skan      /* For a destructor, add the '~'.  */
4924132718Skan      if (name == complete_dtor_identifier
4925132718Skan	  || name == base_dtor_identifier
4926132718Skan	  || name == deleting_dtor_identifier)
4927132718Skan	{
4928132718Skan	  pretty_name = concat ("~", pretty_name, NULL);
4929132718Skan	  /* Remember that we need to free the memory allocated.  */
4930132718Skan	  *free_p = true;
4931132718Skan	}
4932132718Skan    }
4933132718Skan  else if (IDENTIFIER_TYPENAME_P (name))
4934132718Skan    {
4935132718Skan      pretty_name = concat ("operator ",
4936132718Skan			    type_as_string (TREE_TYPE (name),
4937132718Skan					    TFF_PLAIN_IDENTIFIER),
4938132718Skan			    NULL);
4939132718Skan      /* Remember that we need to free the memory allocated.  */
4940132718Skan      *free_p = true;
4941132718Skan    }
4942132718Skan  else
4943132718Skan    pretty_name = (char *) IDENTIFIER_POINTER (name);
4944132718Skan
4945132718Skan  return pretty_name;
4946132718Skan}
4947132718Skan
4948117395Skan/* Build a call to "INSTANCE.FN (ARGS)".  */
4949117395Skan
4950117395Skantree
4951117395Skanbuild_new_method_call (tree instance, tree fns, tree args,
4952117395Skan		       tree conversion_path, int flags)
4953117395Skan{
495450397Sobrien  struct z_candidate *candidates = 0, *cand;
495550397Sobrien  tree explicit_targs = NULL_TREE;
4956117395Skan  tree basetype = NULL_TREE;
4957117395Skan  tree access_binfo;
4958117395Skan  tree optype;
4959117395Skan  tree mem_args = NULL_TREE, instance_ptr;
4960132718Skan  tree name;
496190075Sobrien  tree user_args;
496290075Sobrien  tree call;
4963132718Skan  tree fn;
4964132718Skan  tree class_type;
496550397Sobrien  int template_only = 0;
4966132718Skan  bool any_viable_p;
4967132718Skan  tree orig_instance;
4968132718Skan  tree orig_fns;
4969132718Skan  tree orig_args;
497018334Speter
4971117395Skan  my_friendly_assert (instance != NULL_TREE, 20020729);
497252284Sobrien
4973132718Skan  if (error_operand_p (instance)
4974132718Skan      || error_operand_p (fns)
4975117395Skan      || args == error_mark_node)
4976117395Skan    return error_mark_node;
497718334Speter
4978132718Skan  orig_instance = instance;
4979132718Skan  orig_fns = fns;
4980132718Skan  orig_args = args;
4981132718Skan
4982132718Skan  if (processing_template_decl)
4983132718Skan    {
4984132718Skan      instance = build_non_dependent_expr (instance);
4985132718Skan      if (!BASELINK_P (fns)
4986132718Skan	  && TREE_CODE (fns) != PSEUDO_DTOR_EXPR
4987132718Skan	  && TREE_TYPE (fns) != unknown_type_node)
4988132718Skan	fns = build_non_dependent_expr (fns);
4989132718Skan      args = build_non_dependent_args (orig_args);
4990132718Skan    }
4991132718Skan
4992117395Skan  /* Process the argument list.  */
499390075Sobrien  user_args = args;
499450397Sobrien  args = resolve_args (args);
499550397Sobrien  if (args == error_mark_node)
499650397Sobrien    return error_mark_node;
499718334Speter
4998117395Skan  if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
4999117395Skan    instance = convert_from_reference (instance);
5000117395Skan  basetype = TYPE_MAIN_VARIANT (TREE_TYPE (instance));
5001117395Skan  instance_ptr = build_this (instance);
5002117395Skan
5003117395Skan  if (!BASELINK_P (fns))
500450397Sobrien    {
5005117395Skan      error ("call to non-function `%D'", fns);
5006117395Skan      return error_mark_node;
5007117395Skan    }
500818334Speter
5009117395Skan  if (!conversion_path)
5010117395Skan    conversion_path = BASELINK_BINFO (fns);
5011117395Skan  access_binfo = BASELINK_ACCESS_BINFO (fns);
5012117395Skan  optype = BASELINK_OPTYPE (fns);
5013117395Skan  fns = BASELINK_FUNCTIONS (fns);
501418334Speter
5015117395Skan  if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
5016117395Skan    {
5017117395Skan      explicit_targs = TREE_OPERAND (fns, 1);
5018117395Skan      fns = TREE_OPERAND (fns, 0);
5019117395Skan      template_only = 1;
502018334Speter    }
502118334Speter
5022117395Skan  my_friendly_assert (TREE_CODE (fns) == FUNCTION_DECL
5023117395Skan		      || TREE_CODE (fns) == TEMPLATE_DECL
5024117395Skan		      || TREE_CODE (fns) == OVERLOAD,
5025117395Skan		      20020712);
502650397Sobrien
5027117395Skan  /* XXX this should be handled before we get here.  */
5028117395Skan  if (! IS_AGGR_TYPE (basetype))
502918334Speter    {
5030117395Skan      if ((flags & LOOKUP_COMPLAIN) && basetype != error_mark_node)
5031117395Skan	error ("request for member `%D' in `%E', which is of non-aggregate type `%T'",
5032117395Skan	       fns, instance, basetype);
503318334Speter
5034117395Skan      return error_mark_node;
503518334Speter    }
503618334Speter
5037132718Skan  fn = get_first_fn (fns);
5038132718Skan  name = DECL_NAME (fn);
503950397Sobrien
504090075Sobrien  if (IDENTIFIER_CTOR_OR_DTOR_P (name))
504190075Sobrien    {
5042117395Skan      /* Callers should explicitly indicate whether they want to construct
5043117395Skan	 the complete object or just the part without virtual bases.  */
5044117395Skan      my_friendly_assert (name != ctor_identifier, 20000408);
5045117395Skan      /* Similarly for destructors.  */
5046117395Skan      my_friendly_assert (name != dtor_identifier, 20000408);
504790075Sobrien    }
504890075Sobrien
5049132718Skan  /* It's OK to call destructors on cv-qualified objects.  Therefore,
5050132718Skan     convert the INSTANCE_PTR to the unqualified type, if necessary.  */
5051132718Skan  if (DECL_DESTRUCTOR_P (fn))
505218334Speter    {
5053132718Skan      tree type = build_pointer_type (basetype);
5054132718Skan      if (!same_type_p (type, TREE_TYPE (instance_ptr)))
5055132718Skan	instance_ptr = build_nop (type, instance_ptr);
5056132718Skan    }
5057117395Skan
5058132718Skan  class_type = (conversion_path ? BINFO_TYPE (conversion_path) : NULL_TREE);
5059132718Skan  mem_args = tree_cons (NULL_TREE, instance_ptr, args);
506050397Sobrien
5061132718Skan  for (fn = fns; fn; fn = OVL_NEXT (fn))
5062132718Skan    {
5063132718Skan      tree t = OVL_CURRENT (fn);
5064132718Skan      tree this_arglist;
506590075Sobrien
5066132718Skan      /* We can end up here for copy-init of same or base class.  */
5067132718Skan      if ((flags & LOOKUP_ONLYCONVERTING)
5068132718Skan	  && DECL_NONCONVERTING_P (t))
5069132718Skan	continue;
507050397Sobrien
5071132718Skan      if (DECL_NONSTATIC_MEMBER_FUNCTION_P (t))
5072132718Skan	this_arglist = mem_args;
5073132718Skan      else
5074132718Skan	this_arglist = args;
5075132718Skan
5076132718Skan      if (TREE_CODE (t) == TEMPLATE_DECL)
5077132718Skan	/* A member template.  */
5078132718Skan	add_template_candidate (&candidates, t,
5079132718Skan				class_type,
5080132718Skan				explicit_targs,
5081132718Skan				this_arglist, optype,
5082132718Skan				access_binfo,
5083132718Skan				conversion_path,
5084132718Skan				flags,
5085132718Skan				DEDUCE_CALL);
5086132718Skan      else if (! template_only)
5087132718Skan	add_function_candidate (&candidates, t,
5088132718Skan				class_type,
5089132718Skan				this_arglist,
5090132718Skan				access_binfo,
5091132718Skan				conversion_path,
5092132718Skan				flags);
509318334Speter    }
509418334Speter
5095132718Skan  candidates = splice_viable (candidates, pedantic, &any_viable_p);
5096132718Skan  if (!any_viable_p)
509718334Speter    {
509850397Sobrien      /* XXX will LOOKUP_SPECULATIVELY be needed when this is done?  */
509918334Speter      if (flags & LOOKUP_SPECULATIVELY)
510018334Speter	return NULL_TREE;
510190075Sobrien      if (!COMPLETE_TYPE_P (basetype))
5102117395Skan	cxx_incomplete_type_error (instance_ptr, basetype);
510352284Sobrien      else
5104132718Skan	{
5105132718Skan	  char *pretty_name;
5106132718Skan	  bool free_p;
5107132718Skan
5108132718Skan	  pretty_name = name_as_c_string (name, basetype, &free_p);
5109132718Skan	  error ("no matching function for call to `%T::%s(%A)%#V'",
5110132718Skan		 basetype, pretty_name, user_args,
5111132718Skan		 TREE_TYPE (TREE_TYPE (instance_ptr)));
5112132718Skan	  if (free_p)
5113132718Skan	    free (pretty_name);
5114132718Skan	}
511550397Sobrien      print_z_candidates (candidates);
511618334Speter      return error_mark_node;
511718334Speter    }
5118132718Skan
511950397Sobrien  cand = tourney (candidates);
512050397Sobrien  if (cand == 0)
512118334Speter    {
5122132718Skan      char *pretty_name;
5123132718Skan      bool free_p;
5124132718Skan
5125132718Skan      pretty_name = name_as_c_string (name, basetype, &free_p);
5126132718Skan      error ("call of overloaded `%s(%A)' is ambiguous", pretty_name,
5127132718Skan	     user_args);
512850397Sobrien      print_z_candidates (candidates);
5129132718Skan      if (free_p)
5130132718Skan	free (pretty_name);
513150397Sobrien      return error_mark_node;
513218334Speter    }
513318334Speter
513490075Sobrien  if (DECL_PURE_VIRTUAL_P (cand->fn)
513550397Sobrien      && instance == current_class_ref
513690075Sobrien      && (DECL_CONSTRUCTOR_P (current_function_decl)
513790075Sobrien	  || DECL_DESTRUCTOR_P (current_function_decl))
513850397Sobrien      && ! (flags & LOOKUP_NONVIRTUAL)
513990075Sobrien      && value_member (cand->fn, CLASSTYPE_PURE_VIRTUALS (basetype)))
514090075Sobrien    error ((DECL_CONSTRUCTOR_P (current_function_decl) ?
514190075Sobrien	       "abstract virtual `%#D' called from constructor"
514290075Sobrien	       : "abstract virtual `%#D' called from destructor"),
514390075Sobrien	      cand->fn);
514450397Sobrien  if (TREE_CODE (TREE_TYPE (cand->fn)) == METHOD_TYPE
514552284Sobrien      && is_dummy_object (instance_ptr))
514652284Sobrien    {
514790075Sobrien      error ("cannot call member function `%D' without object", cand->fn);
514852284Sobrien      return error_mark_node;
514952284Sobrien    }
515050397Sobrien
515150397Sobrien  if (DECL_VINDEX (cand->fn) && ! (flags & LOOKUP_NONVIRTUAL)
515290075Sobrien      && resolves_to_fixed_type_p (instance, 0))
515350397Sobrien    flags |= LOOKUP_NONVIRTUAL;
515450397Sobrien
515590075Sobrien  if (TREE_CODE (TREE_TYPE (cand->fn)) == METHOD_TYPE)
5156132718Skan    call = build_over_call (cand, flags);
515790075Sobrien  else
515890075Sobrien    {
5159132718Skan      call = build_over_call (cand, flags);
5160117395Skan      /* In an expression of the form `a->f()' where `f' turns out to
5161117395Skan	 be a static member function, `a' is none-the-less evaluated.  */
5162132718Skan      if (!is_dummy_object (instance_ptr) && TREE_SIDE_EFFECTS (instance))
516390075Sobrien	call = build (COMPOUND_EXPR, TREE_TYPE (call), instance, call);
516490075Sobrien    }
5165132718Skan
5166132718Skan  if (processing_template_decl && call != error_mark_node)
5167132718Skan    return build_min_non_dep
5168132718Skan      (CALL_EXPR, call,
5169132718Skan       build_min_nt (COMPONENT_REF, orig_instance, orig_fns),
5170132718Skan       orig_args);
517190075Sobrien  return call;
517250397Sobrien}
517350397Sobrien
5174132718Skan/* Returns true iff standard conversion sequence ICS1 is a proper
517550397Sobrien   subsequence of ICS2.  */
517650397Sobrien
5177132718Skanstatic bool
5178132718Skanis_subseq (tree ics1, tree ics2)
517950397Sobrien{
518050397Sobrien  /* We can assume that a conversion of the same code
518150397Sobrien     between the same types indicates a subsequence since we only get
518250397Sobrien     here if the types we are converting from are the same.  */
518350397Sobrien
518450397Sobrien  while (TREE_CODE (ics1) == RVALUE_CONV
518550397Sobrien	 || TREE_CODE (ics1) == LVALUE_CONV)
518650397Sobrien    ics1 = TREE_OPERAND (ics1, 0);
518750397Sobrien
518850397Sobrien  while (1)
518918334Speter    {
519050397Sobrien      while (TREE_CODE (ics2) == RVALUE_CONV
519150397Sobrien	  || TREE_CODE (ics2) == LVALUE_CONV)
519250397Sobrien	ics2 = TREE_OPERAND (ics2, 0);
519350397Sobrien
519450397Sobrien      if (TREE_CODE (ics2) == USER_CONV
519550397Sobrien	  || TREE_CODE (ics2) == AMBIG_CONV
519650397Sobrien	  || TREE_CODE (ics2) == IDENTITY_CONV)
519750397Sobrien	/* At this point, ICS1 cannot be a proper subsequence of
519850397Sobrien	   ICS2.  We can get a USER_CONV when we are comparing the
519950397Sobrien	   second standard conversion sequence of two user conversion
520050397Sobrien	   sequences.  */
5201132718Skan	return false;
520250397Sobrien
520350397Sobrien      ics2 = TREE_OPERAND (ics2, 0);
520450397Sobrien
520550397Sobrien      if (TREE_CODE (ics2) == TREE_CODE (ics1)
520652284Sobrien	  && same_type_p (TREE_TYPE (ics2), TREE_TYPE (ics1))
520752284Sobrien	  && same_type_p (TREE_TYPE (TREE_OPERAND (ics2, 0)),
520852284Sobrien			     TREE_TYPE (TREE_OPERAND (ics1, 0))))
5209132718Skan	return true;
521050397Sobrien    }
521150397Sobrien}
521250397Sobrien
5213117395Skan/* Returns nonzero iff DERIVED is derived from BASE.  The inputs may
521450397Sobrien   be any _TYPE nodes.  */
521550397Sobrien
5216132718Skanbool
5217132718Skanis_properly_derived_from (tree derived, tree base)
521850397Sobrien{
521950397Sobrien  if (!IS_AGGR_TYPE_CODE (TREE_CODE (derived))
522050397Sobrien      || !IS_AGGR_TYPE_CODE (TREE_CODE (base)))
5221132718Skan    return false;
522250397Sobrien
522350397Sobrien  /* We only allow proper derivation here.  The DERIVED_FROM_P macro
522450397Sobrien     considers every class derived from itself.  */
522590075Sobrien  return (!same_type_ignoring_top_level_qualifiers_p (derived, base)
522650397Sobrien	  && DERIVED_FROM_P (base, derived));
522750397Sobrien}
522850397Sobrien
522950397Sobrien/* We build the ICS for an implicit object parameter as a pointer
523050397Sobrien   conversion sequence.  However, such a sequence should be compared
523150397Sobrien   as if it were a reference conversion sequence.  If ICS is the
523250397Sobrien   implicit conversion sequence for an implicit object parameter,
523350397Sobrien   modify it accordingly.  */
523450397Sobrien
523550397Sobrienstatic void
5236132718Skanmaybe_handle_implicit_object (tree *ics)
523750397Sobrien{
523850397Sobrien  if (ICS_THIS_FLAG (*ics))
523950397Sobrien    {
524050397Sobrien      /* [over.match.funcs]
524150397Sobrien
524250397Sobrien	 For non-static member functions, the type of the
524350397Sobrien	 implicit object parameter is "reference to cv X"
524450397Sobrien	 where X is the class of which the function is a
524550397Sobrien	 member and cv is the cv-qualification on the member
524650397Sobrien	 function declaration.  */
524750397Sobrien      tree t = *ics;
524890075Sobrien      tree reference_type;
524990075Sobrien
525090075Sobrien      /* The `this' parameter is a pointer to a class type.  Make the
5251132718Skan	 implicit conversion talk about a reference to that same class
525290075Sobrien	 type.  */
525390075Sobrien      reference_type = TREE_TYPE (TREE_TYPE (*ics));
525490075Sobrien      reference_type = build_reference_type (reference_type);
525590075Sobrien
525650397Sobrien      if (TREE_CODE (t) == QUAL_CONV)
525750397Sobrien	t = TREE_OPERAND (t, 0);
525850397Sobrien      if (TREE_CODE (t) == PTR_CONV)
525950397Sobrien	t = TREE_OPERAND (t, 0);
526050397Sobrien      t = build1 (IDENTITY_CONV, TREE_TYPE (TREE_TYPE (t)), NULL_TREE);
526190075Sobrien      t = direct_reference_binding (reference_type, t);
526250397Sobrien      *ics = t;
526350397Sobrien    }
526450397Sobrien}
526550397Sobrien
526690075Sobrien/* If *ICS is a REF_BIND set *ICS to the remainder of the conversion,
526790075Sobrien   and return the type to which the reference refers.  Otherwise,
526890075Sobrien   leave *ICS unchanged and return NULL_TREE.  */
526950397Sobrien
527090075Sobrienstatic tree
5271132718Skanmaybe_handle_ref_bind (tree *ics)
527250397Sobrien{
527350397Sobrien  if (TREE_CODE (*ics) == REF_BIND)
527450397Sobrien    {
527550397Sobrien      tree old_ics = *ics;
527690075Sobrien      tree type = TREE_TYPE (TREE_TYPE (old_ics));
527790075Sobrien      *ics = TREE_OPERAND (old_ics, 0);
527850397Sobrien      ICS_USER_FLAG (*ics) = ICS_USER_FLAG (old_ics);
527950397Sobrien      ICS_BAD_FLAG (*ics) = ICS_BAD_FLAG (old_ics);
528090075Sobrien      return type;
528118334Speter    }
528290075Sobrien
528390075Sobrien  return NULL_TREE;
528450397Sobrien}
528518334Speter
528650397Sobrien/* Compare two implicit conversion sequences according to the rules set out in
528750397Sobrien   [over.ics.rank].  Return values:
528850397Sobrien
528950397Sobrien      1: ics1 is better than ics2
529050397Sobrien     -1: ics2 is better than ics1
529150397Sobrien      0: ics1 and ics2 are indistinguishable */
529250397Sobrien
529350397Sobrienstatic int
5294132718Skancompare_ics (tree ics1, tree ics2)
529550397Sobrien{
529650397Sobrien  tree from_type1;
529750397Sobrien  tree from_type2;
529850397Sobrien  tree to_type1;
529950397Sobrien  tree to_type2;
530050397Sobrien  tree deref_from_type1 = NULL_TREE;
530152284Sobrien  tree deref_from_type2 = NULL_TREE;
530252284Sobrien  tree deref_to_type1 = NULL_TREE;
530352284Sobrien  tree deref_to_type2 = NULL_TREE;
530490075Sobrien  int rank1, rank2;
530550397Sobrien
5306117395Skan  /* REF_BINDING is nonzero if the result of the conversion sequence
530750397Sobrien     is a reference type.   In that case TARGET_TYPE is the
530850397Sobrien     type referred to by the reference.  */
530950397Sobrien  tree target_type1;
531050397Sobrien  tree target_type2;
531150397Sobrien
531250397Sobrien  /* Handle implicit object parameters.  */
531350397Sobrien  maybe_handle_implicit_object (&ics1);
531450397Sobrien  maybe_handle_implicit_object (&ics2);
531550397Sobrien
531650397Sobrien  /* Handle reference parameters.  */
531790075Sobrien  target_type1 = maybe_handle_ref_bind (&ics1);
531890075Sobrien  target_type2 = maybe_handle_ref_bind (&ics2);
531950397Sobrien
532050397Sobrien  /* [over.ics.rank]
532150397Sobrien
532250397Sobrien     When  comparing  the  basic forms of implicit conversion sequences (as
532350397Sobrien     defined in _over.best.ics_)
532450397Sobrien
532550397Sobrien     --a standard conversion sequence (_over.ics.scs_) is a better
532650397Sobrien       conversion sequence than a user-defined conversion sequence
532750397Sobrien       or an ellipsis conversion sequence, and
532850397Sobrien
532950397Sobrien     --a user-defined conversion sequence (_over.ics.user_) is a
533050397Sobrien       better conversion sequence than an ellipsis conversion sequence
533150397Sobrien       (_over.ics.ellipsis_).  */
533290075Sobrien  rank1 = ICS_RANK (ics1);
533390075Sobrien  rank2 = ICS_RANK (ics2);
533490075Sobrien
533590075Sobrien  if (rank1 > rank2)
533650397Sobrien    return -1;
533790075Sobrien  else if (rank1 < rank2)
533850397Sobrien    return 1;
533950397Sobrien
534090075Sobrien  if (rank1 == BAD_RANK)
534150397Sobrien    {
534290075Sobrien      /* XXX Isn't this an extension? */
534350397Sobrien      /* Both ICS are bad.  We try to make a decision based on what
5344132718Skan	 would have happened if they'd been good.  */
534550397Sobrien      if (ICS_USER_FLAG (ics1) > ICS_USER_FLAG (ics2)
534650397Sobrien	  || ICS_STD_RANK (ics1) > ICS_STD_RANK (ics2))
534750397Sobrien	return -1;
534850397Sobrien      else if (ICS_USER_FLAG (ics1) < ICS_USER_FLAG (ics2)
534950397Sobrien	       || ICS_STD_RANK (ics1) < ICS_STD_RANK (ics2))
535050397Sobrien	return 1;
535150397Sobrien
535250397Sobrien      /* We couldn't make up our minds; try to figure it out below.  */
535350397Sobrien    }
535450397Sobrien
535550397Sobrien  if (ICS_ELLIPSIS_FLAG (ics1))
535650397Sobrien    /* Both conversions are ellipsis conversions.  */
535750397Sobrien    return 0;
535850397Sobrien
535950397Sobrien  /* User-defined  conversion sequence U1 is a better conversion sequence
536050397Sobrien     than another user-defined conversion sequence U2 if they contain the
536150397Sobrien     same user-defined conversion operator or constructor and if the sec-
536250397Sobrien     ond standard conversion sequence of U1 is  better  than  the  second
536350397Sobrien     standard conversion sequence of U2.  */
536450397Sobrien
536550397Sobrien  if (ICS_USER_FLAG (ics1))
536650397Sobrien    {
536750397Sobrien      tree t1, t2;
536850397Sobrien
536950397Sobrien      for (t1 = ics1; TREE_CODE (t1) != USER_CONV; t1 = TREE_OPERAND (t1, 0))
537050397Sobrien	if (TREE_CODE (t1) == AMBIG_CONV)
537150397Sobrien	  return 0;
537250397Sobrien      for (t2 = ics2; TREE_CODE (t2) != USER_CONV; t2 = TREE_OPERAND (t2, 0))
537350397Sobrien	if (TREE_CODE (t2) == AMBIG_CONV)
537450397Sobrien	  return 0;
537550397Sobrien
537650397Sobrien      if (USER_CONV_FN (t1) != USER_CONV_FN (t2))
537750397Sobrien	return 0;
537850397Sobrien
537950397Sobrien      /* We can just fall through here, after setting up
538050397Sobrien	 FROM_TYPE1 and FROM_TYPE2.  */
538150397Sobrien      from_type1 = TREE_TYPE (t1);
538250397Sobrien      from_type2 = TREE_TYPE (t2);
538350397Sobrien    }
538418334Speter  else
538518334Speter    {
538650397Sobrien      /* We're dealing with two standard conversion sequences.
538750397Sobrien
538850397Sobrien	 [over.ics.rank]
538950397Sobrien
539050397Sobrien	 Standard conversion sequence S1 is a better conversion
539150397Sobrien	 sequence than standard conversion sequence S2 if
539250397Sobrien
539350397Sobrien	 --S1 is a proper subsequence of S2 (comparing the conversion
539450397Sobrien	   sequences in the canonical form defined by _over.ics.scs_,
539550397Sobrien	   excluding any Lvalue Transformation; the identity
539650397Sobrien	   conversion sequence is considered to be a subsequence of
539750397Sobrien	   any non-identity conversion sequence */
539850397Sobrien
539950397Sobrien      from_type1 = ics1;
540050397Sobrien      while (TREE_CODE (from_type1) != IDENTITY_CONV)
540150397Sobrien	from_type1 = TREE_OPERAND (from_type1, 0);
540250397Sobrien      from_type1 = TREE_TYPE (from_type1);
540350397Sobrien
540450397Sobrien      from_type2 = ics2;
540550397Sobrien      while (TREE_CODE (from_type2) != IDENTITY_CONV)
540650397Sobrien	from_type2 = TREE_OPERAND (from_type2, 0);
540750397Sobrien      from_type2 = TREE_TYPE (from_type2);
540818334Speter    }
540918334Speter
541052284Sobrien  if (same_type_p (from_type1, from_type2))
541150397Sobrien    {
541250397Sobrien      if (is_subseq (ics1, ics2))
541350397Sobrien	return 1;
541450397Sobrien      if (is_subseq (ics2, ics1))
541550397Sobrien	return -1;
541650397Sobrien    }
541750397Sobrien  /* Otherwise, one sequence cannot be a subsequence of the other; they
541850397Sobrien     don't start with the same type.  This can happen when comparing the
541950397Sobrien     second standard conversion sequence in two user-defined conversion
542050397Sobrien     sequences.  */
542118334Speter
542250397Sobrien  /* [over.ics.rank]
542318334Speter
542450397Sobrien     Or, if not that,
542518334Speter
542650397Sobrien     --the rank of S1 is better than the rank of S2 (by the rules
542750397Sobrien       defined below):
542850397Sobrien
542950397Sobrien    Standard conversion sequences are ordered by their ranks: an Exact
543050397Sobrien    Match is a better conversion than a Promotion, which is a better
543150397Sobrien    conversion than a Conversion.
543250397Sobrien
543350397Sobrien    Two conversion sequences with the same rank are indistinguishable
543450397Sobrien    unless one of the following rules applies:
543550397Sobrien
543650397Sobrien    --A conversion that is not a conversion of a pointer, or pointer
543750397Sobrien      to member, to bool is better than another conversion that is such
543850397Sobrien      a conversion.
543950397Sobrien
544050397Sobrien    The ICS_STD_RANK automatically handles the pointer-to-bool rule,
544150397Sobrien    so that we do not have to check it explicitly.  */
544250397Sobrien  if (ICS_STD_RANK (ics1) < ICS_STD_RANK (ics2))
544350397Sobrien    return 1;
544450397Sobrien  else if (ICS_STD_RANK (ics2) < ICS_STD_RANK (ics1))
544550397Sobrien    return -1;
544650397Sobrien
544750397Sobrien  to_type1 = TREE_TYPE (ics1);
544850397Sobrien  to_type2 = TREE_TYPE (ics2);
544950397Sobrien
545050397Sobrien  if (TYPE_PTR_P (from_type1)
545150397Sobrien      && TYPE_PTR_P (from_type2)
545250397Sobrien      && TYPE_PTR_P (to_type1)
545350397Sobrien      && TYPE_PTR_P (to_type2))
545418334Speter    {
545550397Sobrien      deref_from_type1 = TREE_TYPE (from_type1);
545650397Sobrien      deref_from_type2 = TREE_TYPE (from_type2);
545750397Sobrien      deref_to_type1 = TREE_TYPE (to_type1);
545850397Sobrien      deref_to_type2 = TREE_TYPE (to_type2);
545950397Sobrien    }
546050397Sobrien  /* The rules for pointers to members A::* are just like the rules
546150397Sobrien     for pointers A*, except opposite: if B is derived from A then
546250397Sobrien     A::* converts to B::*, not vice versa.  For that reason, we
546350397Sobrien     switch the from_ and to_ variables here.  */
5464132718Skan  else if ((TYPE_PTRMEM_P (from_type1) && TYPE_PTRMEM_P (from_type2)
5465132718Skan	    && TYPE_PTRMEM_P (to_type1) && TYPE_PTRMEM_P (to_type2))
5466132718Skan	   || (TYPE_PTRMEMFUNC_P (from_type1)
5467132718Skan	       && TYPE_PTRMEMFUNC_P (from_type2)
5468132718Skan	       && TYPE_PTRMEMFUNC_P (to_type1)
5469132718Skan	       && TYPE_PTRMEMFUNC_P (to_type2)))
547050397Sobrien    {
5471132718Skan      deref_to_type1 = TYPE_PTRMEM_CLASS_TYPE (from_type1);
5472132718Skan      deref_to_type2 = TYPE_PTRMEM_CLASS_TYPE (from_type2);
5473132718Skan      deref_from_type1 = TYPE_PTRMEM_CLASS_TYPE (to_type1);
5474132718Skan      deref_from_type2 = TYPE_PTRMEM_CLASS_TYPE (to_type2);
547550397Sobrien    }
547650397Sobrien
547750397Sobrien  if (deref_from_type1 != NULL_TREE
547850397Sobrien      && IS_AGGR_TYPE_CODE (TREE_CODE (deref_from_type1))
547950397Sobrien      && IS_AGGR_TYPE_CODE (TREE_CODE (deref_from_type2)))
548050397Sobrien    {
548150397Sobrien      /* This was one of the pointer or pointer-like conversions.
548250397Sobrien
548350397Sobrien	 [over.ics.rank]
548450397Sobrien
548550397Sobrien	 --If class B is derived directly or indirectly from class A,
548650397Sobrien	   conversion of B* to A* is better than conversion of B* to
548750397Sobrien	   void*, and conversion of A* to void* is better than
548850397Sobrien	   conversion of B* to void*.  */
548950397Sobrien      if (TREE_CODE (deref_to_type1) == VOID_TYPE
549050397Sobrien	  && TREE_CODE (deref_to_type2) == VOID_TYPE)
549118334Speter	{
549250397Sobrien	  if (is_properly_derived_from (deref_from_type1,
549350397Sobrien					deref_from_type2))
549450397Sobrien	    return -1;
549550397Sobrien	  else if (is_properly_derived_from (deref_from_type2,
549650397Sobrien					     deref_from_type1))
549750397Sobrien	    return 1;
549850397Sobrien	}
549950397Sobrien      else if (TREE_CODE (deref_to_type1) == VOID_TYPE
550050397Sobrien	       || TREE_CODE (deref_to_type2) == VOID_TYPE)
550150397Sobrien	{
550252284Sobrien	  if (same_type_p (deref_from_type1, deref_from_type2))
550318334Speter	    {
550450397Sobrien	      if (TREE_CODE (deref_to_type2) == VOID_TYPE)
550550397Sobrien		{
550650397Sobrien		  if (is_properly_derived_from (deref_from_type1,
550750397Sobrien						deref_to_type1))
550850397Sobrien		    return 1;
550950397Sobrien		}
551050397Sobrien	      /* We know that DEREF_TO_TYPE1 is `void' here.  */
551150397Sobrien	      else if (is_properly_derived_from (deref_from_type1,
551250397Sobrien						 deref_to_type2))
551350397Sobrien		return -1;
551418334Speter	    }
551518334Speter	}
551650397Sobrien      else if (IS_AGGR_TYPE_CODE (TREE_CODE (deref_to_type1))
551750397Sobrien	       && IS_AGGR_TYPE_CODE (TREE_CODE (deref_to_type2)))
551818334Speter	{
551950397Sobrien	  /* [over.ics.rank]
552018334Speter
552150397Sobrien	     --If class B is derived directly or indirectly from class A
552250397Sobrien	       and class C is derived directly or indirectly from B,
552350397Sobrien
552450397Sobrien	     --conversion of C* to B* is better than conversion of C* to
552550397Sobrien	       A*,
552650397Sobrien
552750397Sobrien	     --conversion of B* to A* is better than conversion of C* to
552850397Sobrien	       A*  */
552952284Sobrien	  if (same_type_p (deref_from_type1, deref_from_type2))
553050397Sobrien	    {
553150397Sobrien	      if (is_properly_derived_from (deref_to_type1,
553250397Sobrien					    deref_to_type2))
553350397Sobrien		return 1;
553450397Sobrien	      else if (is_properly_derived_from (deref_to_type2,
553550397Sobrien						 deref_to_type1))
553650397Sobrien		return -1;
553750397Sobrien	    }
553852284Sobrien	  else if (same_type_p (deref_to_type1, deref_to_type2))
553950397Sobrien	    {
554050397Sobrien	      if (is_properly_derived_from (deref_from_type2,
554150397Sobrien					    deref_from_type1))
554250397Sobrien		return 1;
554350397Sobrien	      else if (is_properly_derived_from (deref_from_type1,
554450397Sobrien						 deref_from_type2))
554550397Sobrien		return -1;
554650397Sobrien	    }
554718334Speter	}
554850397Sobrien    }
554990075Sobrien  else if (CLASS_TYPE_P (non_reference (from_type1))
555052284Sobrien	   && same_type_p (from_type1, from_type2))
555150397Sobrien    {
555290075Sobrien      tree from = non_reference (from_type1);
555390075Sobrien
555450397Sobrien      /* [over.ics.rank]
555550397Sobrien
555650397Sobrien	 --binding of an expression of type C to a reference of type
555750397Sobrien	   B& is better than binding an expression of type C to a
555850397Sobrien	   reference of type A&
555918334Speter
556050397Sobrien	 --conversion of C to B is better than conversion of C to A,  */
556190075Sobrien      if (is_properly_derived_from (from, to_type1)
556290075Sobrien	  && is_properly_derived_from (from, to_type2))
556318334Speter	{
556450397Sobrien	  if (is_properly_derived_from (to_type1, to_type2))
556550397Sobrien	    return 1;
556650397Sobrien	  else if (is_properly_derived_from (to_type2, to_type1))
556750397Sobrien	    return -1;
556818334Speter	}
556950397Sobrien    }
557090075Sobrien  else if (CLASS_TYPE_P (non_reference (to_type1))
557152284Sobrien	   && same_type_p (to_type1, to_type2))
557250397Sobrien    {
557390075Sobrien      tree to = non_reference (to_type1);
557490075Sobrien
557550397Sobrien      /* [over.ics.rank]
557650397Sobrien
557750397Sobrien	 --binding of an expression of type B to a reference of type
557850397Sobrien	   A& is better than binding an expression of type C to a
557950397Sobrien	   reference of type A&,
558050397Sobrien
558150397Sobrien	 --onversion of B to A is better than conversion of C to A  */
558290075Sobrien      if (is_properly_derived_from (from_type1, to)
558390075Sobrien	  && is_properly_derived_from (from_type2, to))
558418334Speter	{
558550397Sobrien	  if (is_properly_derived_from (from_type2, from_type1))
558650397Sobrien	    return 1;
558750397Sobrien	  else if (is_properly_derived_from (from_type1, from_type2))
558850397Sobrien	    return -1;
558950397Sobrien	}
559050397Sobrien    }
559118334Speter
559250397Sobrien  /* [over.ics.rank]
559318334Speter
559450397Sobrien     --S1 and S2 differ only in their qualification conversion and  yield
559550397Sobrien       similar  types  T1 and T2 (_conv.qual_), respectively, and the cv-
559650397Sobrien       qualification signature of type T1 is a proper subset of  the  cv-
559750397Sobrien       qualification signature of type T2  */
559850397Sobrien  if (TREE_CODE (ics1) == QUAL_CONV
559950397Sobrien      && TREE_CODE (ics2) == QUAL_CONV
560052284Sobrien      && same_type_p (from_type1, from_type2))
560150397Sobrien    return comp_cv_qual_signature (to_type1, to_type2);
560218334Speter
560350397Sobrien  /* [over.ics.rank]
560450397Sobrien
560550397Sobrien     --S1 and S2 are reference bindings (_dcl.init.ref_), and the
560650397Sobrien     types to which the references refer are the same type except for
560750397Sobrien     top-level cv-qualifiers, and the type to which the reference
560850397Sobrien     initialized by S2 refers is more cv-qualified than the type to
560950397Sobrien     which the reference initialized by S1 refers */
561050397Sobrien
561190075Sobrien  if (target_type1 && target_type2
561290075Sobrien      && same_type_ignoring_top_level_qualifiers_p (to_type1, to_type2))
561350397Sobrien    return comp_cv_qualification (target_type2, target_type1);
561418334Speter
561550397Sobrien  /* Neither conversion sequence is better than the other.  */
561650397Sobrien  return 0;
561750397Sobrien}
561818334Speter
561950397Sobrien/* The source type for this standard conversion sequence.  */
562018334Speter
562150397Sobrienstatic tree
5622132718Skansource_type (tree t)
562350397Sobrien{
562450397Sobrien  for (;; t = TREE_OPERAND (t, 0))
562550397Sobrien    {
562650397Sobrien      if (TREE_CODE (t) == USER_CONV
562750397Sobrien	  || TREE_CODE (t) == AMBIG_CONV
562850397Sobrien	  || TREE_CODE (t) == IDENTITY_CONV)
562950397Sobrien	return TREE_TYPE (t);
563050397Sobrien    }
563190075Sobrien  abort ();
563250397Sobrien}
563350397Sobrien
563450397Sobrien/* Note a warning about preferring WINNER to LOSER.  We do this by storing
563550397Sobrien   a pointer to LOSER and re-running joust to produce the warning if WINNER
563650397Sobrien   is actually used.  */
563750397Sobrien
563850397Sobrienstatic void
5639132718Skanadd_warning (struct z_candidate *winner, struct z_candidate *loser)
564050397Sobrien{
564190075Sobrien  winner->warnings = tree_cons (NULL_TREE,
5642117395Skan				build_zc_wrapper (loser),
564390075Sobrien				winner->warnings);
564450397Sobrien}
564550397Sobrien
564650397Sobrien/* Compare two candidates for overloading as described in
564750397Sobrien   [over.match.best].  Return values:
564850397Sobrien
564950397Sobrien      1: cand1 is better than cand2
565050397Sobrien     -1: cand2 is better than cand1
565150397Sobrien      0: cand1 and cand2 are indistinguishable */
565250397Sobrien
565350397Sobrienstatic int
5654132718Skanjoust (struct z_candidate *cand1, struct z_candidate *cand2, bool warn)
565550397Sobrien{
565650397Sobrien  int winner = 0;
565750397Sobrien  int i, off1 = 0, off2 = 0, len;
565850397Sobrien
565950397Sobrien  /* Candidates that involve bad conversions are always worse than those
566050397Sobrien     that don't.  */
566150397Sobrien  if (cand1->viable > cand2->viable)
566250397Sobrien    return 1;
566350397Sobrien  if (cand1->viable < cand2->viable)
566450397Sobrien    return -1;
566550397Sobrien
566652284Sobrien  /* If we have two pseudo-candidates for conversions to the same type,
566790075Sobrien     or two candidates for the same function, arbitrarily pick one.  */
566890075Sobrien  if (cand1->fn == cand2->fn
566990075Sobrien      && (TYPE_P (cand1->fn) || DECL_P (cand1->fn)))
567052284Sobrien    return 1;
567152284Sobrien
567250397Sobrien  /* a viable function F1
567350397Sobrien     is defined to be a better function than another viable function F2  if
567450397Sobrien     for  all arguments i, ICSi(F1) is not a worse conversion sequence than
567550397Sobrien     ICSi(F2), and then */
567650397Sobrien
567750397Sobrien  /* for some argument j, ICSj(F1) is a better conversion  sequence  than
567850397Sobrien     ICSj(F2) */
567950397Sobrien
568090075Sobrien  /* For comparing static and non-static member functions, we ignore
568190075Sobrien     the implicit object parameter of the non-static function.  The
568290075Sobrien     standard says to pretend that the static function has an object
568390075Sobrien     parm, but that won't work with operator overloading.  */
568450397Sobrien  len = TREE_VEC_LENGTH (cand1->convs);
568550397Sobrien  if (len != TREE_VEC_LENGTH (cand2->convs))
568650397Sobrien    {
568750397Sobrien      if (DECL_STATIC_FUNCTION_P (cand1->fn)
568850397Sobrien	  && ! DECL_STATIC_FUNCTION_P (cand2->fn))
568950397Sobrien	off2 = 1;
569050397Sobrien      else if (! DECL_STATIC_FUNCTION_P (cand1->fn)
569150397Sobrien	       && DECL_STATIC_FUNCTION_P (cand2->fn))
569250397Sobrien	{
569350397Sobrien	  off1 = 1;
569450397Sobrien	  --len;
569518334Speter	}
569650397Sobrien      else
569790075Sobrien	abort ();
569818334Speter    }
569918334Speter
570050397Sobrien  for (i = 0; i < len; ++i)
570118334Speter    {
570250397Sobrien      tree t1 = TREE_VEC_ELT (cand1->convs, i+off1);
570350397Sobrien      tree t2 = TREE_VEC_ELT (cand2->convs, i+off2);
570450397Sobrien      int comp = compare_ics (t1, t2);
570518334Speter
570650397Sobrien      if (comp != 0)
570718334Speter	{
570850397Sobrien	  if (warn_sign_promo
570950397Sobrien	      && ICS_RANK (t1) + ICS_RANK (t2) == STD_RANK + PROMO_RANK
571050397Sobrien	      && TREE_CODE (t1) == STD_CONV
571150397Sobrien	      && TREE_CODE (t2) == STD_CONV
571250397Sobrien	      && TREE_CODE (TREE_TYPE (t1)) == INTEGER_TYPE
571350397Sobrien	      && TREE_CODE (TREE_TYPE (t2)) == INTEGER_TYPE
571450397Sobrien	      && (TYPE_PRECISION (TREE_TYPE (t1))
571550397Sobrien		  == TYPE_PRECISION (TREE_TYPE (t2)))
571650397Sobrien	      && (TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (t1, 0)))
571750397Sobrien		  || (TREE_CODE (TREE_TYPE (TREE_OPERAND (t1, 0)))
571850397Sobrien		      == ENUMERAL_TYPE)))
571918334Speter	    {
572050397Sobrien	      tree type = TREE_TYPE (TREE_OPERAND (t1, 0));
572150397Sobrien	      tree type1, type2;
572250397Sobrien	      struct z_candidate *w, *l;
572350397Sobrien	      if (comp > 0)
572450397Sobrien		type1 = TREE_TYPE (t1), type2 = TREE_TYPE (t2),
572550397Sobrien		  w = cand1, l = cand2;
572650397Sobrien	      else
572750397Sobrien		type1 = TREE_TYPE (t2), type2 = TREE_TYPE (t1),
572850397Sobrien		  w = cand2, l = cand1;
572950397Sobrien
573050397Sobrien	      if (warn)
573118334Speter		{
573290075Sobrien		  warning ("passing `%T' chooses `%T' over `%T'",
573350397Sobrien			      type, type1, type2);
573490075Sobrien		  warning ("  in call to `%D'", w->fn);
573518334Speter		}
573650397Sobrien	      else
573750397Sobrien		add_warning (w, l);
573818334Speter	    }
573950397Sobrien
574050397Sobrien	  if (winner && comp != winner)
574150397Sobrien	    {
574250397Sobrien	      winner = 0;
574350397Sobrien	      goto tweak;
574450397Sobrien	    }
574550397Sobrien	  winner = comp;
574650397Sobrien	}
574750397Sobrien    }
574850397Sobrien
574950397Sobrien  /* warn about confusing overload resolution for user-defined conversions,
575050397Sobrien     either between a constructor and a conversion op, or between two
575150397Sobrien     conversion ops.  */
5752132718Skan  if (winner && warn_conversion && cand1->second_conv
5753132718Skan      && (!DECL_CONSTRUCTOR_P (cand1->fn) || !DECL_CONSTRUCTOR_P (cand2->fn))
5754132718Skan      && winner != compare_ics (cand1->second_conv, cand2->second_conv))
575550397Sobrien    {
5756132718Skan      struct z_candidate *w, *l;
5757132718Skan      bool give_warning = false;
5758132718Skan
5759132718Skan      if (winner == 1)
5760132718Skan	w = cand1, l = cand2;
5761132718Skan      else
5762132718Skan	w = cand2, l = cand1;
5763132718Skan
5764132718Skan      /* We don't want to complain about `X::operator T1 ()'
5765132718Skan	 beating `X::operator T2 () const', when T2 is a no less
5766132718Skan	 cv-qualified version of T1.  */
5767132718Skan      if (DECL_CONTEXT (w->fn) == DECL_CONTEXT (l->fn)
5768132718Skan	  && !DECL_CONSTRUCTOR_P (w->fn) && !DECL_CONSTRUCTOR_P (l->fn))
576950397Sobrien	{
5770132718Skan	  tree t = TREE_TYPE (TREE_TYPE (l->fn));
5771132718Skan	  tree f = TREE_TYPE (TREE_TYPE (w->fn));
5772132718Skan
5773132718Skan	  if (TREE_CODE (t) == TREE_CODE (f) && POINTER_TYPE_P (t))
577450397Sobrien	    {
5775132718Skan	      t = TREE_TYPE (t);
5776132718Skan	      f = TREE_TYPE (f);
577750397Sobrien	    }
5778132718Skan	  if (!comp_ptr_ttypes (t, f))
5779132718Skan	    give_warning = true;
578018334Speter	}
5781132718Skan      else
5782132718Skan	give_warning = true;
5783132718Skan
5784132718Skan      if (!give_warning)
5785132718Skan	/*NOP*/;
5786132718Skan      else if (warn)
5787132718Skan	{
5788132718Skan	  tree source = source_type (TREE_VEC_ELT (w->convs, 0));
5789132718Skan	  if (! DECL_CONSTRUCTOR_P (w->fn))
5790132718Skan	    source = TREE_TYPE (source);
5791132718Skan	  warning ("choosing `%D' over `%D'", w->fn, l->fn);
5792132718Skan	  warning ("  for conversion from `%T' to `%T'",
5793132718Skan		   source, TREE_TYPE (w->second_conv));
5794132718Skan	  warning ("  because conversion sequence for the argument is better");
5795132718Skan	}
5796132718Skan      else
5797132718Skan	add_warning (w, l);
579850397Sobrien    }
579950397Sobrien
580050397Sobrien  if (winner)
580150397Sobrien    return winner;
580250397Sobrien
580350397Sobrien  /* or, if not that,
580490075Sobrien     F1 is a non-template function and F2 is a template function
580590075Sobrien     specialization.  */
580690075Sobrien
580750397Sobrien  if (! cand1->template && cand2->template)
580850397Sobrien    return 1;
580950397Sobrien  else if (cand1->template && ! cand2->template)
581050397Sobrien    return -1;
581190075Sobrien
581290075Sobrien  /* or, if not that,
581390075Sobrien     F1 and F2 are template functions and the function template for F1 is
581490075Sobrien     more specialized than the template for F2 according to the partial
581590075Sobrien     ordering rules.  */
581690075Sobrien
581790075Sobrien  if (cand1->template && cand2->template)
581890075Sobrien    {
581990075Sobrien      winner = more_specialized
582090075Sobrien        (TI_TEMPLATE (cand1->template), TI_TEMPLATE (cand2->template),
582190075Sobrien         DEDUCE_ORDER,
582290075Sobrien         /* Tell the deduction code how many real function arguments
582390075Sobrien	    we saw, not counting the implicit 'this' argument.  But,
582490075Sobrien	    add_function_candidate() suppresses the "this" argument
582590075Sobrien	    for constructors.
582650397Sobrien
582790075Sobrien	    [temp.func.order]: The presence of unused ellipsis and default
582890075Sobrien	    arguments has no effect on the partial ordering of function
582990075Sobrien	    templates.  */
583090075Sobrien         TREE_VEC_LENGTH (cand1->convs)
583190075Sobrien	 - (DECL_NONSTATIC_MEMBER_FUNCTION_P (cand1->fn)
583290075Sobrien	    - DECL_CONSTRUCTOR_P (cand1->fn)));
583390075Sobrien      if (winner)
583490075Sobrien        return winner;
583590075Sobrien    }
583690075Sobrien
583750397Sobrien  /* or, if not that,
583850397Sobrien     the  context  is  an  initialization by user-defined conversion (see
583950397Sobrien     _dcl.init_  and  _over.match.user_)  and  the  standard   conversion
584050397Sobrien     sequence  from  the return type of F1 to the destination type (i.e.,
584150397Sobrien     the type of the entity being initialized)  is  a  better  conversion
584250397Sobrien     sequence  than the standard conversion sequence from the return type
584350397Sobrien     of F2 to the destination type.  */
584450397Sobrien
584590075Sobrien  if (cand1->second_conv)
584690075Sobrien    {
584790075Sobrien      winner = compare_ics (cand1->second_conv, cand2->second_conv);
584890075Sobrien      if (winner)
584990075Sobrien        return winner;
585090075Sobrien    }
585190075Sobrien
585290075Sobrien  /* Check whether we can discard a builtin candidate, either because we
585390075Sobrien     have two identical ones or matching builtin and non-builtin candidates.
585450397Sobrien
585590075Sobrien     (Pedantically in the latter case the builtin which matched the user
585690075Sobrien     function should not be added to the overload set, but we spot it here.
585790075Sobrien
585890075Sobrien     [over.match.oper]
585990075Sobrien     ... the builtin candidates include ...
586090075Sobrien     - do not have the same parameter type list as any non-template
586190075Sobrien       non-member candidate.  */
586290075Sobrien
586390075Sobrien  if (TREE_CODE (cand1->fn) == IDENTIFIER_NODE
586490075Sobrien      || TREE_CODE (cand2->fn) == IDENTIFIER_NODE)
586550397Sobrien    {
586650397Sobrien      for (i = 0; i < len; ++i)
586752284Sobrien	if (!same_type_p (TREE_TYPE (TREE_VEC_ELT (cand1->convs, i)),
586852284Sobrien			  TREE_TYPE (TREE_VEC_ELT (cand2->convs, i))))
586950397Sobrien	  break;
587050397Sobrien      if (i == TREE_VEC_LENGTH (cand1->convs))
587150397Sobrien	{
587290075Sobrien	  if (cand1->fn == cand2->fn)
587390075Sobrien	    /* Two built-in candidates; arbitrarily pick one.  */
587490075Sobrien	    return 1;
587590075Sobrien	  else if (TREE_CODE (cand1->fn) == IDENTIFIER_NODE)
587690075Sobrien	    /* cand1 is built-in; prefer cand2.  */
587790075Sobrien	    return -1;
587890075Sobrien	  else
587990075Sobrien	    /* cand2 is built-in; prefer cand1.  */
588090075Sobrien	    return 1;
588150397Sobrien	}
588250397Sobrien    }
588350397Sobrien
588490075Sobrien  /* If the two functions are the same (this can happen with declarations
588590075Sobrien     in multiple scopes and arg-dependent lookup), arbitrarily choose one.  */
588690075Sobrien  if (DECL_P (cand1->fn) && DECL_P (cand2->fn)
588790075Sobrien      && equal_functions (cand1->fn, cand2->fn))
588890075Sobrien    return 1;
5889132718Skan
589050397Sobrientweak:
589150397Sobrien
589250397Sobrien  /* Extension: If the worst conversion for one candidate is worse than the
589350397Sobrien     worst conversion for the other, take the first.  */
589490075Sobrien  if (!pedantic)
589550397Sobrien    {
589650397Sobrien      int rank1 = IDENTITY_RANK, rank2 = IDENTITY_RANK;
589790075Sobrien      struct z_candidate *w = 0, *l = 0;
589850397Sobrien
589950397Sobrien      for (i = 0; i < len; ++i)
590050397Sobrien	{
590150397Sobrien	  if (ICS_RANK (TREE_VEC_ELT (cand1->convs, i+off1)) > rank1)
590250397Sobrien	    rank1 = ICS_RANK (TREE_VEC_ELT (cand1->convs, i+off1));
590350397Sobrien	  if (ICS_RANK (TREE_VEC_ELT (cand2->convs, i+off2)) > rank2)
590450397Sobrien	    rank2 = ICS_RANK (TREE_VEC_ELT (cand2->convs, i+off2));
590550397Sobrien	}
590650397Sobrien      if (rank1 < rank2)
590790075Sobrien	winner = 1, w = cand1, l = cand2;
590850397Sobrien      if (rank1 > rank2)
590990075Sobrien	winner = -1, w = cand2, l = cand1;
591090075Sobrien      if (winner)
591190075Sobrien        {
591290075Sobrien	  if (warn)
5913132718Skan	    {
5914132718Skan	      pedwarn ("\
5915132718SkanISO C++ says that these are ambiguous, even \
5916132718Skanthough the worst conversion for the first is better than \
5917132718Skanthe worst conversion for the second:");
5918132718Skan	      print_z_candidate (_("candidate 1:"), w);
5919132718Skan	      print_z_candidate (_("candidate 2:"), l);
5920132718Skan	    }
592190075Sobrien	  else
592290075Sobrien	    add_warning (w, l);
592390075Sobrien          return winner;
592490075Sobrien        }
592550397Sobrien    }
592650397Sobrien
592790075Sobrien  my_friendly_assert (!winner, 20010121);
592890075Sobrien  return 0;
592950397Sobrien}
593050397Sobrien
593150397Sobrien/* Given a list of candidates for overloading, find the best one, if any.
593250397Sobrien   This algorithm has a worst case of O(2n) (winner is last), and a best
593350397Sobrien   case of O(n/2) (totally ambiguous); much better than a sorting
593450397Sobrien   algorithm.  */
593550397Sobrien
593650397Sobrienstatic struct z_candidate *
5937132718Skantourney (struct z_candidate *candidates)
593850397Sobrien{
593950397Sobrien  struct z_candidate *champ = candidates, *challenger;
594050397Sobrien  int fate;
594150397Sobrien  int champ_compared_to_predecessor = 0;
594250397Sobrien
594350397Sobrien  /* Walk through the list once, comparing each current champ to the next
594450397Sobrien     candidate, knocking out a candidate or two with each comparison.  */
594550397Sobrien
594650397Sobrien  for (challenger = champ->next; challenger; )
594750397Sobrien    {
594850397Sobrien      fate = joust (champ, challenger, 0);
594950397Sobrien      if (fate == 1)
595050397Sobrien	challenger = challenger->next;
595118334Speter      else
595218334Speter	{
595350397Sobrien	  if (fate == 0)
595418334Speter	    {
595550397Sobrien	      champ = challenger->next;
595650397Sobrien	      if (champ == 0)
595750397Sobrien		return 0;
595850397Sobrien	      champ_compared_to_predecessor = 0;
595918334Speter	    }
596018334Speter	  else
596150397Sobrien	    {
596250397Sobrien	      champ = challenger;
596350397Sobrien	      champ_compared_to_predecessor = 1;
596450397Sobrien	    }
596550397Sobrien
596650397Sobrien	  challenger = champ->next;
596718334Speter	}
596850397Sobrien    }
596918334Speter
597050397Sobrien  /* Make sure the champ is better than all the candidates it hasn't yet
597150397Sobrien     been compared to.  */
597218334Speter
597350397Sobrien  for (challenger = candidates;
597450397Sobrien       challenger != champ
597550397Sobrien	 && !(champ_compared_to_predecessor && challenger->next == champ);
597650397Sobrien       challenger = challenger->next)
597750397Sobrien    {
597850397Sobrien      fate = joust (champ, challenger, 0);
597950397Sobrien      if (fate != 1)
598050397Sobrien	return 0;
598118334Speter    }
598218334Speter
598350397Sobrien  return champ;
598418334Speter}
598518334Speter
5986117395Skan/* Returns nonzero if things of type FROM can be converted to TO.  */
598790075Sobrien
5988132718Skanbool
5989132718Skancan_convert (tree to, tree from)
599018334Speter{
599190075Sobrien  return can_convert_arg (to, from, NULL_TREE);
599218334Speter}
599318334Speter
5994117395Skan/* Returns nonzero if ARG (of type FROM) can be converted to TO.  */
599590075Sobrien
5996132718Skanbool
5997132718Skancan_convert_arg (tree to, tree from, tree arg)
599818334Speter{
599950397Sobrien  tree t = implicit_conversion (to, from, arg, LOOKUP_NORMAL);
600050397Sobrien  return (t && ! ICS_BAD_FLAG (t));
600118334Speter}
600290075Sobrien
600390075Sobrien/* Like can_convert_arg, but allows dubious conversions as well.  */
600490075Sobrien
6005132718Skanbool
6006132718Skancan_convert_arg_bad (tree to, tree from, tree arg)
600790075Sobrien{
6008132718Skan  return implicit_conversion (to, from, arg, LOOKUP_NORMAL) != 0;
600990075Sobrien}
601090075Sobrien
601190075Sobrien/* Convert EXPR to TYPE.  Return the converted expression.
601290075Sobrien
601390075Sobrien   Note that we allow bad conversions here because by the time we get to
601490075Sobrien   this point we are committed to doing the conversion.  If we end up
601590075Sobrien   doing a bad conversion, convert_like will complain.  */
601690075Sobrien
601790075Sobrientree
6018132718Skanperform_implicit_conversion (tree type, tree expr)
601990075Sobrien{
602090075Sobrien  tree conv;
602190075Sobrien
6022132718Skan  if (error_operand_p (expr))
602390075Sobrien    return error_mark_node;
602490075Sobrien  conv = implicit_conversion (type, TREE_TYPE (expr), expr,
602590075Sobrien			      LOOKUP_NORMAL);
602690075Sobrien  if (!conv)
602790075Sobrien    {
602890075Sobrien      error ("could not convert `%E' to `%T'", expr, type);
602990075Sobrien      return error_mark_node;
603090075Sobrien    }
603190075Sobrien
603290075Sobrien  return convert_like (conv, expr);
603390075Sobrien}
603490075Sobrien
6035117395Skan/* Convert EXPR to TYPE (as a direct-initialization) if that is
6036117395Skan   permitted.  If the conversion is valid, the converted expression is
6037122180Skan   returned.  Otherwise, NULL_TREE is returned, except in the case
6038122180Skan   that TYPE is a class type; in that case, an error is issued.  */
6039117395Skan
6040117395Skantree
6041117395Skanperform_direct_initialization_if_possible (tree type, tree expr)
6042117395Skan{
6043117395Skan  tree conv;
6044117395Skan
6045117395Skan  if (type == error_mark_node || error_operand_p (expr))
6046117395Skan    return error_mark_node;
6047122180Skan  /* [dcl.init]
6048122180Skan
6049122180Skan     If the destination type is a (possibly cv-qualified) class type:
6050122180Skan
6051122180Skan     -- If the initialization is direct-initialization ...,
6052122180Skan     constructors are considered. ... If no constructor applies, or
6053122180Skan     the overload resolution is ambiguous, the initialization is
6054122180Skan     ill-formed.  */
6055122180Skan  if (CLASS_TYPE_P (type))
6056122180Skan    {
6057122180Skan      expr = build_special_member_call (NULL_TREE, complete_ctor_identifier,
6058122180Skan					build_tree_list (NULL_TREE, expr),
6059122180Skan					TYPE_BINFO (type),
6060122180Skan					LOOKUP_NORMAL);
6061122180Skan      return build_cplus_new (type, expr);
6062122180Skan    }
6063117395Skan  conv = implicit_conversion (type, TREE_TYPE (expr), expr,
6064117395Skan			      LOOKUP_NORMAL);
6065117395Skan  if (!conv || ICS_BAD_FLAG (conv))
6066117395Skan    return NULL_TREE;
6067117395Skan  return convert_like_real (conv, expr, NULL_TREE, 0, 0,
6068117395Skan			    /*issue_conversion_warnings=*/false);
6069117395Skan}
6070117395Skan
6071117395Skan/* DECL is a VAR_DECL whose type is a REFERENCE_TYPE.  The reference
6072117395Skan   is being bound to a temporary.  Create and return a new VAR_DECL
6073117395Skan   with the indicated TYPE; this variable will store the value to
6074117395Skan   which the reference is bound.  */
6075117395Skan
6076117395Skantree
6077117395Skanmake_temporary_var_for_ref_to_temp (tree decl, tree type)
6078117395Skan{
6079117395Skan  tree var;
6080117395Skan
6081117395Skan  /* Create the variable.  */
6082117395Skan  var = build_decl (VAR_DECL, NULL_TREE, type);
6083117395Skan  DECL_ARTIFICIAL (var) = 1;
6084117395Skan  TREE_USED (var) = 1;
6085117395Skan
6086117395Skan  /* Register the variable.  */
6087117395Skan  if (TREE_STATIC (decl))
6088117395Skan    {
6089117395Skan      /* Namespace-scope or local static; give it a mangled name.  */
6090117395Skan      tree name;
6091117395Skan
6092117395Skan      TREE_STATIC (var) = 1;
6093117395Skan      name = mangle_ref_init_variable (decl);
6094117395Skan      DECL_NAME (var) = name;
6095117395Skan      SET_DECL_ASSEMBLER_NAME (var, name);
6096117395Skan      var = pushdecl_top_level (var);
6097117395Skan    }
6098117395Skan  else
6099117395Skan    {
6100117395Skan      /* Create a new cleanup level if necessary.  */
6101117395Skan      maybe_push_cleanup_level (type);
6102117395Skan      /* Don't push unnamed temps.  Do set DECL_CONTEXT, though.  */
6103117395Skan      DECL_CONTEXT (var) = current_function_decl;
6104117395Skan    }
6105117395Skan
6106117395Skan  return var;
6107117395Skan}
6108117395Skan
6109132718Skan/* Convert EXPR to the indicated reference TYPE, in a way suitable for
6110132718Skan   initializing a variable of that TYPE.  If DECL is non-NULL, it is
6111132718Skan   the VAR_DECL being initialized with the EXPR.  (In that case, the
6112132718Skan   type of DECL will be TYPE.)  If DECL is non-NULL, then CLEANUP must
6113132718Skan   also be non-NULL, and with *CLEANUP initialized to NULL.  Upon
6114132718Skan   return, if *CLEANUP is no longer NULL, it will be a CLEANUP_STMT
6115132718Skan   that should be inserted after the returned expression is used to
6116132718Skan   initialize DECL.
611790075Sobrien
6118132718Skan   Return the converted expression.  */
6119117395Skan
612090075Sobrientree
6121132718Skaninitialize_reference (tree type, tree expr, tree decl, tree *cleanup)
612290075Sobrien{
612390075Sobrien  tree conv;
612490075Sobrien
6125117395Skan  if (type == error_mark_node || error_operand_p (expr))
6126117395Skan    return error_mark_node;
6127117395Skan
612890075Sobrien  conv = reference_binding (type, TREE_TYPE (expr), expr, LOOKUP_NORMAL);
612990075Sobrien  if (!conv || ICS_BAD_FLAG (conv))
613090075Sobrien    {
6131122180Skan      if (!(TYPE_QUALS (TREE_TYPE (type)) & TYPE_QUAL_CONST)
6132122180Skan          && !real_lvalue_p (expr))
6133122180Skan        error ("invalid initialization of non-const reference of "
6134122180Skan               "type '%T' from a temporary of type '%T'",
6135122180Skan               type, TREE_TYPE (expr));
6136122180Skan      else
6137122180Skan        error ("invalid initialization of reference of type "
6138122180Skan	       "'%T' from expression of type '%T'", type,
6139122180Skan	       TREE_TYPE (expr));
614090075Sobrien      return error_mark_node;
614190075Sobrien    }
614290075Sobrien
6143117395Skan  /* If DECL is non-NULL, then this special rule applies:
6144117395Skan
6145117395Skan       [class.temporary]
6146117395Skan
6147117395Skan       The temporary to which the reference is bound or the temporary
6148117395Skan       that is the complete object to which the reference is bound
6149117395Skan       persists for the lifetime of the reference.
6150117395Skan
6151117395Skan       The temporaries created during the evaluation of the expression
6152117395Skan       initializing the reference, except the temporary to which the
6153117395Skan       reference is bound, are destroyed at the end of the
6154117395Skan       full-expression in which they are created.
6155117395Skan
6156117395Skan     In that case, we store the converted expression into a new
6157117395Skan     VAR_DECL in a new scope.
6158117395Skan
6159117395Skan     However, we want to be careful not to create temporaries when
6160117395Skan     they are not required.  For example, given:
6161117395Skan
6162117395Skan       struct B {};
6163117395Skan       struct D : public B {};
6164117395Skan       D f();
6165117395Skan       const B& b = f();
6166117395Skan
6167117395Skan     there is no need to copy the return value from "f"; we can just
6168117395Skan     extend its lifetime.  Similarly, given:
6169117395Skan
6170117395Skan       struct S {};
6171117395Skan       struct T { operator S(); };
6172117395Skan       T t;
6173117395Skan       const S& s = t;
6174117395Skan
6175117395Skan    we can extend the lifetime of the return value of the conversion
6176117395Skan    operator.  */
6177117395Skan  my_friendly_assert (TREE_CODE (conv) == REF_BIND, 20030302);
6178117395Skan  if (decl)
6179117395Skan    {
6180117395Skan      tree var;
6181117395Skan      tree base_conv_type;
6182117395Skan
6183117395Skan      /* Skip over the REF_BIND.  */
6184117395Skan      conv = TREE_OPERAND (conv, 0);
6185117395Skan      /* If the next conversion is a BASE_CONV, skip that too -- but
6186117395Skan	 remember that the conversion was required.  */
6187117395Skan      if (TREE_CODE (conv) == BASE_CONV && !NEED_TEMPORARY_P (conv))
6188117395Skan	{
6189132718Skan	  if (CHECK_COPY_CONSTRUCTOR_P (conv))
6190132718Skan	    check_constructor_callable (TREE_TYPE (expr), expr);
6191117395Skan	  base_conv_type = TREE_TYPE (conv);
6192117395Skan	  conv = TREE_OPERAND (conv, 0);
6193117395Skan	}
6194117395Skan      else
6195117395Skan	base_conv_type = NULL_TREE;
6196117395Skan      /* Perform the remainder of the conversion.  */
6197132718Skan      expr = convert_like_real (conv, expr,
6198132718Skan				/*fn=*/NULL_TREE, /*argnum=*/0,
6199132718Skan				/*inner=*/-1,
6200132718Skan				/*issue_conversion_warnings=*/true);
6201146895Skan      if (error_operand_p (expr))
6202146895Skan	return error_mark_node;
6203132718Skan      if (!real_lvalue_p (expr))
6204117395Skan	{
6205117395Skan	  tree init;
6206117395Skan	  tree type;
6207117395Skan
6208117395Skan	  /* Create the temporary variable.  */
6209117395Skan	  type = TREE_TYPE (expr);
6210117395Skan	  var = make_temporary_var_for_ref_to_temp (decl, type);
6211117395Skan	  layout_decl (var, 0);
6212132718Skan	  /* If the rvalue is the result of a function call it will be
6213132718Skan	     a TARGET_EXPR.  If it is some other construct (such as a
6214132718Skan	     member access expression where the underlying object is
6215132718Skan	     itself the result of a function call), turn it into a
6216132718Skan	     TARGET_EXPR here.  It is important that EXPR be a
6217132718Skan	     TARGET_EXPR below since otherwise the INIT_EXPR will
6218132718Skan	     attempt to make a bitwise copy of EXPR to intialize
6219132718Skan	     VAR. */
6220132718Skan	  if (TREE_CODE (expr) != TARGET_EXPR)
6221132718Skan	    expr = get_target_expr (expr);
6222122180Skan	  /* Create the INIT_EXPR that will initialize the temporary
6223122180Skan	     variable.  */
6224122180Skan	  init = build (INIT_EXPR, type, var, expr);
6225117395Skan	  if (at_function_scope_p ())
6226117395Skan	    {
6227122180Skan	      add_decl_stmt (var);
6228122180Skan	      *cleanup = cxx_maybe_build_cleanup (var);
6229122180Skan	      if (*cleanup)
6230122180Skan		/* We must be careful to destroy the temporary only
6231122180Skan		   after its initialization has taken place.  If the
6232122180Skan		   initialization throws an exception, then the
6233122180Skan		   destructor should not be run.  We cannot simply
6234122180Skan		   transform INIT into something like:
6235122180Skan
6236122180Skan		     (INIT, ({ CLEANUP_STMT; }))
6237117395Skan
6238122180Skan		   because emit_local_var always treats the
6239122180Skan		   initializer as a full-expression.  Thus, the
6240122180Skan		   destructor would run too early; it would run at the
6241122180Skan		   end of initializing the reference variable, rather
6242122180Skan		   than at the end of the block enclosing the
6243122180Skan		   reference variable.
6244122180Skan
6245122180Skan		   The solution is to pass back a CLEANUP_STMT which
6246122180Skan		   the caller is responsible for attaching to the
6247122180Skan		   statement tree.  */
6248122180Skan		*cleanup = build_stmt (CLEANUP_STMT, var, *cleanup);
6249117395Skan	    }
6250117395Skan	  else
6251117395Skan	    {
6252117395Skan	      rest_of_decl_compilation (var, NULL, /*toplev=*/1, at_eof);
6253117395Skan	      if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
6254117395Skan		static_aggregates = tree_cons (NULL_TREE, var,
6255117395Skan					       static_aggregates);
6256117395Skan	    }
6257117395Skan	  /* Use its address to initialize the reference variable.  */
6258117395Skan	  expr = build_address (var);
6259117395Skan	  expr = build (COMPOUND_EXPR, TREE_TYPE (expr), init, expr);
6260117395Skan	}
6261117395Skan      else
6262117395Skan	/* Take the address of EXPR.  */
6263117395Skan	expr = build_unary_op (ADDR_EXPR, expr, 0);
6264117395Skan      /* If a BASE_CONV was required, perform it now.  */
6265117395Skan      if (base_conv_type)
6266117395Skan	expr = (perform_implicit_conversion
6267117395Skan		(build_pointer_type (base_conv_type), expr));
6268117395Skan      return build_nop (type, expr);
6269117395Skan    }
6270117395Skan
6271117395Skan  /* Perform the conversion.  */
627290075Sobrien  return convert_like (conv, expr);
627390075Sobrien}
6274117395Skan
6275117395Skan#include "gt-cp-call.h"
6276