call.c revision 146895
1145256Sjkoshy/* Functions related to invoking methods and overloaded functions.
2180148Sjkoshy   Copyright (C) 1987, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3174396Sjkoshy   1999, 2000, 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
4145256Sjkoshy   Contributed by Michael Tiemann (tiemann@cygnus.com) and
5145256Sjkoshy   modified by Brendan Kehoe (brendan@cygnus.com).
6174396Sjkoshy
7174396SjkoshyThis file is part of GCC.
8174396Sjkoshy
9145256SjkoshyGCC is free software; you can redistribute it and/or modify
10145256Sjkoshyit under the terms of the GNU General Public License as published by
11145256Sjkoshythe Free Software Foundation; either version 2, or (at your option)
12145256Sjkoshyany later version.
13145256Sjkoshy
14145256SjkoshyGCC is distributed in the hope that it will be useful,
15145256Sjkoshybut WITHOUT ANY WARRANTY; without even the implied warranty of
16145256SjkoshyMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17145256SjkoshyGNU General Public License for more details.
18145256Sjkoshy
19145256SjkoshyYou should have received a copy of the GNU General Public License
20145256Sjkoshyalong with GCC; see the file COPYING.  If not, write to
21145256Sjkoshythe Free Software Foundation, 59 Temple Place - Suite 330,
22145256SjkoshyBoston, MA 02111-1307, USA.  */
23145256Sjkoshy
24145256Sjkoshy
25145256Sjkoshy/* High-level class interface.  */
26145256Sjkoshy
27145256Sjkoshy#include "config.h"
28145256Sjkoshy#include "system.h"
29145256Sjkoshy#include "coretypes.h"
30145256Sjkoshy#include "tm.h"
31145256Sjkoshy#include "tree.h"
32145256Sjkoshy#include "cp-tree.h"
33145256Sjkoshy#include "output.h"
34224698Sattilio#include "flags.h"
35224698Sattilio#include "rtl.h"
36145256Sjkoshy#include "toplev.h"
37145256Sjkoshy#include "expr.h"
38147708Sjkoshy#include "diagnostic.h"
39147708Sjkoshy#include "intl.h"
40168949Sjkoshy#include "target.h"
41145256Sjkoshy#include "convert.h"
42145256Sjkoshy
43169069Sjkoshystatic struct z_candidate * tourney (struct z_candidate *);
44145256Sjkoshystatic int equal_functions (tree, tree);
45145256Sjkoshystatic int joust (struct z_candidate *, struct z_candidate *, bool);
46145256Sjkoshystatic int compare_ics (tree, tree);
47203790Sfabientstatic tree build_over_call (struct z_candidate *, int);
48145256Sjkoshystatic tree build_java_interface_fn_ref (tree, tree);
49145256Sjkoshy#define convert_like(CONV, EXPR)				\
50145256Sjkoshy  convert_like_real ((CONV), (EXPR), NULL_TREE, 0, 0, 		\
51169069Sjkoshy		     /*issue_conversion_warnings=*/true)
52157144Sjkoshy#define convert_like_with_context(CONV, EXPR, FN, ARGNO)	\
53145256Sjkoshy  convert_like_real ((CONV), (EXPR), (FN), (ARGNO), 0, 		\
54145256Sjkoshy		     /*issue_conversion_warnings=*/true)
55145256Sjkoshystatic tree convert_like_real (tree, tree, tree, int, int, bool);
56147191Sjkoshystatic void op_error (enum tree_code, enum tree_code, tree, tree,
57169069Sjkoshy			    tree, const char *);
58145256Sjkoshystatic tree build_object_call (tree, tree);
59145256Sjkoshystatic tree resolve_args (tree);
60147708Sjkoshystatic struct z_candidate *build_user_type_conversion_1 (tree, tree, int);
61145256Sjkoshystatic void print_z_candidate (const char *, struct z_candidate *);
62145256Sjkoshystatic void print_z_candidates (struct z_candidate *);
63145256Sjkoshystatic tree build_this (tree);
64145256Sjkoshystatic struct z_candidate *splice_viable (struct z_candidate *, bool, bool *);
65145256Sjkoshystatic bool any_strictly_viable (struct z_candidate *);
66145256Sjkoshystatic struct z_candidate *add_template_candidate
67147708Sjkoshy        (struct z_candidate **, tree, tree, tree, tree, tree,
68147708Sjkoshy         tree, tree, int, unification_kind_t);
69147191Sjkoshystatic struct z_candidate *add_template_candidate_real
70147191Sjkoshy	(struct z_candidate **, tree, tree, tree, tree, tree,
71147191Sjkoshy           tree, tree, int, tree, unification_kind_t);
72147191Sjkoshystatic struct z_candidate *add_template_conv_candidate
73147191Sjkoshy        (struct z_candidate **, tree, tree, tree, tree, tree, tree);
74147191Sjkoshystatic void add_builtin_candidates
75147191Sjkoshy	(struct z_candidate **, enum tree_code, enum tree_code,
76147191Sjkoshy	       tree, tree *, int);
77147191Sjkoshystatic void add_builtin_candidate
78147191Sjkoshy	(struct z_candidate **, enum tree_code, enum tree_code,
79147191Sjkoshy	       tree, tree, tree, tree *, tree *, int);
80147191Sjkoshystatic bool is_complete (tree);
81147191Sjkoshystatic void build_builtin_candidate
82147191Sjkoshy	(struct z_candidate **, tree, tree, tree, tree *, tree *,
83147191Sjkoshy	       int);
84168949Sjkoshystatic struct z_candidate *add_conv_candidate
85168949Sjkoshy	(struct z_candidate **, tree, tree, tree, tree, tree);
86168949Sjkoshystatic struct z_candidate *add_function_candidate
87168949Sjkoshy	(struct z_candidate **, tree, tree, tree, tree, tree, int);
88168949Sjkoshystatic tree implicit_conversion (tree, tree, tree, int);
89168949Sjkoshystatic tree standard_conversion (tree, tree, tree, int);
90168949Sjkoshystatic tree reference_binding (tree, tree, tree, int);
91168949Sjkoshystatic tree build_conv (enum tree_code, tree, tree);
92168949Sjkoshystatic bool is_subseq (tree, tree);
93168949Sjkoshystatic tree maybe_handle_ref_bind (tree *);
94168949Sjkoshystatic void maybe_handle_implicit_object (tree *);
95168949Sjkoshystatic struct z_candidate *add_candidate
96168949Sjkoshy        (struct z_candidate **, tree, tree, tree, tree, tree, int);
97168949Sjkoshystatic tree source_type (tree);
98168949Sjkoshystatic void add_warning (struct z_candidate *, struct z_candidate *);
99168949Sjkoshystatic bool reference_related_p (tree, tree);
100168949Sjkoshystatic bool reference_compatible_p (tree, tree);
101228990Suqsstatic tree convert_class_to_reference (tree, tree, tree);
102168949Sjkoshystatic tree direct_reference_binding (tree, tree);
103168949Sjkoshystatic bool promoted_arithmetic_type_p (tree);
104147191Sjkoshystatic tree conditional_conversion (tree, tree);
105147191Sjkoshystatic char *name_as_c_string (tree, tree, bool *);
106147708Sjkoshystatic tree call_builtin_trap (void);
107145256Sjkoshystatic tree prep_operand (tree);
108241737Sedstatic void add_candidates (tree, tree, tree, bool, tree, tree,
109241737Sed			    int, struct z_candidate **);
110241737Sedstatic tree merge_conversion_sequences (tree, tree);
111241737Sedstatic bool magic_varargs_p (tree);
112241737Sedstatic tree build_temp (tree, tree, int, void (**)(const char *, ...));
113241737Sedstatic void check_constructor_callable (tree, tree);
114203790Sfabient
115145256Sjkoshytree
116224698Sattiliobuild_vfield_ref (tree datum, tree type)
117224698Sattilio{
118224698Sattilio  if (datum == error_mark_node)
119283613Sjhb    return error_mark_node;
120224698Sattilio
121224698Sattilio  if (TREE_CODE (TREE_TYPE (datum)) == REFERENCE_TYPE)
122283613Sjhb    datum = convert_from_reference (datum);
123224698Sattilio
124224698Sattilio  if (TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (type)
125224698Sattilio      && !same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (datum), type))
126224698Sattilio    datum = convert_to_base (datum, type, /*check_access=*/false);
127224698Sattilio
128224698Sattilio  return build (COMPONENT_REF, TREE_TYPE (TYPE_VFIELD (type)),
129224698Sattilio		datum, TYPE_VFIELD (type));
130224698Sattilio}
131224698Sattilio
132224698Sattilio/* Returns nonzero iff the destructor name specified in NAME
133224698Sattilio   (a BIT_NOT_EXPR) matches BASETYPE.  The operand of NAME can take many
134224698Sattilio   forms...  */
135224698Sattilio
136224698Sattiliobool
137224698Sattiliocheck_dtor_name (tree basetype, tree name)
138224698Sattilio{
139224698Sattilio  name = TREE_OPERAND (name, 0);
140224698Sattilio
141224698Sattilio  /* Just accept something we've already complained about.  */
142224698Sattilio  if (name == error_mark_node)
143224698Sattilio    return true;
144224698Sattilio
145224698Sattilio  if (TREE_CODE (name) == TYPE_DECL)
146224698Sattilio    name = TREE_TYPE (name);
147224698Sattilio  else if (TYPE_P (name))
148224698Sattilio    /* OK */;
149224698Sattilio  else if (TREE_CODE (name) == IDENTIFIER_NODE)
150224698Sattilio    {
151224698Sattilio      if ((IS_AGGR_TYPE (basetype) && name == constructor_name (basetype))
152224698Sattilio	  || (TREE_CODE (basetype) == ENUMERAL_TYPE
153224698Sattilio	      && name == TYPE_IDENTIFIER (basetype)))
154224698Sattilio	name = basetype;
155224698Sattilio      else
156224698Sattilio	name = get_type_value (name);
157227524Sobrien    }
158227524Sobrien  /* In the case of:
159227524Sobrien
160224698Sattilio       template <class T> struct S { ~S(); };
161224698Sattilio       int i;
162224698Sattilio       i.~S();
163283613Sjhb
164224698Sattilio     NAME will be a class template.  */
165224698Sattilio  else if (DECL_CLASS_TEMPLATE_P (name))
166168949Sjkoshy    return false;
167203790Sfabient  else
168168949Sjkoshy    abort ();
169168949Sjkoshy
170169069Sjkoshy  if (name && TYPE_MAIN_VARIANT (basetype) == TYPE_MAIN_VARIANT (name))
171169069Sjkoshy    return true;
172145256Sjkoshy  return false;
173169069Sjkoshy}
174169069Sjkoshy
175203790Sfabient/* We want the address of a function or method.  We avoid creating a
176169069Sjkoshy   pointer-to-member function.  */
177169069Sjkoshy
178203790Sfabienttree
179169069Sjkoshybuild_addr_func (tree function)
180169069Sjkoshy{
181169069Sjkoshy  tree type = TREE_TYPE (function);
182227524Sobrien
183227524Sobrien  /* We have to do these by hand to avoid real pointer to member
184227524Sobrien     functions.  */
185169069Sjkoshy  if (TREE_CODE (type) == METHOD_TYPE)
186168949Sjkoshy    {
187169069Sjkoshy      if (TREE_CODE (function) == OFFSET_REF)
188169069Sjkoshy	{
189168949Sjkoshy	  tree object = build_address (TREE_OPERAND (function, 0));
190168949Sjkoshy	  return get_member_function_from_ptrfunc (&object,
191168949Sjkoshy						   TREE_OPERAND (function, 1));
192145256Sjkoshy	}
193203790Sfabient      function = build_address (function);
194145256Sjkoshy    }
195145256Sjkoshy  else
196145256Sjkoshy    function = decay_conversion (function);
197145256Sjkoshy
198203790Sfabient  return function;
199145256Sjkoshy}
200185079Sjkoshy
201227524Sobrien/* Build a CALL_EXPR, we can handle FUNCTION_TYPEs, METHOD_TYPEs, or
202227524Sobrien   POINTER_TYPE to those.  Note, pointer to member function types
203145256Sjkoshy   (TYPE_PTRMEMFUNC_P) must be handled by our callers.  */
204227524Sobrien
205227524Sobrientree
206145256Sjkoshybuild_call (tree function, tree parms)
207145256Sjkoshy{
208203790Sfabient  int is_constructor = 0;
209145256Sjkoshy  int nothrow;
210145256Sjkoshy  tree tmp;
211147191Sjkoshy  tree decl;
212147863Sjkoshy  tree result_type;
213203790Sfabient  tree fntype;
214147863Sjkoshy
215147863Sjkoshy  function = build_addr_func (function);
216203790Sfabient
217203790Sfabient  if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function)))
218203790Sfabient    {
219147191Sjkoshy      sorry ("unable to call pointer to member function here");
220147708Sjkoshy      return error_mark_node;
221210794Sfabient    }
222145256Sjkoshy
223145256Sjkoshy  fntype = TREE_TYPE (TREE_TYPE (function));
224145256Sjkoshy  result_type = TREE_TYPE (fntype);
225203790Sfabient
226168949Sjkoshy  if (TREE_CODE (function) == ADDR_EXPR
227168949Sjkoshy      && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
228169069Sjkoshy    decl = TREE_OPERAND (function, 0);
229168949Sjkoshy  else
230169069Sjkoshy    decl = NULL_TREE;
231168949Sjkoshy
232168949Sjkoshy  /* We check both the decl and the type; a function may be known not to
233168949Sjkoshy     throw without being declared throw().  */
234168949Sjkoshy  nothrow = ((decl && TREE_NOTHROW (decl))
235169069Sjkoshy	     || TYPE_NOTHROW_P (TREE_TYPE (TREE_TYPE (function))));
236168949Sjkoshy
237168949Sjkoshy  if (decl && TREE_THIS_VOLATILE (decl) && cfun)
238168949Sjkoshy    current_function_returns_abnormally = 1;
239168949Sjkoshy
240168949Sjkoshy  if (decl && TREE_DEPRECATED (decl))
241168949Sjkoshy    warn_deprecated_use (decl);
242168949Sjkoshy  require_complete_eh_spec_types (fntype, decl);
243168949Sjkoshy
244168949Sjkoshy  if (decl && DECL_CONSTRUCTOR_P (decl))
245168949Sjkoshy    is_constructor = 1;
246168949Sjkoshy
247168949Sjkoshy  if (decl && ! TREE_USED (decl))
248168949Sjkoshy    {
249168949Sjkoshy      /* We invoke build_call directly for several library functions.
250168949Sjkoshy	 These may have been declared normally if we're building libgcc,
251168949Sjkoshy	 so we can't just check DECL_ARTIFICIAL.  */
252168949Sjkoshy      if (DECL_ARTIFICIAL (decl)
253203790Sfabient	  || !strncmp (IDENTIFIER_POINTER (DECL_NAME (decl)), "__", 2))
254168949Sjkoshy	mark_used (decl);
255168949Sjkoshy      else
256203790Sfabient	abort ();
257168949Sjkoshy    }
258168949Sjkoshy
259168949Sjkoshy  /* Don't pass empty class objects by value.  This is useful
260168949Sjkoshy     for tags in STL, which are used to control overload resolution.
261168949Sjkoshy     We don't need to handle other cases of copying empty classes.  */
262168949Sjkoshy  if (! decl || ! DECL_BUILT_IN (decl))
263168949Sjkoshy    for (tmp = parms; tmp; tmp = TREE_CHAIN (tmp))
264168949Sjkoshy      if (is_empty_class (TREE_TYPE (TREE_VALUE (tmp)))
265169069Sjkoshy	  && ! TREE_ADDRESSABLE (TREE_TYPE (TREE_VALUE (tmp))))
266168949Sjkoshy	{
267168949Sjkoshy	  tree t = build (EMPTY_CLASS_EXPR, TREE_TYPE (TREE_VALUE (tmp)));
268169069Sjkoshy	  TREE_VALUE (tmp) = build (COMPOUND_EXPR, TREE_TYPE (t),
269168949Sjkoshy				    TREE_VALUE (tmp), t);
270169069Sjkoshy	}
271169069Sjkoshy
272169069Sjkoshy  function = build (CALL_EXPR, result_type, function, parms);
273169069Sjkoshy  TREE_HAS_CONSTRUCTOR (function) = is_constructor;
274203790Sfabient  TREE_NOTHROW (function) = nothrow;
275169069Sjkoshy
276168949Sjkoshy  return function;
277168949Sjkoshy}
278168949Sjkoshy
279168949Sjkoshy/* Build something of the form ptr->method (args)
280168949Sjkoshy   or object.method (args).  This can also build
281168949Sjkoshy   calls to constructors, and find friends.
282168949Sjkoshy
283169069Sjkoshy   Member functions always take their class variable
284203790Sfabient   as a pointer.
285169069Sjkoshy
286169069Sjkoshy   INSTANCE is a class instance.
287169069Sjkoshy
288169069Sjkoshy   NAME is the name of the method desired, usually an IDENTIFIER_NODE.
289169069Sjkoshy
290169069Sjkoshy   PARMS help to figure out what that NAME really refers to.
291169069Sjkoshy
292169069Sjkoshy   BASETYPE_PATH, if non-NULL, contains a chain from the type of INSTANCE
293169069Sjkoshy   down to the real instance type to use for access checking.  We need this
294169069Sjkoshy   information to get protected accesses correct.
295169069Sjkoshy
296169069Sjkoshy   FLAGS is the logical disjunction of zero or more LOOKUP_
297169069Sjkoshy   flags.  See cp-tree.h for more info.
298169069Sjkoshy
299203790Sfabient   If this is all OK, calls build_function_call with the resolved
300169069Sjkoshy   member function.
301169069Sjkoshy
302169069Sjkoshy   This function must also handle being called to perform
303169069Sjkoshy   initialization, promotion/coercion of arguments, and
304169069Sjkoshy   instantiation of default parameters.
305169069Sjkoshy
306169069Sjkoshy   Note that NAME may refer to an instance variable name.  If
307169069Sjkoshy   `operator()()' is defined for the type of that field, then we return
308169069Sjkoshy   that result.  */
309169069Sjkoshy
310169069Sjkoshy/* New overloading code.  */
311169069Sjkoshy
312169069Sjkoshystruct z_candidate GTY(()) {
313208858Sfabient  /* The FUNCTION_DECL that will be called if this candidate is
314208858Sfabient     selected by overload resolution.  */
315169069Sjkoshy  tree fn;
316169069Sjkoshy  /* The arguments to use when calling this function.  */
317169069Sjkoshy  tree args;
318169069Sjkoshy  /* The implicit conversion sequences for each of the arguments to
319169069Sjkoshy     FN.  */
320169069Sjkoshy  tree convs;
321169069Sjkoshy  /* If FN is a user-defined conversion, the standard conversion
322169069Sjkoshy     sequence from the type returned by FN to the desired destination
323169069Sjkoshy     type.  */
324169069Sjkoshy  tree second_conv;
325169069Sjkoshy  int viable;
326169069Sjkoshy  /* If FN is a member function, the binfo indicating the path used to
327203790Sfabient     qualify the name of FN at the call site.  This path is used to
328169069Sjkoshy     determine whether or not FN is accessible if it is selected by
329169069Sjkoshy     overload resolution.  The DECL_CONTEXT of FN will always be a
330169069Sjkoshy     (possibly improper) base of this binfo.  */
331169069Sjkoshy  tree access_path;
332169069Sjkoshy  /* If FN is a non-static member function, the binfo indicating the
333169069Sjkoshy     subobject to which the `this' pointer should be converted if FN
334169069Sjkoshy     is selected by overload resolution.  The type pointed to the by
335169069Sjkoshy     the `this' pointer must correspond to the most derived class
336169069Sjkoshy     indicated by the CONVERSION_PATH.  */
337169069Sjkoshy  tree conversion_path;
338169069Sjkoshy  tree template;
339169069Sjkoshy  tree warnings;
340169069Sjkoshy  struct z_candidate *next;
341169069Sjkoshy};
342169069Sjkoshy
343169069Sjkoshy#define IDENTITY_RANK 0
344168949Sjkoshy#define EXACT_RANK 1
345203790Sfabient#define PROMO_RANK 2
346169069Sjkoshy#define STD_RANK 3
347169069Sjkoshy#define PBOOL_RANK 4
348169069Sjkoshy#define USER_RANK 5
349203790Sfabient#define ELLIPSIS_RANK 6
350169069Sjkoshy#define BAD_RANK 7
351169069Sjkoshy
352169069Sjkoshy#define ICS_RANK(NODE)				\
353169069Sjkoshy  (ICS_BAD_FLAG (NODE) ? BAD_RANK   		\
354169069Sjkoshy   : ICS_ELLIPSIS_FLAG (NODE) ? ELLIPSIS_RANK	\
355203790Sfabient   : ICS_USER_FLAG (NODE) ? USER_RANK		\
356169069Sjkoshy   : ICS_STD_RANK (NODE))
357169069Sjkoshy
358169069Sjkoshy#define ICS_STD_RANK(NODE) TREE_COMPLEXITY (NODE)
359169069Sjkoshy
360169069Sjkoshy#define ICS_USER_FLAG(NODE) TREE_LANG_FLAG_0 (NODE)
361169069Sjkoshy#define ICS_ELLIPSIS_FLAG(NODE) TREE_LANG_FLAG_1 (NODE)
362169069Sjkoshy#define ICS_THIS_FLAG(NODE) TREE_LANG_FLAG_2 (NODE)
363203790Sfabient#define ICS_BAD_FLAG(NODE) TREE_LANG_FLAG_3 (NODE)
364145256Sjkoshy
365145256Sjkoshy/* In a REF_BIND or a BASE_CONV, this indicates that a temporary
366145256Sjkoshy   should be created to hold the result of the conversion.  */
367169069Sjkoshy#define NEED_TEMPORARY_P(NODE) TREE_LANG_FLAG_4 (NODE)
368145256Sjkoshy
369145256Sjkoshy/* TRUE in an IDENTITY_CONV or BASE_CONV if the copy constructor must
370145256Sjkoshy   be accessible, even though it is not being used.  */
371145256Sjkoshy#define CHECK_COPY_CONSTRUCTOR_P(NODE) TREE_LANG_FLAG_5 (NODE)
372147191Sjkoshy
373145256Sjkoshy#define USER_CONV_CAND(NODE) WRAPPER_ZC (TREE_OPERAND (NODE, 1))
374203790Sfabient#define USER_CONV_FN(NODE) (USER_CONV_CAND (NODE)->fn)
375147191Sjkoshy
376145256Sjkoshy/* Returns true iff T is a null pointer constant in the sense of
377145256Sjkoshy   [conv.ptr].  */
378145256Sjkoshy
379145256Sjkoshybool
380145256Sjkoshynull_ptr_cst_p (tree t)
381145256Sjkoshy{
382203790Sfabient  /* [conv.ptr]
383145256Sjkoshy
384145256Sjkoshy     A null pointer constant is an integral constant expression
385168949Sjkoshy     (_expr.const_) rvalue of integer type that evaluates to zero.  */
386145256Sjkoshy  if (DECL_INTEGRAL_CONSTANT_VAR_P (t))
387203790Sfabient    t = decl_constant_value (t);
388145256Sjkoshy  if (t == null_node
389203790Sfabient      || (CP_INTEGRAL_TYPE_P (TREE_TYPE (t)) && integer_zerop (t)))
390145256Sjkoshy    return true;
391145256Sjkoshy  return false;
392145256Sjkoshy}
393145256Sjkoshy
394145256Sjkoshy
395168949Sjkoshy/* Returns nonzero if PARMLIST consists of only default parms and/or
396203790Sfabient   ellipsis.  */
397168949Sjkoshy
398168949Sjkoshybool
399168949Sjkoshysufficient_parms_p (tree parmlist)
400168949Sjkoshy{
401203790Sfabient  for (; parmlist && parmlist != void_list_node;
402168949Sjkoshy       parmlist = TREE_CHAIN (parmlist))
403168949Sjkoshy    if (!TREE_PURPOSE (parmlist))
404203790Sfabient      return false;
405145256Sjkoshy  return true;
406145256Sjkoshy}
407145256Sjkoshy
408203790Sfabientstatic tree
409145256Sjkoshybuild_conv (enum tree_code code, tree type, tree from)
410145256Sjkoshy{
411145256Sjkoshy  tree t;
412203790Sfabient  int rank = ICS_STD_RANK (from);
413145256Sjkoshy
414145256Sjkoshy  /* We can't use buildl1 here because CODE could be USER_CONV, which
415145256Sjkoshy     takes two arguments.  In that case, the caller is responsible for
416145256Sjkoshy     filling in the second argument.  */
417145256Sjkoshy  t = make_node (code);
418145256Sjkoshy  TREE_TYPE (t) = type;
419145256Sjkoshy  TREE_OPERAND (t, 0) = from;
420203790Sfabient
421145256Sjkoshy  switch (code)
422145256Sjkoshy    {
423145256Sjkoshy    case PTR_CONV:
424145256Sjkoshy    case PMEM_CONV:
425145256Sjkoshy    case BASE_CONV:
426145256Sjkoshy    case STD_CONV:
427227524Sobrien      if (rank < STD_RANK)
428227524Sobrien	rank = STD_RANK;
429145256Sjkoshy      break;
430203790Sfabient
431147708Sjkoshy    case QUAL_CONV:
432147708Sjkoshy      if (rank < EXACT_RANK)
433147708Sjkoshy	rank = EXACT_RANK;
434147708Sjkoshy
435145256Sjkoshy    default:
436145256Sjkoshy      break;
437145256Sjkoshy    }
438145256Sjkoshy  ICS_STD_RANK (t) = rank;
439145256Sjkoshy  ICS_USER_FLAG (t) = (code == USER_CONV || ICS_USER_FLAG (from));
440203790Sfabient  ICS_BAD_FLAG (t) = ICS_BAD_FLAG (from);
441145256Sjkoshy  return t;
442145256Sjkoshy}
443145256Sjkoshy
444145256Sjkoshytree
445145256Sjkoshystrip_top_quals (tree t)
446145256Sjkoshy{
447145256Sjkoshy  if (TREE_CODE (t) == ARRAY_TYPE)
448203790Sfabient    return t;
449145256Sjkoshy  return cp_build_qualified_type (t, 0);
450145256Sjkoshy}
451145256Sjkoshy
452147708Sjkoshy/* Returns the standard conversion path (see [conv]) from type FROM to type
453145256Sjkoshy   TO, if any.  For proper handling of null pointer constants, you must
454203790Sfabient   also pass the expression EXPR to convert from.  */
455145256Sjkoshy
456145256Sjkoshystatic tree
457145256Sjkoshystandard_conversion (tree to, tree from, tree expr, int flags)
458203790Sfabient{
459203790Sfabient  enum tree_code fcode, tcode;
460145256Sjkoshy  tree conv;
461203790Sfabient  bool fromref = false;
462145256Sjkoshy
463145256Sjkoshy  to = non_reference (to);
464145256Sjkoshy  if (TREE_CODE (from) == REFERENCE_TYPE)
465145256Sjkoshy    {
466145256Sjkoshy      fromref = true;
467145256Sjkoshy      from = TREE_TYPE (from);
468145256Sjkoshy    }
469145256Sjkoshy  to = strip_top_quals (to);
470145256Sjkoshy  from = strip_top_quals (from);
471145256Sjkoshy
472145256Sjkoshy  if ((TYPE_PTRFN_P (to) || TYPE_PTRMEMFUNC_P (to))
473145256Sjkoshy      && expr && type_unknown_p (expr))
474145256Sjkoshy    {
475168949Sjkoshy      expr = instantiate_type (to, expr, tf_conv);
476145256Sjkoshy      if (expr == error_mark_node)
477168949Sjkoshy	return NULL_TREE;
478168949Sjkoshy      from = TREE_TYPE (expr);
479168949Sjkoshy    }
480145256Sjkoshy
481168949Sjkoshy  fcode = TREE_CODE (from);
482145256Sjkoshy  tcode = TREE_CODE (to);
483145256Sjkoshy
484145256Sjkoshy  conv = build1 (IDENTITY_CONV, from, expr);
485145256Sjkoshy
486145256Sjkoshy  if (fcode == FUNCTION_TYPE)
487145256Sjkoshy    {
488145256Sjkoshy      from = build_pointer_type (from);
489145256Sjkoshy      fcode = TREE_CODE (from);
490145256Sjkoshy      conv = build_conv (LVALUE_CONV, from, conv);
491145256Sjkoshy    }
492147191Sjkoshy  else if (fcode == ARRAY_TYPE)
493147191Sjkoshy    {
494147191Sjkoshy      from = build_pointer_type (TREE_TYPE (from));
495203790Sfabient      fcode = TREE_CODE (from);
496203790Sfabient      conv = build_conv (LVALUE_CONV, from, conv);
497174396Sjkoshy    }
498157144Sjkoshy  else if (fromref || (expr && lvalue_p (expr)))
499174396Sjkoshy    conv = build_conv (RVALUE_CONV, from, conv);
500147191Sjkoshy
501147191Sjkoshy   /* Allow conversion between `__complex__' data types.  */
502147191Sjkoshy  if (tcode == COMPLEX_TYPE && fcode == COMPLEX_TYPE)
503147191Sjkoshy    {
504203790Sfabient      /* The standard conversion sequence to convert FROM to TO is
505147191Sjkoshy         the standard conversion sequence to perform componentwise
506265086Sscottl         conversion.  */
507168949Sjkoshy      tree part_conv = standard_conversion
508147191Sjkoshy        (TREE_TYPE (to), TREE_TYPE (from), NULL_TREE, flags);
509291016Sjtl
510203790Sfabient      if (part_conv)
511147191Sjkoshy        {
512162804Sru          conv = build_conv (TREE_CODE (part_conv), to, conv);
513266209Sgnn          ICS_STD_RANK (conv) = ICS_STD_RANK (part_conv);
514239571Sjimharris        }
515145256Sjkoshy      else
516145256Sjkoshy        conv = NULL_TREE;
517147191Sjkoshy
518157144Sjkoshy      return conv;
519157144Sjkoshy    }
520147191Sjkoshy
521183186Sjkoshy  if (same_type_p (from, to))
522183186Sjkoshy    return conv;
523157144Sjkoshy
524174396Sjkoshy  if ((tcode == POINTER_TYPE || TYPE_PTR_TO_MEMBER_P (to))
525174396Sjkoshy      && expr && null_ptr_cst_p (expr))
526145256Sjkoshy    conv = build_conv (STD_CONV, to, conv);
527145256Sjkoshy  else if (tcode == POINTER_TYPE && fcode == POINTER_TYPE
528145256Sjkoshy	   && TREE_CODE (TREE_TYPE (to)) == VECTOR_TYPE
529145256Sjkoshy	   && TREE_CODE (TREE_TYPE (from)) == VECTOR_TYPE
530203790Sfabient	   && ((*targetm.vector_opaque_p) (TREE_TYPE (to))
531203790Sfabient	       || (*targetm.vector_opaque_p) (TREE_TYPE (from))))
532203790Sfabient    conv = build_conv (STD_CONV, to, conv);
533203790Sfabient  else if ((tcode == INTEGER_TYPE && fcode == POINTER_TYPE)
534203790Sfabient	   || (tcode == POINTER_TYPE && fcode == INTEGER_TYPE))
535203790Sfabient    {
536203790Sfabient      /* For backwards brain damage compatibility, allow interconversion of
537203790Sfabient	 pointers and integers with a pedwarn.  */
538203790Sfabient      conv = build_conv (STD_CONV, to, conv);
539203790Sfabient      ICS_BAD_FLAG (conv) = 1;
540203790Sfabient    }
541203790Sfabient  else if (tcode == ENUMERAL_TYPE && fcode == INTEGER_TYPE)
542203790Sfabient    {
543203790Sfabient      /* For backwards brain damage compatibility, allow interconversion of
544203790Sfabient	 enums and integers with a pedwarn.  */
545203790Sfabient      conv = build_conv (STD_CONV, to, conv);
546203790Sfabient      ICS_BAD_FLAG (conv) = 1;
547203790Sfabient    }
548145256Sjkoshy  else if ((tcode == POINTER_TYPE && fcode == POINTER_TYPE)
549145256Sjkoshy	   || (TYPE_PTRMEM_P (to) && TYPE_PTRMEM_P (from)))
550145256Sjkoshy    {
551145256Sjkoshy      tree to_pointee;
552145256Sjkoshy      tree from_pointee;
553145256Sjkoshy
554283613Sjhb      if (tcode == POINTER_TYPE
555145256Sjkoshy	  && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (from),
556266209Sgnn							TREE_TYPE (to)))
557283613Sjhb	;
558230350Seadler      else if (VOID_TYPE_P (TREE_TYPE (to))
559174396Sjkoshy	       && !TYPE_PTRMEM_P (from)
560226514Sfabient	       && TREE_CODE (TREE_TYPE (from)) != FUNCTION_TYPE)
561282643Sjhb	{
562174396Sjkoshy	  from = build_pointer_type
563210794Sfabient	    (cp_build_qualified_type (void_type_node,
564147191Sjkoshy				      cp_type_quals (TREE_TYPE (from))));
565203790Sfabient	  conv = build_conv (PTR_CONV, from, conv);
566157144Sjkoshy	}
567174396Sjkoshy      else if (TYPE_PTRMEM_P (from))
568147191Sjkoshy	{
569151542Sjkoshy	  tree fbase = TYPE_PTRMEM_CLASS_TYPE (from);
570145256Sjkoshy	  tree tbase = TYPE_PTRMEM_CLASS_TYPE (to);
571145256Sjkoshy
572145256Sjkoshy	  if (DERIVED_FROM_P (fbase, tbase)
573145256Sjkoshy	      && (same_type_ignoring_top_level_qualifiers_p
574147708Sjkoshy		  (TYPE_PTRMEM_POINTED_TO_TYPE (from),
575157144Sjkoshy		   TYPE_PTRMEM_POINTED_TO_TYPE (to))))
576145256Sjkoshy	    {
577151542Sjkoshy	      from = build_ptrmem_type (tbase,
578145256Sjkoshy					TYPE_PTRMEM_POINTED_TO_TYPE (from));
579174396Sjkoshy	      conv = build_conv (PMEM_CONV, from, conv);
580145256Sjkoshy	    }
581147191Sjkoshy	  else if (!same_type_p (fbase, tbase))
582147191Sjkoshy	    return NULL;
583145256Sjkoshy	}
584174396Sjkoshy      else if (IS_AGGR_TYPE (TREE_TYPE (from))
585147191Sjkoshy	       && IS_AGGR_TYPE (TREE_TYPE (to))
586145256Sjkoshy	       /* [conv.ptr]
587157144Sjkoshy
588147708Sjkoshy	          An rvalue of type "pointer to cv D," where D is a
589157144Sjkoshy		  class type, can be converted to an rvalue of type
590147708Sjkoshy		  "pointer to cv B," where B is a base class (clause
591147708Sjkoshy		  _class.derived_) of D.  If B is an inaccessible
592174396Sjkoshy		  (clause _class.access_) or ambiguous
593174396Sjkoshy		  (_class.member.lookup_) base class of D, a program
594145256Sjkoshy		  that necessitates this conversion is ill-formed.  */
595157144Sjkoshy	       /* Therefore, we use DERIVED_FROM_P, and not
596174396Sjkoshy		  ACESSIBLY_UNIQUELY_DERIVED_FROM_P, in this test.  */
597174396Sjkoshy	       && DERIVED_FROM_P (TREE_TYPE (to), TREE_TYPE (from)))
598203790Sfabient	{
599203790Sfabient	  from =
600203790Sfabient	    cp_build_qualified_type (TREE_TYPE (to),
601203790Sfabient				     cp_type_quals (TREE_TYPE (from)));
602203790Sfabient	  from = build_pointer_type (from);
603203790Sfabient	  conv = build_conv (PTR_CONV, from, conv);
604203790Sfabient	}
605266209Sgnn
606169069Sjkoshy      if (tcode == POINTER_TYPE)
607169069Sjkoshy	{
608153704Sjkoshy	  to_pointee = TREE_TYPE (to);
609153704Sjkoshy	  from_pointee = TREE_TYPE (from);
610145256Sjkoshy	}
611224698Sattilio      else
612145256Sjkoshy	{
613282643Sjhb	  to_pointee = TYPE_PTRMEM_POINTED_TO_TYPE (to);
614282643Sjhb	  from_pointee = TYPE_PTRMEM_POINTED_TO_TYPE (from);
615282643Sjhb	}
616282643Sjhb
617282643Sjhb      if (same_type_p (from, to))
618282643Sjhb	/* OK */;
619282643Sjhb      else if (comp_ptr_ttypes (to_pointee, from_pointee))
620282643Sjhb	conv = build_conv (QUAL_CONV, to, conv);
621174396Sjkoshy      else if (expr && string_conv_p (to, expr, 0))
622283613Sjhb	/* converting from string constant to char *.  */
623283613Sjhb	conv = build_conv (QUAL_CONV, to, conv);
624174396Sjkoshy      else if (ptr_reasonably_similar (to_pointee, from_pointee))
625283613Sjhb	{
626283613Sjhb	  conv = build_conv (PTR_CONV, to, conv);
627283613Sjhb	  ICS_BAD_FLAG (conv) = 1;
628283613Sjhb	}
629168949Sjkoshy      else
630157144Sjkoshy	return 0;
631291016Sjtl
632145256Sjkoshy      from = to;
633262424Sadrian    }
634262424Sadrian  else if (TYPE_PTRMEMFUNC_P (to) && TYPE_PTRMEMFUNC_P (from))
635262424Sadrian    {
636262424Sadrian      tree fromfn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (from));
637262424Sadrian      tree tofn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (to));
638262424Sadrian      tree fbase = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (fromfn)));
639145256Sjkoshy      tree tbase = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (tofn)));
640145256Sjkoshy
641147191Sjkoshy      if (!DERIVED_FROM_P (fbase, tbase)
642145256Sjkoshy	  || !same_type_p (TREE_TYPE (fromfn), TREE_TYPE (tofn))
643145256Sjkoshy	  || !compparms (TREE_CHAIN (TYPE_ARG_TYPES (fromfn)),
644145256Sjkoshy			 TREE_CHAIN (TYPE_ARG_TYPES (tofn)))
645283613Sjhb	  || cp_type_quals (fbase) != cp_type_quals (tbase))
646283613Sjhb	return 0;
647283613Sjhb
648224698Sattilio      from = cp_build_qualified_type (tbase, cp_type_quals (fbase));
649168949Sjkoshy      from = build_method_type_directly (from,
650210797Sfabient					 TREE_TYPE (fromfn),
651147191Sjkoshy					 TREE_CHAIN (TYPE_ARG_TYPES (fromfn)));
652145256Sjkoshy      from = build_ptrmemfunc_type (build_pointer_type (from));
653145256Sjkoshy      conv = build_conv (PMEM_CONV, from, conv);
654147708Sjkoshy    }
655147708Sjkoshy  else if (tcode == BOOLEAN_TYPE)
656147708Sjkoshy    {
657147708Sjkoshy      /* [conv.bool]
658147708Sjkoshy
659227524Sobrien          An rvalue of arithmetic, enumeration, pointer, or pointer to
660227524Sobrien	  member type can be converted to an rvalue of type bool.  */
661227524Sobrien      if (ARITHMETIC_TYPE_P (from)
662147708Sjkoshy	  || fcode == ENUMERAL_TYPE
663147708Sjkoshy	  || fcode == POINTER_TYPE
664147708Sjkoshy	  || TYPE_PTR_TO_MEMBER_P (from))
665147708Sjkoshy	{
666147708Sjkoshy	  conv = build_conv (STD_CONV, to, conv);
667145256Sjkoshy	  if (fcode == POINTER_TYPE
668145256Sjkoshy	      || TYPE_PTRMEM_P (from)
669147191Sjkoshy	      || (TYPE_PTRMEMFUNC_P (from)
670145256Sjkoshy		  && ICS_STD_RANK (conv) < PBOOL_RANK))
671145256Sjkoshy	    ICS_STD_RANK (conv) = PBOOL_RANK;
672291016Sjtl	  return conv;
673291016Sjtl	}
674291016Sjtl
675291016Sjtl      return NULL_TREE;
676203790Sfabient    }
677203790Sfabient  /* We don't check for ENUMERAL_TYPE here because there are no standard
678203790Sfabient     conversions to enum type.  */
679203790Sfabient  else if (tcode == INTEGER_TYPE || tcode == BOOLEAN_TYPE
680203790Sfabient	   || tcode == REAL_TYPE)
681203790Sfabient    {
682203790Sfabient      if (! (INTEGRAL_CODE_P (fcode) || fcode == REAL_TYPE))
683203790Sfabient	return 0;
684203790Sfabient      conv = build_conv (STD_CONV, to, conv);
685203790Sfabient
686203790Sfabient      /* Give this a better rank if it's a promotion.  */
687203790Sfabient      if (same_type_p (to, type_promotes_to (from))
688174396Sjkoshy	  && ICS_STD_RANK (TREE_OPERAND (conv, 0)) <= PROMO_RANK)
689174396Sjkoshy	ICS_STD_RANK (conv) = PROMO_RANK;
690203790Sfabient    }
691174396Sjkoshy  else if (fcode == VECTOR_TYPE && tcode == VECTOR_TYPE
692174396Sjkoshy      && ((*targetm.vector_opaque_p) (from)
693174396Sjkoshy	  || (*targetm.vector_opaque_p) (to)))
694147191Sjkoshy    return build_conv (STD_CONV, to, conv);
695147191Sjkoshy  else if (!(flags & LOOKUP_CONSTRUCTOR_CALLABLE)
696203790Sfabient	   && IS_AGGR_TYPE (to) && IS_AGGR_TYPE (from)
697203790Sfabient	   && is_properly_derived_from (from, to))
698147191Sjkoshy    {
699147191Sjkoshy      if (TREE_CODE (conv) == RVALUE_CONV)
700147708Sjkoshy	conv = TREE_OPERAND (conv, 0);
701157144Sjkoshy      conv = build_conv (BASE_CONV, to, conv);
702157144Sjkoshy      /* The derived-to-base conversion indicates the initialization
703174396Sjkoshy	 of a parameter with base type from an object of a derived
704147708Sjkoshy	 type.  A temporary object is created to hold the result of
705147191Sjkoshy	 the conversion.  */
706147191Sjkoshy      NEED_TEMPORARY_P (conv) = 1;
707266209Sgnn    }
708266209Sgnn  else
709266209Sgnn    return 0;
710266209Sgnn
711266209Sgnn  return conv;
712266209Sgnn}
713266209Sgnn
714266209Sgnn/* Returns nonzero if T1 is reference-related to T2.  */
715266209Sgnn
716185322Sattiliostatic bool
717203790Sfabientreference_related_p (tree t1, tree t2)
718203790Sfabient{
719203790Sfabient  t1 = TYPE_MAIN_VARIANT (t1);
720185322Sattilio  t2 = TYPE_MAIN_VARIANT (t2);
721185322Sattilio
722147191Sjkoshy  /* [dcl.init.ref]
723147191Sjkoshy
724147191Sjkoshy     Given types "cv1 T1" and "cv2 T2," "cv1 T1" is reference-related
725147708Sjkoshy     to "cv2 T2" if T1 is the same type as T2, or T1 is a base class
726147191Sjkoshy     of T2.  */
727147191Sjkoshy  return (same_type_p (t1, t2)
728157144Sjkoshy	  || (CLASS_TYPE_P (t1) && CLASS_TYPE_P (t2)
729157144Sjkoshy	      && DERIVED_FROM_P (t1, t2)));
730157144Sjkoshy}
731157144Sjkoshy
732174396Sjkoshy/* Returns nonzero if T1 is reference-compatible with T2.  */
733174396Sjkoshy
734174396Sjkoshystatic bool
735174396Sjkoshyreference_compatible_p (tree t1, tree t2)
736174396Sjkoshy{
737145256Sjkoshy  /* [dcl.init.ref]
738145256Sjkoshy
739145256Sjkoshy     "cv1 T1" is reference compatible with "cv2 T2" if T1 is
740145256Sjkoshy     reference-related to T2 and cv1 is the same cv-qualification as,
741145256Sjkoshy     or greater cv-qualification than, cv2.  */
742147191Sjkoshy  return (reference_related_p (t1, t2)
743145256Sjkoshy	  && at_least_as_qualified_p (t1, t2));
744145256Sjkoshy}
745145256Sjkoshy
746145256Sjkoshy/* Determine whether or not the EXPR (of class type S) can be
747145256Sjkoshy   converted to T as in [over.match.ref].  */
748145256Sjkoshy
749145256Sjkoshystatic tree
750145256Sjkoshyconvert_class_to_reference (tree t, tree s, tree expr)
751147191Sjkoshy{
752147191Sjkoshy  tree conversions;
753147708Sjkoshy  tree arglist;
754169069Sjkoshy  tree conv;
755147191Sjkoshy  tree reference_type;
756145256Sjkoshy  struct z_candidate *candidates;
757147191Sjkoshy  struct z_candidate *cand;
758147191Sjkoshy  bool any_viable_p;
759147708Sjkoshy
760147708Sjkoshy  conversions = lookup_conversions (s);
761147191Sjkoshy  if (!conversions)
762145256Sjkoshy    return NULL_TREE;
763145256Sjkoshy
764147191Sjkoshy  /* [over.match.ref]
765145256Sjkoshy
766147191Sjkoshy     Assuming that "cv1 T" is the underlying type of the reference
767147191Sjkoshy     being initialized, and "cv S" is the type of the initializer
768147191Sjkoshy     expression, with S a class type, the candidate functions are
769145256Sjkoshy     selected as follows:
770145256Sjkoshy
771145256Sjkoshy     --The conversion functions of S and its base classes are
772145256Sjkoshy       considered.  Those that are not hidden within S and yield type
773145256Sjkoshy       "reference to cv2 T2", where "cv1 T" is reference-compatible
774145256Sjkoshy       (_dcl.init.ref_) with "cv2 T2", are candidate functions.
775145256Sjkoshy
776283613Sjhb     The argument list has one argument, which is the initializer
777287374Sjhb     expression.  */
778283613Sjhb
779145256Sjkoshy  candidates = 0;
780145256Sjkoshy
781147191Sjkoshy  /* Conceptually, we should take the address of EXPR and put it in
782174396Sjkoshy     the argument list.  Unfortunately, however, that can result in
783174396Sjkoshy     error messages, which we should not issue now because we are just
784147191Sjkoshy     trying to find a conversion operator.  Therefore, we use NULL,
785147191Sjkoshy     cast to the appropriate type.  */
786147191Sjkoshy  arglist = build_int_2 (0, 0);
787147191Sjkoshy  TREE_TYPE (arglist) = build_pointer_type (s);
788147191Sjkoshy  arglist = build_tree_list (NULL_TREE, arglist);
789147191Sjkoshy
790147191Sjkoshy  reference_type = build_reference_type (t);
791145256Sjkoshy
792145256Sjkoshy  while (conversions)
793145256Sjkoshy    {
794145256Sjkoshy      tree fns = TREE_VALUE (conversions);
795145256Sjkoshy
796145256Sjkoshy      for (; fns; fns = OVL_NEXT (fns))
797145256Sjkoshy	{
798145256Sjkoshy	  tree f = OVL_CURRENT (fns);
799145256Sjkoshy	  tree t2 = TREE_TYPE (TREE_TYPE (f));
800145256Sjkoshy
801145256Sjkoshy	  cand = NULL;
802169069Sjkoshy
803145256Sjkoshy	  /* If this is a template function, try to get an exact
804224698Sattilio             match.  */
805224698Sattilio	  if (TREE_CODE (f) == TEMPLATE_DECL)
806224698Sattilio	    {
807283613Sjhb	      cand = add_template_candidate (&candidates,
808224698Sattilio					     f, s,
809168949Sjkoshy					     NULL_TREE,
810145256Sjkoshy					     arglist,
811145256Sjkoshy					     reference_type,
812145256Sjkoshy					     TYPE_BINFO (s),
813145256Sjkoshy					     TREE_PURPOSE (conversions),
814145256Sjkoshy					     LOOKUP_NORMAL,
815145256Sjkoshy					     DEDUCE_CONV);
816147191Sjkoshy
817145256Sjkoshy	      if (cand)
818147191Sjkoshy		{
819145256Sjkoshy		  /* Now, see if the conversion function really returns
820145256Sjkoshy		     an lvalue of the appropriate type.  From the
821145256Sjkoshy		     point of view of unification, simply returning an
822226986Sfabient		     rvalue of the right type is good enough.  */
823226986Sfabient		  f = cand->fn;
824226986Sfabient		  t2 = TREE_TYPE (TREE_TYPE (f));
825147708Sjkoshy		  if (TREE_CODE (t2) != REFERENCE_TYPE
826147708Sjkoshy		      || !reference_compatible_p (t, TREE_TYPE (t2)))
827227524Sobrien		    {
828227524Sobrien		      candidates = candidates->next;
829227524Sobrien		      cand = NULL;
830147708Sjkoshy		    }
831147191Sjkoshy		}
832145256Sjkoshy	    }
833145256Sjkoshy	  else if (TREE_CODE (t2) == REFERENCE_TYPE
834147708Sjkoshy		   && reference_compatible_p (t, TREE_TYPE (t2)))
835227524Sobrien	    cand = add_function_candidate (&candidates, f, s, arglist,
836227524Sobrien					   TYPE_BINFO (s),
837147708Sjkoshy					   TREE_PURPOSE (conversions),
838147708Sjkoshy					   LOOKUP_NORMAL);
839145256Sjkoshy
840145256Sjkoshy	  if (cand)
841157144Sjkoshy	    {
842157144Sjkoshy	      /* Build a standard conversion sequence indicating the
843157144Sjkoshy		 binding from the reference type returned by the
844157144Sjkoshy		 function to the desired REFERENCE_TYPE.  */
845157144Sjkoshy	      cand->second_conv
846157144Sjkoshy		= (direct_reference_binding
847157144Sjkoshy		   (reference_type,
848157144Sjkoshy		    build1 (IDENTITY_CONV,
849147708Sjkoshy			    TREE_TYPE (TREE_TYPE (TREE_TYPE (cand->fn))),
850174396Sjkoshy			    NULL_TREE)));
851227524Sobrien	      ICS_BAD_FLAG (cand->second_conv)
852227524Sobrien		|= ICS_BAD_FLAG (TREE_VEC_ELT (cand->convs, 0));
853147708Sjkoshy	    }
854147708Sjkoshy	}
855147708Sjkoshy      conversions = TREE_CHAIN (conversions);
856147708Sjkoshy    }
857147708Sjkoshy
858147708Sjkoshy  candidates = splice_viable (candidates, pedantic, &any_viable_p);
859169069Sjkoshy  /* If none of the conversion functions worked out, let our caller
860203790Sfabient     know.  */
861145256Sjkoshy  if (!any_viable_p)
862169069Sjkoshy    return NULL_TREE;
863147191Sjkoshy
864145256Sjkoshy  cand = tourney (candidates);
865145256Sjkoshy  if (!cand)
866203790Sfabient    return NULL_TREE;
867203790Sfabient
868203790Sfabient  /* Now that we know that this is the function we're going to use fix
869203790Sfabient     the dummy first argument.  */
870203790Sfabient  cand->args = tree_cons (NULL_TREE,
871203790Sfabient			  build_this (expr),
872203790Sfabient			  TREE_CHAIN (cand->args));
873203790Sfabient
874203790Sfabient  /* Build a user-defined conversion sequence representing the
875157144Sjkoshy     conversion.  */
876157144Sjkoshy  conv = build_conv (USER_CONV,
877157144Sjkoshy		     TREE_TYPE (TREE_TYPE (cand->fn)),
878157144Sjkoshy		     build1 (IDENTITY_CONV, TREE_TYPE (expr), expr));
879145256Sjkoshy  TREE_OPERAND (conv, 1) = build_zc_wrapper (cand);
880145256Sjkoshy
881145256Sjkoshy  /* Merge it with the standard conversion sequence from the
882227524Sobrien     conversion function's return type to the desired type.  */
883227524Sobrien  cand->second_conv = merge_conversion_sequences (conv, cand->second_conv);
884227524Sobrien
885145256Sjkoshy  if (cand->viable == -1)
886145256Sjkoshy    ICS_BAD_FLAG (conv) = 1;
887147191Sjkoshy
888145256Sjkoshy  return cand->second_conv;
889147191Sjkoshy}
890147191Sjkoshy
891147191Sjkoshy/* A reference of the indicated TYPE is being bound directly to the
892147708Sjkoshy   expression represented by the implicit conversion sequence CONV.
893145256Sjkoshy   Return a conversion sequence for this binding.  */
894145256Sjkoshy
895174396Sjkoshystatic tree
896174396Sjkoshydirect_reference_binding (tree type, tree conv)
897174396Sjkoshy{
898227524Sobrien  tree t;
899227524Sobrien
900227524Sobrien  my_friendly_assert (TREE_CODE (type) == REFERENCE_TYPE, 20030306);
901174396Sjkoshy  my_friendly_assert (TREE_CODE (TREE_TYPE (conv)) != REFERENCE_TYPE,
902174396Sjkoshy		      20030306);
903174396Sjkoshy
904174396Sjkoshy  t = TREE_TYPE (type);
905145256Sjkoshy
906145256Sjkoshy  /* [over.ics.rank]
907145256Sjkoshy
908145256Sjkoshy     When a parameter of reference type binds directly
909145256Sjkoshy     (_dcl.init.ref_) to an argument expression, the implicit
910145256Sjkoshy     conversion sequence is the identity conversion, unless the
911145256Sjkoshy     argument expression has a type that is a derived class of the
912145256Sjkoshy     parameter type, in which case the implicit conversion sequence is
913145256Sjkoshy     a derived-to-base Conversion.
914145256Sjkoshy
915210797Sfabient     If the parameter binds directly to the result of applying a
916210797Sfabient     conversion function to the argument expression, the implicit
917210797Sfabient     conversion sequence is a user-defined conversion sequence
918210797Sfabient     (_over.ics.user_), with the second standard conversion sequence
919210797Sfabient     either an identity conversion or, if the conversion function
920224698Sattilio     returns an entity of a type that is a derived class of the
921210797Sfabient     parameter type, a derived-to-base conversion.  */
922174396Sjkoshy  if (!same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (conv)))
923174396Sjkoshy    {
924147708Sjkoshy      /* Represent the derived-to-base conversion.  */
925147708Sjkoshy      conv = build_conv (BASE_CONV, t, conv);
926145256Sjkoshy      /* We will actually be binding to the base-class subobject in
927185322Sattilio	 the derived class, so we mark this conversion appropriately.
928203790Sfabient	 That way, convert_like knows not to generate a temporary.  */
929174396Sjkoshy      NEED_TEMPORARY_P (conv) = 0;
930174396Sjkoshy    }
931145256Sjkoshy  return build_conv (REF_BIND, type, conv);
932145256Sjkoshy}
933145256Sjkoshy
934145256Sjkoshy/* Returns the conversion path from type FROM to reference type TO for
935147708Sjkoshy   purposes of reference binding.  For lvalue binding, either pass a
936147708Sjkoshy   reference type to FROM or an lvalue expression to EXPR.  If the
937227524Sobrien   reference will be bound to a temporary, NEED_TEMPORARY_P is set for
938227524Sobrien   the conversion returned.  */
939147708Sjkoshy
940266209Sgnnstatic tree
941266209Sgnnreference_binding (tree rto, tree rfrom, tree expr, int flags)
942266209Sgnn{
943266209Sgnn  tree conv = NULL_TREE;
944266209Sgnn  tree to = TREE_TYPE (rto);
945266209Sgnn  tree from = rfrom;
946280793Svangyzen  bool related_p;
947203790Sfabient  bool compatible_p;
948262424Sadrian  cp_lvalue_kind lvalue_p = clk_none;
949262424Sadrian
950185322Sattilio  if (TREE_CODE (to) == FUNCTION_TYPE && expr && type_unknown_p (expr))
951185322Sattilio    {
952203790Sfabient      expr = instantiate_type (to, expr, tf_none);
953185322Sattilio      if (expr == error_mark_node)
954227524Sobrien	return NULL_TREE;
955227524Sobrien      from = TREE_TYPE (expr);
956185322Sattilio    }
957147708Sjkoshy
958147191Sjkoshy  if (TREE_CODE (from) == REFERENCE_TYPE)
959147708Sjkoshy    {
960147191Sjkoshy      /* Anything with reference type is an lvalue.  */
961169069Sjkoshy      lvalue_p = clk_ordinary;
962147191Sjkoshy      from = TREE_TYPE (from);
963169069Sjkoshy    }
964147191Sjkoshy  else if (expr)
965147191Sjkoshy    lvalue_p = real_lvalue_p (expr);
966227524Sobrien
967227524Sobrien  /* Figure out whether or not the types are reference-related and
968227524Sobrien     reference compatible.  We have do do this after stripping
969169069Sjkoshy     references from FROM.  */
970147708Sjkoshy  related_p = reference_related_p (to, from);
971145256Sjkoshy  compatible_p = reference_compatible_p (to, from);
972145256Sjkoshy
973147191Sjkoshy  if (lvalue_p && compatible_p)
974169069Sjkoshy    {
975147191Sjkoshy      /* [dcl.init.ref]
976227524Sobrien
977227524Sobrien	 If the initializer expression
978227524Sobrien
979147191Sjkoshy	 -- is an lvalue (but not an lvalue for a bit-field), and "cv1 T1"
980147191Sjkoshy	    is reference-compatible with "cv2 T2,"
981147191Sjkoshy
982169069Sjkoshy	 the reference is bound directly to the initializer expression
983227524Sobrien	 lvalue.  */
984227524Sobrien      conv = build1 (IDENTITY_CONV, from, expr);
985227524Sobrien      conv = direct_reference_binding (rto, conv);
986147191Sjkoshy      if ((lvalue_p & clk_bitfield) != 0
987147191Sjkoshy	  || ((lvalue_p & clk_packed) != 0 && !TYPE_PACKED (to)))
988169069Sjkoshy	/* For the purposes of overload resolution, we ignore the fact
989169069Sjkoshy	   this expression is a bitfield or packed field. (In particular,
990227524Sobrien	   [over.ics.ref] says specifically that a function with a
991227524Sobrien	   non-const reference parameter is viable even if the
992227524Sobrien	   argument is a bitfield.)
993147191Sjkoshy
994147191Sjkoshy	   However, when we actually call the function we must create
995147191Sjkoshy	   a temporary to which to bind the reference.  If the
996147191Sjkoshy	   reference is volatile, or isn't const, then we cannot make
997227524Sobrien	   a temporary, so we just issue an error when the conversion
998227524Sobrien	   actually occurs.  */
999227524Sobrien	NEED_TEMPORARY_P (conv) = 1;
1000147191Sjkoshy
1001174396Sjkoshy      return conv;
1002147191Sjkoshy    }
1003174396Sjkoshy  else if (CLASS_TYPE_P (from) && !(flags & LOOKUP_NO_CONVERSION))
1004174396Sjkoshy    {
1005227524Sobrien      /* [dcl.init.ref]
1006227524Sobrien
1007227524Sobrien	 If the initializer expression
1008147191Sjkoshy
1009147191Sjkoshy	 -- has a class type (i.e., T2 is a class type) can be
1010147191Sjkoshy	    implicitly converted to an lvalue of type "cv3 T3," where
1011147191Sjkoshy	    "cv1 T1" is reference-compatible with "cv3 T3".  (this
1012227524Sobrien	    conversion is selected by enumerating the applicable
1013227524Sobrien	    conversion functions (_over.match.ref_) and choosing the
1014227524Sobrien	    best one through overload resolution.  (_over.match_).
1015147191Sjkoshy
1016147191Sjkoshy        the reference is bound to the lvalue result of the conversion
1017147191Sjkoshy	in the second case.  */
1018147191Sjkoshy      conv = convert_class_to_reference (to, from, expr);
1019227524Sobrien      if (conv)
1020227524Sobrien	return conv;
1021227524Sobrien    }
1022147191Sjkoshy
1023203790Sfabient  /* From this point on, we conceptually need temporaries, even if we
1024174396Sjkoshy     elide them.  Only the cases above are "direct bindings".  */
1025147708Sjkoshy  if (flags & LOOKUP_NO_TEMP_BIND)
1026227524Sobrien    return NULL_TREE;
1027227524Sobrien
1028227524Sobrien  /* [over.ics.rank]
1029147708Sjkoshy
1030291016Sjtl     When a parameter of reference type is not bound directly to an
1031291016Sjtl     argument expression, the conversion sequence is the one required
1032291016Sjtl     to convert the argument expression to the underlying type of the
1033291016Sjtl     reference according to _over.best.ics_.  Conceptually, this
1034291016Sjtl     conversion sequence corresponds to copy-initializing a temporary
1035291016Sjtl     of the underlying type with the argument expression.  Any
1036291016Sjtl     difference in top-level cv-qualification is subsumed by the
1037147191Sjkoshy     initialization itself and does not constitute a conversion.  */
1038147708Sjkoshy
1039147708Sjkoshy  /* [dcl.init.ref]
1040147191Sjkoshy
1041227524Sobrien     Otherwise, the reference shall be to a non-volatile const type.  */
1042227524Sobrien  if (!CP_TYPE_CONST_NON_VOLATILE_P (to))
1043147191Sjkoshy    return NULL_TREE;
1044203790Sfabient
1045147708Sjkoshy  /* [dcl.init.ref]
1046174396Sjkoshy
1047157144Sjkoshy     If the initializer expression is an rvalue, with T2 a class type,
1048203790Sfabient     and "cv1 T1" is reference-compatible with "cv2 T2", the reference
1049147708Sjkoshy     is bound in one of the following ways:
1050174396Sjkoshy
1051147708Sjkoshy     -- The reference is bound to the object represented by the rvalue
1052174396Sjkoshy        or to a sub-object within that object.
1053174396Sjkoshy
1054147708Sjkoshy     -- ...
1055157144Sjkoshy
1056157144Sjkoshy     We use the first alternative.  The implicit conversion sequence
1057157144Sjkoshy     is supposed to be same as we would obtain by generating a
1058157144Sjkoshy     temporary.  Fortunately, if the types are reference compatible,
1059157144Sjkoshy     then this is either an identity conversion or the derived-to-base
1060157144Sjkoshy     conversion, just as for direct binding.  */
1061147708Sjkoshy  if (CLASS_TYPE_P (from) && compatible_p)
1062147708Sjkoshy    {
1063147708Sjkoshy      conv = build1 (IDENTITY_CONV, from, expr);
1064147708Sjkoshy      conv = direct_reference_binding (rto, conv);
1065147708Sjkoshy      if (!(flags & LOOKUP_CONSTRUCTOR_CALLABLE))
1066147708Sjkoshy	CHECK_COPY_CONSTRUCTOR_P (TREE_OPERAND (conv, 0)) = 1;
1067147708Sjkoshy      return conv;
1068168927Sjkoshy    }
1069227524Sobrien
1070227524Sobrien  /* [dcl.init.ref]
1071227524Sobrien
1072147708Sjkoshy     Otherwise, a temporary of type "cv1 T1" is created and
1073157144Sjkoshy     initialized from the initializer expression using the rules for a
1074282643Sjhb     non-reference copy initialization.  If T1 is reference-related to
1075282643Sjhb     T2, cv1 must be the same cv-qualification as, or greater
1076282643Sjhb     cv-qualification than, cv2; otherwise, the program is ill-formed.  */
1077157144Sjkoshy  if (related_p && !at_least_as_qualified_p (to, from))
1078282643Sjhb    return NULL_TREE;
1079282643Sjhb
1080282643Sjhb  conv = implicit_conversion (to, from, expr, flags);
1081282643Sjhb  if (!conv)
1082282643Sjhb    return NULL_TREE;
1083282643Sjhb
1084282643Sjhb  conv = build_conv (REF_BIND, rto, conv);
1085282643Sjhb  /* This reference binding, unlike those above, requires the
1086282643Sjhb     creation of a temporary.  */
1087282643Sjhb  NEED_TEMPORARY_P (conv) = 1;
1088282643Sjhb
1089282643Sjhb  return conv;
1090282643Sjhb}
1091282643Sjhb
1092157144Sjkoshy/* Returns the implicit conversion sequence (see [over.ics]) from type FROM
1093282643Sjhb   to type TO.  The optional expression EXPR may affect the conversion.
1094157144Sjkoshy   FLAGS are the usual overloading flags.  Only LOOKUP_NO_CONVERSION is
1095282643Sjhb   significant.  */
1096282643Sjhb
1097282643Sjhbstatic tree
1098157144Sjkoshyimplicit_conversion (tree to, tree from, tree expr, int flags)
1099157144Sjkoshy{
1100168949Sjkoshy  tree conv;
1101174396Sjkoshy
1102174396Sjkoshy  if (from == error_mark_node || to == error_mark_node
1103174396Sjkoshy      || expr == error_mark_node)
1104174396Sjkoshy    return NULL_TREE;
1105174396Sjkoshy
1106174396Sjkoshy  if (TREE_CODE (to) == REFERENCE_TYPE)
1107174396Sjkoshy    conv = reference_binding (to, from, expr, flags);
1108174396Sjkoshy  else
1109174396Sjkoshy    conv = standard_conversion (to, from, expr, flags);
1110227524Sobrien
1111227524Sobrien  if (conv)
1112227524Sobrien    return conv;
1113174396Sjkoshy
1114174396Sjkoshy  if (expr != NULL_TREE
1115203790Sfabient      && (IS_AGGR_TYPE (from)
1116185322Sattilio	  || IS_AGGR_TYPE (to))
1117185322Sattilio      && (flags & LOOKUP_NO_CONVERSION) == 0)
1118185322Sattilio    {
1119185322Sattilio      struct z_candidate *cand;
1120185322Sattilio
1121174396Sjkoshy      cand = build_user_type_conversion_1
1122210794Sfabient	(to, expr, LOOKUP_ONLYCONVERTING);
1123210794Sfabient      if (cand)
1124210794Sfabient	conv = cand->second_conv;
1125210794Sfabient
1126227524Sobrien      /* We used to try to bind a reference to a temporary here, but that
1127227524Sobrien	 is now handled by the recursive call to this function at the end
1128210794Sfabient	 of reference_binding.  */
1129210794Sfabient      return conv;
1130227524Sobrien    }
1131227524Sobrien
1132227524Sobrien  return NULL_TREE;
1133210794Sfabient}
1134210794Sfabient
1135210794Sfabient/* Add a new entry to the list of candidates.  Used by the add_*_candidate
1136210794Sfabient   functions.  */
1137210794Sfabient
1138210794Sfabientstatic struct z_candidate *
1139210794Sfabientadd_candidate (struct z_candidate **candidates,
1140147708Sjkoshy	       tree fn, tree args, tree convs, tree access_path,
1141147708Sjkoshy	       tree conversion_path, int viable)
1142147708Sjkoshy{
1143174396Sjkoshy  struct z_candidate *cand = ggc_alloc_cleared (sizeof (struct z_candidate));
1144147708Sjkoshy
1145174396Sjkoshy  cand->fn = fn;
1146147708Sjkoshy  cand->args = args;
1147147708Sjkoshy  cand->convs = convs;
1148203790Sfabient  cand->access_path = access_path;
1149210794Sfabient  cand->conversion_path = conversion_path;
1150157406Sjkoshy  cand->viable = viable;
1151210794Sfabient  cand->next = *candidates;
1152147708Sjkoshy  *candidates = cand;
1153210794Sfabient
1154210794Sfabient  return cand;
1155210794Sfabient}
1156210794Sfabient
1157210794Sfabient/* Create an overload candidate for the function or method FN called with
1158210794Sfabient   the argument list ARGLIST and add it to CANDIDATES.  FLAGS is passed on
1159147191Sjkoshy   to implicit_conversion.
1160145256Sjkoshy
1161147708Sjkoshy   CTYPE, if non-NULL, is the type we want to pretend this function
1162147708Sjkoshy   comes from for purposes of overload resolution.  */
1163147708Sjkoshy
1164147708Sjkoshystatic struct z_candidate *
1165157406Sjkoshyadd_function_candidate (struct z_candidate **candidates,
1166157406Sjkoshy			tree fn, tree ctype, tree arglist,
1167157406Sjkoshy			tree access_path, tree conversion_path,
1168157406Sjkoshy			int flags)
1169147708Sjkoshy{
1170147708Sjkoshy  tree parmlist = TYPE_ARG_TYPES (TREE_TYPE (fn));
1171147708Sjkoshy  int i, len;
1172147708Sjkoshy  tree convs;
1173147708Sjkoshy  tree parmnode, argnode;
1174147708Sjkoshy  tree orig_arglist;
1175147708Sjkoshy  int viable = 1;
1176147708Sjkoshy
1177147708Sjkoshy  /* Built-in functions that haven't been declared don't really
1178147708Sjkoshy     exist.  */
1179147708Sjkoshy  if (DECL_ANTICIPATED (fn))
1180147708Sjkoshy    return NULL;
1181147708Sjkoshy
1182147708Sjkoshy  /* The `this', `in_chrg' and VTT arguments to constructors are not
1183147708Sjkoshy     considered in overload resolution.  */
1184147708Sjkoshy  if (DECL_CONSTRUCTOR_P (fn))
1185147708Sjkoshy    {
1186147708Sjkoshy      parmlist = skip_artificial_parms_for (fn, parmlist);
1187203790Sfabient      orig_arglist = arglist;
1188203790Sfabient      arglist = skip_artificial_parms_for (fn, arglist);
1189203790Sfabient    }
1190147708Sjkoshy  else
1191147708Sjkoshy    orig_arglist = arglist;
1192147708Sjkoshy
1193147708Sjkoshy  len = list_length (arglist);
1194147708Sjkoshy  convs = make_tree_vec (len);
1195147708Sjkoshy
1196147708Sjkoshy  /* 13.3.2 - Viable functions [over.match.viable]
1197151542Sjkoshy     First, to be a viable function, a candidate function shall have enough
1198151542Sjkoshy     parameters to agree in number with the arguments in the list.
1199151542Sjkoshy
1200151542Sjkoshy     We need to check this first; otherwise, checking the ICSes might cause
1201147708Sjkoshy     us to produce an ill-formed template instantiation.  */
1202210794Sfabient
1203145256Sjkoshy  parmnode = parmlist;
1204145256Sjkoshy  for (i = 0; i < len; ++i)
1205145256Sjkoshy    {
1206169069Sjkoshy      if (parmnode == NULL_TREE || parmnode == void_list_node)
1207227524Sobrien	break;
1208147191Sjkoshy      parmnode = TREE_CHAIN (parmnode);
1209227524Sobrien    }
1210227524Sobrien
1211227524Sobrien  if (i < len && parmnode)
1212227524Sobrien    viable = 0;
1213145256Sjkoshy
1214227524Sobrien  /* Make sure there are default args for the rest of the parms.  */
1215227524Sobrien  else if (!sufficient_parms_p (parmnode))
1216227524Sobrien    viable = 0;
1217227524Sobrien
1218227524Sobrien  if (! viable)
1219147708Sjkoshy    goto out;
1220147708Sjkoshy
1221145256Sjkoshy  /* Second, for F to be a viable function, there shall exist for each
1222169069Sjkoshy     argument an implicit conversion sequence that converts that argument
1223145774Sjkoshy     to the corresponding parameter of F.  */
1224145774Sjkoshy
1225145774Sjkoshy  parmnode = parmlist;
1226145256Sjkoshy  argnode = arglist;
1227145774Sjkoshy
1228168949Sjkoshy  for (i = 0; i < len; ++i)
1229145774Sjkoshy    {
1230145256Sjkoshy      tree arg = TREE_VALUE (argnode);
1231168949Sjkoshy      tree argtype = lvalue_type (arg);
1232168949Sjkoshy      tree t;
1233168949Sjkoshy      int is_this;
1234145774Sjkoshy
1235145256Sjkoshy      if (parmnode == void_list_node)
1236145774Sjkoshy	break;
1237145256Sjkoshy
1238145774Sjkoshy      is_this = (i == 0 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
1239145774Sjkoshy		 && ! DECL_CONSTRUCTOR_P (fn));
1240145774Sjkoshy
1241145256Sjkoshy      if (parmnode)
1242145256Sjkoshy	{
1243145256Sjkoshy	  tree parmtype = TREE_VALUE (parmnode);
1244145256Sjkoshy
1245145256Sjkoshy	  /* The type of the implicit object parameter ('this') for
1246145256Sjkoshy	     overload resolution is not always the same as for the
1247145256Sjkoshy	     function itself; conversion functions are considered to
1248145256Sjkoshy	     be members of the class being converted, and functions
1249147708Sjkoshy	     introduced by a using-declaration are considered to be
1250145256Sjkoshy	     members of the class that uses them.
1251147708Sjkoshy
1252145256Sjkoshy	     Since build_over_call ignores the ICS for the `this'
1253145256Sjkoshy	     parameter, we can just change the parm type.  */
1254145256Sjkoshy	  if (ctype && is_this)
1255203790Sfabient	    {
1256145256Sjkoshy	      parmtype
1257145256Sjkoshy		= build_qualified_type (ctype,
1258145256Sjkoshy					TYPE_QUALS (TREE_TYPE (parmtype)));
1259145256Sjkoshy	      parmtype = build_pointer_type (parmtype);
1260227524Sobrien	    }
1261227524Sobrien
1262203790Sfabient	  t = implicit_conversion (parmtype, argtype, arg, flags);
1263203790Sfabient	}
1264145256Sjkoshy      else
1265145256Sjkoshy	{
1266203790Sfabient	  t = build1 (IDENTITY_CONV, argtype, arg);
1267203790Sfabient	  ICS_ELLIPSIS_FLAG (t) = 1;
1268203790Sfabient	}
1269203790Sfabient
1270203790Sfabient      if (t && is_this)
1271203790Sfabient	ICS_THIS_FLAG (t) = 1;
1272203790Sfabient
1273203790Sfabient      TREE_VEC_ELT (convs, i) = t;
1274203790Sfabient      if (! t)
1275145256Sjkoshy	{
1276145256Sjkoshy	  viable = 0;
1277145256Sjkoshy	  break;
1278145256Sjkoshy	}
1279147191Sjkoshy
1280147191Sjkoshy      if (ICS_BAD_FLAG (t))
1281147191Sjkoshy	viable = -1;
1282145256Sjkoshy
1283147191Sjkoshy      if (parmnode)
1284147191Sjkoshy	parmnode = TREE_CHAIN (parmnode);
1285147191Sjkoshy      argnode = TREE_CHAIN (argnode);
1286147191Sjkoshy    }
1287147191Sjkoshy
1288147191Sjkoshy out:
1289147191Sjkoshy  return add_candidate (candidates, fn, orig_arglist, convs, access_path,
1290147191Sjkoshy			conversion_path, viable);
1291147191Sjkoshy}
1292145256Sjkoshy
1293203790Sfabient/* Create an overload candidate for the conversion function FN which will
1294203790Sfabient   be invoked for expression OBJ, producing a pointer-to-function which
1295203790Sfabient   will in turn be called with the argument list ARGLIST, and add it to
1296203790Sfabient   CANDIDATES.  FLAGS is passed on to implicit_conversion.
1297203790Sfabient
1298203790Sfabient   Actually, we don't really care about FN; we care about the type it
1299203790Sfabient   converts to.  There may be multiple conversion functions that will
1300145256Sjkoshy   convert to that type, and we rely on build_user_type_conversion_1 to
1301145256Sjkoshy   choose the best one; so when we create our candidate, we record the type
1302145256Sjkoshy   instead of the function.  */
1303145256Sjkoshy
1304227524Sobrienstatic struct z_candidate *
1305227524Sobrienadd_conv_candidate (struct z_candidate **candidates, tree fn, tree obj,
1306145256Sjkoshy                    tree arglist, tree access_path, tree conversion_path)
1307145256Sjkoshy{
1308266209Sgnn  tree totype = TREE_TYPE (TREE_TYPE (fn));
1309266209Sgnn  int i, len, viable, flags;
1310266209Sgnn  tree parmlist, convs, parmnode, argnode;
1311266209Sgnn
1312266209Sgnn  for (parmlist = totype; TREE_CODE (parmlist) != FUNCTION_TYPE; )
1313266209Sgnn    parmlist = TREE_TYPE (parmlist);
1314266209Sgnn  parmlist = TYPE_ARG_TYPES (parmlist);
1315266209Sgnn
1316266209Sgnn  len = list_length (arglist) + 1;
1317266209Sgnn  convs = make_tree_vec (len);
1318266209Sgnn  parmnode = parmlist;
1319266209Sgnn  argnode = arglist;
1320266209Sgnn  viable = 1;
1321266209Sgnn  flags = LOOKUP_NORMAL;
1322145256Sjkoshy
1323168949Sjkoshy  /* Don't bother looking up the same type twice.  */
1324203790Sfabient  if (*candidates && (*candidates)->fn == totype)
1325145256Sjkoshy    return NULL;
1326151542Sjkoshy
1327151542Sjkoshy  for (i = 0; i < len; ++i)
1328151542Sjkoshy    {
1329168949Sjkoshy      tree arg = i == 0 ? obj : TREE_VALUE (argnode);
1330180148Sjkoshy      tree argtype = lvalue_type (arg);
1331169069Sjkoshy      tree t;
1332227524Sobrien
1333227524Sobrien      if (i == 0)
1334183672Sjkoshy	t = implicit_conversion (totype, argtype, arg, flags);
1335203790Sfabient      else if (parmnode == void_list_node)
1336168949Sjkoshy	break;
1337169069Sjkoshy      else if (parmnode)
1338169069Sjkoshy	t = implicit_conversion (TREE_VALUE (parmnode), argtype, arg, flags);
1339169069Sjkoshy      else
1340169069Sjkoshy	{
1341169069Sjkoshy	  t = build1 (IDENTITY_CONV, argtype, arg);
1342169069Sjkoshy	  ICS_ELLIPSIS_FLAG (t) = 1;
1343145256Sjkoshy	}
1344203790Sfabient
1345145256Sjkoshy      TREE_VEC_ELT (convs, i) = t;
1346145256Sjkoshy      if (! t)
1347147708Sjkoshy	break;
1348168949Sjkoshy
1349145256Sjkoshy      if (ICS_BAD_FLAG (t))
1350210794Sfabient	viable = -1;
1351210794Sfabient
1352147708Sjkoshy      if (i == 0)
1353145256Sjkoshy	continue;
1354145256Sjkoshy
1355145256Sjkoshy      if (parmnode)
1356145256Sjkoshy	parmnode = TREE_CHAIN (parmnode);
1357145256Sjkoshy      argnode = TREE_CHAIN (argnode);
1358145256Sjkoshy    }
1359145256Sjkoshy
1360145256Sjkoshy  if (i < len)
1361145256Sjkoshy    viable = 0;
1362203790Sfabient
1363203790Sfabient  if (!sufficient_parms_p (parmnode))
1364203790Sfabient    viable = 0;
1365203790Sfabient
1366203790Sfabient  return add_candidate (candidates, totype, arglist, convs, access_path,
1367203790Sfabient			conversion_path, viable);
1368203790Sfabient}
1369203790Sfabient
1370203790Sfabientstatic void
1371203790Sfabientbuild_builtin_candidate (struct z_candidate **candidates, tree fnname,
1372203790Sfabient                         tree type1, tree type2, tree *args, tree *argtypes,
1373203790Sfabient                         int flags)
1374203790Sfabient{
1375203790Sfabient  tree t, convs;
1376203790Sfabient  int viable = 1, i;
1377203790Sfabient  tree types[2];
1378203790Sfabient
1379203790Sfabient  types[0] = type1;
1380203790Sfabient  types[1] = type2;
1381203790Sfabient
1382203790Sfabient  convs = make_tree_vec (args[2] ? 3 : (args[1] ? 2 : 1));
1383203790Sfabient
1384203790Sfabient  for (i = 0; i < 2; ++i)
1385203790Sfabient    {
1386203790Sfabient      if (! args[i])
1387203790Sfabient	break;
1388204783Sfabient
1389227524Sobrien      t = implicit_conversion (types[i], argtypes[i], args[i], flags);
1390227524Sobrien      if (! t)
1391204783Sfabient	{
1392203790Sfabient	  viable = 0;
1393203790Sfabient	  /* We need something for printing the candidate.  */
1394203790Sfabient	  t = build1 (IDENTITY_CONV, types[i], NULL_TREE);
1395203790Sfabient	}
1396203790Sfabient      else if (ICS_BAD_FLAG (t))
1397145256Sjkoshy	viable = 0;
1398266209Sgnn      TREE_VEC_ELT (convs, i) = t;
1399145256Sjkoshy    }
1400147191Sjkoshy
1401226514Sfabient  /* For COND_EXPR we rearranged the arguments; undo that now.  */
1402145256Sjkoshy  if (args[2])
1403145256Sjkoshy    {
1404145256Sjkoshy      TREE_VEC_ELT (convs, 2) = TREE_VEC_ELT (convs, 1);
1405145256Sjkoshy      TREE_VEC_ELT (convs, 1) = TREE_VEC_ELT (convs, 0);
1406145256Sjkoshy      t = implicit_conversion (boolean_type_node, argtypes[2], args[2], flags);
1407145256Sjkoshy      if (t)
1408145256Sjkoshy	TREE_VEC_ELT (convs, 0) = t;
1409145256Sjkoshy      else
1410145256Sjkoshy	viable = 0;
1411145256Sjkoshy    }
1412145256Sjkoshy
1413145256Sjkoshy  add_candidate (candidates, fnname, /*args=*/NULL_TREE, convs,
1414147191Sjkoshy		 /*access_path=*/NULL_TREE,
1415210794Sfabient		 /*conversion_path=*/NULL_TREE,
1416148688Sjkoshy		 viable);
1417147191Sjkoshy}
1418145256Sjkoshy
1419147191Sjkoshystatic bool
1420210794Sfabientis_complete (tree t)
1421210794Sfabient{
1422203790Sfabient  return COMPLETE_TYPE_P (complete_type (t));
1423203790Sfabient}
1424226514Sfabient
1425226514Sfabient/* Returns nonzero if TYPE is a promoted arithmetic type.  */
1426203790Sfabient
1427226514Sfabientstatic bool
1428145256Sjkoshypromoted_arithmetic_type_p (tree type)
1429145256Sjkoshy{
1430145256Sjkoshy  /* [over.built]
1431147191Sjkoshy
1432147191Sjkoshy     In this section, the term promoted integral type is used to refer
1433147191Sjkoshy     to those integral types which are preserved by integral promotion
1434147191Sjkoshy     (including e.g.  int and long but excluding e.g.  char).
1435147191Sjkoshy     Similarly, the term promoted arithmetic type refers to promoted
1436147191Sjkoshy     integral types plus floating types.  */
1437147191Sjkoshy  return ((INTEGRAL_TYPE_P (type)
1438147191Sjkoshy	   && same_type_p (type_promotes_to (type), type))
1439147191Sjkoshy	  || TREE_CODE (type) == REAL_TYPE);
1440147191Sjkoshy}
1441147191Sjkoshy
1442147191Sjkoshy/* Create any builtin operator overload candidates for the operator in
1443147191Sjkoshy   question given the converted operand types TYPE1 and TYPE2.  The other
1444147191Sjkoshy   args are passed through from add_builtin_candidates to
1445147191Sjkoshy   build_builtin_candidate.
1446210794Sfabient
1447147191Sjkoshy   TYPE1 and TYPE2 may not be permissible, and we must filter them.
1448147191Sjkoshy   If CODE is requires candidates operands of the same type of the kind
1449147708Sjkoshy   of which TYPE1 and TYPE2 are, we add both candidates
1450147708Sjkoshy   CODE (TYPE1, TYPE1) and CODE (TYPE2, TYPE2).  */
1451203790Sfabient
1452210794Sfabientstatic void
1453145256Sjkoshyadd_builtin_candidate (struct z_candidate **candidates, enum tree_code code,
1454147708Sjkoshy                       enum tree_code code2, tree fnname, tree type1,
1455145256Sjkoshy                       tree type2, tree *args, tree *argtypes, int flags)
1456227524Sobrien{
1457227524Sobrien  switch (code)
1458145256Sjkoshy    {
1459203790Sfabient    case POSTINCREMENT_EXPR:
1460145256Sjkoshy    case POSTDECREMENT_EXPR:
1461145256Sjkoshy      args[1] = integer_zero_node;
1462145256Sjkoshy      type2 = integer_type_node;
1463145256Sjkoshy      break;
1464147191Sjkoshy    default:
1465266209Sgnn      break;
1466266209Sgnn    }
1467266209Sgnn
1468266209Sgnn  switch (code)
1469266209Sgnn    {
1470266209Sgnn
1471266209Sgnn/* 4 For every pair T, VQ), where T is an arithmetic or  enumeration  type,
1472226514Sfabient     and  VQ  is  either  volatile or empty, there exist candidate operator
1473226986Sfabient     functions of the form
1474226514Sfabient	     VQ T&   operator++(VQ T&);
1475147191Sjkoshy	     T       operator++(VQ T&, int);
1476147191Sjkoshy   5 For every pair T, VQ), where T is an enumeration type or an arithmetic
1477147191Sjkoshy     type  other than bool, and VQ is either volatile or empty, there exist
1478145256Sjkoshy     candidate operator functions of the form
1479145256Sjkoshy	     VQ T&   operator--(VQ T&);
1480226514Sfabient	     T       operator--(VQ T&, int);
1481203790Sfabient   6 For every pair T, VQ), where T is  a  cv-qualified  or  cv-unqualified
1482203790Sfabient     complete  object type, and VQ is either volatile or empty, there exist
1483227524Sobrien     candidate operator functions of the form
1484227524Sobrien	     T*VQ&   operator++(T*VQ&);
1485203790Sfabient	     T*VQ&   operator--(T*VQ&);
1486203790Sfabient	     T*      operator++(T*VQ&, int);
1487203790Sfabient	     T*      operator--(T*VQ&, int);  */
1488203790Sfabient
1489203790Sfabient    case POSTDECREMENT_EXPR:
1490147191Sjkoshy    case PREDECREMENT_EXPR:
1491147191Sjkoshy      if (TREE_CODE (type1) == BOOLEAN_TYPE)
1492145256Sjkoshy	return;
1493147191Sjkoshy    case POSTINCREMENT_EXPR:
1494147191Sjkoshy    case PREINCREMENT_EXPR:
1495203790Sfabient      if (ARITHMETIC_TYPE_P (type1) || TYPE_PTROB_P (type1))
1496203790Sfabient	{
1497203790Sfabient	  type1 = build_reference_type (type1);
1498203790Sfabient	  break;
1499203790Sfabient	}
1500147191Sjkoshy      return;
1501147708Sjkoshy
1502226514Sfabient/* 7 For every cv-qualified or cv-unqualified complete object type T, there
1503147191Sjkoshy     exist candidate operator functions of the form
1504203790Sfabient
1505145256Sjkoshy	     T&      operator*(T*);
1506157144Sjkoshy
1507157144Sjkoshy   8 For every function type T, there exist candidate operator functions of
1508151542Sjkoshy     the form
1509151542Sjkoshy	     T&      operator*(T*);  */
1510151542Sjkoshy
1511227524Sobrien    case INDIRECT_REF:
1512227524Sobrien      if (TREE_CODE (type1) == POINTER_TYPE
1513157144Sjkoshy	  && (TYPE_PTROB_P (type1)
1514157144Sjkoshy	      || TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE))
1515290929Sjtl	break;
1516290929Sjtl      return;
1517290929Sjtl
1518290929Sjtl/* 9 For every type T, there exist candidate operator functions of the form
1519290929Sjtl	     T*      operator+(T*);
1520290929Sjtl
1521290929Sjtl   10For  every  promoted arithmetic type T, there exist candidate operator
1522227524Sobrien     functions of the form
1523151542Sjkoshy	     T       operator+(T);
1524157144Sjkoshy	     T       operator-(T);  */
1525157144Sjkoshy
1526290929Sjtl    case CONVERT_EXPR: /* unary + */
1527290929Sjtl      if (TREE_CODE (type1) == POINTER_TYPE)
1528290929Sjtl	break;
1529290929Sjtl    case NEGATE_EXPR:
1530290929Sjtl      if (ARITHMETIC_TYPE_P (type1))
1531290929Sjtl	break;
1532290929Sjtl      return;
1533227524Sobrien
1534151542Sjkoshy/* 11For every promoted integral type T,  there  exist  candidate  operator
1535151542Sjkoshy     functions of the form
1536151542Sjkoshy	     T       operator~(T);  */
1537145256Sjkoshy
1538    case BIT_NOT_EXPR:
1539      if (INTEGRAL_TYPE_P (type1))
1540	break;
1541      return;
1542
1543/* 12For every quintuple C1, C2, T, CV1, CV2), where C2 is a class type, C1
1544     is the same type as C2 or is a derived class of C2, T  is  a  complete
1545     object type or a function type, and CV1 and CV2 are cv-qualifier-seqs,
1546     there exist candidate operator functions of the form
1547	     CV12 T& operator->*(CV1 C1*, CV2 T C2::*);
1548     where CV12 is the union of CV1 and CV2.  */
1549
1550    case MEMBER_REF:
1551      if (TREE_CODE (type1) == POINTER_TYPE
1552	  && TYPE_PTR_TO_MEMBER_P (type2))
1553	{
1554	  tree c1 = TREE_TYPE (type1);
1555	  tree c2 = TYPE_PTRMEM_CLASS_TYPE (type2);
1556
1557	  if (IS_AGGR_TYPE (c1) && DERIVED_FROM_P (c2, c1)
1558	      && (TYPE_PTRMEMFUNC_P (type2)
1559		  || is_complete (TREE_TYPE (TREE_TYPE (type2)))))
1560	    break;
1561	}
1562      return;
1563
1564/* 13For every pair of promoted arithmetic types L and R, there exist  can-
1565     didate operator functions of the form
1566	     LR      operator*(L, R);
1567	     LR      operator/(L, R);
1568	     LR      operator+(L, R);
1569	     LR      operator-(L, R);
1570	     bool    operator<(L, R);
1571	     bool    operator>(L, R);
1572	     bool    operator<=(L, R);
1573	     bool    operator>=(L, R);
1574	     bool    operator==(L, R);
1575	     bool    operator!=(L, R);
1576     where  LR  is  the  result of the usual arithmetic conversions between
1577     types L and R.
1578
1579   14For every pair of types T and I, where T  is  a  cv-qualified  or  cv-
1580     unqualified  complete  object  type and I is a promoted integral type,
1581     there exist candidate operator functions of the form
1582	     T*      operator+(T*, I);
1583	     T&      operator[](T*, I);
1584	     T*      operator-(T*, I);
1585	     T*      operator+(I, T*);
1586	     T&      operator[](I, T*);
1587
1588   15For every T, where T is a pointer to complete object type, there exist
1589     candidate operator functions of the form112)
1590	     ptrdiff_t operator-(T, T);
1591
1592   16For every pointer or enumeration type T, there exist candidate operator
1593     functions of the form
1594	     bool    operator<(T, T);
1595	     bool    operator>(T, T);
1596	     bool    operator<=(T, T);
1597	     bool    operator>=(T, T);
1598	     bool    operator==(T, T);
1599	     bool    operator!=(T, T);
1600
1601   17For every pointer to member type T,  there  exist  candidate  operator
1602     functions of the form
1603	     bool    operator==(T, T);
1604	     bool    operator!=(T, T);  */
1605
1606    case MINUS_EXPR:
1607      if (TYPE_PTROB_P (type1) && TYPE_PTROB_P (type2))
1608	break;
1609      if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2))
1610	{
1611	  type2 = ptrdiff_type_node;
1612	  break;
1613	}
1614    case MULT_EXPR:
1615    case TRUNC_DIV_EXPR:
1616      if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1617	break;
1618      return;
1619
1620    case EQ_EXPR:
1621    case NE_EXPR:
1622      if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2))
1623	  || (TYPE_PTRMEM_P (type1) && TYPE_PTRMEM_P (type2)))
1624	break;
1625      if (TYPE_PTR_TO_MEMBER_P (type1) && null_ptr_cst_p (args[1]))
1626	{
1627	  type2 = type1;
1628	  break;
1629	}
1630      if (TYPE_PTR_TO_MEMBER_P (type2) && null_ptr_cst_p (args[0]))
1631	{
1632	  type1 = type2;
1633	  break;
1634	}
1635      /* Fall through.  */
1636    case LT_EXPR:
1637    case GT_EXPR:
1638    case LE_EXPR:
1639    case GE_EXPR:
1640    case MAX_EXPR:
1641    case MIN_EXPR:
1642      if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1643        break;
1644      if (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
1645	break;
1646      if (TREE_CODE (type1) == ENUMERAL_TYPE && TREE_CODE (type2) == ENUMERAL_TYPE)
1647        break;
1648      if (TYPE_PTR_P (type1) && null_ptr_cst_p (args[1]))
1649	{
1650	  type2 = type1;
1651	  break;
1652	}
1653      if (null_ptr_cst_p (args[0]) && TYPE_PTR_P (type2))
1654	{
1655	  type1 = type2;
1656	  break;
1657	}
1658      return;
1659
1660    case PLUS_EXPR:
1661      if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1662	break;
1663    case ARRAY_REF:
1664      if (INTEGRAL_TYPE_P (type1) && TYPE_PTROB_P (type2))
1665	{
1666	  type1 = ptrdiff_type_node;
1667	  break;
1668	}
1669      if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2))
1670	{
1671	  type2 = ptrdiff_type_node;
1672	  break;
1673	}
1674      return;
1675
1676/* 18For  every pair of promoted integral types L and R, there exist candi-
1677     date operator functions of the form
1678	     LR      operator%(L, R);
1679	     LR      operator&(L, R);
1680	     LR      operator^(L, R);
1681	     LR      operator|(L, R);
1682	     L       operator<<(L, R);
1683	     L       operator>>(L, R);
1684     where LR is the result of the  usual  arithmetic  conversions  between
1685     types L and R.  */
1686
1687    case TRUNC_MOD_EXPR:
1688    case BIT_AND_EXPR:
1689    case BIT_IOR_EXPR:
1690    case BIT_XOR_EXPR:
1691    case LSHIFT_EXPR:
1692    case RSHIFT_EXPR:
1693      if (INTEGRAL_TYPE_P (type1) && INTEGRAL_TYPE_P (type2))
1694	break;
1695      return;
1696
1697/* 19For  every  triple  L, VQ, R), where L is an arithmetic or enumeration
1698     type, VQ is either volatile or empty, and R is a  promoted  arithmetic
1699     type, there exist candidate operator functions of the form
1700	     VQ L&   operator=(VQ L&, R);
1701	     VQ L&   operator*=(VQ L&, R);
1702	     VQ L&   operator/=(VQ L&, R);
1703	     VQ L&   operator+=(VQ L&, R);
1704	     VQ L&   operator-=(VQ L&, R);
1705
1706   20For  every  pair T, VQ), where T is any type and VQ is either volatile
1707     or empty, there exist candidate operator functions of the form
1708	     T*VQ&   operator=(T*VQ&, T*);
1709
1710   21For every pair T, VQ), where T is a pointer to member type and  VQ  is
1711     either  volatile or empty, there exist candidate operator functions of
1712     the form
1713	     VQ T&   operator=(VQ T&, T);
1714
1715   22For every triple  T,  VQ,  I),  where  T  is  a  cv-qualified  or  cv-
1716     unqualified  complete object type, VQ is either volatile or empty, and
1717     I is a promoted integral type, there exist  candidate  operator  func-
1718     tions of the form
1719	     T*VQ&   operator+=(T*VQ&, I);
1720	     T*VQ&   operator-=(T*VQ&, I);
1721
1722   23For  every  triple  L,  VQ,  R), where L is an integral or enumeration
1723     type, VQ is either volatile or empty, and R  is  a  promoted  integral
1724     type, there exist candidate operator functions of the form
1725
1726	     VQ L&   operator%=(VQ L&, R);
1727	     VQ L&   operator<<=(VQ L&, R);
1728	     VQ L&   operator>>=(VQ L&, R);
1729	     VQ L&   operator&=(VQ L&, R);
1730	     VQ L&   operator^=(VQ L&, R);
1731	     VQ L&   operator|=(VQ L&, R);  */
1732
1733    case MODIFY_EXPR:
1734      switch (code2)
1735	{
1736	case PLUS_EXPR:
1737	case MINUS_EXPR:
1738	  if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2))
1739	    {
1740	      type2 = ptrdiff_type_node;
1741	      break;
1742	    }
1743	case MULT_EXPR:
1744	case TRUNC_DIV_EXPR:
1745	  if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1746	    break;
1747	  return;
1748
1749	case TRUNC_MOD_EXPR:
1750	case BIT_AND_EXPR:
1751	case BIT_IOR_EXPR:
1752	case BIT_XOR_EXPR:
1753	case LSHIFT_EXPR:
1754	case RSHIFT_EXPR:
1755	  if (INTEGRAL_TYPE_P (type1) && INTEGRAL_TYPE_P (type2))
1756	    break;
1757	  return;
1758
1759	case NOP_EXPR:
1760	  if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1761	    break;
1762	  if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2))
1763	      || (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
1764	      || (TYPE_PTRMEM_P (type1) && TYPE_PTRMEM_P (type2))
1765	      || ((TYPE_PTRMEMFUNC_P (type1)
1766		   || TREE_CODE (type1) == POINTER_TYPE)
1767		  && null_ptr_cst_p (args[1])))
1768	    {
1769	      type2 = type1;
1770	      break;
1771	    }
1772	  return;
1773
1774	default:
1775	  abort ();
1776	}
1777      type1 = build_reference_type (type1);
1778      break;
1779
1780    case COND_EXPR:
1781      /* [over.built]
1782
1783	 For every pair of promoted arithmetic types L and R, there
1784	 exist candidate operator functions of the form
1785
1786	 LR operator?(bool, L, R);
1787
1788	 where LR is the result of the usual arithmetic conversions
1789	 between types L and R.
1790
1791	 For every type T, where T is a pointer or pointer-to-member
1792	 type, there exist candidate operator functions of the form T
1793	 operator?(bool, T, T);  */
1794
1795      if (promoted_arithmetic_type_p (type1)
1796	  && promoted_arithmetic_type_p (type2))
1797	/* That's OK.  */
1798	break;
1799
1800      /* Otherwise, the types should be pointers.  */
1801      if (!(TYPE_PTR_P (type1) || TYPE_PTR_TO_MEMBER_P (type1))
1802	  || !(TYPE_PTR_P (type2) || TYPE_PTR_TO_MEMBER_P (type2)))
1803	return;
1804
1805      /* We don't check that the two types are the same; the logic
1806	 below will actually create two candidates; one in which both
1807	 parameter types are TYPE1, and one in which both parameter
1808	 types are TYPE2.  */
1809      break;
1810
1811    default:
1812      abort ();
1813    }
1814
1815  /* If we're dealing with two pointer types or two enumeral types,
1816     we need candidates for both of them.  */
1817  if (type2 && !same_type_p (type1, type2)
1818      && TREE_CODE (type1) == TREE_CODE (type2)
1819      && (TREE_CODE (type1) == REFERENCE_TYPE
1820	  || (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
1821	  || (TYPE_PTRMEM_P (type1) && TYPE_PTRMEM_P (type2))
1822	  || TYPE_PTRMEMFUNC_P (type1)
1823	  || IS_AGGR_TYPE (type1)
1824	  || TREE_CODE (type1) == ENUMERAL_TYPE))
1825    {
1826      build_builtin_candidate
1827	(candidates, fnname, type1, type1, args, argtypes, flags);
1828      build_builtin_candidate
1829	(candidates, fnname, type2, type2, args, argtypes, flags);
1830      return;
1831    }
1832
1833  build_builtin_candidate
1834    (candidates, fnname, type1, type2, args, argtypes, flags);
1835}
1836
1837tree
1838type_decays_to (tree type)
1839{
1840  if (TREE_CODE (type) == ARRAY_TYPE)
1841    return build_pointer_type (TREE_TYPE (type));
1842  if (TREE_CODE (type) == FUNCTION_TYPE)
1843    return build_pointer_type (type);
1844  return type;
1845}
1846
1847/* There are three conditions of builtin candidates:
1848
1849   1) bool-taking candidates.  These are the same regardless of the input.
1850   2) pointer-pair taking candidates.  These are generated for each type
1851      one of the input types converts to.
1852   3) arithmetic candidates.  According to the standard, we should generate
1853      all of these, but I'm trying not to...
1854
1855   Here we generate a superset of the possible candidates for this particular
1856   case.  That is a subset of the full set the standard defines, plus some
1857   other cases which the standard disallows. add_builtin_candidate will
1858   filter out the invalid set.  */
1859
1860static void
1861add_builtin_candidates (struct z_candidate **candidates, enum tree_code code,
1862                        enum tree_code code2, tree fnname, tree *args,
1863                        int flags)
1864{
1865  int ref1, i;
1866  int enum_p = 0;
1867  tree type, argtypes[3];
1868  /* TYPES[i] is the set of possible builtin-operator parameter types
1869     we will consider for the Ith argument.  These are represented as
1870     a TREE_LIST; the TREE_VALUE of each node is the potential
1871     parameter type.  */
1872  tree types[2];
1873
1874  for (i = 0; i < 3; ++i)
1875    {
1876      if (args[i])
1877	argtypes[i]  = lvalue_type (args[i]);
1878      else
1879	argtypes[i] = NULL_TREE;
1880    }
1881
1882  switch (code)
1883    {
1884/* 4 For every pair T, VQ), where T is an arithmetic or  enumeration  type,
1885     and  VQ  is  either  volatile or empty, there exist candidate operator
1886     functions of the form
1887		 VQ T&   operator++(VQ T&);  */
1888
1889    case POSTINCREMENT_EXPR:
1890    case PREINCREMENT_EXPR:
1891    case POSTDECREMENT_EXPR:
1892    case PREDECREMENT_EXPR:
1893    case MODIFY_EXPR:
1894      ref1 = 1;
1895      break;
1896
1897/* 24There also exist candidate operator functions of the form
1898	     bool    operator!(bool);
1899	     bool    operator&&(bool, bool);
1900	     bool    operator||(bool, bool);  */
1901
1902    case TRUTH_NOT_EXPR:
1903      build_builtin_candidate
1904	(candidates, fnname, boolean_type_node,
1905	 NULL_TREE, args, argtypes, flags);
1906      return;
1907
1908    case TRUTH_ORIF_EXPR:
1909    case TRUTH_ANDIF_EXPR:
1910      build_builtin_candidate
1911	(candidates, fnname, boolean_type_node,
1912	 boolean_type_node, args, argtypes, flags);
1913      return;
1914
1915    case ADDR_EXPR:
1916    case COMPOUND_EXPR:
1917    case COMPONENT_REF:
1918      return;
1919
1920    case COND_EXPR:
1921    case EQ_EXPR:
1922    case NE_EXPR:
1923    case LT_EXPR:
1924    case LE_EXPR:
1925    case GT_EXPR:
1926    case GE_EXPR:
1927      enum_p = 1;
1928      /* Fall through.  */
1929
1930    default:
1931      ref1 = 0;
1932    }
1933
1934  types[0] = types[1] = NULL_TREE;
1935
1936  for (i = 0; i < 2; ++i)
1937    {
1938      if (! args[i])
1939	;
1940      else if (IS_AGGR_TYPE (argtypes[i]))
1941	{
1942	  tree convs;
1943
1944	  if (i == 0 && code == MODIFY_EXPR && code2 == NOP_EXPR)
1945	    return;
1946
1947	  convs = lookup_conversions (argtypes[i]);
1948
1949	  if (code == COND_EXPR)
1950	    {
1951	      if (real_lvalue_p (args[i]))
1952		types[i] = tree_cons
1953		  (NULL_TREE, build_reference_type (argtypes[i]), types[i]);
1954
1955	      types[i] = tree_cons
1956		(NULL_TREE, TYPE_MAIN_VARIANT (argtypes[i]), types[i]);
1957	    }
1958
1959	  else if (! convs)
1960	    return;
1961
1962	  for (; convs; convs = TREE_CHAIN (convs))
1963	    {
1964	      type = TREE_TYPE (TREE_TYPE (OVL_CURRENT (TREE_VALUE (convs))));
1965
1966	      if (i == 0 && ref1
1967		  && (TREE_CODE (type) != REFERENCE_TYPE
1968		      || CP_TYPE_CONST_P (TREE_TYPE (type))))
1969		continue;
1970
1971	      if (code == COND_EXPR && TREE_CODE (type) == REFERENCE_TYPE)
1972		types[i] = tree_cons (NULL_TREE, type, types[i]);
1973
1974	      type = non_reference (type);
1975	      if (i != 0 || ! ref1)
1976		{
1977		  type = TYPE_MAIN_VARIANT (type_decays_to (type));
1978	          if (enum_p && TREE_CODE (type) == ENUMERAL_TYPE)
1979	            types[i] = tree_cons (NULL_TREE, type, types[i]);
1980		  if (INTEGRAL_TYPE_P (type))
1981		    type = type_promotes_to (type);
1982		}
1983
1984	      if (! value_member (type, types[i]))
1985		types[i] = tree_cons (NULL_TREE, type, types[i]);
1986	    }
1987	}
1988      else
1989	{
1990	  if (code == COND_EXPR && real_lvalue_p (args[i]))
1991	    types[i] = tree_cons
1992	      (NULL_TREE, build_reference_type (argtypes[i]), types[i]);
1993	  type = non_reference (argtypes[i]);
1994	  if (i != 0 || ! ref1)
1995	    {
1996	      type = TYPE_MAIN_VARIANT (type_decays_to (type));
1997	      if (enum_p && TREE_CODE (type) == ENUMERAL_TYPE)
1998	        types[i] = tree_cons (NULL_TREE, type, types[i]);
1999	      if (INTEGRAL_TYPE_P (type))
2000		type = type_promotes_to (type);
2001	    }
2002	  types[i] = tree_cons (NULL_TREE, type, types[i]);
2003	}
2004    }
2005
2006  /* Run through the possible parameter types of both arguments,
2007     creating candidates with those parameter types.  */
2008  for (; types[0]; types[0] = TREE_CHAIN (types[0]))
2009    {
2010      if (types[1])
2011	for (type = types[1]; type; type = TREE_CHAIN (type))
2012	  add_builtin_candidate
2013	    (candidates, code, code2, fnname, TREE_VALUE (types[0]),
2014	     TREE_VALUE (type), args, argtypes, flags);
2015      else
2016	add_builtin_candidate
2017	  (candidates, code, code2, fnname, TREE_VALUE (types[0]),
2018	   NULL_TREE, args, argtypes, flags);
2019    }
2020
2021  return;
2022}
2023
2024
2025/* If TMPL can be successfully instantiated as indicated by
2026   EXPLICIT_TARGS and ARGLIST, adds the instantiation to CANDIDATES.
2027
2028   TMPL is the template.  EXPLICIT_TARGS are any explicit template
2029   arguments.  ARGLIST is the arguments provided at the call-site.
2030   The RETURN_TYPE is the desired type for conversion operators.  If
2031   OBJ is NULL_TREE, FLAGS and CTYPE are as for add_function_candidate.
2032   If an OBJ is supplied, FLAGS and CTYPE are ignored, and OBJ is as for
2033   add_conv_candidate.  */
2034
2035static struct z_candidate*
2036add_template_candidate_real (struct z_candidate **candidates, tree tmpl,
2037                             tree ctype, tree explicit_targs, tree arglist,
2038                             tree return_type, tree access_path,
2039			     tree conversion_path, int flags, tree obj,
2040                             unification_kind_t strict)
2041{
2042  int ntparms = DECL_NTPARMS (tmpl);
2043  tree targs = make_tree_vec (ntparms);
2044  tree args_without_in_chrg = arglist;
2045  struct z_candidate *cand;
2046  int i;
2047  tree fn;
2048
2049  /* We don't do deduction on the in-charge parameter, the VTT
2050     parameter or 'this'.  */
2051  if (DECL_NONSTATIC_MEMBER_FUNCTION_P (tmpl))
2052    args_without_in_chrg = TREE_CHAIN (args_without_in_chrg);
2053
2054  if ((DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (tmpl)
2055       || DECL_BASE_CONSTRUCTOR_P (tmpl))
2056      && TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (tmpl)))
2057    args_without_in_chrg = TREE_CHAIN (args_without_in_chrg);
2058
2059  i = fn_type_unification (tmpl, explicit_targs, targs,
2060			   args_without_in_chrg,
2061			   return_type, strict, -1);
2062
2063  if (i != 0)
2064    return NULL;
2065
2066  fn = instantiate_template (tmpl, targs, tf_none);
2067  if (fn == error_mark_node)
2068    return NULL;
2069
2070  /* In [class.copy]:
2071
2072       A member function template is never instantiated to perform the
2073       copy of a class object to an object of its class type.
2074
2075     It's a little unclear what this means; the standard explicitly
2076     does allow a template to be used to copy a class.  For example,
2077     in:
2078
2079       struct A {
2080         A(A&);
2081	 template <class T> A(const T&);
2082       };
2083       const A f ();
2084       void g () { A a (f ()); }
2085
2086     the member template will be used to make the copy.  The section
2087     quoted above appears in the paragraph that forbids constructors
2088     whose only parameter is (a possibly cv-qualified variant of) the
2089     class type, and a logical interpretation is that the intent was
2090     to forbid the instantiation of member templates which would then
2091     have that form.  */
2092  if (DECL_CONSTRUCTOR_P (fn) && list_length (arglist) == 2)
2093    {
2094      tree arg_types = FUNCTION_FIRST_USER_PARMTYPE (fn);
2095      if (arg_types && same_type_p (TYPE_MAIN_VARIANT (TREE_VALUE (arg_types)),
2096				    ctype))
2097	return NULL;
2098    }
2099
2100  if (obj != NULL_TREE)
2101    /* Aha, this is a conversion function.  */
2102    cand = add_conv_candidate (candidates, fn, obj, access_path,
2103			       conversion_path, arglist);
2104  else
2105    cand = add_function_candidate (candidates, fn, ctype,
2106				   arglist, access_path,
2107				   conversion_path, flags);
2108  if (DECL_TI_TEMPLATE (fn) != tmpl)
2109    /* This situation can occur if a member template of a template
2110       class is specialized.  Then, instantiate_template might return
2111       an instantiation of the specialization, in which case the
2112       DECL_TI_TEMPLATE field will point at the original
2113       specialization.  For example:
2114
2115	 template <class T> struct S { template <class U> void f(U);
2116				       template <> void f(int) {}; };
2117	 S<double> sd;
2118	 sd.f(3);
2119
2120       Here, TMPL will be template <class U> S<double>::f(U).
2121       And, instantiate template will give us the specialization
2122       template <> S<double>::f(int).  But, the DECL_TI_TEMPLATE field
2123       for this will point at template <class T> template <> S<T>::f(int),
2124       so that we can find the definition.  For the purposes of
2125       overload resolution, however, we want the original TMPL.  */
2126    cand->template = tree_cons (tmpl, targs, NULL_TREE);
2127  else
2128    cand->template = DECL_TEMPLATE_INFO (fn);
2129
2130  return cand;
2131}
2132
2133
2134static struct z_candidate *
2135add_template_candidate (struct z_candidate **candidates, tree tmpl, tree ctype,
2136                        tree explicit_targs, tree arglist, tree return_type,
2137                        tree access_path, tree conversion_path, int flags,
2138                        unification_kind_t strict)
2139{
2140  return
2141    add_template_candidate_real (candidates, tmpl, ctype,
2142				 explicit_targs, arglist, return_type,
2143				 access_path, conversion_path,
2144				 flags, NULL_TREE, strict);
2145}
2146
2147
2148static struct z_candidate *
2149add_template_conv_candidate (struct z_candidate **candidates, tree tmpl,
2150                             tree obj, tree arglist, tree return_type,
2151			     tree access_path, tree conversion_path)
2152{
2153  return
2154    add_template_candidate_real (candidates, tmpl, NULL_TREE, NULL_TREE,
2155				 arglist, return_type, access_path,
2156				 conversion_path, 0, obj, DEDUCE_CONV);
2157}
2158
2159/* The CANDS are the set of candidates that were considered for
2160   overload resolution.  Return the set of viable candidates.  If none
2161   of the candidates were viable, set *ANY_VIABLE_P to true.  STRICT_P
2162   is true if a candidate should be considered viable only if it is
2163   strictly viable.  */
2164
2165static struct z_candidate*
2166splice_viable (struct z_candidate *cands,
2167	       bool strict_p,
2168	       bool *any_viable_p)
2169{
2170  struct z_candidate *viable;
2171  struct z_candidate **last_viable;
2172  struct z_candidate **cand;
2173
2174  viable = NULL;
2175  last_viable = &viable;
2176  *any_viable_p = false;
2177
2178  cand = &cands;
2179  while (*cand)
2180    {
2181      struct z_candidate *c = *cand;
2182      if (strict_p ? c->viable == 1 : c->viable)
2183	{
2184	  *last_viable = c;
2185	  *cand = c->next;
2186	  c->next = NULL;
2187	  last_viable = &c->next;
2188	  *any_viable_p = true;
2189	}
2190      else
2191	cand = &c->next;
2192    }
2193
2194  return viable ? viable : cands;
2195}
2196
2197static bool
2198any_strictly_viable (struct z_candidate *cands)
2199{
2200  for (; cands; cands = cands->next)
2201    if (cands->viable == 1)
2202      return true;
2203  return false;
2204}
2205
2206/* OBJ is being used in an expression like "OBJ.f (...)".  In other
2207   words, it is about to become the "this" pointer for a member
2208   function call.  Take the address of the object.  */
2209
2210static tree
2211build_this (tree obj)
2212{
2213  /* In a template, we are only concerned about the type of the
2214     expression, so we can take a shortcut.  */
2215  if (processing_template_decl)
2216    return build_address (obj);
2217
2218  return build_unary_op (ADDR_EXPR, obj, 0);
2219}
2220
2221/* Returns true iff functions are equivalent. Equivalent functions are
2222   not '==' only if one is a function-local extern function or if
2223   both are extern "C".  */
2224
2225static inline int
2226equal_functions (tree fn1, tree fn2)
2227{
2228  if (DECL_LOCAL_FUNCTION_P (fn1) || DECL_LOCAL_FUNCTION_P (fn2)
2229      || DECL_EXTERN_C_FUNCTION_P (fn1))
2230    return decls_match (fn1, fn2);
2231  return fn1 == fn2;
2232}
2233
2234/* Print information about one overload candidate CANDIDATE.  MSGSTR
2235   is the text to print before the candidate itself.
2236
2237   NOTE: Unlike most diagnostic functions in GCC, MSGSTR is expected
2238   to have been run through gettext by the caller.  This wart makes
2239   life simpler in print_z_candidates and for the translators.  */
2240
2241static void
2242print_z_candidate (const char *msgstr, struct z_candidate *candidate)
2243{
2244  if (TREE_CODE (candidate->fn) == IDENTIFIER_NODE)
2245    {
2246      if (TREE_VEC_LENGTH (candidate->convs) == 3)
2247	inform ("%s %D(%T, %T, %T) <built-in>", msgstr, candidate->fn,
2248		TREE_TYPE (TREE_VEC_ELT (candidate->convs, 0)),
2249		TREE_TYPE (TREE_VEC_ELT (candidate->convs, 1)),
2250		TREE_TYPE (TREE_VEC_ELT (candidate->convs, 2)));
2251      else if (TREE_VEC_LENGTH (candidate->convs) == 2)
2252	inform ("%s %D(%T, %T) <built-in>", msgstr, candidate->fn,
2253		TREE_TYPE (TREE_VEC_ELT (candidate->convs, 0)),
2254		TREE_TYPE (TREE_VEC_ELT (candidate->convs, 1)));
2255      else
2256	inform ("%s %D(%T) <built-in>", msgstr, candidate->fn,
2257		TREE_TYPE (TREE_VEC_ELT (candidate->convs, 0)));
2258    }
2259  else if (TYPE_P (candidate->fn))
2260    inform ("%s %T <conversion>", msgstr, candidate->fn);
2261  else if (candidate->viable == -1)
2262    inform ("%J%s %+#D <near match>", candidate->fn, msgstr, candidate->fn);
2263  else
2264    inform ("%J%s %+#D", candidate->fn, msgstr, candidate->fn);
2265}
2266
2267static void
2268print_z_candidates (struct z_candidate *candidates)
2269{
2270  const char *str;
2271  struct z_candidate *cand1;
2272  struct z_candidate **cand2;
2273
2274  /* There may be duplicates in the set of candidates.  We put off
2275     checking this condition as long as possible, since we have no way
2276     to eliminate duplicates from a set of functions in less than n^2
2277     time.  Now we are about to emit an error message, so it is more
2278     permissible to go slowly.  */
2279  for (cand1 = candidates; cand1; cand1 = cand1->next)
2280    {
2281      tree fn = cand1->fn;
2282      /* Skip builtin candidates and conversion functions.  */
2283      if (TREE_CODE (fn) != FUNCTION_DECL)
2284	continue;
2285      cand2 = &cand1->next;
2286      while (*cand2)
2287	{
2288	  if (TREE_CODE ((*cand2)->fn) == FUNCTION_DECL
2289	      && equal_functions (fn, (*cand2)->fn))
2290	    *cand2 = (*cand2)->next;
2291	  else
2292	    cand2 = &(*cand2)->next;
2293	}
2294    }
2295
2296  if (!candidates)
2297    return;
2298
2299  str = _("candidates are:");
2300  print_z_candidate (str, candidates);
2301  if (candidates->next)
2302    {
2303      /* Indent successive candidates by the width of the translation
2304	 of the above string.  */
2305      size_t len = gcc_gettext_width (str) + 1;
2306      char *spaces = alloca (len);
2307      memset (spaces, ' ', len-1);
2308      spaces[len - 1] = '\0';
2309
2310      candidates = candidates->next;
2311      do
2312	{
2313	  print_z_candidate (spaces, candidates);
2314	  candidates = candidates->next;
2315	}
2316      while (candidates);
2317    }
2318}
2319
2320/* USER_SEQ is a user-defined conversion sequence, beginning with a
2321   USER_CONV.  STD_SEQ is the standard conversion sequence applied to
2322   the result of the conversion function to convert it to the final
2323   desired type.  Merge the the two sequences into a single sequence,
2324   and return the merged sequence.  */
2325
2326static tree
2327merge_conversion_sequences (tree user_seq, tree std_seq)
2328{
2329  tree *t;
2330
2331  my_friendly_assert (TREE_CODE (user_seq) == USER_CONV,
2332		      20030306);
2333
2334  /* Find the end of the second conversion sequence.  */
2335  t = &(std_seq);
2336  while (TREE_CODE (*t) != IDENTITY_CONV)
2337    t = &TREE_OPERAND (*t, 0);
2338
2339  /* Replace the identity conversion with the user conversion
2340     sequence.  */
2341  *t = user_seq;
2342
2343  /* The entire sequence is a user-conversion sequence.  */
2344  ICS_USER_FLAG (std_seq) = 1;
2345
2346  return std_seq;
2347}
2348
2349/* Returns the best overload candidate to perform the requested
2350   conversion.  This function is used for three the overloading situations
2351   described in [over.match.copy], [over.match.conv], and [over.match.ref].
2352   If TOTYPE is a REFERENCE_TYPE, we're trying to find an lvalue binding as
2353   per [dcl.init.ref], so we ignore temporary bindings.  */
2354
2355static struct z_candidate *
2356build_user_type_conversion_1 (tree totype, tree expr, int flags)
2357{
2358  struct z_candidate *candidates, *cand;
2359  tree fromtype = TREE_TYPE (expr);
2360  tree ctors = NULL_TREE, convs = NULL_TREE;
2361  tree args = NULL_TREE;
2362  bool any_viable_p;
2363
2364  /* We represent conversion within a hierarchy using RVALUE_CONV and
2365     BASE_CONV, as specified by [over.best.ics]; these become plain
2366     constructor calls, as specified in [dcl.init].  */
2367  my_friendly_assert (!IS_AGGR_TYPE (fromtype) || !IS_AGGR_TYPE (totype)
2368		      || !DERIVED_FROM_P (totype, fromtype), 20011226);
2369
2370  if (IS_AGGR_TYPE (totype))
2371    ctors = lookup_fnfields (TYPE_BINFO (totype),
2372			     complete_ctor_identifier,
2373			     0);
2374
2375  if (IS_AGGR_TYPE (fromtype))
2376    convs = lookup_conversions (fromtype);
2377
2378  candidates = 0;
2379  flags |= LOOKUP_NO_CONVERSION;
2380
2381  if (ctors)
2382    {
2383      tree t;
2384
2385      ctors = BASELINK_FUNCTIONS (ctors);
2386
2387      t = build_int_2 (0, 0);
2388      TREE_TYPE (t) = build_pointer_type (totype);
2389      args = build_tree_list (NULL_TREE, expr);
2390      /* We should never try to call the abstract or base constructor
2391	 from here.  */
2392      my_friendly_assert (!DECL_HAS_IN_CHARGE_PARM_P (OVL_CURRENT (ctors))
2393			  && !DECL_HAS_VTT_PARM_P (OVL_CURRENT (ctors)),
2394			  20011226);
2395      args = tree_cons (NULL_TREE, t, args);
2396    }
2397  for (; ctors; ctors = OVL_NEXT (ctors))
2398    {
2399      tree ctor = OVL_CURRENT (ctors);
2400      if (DECL_NONCONVERTING_P (ctor))
2401	continue;
2402
2403      if (TREE_CODE (ctor) == TEMPLATE_DECL)
2404	cand = add_template_candidate (&candidates, ctor, totype,
2405				       NULL_TREE, args, NULL_TREE,
2406				       TYPE_BINFO (totype),
2407				       TYPE_BINFO (totype),
2408				       flags,
2409				       DEDUCE_CALL);
2410      else
2411	cand = add_function_candidate (&candidates, ctor, totype,
2412				       args, TYPE_BINFO (totype),
2413				       TYPE_BINFO (totype),
2414				       flags);
2415
2416      if (cand)
2417	cand->second_conv = build1 (IDENTITY_CONV, totype, NULL_TREE);
2418    }
2419
2420  if (convs)
2421    args = build_tree_list (NULL_TREE, build_this (expr));
2422
2423  for (; convs; convs = TREE_CHAIN (convs))
2424    {
2425      tree fns;
2426      tree conversion_path = TREE_PURPOSE (convs);
2427      int convflags = LOOKUP_NO_CONVERSION;
2428
2429      /* If we are called to convert to a reference type, we are trying to
2430	 find an lvalue binding, so don't even consider temporaries.  If
2431	 we don't find an lvalue binding, the caller will try again to
2432	 look for a temporary binding.  */
2433      if (TREE_CODE (totype) == REFERENCE_TYPE)
2434	convflags |= LOOKUP_NO_TEMP_BIND;
2435
2436      for (fns = TREE_VALUE (convs); fns; fns = OVL_NEXT (fns))
2437	{
2438	  tree fn = OVL_CURRENT (fns);
2439
2440	  /* [over.match.funcs] For conversion functions, the function
2441	     is considered to be a member of the class of the implicit
2442	     object argument for the purpose of defining the type of
2443	     the implicit object parameter.
2444
2445	     So we pass fromtype as CTYPE to add_*_candidate.  */
2446
2447	  if (TREE_CODE (fn) == TEMPLATE_DECL)
2448	    cand = add_template_candidate (&candidates, fn, fromtype,
2449					   NULL_TREE,
2450					   args, totype,
2451					   TYPE_BINFO (fromtype),
2452					   conversion_path,
2453					   flags,
2454					   DEDUCE_CONV);
2455	  else
2456	    cand = add_function_candidate (&candidates, fn, fromtype,
2457					   args,
2458					   TYPE_BINFO (fromtype),
2459					   conversion_path,
2460					   flags);
2461
2462	  if (cand)
2463	    {
2464	      tree ics = implicit_conversion (totype,
2465					      TREE_TYPE (TREE_TYPE (cand->fn)),
2466					      0, convflags);
2467
2468	      cand->second_conv = ics;
2469
2470	      if (ics == NULL_TREE)
2471		cand->viable = 0;
2472	      else if (candidates->viable == 1 && ICS_BAD_FLAG (ics))
2473		cand->viable = -1;
2474	    }
2475	}
2476    }
2477
2478  candidates = splice_viable (candidates, pedantic, &any_viable_p);
2479  if (!any_viable_p)
2480    return 0;
2481
2482  cand = tourney (candidates);
2483  if (cand == 0)
2484    {
2485      if (flags & LOOKUP_COMPLAIN)
2486	{
2487	  error ("conversion from `%T' to `%T' is ambiguous",
2488		    fromtype, totype);
2489	  print_z_candidates (candidates);
2490	}
2491
2492      cand = candidates;	/* any one will do */
2493      cand->second_conv = build1 (AMBIG_CONV, totype, expr);
2494      ICS_USER_FLAG (cand->second_conv) = 1;
2495      if (!any_strictly_viable (candidates))
2496	ICS_BAD_FLAG (cand->second_conv) = 1;
2497      /* If there are viable candidates, don't set ICS_BAD_FLAG; an
2498	 ambiguous conversion is no worse than another user-defined
2499	 conversion.  */
2500
2501      return cand;
2502    }
2503
2504  /* Build the user conversion sequence.  */
2505  convs = build_conv
2506    (USER_CONV,
2507     (DECL_CONSTRUCTOR_P (cand->fn)
2508      ? totype : non_reference (TREE_TYPE (TREE_TYPE (cand->fn)))),
2509     build1 (IDENTITY_CONV, TREE_TYPE (expr), expr));
2510  TREE_OPERAND (convs, 1) = build_zc_wrapper (cand);
2511
2512  /* Combine it with the second conversion sequence.  */
2513  cand->second_conv = merge_conversion_sequences (convs,
2514						  cand->second_conv);
2515
2516  if (cand->viable == -1)
2517    ICS_BAD_FLAG (cand->second_conv) = 1;
2518
2519  return cand;
2520}
2521
2522tree
2523build_user_type_conversion (tree totype, tree expr, int flags)
2524{
2525  struct z_candidate *cand
2526    = build_user_type_conversion_1 (totype, expr, flags);
2527
2528  if (cand)
2529    {
2530      if (TREE_CODE (cand->second_conv) == AMBIG_CONV)
2531	return error_mark_node;
2532      return convert_from_reference (convert_like (cand->second_conv, expr));
2533    }
2534  return NULL_TREE;
2535}
2536
2537/* Do any initial processing on the arguments to a function call.  */
2538
2539static tree
2540resolve_args (tree args)
2541{
2542  tree t;
2543  for (t = args; t; t = TREE_CHAIN (t))
2544    {
2545      tree arg = TREE_VALUE (t);
2546
2547      if (arg == error_mark_node)
2548	return error_mark_node;
2549      else if (VOID_TYPE_P (TREE_TYPE (arg)))
2550	{
2551	  error ("invalid use of void expression");
2552	  return error_mark_node;
2553	}
2554      arg = convert_from_reference (arg);
2555      TREE_VALUE (t) = arg;
2556    }
2557  return args;
2558}
2559
2560/* Perform overload resolution on FN, which is called with the ARGS.
2561
2562   Return the candidate function selected by overload resolution, or
2563   NULL if the event that overload resolution failed.  In the case
2564   that overload resolution fails, *CANDIDATES will be the set of
2565   candidates considered, and ANY_VIABLE_P will be set to true or
2566   false to indicate whether or not any of the candidates were
2567   viable.
2568
2569   The ARGS should already have gone through RESOLVE_ARGS before this
2570   function is called.  */
2571
2572static struct z_candidate *
2573perform_overload_resolution (tree fn,
2574			     tree args,
2575			     struct z_candidate **candidates,
2576			     bool *any_viable_p)
2577{
2578  struct z_candidate *cand;
2579  tree explicit_targs = NULL_TREE;
2580  int template_only = 0;
2581
2582  *candidates = NULL;
2583  *any_viable_p = true;
2584
2585  /* Check FN and ARGS.  */
2586  my_friendly_assert (TREE_CODE (fn) == FUNCTION_DECL
2587		      || TREE_CODE (fn) == TEMPLATE_DECL
2588		      || TREE_CODE (fn) == OVERLOAD
2589		      || TREE_CODE (fn) == TEMPLATE_ID_EXPR,
2590		      20020712);
2591  my_friendly_assert (!args || TREE_CODE (args) == TREE_LIST,
2592		      20020712);
2593
2594  if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
2595    {
2596      explicit_targs = TREE_OPERAND (fn, 1);
2597      fn = TREE_OPERAND (fn, 0);
2598      template_only = 1;
2599    }
2600
2601  /* Add the various candidate functions.  */
2602  add_candidates (fn, args, explicit_targs, template_only,
2603		  /*conversion_path=*/NULL_TREE,
2604		  /*access_path=*/NULL_TREE,
2605		  LOOKUP_NORMAL,
2606		  candidates);
2607
2608  *candidates = splice_viable (*candidates, pedantic, any_viable_p);
2609  if (!*any_viable_p)
2610    return NULL;
2611
2612  cand = tourney (*candidates);
2613  return cand;
2614}
2615
2616/* Return an expression for a call to FN (a namespace-scope function,
2617   or a static member function) with the ARGS.  */
2618
2619tree
2620build_new_function_call (tree fn, tree args)
2621{
2622  struct z_candidate *candidates, *cand;
2623  bool any_viable_p;
2624
2625  args = resolve_args (args);
2626  if (args == error_mark_node)
2627    return error_mark_node;
2628
2629  cand = perform_overload_resolution (fn, args, &candidates, &any_viable_p);
2630
2631  if (!cand)
2632    {
2633      if (!any_viable_p && candidates && ! candidates->next)
2634	return build_function_call (candidates->fn, args);
2635      if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
2636	fn = TREE_OPERAND (fn, 0);
2637      if (!any_viable_p)
2638	error ("no matching function for call to `%D(%A)'",
2639	       DECL_NAME (OVL_CURRENT (fn)), args);
2640      else
2641	error ("call of overloaded `%D(%A)' is ambiguous",
2642	       DECL_NAME (OVL_CURRENT (fn)), args);
2643      if (candidates)
2644	print_z_candidates (candidates);
2645      return error_mark_node;
2646    }
2647
2648  return build_over_call (cand, LOOKUP_NORMAL);
2649}
2650
2651/* Build a call to a global operator new.  FNNAME is the name of the
2652   operator (either "operator new" or "operator new[]") and ARGS are
2653   the arguments provided.  *SIZE points to the total number of bytes
2654   required by the allocation, and is updated if that is changed here.
2655   *COOKIE_SIZE is non-NULL if a cookie should be used.  If this
2656   function determines that no cookie should be used, after all,
2657   *COOKIE_SIZE is set to NULL_TREE.  */
2658
2659tree
2660build_operator_new_call (tree fnname, tree args, tree *size, tree *cookie_size)
2661{
2662  tree fns;
2663  struct z_candidate *candidates;
2664  struct z_candidate *cand;
2665  bool any_viable_p;
2666
2667  args = tree_cons (NULL_TREE, *size, args);
2668  args = resolve_args (args);
2669  if (args == error_mark_node)
2670    return args;
2671
2672  fns = lookup_function_nonclass (fnname, args);
2673
2674  /* Figure out what function is being called.  */
2675  cand = perform_overload_resolution (fns, args, &candidates, &any_viable_p);
2676
2677  /* If no suitable function could be found, issue an error message
2678     and give up.  */
2679  if (!cand)
2680    {
2681      if (!any_viable_p)
2682	error ("no matching function for call to `%D(%A)'",
2683	       DECL_NAME (OVL_CURRENT (fns)), args);
2684      else
2685	error ("call of overloaded `%D(%A)' is ambiguous",
2686	       DECL_NAME (OVL_CURRENT (fns)), args);
2687      if (candidates)
2688	print_z_candidates (candidates);
2689      return error_mark_node;
2690    }
2691
2692   /* If a cookie is required, add some extra space.  Whether
2693      or not a cookie is required cannot be determined until
2694      after we know which function was called.  */
2695   if (*cookie_size)
2696     {
2697       bool use_cookie = true;
2698       if (!abi_version_at_least (2))
2699	 {
2700	   tree placement = TREE_CHAIN (args);
2701	   /* In G++ 3.2, the check was implemented incorrectly; it
2702	      looked at the placement expression, rather than the
2703	      type of the function.  */
2704	   if (placement && !TREE_CHAIN (placement)
2705	       && same_type_p (TREE_TYPE (TREE_VALUE (placement)),
2706			       ptr_type_node))
2707	     use_cookie = false;
2708	 }
2709       else
2710	 {
2711	   tree arg_types;
2712
2713	   arg_types = TYPE_ARG_TYPES (TREE_TYPE (cand->fn));
2714	   /* Skip the size_t parameter.  */
2715	   arg_types = TREE_CHAIN (arg_types);
2716	   /* Check the remaining parameters (if any).  */
2717	   if (arg_types
2718	       && TREE_CHAIN (arg_types) == void_list_node
2719	       && same_type_p (TREE_VALUE (arg_types),
2720			       ptr_type_node))
2721	     use_cookie = false;
2722	 }
2723       /* If we need a cookie, adjust the number of bytes allocated.  */
2724       if (use_cookie)
2725	 {
2726	   /* Update the total size.  */
2727	   *size = size_binop (PLUS_EXPR, *size, *cookie_size);
2728	   /* Update the argument list to reflect the adjusted size.  */
2729	   TREE_VALUE (args) = *size;
2730	 }
2731       else
2732	 *cookie_size = NULL_TREE;
2733     }
2734
2735   /* Build the CALL_EXPR.  */
2736   return build_over_call (cand, LOOKUP_NORMAL);
2737}
2738
2739static tree
2740build_object_call (tree obj, tree args)
2741{
2742  struct z_candidate *candidates = 0, *cand;
2743  tree fns, convs, mem_args = NULL_TREE;
2744  tree type = TREE_TYPE (obj);
2745  bool any_viable_p;
2746
2747  if (TYPE_PTRMEMFUNC_P (type))
2748    {
2749      /* It's no good looking for an overloaded operator() on a
2750	 pointer-to-member-function.  */
2751      error ("pointer-to-member function %E cannot be called without an object; consider using .* or ->*", obj);
2752      return error_mark_node;
2753    }
2754
2755  fns = lookup_fnfields (TYPE_BINFO (type), ansi_opname (CALL_EXPR), 1);
2756  if (fns == error_mark_node)
2757    return error_mark_node;
2758
2759  args = resolve_args (args);
2760
2761  if (args == error_mark_node)
2762    return error_mark_node;
2763
2764  if (fns)
2765    {
2766      tree base = BINFO_TYPE (BASELINK_BINFO (fns));
2767      mem_args = tree_cons (NULL_TREE, build_this (obj), args);
2768
2769      for (fns = BASELINK_FUNCTIONS (fns); fns; fns = OVL_NEXT (fns))
2770	{
2771	  tree fn = OVL_CURRENT (fns);
2772	  if (TREE_CODE (fn) == TEMPLATE_DECL)
2773	    add_template_candidate (&candidates, fn, base, NULL_TREE,
2774				    mem_args, NULL_TREE,
2775				    TYPE_BINFO (type),
2776				    TYPE_BINFO (type),
2777				    LOOKUP_NORMAL, DEDUCE_CALL);
2778	  else
2779	    add_function_candidate
2780	      (&candidates, fn, base, mem_args, TYPE_BINFO (type),
2781	       TYPE_BINFO (type), LOOKUP_NORMAL);
2782	}
2783    }
2784
2785  convs = lookup_conversions (type);
2786
2787  for (; convs; convs = TREE_CHAIN (convs))
2788    {
2789      tree fns = TREE_VALUE (convs);
2790      tree totype = TREE_TYPE (TREE_TYPE (OVL_CURRENT (fns)));
2791
2792      if ((TREE_CODE (totype) == POINTER_TYPE
2793	   && TREE_CODE (TREE_TYPE (totype)) == FUNCTION_TYPE)
2794	  || (TREE_CODE (totype) == REFERENCE_TYPE
2795	      && TREE_CODE (TREE_TYPE (totype)) == FUNCTION_TYPE)
2796	  || (TREE_CODE (totype) == REFERENCE_TYPE
2797	      && TREE_CODE (TREE_TYPE (totype)) == POINTER_TYPE
2798	      && TREE_CODE (TREE_TYPE (TREE_TYPE (totype))) == FUNCTION_TYPE))
2799	for (; fns; fns = OVL_NEXT (fns))
2800	  {
2801	    tree fn = OVL_CURRENT (fns);
2802	    if (TREE_CODE (fn) == TEMPLATE_DECL)
2803	      add_template_conv_candidate
2804		(&candidates, fn, obj, args, totype,
2805		 /*access_path=*/NULL_TREE,
2806		 /*conversion_path=*/NULL_TREE);
2807	    else
2808	      add_conv_candidate (&candidates, fn, obj, args,
2809				  /*conversion_path=*/NULL_TREE,
2810				  /*access_path=*/NULL_TREE);
2811	  }
2812    }
2813
2814  candidates = splice_viable (candidates, pedantic, &any_viable_p);
2815  if (!any_viable_p)
2816    {
2817      error ("no match for call to `(%T) (%A)'", TREE_TYPE (obj), args);
2818      print_z_candidates (candidates);
2819      return error_mark_node;
2820    }
2821
2822  cand = tourney (candidates);
2823  if (cand == 0)
2824    {
2825      error ("call of `(%T) (%A)' is ambiguous", TREE_TYPE (obj), args);
2826      print_z_candidates (candidates);
2827      return error_mark_node;
2828    }
2829
2830  /* Since cand->fn will be a type, not a function, for a conversion
2831     function, we must be careful not to unconditionally look at
2832     DECL_NAME here.  */
2833  if (TREE_CODE (cand->fn) == FUNCTION_DECL
2834      && DECL_OVERLOADED_OPERATOR_P (cand->fn) == CALL_EXPR)
2835    return build_over_call (cand, LOOKUP_NORMAL);
2836
2837  obj = convert_like_with_context
2838          (TREE_VEC_ELT (cand->convs, 0), obj, cand->fn, -1);
2839
2840  /* FIXME */
2841  return build_function_call (obj, args);
2842}
2843
2844static void
2845op_error (enum tree_code code, enum tree_code code2,
2846          tree arg1, tree arg2, tree arg3, const char *problem)
2847{
2848  const char *opname;
2849
2850  if (code == MODIFY_EXPR)
2851    opname = assignment_operator_name_info[code2].name;
2852  else
2853    opname = operator_name_info[code].name;
2854
2855  switch (code)
2856    {
2857    case COND_EXPR:
2858      error ("%s for ternary 'operator?:' in '%E ? %E : %E'",
2859             problem, arg1, arg2, arg3);
2860      break;
2861
2862    case POSTINCREMENT_EXPR:
2863    case POSTDECREMENT_EXPR:
2864      error ("%s for 'operator%s' in '%E%s'", problem, opname, arg1, opname);
2865      break;
2866
2867    case ARRAY_REF:
2868      error ("%s for 'operator[]' in '%E[%E]'", problem, arg1, arg2);
2869      break;
2870
2871    case REALPART_EXPR:
2872    case IMAGPART_EXPR:
2873      error ("%s for '%s' in '%s %E'", problem, opname, opname, arg1);
2874      break;
2875
2876    default:
2877      if (arg2)
2878	error ("%s for 'operator%s' in '%E %s %E'",
2879               problem, opname, arg1, opname, arg2);
2880      else
2881	error ("%s for 'operator%s' in '%s%E'",
2882               problem, opname, opname, arg1);
2883      break;
2884    }
2885}
2886
2887/* Return the implicit conversion sequence that could be used to
2888   convert E1 to E2 in [expr.cond].  */
2889
2890static tree
2891conditional_conversion (tree e1, tree e2)
2892{
2893  tree t1 = non_reference (TREE_TYPE (e1));
2894  tree t2 = non_reference (TREE_TYPE (e2));
2895  tree conv;
2896  bool good_base;
2897
2898  /* [expr.cond]
2899
2900     If E2 is an lvalue: E1 can be converted to match E2 if E1 can be
2901     implicitly converted (clause _conv_) to the type "reference to
2902     T2", subject to the constraint that in the conversion the
2903     reference must bind directly (_dcl.init.ref_) to E1.  */
2904  if (real_lvalue_p (e2))
2905    {
2906      conv = implicit_conversion (build_reference_type (t2),
2907				  t1,
2908				  e1,
2909				  LOOKUP_NO_TEMP_BIND);
2910      if (conv)
2911	return conv;
2912    }
2913
2914  /* [expr.cond]
2915
2916     If E1 and E2 have class type, and the underlying class types are
2917     the same or one is a base class of the other: E1 can be converted
2918     to match E2 if the class of T2 is the same type as, or a base
2919     class of, the class of T1, and the cv-qualification of T2 is the
2920     same cv-qualification as, or a greater cv-qualification than, the
2921     cv-qualification of T1.  If the conversion is applied, E1 is
2922     changed to an rvalue of type T2 that still refers to the original
2923     source class object (or the appropriate subobject thereof).  */
2924  if (CLASS_TYPE_P (t1) && CLASS_TYPE_P (t2)
2925      && ((good_base = DERIVED_FROM_P (t2, t1)) || DERIVED_FROM_P (t1, t2)))
2926    {
2927      if (good_base && at_least_as_qualified_p (t2, t1))
2928	{
2929	  conv = build1 (IDENTITY_CONV, t1, e1);
2930	  if (!same_type_p (TYPE_MAIN_VARIANT (t1),
2931			    TYPE_MAIN_VARIANT (t2)))
2932	    conv = build_conv (BASE_CONV, t2, conv);
2933	  else
2934	    conv = build_conv (RVALUE_CONV, t2, conv);
2935	  return conv;
2936	}
2937      else
2938	return NULL_TREE;
2939    }
2940  else
2941    /* [expr.cond]
2942
2943       Otherwise: E1 can be converted to match E2 if E1 can be implicitly
2944       converted to the type that expression E2 would have if E2 were
2945       converted to an rvalue (or the type it has, if E2 is an rvalue).  */
2946    return implicit_conversion (t2, t1, e1, LOOKUP_NORMAL);
2947}
2948
2949/* Implement [expr.cond].  ARG1, ARG2, and ARG3 are the three
2950   arguments to the conditional expression.  */
2951
2952tree
2953build_conditional_expr (tree arg1, tree arg2, tree arg3)
2954{
2955  tree arg2_type;
2956  tree arg3_type;
2957  tree result;
2958  tree result_type = NULL_TREE;
2959  bool lvalue_p = true;
2960  struct z_candidate *candidates = 0;
2961  struct z_candidate *cand;
2962
2963  /* As a G++ extension, the second argument to the conditional can be
2964     omitted.  (So that `a ? : c' is roughly equivalent to `a ? a :
2965     c'.)  If the second operand is omitted, make sure it is
2966     calculated only once.  */
2967  if (!arg2)
2968    {
2969      if (pedantic)
2970	pedwarn ("ISO C++ forbids omitting the middle term of a ?: expression");
2971
2972      /* Make sure that lvalues remain lvalues.  See g++.oliva/ext1.C.  */
2973      if (real_lvalue_p (arg1))
2974	arg2 = arg1 = stabilize_reference (arg1);
2975      else
2976	arg2 = arg1 = save_expr (arg1);
2977    }
2978
2979  /* [expr.cond]
2980
2981     The first expr ession is implicitly converted to bool (clause
2982     _conv_).  */
2983  arg1 = perform_implicit_conversion (boolean_type_node, arg1);
2984
2985  /* If something has already gone wrong, just pass that fact up the
2986     tree.  */
2987  if (error_operand_p (arg1)
2988      || error_operand_p (arg2)
2989      || error_operand_p (arg3))
2990    return error_mark_node;
2991
2992  /* [expr.cond]
2993
2994     If either the second or the third operand has type (possibly
2995     cv-qualified) void, then the lvalue-to-rvalue (_conv.lval_),
2996     array-to-pointer (_conv.array_), and function-to-pointer
2997     (_conv.func_) standard conversions are performed on the second
2998     and third operands.  */
2999  arg2_type = TREE_TYPE (arg2);
3000  arg3_type = TREE_TYPE (arg3);
3001  if (VOID_TYPE_P (arg2_type) || VOID_TYPE_P (arg3_type))
3002    {
3003      /* Do the conversions.  We don't these for `void' type arguments
3004	 since it can't have any effect and since decay_conversion
3005	 does not handle that case gracefully.  */
3006      if (!VOID_TYPE_P (arg2_type))
3007	arg2 = decay_conversion (arg2);
3008      if (!VOID_TYPE_P (arg3_type))
3009	arg3 = decay_conversion (arg3);
3010      arg2_type = TREE_TYPE (arg2);
3011      arg3_type = TREE_TYPE (arg3);
3012
3013      /* [expr.cond]
3014
3015	 One of the following shall hold:
3016
3017	 --The second or the third operand (but not both) is a
3018	   throw-expression (_except.throw_); the result is of the
3019	   type of the other and is an rvalue.
3020
3021	 --Both the second and the third operands have type void; the
3022	   result is of type void and is an rvalue.
3023
3024         We must avoid calling force_rvalue for expressions of type
3025	 "void" because it will complain that their value is being
3026	 used.   */
3027      if (TREE_CODE (arg2) == THROW_EXPR
3028	  && TREE_CODE (arg3) != THROW_EXPR)
3029	{
3030	  if (!VOID_TYPE_P (arg3_type))
3031	    arg3 = force_rvalue (arg3);
3032	  arg3_type = TREE_TYPE (arg3);
3033	  result_type = arg3_type;
3034	}
3035      else if (TREE_CODE (arg2) != THROW_EXPR
3036	       && TREE_CODE (arg3) == THROW_EXPR)
3037	{
3038	  if (!VOID_TYPE_P (arg2_type))
3039	    arg2 = force_rvalue (arg2);
3040	  arg2_type = TREE_TYPE (arg2);
3041	  result_type = arg2_type;
3042	}
3043      else if (VOID_TYPE_P (arg2_type) && VOID_TYPE_P (arg3_type))
3044	result_type = void_type_node;
3045      else
3046	{
3047	  error ("`%E' has type `void' and is not a throw-expression",
3048		    VOID_TYPE_P (arg2_type) ? arg2 : arg3);
3049	  return error_mark_node;
3050	}
3051
3052      lvalue_p = false;
3053      goto valid_operands;
3054    }
3055  /* [expr.cond]
3056
3057     Otherwise, if the second and third operand have different types,
3058     and either has (possibly cv-qualified) class type, an attempt is
3059     made to convert each of those operands to the type of the other.  */
3060  else if (!same_type_p (arg2_type, arg3_type)
3061	   && (CLASS_TYPE_P (arg2_type) || CLASS_TYPE_P (arg3_type)))
3062    {
3063      tree conv2 = conditional_conversion (arg2, arg3);
3064      tree conv3 = conditional_conversion (arg3, arg2);
3065
3066      /* [expr.cond]
3067
3068	 If both can be converted, or one can be converted but the
3069	 conversion is ambiguous, the program is ill-formed.  If
3070	 neither can be converted, the operands are left unchanged and
3071	 further checking is performed as described below.  If exactly
3072	 one conversion is possible, that conversion is applied to the
3073	 chosen operand and the converted operand is used in place of
3074	 the original operand for the remainder of this section.  */
3075      if ((conv2 && !ICS_BAD_FLAG (conv2)
3076	   && conv3 && !ICS_BAD_FLAG (conv3))
3077	  || (conv2 && TREE_CODE (conv2) == AMBIG_CONV)
3078	  || (conv3 && TREE_CODE (conv3) == AMBIG_CONV))
3079	{
3080	  error ("operands to ?: have different types");
3081	  return error_mark_node;
3082	}
3083      else if (conv2 && !ICS_BAD_FLAG (conv2))
3084	{
3085	  arg2 = convert_like (conv2, arg2);
3086	  arg2 = convert_from_reference (arg2);
3087	  arg2_type = TREE_TYPE (arg2);
3088	}
3089      else if (conv3 && !ICS_BAD_FLAG (conv3))
3090	{
3091	  arg3 = convert_like (conv3, arg3);
3092	  arg3 = convert_from_reference (arg3);
3093	  arg3_type = TREE_TYPE (arg3);
3094	}
3095
3096      /* If, after the conversion, both operands have class type,
3097	 treat the cv-qualification of both operands as if it were the
3098	 union of the cv-qualification of the operands.
3099
3100	 The standard is not clear about what to do in this
3101	 circumstance.  For example, if the first operand has type
3102	 "const X" and the second operand has a user-defined
3103	 conversion to "volatile X", what is the type of the second
3104	 operand after this step?  Making it be "const X" (matching
3105	 the first operand) seems wrong, as that discards the
3106	 qualification without actuall performing a copy.  Leaving it
3107	 as "volatile X" seems wrong as that will result in the
3108	 conditional expression failing altogether, even though,
3109	 according to this step, the one operand could be converted to
3110	 the type of the other.  */
3111      if ((conv2 || conv3)
3112	  && CLASS_TYPE_P (arg2_type)
3113	  && TYPE_QUALS (arg2_type) != TYPE_QUALS (arg3_type))
3114	arg2_type = arg3_type =
3115	  cp_build_qualified_type (arg2_type,
3116				   TYPE_QUALS (arg2_type)
3117				   | TYPE_QUALS (arg3_type));
3118    }
3119
3120  /* [expr.cond]
3121
3122     If the second and third operands are lvalues and have the same
3123     type, the result is of that type and is an lvalue.  */
3124  if (real_lvalue_p (arg2)
3125      && real_lvalue_p (arg3)
3126      && same_type_p (arg2_type, arg3_type))
3127    {
3128      result_type = arg2_type;
3129      goto valid_operands;
3130    }
3131
3132  /* [expr.cond]
3133
3134     Otherwise, the result is an rvalue.  If the second and third
3135     operand do not have the same type, and either has (possibly
3136     cv-qualified) class type, overload resolution is used to
3137     determine the conversions (if any) to be applied to the operands
3138     (_over.match.oper_, _over.built_).  */
3139  lvalue_p = false;
3140  if (!same_type_p (arg2_type, arg3_type)
3141      && (CLASS_TYPE_P (arg2_type) || CLASS_TYPE_P (arg3_type)))
3142    {
3143      tree args[3];
3144      tree conv;
3145      bool any_viable_p;
3146
3147      /* Rearrange the arguments so that add_builtin_candidate only has
3148	 to know about two args.  In build_builtin_candidates, the
3149	 arguments are unscrambled.  */
3150      args[0] = arg2;
3151      args[1] = arg3;
3152      args[2] = arg1;
3153      add_builtin_candidates (&candidates,
3154			      COND_EXPR,
3155			      NOP_EXPR,
3156			      ansi_opname (COND_EXPR),
3157			      args,
3158			      LOOKUP_NORMAL);
3159
3160      /* [expr.cond]
3161
3162	 If the overload resolution fails, the program is
3163	 ill-formed.  */
3164      candidates = splice_viable (candidates, pedantic, &any_viable_p);
3165      if (!any_viable_p)
3166	{
3167	  op_error (COND_EXPR, NOP_EXPR, arg1, arg2, arg3, "no match");
3168	  print_z_candidates (candidates);
3169	  return error_mark_node;
3170	}
3171      cand = tourney (candidates);
3172      if (!cand)
3173	{
3174	  op_error (COND_EXPR, NOP_EXPR, arg1, arg2, arg3, "no match");
3175	  print_z_candidates (candidates);
3176	  return error_mark_node;
3177	}
3178
3179      /* [expr.cond]
3180
3181	 Otherwise, the conversions thus determined are applied, and
3182	 the converted operands are used in place of the original
3183	 operands for the remainder of this section.  */
3184      conv = TREE_VEC_ELT (cand->convs, 0);
3185      arg1 = convert_like (conv, arg1);
3186      conv = TREE_VEC_ELT (cand->convs, 1);
3187      arg2 = convert_like (conv, arg2);
3188      conv = TREE_VEC_ELT (cand->convs, 2);
3189      arg3 = convert_like (conv, arg3);
3190    }
3191
3192  /* [expr.cond]
3193
3194     Lvalue-to-rvalue (_conv.lval_), array-to-pointer (_conv.array_),
3195     and function-to-pointer (_conv.func_) standard conversions are
3196     performed on the second and third operands.
3197
3198     We need to force the lvalue-to-rvalue conversion here for class types,
3199     so we get TARGET_EXPRs; trying to deal with a COND_EXPR of class rvalues
3200     that isn't wrapped with a TARGET_EXPR plays havoc with exception
3201     regions.  */
3202
3203  arg2 = force_rvalue (arg2);
3204  if (!CLASS_TYPE_P (arg2_type))
3205    arg2_type = TREE_TYPE (arg2);
3206
3207  arg3 = force_rvalue (arg3);
3208  if (!CLASS_TYPE_P (arg2_type))
3209    arg3_type = TREE_TYPE (arg3);
3210
3211  if (arg2 == error_mark_node || arg3 == error_mark_node)
3212    return error_mark_node;
3213
3214  /* [expr.cond]
3215
3216     After those conversions, one of the following shall hold:
3217
3218     --The second and third operands have the same type; the result  is  of
3219       that type.  */
3220  if (same_type_p (arg2_type, arg3_type))
3221    result_type = arg2_type;
3222  /* [expr.cond]
3223
3224     --The second and third operands have arithmetic or enumeration
3225       type; the usual arithmetic conversions are performed to bring
3226       them to a common type, and the result is of that type.  */
3227  else if ((ARITHMETIC_TYPE_P (arg2_type)
3228	    || TREE_CODE (arg2_type) == ENUMERAL_TYPE)
3229	   && (ARITHMETIC_TYPE_P (arg3_type)
3230	       || TREE_CODE (arg3_type) == ENUMERAL_TYPE))
3231    {
3232      /* In this case, there is always a common type.  */
3233      result_type = type_after_usual_arithmetic_conversions (arg2_type,
3234							     arg3_type);
3235
3236      if (TREE_CODE (arg2_type) == ENUMERAL_TYPE
3237          && TREE_CODE (arg3_type) == ENUMERAL_TYPE)
3238         warning ("enumeral mismatch in conditional expression: `%T' vs `%T'",
3239                   arg2_type, arg3_type);
3240      else if (extra_warnings
3241               && ((TREE_CODE (arg2_type) == ENUMERAL_TYPE
3242                    && !same_type_p (arg3_type, type_promotes_to (arg2_type)))
3243                   || (TREE_CODE (arg3_type) == ENUMERAL_TYPE
3244                       && !same_type_p (arg2_type, type_promotes_to (arg3_type)))))
3245        warning ("enumeral and non-enumeral type in conditional expression");
3246
3247      arg2 = perform_implicit_conversion (result_type, arg2);
3248      arg3 = perform_implicit_conversion (result_type, arg3);
3249    }
3250  /* [expr.cond]
3251
3252     --The second and third operands have pointer type, or one has
3253       pointer type and the other is a null pointer constant; pointer
3254       conversions (_conv.ptr_) and qualification conversions
3255       (_conv.qual_) are performed to bring them to their composite
3256       pointer type (_expr.rel_).  The result is of the composite
3257       pointer type.
3258
3259     --The second and third operands have pointer to member type, or
3260       one has pointer to member type and the other is a null pointer
3261       constant; pointer to member conversions (_conv.mem_) and
3262       qualification conversions (_conv.qual_) are performed to bring
3263       them to a common type, whose cv-qualification shall match the
3264       cv-qualification of either the second or the third operand.
3265       The result is of the common type.  */
3266  else if ((null_ptr_cst_p (arg2)
3267	    && (TYPE_PTR_P (arg3_type) || TYPE_PTR_TO_MEMBER_P (arg3_type)))
3268	   || (null_ptr_cst_p (arg3)
3269	       && (TYPE_PTR_P (arg2_type) || TYPE_PTR_TO_MEMBER_P (arg2_type)))
3270	   || (TYPE_PTR_P (arg2_type) && TYPE_PTR_P (arg3_type))
3271	   || (TYPE_PTRMEM_P (arg2_type) && TYPE_PTRMEM_P (arg3_type))
3272	   || (TYPE_PTRMEMFUNC_P (arg2_type) && TYPE_PTRMEMFUNC_P (arg3_type)))
3273    {
3274      result_type = composite_pointer_type (arg2_type, arg3_type, arg2,
3275					    arg3, "conditional expression");
3276      if (result_type == error_mark_node)
3277	return error_mark_node;
3278      arg2 = perform_implicit_conversion (result_type, arg2);
3279      arg3 = perform_implicit_conversion (result_type, arg3);
3280    }
3281
3282  if (!result_type)
3283    {
3284      error ("operands to ?: have different types");
3285      return error_mark_node;
3286    }
3287
3288 valid_operands:
3289  result = fold_if_not_in_template (build (COND_EXPR, result_type,
3290					   arg1, arg2, arg3));
3291  /* We can't use result_type below, as fold might have returned a
3292     throw_expr.  */
3293
3294  /* Expand both sides into the same slot, hopefully the target of the
3295     ?: expression.  We used to check for TARGET_EXPRs here, but now we
3296     sometimes wrap them in NOP_EXPRs so the test would fail.  */
3297  if (!lvalue_p && CLASS_TYPE_P (TREE_TYPE (result)))
3298    result = get_target_expr (result);
3299
3300  /* If this expression is an rvalue, but might be mistaken for an
3301     lvalue, we must add a NON_LVALUE_EXPR.  */
3302  if (!lvalue_p && real_lvalue_p (result))
3303    result = build1 (NON_LVALUE_EXPR, TREE_TYPE (result), result);
3304
3305  return result;
3306}
3307
3308/* OPERAND is an operand to an expression.  Perform necessary steps
3309   required before using it.  If OPERAND is NULL_TREE, NULL_TREE is
3310   returned.  */
3311
3312static tree
3313prep_operand (tree operand)
3314{
3315  if (operand)
3316    {
3317      operand = convert_from_reference (operand);
3318      if (CLASS_TYPE_P (TREE_TYPE (operand))
3319	  && CLASSTYPE_TEMPLATE_INSTANTIATION (TREE_TYPE (operand)))
3320	/* Make sure the template type is instantiated now.  */
3321	instantiate_class_template (TYPE_MAIN_VARIANT (TREE_TYPE (operand)));
3322    }
3323
3324  return operand;
3325}
3326
3327/* Add each of the viable functions in FNS (a FUNCTION_DECL or
3328   OVERLOAD) to the CANDIDATES, returning an updated list of
3329   CANDIDATES.  The ARGS are the arguments provided to the call,
3330   without any implicit object parameter.  The EXPLICIT_TARGS are
3331   explicit template arguments provided.  TEMPLATE_ONLY is true if
3332   only template functions should be considered.  CONVERSION_PATH,
3333   ACCESS_PATH, and FLAGS are as for add_function_candidate.  */
3334
3335static void
3336add_candidates (tree fns, tree args,
3337		tree explicit_targs, bool template_only,
3338		tree conversion_path, tree access_path,
3339		int flags,
3340		struct z_candidate **candidates)
3341{
3342  tree ctype;
3343  tree non_static_args;
3344
3345  ctype = conversion_path ? BINFO_TYPE (conversion_path) : NULL_TREE;
3346  /* Delay creating the implicit this parameter until it is needed.  */
3347  non_static_args = NULL_TREE;
3348
3349  while (fns)
3350    {
3351      tree fn;
3352      tree fn_args;
3353
3354      fn = OVL_CURRENT (fns);
3355      /* Figure out which set of arguments to use.  */
3356      if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
3357	{
3358	  /* If this function is a non-static member, prepend the implicit
3359	     object parameter.  */
3360	  if (!non_static_args)
3361	    non_static_args = tree_cons (NULL_TREE,
3362					 build_this (TREE_VALUE (args)),
3363					 TREE_CHAIN (args));
3364	  fn_args = non_static_args;
3365	}
3366      else
3367	/* Otherwise, just use the list of arguments provided.  */
3368	fn_args = args;
3369
3370      if (TREE_CODE (fn) == TEMPLATE_DECL)
3371	add_template_candidate (candidates,
3372				fn,
3373				ctype,
3374				explicit_targs,
3375				fn_args,
3376				NULL_TREE,
3377				access_path,
3378				conversion_path,
3379				flags,
3380				DEDUCE_CALL);
3381      else if (!template_only)
3382	add_function_candidate (candidates,
3383				fn,
3384				ctype,
3385				fn_args,
3386				access_path,
3387				conversion_path,
3388				flags);
3389      fns = OVL_NEXT (fns);
3390    }
3391}
3392
3393tree
3394build_new_op (enum tree_code code, int flags, tree arg1, tree arg2, tree arg3,
3395	      bool *overloaded_p)
3396{
3397  struct z_candidate *candidates = 0, *cand;
3398  tree arglist, fnname;
3399  tree args[3];
3400  enum tree_code code2 = NOP_EXPR;
3401  tree conv;
3402  bool strict_p;
3403  bool any_viable_p;
3404
3405  if (error_operand_p (arg1)
3406      || error_operand_p (arg2)
3407      || error_operand_p (arg3))
3408    return error_mark_node;
3409
3410  if (code == MODIFY_EXPR)
3411    {
3412      code2 = TREE_CODE (arg3);
3413      arg3 = NULL_TREE;
3414      fnname = ansi_assopname (code2);
3415    }
3416  else
3417    fnname = ansi_opname (code);
3418
3419  arg1 = prep_operand (arg1);
3420
3421  switch (code)
3422    {
3423    case NEW_EXPR:
3424    case VEC_NEW_EXPR:
3425    case VEC_DELETE_EXPR:
3426    case DELETE_EXPR:
3427      /* Use build_op_new_call and build_op_delete_call instead.  */
3428      abort ();
3429
3430    case CALL_EXPR:
3431      return build_object_call (arg1, arg2);
3432
3433    default:
3434      break;
3435    }
3436
3437  arg2 = prep_operand (arg2);
3438  arg3 = prep_operand (arg3);
3439
3440  if (code == COND_EXPR)
3441    {
3442      if (arg2 == NULL_TREE
3443	  || TREE_CODE (TREE_TYPE (arg2)) == VOID_TYPE
3444	  || TREE_CODE (TREE_TYPE (arg3)) == VOID_TYPE
3445	  || (! IS_OVERLOAD_TYPE (TREE_TYPE (arg2))
3446	      && ! IS_OVERLOAD_TYPE (TREE_TYPE (arg3))))
3447	goto builtin;
3448    }
3449  else if (! IS_OVERLOAD_TYPE (TREE_TYPE (arg1))
3450	   && (! arg2 || ! IS_OVERLOAD_TYPE (TREE_TYPE (arg2))))
3451    goto builtin;
3452
3453  if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
3454    arg2 = integer_zero_node;
3455
3456  arglist = NULL_TREE;
3457  if (arg3)
3458    arglist = tree_cons (NULL_TREE, arg3, arglist);
3459  if (arg2)
3460    arglist = tree_cons (NULL_TREE, arg2, arglist);
3461  arglist = tree_cons (NULL_TREE, arg1, arglist);
3462
3463  /* Add namespace-scope operators to the list of functions to
3464     consider.  */
3465  add_candidates (lookup_function_nonclass (fnname, arglist),
3466		  arglist, NULL_TREE, false, NULL_TREE, NULL_TREE,
3467		  flags, &candidates);
3468  /* Add class-member operators to the candidate set.  */
3469  if (CLASS_TYPE_P (TREE_TYPE (arg1)))
3470    {
3471      tree fns;
3472
3473      fns = lookup_fnfields (TYPE_BINFO (TREE_TYPE (arg1)), fnname, 1);
3474      if (fns == error_mark_node)
3475	return fns;
3476      if (fns)
3477	add_candidates (BASELINK_FUNCTIONS (fns), arglist,
3478			NULL_TREE, false,
3479			BASELINK_BINFO (fns),
3480			TYPE_BINFO (TREE_TYPE (arg1)),
3481			flags, &candidates);
3482    }
3483
3484  /* Rearrange the arguments for ?: so that add_builtin_candidate only has
3485     to know about two args; a builtin candidate will always have a first
3486     parameter of type bool.  We'll handle that in
3487     build_builtin_candidate.  */
3488  if (code == COND_EXPR)
3489    {
3490      args[0] = arg2;
3491      args[1] = arg3;
3492      args[2] = arg1;
3493    }
3494  else
3495    {
3496      args[0] = arg1;
3497      args[1] = arg2;
3498      args[2] = NULL_TREE;
3499    }
3500
3501  add_builtin_candidates (&candidates, code, code2, fnname, args, flags);
3502
3503  switch (code)
3504    {
3505    case COMPOUND_EXPR:
3506    case ADDR_EXPR:
3507      /* For these, the built-in candidates set is empty
3508	 [over.match.oper]/3.  We don't want non-strict matches
3509	 because exact matches are always possible with built-in
3510	 operators.  The built-in candidate set for COMPONENT_REF
3511	 would be empty too, but since there are no such built-in
3512	 operators, we accept non-strict matches for them.  */
3513      strict_p = true;
3514      break;
3515
3516    default:
3517      strict_p = pedantic;
3518      break;
3519    }
3520
3521  candidates = splice_viable (candidates, strict_p, &any_viable_p);
3522  if (!any_viable_p)
3523    {
3524      switch (code)
3525	{
3526	case POSTINCREMENT_EXPR:
3527	case POSTDECREMENT_EXPR:
3528	  /* Look for an `operator++ (int)'.  If they didn't have
3529	     one, then we fall back to the old way of doing things.  */
3530	  if (flags & LOOKUP_COMPLAIN)
3531	    pedwarn ("no `%D(int)' declared for postfix `%s', trying prefix operator instead",
3532			fnname,
3533			operator_name_info[code].name);
3534	  if (code == POSTINCREMENT_EXPR)
3535	    code = PREINCREMENT_EXPR;
3536	  else
3537	    code = PREDECREMENT_EXPR;
3538	  return build_new_op (code, flags, arg1, NULL_TREE, NULL_TREE,
3539			       overloaded_p);
3540
3541	  /* The caller will deal with these.  */
3542	case ADDR_EXPR:
3543	case COMPOUND_EXPR:
3544	case COMPONENT_REF:
3545	  return NULL_TREE;
3546
3547	default:
3548	  break;
3549	}
3550      if (flags & LOOKUP_COMPLAIN)
3551	{
3552	  op_error (code, code2, arg1, arg2, arg3, "no match");
3553	  print_z_candidates (candidates);
3554	}
3555      return error_mark_node;
3556    }
3557
3558  cand = tourney (candidates);
3559  if (cand == 0)
3560    {
3561      if (flags & LOOKUP_COMPLAIN)
3562	{
3563	  op_error (code, code2, arg1, arg2, arg3, "ambiguous overload");
3564	  print_z_candidates (candidates);
3565	}
3566      return error_mark_node;
3567    }
3568
3569  if (TREE_CODE (cand->fn) == FUNCTION_DECL)
3570    {
3571      if (overloaded_p)
3572	*overloaded_p = true;
3573
3574      return build_over_call (cand, LOOKUP_NORMAL);
3575    }
3576
3577  /* Check for comparison of different enum types.  */
3578  switch (code)
3579    {
3580    case GT_EXPR:
3581    case LT_EXPR:
3582    case GE_EXPR:
3583    case LE_EXPR:
3584    case EQ_EXPR:
3585    case NE_EXPR:
3586      if (TREE_CODE (TREE_TYPE (arg1)) == ENUMERAL_TYPE
3587	  && TREE_CODE (TREE_TYPE (arg2)) == ENUMERAL_TYPE
3588	  && (TYPE_MAIN_VARIANT (TREE_TYPE (arg1))
3589	      != TYPE_MAIN_VARIANT (TREE_TYPE (arg2))))
3590	{
3591	  warning ("comparison between `%#T' and `%#T'",
3592		      TREE_TYPE (arg1), TREE_TYPE (arg2));
3593	}
3594      break;
3595    default:
3596      break;
3597    }
3598
3599  /* We need to strip any leading REF_BIND so that bitfields don't cause
3600     errors.  This should not remove any important conversions, because
3601     builtins don't apply to class objects directly.  */
3602  conv = TREE_VEC_ELT (cand->convs, 0);
3603  if (TREE_CODE (conv) == REF_BIND)
3604    conv = TREE_OPERAND (conv, 0);
3605  arg1 = convert_like (conv, arg1);
3606  if (arg2)
3607    {
3608      conv = TREE_VEC_ELT (cand->convs, 1);
3609      if (TREE_CODE (conv) == REF_BIND)
3610        conv = TREE_OPERAND (conv, 0);
3611      arg2 = convert_like (conv, arg2);
3612    }
3613  if (arg3)
3614    {
3615      conv = TREE_VEC_ELT (cand->convs, 2);
3616      if (TREE_CODE (conv) == REF_BIND)
3617        conv = TREE_OPERAND (conv, 0);
3618      arg3 = convert_like (conv, arg3);
3619    }
3620
3621builtin:
3622  switch (code)
3623    {
3624    case MODIFY_EXPR:
3625      return build_modify_expr (arg1, code2, arg2);
3626
3627    case INDIRECT_REF:
3628      return build_indirect_ref (arg1, "unary *");
3629
3630    case PLUS_EXPR:
3631    case MINUS_EXPR:
3632    case MULT_EXPR:
3633    case TRUNC_DIV_EXPR:
3634    case GT_EXPR:
3635    case LT_EXPR:
3636    case GE_EXPR:
3637    case LE_EXPR:
3638    case EQ_EXPR:
3639    case NE_EXPR:
3640    case MAX_EXPR:
3641    case MIN_EXPR:
3642    case LSHIFT_EXPR:
3643    case RSHIFT_EXPR:
3644    case TRUNC_MOD_EXPR:
3645    case BIT_AND_EXPR:
3646    case BIT_IOR_EXPR:
3647    case BIT_XOR_EXPR:
3648    case TRUTH_ANDIF_EXPR:
3649    case TRUTH_ORIF_EXPR:
3650      return cp_build_binary_op (code, arg1, arg2);
3651
3652    case CONVERT_EXPR:
3653    case NEGATE_EXPR:
3654    case BIT_NOT_EXPR:
3655    case TRUTH_NOT_EXPR:
3656    case PREINCREMENT_EXPR:
3657    case POSTINCREMENT_EXPR:
3658    case PREDECREMENT_EXPR:
3659    case POSTDECREMENT_EXPR:
3660    case REALPART_EXPR:
3661    case IMAGPART_EXPR:
3662      return build_unary_op (code, arg1, candidates != 0);
3663
3664    case ARRAY_REF:
3665      return build_array_ref (arg1, arg2);
3666
3667    case COND_EXPR:
3668      return build_conditional_expr (arg1, arg2, arg3);
3669
3670    case MEMBER_REF:
3671      return build_m_component_ref
3672	(build_indirect_ref (arg1, NULL), arg2);
3673
3674      /* The caller will deal with these.  */
3675    case ADDR_EXPR:
3676    case COMPONENT_REF:
3677    case COMPOUND_EXPR:
3678      return NULL_TREE;
3679
3680    default:
3681      abort ();
3682      return NULL_TREE;
3683    }
3684}
3685
3686/* Build a call to operator delete.  This has to be handled very specially,
3687   because the restrictions on what signatures match are different from all
3688   other call instances.  For a normal delete, only a delete taking (void *)
3689   or (void *, size_t) is accepted.  For a placement delete, only an exact
3690   match with the placement new is accepted.
3691
3692   CODE is either DELETE_EXPR or VEC_DELETE_EXPR.
3693   ADDR is the pointer to be deleted.
3694   SIZE is the size of the memory block to be deleted.
3695   FLAGS are the usual overloading flags.
3696   PLACEMENT is the corresponding placement new call, or NULL_TREE.  */
3697
3698tree
3699build_op_delete_call (enum tree_code code, tree addr, tree size,
3700                      int flags, tree placement)
3701{
3702  tree fn = NULL_TREE;
3703  tree fns, fnname, argtypes, args, type;
3704  int pass;
3705
3706  if (addr == error_mark_node)
3707    return error_mark_node;
3708
3709  type = strip_array_types (TREE_TYPE (TREE_TYPE (addr)));
3710
3711  fnname = ansi_opname (code);
3712
3713  if (IS_AGGR_TYPE (type) && ! (flags & LOOKUP_GLOBAL))
3714    /* In [class.free]
3715
3716       If the result of the lookup is ambiguous or inaccessible, or if
3717       the lookup selects a placement deallocation function, the
3718       program is ill-formed.
3719
3720       Therefore, we ask lookup_fnfields to complain about ambiguity.  */
3721    {
3722      fns = lookup_fnfields (TYPE_BINFO (type), fnname, 1);
3723      if (fns == error_mark_node)
3724	return error_mark_node;
3725    }
3726  else
3727    fns = NULL_TREE;
3728
3729  if (fns == NULL_TREE)
3730    fns = lookup_name_nonclass (fnname);
3731
3732  if (placement)
3733    {
3734      tree alloc_fn;
3735      tree call_expr;
3736
3737      /* Find the allocation function that is being called.  */
3738      call_expr = placement;
3739      /* Extract the function.  */
3740      alloc_fn = get_callee_fndecl (call_expr);
3741      my_friendly_assert (alloc_fn != NULL_TREE, 20020327);
3742      /* Then the second parm type.  */
3743      argtypes = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (alloc_fn)));
3744      /* Also the second argument.  */
3745      args = TREE_CHAIN (TREE_OPERAND (call_expr, 1));
3746    }
3747  else
3748    {
3749      /* First try it without the size argument.  */
3750      argtypes = void_list_node;
3751      args = NULL_TREE;
3752    }
3753
3754  /* Strip const and volatile from addr.  */
3755  addr = cp_convert (ptr_type_node, addr);
3756
3757  /* We make two tries at finding a matching `operator delete'.  On
3758     the first pass, we look for a one-operator (or placement)
3759     operator delete.  If we're not doing placement delete, then on
3760     the second pass we look for a two-argument delete.  */
3761  for (pass = 0; pass < (placement ? 1 : 2); ++pass)
3762    {
3763      /* Go through the `operator delete' functions looking for one
3764	 with a matching type.  */
3765      for (fn = BASELINK_P (fns) ? BASELINK_FUNCTIONS (fns) : fns;
3766	   fn;
3767	   fn = OVL_NEXT (fn))
3768	{
3769	  tree t;
3770
3771	  /* The first argument must be "void *".  */
3772	  t = TYPE_ARG_TYPES (TREE_TYPE (OVL_CURRENT (fn)));
3773	  if (!same_type_p (TREE_VALUE (t), ptr_type_node))
3774	    continue;
3775	  t = TREE_CHAIN (t);
3776	  /* On the first pass, check the rest of the arguments.  */
3777	  if (pass == 0)
3778	    {
3779	      tree a = argtypes;
3780	      while (a && t)
3781		{
3782		  if (!same_type_p (TREE_VALUE (a), TREE_VALUE (t)))
3783		    break;
3784		  a = TREE_CHAIN (a);
3785		  t = TREE_CHAIN (t);
3786		}
3787	      if (!a && !t)
3788		break;
3789	    }
3790	  /* On the second pass, the second argument must be
3791	     "size_t".  */
3792	  else if (pass == 1
3793		   && same_type_p (TREE_VALUE (t), sizetype)
3794		   && TREE_CHAIN (t) == void_list_node)
3795	    break;
3796	}
3797
3798      /* If we found a match, we're done.  */
3799      if (fn)
3800	break;
3801    }
3802
3803  /* If we have a matching function, call it.  */
3804  if (fn)
3805    {
3806      /* Make sure we have the actual function, and not an
3807	 OVERLOAD.  */
3808      fn = OVL_CURRENT (fn);
3809
3810      /* If the FN is a member function, make sure that it is
3811	 accessible.  */
3812      if (DECL_CLASS_SCOPE_P (fn))
3813	perform_or_defer_access_check (TYPE_BINFO (type), fn);
3814
3815      if (pass == 0)
3816	args = tree_cons (NULL_TREE, addr, args);
3817      else
3818	args = tree_cons (NULL_TREE, addr,
3819			  build_tree_list (NULL_TREE, size));
3820
3821      if (placement)
3822	{
3823	  /* The placement args might not be suitable for overload
3824	     resolution at this point, so build the call directly.  */
3825	  mark_used (fn);
3826	  return build_cxx_call (fn, args, args);
3827	}
3828      else
3829	return build_function_call (fn, args);
3830    }
3831
3832  /* If we are doing placement delete we do nothing if we don't find a
3833     matching op delete.  */
3834  if (placement)
3835    return NULL_TREE;
3836
3837  error ("no suitable `operator %s' for `%T'",
3838	 operator_name_info[(int)code].name, type);
3839  return error_mark_node;
3840}
3841
3842/* If the current scope isn't allowed to access DECL along
3843   BASETYPE_PATH, give an error.  The most derived class in
3844   BASETYPE_PATH is the one used to qualify DECL.  */
3845
3846bool
3847enforce_access (tree basetype_path, tree decl)
3848{
3849  my_friendly_assert (TREE_CODE (basetype_path) == TREE_VEC, 20030624);
3850
3851  if (!accessible_p (basetype_path, decl))
3852    {
3853      if (TREE_PRIVATE (decl))
3854	cp_error_at ("`%+#D' is private", decl);
3855      else if (TREE_PROTECTED (decl))
3856	cp_error_at ("`%+#D' is protected", decl);
3857      else
3858	cp_error_at ("`%+#D' is inaccessible", decl);
3859      error ("within this context");
3860      return false;
3861    }
3862
3863  return true;
3864}
3865
3866/* Check that a callable constructor to initialize a temporary of
3867   TYPE from an EXPR exists.  */
3868
3869static void
3870check_constructor_callable (tree type, tree expr)
3871{
3872  build_special_member_call (NULL_TREE,
3873			     complete_ctor_identifier,
3874			     build_tree_list (NULL_TREE, expr),
3875			     TYPE_BINFO (type),
3876			     LOOKUP_NORMAL | LOOKUP_ONLYCONVERTING
3877			     | LOOKUP_NO_CONVERSION
3878			     | LOOKUP_CONSTRUCTOR_CALLABLE);
3879}
3880
3881/* Initialize a temporary of type TYPE with EXPR.  The FLAGS are a
3882   bitwise or of LOOKUP_* values.  If any errors are warnings are
3883   generated, set *DIAGNOSTIC_FN to "error" or "warning",
3884   respectively.  If no diagnostics are generated, set *DIAGNOSTIC_FN
3885   to NULL.  */
3886
3887static tree
3888build_temp (tree expr, tree type, int flags,
3889	    void (**diagnostic_fn)(const char *, ...))
3890{
3891  int savew, savee;
3892
3893  savew = warningcount, savee = errorcount;
3894  expr = build_special_member_call (NULL_TREE,
3895				    complete_ctor_identifier,
3896				    build_tree_list (NULL_TREE, expr),
3897				    TYPE_BINFO (type),
3898				    flags);
3899  if (warningcount > savew)
3900    *diagnostic_fn = warning;
3901  else if (errorcount > savee)
3902    *diagnostic_fn = error;
3903  else
3904    *diagnostic_fn = NULL;
3905  return expr;
3906}
3907
3908
3909/* Perform the conversions in CONVS on the expression EXPR.  FN and
3910   ARGNUM are used for diagnostics.  ARGNUM is zero based, -1
3911   indicates the `this' argument of a method.  INNER is nonzero when
3912   being called to continue a conversion chain. It is negative when a
3913   reference binding will be applied, positive otherwise.  If
3914   ISSUE_CONVERSION_WARNINGS is true, warnings about suspicious
3915   conversions will be emitted if appropriate.  */
3916
3917static tree
3918convert_like_real (tree convs, tree expr, tree fn, int argnum, int inner,
3919		   bool issue_conversion_warnings)
3920{
3921  tree totype = TREE_TYPE (convs);
3922  void (*diagnostic_fn)(const char *, ...);
3923
3924  if (ICS_BAD_FLAG (convs)
3925      && TREE_CODE (convs) != USER_CONV
3926      && TREE_CODE (convs) != AMBIG_CONV
3927      && TREE_CODE (convs) != REF_BIND)
3928    {
3929      tree t = convs;
3930      for (; t; t = TREE_OPERAND (t, 0))
3931	{
3932	  if (TREE_CODE (t) == USER_CONV || !ICS_BAD_FLAG (t))
3933	    {
3934	      expr = convert_like_real (t, expr, fn, argnum, 1,
3935					/*issue_conversion_warnings=*/false);
3936	      break;
3937	    }
3938	  else if (TREE_CODE (t) == AMBIG_CONV)
3939	    return convert_like_real (t, expr, fn, argnum, 1,
3940				      /*issue_conversion_warnings=*/false);
3941	  else if (TREE_CODE (t) == IDENTITY_CONV)
3942	    break;
3943	}
3944      pedwarn ("invalid conversion from `%T' to `%T'", TREE_TYPE (expr), totype);
3945      if (fn)
3946	pedwarn ("  initializing argument %P of `%D'", argnum, fn);
3947      return cp_convert (totype, expr);
3948    }
3949
3950  if (issue_conversion_warnings)
3951    expr = dubious_conversion_warnings
3952             (totype, expr, "converting", fn, argnum);
3953  switch (TREE_CODE (convs))
3954    {
3955    case USER_CONV:
3956      {
3957	struct z_candidate *cand = USER_CONV_CAND (convs);
3958	tree convfn = cand->fn;
3959	tree args;
3960
3961	if (DECL_CONSTRUCTOR_P (convfn))
3962	  {
3963	    tree t = build_int_2 (0, 0);
3964	    TREE_TYPE (t) = build_pointer_type (DECL_CONTEXT (convfn));
3965
3966	    args = build_tree_list (NULL_TREE, expr);
3967	    if (DECL_HAS_IN_CHARGE_PARM_P (convfn)
3968		|| DECL_HAS_VTT_PARM_P (convfn))
3969	      /* We should never try to call the abstract or base constructor
3970		 from here.  */
3971	      abort ();
3972	    args = tree_cons (NULL_TREE, t, args);
3973	  }
3974	else
3975	  args = build_this (expr);
3976	expr = build_over_call (cand, LOOKUP_NORMAL);
3977
3978	/* If this is a constructor or a function returning an aggr type,
3979	   we need to build up a TARGET_EXPR.  */
3980	if (DECL_CONSTRUCTOR_P (convfn))
3981	  expr = build_cplus_new (totype, expr);
3982
3983	/* The result of the call is then used to direct-initialize the object
3984	   that is the destination of the copy-initialization.  [dcl.init]
3985
3986	   Note that this step is not reflected in the conversion sequence;
3987	   it affects the semantics when we actually perform the
3988	   conversion, but is not considered during overload resolution.
3989
3990	   If the target is a class, that means call a ctor.  */
3991	if (IS_AGGR_TYPE (totype)
3992	    && (inner >= 0 || !lvalue_p (expr)))
3993	  {
3994	    expr = (build_temp
3995		    (expr, totype,
3996		     /* Core issue 84, now a DR, says that we don't
3997			allow UDCs for these args (which deliberately
3998			breaks copy-init of an auto_ptr<Base> from an
3999			auto_ptr<Derived>).  */
4000		     LOOKUP_NORMAL|LOOKUP_ONLYCONVERTING|LOOKUP_NO_CONVERSION,
4001		     &diagnostic_fn));
4002
4003	    if (diagnostic_fn)
4004	      {
4005		if (fn)
4006		  diagnostic_fn
4007		    ("  initializing argument %P of `%D' from result of `%D'",
4008		     argnum, fn, convfn);
4009		else
4010		 diagnostic_fn
4011		   ("  initializing temporary from result of `%D'",  convfn);
4012	      }
4013	    expr = build_cplus_new (totype, expr);
4014	  }
4015	return expr;
4016      }
4017    case IDENTITY_CONV:
4018      if (type_unknown_p (expr))
4019	expr = instantiate_type (totype, expr, tf_error | tf_warning);
4020      /* Convert a non-array constant variable to its underlying
4021	 value, unless we are about to bind it to a reference, in
4022	 which case we need to leave it as an lvalue.  */
4023      if (inner >= 0
4024	  && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
4025	expr = decl_constant_value (expr);
4026      if (CHECK_COPY_CONSTRUCTOR_P (convs))
4027	check_constructor_callable (totype, expr);
4028
4029      return expr;
4030    case AMBIG_CONV:
4031      /* Call build_user_type_conversion again for the error.  */
4032      return build_user_type_conversion
4033	(totype, TREE_OPERAND (convs, 0), LOOKUP_NORMAL);
4034
4035    default:
4036      break;
4037    };
4038
4039  expr = convert_like_real (TREE_OPERAND (convs, 0), expr, fn, argnum,
4040                            TREE_CODE (convs) == REF_BIND ? -1 : 1,
4041			    /*issue_conversion_warnings=*/false);
4042  if (expr == error_mark_node)
4043    return error_mark_node;
4044
4045  switch (TREE_CODE (convs))
4046    {
4047    case RVALUE_CONV:
4048      if (! IS_AGGR_TYPE (totype))
4049	return expr;
4050      /* Else fall through.  */
4051    case BASE_CONV:
4052      if (TREE_CODE (convs) == BASE_CONV && !NEED_TEMPORARY_P (convs))
4053	{
4054	  /* We are going to bind a reference directly to a base-class
4055	     subobject of EXPR.  */
4056	  if (CHECK_COPY_CONSTRUCTOR_P (convs))
4057	    check_constructor_callable (TREE_TYPE (expr), expr);
4058	  /* Build an expression for `*((base*) &expr)'.  */
4059	  expr = build_unary_op (ADDR_EXPR, expr, 0);
4060	  expr = perform_implicit_conversion (build_pointer_type (totype),
4061					      expr);
4062	  expr = build_indirect_ref (expr, "implicit conversion");
4063	  return expr;
4064	}
4065
4066      /* Copy-initialization where the cv-unqualified version of the source
4067	 type is the same class as, or a derived class of, the class of the
4068	 destination [is treated as direct-initialization].  [dcl.init] */
4069      expr = build_temp (expr, totype, LOOKUP_NORMAL|LOOKUP_ONLYCONVERTING,
4070			 &diagnostic_fn);
4071      if (diagnostic_fn && fn)
4072	diagnostic_fn ("  initializing argument %P of `%D'", argnum, fn);
4073      return build_cplus_new (totype, expr);
4074
4075    case REF_BIND:
4076      {
4077	tree ref_type = totype;
4078
4079	/* If necessary, create a temporary.  */
4080	if (NEED_TEMPORARY_P (convs) || !lvalue_p (expr))
4081	  {
4082	    tree type = TREE_TYPE (TREE_OPERAND (convs, 0));
4083
4084	    if (!CP_TYPE_CONST_NON_VOLATILE_P (TREE_TYPE (ref_type)))
4085	      {
4086		/* If the reference is volatile or non-const, we
4087		   cannot create a temporary.  */
4088		cp_lvalue_kind lvalue = real_lvalue_p (expr);
4089
4090		if (lvalue & clk_bitfield)
4091		  error ("cannot bind bitfield `%E' to `%T'",
4092			 expr, ref_type);
4093		else if (lvalue & clk_packed)
4094		  error ("cannot bind packed field `%E' to `%T'",
4095			 expr, ref_type);
4096		else
4097		  error ("cannot bind rvalue `%E' to `%T'", expr, ref_type);
4098		return error_mark_node;
4099	      }
4100	    expr = build_target_expr_with_type (expr, type);
4101	  }
4102
4103	/* Take the address of the thing to which we will bind the
4104	   reference.  */
4105	expr = build_unary_op (ADDR_EXPR, expr, 1);
4106	if (expr == error_mark_node)
4107	  return error_mark_node;
4108
4109	/* Convert it to a pointer to the type referred to by the
4110	   reference.  This will adjust the pointer if a derived to
4111	   base conversion is being performed.  */
4112	expr = cp_convert (build_pointer_type (TREE_TYPE (ref_type)),
4113			   expr);
4114	/* Convert the pointer to the desired reference type.  */
4115	return build_nop (ref_type, expr);
4116      }
4117
4118    case LVALUE_CONV:
4119      return decay_conversion (expr);
4120
4121    case QUAL_CONV:
4122      /* Warn about deprecated conversion if appropriate.  */
4123      string_conv_p (totype, expr, 1);
4124      break;
4125
4126    default:
4127      break;
4128    }
4129  return ocp_convert (totype, expr, CONV_IMPLICIT,
4130		      LOOKUP_NORMAL|LOOKUP_NO_CONVERSION);
4131}
4132
4133/* Build a call to __builtin_trap.  */
4134
4135static tree
4136call_builtin_trap (void)
4137{
4138  tree fn = IDENTIFIER_GLOBAL_VALUE (get_identifier ("__builtin_trap"));
4139
4140  my_friendly_assert (fn != NULL, 20030927);
4141  fn = build_call (fn, NULL_TREE);
4142  return fn;
4143}
4144
4145/* ARG is being passed to a varargs function.  Perform any conversions
4146   required.  Return the converted value.  */
4147
4148tree
4149convert_arg_to_ellipsis (tree arg)
4150{
4151  /* [expr.call]
4152
4153     The lvalue-to-rvalue, array-to-pointer, and function-to-pointer
4154     standard conversions are performed.  */
4155  arg = decay_conversion (arg);
4156  /* [expr.call]
4157
4158     If the argument has integral or enumeration type that is subject
4159     to the integral promotions (_conv.prom_), or a floating point
4160     type that is subject to the floating point promotion
4161     (_conv.fpprom_), the value of the argument is converted to the
4162     promoted type before the call.  */
4163  if (TREE_CODE (TREE_TYPE (arg)) == REAL_TYPE
4164      && (TYPE_PRECISION (TREE_TYPE (arg))
4165	  < TYPE_PRECISION (double_type_node)))
4166    arg = convert_to_real (double_type_node, arg);
4167  else if (INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (arg)))
4168    arg = perform_integral_promotions (arg);
4169
4170  arg = require_complete_type (arg);
4171
4172  if (arg != error_mark_node
4173      && !pod_type_p (TREE_TYPE (arg)))
4174    {
4175      /* Undefined behavior [expr.call] 5.2.2/7.  We used to just warn
4176	 here and do a bitwise copy, but now cp_expr_size will abort if we
4177	 try to do that.
4178	 If the call appears in the context of a sizeof expression,
4179	 there is no need to emit a warning, since the expression won't be
4180	 evaluated. We keep the builtin_trap just as a safety check.  */
4181      if (!skip_evaluation)
4182	warning ("cannot pass objects of non-POD type `%#T' through `...'; "
4183	         "call will abort at runtime", TREE_TYPE (arg));
4184      arg = call_builtin_trap ();
4185      arg = build (COMPOUND_EXPR, integer_type_node, arg,
4186		   integer_zero_node);
4187    }
4188
4189  return arg;
4190}
4191
4192/* va_arg (EXPR, TYPE) is a builtin. Make sure it is not abused.  */
4193
4194tree
4195build_x_va_arg (tree expr, tree type)
4196{
4197  if (processing_template_decl)
4198    return build_min (VA_ARG_EXPR, type, expr);
4199
4200  type = complete_type_or_else (type, NULL_TREE);
4201
4202  if (expr == error_mark_node || !type)
4203    return error_mark_node;
4204
4205  if (! pod_type_p (type))
4206    {
4207      /* Undefined behavior [expr.call] 5.2.2/7.  */
4208      warning ("cannot receive objects of non-POD type `%#T' through `...'; \
4209call will abort at runtime",
4210	       type);
4211      expr = convert (build_pointer_type (type), null_node);
4212      expr = build (COMPOUND_EXPR, TREE_TYPE (expr),
4213		    call_builtin_trap (), expr);
4214      expr = build_indirect_ref (expr, NULL);
4215      return expr;
4216    }
4217
4218  return build_va_arg (expr, type);
4219}
4220
4221/* TYPE has been given to va_arg.  Apply the default conversions which
4222   would have happened when passed via ellipsis.  Return the promoted
4223   type, or the passed type if there is no change.  */
4224
4225tree
4226cxx_type_promotes_to (tree type)
4227{
4228  tree promote;
4229
4230  /* Perform the array-to-pointer and function-to-pointer
4231     conversions.  */
4232  type = type_decays_to (type);
4233
4234  promote = type_promotes_to (type);
4235  if (same_type_p (type, promote))
4236    promote = type;
4237
4238  return promote;
4239}
4240
4241/* ARG is a default argument expression being passed to a parameter of
4242   the indicated TYPE, which is a parameter to FN.  Do any required
4243   conversions.  Return the converted value.  */
4244
4245tree
4246convert_default_arg (tree type, tree arg, tree fn, int parmnum)
4247{
4248  /* If the ARG is an unparsed default argument expression, the
4249     conversion cannot be performed.  */
4250  if (TREE_CODE (arg) == DEFAULT_ARG)
4251    {
4252      error ("the default argument for parameter %d of `%D' has "
4253	     "not yet been parsed",
4254	     parmnum, fn);
4255      return error_mark_node;
4256    }
4257
4258  if (fn && DECL_TEMPLATE_INFO (fn))
4259    arg = tsubst_default_argument (fn, type, arg);
4260
4261  arg = break_out_target_exprs (arg);
4262
4263  if (TREE_CODE (arg) == CONSTRUCTOR)
4264    {
4265      arg = digest_init (type, arg, 0);
4266      arg = convert_for_initialization (0, type, arg, LOOKUP_NORMAL,
4267					"default argument", fn, parmnum);
4268    }
4269  else
4270    {
4271      /* This could get clobbered by the following call.  */
4272      if (TREE_HAS_CONSTRUCTOR (arg))
4273	arg = copy_node (arg);
4274
4275      arg = convert_for_initialization (0, type, arg, LOOKUP_NORMAL,
4276					"default argument", fn, parmnum);
4277      arg = convert_for_arg_passing (type, arg);
4278    }
4279
4280  return arg;
4281}
4282
4283/* Returns the type which will really be used for passing an argument of
4284   type TYPE.  */
4285
4286tree
4287type_passed_as (tree type)
4288{
4289  /* Pass classes with copy ctors by invisible reference.  */
4290  if (TREE_ADDRESSABLE (type))
4291    type = build_reference_type (type);
4292  else if (PROMOTE_PROTOTYPES
4293	   && INTEGRAL_TYPE_P (type)
4294	   && COMPLETE_TYPE_P (type)
4295	   && INT_CST_LT_UNSIGNED (TYPE_SIZE (type),
4296				   TYPE_SIZE (integer_type_node)))
4297    type = integer_type_node;
4298
4299  return type;
4300}
4301
4302/* Actually perform the appropriate conversion.  */
4303
4304tree
4305convert_for_arg_passing (tree type, tree val)
4306{
4307  if (val == error_mark_node)
4308    ;
4309  /* Pass classes with copy ctors by invisible reference.  */
4310  else if (TREE_ADDRESSABLE (type))
4311    val = build1 (ADDR_EXPR, build_reference_type (type), val);
4312  else if (PROMOTE_PROTOTYPES
4313	   && INTEGRAL_TYPE_P (type)
4314	   && COMPLETE_TYPE_P (type)
4315	   && INT_CST_LT_UNSIGNED (TYPE_SIZE (type),
4316				   TYPE_SIZE (integer_type_node)))
4317    val = perform_integral_promotions (val);
4318  return val;
4319}
4320
4321/* Returns true iff FN is a function with magic varargs, i.e. ones for
4322   which no conversions at all should be done.  This is true for some
4323   builtins which don't act like normal functions.  */
4324
4325static bool
4326magic_varargs_p (tree fn)
4327{
4328  if (DECL_BUILT_IN (fn))
4329    switch (DECL_FUNCTION_CODE (fn))
4330      {
4331      case BUILT_IN_CLASSIFY_TYPE:
4332      case BUILT_IN_CONSTANT_P:
4333      case BUILT_IN_NEXT_ARG:
4334      case BUILT_IN_STDARG_START:
4335      case BUILT_IN_VA_START:
4336	return true;
4337
4338      default:;
4339      }
4340
4341  return false;
4342}
4343
4344/* Subroutine of the various build_*_call functions.  Overload resolution
4345   has chosen a winning candidate CAND; build up a CALL_EXPR accordingly.
4346   ARGS is a TREE_LIST of the unconverted arguments to the call.  FLAGS is a
4347   bitmask of various LOOKUP_* flags which apply to the call itself.  */
4348
4349static tree
4350build_over_call (struct z_candidate *cand, int flags)
4351{
4352  tree fn = cand->fn;
4353  tree args = cand->args;
4354  tree convs = cand->convs;
4355  tree converted_args = NULL_TREE;
4356  tree parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
4357  tree conv, arg, val;
4358  int i = 0;
4359  int is_method = 0;
4360
4361  /* In a template, there is no need to perform all of the work that
4362     is normally done.  We are only interested in the type of the call
4363     expression, i.e., the return type of the function.  Any semantic
4364     errors will be deferred until the template is instantiated.  */
4365  if (processing_template_decl)
4366    {
4367      tree expr;
4368      tree return_type;
4369      return_type = TREE_TYPE (TREE_TYPE (fn));
4370      expr = build (CALL_EXPR, return_type, fn, args);
4371      if (TREE_THIS_VOLATILE (fn) && cfun)
4372	current_function_returns_abnormally = 1;
4373      if (!VOID_TYPE_P (return_type))
4374	require_complete_type (return_type);
4375      return convert_from_reference (expr);
4376    }
4377
4378  /* Give any warnings we noticed during overload resolution.  */
4379  if (cand->warnings)
4380    for (val = cand->warnings; val; val = TREE_CHAIN (val))
4381      joust (cand, WRAPPER_ZC (TREE_VALUE (val)), 1);
4382
4383  if (DECL_FUNCTION_MEMBER_P (fn))
4384    {
4385      /* If FN is a template function, two cases must be considered.
4386	 For example:
4387
4388	   struct A {
4389	     protected:
4390	       template <class T> void f();
4391	   };
4392	   template <class T> struct B {
4393	     protected:
4394	       void g();
4395	   };
4396	   struct C : A, B<int> {
4397	     using A::f;	// #1
4398	     using B<int>::g;	// #2
4399	   };
4400
4401	 In case #1 where `A::f' is a member template, DECL_ACCESS is
4402	 recorded in the primary template but not in its specialization.
4403	 We check access of FN using its primary template.
4404
4405	 In case #2, where `B<int>::g' has a DECL_TEMPLATE_INFO simply
4406	 because it is a member of class template B, DECL_ACCESS is
4407	 recorded in the specialization `B<int>::g'.  We cannot use its
4408	 primary template because `B<T>::g' and `B<int>::g' may have
4409	 different access.  */
4410      if (DECL_TEMPLATE_INFO (fn)
4411	  && is_member_template (DECL_TI_TEMPLATE (fn)))
4412	perform_or_defer_access_check (cand->access_path,
4413				       DECL_TI_TEMPLATE (fn));
4414      else
4415	perform_or_defer_access_check (cand->access_path, fn);
4416    }
4417
4418  if (args && TREE_CODE (args) != TREE_LIST)
4419    args = build_tree_list (NULL_TREE, args);
4420  arg = args;
4421
4422  /* The implicit parameters to a constructor are not considered by overload
4423     resolution, and must be of the proper type.  */
4424  if (DECL_CONSTRUCTOR_P (fn))
4425    {
4426      converted_args = tree_cons (NULL_TREE, TREE_VALUE (arg), converted_args);
4427      arg = TREE_CHAIN (arg);
4428      parm = TREE_CHAIN (parm);
4429      if (DECL_HAS_IN_CHARGE_PARM_P (fn))
4430	/* We should never try to call the abstract constructor.  */
4431	abort ();
4432      if (DECL_HAS_VTT_PARM_P (fn))
4433	{
4434	  converted_args = tree_cons
4435	    (NULL_TREE, TREE_VALUE (arg), converted_args);
4436	  arg = TREE_CHAIN (arg);
4437	  parm = TREE_CHAIN (parm);
4438	}
4439    }
4440  /* Bypass access control for 'this' parameter.  */
4441  else if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
4442    {
4443      tree parmtype = TREE_VALUE (parm);
4444      tree argtype = TREE_TYPE (TREE_VALUE (arg));
4445      tree converted_arg;
4446      tree base_binfo;
4447
4448      if (ICS_BAD_FLAG (TREE_VEC_ELT (convs, i)))
4449	pedwarn ("passing `%T' as `this' argument of `%#D' discards qualifiers",
4450		    TREE_TYPE (argtype), fn);
4451
4452      /* [class.mfct.nonstatic]: If a nonstatic member function of a class
4453	 X is called for an object that is not of type X, or of a type
4454	 derived from X, the behavior is undefined.
4455
4456         So we can assume that anything passed as 'this' is non-null, and
4457	 optimize accordingly.  */
4458      my_friendly_assert (TREE_CODE (parmtype) == POINTER_TYPE, 19990811);
4459      /* Convert to the base in which the function was declared.  */
4460      my_friendly_assert (cand->conversion_path != NULL_TREE, 20020730);
4461      converted_arg = build_base_path (PLUS_EXPR,
4462				       TREE_VALUE (arg),
4463				       cand->conversion_path,
4464				       1);
4465      /* Check that the base class is accessible.  */
4466      if (!accessible_base_p (TREE_TYPE (argtype),
4467			      BINFO_TYPE (cand->conversion_path)))
4468	error ("`%T' is not an accessible base of `%T'",
4469	       BINFO_TYPE (cand->conversion_path),
4470	       TREE_TYPE (argtype));
4471      /* If fn was found by a using declaration, the conversion path
4472         will be to the derived class, not the base declaring fn. We
4473         must convert from derived to base.  */
4474      base_binfo = lookup_base (TREE_TYPE (TREE_TYPE (converted_arg)),
4475				TREE_TYPE (parmtype), ba_ignore, NULL);
4476      converted_arg = build_base_path (PLUS_EXPR, converted_arg,
4477				       base_binfo, 1);
4478
4479      converted_args = tree_cons (NULL_TREE, converted_arg, converted_args);
4480      parm = TREE_CHAIN (parm);
4481      arg = TREE_CHAIN (arg);
4482      ++i;
4483      is_method = 1;
4484    }
4485
4486  for (; arg && parm;
4487       parm = TREE_CHAIN (parm), arg = TREE_CHAIN (arg), ++i)
4488    {
4489      tree type = TREE_VALUE (parm);
4490
4491      conv = TREE_VEC_ELT (convs, i);
4492      val = convert_like_with_context
4493	(conv, TREE_VALUE (arg), fn, i - is_method);
4494
4495      val = convert_for_arg_passing (type, val);
4496      converted_args = tree_cons (NULL_TREE, val, converted_args);
4497    }
4498
4499  /* Default arguments */
4500  for (; parm && parm != void_list_node; parm = TREE_CHAIN (parm), i++)
4501    converted_args
4502      = tree_cons (NULL_TREE,
4503		   convert_default_arg (TREE_VALUE (parm),
4504					TREE_PURPOSE (parm),
4505					fn, i - is_method),
4506		   converted_args);
4507
4508  /* Ellipsis */
4509  for (; arg; arg = TREE_CHAIN (arg))
4510    {
4511      tree a = TREE_VALUE (arg);
4512      if (magic_varargs_p (fn))
4513	/* Do no conversions for magic varargs.  */;
4514      else
4515	a = convert_arg_to_ellipsis (a);
4516      converted_args = tree_cons (NULL_TREE, a, converted_args);
4517    }
4518
4519  converted_args = nreverse (converted_args);
4520
4521  if (warn_format)
4522    check_function_format (NULL, TYPE_ATTRIBUTES (TREE_TYPE (fn)),
4523			   converted_args);
4524
4525  /* Avoid actually calling copy constructors and copy assignment operators,
4526     if possible.  */
4527
4528  if (! flag_elide_constructors)
4529    /* Do things the hard way.  */;
4530  else if (TREE_VEC_LENGTH (convs) == 1
4531	   && DECL_COPY_CONSTRUCTOR_P (fn))
4532    {
4533      tree targ;
4534      arg = skip_artificial_parms_for (fn, converted_args);
4535      arg = TREE_VALUE (arg);
4536
4537      /* Pull out the real argument, disregarding const-correctness.  */
4538      targ = arg;
4539      while (TREE_CODE (targ) == NOP_EXPR
4540	     || TREE_CODE (targ) == NON_LVALUE_EXPR
4541	     || TREE_CODE (targ) == CONVERT_EXPR)
4542	targ = TREE_OPERAND (targ, 0);
4543      if (TREE_CODE (targ) == ADDR_EXPR)
4544	{
4545	  targ = TREE_OPERAND (targ, 0);
4546	  if (!same_type_ignoring_top_level_qualifiers_p
4547	      (TREE_TYPE (TREE_TYPE (arg)), TREE_TYPE (targ)))
4548	    targ = NULL_TREE;
4549	}
4550      else
4551	targ = NULL_TREE;
4552
4553      if (targ)
4554	arg = targ;
4555      else
4556	arg = build_indirect_ref (arg, 0);
4557
4558      /* [class.copy]: the copy constructor is implicitly defined even if
4559	 the implementation elided its use.  */
4560      if (TYPE_HAS_COMPLEX_INIT_REF (DECL_CONTEXT (fn)))
4561	mark_used (fn);
4562
4563      /* If we're creating a temp and we already have one, don't create a
4564         new one.  If we're not creating a temp but we get one, use
4565         INIT_EXPR to collapse the temp into our target.  Otherwise, if the
4566         ctor is trivial, do a bitwise copy with a simple TARGET_EXPR for a
4567         temp or an INIT_EXPR otherwise.  */
4568      if (integer_zerop (TREE_VALUE (args)))
4569	{
4570	  if (TREE_CODE (arg) == TARGET_EXPR)
4571	    return arg;
4572	  else if (TYPE_HAS_TRIVIAL_INIT_REF (DECL_CONTEXT (fn)))
4573	    return build_target_expr_with_type (arg, DECL_CONTEXT (fn));
4574	}
4575      else if (TREE_CODE (arg) == TARGET_EXPR
4576	       || TYPE_HAS_TRIVIAL_INIT_REF (DECL_CONTEXT (fn)))
4577	{
4578	  tree to = stabilize_reference
4579	    (build_indirect_ref (TREE_VALUE (args), 0));
4580
4581	  val = build (INIT_EXPR, DECL_CONTEXT (fn), to, arg);
4582	  return val;
4583	}
4584    }
4585  else if (DECL_OVERLOADED_OPERATOR_P (fn) == NOP_EXPR
4586	   && copy_fn_p (fn)
4587	   && TYPE_HAS_TRIVIAL_ASSIGN_REF (DECL_CONTEXT (fn)))
4588    {
4589      tree to = stabilize_reference
4590	(build_indirect_ref (TREE_VALUE (converted_args), 0));
4591      tree type = TREE_TYPE (to);
4592      tree as_base = CLASSTYPE_AS_BASE (type);
4593
4594      arg = build_indirect_ref (TREE_VALUE (TREE_CHAIN (converted_args)), 0);
4595      if (tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (as_base)))
4596	val = build (MODIFY_EXPR, TREE_TYPE (to), to, arg);
4597      else
4598	{
4599	  /* We must only copy the non-tail padding parts. Use
4600	     CLASSTYPE_AS_BASE for the bitwise copy.  */
4601	  tree to_ptr, arg_ptr, to_as_base, arg_as_base, base_ptr_type;
4602	  tree save_to;
4603
4604	  to_ptr = save_expr (build_unary_op (ADDR_EXPR, to, 0));
4605	  arg_ptr = build_unary_op (ADDR_EXPR, arg, 0);
4606
4607	  base_ptr_type = build_pointer_type (as_base);
4608	  to_as_base = build_nop (base_ptr_type, to_ptr);
4609	  to_as_base = build_indirect_ref (to_as_base, 0);
4610	  arg_as_base = build_nop (base_ptr_type, arg_ptr);
4611	  arg_as_base = build_indirect_ref (arg_as_base, 0);
4612
4613	  save_to = build_indirect_ref (to_ptr, 0);
4614
4615	  val = build (MODIFY_EXPR, as_base, to_as_base, arg_as_base);
4616	  val = convert_to_void (val, NULL);
4617	  val = build (COMPOUND_EXPR, type, val, save_to);
4618	  TREE_NO_UNUSED_WARNING (val) = 1;
4619	}
4620
4621      return val;
4622    }
4623
4624  mark_used (fn);
4625
4626  if (DECL_VINDEX (fn) && (flags & LOOKUP_NONVIRTUAL) == 0)
4627    {
4628      tree t, *p = &TREE_VALUE (converted_args);
4629      tree binfo = lookup_base (TREE_TYPE (TREE_TYPE (*p)),
4630				DECL_CONTEXT (fn),
4631				ba_any, NULL);
4632      my_friendly_assert (binfo && binfo != error_mark_node, 20010730);
4633
4634      *p = build_base_path (PLUS_EXPR, *p, binfo, 1);
4635      if (TREE_SIDE_EFFECTS (*p))
4636	*p = save_expr (*p);
4637      t = build_pointer_type (TREE_TYPE (fn));
4638      if (DECL_CONTEXT (fn) && TYPE_JAVA_INTERFACE (DECL_CONTEXT (fn)))
4639	fn = build_java_interface_fn_ref (fn, *p);
4640      else
4641	fn = build_vfn_ref (build_indirect_ref (*p, 0), DECL_VINDEX (fn));
4642      TREE_TYPE (fn) = t;
4643    }
4644  else if (DECL_INLINE (fn))
4645    fn = inline_conversion (fn);
4646  else
4647    fn = build_addr_func (fn);
4648
4649  return build_cxx_call (fn, args, converted_args);
4650}
4651
4652/* Build and return a call to FN, using the the CONVERTED_ARGS.  ARGS
4653   gives the original form of the arguments.  This function performs
4654   no overload resolution, conversion, or other high-level
4655   operations.  */
4656
4657tree
4658build_cxx_call(tree fn, tree args, tree converted_args)
4659{
4660  tree fndecl;
4661
4662  /* Recognize certain built-in functions so we can make tree-codes
4663     other than CALL_EXPR.  We do this when it enables fold-const.c
4664     to do something useful.  */
4665  if (TREE_CODE (fn) == ADDR_EXPR
4666      && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL
4667      && DECL_BUILT_IN (TREE_OPERAND (fn, 0)))
4668    {
4669      tree exp;
4670      exp = expand_tree_builtin (TREE_OPERAND (fn, 0), args, converted_args);
4671      if (exp)
4672	return exp;
4673    }
4674
4675  fn = build_call (fn, converted_args);
4676
4677  /* If this call might throw an exception, note that fact.  */
4678  fndecl = get_callee_fndecl (fn);
4679  if ((!fndecl || !TREE_NOTHROW (fndecl))
4680      && at_function_scope_p ()
4681      && cfun)
4682    cp_function_chain->can_throw = 1;
4683
4684  /* Some built-in function calls will be evaluated at compile-time in
4685     fold ().  */
4686  fn = fold (fn);
4687
4688  if (VOID_TYPE_P (TREE_TYPE (fn)))
4689    return fn;
4690
4691  fn = require_complete_type (fn);
4692  if (fn == error_mark_node)
4693    return error_mark_node;
4694
4695  if (IS_AGGR_TYPE (TREE_TYPE (fn)))
4696    fn = build_cplus_new (TREE_TYPE (fn), fn);
4697  return convert_from_reference (fn);
4698}
4699
4700static GTY(()) tree java_iface_lookup_fn;
4701
4702/* Make an expression which yields the address of the Java interface
4703   method FN.  This is achieved by generating a call to libjava's
4704   _Jv_LookupInterfaceMethodIdx().  */
4705
4706static tree
4707build_java_interface_fn_ref (tree fn, tree instance)
4708{
4709  tree lookup_args, lookup_fn, method, idx;
4710  tree klass_ref, iface, iface_ref;
4711  int i;
4712
4713  if (!java_iface_lookup_fn)
4714    {
4715      tree endlink = build_void_list_node ();
4716      tree t = tree_cons (NULL_TREE, ptr_type_node,
4717			  tree_cons (NULL_TREE, ptr_type_node,
4718				     tree_cons (NULL_TREE, java_int_type_node,
4719						endlink)));
4720      java_iface_lookup_fn
4721	= builtin_function ("_Jv_LookupInterfaceMethodIdx",
4722			    build_function_type (ptr_type_node, t),
4723			    0, NOT_BUILT_IN, NULL, NULL_TREE);
4724    }
4725
4726  /* Look up the pointer to the runtime java.lang.Class object for `instance'.
4727     This is the first entry in the vtable.  */
4728  klass_ref = build_vtbl_ref (build_indirect_ref (instance, 0),
4729			      integer_zero_node);
4730
4731  /* Get the java.lang.Class pointer for the interface being called.  */
4732  iface = DECL_CONTEXT (fn);
4733  iface_ref = lookup_field (iface, get_identifier ("class$"), 0, false);
4734  if (!iface_ref || TREE_CODE (iface_ref) != VAR_DECL
4735      || DECL_CONTEXT (iface_ref) != iface)
4736    {
4737      error ("could not find class$ field in java interface type `%T'",
4738		iface);
4739      return error_mark_node;
4740    }
4741  iface_ref = build1 (ADDR_EXPR, build_pointer_type (iface), iface_ref);
4742
4743  /* Determine the itable index of FN.  */
4744  i = 1;
4745  for (method = TYPE_METHODS (iface); method; method = TREE_CHAIN (method))
4746    {
4747      if (!DECL_VIRTUAL_P (method))
4748        continue;
4749      if (fn == method)
4750        break;
4751      i++;
4752    }
4753  idx = build_int_2 (i, 0);
4754
4755  lookup_args = tree_cons (NULL_TREE, klass_ref,
4756			   tree_cons (NULL_TREE, iface_ref,
4757				      build_tree_list (NULL_TREE, idx)));
4758  lookup_fn = build1 (ADDR_EXPR,
4759		      build_pointer_type (TREE_TYPE (java_iface_lookup_fn)),
4760		      java_iface_lookup_fn);
4761  return build (CALL_EXPR, ptr_type_node, lookup_fn, lookup_args, NULL_TREE);
4762}
4763
4764/* Returns the value to use for the in-charge parameter when making a
4765   call to a function with the indicated NAME.  */
4766
4767tree
4768in_charge_arg_for_name (tree name)
4769{
4770  if (name == base_ctor_identifier
4771      || name == base_dtor_identifier)
4772    return integer_zero_node;
4773  else if (name == complete_ctor_identifier)
4774    return integer_one_node;
4775  else if (name == complete_dtor_identifier)
4776    return integer_two_node;
4777  else if (name == deleting_dtor_identifier)
4778    return integer_three_node;
4779
4780  /* This function should only be called with one of the names listed
4781     above.  */
4782  abort ();
4783  return NULL_TREE;
4784}
4785
4786/* Build a call to a constructor, destructor, or an assignment
4787   operator for INSTANCE, an expression with class type.  NAME
4788   indicates the special member function to call; ARGS are the
4789   arguments.  BINFO indicates the base of INSTANCE that is to be
4790   passed as the `this' parameter to the member function called.
4791
4792   FLAGS are the LOOKUP_* flags to use when processing the call.
4793
4794   If NAME indicates a complete object constructor, INSTANCE may be
4795   NULL_TREE.  In this case, the caller will call build_cplus_new to
4796   store the newly constructed object into a VAR_DECL.  */
4797
4798tree
4799build_special_member_call (tree instance, tree name, tree args,
4800			   tree binfo, int flags)
4801{
4802  tree fns;
4803  /* The type of the subobject to be constructed or destroyed.  */
4804  tree class_type;
4805
4806  my_friendly_assert (name == complete_ctor_identifier
4807		      || name == base_ctor_identifier
4808		      || name == complete_dtor_identifier
4809		      || name == base_dtor_identifier
4810		      || name == deleting_dtor_identifier
4811		      || name == ansi_assopname (NOP_EXPR),
4812		      20020712);
4813  my_friendly_assert (binfo != NULL_TREE, 20020712);
4814
4815  class_type = BINFO_TYPE (binfo);
4816
4817  /* Handle the special case where INSTANCE is NULL_TREE.  */
4818  if (name == complete_ctor_identifier && !instance)
4819    {
4820      instance = build_int_2 (0, 0);
4821      TREE_TYPE (instance) = build_pointer_type (class_type);
4822      instance = build1 (INDIRECT_REF, class_type, instance);
4823    }
4824  else
4825    {
4826      if (name == complete_dtor_identifier
4827	  || name == base_dtor_identifier
4828	  || name == deleting_dtor_identifier)
4829	my_friendly_assert (args == NULL_TREE, 20020712);
4830
4831      /* Convert to the base class, if necessary.  */
4832      if (!same_type_ignoring_top_level_qualifiers_p
4833	  (TREE_TYPE (instance), BINFO_TYPE (binfo)))
4834	{
4835	  if (name != ansi_assopname (NOP_EXPR))
4836	    /* For constructors and destructors, either the base is
4837	       non-virtual, or it is virtual but we are doing the
4838	       conversion from a constructor or destructor for the
4839	       complete object.  In either case, we can convert
4840	       statically.  */
4841	    instance = convert_to_base_statically (instance, binfo);
4842	  else
4843	    /* However, for assignment operators, we must convert
4844	       dynamically if the base is virtual.  */
4845	    instance = build_base_path (PLUS_EXPR, instance,
4846					binfo, /*nonnull=*/1);
4847	}
4848    }
4849
4850  my_friendly_assert (instance != NULL_TREE, 20020712);
4851
4852  /* Resolve the name.  */
4853  if (!complete_type_or_else (BINFO_TYPE (binfo), NULL_TREE))
4854    return error_mark_node;
4855
4856  fns = lookup_fnfields (binfo, name, 1);
4857
4858  /* When making a call to a constructor or destructor for a subobject
4859     that uses virtual base classes, pass down a pointer to a VTT for
4860     the subobject.  */
4861  if ((name == base_ctor_identifier
4862       || name == base_dtor_identifier)
4863      && TYPE_USES_VIRTUAL_BASECLASSES (class_type))
4864    {
4865      tree vtt;
4866      tree sub_vtt;
4867
4868      /* If the current function is a complete object constructor
4869	 or destructor, then we fetch the VTT directly.
4870	 Otherwise, we look it up using the VTT we were given.  */
4871      vtt = TREE_CHAIN (CLASSTYPE_VTABLES (current_class_type));
4872      vtt = decay_conversion (vtt);
4873      vtt = build (COND_EXPR, TREE_TYPE (vtt),
4874		   build (EQ_EXPR, boolean_type_node,
4875			  current_in_charge_parm, integer_zero_node),
4876		   current_vtt_parm,
4877		   vtt);
4878      my_friendly_assert (BINFO_SUBVTT_INDEX (binfo), 20010110);
4879      sub_vtt = build (PLUS_EXPR, TREE_TYPE (vtt), vtt,
4880		       BINFO_SUBVTT_INDEX (binfo));
4881
4882      args = tree_cons (NULL_TREE, sub_vtt, args);
4883    }
4884
4885  return build_new_method_call (instance, fns, args,
4886				TYPE_BINFO (BINFO_TYPE (binfo)),
4887				flags);
4888}
4889
4890/* Return the NAME, as a C string.  The NAME indicates a function that
4891   is a member of TYPE.  *FREE_P is set to true if the caller must
4892   free the memory returned.
4893
4894   Rather than go through all of this, we should simply set the names
4895   of constructors and destructors appropriately, and dispense with
4896   ctor_identifier, dtor_identifier, etc.  */
4897
4898static char *
4899name_as_c_string (tree name, tree type, bool *free_p)
4900{
4901  char *pretty_name;
4902
4903  /* Assume that we will not allocate memory.  */
4904  *free_p = false;
4905  /* Constructors and destructors are special.  */
4906  if (IDENTIFIER_CTOR_OR_DTOR_P (name))
4907    {
4908      pretty_name
4909	= (char *) IDENTIFIER_POINTER (constructor_name (type));
4910      /* For a destructor, add the '~'.  */
4911      if (name == complete_dtor_identifier
4912	  || name == base_dtor_identifier
4913	  || name == deleting_dtor_identifier)
4914	{
4915	  pretty_name = concat ("~", pretty_name, NULL);
4916	  /* Remember that we need to free the memory allocated.  */
4917	  *free_p = true;
4918	}
4919    }
4920  else if (IDENTIFIER_TYPENAME_P (name))
4921    {
4922      pretty_name = concat ("operator ",
4923			    type_as_string (TREE_TYPE (name),
4924					    TFF_PLAIN_IDENTIFIER),
4925			    NULL);
4926      /* Remember that we need to free the memory allocated.  */
4927      *free_p = true;
4928    }
4929  else
4930    pretty_name = (char *) IDENTIFIER_POINTER (name);
4931
4932  return pretty_name;
4933}
4934
4935/* Build a call to "INSTANCE.FN (ARGS)".  */
4936
4937tree
4938build_new_method_call (tree instance, tree fns, tree args,
4939		       tree conversion_path, int flags)
4940{
4941  struct z_candidate *candidates = 0, *cand;
4942  tree explicit_targs = NULL_TREE;
4943  tree basetype = NULL_TREE;
4944  tree access_binfo;
4945  tree optype;
4946  tree mem_args = NULL_TREE, instance_ptr;
4947  tree name;
4948  tree user_args;
4949  tree call;
4950  tree fn;
4951  tree class_type;
4952  int template_only = 0;
4953  bool any_viable_p;
4954  tree orig_instance;
4955  tree orig_fns;
4956  tree orig_args;
4957
4958  my_friendly_assert (instance != NULL_TREE, 20020729);
4959
4960  if (error_operand_p (instance)
4961      || error_operand_p (fns)
4962      || args == error_mark_node)
4963    return error_mark_node;
4964
4965  orig_instance = instance;
4966  orig_fns = fns;
4967  orig_args = args;
4968
4969  if (processing_template_decl)
4970    {
4971      instance = build_non_dependent_expr (instance);
4972      if (!BASELINK_P (fns)
4973	  && TREE_CODE (fns) != PSEUDO_DTOR_EXPR
4974	  && TREE_TYPE (fns) != unknown_type_node)
4975	fns = build_non_dependent_expr (fns);
4976      args = build_non_dependent_args (orig_args);
4977    }
4978
4979  /* Process the argument list.  */
4980  user_args = args;
4981  args = resolve_args (args);
4982  if (args == error_mark_node)
4983    return error_mark_node;
4984
4985  if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
4986    instance = convert_from_reference (instance);
4987  basetype = TYPE_MAIN_VARIANT (TREE_TYPE (instance));
4988  instance_ptr = build_this (instance);
4989
4990  if (!BASELINK_P (fns))
4991    {
4992      error ("call to non-function `%D'", fns);
4993      return error_mark_node;
4994    }
4995
4996  if (!conversion_path)
4997    conversion_path = BASELINK_BINFO (fns);
4998  access_binfo = BASELINK_ACCESS_BINFO (fns);
4999  optype = BASELINK_OPTYPE (fns);
5000  fns = BASELINK_FUNCTIONS (fns);
5001
5002  if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
5003    {
5004      explicit_targs = TREE_OPERAND (fns, 1);
5005      fns = TREE_OPERAND (fns, 0);
5006      template_only = 1;
5007    }
5008
5009  my_friendly_assert (TREE_CODE (fns) == FUNCTION_DECL
5010		      || TREE_CODE (fns) == TEMPLATE_DECL
5011		      || TREE_CODE (fns) == OVERLOAD,
5012		      20020712);
5013
5014  /* XXX this should be handled before we get here.  */
5015  if (! IS_AGGR_TYPE (basetype))
5016    {
5017      if ((flags & LOOKUP_COMPLAIN) && basetype != error_mark_node)
5018	error ("request for member `%D' in `%E', which is of non-aggregate type `%T'",
5019	       fns, instance, basetype);
5020
5021      return error_mark_node;
5022    }
5023
5024  fn = get_first_fn (fns);
5025  name = DECL_NAME (fn);
5026
5027  if (IDENTIFIER_CTOR_OR_DTOR_P (name))
5028    {
5029      /* Callers should explicitly indicate whether they want to construct
5030	 the complete object or just the part without virtual bases.  */
5031      my_friendly_assert (name != ctor_identifier, 20000408);
5032      /* Similarly for destructors.  */
5033      my_friendly_assert (name != dtor_identifier, 20000408);
5034    }
5035
5036  /* It's OK to call destructors on cv-qualified objects.  Therefore,
5037     convert the INSTANCE_PTR to the unqualified type, if necessary.  */
5038  if (DECL_DESTRUCTOR_P (fn))
5039    {
5040      tree type = build_pointer_type (basetype);
5041      if (!same_type_p (type, TREE_TYPE (instance_ptr)))
5042	instance_ptr = build_nop (type, instance_ptr);
5043    }
5044
5045  class_type = (conversion_path ? BINFO_TYPE (conversion_path) : NULL_TREE);
5046  mem_args = tree_cons (NULL_TREE, instance_ptr, args);
5047
5048  for (fn = fns; fn; fn = OVL_NEXT (fn))
5049    {
5050      tree t = OVL_CURRENT (fn);
5051      tree this_arglist;
5052
5053      /* We can end up here for copy-init of same or base class.  */
5054      if ((flags & LOOKUP_ONLYCONVERTING)
5055	  && DECL_NONCONVERTING_P (t))
5056	continue;
5057
5058      if (DECL_NONSTATIC_MEMBER_FUNCTION_P (t))
5059	this_arglist = mem_args;
5060      else
5061	this_arglist = args;
5062
5063      if (TREE_CODE (t) == TEMPLATE_DECL)
5064	/* A member template.  */
5065	add_template_candidate (&candidates, t,
5066				class_type,
5067				explicit_targs,
5068				this_arglist, optype,
5069				access_binfo,
5070				conversion_path,
5071				flags,
5072				DEDUCE_CALL);
5073      else if (! template_only)
5074	add_function_candidate (&candidates, t,
5075				class_type,
5076				this_arglist,
5077				access_binfo,
5078				conversion_path,
5079				flags);
5080    }
5081
5082  candidates = splice_viable (candidates, pedantic, &any_viable_p);
5083  if (!any_viable_p)
5084    {
5085      /* XXX will LOOKUP_SPECULATIVELY be needed when this is done?  */
5086      if (flags & LOOKUP_SPECULATIVELY)
5087	return NULL_TREE;
5088      if (!COMPLETE_TYPE_P (basetype))
5089	cxx_incomplete_type_error (instance_ptr, basetype);
5090      else
5091	{
5092	  char *pretty_name;
5093	  bool free_p;
5094
5095	  pretty_name = name_as_c_string (name, basetype, &free_p);
5096	  error ("no matching function for call to `%T::%s(%A)%#V'",
5097		 basetype, pretty_name, user_args,
5098		 TREE_TYPE (TREE_TYPE (instance_ptr)));
5099	  if (free_p)
5100	    free (pretty_name);
5101	}
5102      print_z_candidates (candidates);
5103      return error_mark_node;
5104    }
5105
5106  cand = tourney (candidates);
5107  if (cand == 0)
5108    {
5109      char *pretty_name;
5110      bool free_p;
5111
5112      pretty_name = name_as_c_string (name, basetype, &free_p);
5113      error ("call of overloaded `%s(%A)' is ambiguous", pretty_name,
5114	     user_args);
5115      print_z_candidates (candidates);
5116      if (free_p)
5117	free (pretty_name);
5118      return error_mark_node;
5119    }
5120
5121  if (DECL_PURE_VIRTUAL_P (cand->fn)
5122      && instance == current_class_ref
5123      && (DECL_CONSTRUCTOR_P (current_function_decl)
5124	  || DECL_DESTRUCTOR_P (current_function_decl))
5125      && ! (flags & LOOKUP_NONVIRTUAL)
5126      && value_member (cand->fn, CLASSTYPE_PURE_VIRTUALS (basetype)))
5127    error ((DECL_CONSTRUCTOR_P (current_function_decl) ?
5128	       "abstract virtual `%#D' called from constructor"
5129	       : "abstract virtual `%#D' called from destructor"),
5130	      cand->fn);
5131  if (TREE_CODE (TREE_TYPE (cand->fn)) == METHOD_TYPE
5132      && is_dummy_object (instance_ptr))
5133    {
5134      error ("cannot call member function `%D' without object", cand->fn);
5135      return error_mark_node;
5136    }
5137
5138  if (DECL_VINDEX (cand->fn) && ! (flags & LOOKUP_NONVIRTUAL)
5139      && resolves_to_fixed_type_p (instance, 0))
5140    flags |= LOOKUP_NONVIRTUAL;
5141
5142  if (TREE_CODE (TREE_TYPE (cand->fn)) == METHOD_TYPE)
5143    call = build_over_call (cand, flags);
5144  else
5145    {
5146      call = build_over_call (cand, flags);
5147      /* In an expression of the form `a->f()' where `f' turns out to
5148	 be a static member function, `a' is none-the-less evaluated.  */
5149      if (!is_dummy_object (instance_ptr) && TREE_SIDE_EFFECTS (instance))
5150	call = build (COMPOUND_EXPR, TREE_TYPE (call), instance, call);
5151    }
5152
5153  if (processing_template_decl && call != error_mark_node)
5154    return build_min_non_dep
5155      (CALL_EXPR, call,
5156       build_min_nt (COMPONENT_REF, orig_instance, orig_fns),
5157       orig_args);
5158  return call;
5159}
5160
5161/* Returns true iff standard conversion sequence ICS1 is a proper
5162   subsequence of ICS2.  */
5163
5164static bool
5165is_subseq (tree ics1, tree ics2)
5166{
5167  /* We can assume that a conversion of the same code
5168     between the same types indicates a subsequence since we only get
5169     here if the types we are converting from are the same.  */
5170
5171  while (TREE_CODE (ics1) == RVALUE_CONV
5172	 || TREE_CODE (ics1) == LVALUE_CONV)
5173    ics1 = TREE_OPERAND (ics1, 0);
5174
5175  while (1)
5176    {
5177      while (TREE_CODE (ics2) == RVALUE_CONV
5178	  || TREE_CODE (ics2) == LVALUE_CONV)
5179	ics2 = TREE_OPERAND (ics2, 0);
5180
5181      if (TREE_CODE (ics2) == USER_CONV
5182	  || TREE_CODE (ics2) == AMBIG_CONV
5183	  || TREE_CODE (ics2) == IDENTITY_CONV)
5184	/* At this point, ICS1 cannot be a proper subsequence of
5185	   ICS2.  We can get a USER_CONV when we are comparing the
5186	   second standard conversion sequence of two user conversion
5187	   sequences.  */
5188	return false;
5189
5190      ics2 = TREE_OPERAND (ics2, 0);
5191
5192      if (TREE_CODE (ics2) == TREE_CODE (ics1)
5193	  && same_type_p (TREE_TYPE (ics2), TREE_TYPE (ics1))
5194	  && same_type_p (TREE_TYPE (TREE_OPERAND (ics2, 0)),
5195			     TREE_TYPE (TREE_OPERAND (ics1, 0))))
5196	return true;
5197    }
5198}
5199
5200/* Returns nonzero iff DERIVED is derived from BASE.  The inputs may
5201   be any _TYPE nodes.  */
5202
5203bool
5204is_properly_derived_from (tree derived, tree base)
5205{
5206  if (!IS_AGGR_TYPE_CODE (TREE_CODE (derived))
5207      || !IS_AGGR_TYPE_CODE (TREE_CODE (base)))
5208    return false;
5209
5210  /* We only allow proper derivation here.  The DERIVED_FROM_P macro
5211     considers every class derived from itself.  */
5212  return (!same_type_ignoring_top_level_qualifiers_p (derived, base)
5213	  && DERIVED_FROM_P (base, derived));
5214}
5215
5216/* We build the ICS for an implicit object parameter as a pointer
5217   conversion sequence.  However, such a sequence should be compared
5218   as if it were a reference conversion sequence.  If ICS is the
5219   implicit conversion sequence for an implicit object parameter,
5220   modify it accordingly.  */
5221
5222static void
5223maybe_handle_implicit_object (tree *ics)
5224{
5225  if (ICS_THIS_FLAG (*ics))
5226    {
5227      /* [over.match.funcs]
5228
5229	 For non-static member functions, the type of the
5230	 implicit object parameter is "reference to cv X"
5231	 where X is the class of which the function is a
5232	 member and cv is the cv-qualification on the member
5233	 function declaration.  */
5234      tree t = *ics;
5235      tree reference_type;
5236
5237      /* The `this' parameter is a pointer to a class type.  Make the
5238	 implicit conversion talk about a reference to that same class
5239	 type.  */
5240      reference_type = TREE_TYPE (TREE_TYPE (*ics));
5241      reference_type = build_reference_type (reference_type);
5242
5243      if (TREE_CODE (t) == QUAL_CONV)
5244	t = TREE_OPERAND (t, 0);
5245      if (TREE_CODE (t) == PTR_CONV)
5246	t = TREE_OPERAND (t, 0);
5247      t = build1 (IDENTITY_CONV, TREE_TYPE (TREE_TYPE (t)), NULL_TREE);
5248      t = direct_reference_binding (reference_type, t);
5249      *ics = t;
5250    }
5251}
5252
5253/* If *ICS is a REF_BIND set *ICS to the remainder of the conversion,
5254   and return the type to which the reference refers.  Otherwise,
5255   leave *ICS unchanged and return NULL_TREE.  */
5256
5257static tree
5258maybe_handle_ref_bind (tree *ics)
5259{
5260  if (TREE_CODE (*ics) == REF_BIND)
5261    {
5262      tree old_ics = *ics;
5263      tree type = TREE_TYPE (TREE_TYPE (old_ics));
5264      *ics = TREE_OPERAND (old_ics, 0);
5265      ICS_USER_FLAG (*ics) = ICS_USER_FLAG (old_ics);
5266      ICS_BAD_FLAG (*ics) = ICS_BAD_FLAG (old_ics);
5267      return type;
5268    }
5269
5270  return NULL_TREE;
5271}
5272
5273/* Compare two implicit conversion sequences according to the rules set out in
5274   [over.ics.rank].  Return values:
5275
5276      1: ics1 is better than ics2
5277     -1: ics2 is better than ics1
5278      0: ics1 and ics2 are indistinguishable */
5279
5280static int
5281compare_ics (tree ics1, tree ics2)
5282{
5283  tree from_type1;
5284  tree from_type2;
5285  tree to_type1;
5286  tree to_type2;
5287  tree deref_from_type1 = NULL_TREE;
5288  tree deref_from_type2 = NULL_TREE;
5289  tree deref_to_type1 = NULL_TREE;
5290  tree deref_to_type2 = NULL_TREE;
5291  int rank1, rank2;
5292
5293  /* REF_BINDING is nonzero if the result of the conversion sequence
5294     is a reference type.   In that case TARGET_TYPE is the
5295     type referred to by the reference.  */
5296  tree target_type1;
5297  tree target_type2;
5298
5299  /* Handle implicit object parameters.  */
5300  maybe_handle_implicit_object (&ics1);
5301  maybe_handle_implicit_object (&ics2);
5302
5303  /* Handle reference parameters.  */
5304  target_type1 = maybe_handle_ref_bind (&ics1);
5305  target_type2 = maybe_handle_ref_bind (&ics2);
5306
5307  /* [over.ics.rank]
5308
5309     When  comparing  the  basic forms of implicit conversion sequences (as
5310     defined in _over.best.ics_)
5311
5312     --a standard conversion sequence (_over.ics.scs_) is a better
5313       conversion sequence than a user-defined conversion sequence
5314       or an ellipsis conversion sequence, and
5315
5316     --a user-defined conversion sequence (_over.ics.user_) is a
5317       better conversion sequence than an ellipsis conversion sequence
5318       (_over.ics.ellipsis_).  */
5319  rank1 = ICS_RANK (ics1);
5320  rank2 = ICS_RANK (ics2);
5321
5322  if (rank1 > rank2)
5323    return -1;
5324  else if (rank1 < rank2)
5325    return 1;
5326
5327  if (rank1 == BAD_RANK)
5328    {
5329      /* XXX Isn't this an extension? */
5330      /* Both ICS are bad.  We try to make a decision based on what
5331	 would have happened if they'd been good.  */
5332      if (ICS_USER_FLAG (ics1) > ICS_USER_FLAG (ics2)
5333	  || ICS_STD_RANK (ics1) > ICS_STD_RANK (ics2))
5334	return -1;
5335      else if (ICS_USER_FLAG (ics1) < ICS_USER_FLAG (ics2)
5336	       || ICS_STD_RANK (ics1) < ICS_STD_RANK (ics2))
5337	return 1;
5338
5339      /* We couldn't make up our minds; try to figure it out below.  */
5340    }
5341
5342  if (ICS_ELLIPSIS_FLAG (ics1))
5343    /* Both conversions are ellipsis conversions.  */
5344    return 0;
5345
5346  /* User-defined  conversion sequence U1 is a better conversion sequence
5347     than another user-defined conversion sequence U2 if they contain the
5348     same user-defined conversion operator or constructor and if the sec-
5349     ond standard conversion sequence of U1 is  better  than  the  second
5350     standard conversion sequence of U2.  */
5351
5352  if (ICS_USER_FLAG (ics1))
5353    {
5354      tree t1, t2;
5355
5356      for (t1 = ics1; TREE_CODE (t1) != USER_CONV; t1 = TREE_OPERAND (t1, 0))
5357	if (TREE_CODE (t1) == AMBIG_CONV)
5358	  return 0;
5359      for (t2 = ics2; TREE_CODE (t2) != USER_CONV; t2 = TREE_OPERAND (t2, 0))
5360	if (TREE_CODE (t2) == AMBIG_CONV)
5361	  return 0;
5362
5363      if (USER_CONV_FN (t1) != USER_CONV_FN (t2))
5364	return 0;
5365
5366      /* We can just fall through here, after setting up
5367	 FROM_TYPE1 and FROM_TYPE2.  */
5368      from_type1 = TREE_TYPE (t1);
5369      from_type2 = TREE_TYPE (t2);
5370    }
5371  else
5372    {
5373      /* We're dealing with two standard conversion sequences.
5374
5375	 [over.ics.rank]
5376
5377	 Standard conversion sequence S1 is a better conversion
5378	 sequence than standard conversion sequence S2 if
5379
5380	 --S1 is a proper subsequence of S2 (comparing the conversion
5381	   sequences in the canonical form defined by _over.ics.scs_,
5382	   excluding any Lvalue Transformation; the identity
5383	   conversion sequence is considered to be a subsequence of
5384	   any non-identity conversion sequence */
5385
5386      from_type1 = ics1;
5387      while (TREE_CODE (from_type1) != IDENTITY_CONV)
5388	from_type1 = TREE_OPERAND (from_type1, 0);
5389      from_type1 = TREE_TYPE (from_type1);
5390
5391      from_type2 = ics2;
5392      while (TREE_CODE (from_type2) != IDENTITY_CONV)
5393	from_type2 = TREE_OPERAND (from_type2, 0);
5394      from_type2 = TREE_TYPE (from_type2);
5395    }
5396
5397  if (same_type_p (from_type1, from_type2))
5398    {
5399      if (is_subseq (ics1, ics2))
5400	return 1;
5401      if (is_subseq (ics2, ics1))
5402	return -1;
5403    }
5404  /* Otherwise, one sequence cannot be a subsequence of the other; they
5405     don't start with the same type.  This can happen when comparing the
5406     second standard conversion sequence in two user-defined conversion
5407     sequences.  */
5408
5409  /* [over.ics.rank]
5410
5411     Or, if not that,
5412
5413     --the rank of S1 is better than the rank of S2 (by the rules
5414       defined below):
5415
5416    Standard conversion sequences are ordered by their ranks: an Exact
5417    Match is a better conversion than a Promotion, which is a better
5418    conversion than a Conversion.
5419
5420    Two conversion sequences with the same rank are indistinguishable
5421    unless one of the following rules applies:
5422
5423    --A conversion that is not a conversion of a pointer, or pointer
5424      to member, to bool is better than another conversion that is such
5425      a conversion.
5426
5427    The ICS_STD_RANK automatically handles the pointer-to-bool rule,
5428    so that we do not have to check it explicitly.  */
5429  if (ICS_STD_RANK (ics1) < ICS_STD_RANK (ics2))
5430    return 1;
5431  else if (ICS_STD_RANK (ics2) < ICS_STD_RANK (ics1))
5432    return -1;
5433
5434  to_type1 = TREE_TYPE (ics1);
5435  to_type2 = TREE_TYPE (ics2);
5436
5437  if (TYPE_PTR_P (from_type1)
5438      && TYPE_PTR_P (from_type2)
5439      && TYPE_PTR_P (to_type1)
5440      && TYPE_PTR_P (to_type2))
5441    {
5442      deref_from_type1 = TREE_TYPE (from_type1);
5443      deref_from_type2 = TREE_TYPE (from_type2);
5444      deref_to_type1 = TREE_TYPE (to_type1);
5445      deref_to_type2 = TREE_TYPE (to_type2);
5446    }
5447  /* The rules for pointers to members A::* are just like the rules
5448     for pointers A*, except opposite: if B is derived from A then
5449     A::* converts to B::*, not vice versa.  For that reason, we
5450     switch the from_ and to_ variables here.  */
5451  else if ((TYPE_PTRMEM_P (from_type1) && TYPE_PTRMEM_P (from_type2)
5452	    && TYPE_PTRMEM_P (to_type1) && TYPE_PTRMEM_P (to_type2))
5453	   || (TYPE_PTRMEMFUNC_P (from_type1)
5454	       && TYPE_PTRMEMFUNC_P (from_type2)
5455	       && TYPE_PTRMEMFUNC_P (to_type1)
5456	       && TYPE_PTRMEMFUNC_P (to_type2)))
5457    {
5458      deref_to_type1 = TYPE_PTRMEM_CLASS_TYPE (from_type1);
5459      deref_to_type2 = TYPE_PTRMEM_CLASS_TYPE (from_type2);
5460      deref_from_type1 = TYPE_PTRMEM_CLASS_TYPE (to_type1);
5461      deref_from_type2 = TYPE_PTRMEM_CLASS_TYPE (to_type2);
5462    }
5463
5464  if (deref_from_type1 != NULL_TREE
5465      && IS_AGGR_TYPE_CODE (TREE_CODE (deref_from_type1))
5466      && IS_AGGR_TYPE_CODE (TREE_CODE (deref_from_type2)))
5467    {
5468      /* This was one of the pointer or pointer-like conversions.
5469
5470	 [over.ics.rank]
5471
5472	 --If class B is derived directly or indirectly from class A,
5473	   conversion of B* to A* is better than conversion of B* to
5474	   void*, and conversion of A* to void* is better than
5475	   conversion of B* to void*.  */
5476      if (TREE_CODE (deref_to_type1) == VOID_TYPE
5477	  && TREE_CODE (deref_to_type2) == VOID_TYPE)
5478	{
5479	  if (is_properly_derived_from (deref_from_type1,
5480					deref_from_type2))
5481	    return -1;
5482	  else if (is_properly_derived_from (deref_from_type2,
5483					     deref_from_type1))
5484	    return 1;
5485	}
5486      else if (TREE_CODE (deref_to_type1) == VOID_TYPE
5487	       || TREE_CODE (deref_to_type2) == VOID_TYPE)
5488	{
5489	  if (same_type_p (deref_from_type1, deref_from_type2))
5490	    {
5491	      if (TREE_CODE (deref_to_type2) == VOID_TYPE)
5492		{
5493		  if (is_properly_derived_from (deref_from_type1,
5494						deref_to_type1))
5495		    return 1;
5496		}
5497	      /* We know that DEREF_TO_TYPE1 is `void' here.  */
5498	      else if (is_properly_derived_from (deref_from_type1,
5499						 deref_to_type2))
5500		return -1;
5501	    }
5502	}
5503      else if (IS_AGGR_TYPE_CODE (TREE_CODE (deref_to_type1))
5504	       && IS_AGGR_TYPE_CODE (TREE_CODE (deref_to_type2)))
5505	{
5506	  /* [over.ics.rank]
5507
5508	     --If class B is derived directly or indirectly from class A
5509	       and class C is derived directly or indirectly from B,
5510
5511	     --conversion of C* to B* is better than conversion of C* to
5512	       A*,
5513
5514	     --conversion of B* to A* is better than conversion of C* to
5515	       A*  */
5516	  if (same_type_p (deref_from_type1, deref_from_type2))
5517	    {
5518	      if (is_properly_derived_from (deref_to_type1,
5519					    deref_to_type2))
5520		return 1;
5521	      else if (is_properly_derived_from (deref_to_type2,
5522						 deref_to_type1))
5523		return -1;
5524	    }
5525	  else if (same_type_p (deref_to_type1, deref_to_type2))
5526	    {
5527	      if (is_properly_derived_from (deref_from_type2,
5528					    deref_from_type1))
5529		return 1;
5530	      else if (is_properly_derived_from (deref_from_type1,
5531						 deref_from_type2))
5532		return -1;
5533	    }
5534	}
5535    }
5536  else if (CLASS_TYPE_P (non_reference (from_type1))
5537	   && same_type_p (from_type1, from_type2))
5538    {
5539      tree from = non_reference (from_type1);
5540
5541      /* [over.ics.rank]
5542
5543	 --binding of an expression of type C to a reference of type
5544	   B& is better than binding an expression of type C to a
5545	   reference of type A&
5546
5547	 --conversion of C to B is better than conversion of C to A,  */
5548      if (is_properly_derived_from (from, to_type1)
5549	  && is_properly_derived_from (from, to_type2))
5550	{
5551	  if (is_properly_derived_from (to_type1, to_type2))
5552	    return 1;
5553	  else if (is_properly_derived_from (to_type2, to_type1))
5554	    return -1;
5555	}
5556    }
5557  else if (CLASS_TYPE_P (non_reference (to_type1))
5558	   && same_type_p (to_type1, to_type2))
5559    {
5560      tree to = non_reference (to_type1);
5561
5562      /* [over.ics.rank]
5563
5564	 --binding of an expression of type B to a reference of type
5565	   A& is better than binding an expression of type C to a
5566	   reference of type A&,
5567
5568	 --onversion of B to A is better than conversion of C to A  */
5569      if (is_properly_derived_from (from_type1, to)
5570	  && is_properly_derived_from (from_type2, to))
5571	{
5572	  if (is_properly_derived_from (from_type2, from_type1))
5573	    return 1;
5574	  else if (is_properly_derived_from (from_type1, from_type2))
5575	    return -1;
5576	}
5577    }
5578
5579  /* [over.ics.rank]
5580
5581     --S1 and S2 differ only in their qualification conversion and  yield
5582       similar  types  T1 and T2 (_conv.qual_), respectively, and the cv-
5583       qualification signature of type T1 is a proper subset of  the  cv-
5584       qualification signature of type T2  */
5585  if (TREE_CODE (ics1) == QUAL_CONV
5586      && TREE_CODE (ics2) == QUAL_CONV
5587      && same_type_p (from_type1, from_type2))
5588    return comp_cv_qual_signature (to_type1, to_type2);
5589
5590  /* [over.ics.rank]
5591
5592     --S1 and S2 are reference bindings (_dcl.init.ref_), and the
5593     types to which the references refer are the same type except for
5594     top-level cv-qualifiers, and the type to which the reference
5595     initialized by S2 refers is more cv-qualified than the type to
5596     which the reference initialized by S1 refers */
5597
5598  if (target_type1 && target_type2
5599      && same_type_ignoring_top_level_qualifiers_p (to_type1, to_type2))
5600    return comp_cv_qualification (target_type2, target_type1);
5601
5602  /* Neither conversion sequence is better than the other.  */
5603  return 0;
5604}
5605
5606/* The source type for this standard conversion sequence.  */
5607
5608static tree
5609source_type (tree t)
5610{
5611  for (;; t = TREE_OPERAND (t, 0))
5612    {
5613      if (TREE_CODE (t) == USER_CONV
5614	  || TREE_CODE (t) == AMBIG_CONV
5615	  || TREE_CODE (t) == IDENTITY_CONV)
5616	return TREE_TYPE (t);
5617    }
5618  abort ();
5619}
5620
5621/* Note a warning about preferring WINNER to LOSER.  We do this by storing
5622   a pointer to LOSER and re-running joust to produce the warning if WINNER
5623   is actually used.  */
5624
5625static void
5626add_warning (struct z_candidate *winner, struct z_candidate *loser)
5627{
5628  winner->warnings = tree_cons (NULL_TREE,
5629				build_zc_wrapper (loser),
5630				winner->warnings);
5631}
5632
5633/* Compare two candidates for overloading as described in
5634   [over.match.best].  Return values:
5635
5636      1: cand1 is better than cand2
5637     -1: cand2 is better than cand1
5638      0: cand1 and cand2 are indistinguishable */
5639
5640static int
5641joust (struct z_candidate *cand1, struct z_candidate *cand2, bool warn)
5642{
5643  int winner = 0;
5644  int i, off1 = 0, off2 = 0, len;
5645
5646  /* Candidates that involve bad conversions are always worse than those
5647     that don't.  */
5648  if (cand1->viable > cand2->viable)
5649    return 1;
5650  if (cand1->viable < cand2->viable)
5651    return -1;
5652
5653  /* If we have two pseudo-candidates for conversions to the same type,
5654     or two candidates for the same function, arbitrarily pick one.  */
5655  if (cand1->fn == cand2->fn
5656      && (TYPE_P (cand1->fn) || DECL_P (cand1->fn)))
5657    return 1;
5658
5659  /* a viable function F1
5660     is defined to be a better function than another viable function F2  if
5661     for  all arguments i, ICSi(F1) is not a worse conversion sequence than
5662     ICSi(F2), and then */
5663
5664  /* for some argument j, ICSj(F1) is a better conversion  sequence  than
5665     ICSj(F2) */
5666
5667  /* For comparing static and non-static member functions, we ignore
5668     the implicit object parameter of the non-static function.  The
5669     standard says to pretend that the static function has an object
5670     parm, but that won't work with operator overloading.  */
5671  len = TREE_VEC_LENGTH (cand1->convs);
5672  if (len != TREE_VEC_LENGTH (cand2->convs))
5673    {
5674      if (DECL_STATIC_FUNCTION_P (cand1->fn)
5675	  && ! DECL_STATIC_FUNCTION_P (cand2->fn))
5676	off2 = 1;
5677      else if (! DECL_STATIC_FUNCTION_P (cand1->fn)
5678	       && DECL_STATIC_FUNCTION_P (cand2->fn))
5679	{
5680	  off1 = 1;
5681	  --len;
5682	}
5683      else
5684	abort ();
5685    }
5686
5687  for (i = 0; i < len; ++i)
5688    {
5689      tree t1 = TREE_VEC_ELT (cand1->convs, i+off1);
5690      tree t2 = TREE_VEC_ELT (cand2->convs, i+off2);
5691      int comp = compare_ics (t1, t2);
5692
5693      if (comp != 0)
5694	{
5695	  if (warn_sign_promo
5696	      && ICS_RANK (t1) + ICS_RANK (t2) == STD_RANK + PROMO_RANK
5697	      && TREE_CODE (t1) == STD_CONV
5698	      && TREE_CODE (t2) == STD_CONV
5699	      && TREE_CODE (TREE_TYPE (t1)) == INTEGER_TYPE
5700	      && TREE_CODE (TREE_TYPE (t2)) == INTEGER_TYPE
5701	      && (TYPE_PRECISION (TREE_TYPE (t1))
5702		  == TYPE_PRECISION (TREE_TYPE (t2)))
5703	      && (TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (t1, 0)))
5704		  || (TREE_CODE (TREE_TYPE (TREE_OPERAND (t1, 0)))
5705		      == ENUMERAL_TYPE)))
5706	    {
5707	      tree type = TREE_TYPE (TREE_OPERAND (t1, 0));
5708	      tree type1, type2;
5709	      struct z_candidate *w, *l;
5710	      if (comp > 0)
5711		type1 = TREE_TYPE (t1), type2 = TREE_TYPE (t2),
5712		  w = cand1, l = cand2;
5713	      else
5714		type1 = TREE_TYPE (t2), type2 = TREE_TYPE (t1),
5715		  w = cand2, l = cand1;
5716
5717	      if (warn)
5718		{
5719		  warning ("passing `%T' chooses `%T' over `%T'",
5720			      type, type1, type2);
5721		  warning ("  in call to `%D'", w->fn);
5722		}
5723	      else
5724		add_warning (w, l);
5725	    }
5726
5727	  if (winner && comp != winner)
5728	    {
5729	      winner = 0;
5730	      goto tweak;
5731	    }
5732	  winner = comp;
5733	}
5734    }
5735
5736  /* warn about confusing overload resolution for user-defined conversions,
5737     either between a constructor and a conversion op, or between two
5738     conversion ops.  */
5739  if (winner && warn_conversion && cand1->second_conv
5740      && (!DECL_CONSTRUCTOR_P (cand1->fn) || !DECL_CONSTRUCTOR_P (cand2->fn))
5741      && winner != compare_ics (cand1->second_conv, cand2->second_conv))
5742    {
5743      struct z_candidate *w, *l;
5744      bool give_warning = false;
5745
5746      if (winner == 1)
5747	w = cand1, l = cand2;
5748      else
5749	w = cand2, l = cand1;
5750
5751      /* We don't want to complain about `X::operator T1 ()'
5752	 beating `X::operator T2 () const', when T2 is a no less
5753	 cv-qualified version of T1.  */
5754      if (DECL_CONTEXT (w->fn) == DECL_CONTEXT (l->fn)
5755	  && !DECL_CONSTRUCTOR_P (w->fn) && !DECL_CONSTRUCTOR_P (l->fn))
5756	{
5757	  tree t = TREE_TYPE (TREE_TYPE (l->fn));
5758	  tree f = TREE_TYPE (TREE_TYPE (w->fn));
5759
5760	  if (TREE_CODE (t) == TREE_CODE (f) && POINTER_TYPE_P (t))
5761	    {
5762	      t = TREE_TYPE (t);
5763	      f = TREE_TYPE (f);
5764	    }
5765	  if (!comp_ptr_ttypes (t, f))
5766	    give_warning = true;
5767	}
5768      else
5769	give_warning = true;
5770
5771      if (!give_warning)
5772	/*NOP*/;
5773      else if (warn)
5774	{
5775	  tree source = source_type (TREE_VEC_ELT (w->convs, 0));
5776	  if (! DECL_CONSTRUCTOR_P (w->fn))
5777	    source = TREE_TYPE (source);
5778	  warning ("choosing `%D' over `%D'", w->fn, l->fn);
5779	  warning ("  for conversion from `%T' to `%T'",
5780		   source, TREE_TYPE (w->second_conv));
5781	  warning ("  because conversion sequence for the argument is better");
5782	}
5783      else
5784	add_warning (w, l);
5785    }
5786
5787  if (winner)
5788    return winner;
5789
5790  /* or, if not that,
5791     F1 is a non-template function and F2 is a template function
5792     specialization.  */
5793
5794  if (! cand1->template && cand2->template)
5795    return 1;
5796  else if (cand1->template && ! cand2->template)
5797    return -1;
5798
5799  /* or, if not that,
5800     F1 and F2 are template functions and the function template for F1 is
5801     more specialized than the template for F2 according to the partial
5802     ordering rules.  */
5803
5804  if (cand1->template && cand2->template)
5805    {
5806      winner = more_specialized
5807        (TI_TEMPLATE (cand1->template), TI_TEMPLATE (cand2->template),
5808         DEDUCE_ORDER,
5809         /* Tell the deduction code how many real function arguments
5810	    we saw, not counting the implicit 'this' argument.  But,
5811	    add_function_candidate() suppresses the "this" argument
5812	    for constructors.
5813
5814	    [temp.func.order]: The presence of unused ellipsis and default
5815	    arguments has no effect on the partial ordering of function
5816	    templates.  */
5817         TREE_VEC_LENGTH (cand1->convs)
5818	 - (DECL_NONSTATIC_MEMBER_FUNCTION_P (cand1->fn)
5819	    - DECL_CONSTRUCTOR_P (cand1->fn)));
5820      if (winner)
5821        return winner;
5822    }
5823
5824  /* or, if not that,
5825     the  context  is  an  initialization by user-defined conversion (see
5826     _dcl.init_  and  _over.match.user_)  and  the  standard   conversion
5827     sequence  from  the return type of F1 to the destination type (i.e.,
5828     the type of the entity being initialized)  is  a  better  conversion
5829     sequence  than the standard conversion sequence from the return type
5830     of F2 to the destination type.  */
5831
5832  if (cand1->second_conv)
5833    {
5834      winner = compare_ics (cand1->second_conv, cand2->second_conv);
5835      if (winner)
5836        return winner;
5837    }
5838
5839  /* Check whether we can discard a builtin candidate, either because we
5840     have two identical ones or matching builtin and non-builtin candidates.
5841
5842     (Pedantically in the latter case the builtin which matched the user
5843     function should not be added to the overload set, but we spot it here.
5844
5845     [over.match.oper]
5846     ... the builtin candidates include ...
5847     - do not have the same parameter type list as any non-template
5848       non-member candidate.  */
5849
5850  if (TREE_CODE (cand1->fn) == IDENTIFIER_NODE
5851      || TREE_CODE (cand2->fn) == IDENTIFIER_NODE)
5852    {
5853      for (i = 0; i < len; ++i)
5854	if (!same_type_p (TREE_TYPE (TREE_VEC_ELT (cand1->convs, i)),
5855			  TREE_TYPE (TREE_VEC_ELT (cand2->convs, i))))
5856	  break;
5857      if (i == TREE_VEC_LENGTH (cand1->convs))
5858	{
5859	  if (cand1->fn == cand2->fn)
5860	    /* Two built-in candidates; arbitrarily pick one.  */
5861	    return 1;
5862	  else if (TREE_CODE (cand1->fn) == IDENTIFIER_NODE)
5863	    /* cand1 is built-in; prefer cand2.  */
5864	    return -1;
5865	  else
5866	    /* cand2 is built-in; prefer cand1.  */
5867	    return 1;
5868	}
5869    }
5870
5871  /* If the two functions are the same (this can happen with declarations
5872     in multiple scopes and arg-dependent lookup), arbitrarily choose one.  */
5873  if (DECL_P (cand1->fn) && DECL_P (cand2->fn)
5874      && equal_functions (cand1->fn, cand2->fn))
5875    return 1;
5876
5877tweak:
5878
5879  /* Extension: If the worst conversion for one candidate is worse than the
5880     worst conversion for the other, take the first.  */
5881  if (!pedantic)
5882    {
5883      int rank1 = IDENTITY_RANK, rank2 = IDENTITY_RANK;
5884      struct z_candidate *w = 0, *l = 0;
5885
5886      for (i = 0; i < len; ++i)
5887	{
5888	  if (ICS_RANK (TREE_VEC_ELT (cand1->convs, i+off1)) > rank1)
5889	    rank1 = ICS_RANK (TREE_VEC_ELT (cand1->convs, i+off1));
5890	  if (ICS_RANK (TREE_VEC_ELT (cand2->convs, i+off2)) > rank2)
5891	    rank2 = ICS_RANK (TREE_VEC_ELT (cand2->convs, i+off2));
5892	}
5893      if (rank1 < rank2)
5894	winner = 1, w = cand1, l = cand2;
5895      if (rank1 > rank2)
5896	winner = -1, w = cand2, l = cand1;
5897      if (winner)
5898        {
5899	  if (warn)
5900	    {
5901	      pedwarn ("\
5902ISO C++ says that these are ambiguous, even \
5903though the worst conversion for the first is better than \
5904the worst conversion for the second:");
5905	      print_z_candidate (_("candidate 1:"), w);
5906	      print_z_candidate (_("candidate 2:"), l);
5907	    }
5908	  else
5909	    add_warning (w, l);
5910          return winner;
5911        }
5912    }
5913
5914  my_friendly_assert (!winner, 20010121);
5915  return 0;
5916}
5917
5918/* Given a list of candidates for overloading, find the best one, if any.
5919   This algorithm has a worst case of O(2n) (winner is last), and a best
5920   case of O(n/2) (totally ambiguous); much better than a sorting
5921   algorithm.  */
5922
5923static struct z_candidate *
5924tourney (struct z_candidate *candidates)
5925{
5926  struct z_candidate *champ = candidates, *challenger;
5927  int fate;
5928  int champ_compared_to_predecessor = 0;
5929
5930  /* Walk through the list once, comparing each current champ to the next
5931     candidate, knocking out a candidate or two with each comparison.  */
5932
5933  for (challenger = champ->next; challenger; )
5934    {
5935      fate = joust (champ, challenger, 0);
5936      if (fate == 1)
5937	challenger = challenger->next;
5938      else
5939	{
5940	  if (fate == 0)
5941	    {
5942	      champ = challenger->next;
5943	      if (champ == 0)
5944		return 0;
5945	      champ_compared_to_predecessor = 0;
5946	    }
5947	  else
5948	    {
5949	      champ = challenger;
5950	      champ_compared_to_predecessor = 1;
5951	    }
5952
5953	  challenger = champ->next;
5954	}
5955    }
5956
5957  /* Make sure the champ is better than all the candidates it hasn't yet
5958     been compared to.  */
5959
5960  for (challenger = candidates;
5961       challenger != champ
5962	 && !(champ_compared_to_predecessor && challenger->next == champ);
5963       challenger = challenger->next)
5964    {
5965      fate = joust (champ, challenger, 0);
5966      if (fate != 1)
5967	return 0;
5968    }
5969
5970  return champ;
5971}
5972
5973/* Returns nonzero if things of type FROM can be converted to TO.  */
5974
5975bool
5976can_convert (tree to, tree from)
5977{
5978  return can_convert_arg (to, from, NULL_TREE);
5979}
5980
5981/* Returns nonzero if ARG (of type FROM) can be converted to TO.  */
5982
5983bool
5984can_convert_arg (tree to, tree from, tree arg)
5985{
5986  tree t = implicit_conversion (to, from, arg, LOOKUP_NORMAL);
5987  return (t && ! ICS_BAD_FLAG (t));
5988}
5989
5990/* Like can_convert_arg, but allows dubious conversions as well.  */
5991
5992bool
5993can_convert_arg_bad (tree to, tree from, tree arg)
5994{
5995  return implicit_conversion (to, from, arg, LOOKUP_NORMAL) != 0;
5996}
5997
5998/* Convert EXPR to TYPE.  Return the converted expression.
5999
6000   Note that we allow bad conversions here because by the time we get to
6001   this point we are committed to doing the conversion.  If we end up
6002   doing a bad conversion, convert_like will complain.  */
6003
6004tree
6005perform_implicit_conversion (tree type, tree expr)
6006{
6007  tree conv;
6008
6009  if (error_operand_p (expr))
6010    return error_mark_node;
6011  conv = implicit_conversion (type, TREE_TYPE (expr), expr,
6012			      LOOKUP_NORMAL);
6013  if (!conv)
6014    {
6015      error ("could not convert `%E' to `%T'", expr, type);
6016      return error_mark_node;
6017    }
6018
6019  return convert_like (conv, expr);
6020}
6021
6022/* Convert EXPR to TYPE (as a direct-initialization) if that is
6023   permitted.  If the conversion is valid, the converted expression is
6024   returned.  Otherwise, NULL_TREE is returned, except in the case
6025   that TYPE is a class type; in that case, an error is issued.  */
6026
6027tree
6028perform_direct_initialization_if_possible (tree type, tree expr)
6029{
6030  tree conv;
6031
6032  if (type == error_mark_node || error_operand_p (expr))
6033    return error_mark_node;
6034  /* [dcl.init]
6035
6036     If the destination type is a (possibly cv-qualified) class type:
6037
6038     -- If the initialization is direct-initialization ...,
6039     constructors are considered. ... If no constructor applies, or
6040     the overload resolution is ambiguous, the initialization is
6041     ill-formed.  */
6042  if (CLASS_TYPE_P (type))
6043    {
6044      expr = build_special_member_call (NULL_TREE, complete_ctor_identifier,
6045					build_tree_list (NULL_TREE, expr),
6046					TYPE_BINFO (type),
6047					LOOKUP_NORMAL);
6048      return build_cplus_new (type, expr);
6049    }
6050  conv = implicit_conversion (type, TREE_TYPE (expr), expr,
6051			      LOOKUP_NORMAL);
6052  if (!conv || ICS_BAD_FLAG (conv))
6053    return NULL_TREE;
6054  return convert_like_real (conv, expr, NULL_TREE, 0, 0,
6055			    /*issue_conversion_warnings=*/false);
6056}
6057
6058/* DECL is a VAR_DECL whose type is a REFERENCE_TYPE.  The reference
6059   is being bound to a temporary.  Create and return a new VAR_DECL
6060   with the indicated TYPE; this variable will store the value to
6061   which the reference is bound.  */
6062
6063tree
6064make_temporary_var_for_ref_to_temp (tree decl, tree type)
6065{
6066  tree var;
6067
6068  /* Create the variable.  */
6069  var = build_decl (VAR_DECL, NULL_TREE, type);
6070  DECL_ARTIFICIAL (var) = 1;
6071  TREE_USED (var) = 1;
6072
6073  /* Register the variable.  */
6074  if (TREE_STATIC (decl))
6075    {
6076      /* Namespace-scope or local static; give it a mangled name.  */
6077      tree name;
6078
6079      TREE_STATIC (var) = 1;
6080      name = mangle_ref_init_variable (decl);
6081      DECL_NAME (var) = name;
6082      SET_DECL_ASSEMBLER_NAME (var, name);
6083      var = pushdecl_top_level (var);
6084    }
6085  else
6086    {
6087      /* Create a new cleanup level if necessary.  */
6088      maybe_push_cleanup_level (type);
6089      /* Don't push unnamed temps.  Do set DECL_CONTEXT, though.  */
6090      DECL_CONTEXT (var) = current_function_decl;
6091    }
6092
6093  return var;
6094}
6095
6096/* Convert EXPR to the indicated reference TYPE, in a way suitable for
6097   initializing a variable of that TYPE.  If DECL is non-NULL, it is
6098   the VAR_DECL being initialized with the EXPR.  (In that case, the
6099   type of DECL will be TYPE.)  If DECL is non-NULL, then CLEANUP must
6100   also be non-NULL, and with *CLEANUP initialized to NULL.  Upon
6101   return, if *CLEANUP is no longer NULL, it will be a CLEANUP_STMT
6102   that should be inserted after the returned expression is used to
6103   initialize DECL.
6104
6105   Return the converted expression.  */
6106
6107tree
6108initialize_reference (tree type, tree expr, tree decl, tree *cleanup)
6109{
6110  tree conv;
6111
6112  if (type == error_mark_node || error_operand_p (expr))
6113    return error_mark_node;
6114
6115  conv = reference_binding (type, TREE_TYPE (expr), expr, LOOKUP_NORMAL);
6116  if (!conv || ICS_BAD_FLAG (conv))
6117    {
6118      if (!(TYPE_QUALS (TREE_TYPE (type)) & TYPE_QUAL_CONST)
6119          && !real_lvalue_p (expr))
6120        error ("invalid initialization of non-const reference of "
6121               "type '%T' from a temporary of type '%T'",
6122               type, TREE_TYPE (expr));
6123      else
6124        error ("invalid initialization of reference of type "
6125	       "'%T' from expression of type '%T'", type,
6126	       TREE_TYPE (expr));
6127      return error_mark_node;
6128    }
6129
6130  /* If DECL is non-NULL, then this special rule applies:
6131
6132       [class.temporary]
6133
6134       The temporary to which the reference is bound or the temporary
6135       that is the complete object to which the reference is bound
6136       persists for the lifetime of the reference.
6137
6138       The temporaries created during the evaluation of the expression
6139       initializing the reference, except the temporary to which the
6140       reference is bound, are destroyed at the end of the
6141       full-expression in which they are created.
6142
6143     In that case, we store the converted expression into a new
6144     VAR_DECL in a new scope.
6145
6146     However, we want to be careful not to create temporaries when
6147     they are not required.  For example, given:
6148
6149       struct B {};
6150       struct D : public B {};
6151       D f();
6152       const B& b = f();
6153
6154     there is no need to copy the return value from "f"; we can just
6155     extend its lifetime.  Similarly, given:
6156
6157       struct S {};
6158       struct T { operator S(); };
6159       T t;
6160       const S& s = t;
6161
6162    we can extend the lifetime of the return value of the conversion
6163    operator.  */
6164  my_friendly_assert (TREE_CODE (conv) == REF_BIND, 20030302);
6165  if (decl)
6166    {
6167      tree var;
6168      tree base_conv_type;
6169
6170      /* Skip over the REF_BIND.  */
6171      conv = TREE_OPERAND (conv, 0);
6172      /* If the next conversion is a BASE_CONV, skip that too -- but
6173	 remember that the conversion was required.  */
6174      if (TREE_CODE (conv) == BASE_CONV && !NEED_TEMPORARY_P (conv))
6175	{
6176	  if (CHECK_COPY_CONSTRUCTOR_P (conv))
6177	    check_constructor_callable (TREE_TYPE (expr), expr);
6178	  base_conv_type = TREE_TYPE (conv);
6179	  conv = TREE_OPERAND (conv, 0);
6180	}
6181      else
6182	base_conv_type = NULL_TREE;
6183      /* Perform the remainder of the conversion.  */
6184      expr = convert_like_real (conv, expr,
6185				/*fn=*/NULL_TREE, /*argnum=*/0,
6186				/*inner=*/-1,
6187				/*issue_conversion_warnings=*/true);
6188      if (error_operand_p (expr))
6189	return error_mark_node;
6190      if (!real_lvalue_p (expr))
6191	{
6192	  tree init;
6193	  tree type;
6194
6195	  /* Create the temporary variable.  */
6196	  type = TREE_TYPE (expr);
6197	  var = make_temporary_var_for_ref_to_temp (decl, type);
6198	  layout_decl (var, 0);
6199	  /* If the rvalue is the result of a function call it will be
6200	     a TARGET_EXPR.  If it is some other construct (such as a
6201	     member access expression where the underlying object is
6202	     itself the result of a function call), turn it into a
6203	     TARGET_EXPR here.  It is important that EXPR be a
6204	     TARGET_EXPR below since otherwise the INIT_EXPR will
6205	     attempt to make a bitwise copy of EXPR to intialize
6206	     VAR. */
6207	  if (TREE_CODE (expr) != TARGET_EXPR)
6208	    expr = get_target_expr (expr);
6209	  /* Create the INIT_EXPR that will initialize the temporary
6210	     variable.  */
6211	  init = build (INIT_EXPR, type, var, expr);
6212	  if (at_function_scope_p ())
6213	    {
6214	      add_decl_stmt (var);
6215	      *cleanup = cxx_maybe_build_cleanup (var);
6216	      if (*cleanup)
6217		/* We must be careful to destroy the temporary only
6218		   after its initialization has taken place.  If the
6219		   initialization throws an exception, then the
6220		   destructor should not be run.  We cannot simply
6221		   transform INIT into something like:
6222
6223		     (INIT, ({ CLEANUP_STMT; }))
6224
6225		   because emit_local_var always treats the
6226		   initializer as a full-expression.  Thus, the
6227		   destructor would run too early; it would run at the
6228		   end of initializing the reference variable, rather
6229		   than at the end of the block enclosing the
6230		   reference variable.
6231
6232		   The solution is to pass back a CLEANUP_STMT which
6233		   the caller is responsible for attaching to the
6234		   statement tree.  */
6235		*cleanup = build_stmt (CLEANUP_STMT, var, *cleanup);
6236	    }
6237	  else
6238	    {
6239	      rest_of_decl_compilation (var, NULL, /*toplev=*/1, at_eof);
6240	      if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
6241		static_aggregates = tree_cons (NULL_TREE, var,
6242					       static_aggregates);
6243	    }
6244	  /* Use its address to initialize the reference variable.  */
6245	  expr = build_address (var);
6246	  expr = build (COMPOUND_EXPR, TREE_TYPE (expr), init, expr);
6247	}
6248      else
6249	/* Take the address of EXPR.  */
6250	expr = build_unary_op (ADDR_EXPR, expr, 0);
6251      /* If a BASE_CONV was required, perform it now.  */
6252      if (base_conv_type)
6253	expr = (perform_implicit_conversion
6254		(build_pointer_type (base_conv_type), expr));
6255      return build_nop (type, expr);
6256    }
6257
6258  /* Perform the conversion.  */
6259  return convert_like (conv, expr);
6260}
6261
6262#include "gt-cp-call.h"
6263