118334Speter/* Functions related to invoking methods and overloaded functions.
2169689Skan   Copyright (C) 1987, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3169689Skan   1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
418334Speter   Contributed by Michael Tiemann (tiemann@cygnus.com) and
550397Sobrien   modified by Brendan Kehoe (brendan@cygnus.com).
618334Speter
7132718SkanThis file is part of GCC.
818334Speter
9132718SkanGCC is free software; you can redistribute it and/or modify
1018334Speterit under the terms of the GNU General Public License as published by
1118334Speterthe Free Software Foundation; either version 2, or (at your option)
1218334Speterany later version.
1318334Speter
14132718SkanGCC is distributed in the hope that it will be useful,
1518334Speterbut WITHOUT ANY WARRANTY; without even the implied warranty of
1618334SpeterMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1718334SpeterGNU General Public License for more details.
1818334Speter
1918334SpeterYou should have received a copy of the GNU General Public License
20132718Skanalong with GCC; see the file COPYING.  If not, write to
21169689Skanthe Free Software Foundation, 51 Franklin Street, Fifth Floor,
22169689SkanBoston, MA 02110-1301, USA.  */
2318334Speter
2418334Speter
2550397Sobrien/* High-level class interface.  */
2618334Speter
2718334Speter#include "config.h"
2850397Sobrien#include "system.h"
29132718Skan#include "coretypes.h"
30132718Skan#include "tm.h"
3118334Speter#include "tree.h"
3218334Speter#include "cp-tree.h"
3318334Speter#include "output.h"
3418334Speter#include "flags.h"
3550397Sobrien#include "rtl.h"
3650397Sobrien#include "toplev.h"
3790075Sobrien#include "expr.h"
3890075Sobrien#include "diagnostic.h"
39132718Skan#include "intl.h"
40132718Skan#include "target.h"
41132718Skan#include "convert.h"
4218334Speter
43169689Skan/* The various kinds of conversion.  */
44169689Skan
45169689Skantypedef enum conversion_kind {
46169689Skan  ck_identity,
47169689Skan  ck_lvalue,
48169689Skan  ck_qual,
49169689Skan  ck_std,
50169689Skan  ck_ptr,
51169689Skan  ck_pmem,
52169689Skan  ck_base,
53169689Skan  ck_ref_bind,
54169689Skan  ck_user,
55169689Skan  ck_ambig,
56169689Skan  ck_rvalue
57169689Skan} conversion_kind;
58169689Skan
59169689Skan/* The rank of the conversion.  Order of the enumerals matters; better
60169689Skan   conversions should come earlier in the list.  */
61169689Skan
62169689Skantypedef enum conversion_rank {
63169689Skan  cr_identity,
64169689Skan  cr_exact,
65169689Skan  cr_promotion,
66169689Skan  cr_std,
67169689Skan  cr_pbool,
68169689Skan  cr_user,
69169689Skan  cr_ellipsis,
70169689Skan  cr_bad
71169689Skan} conversion_rank;
72169689Skan
73169689Skan/* An implicit conversion sequence, in the sense of [over.best.ics].
74169689Skan   The first conversion to be performed is at the end of the chain.
75169689Skan   That conversion is always a cr_identity conversion.  */
76169689Skan
77169689Skantypedef struct conversion conversion;
78169689Skanstruct conversion {
79169689Skan  /* The kind of conversion represented by this step.  */
80169689Skan  conversion_kind kind;
81169689Skan  /* The rank of this conversion.  */
82169689Skan  conversion_rank rank;
83169689Skan  BOOL_BITFIELD user_conv_p : 1;
84169689Skan  BOOL_BITFIELD ellipsis_p : 1;
85169689Skan  BOOL_BITFIELD this_p : 1;
86169689Skan  BOOL_BITFIELD bad_p : 1;
87169689Skan  /* If KIND is ck_ref_bind ck_base_conv, true to indicate that a
88169689Skan     temporary should be created to hold the result of the
89169689Skan     conversion.  */
90169689Skan  BOOL_BITFIELD need_temporary_p : 1;
91169689Skan  /* If KIND is ck_identity or ck_base_conv, true to indicate that the
92169689Skan     copy constructor must be accessible, even though it is not being
93169689Skan     used.  */
94169689Skan  BOOL_BITFIELD check_copy_constructor_p : 1;
95169689Skan  /* If KIND is ck_ptr or ck_pmem, true to indicate that a conversion
96169689Skan     from a pointer-to-derived to pointer-to-base is being performed.  */
97169689Skan  BOOL_BITFIELD base_p : 1;
98169689Skan  /* The type of the expression resulting from the conversion.  */
99169689Skan  tree type;
100169689Skan  union {
101169689Skan    /* The next conversion in the chain.  Since the conversions are
102169689Skan       arranged from outermost to innermost, the NEXT conversion will
103169689Skan       actually be performed before this conversion.  This variant is
104169689Skan       used only when KIND is neither ck_identity nor ck_ambig.  */
105169689Skan    conversion *next;
106169689Skan    /* The expression at the beginning of the conversion chain.  This
107169689Skan       variant is used only if KIND is ck_identity or ck_ambig.  */
108169689Skan    tree expr;
109169689Skan  } u;
110169689Skan  /* The function candidate corresponding to this conversion
111169689Skan     sequence.  This field is only used if KIND is ck_user.  */
112169689Skan  struct z_candidate *cand;
113169689Skan};
114169689Skan
115169689Skan#define CONVERSION_RANK(NODE)			\
116169689Skan  ((NODE)->bad_p ? cr_bad			\
117169689Skan   : (NODE)->ellipsis_p ? cr_ellipsis		\
118169689Skan   : (NODE)->user_conv_p ? cr_user		\
119169689Skan   : (NODE)->rank)
120169689Skan
121169689Skanstatic struct obstack conversion_obstack;
122169689Skanstatic bool conversion_obstack_initialized;
123169689Skan
124132718Skanstatic struct z_candidate * tourney (struct z_candidate *);
125132718Skanstatic int equal_functions (tree, tree);
126132718Skanstatic int joust (struct z_candidate *, struct z_candidate *, bool);
127169689Skanstatic int compare_ics (conversion *, conversion *);
128132718Skanstatic tree build_over_call (struct z_candidate *, int);
129132718Skanstatic tree build_java_interface_fn_ref (tree, tree);
130117395Skan#define convert_like(CONV, EXPR)				\
131169689Skan  convert_like_real ((CONV), (EXPR), NULL_TREE, 0, 0,		\
132169689Skan		     /*issue_conversion_warnings=*/true,	\
133169689Skan		     /*c_cast_p=*/false)
134117395Skan#define convert_like_with_context(CONV, EXPR, FN, ARGNO)	\
135169689Skan  convert_like_real ((CONV), (EXPR), (FN), (ARGNO), 0,		\
136169689Skan		     /*issue_conversion_warnings=*/true,	\
137169689Skan		     /*c_cast_p=*/false)
138169689Skanstatic tree convert_like_real (conversion *, tree, tree, int, int, bool,
139169689Skan			       bool);
140132718Skanstatic void op_error (enum tree_code, enum tree_code, tree, tree,
141169689Skan		      tree, const char *);
142132718Skanstatic tree build_object_call (tree, tree);
143132718Skanstatic tree resolve_args (tree);
144132718Skanstatic struct z_candidate *build_user_type_conversion_1 (tree, tree, int);
145132718Skanstatic void print_z_candidate (const char *, struct z_candidate *);
146132718Skanstatic void print_z_candidates (struct z_candidate *);
147132718Skanstatic tree build_this (tree);
148132718Skanstatic struct z_candidate *splice_viable (struct z_candidate *, bool, bool *);
149132718Skanstatic bool any_strictly_viable (struct z_candidate *);
150132718Skanstatic struct z_candidate *add_template_candidate
151169689Skan	(struct z_candidate **, tree, tree, tree, tree, tree,
152169689Skan	 tree, tree, int, unification_kind_t);
153132718Skanstatic struct z_candidate *add_template_candidate_real
154169689Skan	(struct z_candidate **, tree, tree, tree, tree, tree,
155169689Skan	 tree, tree, int, tree, unification_kind_t);
156169689Skanstatic struct z_candidate *add_template_conv_candidate
157169689Skan	(struct z_candidate **, tree, tree, tree, tree, tree, tree);
158117395Skanstatic void add_builtin_candidates
159132718Skan	(struct z_candidate **, enum tree_code, enum tree_code,
160169689Skan	 tree, tree *, int);
161117395Skanstatic void add_builtin_candidate
162132718Skan	(struct z_candidate **, enum tree_code, enum tree_code,
163169689Skan	 tree, tree, tree, tree *, tree *, int);
164132718Skanstatic bool is_complete (tree);
165169689Skanstatic void build_builtin_candidate
166132718Skan	(struct z_candidate **, tree, tree, tree, tree *, tree *,
167169689Skan	 int);
168169689Skanstatic struct z_candidate *add_conv_candidate
169132718Skan	(struct z_candidate **, tree, tree, tree, tree, tree);
170169689Skanstatic struct z_candidate *add_function_candidate
171117395Skan	(struct z_candidate **, tree, tree, tree, tree, tree, int);
172169689Skanstatic conversion *implicit_conversion (tree, tree, tree, bool, int);
173169689Skanstatic conversion *standard_conversion (tree, tree, tree, bool, int);
174169689Skanstatic conversion *reference_binding (tree, tree, tree, bool, int);
175169689Skanstatic conversion *build_conv (conversion_kind, tree, conversion *);
176169689Skanstatic bool is_subseq (conversion *, conversion *);
177169689Skanstatic tree maybe_handle_ref_bind (conversion **);
178169689Skanstatic void maybe_handle_implicit_object (conversion **);
179169689Skanstatic struct z_candidate *add_candidate
180169689Skan	(struct z_candidate **, tree, tree, size_t,
181169689Skan	 conversion **, tree, tree, int);
182169689Skanstatic tree source_type (conversion *);
183132718Skanstatic void add_warning (struct z_candidate *, struct z_candidate *);
184132718Skanstatic bool reference_related_p (tree, tree);
185132718Skanstatic bool reference_compatible_p (tree, tree);
186169689Skanstatic conversion *convert_class_to_reference (tree, tree, tree);
187169689Skanstatic conversion *direct_reference_binding (tree, conversion *);
188132718Skanstatic bool promoted_arithmetic_type_p (tree);
189169689Skanstatic conversion *conditional_conversion (tree, tree);
190132718Skanstatic char *name_as_c_string (tree, tree, bool *);
191132718Skanstatic tree call_builtin_trap (void);
192132718Skanstatic tree prep_operand (tree);
193132718Skanstatic void add_candidates (tree, tree, tree, bool, tree, tree,
194132718Skan			    int, struct z_candidate **);
195169689Skanstatic conversion *merge_conversion_sequences (conversion *, conversion *);
196132718Skanstatic bool magic_varargs_p (tree);
197169689Skantypedef void (*diagnostic_fn_t) (const char *, ...) ATTRIBUTE_GCC_CXXDIAG(1,2);
198169689Skanstatic tree build_temp (tree, tree, int, diagnostic_fn_t *);
199132718Skanstatic void check_constructor_callable (tree, tree);
20018334Speter
201169689Skan/* Returns nonzero iff the destructor name specified in NAME matches BASETYPE.
202169689Skan   NAME can take many forms...  */
20318334Speter
204132718Skanbool
205132718Skancheck_dtor_name (tree basetype, tree name)
20650397Sobrien{
20752284Sobrien  /* Just accept something we've already complained about.  */
20852284Sobrien  if (name == error_mark_node)
209132718Skan    return true;
21052284Sobrien
21150397Sobrien  if (TREE_CODE (name) == TYPE_DECL)
21250397Sobrien    name = TREE_TYPE (name);
21390075Sobrien  else if (TYPE_P (name))
21450397Sobrien    /* OK */;
21550397Sobrien  else if (TREE_CODE (name) == IDENTIFIER_NODE)
21650397Sobrien    {
21750397Sobrien      if ((IS_AGGR_TYPE (basetype) && name == constructor_name (basetype))
21850397Sobrien	  || (TREE_CODE (basetype) == ENUMERAL_TYPE
21950397Sobrien	      && name == TYPE_IDENTIFIER (basetype)))
220169689Skan	return true;
22150397Sobrien      else
22250397Sobrien	name = get_type_value (name);
22350397Sobrien    }
224169689Skan  else
225169689Skan    {
226169689Skan      /* In the case of:
22790075Sobrien
228169689Skan	 template <class T> struct S { ~S(); };
229169689Skan	 int i;
230169689Skan	 i.~S();
231169689Skan
232169689Skan	 NAME will be a class template.  */
233169689Skan      gcc_assert (DECL_CLASS_TEMPLATE_P (name));
234169689Skan      return false;
235169689Skan    }
236169689Skan
237169689Skan  if (!name)
238132718Skan    return false;
239169689Skan  return same_type_p (TYPE_MAIN_VARIANT (basetype), TYPE_MAIN_VARIANT (name));
24050397Sobrien}
24150397Sobrien
24250397Sobrien/* We want the address of a function or method.  We avoid creating a
24350397Sobrien   pointer-to-member function.  */
24450397Sobrien
24550397Sobrientree
246132718Skanbuild_addr_func (tree function)
24718334Speter{
24850397Sobrien  tree type = TREE_TYPE (function);
24918334Speter
25050397Sobrien  /* We have to do these by hand to avoid real pointer to member
25150397Sobrien     functions.  */
25250397Sobrien  if (TREE_CODE (type) == METHOD_TYPE)
25318334Speter    {
254132718Skan      if (TREE_CODE (function) == OFFSET_REF)
255132718Skan	{
256132718Skan	  tree object = build_address (TREE_OPERAND (function, 0));
257132718Skan	  return get_member_function_from_ptrfunc (&object,
258132718Skan						   TREE_OPERAND (function, 1));
259132718Skan	}
260132718Skan      function = build_address (function);
26118334Speter    }
26250397Sobrien  else
263132718Skan    function = decay_conversion (function);
26450397Sobrien
26550397Sobrien  return function;
26618334Speter}
26718334Speter
26850397Sobrien/* Build a CALL_EXPR, we can handle FUNCTION_TYPEs, METHOD_TYPEs, or
26950397Sobrien   POINTER_TYPE to those.  Note, pointer to member function types
27050397Sobrien   (TYPE_PTRMEMFUNC_P) must be handled by our callers.  */
27150397Sobrien
27250397Sobrientree
273132718Skanbuild_call (tree function, tree parms)
27418334Speter{
27550397Sobrien  int is_constructor = 0;
27690075Sobrien  int nothrow;
27750397Sobrien  tree tmp;
27850397Sobrien  tree decl;
27990075Sobrien  tree result_type;
280117395Skan  tree fntype;
28118334Speter
28250397Sobrien  function = build_addr_func (function);
28350397Sobrien
284261188Spfg  /* APPLE LOCAL blocks 6040305 */
285261188Spfg  gcc_assert (TYPE_PTR_P (TREE_TYPE (function)) || TREE_CODE (TREE_TYPE (function)) == BLOCK_POINTER_TYPE);
286117395Skan  fntype = TREE_TYPE (TREE_TYPE (function));
287169689Skan  gcc_assert (TREE_CODE (fntype) == FUNCTION_TYPE
288169689Skan	      || TREE_CODE (fntype) == METHOD_TYPE);
289117395Skan  result_type = TREE_TYPE (fntype);
29090075Sobrien
29150397Sobrien  if (TREE_CODE (function) == ADDR_EXPR
29250397Sobrien      && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
293169689Skan    {
294169689Skan      decl = TREE_OPERAND (function, 0);
295169689Skan      if (!TREE_USED (decl))
296169689Skan	{
297169689Skan	  /* We invoke build_call directly for several library
298169689Skan	     functions.  These may have been declared normally if
299169689Skan	     we're building libgcc, so we can't just check
300169689Skan	     DECL_ARTIFICIAL.  */
301169689Skan	  gcc_assert (DECL_ARTIFICIAL (decl)
302169689Skan		      || !strncmp (IDENTIFIER_POINTER (DECL_NAME (decl)),
303169689Skan				   "__", 2));
304169689Skan	  mark_used (decl);
305169689Skan	}
306169689Skan    }
30750397Sobrien  else
30850397Sobrien    decl = NULL_TREE;
30950397Sobrien
31090075Sobrien  /* We check both the decl and the type; a function may be known not to
31190075Sobrien     throw without being declared throw().  */
31290075Sobrien  nothrow = ((decl && TREE_NOTHROW (decl))
31390075Sobrien	     || TYPE_NOTHROW_P (TREE_TYPE (TREE_TYPE (function))));
31490075Sobrien
315117395Skan  if (decl && TREE_THIS_VOLATILE (decl) && cfun)
31696263Sobrien    current_function_returns_abnormally = 1;
31796263Sobrien
31890075Sobrien  if (decl && TREE_DEPRECATED (decl))
31990075Sobrien    warn_deprecated_use (decl);
320117395Skan  require_complete_eh_spec_types (fntype, decl);
32190075Sobrien
32250397Sobrien  if (decl && DECL_CONSTRUCTOR_P (decl))
32350397Sobrien    is_constructor = 1;
32450397Sobrien
32550397Sobrien  /* Don't pass empty class objects by value.  This is useful
32650397Sobrien     for tags in STL, which are used to control overload resolution.
32750397Sobrien     We don't need to handle other cases of copying empty classes.  */
32850397Sobrien  if (! decl || ! DECL_BUILT_IN (decl))
32950397Sobrien    for (tmp = parms; tmp; tmp = TREE_CHAIN (tmp))
33050397Sobrien      if (is_empty_class (TREE_TYPE (TREE_VALUE (tmp)))
33150397Sobrien	  && ! TREE_ADDRESSABLE (TREE_TYPE (TREE_VALUE (tmp))))
33250397Sobrien	{
333169689Skan	  tree t = build0 (EMPTY_CLASS_EXPR, TREE_TYPE (TREE_VALUE (tmp)));
334169689Skan	  TREE_VALUE (tmp) = build2 (COMPOUND_EXPR, TREE_TYPE (t),
335169689Skan				     TREE_VALUE (tmp), t);
33650397Sobrien	}
33750397Sobrien
338169689Skan  function = build3 (CALL_EXPR, result_type, function, parms, NULL_TREE);
33950397Sobrien  TREE_HAS_CONSTRUCTOR (function) = is_constructor;
34090075Sobrien  TREE_NOTHROW (function) = nothrow;
341169689Skan
34250397Sobrien  return function;
34318334Speter}
34418334Speter
34518334Speter/* Build something of the form ptr->method (args)
34618334Speter   or object.method (args).  This can also build
34718334Speter   calls to constructors, and find friends.
34818334Speter
34918334Speter   Member functions always take their class variable
35018334Speter   as a pointer.
35118334Speter
35218334Speter   INSTANCE is a class instance.
35318334Speter
35418334Speter   NAME is the name of the method desired, usually an IDENTIFIER_NODE.
35518334Speter
35618334Speter   PARMS help to figure out what that NAME really refers to.
35718334Speter
35818334Speter   BASETYPE_PATH, if non-NULL, contains a chain from the type of INSTANCE
35918334Speter   down to the real instance type to use for access checking.  We need this
360132718Skan   information to get protected accesses correct.
36118334Speter
36218334Speter   FLAGS is the logical disjunction of zero or more LOOKUP_
36318334Speter   flags.  See cp-tree.h for more info.
36418334Speter
36518334Speter   If this is all OK, calls build_function_call with the resolved
36618334Speter   member function.
36718334Speter
36818334Speter   This function must also handle being called to perform
36918334Speter   initialization, promotion/coercion of arguments, and
37018334Speter   instantiation of default parameters.
37118334Speter
37218334Speter   Note that NAME may refer to an instance variable name.  If
37318334Speter   `operator()()' is defined for the type of that field, then we return
37418334Speter   that result.  */
37550397Sobrien
37650397Sobrien/* New overloading code.  */
37718334Speter
378169689Skantypedef struct z_candidate z_candidate;
379169689Skan
380169689Skantypedef struct candidate_warning candidate_warning;
381169689Skanstruct candidate_warning {
382169689Skan  z_candidate *loser;
383169689Skan  candidate_warning *next;
384169689Skan};
385169689Skan
386169689Skanstruct z_candidate {
387117395Skan  /* The FUNCTION_DECL that will be called if this candidate is
388117395Skan     selected by overload resolution.  */
38950397Sobrien  tree fn;
390132718Skan  /* The arguments to use when calling this function.  */
391132718Skan  tree args;
392132718Skan  /* The implicit conversion sequences for each of the arguments to
393132718Skan     FN.  */
394169689Skan  conversion **convs;
395169689Skan  /* The number of implicit conversion sequences.  */
396169689Skan  size_t num_convs;
397132718Skan  /* If FN is a user-defined conversion, the standard conversion
398132718Skan     sequence from the type returned by FN to the desired destination
399132718Skan     type.  */
400169689Skan  conversion *second_conv;
40150397Sobrien  int viable;
402117395Skan  /* If FN is a member function, the binfo indicating the path used to
403117395Skan     qualify the name of FN at the call site.  This path is used to
404117395Skan     determine whether or not FN is accessible if it is selected by
405117395Skan     overload resolution.  The DECL_CONTEXT of FN will always be a
406117395Skan     (possibly improper) base of this binfo.  */
407117395Skan  tree access_path;
408117395Skan  /* If FN is a non-static member function, the binfo indicating the
409117395Skan     subobject to which the `this' pointer should be converted if FN
410117395Skan     is selected by overload resolution.  The type pointed to the by
411117395Skan     the `this' pointer must correspond to the most derived class
412117395Skan     indicated by the CONVERSION_PATH.  */
413117395Skan  tree conversion_path;
414169689Skan  tree template_decl;
415169689Skan  candidate_warning *warnings;
416169689Skan  z_candidate *next;
41750397Sobrien};
41850397Sobrien
419146895Skan/* Returns true iff T is a null pointer constant in the sense of
420146895Skan   [conv.ptr].  */
421146895Skan
422132718Skanbool
423132718Skannull_ptr_cst_p (tree t)
42450397Sobrien{
42590075Sobrien  /* [conv.ptr]
42690075Sobrien
42790075Sobrien     A null pointer constant is an integral constant expression
42890075Sobrien     (_expr.const_) rvalue of integer type that evaluates to zero.  */
429169689Skan  t = integral_constant_value (t);
430169689Skan  if (t == null_node)
431132718Skan    return true;
432169689Skan  if (CP_INTEGRAL_TYPE_P (TREE_TYPE (t)) && integer_zerop (t))
433169689Skan    {
434169689Skan      STRIP_NOPS (t);
435169689Skan      if (!TREE_CONSTANT_OVERFLOW (t))
436169689Skan	return true;
437169689Skan    }
438132718Skan  return false;
43950397Sobrien}
44050397Sobrien
441117395Skan/* Returns nonzero if PARMLIST consists of only default parms and/or
442117395Skan   ellipsis.  */
44390075Sobrien
444132718Skanbool
445132718Skansufficient_parms_p (tree parmlist)
44690075Sobrien{
44790075Sobrien  for (; parmlist && parmlist != void_list_node;
44890075Sobrien       parmlist = TREE_CHAIN (parmlist))
44990075Sobrien    if (!TREE_PURPOSE (parmlist))
450132718Skan      return false;
451132718Skan  return true;
45290075Sobrien}
45390075Sobrien
454169689Skan/* Allocate N bytes of memory from the conversion obstack.  The memory
455169689Skan   is zeroed before being returned.  */
456169689Skan
457169689Skanstatic void *
458169689Skanconversion_obstack_alloc (size_t n)
45950397Sobrien{
460169689Skan  void *p;
461169689Skan  if (!conversion_obstack_initialized)
462169689Skan    {
463169689Skan      gcc_obstack_init (&conversion_obstack);
464169689Skan      conversion_obstack_initialized = true;
465169689Skan    }
466169689Skan  p = obstack_alloc (&conversion_obstack, n);
467169689Skan  memset (p, 0, n);
468169689Skan  return p;
469169689Skan}
47090075Sobrien
471169689Skan/* Dynamically allocate a conversion.  */
472169689Skan
473169689Skanstatic conversion *
474169689Skanalloc_conversion (conversion_kind kind)
475169689Skan{
476169689Skan  conversion *c;
477169689Skan  c = (conversion *) conversion_obstack_alloc (sizeof (conversion));
478169689Skan  c->kind = kind;
479169689Skan  return c;
480169689Skan}
481169689Skan
482169689Skan#ifdef ENABLE_CHECKING
483169689Skan
484169689Skan/* Make sure that all memory on the conversion obstack has been
485169689Skan   freed.  */
486169689Skan
487169689Skanvoid
488169689Skanvalidate_conversion_obstack (void)
489169689Skan{
490169689Skan  if (conversion_obstack_initialized)
491169689Skan    gcc_assert ((obstack_next_free (&conversion_obstack)
492169689Skan		 == obstack_base (&conversion_obstack)));
493169689Skan}
494169689Skan
495169689Skan#endif /* ENABLE_CHECKING */
496169689Skan
497169689Skan/* Dynamically allocate an array of N conversions.  */
498169689Skan
499169689Skanstatic conversion **
500169689Skanalloc_conversions (size_t n)
501169689Skan{
502169689Skan  return (conversion **) conversion_obstack_alloc (n * sizeof (conversion *));
503169689Skan}
504169689Skan
505169689Skanstatic conversion *
506169689Skanbuild_conv (conversion_kind code, tree type, conversion *from)
507169689Skan{
508169689Skan  conversion *t;
509169689Skan  conversion_rank rank = CONVERSION_RANK (from);
510169689Skan
51190075Sobrien  /* We can't use buildl1 here because CODE could be USER_CONV, which
51290075Sobrien     takes two arguments.  In that case, the caller is responsible for
51390075Sobrien     filling in the second argument.  */
514169689Skan  t = alloc_conversion (code);
515169689Skan  t->type = type;
516169689Skan  t->u.next = from;
51790075Sobrien
51850397Sobrien  switch (code)
51918334Speter    {
520169689Skan    case ck_ptr:
521169689Skan    case ck_pmem:
522169689Skan    case ck_base:
523169689Skan    case ck_std:
524169689Skan      if (rank < cr_std)
525169689Skan	rank = cr_std;
52650397Sobrien      break;
52750397Sobrien
528169689Skan    case ck_qual:
529169689Skan      if (rank < cr_exact)
530169689Skan	rank = cr_exact;
531169689Skan      break;
53250397Sobrien
53350397Sobrien    default:
53450397Sobrien      break;
53550397Sobrien    }
536169689Skan  t->rank = rank;
537169689Skan  t->user_conv_p = (code == ck_user || from->user_conv_p);
538169689Skan  t->bad_p = from->bad_p;
539169689Skan  t->base_p = false;
54050397Sobrien  return t;
54150397Sobrien}
54250397Sobrien
543169689Skan/* Build a representation of the identity conversion from EXPR to
544169689Skan   itself.  The TYPE should match the type of EXPR, if EXPR is non-NULL.  */
545169689Skan
546169689Skanstatic conversion *
547169689Skanbuild_identity_conv (tree type, tree expr)
548169689Skan{
549169689Skan  conversion *c;
550169689Skan
551169689Skan  c = alloc_conversion (ck_identity);
552169689Skan  c->type = type;
553169689Skan  c->u.expr = expr;
554169689Skan
555169689Skan  return c;
556169689Skan}
557169689Skan
558169689Skan/* Converting from EXPR to TYPE was ambiguous in the sense that there
559169689Skan   were multiple user-defined conversions to accomplish the job.
560169689Skan   Build a conversion that indicates that ambiguity.  */
561169689Skan
562169689Skanstatic conversion *
563169689Skanbuild_ambiguous_conv (tree type, tree expr)
564169689Skan{
565169689Skan  conversion *c;
566169689Skan
567169689Skan  c = alloc_conversion (ck_ambig);
568169689Skan  c->type = type;
569169689Skan  c->u.expr = expr;
570169689Skan
571169689Skan  return c;
572169689Skan}
573169689Skan
57490075Sobrientree
575132718Skanstrip_top_quals (tree t)
57650397Sobrien{
57750397Sobrien  if (TREE_CODE (t) == ARRAY_TYPE)
57850397Sobrien    return t;
579132718Skan  return cp_build_qualified_type (t, 0);
58050397Sobrien}
58150397Sobrien
58250397Sobrien/* Returns the standard conversion path (see [conv]) from type FROM to type
58350397Sobrien   TO, if any.  For proper handling of null pointer constants, you must
584169689Skan   also pass the expression EXPR to convert from.  If C_CAST_P is true,
585169689Skan   this conversion is coming from a C-style cast.  */
58650397Sobrien
587169689Skanstatic conversion *
588169689Skanstandard_conversion (tree to, tree from, tree expr, bool c_cast_p,
589169689Skan		     int flags)
59050397Sobrien{
59150397Sobrien  enum tree_code fcode, tcode;
592169689Skan  conversion *conv;
593132718Skan  bool fromref = false;
59450397Sobrien
595132718Skan  to = non_reference (to);
59650397Sobrien  if (TREE_CODE (from) == REFERENCE_TYPE)
59750397Sobrien    {
598132718Skan      fromref = true;
59950397Sobrien      from = TREE_TYPE (from);
60050397Sobrien    }
60150397Sobrien  to = strip_top_quals (to);
60250397Sobrien  from = strip_top_quals (from);
60350397Sobrien
60452284Sobrien  if ((TYPE_PTRFN_P (to) || TYPE_PTRMEMFUNC_P (to))
60552284Sobrien      && expr && type_unknown_p (expr))
60652284Sobrien    {
607122180Skan      expr = instantiate_type (to, expr, tf_conv);
60852284Sobrien      if (expr == error_mark_node)
609169689Skan	return NULL;
61052284Sobrien      from = TREE_TYPE (expr);
61152284Sobrien    }
61252284Sobrien
61350397Sobrien  fcode = TREE_CODE (from);
61450397Sobrien  tcode = TREE_CODE (to);
61550397Sobrien
616169689Skan  conv = build_identity_conv (from, expr);
617169689Skan  if (fcode == FUNCTION_TYPE || fcode == ARRAY_TYPE)
61850397Sobrien    {
619169689Skan      from = type_decays_to (from);
62050397Sobrien      fcode = TREE_CODE (from);
621169689Skan      conv = build_conv (ck_lvalue, from, conv);
62250397Sobrien    }
623169689Skan  else if (fromref || (expr && lvalue_p (expr)))
62450397Sobrien    {
625169689Skan      if (expr)
626169689Skan	{
627169689Skan	  tree bitfield_type;
628169689Skan	  bitfield_type = is_bitfield_expr_with_lowered_type (expr);
629169689Skan	  if (bitfield_type)
630169689Skan	    {
631169689Skan	      from = strip_top_quals (bitfield_type);
632169689Skan	      fcode = TREE_CODE (from);
633169689Skan	    }
634169689Skan	}
635169689Skan      conv = build_conv (ck_rvalue, from, conv);
63650397Sobrien    }
63750397Sobrien
638132718Skan   /* Allow conversion between `__complex__' data types.  */
63990075Sobrien  if (tcode == COMPLEX_TYPE && fcode == COMPLEX_TYPE)
64090075Sobrien    {
64190075Sobrien      /* The standard conversion sequence to convert FROM to TO is
642169689Skan	 the standard conversion sequence to perform componentwise
643169689Skan	 conversion.  */
644169689Skan      conversion *part_conv = standard_conversion
645169689Skan	(TREE_TYPE (to), TREE_TYPE (from), NULL_TREE, c_cast_p, flags);
646169689Skan
64790075Sobrien      if (part_conv)
648169689Skan	{
649169689Skan	  conv = build_conv (part_conv->kind, to, conv);
650169689Skan	  conv->rank = part_conv->rank;
651169689Skan	}
65290075Sobrien      else
653169689Skan	conv = NULL;
65490075Sobrien
65590075Sobrien      return conv;
65690075Sobrien    }
65790075Sobrien
65890075Sobrien  if (same_type_p (from, to))
65950397Sobrien    return conv;
66050397Sobrien
661261188Spfg  /* APPLE LOCAL blocks 6040305 (ck) */
662261188Spfg  if ((tcode == POINTER_TYPE || tcode == BLOCK_POINTER_TYPE || TYPE_PTR_TO_MEMBER_P (to))
66350397Sobrien      && expr && null_ptr_cst_p (expr))
664169689Skan    conv = build_conv (ck_std, to, conv);
66590075Sobrien  else if ((tcode == INTEGER_TYPE && fcode == POINTER_TYPE)
66690075Sobrien	   || (tcode == POINTER_TYPE && fcode == INTEGER_TYPE))
66790075Sobrien    {
66890075Sobrien      /* For backwards brain damage compatibility, allow interconversion of
66990075Sobrien	 pointers and integers with a pedwarn.  */
670169689Skan      conv = build_conv (ck_std, to, conv);
671169689Skan      conv->bad_p = true;
67290075Sobrien    }
673132718Skan  else if (tcode == ENUMERAL_TYPE && fcode == INTEGER_TYPE)
67490075Sobrien    {
67590075Sobrien      /* For backwards brain damage compatibility, allow interconversion of
67690075Sobrien	 enums and integers with a pedwarn.  */
677169689Skan      conv = build_conv (ck_std, to, conv);
678169689Skan      conv->bad_p = true;
67990075Sobrien    }
680132718Skan  else if ((tcode == POINTER_TYPE && fcode == POINTER_TYPE)
681132718Skan	   || (TYPE_PTRMEM_P (to) && TYPE_PTRMEM_P (from)))
68250397Sobrien    {
683132718Skan      tree to_pointee;
684132718Skan      tree from_pointee;
68550397Sobrien
686132718Skan      if (tcode == POINTER_TYPE
687132718Skan	  && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (from),
688132718Skan							TREE_TYPE (to)))
68950397Sobrien	;
690132718Skan      else if (VOID_TYPE_P (TREE_TYPE (to))
691132718Skan	       && !TYPE_PTRMEM_P (from)
692132718Skan	       && TREE_CODE (TREE_TYPE (from)) != FUNCTION_TYPE)
69318334Speter	{
69450397Sobrien	  from = build_pointer_type
695169689Skan	    (cp_build_qualified_type (void_type_node,
69690075Sobrien				      cp_type_quals (TREE_TYPE (from))));
697169689Skan	  conv = build_conv (ck_ptr, from, conv);
69818334Speter	}
699132718Skan      else if (TYPE_PTRMEM_P (from))
70018334Speter	{
701132718Skan	  tree fbase = TYPE_PTRMEM_CLASS_TYPE (from);
702132718Skan	  tree tbase = TYPE_PTRMEM_CLASS_TYPE (to);
70350397Sobrien
70496263Sobrien	  if (DERIVED_FROM_P (fbase, tbase)
70590075Sobrien	      && (same_type_ignoring_top_level_qualifiers_p
706132718Skan		  (TYPE_PTRMEM_POINTED_TO_TYPE (from),
707132718Skan		   TYPE_PTRMEM_POINTED_TO_TYPE (to))))
70850397Sobrien	    {
709169689Skan	      from = build_ptrmem_type (tbase,
710132718Skan					TYPE_PTRMEM_POINTED_TO_TYPE (from));
711169689Skan	      conv = build_conv (ck_pmem, from, conv);
71250397Sobrien	    }
713146895Skan	  else if (!same_type_p (fbase, tbase))
714146895Skan	    return NULL;
71518334Speter	}
71650397Sobrien      else if (IS_AGGR_TYPE (TREE_TYPE (from))
717122180Skan	       && IS_AGGR_TYPE (TREE_TYPE (to))
718122180Skan	       /* [conv.ptr]
719169689Skan
720169689Skan		  An rvalue of type "pointer to cv D," where D is a
721122180Skan		  class type, can be converted to an rvalue of type
722122180Skan		  "pointer to cv B," where B is a base class (clause
723122180Skan		  _class.derived_) of D.  If B is an inaccessible
724122180Skan		  (clause _class.access_) or ambiguous
725122180Skan		  (_class.member.lookup_) base class of D, a program
726169689Skan		  that necessitates this conversion is ill-formed.
727169689Skan		  Therefore, we use DERIVED_FROM_P, and do not check
728169689Skan		  access or uniqueness.  */
729169689Skan	       && DERIVED_FROM_P (TREE_TYPE (to), TREE_TYPE (from))
730169689Skan	       /* If FROM is not yet complete, then we must be parsing
731169689Skan		  the body of a class.  We know what's derived from
732169689Skan		  what, but we can't actually perform a
733169689Skan		  derived-to-base conversion.  For example, in:
734169689Skan
735169689Skan		     struct D : public B {
736169689Skan                       static const int i = sizeof((B*)(D*)0);
737169689Skan                     };
738169689Skan
739169689Skan                  the D*-to-B* conversion is a reinterpret_cast, not a
740169689Skan		  static_cast.  */
741169689Skan	       && COMPLETE_TYPE_P (TREE_TYPE (from)))
74250397Sobrien	{
743169689Skan	  from =
744122180Skan	    cp_build_qualified_type (TREE_TYPE (to),
745122180Skan				     cp_type_quals (TREE_TYPE (from)));
746122180Skan	  from = build_pointer_type (from);
747169689Skan	  conv = build_conv (ck_ptr, from, conv);
748169689Skan	  conv->base_p = true;
74950397Sobrien	}
75018334Speter
751132718Skan      if (tcode == POINTER_TYPE)
752132718Skan	{
753132718Skan	  to_pointee = TREE_TYPE (to);
754132718Skan	  from_pointee = TREE_TYPE (from);
755132718Skan	}
756132718Skan      else
757132718Skan	{
758132718Skan	  to_pointee = TYPE_PTRMEM_POINTED_TO_TYPE (to);
759132718Skan	  from_pointee = TYPE_PTRMEM_POINTED_TO_TYPE (from);
760132718Skan	}
761132718Skan
76252284Sobrien      if (same_type_p (from, to))
76350397Sobrien	/* OK */;
764169689Skan      else if (c_cast_p && comp_ptr_ttypes_const (to, from))
765169689Skan	/* In a C-style cast, we ignore CV-qualification because we
766169689Skan	   are allowed to perform a static_cast followed by a
767169689Skan	   const_cast.  */
768169689Skan	conv = build_conv (ck_qual, to, conv);
769169689Skan      else if (!c_cast_p && comp_ptr_ttypes (to_pointee, from_pointee))
770169689Skan	conv = build_conv (ck_qual, to, conv);
77152284Sobrien      else if (expr && string_conv_p (to, expr, 0))
77252284Sobrien	/* converting from string constant to char *.  */
773169689Skan	conv = build_conv (ck_qual, to, conv);
774132718Skan      else if (ptr_reasonably_similar (to_pointee, from_pointee))
77518334Speter	{
776169689Skan	  conv = build_conv (ck_ptr, to, conv);
777169689Skan	  conv->bad_p = true;
77818334Speter	}
77918334Speter      else
780169689Skan	return NULL;
78150397Sobrien
78250397Sobrien      from = to;
78350397Sobrien    }
78450397Sobrien  else if (TYPE_PTRMEMFUNC_P (to) && TYPE_PTRMEMFUNC_P (from))
78550397Sobrien    {
78650397Sobrien      tree fromfn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (from));
78750397Sobrien      tree tofn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (to));
78850397Sobrien      tree fbase = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (fromfn)));
78950397Sobrien      tree tbase = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (tofn)));
79050397Sobrien
79196263Sobrien      if (!DERIVED_FROM_P (fbase, tbase)
79290075Sobrien	  || !same_type_p (TREE_TYPE (fromfn), TREE_TYPE (tofn))
79390075Sobrien	  || !compparms (TREE_CHAIN (TYPE_ARG_TYPES (fromfn)),
79490075Sobrien			 TREE_CHAIN (TYPE_ARG_TYPES (tofn)))
79590075Sobrien	  || cp_type_quals (fbase) != cp_type_quals (tbase))
796169689Skan	return NULL;
79750397Sobrien
79890075Sobrien      from = cp_build_qualified_type (tbase, cp_type_quals (fbase));
799169689Skan      from = build_method_type_directly (from,
800132718Skan					 TREE_TYPE (fromfn),
801132718Skan					 TREE_CHAIN (TYPE_ARG_TYPES (fromfn)));
80250397Sobrien      from = build_ptrmemfunc_type (build_pointer_type (from));
803169689Skan      conv = build_conv (ck_pmem, from, conv);
804169689Skan      conv->base_p = true;
80550397Sobrien    }
80650397Sobrien  else if (tcode == BOOLEAN_TYPE)
80750397Sobrien    {
808132718Skan      /* [conv.bool]
80950397Sobrien
810169689Skan	  An rvalue of arithmetic, enumeration, pointer, or pointer to
811132718Skan	  member type can be converted to an rvalue of type bool.  */
812132718Skan      if (ARITHMETIC_TYPE_P (from)
813132718Skan	  || fcode == ENUMERAL_TYPE
814132718Skan	  || fcode == POINTER_TYPE
815261188Spfg	  /* APPLE LOCAL blocks 6040305 (cl) */
816261188Spfg	  || fcode == BLOCK_POINTER_TYPE
817132718Skan	  || TYPE_PTR_TO_MEMBER_P (from))
818132718Skan	{
819169689Skan	  conv = build_conv (ck_std, to, conv);
820132718Skan	  if (fcode == POINTER_TYPE
821132718Skan	      || TYPE_PTRMEM_P (from)
822169689Skan	      || (TYPE_PTRMEMFUNC_P (from)
823169689Skan		  && conv->rank < cr_pbool))
824169689Skan	    conv->rank = cr_pbool;
825132718Skan	  return conv;
826132718Skan	}
827169689Skan
828169689Skan      return NULL;
82950397Sobrien    }
83050397Sobrien  /* We don't check for ENUMERAL_TYPE here because there are no standard
83150397Sobrien     conversions to enum type.  */
83250397Sobrien  else if (tcode == INTEGER_TYPE || tcode == BOOLEAN_TYPE
83350397Sobrien	   || tcode == REAL_TYPE)
83450397Sobrien    {
83550397Sobrien      if (! (INTEGRAL_CODE_P (fcode) || fcode == REAL_TYPE))
836169689Skan	return NULL;
837169689Skan      conv = build_conv (ck_std, to, conv);
83850397Sobrien
83950397Sobrien      /* Give this a better rank if it's a promotion.  */
840132718Skan      if (same_type_p (to, type_promotes_to (from))
841169689Skan	  && conv->u.next->rank <= cr_promotion)
842169689Skan	conv->rank = cr_promotion;
84350397Sobrien    }
844132718Skan  else if (fcode == VECTOR_TYPE && tcode == VECTOR_TYPE
845260074Spfg	   && vector_types_convertible_p (from, to, false))
846169689Skan    return build_conv (ck_std, to, conv);
847146895Skan  else if (!(flags & LOOKUP_CONSTRUCTOR_CALLABLE)
848146895Skan	   && IS_AGGR_TYPE (to) && IS_AGGR_TYPE (from)
84990075Sobrien	   && is_properly_derived_from (from, to))
85050397Sobrien    {
851169689Skan      if (conv->kind == ck_rvalue)
852169689Skan	conv = conv->u.next;
853169689Skan      conv = build_conv (ck_base, to, conv);
85490075Sobrien      /* The derived-to-base conversion indicates the initialization
85590075Sobrien	 of a parameter with base type from an object of a derived
85690075Sobrien	 type.  A temporary object is created to hold the result of
85790075Sobrien	 the conversion.  */
858169689Skan      conv->need_temporary_p = true;
85950397Sobrien    }
86050397Sobrien  else
861169689Skan    return NULL;
86250397Sobrien
86350397Sobrien  return conv;
86450397Sobrien}
86550397Sobrien
866117395Skan/* Returns nonzero if T1 is reference-related to T2.  */
86790075Sobrien
868132718Skanstatic bool
869132718Skanreference_related_p (tree t1, tree t2)
87090075Sobrien{
87190075Sobrien  t1 = TYPE_MAIN_VARIANT (t1);
87290075Sobrien  t2 = TYPE_MAIN_VARIANT (t2);
87390075Sobrien
87490075Sobrien  /* [dcl.init.ref]
87590075Sobrien
87690075Sobrien     Given types "cv1 T1" and "cv2 T2," "cv1 T1" is reference-related
87790075Sobrien     to "cv2 T2" if T1 is the same type as T2, or T1 is a base class
87890075Sobrien     of T2.  */
87990075Sobrien  return (same_type_p (t1, t2)
88090075Sobrien	  || (CLASS_TYPE_P (t1) && CLASS_TYPE_P (t2)
88190075Sobrien	      && DERIVED_FROM_P (t1, t2)));
88290075Sobrien}
88390075Sobrien
884261188Spfg/* APPLE LOCAL begin radar 6029624 */
885261188Spfg/* Used in objective-c++, same as reference_related_p */
886261188Spfgbool
887261188Spfgobjcp_reference_related_p (tree t1, tree t2)
888261188Spfg{
889261188Spfg  return reference_related_p (t1, t2);
890261188Spfg}
891261188Spfg/* APPLE LOCAL end radar 6029624 */
892261188Spfg
893117395Skan/* Returns nonzero if T1 is reference-compatible with T2.  */
89490075Sobrien
895132718Skanstatic bool
896132718Skanreference_compatible_p (tree t1, tree t2)
89790075Sobrien{
89890075Sobrien  /* [dcl.init.ref]
89990075Sobrien
90090075Sobrien     "cv1 T1" is reference compatible with "cv2 T2" if T1 is
90190075Sobrien     reference-related to T2 and cv1 is the same cv-qualification as,
90290075Sobrien     or greater cv-qualification than, cv2.  */
90390075Sobrien  return (reference_related_p (t1, t2)
90490075Sobrien	  && at_least_as_qualified_p (t1, t2));
90590075Sobrien}
90690075Sobrien
90790075Sobrien/* Determine whether or not the EXPR (of class type S) can be
90890075Sobrien   converted to T as in [over.match.ref].  */
90990075Sobrien
910169689Skanstatic conversion *
911132718Skanconvert_class_to_reference (tree t, tree s, tree expr)
91290075Sobrien{
91390075Sobrien  tree conversions;
91490075Sobrien  tree arglist;
915169689Skan  conversion *conv;
916117395Skan  tree reference_type;
91790075Sobrien  struct z_candidate *candidates;
91890075Sobrien  struct z_candidate *cand;
919132718Skan  bool any_viable_p;
92090075Sobrien
921117395Skan  conversions = lookup_conversions (s);
922117395Skan  if (!conversions)
923169689Skan    return NULL;
924117395Skan
92590075Sobrien  /* [over.match.ref]
92690075Sobrien
92790075Sobrien     Assuming that "cv1 T" is the underlying type of the reference
92890075Sobrien     being initialized, and "cv S" is the type of the initializer
92990075Sobrien     expression, with S a class type, the candidate functions are
93090075Sobrien     selected as follows:
93190075Sobrien
93290075Sobrien     --The conversion functions of S and its base classes are
93390075Sobrien       considered.  Those that are not hidden within S and yield type
93490075Sobrien       "reference to cv2 T2", where "cv1 T" is reference-compatible
93590075Sobrien       (_dcl.init.ref_) with "cv2 T2", are candidate functions.
93690075Sobrien
93790075Sobrien     The argument list has one argument, which is the initializer
93890075Sobrien     expression.  */
93990075Sobrien
94090075Sobrien  candidates = 0;
94190075Sobrien
94290075Sobrien  /* Conceptually, we should take the address of EXPR and put it in
94390075Sobrien     the argument list.  Unfortunately, however, that can result in
94490075Sobrien     error messages, which we should not issue now because we are just
94590075Sobrien     trying to find a conversion operator.  Therefore, we use NULL,
94690075Sobrien     cast to the appropriate type.  */
947169689Skan  arglist = build_int_cst (build_pointer_type (s), 0);
94890075Sobrien  arglist = build_tree_list (NULL_TREE, arglist);
949117395Skan
950117395Skan  reference_type = build_reference_type (t);
951117395Skan
952117395Skan  while (conversions)
95390075Sobrien    {
95490075Sobrien      tree fns = TREE_VALUE (conversions);
95590075Sobrien
95690075Sobrien      for (; fns; fns = OVL_NEXT (fns))
95790075Sobrien	{
95890075Sobrien	  tree f = OVL_CURRENT (fns);
95990075Sobrien	  tree t2 = TREE_TYPE (TREE_TYPE (f));
960169689Skan
961117395Skan	  cand = NULL;
96290075Sobrien
96390075Sobrien	  /* If this is a template function, try to get an exact
964169689Skan	     match.  */
96590075Sobrien	  if (TREE_CODE (f) == TEMPLATE_DECL)
96690075Sobrien	    {
967117395Skan	      cand = add_template_candidate (&candidates,
968117395Skan					     f, s,
969117395Skan					     NULL_TREE,
970117395Skan					     arglist,
971117395Skan					     reference_type,
972117395Skan					     TYPE_BINFO (s),
973117395Skan					     TREE_PURPOSE (conversions),
974117395Skan					     LOOKUP_NORMAL,
975117395Skan					     DEDUCE_CONV);
976169689Skan
977117395Skan	      if (cand)
97890075Sobrien		{
97990075Sobrien		  /* Now, see if the conversion function really returns
98090075Sobrien		     an lvalue of the appropriate type.  From the
98190075Sobrien		     point of view of unification, simply returning an
98290075Sobrien		     rvalue of the right type is good enough.  */
983117395Skan		  f = cand->fn;
98490075Sobrien		  t2 = TREE_TYPE (TREE_TYPE (f));
98590075Sobrien		  if (TREE_CODE (t2) != REFERENCE_TYPE
98690075Sobrien		      || !reference_compatible_p (t, TREE_TYPE (t2)))
987117395Skan		    {
988117395Skan		      candidates = candidates->next;
989117395Skan		      cand = NULL;
990117395Skan		    }
99190075Sobrien		}
99290075Sobrien	    }
99390075Sobrien	  else if (TREE_CODE (t2) == REFERENCE_TYPE
99490075Sobrien		   && reference_compatible_p (t, TREE_TYPE (t2)))
995169689Skan	    cand = add_function_candidate (&candidates, f, s, arglist,
996169689Skan					   TYPE_BINFO (s),
997117395Skan					   TREE_PURPOSE (conversions),
998117395Skan					   LOOKUP_NORMAL);
999169689Skan
1000117395Skan	  if (cand)
1001132718Skan	    {
1002169689Skan	      conversion *identity_conv;
1003132718Skan	      /* Build a standard conversion sequence indicating the
1004132718Skan		 binding from the reference type returned by the
1005132718Skan		 function to the desired REFERENCE_TYPE.  */
1006169689Skan	      identity_conv
1007169689Skan		= build_identity_conv (TREE_TYPE (TREE_TYPE
1008169689Skan						  (TREE_TYPE (cand->fn))),
1009169689Skan				       NULL_TREE);
1010132718Skan	      cand->second_conv
1011169689Skan		= (direct_reference_binding
1012169689Skan		   (reference_type, identity_conv));
1013169689Skan	      cand->second_conv->bad_p |= cand->convs[0]->bad_p;
1014132718Skan	    }
101590075Sobrien	}
1016117395Skan      conversions = TREE_CHAIN (conversions);
101790075Sobrien    }
101890075Sobrien
1019132718Skan  candidates = splice_viable (candidates, pedantic, &any_viable_p);
102090075Sobrien  /* If none of the conversion functions worked out, let our caller
102190075Sobrien     know.  */
1022132718Skan  if (!any_viable_p)
1023169689Skan    return NULL;
1024132718Skan
102590075Sobrien  cand = tourney (candidates);
102690075Sobrien  if (!cand)
1027169689Skan    return NULL;
102890075Sobrien
1029132718Skan  /* Now that we know that this is the function we're going to use fix
1030132718Skan     the dummy first argument.  */
1031132718Skan  cand->args = tree_cons (NULL_TREE,
1032132718Skan			  build_this (expr),
1033132718Skan			  TREE_CHAIN (cand->args));
1034132718Skan
1035117395Skan  /* Build a user-defined conversion sequence representing the
1036117395Skan     conversion.  */
1037169689Skan  conv = build_conv (ck_user,
1038117395Skan		     TREE_TYPE (TREE_TYPE (cand->fn)),
1039169689Skan		     build_identity_conv (TREE_TYPE (expr), expr));
1040169689Skan  conv->cand = cand;
1041117395Skan
1042117395Skan  /* Merge it with the standard conversion sequence from the
1043117395Skan     conversion function's return type to the desired type.  */
1044117395Skan  cand->second_conv = merge_conversion_sequences (conv, cand->second_conv);
1045117395Skan
104690075Sobrien  if (cand->viable == -1)
1047169689Skan    conv->bad_p = true;
1048169689Skan
1049117395Skan  return cand->second_conv;
105090075Sobrien}
105190075Sobrien
105290075Sobrien/* A reference of the indicated TYPE is being bound directly to the
105390075Sobrien   expression represented by the implicit conversion sequence CONV.
105490075Sobrien   Return a conversion sequence for this binding.  */
105590075Sobrien
1056169689Skanstatic conversion *
1057169689Skandirect_reference_binding (tree type, conversion *conv)
105890075Sobrien{
1059117395Skan  tree t;
106090075Sobrien
1061169689Skan  gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
1062169689Skan  gcc_assert (TREE_CODE (conv->type) != REFERENCE_TYPE);
1063132718Skan
1064117395Skan  t = TREE_TYPE (type);
1065117395Skan
1066169689Skan  /* [over.ics.rank]
1067169689Skan
106890075Sobrien     When a parameter of reference type binds directly
106990075Sobrien     (_dcl.init.ref_) to an argument expression, the implicit
107090075Sobrien     conversion sequence is the identity conversion, unless the
107190075Sobrien     argument expression has a type that is a derived class of the
107290075Sobrien     parameter type, in which case the implicit conversion sequence is
107390075Sobrien     a derived-to-base Conversion.
1074169689Skan
107590075Sobrien     If the parameter binds directly to the result of applying a
107690075Sobrien     conversion function to the argument expression, the implicit
107790075Sobrien     conversion sequence is a user-defined conversion sequence
107890075Sobrien     (_over.ics.user_), with the second standard conversion sequence
107990075Sobrien     either an identity conversion or, if the conversion function
108090075Sobrien     returns an entity of a type that is a derived class of the
108190075Sobrien     parameter type, a derived-to-base conversion.  */
1082169689Skan  if (!same_type_ignoring_top_level_qualifiers_p (t, conv->type))
108390075Sobrien    {
108490075Sobrien      /* Represent the derived-to-base conversion.  */
1085169689Skan      conv = build_conv (ck_base, t, conv);
108690075Sobrien      /* We will actually be binding to the base-class subobject in
108790075Sobrien	 the derived class, so we mark this conversion appropriately.
108890075Sobrien	 That way, convert_like knows not to generate a temporary.  */
1089169689Skan      conv->need_temporary_p = false;
109090075Sobrien    }
1091169689Skan  return build_conv (ck_ref_bind, type, conv);
109290075Sobrien}
109390075Sobrien
109450397Sobrien/* Returns the conversion path from type FROM to reference type TO for
109550397Sobrien   purposes of reference binding.  For lvalue binding, either pass a
109690075Sobrien   reference type to FROM or an lvalue expression to EXPR.  If the
109790075Sobrien   reference will be bound to a temporary, NEED_TEMPORARY_P is set for
1098169689Skan   the conversion returned.  If C_CAST_P is true, this
1099169689Skan   conversion is coming from a C-style cast.  */
110050397Sobrien
1101169689Skanstatic conversion *
1102169689Skanreference_binding (tree rto, tree rfrom, tree expr, bool c_cast_p, int flags)
110350397Sobrien{
1104169689Skan  conversion *conv = NULL;
110550397Sobrien  tree to = TREE_TYPE (rto);
110650397Sobrien  tree from = rfrom;
1107132718Skan  bool related_p;
1108132718Skan  bool compatible_p;
110990075Sobrien  cp_lvalue_kind lvalue_p = clk_none;
111050397Sobrien
111152284Sobrien  if (TREE_CODE (to) == FUNCTION_TYPE && expr && type_unknown_p (expr))
111252284Sobrien    {
111396263Sobrien      expr = instantiate_type (to, expr, tf_none);
111452284Sobrien      if (expr == error_mark_node)
1115169689Skan	return NULL;
111652284Sobrien      from = TREE_TYPE (expr);
111752284Sobrien    }
111852284Sobrien
111950397Sobrien  if (TREE_CODE (from) == REFERENCE_TYPE)
112090075Sobrien    {
112190075Sobrien      /* Anything with reference type is an lvalue.  */
112290075Sobrien      lvalue_p = clk_ordinary;
112390075Sobrien      from = TREE_TYPE (from);
112490075Sobrien    }
112590075Sobrien  else if (expr)
112690075Sobrien    lvalue_p = real_lvalue_p (expr);
112750397Sobrien
112890075Sobrien  /* Figure out whether or not the types are reference-related and
112990075Sobrien     reference compatible.  We have do do this after stripping
113090075Sobrien     references from FROM.  */
113190075Sobrien  related_p = reference_related_p (to, from);
1132169689Skan  /* If this is a C cast, first convert to an appropriately qualified
1133169689Skan     type, so that we can later do a const_cast to the desired type.  */
1134169689Skan  if (related_p && c_cast_p
1135169689Skan      && !at_least_as_qualified_p (to, from))
1136169689Skan    to = build_qualified_type (to, cp_type_quals (from));
113790075Sobrien  compatible_p = reference_compatible_p (to, from);
113850397Sobrien
113990075Sobrien  if (lvalue_p && compatible_p)
114050397Sobrien    {
114190075Sobrien      /* [dcl.init.ref]
114290075Sobrien
1143169689Skan	 If the initializer expression
1144169689Skan
114590075Sobrien	 -- is an lvalue (but not an lvalue for a bit-field), and "cv1 T1"
114690075Sobrien	    is reference-compatible with "cv2 T2,"
1147169689Skan
1148132718Skan	 the reference is bound directly to the initializer expression
114990075Sobrien	 lvalue.  */
1150169689Skan      conv = build_identity_conv (from, expr);
115190075Sobrien      conv = direct_reference_binding (rto, conv);
1152132718Skan      if ((lvalue_p & clk_bitfield) != 0
1153132718Skan	  || ((lvalue_p & clk_packed) != 0 && !TYPE_PACKED (to)))
115490075Sobrien	/* For the purposes of overload resolution, we ignore the fact
1155132718Skan	   this expression is a bitfield or packed field. (In particular,
115690075Sobrien	   [over.ics.ref] says specifically that a function with a
115790075Sobrien	   non-const reference parameter is viable even if the
115890075Sobrien	   argument is a bitfield.)
115950397Sobrien
116090075Sobrien	   However, when we actually call the function we must create
116190075Sobrien	   a temporary to which to bind the reference.  If the
116290075Sobrien	   reference is volatile, or isn't const, then we cannot make
116390075Sobrien	   a temporary, so we just issue an error when the conversion
116490075Sobrien	   actually occurs.  */
1165169689Skan	conv->need_temporary_p = true;
1166169689Skan
116790075Sobrien      return conv;
116850397Sobrien    }
116990075Sobrien  else if (CLASS_TYPE_P (from) && !(flags & LOOKUP_NO_CONVERSION))
117090075Sobrien    {
117190075Sobrien      /* [dcl.init.ref]
117250397Sobrien
1173132718Skan	 If the initializer expression
117490075Sobrien
117590075Sobrien	 -- has a class type (i.e., T2 is a class type) can be
117690075Sobrien	    implicitly converted to an lvalue of type "cv3 T3," where
117790075Sobrien	    "cv1 T1" is reference-compatible with "cv3 T3".  (this
117890075Sobrien	    conversion is selected by enumerating the applicable
117990075Sobrien	    conversion functions (_over.match.ref_) and choosing the
1180169689Skan	    best one through overload resolution.  (_over.match_).
118190075Sobrien
1182169689Skan	the reference is bound to the lvalue result of the conversion
118390075Sobrien	in the second case.  */
118490075Sobrien      conv = convert_class_to_reference (to, from, expr);
118550397Sobrien      if (conv)
1186117395Skan	return conv;
118790075Sobrien    }
118850397Sobrien
118990075Sobrien  /* From this point on, we conceptually need temporaries, even if we
119090075Sobrien     elide them.  Only the cases above are "direct bindings".  */
119190075Sobrien  if (flags & LOOKUP_NO_TEMP_BIND)
1192169689Skan    return NULL;
119390075Sobrien
119490075Sobrien  /* [over.ics.rank]
1195169689Skan
119690075Sobrien     When a parameter of reference type is not bound directly to an
119790075Sobrien     argument expression, the conversion sequence is the one required
119890075Sobrien     to convert the argument expression to the underlying type of the
119990075Sobrien     reference according to _over.best.ics_.  Conceptually, this
120090075Sobrien     conversion sequence corresponds to copy-initializing a temporary
120190075Sobrien     of the underlying type with the argument expression.  Any
120290075Sobrien     difference in top-level cv-qualification is subsumed by the
120390075Sobrien     initialization itself and does not constitute a conversion.  */
120490075Sobrien
120590075Sobrien  /* [dcl.init.ref]
120690075Sobrien
120790075Sobrien     Otherwise, the reference shall be to a non-volatile const type.  */
120890075Sobrien  if (!CP_TYPE_CONST_NON_VOLATILE_P (to))
1209169689Skan    return NULL;
121090075Sobrien
121190075Sobrien  /* [dcl.init.ref]
1212169689Skan
121390075Sobrien     If the initializer expression is an rvalue, with T2 a class type,
121490075Sobrien     and "cv1 T1" is reference-compatible with "cv2 T2", the reference
121590075Sobrien     is bound in one of the following ways:
1216169689Skan
121790075Sobrien     -- The reference is bound to the object represented by the rvalue
1218169689Skan	or to a sub-object within that object.
121990075Sobrien
1220117395Skan     -- ...
1221169689Skan
1222117395Skan     We use the first alternative.  The implicit conversion sequence
1223117395Skan     is supposed to be same as we would obtain by generating a
1224117395Skan     temporary.  Fortunately, if the types are reference compatible,
1225117395Skan     then this is either an identity conversion or the derived-to-base
1226117395Skan     conversion, just as for direct binding.  */
122790075Sobrien  if (CLASS_TYPE_P (from) && compatible_p)
122890075Sobrien    {
1229169689Skan      conv = build_identity_conv (from, expr);
1230132718Skan      conv = direct_reference_binding (rto, conv);
1231132718Skan      if (!(flags & LOOKUP_CONSTRUCTOR_CALLABLE))
1232169689Skan	conv->u.next->check_copy_constructor_p = true;
1233132718Skan      return conv;
123450397Sobrien    }
123550397Sobrien
123690075Sobrien  /* [dcl.init.ref]
123790075Sobrien
123890075Sobrien     Otherwise, a temporary of type "cv1 T1" is created and
123990075Sobrien     initialized from the initializer expression using the rules for a
124090075Sobrien     non-reference copy initialization.  If T1 is reference-related to
124190075Sobrien     T2, cv1 must be the same cv-qualification as, or greater
124290075Sobrien     cv-qualification than, cv2; otherwise, the program is ill-formed.  */
124390075Sobrien  if (related_p && !at_least_as_qualified_p (to, from))
1244169689Skan    return NULL;
124590075Sobrien
1246169689Skan  conv = implicit_conversion (to, from, expr, c_cast_p,
1247169689Skan			      flags);
124890075Sobrien  if (!conv)
1249169689Skan    return NULL;
125090075Sobrien
1251169689Skan  conv = build_conv (ck_ref_bind, rto, conv);
125290075Sobrien  /* This reference binding, unlike those above, requires the
125390075Sobrien     creation of a temporary.  */
1254169689Skan  conv->need_temporary_p = true;
125590075Sobrien
125650397Sobrien  return conv;
125750397Sobrien}
125850397Sobrien
1259169689Skan/* Returns the implicit conversion sequence (see [over.ics]) from type
1260169689Skan   FROM to type TO.  The optional expression EXPR may affect the
1261169689Skan   conversion.  FLAGS are the usual overloading flags.  Only
1262169689Skan   LOOKUP_NO_CONVERSION is significant.  If C_CAST_P is true, this
1263169689Skan   conversion is coming from a C-style cast.  */
126450397Sobrien
1265169689Skanstatic conversion *
1266169689Skanimplicit_conversion (tree to, tree from, tree expr, bool c_cast_p,
1267169689Skan		     int flags)
126850397Sobrien{
1269169689Skan  conversion *conv;
127050397Sobrien
127190075Sobrien  if (from == error_mark_node || to == error_mark_node
127290075Sobrien      || expr == error_mark_node)
1273169689Skan    return NULL;
127490075Sobrien
127550397Sobrien  if (TREE_CODE (to) == REFERENCE_TYPE)
1276169689Skan    conv = reference_binding (to, from, expr, c_cast_p, flags);
127750397Sobrien  else
1278169689Skan    conv = standard_conversion (to, from, expr, c_cast_p, flags);
127950397Sobrien
128050397Sobrien  if (conv)
1281132718Skan    return conv;
1282132718Skan
1283132718Skan  if (expr != NULL_TREE
1284132718Skan      && (IS_AGGR_TYPE (from)
1285132718Skan	  || IS_AGGR_TYPE (to))
1286132718Skan      && (flags & LOOKUP_NO_CONVERSION) == 0)
128750397Sobrien    {
1288117395Skan      struct z_candidate *cand;
1289117395Skan
129050397Sobrien      cand = build_user_type_conversion_1
129150397Sobrien	(to, expr, LOOKUP_ONLYCONVERTING);
129250397Sobrien      if (cand)
129350397Sobrien	conv = cand->second_conv;
129490075Sobrien
129590075Sobrien      /* We used to try to bind a reference to a temporary here, but that
129690075Sobrien	 is now handled by the recursive call to this function at the end
129790075Sobrien	 of reference_binding.  */
1298132718Skan      return conv;
129950397Sobrien    }
130018334Speter
1301169689Skan  return NULL;
130250397Sobrien}
130350397Sobrien
130450397Sobrien/* Add a new entry to the list of candidates.  Used by the add_*_candidate
130550397Sobrien   functions.  */
130650397Sobrien
130750397Sobrienstatic struct z_candidate *
1308169689Skanadd_candidate (struct z_candidate **candidates,
1309169689Skan	       tree fn, tree args,
1310169689Skan	       size_t num_convs, conversion **convs,
1311169689Skan	       tree access_path, tree conversion_path,
1312169689Skan	       int viable)
131350397Sobrien{
1314169689Skan  struct z_candidate *cand = (struct z_candidate *)
1315169689Skan    conversion_obstack_alloc (sizeof (struct z_candidate));
131650397Sobrien
131750397Sobrien  cand->fn = fn;
1318132718Skan  cand->args = args;
131950397Sobrien  cand->convs = convs;
1320169689Skan  cand->num_convs = num_convs;
1321117395Skan  cand->access_path = access_path;
1322117395Skan  cand->conversion_path = conversion_path;
132350397Sobrien  cand->viable = viable;
1324117395Skan  cand->next = *candidates;
1325117395Skan  *candidates = cand;
132650397Sobrien
132750397Sobrien  return cand;
132850397Sobrien}
132950397Sobrien
133050397Sobrien/* Create an overload candidate for the function or method FN called with
133150397Sobrien   the argument list ARGLIST and add it to CANDIDATES.  FLAGS is passed on
133290075Sobrien   to implicit_conversion.
133350397Sobrien
133490075Sobrien   CTYPE, if non-NULL, is the type we want to pretend this function
133590075Sobrien   comes from for purposes of overload resolution.  */
133690075Sobrien
133750397Sobrienstatic struct z_candidate *
1338169689Skanadd_function_candidate (struct z_candidate **candidates,
1339169689Skan			tree fn, tree ctype, tree arglist,
1340117395Skan			tree access_path, tree conversion_path,
1341117395Skan			int flags)
134250397Sobrien{
134350397Sobrien  tree parmlist = TYPE_ARG_TYPES (TREE_TYPE (fn));
134450397Sobrien  int i, len;
1345169689Skan  conversion **convs;
134652284Sobrien  tree parmnode, argnode;
1347132718Skan  tree orig_arglist;
134850397Sobrien  int viable = 1;
134950397Sobrien
1350169689Skan  /* At this point we should not see any functions which haven't been
1351169689Skan     explicitly declared, except for friend functions which will have
1352169689Skan     been found using argument dependent lookup.  */
1353169689Skan  gcc_assert (!DECL_ANTICIPATED (fn) || DECL_HIDDEN_FRIEND_P (fn));
1354117395Skan
135590075Sobrien  /* The `this', `in_chrg' and VTT arguments to constructors are not
135690075Sobrien     considered in overload resolution.  */
135750397Sobrien  if (DECL_CONSTRUCTOR_P (fn))
135850397Sobrien    {
135990075Sobrien      parmlist = skip_artificial_parms_for (fn, parmlist);
1360132718Skan      orig_arglist = arglist;
136190075Sobrien      arglist = skip_artificial_parms_for (fn, arglist);
136218334Speter    }
1363169689Skan  else
1364132718Skan    orig_arglist = arglist;
136550397Sobrien
136652284Sobrien  len = list_length (arglist);
1367169689Skan  convs = alloc_conversions (len);
136850397Sobrien
136952284Sobrien  /* 13.3.2 - Viable functions [over.match.viable]
137052284Sobrien     First, to be a viable function, a candidate function shall have enough
137152284Sobrien     parameters to agree in number with the arguments in the list.
137252284Sobrien
137352284Sobrien     We need to check this first; otherwise, checking the ICSes might cause
137452284Sobrien     us to produce an ill-formed template instantiation.  */
137552284Sobrien
137652284Sobrien  parmnode = parmlist;
137750397Sobrien  for (i = 0; i < len; ++i)
137818334Speter    {
137952284Sobrien      if (parmnode == NULL_TREE || parmnode == void_list_node)
138052284Sobrien	break;
138152284Sobrien      parmnode = TREE_CHAIN (parmnode);
138252284Sobrien    }
138352284Sobrien
138452284Sobrien  if (i < len && parmnode)
138552284Sobrien    viable = 0;
138652284Sobrien
138752284Sobrien  /* Make sure there are default args for the rest of the parms.  */
138890075Sobrien  else if (!sufficient_parms_p (parmnode))
138990075Sobrien    viable = 0;
139052284Sobrien
139152284Sobrien  if (! viable)
139252284Sobrien    goto out;
139352284Sobrien
139452284Sobrien  /* Second, for F to be a viable function, there shall exist for each
139552284Sobrien     argument an implicit conversion sequence that converts that argument
139652284Sobrien     to the corresponding parameter of F.  */
139752284Sobrien
139852284Sobrien  parmnode = parmlist;
139952284Sobrien  argnode = arglist;
140052284Sobrien
140152284Sobrien  for (i = 0; i < len; ++i)
140252284Sobrien    {
140350397Sobrien      tree arg = TREE_VALUE (argnode);
140452284Sobrien      tree argtype = lvalue_type (arg);
1405169689Skan      conversion *t;
140690075Sobrien      int is_this;
140718334Speter
140850397Sobrien      if (parmnode == void_list_node)
140950397Sobrien	break;
141052284Sobrien
141190075Sobrien      is_this = (i == 0 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
141290075Sobrien		 && ! DECL_CONSTRUCTOR_P (fn));
141390075Sobrien
141452284Sobrien      if (parmnode)
141552284Sobrien	{
141652284Sobrien	  tree parmtype = TREE_VALUE (parmnode);
141752284Sobrien
141890075Sobrien	  /* The type of the implicit object parameter ('this') for
141990075Sobrien	     overload resolution is not always the same as for the
142090075Sobrien	     function itself; conversion functions are considered to
142190075Sobrien	     be members of the class being converted, and functions
142290075Sobrien	     introduced by a using-declaration are considered to be
142390075Sobrien	     members of the class that uses them.
142452284Sobrien
142590075Sobrien	     Since build_over_call ignores the ICS for the `this'
142690075Sobrien	     parameter, we can just change the parm type.  */
142790075Sobrien	  if (ctype && is_this)
142852284Sobrien	    {
142952284Sobrien	      parmtype
143090075Sobrien		= build_qualified_type (ctype,
143152284Sobrien					TYPE_QUALS (TREE_TYPE (parmtype)));
143252284Sobrien	      parmtype = build_pointer_type (parmtype);
143352284Sobrien	    }
143452284Sobrien
1435169689Skan	  t = implicit_conversion (parmtype, argtype, arg,
1436169689Skan				   /*c_cast_p=*/false, flags);
143752284Sobrien	}
143850397Sobrien      else
143918334Speter	{
1440169689Skan	  t = build_identity_conv (argtype, arg);
1441169689Skan	  t->ellipsis_p = true;
144218334Speter	}
144350397Sobrien
144490075Sobrien      if (t && is_this)
1445169689Skan	t->this_p = true;
144650397Sobrien
1447169689Skan      convs[i] = t;
144850397Sobrien      if (! t)
144952284Sobrien	{
145052284Sobrien	  viable = 0;
145152284Sobrien	  break;
145252284Sobrien	}
145350397Sobrien
1454169689Skan      if (t->bad_p)
145550397Sobrien	viable = -1;
145650397Sobrien
145750397Sobrien      if (parmnode)
145850397Sobrien	parmnode = TREE_CHAIN (parmnode);
145950397Sobrien      argnode = TREE_CHAIN (argnode);
146050397Sobrien    }
146150397Sobrien
146252284Sobrien out:
1463169689Skan  return add_candidate (candidates, fn, orig_arglist, len, convs,
1464169689Skan			access_path, conversion_path, viable);
146550397Sobrien}
146650397Sobrien
146750397Sobrien/* Create an overload candidate for the conversion function FN which will
146850397Sobrien   be invoked for expression OBJ, producing a pointer-to-function which
146950397Sobrien   will in turn be called with the argument list ARGLIST, and add it to
147052284Sobrien   CANDIDATES.  FLAGS is passed on to implicit_conversion.
147150397Sobrien
147252284Sobrien   Actually, we don't really care about FN; we care about the type it
147352284Sobrien   converts to.  There may be multiple conversion functions that will
147452284Sobrien   convert to that type, and we rely on build_user_type_conversion_1 to
147552284Sobrien   choose the best one; so when we create our candidate, we record the type
147652284Sobrien   instead of the function.  */
147752284Sobrien
147850397Sobrienstatic struct z_candidate *
1479132718Skanadd_conv_candidate (struct z_candidate **candidates, tree fn, tree obj,
1480169689Skan		    tree arglist, tree access_path, tree conversion_path)
148150397Sobrien{
148250397Sobrien  tree totype = TREE_TYPE (TREE_TYPE (fn));
148390075Sobrien  int i, len, viable, flags;
1484169689Skan  tree parmlist, parmnode, argnode;
1485169689Skan  conversion **convs;
148650397Sobrien
148790075Sobrien  for (parmlist = totype; TREE_CODE (parmlist) != FUNCTION_TYPE; )
148890075Sobrien    parmlist = TREE_TYPE (parmlist);
148990075Sobrien  parmlist = TYPE_ARG_TYPES (parmlist);
149090075Sobrien
149190075Sobrien  len = list_length (arglist) + 1;
1492169689Skan  convs = alloc_conversions (len);
149390075Sobrien  parmnode = parmlist;
149490075Sobrien  argnode = arglist;
149590075Sobrien  viable = 1;
149690075Sobrien  flags = LOOKUP_NORMAL;
149790075Sobrien
149852284Sobrien  /* Don't bother looking up the same type twice.  */
1499117395Skan  if (*candidates && (*candidates)->fn == totype)
1500117395Skan    return NULL;
150152284Sobrien
150250397Sobrien  for (i = 0; i < len; ++i)
150350397Sobrien    {
150450397Sobrien      tree arg = i == 0 ? obj : TREE_VALUE (argnode);
150550397Sobrien      tree argtype = lvalue_type (arg);
1506169689Skan      conversion *t;
150750397Sobrien
150850397Sobrien      if (i == 0)
1509169689Skan	t = implicit_conversion (totype, argtype, arg, /*c_cast_p=*/false,
1510169689Skan				 flags);
151150397Sobrien      else if (parmnode == void_list_node)
151250397Sobrien	break;
151350397Sobrien      else if (parmnode)
1514169689Skan	t = implicit_conversion (TREE_VALUE (parmnode), argtype, arg,
1515169689Skan				 /*c_cast_p=*/false, flags);
151618334Speter      else
151718334Speter	{
1518169689Skan	  t = build_identity_conv (argtype, arg);
1519169689Skan	  t->ellipsis_p = true;
152018334Speter	}
152118334Speter
1522169689Skan      convs[i] = t;
152350397Sobrien      if (! t)
152450397Sobrien	break;
152550397Sobrien
1526169689Skan      if (t->bad_p)
152750397Sobrien	viable = -1;
152850397Sobrien
152950397Sobrien      if (i == 0)
153050397Sobrien	continue;
153150397Sobrien
153250397Sobrien      if (parmnode)
153350397Sobrien	parmnode = TREE_CHAIN (parmnode);
153450397Sobrien      argnode = TREE_CHAIN (argnode);
153518334Speter    }
153650397Sobrien
153750397Sobrien  if (i < len)
153850397Sobrien    viable = 0;
153950397Sobrien
154090075Sobrien  if (!sufficient_parms_p (parmnode))
154190075Sobrien    viable = 0;
154250397Sobrien
1543169689Skan  return add_candidate (candidates, totype, arglist, len, convs,
1544169689Skan			access_path, conversion_path, viable);
154550397Sobrien}
154650397Sobrien
1547117395Skanstatic void
1548132718Skanbuild_builtin_candidate (struct z_candidate **candidates, tree fnname,
1549169689Skan			 tree type1, tree type2, tree *args, tree *argtypes,
1550169689Skan			 int flags)
155150397Sobrien{
1552169689Skan  conversion *t;
1553169689Skan  conversion **convs;
1554169689Skan  size_t num_convs;
155550397Sobrien  int viable = 1, i;
155650397Sobrien  tree types[2];
155750397Sobrien
155850397Sobrien  types[0] = type1;
155950397Sobrien  types[1] = type2;
156050397Sobrien
1561169689Skan  num_convs =  args[2] ? 3 : (args[1] ? 2 : 1);
1562169689Skan  convs = alloc_conversions (num_convs);
156350397Sobrien
156450397Sobrien  for (i = 0; i < 2; ++i)
156518334Speter    {
156650397Sobrien      if (! args[i])
156750397Sobrien	break;
156850397Sobrien
1569169689Skan      t = implicit_conversion (types[i], argtypes[i], args[i],
1570169689Skan			       /*c_cast_p=*/false, flags);
157150397Sobrien      if (! t)
157250397Sobrien	{
157350397Sobrien	  viable = 0;
157450397Sobrien	  /* We need something for printing the candidate.  */
1575169689Skan	  t = build_identity_conv (types[i], NULL_TREE);
157650397Sobrien	}
1577169689Skan      else if (t->bad_p)
157850397Sobrien	viable = 0;
1579169689Skan      convs[i] = t;
158018334Speter    }
158150397Sobrien
158250397Sobrien  /* For COND_EXPR we rearranged the arguments; undo that now.  */
158350397Sobrien  if (args[2])
158418334Speter    {
1585169689Skan      convs[2] = convs[1];
1586169689Skan      convs[1] = convs[0];
1587169689Skan      t = implicit_conversion (boolean_type_node, argtypes[2], args[2],
1588169689Skan			       /*c_cast_p=*/false, flags);
158950397Sobrien      if (t)
1590169689Skan	convs[0] = t;
159150397Sobrien      else
159250397Sobrien	viable = 0;
1593169689Skan    }
159418334Speter
1595169689Skan  add_candidate (candidates, fnname, /*args=*/NULL_TREE,
1596169689Skan		 num_convs, convs,
1597117395Skan		 /*access_path=*/NULL_TREE,
1598117395Skan		 /*conversion_path=*/NULL_TREE,
1599117395Skan		 viable);
160050397Sobrien}
160118334Speter
1602132718Skanstatic bool
1603132718Skanis_complete (tree t)
160450397Sobrien{
160590075Sobrien  return COMPLETE_TYPE_P (complete_type (t));
160650397Sobrien}
160718334Speter
1608117395Skan/* Returns nonzero if TYPE is a promoted arithmetic type.  */
160990075Sobrien
1610132718Skanstatic bool
1611132718Skanpromoted_arithmetic_type_p (tree type)
161290075Sobrien{
161390075Sobrien  /* [over.built]
161490075Sobrien
161590075Sobrien     In this section, the term promoted integral type is used to refer
161690075Sobrien     to those integral types which are preserved by integral promotion
161790075Sobrien     (including e.g.  int and long but excluding e.g.  char).
161890075Sobrien     Similarly, the term promoted arithmetic type refers to promoted
161990075Sobrien     integral types plus floating types.  */
162090075Sobrien  return ((INTEGRAL_TYPE_P (type)
162190075Sobrien	   && same_type_p (type_promotes_to (type), type))
162290075Sobrien	  || TREE_CODE (type) == REAL_TYPE);
162390075Sobrien}
162490075Sobrien
162550397Sobrien/* Create any builtin operator overload candidates for the operator in
162650397Sobrien   question given the converted operand types TYPE1 and TYPE2.  The other
162750397Sobrien   args are passed through from add_builtin_candidates to
1628169689Skan   build_builtin_candidate.
1629169689Skan
1630169689Skan   TYPE1 and TYPE2 may not be permissible, and we must filter them.
163190075Sobrien   If CODE is requires candidates operands of the same type of the kind
163290075Sobrien   of which TYPE1 and TYPE2 are, we add both candidates
163390075Sobrien   CODE (TYPE1, TYPE1) and CODE (TYPE2, TYPE2).  */
163418334Speter
1635117395Skanstatic void
1636132718Skanadd_builtin_candidate (struct z_candidate **candidates, enum tree_code code,
1637169689Skan		       enum tree_code code2, tree fnname, tree type1,
1638169689Skan		       tree type2, tree *args, tree *argtypes, int flags)
163950397Sobrien{
164050397Sobrien  switch (code)
164150397Sobrien    {
164250397Sobrien    case POSTINCREMENT_EXPR:
164350397Sobrien    case POSTDECREMENT_EXPR:
164450397Sobrien      args[1] = integer_zero_node;
164550397Sobrien      type2 = integer_type_node;
164650397Sobrien      break;
164750397Sobrien    default:
164850397Sobrien      break;
164950397Sobrien    }
165050397Sobrien
165150397Sobrien  switch (code)
165250397Sobrien    {
165350397Sobrien
165450397Sobrien/* 4 For every pair T, VQ), where T is an arithmetic or  enumeration  type,
165550397Sobrien     and  VQ  is  either  volatile or empty, there exist candidate operator
165650397Sobrien     functions of the form
165750397Sobrien	     VQ T&   operator++(VQ T&);
165850397Sobrien	     T       operator++(VQ T&, int);
165950397Sobrien   5 For every pair T, VQ), where T is an enumeration type or an arithmetic
166050397Sobrien     type  other than bool, and VQ is either volatile or empty, there exist
166150397Sobrien     candidate operator functions of the form
166250397Sobrien	     VQ T&   operator--(VQ T&);
166350397Sobrien	     T       operator--(VQ T&, int);
166450397Sobrien   6 For every pair T, VQ), where T is  a  cv-qualified  or  cv-unqualified
166550397Sobrien     complete  object type, and VQ is either volatile or empty, there exist
166650397Sobrien     candidate operator functions of the form
166750397Sobrien	     T*VQ&   operator++(T*VQ&);
166850397Sobrien	     T*VQ&   operator--(T*VQ&);
166950397Sobrien	     T*      operator++(T*VQ&, int);
167050397Sobrien	     T*      operator--(T*VQ&, int);  */
167150397Sobrien
167250397Sobrien    case POSTDECREMENT_EXPR:
167350397Sobrien    case PREDECREMENT_EXPR:
167450397Sobrien      if (TREE_CODE (type1) == BOOLEAN_TYPE)
1675117395Skan	return;
167650397Sobrien    case POSTINCREMENT_EXPR:
167750397Sobrien    case PREINCREMENT_EXPR:
167890075Sobrien      if (ARITHMETIC_TYPE_P (type1) || TYPE_PTROB_P (type1))
167918334Speter	{
168050397Sobrien	  type1 = build_reference_type (type1);
168150397Sobrien	  break;
168218334Speter	}
1683117395Skan      return;
168450397Sobrien
168550397Sobrien/* 7 For every cv-qualified or cv-unqualified complete object type T, there
168650397Sobrien     exist candidate operator functions of the form
168750397Sobrien
168850397Sobrien	     T&      operator*(T*);
168950397Sobrien
169050397Sobrien   8 For every function type T, there exist candidate operator functions of
169150397Sobrien     the form
169250397Sobrien	     T&      operator*(T*);  */
169350397Sobrien
169450397Sobrien    case INDIRECT_REF:
169550397Sobrien      if (TREE_CODE (type1) == POINTER_TYPE
169650397Sobrien	  && (TYPE_PTROB_P (type1)
169750397Sobrien	      || TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE))
169850397Sobrien	break;
1699169689Skan      return;
170050397Sobrien
170150397Sobrien/* 9 For every type T, there exist candidate operator functions of the form
170250397Sobrien	     T*      operator+(T*);
170350397Sobrien
170450397Sobrien   10For  every  promoted arithmetic type T, there exist candidate operator
170550397Sobrien     functions of the form
170650397Sobrien	     T       operator+(T);
170750397Sobrien	     T       operator-(T);  */
170850397Sobrien
1709169689Skan    case UNARY_PLUS_EXPR: /* unary + */
1710132718Skan      if (TREE_CODE (type1) == POINTER_TYPE)
171150397Sobrien	break;
171250397Sobrien    case NEGATE_EXPR:
171350397Sobrien      if (ARITHMETIC_TYPE_P (type1))
171450397Sobrien	break;
1715117395Skan      return;
171650397Sobrien
171750397Sobrien/* 11For every promoted integral type T,  there  exist  candidate  operator
171850397Sobrien     functions of the form
171950397Sobrien	     T       operator~(T);  */
172050397Sobrien
172150397Sobrien    case BIT_NOT_EXPR:
172250397Sobrien      if (INTEGRAL_TYPE_P (type1))
172350397Sobrien	break;
1724117395Skan      return;
172550397Sobrien
172650397Sobrien/* 12For every quintuple C1, C2, T, CV1, CV2), where C2 is a class type, C1
172750397Sobrien     is the same type as C2 or is a derived class of C2, T  is  a  complete
172850397Sobrien     object type or a function type, and CV1 and CV2 are cv-qualifier-seqs,
172950397Sobrien     there exist candidate operator functions of the form
173050397Sobrien	     CV12 T& operator->*(CV1 C1*, CV2 T C2::*);
173150397Sobrien     where CV12 is the union of CV1 and CV2.  */
173250397Sobrien
173350397Sobrien    case MEMBER_REF:
173450397Sobrien      if (TREE_CODE (type1) == POINTER_TYPE
1735132718Skan	  && TYPE_PTR_TO_MEMBER_P (type2))
173618334Speter	{
173750397Sobrien	  tree c1 = TREE_TYPE (type1);
1738132718Skan	  tree c2 = TYPE_PTRMEM_CLASS_TYPE (type2);
173918334Speter
174050397Sobrien	  if (IS_AGGR_TYPE (c1) && DERIVED_FROM_P (c2, c1)
174150397Sobrien	      && (TYPE_PTRMEMFUNC_P (type2)
1742161651Skan		  || is_complete (TYPE_PTRMEM_POINTED_TO_TYPE (type2))))
174350397Sobrien	    break;
174450397Sobrien	}
1745117395Skan      return;
174618334Speter
174750397Sobrien/* 13For every pair of promoted arithmetic types L and R, there exist  can-
174850397Sobrien     didate operator functions of the form
174950397Sobrien	     LR      operator*(L, R);
175050397Sobrien	     LR      operator/(L, R);
175150397Sobrien	     LR      operator+(L, R);
175250397Sobrien	     LR      operator-(L, R);
175350397Sobrien	     bool    operator<(L, R);
175450397Sobrien	     bool    operator>(L, R);
175550397Sobrien	     bool    operator<=(L, R);
175650397Sobrien	     bool    operator>=(L, R);
175750397Sobrien	     bool    operator==(L, R);
175850397Sobrien	     bool    operator!=(L, R);
175950397Sobrien     where  LR  is  the  result of the usual arithmetic conversions between
176050397Sobrien     types L and R.
176150397Sobrien
176250397Sobrien   14For every pair of types T and I, where T  is  a  cv-qualified  or  cv-
176350397Sobrien     unqualified  complete  object  type and I is a promoted integral type,
176450397Sobrien     there exist candidate operator functions of the form
176550397Sobrien	     T*      operator+(T*, I);
176650397Sobrien	     T&      operator[](T*, I);
176750397Sobrien	     T*      operator-(T*, I);
176850397Sobrien	     T*      operator+(I, T*);
176950397Sobrien	     T&      operator[](I, T*);
177050397Sobrien
177150397Sobrien   15For every T, where T is a pointer to complete object type, there exist
177250397Sobrien     candidate operator functions of the form112)
177350397Sobrien	     ptrdiff_t operator-(T, T);
177450397Sobrien
177590075Sobrien   16For every pointer or enumeration type T, there exist candidate operator
177690075Sobrien     functions of the form
177750397Sobrien	     bool    operator<(T, T);
177850397Sobrien	     bool    operator>(T, T);
177950397Sobrien	     bool    operator<=(T, T);
178050397Sobrien	     bool    operator>=(T, T);
178150397Sobrien	     bool    operator==(T, T);
178250397Sobrien	     bool    operator!=(T, T);
178350397Sobrien
178450397Sobrien   17For every pointer to member type T,  there  exist  candidate  operator
178550397Sobrien     functions of the form
178650397Sobrien	     bool    operator==(T, T);
178750397Sobrien	     bool    operator!=(T, T);  */
178850397Sobrien
178950397Sobrien    case MINUS_EXPR:
179050397Sobrien      if (TYPE_PTROB_P (type1) && TYPE_PTROB_P (type2))
179150397Sobrien	break;
179250397Sobrien      if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2))
179350397Sobrien	{
179450397Sobrien	  type2 = ptrdiff_type_node;
179550397Sobrien	  break;
179650397Sobrien	}
179750397Sobrien    case MULT_EXPR:
179850397Sobrien    case TRUNC_DIV_EXPR:
179950397Sobrien      if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
180050397Sobrien	break;
1801117395Skan      return;
180250397Sobrien
180350397Sobrien    case EQ_EXPR:
180450397Sobrien    case NE_EXPR:
180550397Sobrien      if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2))
180650397Sobrien	  || (TYPE_PTRMEM_P (type1) && TYPE_PTRMEM_P (type2)))
180750397Sobrien	break;
1808132718Skan      if (TYPE_PTR_TO_MEMBER_P (type1) && null_ptr_cst_p (args[1]))
180950397Sobrien	{
181050397Sobrien	  type2 = type1;
181150397Sobrien	  break;
181250397Sobrien	}
1813132718Skan      if (TYPE_PTR_TO_MEMBER_P (type2) && null_ptr_cst_p (args[0]))
181450397Sobrien	{
181550397Sobrien	  type1 = type2;
181650397Sobrien	  break;
181750397Sobrien	}
1818132718Skan      /* Fall through.  */
181950397Sobrien    case LT_EXPR:
182050397Sobrien    case GT_EXPR:
182150397Sobrien    case LE_EXPR:
182250397Sobrien    case GE_EXPR:
182350397Sobrien    case MAX_EXPR:
182450397Sobrien    case MIN_EXPR:
182590075Sobrien      if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1826169689Skan	break;
182790075Sobrien      if (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
182850397Sobrien	break;
1829169689Skan      if (TREE_CODE (type1) == ENUMERAL_TYPE
1830169689Skan	  && TREE_CODE (type2) == ENUMERAL_TYPE)
1831169689Skan	break;
1832169689Skan      if (TYPE_PTR_P (type1)
1833169689Skan	  && null_ptr_cst_p (args[1])
1834169689Skan	  && !uses_template_parms (type1))
183550397Sobrien	{
183650397Sobrien	  type2 = type1;
183750397Sobrien	  break;
183850397Sobrien	}
1839169689Skan      if (null_ptr_cst_p (args[0])
1840169689Skan	  && TYPE_PTR_P (type2)
1841169689Skan	  && !uses_template_parms (type2))
184250397Sobrien	{
184350397Sobrien	  type1 = type2;
184450397Sobrien	  break;
184550397Sobrien	}
1846117395Skan      return;
184750397Sobrien
184850397Sobrien    case PLUS_EXPR:
184950397Sobrien      if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
185050397Sobrien	break;
185150397Sobrien    case ARRAY_REF:
185250397Sobrien      if (INTEGRAL_TYPE_P (type1) && TYPE_PTROB_P (type2))
185350397Sobrien	{
185450397Sobrien	  type1 = ptrdiff_type_node;
185550397Sobrien	  break;
185650397Sobrien	}
185750397Sobrien      if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2))
185850397Sobrien	{
185950397Sobrien	  type2 = ptrdiff_type_node;
186050397Sobrien	  break;
186150397Sobrien	}
1862117395Skan      return;
186350397Sobrien
186450397Sobrien/* 18For  every pair of promoted integral types L and R, there exist candi-
186550397Sobrien     date operator functions of the form
186650397Sobrien	     LR      operator%(L, R);
186750397Sobrien	     LR      operator&(L, R);
186850397Sobrien	     LR      operator^(L, R);
186950397Sobrien	     LR      operator|(L, R);
187050397Sobrien	     L       operator<<(L, R);
187150397Sobrien	     L       operator>>(L, R);
187250397Sobrien     where LR is the result of the  usual  arithmetic  conversions  between
187350397Sobrien     types L and R.  */
187450397Sobrien
187550397Sobrien    case TRUNC_MOD_EXPR:
187650397Sobrien    case BIT_AND_EXPR:
187750397Sobrien    case BIT_IOR_EXPR:
187850397Sobrien    case BIT_XOR_EXPR:
187950397Sobrien    case LSHIFT_EXPR:
188050397Sobrien    case RSHIFT_EXPR:
188150397Sobrien      if (INTEGRAL_TYPE_P (type1) && INTEGRAL_TYPE_P (type2))
188250397Sobrien	break;
1883117395Skan      return;
188450397Sobrien
188550397Sobrien/* 19For  every  triple  L, VQ, R), where L is an arithmetic or enumeration
188650397Sobrien     type, VQ is either volatile or empty, and R is a  promoted  arithmetic
188750397Sobrien     type, there exist candidate operator functions of the form
188850397Sobrien	     VQ L&   operator=(VQ L&, R);
188950397Sobrien	     VQ L&   operator*=(VQ L&, R);
189050397Sobrien	     VQ L&   operator/=(VQ L&, R);
189150397Sobrien	     VQ L&   operator+=(VQ L&, R);
189250397Sobrien	     VQ L&   operator-=(VQ L&, R);
189350397Sobrien
189450397Sobrien   20For  every  pair T, VQ), where T is any type and VQ is either volatile
189550397Sobrien     or empty, there exist candidate operator functions of the form
189650397Sobrien	     T*VQ&   operator=(T*VQ&, T*);
189750397Sobrien
189850397Sobrien   21For every pair T, VQ), where T is a pointer to member type and  VQ  is
189950397Sobrien     either  volatile or empty, there exist candidate operator functions of
190050397Sobrien     the form
190150397Sobrien	     VQ T&   operator=(VQ T&, T);
190250397Sobrien
190350397Sobrien   22For every triple  T,  VQ,  I),  where  T  is  a  cv-qualified  or  cv-
190450397Sobrien     unqualified  complete object type, VQ is either volatile or empty, and
190550397Sobrien     I is a promoted integral type, there exist  candidate  operator  func-
190650397Sobrien     tions of the form
190750397Sobrien	     T*VQ&   operator+=(T*VQ&, I);
190850397Sobrien	     T*VQ&   operator-=(T*VQ&, I);
190950397Sobrien
191050397Sobrien   23For  every  triple  L,  VQ,  R), where L is an integral or enumeration
191150397Sobrien     type, VQ is either volatile or empty, and R  is  a  promoted  integral
191250397Sobrien     type, there exist candidate operator functions of the form
191350397Sobrien
191450397Sobrien	     VQ L&   operator%=(VQ L&, R);
191550397Sobrien	     VQ L&   operator<<=(VQ L&, R);
191650397Sobrien	     VQ L&   operator>>=(VQ L&, R);
191750397Sobrien	     VQ L&   operator&=(VQ L&, R);
191850397Sobrien	     VQ L&   operator^=(VQ L&, R);
191950397Sobrien	     VQ L&   operator|=(VQ L&, R);  */
192050397Sobrien
192150397Sobrien    case MODIFY_EXPR:
192250397Sobrien      switch (code2)
192350397Sobrien	{
192450397Sobrien	case PLUS_EXPR:
192550397Sobrien	case MINUS_EXPR:
192650397Sobrien	  if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2))
192718334Speter	    {
192850397Sobrien	      type2 = ptrdiff_type_node;
192950397Sobrien	      break;
193018334Speter	    }
193150397Sobrien	case MULT_EXPR:
193250397Sobrien	case TRUNC_DIV_EXPR:
193350397Sobrien	  if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
193450397Sobrien	    break;
1935117395Skan	  return;
193650397Sobrien
193750397Sobrien	case TRUNC_MOD_EXPR:
193850397Sobrien	case BIT_AND_EXPR:
193950397Sobrien	case BIT_IOR_EXPR:
194050397Sobrien	case BIT_XOR_EXPR:
194150397Sobrien	case LSHIFT_EXPR:
194250397Sobrien	case RSHIFT_EXPR:
194350397Sobrien	  if (INTEGRAL_TYPE_P (type1) && INTEGRAL_TYPE_P (type2))
194450397Sobrien	    break;
1945117395Skan	  return;
194650397Sobrien
194750397Sobrien	case NOP_EXPR:
194850397Sobrien	  if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
194950397Sobrien	    break;
195050397Sobrien	  if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2))
195150397Sobrien	      || (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
195250397Sobrien	      || (TYPE_PTRMEM_P (type1) && TYPE_PTRMEM_P (type2))
195350397Sobrien	      || ((TYPE_PTRMEMFUNC_P (type1)
195450397Sobrien		   || TREE_CODE (type1) == POINTER_TYPE)
195550397Sobrien		  && null_ptr_cst_p (args[1])))
195618334Speter	    {
195750397Sobrien	      type2 = type1;
195850397Sobrien	      break;
195918334Speter	    }
1960117395Skan	  return;
196150397Sobrien
196250397Sobrien	default:
1963169689Skan	  gcc_unreachable ();
196418334Speter	}
196550397Sobrien      type1 = build_reference_type (type1);
196650397Sobrien      break;
196718334Speter
196850397Sobrien    case COND_EXPR:
196990075Sobrien      /* [over.built]
197018334Speter
197190075Sobrien	 For every pair of promoted arithmetic types L and R, there
1972169689Skan	 exist candidate operator functions of the form
197390075Sobrien
1974169689Skan	 LR operator?(bool, L, R);
197590075Sobrien
197690075Sobrien	 where LR is the result of the usual arithmetic conversions
197790075Sobrien	 between types L and R.
197890075Sobrien
197990075Sobrien	 For every type T, where T is a pointer or pointer-to-member
198090075Sobrien	 type, there exist candidate operator functions of the form T
198190075Sobrien	 operator?(bool, T, T);  */
198290075Sobrien
198390075Sobrien      if (promoted_arithmetic_type_p (type1)
198490075Sobrien	  && promoted_arithmetic_type_p (type2))
198590075Sobrien	/* That's OK.  */
198650397Sobrien	break;
198790075Sobrien
198890075Sobrien      /* Otherwise, the types should be pointers.  */
1989132718Skan      if (!(TYPE_PTR_P (type1) || TYPE_PTR_TO_MEMBER_P (type1))
1990132718Skan	  || !(TYPE_PTR_P (type2) || TYPE_PTR_TO_MEMBER_P (type2)))
1991117395Skan	return;
1992169689Skan
199390075Sobrien      /* We don't check that the two types are the same; the logic
199490075Sobrien	 below will actually create two candidates; one in which both
199590075Sobrien	 parameter types are TYPE1, and one in which both parameter
199690075Sobrien	 types are TYPE2.  */
1997117395Skan      break;
199890075Sobrien
199950397Sobrien    default:
2000169689Skan      gcc_unreachable ();
200150397Sobrien    }
200250397Sobrien
200390075Sobrien  /* If we're dealing with two pointer types or two enumeral types,
200490075Sobrien     we need candidates for both of them.  */
200590075Sobrien  if (type2 && !same_type_p (type1, type2)
200650397Sobrien      && TREE_CODE (type1) == TREE_CODE (type2)
200750397Sobrien      && (TREE_CODE (type1) == REFERENCE_TYPE
2008132718Skan	  || (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
2009132718Skan	  || (TYPE_PTRMEM_P (type1) && TYPE_PTRMEM_P (type2))
201050397Sobrien	  || TYPE_PTRMEMFUNC_P (type1)
201190075Sobrien	  || IS_AGGR_TYPE (type1)
201290075Sobrien	  || TREE_CODE (type1) == ENUMERAL_TYPE))
201350397Sobrien    {
2014117395Skan      build_builtin_candidate
201550397Sobrien	(candidates, fnname, type1, type1, args, argtypes, flags);
2016117395Skan      build_builtin_candidate
201750397Sobrien	(candidates, fnname, type2, type2, args, argtypes, flags);
2018117395Skan      return;
201950397Sobrien    }
202050397Sobrien
2021117395Skan  build_builtin_candidate
202250397Sobrien    (candidates, fnname, type1, type2, args, argtypes, flags);
202350397Sobrien}
202450397Sobrien
202550397Sobrientree
2026132718Skantype_decays_to (tree type)
202750397Sobrien{
202850397Sobrien  if (TREE_CODE (type) == ARRAY_TYPE)
202950397Sobrien    return build_pointer_type (TREE_TYPE (type));
203050397Sobrien  if (TREE_CODE (type) == FUNCTION_TYPE)
203150397Sobrien    return build_pointer_type (type);
203250397Sobrien  return type;
203350397Sobrien}
203450397Sobrien
203550397Sobrien/* There are three conditions of builtin candidates:
203650397Sobrien
203750397Sobrien   1) bool-taking candidates.  These are the same regardless of the input.
203850397Sobrien   2) pointer-pair taking candidates.  These are generated for each type
203950397Sobrien      one of the input types converts to.
204090075Sobrien   3) arithmetic candidates.  According to the standard, we should generate
204190075Sobrien      all of these, but I'm trying not to...
2042169689Skan
204390075Sobrien   Here we generate a superset of the possible candidates for this particular
204490075Sobrien   case.  That is a subset of the full set the standard defines, plus some
204590075Sobrien   other cases which the standard disallows. add_builtin_candidate will
2046117395Skan   filter out the invalid set.  */
204750397Sobrien
2048117395Skanstatic void
2049132718Skanadd_builtin_candidates (struct z_candidate **candidates, enum tree_code code,
2050169689Skan			enum tree_code code2, tree fnname, tree *args,
2051169689Skan			int flags)
205250397Sobrien{
205350397Sobrien  int ref1, i;
205490075Sobrien  int enum_p = 0;
205590075Sobrien  tree type, argtypes[3];
205690075Sobrien  /* TYPES[i] is the set of possible builtin-operator parameter types
205790075Sobrien     we will consider for the Ith argument.  These are represented as
205890075Sobrien     a TREE_LIST; the TREE_VALUE of each node is the potential
205990075Sobrien     parameter type.  */
206090075Sobrien  tree types[2];
206150397Sobrien
206250397Sobrien  for (i = 0; i < 3; ++i)
206350397Sobrien    {
206450397Sobrien      if (args[i])
206550397Sobrien	argtypes[i]  = lvalue_type (args[i]);
206650397Sobrien      else
206750397Sobrien	argtypes[i] = NULL_TREE;
206850397Sobrien    }
206950397Sobrien
207050397Sobrien  switch (code)
207150397Sobrien    {
207250397Sobrien/* 4 For every pair T, VQ), where T is an arithmetic or  enumeration  type,
207350397Sobrien     and  VQ  is  either  volatile or empty, there exist candidate operator
207450397Sobrien     functions of the form
207550397Sobrien		 VQ T&   operator++(VQ T&);  */
207650397Sobrien
207750397Sobrien    case POSTINCREMENT_EXPR:
207850397Sobrien    case PREINCREMENT_EXPR:
207950397Sobrien    case POSTDECREMENT_EXPR:
208050397Sobrien    case PREDECREMENT_EXPR:
208150397Sobrien    case MODIFY_EXPR:
208250397Sobrien      ref1 = 1;
208350397Sobrien      break;
208450397Sobrien
208550397Sobrien/* 24There also exist candidate operator functions of the form
208650397Sobrien	     bool    operator!(bool);
208750397Sobrien	     bool    operator&&(bool, bool);
208850397Sobrien	     bool    operator||(bool, bool);  */
208950397Sobrien
209050397Sobrien    case TRUTH_NOT_EXPR:
2091117395Skan      build_builtin_candidate
209250397Sobrien	(candidates, fnname, boolean_type_node,
209350397Sobrien	 NULL_TREE, args, argtypes, flags);
2094117395Skan      return;
209550397Sobrien
209650397Sobrien    case TRUTH_ORIF_EXPR:
209750397Sobrien    case TRUTH_ANDIF_EXPR:
2098117395Skan      build_builtin_candidate
209950397Sobrien	(candidates, fnname, boolean_type_node,
210050397Sobrien	 boolean_type_node, args, argtypes, flags);
2101117395Skan      return;
210250397Sobrien
210350397Sobrien    case ADDR_EXPR:
210450397Sobrien    case COMPOUND_EXPR:
210550397Sobrien    case COMPONENT_REF:
2106117395Skan      return;
210750397Sobrien
210890075Sobrien    case COND_EXPR:
210990075Sobrien    case EQ_EXPR:
211090075Sobrien    case NE_EXPR:
211190075Sobrien    case LT_EXPR:
211290075Sobrien    case LE_EXPR:
211390075Sobrien    case GT_EXPR:
211490075Sobrien    case GE_EXPR:
211590075Sobrien      enum_p = 1;
2116132718Skan      /* Fall through.  */
2117169689Skan
211850397Sobrien    default:
211950397Sobrien      ref1 = 0;
212050397Sobrien    }
212150397Sobrien
212250397Sobrien  types[0] = types[1] = NULL_TREE;
212350397Sobrien
212450397Sobrien  for (i = 0; i < 2; ++i)
212550397Sobrien    {
212650397Sobrien      if (! args[i])
212750397Sobrien	;
212850397Sobrien      else if (IS_AGGR_TYPE (argtypes[i]))
212918334Speter	{
213052284Sobrien	  tree convs;
213150397Sobrien
213250397Sobrien	  if (i == 0 && code == MODIFY_EXPR && code2 == NOP_EXPR)
2133117395Skan	    return;
213450397Sobrien
213550397Sobrien	  convs = lookup_conversions (argtypes[i]);
213650397Sobrien
213750397Sobrien	  if (code == COND_EXPR)
213818334Speter	    {
213950397Sobrien	      if (real_lvalue_p (args[i]))
214090075Sobrien		types[i] = tree_cons
214150397Sobrien		  (NULL_TREE, build_reference_type (argtypes[i]), types[i]);
214250397Sobrien
214390075Sobrien	      types[i] = tree_cons
214450397Sobrien		(NULL_TREE, TYPE_MAIN_VARIANT (argtypes[i]), types[i]);
214518334Speter	    }
214650397Sobrien
214750397Sobrien	  else if (! convs)
2148117395Skan	    return;
214950397Sobrien
215050397Sobrien	  for (; convs; convs = TREE_CHAIN (convs))
215118334Speter	    {
215252284Sobrien	      type = TREE_TYPE (TREE_TYPE (OVL_CURRENT (TREE_VALUE (convs))));
215350397Sobrien
215450397Sobrien	      if (i == 0 && ref1
215550397Sobrien		  && (TREE_CODE (type) != REFERENCE_TYPE
215652284Sobrien		      || CP_TYPE_CONST_P (TREE_TYPE (type))))
215750397Sobrien		continue;
215850397Sobrien
215950397Sobrien	      if (code == COND_EXPR && TREE_CODE (type) == REFERENCE_TYPE)
216090075Sobrien		types[i] = tree_cons (NULL_TREE, type, types[i]);
216150397Sobrien
216250397Sobrien	      type = non_reference (type);
216350397Sobrien	      if (i != 0 || ! ref1)
216450397Sobrien		{
216550397Sobrien		  type = TYPE_MAIN_VARIANT (type_decays_to (type));
2166169689Skan		  if (enum_p && TREE_CODE (type) == ENUMERAL_TYPE)
2167169689Skan		    types[i] = tree_cons (NULL_TREE, type, types[i]);
216850397Sobrien		  if (INTEGRAL_TYPE_P (type))
216950397Sobrien		    type = type_promotes_to (type);
217050397Sobrien		}
217150397Sobrien
217250397Sobrien	      if (! value_member (type, types[i]))
217390075Sobrien		types[i] = tree_cons (NULL_TREE, type, types[i]);
217418334Speter	    }
217518334Speter	}
217650397Sobrien      else
217750397Sobrien	{
217850397Sobrien	  if (code == COND_EXPR && real_lvalue_p (args[i]))
217990075Sobrien	    types[i] = tree_cons
218050397Sobrien	      (NULL_TREE, build_reference_type (argtypes[i]), types[i]);
218150397Sobrien	  type = non_reference (argtypes[i]);
218250397Sobrien	  if (i != 0 || ! ref1)
218350397Sobrien	    {
218450397Sobrien	      type = TYPE_MAIN_VARIANT (type_decays_to (type));
218590075Sobrien	      if (enum_p && TREE_CODE (type) == ENUMERAL_TYPE)
2186169689Skan		types[i] = tree_cons (NULL_TREE, type, types[i]);
218750397Sobrien	      if (INTEGRAL_TYPE_P (type))
218850397Sobrien		type = type_promotes_to (type);
218950397Sobrien	    }
219090075Sobrien	  types[i] = tree_cons (NULL_TREE, type, types[i]);
219150397Sobrien	}
219218334Speter    }
219318334Speter
219490075Sobrien  /* Run through the possible parameter types of both arguments,
219590075Sobrien     creating candidates with those parameter types.  */
219650397Sobrien  for (; types[0]; types[0] = TREE_CHAIN (types[0]))
219718334Speter    {
219850397Sobrien      if (types[1])
219950397Sobrien	for (type = types[1]; type; type = TREE_CHAIN (type))
2200117395Skan	  add_builtin_candidate
220150397Sobrien	    (candidates, code, code2, fnname, TREE_VALUE (types[0]),
220250397Sobrien	     TREE_VALUE (type), args, argtypes, flags);
220350397Sobrien      else
2204117395Skan	add_builtin_candidate
220550397Sobrien	  (candidates, code, code2, fnname, TREE_VALUE (types[0]),
220650397Sobrien	   NULL_TREE, args, argtypes, flags);
220718334Speter    }
220850397Sobrien}
220918334Speter
221018334Speter
221150397Sobrien/* If TMPL can be successfully instantiated as indicated by
221250397Sobrien   EXPLICIT_TARGS and ARGLIST, adds the instantiation to CANDIDATES.
221350397Sobrien
221450397Sobrien   TMPL is the template.  EXPLICIT_TARGS are any explicit template
221550397Sobrien   arguments.  ARGLIST is the arguments provided at the call-site.
221650397Sobrien   The RETURN_TYPE is the desired type for conversion operators.  If
221790075Sobrien   OBJ is NULL_TREE, FLAGS and CTYPE are as for add_function_candidate.
221890075Sobrien   If an OBJ is supplied, FLAGS and CTYPE are ignored, and OBJ is as for
221950397Sobrien   add_conv_candidate.  */
222050397Sobrien
222150397Sobrienstatic struct z_candidate*
2222132718Skanadd_template_candidate_real (struct z_candidate **candidates, tree tmpl,
2223169689Skan			     tree ctype, tree explicit_targs, tree arglist,
2224169689Skan			     tree return_type, tree access_path,
2225132718Skan			     tree conversion_path, int flags, tree obj,
2226169689Skan			     unification_kind_t strict)
222750397Sobrien{
222850397Sobrien  int ntparms = DECL_NTPARMS (tmpl);
222990075Sobrien  tree targs = make_tree_vec (ntparms);
223090075Sobrien  tree args_without_in_chrg = arglist;
223150397Sobrien  struct z_candidate *cand;
223250397Sobrien  int i;
223350397Sobrien  tree fn;
223450397Sobrien
223590075Sobrien  /* We don't do deduction on the in-charge parameter, the VTT
223690075Sobrien     parameter or 'this'.  */
223790075Sobrien  if (DECL_NONSTATIC_MEMBER_FUNCTION_P (tmpl))
223890075Sobrien    args_without_in_chrg = TREE_CHAIN (args_without_in_chrg);
223950397Sobrien
224090075Sobrien  if ((DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (tmpl)
224190075Sobrien       || DECL_BASE_CONSTRUCTOR_P (tmpl))
2242169689Skan      && CLASSTYPE_VBASECLASSES (DECL_CONTEXT (tmpl)))
224390075Sobrien    args_without_in_chrg = TREE_CHAIN (args_without_in_chrg);
224490075Sobrien
224590075Sobrien  i = fn_type_unification (tmpl, explicit_targs, targs,
224690075Sobrien			   args_without_in_chrg,
2247169689Skan			   return_type, strict, flags);
224890075Sobrien
224950397Sobrien  if (i != 0)
2250117395Skan    return NULL;
225150397Sobrien
2252132718Skan  fn = instantiate_template (tmpl, targs, tf_none);
225350397Sobrien  if (fn == error_mark_node)
2254117395Skan    return NULL;
225550397Sobrien
225696263Sobrien  /* In [class.copy]:
225796263Sobrien
225896263Sobrien       A member function template is never instantiated to perform the
2259169689Skan       copy of a class object to an object of its class type.
226096263Sobrien
226196263Sobrien     It's a little unclear what this means; the standard explicitly
226296263Sobrien     does allow a template to be used to copy a class.  For example,
226396263Sobrien     in:
226496263Sobrien
226596263Sobrien       struct A {
2266169689Skan	 A(A&);
226796263Sobrien	 template <class T> A(const T&);
226896263Sobrien       };
226996263Sobrien       const A f ();
227096263Sobrien       void g () { A a (f ()); }
2271169689Skan
227296263Sobrien     the member template will be used to make the copy.  The section
227396263Sobrien     quoted above appears in the paragraph that forbids constructors
227496263Sobrien     whose only parameter is (a possibly cv-qualified variant of) the
227596263Sobrien     class type, and a logical interpretation is that the intent was
227696263Sobrien     to forbid the instantiation of member templates which would then
227796263Sobrien     have that form.  */
2278169689Skan  if (DECL_CONSTRUCTOR_P (fn) && list_length (arglist) == 2)
227996263Sobrien    {
228096263Sobrien      tree arg_types = FUNCTION_FIRST_USER_PARMTYPE (fn);
228196263Sobrien      if (arg_types && same_type_p (TYPE_MAIN_VARIANT (TREE_VALUE (arg_types)),
228296263Sobrien				    ctype))
2283117395Skan	return NULL;
228496263Sobrien    }
228596263Sobrien
228650397Sobrien  if (obj != NULL_TREE)
228750397Sobrien    /* Aha, this is a conversion function.  */
2288117395Skan    cand = add_conv_candidate (candidates, fn, obj, access_path,
2289117395Skan			       conversion_path, arglist);
229050397Sobrien  else
229190075Sobrien    cand = add_function_candidate (candidates, fn, ctype,
2292169689Skan				   arglist, access_path,
2293117395Skan				   conversion_path, flags);
229450397Sobrien  if (DECL_TI_TEMPLATE (fn) != tmpl)
229550397Sobrien    /* This situation can occur if a member template of a template
229650397Sobrien       class is specialized.  Then, instantiate_template might return
229750397Sobrien       an instantiation of the specialization, in which case the
229850397Sobrien       DECL_TI_TEMPLATE field will point at the original
229950397Sobrien       specialization.  For example:
230050397Sobrien
230150397Sobrien	 template <class T> struct S { template <class U> void f(U);
230250397Sobrien				       template <> void f(int) {}; };
230350397Sobrien	 S<double> sd;
230450397Sobrien	 sd.f(3);
230550397Sobrien
230650397Sobrien       Here, TMPL will be template <class U> S<double>::f(U).
230750397Sobrien       And, instantiate template will give us the specialization
230850397Sobrien       template <> S<double>::f(int).  But, the DECL_TI_TEMPLATE field
230950397Sobrien       for this will point at template <class T> template <> S<T>::f(int),
231050397Sobrien       so that we can find the definition.  For the purposes of
231150397Sobrien       overload resolution, however, we want the original TMPL.  */
2312169689Skan    cand->template_decl = tree_cons (tmpl, targs, NULL_TREE);
231350397Sobrien  else
2314169689Skan    cand->template_decl = DECL_TEMPLATE_INFO (fn);
231550397Sobrien
231650397Sobrien  return cand;
231750397Sobrien}
231850397Sobrien
231950397Sobrien
232050397Sobrienstatic struct z_candidate *
2321132718Skanadd_template_candidate (struct z_candidate **candidates, tree tmpl, tree ctype,
2322169689Skan			tree explicit_targs, tree arglist, tree return_type,
2323169689Skan			tree access_path, tree conversion_path, int flags,
2324169689Skan			unification_kind_t strict)
232550397Sobrien{
2326169689Skan  return
232790075Sobrien    add_template_candidate_real (candidates, tmpl, ctype,
2328169689Skan				 explicit_targs, arglist, return_type,
2329117395Skan				 access_path, conversion_path,
2330117395Skan				 flags, NULL_TREE, strict);
233150397Sobrien}
233250397Sobrien
233350397Sobrien
233450397Sobrienstatic struct z_candidate *
2335132718Skanadd_template_conv_candidate (struct z_candidate **candidates, tree tmpl,
2336169689Skan			     tree obj, tree arglist, tree return_type,
2337132718Skan			     tree access_path, tree conversion_path)
233850397Sobrien{
2339169689Skan  return
234090075Sobrien    add_template_candidate_real (candidates, tmpl, NULL_TREE, NULL_TREE,
2341117395Skan				 arglist, return_type, access_path,
2342117395Skan				 conversion_path, 0, obj, DEDUCE_CONV);
234350397Sobrien}
234450397Sobrien
2345132718Skan/* The CANDS are the set of candidates that were considered for
2346132718Skan   overload resolution.  Return the set of viable candidates.  If none
2347132718Skan   of the candidates were viable, set *ANY_VIABLE_P to true.  STRICT_P
2348132718Skan   is true if a candidate should be considered viable only if it is
2349132718Skan   strictly viable.  */
235050397Sobrien
2351132718Skanstatic struct z_candidate*
2352132718Skansplice_viable (struct z_candidate *cands,
2353132718Skan	       bool strict_p,
2354132718Skan	       bool *any_viable_p)
235550397Sobrien{
2356132718Skan  struct z_candidate *viable;
2357132718Skan  struct z_candidate **last_viable;
2358132718Skan  struct z_candidate **cand;
235950397Sobrien
2360132718Skan  viable = NULL;
2361132718Skan  last_viable = &viable;
2362132718Skan  *any_viable_p = false;
2363117395Skan
2364169689Skan  cand = &cands;
2365169689Skan  while (*cand)
236618334Speter    {
2367132718Skan      struct z_candidate *c = *cand;
2368132718Skan      if (strict_p ? c->viable == 1 : c->viable)
2369132718Skan	{
2370132718Skan	  *last_viable = c;
2371132718Skan	  *cand = c->next;
2372132718Skan	  c->next = NULL;
2373132718Skan	  last_viable = &c->next;
2374132718Skan	  *any_viable_p = true;
2375132718Skan	}
237650397Sobrien      else
2377132718Skan	cand = &c->next;
237850397Sobrien    }
237950397Sobrien
2380132718Skan  return viable ? viable : cands;
238150397Sobrien}
238250397Sobrien
2383132718Skanstatic bool
2384132718Skanany_strictly_viable (struct z_candidate *cands)
2385132718Skan{
2386132718Skan  for (; cands; cands = cands->next)
2387132718Skan    if (cands->viable == 1)
2388132718Skan      return true;
2389132718Skan  return false;
2390132718Skan}
2391132718Skan
2392146895Skan/* OBJ is being used in an expression like "OBJ.f (...)".  In other
2393146895Skan   words, it is about to become the "this" pointer for a member
2394146895Skan   function call.  Take the address of the object.  */
2395146895Skan
239650397Sobrienstatic tree
2397132718Skanbuild_this (tree obj)
239850397Sobrien{
2399146895Skan  /* In a template, we are only concerned about the type of the
2400146895Skan     expression, so we can take a shortcut.  */
2401146895Skan  if (processing_template_decl)
2402146895Skan    return build_address (obj);
2403146895Skan
240490075Sobrien  return build_unary_op (ADDR_EXPR, obj, 0);
240550397Sobrien}
240650397Sobrien
2407117395Skan/* Returns true iff functions are equivalent. Equivalent functions are
2408117395Skan   not '==' only if one is a function-local extern function or if
2409117395Skan   both are extern "C".  */
2410117395Skan
2411117395Skanstatic inline int
2412132718Skanequal_functions (tree fn1, tree fn2)
2413117395Skan{
2414117395Skan  if (DECL_LOCAL_FUNCTION_P (fn1) || DECL_LOCAL_FUNCTION_P (fn2)
2415117395Skan      || DECL_EXTERN_C_FUNCTION_P (fn1))
2416117395Skan    return decls_match (fn1, fn2);
2417117395Skan  return fn1 == fn2;
2418117395Skan}
2419117395Skan
2420132718Skan/* Print information about one overload candidate CANDIDATE.  MSGSTR
2421132718Skan   is the text to print before the candidate itself.
2422132718Skan
2423132718Skan   NOTE: Unlike most diagnostic functions in GCC, MSGSTR is expected
2424132718Skan   to have been run through gettext by the caller.  This wart makes
2425132718Skan   life simpler in print_z_candidates and for the translators.  */
2426132718Skan
242750397Sobrienstatic void
2428132718Skanprint_z_candidate (const char *msgstr, struct z_candidate *candidate)
2429132718Skan{
2430132718Skan  if (TREE_CODE (candidate->fn) == IDENTIFIER_NODE)
2431132718Skan    {
2432169689Skan      if (candidate->num_convs == 3)
2433132718Skan	inform ("%s %D(%T, %T, %T) <built-in>", msgstr, candidate->fn,
2434169689Skan		candidate->convs[0]->type,
2435169689Skan		candidate->convs[1]->type,
2436169689Skan		candidate->convs[2]->type);
2437169689Skan      else if (candidate->num_convs == 2)
2438132718Skan	inform ("%s %D(%T, %T) <built-in>", msgstr, candidate->fn,
2439169689Skan		candidate->convs[0]->type,
2440169689Skan		candidate->convs[1]->type);
2441132718Skan      else
2442132718Skan	inform ("%s %D(%T) <built-in>", msgstr, candidate->fn,
2443169689Skan		candidate->convs[0]->type);
2444132718Skan    }
2445132718Skan  else if (TYPE_P (candidate->fn))
2446132718Skan    inform ("%s %T <conversion>", msgstr, candidate->fn);
2447132718Skan  else if (candidate->viable == -1)
2448169689Skan    inform ("%s %+#D <near match>", msgstr, candidate->fn);
2449132718Skan  else
2450169689Skan    inform ("%s %+#D", msgstr, candidate->fn);
2451132718Skan}
2452132718Skan
2453132718Skanstatic void
2454117395Skanprint_z_candidates (struct z_candidate *candidates)
245550397Sobrien{
2456117395Skan  const char *str;
2457117395Skan  struct z_candidate *cand1;
2458117395Skan  struct z_candidate **cand2;
2459117395Skan
2460117395Skan  /* There may be duplicates in the set of candidates.  We put off
2461117395Skan     checking this condition as long as possible, since we have no way
2462117395Skan     to eliminate duplicates from a set of functions in less than n^2
2463117395Skan     time.  Now we are about to emit an error message, so it is more
2464117395Skan     permissible to go slowly.  */
2465117395Skan  for (cand1 = candidates; cand1; cand1 = cand1->next)
2466117395Skan    {
2467117395Skan      tree fn = cand1->fn;
2468117395Skan      /* Skip builtin candidates and conversion functions.  */
2469117395Skan      if (TREE_CODE (fn) != FUNCTION_DECL)
2470117395Skan	continue;
2471117395Skan      cand2 = &cand1->next;
2472117395Skan      while (*cand2)
2473117395Skan	{
2474117395Skan	  if (TREE_CODE ((*cand2)->fn) == FUNCTION_DECL
2475117395Skan	      && equal_functions (fn, (*cand2)->fn))
2476117395Skan	    *cand2 = (*cand2)->next;
2477117395Skan	  else
2478117395Skan	    cand2 = &(*cand2)->next;
2479117395Skan	}
2480117395Skan    }
2481117395Skan
2482132718Skan  if (!candidates)
2483132718Skan    return;
2484132718Skan
2485132718Skan  str = _("candidates are:");
2486132718Skan  print_z_candidate (str, candidates);
2487132718Skan  if (candidates->next)
248850397Sobrien    {
2489132718Skan      /* Indent successive candidates by the width of the translation
2490132718Skan	 of the above string.  */
2491132718Skan      size_t len = gcc_gettext_width (str) + 1;
2492169689Skan      char *spaces = (char *) alloca (len);
2493132718Skan      memset (spaces, ' ', len-1);
2494132718Skan      spaces[len - 1] = '\0';
2495132718Skan
2496132718Skan      candidates = candidates->next;
2497132718Skan      do
249818334Speter	{
2499132718Skan	  print_z_candidate (spaces, candidates);
2500132718Skan	  candidates = candidates->next;
250118334Speter	}
2502132718Skan      while (candidates);
250350397Sobrien    }
250450397Sobrien}
250550397Sobrien
2506117395Skan/* USER_SEQ is a user-defined conversion sequence, beginning with a
2507117395Skan   USER_CONV.  STD_SEQ is the standard conversion sequence applied to
2508117395Skan   the result of the conversion function to convert it to the final
2509169689Skan   desired type.  Merge the two sequences into a single sequence,
2510117395Skan   and return the merged sequence.  */
2511117395Skan
2512169689Skanstatic conversion *
2513169689Skanmerge_conversion_sequences (conversion *user_seq, conversion *std_seq)
2514117395Skan{
2515169689Skan  conversion **t;
2516117395Skan
2517169689Skan  gcc_assert (user_seq->kind == ck_user);
2518117395Skan
2519117395Skan  /* Find the end of the second conversion sequence.  */
2520169689Skan  t = &(std_seq);
2521169689Skan  while ((*t)->kind != ck_identity)
2522169689Skan    t = &((*t)->u.next);
2523117395Skan
2524117395Skan  /* Replace the identity conversion with the user conversion
2525117395Skan     sequence.  */
2526117395Skan  *t = user_seq;
2527117395Skan
2528117395Skan  /* The entire sequence is a user-conversion sequence.  */
2529169689Skan  std_seq->user_conv_p = true;
2530117395Skan
2531117395Skan  return std_seq;
2532117395Skan}
2533117395Skan
253450397Sobrien/* Returns the best overload candidate to perform the requested
253550397Sobrien   conversion.  This function is used for three the overloading situations
253650397Sobrien   described in [over.match.copy], [over.match.conv], and [over.match.ref].
253750397Sobrien   If TOTYPE is a REFERENCE_TYPE, we're trying to find an lvalue binding as
253850397Sobrien   per [dcl.init.ref], so we ignore temporary bindings.  */
253950397Sobrien
254050397Sobrienstatic struct z_candidate *
2541132718Skanbuild_user_type_conversion_1 (tree totype, tree expr, int flags)
254250397Sobrien{
254350397Sobrien  struct z_candidate *candidates, *cand;
254450397Sobrien  tree fromtype = TREE_TYPE (expr);
2545169689Skan  tree ctors = NULL_TREE;
2546169689Skan  tree conv_fns = NULL_TREE;
2547169689Skan  conversion *conv = NULL;
254850397Sobrien  tree args = NULL_TREE;
2549132718Skan  bool any_viable_p;
255050397Sobrien
255190075Sobrien  /* We represent conversion within a hierarchy using RVALUE_CONV and
255290075Sobrien     BASE_CONV, as specified by [over.best.ics]; these become plain
255390075Sobrien     constructor calls, as specified in [dcl.init].  */
2554169689Skan  gcc_assert (!IS_AGGR_TYPE (fromtype) || !IS_AGGR_TYPE (totype)
2555169689Skan	      || !DERIVED_FROM_P (totype, fromtype));
255690075Sobrien
255750397Sobrien  if (IS_AGGR_TYPE (totype))
2558169689Skan    ctors = lookup_fnfields (totype, complete_ctor_identifier, 0);
255990075Sobrien
256090075Sobrien  if (IS_AGGR_TYPE (fromtype))
2561169689Skan    conv_fns = lookup_conversions (fromtype);
256250397Sobrien
256350397Sobrien  candidates = 0;
256450397Sobrien  flags |= LOOKUP_NO_CONVERSION;
256550397Sobrien
256650397Sobrien  if (ctors)
256750397Sobrien    {
256890075Sobrien      tree t;
256950397Sobrien
2570117395Skan      ctors = BASELINK_FUNCTIONS (ctors);
257190075Sobrien
2572169689Skan      t = build_int_cst (build_pointer_type (totype), 0);
257390075Sobrien      args = build_tree_list (NULL_TREE, expr);
257490075Sobrien      /* We should never try to call the abstract or base constructor
257590075Sobrien	 from here.  */
2576169689Skan      gcc_assert (!DECL_HAS_IN_CHARGE_PARM_P (OVL_CURRENT (ctors))
2577169689Skan		  && !DECL_HAS_VTT_PARM_P (OVL_CURRENT (ctors)));
257890075Sobrien      args = tree_cons (NULL_TREE, t, args);
257950397Sobrien    }
258050397Sobrien  for (; ctors; ctors = OVL_NEXT (ctors))
258150397Sobrien    {
258250397Sobrien      tree ctor = OVL_CURRENT (ctors);
258350397Sobrien      if (DECL_NONCONVERTING_P (ctor))
258450397Sobrien	continue;
258550397Sobrien
2586169689Skan      if (TREE_CODE (ctor) == TEMPLATE_DECL)
2587117395Skan	cand = add_template_candidate (&candidates, ctor, totype,
2588169689Skan				       NULL_TREE, args, NULL_TREE,
2589117395Skan				       TYPE_BINFO (totype),
2590117395Skan				       TYPE_BINFO (totype),
2591117395Skan				       flags,
2592117395Skan				       DEDUCE_CALL);
2593169689Skan      else
2594117395Skan	cand = add_function_candidate (&candidates, ctor, totype,
2595169689Skan				       args, TYPE_BINFO (totype),
2596117395Skan				       TYPE_BINFO (totype),
2597169689Skan				       flags);
259850397Sobrien
2599117395Skan      if (cand)
2600169689Skan	cand->second_conv = build_identity_conv (totype, NULL_TREE);
260150397Sobrien    }
260250397Sobrien
2603169689Skan  if (conv_fns)
260490075Sobrien    args = build_tree_list (NULL_TREE, build_this (expr));
260550397Sobrien
2606169689Skan  for (; conv_fns; conv_fns = TREE_CHAIN (conv_fns))
260750397Sobrien    {
2608117395Skan      tree fns;
2609169689Skan      tree conversion_path = TREE_PURPOSE (conv_fns);
261050397Sobrien      int convflags = LOOKUP_NO_CONVERSION;
261150397Sobrien
261250397Sobrien      /* If we are called to convert to a reference type, we are trying to
261350397Sobrien	 find an lvalue binding, so don't even consider temporaries.  If
261450397Sobrien	 we don't find an lvalue binding, the caller will try again to
261550397Sobrien	 look for a temporary binding.  */
261650397Sobrien      if (TREE_CODE (totype) == REFERENCE_TYPE)
261750397Sobrien	convflags |= LOOKUP_NO_TEMP_BIND;
2618169689Skan
2619169689Skan      for (fns = TREE_VALUE (conv_fns); fns; fns = OVL_NEXT (fns))
2620117395Skan	{
2621117395Skan	  tree fn = OVL_CURRENT (fns);
2622169689Skan
2623117395Skan	  /* [over.match.funcs] For conversion functions, the function
2624117395Skan	     is considered to be a member of the class of the implicit
2625117395Skan	     object argument for the purpose of defining the type of
2626117395Skan	     the implicit object parameter.
262750397Sobrien
2628117395Skan	     So we pass fromtype as CTYPE to add_*_candidate.  */
262950397Sobrien
2630117395Skan	  if (TREE_CODE (fn) == TEMPLATE_DECL)
2631169689Skan	    cand = add_template_candidate (&candidates, fn, fromtype,
2632132718Skan					   NULL_TREE,
2633169689Skan					   args, totype,
2634169689Skan					   TYPE_BINFO (fromtype),
2635117395Skan					   conversion_path,
2636117395Skan					   flags,
2637117395Skan					   DEDUCE_CONV);
2638169689Skan	  else
2639117395Skan	    cand = add_function_candidate (&candidates, fn, fromtype,
2640117395Skan					   args,
2641117395Skan					   TYPE_BINFO (fromtype),
2642117395Skan					   conversion_path,
2643169689Skan					   flags);
264450397Sobrien
2645117395Skan	  if (cand)
2646117395Skan	    {
2647169689Skan	      conversion *ics
2648169689Skan		= implicit_conversion (totype,
2649169689Skan				       TREE_TYPE (TREE_TYPE (cand->fn)),
2650169689Skan				       0,
2651169689Skan				       /*c_cast_p=*/false, convflags);
265290075Sobrien
2653117395Skan	      cand->second_conv = ics;
2654169689Skan
2655169689Skan	      if (!ics)
2656117395Skan		cand->viable = 0;
2657169689Skan	      else if (candidates->viable == 1 && ics->bad_p)
2658117395Skan		cand->viable = -1;
2659117395Skan	    }
2660117395Skan	}
266150397Sobrien    }
266250397Sobrien
2663132718Skan  candidates = splice_viable (candidates, pedantic, &any_viable_p);
2664132718Skan  if (!any_viable_p)
2665169689Skan    return NULL;
266650397Sobrien
266750397Sobrien  cand = tourney (candidates);
266850397Sobrien  if (cand == 0)
266950397Sobrien    {
267050397Sobrien      if (flags & LOOKUP_COMPLAIN)
267150397Sobrien	{
2672169689Skan	  error ("conversion from %qT to %qT is ambiguous",
267350397Sobrien		    fromtype, totype);
267450397Sobrien	  print_z_candidates (candidates);
267550397Sobrien	}
267650397Sobrien
267750397Sobrien      cand = candidates;	/* any one will do */
2678169689Skan      cand->second_conv = build_ambiguous_conv (totype, expr);
2679169689Skan      cand->second_conv->user_conv_p = true;
2680117395Skan      if (!any_strictly_viable (candidates))
2681169689Skan	cand->second_conv->bad_p = true;
2682117395Skan      /* If there are viable candidates, don't set ICS_BAD_FLAG; an
2683117395Skan	 ambiguous conversion is no worse than another user-defined
2684117395Skan	 conversion.  */
268550397Sobrien
268650397Sobrien      return cand;
268750397Sobrien    }
268850397Sobrien
2689117395Skan  /* Build the user conversion sequence.  */
2690169689Skan  conv = build_conv
2691169689Skan    (ck_user,
269250397Sobrien     (DECL_CONSTRUCTOR_P (cand->fn)
269350397Sobrien      ? totype : non_reference (TREE_TYPE (TREE_TYPE (cand->fn)))),
2694169689Skan     build_identity_conv (TREE_TYPE (expr), expr));
2695169689Skan  conv->cand = cand;
2696117395Skan
2697117395Skan  /* Combine it with the second conversion sequence.  */
2698169689Skan  cand->second_conv = merge_conversion_sequences (conv,
2699117395Skan						  cand->second_conv);
2700117395Skan
270150397Sobrien  if (cand->viable == -1)
2702169689Skan    cand->second_conv->bad_p = true;
270350397Sobrien
270450397Sobrien  return cand;
270550397Sobrien}
270650397Sobrien
270750397Sobrientree
2708132718Skanbuild_user_type_conversion (tree totype, tree expr, int flags)
270950397Sobrien{
271050397Sobrien  struct z_candidate *cand
271150397Sobrien    = build_user_type_conversion_1 (totype, expr, flags);
271250397Sobrien
271350397Sobrien  if (cand)
271450397Sobrien    {
2715169689Skan      if (cand->second_conv->kind == ck_ambig)
271618334Speter	return error_mark_node;
2717169689Skan      expr = convert_like (cand->second_conv, expr);
2718169689Skan      return convert_from_reference (expr);
271918334Speter    }
272050397Sobrien  return NULL_TREE;
272150397Sobrien}
272218334Speter
272350397Sobrien/* Do any initial processing on the arguments to a function call.  */
272450397Sobrien
272550397Sobrienstatic tree
2726132718Skanresolve_args (tree args)
272750397Sobrien{
272850397Sobrien  tree t;
272950397Sobrien  for (t = args; t; t = TREE_CHAIN (t))
273018334Speter    {
273190075Sobrien      tree arg = TREE_VALUE (t);
2732169689Skan
2733161651Skan      if (error_operand_p (arg))
273450397Sobrien	return error_mark_node;
273590075Sobrien      else if (VOID_TYPE_P (TREE_TYPE (arg)))
273650397Sobrien	{
273750397Sobrien	  error ("invalid use of void expression");
273850397Sobrien	  return error_mark_node;
273950397Sobrien	}
2740169689Skan      else if (invalid_nonstatic_memfn_p (arg))
2741169689Skan	return error_mark_node;
274218334Speter    }
274350397Sobrien  return args;
274450397Sobrien}
2745117395Skan
2746132718Skan/* Perform overload resolution on FN, which is called with the ARGS.
2747132718Skan
2748132718Skan   Return the candidate function selected by overload resolution, or
2749132718Skan   NULL if the event that overload resolution failed.  In the case
2750132718Skan   that overload resolution fails, *CANDIDATES will be the set of
2751132718Skan   candidates considered, and ANY_VIABLE_P will be set to true or
2752132718Skan   false to indicate whether or not any of the candidates were
2753169689Skan   viable.
2754132718Skan
2755132718Skan   The ARGS should already have gone through RESOLVE_ARGS before this
2756132718Skan   function is called.  */
2757132718Skan
2758132718Skanstatic struct z_candidate *
2759169689Skanperform_overload_resolution (tree fn,
2760169689Skan			     tree args,
2761132718Skan			     struct z_candidate **candidates,
2762132718Skan			     bool *any_viable_p)
276350397Sobrien{
2764132718Skan  struct z_candidate *cand;
276550397Sobrien  tree explicit_targs = NULL_TREE;
276650397Sobrien  int template_only = 0;
276750397Sobrien
2768132718Skan  *candidates = NULL;
2769132718Skan  *any_viable_p = true;
2770132718Skan
2771117395Skan  /* Check FN and ARGS.  */
2772169689Skan  gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
2773169689Skan	      || TREE_CODE (fn) == TEMPLATE_DECL
2774169689Skan	      || TREE_CODE (fn) == OVERLOAD
2775169689Skan	      || TREE_CODE (fn) == TEMPLATE_ID_EXPR);
2776169689Skan  gcc_assert (!args || TREE_CODE (args) == TREE_LIST);
2777117395Skan
277850397Sobrien  if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
277918334Speter    {
278050397Sobrien      explicit_targs = TREE_OPERAND (fn, 1);
278150397Sobrien      fn = TREE_OPERAND (fn, 0);
278250397Sobrien      template_only = 1;
278318334Speter    }
278450397Sobrien
2785132718Skan  /* Add the various candidate functions.  */
2786132718Skan  add_candidates (fn, args, explicit_targs, template_only,
2787132718Skan		  /*conversion_path=*/NULL_TREE,
2788132718Skan		  /*access_path=*/NULL_TREE,
2789132718Skan		  LOOKUP_NORMAL,
2790132718Skan		  candidates);
2791132718Skan
2792132718Skan  *candidates = splice_viable (*candidates, pedantic, any_viable_p);
2793132718Skan  if (!*any_viable_p)
2794132718Skan    return NULL;
2795132718Skan
2796132718Skan  cand = tourney (*candidates);
2797132718Skan  return cand;
2798132718Skan}
2799132718Skan
2800132718Skan/* Return an expression for a call to FN (a namespace-scope function,
2801132718Skan   or a static member function) with the ARGS.  */
2802169689Skan
2803132718Skantree
2804169689Skanbuild_new_function_call (tree fn, tree args, bool koenig_p)
2805132718Skan{
2806132718Skan  struct z_candidate *candidates, *cand;
2807132718Skan  bool any_viable_p;
2808169689Skan  void *p;
2809169689Skan  tree result;
2810132718Skan
2811132718Skan  args = resolve_args (args);
2812132718Skan  if (args == error_mark_node)
2813132718Skan    return error_mark_node;
2814132718Skan
2815169689Skan  /* If this function was found without using argument dependent
2816169689Skan     lookup, then we want to ignore any undeclared friend
2817169689Skan     functions.  */
2818169689Skan  if (!koenig_p)
2819169689Skan    {
2820169689Skan      tree orig_fn = fn;
2821169689Skan
2822169689Skan      fn = remove_hidden_names (fn);
2823169689Skan      if (!fn)
2824169689Skan	{
2825169689Skan	  error ("no matching function for call to %<%D(%A)%>",
2826169689Skan		 DECL_NAME (OVL_CURRENT (orig_fn)), args);
2827169689Skan	  return error_mark_node;
2828169689Skan	}
2829169689Skan    }
2830169689Skan
2831169689Skan  /* Get the high-water mark for the CONVERSION_OBSTACK.  */
2832169689Skan  p = conversion_obstack_alloc (0);
2833169689Skan
2834132718Skan  cand = perform_overload_resolution (fn, args, &candidates, &any_viable_p);
2835132718Skan
2836132718Skan  if (!cand)
283718334Speter    {
2838132718Skan      if (!any_viable_p && candidates && ! candidates->next)
2839132718Skan	return build_function_call (candidates->fn, args);
2840132718Skan      if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
2841132718Skan	fn = TREE_OPERAND (fn, 0);
2842132718Skan      if (!any_viable_p)
2843169689Skan	error ("no matching function for call to %<%D(%A)%>",
2844132718Skan	       DECL_NAME (OVL_CURRENT (fn)), args);
2845132718Skan      else
2846169689Skan	error ("call of overloaded %<%D(%A)%> is ambiguous",
2847132718Skan	       DECL_NAME (OVL_CURRENT (fn)), args);
2848132718Skan      if (candidates)
2849132718Skan	print_z_candidates (candidates);
2850169689Skan      result = error_mark_node;
2851132718Skan    }
2852169689Skan  else
2853169689Skan    result = build_over_call (cand, LOOKUP_NORMAL);
285450397Sobrien
2855169689Skan  /* Free all the conversions we allocated.  */
2856169689Skan  obstack_free (&conversion_obstack, p);
2857169689Skan
2858169689Skan  return result;
2859132718Skan}
286050397Sobrien
2861132718Skan/* Build a call to a global operator new.  FNNAME is the name of the
2862132718Skan   operator (either "operator new" or "operator new[]") and ARGS are
2863132718Skan   the arguments provided.  *SIZE points to the total number of bytes
2864132718Skan   required by the allocation, and is updated if that is changed here.
2865132718Skan   *COOKIE_SIZE is non-NULL if a cookie should be used.  If this
2866132718Skan   function determines that no cookie should be used, after all,
2867169689Skan   *COOKIE_SIZE is set to NULL_TREE.  If FN is non-NULL, it will be
2868169689Skan   set, upon return, to the allocation function called.  */
286950397Sobrien
2870132718Skantree
2871169689Skanbuild_operator_new_call (tree fnname, tree args,
2872169689Skan			 tree *size, tree *cookie_size,
2873169689Skan			 tree *fn)
2874132718Skan{
2875132718Skan  tree fns;
2876132718Skan  struct z_candidate *candidates;
2877132718Skan  struct z_candidate *cand;
2878132718Skan  bool any_viable_p;
287952284Sobrien
2880169689Skan  if (fn)
2881169689Skan    *fn = NULL_TREE;
2882132718Skan  args = tree_cons (NULL_TREE, *size, args);
2883132718Skan  args = resolve_args (args);
2884132718Skan  if (args == error_mark_node)
2885132718Skan    return args;
288618334Speter
2887169689Skan  /* Based on:
288850397Sobrien
2889169689Skan       [expr.new]
2890169689Skan
2891169689Skan       If this lookup fails to find the name, or if the allocated type
2892169689Skan       is not a class type, the allocation function's name is looked
2893169689Skan       up in the global scope.
2894169689Skan
2895169689Skan     we disregard block-scope declarations of "operator new".  */
2896169689Skan  fns = lookup_function_nonclass (fnname, args, /*block_p=*/false);
2897169689Skan
2898132718Skan  /* Figure out what function is being called.  */
2899132718Skan  cand = perform_overload_resolution (fns, args, &candidates, &any_viable_p);
2900169689Skan
2901132718Skan  /* If no suitable function could be found, issue an error message
2902132718Skan     and give up.  */
2903132718Skan  if (!cand)
2904132718Skan    {
2905132718Skan      if (!any_viable_p)
2906169689Skan	error ("no matching function for call to %<%D(%A)%>",
2907132718Skan	       DECL_NAME (OVL_CURRENT (fns)), args);
2908132718Skan      else
2909169689Skan	error ("call of overloaded %<%D(%A)%> is ambiguous",
2910132718Skan	       DECL_NAME (OVL_CURRENT (fns)), args);
2911132718Skan      if (candidates)
2912132718Skan	print_z_candidates (candidates);
2913132718Skan      return error_mark_node;
291418334Speter    }
291518334Speter
2916132718Skan   /* If a cookie is required, add some extra space.  Whether
2917132718Skan      or not a cookie is required cannot be determined until
2918132718Skan      after we know which function was called.  */
2919132718Skan   if (*cookie_size)
2920132718Skan     {
2921132718Skan       bool use_cookie = true;
2922132718Skan       if (!abi_version_at_least (2))
2923132718Skan	 {
2924132718Skan	   tree placement = TREE_CHAIN (args);
2925132718Skan	   /* In G++ 3.2, the check was implemented incorrectly; it
2926132718Skan	      looked at the placement expression, rather than the
2927132718Skan	      type of the function.  */
2928132718Skan	   if (placement && !TREE_CHAIN (placement)
2929132718Skan	       && same_type_p (TREE_TYPE (TREE_VALUE (placement)),
2930132718Skan			       ptr_type_node))
2931132718Skan	     use_cookie = false;
2932132718Skan	 }
2933132718Skan       else
2934132718Skan	 {
2935132718Skan	   tree arg_types;
293618334Speter
2937132718Skan	   arg_types = TYPE_ARG_TYPES (TREE_TYPE (cand->fn));
2938132718Skan	   /* Skip the size_t parameter.  */
2939132718Skan	   arg_types = TREE_CHAIN (arg_types);
2940132718Skan	   /* Check the remaining parameters (if any).  */
2941169689Skan	   if (arg_types
2942132718Skan	       && TREE_CHAIN (arg_types) == void_list_node
2943132718Skan	       && same_type_p (TREE_VALUE (arg_types),
2944132718Skan			       ptr_type_node))
2945132718Skan	     use_cookie = false;
2946132718Skan	 }
2947132718Skan       /* If we need a cookie, adjust the number of bytes allocated.  */
2948132718Skan       if (use_cookie)
2949132718Skan	 {
2950132718Skan	   /* Update the total size.  */
2951132718Skan	   *size = size_binop (PLUS_EXPR, *size, *cookie_size);
2952132718Skan	   /* Update the argument list to reflect the adjusted size.  */
2953132718Skan	   TREE_VALUE (args) = *size;
2954132718Skan	 }
2955132718Skan       else
2956132718Skan	 *cookie_size = NULL_TREE;
2957132718Skan     }
2958132718Skan
2959169689Skan   /* Tell our caller which function we decided to call.  */
2960169689Skan   if (fn)
2961169689Skan     *fn = cand->fn;
2962169689Skan
2963132718Skan   /* Build the CALL_EXPR.  */
2964132718Skan   return build_over_call (cand, LOOKUP_NORMAL);
296550397Sobrien}
296618334Speter
296750397Sobrienstatic tree
2968132718Skanbuild_object_call (tree obj, tree args)
296950397Sobrien{
297050397Sobrien  struct z_candidate *candidates = 0, *cand;
297150397Sobrien  tree fns, convs, mem_args = NULL_TREE;
297250397Sobrien  tree type = TREE_TYPE (obj);
2973132718Skan  bool any_viable_p;
2974169689Skan  tree result = NULL_TREE;
2975169689Skan  void *p;
297618334Speter
297750397Sobrien  if (TYPE_PTRMEMFUNC_P (type))
297818334Speter    {
297950397Sobrien      /* It's no good looking for an overloaded operator() on a
298050397Sobrien	 pointer-to-member-function.  */
298190075Sobrien      error ("pointer-to-member function %E cannot be called without an object; consider using .* or ->*", obj);
298250397Sobrien      return error_mark_node;
298350397Sobrien    }
298450397Sobrien
2985169689Skan  if (TYPE_BINFO (type))
2986169689Skan    {
2987169689Skan      fns = lookup_fnfields (TYPE_BINFO (type), ansi_opname (CALL_EXPR), 1);
2988169689Skan      if (fns == error_mark_node)
2989169689Skan	return error_mark_node;
2990169689Skan    }
2991169689Skan  else
2992169689Skan    fns = NULL_TREE;
299350397Sobrien
299450397Sobrien  args = resolve_args (args);
299550397Sobrien
299650397Sobrien  if (args == error_mark_node)
299750397Sobrien    return error_mark_node;
299850397Sobrien
2999169689Skan  /* Get the high-water mark for the CONVERSION_OBSTACK.  */
3000169689Skan  p = conversion_obstack_alloc (0);
3001169689Skan
300250397Sobrien  if (fns)
300350397Sobrien    {
3004117395Skan      tree base = BINFO_TYPE (BASELINK_BINFO (fns));
300590075Sobrien      mem_args = tree_cons (NULL_TREE, build_this (obj), args);
300650397Sobrien
3007117395Skan      for (fns = BASELINK_FUNCTIONS (fns); fns; fns = OVL_NEXT (fns))
300818334Speter	{
300950397Sobrien	  tree fn = OVL_CURRENT (fns);
301050397Sobrien	  if (TREE_CODE (fn) == TEMPLATE_DECL)
3011117395Skan	    add_template_candidate (&candidates, fn, base, NULL_TREE,
3012169689Skan				    mem_args, NULL_TREE,
3013117395Skan				    TYPE_BINFO (type),
3014117395Skan				    TYPE_BINFO (type),
3015117395Skan				    LOOKUP_NORMAL, DEDUCE_CALL);
301650397Sobrien	  else
3017117395Skan	    add_function_candidate
3018117395Skan	      (&candidates, fn, base, mem_args, TYPE_BINFO (type),
3019117395Skan	       TYPE_BINFO (type), LOOKUP_NORMAL);
302018334Speter	}
302118334Speter    }
302218334Speter
302350397Sobrien  convs = lookup_conversions (type);
302418334Speter
302550397Sobrien  for (; convs; convs = TREE_CHAIN (convs))
302650397Sobrien    {
302750397Sobrien      tree fns = TREE_VALUE (convs);
302850397Sobrien      tree totype = TREE_TYPE (TREE_TYPE (OVL_CURRENT (fns)));
302918334Speter
303052284Sobrien      if ((TREE_CODE (totype) == POINTER_TYPE
303190075Sobrien	   && TREE_CODE (TREE_TYPE (totype)) == FUNCTION_TYPE)
303290075Sobrien	  || (TREE_CODE (totype) == REFERENCE_TYPE
303390075Sobrien	      && TREE_CODE (TREE_TYPE (totype)) == FUNCTION_TYPE)
303490075Sobrien	  || (TREE_CODE (totype) == REFERENCE_TYPE
303590075Sobrien	      && TREE_CODE (TREE_TYPE (totype)) == POINTER_TYPE
303690075Sobrien	      && TREE_CODE (TREE_TYPE (TREE_TYPE (totype))) == FUNCTION_TYPE))
303752284Sobrien	for (; fns; fns = OVL_NEXT (fns))
303850397Sobrien	  {
303952284Sobrien	    tree fn = OVL_CURRENT (fns);
3040169689Skan	    if (TREE_CODE (fn) == TEMPLATE_DECL)
3041169689Skan	      add_template_conv_candidate
3042117395Skan		(&candidates, fn, obj, args, totype,
3043117395Skan		 /*access_path=*/NULL_TREE,
3044117395Skan		 /*conversion_path=*/NULL_TREE);
304550397Sobrien	    else
3046117395Skan	      add_conv_candidate (&candidates, fn, obj, args,
3047117395Skan				  /*conversion_path=*/NULL_TREE,
3048117395Skan				  /*access_path=*/NULL_TREE);
304950397Sobrien	  }
305050397Sobrien    }
305150397Sobrien
3052132718Skan  candidates = splice_viable (candidates, pedantic, &any_viable_p);
3053132718Skan  if (!any_viable_p)
305418334Speter    {
3055169689Skan      error ("no match for call to %<(%T) (%A)%>", TREE_TYPE (obj), args);
305650397Sobrien      print_z_candidates (candidates);
3057169689Skan      result = error_mark_node;
305850397Sobrien    }
3059169689Skan  else
306050397Sobrien    {
3061169689Skan      cand = tourney (candidates);
3062169689Skan      if (cand == 0)
3063169689Skan	{
3064169689Skan	  error ("call of %<(%T) (%A)%> is ambiguous", TREE_TYPE (obj), args);
3065169689Skan	  print_z_candidates (candidates);
3066169689Skan	  result = error_mark_node;
3067169689Skan	}
3068169689Skan      /* Since cand->fn will be a type, not a function, for a conversion
3069169689Skan	 function, we must be careful not to unconditionally look at
3070169689Skan	 DECL_NAME here.  */
3071169689Skan      else if (TREE_CODE (cand->fn) == FUNCTION_DECL
3072169689Skan	       && DECL_OVERLOADED_OPERATOR_P (cand->fn) == CALL_EXPR)
3073169689Skan	result = build_over_call (cand, LOOKUP_NORMAL);
3074169689Skan      else
3075169689Skan	{
3076169689Skan	  obj = convert_like_with_context (cand->convs[0], obj, cand->fn, -1);
3077169689Skan	  obj = convert_from_reference (obj);
3078169689Skan	  result = build_function_call (obj, args);
3079169689Skan	}
308050397Sobrien    }
308118334Speter
3082169689Skan  /* Free all the conversions we allocated.  */
3083169689Skan  obstack_free (&conversion_obstack, p);
308418334Speter
3085169689Skan  return result;
308650397Sobrien}
308718334Speter
308850397Sobrienstatic void
3089132718Skanop_error (enum tree_code code, enum tree_code code2,
3090169689Skan	  tree arg1, tree arg2, tree arg3, const char *problem)
309150397Sobrien{
309290075Sobrien  const char *opname;
309318334Speter
309490075Sobrien  if (code == MODIFY_EXPR)
309590075Sobrien    opname = assignment_operator_name_info[code2].name;
309690075Sobrien  else
309790075Sobrien    opname = operator_name_info[code].name;
309890075Sobrien
309950397Sobrien  switch (code)
310050397Sobrien    {
310150397Sobrien    case COND_EXPR:
3102169689Skan      error ("%s for ternary %<operator?:%> in %<%E ? %E : %E%>",
3103169689Skan	     problem, arg1, arg2, arg3);
310450397Sobrien      break;
3105169689Skan
310650397Sobrien    case POSTINCREMENT_EXPR:
310750397Sobrien    case POSTDECREMENT_EXPR:
3108169689Skan      error ("%s for %<operator%s%> in %<%E%s%>", problem, opname, arg1, opname);
310950397Sobrien      break;
3110169689Skan
311150397Sobrien    case ARRAY_REF:
3112169689Skan      error ("%s for %<operator[]%> in %<%E[%E]%>", problem, arg1, arg2);
311350397Sobrien      break;
3114132718Skan
3115132718Skan    case REALPART_EXPR:
3116132718Skan    case IMAGPART_EXPR:
3117169689Skan      error ("%s for %qs in %<%s %E%>", problem, opname, opname, arg1);
3118132718Skan      break;
3119169689Skan
312050397Sobrien    default:
312150397Sobrien      if (arg2)
3122169689Skan	error ("%s for %<operator%s%> in %<%E %s %E%>",
3123169689Skan	       problem, opname, arg1, opname, arg2);
312450397Sobrien      else
3125169689Skan	error ("%s for %<operator%s%> in %<%s%E%>",
3126169689Skan	       problem, opname, opname, arg1);
3127117395Skan      break;
312850397Sobrien    }
312950397Sobrien}
313018334Speter
313190075Sobrien/* Return the implicit conversion sequence that could be used to
313290075Sobrien   convert E1 to E2 in [expr.cond].  */
313390075Sobrien
3134169689Skanstatic conversion *
3135132718Skanconditional_conversion (tree e1, tree e2)
313690075Sobrien{
313790075Sobrien  tree t1 = non_reference (TREE_TYPE (e1));
313890075Sobrien  tree t2 = non_reference (TREE_TYPE (e2));
3139169689Skan  conversion *conv;
3140117395Skan  bool good_base;
314190075Sobrien
314290075Sobrien  /* [expr.cond]
314390075Sobrien
314490075Sobrien     If E2 is an lvalue: E1 can be converted to match E2 if E1 can be
314590075Sobrien     implicitly converted (clause _conv_) to the type "reference to
314690075Sobrien     T2", subject to the constraint that in the conversion the
314790075Sobrien     reference must bind directly (_dcl.init.ref_) to E1.  */
314890075Sobrien  if (real_lvalue_p (e2))
314990075Sobrien    {
3150169689Skan      conv = implicit_conversion (build_reference_type (t2),
315190075Sobrien				  t1,
315290075Sobrien				  e1,
3153169689Skan				  /*c_cast_p=*/false,
315490075Sobrien				  LOOKUP_NO_TEMP_BIND);
315590075Sobrien      if (conv)
315690075Sobrien	return conv;
315790075Sobrien    }
315890075Sobrien
315990075Sobrien  /* [expr.cond]
316090075Sobrien
316190075Sobrien     If E1 and E2 have class type, and the underlying class types are
316290075Sobrien     the same or one is a base class of the other: E1 can be converted
316390075Sobrien     to match E2 if the class of T2 is the same type as, or a base
316490075Sobrien     class of, the class of T1, and the cv-qualification of T2 is the
316590075Sobrien     same cv-qualification as, or a greater cv-qualification than, the
316690075Sobrien     cv-qualification of T1.  If the conversion is applied, E1 is
316790075Sobrien     changed to an rvalue of type T2 that still refers to the original
316890075Sobrien     source class object (or the appropriate subobject thereof).  */
316990075Sobrien  if (CLASS_TYPE_P (t1) && CLASS_TYPE_P (t2)
3170117395Skan      && ((good_base = DERIVED_FROM_P (t2, t1)) || DERIVED_FROM_P (t1, t2)))
317190075Sobrien    {
3172117395Skan      if (good_base && at_least_as_qualified_p (t2, t1))
317390075Sobrien	{
3174169689Skan	  conv = build_identity_conv (t1, e1);
3175169689Skan	  if (!same_type_p (TYPE_MAIN_VARIANT (t1),
317690075Sobrien			    TYPE_MAIN_VARIANT (t2)))
3177169689Skan	    conv = build_conv (ck_base, t2, conv);
3178132718Skan	  else
3179169689Skan	    conv = build_conv (ck_rvalue, t2, conv);
318090075Sobrien	  return conv;
318190075Sobrien	}
318290075Sobrien      else
3183169689Skan	return NULL;
318490075Sobrien    }
3185117395Skan  else
3186117395Skan    /* [expr.cond]
318790075Sobrien
3188117395Skan       Otherwise: E1 can be converted to match E2 if E1 can be implicitly
3189117395Skan       converted to the type that expression E2 would have if E2 were
3190117395Skan       converted to an rvalue (or the type it has, if E2 is an rvalue).  */
3191169689Skan    return implicit_conversion (t2, t1, e1, /*c_cast_p=*/false,
3192169689Skan				LOOKUP_NORMAL);
319390075Sobrien}
319490075Sobrien
319590075Sobrien/* Implement [expr.cond].  ARG1, ARG2, and ARG3 are the three
3196117395Skan   arguments to the conditional expression.  */
319790075Sobrien
319850397Sobrientree
3199132718Skanbuild_conditional_expr (tree arg1, tree arg2, tree arg3)
320090075Sobrien{
320190075Sobrien  tree arg2_type;
320290075Sobrien  tree arg3_type;
3203169689Skan  tree result = NULL_TREE;
320490075Sobrien  tree result_type = NULL_TREE;
3205132718Skan  bool lvalue_p = true;
320690075Sobrien  struct z_candidate *candidates = 0;
320790075Sobrien  struct z_candidate *cand;
3208169689Skan  void *p;
320990075Sobrien
321090075Sobrien  /* As a G++ extension, the second argument to the conditional can be
321190075Sobrien     omitted.  (So that `a ? : c' is roughly equivalent to `a ? a :
321290075Sobrien     c'.)  If the second operand is omitted, make sure it is
321390075Sobrien     calculated only once.  */
321490075Sobrien  if (!arg2)
321590075Sobrien    {
321690075Sobrien      if (pedantic)
321790075Sobrien	pedwarn ("ISO C++ forbids omitting the middle term of a ?: expression");
3218132718Skan
3219132718Skan      /* Make sure that lvalues remain lvalues.  See g++.oliva/ext1.C.  */
3220132718Skan      if (real_lvalue_p (arg1))
3221132718Skan	arg2 = arg1 = stabilize_reference (arg1);
3222132718Skan      else
3223132718Skan	arg2 = arg1 = save_expr (arg1);
322490075Sobrien    }
322590075Sobrien
322690075Sobrien  /* [expr.cond]
3227169689Skan
322890075Sobrien     The first expr ession is implicitly converted to bool (clause
322990075Sobrien     _conv_).  */
3230132718Skan  arg1 = perform_implicit_conversion (boolean_type_node, arg1);
323190075Sobrien
323290075Sobrien  /* If something has already gone wrong, just pass that fact up the
323390075Sobrien     tree.  */
3234132718Skan  if (error_operand_p (arg1)
3235132718Skan      || error_operand_p (arg2)
3236132718Skan      || error_operand_p (arg3))
323790075Sobrien    return error_mark_node;
323890075Sobrien
323990075Sobrien  /* [expr.cond]
324090075Sobrien
324190075Sobrien     If either the second or the third operand has type (possibly
324290075Sobrien     cv-qualified) void, then the lvalue-to-rvalue (_conv.lval_),
324390075Sobrien     array-to-pointer (_conv.array_), and function-to-pointer
324490075Sobrien     (_conv.func_) standard conversions are performed on the second
324590075Sobrien     and third operands.  */
3246169689Skan  arg2_type = unlowered_expr_type (arg2);
3247169689Skan  arg3_type = unlowered_expr_type (arg3);
324890075Sobrien  if (VOID_TYPE_P (arg2_type) || VOID_TYPE_P (arg3_type))
324990075Sobrien    {
325090075Sobrien      /* Do the conversions.  We don't these for `void' type arguments
325190075Sobrien	 since it can't have any effect and since decay_conversion
325290075Sobrien	 does not handle that case gracefully.  */
325390075Sobrien      if (!VOID_TYPE_P (arg2_type))
325490075Sobrien	arg2 = decay_conversion (arg2);
325590075Sobrien      if (!VOID_TYPE_P (arg3_type))
325690075Sobrien	arg3 = decay_conversion (arg3);
325790075Sobrien      arg2_type = TREE_TYPE (arg2);
325890075Sobrien      arg3_type = TREE_TYPE (arg3);
325990075Sobrien
326090075Sobrien      /* [expr.cond]
326190075Sobrien
326290075Sobrien	 One of the following shall hold:
326390075Sobrien
326490075Sobrien	 --The second or the third operand (but not both) is a
326590075Sobrien	   throw-expression (_except.throw_); the result is of the
326690075Sobrien	   type of the other and is an rvalue.
326790075Sobrien
326890075Sobrien	 --Both the second and the third operands have type void; the
3269169689Skan	   result is of type void and is an rvalue.
3270132718Skan
3271169689Skan	 We must avoid calling force_rvalue for expressions of type
3272132718Skan	 "void" because it will complain that their value is being
3273169689Skan	 used.  */
3274169689Skan      if (TREE_CODE (arg2) == THROW_EXPR
3275132718Skan	  && TREE_CODE (arg3) != THROW_EXPR)
3276132718Skan	{
3277132718Skan	  if (!VOID_TYPE_P (arg3_type))
3278132718Skan	    arg3 = force_rvalue (arg3);
3279132718Skan	  arg3_type = TREE_TYPE (arg3);
3280132718Skan	  result_type = arg3_type;
3281132718Skan	}
3282169689Skan      else if (TREE_CODE (arg2) != THROW_EXPR
3283132718Skan	       && TREE_CODE (arg3) == THROW_EXPR)
3284132718Skan	{
3285132718Skan	  if (!VOID_TYPE_P (arg2_type))
3286132718Skan	    arg2 = force_rvalue (arg2);
3287132718Skan	  arg2_type = TREE_TYPE (arg2);
3288132718Skan	  result_type = arg2_type;
3289132718Skan	}
329090075Sobrien      else if (VOID_TYPE_P (arg2_type) && VOID_TYPE_P (arg3_type))
329190075Sobrien	result_type = void_type_node;
329290075Sobrien      else
329390075Sobrien	{
3294169689Skan	  error ("%qE has type %<void%> and is not a throw-expression",
329590075Sobrien		    VOID_TYPE_P (arg2_type) ? arg2 : arg3);
329690075Sobrien	  return error_mark_node;
329790075Sobrien	}
329890075Sobrien
3299132718Skan      lvalue_p = false;
330090075Sobrien      goto valid_operands;
330190075Sobrien    }
330290075Sobrien  /* [expr.cond]
330390075Sobrien
330490075Sobrien     Otherwise, if the second and third operand have different types,
330590075Sobrien     and either has (possibly cv-qualified) class type, an attempt is
330690075Sobrien     made to convert each of those operands to the type of the other.  */
330790075Sobrien  else if (!same_type_p (arg2_type, arg3_type)
330890075Sobrien	   && (CLASS_TYPE_P (arg2_type) || CLASS_TYPE_P (arg3_type)))
330990075Sobrien    {
3310169689Skan      conversion *conv2;
3311169689Skan      conversion *conv3;
3312169689Skan
3313169689Skan      /* Get the high-water mark for the CONVERSION_OBSTACK.  */
3314169689Skan      p = conversion_obstack_alloc (0);
3315169689Skan
3316169689Skan      conv2 = conditional_conversion (arg2, arg3);
3317169689Skan      conv3 = conditional_conversion (arg3, arg2);
3318169689Skan
331990075Sobrien      /* [expr.cond]
332090075Sobrien
332190075Sobrien	 If both can be converted, or one can be converted but the
332290075Sobrien	 conversion is ambiguous, the program is ill-formed.  If
332390075Sobrien	 neither can be converted, the operands are left unchanged and
332490075Sobrien	 further checking is performed as described below.  If exactly
332590075Sobrien	 one conversion is possible, that conversion is applied to the
332690075Sobrien	 chosen operand and the converted operand is used in place of
332790075Sobrien	 the original operand for the remainder of this section.  */
3328169689Skan      if ((conv2 && !conv2->bad_p
3329169689Skan	   && conv3 && !conv3->bad_p)
3330169689Skan	  || (conv2 && conv2->kind == ck_ambig)
3331169689Skan	  || (conv3 && conv3->kind == ck_ambig))
333290075Sobrien	{
3333169689Skan	  error ("operands to ?: have different types %qT and %qT",
3334169689Skan		 arg2_type, arg3_type);
3335169689Skan	  result = error_mark_node;
333690075Sobrien	}
3337169689Skan      else if (conv2 && (!conv2->bad_p || !conv3))
333890075Sobrien	{
333990075Sobrien	  arg2 = convert_like (conv2, arg2);
334090075Sobrien	  arg2 = convert_from_reference (arg2);
334190075Sobrien	  arg2_type = TREE_TYPE (arg2);
3342169689Skan	  /* Even if CONV2 is a valid conversion, the result of the
3343169689Skan	     conversion may be invalid.  For example, if ARG3 has type
3344169689Skan	     "volatile X", and X does not have a copy constructor
3345169689Skan	     accepting a "volatile X&", then even if ARG2 can be
3346169689Skan	     converted to X, the conversion will fail.  */
3347169689Skan	  if (error_operand_p (arg2))
3348169689Skan	    result = error_mark_node;
334990075Sobrien	}
3350169689Skan      else if (conv3 && (!conv3->bad_p || !conv2))
335190075Sobrien	{
335290075Sobrien	  arg3 = convert_like (conv3, arg3);
335390075Sobrien	  arg3 = convert_from_reference (arg3);
335490075Sobrien	  arg3_type = TREE_TYPE (arg3);
3355169689Skan	  if (error_operand_p (arg3))
3356169689Skan	    result = error_mark_node;
335790075Sobrien	}
3358132718Skan
3359169689Skan      /* Free all the conversions we allocated.  */
3360169689Skan      obstack_free (&conversion_obstack, p);
3361169689Skan
3362169689Skan      if (result)
3363169689Skan	return result;
3364169689Skan
3365132718Skan      /* If, after the conversion, both operands have class type,
3366132718Skan	 treat the cv-qualification of both operands as if it were the
3367169689Skan	 union of the cv-qualification of the operands.
3368132718Skan
3369132718Skan	 The standard is not clear about what to do in this
3370132718Skan	 circumstance.  For example, if the first operand has type
3371132718Skan	 "const X" and the second operand has a user-defined
3372132718Skan	 conversion to "volatile X", what is the type of the second
3373132718Skan	 operand after this step?  Making it be "const X" (matching
3374132718Skan	 the first operand) seems wrong, as that discards the
3375169689Skan	 qualification without actually performing a copy.  Leaving it
3376132718Skan	 as "volatile X" seems wrong as that will result in the
3377132718Skan	 conditional expression failing altogether, even though,
3378132718Skan	 according to this step, the one operand could be converted to
3379132718Skan	 the type of the other.  */
3380132718Skan      if ((conv2 || conv3)
3381132718Skan	  && CLASS_TYPE_P (arg2_type)
3382132718Skan	  && TYPE_QUALS (arg2_type) != TYPE_QUALS (arg3_type))
3383169689Skan	arg2_type = arg3_type =
3384132718Skan	  cp_build_qualified_type (arg2_type,
3385132718Skan				   TYPE_QUALS (arg2_type)
3386132718Skan				   | TYPE_QUALS (arg3_type));
338790075Sobrien    }
338890075Sobrien
338990075Sobrien  /* [expr.cond]
339090075Sobrien
339190075Sobrien     If the second and third operands are lvalues and have the same
339290075Sobrien     type, the result is of that type and is an lvalue.  */
3393169689Skan  if (real_lvalue_p (arg2)
3394169689Skan      && real_lvalue_p (arg3)
3395132718Skan      && same_type_p (arg2_type, arg3_type))
339690075Sobrien    {
339790075Sobrien      result_type = arg2_type;
339890075Sobrien      goto valid_operands;
339990075Sobrien    }
340090075Sobrien
340190075Sobrien  /* [expr.cond]
340290075Sobrien
340390075Sobrien     Otherwise, the result is an rvalue.  If the second and third
340490075Sobrien     operand do not have the same type, and either has (possibly
340590075Sobrien     cv-qualified) class type, overload resolution is used to
340690075Sobrien     determine the conversions (if any) to be applied to the operands
340790075Sobrien     (_over.match.oper_, _over.built_).  */
3408132718Skan  lvalue_p = false;
340990075Sobrien  if (!same_type_p (arg2_type, arg3_type)
341090075Sobrien      && (CLASS_TYPE_P (arg2_type) || CLASS_TYPE_P (arg3_type)))
341190075Sobrien    {
341290075Sobrien      tree args[3];
3413169689Skan      conversion *conv;
3414132718Skan      bool any_viable_p;
341590075Sobrien
341690075Sobrien      /* Rearrange the arguments so that add_builtin_candidate only has
341790075Sobrien	 to know about two args.  In build_builtin_candidates, the
341890075Sobrien	 arguments are unscrambled.  */
341990075Sobrien      args[0] = arg2;
342090075Sobrien      args[1] = arg3;
342190075Sobrien      args[2] = arg1;
3422169689Skan      add_builtin_candidates (&candidates,
3423169689Skan			      COND_EXPR,
3424117395Skan			      NOP_EXPR,
3425117395Skan			      ansi_opname (COND_EXPR),
3426117395Skan			      args,
3427117395Skan			      LOOKUP_NORMAL);
342890075Sobrien
342990075Sobrien      /* [expr.cond]
343090075Sobrien
343190075Sobrien	 If the overload resolution fails, the program is
343290075Sobrien	 ill-formed.  */
3433132718Skan      candidates = splice_viable (candidates, pedantic, &any_viable_p);
3434132718Skan      if (!any_viable_p)
343590075Sobrien	{
343690075Sobrien	  op_error (COND_EXPR, NOP_EXPR, arg1, arg2, arg3, "no match");
343790075Sobrien	  print_z_candidates (candidates);
343890075Sobrien	  return error_mark_node;
343990075Sobrien	}
344090075Sobrien      cand = tourney (candidates);
344190075Sobrien      if (!cand)
344290075Sobrien	{
344390075Sobrien	  op_error (COND_EXPR, NOP_EXPR, arg1, arg2, arg3, "no match");
344490075Sobrien	  print_z_candidates (candidates);
344590075Sobrien	  return error_mark_node;
344690075Sobrien	}
344790075Sobrien
344890075Sobrien      /* [expr.cond]
344990075Sobrien
345090075Sobrien	 Otherwise, the conversions thus determined are applied, and
345190075Sobrien	 the converted operands are used in place of the original
345290075Sobrien	 operands for the remainder of this section.  */
3453169689Skan      conv = cand->convs[0];
345490075Sobrien      arg1 = convert_like (conv, arg1);
3455169689Skan      conv = cand->convs[1];
345690075Sobrien      arg2 = convert_like (conv, arg2);
3457169689Skan      conv = cand->convs[2];
345890075Sobrien      arg3 = convert_like (conv, arg3);
345990075Sobrien    }
346090075Sobrien
346190075Sobrien  /* [expr.cond]
346290075Sobrien
346390075Sobrien     Lvalue-to-rvalue (_conv.lval_), array-to-pointer (_conv.array_),
346490075Sobrien     and function-to-pointer (_conv.func_) standard conversions are
346590075Sobrien     performed on the second and third operands.
346690075Sobrien
346790075Sobrien     We need to force the lvalue-to-rvalue conversion here for class types,
346890075Sobrien     so we get TARGET_EXPRs; trying to deal with a COND_EXPR of class rvalues
346990075Sobrien     that isn't wrapped with a TARGET_EXPR plays havoc with exception
3470132718Skan     regions.  */
347190075Sobrien
3472117395Skan  arg2 = force_rvalue (arg2);
3473132718Skan  if (!CLASS_TYPE_P (arg2_type))
3474132718Skan    arg2_type = TREE_TYPE (arg2);
347590075Sobrien
3476117395Skan  arg3 = force_rvalue (arg3);
3477132718Skan  if (!CLASS_TYPE_P (arg2_type))
3478132718Skan    arg3_type = TREE_TYPE (arg3);
347990075Sobrien
348090075Sobrien  if (arg2 == error_mark_node || arg3 == error_mark_node)
348190075Sobrien    return error_mark_node;
3482169689Skan
348390075Sobrien  /* [expr.cond]
3484169689Skan
348590075Sobrien     After those conversions, one of the following shall hold:
348690075Sobrien
348790075Sobrien     --The second and third operands have the same type; the result  is  of
348890075Sobrien       that type.  */
348990075Sobrien  if (same_type_p (arg2_type, arg3_type))
349090075Sobrien    result_type = arg2_type;
349190075Sobrien  /* [expr.cond]
349290075Sobrien
349390075Sobrien     --The second and third operands have arithmetic or enumeration
349490075Sobrien       type; the usual arithmetic conversions are performed to bring
349590075Sobrien       them to a common type, and the result is of that type.  */
3496169689Skan  else if ((ARITHMETIC_TYPE_P (arg2_type)
349790075Sobrien	    || TREE_CODE (arg2_type) == ENUMERAL_TYPE)
349890075Sobrien	   && (ARITHMETIC_TYPE_P (arg3_type)
349990075Sobrien	       || TREE_CODE (arg3_type) == ENUMERAL_TYPE))
350090075Sobrien    {
350190075Sobrien      /* In this case, there is always a common type.  */
3502169689Skan      result_type = type_after_usual_arithmetic_conversions (arg2_type,
350390075Sobrien							     arg3_type);
3504169689Skan
350590075Sobrien      if (TREE_CODE (arg2_type) == ENUMERAL_TYPE
3506169689Skan	  && TREE_CODE (arg3_type) == ENUMERAL_TYPE)
3507169689Skan	 warning (0, "enumeral mismatch in conditional expression: %qT vs %qT",
3508169689Skan		   arg2_type, arg3_type);
350990075Sobrien      else if (extra_warnings
3510169689Skan	       && ((TREE_CODE (arg2_type) == ENUMERAL_TYPE
3511169689Skan		    && !same_type_p (arg3_type, type_promotes_to (arg2_type)))
3512169689Skan		   || (TREE_CODE (arg3_type) == ENUMERAL_TYPE
3513169689Skan		       && !same_type_p (arg2_type, type_promotes_to (arg3_type)))))
3514169689Skan	warning (0, "enumeral and non-enumeral type in conditional expression");
3515169689Skan
351690075Sobrien      arg2 = perform_implicit_conversion (result_type, arg2);
351790075Sobrien      arg3 = perform_implicit_conversion (result_type, arg3);
351890075Sobrien    }
351990075Sobrien  /* [expr.cond]
352090075Sobrien
352190075Sobrien     --The second and third operands have pointer type, or one has
352290075Sobrien       pointer type and the other is a null pointer constant; pointer
352390075Sobrien       conversions (_conv.ptr_) and qualification conversions
352490075Sobrien       (_conv.qual_) are performed to bring them to their composite
352590075Sobrien       pointer type (_expr.rel_).  The result is of the composite
352690075Sobrien       pointer type.
352790075Sobrien
352890075Sobrien     --The second and third operands have pointer to member type, or
352990075Sobrien       one has pointer to member type and the other is a null pointer
353090075Sobrien       constant; pointer to member conversions (_conv.mem_) and
353190075Sobrien       qualification conversions (_conv.qual_) are performed to bring
353290075Sobrien       them to a common type, whose cv-qualification shall match the
353390075Sobrien       cv-qualification of either the second or the third operand.
3534117395Skan       The result is of the common type.  */
3535169689Skan  else if ((null_ptr_cst_p (arg2)
3536261188Spfg	   /* APPLE LOCAL begin blocks 6040305 (co) */
3537261188Spfg	    && (TYPE_PTR_P (arg3_type) || TYPE_PTR_TO_MEMBER_P (arg3_type)
3538261188Spfg		|| TREE_CODE (arg3_type) == BLOCK_POINTER_TYPE))
3539261188Spfg	   /* APPLE LOCAL end blocks 6040305 (co) */
3540169689Skan	   || (null_ptr_cst_p (arg3)
3541261188Spfg	   /* APPLE LOCAL begin blocks 6040305 (co) */
3542261188Spfg	       && (TYPE_PTR_P (arg2_type) || TYPE_PTR_TO_MEMBER_P (arg2_type)
3543261188Spfg		   || TREE_CODE (arg2_type) == BLOCK_POINTER_TYPE))
3544261188Spfg	   || ((TYPE_PTR_P (arg2_type)
3545261188Spfg		||  TREE_CODE (arg2_type) == BLOCK_POINTER_TYPE)
3546261188Spfg	       && (TYPE_PTR_P (arg3_type)
3547261188Spfg		   || TREE_CODE (arg3_type) == BLOCK_POINTER_TYPE))
3548261188Spfg	   /* APPLE LOCAL end blocks 6040305 (co) */
354990075Sobrien	   || (TYPE_PTRMEM_P (arg2_type) && TYPE_PTRMEM_P (arg3_type))
3550132718Skan	   || (TYPE_PTRMEMFUNC_P (arg2_type) && TYPE_PTRMEMFUNC_P (arg3_type)))
355190075Sobrien    {
355290075Sobrien      result_type = composite_pointer_type (arg2_type, arg3_type, arg2,
355390075Sobrien					    arg3, "conditional expression");
3554132718Skan      if (result_type == error_mark_node)
3555132718Skan	return error_mark_node;
355690075Sobrien      arg2 = perform_implicit_conversion (result_type, arg2);
355790075Sobrien      arg3 = perform_implicit_conversion (result_type, arg3);
355890075Sobrien    }
355990075Sobrien
356090075Sobrien  if (!result_type)
356190075Sobrien    {
3562169689Skan      error ("operands to ?: have different types %qT and %qT",
3563169689Skan	     arg2_type, arg3_type);
356490075Sobrien      return error_mark_node;
356590075Sobrien    }
356690075Sobrien
356790075Sobrien valid_operands:
3568169689Skan  result = fold_if_not_in_template (build3 (COND_EXPR, result_type, arg1,
3569169689Skan					    arg2, arg3));
3570132718Skan  /* We can't use result_type below, as fold might have returned a
3571132718Skan     throw_expr.  */
3572132718Skan
3573169689Skan  if (!lvalue_p)
3574169689Skan    {
3575169689Skan      /* Expand both sides into the same slot, hopefully the target of
3576169689Skan	 the ?: expression.  We used to check for TARGET_EXPRs here,
3577169689Skan	 but now we sometimes wrap them in NOP_EXPRs so the test would
3578169689Skan	 fail.  */
3579169689Skan      if (CLASS_TYPE_P (TREE_TYPE (result)))
3580169689Skan	result = get_target_expr (result);
3581169689Skan      /* If this expression is an rvalue, but might be mistaken for an
3582169689Skan	 lvalue, we must add a NON_LVALUE_EXPR.  */
3583169689Skan      result = rvalue (result);
3584169689Skan    }
358590075Sobrien
358690075Sobrien  return result;
358790075Sobrien}
358890075Sobrien
3589132718Skan/* OPERAND is an operand to an expression.  Perform necessary steps
3590132718Skan   required before using it.  If OPERAND is NULL_TREE, NULL_TREE is
3591132718Skan   returned.  */
3592132718Skan
3593132718Skanstatic tree
3594132718Skanprep_operand (tree operand)
3595132718Skan{
3596132718Skan  if (operand)
3597132718Skan    {
3598132718Skan      if (CLASS_TYPE_P (TREE_TYPE (operand))
3599132718Skan	  && CLASSTYPE_TEMPLATE_INSTANTIATION (TREE_TYPE (operand)))
3600132718Skan	/* Make sure the template type is instantiated now.  */
3601132718Skan	instantiate_class_template (TYPE_MAIN_VARIANT (TREE_TYPE (operand)));
3602132718Skan    }
3603132718Skan
3604132718Skan  return operand;
3605132718Skan}
3606132718Skan
3607132718Skan/* Add each of the viable functions in FNS (a FUNCTION_DECL or
3608132718Skan   OVERLOAD) to the CANDIDATES, returning an updated list of
3609132718Skan   CANDIDATES.  The ARGS are the arguments provided to the call,
3610132718Skan   without any implicit object parameter.  The EXPLICIT_TARGS are
3611132718Skan   explicit template arguments provided.  TEMPLATE_ONLY is true if
3612132718Skan   only template functions should be considered.  CONVERSION_PATH,
3613132718Skan   ACCESS_PATH, and FLAGS are as for add_function_candidate.  */
3614132718Skan
3615132718Skanstatic void
3616169689Skanadd_candidates (tree fns, tree args,
3617132718Skan		tree explicit_targs, bool template_only,
3618132718Skan		tree conversion_path, tree access_path,
3619132718Skan		int flags,
3620132718Skan		struct z_candidate **candidates)
3621132718Skan{
3622132718Skan  tree ctype;
3623132718Skan  tree non_static_args;
3624132718Skan
3625132718Skan  ctype = conversion_path ? BINFO_TYPE (conversion_path) : NULL_TREE;
3626132718Skan  /* Delay creating the implicit this parameter until it is needed.  */
3627132718Skan  non_static_args = NULL_TREE;
3628132718Skan
3629169689Skan  while (fns)
3630132718Skan    {
3631132718Skan      tree fn;
3632132718Skan      tree fn_args;
3633132718Skan
3634132718Skan      fn = OVL_CURRENT (fns);
3635132718Skan      /* Figure out which set of arguments to use.  */
3636132718Skan      if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
3637132718Skan	{
3638132718Skan	  /* If this function is a non-static member, prepend the implicit
3639132718Skan	     object parameter.  */
3640132718Skan	  if (!non_static_args)
3641132718Skan	    non_static_args = tree_cons (NULL_TREE,
3642132718Skan					 build_this (TREE_VALUE (args)),
3643132718Skan					 TREE_CHAIN (args));
3644132718Skan	  fn_args = non_static_args;
3645132718Skan	}
3646132718Skan      else
3647132718Skan	/* Otherwise, just use the list of arguments provided.  */
3648132718Skan	fn_args = args;
3649132718Skan
3650132718Skan      if (TREE_CODE (fn) == TEMPLATE_DECL)
3651169689Skan	add_template_candidate (candidates,
3652169689Skan				fn,
3653132718Skan				ctype,
3654132718Skan				explicit_targs,
3655132718Skan				fn_args,
3656132718Skan				NULL_TREE,
3657132718Skan				access_path,
3658132718Skan				conversion_path,
3659132718Skan				flags,
3660132718Skan				DEDUCE_CALL);
3661132718Skan      else if (!template_only)
3662132718Skan	add_function_candidate (candidates,
3663132718Skan				fn,
3664132718Skan				ctype,
3665132718Skan				fn_args,
3666132718Skan				access_path,
3667132718Skan				conversion_path,
3668132718Skan				flags);
3669132718Skan      fns = OVL_NEXT (fns);
3670132718Skan    }
3671132718Skan}
3672132718Skan
367390075Sobrientree
3674132718Skanbuild_new_op (enum tree_code code, int flags, tree arg1, tree arg2, tree arg3,
3675132718Skan	      bool *overloaded_p)
367650397Sobrien{
367750397Sobrien  struct z_candidate *candidates = 0, *cand;
3678132718Skan  tree arglist, fnname;
3679132718Skan  tree args[3];
3680169689Skan  tree result = NULL_TREE;
3681169689Skan  bool result_valid_p = false;
368250397Sobrien  enum tree_code code2 = NOP_EXPR;
3683169689Skan  conversion *conv;
3684169689Skan  void *p;
3685132718Skan  bool strict_p;
3686132718Skan  bool any_viable_p;
368718334Speter
3688169689Skan  if (error_operand_p (arg1)
3689169689Skan      || error_operand_p (arg2)
3690132718Skan      || error_operand_p (arg3))
369150397Sobrien    return error_mark_node;
369218334Speter
369350397Sobrien  if (code == MODIFY_EXPR)
369450397Sobrien    {
369550397Sobrien      code2 = TREE_CODE (arg3);
369650397Sobrien      arg3 = NULL_TREE;
369790075Sobrien      fnname = ansi_assopname (code2);
369850397Sobrien    }
369950397Sobrien  else
370090075Sobrien    fnname = ansi_opname (code);
370118334Speter
3702132718Skan  arg1 = prep_operand (arg1);
3703169689Skan
370450397Sobrien  switch (code)
370550397Sobrien    {
370650397Sobrien    case NEW_EXPR:
370750397Sobrien    case VEC_NEW_EXPR:
370850397Sobrien    case VEC_DELETE_EXPR:
370950397Sobrien    case DELETE_EXPR:
3710117395Skan      /* Use build_op_new_call and build_op_delete_call instead.  */
3711169689Skan      gcc_unreachable ();
371218334Speter
371350397Sobrien    case CALL_EXPR:
371450397Sobrien      return build_object_call (arg1, arg2);
371518334Speter
371650397Sobrien    default:
371750397Sobrien      break;
371850397Sobrien    }
371918334Speter
3720132718Skan  arg2 = prep_operand (arg2);
3721132718Skan  arg3 = prep_operand (arg3);
3722169689Skan
372350397Sobrien  if (code == COND_EXPR)
372450397Sobrien    {
372550397Sobrien      if (arg2 == NULL_TREE
372650397Sobrien	  || TREE_CODE (TREE_TYPE (arg2)) == VOID_TYPE
372750397Sobrien	  || TREE_CODE (TREE_TYPE (arg3)) == VOID_TYPE
372850397Sobrien	  || (! IS_OVERLOAD_TYPE (TREE_TYPE (arg2))
372950397Sobrien	      && ! IS_OVERLOAD_TYPE (TREE_TYPE (arg3))))
373050397Sobrien	goto builtin;
373150397Sobrien    }
373250397Sobrien  else if (! IS_OVERLOAD_TYPE (TREE_TYPE (arg1))
373350397Sobrien	   && (! arg2 || ! IS_OVERLOAD_TYPE (TREE_TYPE (arg2))))
373450397Sobrien    goto builtin;
373518334Speter
373650397Sobrien  if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
373750397Sobrien    arg2 = integer_zero_node;
373818334Speter
373990075Sobrien  arglist = NULL_TREE;
374090075Sobrien  if (arg3)
374190075Sobrien    arglist = tree_cons (NULL_TREE, arg3, arglist);
374290075Sobrien  if (arg2)
374390075Sobrien    arglist = tree_cons (NULL_TREE, arg2, arglist);
374490075Sobrien  arglist = tree_cons (NULL_TREE, arg1, arglist);
374518334Speter
3746169689Skan  /* Get the high-water mark for the CONVERSION_OBSTACK.  */
3747169689Skan  p = conversion_obstack_alloc (0);
3748169689Skan
3749132718Skan  /* Add namespace-scope operators to the list of functions to
3750132718Skan     consider.  */
3751169689Skan  add_candidates (lookup_function_nonclass (fnname, arglist, /*block_p=*/true),
3752132718Skan		  arglist, NULL_TREE, false, NULL_TREE, NULL_TREE,
3753132718Skan		  flags, &candidates);
3754132718Skan  /* Add class-member operators to the candidate set.  */
3755132718Skan  if (CLASS_TYPE_P (TREE_TYPE (arg1)))
375650397Sobrien    {
3757132718Skan      tree fns;
375818334Speter
3759169689Skan      fns = lookup_fnfields (TREE_TYPE (arg1), fnname, 1);
376050397Sobrien      if (fns == error_mark_node)
3761169689Skan	{
3762169689Skan	  result = error_mark_node;
3763169689Skan	  goto user_defined_result_ready;
3764169689Skan	}
3765132718Skan      if (fns)
3766169689Skan	add_candidates (BASELINK_FUNCTIONS (fns), arglist,
3767132718Skan			NULL_TREE, false,
3768132718Skan			BASELINK_BINFO (fns),
3769132718Skan			TYPE_BINFO (TREE_TYPE (arg1)),
3770132718Skan			flags, &candidates);
377150397Sobrien    }
377218334Speter
3773132718Skan  /* Rearrange the arguments for ?: so that add_builtin_candidate only has
3774132718Skan     to know about two args; a builtin candidate will always have a first
3775132718Skan     parameter of type bool.  We'll handle that in
3776132718Skan     build_builtin_candidate.  */
3777132718Skan  if (code == COND_EXPR)
377850397Sobrien    {
3779132718Skan      args[0] = arg2;
3780132718Skan      args[1] = arg3;
3781132718Skan      args[2] = arg1;
378250397Sobrien    }
3783132718Skan  else
3784132718Skan    {
3785132718Skan      args[0] = arg1;
3786132718Skan      args[1] = arg2;
3787132718Skan      args[2] = NULL_TREE;
3788132718Skan    }
378918334Speter
3790132718Skan  add_builtin_candidates (&candidates, code, code2, fnname, args, flags);
379118334Speter
3792117395Skan  switch (code)
379350397Sobrien    {
3794117395Skan    case COMPOUND_EXPR:
3795117395Skan    case ADDR_EXPR:
3796117395Skan      /* For these, the built-in candidates set is empty
3797117395Skan	 [over.match.oper]/3.  We don't want non-strict matches
3798117395Skan	 because exact matches are always possible with built-in
3799117395Skan	 operators.  The built-in candidate set for COMPONENT_REF
3800117395Skan	 would be empty too, but since there are no such built-in
3801117395Skan	 operators, we accept non-strict matches for them.  */
3802132718Skan      strict_p = true;
3803117395Skan      break;
3804117395Skan
3805117395Skan    default:
3806132718Skan      strict_p = pedantic;
3807117395Skan      break;
3808169689Skan    }
3809117395Skan
3810132718Skan  candidates = splice_viable (candidates, strict_p, &any_viable_p);
3811132718Skan  if (!any_viable_p)
3812117395Skan    {
381350397Sobrien      switch (code)
381418334Speter	{
381550397Sobrien	case POSTINCREMENT_EXPR:
381650397Sobrien	case POSTDECREMENT_EXPR:
381750397Sobrien	  /* Look for an `operator++ (int)'.  If they didn't have
381850397Sobrien	     one, then we fall back to the old way of doing things.  */
381918334Speter	  if (flags & LOOKUP_COMPLAIN)
3820169689Skan	    pedwarn ("no %<%D(int)%> declared for postfix %qs, "
3821169689Skan		     "trying prefix operator instead",
3822169689Skan		     fnname,
3823169689Skan		     operator_name_info[code].name);
382450397Sobrien	  if (code == POSTINCREMENT_EXPR)
382550397Sobrien	    code = PREINCREMENT_EXPR;
382618334Speter	  else
3827169689Skan	    code = PREDECREMENT_EXPR;
3828169689Skan	  result = build_new_op (code, flags, arg1, NULL_TREE, NULL_TREE,
3829169689Skan				 overloaded_p);
3830169689Skan	  break;
3831169689Skan
383250397Sobrien	  /* The caller will deal with these.  */
383350397Sobrien	case ADDR_EXPR:
383450397Sobrien	case COMPOUND_EXPR:
383550397Sobrien	case COMPONENT_REF:
3836169689Skan	  result = NULL_TREE;
3837169689Skan	  result_valid_p = true;
3838169689Skan	  break;
383950397Sobrien
384050397Sobrien	default:
3841169689Skan	  if (flags & LOOKUP_COMPLAIN)
3842169689Skan	    {
3843169689Skan	      op_error (code, code2, arg1, arg2, arg3, "no match");
3844169689Skan	      print_z_candidates (candidates);
3845169689Skan	    }
3846169689Skan	  result = error_mark_node;
384750397Sobrien	  break;
384818334Speter	}
384918334Speter    }
3850169689Skan  else
385118334Speter    {
3852169689Skan      cand = tourney (candidates);
3853169689Skan      if (cand == 0)
385418334Speter	{
3855169689Skan	  if (flags & LOOKUP_COMPLAIN)
3856169689Skan	    {
3857169689Skan	      op_error (code, code2, arg1, arg2, arg3, "ambiguous overload");
3858169689Skan	      print_z_candidates (candidates);
3859169689Skan	    }
3860169689Skan	  result = error_mark_node;
386118334Speter	}
3862169689Skan      else if (TREE_CODE (cand->fn) == FUNCTION_DECL)
3863169689Skan	{
3864169689Skan	  if (overloaded_p)
3865169689Skan	    *overloaded_p = true;
386618334Speter
3867169689Skan	  result = build_over_call (cand, LOOKUP_NORMAL);
3868169689Skan	}
3869169689Skan      else
3870169689Skan	{
3871169689Skan	  /* Give any warnings we noticed during overload resolution.  */
3872169689Skan	  if (cand->warnings)
3873169689Skan	    {
3874169689Skan	      struct candidate_warning *w;
3875169689Skan	      for (w = cand->warnings; w; w = w->next)
3876169689Skan		joust (cand, w->loser, 1);
3877169689Skan	    }
3878132718Skan
3879169689Skan	  /* Check for comparison of different enum types.  */
3880169689Skan	  switch (code)
3881169689Skan	    {
3882169689Skan	    case GT_EXPR:
3883169689Skan	    case LT_EXPR:
3884169689Skan	    case GE_EXPR:
3885169689Skan	    case LE_EXPR:
3886169689Skan	    case EQ_EXPR:
3887169689Skan	    case NE_EXPR:
3888169689Skan	      if (TREE_CODE (TREE_TYPE (arg1)) == ENUMERAL_TYPE
3889169689Skan		  && TREE_CODE (TREE_TYPE (arg2)) == ENUMERAL_TYPE
3890169689Skan		  && (TYPE_MAIN_VARIANT (TREE_TYPE (arg1))
3891169689Skan		      != TYPE_MAIN_VARIANT (TREE_TYPE (arg2))))
3892169689Skan		{
3893169689Skan		  warning (0, "comparison between %q#T and %q#T",
3894169689Skan			   TREE_TYPE (arg1), TREE_TYPE (arg2));
3895169689Skan		}
3896169689Skan	      break;
3897169689Skan	    default:
3898169689Skan	      break;
3899169689Skan	    }
390050397Sobrien
3901169689Skan	  /* We need to strip any leading REF_BIND so that bitfields
3902169689Skan	     don't cause errors.  This should not remove any important
3903169689Skan	     conversions, because builtins don't apply to class
3904169689Skan	     objects directly.  */
3905169689Skan	  conv = cand->convs[0];
3906169689Skan	  if (conv->kind == ck_ref_bind)
3907169689Skan	    conv = conv->u.next;
3908169689Skan	  arg1 = convert_like (conv, arg1);
3909169689Skan	  if (arg2)
3910169689Skan	    {
3911169689Skan	      conv = cand->convs[1];
3912169689Skan	      if (conv->kind == ck_ref_bind)
3913169689Skan		conv = conv->u.next;
3914169689Skan	      arg2 = convert_like (conv, arg2);
3915169689Skan	    }
3916169689Skan	  if (arg3)
3917169689Skan	    {
3918169689Skan	      conv = cand->convs[2];
3919169689Skan	      if (conv->kind == ck_ref_bind)
3920169689Skan		conv = conv->u.next;
3921169689Skan	      arg3 = convert_like (conv, arg3);
3922169689Skan	    }
392318334Speter	}
392450397Sobrien    }
392518334Speter
3926169689Skan user_defined_result_ready:
392718334Speter
3928169689Skan  /* Free all the conversions we allocated.  */
3929169689Skan  obstack_free (&conversion_obstack, p);
3930169689Skan
3931169689Skan  if (result || result_valid_p)
3932169689Skan    return result;
3933169689Skan
3934169689Skan builtin:
393550397Sobrien  switch (code)
393650397Sobrien    {
393750397Sobrien    case MODIFY_EXPR:
393850397Sobrien      return build_modify_expr (arg1, code2, arg2);
393918334Speter
394050397Sobrien    case INDIRECT_REF:
394150397Sobrien      return build_indirect_ref (arg1, "unary *");
394218334Speter
394350397Sobrien    case PLUS_EXPR:
394450397Sobrien    case MINUS_EXPR:
394550397Sobrien    case MULT_EXPR:
394650397Sobrien    case TRUNC_DIV_EXPR:
394750397Sobrien    case GT_EXPR:
394850397Sobrien    case LT_EXPR:
394950397Sobrien    case GE_EXPR:
395050397Sobrien    case LE_EXPR:
395150397Sobrien    case EQ_EXPR:
395250397Sobrien    case NE_EXPR:
395350397Sobrien    case MAX_EXPR:
395450397Sobrien    case MIN_EXPR:
395550397Sobrien    case LSHIFT_EXPR:
395650397Sobrien    case RSHIFT_EXPR:
395750397Sobrien    case TRUNC_MOD_EXPR:
395850397Sobrien    case BIT_AND_EXPR:
395950397Sobrien    case BIT_IOR_EXPR:
396050397Sobrien    case BIT_XOR_EXPR:
396150397Sobrien    case TRUTH_ANDIF_EXPR:
396250397Sobrien    case TRUTH_ORIF_EXPR:
396390075Sobrien      return cp_build_binary_op (code, arg1, arg2);
396418334Speter
3965169689Skan    case UNARY_PLUS_EXPR:
396650397Sobrien    case NEGATE_EXPR:
396750397Sobrien    case BIT_NOT_EXPR:
396850397Sobrien    case TRUTH_NOT_EXPR:
396950397Sobrien    case PREINCREMENT_EXPR:
397050397Sobrien    case POSTINCREMENT_EXPR:
397150397Sobrien    case PREDECREMENT_EXPR:
397250397Sobrien    case POSTDECREMENT_EXPR:
397350397Sobrien    case REALPART_EXPR:
397450397Sobrien    case IMAGPART_EXPR:
397550397Sobrien      return build_unary_op (code, arg1, candidates != 0);
397650397Sobrien
397750397Sobrien    case ARRAY_REF:
397850397Sobrien      return build_array_ref (arg1, arg2);
397950397Sobrien
398050397Sobrien    case COND_EXPR:
398150397Sobrien      return build_conditional_expr (arg1, arg2, arg3);
398250397Sobrien
398350397Sobrien    case MEMBER_REF:
3984169689Skan      return build_m_component_ref (build_indirect_ref (arg1, NULL), arg2);
398550397Sobrien
398650397Sobrien      /* The caller will deal with these.  */
398750397Sobrien    case ADDR_EXPR:
398850397Sobrien    case COMPONENT_REF:
398950397Sobrien    case COMPOUND_EXPR:
399050397Sobrien      return NULL_TREE;
399150397Sobrien
399250397Sobrien    default:
3993169689Skan      gcc_unreachable ();
399450397Sobrien    }
3995169689Skan  return NULL_TREE;
399650397Sobrien}
399750397Sobrien
399850397Sobrien/* Build a call to operator delete.  This has to be handled very specially,
399950397Sobrien   because the restrictions on what signatures match are different from all
400050397Sobrien   other call instances.  For a normal delete, only a delete taking (void *)
400150397Sobrien   or (void *, size_t) is accepted.  For a placement delete, only an exact
400250397Sobrien   match with the placement new is accepted.
400350397Sobrien
400450397Sobrien   CODE is either DELETE_EXPR or VEC_DELETE_EXPR.
400596263Sobrien   ADDR is the pointer to be deleted.
400650397Sobrien   SIZE is the size of the memory block to be deleted.
4007169689Skan   GLOBAL_P is true if the delete-expression should not consider
4008169689Skan   class-specific delete operators.
4009169689Skan   PLACEMENT is the corresponding placement new call, or NULL_TREE.
401050397Sobrien
4011171825Skan   If this call to "operator delete" is being generated as part to
4012171825Skan   deallocate memory allocated via a new-expression (as per [expr.new]
4013171825Skan   which requires that if the initialization throws an exception then
4014171825Skan   we call a deallocation function), then ALLOC_FN is the allocation
4015171825Skan   function.  */
4016171825Skan
401750397Sobrientree
4018132718Skanbuild_op_delete_call (enum tree_code code, tree addr, tree size,
4019169689Skan		      bool global_p, tree placement,
4020169689Skan		      tree alloc_fn)
402150397Sobrien{
402290075Sobrien  tree fn = NULL_TREE;
4023132718Skan  tree fns, fnname, argtypes, args, type;
402490075Sobrien  int pass;
402550397Sobrien
402650397Sobrien  if (addr == error_mark_node)
402750397Sobrien    return error_mark_node;
402850397Sobrien
4029132718Skan  type = strip_array_types (TREE_TYPE (TREE_TYPE (addr)));
403050397Sobrien
403190075Sobrien  fnname = ansi_opname (code);
403290075Sobrien
4033169689Skan  if (CLASS_TYPE_P (type)
4034169689Skan      && COMPLETE_TYPE_P (complete_type (type))
4035169689Skan      && !global_p)
403650397Sobrien    /* In [class.free]
403750397Sobrien
403850397Sobrien       If the result of the lookup is ambiguous or inaccessible, or if
403950397Sobrien       the lookup selects a placement deallocation function, the
404050397Sobrien       program is ill-formed.
4041169689Skan
4042132718Skan       Therefore, we ask lookup_fnfields to complain about ambiguity.  */
404350397Sobrien    {
404450397Sobrien      fns = lookup_fnfields (TYPE_BINFO (type), fnname, 1);
404550397Sobrien      if (fns == error_mark_node)
404650397Sobrien	return error_mark_node;
404718334Speter    }
404850397Sobrien  else
404950397Sobrien    fns = NULL_TREE;
405018334Speter
405150397Sobrien  if (fns == NULL_TREE)
405250397Sobrien    fns = lookup_name_nonclass (fnname);
405350397Sobrien
405450397Sobrien  if (placement)
405518334Speter    {
4056169689Skan      /* Get the parameter types for the allocation function that is
4057169689Skan	 being called.  */
4058169689Skan      gcc_assert (alloc_fn != NULL_TREE);
405996263Sobrien      argtypes = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (alloc_fn)));
406050397Sobrien      /* Also the second argument.  */
4061169689Skan      args = TREE_CHAIN (TREE_OPERAND (placement, 1));
406250397Sobrien    }
406350397Sobrien  else
406450397Sobrien    {
406550397Sobrien      /* First try it without the size argument.  */
406650397Sobrien      argtypes = void_list_node;
406750397Sobrien      args = NULL_TREE;
406850397Sobrien    }
406950397Sobrien
407050397Sobrien  /* Strip const and volatile from addr.  */
407190075Sobrien  addr = cp_convert (ptr_type_node, addr);
407250397Sobrien
407390075Sobrien  /* We make two tries at finding a matching `operator delete'.  On
4074132718Skan     the first pass, we look for a one-operator (or placement)
407590075Sobrien     operator delete.  If we're not doing placement delete, then on
407690075Sobrien     the second pass we look for a two-argument delete.  */
4077169689Skan  for (pass = 0; pass < (placement ? 1 : 2); ++pass)
407890075Sobrien    {
407990075Sobrien      /* Go through the `operator delete' functions looking for one
408090075Sobrien	 with a matching type.  */
4081169689Skan      for (fn = BASELINK_P (fns) ? BASELINK_FUNCTIONS (fns) : fns;
4082169689Skan	   fn;
408390075Sobrien	   fn = OVL_NEXT (fn))
408490075Sobrien	{
408590075Sobrien	  tree t;
408690075Sobrien
4087132718Skan	  /* The first argument must be "void *".  */
4088132718Skan	  t = TYPE_ARG_TYPES (TREE_TYPE (OVL_CURRENT (fn)));
4089132718Skan	  if (!same_type_p (TREE_VALUE (t), ptr_type_node))
4090132718Skan	    continue;
4091132718Skan	  t = TREE_CHAIN (t);
4092132718Skan	  /* On the first pass, check the rest of the arguments.  */
4093132718Skan	  if (pass == 0)
4094132718Skan	    {
4095132718Skan	      tree a = argtypes;
4096132718Skan	      while (a && t)
4097132718Skan		{
4098132718Skan		  if (!same_type_p (TREE_VALUE (a), TREE_VALUE (t)))
4099132718Skan		    break;
4100132718Skan		  a = TREE_CHAIN (a);
4101132718Skan		  t = TREE_CHAIN (t);
4102132718Skan		}
4103132718Skan	      if (!a && !t)
4104132718Skan		break;
4105132718Skan	    }
4106171825Skan	  /* On the second pass, look for a function with exactly two
4107171825Skan	     arguments: "void *" and "size_t".  */
4108132718Skan	  else if (pass == 1
4109171825Skan		   /* For "operator delete(void *, ...)" there will be
4110171825Skan		      no second argument, but we will not get an exact
4111171825Skan		      match above.  */
4112171825Skan		   && t
4113132718Skan		   && same_type_p (TREE_VALUE (t), sizetype)
4114132718Skan		   && TREE_CHAIN (t) == void_list_node)
411590075Sobrien	    break;
411690075Sobrien	}
411790075Sobrien
411890075Sobrien      /* If we found a match, we're done.  */
411990075Sobrien      if (fn)
412090075Sobrien	break;
412150397Sobrien    }
412250397Sobrien
412390075Sobrien  /* If we have a matching function, call it.  */
412490075Sobrien  if (fn)
412590075Sobrien    {
412690075Sobrien      /* Make sure we have the actual function, and not an
412790075Sobrien	 OVERLOAD.  */
412890075Sobrien      fn = OVL_CURRENT (fn);
412950397Sobrien
413090075Sobrien      /* If the FN is a member function, make sure that it is
413190075Sobrien	 accessible.  */
413290075Sobrien      if (DECL_CLASS_SCOPE_P (fn))
4133169689Skan	perform_or_defer_access_check (TYPE_BINFO (type), fn, fn);
413450397Sobrien
413590075Sobrien      if (pass == 0)
413690075Sobrien	args = tree_cons (NULL_TREE, addr, args);
413790075Sobrien      else
4138169689Skan	args = tree_cons (NULL_TREE, addr,
413990075Sobrien			  build_tree_list (NULL_TREE, size));
414050397Sobrien
4141132718Skan      if (placement)
4142132718Skan	{
4143132718Skan	  /* The placement args might not be suitable for overload
4144132718Skan	     resolution at this point, so build the call directly.  */
4145132718Skan	  mark_used (fn);
4146169689Skan	  return build_cxx_call (fn, args);
4147132718Skan	}
4148132718Skan      else
4149132718Skan	return build_function_call (fn, args);
415050397Sobrien    }
415150397Sobrien
4152171825Skan  /* [expr.new]
415350397Sobrien
4154171825Skan     If no unambiguous matching deallocation function can be found,
4155171825Skan     propagating the exception does not cause the object's memory to
4156171825Skan     be freed.  */
4157171825Skan  if (alloc_fn)
4158171825Skan    {
4159171825Skan      if (!placement)
4160171825Skan	warning (0, "no corresponding deallocation function for `%D'",
4161171825Skan		 alloc_fn);
4162171825Skan      return NULL_TREE;
4163171825Skan    }
4164171825Skan
4165169689Skan  error ("no suitable %<operator %s%> for %qT",
4166132718Skan	 operator_name_info[(int)code].name, type);
416750397Sobrien  return error_mark_node;
416850397Sobrien}
416950397Sobrien
417050397Sobrien/* If the current scope isn't allowed to access DECL along
417152284Sobrien   BASETYPE_PATH, give an error.  The most derived class in
4172169689Skan   BASETYPE_PATH is the one used to qualify DECL. DIAG_DECL is
4173169689Skan   the declaration to use in the error diagnostic.  */
417450397Sobrien
4175132718Skanbool
4176169689Skanenforce_access (tree basetype_path, tree decl, tree diag_decl)
417750397Sobrien{
4178169689Skan  gcc_assert (TREE_CODE (basetype_path) == TREE_BINFO);
4179169689Skan
4180169689Skan  if (!accessible_p (basetype_path, decl, true))
418150397Sobrien    {
418252284Sobrien      if (TREE_PRIVATE (decl))
4183169689Skan	error ("%q+#D is private", diag_decl);
418452284Sobrien      else if (TREE_PROTECTED (decl))
4185169689Skan	error ("%q+#D is protected", diag_decl);
418652284Sobrien      else
4187169689Skan	error ("%q+#D is inaccessible", diag_decl);
418890075Sobrien      error ("within this context");
4189132718Skan      return false;
419050397Sobrien    }
419152284Sobrien
4192132718Skan  return true;
419350397Sobrien}
419450397Sobrien
4195132718Skan/* Check that a callable constructor to initialize a temporary of
4196132718Skan   TYPE from an EXPR exists.  */
4197132718Skan
4198132718Skanstatic void
4199132718Skancheck_constructor_callable (tree type, tree expr)
4200132718Skan{
4201132718Skan  build_special_member_call (NULL_TREE,
4202132718Skan			     complete_ctor_identifier,
4203169689Skan			     build_tree_list (NULL_TREE, expr),
4204169689Skan			     type,
4205132718Skan			     LOOKUP_NORMAL | LOOKUP_ONLYCONVERTING
4206146895Skan			     | LOOKUP_NO_CONVERSION
4207132718Skan			     | LOOKUP_CONSTRUCTOR_CALLABLE);
4208132718Skan}
4209132718Skan
4210132718Skan/* Initialize a temporary of type TYPE with EXPR.  The FLAGS are a
4211132718Skan   bitwise or of LOOKUP_* values.  If any errors are warnings are
4212132718Skan   generated, set *DIAGNOSTIC_FN to "error" or "warning",
4213132718Skan   respectively.  If no diagnostics are generated, set *DIAGNOSTIC_FN
4214132718Skan   to NULL.  */
4215132718Skan
4216132718Skanstatic tree
4217169689Skanbuild_temp (tree expr, tree type, int flags,
4218169689Skan	    diagnostic_fn_t *diagnostic_fn)
4219132718Skan{
4220132718Skan  int savew, savee;
4221169689Skan
4222132718Skan  savew = warningcount, savee = errorcount;
4223132718Skan  expr = build_special_member_call (NULL_TREE,
4224132718Skan				    complete_ctor_identifier,
4225169689Skan				    build_tree_list (NULL_TREE, expr),
4226169689Skan				    type, flags);
4227132718Skan  if (warningcount > savew)
4228169689Skan    *diagnostic_fn = warning0;
4229132718Skan  else if (errorcount > savee)
4230132718Skan    *diagnostic_fn = error;
4231132718Skan  else
4232132718Skan    *diagnostic_fn = NULL;
4233132718Skan  return expr;
4234132718Skan}
4235132718Skan
4236169689Skan
4237117395Skan/* Perform the conversions in CONVS on the expression EXPR.  FN and
4238117395Skan   ARGNUM are used for diagnostics.  ARGNUM is zero based, -1
4239117395Skan   indicates the `this' argument of a method.  INNER is nonzero when
424090075Sobrien   being called to continue a conversion chain. It is negative when a
4241117395Skan   reference binding will be applied, positive otherwise.  If
4242117395Skan   ISSUE_CONVERSION_WARNINGS is true, warnings about suspicious
4243169689Skan   conversions will be emitted if appropriate.  If C_CAST_P is true,
4244169689Skan   this conversion is coming from a C-style cast; in that case,
4245169689Skan   conversions to inaccessible bases are permitted.  */
424650397Sobrien
424750397Sobrienstatic tree
4248169689Skanconvert_like_real (conversion *convs, tree expr, tree fn, int argnum,
4249169689Skan		   int inner, bool issue_conversion_warnings,
4250169689Skan		   bool c_cast_p)
425150397Sobrien{
4252169689Skan  tree totype = convs->type;
4253169689Skan  diagnostic_fn_t diagnostic_fn;
425490075Sobrien
4255169689Skan  if (convs->bad_p
4256169689Skan      && convs->kind != ck_user
4257169689Skan      && convs->kind != ck_ambig
4258169689Skan      && convs->kind != ck_ref_bind)
425950397Sobrien    {
4260169689Skan      conversion *t = convs;
4261169689Skan      for (; t; t = convs->u.next)
426218334Speter	{
4263169689Skan	  if (t->kind == ck_user || !t->bad_p)
426418334Speter	    {
4265117395Skan	      expr = convert_like_real (t, expr, fn, argnum, 1,
4266169689Skan					/*issue_conversion_warnings=*/false,
4267169689Skan					/*c_cast_p=*/false);
426850397Sobrien	      break;
426918334Speter	    }
4270169689Skan	  else if (t->kind == ck_ambig)
4271117395Skan	    return convert_like_real (t, expr, fn, argnum, 1,
4272169689Skan				      /*issue_conversion_warnings=*/false,
4273169689Skan				      /*c_cast_p=*/false);
4274169689Skan	  else if (t->kind == ck_identity)
427550397Sobrien	    break;
427618334Speter	}
4277169689Skan      pedwarn ("invalid conversion from %qT to %qT", TREE_TYPE (expr), totype);
427890075Sobrien      if (fn)
4279169689Skan	pedwarn ("  initializing argument %P of %qD", argnum, fn);
428090075Sobrien      return cp_convert (totype, expr);
428118334Speter    }
4282169689Skan
4283117395Skan  if (issue_conversion_warnings)
428450397Sobrien    {
4285169689Skan      tree t = non_reference (totype);
4286169689Skan
4287169689Skan      /* Issue warnings about peculiar, but valid, uses of NULL.  */
4288169689Skan      if (ARITHMETIC_TYPE_P (t) && expr == null_node)
4289169689Skan	{
4290169689Skan	  if (fn)
4291169689Skan	    warning (OPT_Wconversion, "passing NULL to non-pointer argument %P of %qD",
4292169689Skan		     argnum, fn);
4293169689Skan	  else
4294169689Skan	    warning (OPT_Wconversion, "converting to non-pointer type %qT from NULL", t);
4295169689Skan	}
4296169689Skan
4297169689Skan      /* Warn about assigning a floating-point type to an integer type.  */
4298169689Skan      if (TREE_CODE (TREE_TYPE (expr)) == REAL_TYPE
4299169689Skan	  && TREE_CODE (t) == INTEGER_TYPE)
4300169689Skan	{
4301169689Skan	  if (fn)
4302169689Skan	    warning (OPT_Wconversion, "passing %qT for argument %P to %qD",
4303169689Skan		     TREE_TYPE (expr), argnum, fn);
4304169689Skan	  else
4305169689Skan	    warning (OPT_Wconversion, "converting to %qT from %qT", t, TREE_TYPE (expr));
4306169689Skan	}
4307169689Skan    }
4308169689Skan
4309169689Skan  switch (convs->kind)
4310169689Skan    {
4311169689Skan    case ck_user:
431250397Sobrien      {
4313169689Skan	struct z_candidate *cand = convs->cand;
431490075Sobrien	tree convfn = cand->fn;
431550397Sobrien	tree args;
431618334Speter
431790075Sobrien	if (DECL_CONSTRUCTOR_P (convfn))
431850397Sobrien	  {
4319169689Skan	    tree t = build_int_cst (build_pointer_type (DECL_CONTEXT (convfn)),
4320169689Skan				    0);
432150397Sobrien
432290075Sobrien	    args = build_tree_list (NULL_TREE, expr);
4323169689Skan	    /* We should never try to call the abstract or base constructor
4324169689Skan	       from here.  */
4325169689Skan	    gcc_assert (!DECL_HAS_IN_CHARGE_PARM_P (convfn)
4326169689Skan			&& !DECL_HAS_VTT_PARM_P (convfn));
432790075Sobrien	    args = tree_cons (NULL_TREE, t, args);
432850397Sobrien	  }
432950397Sobrien	else
433050397Sobrien	  args = build_this (expr);
4331132718Skan	expr = build_over_call (cand, LOOKUP_NORMAL);
433250397Sobrien
433350397Sobrien	/* If this is a constructor or a function returning an aggr type,
433450397Sobrien	   we need to build up a TARGET_EXPR.  */
433590075Sobrien	if (DECL_CONSTRUCTOR_P (convfn))
433690075Sobrien	  expr = build_cplus_new (totype, expr);
433750397Sobrien
433890075Sobrien	/* The result of the call is then used to direct-initialize the object
433990075Sobrien	   that is the destination of the copy-initialization.  [dcl.init]
434090075Sobrien
434190075Sobrien	   Note that this step is not reflected in the conversion sequence;
434290075Sobrien	   it affects the semantics when we actually perform the
434390075Sobrien	   conversion, but is not considered during overload resolution.
434490075Sobrien
434590075Sobrien	   If the target is a class, that means call a ctor.  */
434690075Sobrien	if (IS_AGGR_TYPE (totype)
434790075Sobrien	    && (inner >= 0 || !lvalue_p (expr)))
434890075Sobrien	  {
4349169689Skan	    expr = (build_temp
4350169689Skan		    (expr, totype,
4351132718Skan		     /* Core issue 84, now a DR, says that we don't
4352132718Skan			allow UDCs for these args (which deliberately
4353132718Skan			breaks copy-init of an auto_ptr<Base> from an
4354132718Skan			auto_ptr<Derived>).  */
4355132718Skan		     LOOKUP_NORMAL|LOOKUP_ONLYCONVERTING|LOOKUP_NO_CONVERSION,
4356132718Skan		     &diagnostic_fn));
4357169689Skan
4358132718Skan	    if (diagnostic_fn)
435990075Sobrien	      {
4360132718Skan		if (fn)
4361169689Skan		  diagnostic_fn
4362169689Skan		    ("  initializing argument %P of %qD from result of %qD",
436390075Sobrien		     argnum, fn, convfn);
4364132718Skan		else
4365169689Skan		 diagnostic_fn
4366169689Skan		   ("  initializing temporary from result of %qD",  convfn);
436790075Sobrien	      }
436890075Sobrien	    expr = build_cplus_new (totype, expr);
436990075Sobrien	  }
437050397Sobrien	return expr;
437150397Sobrien      }
4372169689Skan    case ck_identity:
437350397Sobrien      if (type_unknown_p (expr))
4374169689Skan	expr = instantiate_type (totype, expr, tf_warning_or_error);
4375169689Skan      /* Convert a constant to its underlying value, unless we are
4376169689Skan	 about to bind it to a reference, in which case we need to
4377169689Skan	 leave it as an lvalue.  */
4378169689Skan      if (inner >= 0)
4379132718Skan	expr = decl_constant_value (expr);
4380169689Skan      if (convs->check_copy_constructor_p)
4381132718Skan	check_constructor_callable (totype, expr);
438250397Sobrien      return expr;
4383169689Skan    case ck_ambig:
438450397Sobrien      /* Call build_user_type_conversion again for the error.  */
438550397Sobrien      return build_user_type_conversion
4386169689Skan	(totype, convs->u.expr, LOOKUP_NORMAL);
438750397Sobrien
438850397Sobrien    default:
438950397Sobrien      break;
439050397Sobrien    };
439150397Sobrien
4392169689Skan  expr = convert_like_real (convs->u.next, expr, fn, argnum,
4393169689Skan			    convs->kind == ck_ref_bind ? -1 : 1,
4394169689Skan			    /*issue_conversion_warnings=*/false,
4395169689Skan			    c_cast_p);
439650397Sobrien  if (expr == error_mark_node)
439750397Sobrien    return error_mark_node;
439850397Sobrien
4399169689Skan  switch (convs->kind)
440018334Speter    {
4401169689Skan    case ck_rvalue:
4402169689Skan      expr = convert_bitfield_to_declared_type (expr);
440390075Sobrien      if (! IS_AGGR_TYPE (totype))
440450397Sobrien	return expr;
4405132718Skan      /* Else fall through.  */
4406169689Skan    case ck_base:
4407169689Skan      if (convs->kind == ck_base && !convs->need_temporary_p)
440890075Sobrien	{
440990075Sobrien	  /* We are going to bind a reference directly to a base-class
441090075Sobrien	     subobject of EXPR.  */
4411169689Skan	  if (convs->check_copy_constructor_p)
4412132718Skan	    check_constructor_callable (TREE_TYPE (expr), expr);
441390075Sobrien	  /* Build an expression for `*((base*) &expr)'.  */
441490075Sobrien	  expr = build_unary_op (ADDR_EXPR, expr, 0);
4415169689Skan	  expr = convert_to_base (expr, build_pointer_type (totype),
4416169689Skan				  !c_cast_p, /*nonnull=*/true);
441790075Sobrien	  expr = build_indirect_ref (expr, "implicit conversion");
441890075Sobrien	  return expr;
441990075Sobrien	}
442090075Sobrien
442190075Sobrien      /* Copy-initialization where the cv-unqualified version of the source
442290075Sobrien	 type is the same class as, or a derived class of, the class of the
442390075Sobrien	 destination [is treated as direct-initialization].  [dcl.init] */
4424132718Skan      expr = build_temp (expr, totype, LOOKUP_NORMAL|LOOKUP_ONLYCONVERTING,
4425132718Skan			 &diagnostic_fn);
4426132718Skan      if (diagnostic_fn && fn)
4427169689Skan	diagnostic_fn ("  initializing argument %P of %qD", argnum, fn);
442890075Sobrien      return build_cplus_new (totype, expr);
442990075Sobrien
4430169689Skan    case ck_ref_bind:
443150397Sobrien      {
443290075Sobrien	tree ref_type = totype;
443390075Sobrien
443490075Sobrien	/* If necessary, create a temporary.  */
4435169689Skan	if (convs->need_temporary_p || !lvalue_p (expr))
443650397Sobrien	  {
4437169689Skan	    tree type = convs->u.next->type;
4438161651Skan	    cp_lvalue_kind lvalue = real_lvalue_p (expr);
4439132718Skan
4440132718Skan	    if (!CP_TYPE_CONST_NON_VOLATILE_P (TREE_TYPE (ref_type)))
4441132718Skan	      {
4442132718Skan		/* If the reference is volatile or non-const, we
4443132718Skan		   cannot create a temporary.  */
4444132718Skan		if (lvalue & clk_bitfield)
4445169689Skan		  error ("cannot bind bitfield %qE to %qT",
4446132718Skan			 expr, ref_type);
4447132718Skan		else if (lvalue & clk_packed)
4448169689Skan		  error ("cannot bind packed field %qE to %qT",
4449132718Skan			 expr, ref_type);
4450132718Skan		else
4451169689Skan		  error ("cannot bind rvalue %qE to %qT", expr, ref_type);
4452132718Skan		return error_mark_node;
4453132718Skan	      }
4454161651Skan	    /* If the source is a packed field, and we must use a copy
4455161651Skan	       constructor, then building the target expr will require
4456161651Skan	       binding the field to the reference parameter to the
4457161651Skan	       copy constructor, and we'll end up with an infinite
4458161651Skan	       loop.  If we can use a bitwise copy, then we'll be
4459161651Skan	       OK.  */
4460169689Skan	    if ((lvalue & clk_packed)
4461169689Skan		&& CLASS_TYPE_P (type)
4462161651Skan		&& !TYPE_HAS_TRIVIAL_INIT_REF (type))
4463161651Skan	      {
4464169689Skan		error ("cannot bind packed field %qE to %qT",
4465161651Skan		       expr, ref_type);
4466161651Skan		return error_mark_node;
4467161651Skan	      }
446890075Sobrien	    expr = build_target_expr_with_type (expr, type);
446950397Sobrien	  }
447090075Sobrien
447190075Sobrien	/* Take the address of the thing to which we will bind the
447290075Sobrien	   reference.  */
447390075Sobrien	expr = build_unary_op (ADDR_EXPR, expr, 1);
447490075Sobrien	if (expr == error_mark_node)
447590075Sobrien	  return error_mark_node;
447690075Sobrien
447790075Sobrien	/* Convert it to a pointer to the type referred to by the
447890075Sobrien	   reference.  This will adjust the pointer if a derived to
447990075Sobrien	   base conversion is being performed.  */
4480169689Skan	expr = cp_convert (build_pointer_type (TREE_TYPE (ref_type)),
448190075Sobrien			   expr);
448290075Sobrien	/* Convert the pointer to the desired reference type.  */
4483117395Skan	return build_nop (ref_type, expr);
448450397Sobrien      }
448550397Sobrien
4486169689Skan    case ck_lvalue:
448750397Sobrien      return decay_conversion (expr);
448850397Sobrien
4489169689Skan    case ck_qual:
449052284Sobrien      /* Warn about deprecated conversion if appropriate.  */
449190075Sobrien      string_conv_p (totype, expr, 1);
449252284Sobrien      break;
4493169689Skan
4494169689Skan    case ck_ptr:
4495169689Skan      if (convs->base_p)
4496169689Skan	expr = convert_to_base (expr, totype, !c_cast_p,
4497169689Skan				/*nonnull=*/false);
4498169689Skan      return build_nop (totype, expr);
4499169689Skan
4500169689Skan    case ck_pmem:
4501169689Skan      return convert_ptrmem (totype, expr, /*allow_inverse_p=*/false,
4502169689Skan			     c_cast_p);
4503169689Skan
450450397Sobrien    default:
450550397Sobrien      break;
450618334Speter    }
4507169689Skan
4508169689Skan  if (issue_conversion_warnings)
4509169689Skan    expr = convert_and_check (totype, expr);
4510169689Skan  else
4511169689Skan    expr = convert (totype, expr);
4512169689Skan
4513169689Skan  return expr;
451450397Sobrien}
451518334Speter
4516132718Skan/* Build a call to __builtin_trap.  */
4517117395Skan
4518117395Skanstatic tree
4519132718Skancall_builtin_trap (void)
4520117395Skan{
4521169689Skan  tree fn = implicit_built_in_decls[BUILT_IN_TRAP];
4522117395Skan
4523169689Skan  gcc_assert (fn != NULL);
4524117395Skan  fn = build_call (fn, NULL_TREE);
4525117395Skan  return fn;
4526117395Skan}
4527117395Skan
452850397Sobrien/* ARG is being passed to a varargs function.  Perform any conversions
4529132718Skan   required.  Return the converted value.  */
453050397Sobrien
453150397Sobrientree
4532132718Skanconvert_arg_to_ellipsis (tree arg)
453350397Sobrien{
4534132718Skan  /* [expr.call]
4535132718Skan
4536132718Skan     The lvalue-to-rvalue, array-to-pointer, and function-to-pointer
4537132718Skan     standard conversions are performed.  */
4538132718Skan  arg = decay_conversion (arg);
4539132718Skan  /* [expr.call]
4540132718Skan
4541132718Skan     If the argument has integral or enumeration type that is subject
4542132718Skan     to the integral promotions (_conv.prom_), or a floating point
4543132718Skan     type that is subject to the floating point promotion
4544132718Skan     (_conv.fpprom_), the value of the argument is converted to the
4545132718Skan     promoted type before the call.  */
454650397Sobrien  if (TREE_CODE (TREE_TYPE (arg)) == REAL_TYPE
454750397Sobrien      && (TYPE_PRECISION (TREE_TYPE (arg))
454850397Sobrien	  < TYPE_PRECISION (double_type_node)))
4549132718Skan    arg = convert_to_real (double_type_node, arg);
4550132718Skan  else if (INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (arg)))
4551132718Skan    arg = perform_integral_promotions (arg);
455250397Sobrien
455352284Sobrien  arg = require_complete_type (arg);
4554169689Skan
4555132718Skan  if (arg != error_mark_node
4556132718Skan      && !pod_type_p (TREE_TYPE (arg)))
455790075Sobrien    {
4558117395Skan      /* Undefined behavior [expr.call] 5.2.2/7.  We used to just warn
4559102780Skan	 here and do a bitwise copy, but now cp_expr_size will abort if we
4560169689Skan	 try to do that.
4561169689Skan	 If the call appears in the context of a sizeof expression,
4562169689Skan	 there is no need to emit a warning, since the expression won't be
4563132718Skan	 evaluated. We keep the builtin_trap just as a safety check.  */
4564132718Skan      if (!skip_evaluation)
4565169689Skan	warning (0, "cannot pass objects of non-POD type %q#T through %<...%>; "
4566169689Skan		 "call will abort at runtime", TREE_TYPE (arg));
4567117395Skan      arg = call_builtin_trap ();
4568169689Skan      arg = build2 (COMPOUND_EXPR, integer_type_node, arg,
4569169689Skan		    integer_zero_node);
457090075Sobrien    }
457190075Sobrien
457250397Sobrien  return arg;
457350397Sobrien}
457450397Sobrien
457590075Sobrien/* va_arg (EXPR, TYPE) is a builtin. Make sure it is not abused.  */
457690075Sobrien
457790075Sobrientree
4578132718Skanbuild_x_va_arg (tree expr, tree type)
457990075Sobrien{
458090075Sobrien  if (processing_template_decl)
458190075Sobrien    return build_min (VA_ARG_EXPR, type, expr);
4582169689Skan
458390075Sobrien  type = complete_type_or_else (type, NULL_TREE);
458490075Sobrien
458590075Sobrien  if (expr == error_mark_node || !type)
458690075Sobrien    return error_mark_node;
4587169689Skan
458890075Sobrien  if (! pod_type_p (type))
458990075Sobrien    {
4590169689Skan      /* Remove reference types so we don't ICE later on.  */
4591169689Skan      tree type1 = non_reference (type);
4592117395Skan      /* Undefined behavior [expr.call] 5.2.2/7.  */
4593169689Skan      warning (0, "cannot receive objects of non-POD type %q#T through %<...%>; "
4594169689Skan	       "call will abort at runtime", type);
4595169689Skan      expr = convert (build_pointer_type (type1), null_node);
4596169689Skan      expr = build2 (COMPOUND_EXPR, TREE_TYPE (expr),
4597169689Skan		     call_builtin_trap (), expr);
4598132718Skan      expr = build_indirect_ref (expr, NULL);
4599132718Skan      return expr;
460090075Sobrien    }
4601169689Skan
460290075Sobrien  return build_va_arg (expr, type);
460390075Sobrien}
460490075Sobrien
4605117395Skan/* TYPE has been given to va_arg.  Apply the default conversions which
4606117395Skan   would have happened when passed via ellipsis.  Return the promoted
4607117395Skan   type, or the passed type if there is no change.  */
460890075Sobrien
460990075Sobrientree
4610132718Skancxx_type_promotes_to (tree type)
461190075Sobrien{
461290075Sobrien  tree promote;
4613117395Skan
4614132718Skan  /* Perform the array-to-pointer and function-to-pointer
4615132718Skan     conversions.  */
4616132718Skan  type = type_decays_to (type);
4617117395Skan
4618117395Skan  promote = type_promotes_to (type);
4619117395Skan  if (same_type_p (type, promote))
4620117395Skan    promote = type;
4621169689Skan
4622117395Skan  return promote;
462390075Sobrien}
462490075Sobrien
462550397Sobrien/* ARG is a default argument expression being passed to a parameter of
462652284Sobrien   the indicated TYPE, which is a parameter to FN.  Do any required
462752284Sobrien   conversions.  Return the converted value.  */
462850397Sobrien
462950397Sobrientree
4630132718Skanconvert_default_arg (tree type, tree arg, tree fn, int parmnum)
463150397Sobrien{
4632132718Skan  /* If the ARG is an unparsed default argument expression, the
4633132718Skan     conversion cannot be performed.  */
463490075Sobrien  if (TREE_CODE (arg) == DEFAULT_ARG)
463552284Sobrien    {
4636169689Skan      error ("the default argument for parameter %d of %qD has "
4637132718Skan	     "not yet been parsed",
4638132718Skan	     parmnum, fn);
4639132718Skan      return error_mark_node;
464090075Sobrien    }
464152284Sobrien
464290075Sobrien  if (fn && DECL_TEMPLATE_INFO (fn))
464390075Sobrien    arg = tsubst_default_argument (fn, type, arg);
464452284Sobrien
464550397Sobrien  arg = break_out_target_exprs (arg);
464650397Sobrien
464750397Sobrien  if (TREE_CODE (arg) == CONSTRUCTOR)
464818334Speter    {
4649169689Skan      arg = digest_init (type, arg);
465050397Sobrien      arg = convert_for_initialization (0, type, arg, LOOKUP_NORMAL,
465190075Sobrien					"default argument", fn, parmnum);
465250397Sobrien    }
465350397Sobrien  else
465450397Sobrien    {
4655169689Skan      /* We must make a copy of ARG, in case subsequent processing
4656169689Skan	 alters any part of it.  For example, during gimplification a
4657169689Skan	 cast of the form (T) &X::f (where "f" is a member function)
4658169689Skan	 will lead to replacing the PTRMEM_CST for &X::f with a
4659169689Skan	 VAR_DECL.  We can avoid the copy for constants, since they
4660169689Skan	 are never modified in place.  */
4661169689Skan      if (!CONSTANT_CLASS_P (arg))
4662169689Skan	arg = unshare_expr (arg);
466350397Sobrien      arg = convert_for_initialization (0, type, arg, LOOKUP_NORMAL,
466490075Sobrien					"default argument", fn, parmnum);
4665117395Skan      arg = convert_for_arg_passing (type, arg);
466650397Sobrien    }
466750397Sobrien
466850397Sobrien  return arg;
466950397Sobrien}
467050397Sobrien
4671117395Skan/* Returns the type which will really be used for passing an argument of
4672117395Skan   type TYPE.  */
4673117395Skan
4674117395Skantree
4675132718Skantype_passed_as (tree type)
4676117395Skan{
4677117395Skan  /* Pass classes with copy ctors by invisible reference.  */
4678117395Skan  if (TREE_ADDRESSABLE (type))
4679169689Skan    {
4680169689Skan      type = build_reference_type (type);
4681169689Skan      /* There are no other pointers to this temporary.  */
4682169689Skan      type = build_qualified_type (type, TYPE_QUAL_RESTRICT);
4683169689Skan    }
4684169689Skan  else if (targetm.calls.promote_prototypes (type)
4685117395Skan	   && INTEGRAL_TYPE_P (type)
4686132718Skan	   && COMPLETE_TYPE_P (type)
4687132718Skan	   && INT_CST_LT_UNSIGNED (TYPE_SIZE (type),
4688132718Skan				   TYPE_SIZE (integer_type_node)))
4689117395Skan    type = integer_type_node;
4690117395Skan
4691117395Skan  return type;
4692117395Skan}
4693117395Skan
4694117395Skan/* Actually perform the appropriate conversion.  */
4695117395Skan
4696117395Skantree
4697132718Skanconvert_for_arg_passing (tree type, tree val)
4698117395Skan{
4699220150Smm  tree bitfield_type;
4700220150Smm
4701220150Smm  /* If VAL is a bitfield, then -- since it has already been converted
4702220150Smm     to TYPE -- it cannot have a precision greater than TYPE.
4703220150Smm
4704220150Smm     If it has a smaller precision, we must widen it here.  For
4705220150Smm     example, passing "int f:3;" to a function expecting an "int" will
4706220150Smm     not result in any conversion before this point.
4707220150Smm
4708220150Smm     If the precision is the same we must not risk widening.  For
4709220150Smm     example, the COMPONENT_REF for a 32-bit "long long" bitfield will
4710220150Smm     often have type "int", even though the C++ type for the field is
4711220150Smm     "long long".  If the value is being passed to a function
4712220150Smm     expecting an "int", then no conversions will be required.  But,
4713220150Smm     if we call convert_bitfield_to_declared_type, the bitfield will
4714220150Smm     be converted to "long long".  */
4715220150Smm  bitfield_type = is_bitfield_expr_with_lowered_type (val);
4716220150Smm  if (bitfield_type
4717220150Smm      && TYPE_PRECISION (TREE_TYPE (val)) < TYPE_PRECISION (type))
4718220150Smm    val = convert_to_integer (TYPE_MAIN_VARIANT (bitfield_type), val);
4719220150Smm
4720117395Skan  if (val == error_mark_node)
4721117395Skan    ;
4722117395Skan  /* Pass classes with copy ctors by invisible reference.  */
4723117395Skan  else if (TREE_ADDRESSABLE (type))
4724117395Skan    val = build1 (ADDR_EXPR, build_reference_type (type), val);
4725169689Skan  else if (targetm.calls.promote_prototypes (type)
4726117395Skan	   && INTEGRAL_TYPE_P (type)
4727132718Skan	   && COMPLETE_TYPE_P (type)
4728132718Skan	   && INT_CST_LT_UNSIGNED (TYPE_SIZE (type),
4729132718Skan				   TYPE_SIZE (integer_type_node)))
4730132718Skan    val = perform_integral_promotions (val);
4731169689Skan  if (warn_missing_format_attribute)
4732169689Skan    {
4733169689Skan      tree rhstype = TREE_TYPE (val);
4734169689Skan      const enum tree_code coder = TREE_CODE (rhstype);
4735169689Skan      const enum tree_code codel = TREE_CODE (type);
4736169689Skan      if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
4737169689Skan	  && coder == codel
4738169689Skan	  && check_missing_format_attribute (type, rhstype))
4739169689Skan	warning (OPT_Wmissing_format_attribute,
4740169689Skan		 "argument of function call might be a candidate for a format attribute");
4741169689Skan    }
4742117395Skan  return val;
4743117395Skan}
4744117395Skan
4745132718Skan/* Returns true iff FN is a function with magic varargs, i.e. ones for
4746132718Skan   which no conversions at all should be done.  This is true for some
4747132718Skan   builtins which don't act like normal functions.  */
4748132718Skan
4749132718Skanstatic bool
4750132718Skanmagic_varargs_p (tree fn)
4751132718Skan{
4752132718Skan  if (DECL_BUILT_IN (fn))
4753132718Skan    switch (DECL_FUNCTION_CODE (fn))
4754132718Skan      {
4755132718Skan      case BUILT_IN_CLASSIFY_TYPE:
4756132718Skan      case BUILT_IN_CONSTANT_P:
4757132718Skan      case BUILT_IN_NEXT_ARG:
4758132718Skan      case BUILT_IN_STDARG_START:
4759132718Skan      case BUILT_IN_VA_START:
4760132718Skan	return true;
4761132718Skan
4762132718Skan      default:;
4763132718Skan      }
4764132718Skan
4765132718Skan  return false;
4766132718Skan}
4767132718Skan
476890075Sobrien/* Subroutine of the various build_*_call functions.  Overload resolution
476990075Sobrien   has chosen a winning candidate CAND; build up a CALL_EXPR accordingly.
477090075Sobrien   ARGS is a TREE_LIST of the unconverted arguments to the call.  FLAGS is a
477190075Sobrien   bitmask of various LOOKUP_* flags which apply to the call itself.  */
477290075Sobrien
477350397Sobrienstatic tree
4774132718Skanbuild_over_call (struct z_candidate *cand, int flags)
477550397Sobrien{
477650397Sobrien  tree fn = cand->fn;
4777132718Skan  tree args = cand->args;
4778169689Skan  conversion **convs = cand->convs;
4779169689Skan  conversion *conv;
478050397Sobrien  tree converted_args = NULL_TREE;
478150397Sobrien  tree parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
4782169689Skan  tree arg, val;
478350397Sobrien  int i = 0;
478450397Sobrien  int is_method = 0;
478550397Sobrien
4786132718Skan  /* In a template, there is no need to perform all of the work that
4787132718Skan     is normally done.  We are only interested in the type of the call
4788132718Skan     expression, i.e., the return type of the function.  Any semantic
4789132718Skan     errors will be deferred until the template is instantiated.  */
4790132718Skan  if (processing_template_decl)
4791132718Skan    {
4792132718Skan      tree expr;
4793132718Skan      tree return_type;
4794132718Skan      return_type = TREE_TYPE (TREE_TYPE (fn));
4795169689Skan      expr = build3 (CALL_EXPR, return_type, fn, args, NULL_TREE);
4796132718Skan      if (TREE_THIS_VOLATILE (fn) && cfun)
4797132718Skan	current_function_returns_abnormally = 1;
4798132718Skan      if (!VOID_TYPE_P (return_type))
4799132718Skan	require_complete_type (return_type);
4800132718Skan      return convert_from_reference (expr);
4801132718Skan    }
4802132718Skan
480350397Sobrien  /* Give any warnings we noticed during overload resolution.  */
480450397Sobrien  if (cand->warnings)
4805169689Skan    {
4806169689Skan      struct candidate_warning *w;
4807169689Skan      for (w = cand->warnings; w; w = w->next)
4808169689Skan	joust (cand, w->loser, 1);
4809169689Skan    }
481050397Sobrien
481150397Sobrien  if (DECL_FUNCTION_MEMBER_P (fn))
4812132718Skan    {
4813132718Skan      /* If FN is a template function, two cases must be considered.
4814132718Skan	 For example:
481550397Sobrien
4816132718Skan	   struct A {
4817132718Skan	     protected:
4818132718Skan	       template <class T> void f();
4819132718Skan	   };
4820132718Skan	   template <class T> struct B {
4821132718Skan	     protected:
4822132718Skan	       void g();
4823132718Skan	   };
4824132718Skan	   struct C : A, B<int> {
4825132718Skan	     using A::f;	// #1
4826132718Skan	     using B<int>::g;	// #2
4827132718Skan	   };
4828132718Skan
4829132718Skan	 In case #1 where `A::f' is a member template, DECL_ACCESS is
4830132718Skan	 recorded in the primary template but not in its specialization.
4831132718Skan	 We check access of FN using its primary template.
4832132718Skan
4833132718Skan	 In case #2, where `B<int>::g' has a DECL_TEMPLATE_INFO simply
4834132718Skan	 because it is a member of class template B, DECL_ACCESS is
4835132718Skan	 recorded in the specialization `B<int>::g'.  We cannot use its
4836132718Skan	 primary template because `B<T>::g' and `B<int>::g' may have
4837132718Skan	 different access.  */
4838132718Skan      if (DECL_TEMPLATE_INFO (fn)
4839169689Skan	  && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (fn)))
4840132718Skan	perform_or_defer_access_check (cand->access_path,
4841169689Skan				       DECL_TI_TEMPLATE (fn), fn);
4842132718Skan      else
4843169689Skan	perform_or_defer_access_check (cand->access_path, fn, fn);
4844132718Skan    }
4845132718Skan
484650397Sobrien  if (args && TREE_CODE (args) != TREE_LIST)
484790075Sobrien    args = build_tree_list (NULL_TREE, args);
484850397Sobrien  arg = args;
484950397Sobrien
485050397Sobrien  /* The implicit parameters to a constructor are not considered by overload
485150397Sobrien     resolution, and must be of the proper type.  */
485250397Sobrien  if (DECL_CONSTRUCTOR_P (fn))
485350397Sobrien    {
485490075Sobrien      converted_args = tree_cons (NULL_TREE, TREE_VALUE (arg), converted_args);
485550397Sobrien      arg = TREE_CHAIN (arg);
485650397Sobrien      parm = TREE_CHAIN (parm);
4857169689Skan      /* We should never try to call the abstract constructor.  */
4858169689Skan      gcc_assert (!DECL_HAS_IN_CHARGE_PARM_P (fn));
4859169689Skan
486090075Sobrien      if (DECL_HAS_VTT_PARM_P (fn))
486118334Speter	{
486290075Sobrien	  converted_args = tree_cons
486350397Sobrien	    (NULL_TREE, TREE_VALUE (arg), converted_args);
486450397Sobrien	  arg = TREE_CHAIN (arg);
486550397Sobrien	  parm = TREE_CHAIN (parm);
486618334Speter	}
4867169689Skan    }
486850397Sobrien  /* Bypass access control for 'this' parameter.  */
486950397Sobrien  else if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
487050397Sobrien    {
487150397Sobrien      tree parmtype = TREE_VALUE (parm);
487250397Sobrien      tree argtype = TREE_TYPE (TREE_VALUE (arg));
4873117395Skan      tree converted_arg;
4874117395Skan      tree base_binfo;
487550397Sobrien
4876169689Skan      if (convs[i]->bad_p)
4877169689Skan	pedwarn ("passing %qT as %<this%> argument of %q#D discards qualifiers",
4878169689Skan		 TREE_TYPE (argtype), fn);
4879169689Skan
488052284Sobrien      /* [class.mfct.nonstatic]: If a nonstatic member function of a class
488152284Sobrien	 X is called for an object that is not of type X, or of a type
488252284Sobrien	 derived from X, the behavior is undefined.
488352284Sobrien
4884169689Skan	 So we can assume that anything passed as 'this' is non-null, and
488552284Sobrien	 optimize accordingly.  */
4886169689Skan      gcc_assert (TREE_CODE (parmtype) == POINTER_TYPE);
4887117395Skan      /* Convert to the base in which the function was declared.  */
4888169689Skan      gcc_assert (cand->conversion_path != NULL_TREE);
4889117395Skan      converted_arg = build_base_path (PLUS_EXPR,
4890117395Skan				       TREE_VALUE (arg),
4891117395Skan				       cand->conversion_path,
4892117395Skan				       1);
4893119256Skan      /* Check that the base class is accessible.  */
4894169689Skan      if (!accessible_base_p (TREE_TYPE (argtype),
4895169689Skan			      BINFO_TYPE (cand->conversion_path), true))
4896169689Skan	error ("%qT is not an accessible base of %qT",
4897119256Skan	       BINFO_TYPE (cand->conversion_path),
4898119256Skan	       TREE_TYPE (argtype));
4899117395Skan      /* If fn was found by a using declaration, the conversion path
4900169689Skan	 will be to the derived class, not the base declaring fn. We
4901169689Skan	 must convert from derived to base.  */
4902117395Skan      base_binfo = lookup_base (TREE_TYPE (TREE_TYPE (converted_arg)),
4903169689Skan				TREE_TYPE (parmtype), ba_unique, NULL);
4904117395Skan      converted_arg = build_base_path (PLUS_EXPR, converted_arg,
4905117395Skan				       base_binfo, 1);
4906169689Skan
4907117395Skan      converted_args = tree_cons (NULL_TREE, converted_arg, converted_args);
490850397Sobrien      parm = TREE_CHAIN (parm);
490950397Sobrien      arg = TREE_CHAIN (arg);
491050397Sobrien      ++i;
491150397Sobrien      is_method = 1;
491218334Speter    }
491350397Sobrien
491450397Sobrien  for (; arg && parm;
491550397Sobrien       parm = TREE_CHAIN (parm), arg = TREE_CHAIN (arg), ++i)
491618334Speter    {
491750397Sobrien      tree type = TREE_VALUE (parm);
491818334Speter
4919169689Skan      conv = convs[i];
4920169689Skan
4921169689Skan      /* Don't make a copy here if build_call is going to.  */
4922169689Skan      if (conv->kind == ck_rvalue
4923169689Skan	  && !TREE_ADDRESSABLE (complete_type (type)))
4924169689Skan	conv = conv->u.next;
4925169689Skan
492690075Sobrien      val = convert_like_with_context
492790075Sobrien	(conv, TREE_VALUE (arg), fn, i - is_method);
492818334Speter
4929117395Skan      val = convert_for_arg_passing (type, val);
493090075Sobrien      converted_args = tree_cons (NULL_TREE, val, converted_args);
493118334Speter    }
493218334Speter
493350397Sobrien  /* Default arguments */
493490075Sobrien  for (; parm && parm != void_list_node; parm = TREE_CHAIN (parm), i++)
4935169689Skan    converted_args
4936169689Skan      = tree_cons (NULL_TREE,
4937169689Skan		   convert_default_arg (TREE_VALUE (parm),
493890075Sobrien					TREE_PURPOSE (parm),
493990075Sobrien					fn, i - is_method),
494090075Sobrien		   converted_args);
494118334Speter
494250397Sobrien  /* Ellipsis */
494350397Sobrien  for (; arg; arg = TREE_CHAIN (arg))
4944132718Skan    {
4945132718Skan      tree a = TREE_VALUE (arg);
4946132718Skan      if (magic_varargs_p (fn))
4947132718Skan	/* Do no conversions for magic varargs.  */;
4948132718Skan      else
4949132718Skan	a = convert_arg_to_ellipsis (a);
4950132718Skan      converted_args = tree_cons (NULL_TREE, a, converted_args);
4951132718Skan    }
495250397Sobrien
495350397Sobrien  converted_args = nreverse (converted_args);
495450397Sobrien
4955169689Skan  check_function_arguments (TYPE_ATTRIBUTES (TREE_TYPE (fn)),
4956169689Skan			    converted_args, TYPE_ARG_TYPES (TREE_TYPE (fn)));
495752284Sobrien
495850397Sobrien  /* Avoid actually calling copy constructors and copy assignment operators,
495950397Sobrien     if possible.  */
496052284Sobrien
496152284Sobrien  if (! flag_elide_constructors)
496252284Sobrien    /* Do things the hard way.  */;
4963169689Skan  else if (cand->num_convs == 1 && DECL_COPY_CONSTRUCTOR_P (fn))
496450397Sobrien    {
496550397Sobrien      tree targ;
496690075Sobrien      arg = skip_artificial_parms_for (fn, converted_args);
496750397Sobrien      arg = TREE_VALUE (arg);
496850397Sobrien
496950397Sobrien      /* Pull out the real argument, disregarding const-correctness.  */
497050397Sobrien      targ = arg;
497150397Sobrien      while (TREE_CODE (targ) == NOP_EXPR
497250397Sobrien	     || TREE_CODE (targ) == NON_LVALUE_EXPR
497350397Sobrien	     || TREE_CODE (targ) == CONVERT_EXPR)
497450397Sobrien	targ = TREE_OPERAND (targ, 0);
497550397Sobrien      if (TREE_CODE (targ) == ADDR_EXPR)
497650397Sobrien	{
497750397Sobrien	  targ = TREE_OPERAND (targ, 0);
4978169689Skan	  if (!same_type_ignoring_top_level_qualifiers_p
497990075Sobrien	      (TREE_TYPE (TREE_TYPE (arg)), TREE_TYPE (targ)))
498050397Sobrien	    targ = NULL_TREE;
498150397Sobrien	}
498218334Speter      else
498350397Sobrien	targ = NULL_TREE;
498418334Speter
498550397Sobrien      if (targ)
498650397Sobrien	arg = targ;
498750397Sobrien      else
498850397Sobrien	arg = build_indirect_ref (arg, 0);
498918334Speter
499050397Sobrien      /* [class.copy]: the copy constructor is implicitly defined even if
499150397Sobrien	 the implementation elided its use.  */
499250397Sobrien      if (TYPE_HAS_COMPLEX_INIT_REF (DECL_CONTEXT (fn)))
499350397Sobrien	mark_used (fn);
499450397Sobrien
499550397Sobrien      /* If we're creating a temp and we already have one, don't create a
4996169689Skan	 new one.  If we're not creating a temp but we get one, use
4997169689Skan	 INIT_EXPR to collapse the temp into our target.  Otherwise, if the
4998169689Skan	 ctor is trivial, do a bitwise copy with a simple TARGET_EXPR for a
4999169689Skan	 temp or an INIT_EXPR otherwise.  */
500050397Sobrien      if (integer_zerop (TREE_VALUE (args)))
500118334Speter	{
5002117395Skan	  if (TREE_CODE (arg) == TARGET_EXPR)
500350397Sobrien	    return arg;
500450397Sobrien	  else if (TYPE_HAS_TRIVIAL_INIT_REF (DECL_CONTEXT (fn)))
500590075Sobrien	    return build_target_expr_with_type (arg, DECL_CONTEXT (fn));
500618334Speter	}
5007117395Skan      else if (TREE_CODE (arg) == TARGET_EXPR
5008117395Skan	       || TYPE_HAS_TRIVIAL_INIT_REF (DECL_CONTEXT (fn)))
500950397Sobrien	{
501050397Sobrien	  tree to = stabilize_reference
501150397Sobrien	    (build_indirect_ref (TREE_VALUE (args), 0));
501218334Speter
5013169689Skan	  val = build2 (INIT_EXPR, DECL_CONTEXT (fn), to, arg);
5014132718Skan	  return val;
501550397Sobrien	}
501618334Speter    }
501790075Sobrien  else if (DECL_OVERLOADED_OPERATOR_P (fn) == NOP_EXPR
501890075Sobrien	   && copy_fn_p (fn)
501990075Sobrien	   && TYPE_HAS_TRIVIAL_ASSIGN_REF (DECL_CONTEXT (fn)))
502050397Sobrien    {
502150397Sobrien      tree to = stabilize_reference
502250397Sobrien	(build_indirect_ref (TREE_VALUE (converted_args), 0));
5023132718Skan      tree type = TREE_TYPE (to);
5024132718Skan      tree as_base = CLASSTYPE_AS_BASE (type);
502518334Speter
5026169689Skan      arg = TREE_VALUE (TREE_CHAIN (converted_args));
5027132718Skan      if (tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (as_base)))
5028169689Skan	{
5029169689Skan	  arg = build_indirect_ref (arg, 0);
5030169689Skan	  val = build2 (MODIFY_EXPR, TREE_TYPE (to), to, arg);
5031169689Skan	}
5032132718Skan      else
5033132718Skan	{
5034169689Skan	  /* We must only copy the non-tail padding parts.
5035169689Skan	     Use __builtin_memcpy for the bitwise copy.  */
5036132718Skan
5037169689Skan	  tree args, t;
5038132718Skan
5039169689Skan	  args = tree_cons (NULL, TYPE_SIZE_UNIT (as_base), NULL);
5040169689Skan	  args = tree_cons (NULL, arg, args);
5041169689Skan	  t = build_unary_op (ADDR_EXPR, to, 0);
5042169689Skan	  args = tree_cons (NULL, t, args);
5043169689Skan	  t = implicit_built_in_decls[BUILT_IN_MEMCPY];
5044169689Skan	  t = build_call (t, args);
5045132718Skan
5046169689Skan	  t = convert (TREE_TYPE (TREE_VALUE (args)), t);
5047169689Skan	  val = build_indirect_ref (t, 0);
5048169689Skan	}
5049132718Skan
505050397Sobrien      return val;
505150397Sobrien    }
505250397Sobrien
505350397Sobrien  mark_used (fn);
505450397Sobrien
505590075Sobrien  if (DECL_VINDEX (fn) && (flags & LOOKUP_NONVIRTUAL) == 0)
505618334Speter    {
505750397Sobrien      tree t, *p = &TREE_VALUE (converted_args);
505890075Sobrien      tree binfo = lookup_base (TREE_TYPE (TREE_TYPE (*p)),
5059117395Skan				DECL_CONTEXT (fn),
506090075Sobrien				ba_any, NULL);
5061169689Skan      gcc_assert (binfo && binfo != error_mark_node);
5062169689Skan
506390075Sobrien      *p = build_base_path (PLUS_EXPR, *p, binfo, 1);
506450397Sobrien      if (TREE_SIDE_EFFECTS (*p))
506550397Sobrien	*p = save_expr (*p);
506650397Sobrien      t = build_pointer_type (TREE_TYPE (fn));
506790075Sobrien      if (DECL_CONTEXT (fn) && TYPE_JAVA_INTERFACE (DECL_CONTEXT (fn)))
506890075Sobrien	fn = build_java_interface_fn_ref (fn, *p);
506990075Sobrien      else
5070169689Skan	fn = build_vfn_ref (*p, DECL_VINDEX (fn));
507150397Sobrien      TREE_TYPE (fn) = t;
507218334Speter    }
507350397Sobrien  else if (DECL_INLINE (fn))
507450397Sobrien    fn = inline_conversion (fn);
507550397Sobrien  else
507650397Sobrien    fn = build_addr_func (fn);
507718334Speter
5078169689Skan  return build_cxx_call (fn, converted_args);
5079117395Skan}
5080117395Skan
5081169689Skan/* Build and return a call to FN, using ARGS.  This function performs
5082117395Skan   no overload resolution, conversion, or other high-level
5083117395Skan   operations.  */
5084117395Skan
5085117395Skantree
5086169689Skanbuild_cxx_call (tree fn, tree args)
5087117395Skan{
5088117395Skan  tree fndecl;
5089117395Skan
5090169689Skan  fn = build_call (fn, args);
509118334Speter
5092117395Skan  /* If this call might throw an exception, note that fact.  */
5093117395Skan  fndecl = get_callee_fndecl (fn);
5094169689Skan  if ((!fndecl || !TREE_NOTHROW (fndecl))
5095117395Skan      && at_function_scope_p ()
5096117395Skan      && cfun)
5097117395Skan    cp_function_chain->can_throw = 1;
5098117395Skan
5099117395Skan  /* Some built-in function calls will be evaluated at compile-time in
5100117395Skan     fold ().  */
5101169689Skan  fn = fold_if_not_in_template (fn);
5102117395Skan
510390075Sobrien  if (VOID_TYPE_P (TREE_TYPE (fn)))
510450397Sobrien    return fn;
5105117395Skan
510650397Sobrien  fn = require_complete_type (fn);
510790075Sobrien  if (fn == error_mark_node)
510890075Sobrien    return error_mark_node;
5109117395Skan
511050397Sobrien  if (IS_AGGR_TYPE (TREE_TYPE (fn)))
511150397Sobrien    fn = build_cplus_new (TREE_TYPE (fn), fn);
511250397Sobrien  return convert_from_reference (fn);
511318334Speter}
511418334Speter
5115117395Skanstatic GTY(()) tree java_iface_lookup_fn;
511690075Sobrien
511790075Sobrien/* Make an expression which yields the address of the Java interface
511890075Sobrien   method FN.  This is achieved by generating a call to libjava's
511990075Sobrien   _Jv_LookupInterfaceMethodIdx().  */
512090075Sobrien
512150397Sobrienstatic tree
5122132718Skanbuild_java_interface_fn_ref (tree fn, tree instance)
512390075Sobrien{
512490075Sobrien  tree lookup_args, lookup_fn, method, idx;
512590075Sobrien  tree klass_ref, iface, iface_ref;
512690075Sobrien  int i;
5127169689Skan
512890075Sobrien  if (!java_iface_lookup_fn)
512990075Sobrien    {
513090075Sobrien      tree endlink = build_void_list_node ();
513190075Sobrien      tree t = tree_cons (NULL_TREE, ptr_type_node,
513290075Sobrien			  tree_cons (NULL_TREE, ptr_type_node,
513390075Sobrien				     tree_cons (NULL_TREE, java_int_type_node,
513490075Sobrien						endlink)));
5135169689Skan      java_iface_lookup_fn
513690075Sobrien	= builtin_function ("_Jv_LookupInterfaceMethodIdx",
513790075Sobrien			    build_function_type (ptr_type_node, t),
5138117395Skan			    0, NOT_BUILT_IN, NULL, NULL_TREE);
513990075Sobrien    }
514090075Sobrien
5141169689Skan  /* Look up the pointer to the runtime java.lang.Class object for `instance'.
5142117395Skan     This is the first entry in the vtable.  */
5143169689Skan  klass_ref = build_vtbl_ref (build_indirect_ref (instance, 0),
514490075Sobrien			      integer_zero_node);
514590075Sobrien
5146117395Skan  /* Get the java.lang.Class pointer for the interface being called.  */
514790075Sobrien  iface = DECL_CONTEXT (fn);
5148132718Skan  iface_ref = lookup_field (iface, get_identifier ("class$"), 0, false);
514990075Sobrien  if (!iface_ref || TREE_CODE (iface_ref) != VAR_DECL
515090075Sobrien      || DECL_CONTEXT (iface_ref) != iface)
515190075Sobrien    {
5152169689Skan      error ("could not find class$ field in java interface type %qT",
515390075Sobrien		iface);
515490075Sobrien      return error_mark_node;
515590075Sobrien    }
5156169689Skan  iface_ref = build_address (iface_ref);
5157169689Skan  iface_ref = convert (build_pointer_type (iface), iface_ref);
5158169689Skan
5159117395Skan  /* Determine the itable index of FN.  */
516090075Sobrien  i = 1;
516190075Sobrien  for (method = TYPE_METHODS (iface); method; method = TREE_CHAIN (method))
516290075Sobrien    {
516390075Sobrien      if (!DECL_VIRTUAL_P (method))
5164169689Skan	continue;
516590075Sobrien      if (fn == method)
5166169689Skan	break;
516790075Sobrien      i++;
516890075Sobrien    }
5169169689Skan  idx = build_int_cst (NULL_TREE, i);
517090075Sobrien
5171169689Skan  lookup_args = tree_cons (NULL_TREE, klass_ref,
517290075Sobrien			   tree_cons (NULL_TREE, iface_ref,
517390075Sobrien				      build_tree_list (NULL_TREE, idx)));
5174169689Skan  lookup_fn = build1 (ADDR_EXPR,
517590075Sobrien		      build_pointer_type (TREE_TYPE (java_iface_lookup_fn)),
517690075Sobrien		      java_iface_lookup_fn);
5177169689Skan  return build3 (CALL_EXPR, ptr_type_node, lookup_fn, lookup_args, NULL_TREE);
517890075Sobrien}
517990075Sobrien
518090075Sobrien/* Returns the value to use for the in-charge parameter when making a
5181169689Skan   call to a function with the indicated NAME.
518290075Sobrien
5183169689Skan   FIXME:Can't we find a neater way to do this mapping?  */
5184169689Skan
518590075Sobrientree
5186132718Skanin_charge_arg_for_name (tree name)
518790075Sobrien{
5188169689Skan if (name == base_ctor_identifier
518990075Sobrien      || name == base_dtor_identifier)
519090075Sobrien    return integer_zero_node;
519190075Sobrien  else if (name == complete_ctor_identifier)
519290075Sobrien    return integer_one_node;
519390075Sobrien  else if (name == complete_dtor_identifier)
519490075Sobrien    return integer_two_node;
519590075Sobrien  else if (name == deleting_dtor_identifier)
519690075Sobrien    return integer_three_node;
519790075Sobrien
519890075Sobrien  /* This function should only be called with one of the names listed
519990075Sobrien     above.  */
5200169689Skan  gcc_unreachable ();
520190075Sobrien  return NULL_TREE;
520290075Sobrien}
520390075Sobrien
5204117395Skan/* Build a call to a constructor, destructor, or an assignment
5205117395Skan   operator for INSTANCE, an expression with class type.  NAME
5206117395Skan   indicates the special member function to call; ARGS are the
5207117395Skan   arguments.  BINFO indicates the base of INSTANCE that is to be
5208117395Skan   passed as the `this' parameter to the member function called.
5209117395Skan
5210117395Skan   FLAGS are the LOOKUP_* flags to use when processing the call.
5211117395Skan
5212117395Skan   If NAME indicates a complete object constructor, INSTANCE may be
5213117395Skan   NULL_TREE.  In this case, the caller will call build_cplus_new to
5214117395Skan   store the newly constructed object into a VAR_DECL.  */
5215117395Skan
5216117395Skantree
5217169689Skanbuild_special_member_call (tree instance, tree name, tree args,
5218117395Skan			   tree binfo, int flags)
521950397Sobrien{
5220117395Skan  tree fns;
5221117395Skan  /* The type of the subobject to be constructed or destroyed.  */
5222117395Skan  tree class_type;
5223117395Skan
5224169689Skan  gcc_assert (name == complete_ctor_identifier
5225169689Skan	      || name == base_ctor_identifier
5226169689Skan	      || name == complete_dtor_identifier
5227169689Skan	      || name == base_dtor_identifier
5228169689Skan	      || name == deleting_dtor_identifier
5229169689Skan	      || name == ansi_assopname (NOP_EXPR));
5230169689Skan  if (TYPE_P (binfo))
5231169689Skan    {
5232169689Skan      /* Resolve the name.  */
5233169689Skan      if (!complete_type_or_else (binfo, NULL_TREE))
5234169689Skan	return error_mark_node;
5235117395Skan
5236169689Skan      binfo = TYPE_BINFO (binfo);
5237169689Skan    }
5238169689Skan
5239169689Skan  gcc_assert (binfo != NULL_TREE);
5240169689Skan
5241117395Skan  class_type = BINFO_TYPE (binfo);
5242117395Skan
5243117395Skan  /* Handle the special case where INSTANCE is NULL_TREE.  */
5244117395Skan  if (name == complete_ctor_identifier && !instance)
5245117395Skan    {
5246169689Skan      instance = build_int_cst (build_pointer_type (class_type), 0);
5247117395Skan      instance = build1 (INDIRECT_REF, class_type, instance);
5248117395Skan    }
5249119256Skan  else
5250119256Skan    {
5251169689Skan      if (name == complete_dtor_identifier
5252119256Skan	  || name == base_dtor_identifier
5253119256Skan	  || name == deleting_dtor_identifier)
5254169689Skan	gcc_assert (args == NULL_TREE);
5255117395Skan
5256132718Skan      /* Convert to the base class, if necessary.  */
5257169689Skan      if (!same_type_ignoring_top_level_qualifiers_p
5258119256Skan	  (TREE_TYPE (instance), BINFO_TYPE (binfo)))
5259132718Skan	{
5260132718Skan	  if (name != ansi_assopname (NOP_EXPR))
5261132718Skan	    /* For constructors and destructors, either the base is
5262132718Skan	       non-virtual, or it is virtual but we are doing the
5263132718Skan	       conversion from a constructor or destructor for the
5264132718Skan	       complete object.  In either case, we can convert
5265132718Skan	       statically.  */
5266132718Skan	    instance = convert_to_base_statically (instance, binfo);
5267132718Skan	  else
5268132718Skan	    /* However, for assignment operators, we must convert
5269132718Skan	       dynamically if the base is virtual.  */
5270132718Skan	    instance = build_base_path (PLUS_EXPR, instance,
5271132718Skan					binfo, /*nonnull=*/1);
5272132718Skan	}
5273119256Skan    }
5274117395Skan
5275169689Skan  gcc_assert (instance != NULL_TREE);
5276117395Skan
5277117395Skan  fns = lookup_fnfields (binfo, name, 1);
5278169689Skan
5279117395Skan  /* When making a call to a constructor or destructor for a subobject
5280117395Skan     that uses virtual base classes, pass down a pointer to a VTT for
5281117395Skan     the subobject.  */
5282117395Skan  if ((name == base_ctor_identifier
5283117395Skan       || name == base_dtor_identifier)
5284169689Skan      && CLASSTYPE_VBASECLASSES (class_type))
5285117395Skan    {
5286117395Skan      tree vtt;
5287117395Skan      tree sub_vtt;
5288117395Skan
5289117395Skan      /* If the current function is a complete object constructor
5290117395Skan	 or destructor, then we fetch the VTT directly.
5291117395Skan	 Otherwise, we look it up using the VTT we were given.  */
5292117395Skan      vtt = TREE_CHAIN (CLASSTYPE_VTABLES (current_class_type));
5293117395Skan      vtt = decay_conversion (vtt);
5294169689Skan      vtt = build3 (COND_EXPR, TREE_TYPE (vtt),
5295169689Skan		    build2 (EQ_EXPR, boolean_type_node,
5296169689Skan			    current_in_charge_parm, integer_zero_node),
5297169689Skan		    current_vtt_parm,
5298169689Skan		    vtt);
5299169689Skan      gcc_assert (BINFO_SUBVTT_INDEX (binfo));
5300169689Skan      sub_vtt = build2 (PLUS_EXPR, TREE_TYPE (vtt), vtt,
5301169689Skan			BINFO_SUBVTT_INDEX (binfo));
5302117395Skan
5303117395Skan      args = tree_cons (NULL_TREE, sub_vtt, args);
5304117395Skan    }
5305117395Skan
5306169689Skan  return build_new_method_call (instance, fns, args,
5307169689Skan				TYPE_BINFO (BINFO_TYPE (binfo)),
5308169689Skan				flags, /*fn=*/NULL);
5309117395Skan}
5310117395Skan
5311132718Skan/* Return the NAME, as a C string.  The NAME indicates a function that
5312132718Skan   is a member of TYPE.  *FREE_P is set to true if the caller must
5313169689Skan   free the memory returned.
5314132718Skan
5315132718Skan   Rather than go through all of this, we should simply set the names
5316132718Skan   of constructors and destructors appropriately, and dispense with
5317132718Skan   ctor_identifier, dtor_identifier, etc.  */
5318132718Skan
5319132718Skanstatic char *
5320132718Skanname_as_c_string (tree name, tree type, bool *free_p)
5321132718Skan{
5322132718Skan  char *pretty_name;
5323132718Skan
5324132718Skan  /* Assume that we will not allocate memory.  */
5325132718Skan  *free_p = false;
5326132718Skan  /* Constructors and destructors are special.  */
5327132718Skan  if (IDENTIFIER_CTOR_OR_DTOR_P (name))
5328132718Skan    {
5329169689Skan      pretty_name
5330132718Skan	= (char *) IDENTIFIER_POINTER (constructor_name (type));
5331132718Skan      /* For a destructor, add the '~'.  */
5332132718Skan      if (name == complete_dtor_identifier
5333132718Skan	  || name == base_dtor_identifier
5334132718Skan	  || name == deleting_dtor_identifier)
5335132718Skan	{
5336132718Skan	  pretty_name = concat ("~", pretty_name, NULL);
5337132718Skan	  /* Remember that we need to free the memory allocated.  */
5338132718Skan	  *free_p = true;
5339132718Skan	}
5340132718Skan    }
5341132718Skan  else if (IDENTIFIER_TYPENAME_P (name))
5342132718Skan    {
5343132718Skan      pretty_name = concat ("operator ",
5344132718Skan			    type_as_string (TREE_TYPE (name),
5345132718Skan					    TFF_PLAIN_IDENTIFIER),
5346132718Skan			    NULL);
5347132718Skan      /* Remember that we need to free the memory allocated.  */
5348132718Skan      *free_p = true;
5349132718Skan    }
5350132718Skan  else
5351132718Skan    pretty_name = (char *) IDENTIFIER_POINTER (name);
5352132718Skan
5353132718Skan  return pretty_name;
5354132718Skan}
5355132718Skan
5356169689Skan/* Build a call to "INSTANCE.FN (ARGS)".  If FN_P is non-NULL, it will
5357169689Skan   be set, upon return, to the function called.  */
5358117395Skan
5359117395Skantree
5360169689Skanbuild_new_method_call (tree instance, tree fns, tree args,
5361169689Skan		       tree conversion_path, int flags,
5362169689Skan		       tree *fn_p)
5363117395Skan{
536450397Sobrien  struct z_candidate *candidates = 0, *cand;
536550397Sobrien  tree explicit_targs = NULL_TREE;
5366117395Skan  tree basetype = NULL_TREE;
5367117395Skan  tree access_binfo;
5368117395Skan  tree optype;
5369117395Skan  tree mem_args = NULL_TREE, instance_ptr;
5370132718Skan  tree name;
537190075Sobrien  tree user_args;
537290075Sobrien  tree call;
5373132718Skan  tree fn;
5374132718Skan  tree class_type;
537550397Sobrien  int template_only = 0;
5376132718Skan  bool any_viable_p;
5377132718Skan  tree orig_instance;
5378132718Skan  tree orig_fns;
5379132718Skan  tree orig_args;
5380169689Skan  void *p;
538118334Speter
5382169689Skan  gcc_assert (instance != NULL_TREE);
538352284Sobrien
5384169689Skan  /* We don't know what function we're going to call, yet.  */
5385169689Skan  if (fn_p)
5386169689Skan    *fn_p = NULL_TREE;
5387169689Skan
5388169689Skan  if (error_operand_p (instance)
5389132718Skan      || error_operand_p (fns)
5390117395Skan      || args == error_mark_node)
5391117395Skan    return error_mark_node;
539218334Speter
5393117395Skan  if (!BASELINK_P (fns))
539450397Sobrien    {
5395169689Skan      error ("call to non-function %qD", fns);
5396117395Skan      return error_mark_node;
5397117395Skan    }
539818334Speter
5399169689Skan  orig_instance = instance;
5400169689Skan  orig_fns = fns;
5401169689Skan  orig_args = args;
5402169689Skan
5403169689Skan  /* Dismantle the baselink to collect all the information we need.  */
5404117395Skan  if (!conversion_path)
5405117395Skan    conversion_path = BASELINK_BINFO (fns);
5406117395Skan  access_binfo = BASELINK_ACCESS_BINFO (fns);
5407117395Skan  optype = BASELINK_OPTYPE (fns);
5408117395Skan  fns = BASELINK_FUNCTIONS (fns);
5409117395Skan  if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
5410117395Skan    {
5411117395Skan      explicit_targs = TREE_OPERAND (fns, 1);
5412117395Skan      fns = TREE_OPERAND (fns, 0);
5413117395Skan      template_only = 1;
541418334Speter    }
5415169689Skan  gcc_assert (TREE_CODE (fns) == FUNCTION_DECL
5416169689Skan	      || TREE_CODE (fns) == TEMPLATE_DECL
5417169689Skan	      || TREE_CODE (fns) == OVERLOAD);
5418169689Skan  fn = get_first_fn (fns);
5419169689Skan  name = DECL_NAME (fn);
542018334Speter
5421169689Skan  basetype = TYPE_MAIN_VARIANT (TREE_TYPE (instance));
5422169689Skan  gcc_assert (CLASS_TYPE_P (basetype));
542350397Sobrien
5424169689Skan  if (processing_template_decl)
542518334Speter    {
5426169689Skan      instance = build_non_dependent_expr (instance);
5427169689Skan      args = build_non_dependent_args (orig_args);
542818334Speter    }
542918334Speter
5430169689Skan  /* The USER_ARGS are the arguments we will display to users if an
5431169689Skan     error occurs.  The USER_ARGS should not include any
5432169689Skan     compiler-generated arguments.  The "this" pointer hasn't been
5433169689Skan     added yet.  However, we must remove the VTT pointer if this is a
5434169689Skan     call to a base-class constructor or destructor.  */
5435169689Skan  user_args = args;
543690075Sobrien  if (IDENTIFIER_CTOR_OR_DTOR_P (name))
543790075Sobrien    {
5438117395Skan      /* Callers should explicitly indicate whether they want to construct
5439117395Skan	 the complete object or just the part without virtual bases.  */
5440169689Skan      gcc_assert (name != ctor_identifier);
5441117395Skan      /* Similarly for destructors.  */
5442169689Skan      gcc_assert (name != dtor_identifier);
5443169689Skan      /* Remove the VTT pointer, if present.  */
5444169689Skan      if ((name == base_ctor_identifier || name == base_dtor_identifier)
5445169689Skan	  && CLASSTYPE_VBASECLASSES (basetype))
5446169689Skan	user_args = TREE_CHAIN (user_args);
544790075Sobrien    }
544890075Sobrien
5449169689Skan  /* Process the argument list.  */
5450169689Skan  args = resolve_args (args);
5451169689Skan  if (args == error_mark_node)
5452169689Skan    return error_mark_node;
5453169689Skan
5454169689Skan  instance_ptr = build_this (instance);
5455169689Skan
5456132718Skan  /* It's OK to call destructors on cv-qualified objects.  Therefore,
5457132718Skan     convert the INSTANCE_PTR to the unqualified type, if necessary.  */
5458132718Skan  if (DECL_DESTRUCTOR_P (fn))
545918334Speter    {
5460132718Skan      tree type = build_pointer_type (basetype);
5461132718Skan      if (!same_type_p (type, TREE_TYPE (instance_ptr)))
5462132718Skan	instance_ptr = build_nop (type, instance_ptr);
5463169689Skan      name = complete_dtor_identifier;
5464132718Skan    }
5465117395Skan
5466132718Skan  class_type = (conversion_path ? BINFO_TYPE (conversion_path) : NULL_TREE);
5467132718Skan  mem_args = tree_cons (NULL_TREE, instance_ptr, args);
546850397Sobrien
5469169689Skan  /* Get the high-water mark for the CONVERSION_OBSTACK.  */
5470169689Skan  p = conversion_obstack_alloc (0);
5471169689Skan
5472132718Skan  for (fn = fns; fn; fn = OVL_NEXT (fn))
5473132718Skan    {
5474132718Skan      tree t = OVL_CURRENT (fn);
5475132718Skan      tree this_arglist;
547690075Sobrien
5477132718Skan      /* We can end up here for copy-init of same or base class.  */
5478132718Skan      if ((flags & LOOKUP_ONLYCONVERTING)
5479132718Skan	  && DECL_NONCONVERTING_P (t))
5480132718Skan	continue;
548150397Sobrien
5482132718Skan      if (DECL_NONSTATIC_MEMBER_FUNCTION_P (t))
5483132718Skan	this_arglist = mem_args;
5484132718Skan      else
5485132718Skan	this_arglist = args;
5486132718Skan
5487132718Skan      if (TREE_CODE (t) == TEMPLATE_DECL)
5488132718Skan	/* A member template.  */
5489169689Skan	add_template_candidate (&candidates, t,
5490132718Skan				class_type,
5491132718Skan				explicit_targs,
5492132718Skan				this_arglist, optype,
5493169689Skan				access_binfo,
5494132718Skan				conversion_path,
5495132718Skan				flags,
5496132718Skan				DEDUCE_CALL);
5497132718Skan      else if (! template_only)
5498169689Skan	add_function_candidate (&candidates, t,
5499132718Skan				class_type,
5500132718Skan				this_arglist,
5501132718Skan				access_binfo,
5502132718Skan				conversion_path,
5503132718Skan				flags);
550418334Speter    }
550518334Speter
5506132718Skan  candidates = splice_viable (candidates, pedantic, &any_viable_p);
5507132718Skan  if (!any_viable_p)
550818334Speter    {
550990075Sobrien      if (!COMPLETE_TYPE_P (basetype))
5510117395Skan	cxx_incomplete_type_error (instance_ptr, basetype);
551152284Sobrien      else
5512132718Skan	{
5513132718Skan	  char *pretty_name;
5514132718Skan	  bool free_p;
5515132718Skan
5516132718Skan	  pretty_name = name_as_c_string (name, basetype, &free_p);
5517169689Skan	  error ("no matching function for call to %<%T::%s(%A)%#V%>",
5518132718Skan		 basetype, pretty_name, user_args,
5519132718Skan		 TREE_TYPE (TREE_TYPE (instance_ptr)));
5520132718Skan	  if (free_p)
5521132718Skan	    free (pretty_name);
5522132718Skan	}
552350397Sobrien      print_z_candidates (candidates);
5524169689Skan      call = error_mark_node;
552518334Speter    }
5526169689Skan  else
552718334Speter    {
5528169689Skan      cand = tourney (candidates);
5529169689Skan      if (cand == 0)
5530169689Skan	{
5531169689Skan	  char *pretty_name;
5532169689Skan	  bool free_p;
5533132718Skan
5534169689Skan	  pretty_name = name_as_c_string (name, basetype, &free_p);
5535169689Skan	  error ("call of overloaded %<%s(%A)%> is ambiguous", pretty_name,
5536169689Skan		 user_args);
5537169689Skan	  print_z_candidates (candidates);
5538169689Skan	  if (free_p)
5539169689Skan	    free (pretty_name);
5540169689Skan	  call = error_mark_node;
5541169689Skan	}
5542169689Skan      else
5543169689Skan	{
5544169689Skan	  fn = cand->fn;
554518334Speter
5546169689Skan	  if (!(flags & LOOKUP_NONVIRTUAL)
5547169689Skan	      && DECL_PURE_VIRTUAL_P (fn)
5548169689Skan	      && instance == current_class_ref
5549169689Skan	      && (DECL_CONSTRUCTOR_P (current_function_decl)
5550169689Skan		  || DECL_DESTRUCTOR_P (current_function_decl)))
5551169689Skan	    /* This is not an error, it is runtime undefined
5552169689Skan	       behavior.  */
5553169689Skan	    warning (0, (DECL_CONSTRUCTOR_P (current_function_decl) ?
5554169689Skan		      "abstract virtual %q#D called from constructor"
5555169689Skan		      : "abstract virtual %q#D called from destructor"),
5556169689Skan		     fn);
5557169689Skan
5558169689Skan	  if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE
5559169689Skan	      && is_dummy_object (instance_ptr))
5560169689Skan	    {
5561169689Skan	      error ("cannot call member function %qD without object",
5562169689Skan		     fn);
5563169689Skan	      call = error_mark_node;
5564169689Skan	    }
5565169689Skan	  else
5566169689Skan	    {
5567169689Skan	      if (DECL_VINDEX (fn) && ! (flags & LOOKUP_NONVIRTUAL)
5568169689Skan		  && resolves_to_fixed_type_p (instance, 0))
5569169689Skan		flags |= LOOKUP_NONVIRTUAL;
5570169689Skan	      /* Now we know what function is being called.  */
5571169689Skan	      if (fn_p)
5572169689Skan		*fn_p = fn;
5573169689Skan	      /* Build the actual CALL_EXPR.  */
5574169689Skan	      call = build_over_call (cand, flags);
5575169689Skan	      /* In an expression of the form `a->f()' where `f' turns
5576169689Skan		 out to be a static member function, `a' is
5577169689Skan		 none-the-less evaluated.  */
5578169689Skan	      if (TREE_CODE (TREE_TYPE (fn)) != METHOD_TYPE
5579169689Skan		  && !is_dummy_object (instance_ptr)
5580169689Skan		  && TREE_SIDE_EFFECTS (instance_ptr))
5581169689Skan		call = build2 (COMPOUND_EXPR, TREE_TYPE (call),
5582169689Skan			       instance_ptr, call);
5583169689Skan	      else if (call != error_mark_node
5584169689Skan		       && DECL_DESTRUCTOR_P (cand->fn)
5585169689Skan		       && !VOID_TYPE_P (TREE_TYPE (call)))
5586169689Skan		/* An explicit call of the form "x->~X()" has type
5587169689Skan		   "void".  However, on platforms where destructors
5588169689Skan		   return "this" (i.e., those where
5589169689Skan		   targetm.cxx.cdtor_returns_this is true), such calls
5590169689Skan		   will appear to have a return value of pointer type
5591169689Skan		   to the low-level call machinery.  We do not want to
5592169689Skan		   change the low-level machinery, since we want to be
5593169689Skan		   able to optimize "delete f()" on such platforms as
5594169689Skan		   "operator delete(~X(f()))" (rather than generating
5595169689Skan		   "t = f(), ~X(t), operator delete (t)").  */
5596169689Skan		call = build_nop (void_type_node, call);
5597169689Skan	    }
5598169689Skan	}
559952284Sobrien    }
560050397Sobrien
5601169689Skan  if (processing_template_decl && call != error_mark_node)
5602169689Skan    call = (build_min_non_dep
5603169689Skan	    (CALL_EXPR, call,
5604169689Skan	     build_min_nt (COMPONENT_REF, orig_instance, orig_fns, NULL_TREE),
5605169689Skan	     orig_args, NULL_TREE));
560650397Sobrien
5607169689Skan /* Free all the conversions we allocated.  */
5608169689Skan  obstack_free (&conversion_obstack, p);
5609169689Skan
561090075Sobrien  return call;
561150397Sobrien}
561250397Sobrien
5613132718Skan/* Returns true iff standard conversion sequence ICS1 is a proper
561450397Sobrien   subsequence of ICS2.  */
561550397Sobrien
5616132718Skanstatic bool
5617169689Skanis_subseq (conversion *ics1, conversion *ics2)
561850397Sobrien{
561950397Sobrien  /* We can assume that a conversion of the same code
562050397Sobrien     between the same types indicates a subsequence since we only get
562150397Sobrien     here if the types we are converting from are the same.  */
562250397Sobrien
5623169689Skan  while (ics1->kind == ck_rvalue
5624169689Skan	 || ics1->kind == ck_lvalue)
5625169689Skan    ics1 = ics1->u.next;
562650397Sobrien
562750397Sobrien  while (1)
562818334Speter    {
5629169689Skan      while (ics2->kind == ck_rvalue
5630169689Skan	     || ics2->kind == ck_lvalue)
5631169689Skan	ics2 = ics2->u.next;
563250397Sobrien
5633169689Skan      if (ics2->kind == ck_user
5634169689Skan	  || ics2->kind == ck_ambig
5635169689Skan	  || ics2->kind == ck_identity)
563650397Sobrien	/* At this point, ICS1 cannot be a proper subsequence of
563750397Sobrien	   ICS2.  We can get a USER_CONV when we are comparing the
563850397Sobrien	   second standard conversion sequence of two user conversion
563950397Sobrien	   sequences.  */
5640132718Skan	return false;
564150397Sobrien
5642169689Skan      ics2 = ics2->u.next;
564350397Sobrien
5644169689Skan      if (ics2->kind == ics1->kind
5645169689Skan	  && same_type_p (ics2->type, ics1->type)
5646169689Skan	  && same_type_p (ics2->u.next->type,
5647169689Skan			  ics1->u.next->type))
5648132718Skan	return true;
564950397Sobrien    }
565050397Sobrien}
565150397Sobrien
5652117395Skan/* Returns nonzero iff DERIVED is derived from BASE.  The inputs may
565350397Sobrien   be any _TYPE nodes.  */
565450397Sobrien
5655132718Skanbool
5656132718Skanis_properly_derived_from (tree derived, tree base)
565750397Sobrien{
565850397Sobrien  if (!IS_AGGR_TYPE_CODE (TREE_CODE (derived))
565950397Sobrien      || !IS_AGGR_TYPE_CODE (TREE_CODE (base)))
5660132718Skan    return false;
566150397Sobrien
566250397Sobrien  /* We only allow proper derivation here.  The DERIVED_FROM_P macro
566350397Sobrien     considers every class derived from itself.  */
566490075Sobrien  return (!same_type_ignoring_top_level_qualifiers_p (derived, base)
566550397Sobrien	  && DERIVED_FROM_P (base, derived));
566650397Sobrien}
566750397Sobrien
566850397Sobrien/* We build the ICS for an implicit object parameter as a pointer
566950397Sobrien   conversion sequence.  However, such a sequence should be compared
567050397Sobrien   as if it were a reference conversion sequence.  If ICS is the
567150397Sobrien   implicit conversion sequence for an implicit object parameter,
567250397Sobrien   modify it accordingly.  */
567350397Sobrien
567450397Sobrienstatic void
5675169689Skanmaybe_handle_implicit_object (conversion **ics)
567650397Sobrien{
5677169689Skan  if ((*ics)->this_p)
567850397Sobrien    {
567950397Sobrien      /* [over.match.funcs]
5680169689Skan
568150397Sobrien	 For non-static member functions, the type of the
568250397Sobrien	 implicit object parameter is "reference to cv X"
568350397Sobrien	 where X is the class of which the function is a
568450397Sobrien	 member and cv is the cv-qualification on the member
568550397Sobrien	 function declaration.  */
5686169689Skan      conversion *t = *ics;
568790075Sobrien      tree reference_type;
568890075Sobrien
568990075Sobrien      /* The `this' parameter is a pointer to a class type.  Make the
5690132718Skan	 implicit conversion talk about a reference to that same class
569190075Sobrien	 type.  */
5692169689Skan      reference_type = TREE_TYPE (t->type);
569390075Sobrien      reference_type = build_reference_type (reference_type);
569490075Sobrien
5695169689Skan      if (t->kind == ck_qual)
5696169689Skan	t = t->u.next;
5697169689Skan      if (t->kind == ck_ptr)
5698169689Skan	t = t->u.next;
5699169689Skan      t = build_identity_conv (TREE_TYPE (t->type), NULL_TREE);
5700169689Skan      t = direct_reference_binding (reference_type, t);
570150397Sobrien      *ics = t;
570250397Sobrien    }
570350397Sobrien}
570450397Sobrien
570590075Sobrien/* If *ICS is a REF_BIND set *ICS to the remainder of the conversion,
570690075Sobrien   and return the type to which the reference refers.  Otherwise,
570790075Sobrien   leave *ICS unchanged and return NULL_TREE.  */
570850397Sobrien
570990075Sobrienstatic tree
5710169689Skanmaybe_handle_ref_bind (conversion **ics)
571150397Sobrien{
5712169689Skan  if ((*ics)->kind == ck_ref_bind)
571350397Sobrien    {
5714169689Skan      conversion *old_ics = *ics;
5715169689Skan      tree type = TREE_TYPE (old_ics->type);
5716169689Skan      *ics = old_ics->u.next;
5717169689Skan      (*ics)->user_conv_p = old_ics->user_conv_p;
5718169689Skan      (*ics)->bad_p = old_ics->bad_p;
571990075Sobrien      return type;
572018334Speter    }
572190075Sobrien
572290075Sobrien  return NULL_TREE;
572350397Sobrien}
572418334Speter
572550397Sobrien/* Compare two implicit conversion sequences according to the rules set out in
572650397Sobrien   [over.ics.rank].  Return values:
572750397Sobrien
572850397Sobrien      1: ics1 is better than ics2
572950397Sobrien     -1: ics2 is better than ics1
573050397Sobrien      0: ics1 and ics2 are indistinguishable */
573150397Sobrien
573250397Sobrienstatic int
5733169689Skancompare_ics (conversion *ics1, conversion *ics2)
573450397Sobrien{
573550397Sobrien  tree from_type1;
573650397Sobrien  tree from_type2;
573750397Sobrien  tree to_type1;
573850397Sobrien  tree to_type2;
573950397Sobrien  tree deref_from_type1 = NULL_TREE;
574052284Sobrien  tree deref_from_type2 = NULL_TREE;
574152284Sobrien  tree deref_to_type1 = NULL_TREE;
574252284Sobrien  tree deref_to_type2 = NULL_TREE;
5743169689Skan  conversion_rank rank1, rank2;
574450397Sobrien
5745117395Skan  /* REF_BINDING is nonzero if the result of the conversion sequence
574650397Sobrien     is a reference type.   In that case TARGET_TYPE is the
574750397Sobrien     type referred to by the reference.  */
574850397Sobrien  tree target_type1;
574950397Sobrien  tree target_type2;
575050397Sobrien
575150397Sobrien  /* Handle implicit object parameters.  */
575250397Sobrien  maybe_handle_implicit_object (&ics1);
575350397Sobrien  maybe_handle_implicit_object (&ics2);
575450397Sobrien
575550397Sobrien  /* Handle reference parameters.  */
575690075Sobrien  target_type1 = maybe_handle_ref_bind (&ics1);
575790075Sobrien  target_type2 = maybe_handle_ref_bind (&ics2);
575850397Sobrien
575950397Sobrien  /* [over.ics.rank]
576050397Sobrien
576150397Sobrien     When  comparing  the  basic forms of implicit conversion sequences (as
576250397Sobrien     defined in _over.best.ics_)
576350397Sobrien
576450397Sobrien     --a standard conversion sequence (_over.ics.scs_) is a better
576550397Sobrien       conversion sequence than a user-defined conversion sequence
576650397Sobrien       or an ellipsis conversion sequence, and
5767169689Skan
576850397Sobrien     --a user-defined conversion sequence (_over.ics.user_) is a
576950397Sobrien       better conversion sequence than an ellipsis conversion sequence
577050397Sobrien       (_over.ics.ellipsis_).  */
5771169689Skan  rank1 = CONVERSION_RANK (ics1);
5772169689Skan  rank2 = CONVERSION_RANK (ics2);
5773169689Skan
577490075Sobrien  if (rank1 > rank2)
577550397Sobrien    return -1;
577690075Sobrien  else if (rank1 < rank2)
577750397Sobrien    return 1;
577850397Sobrien
5779169689Skan  if (rank1 == cr_bad)
578050397Sobrien    {
578190075Sobrien      /* XXX Isn't this an extension? */
578250397Sobrien      /* Both ICS are bad.  We try to make a decision based on what
5783132718Skan	 would have happened if they'd been good.  */
5784169689Skan      if (ics1->user_conv_p > ics2->user_conv_p
5785169689Skan	  || ics1->rank  > ics2->rank)
578650397Sobrien	return -1;
5787169689Skan      else if (ics1->user_conv_p < ics2->user_conv_p
5788169689Skan	       || ics1->rank < ics2->rank)
578950397Sobrien	return 1;
579050397Sobrien
579150397Sobrien      /* We couldn't make up our minds; try to figure it out below.  */
579250397Sobrien    }
579350397Sobrien
5794169689Skan  if (ics1->ellipsis_p)
579550397Sobrien    /* Both conversions are ellipsis conversions.  */
579650397Sobrien    return 0;
579750397Sobrien
579850397Sobrien  /* User-defined  conversion sequence U1 is a better conversion sequence
579950397Sobrien     than another user-defined conversion sequence U2 if they contain the
580050397Sobrien     same user-defined conversion operator or constructor and if the sec-
580150397Sobrien     ond standard conversion sequence of U1 is  better  than  the  second
580250397Sobrien     standard conversion sequence of U2.  */
580350397Sobrien
5804169689Skan  if (ics1->user_conv_p)
580550397Sobrien    {
5806169689Skan      conversion *t1;
5807169689Skan      conversion *t2;
580850397Sobrien
5809169689Skan      for (t1 = ics1; t1->kind != ck_user; t1 = t1->u.next)
5810169689Skan	if (t1->kind == ck_ambig)
581150397Sobrien	  return 0;
5812169689Skan      for (t2 = ics2; t2->kind != ck_user; t2 = t2->u.next)
5813169689Skan	if (t2->kind == ck_ambig)
581450397Sobrien	  return 0;
581550397Sobrien
5816169689Skan      if (t1->cand->fn != t2->cand->fn)
581750397Sobrien	return 0;
581850397Sobrien
581950397Sobrien      /* We can just fall through here, after setting up
582050397Sobrien	 FROM_TYPE1 and FROM_TYPE2.  */
5821169689Skan      from_type1 = t1->type;
5822169689Skan      from_type2 = t2->type;
582350397Sobrien    }
582418334Speter  else
582518334Speter    {
5826169689Skan      conversion *t1;
5827169689Skan      conversion *t2;
582850397Sobrien
5829169689Skan      /* We're dealing with two standard conversion sequences.
5830169689Skan
583150397Sobrien	 [over.ics.rank]
5832169689Skan
583350397Sobrien	 Standard conversion sequence S1 is a better conversion
583450397Sobrien	 sequence than standard conversion sequence S2 if
5835169689Skan
583650397Sobrien	 --S1 is a proper subsequence of S2 (comparing the conversion
583750397Sobrien	   sequences in the canonical form defined by _over.ics.scs_,
583850397Sobrien	   excluding any Lvalue Transformation; the identity
583950397Sobrien	   conversion sequence is considered to be a subsequence of
584050397Sobrien	   any non-identity conversion sequence */
5841169689Skan
5842169689Skan      t1 = ics1;
5843169689Skan      while (t1->kind != ck_identity)
5844169689Skan	t1 = t1->u.next;
5845169689Skan      from_type1 = t1->type;
5846169689Skan
5847169689Skan      t2 = ics2;
5848169689Skan      while (t2->kind != ck_identity)
5849169689Skan	t2 = t2->u.next;
5850169689Skan      from_type2 = t2->type;
585118334Speter    }
585218334Speter
585352284Sobrien  if (same_type_p (from_type1, from_type2))
585450397Sobrien    {
585550397Sobrien      if (is_subseq (ics1, ics2))
585650397Sobrien	return 1;
585750397Sobrien      if (is_subseq (ics2, ics1))
585850397Sobrien	return -1;
585950397Sobrien    }
586050397Sobrien  /* Otherwise, one sequence cannot be a subsequence of the other; they
586150397Sobrien     don't start with the same type.  This can happen when comparing the
586250397Sobrien     second standard conversion sequence in two user-defined conversion
586350397Sobrien     sequences.  */
586418334Speter
586550397Sobrien  /* [over.ics.rank]
586618334Speter
586750397Sobrien     Or, if not that,
586818334Speter
586950397Sobrien     --the rank of S1 is better than the rank of S2 (by the rules
587050397Sobrien       defined below):
587150397Sobrien
587250397Sobrien    Standard conversion sequences are ordered by their ranks: an Exact
587350397Sobrien    Match is a better conversion than a Promotion, which is a better
587450397Sobrien    conversion than a Conversion.
587550397Sobrien
587650397Sobrien    Two conversion sequences with the same rank are indistinguishable
587750397Sobrien    unless one of the following rules applies:
587850397Sobrien
587950397Sobrien    --A conversion that is not a conversion of a pointer, or pointer
588050397Sobrien      to member, to bool is better than another conversion that is such
5881169689Skan      a conversion.
588250397Sobrien
588350397Sobrien    The ICS_STD_RANK automatically handles the pointer-to-bool rule,
588450397Sobrien    so that we do not have to check it explicitly.  */
5885169689Skan  if (ics1->rank < ics2->rank)
588650397Sobrien    return 1;
5887169689Skan  else if (ics2->rank < ics1->rank)
588850397Sobrien    return -1;
588950397Sobrien
5890169689Skan  to_type1 = ics1->type;
5891169689Skan  to_type2 = ics2->type;
589250397Sobrien
589350397Sobrien  if (TYPE_PTR_P (from_type1)
589450397Sobrien      && TYPE_PTR_P (from_type2)
589550397Sobrien      && TYPE_PTR_P (to_type1)
589650397Sobrien      && TYPE_PTR_P (to_type2))
589718334Speter    {
589850397Sobrien      deref_from_type1 = TREE_TYPE (from_type1);
589950397Sobrien      deref_from_type2 = TREE_TYPE (from_type2);
590050397Sobrien      deref_to_type1 = TREE_TYPE (to_type1);
590150397Sobrien      deref_to_type2 = TREE_TYPE (to_type2);
590250397Sobrien    }
590350397Sobrien  /* The rules for pointers to members A::* are just like the rules
590450397Sobrien     for pointers A*, except opposite: if B is derived from A then
590550397Sobrien     A::* converts to B::*, not vice versa.  For that reason, we
590650397Sobrien     switch the from_ and to_ variables here.  */
5907132718Skan  else if ((TYPE_PTRMEM_P (from_type1) && TYPE_PTRMEM_P (from_type2)
5908132718Skan	    && TYPE_PTRMEM_P (to_type1) && TYPE_PTRMEM_P (to_type2))
5909132718Skan	   || (TYPE_PTRMEMFUNC_P (from_type1)
5910132718Skan	       && TYPE_PTRMEMFUNC_P (from_type2)
5911132718Skan	       && TYPE_PTRMEMFUNC_P (to_type1)
5912132718Skan	       && TYPE_PTRMEMFUNC_P (to_type2)))
591350397Sobrien    {
5914132718Skan      deref_to_type1 = TYPE_PTRMEM_CLASS_TYPE (from_type1);
5915132718Skan      deref_to_type2 = TYPE_PTRMEM_CLASS_TYPE (from_type2);
5916132718Skan      deref_from_type1 = TYPE_PTRMEM_CLASS_TYPE (to_type1);
5917132718Skan      deref_from_type2 = TYPE_PTRMEM_CLASS_TYPE (to_type2);
591850397Sobrien    }
591950397Sobrien
592050397Sobrien  if (deref_from_type1 != NULL_TREE
592150397Sobrien      && IS_AGGR_TYPE_CODE (TREE_CODE (deref_from_type1))
592250397Sobrien      && IS_AGGR_TYPE_CODE (TREE_CODE (deref_from_type2)))
592350397Sobrien    {
5924169689Skan      /* This was one of the pointer or pointer-like conversions.
592550397Sobrien
592650397Sobrien	 [over.ics.rank]
5927169689Skan
592850397Sobrien	 --If class B is derived directly or indirectly from class A,
592950397Sobrien	   conversion of B* to A* is better than conversion of B* to
593050397Sobrien	   void*, and conversion of A* to void* is better than
593150397Sobrien	   conversion of B* to void*.  */
593250397Sobrien      if (TREE_CODE (deref_to_type1) == VOID_TYPE
593350397Sobrien	  && TREE_CODE (deref_to_type2) == VOID_TYPE)
593418334Speter	{
593550397Sobrien	  if (is_properly_derived_from (deref_from_type1,
593650397Sobrien					deref_from_type2))
593750397Sobrien	    return -1;
593850397Sobrien	  else if (is_properly_derived_from (deref_from_type2,
593950397Sobrien					     deref_from_type1))
594050397Sobrien	    return 1;
594150397Sobrien	}
594250397Sobrien      else if (TREE_CODE (deref_to_type1) == VOID_TYPE
594350397Sobrien	       || TREE_CODE (deref_to_type2) == VOID_TYPE)
594450397Sobrien	{
594552284Sobrien	  if (same_type_p (deref_from_type1, deref_from_type2))
594618334Speter	    {
594750397Sobrien	      if (TREE_CODE (deref_to_type2) == VOID_TYPE)
594850397Sobrien		{
594950397Sobrien		  if (is_properly_derived_from (deref_from_type1,
595050397Sobrien						deref_to_type1))
595150397Sobrien		    return 1;
595250397Sobrien		}
595350397Sobrien	      /* We know that DEREF_TO_TYPE1 is `void' here.  */
595450397Sobrien	      else if (is_properly_derived_from (deref_from_type1,
595550397Sobrien						 deref_to_type2))
595650397Sobrien		return -1;
595718334Speter	    }
595818334Speter	}
595950397Sobrien      else if (IS_AGGR_TYPE_CODE (TREE_CODE (deref_to_type1))
596050397Sobrien	       && IS_AGGR_TYPE_CODE (TREE_CODE (deref_to_type2)))
596118334Speter	{
596250397Sobrien	  /* [over.ics.rank]
596318334Speter
596450397Sobrien	     --If class B is derived directly or indirectly from class A
596550397Sobrien	       and class C is derived directly or indirectly from B,
5966169689Skan
596750397Sobrien	     --conversion of C* to B* is better than conversion of C* to
5968169689Skan	       A*,
5969169689Skan
597050397Sobrien	     --conversion of B* to A* is better than conversion of C* to
597150397Sobrien	       A*  */
597252284Sobrien	  if (same_type_p (deref_from_type1, deref_from_type2))
597350397Sobrien	    {
597450397Sobrien	      if (is_properly_derived_from (deref_to_type1,
597550397Sobrien					    deref_to_type2))
597650397Sobrien		return 1;
597750397Sobrien	      else if (is_properly_derived_from (deref_to_type2,
597850397Sobrien						 deref_to_type1))
597950397Sobrien		return -1;
598050397Sobrien	    }
598152284Sobrien	  else if (same_type_p (deref_to_type1, deref_to_type2))
598250397Sobrien	    {
598350397Sobrien	      if (is_properly_derived_from (deref_from_type2,
598450397Sobrien					    deref_from_type1))
598550397Sobrien		return 1;
598650397Sobrien	      else if (is_properly_derived_from (deref_from_type1,
598750397Sobrien						 deref_from_type2))
598850397Sobrien		return -1;
598950397Sobrien	    }
599018334Speter	}
599150397Sobrien    }
599290075Sobrien  else if (CLASS_TYPE_P (non_reference (from_type1))
599352284Sobrien	   && same_type_p (from_type1, from_type2))
599450397Sobrien    {
599590075Sobrien      tree from = non_reference (from_type1);
599690075Sobrien
599750397Sobrien      /* [over.ics.rank]
5998169689Skan
599950397Sobrien	 --binding of an expression of type C to a reference of type
600050397Sobrien	   B& is better than binding an expression of type C to a
600150397Sobrien	   reference of type A&
600218334Speter
600350397Sobrien	 --conversion of C to B is better than conversion of C to A,  */
600490075Sobrien      if (is_properly_derived_from (from, to_type1)
600590075Sobrien	  && is_properly_derived_from (from, to_type2))
600618334Speter	{
600750397Sobrien	  if (is_properly_derived_from (to_type1, to_type2))
600850397Sobrien	    return 1;
600950397Sobrien	  else if (is_properly_derived_from (to_type2, to_type1))
601050397Sobrien	    return -1;
601118334Speter	}
601250397Sobrien    }
601390075Sobrien  else if (CLASS_TYPE_P (non_reference (to_type1))
601452284Sobrien	   && same_type_p (to_type1, to_type2))
601550397Sobrien    {
601690075Sobrien      tree to = non_reference (to_type1);
601790075Sobrien
601850397Sobrien      /* [over.ics.rank]
601950397Sobrien
602050397Sobrien	 --binding of an expression of type B to a reference of type
602150397Sobrien	   A& is better than binding an expression of type C to a
6022169689Skan	   reference of type A&,
602350397Sobrien
6024169689Skan	 --conversion of B to A is better than conversion of C to A  */
602590075Sobrien      if (is_properly_derived_from (from_type1, to)
602690075Sobrien	  && is_properly_derived_from (from_type2, to))
602718334Speter	{
602850397Sobrien	  if (is_properly_derived_from (from_type2, from_type1))
602950397Sobrien	    return 1;
603050397Sobrien	  else if (is_properly_derived_from (from_type1, from_type2))
603150397Sobrien	    return -1;
603250397Sobrien	}
603350397Sobrien    }
603418334Speter
603550397Sobrien  /* [over.ics.rank]
603618334Speter
603750397Sobrien     --S1 and S2 differ only in their qualification conversion and  yield
603850397Sobrien       similar  types  T1 and T2 (_conv.qual_), respectively, and the cv-
603950397Sobrien       qualification signature of type T1 is a proper subset of  the  cv-
604050397Sobrien       qualification signature of type T2  */
6041169689Skan  if (ics1->kind == ck_qual
6042169689Skan      && ics2->kind == ck_qual
604352284Sobrien      && same_type_p (from_type1, from_type2))
604450397Sobrien    return comp_cv_qual_signature (to_type1, to_type2);
604518334Speter
604650397Sobrien  /* [over.ics.rank]
6047169689Skan
604850397Sobrien     --S1 and S2 are reference bindings (_dcl.init.ref_), and the
604950397Sobrien     types to which the references refer are the same type except for
605050397Sobrien     top-level cv-qualifiers, and the type to which the reference
605150397Sobrien     initialized by S2 refers is more cv-qualified than the type to
605250397Sobrien     which the reference initialized by S1 refers */
6053169689Skan
605490075Sobrien  if (target_type1 && target_type2
605590075Sobrien      && same_type_ignoring_top_level_qualifiers_p (to_type1, to_type2))
605650397Sobrien    return comp_cv_qualification (target_type2, target_type1);
605718334Speter
605850397Sobrien  /* Neither conversion sequence is better than the other.  */
605950397Sobrien  return 0;
606050397Sobrien}
606118334Speter
606250397Sobrien/* The source type for this standard conversion sequence.  */
606318334Speter
606450397Sobrienstatic tree
6065169689Skansource_type (conversion *t)
606650397Sobrien{
6067169689Skan  for (;; t = t->u.next)
606850397Sobrien    {
6069169689Skan      if (t->kind == ck_user
6070169689Skan	  || t->kind == ck_ambig
6071169689Skan	  || t->kind == ck_identity)
6072169689Skan	return t->type;
607350397Sobrien    }
6074169689Skan  gcc_unreachable ();
607550397Sobrien}
607650397Sobrien
607750397Sobrien/* Note a warning about preferring WINNER to LOSER.  We do this by storing
607850397Sobrien   a pointer to LOSER and re-running joust to produce the warning if WINNER
607950397Sobrien   is actually used.  */
608050397Sobrien
608150397Sobrienstatic void
6082132718Skanadd_warning (struct z_candidate *winner, struct z_candidate *loser)
608350397Sobrien{
6084169689Skan  candidate_warning *cw = (candidate_warning *)
6085169689Skan    conversion_obstack_alloc (sizeof (candidate_warning));
6086169689Skan  cw->loser = loser;
6087169689Skan  cw->next = winner->warnings;
6088169689Skan  winner->warnings = cw;
608950397Sobrien}
609050397Sobrien
609150397Sobrien/* Compare two candidates for overloading as described in
609250397Sobrien   [over.match.best].  Return values:
609350397Sobrien
609450397Sobrien      1: cand1 is better than cand2
609550397Sobrien     -1: cand2 is better than cand1
609650397Sobrien      0: cand1 and cand2 are indistinguishable */
609750397Sobrien
609850397Sobrienstatic int
6099132718Skanjoust (struct z_candidate *cand1, struct z_candidate *cand2, bool warn)
610050397Sobrien{
610150397Sobrien  int winner = 0;
6102169689Skan  int off1 = 0, off2 = 0;
6103169689Skan  size_t i;
6104169689Skan  size_t len;
610550397Sobrien
610650397Sobrien  /* Candidates that involve bad conversions are always worse than those
610750397Sobrien     that don't.  */
610850397Sobrien  if (cand1->viable > cand2->viable)
610950397Sobrien    return 1;
611050397Sobrien  if (cand1->viable < cand2->viable)
611150397Sobrien    return -1;
611250397Sobrien
611352284Sobrien  /* If we have two pseudo-candidates for conversions to the same type,
611490075Sobrien     or two candidates for the same function, arbitrarily pick one.  */
611590075Sobrien  if (cand1->fn == cand2->fn
6116169689Skan      && (IS_TYPE_OR_DECL_P (cand1->fn)))
611752284Sobrien    return 1;
611852284Sobrien
611950397Sobrien  /* a viable function F1
612050397Sobrien     is defined to be a better function than another viable function F2  if
612150397Sobrien     for  all arguments i, ICSi(F1) is not a worse conversion sequence than
612250397Sobrien     ICSi(F2), and then */
612350397Sobrien
612450397Sobrien  /* for some argument j, ICSj(F1) is a better conversion  sequence  than
612550397Sobrien     ICSj(F2) */
612650397Sobrien
612790075Sobrien  /* For comparing static and non-static member functions, we ignore
612890075Sobrien     the implicit object parameter of the non-static function.  The
612990075Sobrien     standard says to pretend that the static function has an object
613090075Sobrien     parm, but that won't work with operator overloading.  */
6131169689Skan  len = cand1->num_convs;
6132169689Skan  if (len != cand2->num_convs)
613350397Sobrien    {
6134169689Skan      int static_1 = DECL_STATIC_FUNCTION_P (cand1->fn);
6135169689Skan      int static_2 = DECL_STATIC_FUNCTION_P (cand2->fn);
6136169689Skan
6137169689Skan      gcc_assert (static_1 != static_2);
6138169689Skan
6139169689Skan      if (static_1)
614050397Sobrien	off2 = 1;
6141169689Skan      else
614250397Sobrien	{
614350397Sobrien	  off1 = 1;
614450397Sobrien	  --len;
614518334Speter	}
614618334Speter    }
614718334Speter
614850397Sobrien  for (i = 0; i < len; ++i)
614918334Speter    {
6150169689Skan      conversion *t1 = cand1->convs[i + off1];
6151169689Skan      conversion *t2 = cand2->convs[i + off2];
615250397Sobrien      int comp = compare_ics (t1, t2);
615318334Speter
615450397Sobrien      if (comp != 0)
615518334Speter	{
615650397Sobrien	  if (warn_sign_promo
6157169689Skan	      && (CONVERSION_RANK (t1) + CONVERSION_RANK (t2)
6158169689Skan		  == cr_std + cr_promotion)
6159169689Skan	      && t1->kind == ck_std
6160169689Skan	      && t2->kind == ck_std
6161169689Skan	      && TREE_CODE (t1->type) == INTEGER_TYPE
6162169689Skan	      && TREE_CODE (t2->type) == INTEGER_TYPE
6163169689Skan	      && (TYPE_PRECISION (t1->type)
6164169689Skan		  == TYPE_PRECISION (t2->type))
6165169689Skan	      && (TYPE_UNSIGNED (t1->u.next->type)
6166169689Skan		  || (TREE_CODE (t1->u.next->type)
616750397Sobrien		      == ENUMERAL_TYPE)))
616818334Speter	    {
6169169689Skan	      tree type = t1->u.next->type;
617050397Sobrien	      tree type1, type2;
617150397Sobrien	      struct z_candidate *w, *l;
617250397Sobrien	      if (comp > 0)
6173169689Skan		type1 = t1->type, type2 = t2->type,
617450397Sobrien		  w = cand1, l = cand2;
617550397Sobrien	      else
6176169689Skan		type1 = t2->type, type2 = t1->type,
617750397Sobrien		  w = cand2, l = cand1;
617850397Sobrien
617950397Sobrien	      if (warn)
618018334Speter		{
6181169689Skan		  warning (OPT_Wsign_promo, "passing %qT chooses %qT over %qT",
6182169689Skan			   type, type1, type2);
6183169689Skan		  warning (OPT_Wsign_promo, "  in call to %qD", w->fn);
618418334Speter		}
618550397Sobrien	      else
618650397Sobrien		add_warning (w, l);
618718334Speter	    }
618850397Sobrien
618950397Sobrien	  if (winner && comp != winner)
619050397Sobrien	    {
619150397Sobrien	      winner = 0;
619250397Sobrien	      goto tweak;
619350397Sobrien	    }
619450397Sobrien	  winner = comp;
619550397Sobrien	}
619650397Sobrien    }
619750397Sobrien
619850397Sobrien  /* warn about confusing overload resolution for user-defined conversions,
619950397Sobrien     either between a constructor and a conversion op, or between two
620050397Sobrien     conversion ops.  */
6201132718Skan  if (winner && warn_conversion && cand1->second_conv
6202132718Skan      && (!DECL_CONSTRUCTOR_P (cand1->fn) || !DECL_CONSTRUCTOR_P (cand2->fn))
6203132718Skan      && winner != compare_ics (cand1->second_conv, cand2->second_conv))
620450397Sobrien    {
6205132718Skan      struct z_candidate *w, *l;
6206132718Skan      bool give_warning = false;
6207169689Skan
6208132718Skan      if (winner == 1)
6209132718Skan	w = cand1, l = cand2;
6210132718Skan      else
6211132718Skan	w = cand2, l = cand1;
6212169689Skan
6213132718Skan      /* We don't want to complain about `X::operator T1 ()'
6214132718Skan	 beating `X::operator T2 () const', when T2 is a no less
6215132718Skan	 cv-qualified version of T1.  */
6216132718Skan      if (DECL_CONTEXT (w->fn) == DECL_CONTEXT (l->fn)
6217132718Skan	  && !DECL_CONSTRUCTOR_P (w->fn) && !DECL_CONSTRUCTOR_P (l->fn))
621850397Sobrien	{
6219132718Skan	  tree t = TREE_TYPE (TREE_TYPE (l->fn));
6220132718Skan	  tree f = TREE_TYPE (TREE_TYPE (w->fn));
6221169689Skan
6222132718Skan	  if (TREE_CODE (t) == TREE_CODE (f) && POINTER_TYPE_P (t))
622350397Sobrien	    {
6224132718Skan	      t = TREE_TYPE (t);
6225132718Skan	      f = TREE_TYPE (f);
622650397Sobrien	    }
6227132718Skan	  if (!comp_ptr_ttypes (t, f))
6228132718Skan	    give_warning = true;
622918334Speter	}
6230132718Skan      else
6231132718Skan	give_warning = true;
6232169689Skan
6233132718Skan      if (!give_warning)
6234132718Skan	/*NOP*/;
6235132718Skan      else if (warn)
6236132718Skan	{
6237169689Skan	  tree source = source_type (w->convs[0]);
6238132718Skan	  if (! DECL_CONSTRUCTOR_P (w->fn))
6239132718Skan	    source = TREE_TYPE (source);
6240169689Skan	  warning (OPT_Wconversion, "choosing %qD over %qD", w->fn, l->fn);
6241169689Skan	  warning (OPT_Wconversion, "  for conversion from %qT to %qT",
6242169689Skan		   source, w->second_conv->type);
6243169689Skan	  inform ("  because conversion sequence for the argument is better");
6244132718Skan	}
6245132718Skan      else
6246132718Skan	add_warning (w, l);
624750397Sobrien    }
624850397Sobrien
624950397Sobrien  if (winner)
625050397Sobrien    return winner;
625150397Sobrien
625250397Sobrien  /* or, if not that,
625390075Sobrien     F1 is a non-template function and F2 is a template function
625490075Sobrien     specialization.  */
6255169689Skan
6256169689Skan  if (!cand1->template_decl && cand2->template_decl)
625750397Sobrien    return 1;
6258169689Skan  else if (cand1->template_decl && !cand2->template_decl)
625950397Sobrien    return -1;
6260169689Skan
626190075Sobrien  /* or, if not that,
626290075Sobrien     F1 and F2 are template functions and the function template for F1 is
626390075Sobrien     more specialized than the template for F2 according to the partial
626490075Sobrien     ordering rules.  */
6265169689Skan
6266169689Skan  if (cand1->template_decl && cand2->template_decl)
626790075Sobrien    {
6268169689Skan      winner = more_specialized_fn
6269169689Skan	(TI_TEMPLATE (cand1->template_decl),
6270169689Skan	 TI_TEMPLATE (cand2->template_decl),
6271169689Skan	 /* [temp.func.order]: The presence of unused ellipsis and default
627290075Sobrien	    arguments has no effect on the partial ordering of function
6273169689Skan	    templates.   add_function_candidate() will not have
6274169689Skan	    counted the "this" argument for constructors.  */
6275169689Skan	 cand1->num_convs + DECL_CONSTRUCTOR_P (cand1->fn));
627690075Sobrien      if (winner)
6277169689Skan	return winner;
627890075Sobrien    }
627990075Sobrien
628050397Sobrien  /* or, if not that,
628150397Sobrien     the  context  is  an  initialization by user-defined conversion (see
628250397Sobrien     _dcl.init_  and  _over.match.user_)  and  the  standard   conversion
628350397Sobrien     sequence  from  the return type of F1 to the destination type (i.e.,
628450397Sobrien     the type of the entity being initialized)  is  a  better  conversion
628550397Sobrien     sequence  than the standard conversion sequence from the return type
628650397Sobrien     of F2 to the destination type.  */
628750397Sobrien
628890075Sobrien  if (cand1->second_conv)
628990075Sobrien    {
629090075Sobrien      winner = compare_ics (cand1->second_conv, cand2->second_conv);
629190075Sobrien      if (winner)
6292169689Skan	return winner;
629390075Sobrien    }
6294169689Skan
629590075Sobrien  /* Check whether we can discard a builtin candidate, either because we
629690075Sobrien     have two identical ones or matching builtin and non-builtin candidates.
629750397Sobrien
629890075Sobrien     (Pedantically in the latter case the builtin which matched the user
629990075Sobrien     function should not be added to the overload set, but we spot it here.
6300169689Skan
630190075Sobrien     [over.match.oper]
630290075Sobrien     ... the builtin candidates include ...
630390075Sobrien     - do not have the same parameter type list as any non-template
630490075Sobrien       non-member candidate.  */
6305169689Skan
630690075Sobrien  if (TREE_CODE (cand1->fn) == IDENTIFIER_NODE
630790075Sobrien      || TREE_CODE (cand2->fn) == IDENTIFIER_NODE)
630850397Sobrien    {
630950397Sobrien      for (i = 0; i < len; ++i)
6310169689Skan	if (!same_type_p (cand1->convs[i]->type,
6311169689Skan			  cand2->convs[i]->type))
631250397Sobrien	  break;
6313169689Skan      if (i == cand1->num_convs)
631450397Sobrien	{
631590075Sobrien	  if (cand1->fn == cand2->fn)
631690075Sobrien	    /* Two built-in candidates; arbitrarily pick one.  */
631790075Sobrien	    return 1;
631890075Sobrien	  else if (TREE_CODE (cand1->fn) == IDENTIFIER_NODE)
631990075Sobrien	    /* cand1 is built-in; prefer cand2.  */
632090075Sobrien	    return -1;
632190075Sobrien	  else
632290075Sobrien	    /* cand2 is built-in; prefer cand1.  */
632390075Sobrien	    return 1;
632450397Sobrien	}
632550397Sobrien    }
632650397Sobrien
632790075Sobrien  /* If the two functions are the same (this can happen with declarations
632890075Sobrien     in multiple scopes and arg-dependent lookup), arbitrarily choose one.  */
632990075Sobrien  if (DECL_P (cand1->fn) && DECL_P (cand2->fn)
633090075Sobrien      && equal_functions (cand1->fn, cand2->fn))
633190075Sobrien    return 1;
6332169689Skan
633350397Sobrientweak:
633450397Sobrien
633550397Sobrien  /* Extension: If the worst conversion for one candidate is worse than the
633650397Sobrien     worst conversion for the other, take the first.  */
633790075Sobrien  if (!pedantic)
633850397Sobrien    {
6339169689Skan      conversion_rank rank1 = cr_identity, rank2 = cr_identity;
634090075Sobrien      struct z_candidate *w = 0, *l = 0;
634150397Sobrien
634250397Sobrien      for (i = 0; i < len; ++i)
634350397Sobrien	{
6344169689Skan	  if (CONVERSION_RANK (cand1->convs[i+off1]) > rank1)
6345169689Skan	    rank1 = CONVERSION_RANK (cand1->convs[i+off1]);
6346169689Skan	  if (CONVERSION_RANK (cand2->convs[i + off2]) > rank2)
6347169689Skan	    rank2 = CONVERSION_RANK (cand2->convs[i + off2]);
634850397Sobrien	}
634950397Sobrien      if (rank1 < rank2)
635090075Sobrien	winner = 1, w = cand1, l = cand2;
635150397Sobrien      if (rank1 > rank2)
635290075Sobrien	winner = -1, w = cand2, l = cand1;
635390075Sobrien      if (winner)
6354169689Skan	{
635590075Sobrien	  if (warn)
6356132718Skan	    {
6357132718Skan	      pedwarn ("\
6358132718SkanISO C++ says that these are ambiguous, even \
6359132718Skanthough the worst conversion for the first is better than \
6360132718Skanthe worst conversion for the second:");
6361132718Skan	      print_z_candidate (_("candidate 1:"), w);
6362132718Skan	      print_z_candidate (_("candidate 2:"), l);
6363132718Skan	    }
636490075Sobrien	  else
636590075Sobrien	    add_warning (w, l);
6366169689Skan	  return winner;
6367169689Skan	}
636850397Sobrien    }
636950397Sobrien
6370169689Skan  gcc_assert (!winner);
637190075Sobrien  return 0;
637250397Sobrien}
637350397Sobrien
637450397Sobrien/* Given a list of candidates for overloading, find the best one, if any.
637550397Sobrien   This algorithm has a worst case of O(2n) (winner is last), and a best
637650397Sobrien   case of O(n/2) (totally ambiguous); much better than a sorting
637750397Sobrien   algorithm.  */
637850397Sobrien
637950397Sobrienstatic struct z_candidate *
6380132718Skantourney (struct z_candidate *candidates)
638150397Sobrien{
638250397Sobrien  struct z_candidate *champ = candidates, *challenger;
638350397Sobrien  int fate;
638450397Sobrien  int champ_compared_to_predecessor = 0;
638550397Sobrien
638650397Sobrien  /* Walk through the list once, comparing each current champ to the next
638750397Sobrien     candidate, knocking out a candidate or two with each comparison.  */
638850397Sobrien
638950397Sobrien  for (challenger = champ->next; challenger; )
639050397Sobrien    {
639150397Sobrien      fate = joust (champ, challenger, 0);
639250397Sobrien      if (fate == 1)
639350397Sobrien	challenger = challenger->next;
639418334Speter      else
639518334Speter	{
639650397Sobrien	  if (fate == 0)
639718334Speter	    {
639850397Sobrien	      champ = challenger->next;
639950397Sobrien	      if (champ == 0)
6400169689Skan		return NULL;
640150397Sobrien	      champ_compared_to_predecessor = 0;
640218334Speter	    }
640318334Speter	  else
640450397Sobrien	    {
640550397Sobrien	      champ = challenger;
640650397Sobrien	      champ_compared_to_predecessor = 1;
640750397Sobrien	    }
640850397Sobrien
640950397Sobrien	  challenger = champ->next;
641018334Speter	}
641150397Sobrien    }
641218334Speter
641350397Sobrien  /* Make sure the champ is better than all the candidates it hasn't yet
641450397Sobrien     been compared to.  */
641518334Speter
6416169689Skan  for (challenger = candidates;
6417169689Skan       challenger != champ
641850397Sobrien	 && !(champ_compared_to_predecessor && challenger->next == champ);
641950397Sobrien       challenger = challenger->next)
642050397Sobrien    {
642150397Sobrien      fate = joust (champ, challenger, 0);
642250397Sobrien      if (fate != 1)
6423169689Skan	return NULL;
642418334Speter    }
642518334Speter
642650397Sobrien  return champ;
642718334Speter}
642818334Speter
6429117395Skan/* Returns nonzero if things of type FROM can be converted to TO.  */
643090075Sobrien
6431132718Skanbool
6432132718Skancan_convert (tree to, tree from)
643318334Speter{
6434169689Skan  return can_convert_arg (to, from, NULL_TREE, LOOKUP_NORMAL);
643518334Speter}
643618334Speter
6437117395Skan/* Returns nonzero if ARG (of type FROM) can be converted to TO.  */
643890075Sobrien
6439132718Skanbool
6440169689Skancan_convert_arg (tree to, tree from, tree arg, int flags)
644118334Speter{
6442169689Skan  conversion *t;
6443169689Skan  void *p;
6444169689Skan  bool ok_p;
6445169689Skan
6446169689Skan  /* Get the high-water mark for the CONVERSION_OBSTACK.  */
6447169689Skan  p = conversion_obstack_alloc (0);
6448169689Skan
6449169689Skan  t  = implicit_conversion (to, from, arg, /*c_cast_p=*/false,
6450169689Skan			    flags);
6451169689Skan  ok_p = (t && !t->bad_p);
6452169689Skan
6453169689Skan  /* Free all the conversions we allocated.  */
6454169689Skan  obstack_free (&conversion_obstack, p);
6455169689Skan
6456169689Skan  return ok_p;
645718334Speter}
645890075Sobrien
645990075Sobrien/* Like can_convert_arg, but allows dubious conversions as well.  */
646090075Sobrien
6461132718Skanbool
6462132718Skancan_convert_arg_bad (tree to, tree from, tree arg)
646390075Sobrien{
6464169689Skan  conversion *t;
6465169689Skan  void *p;
6466169689Skan
6467169689Skan  /* Get the high-water mark for the CONVERSION_OBSTACK.  */
6468169689Skan  p = conversion_obstack_alloc (0);
6469169689Skan  /* Try to perform the conversion.  */
6470169689Skan  t  = implicit_conversion (to, from, arg, /*c_cast_p=*/false,
6471169689Skan			    LOOKUP_NORMAL);
6472169689Skan  /* Free all the conversions we allocated.  */
6473169689Skan  obstack_free (&conversion_obstack, p);
6474169689Skan
6475169689Skan  return t != NULL;
647690075Sobrien}
647790075Sobrien
647890075Sobrien/* Convert EXPR to TYPE.  Return the converted expression.
647990075Sobrien
648090075Sobrien   Note that we allow bad conversions here because by the time we get to
648190075Sobrien   this point we are committed to doing the conversion.  If we end up
648290075Sobrien   doing a bad conversion, convert_like will complain.  */
648390075Sobrien
648490075Sobrientree
6485132718Skanperform_implicit_conversion (tree type, tree expr)
648690075Sobrien{
6487169689Skan  conversion *conv;
6488169689Skan  void *p;
6489169689Skan
6490132718Skan  if (error_operand_p (expr))
649190075Sobrien    return error_mark_node;
6492169689Skan
6493169689Skan  /* Get the high-water mark for the CONVERSION_OBSTACK.  */
6494169689Skan  p = conversion_obstack_alloc (0);
6495169689Skan
649690075Sobrien  conv = implicit_conversion (type, TREE_TYPE (expr), expr,
6497169689Skan			      /*c_cast_p=*/false,
649890075Sobrien			      LOOKUP_NORMAL);
649990075Sobrien  if (!conv)
650090075Sobrien    {
6501169689Skan      error ("could not convert %qE to %qT", expr, type);
6502169689Skan      expr = error_mark_node;
650390075Sobrien    }
6504169689Skan  else if (processing_template_decl)
6505169689Skan    {
6506169689Skan      /* In a template, we are only concerned about determining the
6507169689Skan	 type of non-dependent expressions, so we do not have to
6508169689Skan	 perform the actual conversion.  */
6509169689Skan      if (TREE_TYPE (expr) != type)
6510169689Skan	expr = build_nop (type, expr);
6511169689Skan    }
6512169689Skan  else
6513169689Skan    expr = convert_like (conv, expr);
651490075Sobrien
6515169689Skan  /* Free all the conversions we allocated.  */
6516169689Skan  obstack_free (&conversion_obstack, p);
6517169689Skan
6518169689Skan  return expr;
651990075Sobrien}
652090075Sobrien
6521117395Skan/* Convert EXPR to TYPE (as a direct-initialization) if that is
6522117395Skan   permitted.  If the conversion is valid, the converted expression is
6523122180Skan   returned.  Otherwise, NULL_TREE is returned, except in the case
6524169689Skan   that TYPE is a class type; in that case, an error is issued.  If
6525169689Skan   C_CAST_P is true, then this direction initialization is taking
6526169689Skan   place as part of a static_cast being attempted as part of a C-style
6527169689Skan   cast.  */
6528117395Skan
6529117395Skantree
6530169689Skanperform_direct_initialization_if_possible (tree type,
6531169689Skan					   tree expr,
6532169689Skan					   bool c_cast_p)
6533117395Skan{
6534169689Skan  conversion *conv;
6535169689Skan  void *p;
6536169689Skan
6537117395Skan  if (type == error_mark_node || error_operand_p (expr))
6538117395Skan    return error_mark_node;
6539122180Skan  /* [dcl.init]
6540122180Skan
6541122180Skan     If the destination type is a (possibly cv-qualified) class type:
6542122180Skan
6543122180Skan     -- If the initialization is direct-initialization ...,
6544122180Skan     constructors are considered. ... If no constructor applies, or
6545122180Skan     the overload resolution is ambiguous, the initialization is
6546122180Skan     ill-formed.  */
6547122180Skan  if (CLASS_TYPE_P (type))
6548122180Skan    {
6549122180Skan      expr = build_special_member_call (NULL_TREE, complete_ctor_identifier,
6550122180Skan					build_tree_list (NULL_TREE, expr),
6551169689Skan					type, LOOKUP_NORMAL);
6552122180Skan      return build_cplus_new (type, expr);
6553122180Skan    }
6554169689Skan
6555169689Skan  /* Get the high-water mark for the CONVERSION_OBSTACK.  */
6556169689Skan  p = conversion_obstack_alloc (0);
6557169689Skan
6558117395Skan  conv = implicit_conversion (type, TREE_TYPE (expr), expr,
6559169689Skan			      c_cast_p,
6560117395Skan			      LOOKUP_NORMAL);
6561169689Skan  if (!conv || conv->bad_p)
6562169689Skan    expr = NULL_TREE;
6563169689Skan  else
6564169689Skan    expr = convert_like_real (conv, expr, NULL_TREE, 0, 0,
6565169689Skan			      /*issue_conversion_warnings=*/false,
6566169689Skan			      c_cast_p);
6567169689Skan
6568169689Skan  /* Free all the conversions we allocated.  */
6569169689Skan  obstack_free (&conversion_obstack, p);
6570169689Skan
6571169689Skan  return expr;
6572117395Skan}
6573117395Skan
6574117395Skan/* DECL is a VAR_DECL whose type is a REFERENCE_TYPE.  The reference
6575117395Skan   is being bound to a temporary.  Create and return a new VAR_DECL
6576117395Skan   with the indicated TYPE; this variable will store the value to
6577117395Skan   which the reference is bound.  */
6578117395Skan
6579169689Skantree
6580117395Skanmake_temporary_var_for_ref_to_temp (tree decl, tree type)
6581117395Skan{
6582117395Skan  tree var;
6583117395Skan
6584117395Skan  /* Create the variable.  */
6585169689Skan  var = create_temporary_var (type);
6586117395Skan
6587117395Skan  /* Register the variable.  */
6588117395Skan  if (TREE_STATIC (decl))
6589117395Skan    {
6590117395Skan      /* Namespace-scope or local static; give it a mangled name.  */
6591117395Skan      tree name;
6592117395Skan
6593117395Skan      TREE_STATIC (var) = 1;
6594117395Skan      name = mangle_ref_init_variable (decl);
6595117395Skan      DECL_NAME (var) = name;
6596117395Skan      SET_DECL_ASSEMBLER_NAME (var, name);
6597117395Skan      var = pushdecl_top_level (var);
6598117395Skan    }
6599117395Skan  else
6600169689Skan    /* Create a new cleanup level if necessary.  */
6601169689Skan    maybe_push_cleanup_level (type);
6602117395Skan
6603117395Skan  return var;
6604117395Skan}
6605117395Skan
6606132718Skan/* Convert EXPR to the indicated reference TYPE, in a way suitable for
6607132718Skan   initializing a variable of that TYPE.  If DECL is non-NULL, it is
6608132718Skan   the VAR_DECL being initialized with the EXPR.  (In that case, the
6609132718Skan   type of DECL will be TYPE.)  If DECL is non-NULL, then CLEANUP must
6610132718Skan   also be non-NULL, and with *CLEANUP initialized to NULL.  Upon
6611169689Skan   return, if *CLEANUP is no longer NULL, it will be an expression
6612169689Skan   that should be pushed as a cleanup after the returned expression
6613169689Skan   is used to initialize DECL.
661490075Sobrien
6615132718Skan   Return the converted expression.  */
6616117395Skan
661790075Sobrientree
6618132718Skaninitialize_reference (tree type, tree expr, tree decl, tree *cleanup)
661990075Sobrien{
6620169689Skan  conversion *conv;
6621169689Skan  void *p;
662290075Sobrien
6623117395Skan  if (type == error_mark_node || error_operand_p (expr))
6624117395Skan    return error_mark_node;
6625117395Skan
6626169689Skan  /* Get the high-water mark for the CONVERSION_OBSTACK.  */
6627169689Skan  p = conversion_obstack_alloc (0);
6628169689Skan
6629169689Skan  conv = reference_binding (type, TREE_TYPE (expr), expr, /*c_cast_p=*/false,
6630169689Skan			    LOOKUP_NORMAL);
6631169689Skan  if (!conv || conv->bad_p)
663290075Sobrien    {
6633122180Skan      if (!(TYPE_QUALS (TREE_TYPE (type)) & TYPE_QUAL_CONST)
6634169689Skan	  && !real_lvalue_p (expr))
6635169689Skan	error ("invalid initialization of non-const reference of "
6636169689Skan	       "type %qT from a temporary of type %qT",
6637169689Skan	       type, TREE_TYPE (expr));
6638122180Skan      else
6639169689Skan	error ("invalid initialization of reference of type "
6640169689Skan	       "%qT from expression of type %qT", type,
6641122180Skan	       TREE_TYPE (expr));
664290075Sobrien      return error_mark_node;
664390075Sobrien    }
664490075Sobrien
6645117395Skan  /* If DECL is non-NULL, then this special rule applies:
6646117395Skan
6647117395Skan       [class.temporary]
6648117395Skan
6649117395Skan       The temporary to which the reference is bound or the temporary
6650117395Skan       that is the complete object to which the reference is bound
6651117395Skan       persists for the lifetime of the reference.
6652117395Skan
6653117395Skan       The temporaries created during the evaluation of the expression
6654117395Skan       initializing the reference, except the temporary to which the
6655117395Skan       reference is bound, are destroyed at the end of the
6656117395Skan       full-expression in which they are created.
6657117395Skan
6658117395Skan     In that case, we store the converted expression into a new
6659169689Skan     VAR_DECL in a new scope.
6660117395Skan
6661117395Skan     However, we want to be careful not to create temporaries when
6662117395Skan     they are not required.  For example, given:
6663117395Skan
6664169689Skan       struct B {};
6665117395Skan       struct D : public B {};
6666117395Skan       D f();
6667117395Skan       const B& b = f();
6668117395Skan
6669117395Skan     there is no need to copy the return value from "f"; we can just
6670117395Skan     extend its lifetime.  Similarly, given:
6671117395Skan
6672117395Skan       struct S {};
6673117395Skan       struct T { operator S(); };
6674117395Skan       T t;
6675117395Skan       const S& s = t;
6676117395Skan
6677117395Skan    we can extend the lifetime of the return value of the conversion
6678117395Skan    operator.  */
6679169689Skan  gcc_assert (conv->kind == ck_ref_bind);
6680117395Skan  if (decl)
6681117395Skan    {
6682117395Skan      tree var;
6683117395Skan      tree base_conv_type;
6684117395Skan
6685117395Skan      /* Skip over the REF_BIND.  */
6686169689Skan      conv = conv->u.next;
6687117395Skan      /* If the next conversion is a BASE_CONV, skip that too -- but
6688117395Skan	 remember that the conversion was required.  */
6689169689Skan      if (conv->kind == ck_base)
6690117395Skan	{
6691169689Skan	  if (conv->check_copy_constructor_p)
6692132718Skan	    check_constructor_callable (TREE_TYPE (expr), expr);
6693169689Skan	  base_conv_type = conv->type;
6694169689Skan	  conv = conv->u.next;
6695117395Skan	}
6696117395Skan      else
6697117395Skan	base_conv_type = NULL_TREE;
6698117395Skan      /* Perform the remainder of the conversion.  */
6699132718Skan      expr = convert_like_real (conv, expr,
6700132718Skan				/*fn=*/NULL_TREE, /*argnum=*/0,
6701132718Skan				/*inner=*/-1,
6702169689Skan				/*issue_conversion_warnings=*/true,
6703169689Skan				/*c_cast_p=*/false);
6704146895Skan      if (error_operand_p (expr))
6705169689Skan	expr = error_mark_node;
6706169689Skan      else
6707117395Skan	{
6708169689Skan	  if (!real_lvalue_p (expr))
6709117395Skan	    {
6710169689Skan	      tree init;
6711169689Skan	      tree type;
6712117395Skan
6713169689Skan	      /* Create the temporary variable.  */
6714169689Skan	      type = TREE_TYPE (expr);
6715169689Skan	      var = make_temporary_var_for_ref_to_temp (decl, type);
6716169689Skan	      layout_decl (var, 0);
6717169689Skan	      /* If the rvalue is the result of a function call it will be
6718169689Skan		 a TARGET_EXPR.  If it is some other construct (such as a
6719169689Skan		 member access expression where the underlying object is
6720169689Skan		 itself the result of a function call), turn it into a
6721169689Skan		 TARGET_EXPR here.  It is important that EXPR be a
6722169689Skan		 TARGET_EXPR below since otherwise the INIT_EXPR will
6723169689Skan		 attempt to make a bitwise copy of EXPR to initialize
6724169689Skan		 VAR.  */
6725169689Skan	      if (TREE_CODE (expr) != TARGET_EXPR)
6726169689Skan		expr = get_target_expr (expr);
6727169689Skan	      /* Create the INIT_EXPR that will initialize the temporary
6728169689Skan		 variable.  */
6729169689Skan	      init = build2 (INIT_EXPR, type, var, expr);
6730169689Skan	      if (at_function_scope_p ())
6731169689Skan		{
6732169689Skan		  add_decl_expr (var);
6733169689Skan		  *cleanup = cxx_maybe_build_cleanup (var);
6734122180Skan
6735169689Skan		  /* We must be careful to destroy the temporary only
6736169689Skan		     after its initialization has taken place.  If the
6737169689Skan		     initialization throws an exception, then the
6738169689Skan		     destructor should not be run.  We cannot simply
6739169689Skan		     transform INIT into something like:
6740169689Skan
6741169689Skan			 (INIT, ({ CLEANUP_STMT; }))
6742169689Skan
6743169689Skan		     because emit_local_var always treats the
6744169689Skan		     initializer as a full-expression.  Thus, the
6745169689Skan		     destructor would run too early; it would run at the
6746169689Skan		     end of initializing the reference variable, rather
6747169689Skan		     than at the end of the block enclosing the
6748169689Skan		     reference variable.
6749169689Skan
6750169689Skan		     The solution is to pass back a cleanup expression
6751169689Skan		     which the caller is responsible for attaching to
6752169689Skan		     the statement tree.  */
6753169689Skan		}
6754169689Skan	      else
6755169689Skan		{
6756169689Skan		  rest_of_decl_compilation (var, /*toplev=*/1, at_eof);
6757169689Skan		  if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
6758169689Skan		    static_aggregates = tree_cons (NULL_TREE, var,
6759169689Skan						   static_aggregates);
6760169689Skan		}
6761169689Skan	      /* Use its address to initialize the reference variable.  */
6762169689Skan	      expr = build_address (var);
6763169689Skan	      if (base_conv_type)
6764169689Skan		expr = convert_to_base (expr,
6765169689Skan					build_pointer_type (base_conv_type),
6766169689Skan					/*check_access=*/true,
6767169689Skan					/*nonnull=*/true);
6768169689Skan	      expr = build2 (COMPOUND_EXPR, TREE_TYPE (expr), init, expr);
6769117395Skan	    }
6770117395Skan	  else
6771169689Skan	    /* Take the address of EXPR.  */
6772169689Skan	    expr = build_unary_op (ADDR_EXPR, expr, 0);
6773169689Skan	  /* If a BASE_CONV was required, perform it now.  */
6774169689Skan	  if (base_conv_type)
6775169689Skan	    expr = (perform_implicit_conversion
6776169689Skan		    (build_pointer_type (base_conv_type), expr));
6777169689Skan	  expr = build_nop (type, expr);
6778117395Skan	}
6779117395Skan    }
6780169689Skan  else
6781169689Skan    /* Perform the conversion.  */
6782169689Skan    expr = convert_like (conv, expr);
6783117395Skan
6784169689Skan  /* Free all the conversions we allocated.  */
6785169689Skan  obstack_free (&conversion_obstack, p);
6786169689Skan
6787169689Skan  return expr;
678890075Sobrien}
6789117395Skan
6790117395Skan#include "gt-cp-call.h"
6791