call.c revision 102780
1202375Srdivacky/* Functions related to invoking methods and overloaded functions.
2202375Srdivacky   Copyright (C) 1987, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3202375Srdivacky   1999, 2000, 2001, 2002 Free Software Foundation, Inc.
4202375Srdivacky   Contributed by Michael Tiemann (tiemann@cygnus.com) and
5202375Srdivacky   modified by Brendan Kehoe (brendan@cygnus.com).
6202375Srdivacky
7202375SrdivackyThis file is part of GNU CC.
8202375Srdivacky
9202375SrdivackyGNU CC is free software; you can redistribute it and/or modify
10202375Srdivackyit under the terms of the GNU General Public License as published by
11202375Srdivackythe Free Software Foundation; either version 2, or (at your option)
12202375Srdivackyany later version.
13202375Srdivacky
14202375SrdivackyGNU CC is distributed in the hope that it will be useful,
15226633Sdimbut WITHOUT ANY WARRANTY; without even the implied warranty of
16249423SdimMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17249423SdimGNU General Public License for more details.
18234353Sdim
19202375SrdivackyYou should have received a copy of the GNU General Public License
20202375Srdivackyalong with GNU CC; see the file COPYING.  If not, write to
21202375Srdivackythe Free Software Foundation, 59 Temple Place - Suite 330,
22202375SrdivackyBoston, MA 02111-1307, USA.  */
23202375Srdivacky
24202375Srdivacky
25202375Srdivacky/* High-level class interface.  */
26202375Srdivacky
27210299Sed#include "config.h"
28202375Srdivacky#include "system.h"
29202375Srdivacky#include "tree.h"
30202375Srdivacky#include "cp-tree.h"
31210299Sed#include "output.h"
32202375Srdivacky#include "flags.h"
33249423Sdim#include "rtl.h"
34202375Srdivacky#include "toplev.h"
35224145Sdim#include "expr.h"
36224145Sdim#include "ggc.h"
37239462Sdim#include "diagnostic.h"
38224145Sdim
39224145Sdimextern int inhibit_warnings;
40224145Sdim
41224145Sdimstatic tree build_new_method_call PARAMS ((tree, tree, tree, tree, int));
42224145Sdim
43202375Srdivackystatic tree build_field_call PARAMS ((tree, tree, tree, tree));
44202375Srdivackystatic struct z_candidate * tourney PARAMS ((struct z_candidate *));
45202375Srdivackystatic int equal_functions PARAMS ((tree, tree));
46210299Sedstatic int joust PARAMS ((struct z_candidate *, struct z_candidate *, int));
47202375Srdivackystatic int compare_ics PARAMS ((tree, tree));
48202375Srdivackystatic tree build_over_call PARAMS ((struct z_candidate *, tree, int));
49202375Srdivackystatic tree build_java_interface_fn_ref PARAMS ((tree, tree));
50249423Sdim#define convert_like(CONV, EXPR) \
51202375Srdivacky  convert_like_real ((CONV), (EXPR), NULL_TREE, 0, 0)
52202375Srdivacky#define convert_like_with_context(CONV, EXPR, FN, ARGNO) \
53202375Srdivacky  convert_like_real ((CONV), (EXPR), (FN), (ARGNO), 0)
54202375Srdivackystatic tree convert_like_real PARAMS ((tree, tree, tree, int, int));
55202375Srdivackystatic void op_error PARAMS ((enum tree_code, enum tree_code, tree, tree,
56202375Srdivacky			    tree, const char *));
57249423Sdimstatic tree build_object_call PARAMS ((tree, tree));
58202375Srdivackystatic tree resolve_args PARAMS ((tree));
59249423Sdimstatic struct z_candidate * build_user_type_conversion_1
60202375Srdivacky	PARAMS ((tree, tree, int));
61202375Srdivackystatic void print_z_candidates PARAMS ((struct z_candidate *));
62249423Sdimstatic tree build_this PARAMS ((tree));
63202375Srdivackystatic struct z_candidate * splice_viable PARAMS ((struct z_candidate *));
64202375Srdivackystatic int any_viable PARAMS ((struct z_candidate *));
65202375Srdivackystatic struct z_candidate * add_template_candidate
66202375Srdivacky	PARAMS ((struct z_candidate *, tree, tree, tree, tree, tree, int,
67202375Srdivacky	       unification_kind_t));
68202375Srdivackystatic struct z_candidate * add_template_candidate_real
69202375Srdivacky	PARAMS ((struct z_candidate *, tree, tree, tree, tree, tree, int,
70202375Srdivacky	       tree, unification_kind_t));
71202375Srdivackystatic struct z_candidate * add_template_conv_candidate
72202375Srdivacky        PARAMS ((struct z_candidate *, tree, tree, tree, tree));
73202375Srdivackystatic struct z_candidate * add_builtin_candidates
74202375Srdivacky	PARAMS ((struct z_candidate *, enum tree_code, enum tree_code,
75202375Srdivacky	       tree, tree *, int));
76202375Srdivackystatic struct z_candidate * add_builtin_candidate
77202375Srdivacky	PARAMS ((struct z_candidate *, enum tree_code, enum tree_code,
78202375Srdivacky	       tree, tree, tree, tree *, tree *, int));
79202375Srdivackystatic int is_complete PARAMS ((tree));
80202375Srdivackystatic struct z_candidate * build_builtin_candidate
81243830Sdim	PARAMS ((struct z_candidate *, tree, tree, tree, tree *, tree *,
82202375Srdivacky	       int));
83202375Srdivackystatic struct z_candidate * add_conv_candidate
84226633Sdim	PARAMS ((struct z_candidate *, tree, tree, tree));
85249423Sdimstatic struct z_candidate * add_function_candidate
86202375Srdivacky	PARAMS ((struct z_candidate *, tree, tree, tree, int));
87202375Srdivackystatic tree implicit_conversion PARAMS ((tree, tree, tree, int));
88202375Srdivackystatic tree standard_conversion PARAMS ((tree, tree, tree));
89202375Srdivackystatic tree reference_binding PARAMS ((tree, tree, tree, int));
90226633Sdimstatic tree non_reference PARAMS ((tree));
91226633Sdimstatic tree build_conv PARAMS ((enum tree_code, tree, tree));
92202375Srdivackystatic int is_subseq PARAMS ((tree, tree));
93202375Srdivackystatic tree maybe_handle_ref_bind PARAMS ((tree*));
94202375Srdivackystatic void maybe_handle_implicit_object PARAMS ((tree*));
95202375Srdivackystatic struct z_candidate * add_candidate PARAMS ((struct z_candidate *,
96202375Srdivacky						   tree, tree, int));
97202375Srdivackystatic tree source_type PARAMS ((tree));
98202375Srdivackystatic void add_warning PARAMS ((struct z_candidate *, struct z_candidate *));
99202375Srdivackystatic int reference_related_p PARAMS ((tree, tree));
100221345Sdimstatic int reference_compatible_p PARAMS ((tree, tree));
101221345Sdimstatic tree convert_class_to_reference PARAMS ((tree, tree, tree));
102202375Srdivackystatic tree direct_reference_binding PARAMS ((tree, tree));
103202375Srdivackystatic int promoted_arithmetic_type_p PARAMS ((tree));
104202375Srdivackystatic tree conditional_conversion PARAMS ((tree, tree));
105202375Srdivacky
106202375Srdivackytree
107249423Sdimbuild_vfield_ref (datum, type)
108249423Sdim     tree datum, type;
109249423Sdim{
110249423Sdim  tree rval;
111249423Sdim
112249423Sdim  if (datum == error_mark_node)
113202375Srdivacky    return error_mark_node;
114202375Srdivacky
115202375Srdivacky  if (TREE_CODE (TREE_TYPE (datum)) == REFERENCE_TYPE)
116210299Sed    datum = convert_from_reference (datum);
117202375Srdivacky
118202375Srdivacky  if (! TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (type))
119249423Sdim    rval = build (COMPONENT_REF, TREE_TYPE (TYPE_VFIELD (type)),
120202375Srdivacky		  datum, TYPE_VFIELD (type));
121202375Srdivacky  else
122202375Srdivacky    rval = build_component_ref (datum, DECL_NAME (TYPE_VFIELD (type)), NULL_TREE, 0);
123202375Srdivacky
124202375Srdivacky  return rval;
125202375Srdivacky}
126202375Srdivacky
127202375Srdivacky/* Build a call to a member of an object.  I.e., one that overloads
128202375Srdivacky   operator ()(), or is a pointer-to-function or pointer-to-method.  */
129202375Srdivacky
130210299Sedstatic tree
131202375Srdivackybuild_field_call (basetype_path, instance_ptr, name, parms)
132226633Sdim     tree basetype_path, instance_ptr, name, parms;
133202375Srdivacky{
134249423Sdim  tree field, instance;
135210299Sed
136210299Sed  if (IDENTIFIER_CTOR_OR_DTOR_P (name))
137202375Srdivacky    return NULL_TREE;
138226633Sdim
139202375Srdivacky  /* Speed up the common case.  */
140249423Sdim  if (instance_ptr == current_class_ptr
141202375Srdivacky      && IDENTIFIER_CLASS_VALUE (name) == NULL_TREE)
142202375Srdivacky    return NULL_TREE;
143202375Srdivacky
144249423Sdim  field = lookup_field (basetype_path, name, 1, 0);
145202375Srdivacky
146202375Srdivacky  if (field == error_mark_node || field == NULL_TREE)
147202375Srdivacky    return field;
148221345Sdim
149202375Srdivacky  if (TREE_CODE (field) == FIELD_DECL || TREE_CODE (field) == VAR_DECL)
150202375Srdivacky    {
151202375Srdivacky      /* If it's a field, try overloading operator (),
152223017Sdim	 or calling if the field is a pointer-to-function.  */
153202375Srdivacky      instance = build_indirect_ref (instance_ptr, NULL);
154202375Srdivacky      instance = build_component_ref_1 (instance, field, 0);
155202375Srdivacky
156202375Srdivacky      if (instance == error_mark_node)
157249423Sdim	return error_mark_node;
158202375Srdivacky
159202375Srdivacky      if (IS_AGGR_TYPE (TREE_TYPE (instance)))
160249423Sdim	return build_opfncall (CALL_EXPR, LOOKUP_NORMAL,
161202375Srdivacky			       instance, parms, NULL_TREE);
162202375Srdivacky      else if (TREE_CODE (TREE_TYPE (instance)) == FUNCTION_TYPE
163202375Srdivacky	       || (TREE_CODE (TREE_TYPE (instance)) == POINTER_TYPE
164202375Srdivacky		   && (TREE_CODE (TREE_TYPE (TREE_TYPE (instance)))
165202375Srdivacky		       == FUNCTION_TYPE)))
166234353Sdim	return build_function_call (instance, parms);
167202375Srdivacky    }
168202375Srdivacky
169202375Srdivacky  return NULL_TREE;
170202375Srdivacky}
171202375Srdivacky
172202375Srdivacky/* Returns nonzero iff the destructor name specified in NAME
173202375Srdivacky   (a BIT_NOT_EXPR) matches BASETYPE.  The operand of NAME can take many
174202375Srdivacky   forms...  */
175202375Srdivacky
176202375Srdivackyint
177202375Srdivackycheck_dtor_name (basetype, name)
178202375Srdivacky     tree basetype, name;
179202375Srdivacky{
180202375Srdivacky  name = TREE_OPERAND (name, 0);
181202375Srdivacky
182202375Srdivacky  /* Just accept something we've already complained about.  */
183202375Srdivacky  if (name == error_mark_node)
184202375Srdivacky    return 1;
185202375Srdivacky
186202375Srdivacky  if (TREE_CODE (name) == TYPE_DECL)
187202375Srdivacky    name = TREE_TYPE (name);
188202375Srdivacky  else if (TYPE_P (name))
189202375Srdivacky    /* OK */;
190249423Sdim  else if (TREE_CODE (name) == IDENTIFIER_NODE)
191202375Srdivacky    {
192202375Srdivacky      if ((IS_AGGR_TYPE (basetype) && name == constructor_name (basetype))
193202375Srdivacky	  || (TREE_CODE (basetype) == ENUMERAL_TYPE
194202375Srdivacky	      && name == TYPE_IDENTIFIER (basetype)))
195202375Srdivacky	name = basetype;
196202375Srdivacky      else
197202375Srdivacky	name = get_type_value (name);
198202375Srdivacky    }
199249423Sdim  /* In the case of:
200202375Srdivacky
201202375Srdivacky       template <class T> struct S { ~S(); };
202202375Srdivacky       int i;
203202375Srdivacky       i.~S();
204202375Srdivacky
205202375Srdivacky     NAME will be a class template.  */
206202375Srdivacky  else if (DECL_CLASS_TEMPLATE_P (name))
207202375Srdivacky    return 0;
208202375Srdivacky  else
209202375Srdivacky    abort ();
210202375Srdivacky
211202375Srdivacky  if (name && TYPE_MAIN_VARIANT (basetype) == TYPE_MAIN_VARIANT (name))
212202375Srdivacky    return 1;
213221345Sdim  return 0;
214202375Srdivacky}
215202375Srdivacky
216202375Srdivacky/* Build a method call of the form `EXP->SCOPES::NAME (PARMS)'.
217202375Srdivacky   This is how virtual function calls are avoided.  */
218202375Srdivacky
219202375Srdivackytree
220202375Srdivackybuild_scoped_method_call (exp, basetype, name, parms)
221249423Sdim     tree exp, basetype, name, parms;
222202375Srdivacky{
223202375Srdivacky  /* Because this syntactic form does not allow
224202375Srdivacky     a pointer to a base class to be `stolen',
225249423Sdim     we need not protect the derived->base conversion
226202375Srdivacky     that happens here.
227223017Sdim
228202375Srdivacky     @@ But we do have to check access privileges later.  */
229202375Srdivacky  tree binfo, decl;
230202375Srdivacky  tree type = TREE_TYPE (exp);
231202375Srdivacky
232202375Srdivacky  if (type == error_mark_node
233249423Sdim      || basetype == error_mark_node)
234202375Srdivacky    return error_mark_node;
235202375Srdivacky
236202375Srdivacky  if (processing_template_decl)
237226633Sdim    {
238243830Sdim      if (TREE_CODE (name) == BIT_NOT_EXPR
239202375Srdivacky	  && TREE_CODE (TREE_OPERAND (name, 0)) == IDENTIFIER_NODE)
240202375Srdivacky	{
241226633Sdim	  tree type = get_aggr_from_typedef (TREE_OPERAND (name, 0), 0);
242226633Sdim	  if (type)
243202375Srdivacky	    name = build_min_nt (BIT_NOT_EXPR, type);
244202375Srdivacky	}
245202375Srdivacky      name = build_min_nt (SCOPE_REF, basetype, name);
246202375Srdivacky      return build_min_nt (METHOD_CALL_EXPR, name, exp, parms, NULL_TREE);
247243830Sdim    }
248243830Sdim
249243830Sdim  if (TREE_CODE (type) == REFERENCE_TYPE)
250243830Sdim    type = TREE_TYPE (type);
251243830Sdim
252243830Sdim  if (TREE_CODE (basetype) == TREE_VEC)
253243830Sdim    {
254243830Sdim      binfo = basetype;
255243830Sdim      basetype = BINFO_TYPE (binfo);
256202375Srdivacky    }
257202375Srdivacky  else
258202375Srdivacky    binfo = NULL_TREE;
259243830Sdim
260243830Sdim  /* Check the destructor call syntax.  */
261202375Srdivacky  if (TREE_CODE (name) == BIT_NOT_EXPR)
262249423Sdim    {
263202375Srdivacky      /* We can get here if someone writes their destructor call like
264202375Srdivacky	 `obj.NS::~T()'; this isn't really a scoped method call, so hand
265202375Srdivacky	 it off.  */
266203954Srdivacky      if (TREE_CODE (basetype) == NAMESPACE_DECL)
267203954Srdivacky	return build_method_call (exp, name, parms, NULL_TREE, LOOKUP_NORMAL);
268203954Srdivacky
269203954Srdivacky      if (! check_dtor_name (basetype, name))
270203954Srdivacky	error ("qualified type `%T' does not match destructor name `~%T'",
271226633Sdim		  basetype, TREE_OPERAND (name, 0));
272203954Srdivacky
273202375Srdivacky      /* Destructors can be "called" for simple types; see 5.2.4 and 12.4 Note
274249423Sdim	 that explicit ~int is caught in the parser; this deals with typedefs
275203954Srdivacky	 and template parms.  */
276203954Srdivacky      if (! IS_AGGR_TYPE (basetype))
277202375Srdivacky	{
278203954Srdivacky	  if (TYPE_MAIN_VARIANT (type) != TYPE_MAIN_VARIANT (basetype))
279202375Srdivacky	    error ("type of `%E' does not match destructor type `%T' (type was `%T')",
280249423Sdim		      exp, basetype, type);
281203954Srdivacky
282203954Srdivacky	  return cp_convert (void_type_node, exp);
283204642Srdivacky	}
284203954Srdivacky    }
285249423Sdim
286202375Srdivacky  if (TREE_CODE (basetype) == NAMESPACE_DECL)
287202375Srdivacky    {
288202375Srdivacky      error ("`%D' is a namespace", basetype);
289202375Srdivacky      return error_mark_node;
290202375Srdivacky    }
291202375Srdivacky  if (! is_aggr_type (basetype, 1))
292202375Srdivacky    return error_mark_node;
293202375Srdivacky
294202375Srdivacky  if (! IS_AGGR_TYPE (type))
295202375Srdivacky    {
296202375Srdivacky      error ("base object `%E' of scoped method call is of non-aggregate type `%T'",
297249423Sdim		exp, type);
298202375Srdivacky      return error_mark_node;
299202375Srdivacky    }
300202375Srdivacky
301202375Srdivacky  if (! binfo)
302202375Srdivacky    {
303202375Srdivacky      binfo = lookup_base (type, basetype, ba_check, NULL);
304202375Srdivacky      if (binfo == error_mark_node)
305202375Srdivacky	return error_mark_node;
306202375Srdivacky      if (! binfo)
307202375Srdivacky	error_not_base_type (basetype, type);
308202375Srdivacky    }
309202375Srdivacky
310202375Srdivacky  if (binfo)
311202375Srdivacky    {
312202375Srdivacky      if (TREE_CODE (exp) == INDIRECT_REF)
313202375Srdivacky	{
314204642Srdivacky	  decl = build_base_path (PLUS_EXPR,
315204642Srdivacky				  build_unary_op (ADDR_EXPR, exp, 0),
316202375Srdivacky				  binfo, 1);
317202375Srdivacky	  decl = build_indirect_ref (decl, NULL);
318202375Srdivacky	}
319202375Srdivacky      else
320249423Sdim	decl = build_scoped_ref (exp, basetype);
321202375Srdivacky
322202375Srdivacky      /* Call to a destructor.  */
323202375Srdivacky      if (TREE_CODE (name) == BIT_NOT_EXPR)
324202375Srdivacky	{
325202375Srdivacky	  if (! TYPE_HAS_DESTRUCTOR (TREE_TYPE (decl)))
326202375Srdivacky	    return cp_convert (void_type_node, exp);
327202375Srdivacky
328202375Srdivacky	  return build_delete (TREE_TYPE (decl), decl,
329202375Srdivacky			       sfk_complete_destructor,
330202375Srdivacky			       LOOKUP_NORMAL|LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR,
331202375Srdivacky			       0);
332202375Srdivacky	}
333202375Srdivacky
334202375Srdivacky      /* Call to a method.  */
335226633Sdim      return build_method_call (decl, name, parms, binfo,
336202375Srdivacky				LOOKUP_NORMAL|LOOKUP_NONVIRTUAL);
337202375Srdivacky    }
338202375Srdivacky  return error_mark_node;
339249423Sdim}
340202375Srdivacky
341202375Srdivacky/* We want the address of a function or method.  We avoid creating a
342249423Sdim   pointer-to-member function.  */
343226633Sdim
344249423Sdimtree
345202375Srdivackybuild_addr_func (function)
346202375Srdivacky     tree function;
347249423Sdim{
348202375Srdivacky  tree type = TREE_TYPE (function);
349202375Srdivacky
350202375Srdivacky  /* We have to do these by hand to avoid real pointer to member
351202375Srdivacky     functions.  */
352202375Srdivacky  if (TREE_CODE (type) == METHOD_TYPE)
353202375Srdivacky    {
354202375Srdivacky      tree addr;
355202375Srdivacky
356202375Srdivacky      type = build_pointer_type (type);
357202375Srdivacky
358202375Srdivacky      if (mark_addressable (function) == 0)
359202375Srdivacky	return error_mark_node;
360202375Srdivacky
361202375Srdivacky      addr = build1 (ADDR_EXPR, type, function);
362202375Srdivacky
363202375Srdivacky      /* Address of a static or external variable or function counts
364202375Srdivacky	 as a constant */
365202375Srdivacky      if (staticp (function))
366202375Srdivacky	TREE_CONSTANT (addr) = 1;
367202375Srdivacky
368202375Srdivacky      function = addr;
369202375Srdivacky    }
370202375Srdivacky  else
371202375Srdivacky    function = default_conversion (function);
372202375Srdivacky
373202375Srdivacky  return function;
374202375Srdivacky}
375202375Srdivacky
376202375Srdivacky/* Build a CALL_EXPR, we can handle FUNCTION_TYPEs, METHOD_TYPEs, or
377202375Srdivacky   POINTER_TYPE to those.  Note, pointer to member function types
378202375Srdivacky   (TYPE_PTRMEMFUNC_P) must be handled by our callers.  */
379202375Srdivacky
380202375Srdivackytree
381202375Srdivackybuild_call (function, parms)
382202375Srdivacky     tree function, parms;
383202375Srdivacky{
384202375Srdivacky  int is_constructor = 0;
385202375Srdivacky  int nothrow;
386202375Srdivacky  tree tmp;
387202375Srdivacky  tree decl;
388202375Srdivacky  tree result_type;
389202375Srdivacky
390202375Srdivacky  function = build_addr_func (function);
391202375Srdivacky
392202375Srdivacky  if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function)))
393202375Srdivacky    {
394202375Srdivacky      sorry ("unable to call pointer to member function here");
395202375Srdivacky      return error_mark_node;
396202375Srdivacky    }
397202375Srdivacky
398202375Srdivacky  result_type = TREE_TYPE (TREE_TYPE (TREE_TYPE (function)));
399202375Srdivacky
400202375Srdivacky  if (TREE_CODE (function) == ADDR_EXPR
401202375Srdivacky      && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
402202375Srdivacky    decl = TREE_OPERAND (function, 0);
403202375Srdivacky  else
404202375Srdivacky    decl = NULL_TREE;
405202375Srdivacky
406202375Srdivacky  /* We check both the decl and the type; a function may be known not to
407202375Srdivacky     throw without being declared throw().  */
408212904Sdim  nothrow = ((decl && TREE_NOTHROW (decl))
409212904Sdim	     || TYPE_NOTHROW_P (TREE_TYPE (TREE_TYPE (function))));
410212904Sdim
411212904Sdim  if (decl && TREE_THIS_VOLATILE (decl))
412212904Sdim    current_function_returns_abnormally = 1;
413202375Srdivacky
414202375Srdivacky  if (decl && TREE_DEPRECATED (decl))
415202375Srdivacky    warn_deprecated_use (decl);
416202375Srdivacky
417202375Srdivacky  if (decl && DECL_CONSTRUCTOR_P (decl))
418202375Srdivacky    is_constructor = 1;
419202375Srdivacky
420202375Srdivacky  if (decl && ! TREE_USED (decl))
421202375Srdivacky    {
422202375Srdivacky      /* We invoke build_call directly for several library functions.
423202375Srdivacky	 These may have been declared normally if we're building libgcc,
424202375Srdivacky	 so we can't just check DECL_ARTIFICIAL.  */
425202375Srdivacky      if (DECL_ARTIFICIAL (decl)
426202375Srdivacky	  || !strncmp (IDENTIFIER_POINTER (DECL_NAME (decl)), "__", 2))
427202375Srdivacky	mark_used (decl);
428202375Srdivacky      else
429202375Srdivacky	abort ();
430202375Srdivacky    }
431202375Srdivacky
432249423Sdim  /* Don't pass empty class objects by value.  This is useful
433202375Srdivacky     for tags in STL, which are used to control overload resolution.
434202375Srdivacky     We don't need to handle other cases of copying empty classes.  */
435202375Srdivacky  if (! decl || ! DECL_BUILT_IN (decl))
436202375Srdivacky    for (tmp = parms; tmp; tmp = TREE_CHAIN (tmp))
437202375Srdivacky      if (is_empty_class (TREE_TYPE (TREE_VALUE (tmp)))
438202375Srdivacky	  && ! TREE_ADDRESSABLE (TREE_TYPE (TREE_VALUE (tmp))))
439249423Sdim	{
440249423Sdim	  tree t = build (EMPTY_CLASS_EXPR, TREE_TYPE (TREE_VALUE (tmp)));
441202375Srdivacky	  TREE_VALUE (tmp) = build (COMPOUND_EXPR, TREE_TYPE (t),
442202375Srdivacky				    TREE_VALUE (tmp), t);
443202375Srdivacky	}
444249423Sdim
445202375Srdivacky  function = build_nt (CALL_EXPR, function, parms, NULL_TREE);
446226633Sdim  TREE_HAS_CONSTRUCTOR (function) = is_constructor;
447249423Sdim  TREE_TYPE (function) = result_type;
448202375Srdivacky  TREE_SIDE_EFFECTS (function) = 1;
449202375Srdivacky  TREE_NOTHROW (function) = nothrow;
450202375Srdivacky
451202375Srdivacky  return function;
452204642Srdivacky}
453202375Srdivacky
454249423Sdim/* Build something of the form ptr->method (args)
455202375Srdivacky   or object.method (args).  This can also build
456202375Srdivacky   calls to constructors, and find friends.
457202375Srdivacky
458208599Srdivacky   Member functions always take their class variable
459202375Srdivacky   as a pointer.
460202375Srdivacky
461202375Srdivacky   INSTANCE is a class instance.
462202375Srdivacky
463202375Srdivacky   NAME is the name of the method desired, usually an IDENTIFIER_NODE.
464202375Srdivacky
465202375Srdivacky   PARMS help to figure out what that NAME really refers to.
466202375Srdivacky
467226633Sdim   BASETYPE_PATH, if non-NULL, contains a chain from the type of INSTANCE
468202375Srdivacky   down to the real instance type to use for access checking.  We need this
469202375Srdivacky   information to get protected accesses correct.  This parameter is used
470202375Srdivacky   by build_member_call.
471249423Sdim
472212904Sdim   FLAGS is the logical disjunction of zero or more LOOKUP_
473212904Sdim   flags.  See cp-tree.h for more info.
474218893Sdim
475218893Sdim   If this is all OK, calls build_function_call with the resolved
476212904Sdim   member function.
477212904Sdim
478212904Sdim   This function must also handle being called to perform
479212904Sdim   initialization, promotion/coercion of arguments, and
480212904Sdim   instantiation of default parameters.
481249423Sdim
482212904Sdim   Note that NAME may refer to an instance variable name.  If
483212904Sdim   `operator()()' is defined for the type of that field, then we return
484212904Sdim   that result.  */
485212904Sdim
486202375Srdivacky#ifdef GATHER_STATISTICS
487212904Sdimextern int n_build_method_call;
488212904Sdim#endif
489212904Sdim
490212904Sdimtree
491212904Sdimbuild_method_call (instance, name, parms, basetype_path, flags)
492212904Sdim     tree instance, name, parms, basetype_path;
493212904Sdim     int flags;
494249423Sdim{
495218893Sdim  tree basetype, instance_ptr;
496218893Sdim
497218893Sdim#ifdef GATHER_STATISTICS
498218893Sdim  n_build_method_call++;
499218893Sdim#endif
500218893Sdim
501218893Sdim  if (instance == error_mark_node
502218893Sdim      || name == error_mark_node
503218893Sdim      || parms == error_mark_node
504212904Sdim      || (instance != NULL_TREE && TREE_TYPE (instance) == error_mark_node))
505202375Srdivacky    return error_mark_node;
506202375Srdivacky
507202375Srdivacky  if (processing_template_decl)
508202375Srdivacky    {
509202375Srdivacky      /* We need to process template parm names here so that tsubst catches
510202375Srdivacky	 them properly.  Other type names can wait.  */
511202375Srdivacky      if (TREE_CODE (name) == BIT_NOT_EXPR)
512202375Srdivacky	{
513202375Srdivacky	  tree type = NULL_TREE;
514202375Srdivacky
515202375Srdivacky	  if (TREE_CODE (TREE_OPERAND (name, 0)) == IDENTIFIER_NODE)
516202375Srdivacky	    type = get_aggr_from_typedef (TREE_OPERAND (name, 0), 0);
517249423Sdim	  else if (TREE_CODE (TREE_OPERAND (name, 0)) == TYPE_DECL)
518202375Srdivacky	    type = TREE_TYPE (TREE_OPERAND (name, 0));
519202375Srdivacky
520202375Srdivacky	  if (type && TREE_CODE (type) == TEMPLATE_TYPE_PARM)
521202375Srdivacky	    name = build_min_nt (BIT_NOT_EXPR, type);
522202375Srdivacky	}
523202375Srdivacky
524202375Srdivacky      return build_min_nt (METHOD_CALL_EXPR, name, instance, parms, NULL_TREE);
525202375Srdivacky    }
526202375Srdivacky
527202375Srdivacky  if (TREE_CODE (name) == BIT_NOT_EXPR)
528202375Srdivacky    {
529226633Sdim      if (parms)
530202375Srdivacky	error ("destructors take no parameters");
531202375Srdivacky      basetype = TREE_TYPE (instance);
532202375Srdivacky      if (TREE_CODE (basetype) == REFERENCE_TYPE)
533202375Srdivacky	basetype = TREE_TYPE (basetype);
534202375Srdivacky
535202375Srdivacky      if (! check_dtor_name (basetype, name))
536202375Srdivacky	error
537202375Srdivacky	  ("destructor name `~%T' does not match type `%T' of expression",
538234353Sdim	   TREE_OPERAND (name, 0), basetype);
539202375Srdivacky
540202375Srdivacky      if (! TYPE_HAS_DESTRUCTOR (complete_type (basetype)))
541202375Srdivacky	return cp_convert (void_type_node, instance);
542202375Srdivacky      instance = default_conversion (instance);
543202375Srdivacky      instance_ptr = build_unary_op (ADDR_EXPR, instance, 0);
544202375Srdivacky      return build_delete (build_pointer_type (basetype),
545202375Srdivacky			   instance_ptr, sfk_complete_destructor,
546202375Srdivacky			   LOOKUP_NORMAL|LOOKUP_DESTRUCTOR, 0);
547249423Sdim    }
548202375Srdivacky
549202375Srdivacky  return build_new_method_call (instance, name, parms, basetype_path, flags);
550202375Srdivacky}
551202375Srdivacky
552202375Srdivacky/* New overloading code.  */
553234353Sdim
554249423Sdimstruct z_candidate {
555202375Srdivacky  tree fn;
556202375Srdivacky  tree convs;
557202375Srdivacky  tree second_conv;
558202375Srdivacky  int viable;
559202375Srdivacky  tree basetype_path;
560202375Srdivacky  tree template;
561202375Srdivacky  tree warnings;
562202375Srdivacky  struct z_candidate *next;
563202375Srdivacky};
564202375Srdivacky
565202375Srdivacky#define IDENTITY_RANK 0
566202375Srdivacky#define EXACT_RANK 1
567202375Srdivacky#define PROMO_RANK 2
568249423Sdim#define STD_RANK 3
569202375Srdivacky#define PBOOL_RANK 4
570202375Srdivacky#define USER_RANK 5
571202375Srdivacky#define ELLIPSIS_RANK 6
572202375Srdivacky#define BAD_RANK 7
573202375Srdivacky
574202375Srdivacky#define ICS_RANK(NODE)				\
575202375Srdivacky  (ICS_BAD_FLAG (NODE) ? BAD_RANK   		\
576202375Srdivacky   : ICS_ELLIPSIS_FLAG (NODE) ? ELLIPSIS_RANK	\
577249423Sdim   : ICS_USER_FLAG (NODE) ? USER_RANK		\
578202375Srdivacky   : ICS_STD_RANK (NODE))
579202375Srdivacky
580226633Sdim#define ICS_STD_RANK(NODE) TREE_COMPLEXITY (NODE)
581202375Srdivacky
582249423Sdim#define ICS_USER_FLAG(NODE) TREE_LANG_FLAG_0 (NODE)
583202375Srdivacky#define ICS_ELLIPSIS_FLAG(NODE) TREE_LANG_FLAG_1 (NODE)
584202375Srdivacky#define ICS_THIS_FLAG(NODE) TREE_LANG_FLAG_2 (NODE)
585212904Sdim#define ICS_BAD_FLAG(NODE) TREE_LANG_FLAG_3 (NODE)
586202375Srdivacky
587202375Srdivacky/* In a REF_BIND or a BASE_CONV, this indicates that a temporary
588202375Srdivacky   should be created to hold the result of the conversion.  */
589202375Srdivacky#define NEED_TEMPORARY_P(NODE) TREE_LANG_FLAG_4 (NODE)
590202375Srdivacky
591202375Srdivacky#define USER_CONV_CAND(NODE) \
592202375Srdivacky  ((struct z_candidate *)WRAPPER_PTR (TREE_OPERAND (NODE, 1)))
593202375Srdivacky#define USER_CONV_FN(NODE) (USER_CONV_CAND (NODE)->fn)
594226633Sdim
595202375Srdivackyint
596202375Srdivackynull_ptr_cst_p (t)
597202375Srdivacky     tree t;
598202375Srdivacky{
599202375Srdivacky  /* [conv.ptr]
600202375Srdivacky
601234353Sdim     A null pointer constant is an integral constant expression
602234353Sdim     (_expr.const_) rvalue of integer type that evaluates to zero.  */
603202375Srdivacky  if (t == null_node
604202375Srdivacky      || (CP_INTEGRAL_TYPE_P (TREE_TYPE (t)) && integer_zerop (t)))
605202375Srdivacky    return 1;
606202375Srdivacky  return 0;
607202375Srdivacky}
608202375Srdivacky
609202375Srdivacky
610202375Srdivacky/* Returns non-zero if PARMLIST consists of only default parms and/or
611202375Srdivacky   ellipsis. */
612202375Srdivacky
613202375Srdivackyint
614202375Srdivackysufficient_parms_p (parmlist)
615202375Srdivacky     tree parmlist;
616202375Srdivacky{
617202375Srdivacky  for (; parmlist && parmlist != void_list_node;
618202375Srdivacky       parmlist = TREE_CHAIN (parmlist))
619202375Srdivacky    if (!TREE_PURPOSE (parmlist))
620202375Srdivacky      return 0;
621202375Srdivacky  return 1;
622202375Srdivacky}
623202375Srdivacky
624202375Srdivackystatic tree
625202375Srdivackybuild_conv (code, type, from)
626202375Srdivacky     enum tree_code code;
627202375Srdivacky     tree type, from;
628202375Srdivacky{
629202375Srdivacky  tree t;
630202375Srdivacky  int rank = ICS_STD_RANK (from);
631202375Srdivacky
632202375Srdivacky  /* We can't use buildl1 here because CODE could be USER_CONV, which
633202375Srdivacky     takes two arguments.  In that case, the caller is responsible for
634202375Srdivacky     filling in the second argument.  */
635202375Srdivacky  t = make_node (code);
636202375Srdivacky  TREE_TYPE (t) = type;
637202375Srdivacky  TREE_OPERAND (t, 0) = from;
638202375Srdivacky
639202375Srdivacky  switch (code)
640202375Srdivacky    {
641202375Srdivacky    case PTR_CONV:
642202375Srdivacky    case PMEM_CONV:
643202375Srdivacky    case BASE_CONV:
644202375Srdivacky    case STD_CONV:
645202375Srdivacky      if (rank < STD_RANK)
646202375Srdivacky	rank = STD_RANK;
647202375Srdivacky      break;
648202375Srdivacky
649202375Srdivacky    case QUAL_CONV:
650202375Srdivacky      if (rank < EXACT_RANK)
651226633Sdim	rank = EXACT_RANK;
652202375Srdivacky
653202375Srdivacky    default:
654202375Srdivacky      break;
655249423Sdim    }
656202375Srdivacky  ICS_STD_RANK (t) = rank;
657202375Srdivacky  ICS_USER_FLAG (t) = ICS_USER_FLAG (from);
658249423Sdim  ICS_BAD_FLAG (t) = ICS_BAD_FLAG (from);
659202375Srdivacky  return t;
660239462Sdim}
661239462Sdim
662202375Srdivacky/* If T is a REFERENCE_TYPE return the type to which T refers.
663249423Sdim   Otherwise, return T itself.  */
664202375Srdivacky
665202375Srdivackystatic tree
666202375Srdivackynon_reference (t)
667249423Sdim     tree t;
668202375Srdivacky{
669202375Srdivacky  if (TREE_CODE (t) == REFERENCE_TYPE)
670202375Srdivacky    t = TREE_TYPE (t);
671202375Srdivacky  return t;
672202375Srdivacky}
673202375Srdivacky
674202375Srdivackytree
675202375Srdivackystrip_top_quals (t)
676202375Srdivacky     tree t;
677202375Srdivacky{
678202375Srdivacky  if (TREE_CODE (t) == ARRAY_TYPE)
679202375Srdivacky    return t;
680202375Srdivacky  return TYPE_MAIN_VARIANT (t);
681202375Srdivacky}
682202375Srdivacky
683202375Srdivacky/* Returns the standard conversion path (see [conv]) from type FROM to type
684202375Srdivacky   TO, if any.  For proper handling of null pointer constants, you must
685202375Srdivacky   also pass the expression EXPR to convert from.  */
686249423Sdim
687202375Srdivackystatic tree
688202375Srdivackystandard_conversion (to, from, expr)
689202375Srdivacky     tree to, from, expr;
690202375Srdivacky{
691202375Srdivacky  enum tree_code fcode, tcode;
692202375Srdivacky  tree conv;
693202375Srdivacky  int fromref = 0;
694202375Srdivacky
695202375Srdivacky  if (TREE_CODE (to) == REFERENCE_TYPE)
696202375Srdivacky    to = TREE_TYPE (to);
697202375Srdivacky  if (TREE_CODE (from) == REFERENCE_TYPE)
698202375Srdivacky    {
699249423Sdim      fromref = 1;
700202375Srdivacky      from = TREE_TYPE (from);
701202375Srdivacky    }
702249423Sdim  to = strip_top_quals (to);
703263508Sdim  from = strip_top_quals (from);
704263508Sdim
705263508Sdim  if ((TYPE_PTRFN_P (to) || TYPE_PTRMEMFUNC_P (to))
706263508Sdim      && expr && type_unknown_p (expr))
707263508Sdim    {
708263508Sdim      expr = instantiate_type (to, expr, tf_none);
709263508Sdim      if (expr == error_mark_node)
710263508Sdim	return NULL_TREE;
711263508Sdim      from = TREE_TYPE (expr);
712263508Sdim    }
713263508Sdim
714202375Srdivacky  fcode = TREE_CODE (from);
715202375Srdivacky  tcode = TREE_CODE (to);
716202375Srdivacky
717202375Srdivacky  conv = build1 (IDENTITY_CONV, from, expr);
718202375Srdivacky
719202375Srdivacky  if (fcode == FUNCTION_TYPE)
720202375Srdivacky    {
721202375Srdivacky      from = build_pointer_type (from);
722202375Srdivacky      fcode = TREE_CODE (from);
723202375Srdivacky      conv = build_conv (LVALUE_CONV, from, conv);
724202375Srdivacky    }
725202375Srdivacky  else if (fcode == ARRAY_TYPE)
726202375Srdivacky    {
727202375Srdivacky      from = build_pointer_type (TREE_TYPE (from));
728202375Srdivacky      fcode = TREE_CODE (from);
729202375Srdivacky      conv = build_conv (LVALUE_CONV, from, conv);
730202375Srdivacky    }
731202375Srdivacky  else if (fromref || (expr && lvalue_p (expr)))
732202375Srdivacky    conv = build_conv (RVALUE_CONV, from, conv);
733202375Srdivacky
734202375Srdivacky   /* Allow conversion between `__complex__' data types  */
735249423Sdim  if (tcode == COMPLEX_TYPE && fcode == COMPLEX_TYPE)
736202375Srdivacky    {
737202375Srdivacky      /* The standard conversion sequence to convert FROM to TO is
738202375Srdivacky         the standard conversion sequence to perform componentwise
739202375Srdivacky         conversion.  */
740202375Srdivacky      tree part_conv = standard_conversion
741202375Srdivacky        (TREE_TYPE (to), TREE_TYPE (from), NULL_TREE);
742202375Srdivacky
743202375Srdivacky      if (part_conv)
744202375Srdivacky        {
745202375Srdivacky          conv = build_conv (TREE_CODE (part_conv), to, conv);
746202375Srdivacky          ICS_STD_RANK (conv) = ICS_STD_RANK (part_conv);
747202375Srdivacky        }
748202375Srdivacky      else
749202375Srdivacky        conv = NULL_TREE;
750202375Srdivacky
751202375Srdivacky      return conv;
752202375Srdivacky    }
753202375Srdivacky
754202375Srdivacky  if (same_type_p (from, to))
755202375Srdivacky    return conv;
756202375Srdivacky
757202375Srdivacky  if ((tcode == POINTER_TYPE || TYPE_PTRMEMFUNC_P (to))
758249423Sdim      && expr && null_ptr_cst_p (expr))
759202375Srdivacky    {
760202375Srdivacky      conv = build_conv (STD_CONV, to, conv);
761202375Srdivacky    }
762249423Sdim  else if ((tcode == INTEGER_TYPE && fcode == POINTER_TYPE)
763202375Srdivacky	   || (tcode == POINTER_TYPE && fcode == INTEGER_TYPE))
764202375Srdivacky    {
765202375Srdivacky      /* For backwards brain damage compatibility, allow interconversion of
766202375Srdivacky	 pointers and integers with a pedwarn.  */
767249423Sdim      conv = build_conv (STD_CONV, to, conv);
768202375Srdivacky      ICS_BAD_FLAG (conv) = 1;
769202375Srdivacky    }
770202375Srdivacky  else if (tcode == ENUMERAL_TYPE && fcode == INTEGER_TYPE
771249423Sdim	   && TYPE_PRECISION (to) == TYPE_PRECISION (from))
772202375Srdivacky    {
773226633Sdim      /* For backwards brain damage compatibility, allow interconversion of
774249423Sdim	 enums and integers with a pedwarn.  */
775202375Srdivacky      conv = build_conv (STD_CONV, to, conv);
776202375Srdivacky      ICS_BAD_FLAG (conv) = 1;
777202375Srdivacky    }
778202375Srdivacky  else if (tcode == POINTER_TYPE && fcode == POINTER_TYPE)
779202375Srdivacky    {
780204642Srdivacky      enum tree_code ufcode = TREE_CODE (TREE_TYPE (from));
781249423Sdim      enum tree_code utcode = TREE_CODE (TREE_TYPE (to));
782202375Srdivacky
783202375Srdivacky      if (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (from),
784249423Sdim						     TREE_TYPE (to)))
785202375Srdivacky	;
786202375Srdivacky      else if (utcode == VOID_TYPE && ufcode != OFFSET_TYPE
787202375Srdivacky	       && ufcode != FUNCTION_TYPE)
788202375Srdivacky	{
789202375Srdivacky	  from = build_pointer_type
790249423Sdim	    (cp_build_qualified_type (void_type_node,
791202375Srdivacky				      cp_type_quals (TREE_TYPE (from))));
792202375Srdivacky	  conv = build_conv (PTR_CONV, from, conv);
793249423Sdim	}
794202375Srdivacky      else if (ufcode == OFFSET_TYPE && utcode == OFFSET_TYPE)
795202375Srdivacky	{
796202375Srdivacky	  tree fbase = TYPE_OFFSET_BASETYPE (TREE_TYPE (from));
797202375Srdivacky	  tree tbase = TYPE_OFFSET_BASETYPE (TREE_TYPE (to));
798202375Srdivacky
799249423Sdim	  if (DERIVED_FROM_P (fbase, tbase)
800202375Srdivacky	      && (same_type_ignoring_top_level_qualifiers_p
801202375Srdivacky		  (TREE_TYPE (TREE_TYPE (from)),
802202375Srdivacky		   TREE_TYPE (TREE_TYPE (to)))))
803202375Srdivacky	    {
804202375Srdivacky	      from = build_offset_type (tbase, TREE_TYPE (TREE_TYPE (from)));
805202375Srdivacky	      from = build_pointer_type (from);
806202375Srdivacky	      conv = build_conv (PMEM_CONV, from, conv);
807202375Srdivacky	    }
808202375Srdivacky	}
809202375Srdivacky      else if (IS_AGGR_TYPE (TREE_TYPE (from))
810202375Srdivacky	       && IS_AGGR_TYPE (TREE_TYPE (to)))
811249423Sdim	{
812202375Srdivacky	  if (DERIVED_FROM_P (TREE_TYPE (to), TREE_TYPE (from)))
813202375Srdivacky	    {
814202375Srdivacky	      from =
815202375Srdivacky		cp_build_qualified_type (TREE_TYPE (to),
816202375Srdivacky					 cp_type_quals (TREE_TYPE (from)));
817202375Srdivacky	      from = build_pointer_type (from);
818202375Srdivacky	      conv = build_conv (PTR_CONV, from, conv);
819202375Srdivacky	    }
820202375Srdivacky	}
821202375Srdivacky
822202375Srdivacky      if (same_type_p (from, to))
823202375Srdivacky	/* OK */;
824202375Srdivacky      else if (comp_ptr_ttypes (TREE_TYPE (to), TREE_TYPE (from)))
825202375Srdivacky	conv = build_conv (QUAL_CONV, to, conv);
826202375Srdivacky      else if (expr && string_conv_p (to, expr, 0))
827202375Srdivacky	/* converting from string constant to char *.  */
828249423Sdim	conv = build_conv (QUAL_CONV, to, conv);
829202375Srdivacky      else if (ptr_reasonably_similar (TREE_TYPE (to), TREE_TYPE (from)))
830202375Srdivacky	{
831202375Srdivacky	  conv = build_conv (PTR_CONV, to, conv);
832202375Srdivacky	  ICS_BAD_FLAG (conv) = 1;
833202375Srdivacky	}
834202375Srdivacky      else
835226633Sdim	return 0;
836202375Srdivacky
837249423Sdim      from = to;
838202375Srdivacky    }
839202375Srdivacky  else if (TYPE_PTRMEMFUNC_P (to) && TYPE_PTRMEMFUNC_P (from))
840202375Srdivacky    {
841202375Srdivacky      tree fromfn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (from));
842202375Srdivacky      tree tofn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (to));
843202375Srdivacky      tree fbase = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (fromfn)));
844202375Srdivacky      tree tbase = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (tofn)));
845202375Srdivacky
846202375Srdivacky      if (!DERIVED_FROM_P (fbase, tbase)
847202375Srdivacky	  || !same_type_p (TREE_TYPE (fromfn), TREE_TYPE (tofn))
848202375Srdivacky	  || !compparms (TREE_CHAIN (TYPE_ARG_TYPES (fromfn)),
849202375Srdivacky			 TREE_CHAIN (TYPE_ARG_TYPES (tofn)))
850202375Srdivacky	  || cp_type_quals (fbase) != cp_type_quals (tbase))
851202375Srdivacky	return 0;
852202375Srdivacky
853202375Srdivacky      from = cp_build_qualified_type (tbase, cp_type_quals (fbase));
854202375Srdivacky      from = build_cplus_method_type (from, TREE_TYPE (fromfn),
855202375Srdivacky				      TREE_CHAIN (TYPE_ARG_TYPES (fromfn)));
856202375Srdivacky      from = build_ptrmemfunc_type (build_pointer_type (from));
857202375Srdivacky      conv = build_conv (PMEM_CONV, from, conv);
858202375Srdivacky    }
859202375Srdivacky  else if (tcode == BOOLEAN_TYPE)
860202375Srdivacky    {
861202375Srdivacky      if (! (INTEGRAL_CODE_P (fcode) || fcode == REAL_TYPE
862202375Srdivacky	     || fcode == POINTER_TYPE || TYPE_PTRMEMFUNC_P (from)))
863202375Srdivacky	return 0;
864202375Srdivacky
865202375Srdivacky      conv = build_conv (STD_CONV, to, conv);
866202375Srdivacky      if (fcode == POINTER_TYPE
867202375Srdivacky	  || (TYPE_PTRMEMFUNC_P (from) && ICS_STD_RANK (conv) < PBOOL_RANK))
868202375Srdivacky	ICS_STD_RANK (conv) = PBOOL_RANK;
869202375Srdivacky    }
870202375Srdivacky  /* We don't check for ENUMERAL_TYPE here because there are no standard
871202375Srdivacky     conversions to enum type.  */
872202375Srdivacky  else if (tcode == INTEGER_TYPE || tcode == BOOLEAN_TYPE
873202375Srdivacky	   || tcode == REAL_TYPE)
874202375Srdivacky    {
875202375Srdivacky      if (! (INTEGRAL_CODE_P (fcode) || fcode == REAL_TYPE))
876202375Srdivacky	return 0;
877202375Srdivacky      conv = build_conv (STD_CONV, to, conv);
878202375Srdivacky
879202375Srdivacky      /* Give this a better rank if it's a promotion.  */
880202375Srdivacky      if (to == type_promotes_to (from)
881202375Srdivacky	  && ICS_STD_RANK (TREE_OPERAND (conv, 0)) <= PROMO_RANK)
882226633Sdim	ICS_STD_RANK (conv) = PROMO_RANK;
883202375Srdivacky    }
884202375Srdivacky  else if (IS_AGGR_TYPE (to) && IS_AGGR_TYPE (from)
885202375Srdivacky	   && is_properly_derived_from (from, to))
886202375Srdivacky    {
887202375Srdivacky      if (TREE_CODE (conv) == RVALUE_CONV)
888202375Srdivacky	conv = TREE_OPERAND (conv, 0);
889203954Srdivacky      conv = build_conv (BASE_CONV, to, conv);
890202375Srdivacky      /* The derived-to-base conversion indicates the initialization
891202375Srdivacky	 of a parameter with base type from an object of a derived
892202375Srdivacky	 type.  A temporary object is created to hold the result of
893202375Srdivacky	 the conversion.  */
894202375Srdivacky      NEED_TEMPORARY_P (conv) = 1;
895249423Sdim    }
896202375Srdivacky  else
897202375Srdivacky    return 0;
898202375Srdivacky
899221345Sdim  return conv;
900221345Sdim}
901221345Sdim
902221345Sdim/* Returns non-zero if T1 is reference-related to T2.  */
903221345Sdim
904221345Sdimstatic int
905221345Sdimreference_related_p (t1, t2)
906221345Sdim     tree t1;
907221345Sdim     tree t2;
908221345Sdim{
909221345Sdim  t1 = TYPE_MAIN_VARIANT (t1);
910221345Sdim  t2 = TYPE_MAIN_VARIANT (t2);
911221345Sdim
912221345Sdim  /* [dcl.init.ref]
913221345Sdim
914221345Sdim     Given types "cv1 T1" and "cv2 T2," "cv1 T1" is reference-related
915226633Sdim     to "cv2 T2" if T1 is the same type as T2, or T1 is a base class
916221345Sdim     of T2.  */
917221345Sdim  return (same_type_p (t1, t2)
918221345Sdim	  || (CLASS_TYPE_P (t1) && CLASS_TYPE_P (t2)
919221345Sdim	      && DERIVED_FROM_P (t1, t2)));
920221345Sdim}
921221345Sdim
922221345Sdim/* Returns non-zero if T1 is reference-compatible with T2.  */
923221345Sdim
924221345Sdimstatic int
925221345Sdimreference_compatible_p (t1, t2)
926221345Sdim     tree t1;
927221345Sdim     tree t2;
928221345Sdim{
929234353Sdim  /* [dcl.init.ref]
930221345Sdim
931221345Sdim     "cv1 T1" is reference compatible with "cv2 T2" if T1 is
932221345Sdim     reference-related to T2 and cv1 is the same cv-qualification as,
933221345Sdim     or greater cv-qualification than, cv2.  */
934221345Sdim  return (reference_related_p (t1, t2)
935221345Sdim	  && at_least_as_qualified_p (t1, t2));
936221345Sdim}
937221345Sdim
938221345Sdim/* Determine whether or not the EXPR (of class type S) can be
939221345Sdim   converted to T as in [over.match.ref].  */
940221345Sdim
941221345Sdimstatic tree
942221345Sdimconvert_class_to_reference (t, s, expr)
943221345Sdim     tree t;
944221345Sdim     tree s;
945221345Sdim     tree expr;
946221345Sdim{
947221345Sdim  tree conversions;
948221345Sdim  tree arglist;
949221345Sdim  tree conv;
950221345Sdim  struct z_candidate *candidates;
951221345Sdim  struct z_candidate *cand;
952221345Sdim
953221345Sdim  /* [over.match.ref]
954221345Sdim
955221345Sdim     Assuming that "cv1 T" is the underlying type of the reference
956221345Sdim     being initialized, and "cv S" is the type of the initializer
957221345Sdim     expression, with S a class type, the candidate functions are
958221345Sdim     selected as follows:
959221345Sdim
960221345Sdim     --The conversion functions of S and its base classes are
961221345Sdim       considered.  Those that are not hidden within S and yield type
962221345Sdim       "reference to cv2 T2", where "cv1 T" is reference-compatible
963221345Sdim       (_dcl.init.ref_) with "cv2 T2", are candidate functions.
964221345Sdim
965221345Sdim     The argument list has one argument, which is the initializer
966221345Sdim     expression.  */
967221345Sdim
968221345Sdim  candidates = 0;
969221345Sdim
970221345Sdim  /* Conceptually, we should take the address of EXPR and put it in
971221345Sdim     the argument list.  Unfortunately, however, that can result in
972221345Sdim     error messages, which we should not issue now because we are just
973221345Sdim     trying to find a conversion operator.  Therefore, we use NULL,
974221345Sdim     cast to the appropriate type.  */
975221345Sdim  arglist = build_int_2 (0, 0);
976221345Sdim  TREE_TYPE (arglist) = build_pointer_type (s);
977221345Sdim  arglist = build_tree_list (NULL_TREE, arglist);
978221345Sdim
979226633Sdim  for (conversions = lookup_conversions (s);
980221345Sdim       conversions;
981221345Sdim       conversions = TREE_CHAIN (conversions))
982226633Sdim    {
983221345Sdim      tree fns = TREE_VALUE (conversions);
984221345Sdim
985221345Sdim      for (; fns; fns = OVL_NEXT (fns))
986221345Sdim	{
987221345Sdim	  tree f = OVL_CURRENT (fns);
988221345Sdim	  tree t2 = TREE_TYPE (TREE_TYPE (f));
989221345Sdim	  struct z_candidate *old_candidates = candidates;
990221345Sdim
991221345Sdim	  /* If this is a template function, try to get an exact
992221345Sdim             match.  */
993221345Sdim	  if (TREE_CODE (f) == TEMPLATE_DECL)
994202375Srdivacky	    {
995202375Srdivacky	      candidates
996202375Srdivacky		= add_template_candidate (candidates,
997202375Srdivacky					  f, s,
998202375Srdivacky					  NULL_TREE,
999202375Srdivacky					  arglist,
1000202375Srdivacky					  build_reference_type (t),
1001202375Srdivacky					  LOOKUP_NORMAL,
1002226633Sdim					  DEDUCE_CONV);
1003202375Srdivacky
1004202375Srdivacky	      if (candidates != old_candidates)
1005202375Srdivacky		{
1006202375Srdivacky		  /* Now, see if the conversion function really returns
1007202375Srdivacky		     an lvalue of the appropriate type.  From the
1008249423Sdim		     point of view of unification, simply returning an
1009202375Srdivacky		     rvalue of the right type is good enough.  */
1010202375Srdivacky		  f = candidates->fn;
1011249423Sdim		  t2 = TREE_TYPE (TREE_TYPE (f));
1012239462Sdim		  if (TREE_CODE (t2) != REFERENCE_TYPE
1013239462Sdim		      || !reference_compatible_p (t, TREE_TYPE (t2)))
1014202375Srdivacky		    candidates = candidates->next;
1015249423Sdim		}
1016202375Srdivacky	    }
1017202375Srdivacky	  else if (TREE_CODE (t2) == REFERENCE_TYPE
1018202375Srdivacky		   && reference_compatible_p (t, TREE_TYPE (t2)))
1019202375Srdivacky	    candidates
1020202375Srdivacky	      = add_function_candidate (candidates, f, s, arglist,
1021202375Srdivacky					LOOKUP_NORMAL);
1022202375Srdivacky
1023202375Srdivacky	  if (candidates != old_candidates)
1024202375Srdivacky	    candidates->basetype_path = TYPE_BINFO (s);
1025202375Srdivacky	}
1026202375Srdivacky    }
1027202375Srdivacky
1028202375Srdivacky  /* If none of the conversion functions worked out, let our caller
1029202375Srdivacky     know.  */
1030202375Srdivacky  if (!any_viable (candidates))
1031202375Srdivacky    return NULL_TREE;
1032202375Srdivacky
1033202375Srdivacky  candidates = splice_viable (candidates);
1034249423Sdim  cand = tourney (candidates);
1035202375Srdivacky  if (!cand)
1036202375Srdivacky    return NULL_TREE;
1037249423Sdim
1038202375Srdivacky  conv = build1 (IDENTITY_CONV, s, expr);
1039202375Srdivacky  conv = build_conv (USER_CONV, TREE_TYPE (TREE_TYPE (cand->fn)),
1040202375Srdivacky		     conv);
1041249423Sdim  TREE_OPERAND (conv, 1) = build_ptr_wrapper (cand);
1042202375Srdivacky  ICS_USER_FLAG (conv) = 1;
1043202375Srdivacky  if (cand->viable == -1)
1044202375Srdivacky    ICS_BAD_FLAG (conv) = 1;
1045202375Srdivacky  cand->second_conv = conv;
1046202375Srdivacky
1047202375Srdivacky  return conv;
1048202375Srdivacky}
1049202375Srdivacky
1050202375Srdivacky/* A reference of the indicated TYPE is being bound directly to the
1051202375Srdivacky   expression represented by the implicit conversion sequence CONV.
1052202375Srdivacky   Return a conversion sequence for this binding.  */
1053202375Srdivacky
1054202375Srdivackystatic tree
1055249423Sdimdirect_reference_binding (type, conv)
1056202375Srdivacky     tree type;
1057202375Srdivacky     tree conv;
1058202375Srdivacky{
1059202375Srdivacky  tree t = TREE_TYPE (type);
1060249423Sdim
1061249423Sdim  /* [over.ics.rank]
1062202375Srdivacky
1063202375Srdivacky     When a parameter of reference type binds directly
1064249423Sdim     (_dcl.init.ref_) to an argument expression, the implicit
1065202375Srdivacky     conversion sequence is the identity conversion, unless the
1066202375Srdivacky     argument expression has a type that is a derived class of the
1067249423Sdim     parameter type, in which case the implicit conversion sequence is
1068249423Sdim     a derived-to-base Conversion.
1069202375Srdivacky
1070202375Srdivacky     If the parameter binds directly to the result of applying a
1071202375Srdivacky     conversion function to the argument expression, the implicit
1072249423Sdim     conversion sequence is a user-defined conversion sequence
1073202375Srdivacky     (_over.ics.user_), with the second standard conversion sequence
1074226633Sdim     either an identity conversion or, if the conversion function
1075202375Srdivacky     returns an entity of a type that is a derived class of the
1076202375Srdivacky     parameter type, a derived-to-base conversion.  */
1077202375Srdivacky  if (!same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (conv)))
1078202375Srdivacky    {
1079202375Srdivacky      /* Represent the derived-to-base conversion.  */
1080204642Srdivacky      conv = build_conv (BASE_CONV, t, conv);
1081202375Srdivacky      /* We will actually be binding to the base-class subobject in
1082202375Srdivacky	 the derived class, so we mark this conversion appropriately.
1083202375Srdivacky	 That way, convert_like knows not to generate a temporary.  */
1084202375Srdivacky      NEED_TEMPORARY_P (conv) = 0;
1085202375Srdivacky    }
1086202375Srdivacky  return build_conv (REF_BIND, type, conv);
1087202375Srdivacky}
1088202375Srdivacky
1089202375Srdivacky/* Returns the conversion path from type FROM to reference type TO for
1090202375Srdivacky   purposes of reference binding.  For lvalue binding, either pass a
1091202375Srdivacky   reference type to FROM or an lvalue expression to EXPR.  If the
1092202375Srdivacky   reference will be bound to a temporary, NEED_TEMPORARY_P is set for
1093202375Srdivacky   the conversion returned.  */
1094202375Srdivacky
1095249423Sdimstatic tree
1096202375Srdivackyreference_binding (rto, rfrom, expr, flags)
1097202375Srdivacky     tree rto, rfrom, expr;
1098202375Srdivacky     int flags;
1099202375Srdivacky{
1100202375Srdivacky  tree conv = NULL_TREE;
1101202375Srdivacky  tree to = TREE_TYPE (rto);
1102202878Srdivacky  tree from = rfrom;
1103202878Srdivacky  int related_p;
1104202878Srdivacky  int compatible_p;
1105202878Srdivacky  cp_lvalue_kind lvalue_p = clk_none;
1106202878Srdivacky
1107202878Srdivacky  if (TREE_CODE (to) == FUNCTION_TYPE && expr && type_unknown_p (expr))
1108249423Sdim    {
1109202878Srdivacky      expr = instantiate_type (to, expr, tf_none);
1110202878Srdivacky      if (expr == error_mark_node)
1111202878Srdivacky	return NULL_TREE;
1112202878Srdivacky      from = TREE_TYPE (expr);
1113202878Srdivacky    }
1114218893Sdim
1115221345Sdim  if (TREE_CODE (from) == REFERENCE_TYPE)
1116221345Sdim    {
1117218893Sdim      /* Anything with reference type is an lvalue.  */
1118202375Srdivacky      lvalue_p = clk_ordinary;
1119202375Srdivacky      from = TREE_TYPE (from);
1120202375Srdivacky    }
1121202375Srdivacky  else if (expr)
1122202375Srdivacky    lvalue_p = real_lvalue_p (expr);
1123202375Srdivacky
1124202375Srdivacky  /* Figure out whether or not the types are reference-related and
1125202375Srdivacky     reference compatible.  We have do do this after stripping
1126202375Srdivacky     references from FROM.  */
1127202375Srdivacky  related_p = reference_related_p (to, from);
1128202375Srdivacky  compatible_p = reference_compatible_p (to, from);
1129202375Srdivacky
1130202375Srdivacky  if (lvalue_p && compatible_p)
1131202375Srdivacky    {
1132202375Srdivacky      /* [dcl.init.ref]
1133202375Srdivacky
1134202375Srdivacky	 If the initializer expression
1135202375Srdivacky
1136202375Srdivacky	 -- is an lvalue (but not an lvalue for a bit-field), and "cv1 T1"
1137202375Srdivacky	    is reference-compatible with "cv2 T2,"
1138202375Srdivacky
1139202375Srdivacky	 the reference is bound directly to the initializer exprssion
1140202375Srdivacky	 lvalue.  */
1141202375Srdivacky      conv = build1 (IDENTITY_CONV, from, expr);
1142202375Srdivacky      conv = direct_reference_binding (rto, conv);
1143202375Srdivacky      if ((lvalue_p & clk_bitfield) != 0
1144249423Sdim	  && CP_TYPE_CONST_NON_VOLATILE_P (to))
1145202375Srdivacky	/* For the purposes of overload resolution, we ignore the fact
1146202375Srdivacky	   this expression is a bitfield. (In particular,
1147202375Srdivacky	   [over.ics.ref] says specifically that a function with a
1148202375Srdivacky	   non-const reference parameter is viable even if the
1149202375Srdivacky	   argument is a bitfield.)
1150202375Srdivacky
1151202375Srdivacky	   However, when we actually call the function we must create
1152202375Srdivacky	   a temporary to which to bind the reference.  If the
1153202375Srdivacky	   reference is volatile, or isn't const, then we cannot make
1154202375Srdivacky	   a temporary, so we just issue an error when the conversion
1155202375Srdivacky	   actually occurs.  */
1156202375Srdivacky	NEED_TEMPORARY_P (conv) = 1;
1157202375Srdivacky      return conv;
1158202375Srdivacky    }
1159202375Srdivacky  else if (CLASS_TYPE_P (from) && !(flags & LOOKUP_NO_CONVERSION))
1160202375Srdivacky    {
1161202375Srdivacky      /* [dcl.init.ref]
1162202375Srdivacky
1163202375Srdivacky	 If the initializer exprsesion
1164202375Srdivacky
1165202375Srdivacky	 -- has a class type (i.e., T2 is a class type) can be
1166249423Sdim	    implicitly converted to an lvalue of type "cv3 T3," where
1167202375Srdivacky	    "cv1 T1" is reference-compatible with "cv3 T3".  (this
1168202375Srdivacky	    conversion is selected by enumerating the applicable
1169202375Srdivacky	    conversion functions (_over.match.ref_) and choosing the
1170202375Srdivacky	    best one through overload resolution.  (_over.match_).
1171202375Srdivacky
1172202375Srdivacky        the reference is bound to the lvalue result of the conversion
1173234353Sdim	in the second case.  */
1174234353Sdim      conv = convert_class_to_reference (to, from, expr);
1175234353Sdim      if (conv)
1176202375Srdivacky	return direct_reference_binding (rto, conv);
1177202375Srdivacky    }
1178202375Srdivacky
1179202375Srdivacky  /* From this point on, we conceptually need temporaries, even if we
1180202375Srdivacky     elide them.  Only the cases above are "direct bindings".  */
1181202375Srdivacky  if (flags & LOOKUP_NO_TEMP_BIND)
1182202375Srdivacky    return NULL_TREE;
1183202375Srdivacky
1184202375Srdivacky  /* [over.ics.rank]
1185249423Sdim
1186202375Srdivacky     When a parameter of reference type is not bound directly to an
1187202375Srdivacky     argument expression, the conversion sequence is the one required
1188202375Srdivacky     to convert the argument expression to the underlying type of the
1189202375Srdivacky     reference according to _over.best.ics_.  Conceptually, this
1190202375Srdivacky     conversion sequence corresponds to copy-initializing a temporary
1191202375Srdivacky     of the underlying type with the argument expression.  Any
1192249423Sdim     difference in top-level cv-qualification is subsumed by the
1193202375Srdivacky     initialization itself and does not constitute a conversion.  */
1194202375Srdivacky
1195202375Srdivacky  /* [dcl.init.ref]
1196202375Srdivacky
1197202375Srdivacky     Otherwise, the reference shall be to a non-volatile const type.  */
1198202375Srdivacky  if (!CP_TYPE_CONST_NON_VOLATILE_P (to))
1199202375Srdivacky    return NULL_TREE;
1200202375Srdivacky
1201202375Srdivacky  /* [dcl.init.ref]
1202202375Srdivacky
1203202375Srdivacky     If the initializer expression is an rvalue, with T2 a class type,
1204202375Srdivacky     and "cv1 T1" is reference-compatible with "cv2 T2", the reference
1205202375Srdivacky     is bound in one of the following ways:
1206226633Sdim
1207202375Srdivacky     -- The reference is bound to the object represented by the rvalue
1208202375Srdivacky        or to a sub-object within that object.
1209249423Sdim
1210202375Srdivacky     In this case, the implicit conversion sequence is supposed to be
1211202375Srdivacky     same as we would obtain by generating a temporary.  Fortunately,
1212202375Srdivacky     if the types are reference compatible, then this is either an
1213202375Srdivacky     identity conversion or the derived-to-base conversion, just as
1214202375Srdivacky     for direct binding.  */
1215202375Srdivacky  if (CLASS_TYPE_P (from) && compatible_p)
1216202375Srdivacky    {
1217202375Srdivacky      conv = build1 (IDENTITY_CONV, from, expr);
1218202375Srdivacky      return direct_reference_binding (rto, conv);
1219202375Srdivacky    }
1220202375Srdivacky
1221249423Sdim  /* [dcl.init.ref]
1222202375Srdivacky
1223249423Sdim     Otherwise, a temporary of type "cv1 T1" is created and
1224249423Sdim     initialized from the initializer expression using the rules for a
1225249423Sdim     non-reference copy initialization.  If T1 is reference-related to
1226249423Sdim     T2, cv1 must be the same cv-qualification as, or greater
1227249423Sdim     cv-qualification than, cv2; otherwise, the program is ill-formed.  */
1228249423Sdim  if (related_p && !at_least_as_qualified_p (to, from))
1229249423Sdim    return NULL_TREE;
1230202375Srdivacky
1231249423Sdim  conv = implicit_conversion (to, from, expr, flags);
1232263508Sdim  if (!conv)
1233263508Sdim    return NULL_TREE;
1234263508Sdim
1235263508Sdim  conv = build_conv (REF_BIND, rto, conv);
1236263508Sdim  /* This reference binding, unlike those above, requires the
1237263508Sdim     creation of a temporary.  */
1238263508Sdim  NEED_TEMPORARY_P (conv) = 1;
1239263508Sdim
1240263508Sdim  return conv;
1241263508Sdim}
1242263508Sdim
1243263508Sdim/* Returns the implicit conversion sequence (see [over.ics]) from type FROM
1244263508Sdim   to type TO.  The optional expression EXPR may affect the conversion.
1245249423Sdim   FLAGS are the usual overloading flags.  Only LOOKUP_NO_CONVERSION is
1246249423Sdim   significant.  */
1247249423Sdim
1248249423Sdimstatic tree
1249249423Sdimimplicit_conversion (to, from, expr, flags)
1250249423Sdim     tree to, from, expr;
1251249423Sdim     int flags;
1252249423Sdim{
1253249423Sdim  tree conv;
1254249423Sdim  struct z_candidate *cand;
1255249423Sdim
1256249423Sdim  /* Resolve expressions like `A::p' that we thought might become
1257249423Sdim     pointers-to-members.  */
1258249423Sdim  if (expr && TREE_CODE (expr) == OFFSET_REF)
1259249423Sdim    {
1260249423Sdim      expr = resolve_offset_ref (expr);
1261249423Sdim      from = TREE_TYPE (expr);
1262249423Sdim    }
1263249423Sdim
1264212904Sdim  if (from == error_mark_node || to == error_mark_node
1265263508Sdim      || expr == error_mark_node)
1266263508Sdim    return NULL_TREE;
1267263508Sdim
1268263508Sdim  /* Make sure both the FROM and TO types are complete so that
1269212904Sdim     user-defined conversions are available.  */
1270234353Sdim  complete_type (from);
1271263508Sdim  complete_type (to);
1272263508Sdim
1273224145Sdim  if (TREE_CODE (to) == REFERENCE_TYPE)
1274224145Sdim    conv = reference_binding (to, from, expr, flags);
1275212904Sdim  else
1276212904Sdim    conv = standard_conversion (to, from, expr);
1277212904Sdim
1278212904Sdim  if (conv)
1279212904Sdim    ;
1280212904Sdim  else if (expr != NULL_TREE
1281212904Sdim	   && (IS_AGGR_TYPE (from)
1282212904Sdim	       || IS_AGGR_TYPE (to))
1283263508Sdim	   && (flags & LOOKUP_NO_CONVERSION) == 0)
1284263508Sdim    {
1285263508Sdim      cand = build_user_type_conversion_1
1286263508Sdim	(to, expr, LOOKUP_ONLYCONVERTING);
1287263508Sdim      if (cand)
1288212904Sdim	conv = cand->second_conv;
1289212904Sdim
1290212904Sdim      /* We used to try to bind a reference to a temporary here, but that
1291249423Sdim	 is now handled by the recursive call to this function at the end
1292249423Sdim	 of reference_binding.  */
1293212904Sdim    }
1294223017Sdim
1295212904Sdim  return conv;
1296212904Sdim}
1297212904Sdim
1298212904Sdim/* Add a new entry to the list of candidates.  Used by the add_*_candidate
1299249423Sdim   functions.  */
1300202375Srdivacky
1301202375Srdivackystatic struct z_candidate *
1302202375Srdivackyadd_candidate (candidates, fn, convs, viable)
1303202375Srdivacky     struct z_candidate *candidates;
1304202375Srdivacky     tree fn, convs;
1305202375Srdivacky     int viable;
1306202375Srdivacky{
1307202375Srdivacky  struct z_candidate *cand
1308202375Srdivacky    = (struct z_candidate *) ggc_alloc_cleared (sizeof (struct z_candidate));
1309202375Srdivacky
1310202375Srdivacky  cand->fn = fn;
1311202375Srdivacky  cand->convs = convs;
1312202375Srdivacky  cand->viable = viable;
1313202375Srdivacky  cand->next = candidates;
1314202375Srdivacky
1315202375Srdivacky  return cand;
1316202375Srdivacky}
1317249423Sdim
1318202375Srdivacky/* Create an overload candidate for the function or method FN called with
1319202375Srdivacky   the argument list ARGLIST and add it to CANDIDATES.  FLAGS is passed on
1320202375Srdivacky   to implicit_conversion.
1321202375Srdivacky
1322202375Srdivacky   CTYPE, if non-NULL, is the type we want to pretend this function
1323202375Srdivacky   comes from for purposes of overload resolution.  */
1324202375Srdivacky
1325202375Srdivackystatic struct z_candidate *
1326202375Srdivackyadd_function_candidate (candidates, fn, ctype, arglist, flags)
1327202375Srdivacky     struct z_candidate *candidates;
1328202375Srdivacky     tree fn, ctype, arglist;
1329202375Srdivacky     int flags;
1330202375Srdivacky{
1331249423Sdim  tree parmlist = TYPE_ARG_TYPES (TREE_TYPE (fn));
1332202375Srdivacky  int i, len;
1333202375Srdivacky  tree convs;
1334202375Srdivacky  tree parmnode, argnode;
1335202375Srdivacky  int viable = 1;
1336202375Srdivacky
1337249423Sdim  /* The `this', `in_chrg' and VTT arguments to constructors are not
1338202375Srdivacky     considered in overload resolution.  */
1339202375Srdivacky  if (DECL_CONSTRUCTOR_P (fn))
1340202375Srdivacky    {
1341202375Srdivacky      parmlist = skip_artificial_parms_for (fn, parmlist);
1342202375Srdivacky      arglist = skip_artificial_parms_for (fn, arglist);
1343249423Sdim    }
1344202375Srdivacky
1345202375Srdivacky  len = list_length (arglist);
1346202375Srdivacky  convs = make_tree_vec (len);
1347202375Srdivacky
1348202375Srdivacky  /* 13.3.2 - Viable functions [over.match.viable]
1349202375Srdivacky     First, to be a viable function, a candidate function shall have enough
1350202375Srdivacky     parameters to agree in number with the arguments in the list.
1351202375Srdivacky
1352202375Srdivacky     We need to check this first; otherwise, checking the ICSes might cause
1353202375Srdivacky     us to produce an ill-formed template instantiation.  */
1354202375Srdivacky
1355202375Srdivacky  parmnode = parmlist;
1356203954Srdivacky  for (i = 0; i < len; ++i)
1357203954Srdivacky    {
1358203954Srdivacky      if (parmnode == NULL_TREE || parmnode == void_list_node)
1359249423Sdim	break;
1360263508Sdim      parmnode = TREE_CHAIN (parmnode);
1361263508Sdim    }
1362263508Sdim
1363263508Sdim  if (i < len && parmnode)
1364263508Sdim    viable = 0;
1365263508Sdim
1366263508Sdim  /* Make sure there are default args for the rest of the parms.  */
1367263508Sdim  else if (!sufficient_parms_p (parmnode))
1368263508Sdim    viable = 0;
1369263508Sdim
1370263508Sdim  if (! viable)
1371202375Srdivacky    goto out;
1372249423Sdim
1373202375Srdivacky  /* Second, for F to be a viable function, there shall exist for each
1374202375Srdivacky     argument an implicit conversion sequence that converts that argument
1375202375Srdivacky     to the corresponding parameter of F.  */
1376202375Srdivacky
1377202375Srdivacky  parmnode = parmlist;
1378202375Srdivacky  argnode = arglist;
1379202375Srdivacky
1380202375Srdivacky  for (i = 0; i < len; ++i)
1381202375Srdivacky    {
1382249423Sdim      tree arg = TREE_VALUE (argnode);
1383202375Srdivacky      tree argtype = lvalue_type (arg);
1384202375Srdivacky      tree t;
1385202375Srdivacky      int is_this;
1386202375Srdivacky
1387202375Srdivacky      if (parmnode == void_list_node)
1388249423Sdim	break;
1389202375Srdivacky
1390202375Srdivacky      is_this = (i == 0 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
1391202375Srdivacky		 && ! DECL_CONSTRUCTOR_P (fn));
1392202375Srdivacky
1393202375Srdivacky      if (parmnode)
1394249423Sdim	{
1395263508Sdim	  tree parmtype = TREE_VALUE (parmnode);
1396263508Sdim
1397263508Sdim	  /* The type of the implicit object parameter ('this') for
1398202375Srdivacky	     overload resolution is not always the same as for the
1399202375Srdivacky	     function itself; conversion functions are considered to
1400202375Srdivacky	     be members of the class being converted, and functions
1401202375Srdivacky	     introduced by a using-declaration are considered to be
1402263508Sdim	     members of the class that uses them.
1403263508Sdim
1404263508Sdim	     Since build_over_call ignores the ICS for the `this'
1405263508Sdim	     parameter, we can just change the parm type.  */
1406263508Sdim	  if (ctype && is_this)
1407263508Sdim	    {
1408249423Sdim	      parmtype
1409202375Srdivacky		= build_qualified_type (ctype,
1410263508Sdim					TYPE_QUALS (TREE_TYPE (parmtype)));
1411202375Srdivacky	      parmtype = build_pointer_type (parmtype);
1412263508Sdim	    }
1413263508Sdim
1414263508Sdim	  t = implicit_conversion (parmtype, argtype, arg, flags);
1415202375Srdivacky	}
1416202375Srdivacky      else
1417202375Srdivacky	{
1418202375Srdivacky	  t = build1 (IDENTITY_CONV, argtype, arg);
1419263508Sdim	  ICS_ELLIPSIS_FLAG (t) = 1;
1420263508Sdim	}
1421202375Srdivacky
1422249423Sdim      if (t && is_this)
1423202375Srdivacky	ICS_THIS_FLAG (t) = 1;
1424202375Srdivacky
1425202375Srdivacky      TREE_VEC_ELT (convs, i) = t;
1426202375Srdivacky      if (! t)
1427249423Sdim	{
1428202375Srdivacky	  viable = 0;
1429202375Srdivacky	  break;
1430249423Sdim	}
1431202375Srdivacky
1432202375Srdivacky      if (ICS_BAD_FLAG (t))
1433202375Srdivacky	viable = -1;
1434202375Srdivacky
1435203954Srdivacky      if (parmnode)
1436203954Srdivacky	parmnode = TREE_CHAIN (parmnode);
1437203954Srdivacky      argnode = TREE_CHAIN (argnode);
1438249423Sdim    }
1439263508Sdim
1440263508Sdim out:
1441249423Sdim  return add_candidate (candidates, fn, convs, viable);
1442263508Sdim}
1443263508Sdim
1444263508Sdim/* Create an overload candidate for the conversion function FN which will
1445263508Sdim   be invoked for expression OBJ, producing a pointer-to-function which
1446263508Sdim   will in turn be called with the argument list ARGLIST, and add it to
1447263508Sdim   CANDIDATES.  FLAGS is passed on to implicit_conversion.
1448263508Sdim
1449263508Sdim   Actually, we don't really care about FN; we care about the type it
1450263508Sdim   converts to.  There may be multiple conversion functions that will
1451263508Sdim   convert to that type, and we rely on build_user_type_conversion_1 to
1452263508Sdim   choose the best one; so when we create our candidate, we record the type
1453263508Sdim   instead of the function.  */
1454202375Srdivacky
1455202375Srdivackystatic struct z_candidate *
1456208599Srdivackyadd_conv_candidate (candidates, fn, obj, arglist)
1457208599Srdivacky     struct z_candidate *candidates;
1458208599Srdivacky     tree fn, obj, arglist;
1459208599Srdivacky{
1460208599Srdivacky  tree totype = TREE_TYPE (TREE_TYPE (fn));
1461226633Sdim  int i, len, viable, flags;
1462208599Srdivacky  tree parmlist, convs, parmnode, argnode;
1463208599Srdivacky
1464208599Srdivacky  for (parmlist = totype; TREE_CODE (parmlist) != FUNCTION_TYPE; )
1465208599Srdivacky    parmlist = TREE_TYPE (parmlist);
1466226633Sdim  parmlist = TYPE_ARG_TYPES (parmlist);
1467249423Sdim
1468208599Srdivacky  len = list_length (arglist) + 1;
1469208599Srdivacky  convs = make_tree_vec (len);
1470208599Srdivacky  parmnode = parmlist;
1471208599Srdivacky  argnode = arglist;
1472249423Sdim  viable = 1;
1473208599Srdivacky  flags = LOOKUP_NORMAL;
1474208599Srdivacky
1475208599Srdivacky  /* Don't bother looking up the same type twice.  */
1476249423Sdim  if (candidates && candidates->fn == totype)
1477208599Srdivacky    return candidates;
1478208599Srdivacky
1479208599Srdivacky  for (i = 0; i < len; ++i)
1480249423Sdim    {
1481208599Srdivacky      tree arg = i == 0 ? obj : TREE_VALUE (argnode);
1482208599Srdivacky      tree argtype = lvalue_type (arg);
1483208599Srdivacky      tree t;
1484234353Sdim
1485208599Srdivacky      if (i == 0)
1486249423Sdim	t = implicit_conversion (totype, argtype, arg, flags);
1487208599Srdivacky      else if (parmnode == void_list_node)
1488208599Srdivacky	break;
1489208599Srdivacky      else if (parmnode)
1490208599Srdivacky	t = implicit_conversion (TREE_VALUE (parmnode), argtype, arg, flags);
1491208599Srdivacky      else
1492234353Sdim	{
1493249423Sdim	  t = build1 (IDENTITY_CONV, argtype, arg);
1494208599Srdivacky	  ICS_ELLIPSIS_FLAG (t) = 1;
1495208599Srdivacky	}
1496208599Srdivacky
1497208599Srdivacky      TREE_VEC_ELT (convs, i) = t;
1498208599Srdivacky      if (! t)
1499208599Srdivacky	break;
1500208599Srdivacky
1501234353Sdim      if (ICS_BAD_FLAG (t))
1502208599Srdivacky	viable = -1;
1503208599Srdivacky
1504234353Sdim      if (i == 0)
1505234353Sdim	continue;
1506208599Srdivacky
1507249423Sdim      if (parmnode)
1508234353Sdim	parmnode = TREE_CHAIN (parmnode);
1509234353Sdim      argnode = TREE_CHAIN (argnode);
1510234353Sdim    }
1511208599Srdivacky
1512208599Srdivacky  if (i < len)
1513226633Sdim    viable = 0;
1514212904Sdim
1515212904Sdim  if (!sufficient_parms_p (parmnode))
1516208599Srdivacky    viable = 0;
1517226633Sdim
1518212904Sdim  return add_candidate (candidates, totype, convs, viable);
1519212904Sdim}
1520212904Sdim
1521212904Sdimstatic struct z_candidate *
1522212904Sdimbuild_builtin_candidate (candidates, fnname, type1, type2,
1523212904Sdim			 args, argtypes, flags)
1524212904Sdim     struct z_candidate *candidates;
1525212904Sdim     tree fnname, type1, type2, *args, *argtypes;
1526263508Sdim     int flags;
1527263508Sdim
1528212904Sdim{
1529212904Sdim  tree t, convs;
1530212904Sdim  int viable = 1, i;
1531263508Sdim  tree types[2];
1532212904Sdim
1533263508Sdim  types[0] = type1;
1534263508Sdim  types[1] = type2;
1535263508Sdim
1536263508Sdim  convs = make_tree_vec (args[2] ? 3 : (args[1] ? 2 : 1));
1537212904Sdim
1538212904Sdim  for (i = 0; i < 2; ++i)
1539249423Sdim    {
1540212904Sdim      if (! args[i])
1541212904Sdim	break;
1542212904Sdim
1543212904Sdim      t = implicit_conversion (types[i], argtypes[i], args[i], flags);
1544212904Sdim      if (! t)
1545212904Sdim	{
1546212904Sdim	  viable = 0;
1547249423Sdim	  /* We need something for printing the candidate.  */
1548263508Sdim	  t = build1 (IDENTITY_CONV, types[i], NULL_TREE);
1549263508Sdim	}
1550263508Sdim      else if (ICS_BAD_FLAG (t))
1551263508Sdim	viable = 0;
1552212904Sdim      TREE_VEC_ELT (convs, i) = t;
1553263508Sdim    }
1554212904Sdim
1555249423Sdim  /* For COND_EXPR we rearranged the arguments; undo that now.  */
1556212904Sdim  if (args[2])
1557212904Sdim    {
1558212904Sdim      TREE_VEC_ELT (convs, 2) = TREE_VEC_ELT (convs, 1);
1559249423Sdim      TREE_VEC_ELT (convs, 1) = TREE_VEC_ELT (convs, 0);
1560212904Sdim      t = implicit_conversion (boolean_type_node, argtypes[2], args[2], flags);
1561212904Sdim      if (t)
1562212904Sdim	TREE_VEC_ELT (convs, 0) = t;
1563212904Sdim      else
1564212904Sdim	viable = 0;
1565212904Sdim    }
1566212904Sdim
1567212904Sdim  return add_candidate (candidates, fnname, convs, viable);
1568212904Sdim}
1569263508Sdim
1570249423Sdimstatic int
1571212904Sdimis_complete (t)
1572212904Sdim     tree t;
1573212904Sdim{
1574212904Sdim  return COMPLETE_TYPE_P (complete_type (t));
1575212904Sdim}
1576212904Sdim
1577226633Sdim/* Returns non-zero if TYPE is a promoted arithmetic type.  */
1578249423Sdim
1579212904Sdimstatic int
1580263508Sdimpromoted_arithmetic_type_p (type)
1581212904Sdim     tree type;
1582263508Sdim{
1583212904Sdim  /* [over.built]
1584263508Sdim
1585212904Sdim     In this section, the term promoted integral type is used to refer
1586212904Sdim     to those integral types which are preserved by integral promotion
1587212904Sdim     (including e.g.  int and long but excluding e.g.  char).
1588212904Sdim     Similarly, the term promoted arithmetic type refers to promoted
1589249423Sdim     integral types plus floating types.  */
1590212904Sdim  return ((INTEGRAL_TYPE_P (type)
1591249423Sdim	   && same_type_p (type_promotes_to (type), type))
1592212904Sdim	  || TREE_CODE (type) == REAL_TYPE);
1593212904Sdim}
1594212904Sdim
1595212904Sdim/* Create any builtin operator overload candidates for the operator in
1596212904Sdim   question given the converted operand types TYPE1 and TYPE2.  The other
1597263508Sdim   args are passed through from add_builtin_candidates to
1598263508Sdim   build_builtin_candidate.
1599212904Sdim
1600212904Sdim   TYPE1 and TYPE2 may not be permissible, and we must filter them.
1601212904Sdim   If CODE is requires candidates operands of the same type of the kind
1602212904Sdim   of which TYPE1 and TYPE2 are, we add both candidates
1603212904Sdim   CODE (TYPE1, TYPE1) and CODE (TYPE2, TYPE2).  */
1604263508Sdim
1605263508Sdimstatic struct z_candidate *
1606212904Sdimadd_builtin_candidate (candidates, code, code2, fnname, type1, type2,
1607263508Sdim		       args, argtypes, flags)
1608263508Sdim     struct z_candidate *candidates;
1609263508Sdim     enum tree_code code, code2;
1610263508Sdim     tree fnname, type1, type2, *args, *argtypes;
1611212904Sdim     int flags;
1612212904Sdim{
1613212904Sdim  switch (code)
1614212904Sdim    {
1615263508Sdim    case POSTINCREMENT_EXPR:
1616263508Sdim    case POSTDECREMENT_EXPR:
1617263508Sdim      args[1] = integer_zero_node;
1618263508Sdim      type2 = integer_type_node;
1619212904Sdim      break;
1620249423Sdim    default:
1621212904Sdim      break;
1622212904Sdim    }
1623212904Sdim
1624212904Sdim  switch (code)
1625212904Sdim    {
1626212904Sdim
1627212904Sdim/* 4 For every pair T, VQ), where T is an arithmetic or  enumeration  type,
1628212904Sdim     and  VQ  is  either  volatile or empty, there exist candidate operator
1629212904Sdim     functions of the form
1630212904Sdim	     VQ T&   operator++(VQ T&);
1631212904Sdim	     T       operator++(VQ T&, int);
1632212904Sdim   5 For every pair T, VQ), where T is an enumeration type or an arithmetic
1633212904Sdim     type  other than bool, and VQ is either volatile or empty, there exist
1634212904Sdim     candidate operator functions of the form
1635212904Sdim	     VQ T&   operator--(VQ T&);
1636212904Sdim	     T       operator--(VQ T&, int);
1637212904Sdim   6 For every pair T, VQ), where T is  a  cv-qualified  or  cv-unqualified
1638212904Sdim     complete  object type, and VQ is either volatile or empty, there exist
1639212904Sdim     candidate operator functions of the form
1640212904Sdim	     T*VQ&   operator++(T*VQ&);
1641263508Sdim	     T*VQ&   operator--(T*VQ&);
1642263508Sdim	     T*      operator++(T*VQ&, int);
1643263508Sdim	     T*      operator--(T*VQ&, int);  */
1644226633Sdim
1645212904Sdim    case POSTDECREMENT_EXPR:
1646212904Sdim    case PREDECREMENT_EXPR:
1647212904Sdim      if (TREE_CODE (type1) == BOOLEAN_TYPE)
1648212904Sdim	return candidates;
1649263508Sdim    case POSTINCREMENT_EXPR:
1650212904Sdim    case PREINCREMENT_EXPR:
1651212904Sdim      if (ARITHMETIC_TYPE_P (type1) || TYPE_PTROB_P (type1))
1652212904Sdim	{
1653212904Sdim	  type1 = build_reference_type (type1);
1654212904Sdim	  break;
1655212904Sdim	}
1656212904Sdim      return candidates;
1657212904Sdim
1658249423Sdim/* 7 For every cv-qualified or cv-unqualified complete object type T, there
1659212904Sdim     exist candidate operator functions of the form
1660212904Sdim
1661212904Sdim	     T&      operator*(T*);
1662249423Sdim
1663212904Sdim   8 For every function type T, there exist candidate operator functions of
1664212904Sdim     the form
1665212904Sdim	     T&      operator*(T*);  */
1666212904Sdim
1667212904Sdim    case INDIRECT_REF:
1668212904Sdim      if (TREE_CODE (type1) == POINTER_TYPE
1669212904Sdim	  && (TYPE_PTROB_P (type1)
1670249423Sdim	      || TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE))
1671249423Sdim	break;
1672249423Sdim      return candidates;
1673212904Sdim
1674226633Sdim/* 9 For every type T, there exist candidate operator functions of the form
1675212904Sdim	     T*      operator+(T*);
1676212904Sdim
1677212904Sdim   10For  every  promoted arithmetic type T, there exist candidate operator
1678212904Sdim     functions of the form
1679212904Sdim	     T       operator+(T);
1680212904Sdim	     T       operator-(T);  */
1681212904Sdim
1682226633Sdim    case CONVERT_EXPR: /* unary + */
1683212904Sdim      if (TREE_CODE (type1) == POINTER_TYPE
1684212904Sdim	  && TREE_CODE (TREE_TYPE (type1)) != OFFSET_TYPE)
1685212904Sdim	break;
1686212904Sdim    case NEGATE_EXPR:
1687212904Sdim      if (ARITHMETIC_TYPE_P (type1))
1688212904Sdim	break;
1689212904Sdim      return candidates;
1690212904Sdim
1691212904Sdim/* 11For every promoted integral type T,  there  exist  candidate  operator
1692212904Sdim     functions of the form
1693249423Sdim	     T       operator~(T);  */
1694249423Sdim
1695249423Sdim    case BIT_NOT_EXPR:
1696249423Sdim      if (INTEGRAL_TYPE_P (type1))
1697249423Sdim	break;
1698212904Sdim      return candidates;
1699212904Sdim
1700249423Sdim/* 12For every quintuple C1, C2, T, CV1, CV2), where C2 is a class type, C1
1701212904Sdim     is the same type as C2 or is a derived class of C2, T  is  a  complete
1702212904Sdim     object type or a function type, and CV1 and CV2 are cv-qualifier-seqs,
1703212904Sdim     there exist candidate operator functions of the form
1704212904Sdim	     CV12 T& operator->*(CV1 C1*, CV2 T C2::*);
1705212904Sdim     where CV12 is the union of CV1 and CV2.  */
1706226633Sdim
1707212904Sdim    case MEMBER_REF:
1708212904Sdim      if (TREE_CODE (type1) == POINTER_TYPE
1709212904Sdim	  && (TYPE_PTRMEMFUNC_P (type2) || TYPE_PTRMEM_P (type2)))
1710212904Sdim	{
1711212904Sdim	  tree c1 = TREE_TYPE (type1);
1712212904Sdim	  tree c2 = (TYPE_PTRMEMFUNC_P (type2)
1713212904Sdim		     ? TYPE_METHOD_BASETYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (type2)))
1714212904Sdim		     : TYPE_OFFSET_BASETYPE (TREE_TYPE (type2)));
1715212904Sdim
1716212904Sdim	  if (IS_AGGR_TYPE (c1) && DERIVED_FROM_P (c2, c1)
1717249423Sdim	      && (TYPE_PTRMEMFUNC_P (type2)
1718212904Sdim		  || is_complete (TREE_TYPE (TREE_TYPE (type2)))))
1719249423Sdim	    break;
1720249423Sdim	}
1721212904Sdim      return candidates;
1722212904Sdim
1723212904Sdim/* 13For every pair of promoted arithmetic types L and R, there exist  can-
1724212904Sdim     didate operator functions of the form
1725212904Sdim	     LR      operator*(L, R);
1726212904Sdim	     LR      operator/(L, R);
1727202375Srdivacky	     LR      operator+(L, R);
1728202375Srdivacky	     LR      operator-(L, R);
1729202375Srdivacky	     bool    operator<(L, R);
1730202375Srdivacky	     bool    operator>(L, R);
1731226633Sdim	     bool    operator<=(L, R);
1732226633Sdim	     bool    operator>=(L, R);
1733202375Srdivacky	     bool    operator==(L, R);
1734202375Srdivacky	     bool    operator!=(L, R);
1735202375Srdivacky     where  LR  is  the  result of the usual arithmetic conversions between
1736202375Srdivacky     types L and R.
1737202375Srdivacky
1738202375Srdivacky   14For every pair of types T and I, where T  is  a  cv-qualified  or  cv-
1739226633Sdim     unqualified  complete  object  type and I is a promoted integral type,
1740226633Sdim     there exist candidate operator functions of the form
1741226633Sdim	     T*      operator+(T*, I);
1742226633Sdim	     T&      operator[](T*, I);
1743249423Sdim	     T*      operator-(T*, I);
1744202375Srdivacky	     T*      operator+(I, T*);
1745202375Srdivacky	     T&      operator[](I, T*);
1746202375Srdivacky
1747202375Srdivacky   15For every T, where T is a pointer to complete object type, there exist
1748249423Sdim     candidate operator functions of the form112)
1749202375Srdivacky	     ptrdiff_t operator-(T, T);
1750202375Srdivacky
1751202375Srdivacky   16For every pointer or enumeration type T, there exist candidate operator
1752202375Srdivacky     functions of the form
1753202375Srdivacky	     bool    operator<(T, T);
1754202375Srdivacky	     bool    operator>(T, T);
1755202375Srdivacky	     bool    operator<=(T, T);
1756249423Sdim	     bool    operator>=(T, T);
1757202375Srdivacky	     bool    operator==(T, T);
1758202375Srdivacky	     bool    operator!=(T, T);
1759202375Srdivacky
1760202375Srdivacky   17For every pointer to member type T,  there  exist  candidate  operator
1761202375Srdivacky     functions of the form
1762202375Srdivacky	     bool    operator==(T, T);
1763249423Sdim	     bool    operator!=(T, T);  */
1764204642Srdivacky
1765202375Srdivacky    case MINUS_EXPR:
1766202375Srdivacky      if (TYPE_PTROB_P (type1) && TYPE_PTROB_P (type2))
1767202375Srdivacky	break;
1768202375Srdivacky      if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2))
1769202375Srdivacky	{
1770202375Srdivacky	  type2 = ptrdiff_type_node;
1771202375Srdivacky	  break;
1772202375Srdivacky	}
1773226633Sdim    case MULT_EXPR:
1774202375Srdivacky    case TRUNC_DIV_EXPR:
1775202375Srdivacky      if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1776249423Sdim	break;
1777212904Sdim      return candidates;
1778212904Sdim
1779212904Sdim    case EQ_EXPR:
1780212904Sdim    case NE_EXPR:
1781202375Srdivacky      if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2))
1782226633Sdim	  || (TYPE_PTRMEM_P (type1) && TYPE_PTRMEM_P (type2)))
1783204642Srdivacky	break;
1784202375Srdivacky      if ((TYPE_PTRMEMFUNC_P (type1) || TYPE_PTRMEM_P (type1))
1785202375Srdivacky	  && null_ptr_cst_p (args[1]))
1786202375Srdivacky	{
1787202375Srdivacky	  type2 = type1;
1788202375Srdivacky	  break;
1789249423Sdim	}
1790212904Sdim      if ((TYPE_PTRMEMFUNC_P (type2) || TYPE_PTRMEM_P (type2))
1791212904Sdim	  && null_ptr_cst_p (args[0]))
1792212904Sdim	{
1793212904Sdim	  type1 = type2;
1794212904Sdim	  break;
1795212904Sdim	}
1796212904Sdim      /* FALLTHROUGH */
1797212904Sdim    case LT_EXPR:
1798212904Sdim    case GT_EXPR:
1799208599Srdivacky    case LE_EXPR:
1800212904Sdim    case GE_EXPR:
1801212904Sdim    case MAX_EXPR:
1802249423Sdim    case MIN_EXPR:
1803212904Sdim      if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1804212904Sdim        break;
1805212904Sdim      if (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
1806212904Sdim	break;
1807212904Sdim      if (TREE_CODE (type1) == ENUMERAL_TYPE && TREE_CODE (type2) == ENUMERAL_TYPE)
1808208599Srdivacky        break;
1809202375Srdivacky      if (TYPE_PTR_P (type1) && null_ptr_cst_p (args[1]))
1810202375Srdivacky	{
1811226633Sdim	  type2 = type1;
1812249423Sdim	  break;
1813249423Sdim	}
1814249423Sdim      if (null_ptr_cst_p (args[0]) && TYPE_PTR_P (type2))
1815249423Sdim	{
1816249423Sdim	  type1 = type2;
1817249423Sdim	  break;
1818249423Sdim	}
1819249423Sdim      return candidates;
1820249423Sdim
1821249423Sdim    case PLUS_EXPR:
1822249423Sdim      if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1823249423Sdim	break;
1824249423Sdim    case ARRAY_REF:
1825249423Sdim      if (INTEGRAL_TYPE_P (type1) && TYPE_PTROB_P (type2))
1826249423Sdim	{
1827249423Sdim	  type1 = ptrdiff_type_node;
1828202375Srdivacky	  break;
1829202375Srdivacky	}
1830202375Srdivacky      if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2))
1831202375Srdivacky	{
1832202375Srdivacky	  type2 = ptrdiff_type_node;
1833207618Srdivacky	  break;
1834249423Sdim	}
1835263508Sdim      return candidates;
1836202375Srdivacky
1837263508Sdim/* 18For  every pair of promoted integral types L and R, there exist candi-
1838202375Srdivacky     date operator functions of the form
1839202375Srdivacky	     LR      operator%(L, R);
1840202375Srdivacky	     LR      operator&(L, R);
1841202375Srdivacky	     LR      operator^(L, R);
1842249423Sdim	     LR      operator|(L, R);
1843202375Srdivacky	     L       operator<<(L, R);
1844249423Sdim	     L       operator>>(L, R);
1845202375Srdivacky     where LR is the result of the  usual  arithmetic  conversions  between
1846202375Srdivacky     types L and R.  */
1847202375Srdivacky
1848202375Srdivacky    case TRUNC_MOD_EXPR:
1849202375Srdivacky    case BIT_AND_EXPR:
1850202375Srdivacky    case BIT_IOR_EXPR:
1851202375Srdivacky    case BIT_XOR_EXPR:
1852202375Srdivacky    case LSHIFT_EXPR:
1853202375Srdivacky    case RSHIFT_EXPR:
1854249423Sdim      if (INTEGRAL_TYPE_P (type1) && INTEGRAL_TYPE_P (type2))
1855204642Srdivacky	break;
1856202375Srdivacky      return candidates;
1857202375Srdivacky
1858202375Srdivacky/* 19For  every  triple  L, VQ, R), where L is an arithmetic or enumeration
1859263508Sdim     type, VQ is either volatile or empty, and R is a  promoted  arithmetic
1860263508Sdim     type, there exist candidate operator functions of the form
1861263508Sdim	     VQ L&   operator=(VQ L&, R);
1862263508Sdim	     VQ L&   operator*=(VQ L&, R);
1863	     VQ L&   operator/=(VQ L&, R);
1864	     VQ L&   operator+=(VQ L&, R);
1865	     VQ L&   operator-=(VQ L&, R);
1866
1867   20For  every  pair T, VQ), where T is any type and VQ is either volatile
1868     or empty, there exist candidate operator functions of the form
1869	     T*VQ&   operator=(T*VQ&, T*);
1870
1871   21For every pair T, VQ), where T is a pointer to member type and  VQ  is
1872     either  volatile or empty, there exist candidate operator functions of
1873     the form
1874	     VQ T&   operator=(VQ T&, T);
1875
1876   22For every triple  T,  VQ,  I),  where  T  is  a  cv-qualified  or  cv-
1877     unqualified  complete object type, VQ is either volatile or empty, and
1878     I is a promoted integral type, there exist  candidate  operator  func-
1879     tions of the form
1880	     T*VQ&   operator+=(T*VQ&, I);
1881	     T*VQ&   operator-=(T*VQ&, I);
1882
1883   23For  every  triple  L,  VQ,  R), where L is an integral or enumeration
1884     type, VQ is either volatile or empty, and R  is  a  promoted  integral
1885     type, there exist candidate operator functions of the form
1886
1887	     VQ L&   operator%=(VQ L&, R);
1888	     VQ L&   operator<<=(VQ L&, R);
1889	     VQ L&   operator>>=(VQ L&, R);
1890	     VQ L&   operator&=(VQ L&, R);
1891	     VQ L&   operator^=(VQ L&, R);
1892	     VQ L&   operator|=(VQ L&, R);  */
1893
1894    case MODIFY_EXPR:
1895      switch (code2)
1896	{
1897	case PLUS_EXPR:
1898	case MINUS_EXPR:
1899	  if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2))
1900	    {
1901	      type2 = ptrdiff_type_node;
1902	      break;
1903	    }
1904	case MULT_EXPR:
1905	case TRUNC_DIV_EXPR:
1906	  if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1907	    break;
1908	  return candidates;
1909
1910	case TRUNC_MOD_EXPR:
1911	case BIT_AND_EXPR:
1912	case BIT_IOR_EXPR:
1913	case BIT_XOR_EXPR:
1914	case LSHIFT_EXPR:
1915	case RSHIFT_EXPR:
1916	  if (INTEGRAL_TYPE_P (type1) && INTEGRAL_TYPE_P (type2))
1917	    break;
1918	  return candidates;
1919
1920	case NOP_EXPR:
1921	  if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1922	    break;
1923	  if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2))
1924	      || (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
1925	      || (TYPE_PTRMEM_P (type1) && TYPE_PTRMEM_P (type2))
1926	      || ((TYPE_PTRMEMFUNC_P (type1)
1927		   || TREE_CODE (type1) == POINTER_TYPE)
1928		  && null_ptr_cst_p (args[1])))
1929	    {
1930	      type2 = type1;
1931	      break;
1932	    }
1933	  return candidates;
1934
1935	default:
1936	  abort ();
1937	}
1938      type1 = build_reference_type (type1);
1939      break;
1940
1941    case COND_EXPR:
1942      /* [over.built]
1943
1944	 For every pair of promoted arithmetic types L and R, there
1945	 exist candidate operator functions of the form
1946
1947	 LR operator?(bool, L, R);
1948
1949	 where LR is the result of the usual arithmetic conversions
1950	 between types L and R.
1951
1952	 For every type T, where T is a pointer or pointer-to-member
1953	 type, there exist candidate operator functions of the form T
1954	 operator?(bool, T, T);  */
1955
1956      if (promoted_arithmetic_type_p (type1)
1957	  && promoted_arithmetic_type_p (type2))
1958	/* That's OK.  */
1959	break;
1960
1961      /* Otherwise, the types should be pointers.  */
1962      if (!(TREE_CODE (type1) == POINTER_TYPE
1963	    || TYPE_PTRMEM_P (type1)
1964	    || TYPE_PTRMEMFUNC_P (type1))
1965	  || !(TREE_CODE (type2) == POINTER_TYPE
1966	       || TYPE_PTRMEM_P (type2)
1967	       || TYPE_PTRMEMFUNC_P (type2)))
1968	return candidates;
1969
1970      /* We don't check that the two types are the same; the logic
1971	 below will actually create two candidates; one in which both
1972	 parameter types are TYPE1, and one in which both parameter
1973	 types are TYPE2.  */
1974	break;
1975
1976      /* These arguments do not make for a legal overloaded operator.  */
1977      return candidates;
1978
1979    default:
1980      abort ();
1981    }
1982
1983  /* If we're dealing with two pointer types or two enumeral types,
1984     we need candidates for both of them.  */
1985  if (type2 && !same_type_p (type1, type2)
1986      && TREE_CODE (type1) == TREE_CODE (type2)
1987      && (TREE_CODE (type1) == REFERENCE_TYPE
1988	  || (TREE_CODE (type1) == POINTER_TYPE
1989	      && TYPE_PTRMEM_P (type1) == TYPE_PTRMEM_P (type2))
1990	  || TYPE_PTRMEMFUNC_P (type1)
1991	  || IS_AGGR_TYPE (type1)
1992	  || TREE_CODE (type1) == ENUMERAL_TYPE))
1993    {
1994      candidates = build_builtin_candidate
1995	(candidates, fnname, type1, type1, args, argtypes, flags);
1996      return build_builtin_candidate
1997	(candidates, fnname, type2, type2, args, argtypes, flags);
1998    }
1999
2000  return build_builtin_candidate
2001    (candidates, fnname, type1, type2, args, argtypes, flags);
2002}
2003
2004tree
2005type_decays_to (type)
2006     tree type;
2007{
2008  if (TREE_CODE (type) == ARRAY_TYPE)
2009    return build_pointer_type (TREE_TYPE (type));
2010  if (TREE_CODE (type) == FUNCTION_TYPE)
2011    return build_pointer_type (type);
2012  return type;
2013}
2014
2015/* There are three conditions of builtin candidates:
2016
2017   1) bool-taking candidates.  These are the same regardless of the input.
2018   2) pointer-pair taking candidates.  These are generated for each type
2019      one of the input types converts to.
2020   3) arithmetic candidates.  According to the standard, we should generate
2021      all of these, but I'm trying not to...
2022
2023   Here we generate a superset of the possible candidates for this particular
2024   case.  That is a subset of the full set the standard defines, plus some
2025   other cases which the standard disallows. add_builtin_candidate will
2026   filter out the illegal set.  */
2027
2028static struct z_candidate *
2029add_builtin_candidates (candidates, code, code2, fnname, args, flags)
2030     struct z_candidate *candidates;
2031     enum tree_code code, code2;
2032     tree fnname, *args;
2033     int flags;
2034{
2035  int ref1, i;
2036  int enum_p = 0;
2037  tree type, argtypes[3];
2038  /* TYPES[i] is the set of possible builtin-operator parameter types
2039     we will consider for the Ith argument.  These are represented as
2040     a TREE_LIST; the TREE_VALUE of each node is the potential
2041     parameter type.  */
2042  tree types[2];
2043
2044  for (i = 0; i < 3; ++i)
2045    {
2046      if (args[i])
2047	argtypes[i]  = lvalue_type (args[i]);
2048      else
2049	argtypes[i] = NULL_TREE;
2050    }
2051
2052  switch (code)
2053    {
2054/* 4 For every pair T, VQ), where T is an arithmetic or  enumeration  type,
2055     and  VQ  is  either  volatile or empty, there exist candidate operator
2056     functions of the form
2057		 VQ T&   operator++(VQ T&);  */
2058
2059    case POSTINCREMENT_EXPR:
2060    case PREINCREMENT_EXPR:
2061    case POSTDECREMENT_EXPR:
2062    case PREDECREMENT_EXPR:
2063    case MODIFY_EXPR:
2064      ref1 = 1;
2065      break;
2066
2067/* 24There also exist candidate operator functions of the form
2068	     bool    operator!(bool);
2069	     bool    operator&&(bool, bool);
2070	     bool    operator||(bool, bool);  */
2071
2072    case TRUTH_NOT_EXPR:
2073      return build_builtin_candidate
2074	(candidates, fnname, boolean_type_node,
2075	 NULL_TREE, args, argtypes, flags);
2076
2077    case TRUTH_ORIF_EXPR:
2078    case TRUTH_ANDIF_EXPR:
2079      return build_builtin_candidate
2080	(candidates, fnname, boolean_type_node,
2081	 boolean_type_node, args, argtypes, flags);
2082
2083    case ADDR_EXPR:
2084    case COMPOUND_EXPR:
2085    case COMPONENT_REF:
2086      return candidates;
2087
2088    case COND_EXPR:
2089    case EQ_EXPR:
2090    case NE_EXPR:
2091    case LT_EXPR:
2092    case LE_EXPR:
2093    case GT_EXPR:
2094    case GE_EXPR:
2095      enum_p = 1;
2096      /* FALLTHROUGH */
2097
2098    default:
2099      ref1 = 0;
2100    }
2101
2102  types[0] = types[1] = NULL_TREE;
2103
2104  for (i = 0; i < 2; ++i)
2105    {
2106      if (! args[i])
2107	;
2108      else if (IS_AGGR_TYPE (argtypes[i]))
2109	{
2110	  tree convs;
2111
2112	  if (i == 0 && code == MODIFY_EXPR && code2 == NOP_EXPR)
2113	    return candidates;
2114
2115	  convs = lookup_conversions (argtypes[i]);
2116
2117	  if (code == COND_EXPR)
2118	    {
2119	      if (real_lvalue_p (args[i]))
2120		types[i] = tree_cons
2121		  (NULL_TREE, build_reference_type (argtypes[i]), types[i]);
2122
2123	      types[i] = tree_cons
2124		(NULL_TREE, TYPE_MAIN_VARIANT (argtypes[i]), types[i]);
2125	    }
2126
2127	  else if (! convs)
2128	    return candidates;
2129
2130	  for (; convs; convs = TREE_CHAIN (convs))
2131	    {
2132	      type = TREE_TYPE (TREE_TYPE (OVL_CURRENT (TREE_VALUE (convs))));
2133
2134	      if (i == 0 && ref1
2135		  && (TREE_CODE (type) != REFERENCE_TYPE
2136		      || CP_TYPE_CONST_P (TREE_TYPE (type))))
2137		continue;
2138
2139	      if (code == COND_EXPR && TREE_CODE (type) == REFERENCE_TYPE)
2140		types[i] = tree_cons (NULL_TREE, type, types[i]);
2141
2142	      type = non_reference (type);
2143	      if (i != 0 || ! ref1)
2144		{
2145		  type = TYPE_MAIN_VARIANT (type_decays_to (type));
2146	          if (enum_p && TREE_CODE (type) == ENUMERAL_TYPE)
2147	            types[i] = tree_cons (NULL_TREE, type, types[i]);
2148		  if (INTEGRAL_TYPE_P (type))
2149		    type = type_promotes_to (type);
2150		}
2151
2152	      if (! value_member (type, types[i]))
2153		types[i] = tree_cons (NULL_TREE, type, types[i]);
2154	    }
2155	}
2156      else
2157	{
2158	  if (code == COND_EXPR && real_lvalue_p (args[i]))
2159	    types[i] = tree_cons
2160	      (NULL_TREE, build_reference_type (argtypes[i]), types[i]);
2161	  type = non_reference (argtypes[i]);
2162	  if (i != 0 || ! ref1)
2163	    {
2164	      type = TYPE_MAIN_VARIANT (type_decays_to (type));
2165	      if (enum_p && TREE_CODE (type) == ENUMERAL_TYPE)
2166	        types[i] = tree_cons (NULL_TREE, type, types[i]);
2167	      if (INTEGRAL_TYPE_P (type))
2168		type = type_promotes_to (type);
2169	    }
2170	  types[i] = tree_cons (NULL_TREE, type, types[i]);
2171	}
2172    }
2173
2174  /* Run through the possible parameter types of both arguments,
2175     creating candidates with those parameter types.  */
2176  for (; types[0]; types[0] = TREE_CHAIN (types[0]))
2177    {
2178      if (types[1])
2179	for (type = types[1]; type; type = TREE_CHAIN (type))
2180	  candidates = add_builtin_candidate
2181	    (candidates, code, code2, fnname, TREE_VALUE (types[0]),
2182	     TREE_VALUE (type), args, argtypes, flags);
2183      else
2184	candidates = add_builtin_candidate
2185	  (candidates, code, code2, fnname, TREE_VALUE (types[0]),
2186	   NULL_TREE, args, argtypes, flags);
2187    }
2188
2189  return candidates;
2190}
2191
2192
2193/* If TMPL can be successfully instantiated as indicated by
2194   EXPLICIT_TARGS and ARGLIST, adds the instantiation to CANDIDATES.
2195
2196   TMPL is the template.  EXPLICIT_TARGS are any explicit template
2197   arguments.  ARGLIST is the arguments provided at the call-site.
2198   The RETURN_TYPE is the desired type for conversion operators.  If
2199   OBJ is NULL_TREE, FLAGS and CTYPE are as for add_function_candidate.
2200   If an OBJ is supplied, FLAGS and CTYPE are ignored, and OBJ is as for
2201   add_conv_candidate.  */
2202
2203static struct z_candidate*
2204add_template_candidate_real (candidates, tmpl, ctype, explicit_targs,
2205			     arglist, return_type, flags,
2206			     obj, strict)
2207     struct z_candidate *candidates;
2208     tree tmpl, ctype, explicit_targs, arglist, return_type;
2209     int flags;
2210     tree obj;
2211     unification_kind_t strict;
2212{
2213  int ntparms = DECL_NTPARMS (tmpl);
2214  tree targs = make_tree_vec (ntparms);
2215  tree args_without_in_chrg = arglist;
2216  struct z_candidate *cand;
2217  int i;
2218  tree fn;
2219
2220  /* We don't do deduction on the in-charge parameter, the VTT
2221     parameter or 'this'.  */
2222  if (DECL_NONSTATIC_MEMBER_FUNCTION_P (tmpl))
2223    args_without_in_chrg = TREE_CHAIN (args_without_in_chrg);
2224
2225  if ((DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (tmpl)
2226       || DECL_BASE_CONSTRUCTOR_P (tmpl))
2227      && TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (tmpl)))
2228    args_without_in_chrg = TREE_CHAIN (args_without_in_chrg);
2229
2230  i = fn_type_unification (tmpl, explicit_targs, targs,
2231			   args_without_in_chrg,
2232			   return_type, strict, -1);
2233
2234  if (i != 0)
2235    return candidates;
2236
2237  fn = instantiate_template (tmpl, targs);
2238  if (fn == error_mark_node)
2239    return candidates;
2240
2241  /* In [class.copy]:
2242
2243       A member function template is never instantiated to perform the
2244       copy of a class object to an object of its class type.
2245
2246     It's a little unclear what this means; the standard explicitly
2247     does allow a template to be used to copy a class.  For example,
2248     in:
2249
2250       struct A {
2251         A(A&);
2252	 template <class T> A(const T&);
2253       };
2254       const A f ();
2255       void g () { A a (f ()); }
2256
2257     the member template will be used to make the copy.  The section
2258     quoted above appears in the paragraph that forbids constructors
2259     whose only parameter is (a possibly cv-qualified variant of) the
2260     class type, and a logical interpretation is that the intent was
2261     to forbid the instantiation of member templates which would then
2262     have that form.  */
2263  if (DECL_CONSTRUCTOR_P (fn) && list_length (arglist) == 2)
2264    {
2265      tree arg_types = FUNCTION_FIRST_USER_PARMTYPE (fn);
2266      if (arg_types && same_type_p (TYPE_MAIN_VARIANT (TREE_VALUE (arg_types)),
2267				    ctype))
2268	return candidates;
2269    }
2270
2271  if (obj != NULL_TREE)
2272    /* Aha, this is a conversion function.  */
2273    cand = add_conv_candidate (candidates, fn, obj, arglist);
2274  else
2275    cand = add_function_candidate (candidates, fn, ctype,
2276				   arglist, flags);
2277  if (DECL_TI_TEMPLATE (fn) != tmpl)
2278    /* This situation can occur if a member template of a template
2279       class is specialized.  Then, instantiate_template might return
2280       an instantiation of the specialization, in which case the
2281       DECL_TI_TEMPLATE field will point at the original
2282       specialization.  For example:
2283
2284	 template <class T> struct S { template <class U> void f(U);
2285				       template <> void f(int) {}; };
2286	 S<double> sd;
2287	 sd.f(3);
2288
2289       Here, TMPL will be template <class U> S<double>::f(U).
2290       And, instantiate template will give us the specialization
2291       template <> S<double>::f(int).  But, the DECL_TI_TEMPLATE field
2292       for this will point at template <class T> template <> S<T>::f(int),
2293       so that we can find the definition.  For the purposes of
2294       overload resolution, however, we want the original TMPL.  */
2295    cand->template = tree_cons (tmpl, targs, NULL_TREE);
2296  else
2297    cand->template = DECL_TEMPLATE_INFO (fn);
2298
2299  return cand;
2300}
2301
2302
2303static struct z_candidate *
2304add_template_candidate (candidates, tmpl, ctype, explicit_targs,
2305			arglist, return_type, flags, strict)
2306     struct z_candidate *candidates;
2307     tree tmpl, ctype, explicit_targs, arglist, return_type;
2308     int flags;
2309     unification_kind_t strict;
2310{
2311  return
2312    add_template_candidate_real (candidates, tmpl, ctype,
2313				 explicit_targs, arglist, return_type, flags,
2314				 NULL_TREE, strict);
2315}
2316
2317
2318static struct z_candidate *
2319add_template_conv_candidate (candidates, tmpl, obj, arglist, return_type)
2320     struct z_candidate *candidates;
2321     tree tmpl, obj, arglist, return_type;
2322{
2323  return
2324    add_template_candidate_real (candidates, tmpl, NULL_TREE, NULL_TREE,
2325				 arglist, return_type, 0, obj, DEDUCE_CONV);
2326}
2327
2328
2329static int
2330any_viable (cands)
2331     struct z_candidate *cands;
2332{
2333  for (; cands; cands = cands->next)
2334    if (pedantic ? cands->viable == 1 : cands->viable)
2335      return 1;
2336  return 0;
2337}
2338
2339static struct z_candidate *
2340splice_viable (cands)
2341     struct z_candidate *cands;
2342{
2343  struct z_candidate **p = &cands;
2344
2345  for (; *p; )
2346    {
2347      if (pedantic ? (*p)->viable == 1 : (*p)->viable)
2348	p = &((*p)->next);
2349      else
2350	*p = (*p)->next;
2351    }
2352
2353  return cands;
2354}
2355
2356static tree
2357build_this (obj)
2358     tree obj;
2359{
2360  /* Fix this to work on non-lvalues.  */
2361  return build_unary_op (ADDR_EXPR, obj, 0);
2362}
2363
2364static void
2365print_z_candidates (candidates)
2366     struct z_candidate *candidates;
2367{
2368  const char *str = "candidates are:";
2369  for (; candidates; candidates = candidates->next)
2370    {
2371      if (TREE_CODE (candidates->fn) == IDENTIFIER_NODE)
2372	{
2373	  if (TREE_VEC_LENGTH (candidates->convs) == 3)
2374	    error ("%s %D(%T, %T, %T) <built-in>", str, candidates->fn,
2375		      TREE_TYPE (TREE_VEC_ELT (candidates->convs, 0)),
2376		      TREE_TYPE (TREE_VEC_ELT (candidates->convs, 1)),
2377		      TREE_TYPE (TREE_VEC_ELT (candidates->convs, 2)));
2378	  else if (TREE_VEC_LENGTH (candidates->convs) == 2)
2379	    error ("%s %D(%T, %T) <built-in>", str, candidates->fn,
2380		      TREE_TYPE (TREE_VEC_ELT (candidates->convs, 0)),
2381		      TREE_TYPE (TREE_VEC_ELT (candidates->convs, 1)));
2382	  else
2383	    error ("%s %D(%T) <built-in>", str, candidates->fn,
2384		      TREE_TYPE (TREE_VEC_ELT (candidates->convs, 0)));
2385	}
2386      else if (TYPE_P (candidates->fn))
2387	error ("%s %T <conversion>", str, candidates->fn);
2388      else
2389	cp_error_at ("%s %+#D%s", str, candidates->fn,
2390		     candidates->viable == -1 ? " <near match>" : "");
2391      str = "               ";
2392    }
2393}
2394
2395/* Returns the best overload candidate to perform the requested
2396   conversion.  This function is used for three the overloading situations
2397   described in [over.match.copy], [over.match.conv], and [over.match.ref].
2398   If TOTYPE is a REFERENCE_TYPE, we're trying to find an lvalue binding as
2399   per [dcl.init.ref], so we ignore temporary bindings.  */
2400
2401static struct z_candidate *
2402build_user_type_conversion_1 (totype, expr, flags)
2403     tree totype, expr;
2404     int flags;
2405{
2406  struct z_candidate *candidates, *cand;
2407  tree fromtype = TREE_TYPE (expr);
2408  tree ctors = NULL_TREE, convs = NULL_TREE, *p;
2409  tree args = NULL_TREE;
2410  tree templates = NULL_TREE;
2411
2412  /* We represent conversion within a hierarchy using RVALUE_CONV and
2413     BASE_CONV, as specified by [over.best.ics]; these become plain
2414     constructor calls, as specified in [dcl.init].  */
2415  my_friendly_assert (!IS_AGGR_TYPE (fromtype) || !IS_AGGR_TYPE (totype)
2416		      || !DERIVED_FROM_P (totype, fromtype), 20011226);
2417
2418  if (IS_AGGR_TYPE (totype))
2419    ctors = lookup_fnfields (TYPE_BINFO (totype),
2420			     complete_ctor_identifier,
2421			     0);
2422
2423  if (IS_AGGR_TYPE (fromtype))
2424    convs = lookup_conversions (fromtype);
2425
2426  candidates = 0;
2427  flags |= LOOKUP_NO_CONVERSION;
2428
2429  if (ctors)
2430    {
2431      tree t;
2432
2433      ctors = TREE_VALUE (ctors);
2434
2435      t = build_int_2 (0, 0);
2436      TREE_TYPE (t) = build_pointer_type (totype);
2437      args = build_tree_list (NULL_TREE, expr);
2438      /* We should never try to call the abstract or base constructor
2439	 from here.  */
2440      my_friendly_assert (!DECL_HAS_IN_CHARGE_PARM_P (OVL_CURRENT (ctors))
2441			  && !DECL_HAS_VTT_PARM_P (OVL_CURRENT (ctors)),
2442			  20011226);
2443      args = tree_cons (NULL_TREE, t, args);
2444    }
2445  for (; ctors; ctors = OVL_NEXT (ctors))
2446    {
2447      tree ctor = OVL_CURRENT (ctors);
2448      if (DECL_NONCONVERTING_P (ctor))
2449	continue;
2450
2451      if (TREE_CODE (ctor) == TEMPLATE_DECL)
2452	{
2453	  templates = tree_cons (NULL_TREE, ctor, templates);
2454	  candidates =
2455	    add_template_candidate (candidates, ctor, totype,
2456				    NULL_TREE, args, NULL_TREE, flags,
2457				    DEDUCE_CALL);
2458	}
2459      else
2460	candidates = add_function_candidate (candidates, ctor, totype,
2461					     args, flags);
2462
2463      if (candidates)
2464	{
2465	  candidates->second_conv = build1 (IDENTITY_CONV, totype, NULL_TREE);
2466	  candidates->basetype_path = TYPE_BINFO (totype);
2467	}
2468    }
2469
2470  if (convs)
2471    args = build_tree_list (NULL_TREE, build_this (expr));
2472
2473  for (; convs; convs = TREE_CHAIN (convs))
2474    {
2475      tree fns = TREE_VALUE (convs);
2476      int convflags = LOOKUP_NO_CONVERSION;
2477      tree ics;
2478
2479      /* If we are called to convert to a reference type, we are trying to
2480	 find an lvalue binding, so don't even consider temporaries.  If
2481	 we don't find an lvalue binding, the caller will try again to
2482	 look for a temporary binding.  */
2483      if (TREE_CODE (totype) == REFERENCE_TYPE)
2484	convflags |= LOOKUP_NO_TEMP_BIND;
2485
2486      if (TREE_CODE (OVL_CURRENT (fns)) != TEMPLATE_DECL)
2487	ics = implicit_conversion
2488	  (totype, TREE_TYPE (TREE_TYPE (OVL_CURRENT (fns))), 0, convflags);
2489      else
2490	/* We can't compute this yet.  */
2491	ics = error_mark_node;
2492
2493      if (TREE_CODE (totype) == REFERENCE_TYPE && ics && ICS_BAD_FLAG (ics))
2494	/* ignore the near match.  */;
2495      else if (ics)
2496	for (; fns; fns = OVL_NEXT (fns))
2497	  {
2498	    tree fn = OVL_CURRENT (fns);
2499	    struct z_candidate *old_candidates = candidates;
2500
2501	    /* [over.match.funcs] For conversion functions, the function is
2502	       considered to be a member of the class of the implicit object
2503	       argument for the purpose of defining the type of the implicit
2504	       object parameter.
2505
2506	       So we pass fromtype as CTYPE to add_*_candidate.  */
2507
2508	    if (TREE_CODE (fn) == TEMPLATE_DECL)
2509	      {
2510		templates = tree_cons (NULL_TREE, fn, templates);
2511		candidates =
2512		  add_template_candidate (candidates, fn, fromtype, NULL_TREE,
2513					  args, totype, flags,
2514					  DEDUCE_CONV);
2515	      }
2516	    else
2517	      candidates = add_function_candidate (candidates, fn, fromtype,
2518						   args, flags);
2519
2520	    if (candidates != old_candidates)
2521	      {
2522		if (TREE_CODE (fn) == TEMPLATE_DECL)
2523		  ics = implicit_conversion
2524		    (totype, TREE_TYPE (TREE_TYPE (candidates->fn)),
2525		     0, convflags);
2526
2527		candidates->second_conv = ics;
2528		candidates->basetype_path = TYPE_BINFO (fromtype);
2529
2530		if (ics == NULL_TREE)
2531		  candidates->viable = 0;
2532		else if (candidates->viable == 1 && ICS_BAD_FLAG (ics))
2533		  candidates->viable = -1;
2534	      }
2535	  }
2536    }
2537
2538  if (! any_viable (candidates))
2539    {
2540#if 0
2541      if (flags & LOOKUP_COMPLAIN)
2542	{
2543	  if (candidates && ! candidates->next)
2544	    /* say why this one won't work or try to be loose */;
2545	  else
2546	    error ("no viable candidates");
2547	}
2548#endif
2549
2550      return 0;
2551    }
2552
2553  candidates = splice_viable (candidates);
2554  cand = tourney (candidates);
2555
2556  if (cand == 0)
2557    {
2558      if (flags & LOOKUP_COMPLAIN)
2559	{
2560	  error ("conversion from `%T' to `%T' is ambiguous",
2561		    fromtype, totype);
2562	  print_z_candidates (candidates);
2563	}
2564
2565      cand = candidates;	/* any one will do */
2566      cand->second_conv = build1 (AMBIG_CONV, totype, expr);
2567      ICS_USER_FLAG (cand->second_conv) = 1;
2568      ICS_BAD_FLAG (cand->second_conv) = 1;
2569
2570      return cand;
2571    }
2572
2573  for (p = &(cand->second_conv); TREE_CODE (*p) != IDENTITY_CONV; )
2574    p = &(TREE_OPERAND (*p, 0));
2575
2576  *p = build
2577    (USER_CONV,
2578     (DECL_CONSTRUCTOR_P (cand->fn)
2579      ? totype : non_reference (TREE_TYPE (TREE_TYPE (cand->fn)))),
2580     expr, build_ptr_wrapper (cand));
2581
2582  ICS_USER_FLAG (cand->second_conv) = ICS_USER_FLAG (*p) = 1;
2583  if (cand->viable == -1)
2584    ICS_BAD_FLAG (cand->second_conv) = ICS_BAD_FLAG (*p) = 1;
2585
2586  return cand;
2587}
2588
2589tree
2590build_user_type_conversion (totype, expr, flags)
2591     tree totype, expr;
2592     int flags;
2593{
2594  struct z_candidate *cand
2595    = build_user_type_conversion_1 (totype, expr, flags);
2596
2597  if (cand)
2598    {
2599      if (TREE_CODE (cand->second_conv) == AMBIG_CONV)
2600	return error_mark_node;
2601      return convert_from_reference (convert_like (cand->second_conv, expr));
2602    }
2603  return NULL_TREE;
2604}
2605
2606/* Do any initial processing on the arguments to a function call.  */
2607
2608static tree
2609resolve_args (args)
2610     tree args;
2611{
2612  tree t;
2613  for (t = args; t; t = TREE_CHAIN (t))
2614    {
2615      tree arg = TREE_VALUE (t);
2616
2617      if (arg == error_mark_node)
2618	return error_mark_node;
2619      else if (VOID_TYPE_P (TREE_TYPE (arg)))
2620	{
2621	  error ("invalid use of void expression");
2622	  return error_mark_node;
2623	}
2624      else if (TREE_CODE (arg) == OFFSET_REF)
2625	arg = resolve_offset_ref (arg);
2626      arg = convert_from_reference (arg);
2627      TREE_VALUE (t) = arg;
2628    }
2629  return args;
2630}
2631
2632tree
2633build_new_function_call (fn, args)
2634     tree fn, args;
2635{
2636  struct z_candidate *candidates = 0, *cand;
2637  tree explicit_targs = NULL_TREE;
2638  int template_only = 0;
2639
2640  if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
2641    {
2642      explicit_targs = TREE_OPERAND (fn, 1);
2643      fn = TREE_OPERAND (fn, 0);
2644      template_only = 1;
2645    }
2646
2647  if (really_overloaded_fn (fn))
2648    {
2649      tree t1;
2650      tree templates = NULL_TREE;
2651
2652      args = resolve_args (args);
2653
2654      if (args == error_mark_node)
2655	return error_mark_node;
2656
2657      for (t1 = fn; t1; t1 = OVL_CHAIN (t1))
2658	{
2659	  tree t = OVL_FUNCTION (t1);
2660
2661	  if (TREE_CODE (t) == TEMPLATE_DECL)
2662	    {
2663	      templates = tree_cons (NULL_TREE, t, templates);
2664	      candidates = add_template_candidate
2665		(candidates, t, NULL_TREE, explicit_targs, args, NULL_TREE,
2666		 LOOKUP_NORMAL, DEDUCE_CALL);
2667	    }
2668	  else if (! template_only)
2669	    candidates = add_function_candidate
2670	      (candidates, t, NULL_TREE, args, LOOKUP_NORMAL);
2671	}
2672
2673      if (! any_viable (candidates))
2674	{
2675	  if (candidates && ! candidates->next)
2676	    return build_function_call (candidates->fn, args);
2677	  error ("no matching function for call to `%D(%A)'",
2678		    DECL_NAME (OVL_FUNCTION (fn)), args);
2679	  if (candidates)
2680	    print_z_candidates (candidates);
2681	  return error_mark_node;
2682	}
2683      candidates = splice_viable (candidates);
2684      cand = tourney (candidates);
2685
2686      if (cand == 0)
2687	{
2688	  error ("call of overloaded `%D(%A)' is ambiguous",
2689		    DECL_NAME (OVL_FUNCTION (fn)), args);
2690	  print_z_candidates (candidates);
2691	  return error_mark_node;
2692	}
2693
2694      return build_over_call (cand, args, LOOKUP_NORMAL);
2695    }
2696
2697  /* This is not really overloaded. */
2698  fn = OVL_CURRENT (fn);
2699
2700  return build_function_call (fn, args);
2701}
2702
2703static tree
2704build_object_call (obj, args)
2705     tree obj, args;
2706{
2707  struct z_candidate *candidates = 0, *cand;
2708  tree fns, convs, mem_args = NULL_TREE;
2709  tree type = TREE_TYPE (obj);
2710
2711  if (TYPE_PTRMEMFUNC_P (type))
2712    {
2713      /* It's no good looking for an overloaded operator() on a
2714	 pointer-to-member-function.  */
2715      error ("pointer-to-member function %E cannot be called without an object; consider using .* or ->*", obj);
2716      return error_mark_node;
2717    }
2718
2719  fns = lookup_fnfields (TYPE_BINFO (type), ansi_opname (CALL_EXPR), 1);
2720  if (fns == error_mark_node)
2721    return error_mark_node;
2722
2723  args = resolve_args (args);
2724
2725  if (args == error_mark_node)
2726    return error_mark_node;
2727
2728  if (fns)
2729    {
2730      tree base = BINFO_TYPE (TREE_PURPOSE (fns));
2731      mem_args = tree_cons (NULL_TREE, build_this (obj), args);
2732
2733      for (fns = TREE_VALUE (fns); fns; fns = OVL_NEXT (fns))
2734	{
2735	  tree fn = OVL_CURRENT (fns);
2736	  if (TREE_CODE (fn) == TEMPLATE_DECL)
2737	    {
2738	      candidates
2739		= add_template_candidate (candidates, fn, base, NULL_TREE,
2740					  mem_args, NULL_TREE,
2741					  LOOKUP_NORMAL, DEDUCE_CALL);
2742	    }
2743	  else
2744	    candidates = add_function_candidate
2745	      (candidates, fn, base, mem_args, LOOKUP_NORMAL);
2746
2747	  if (candidates)
2748	    candidates->basetype_path = TYPE_BINFO (type);
2749	}
2750    }
2751
2752  convs = lookup_conversions (type);
2753
2754  for (; convs; convs = TREE_CHAIN (convs))
2755    {
2756      tree fns = TREE_VALUE (convs);
2757      tree totype = TREE_TYPE (TREE_TYPE (OVL_CURRENT (fns)));
2758
2759      if ((TREE_CODE (totype) == POINTER_TYPE
2760	   && TREE_CODE (TREE_TYPE (totype)) == FUNCTION_TYPE)
2761	  || (TREE_CODE (totype) == REFERENCE_TYPE
2762	      && TREE_CODE (TREE_TYPE (totype)) == FUNCTION_TYPE)
2763	  || (TREE_CODE (totype) == REFERENCE_TYPE
2764	      && TREE_CODE (TREE_TYPE (totype)) == POINTER_TYPE
2765	      && TREE_CODE (TREE_TYPE (TREE_TYPE (totype))) == FUNCTION_TYPE))
2766	for (; fns; fns = OVL_NEXT (fns))
2767	  {
2768	    tree fn = OVL_CURRENT (fns);
2769	    if (TREE_CODE (fn) == TEMPLATE_DECL)
2770	      {
2771		candidates = add_template_conv_candidate (candidates,
2772							  fn,
2773							  obj,
2774							  args,
2775							  totype);
2776	      }
2777	    else
2778	      candidates = add_conv_candidate (candidates, fn, obj, args);
2779	  }
2780    }
2781
2782  if (! any_viable (candidates))
2783    {
2784      error ("no match for call to `(%T) (%A)'", TREE_TYPE (obj), args);
2785      print_z_candidates (candidates);
2786      return error_mark_node;
2787    }
2788
2789  candidates = splice_viable (candidates);
2790  cand = tourney (candidates);
2791
2792  if (cand == 0)
2793    {
2794      error ("call of `(%T) (%A)' is ambiguous", TREE_TYPE (obj), args);
2795      print_z_candidates (candidates);
2796      return error_mark_node;
2797    }
2798
2799  /* Since cand->fn will be a type, not a function, for a conversion
2800     function, we must be careful not to unconditionally look at
2801     DECL_NAME here.  */
2802  if (TREE_CODE (cand->fn) == FUNCTION_DECL
2803      && DECL_OVERLOADED_OPERATOR_P (cand->fn) == CALL_EXPR)
2804    return build_over_call (cand, mem_args, LOOKUP_NORMAL);
2805
2806  obj = convert_like_with_context
2807          (TREE_VEC_ELT (cand->convs, 0), obj, cand->fn, -1);
2808
2809  /* FIXME */
2810  return build_function_call (obj, args);
2811}
2812
2813static void
2814op_error (code, code2, arg1, arg2, arg3, problem)
2815     enum tree_code code, code2;
2816     tree arg1, arg2, arg3;
2817     const char *problem;
2818{
2819  const char *opname;
2820
2821  if (code == MODIFY_EXPR)
2822    opname = assignment_operator_name_info[code2].name;
2823  else
2824    opname = operator_name_info[code].name;
2825
2826  switch (code)
2827    {
2828    case COND_EXPR:
2829      error ("%s for `%T ? %T : %T' operator", problem,
2830		error_type (arg1), error_type (arg2), error_type (arg3));
2831      break;
2832    case POSTINCREMENT_EXPR:
2833    case POSTDECREMENT_EXPR:
2834      error ("%s for `%T %s' operator", problem, error_type (arg1), opname);
2835      break;
2836    case ARRAY_REF:
2837      error ("%s for `%T [%T]' operator", problem,
2838		error_type (arg1), error_type (arg2));
2839      break;
2840    default:
2841      if (arg2)
2842	error ("%s for `%T %s %T' operator", problem,
2843		  error_type (arg1), opname, error_type (arg2));
2844      else
2845	error ("%s for `%s %T' operator", problem, opname, error_type (arg1));
2846    }
2847}
2848
2849/* Return the implicit conversion sequence that could be used to
2850   convert E1 to E2 in [expr.cond].  */
2851
2852static tree
2853conditional_conversion (e1, e2)
2854     tree e1;
2855     tree e2;
2856{
2857  tree t1 = non_reference (TREE_TYPE (e1));
2858  tree t2 = non_reference (TREE_TYPE (e2));
2859  tree conv;
2860
2861  /* [expr.cond]
2862
2863     If E2 is an lvalue: E1 can be converted to match E2 if E1 can be
2864     implicitly converted (clause _conv_) to the type "reference to
2865     T2", subject to the constraint that in the conversion the
2866     reference must bind directly (_dcl.init.ref_) to E1.  */
2867  if (real_lvalue_p (e2))
2868    {
2869      conv = implicit_conversion (build_reference_type (t2),
2870				  t1,
2871				  e1,
2872				  LOOKUP_NO_TEMP_BIND);
2873      if (conv)
2874	return conv;
2875    }
2876
2877  /* [expr.cond]
2878
2879     If E1 and E2 have class type, and the underlying class types are
2880     the same or one is a base class of the other: E1 can be converted
2881     to match E2 if the class of T2 is the same type as, or a base
2882     class of, the class of T1, and the cv-qualification of T2 is the
2883     same cv-qualification as, or a greater cv-qualification than, the
2884     cv-qualification of T1.  If the conversion is applied, E1 is
2885     changed to an rvalue of type T2 that still refers to the original
2886     source class object (or the appropriate subobject thereof).  */
2887  if (CLASS_TYPE_P (t1) && CLASS_TYPE_P (t2)
2888      && same_or_base_type_p (TYPE_MAIN_VARIANT (t2),
2889			      TYPE_MAIN_VARIANT (t1)))
2890    {
2891      if (at_least_as_qualified_p (t2, t1))
2892	{
2893	  conv = build1 (IDENTITY_CONV, t1, e1);
2894	  if (!same_type_p (TYPE_MAIN_VARIANT (t1),
2895			    TYPE_MAIN_VARIANT (t2)))
2896	    conv = build_conv (BASE_CONV, t2, conv);
2897	  return conv;
2898	}
2899      else
2900	return NULL_TREE;
2901    }
2902
2903  /* [expr.cond]
2904
2905     E1 can be converted to match E2 if E1 can be implicitly converted
2906     to the type that expression E2 would have if E2 were converted to
2907     an rvalue (or the type it has, if E2 is an rvalue).  */
2908  return implicit_conversion (t2, t1, e1, LOOKUP_NORMAL);
2909}
2910
2911/* Implement [expr.cond].  ARG1, ARG2, and ARG3 are the three
2912   arguments to the conditional expression.  By the time this function
2913   is called, any suitable candidate functions are included in
2914   CANDIDATES.  */
2915
2916tree
2917build_conditional_expr (arg1, arg2, arg3)
2918     tree arg1;
2919     tree arg2;
2920     tree arg3;
2921{
2922  tree arg2_type;
2923  tree arg3_type;
2924  tree result;
2925  tree result_type = NULL_TREE;
2926  int lvalue_p = 1;
2927  struct z_candidate *candidates = 0;
2928  struct z_candidate *cand;
2929
2930  /* As a G++ extension, the second argument to the conditional can be
2931     omitted.  (So that `a ? : c' is roughly equivalent to `a ? a :
2932     c'.)  If the second operand is omitted, make sure it is
2933     calculated only once.  */
2934  if (!arg2)
2935    {
2936      if (pedantic)
2937	pedwarn ("ISO C++ forbids omitting the middle term of a ?: expression");
2938      arg1 = arg2 = save_expr (arg1);
2939    }
2940
2941  /* [expr.cond]
2942
2943     The first expr ession is implicitly converted to bool (clause
2944     _conv_).  */
2945  arg1 = cp_convert (boolean_type_node, arg1);
2946
2947  /* If something has already gone wrong, just pass that fact up the
2948     tree.  */
2949  if (arg1 == error_mark_node
2950      || arg2 == error_mark_node
2951      || arg3 == error_mark_node
2952      || TREE_TYPE (arg1) == error_mark_node
2953      || TREE_TYPE (arg2) == error_mark_node
2954      || TREE_TYPE (arg3) == error_mark_node)
2955    return error_mark_node;
2956
2957  /* [expr.cond]
2958
2959     If either the second or the third operand has type (possibly
2960     cv-qualified) void, then the lvalue-to-rvalue (_conv.lval_),
2961     array-to-pointer (_conv.array_), and function-to-pointer
2962     (_conv.func_) standard conversions are performed on the second
2963     and third operands.  */
2964  arg2_type = TREE_TYPE (arg2);
2965  arg3_type = TREE_TYPE (arg3);
2966  if (VOID_TYPE_P (arg2_type) || VOID_TYPE_P (arg3_type))
2967    {
2968      /* Do the conversions.  We don't these for `void' type arguments
2969	 since it can't have any effect and since decay_conversion
2970	 does not handle that case gracefully.  */
2971      if (!VOID_TYPE_P (arg2_type))
2972	arg2 = decay_conversion (arg2);
2973      if (!VOID_TYPE_P (arg3_type))
2974	arg3 = decay_conversion (arg3);
2975      arg2_type = TREE_TYPE (arg2);
2976      arg3_type = TREE_TYPE (arg3);
2977
2978      /* [expr.cond]
2979
2980	 One of the following shall hold:
2981
2982	 --The second or the third operand (but not both) is a
2983	   throw-expression (_except.throw_); the result is of the
2984	   type of the other and is an rvalue.
2985
2986	 --Both the second and the third operands have type void; the
2987	   result is of type void and is an rvalue.   */
2988      if ((TREE_CODE (arg2) == THROW_EXPR)
2989	  ^ (TREE_CODE (arg3) == THROW_EXPR))
2990	result_type = ((TREE_CODE (arg2) == THROW_EXPR)
2991		       ? arg3_type : arg2_type);
2992      else if (VOID_TYPE_P (arg2_type) && VOID_TYPE_P (arg3_type))
2993	result_type = void_type_node;
2994      else
2995	{
2996	  error ("`%E' has type `void' and is not a throw-expression",
2997		    VOID_TYPE_P (arg2_type) ? arg2 : arg3);
2998	  return error_mark_node;
2999	}
3000
3001      lvalue_p = 0;
3002      goto valid_operands;
3003    }
3004  /* [expr.cond]
3005
3006     Otherwise, if the second and third operand have different types,
3007     and either has (possibly cv-qualified) class type, an attempt is
3008     made to convert each of those operands to the type of the other.  */
3009  else if (!same_type_p (arg2_type, arg3_type)
3010	   && (CLASS_TYPE_P (arg2_type) || CLASS_TYPE_P (arg3_type)))
3011    {
3012      tree conv2 = conditional_conversion (arg2, arg3);
3013      tree conv3 = conditional_conversion (arg3, arg2);
3014
3015      /* [expr.cond]
3016
3017	 If both can be converted, or one can be converted but the
3018	 conversion is ambiguous, the program is ill-formed.  If
3019	 neither can be converted, the operands are left unchanged and
3020	 further checking is performed as described below.  If exactly
3021	 one conversion is possible, that conversion is applied to the
3022	 chosen operand and the converted operand is used in place of
3023	 the original operand for the remainder of this section.  */
3024      if ((conv2 && !ICS_BAD_FLAG (conv2)
3025	   && conv3 && !ICS_BAD_FLAG (conv3))
3026	  || (conv2 && TREE_CODE (conv2) == AMBIG_CONV)
3027	  || (conv3 && TREE_CODE (conv3) == AMBIG_CONV))
3028	{
3029	  error ("operands to ?: have different types");
3030	  return error_mark_node;
3031	}
3032      else if (conv2 && !ICS_BAD_FLAG (conv2))
3033	{
3034	  arg2 = convert_like (conv2, arg2);
3035	  arg2 = convert_from_reference (arg2);
3036	  /* That may not quite have done the trick.  If the two types
3037	     are cv-qualified variants of one another, we will have
3038	     just used an IDENTITY_CONV.  (There's no conversion from
3039	     an lvalue of one class type to an lvalue of another type,
3040	     even a cv-qualified variant, and we don't want to lose
3041	     lvalue-ness here.)  So, we manually add a NOP_EXPR here
3042	     if necessary.  */
3043	  if (!same_type_p (TREE_TYPE (arg2), arg3_type))
3044	    arg2 = build1 (NOP_EXPR, arg3_type, arg2);
3045	  arg2_type = TREE_TYPE (arg2);
3046	}
3047      else if (conv3 && !ICS_BAD_FLAG (conv3))
3048	{
3049	  arg3 = convert_like (conv3, arg3);
3050	  arg3 = convert_from_reference (arg3);
3051	  if (!same_type_p (TREE_TYPE (arg3), arg2_type))
3052	    arg3 = build1 (NOP_EXPR, arg2_type, arg3);
3053	  arg3_type = TREE_TYPE (arg3);
3054	}
3055    }
3056
3057  /* [expr.cond]
3058
3059     If the second and third operands are lvalues and have the same
3060     type, the result is of that type and is an lvalue.  */
3061  if (real_lvalue_p (arg2) && real_lvalue_p (arg3) &&
3062      same_type_p (arg2_type, arg3_type))
3063    {
3064      result_type = arg2_type;
3065      goto valid_operands;
3066    }
3067
3068  /* [expr.cond]
3069
3070     Otherwise, the result is an rvalue.  If the second and third
3071     operand do not have the same type, and either has (possibly
3072     cv-qualified) class type, overload resolution is used to
3073     determine the conversions (if any) to be applied to the operands
3074     (_over.match.oper_, _over.built_).  */
3075  lvalue_p = 0;
3076  if (!same_type_p (arg2_type, arg3_type)
3077      && (CLASS_TYPE_P (arg2_type) || CLASS_TYPE_P (arg3_type)))
3078    {
3079      tree args[3];
3080      tree conv;
3081
3082      /* Rearrange the arguments so that add_builtin_candidate only has
3083	 to know about two args.  In build_builtin_candidates, the
3084	 arguments are unscrambled.  */
3085      args[0] = arg2;
3086      args[1] = arg3;
3087      args[2] = arg1;
3088      candidates = add_builtin_candidates (candidates,
3089					   COND_EXPR,
3090					   NOP_EXPR,
3091					   ansi_opname (COND_EXPR),
3092					   args,
3093					   LOOKUP_NORMAL);
3094
3095      /* [expr.cond]
3096
3097	 If the overload resolution fails, the program is
3098	 ill-formed.  */
3099      if (!any_viable (candidates))
3100	{
3101	  op_error (COND_EXPR, NOP_EXPR, arg1, arg2, arg3, "no match");
3102	  print_z_candidates (candidates);
3103	  return error_mark_node;
3104	}
3105      candidates = splice_viable (candidates);
3106      cand = tourney (candidates);
3107      if (!cand)
3108	{
3109	  op_error (COND_EXPR, NOP_EXPR, arg1, arg2, arg3, "no match");
3110	  print_z_candidates (candidates);
3111	  return error_mark_node;
3112	}
3113
3114      /* [expr.cond]
3115
3116	 Otherwise, the conversions thus determined are applied, and
3117	 the converted operands are used in place of the original
3118	 operands for the remainder of this section.  */
3119      conv = TREE_VEC_ELT (cand->convs, 0);
3120      arg1 = convert_like (conv, arg1);
3121      conv = TREE_VEC_ELT (cand->convs, 1);
3122      arg2 = convert_like (conv, arg2);
3123      conv = TREE_VEC_ELT (cand->convs, 2);
3124      arg3 = convert_like (conv, arg3);
3125    }
3126
3127  /* [expr.cond]
3128
3129     Lvalue-to-rvalue (_conv.lval_), array-to-pointer (_conv.array_),
3130     and function-to-pointer (_conv.func_) standard conversions are
3131     performed on the second and third operands.
3132
3133     We need to force the lvalue-to-rvalue conversion here for class types,
3134     so we get TARGET_EXPRs; trying to deal with a COND_EXPR of class rvalues
3135     that isn't wrapped with a TARGET_EXPR plays havoc with exception
3136     regions.
3137
3138     We use ocp_convert rather than build_user_type_conversion because the
3139     latter returns NULL_TREE on failure, while the former gives an error.  */
3140
3141  if (IS_AGGR_TYPE (TREE_TYPE (arg2)) && real_lvalue_p (arg2))
3142    arg2 = ocp_convert (TREE_TYPE (arg2), arg2,
3143			CONV_IMPLICIT|CONV_FORCE_TEMP, LOOKUP_NORMAL);
3144  else
3145    arg2 = decay_conversion (arg2);
3146  arg2_type = TREE_TYPE (arg2);
3147
3148  if (IS_AGGR_TYPE (TREE_TYPE (arg3)) && real_lvalue_p (arg3))
3149    arg3 = ocp_convert (TREE_TYPE (arg3), arg3,
3150			CONV_IMPLICIT|CONV_FORCE_TEMP, LOOKUP_NORMAL);
3151  else
3152    arg3 = decay_conversion (arg3);
3153  arg3_type = TREE_TYPE (arg3);
3154
3155  if (arg2 == error_mark_node || arg3 == error_mark_node)
3156    return error_mark_node;
3157
3158  /* [expr.cond]
3159
3160     After those conversions, one of the following shall hold:
3161
3162     --The second and third operands have the same type; the result  is  of
3163       that type.  */
3164  if (same_type_p (arg2_type, arg3_type))
3165    result_type = arg2_type;
3166  /* [expr.cond]
3167
3168     --The second and third operands have arithmetic or enumeration
3169       type; the usual arithmetic conversions are performed to bring
3170       them to a common type, and the result is of that type.  */
3171  else if ((ARITHMETIC_TYPE_P (arg2_type)
3172	    || TREE_CODE (arg2_type) == ENUMERAL_TYPE)
3173	   && (ARITHMETIC_TYPE_P (arg3_type)
3174	       || TREE_CODE (arg3_type) == ENUMERAL_TYPE))
3175    {
3176      /* In this case, there is always a common type.  */
3177      result_type = type_after_usual_arithmetic_conversions (arg2_type,
3178							     arg3_type);
3179
3180      if (TREE_CODE (arg2_type) == ENUMERAL_TYPE
3181          && TREE_CODE (arg3_type) == ENUMERAL_TYPE)
3182         warning ("enumeral mismatch in conditional expression: `%T' vs `%T'",
3183                   arg2_type, arg3_type);
3184      else if (extra_warnings
3185               && ((TREE_CODE (arg2_type) == ENUMERAL_TYPE
3186                    && !same_type_p (arg3_type, type_promotes_to (arg2_type)))
3187                   || (TREE_CODE (arg3_type) == ENUMERAL_TYPE
3188                       && !same_type_p (arg2_type, type_promotes_to (arg3_type)))))
3189        warning ("enumeral and non-enumeral type in conditional expression");
3190
3191      arg2 = perform_implicit_conversion (result_type, arg2);
3192      arg3 = perform_implicit_conversion (result_type, arg3);
3193    }
3194  /* [expr.cond]
3195
3196     --The second and third operands have pointer type, or one has
3197       pointer type and the other is a null pointer constant; pointer
3198       conversions (_conv.ptr_) and qualification conversions
3199       (_conv.qual_) are performed to bring them to their composite
3200       pointer type (_expr.rel_).  The result is of the composite
3201       pointer type.
3202
3203     --The second and third operands have pointer to member type, or
3204       one has pointer to member type and the other is a null pointer
3205       constant; pointer to member conversions (_conv.mem_) and
3206       qualification conversions (_conv.qual_) are performed to bring
3207       them to a common type, whose cv-qualification shall match the
3208       cv-qualification of either the second or the third operand.
3209       The result is of the common type.   */
3210  else if ((null_ptr_cst_p (arg2)
3211	    && (TYPE_PTR_P (arg3_type) || TYPE_PTRMEM_P (arg3_type)
3212		|| TYPE_PTRMEMFUNC_P (arg3_type)))
3213	   || (null_ptr_cst_p (arg3)
3214	       && (TYPE_PTR_P (arg2_type) || TYPE_PTRMEM_P (arg2_type)
3215		|| TYPE_PTRMEMFUNC_P (arg2_type)))
3216	   || (TYPE_PTR_P (arg2_type) && TYPE_PTR_P (arg3_type))
3217	   || (TYPE_PTRMEM_P (arg2_type) && TYPE_PTRMEM_P (arg3_type))
3218	   || (TYPE_PTRMEMFUNC_P (arg2_type)
3219	       && TYPE_PTRMEMFUNC_P (arg3_type)))
3220    {
3221      result_type = composite_pointer_type (arg2_type, arg3_type, arg2,
3222					    arg3, "conditional expression");
3223      arg2 = perform_implicit_conversion (result_type, arg2);
3224      arg3 = perform_implicit_conversion (result_type, arg3);
3225    }
3226
3227  if (!result_type)
3228    {
3229      error ("operands to ?: have different types");
3230      return error_mark_node;
3231    }
3232
3233 valid_operands:
3234  result = fold (build (COND_EXPR, result_type, arg1, arg2, arg3));
3235  /* Expand both sides into the same slot, hopefully the target of the
3236     ?: expression.  We used to check for TARGET_EXPRs here, but now we
3237     sometimes wrap them in NOP_EXPRs so the test would fail.  */
3238  if (!lvalue_p && IS_AGGR_TYPE (result_type))
3239    result = build_target_expr_with_type (result, result_type);
3240
3241  /* If this expression is an rvalue, but might be mistaken for an
3242     lvalue, we must add a NON_LVALUE_EXPR.  */
3243  if (!lvalue_p && real_lvalue_p (result))
3244    result = build1 (NON_LVALUE_EXPR, result_type, result);
3245
3246  return result;
3247}
3248
3249tree
3250build_new_op (code, flags, arg1, arg2, arg3)
3251     enum tree_code code;
3252     int flags;
3253     tree arg1, arg2, arg3;
3254{
3255  struct z_candidate *candidates = 0, *cand;
3256  tree fns, mem_arglist = NULL_TREE, arglist, fnname;
3257  enum tree_code code2 = NOP_EXPR;
3258  tree templates = NULL_TREE;
3259  tree conv;
3260
3261  if (arg1 == error_mark_node
3262      || arg2 == error_mark_node
3263      || arg3 == error_mark_node)
3264    return error_mark_node;
3265
3266  /* This can happen if a template takes all non-type parameters, e.g.
3267     undeclared_template<1, 5, 72>a;  */
3268  if (code == LT_EXPR && TREE_CODE (arg1) == TEMPLATE_DECL)
3269    {
3270      error ("`%D' must be declared before use", arg1);
3271      return error_mark_node;
3272    }
3273
3274  if (code == MODIFY_EXPR)
3275    {
3276      code2 = TREE_CODE (arg3);
3277      arg3 = NULL_TREE;
3278      fnname = ansi_assopname (code2);
3279    }
3280  else
3281    fnname = ansi_opname (code);
3282
3283  if (TREE_CODE (arg1) == OFFSET_REF)
3284    arg1 = resolve_offset_ref (arg1);
3285  arg1 = convert_from_reference (arg1);
3286
3287  switch (code)
3288    {
3289    case NEW_EXPR:
3290    case VEC_NEW_EXPR:
3291    case VEC_DELETE_EXPR:
3292    case DELETE_EXPR:
3293      /* Use build_op_new_call and build_op_delete_call instead. */
3294      abort ();
3295
3296    case CALL_EXPR:
3297      return build_object_call (arg1, arg2);
3298
3299    default:
3300      break;
3301    }
3302
3303  if (arg2)
3304    {
3305      if (TREE_CODE (arg2) == OFFSET_REF)
3306	arg2 = resolve_offset_ref (arg2);
3307      arg2 = convert_from_reference (arg2);
3308    }
3309  if (arg3)
3310    {
3311      if (TREE_CODE (arg3) == OFFSET_REF)
3312	arg3 = resolve_offset_ref (arg3);
3313      arg3 = convert_from_reference (arg3);
3314    }
3315
3316  if (code == COND_EXPR)
3317    {
3318      if (arg2 == NULL_TREE
3319	  || TREE_CODE (TREE_TYPE (arg2)) == VOID_TYPE
3320	  || TREE_CODE (TREE_TYPE (arg3)) == VOID_TYPE
3321	  || (! IS_OVERLOAD_TYPE (TREE_TYPE (arg2))
3322	      && ! IS_OVERLOAD_TYPE (TREE_TYPE (arg3))))
3323	goto builtin;
3324    }
3325  else if (! IS_OVERLOAD_TYPE (TREE_TYPE (arg1))
3326	   && (! arg2 || ! IS_OVERLOAD_TYPE (TREE_TYPE (arg2))))
3327    goto builtin;
3328
3329  if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
3330    arg2 = integer_zero_node;
3331
3332  arglist = NULL_TREE;
3333  if (arg3)
3334    arglist = tree_cons (NULL_TREE, arg3, arglist);
3335  if (arg2)
3336    arglist = tree_cons (NULL_TREE, arg2, arglist);
3337  arglist = tree_cons (NULL_TREE, arg1, arglist);
3338
3339  fns = lookup_function_nonclass (fnname, arglist);
3340
3341  if (fns && TREE_CODE (fns) == TREE_LIST)
3342    fns = TREE_VALUE (fns);
3343  for (; fns; fns = OVL_NEXT (fns))
3344    {
3345      tree fn = OVL_CURRENT (fns);
3346      if (TREE_CODE (fn) == TEMPLATE_DECL)
3347	{
3348	  templates = tree_cons (NULL_TREE, fn, templates);
3349	  candidates
3350	    = add_template_candidate (candidates, fn, NULL_TREE, NULL_TREE,
3351				      arglist, TREE_TYPE (fnname),
3352				      flags, DEDUCE_CALL);
3353	}
3354      else
3355	candidates = add_function_candidate (candidates, fn, NULL_TREE,
3356					     arglist, flags);
3357    }
3358
3359  if (IS_AGGR_TYPE (TREE_TYPE (arg1)))
3360    {
3361      fns = lookup_fnfields (TYPE_BINFO (TREE_TYPE (arg1)), fnname, 1);
3362      if (fns == error_mark_node)
3363	return fns;
3364    }
3365  else
3366    fns = NULL_TREE;
3367
3368  if (fns)
3369    {
3370      tree basetype = BINFO_TYPE (TREE_PURPOSE (fns));
3371      mem_arglist = tree_cons (NULL_TREE, build_this (arg1), TREE_CHAIN (arglist));
3372      for (fns = TREE_VALUE (fns); fns; fns = OVL_NEXT (fns))
3373	{
3374	  tree fn = OVL_CURRENT (fns);
3375	  tree this_arglist;
3376
3377	  if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
3378	    this_arglist = mem_arglist;
3379	  else
3380	    this_arglist = arglist;
3381
3382	  if (TREE_CODE (fn) == TEMPLATE_DECL)
3383	    {
3384	      /* A member template. */
3385	      templates = tree_cons (NULL_TREE, fn, templates);
3386	      candidates
3387		= add_template_candidate (candidates, fn, basetype, NULL_TREE,
3388					  this_arglist,  TREE_TYPE (fnname),
3389					  flags, DEDUCE_CALL);
3390	    }
3391	  else
3392	    candidates = add_function_candidate
3393	      (candidates, fn, basetype, this_arglist, flags);
3394
3395	  if (candidates)
3396	    candidates->basetype_path = TYPE_BINFO (TREE_TYPE (arg1));
3397	}
3398    }
3399
3400  {
3401    tree args[3];
3402
3403    /* Rearrange the arguments for ?: so that add_builtin_candidate only has
3404       to know about two args; a builtin candidate will always have a first
3405       parameter of type bool.  We'll handle that in
3406       build_builtin_candidate.  */
3407    if (code == COND_EXPR)
3408      {
3409	args[0] = arg2;
3410	args[1] = arg3;
3411	args[2] = arg1;
3412      }
3413    else
3414      {
3415	args[0] = arg1;
3416	args[1] = arg2;
3417	args[2] = NULL_TREE;
3418      }
3419
3420    candidates = add_builtin_candidates
3421      (candidates, code, code2, fnname, args, flags);
3422  }
3423
3424  if (! any_viable (candidates))
3425    {
3426      switch (code)
3427	{
3428	case POSTINCREMENT_EXPR:
3429	case POSTDECREMENT_EXPR:
3430	  /* Look for an `operator++ (int)'.  If they didn't have
3431	     one, then we fall back to the old way of doing things.  */
3432	  if (flags & LOOKUP_COMPLAIN)
3433	    pedwarn ("no `%D(int)' declared for postfix `%s', trying prefix operator instead",
3434			fnname,
3435			operator_name_info[code].name);
3436	  if (code == POSTINCREMENT_EXPR)
3437	    code = PREINCREMENT_EXPR;
3438	  else
3439	    code = PREDECREMENT_EXPR;
3440	  return build_new_op (code, flags, arg1, NULL_TREE, NULL_TREE);
3441
3442	  /* The caller will deal with these.  */
3443	case ADDR_EXPR:
3444	case COMPOUND_EXPR:
3445	case COMPONENT_REF:
3446	  return NULL_TREE;
3447
3448	default:
3449	  break;
3450	}
3451      if (flags & LOOKUP_COMPLAIN)
3452	{
3453	  op_error (code, code2, arg1, arg2, arg3, "no match");
3454	  print_z_candidates (candidates);
3455	}
3456      return error_mark_node;
3457    }
3458  candidates = splice_viable (candidates);
3459  cand = tourney (candidates);
3460
3461  if (cand == 0)
3462    {
3463      if (flags & LOOKUP_COMPLAIN)
3464	{
3465	  op_error (code, code2, arg1, arg2, arg3, "ambiguous overload");
3466	  print_z_candidates (candidates);
3467	}
3468      return error_mark_node;
3469    }
3470
3471  if (TREE_CODE (cand->fn) == FUNCTION_DECL)
3472    {
3473      extern int warn_synth;
3474      if (warn_synth
3475	  && fnname == ansi_assopname (NOP_EXPR)
3476	  && DECL_ARTIFICIAL (cand->fn)
3477	  && candidates->next
3478	  && ! candidates->next->next)
3479	{
3480	  warning ("using synthesized `%#D' for copy assignment",
3481		      cand->fn);
3482	  cp_warning_at ("  where cfront would use `%#D'",
3483			 cand == candidates
3484			 ? candidates->next->fn
3485			 : candidates->fn);
3486	}
3487
3488      return build_over_call
3489	(cand,
3490	 TREE_CODE (TREE_TYPE (cand->fn)) == METHOD_TYPE
3491	 ? mem_arglist : arglist,
3492	 LOOKUP_NORMAL);
3493    }
3494
3495  /* Check for comparison of different enum types.  */
3496  switch (code)
3497    {
3498    case GT_EXPR:
3499    case LT_EXPR:
3500    case GE_EXPR:
3501    case LE_EXPR:
3502    case EQ_EXPR:
3503    case NE_EXPR:
3504      if (TREE_CODE (TREE_TYPE (arg1)) == ENUMERAL_TYPE
3505	  && TREE_CODE (TREE_TYPE (arg2)) == ENUMERAL_TYPE
3506	  && (TYPE_MAIN_VARIANT (TREE_TYPE (arg1))
3507	      != TYPE_MAIN_VARIANT (TREE_TYPE (arg2))))
3508	{
3509	  warning ("comparison between `%#T' and `%#T'",
3510		      TREE_TYPE (arg1), TREE_TYPE (arg2));
3511	}
3512      break;
3513    default:
3514      break;
3515    }
3516
3517  /* We need to strip any leading REF_BIND so that bitfields don't cause
3518     errors.  This should not remove any important conversions, because
3519     builtins don't apply to class objects directly.  */
3520  conv = TREE_VEC_ELT (cand->convs, 0);
3521  if (TREE_CODE (conv) == REF_BIND)
3522    conv = TREE_OPERAND (conv, 0);
3523  arg1 = convert_like (conv, arg1);
3524  if (arg2)
3525    {
3526      conv = TREE_VEC_ELT (cand->convs, 1);
3527      if (TREE_CODE (conv) == REF_BIND)
3528        conv = TREE_OPERAND (conv, 0);
3529      arg2 = convert_like (conv, arg2);
3530    }
3531  if (arg3)
3532    {
3533      conv = TREE_VEC_ELT (cand->convs, 2);
3534      if (TREE_CODE (conv) == REF_BIND)
3535        conv = TREE_OPERAND (conv, 0);
3536      arg3 = convert_like (conv, arg3);
3537    }
3538
3539builtin:
3540  switch (code)
3541    {
3542    case MODIFY_EXPR:
3543      return build_modify_expr (arg1, code2, arg2);
3544
3545    case INDIRECT_REF:
3546      return build_indirect_ref (arg1, "unary *");
3547
3548    case PLUS_EXPR:
3549    case MINUS_EXPR:
3550    case MULT_EXPR:
3551    case TRUNC_DIV_EXPR:
3552    case GT_EXPR:
3553    case LT_EXPR:
3554    case GE_EXPR:
3555    case LE_EXPR:
3556    case EQ_EXPR:
3557    case NE_EXPR:
3558    case MAX_EXPR:
3559    case MIN_EXPR:
3560    case LSHIFT_EXPR:
3561    case RSHIFT_EXPR:
3562    case TRUNC_MOD_EXPR:
3563    case BIT_AND_EXPR:
3564    case BIT_IOR_EXPR:
3565    case BIT_XOR_EXPR:
3566    case TRUTH_ANDIF_EXPR:
3567    case TRUTH_ORIF_EXPR:
3568      return cp_build_binary_op (code, arg1, arg2);
3569
3570    case CONVERT_EXPR:
3571    case NEGATE_EXPR:
3572    case BIT_NOT_EXPR:
3573    case TRUTH_NOT_EXPR:
3574    case PREINCREMENT_EXPR:
3575    case POSTINCREMENT_EXPR:
3576    case PREDECREMENT_EXPR:
3577    case POSTDECREMENT_EXPR:
3578    case REALPART_EXPR:
3579    case IMAGPART_EXPR:
3580      return build_unary_op (code, arg1, candidates != 0);
3581
3582    case ARRAY_REF:
3583      return build_array_ref (arg1, arg2);
3584
3585    case COND_EXPR:
3586      return build_conditional_expr (arg1, arg2, arg3);
3587
3588    case MEMBER_REF:
3589      return build_m_component_ref
3590	(build_indirect_ref (arg1, NULL), arg2);
3591
3592      /* The caller will deal with these.  */
3593    case ADDR_EXPR:
3594    case COMPONENT_REF:
3595    case COMPOUND_EXPR:
3596      return NULL_TREE;
3597
3598    default:
3599      abort ();
3600      return NULL_TREE;
3601    }
3602}
3603
3604/* Build a call to operator delete.  This has to be handled very specially,
3605   because the restrictions on what signatures match are different from all
3606   other call instances.  For a normal delete, only a delete taking (void *)
3607   or (void *, size_t) is accepted.  For a placement delete, only an exact
3608   match with the placement new is accepted.
3609
3610   CODE is either DELETE_EXPR or VEC_DELETE_EXPR.
3611   ADDR is the pointer to be deleted.
3612   SIZE is the size of the memory block to be deleted.
3613   FLAGS are the usual overloading flags.
3614   PLACEMENT is the corresponding placement new call, or NULL_TREE.  */
3615
3616tree
3617build_op_delete_call (code, addr, size, flags, placement)
3618     enum tree_code code;
3619     tree addr, size, placement;
3620     int flags;
3621{
3622  tree fn = NULL_TREE;
3623  tree fns, fnname, fntype, argtypes, args, type;
3624  int pass;
3625
3626  if (addr == error_mark_node)
3627    return error_mark_node;
3628
3629  type = TREE_TYPE (TREE_TYPE (addr));
3630  while (TREE_CODE (type) == ARRAY_TYPE)
3631    type = TREE_TYPE (type);
3632
3633  fnname = ansi_opname (code);
3634
3635  if (IS_AGGR_TYPE (type) && ! (flags & LOOKUP_GLOBAL))
3636    /* In [class.free]
3637
3638       If the result of the lookup is ambiguous or inaccessible, or if
3639       the lookup selects a placement deallocation function, the
3640       program is ill-formed.
3641
3642       Therefore, we ask lookup_fnfields to complain ambout ambiguity.  */
3643    {
3644      fns = lookup_fnfields (TYPE_BINFO (type), fnname, 1);
3645      if (fns == error_mark_node)
3646	return error_mark_node;
3647    }
3648  else
3649    fns = NULL_TREE;
3650
3651  if (fns == NULL_TREE)
3652    fns = lookup_name_nonclass (fnname);
3653
3654  if (placement)
3655    {
3656      tree alloc_fn;
3657      tree call_expr;
3658
3659      /* Find the allocation function that is being called. */
3660      call_expr = placement;
3661      /* Sometimes we have a COMPOUND_EXPR, rather than a simple
3662	 CALL_EXPR. */
3663      while (TREE_CODE (call_expr) == COMPOUND_EXPR)
3664	call_expr = TREE_OPERAND (call_expr, 1);
3665      /* Extract the function.  */
3666      alloc_fn = get_callee_fndecl (call_expr);
3667      my_friendly_assert (alloc_fn != NULL_TREE, 20020327);
3668      /* Then the second parm type.  */
3669      argtypes = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (alloc_fn)));
3670      /* Also the second argument.  */
3671      args = TREE_CHAIN (TREE_OPERAND (call_expr, 1));
3672    }
3673  else
3674    {
3675      /* First try it without the size argument.  */
3676      argtypes = void_list_node;
3677      args = NULL_TREE;
3678    }
3679
3680  /* Strip const and volatile from addr.  */
3681  addr = cp_convert (ptr_type_node, addr);
3682
3683  /* We make two tries at finding a matching `operator delete'.  On
3684     the first pass, we look for an one-operator (or placement)
3685     operator delete.  If we're not doing placement delete, then on
3686     the second pass we look for a two-argument delete.  */
3687  for (pass = 0; pass < (placement ? 1 : 2); ++pass)
3688    {
3689      if (pass == 0)
3690	argtypes = tree_cons (NULL_TREE, ptr_type_node, argtypes);
3691      else
3692	/* Normal delete; now try to find a match including the size
3693	   argument.  */
3694	argtypes = tree_cons (NULL_TREE, ptr_type_node,
3695			      tree_cons (NULL_TREE, sizetype,
3696					 void_list_node));
3697      fntype = build_function_type (void_type_node, argtypes);
3698
3699      /* Go through the `operator delete' functions looking for one
3700	 with a matching type.  */
3701      for (fn = BASELINK_P (fns) ? TREE_VALUE (fns) : fns;
3702	   fn;
3703	   fn = OVL_NEXT (fn))
3704	{
3705	  tree t;
3706
3707	  /* Exception specifications on the `delete' operator do not
3708	     matter.  */
3709	  t = build_exception_variant (TREE_TYPE (OVL_CURRENT (fn)),
3710				       NULL_TREE);
3711	  /* We also don't compare attributes.  We're really just
3712	     trying to check the types of the first two parameters.  */
3713	  if (comptypes (t, fntype, COMPARE_NO_ATTRIBUTES))
3714	    break;
3715	}
3716
3717      /* If we found a match, we're done.  */
3718      if (fn)
3719	break;
3720    }
3721
3722  /* If we have a matching function, call it.  */
3723  if (fn)
3724    {
3725      /* Make sure we have the actual function, and not an
3726	 OVERLOAD.  */
3727      fn = OVL_CURRENT (fn);
3728
3729      /* If the FN is a member function, make sure that it is
3730	 accessible.  */
3731      if (DECL_CLASS_SCOPE_P (fn))
3732	enforce_access (type, fn);
3733
3734      if (pass == 0)
3735	args = tree_cons (NULL_TREE, addr, args);
3736      else
3737	args = tree_cons (NULL_TREE, addr,
3738			  build_tree_list (NULL_TREE, size));
3739
3740      return build_function_call (fn, args);
3741    }
3742
3743  /* If we are doing placement delete we do nothing if we don't find a
3744     matching op delete.  */
3745  if (placement)
3746    return NULL_TREE;
3747
3748  error ("no suitable `operator delete' for `%T'", type);
3749  return error_mark_node;
3750}
3751
3752/* If the current scope isn't allowed to access DECL along
3753   BASETYPE_PATH, give an error.  The most derived class in
3754   BASETYPE_PATH is the one used to qualify DECL.  */
3755
3756int
3757enforce_access (basetype_path, decl)
3758     tree basetype_path;
3759     tree decl;
3760{
3761  int accessible;
3762
3763  accessible = accessible_p (basetype_path, decl);
3764  if (!accessible)
3765    {
3766      if (TREE_PRIVATE (decl))
3767	cp_error_at ("`%+#D' is private", decl);
3768      else if (TREE_PROTECTED (decl))
3769	cp_error_at ("`%+#D' is protected", decl);
3770      else
3771	cp_error_at ("`%+#D' is inaccessible", decl);
3772      error ("within this context");
3773      return 0;
3774    }
3775
3776  return 1;
3777}
3778
3779/* Perform the conversions in CONVS on the expression EXPR.
3780   FN and ARGNUM are used for diagnostics.  ARGNUM is zero based, -1
3781   indicates the `this' argument of a method.  INNER is non-zero when
3782   being called to continue a conversion chain. It is negative when a
3783   reference binding will be applied, positive otherwise. */
3784
3785static tree
3786convert_like_real (convs, expr, fn, argnum, inner)
3787     tree convs, expr;
3788     tree fn;
3789     int argnum;
3790     int inner;
3791{
3792  int savew, savee;
3793
3794  tree totype = TREE_TYPE (convs);
3795
3796  if (ICS_BAD_FLAG (convs)
3797      && TREE_CODE (convs) != USER_CONV
3798      && TREE_CODE (convs) != AMBIG_CONV
3799      && TREE_CODE (convs) != REF_BIND)
3800    {
3801      tree t = convs;
3802      for (; t; t = TREE_OPERAND (t, 0))
3803	{
3804	  if (TREE_CODE (t) == USER_CONV || !ICS_BAD_FLAG (t))
3805	    {
3806	      expr = convert_like_real (t, expr, fn, argnum, 1);
3807	      break;
3808	    }
3809	  else if (TREE_CODE (t) == AMBIG_CONV)
3810	    return convert_like_real (t, expr, fn, argnum, 1);
3811	  else if (TREE_CODE (t) == IDENTITY_CONV)
3812	    break;
3813	}
3814      pedwarn ("invalid conversion from `%T' to `%T'", TREE_TYPE (expr), totype);
3815      if (fn)
3816	pedwarn ("  initializing argument %P of `%D'", argnum, fn);
3817      return cp_convert (totype, expr);
3818    }
3819
3820  if (!inner)
3821    expr = dubious_conversion_warnings
3822             (totype, expr, "argument", fn, argnum);
3823  switch (TREE_CODE (convs))
3824    {
3825    case USER_CONV:
3826      {
3827	struct z_candidate *cand
3828	  = WRAPPER_PTR (TREE_OPERAND (convs, 1));
3829	tree convfn = cand->fn;
3830	tree args;
3831
3832	if (DECL_CONSTRUCTOR_P (convfn))
3833	  {
3834	    tree t = build_int_2 (0, 0);
3835	    TREE_TYPE (t) = build_pointer_type (DECL_CONTEXT (convfn));
3836
3837	    args = build_tree_list (NULL_TREE, expr);
3838	    if (DECL_HAS_IN_CHARGE_PARM_P (convfn)
3839		|| DECL_HAS_VTT_PARM_P (convfn))
3840	      /* We should never try to call the abstract or base constructor
3841		 from here.  */
3842	      abort ();
3843	    args = tree_cons (NULL_TREE, t, args);
3844	  }
3845	else
3846	  args = build_this (expr);
3847	expr = build_over_call (cand, args, LOOKUP_NORMAL);
3848
3849	/* If this is a constructor or a function returning an aggr type,
3850	   we need to build up a TARGET_EXPR.  */
3851	if (DECL_CONSTRUCTOR_P (convfn))
3852	  expr = build_cplus_new (totype, expr);
3853
3854	/* The result of the call is then used to direct-initialize the object
3855	   that is the destination of the copy-initialization.  [dcl.init]
3856
3857	   Note that this step is not reflected in the conversion sequence;
3858	   it affects the semantics when we actually perform the
3859	   conversion, but is not considered during overload resolution.
3860
3861	   If the target is a class, that means call a ctor.  */
3862	if (IS_AGGR_TYPE (totype)
3863	    && (inner >= 0 || !lvalue_p (expr)))
3864	  {
3865	    savew = warningcount, savee = errorcount;
3866	    expr = build_new_method_call
3867	      (NULL_TREE, complete_ctor_identifier,
3868	       build_tree_list (NULL_TREE, expr), TYPE_BINFO (totype),
3869	       /* Core issue 84, now a DR, says that we don't allow UDCs
3870		  for these args (which deliberately breaks copy-init of an
3871		  auto_ptr<Base> from an auto_ptr<Derived>).  */
3872	       LOOKUP_NORMAL|LOOKUP_ONLYCONVERTING|LOOKUP_NO_CONVERSION);
3873
3874	    /* Tell the user where this failing constructor call came from.  */
3875	    if (fn)
3876	      {
3877		if (warningcount > savew)
3878		  warning
3879		    ("  initializing argument %P of `%D' from result of `%D'",
3880		     argnum, fn, convfn);
3881		else if (errorcount > savee)
3882		  error
3883		    ("  initializing argument %P of `%D' from result of `%D'",
3884		     argnum, fn, convfn);
3885	      }
3886	    else
3887	      {
3888		if (warningcount > savew)
3889		  warning ("  initializing temporary from result of `%D'",
3890			      convfn);
3891		else if (errorcount > savee)
3892		  error ("  initializing temporary from result of `%D'",
3893			    convfn);
3894	      }
3895	    expr = build_cplus_new (totype, expr);
3896	  }
3897	return expr;
3898      }
3899    case IDENTITY_CONV:
3900      if (type_unknown_p (expr))
3901	expr = instantiate_type (totype, expr, tf_error | tf_warning);
3902      return expr;
3903    case AMBIG_CONV:
3904      /* Call build_user_type_conversion again for the error.  */
3905      return build_user_type_conversion
3906	(totype, TREE_OPERAND (convs, 0), LOOKUP_NORMAL);
3907
3908    default:
3909      break;
3910    };
3911
3912  expr = convert_like_real (TREE_OPERAND (convs, 0), expr, fn, argnum,
3913                            TREE_CODE (convs) == REF_BIND ? -1 : 1);
3914  if (expr == error_mark_node)
3915    return error_mark_node;
3916
3917  /* Convert a non-array constant variable to its underlying value, unless we
3918     are about to bind it to a reference, in which case we need to
3919     leave it as an lvalue.  */
3920  if (TREE_CODE (convs) != REF_BIND
3921      && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
3922    expr = decl_constant_value (expr);
3923
3924  switch (TREE_CODE (convs))
3925    {
3926    case RVALUE_CONV:
3927      if (! IS_AGGR_TYPE (totype))
3928	return expr;
3929      /* else fall through */
3930    case BASE_CONV:
3931      if (TREE_CODE (convs) == BASE_CONV && !NEED_TEMPORARY_P (convs))
3932	{
3933	  /* We are going to bind a reference directly to a base-class
3934	     subobject of EXPR.  */
3935	  tree base_ptr = build_pointer_type (totype);
3936
3937	  /* Build an expression for `*((base*) &expr)'.  */
3938	  expr = build_unary_op (ADDR_EXPR, expr, 0);
3939	  expr = perform_implicit_conversion (base_ptr, expr);
3940	  expr = build_indirect_ref (expr, "implicit conversion");
3941	  return expr;
3942	}
3943
3944      /* Copy-initialization where the cv-unqualified version of the source
3945	 type is the same class as, or a derived class of, the class of the
3946	 destination [is treated as direct-initialization].  [dcl.init] */
3947      savew = warningcount, savee = errorcount;
3948      expr = build_new_method_call (NULL_TREE, complete_ctor_identifier,
3949				    build_tree_list (NULL_TREE, expr),
3950				    TYPE_BINFO (totype),
3951				    LOOKUP_NORMAL|LOOKUP_ONLYCONVERTING);
3952      if (fn)
3953	{
3954	  if (warningcount > savew)
3955	    warning ("  initializing argument %P of `%D'", argnum, fn);
3956	  else if (errorcount > savee)
3957	    error ("  initializing argument %P of `%D'", argnum, fn);
3958	}
3959      return build_cplus_new (totype, expr);
3960
3961    case REF_BIND:
3962      {
3963	tree ref_type = totype;
3964
3965	/* If necessary, create a temporary.  */
3966	if (NEED_TEMPORARY_P (convs) || !lvalue_p (expr))
3967	  {
3968	    tree type = TREE_TYPE (TREE_OPERAND (convs, 0));
3969	    expr = build_target_expr_with_type (expr, type);
3970	  }
3971
3972	/* Take the address of the thing to which we will bind the
3973	   reference.  */
3974	expr = build_unary_op (ADDR_EXPR, expr, 1);
3975	if (expr == error_mark_node)
3976	  return error_mark_node;
3977
3978	/* Convert it to a pointer to the type referred to by the
3979	   reference.  This will adjust the pointer if a derived to
3980	   base conversion is being performed.  */
3981	expr = cp_convert (build_pointer_type (TREE_TYPE (ref_type)),
3982			   expr);
3983	/* Convert the pointer to the desired reference type.  */
3984	expr = build1 (NOP_EXPR, ref_type, expr);
3985
3986	return expr;
3987      }
3988
3989    case LVALUE_CONV:
3990      return decay_conversion (expr);
3991
3992    case QUAL_CONV:
3993      /* Warn about deprecated conversion if appropriate.  */
3994      string_conv_p (totype, expr, 1);
3995      break;
3996
3997    default:
3998      break;
3999    }
4000  return ocp_convert (totype, expr, CONV_IMPLICIT,
4001		      LOOKUP_NORMAL|LOOKUP_NO_CONVERSION);
4002}
4003
4004/* ARG is being passed to a varargs function.  Perform any conversions
4005   required.  Array/function to pointer decay must have already happened.
4006   Return the converted value.  */
4007
4008tree
4009convert_arg_to_ellipsis (arg)
4010     tree arg;
4011{
4012  if (TREE_CODE (TREE_TYPE (arg)) == REAL_TYPE
4013      && (TYPE_PRECISION (TREE_TYPE (arg))
4014	  < TYPE_PRECISION (double_type_node)))
4015    /* Convert `float' to `double'.  */
4016    arg = cp_convert (double_type_node, arg);
4017  else
4018    /* Convert `short' and `char' to full-size `int'.  */
4019    arg = default_conversion (arg);
4020
4021  arg = require_complete_type (arg);
4022
4023  if (arg != error_mark_node && ! pod_type_p (TREE_TYPE (arg)))
4024    {
4025      /* Undefined behaviour [expr.call] 5.2.2/7.  We used to just warn
4026	 here and do a bitwise copy, but now cp_expr_size will abort if we
4027	 try to do that.  */
4028      error ("cannot pass objects of non-POD type `%#T' through `...'",
4029	     TREE_TYPE (arg));
4030      arg = error_mark_node;
4031    }
4032
4033  return arg;
4034}
4035
4036/* va_arg (EXPR, TYPE) is a builtin. Make sure it is not abused.  */
4037
4038tree
4039build_x_va_arg (expr, type)
4040     tree expr;
4041     tree type;
4042{
4043  if (processing_template_decl)
4044    return build_min (VA_ARG_EXPR, type, expr);
4045
4046  type = complete_type_or_else (type, NULL_TREE);
4047
4048  if (expr == error_mark_node || !type)
4049    return error_mark_node;
4050
4051  if (! pod_type_p (type))
4052    {
4053      /* Undefined behaviour [expr.call] 5.2.2/7.  */
4054      warning ("cannot receive objects of non-POD type `%#T' through `...'",
4055		  type);
4056    }
4057
4058  return build_va_arg (expr, type);
4059}
4060
4061/* TYPE has been given to va_arg. Apply the default conversions which would
4062   have happened when passed via ellipsis. Return the promoted type, or
4063   NULL_TREE, if there is no change.  */
4064
4065tree
4066convert_type_from_ellipsis (type)
4067     tree type;
4068{
4069  tree promote;
4070
4071  if (TREE_CODE (type) == ARRAY_TYPE)
4072    promote = build_pointer_type (TREE_TYPE (type));
4073  else if (TREE_CODE (type) == FUNCTION_TYPE)
4074    promote = build_pointer_type (type);
4075  else
4076    promote = type_promotes_to (type);
4077
4078  return same_type_p (type, promote) ? NULL_TREE : promote;
4079}
4080
4081/* ARG is a default argument expression being passed to a parameter of
4082   the indicated TYPE, which is a parameter to FN.  Do any required
4083   conversions.  Return the converted value.  */
4084
4085tree
4086convert_default_arg (type, arg, fn, parmnum)
4087     tree type;
4088     tree arg;
4089     tree fn;
4090     int parmnum;
4091{
4092  if (TREE_CODE (arg) == DEFAULT_ARG)
4093    {
4094      /* When processing the default args for a class, we can find that
4095         there is an ordering constraint, and we call a function who's
4096         default args have not yet been converted. For instance,
4097          class A {
4098              A (int = 0);
4099              void Foo (A const & = A ());
4100          };
4101         We must process A::A before A::Foo's default arg can be converted.
4102         Remember the dependent function, so do_pending_defargs can retry,
4103         and check loops.  */
4104      unprocessed_defarg_fn (fn);
4105
4106      /* Don't return error_mark node, as we won't be able to distinguish
4107         genuine errors from this case, and that would lead to repeated
4108         diagnostics.  Just make something of the right type.  */
4109      return build1 (NOP_EXPR, type, integer_zero_node);
4110    }
4111
4112  if (fn && DECL_TEMPLATE_INFO (fn))
4113    arg = tsubst_default_argument (fn, type, arg);
4114
4115  arg = break_out_target_exprs (arg);
4116
4117  if (TREE_CODE (arg) == CONSTRUCTOR)
4118    {
4119      arg = digest_init (type, arg, 0);
4120      arg = convert_for_initialization (0, type, arg, LOOKUP_NORMAL,
4121					"default argument", fn, parmnum);
4122    }
4123  else
4124    {
4125      /* This could get clobbered by the following call.  */
4126      if (TREE_HAS_CONSTRUCTOR (arg))
4127	arg = copy_node (arg);
4128
4129      arg = convert_for_initialization (0, type, arg, LOOKUP_NORMAL,
4130					"default argument", fn, parmnum);
4131      if (PROMOTE_PROTOTYPES
4132	  && INTEGRAL_TYPE_P (type)
4133	  && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
4134	arg = default_conversion (arg);
4135    }
4136
4137  return arg;
4138}
4139
4140/* Subroutine of the various build_*_call functions.  Overload resolution
4141   has chosen a winning candidate CAND; build up a CALL_EXPR accordingly.
4142   ARGS is a TREE_LIST of the unconverted arguments to the call.  FLAGS is a
4143   bitmask of various LOOKUP_* flags which apply to the call itself.  */
4144
4145static tree
4146build_over_call (cand, args, flags)
4147     struct z_candidate *cand;
4148     tree args;
4149     int flags;
4150{
4151  tree fn = cand->fn;
4152  tree convs = cand->convs;
4153  tree converted_args = NULL_TREE;
4154  tree parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
4155  tree conv, arg, val;
4156  int i = 0;
4157  int is_method = 0;
4158
4159  /* Give any warnings we noticed during overload resolution.  */
4160  if (cand->warnings)
4161    for (val = cand->warnings; val; val = TREE_CHAIN (val))
4162      joust (cand, WRAPPER_PTR (TREE_VALUE (val)), 1);
4163
4164  if (DECL_FUNCTION_MEMBER_P (fn))
4165    enforce_access (cand->basetype_path, fn);
4166
4167  if (args && TREE_CODE (args) != TREE_LIST)
4168    args = build_tree_list (NULL_TREE, args);
4169  arg = args;
4170
4171  /* The implicit parameters to a constructor are not considered by overload
4172     resolution, and must be of the proper type.  */
4173  if (DECL_CONSTRUCTOR_P (fn))
4174    {
4175      converted_args = tree_cons (NULL_TREE, TREE_VALUE (arg), converted_args);
4176      arg = TREE_CHAIN (arg);
4177      parm = TREE_CHAIN (parm);
4178      if (DECL_HAS_IN_CHARGE_PARM_P (fn))
4179	/* We should never try to call the abstract constructor.  */
4180	abort ();
4181      if (DECL_HAS_VTT_PARM_P (fn))
4182	{
4183	  converted_args = tree_cons
4184	    (NULL_TREE, TREE_VALUE (arg), converted_args);
4185	  arg = TREE_CHAIN (arg);
4186	  parm = TREE_CHAIN (parm);
4187	}
4188    }
4189  /* Bypass access control for 'this' parameter.  */
4190  else if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
4191    {
4192      tree parmtype = TREE_VALUE (parm);
4193      tree argtype = TREE_TYPE (TREE_VALUE (arg));
4194      tree t;
4195      if (ICS_BAD_FLAG (TREE_VEC_ELT (convs, i)))
4196	pedwarn ("passing `%T' as `this' argument of `%#D' discards qualifiers",
4197		    TREE_TYPE (argtype), fn);
4198
4199      /* [class.mfct.nonstatic]: If a nonstatic member function of a class
4200	 X is called for an object that is not of type X, or of a type
4201	 derived from X, the behavior is undefined.
4202
4203         So we can assume that anything passed as 'this' is non-null, and
4204	 optimize accordingly.  */
4205      my_friendly_assert (TREE_CODE (parmtype) == POINTER_TYPE, 19990811);
4206      t = lookup_base (TREE_TYPE (TREE_TYPE (TREE_VALUE (arg))),
4207		       TREE_TYPE (parmtype), ba_ignore, NULL);
4208      t = build_base_path (PLUS_EXPR, TREE_VALUE (arg), t, 1);
4209      converted_args = tree_cons (NULL_TREE, t, converted_args);
4210      parm = TREE_CHAIN (parm);
4211      arg = TREE_CHAIN (arg);
4212      ++i;
4213      is_method = 1;
4214    }
4215
4216  for (; arg && parm;
4217       parm = TREE_CHAIN (parm), arg = TREE_CHAIN (arg), ++i)
4218    {
4219      tree type = TREE_VALUE (parm);
4220
4221      conv = TREE_VEC_ELT (convs, i);
4222      val = convert_like_with_context
4223	(conv, TREE_VALUE (arg), fn, i - is_method);
4224
4225      if (PROMOTE_PROTOTYPES
4226	  && INTEGRAL_TYPE_P (type)
4227	  && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
4228	val = default_conversion (val);
4229      converted_args = tree_cons (NULL_TREE, val, converted_args);
4230    }
4231
4232  /* Default arguments */
4233  for (; parm && parm != void_list_node; parm = TREE_CHAIN (parm), i++)
4234    converted_args
4235      = tree_cons (NULL_TREE,
4236		   convert_default_arg (TREE_VALUE (parm),
4237					TREE_PURPOSE (parm),
4238					fn, i - is_method),
4239		   converted_args);
4240
4241  /* Ellipsis */
4242  for (; arg; arg = TREE_CHAIN (arg))
4243    converted_args
4244      = tree_cons (NULL_TREE,
4245		   convert_arg_to_ellipsis (TREE_VALUE (arg)),
4246		   converted_args);
4247
4248  converted_args = nreverse (converted_args);
4249
4250  if (warn_format)
4251    check_function_format (NULL, TYPE_ATTRIBUTES (TREE_TYPE (fn)),
4252			   converted_args);
4253
4254  /* Avoid actually calling copy constructors and copy assignment operators,
4255     if possible.  */
4256
4257  if (! flag_elide_constructors)
4258    /* Do things the hard way.  */;
4259  else if (TREE_VEC_LENGTH (convs) == 1
4260	   && DECL_COPY_CONSTRUCTOR_P (fn))
4261    {
4262      tree targ;
4263      arg = skip_artificial_parms_for (fn, converted_args);
4264      arg = TREE_VALUE (arg);
4265
4266      /* Pull out the real argument, disregarding const-correctness.  */
4267      targ = arg;
4268      while (TREE_CODE (targ) == NOP_EXPR
4269	     || TREE_CODE (targ) == NON_LVALUE_EXPR
4270	     || TREE_CODE (targ) == CONVERT_EXPR)
4271	targ = TREE_OPERAND (targ, 0);
4272      if (TREE_CODE (targ) == ADDR_EXPR)
4273	{
4274	  targ = TREE_OPERAND (targ, 0);
4275	  if (!same_type_ignoring_top_level_qualifiers_p
4276	      (TREE_TYPE (TREE_TYPE (arg)), TREE_TYPE (targ)))
4277	    targ = NULL_TREE;
4278	}
4279      else
4280	targ = NULL_TREE;
4281
4282      if (targ)
4283	arg = targ;
4284      else
4285	arg = build_indirect_ref (arg, 0);
4286
4287      /* [class.copy]: the copy constructor is implicitly defined even if
4288	 the implementation elided its use.  */
4289      if (TYPE_HAS_COMPLEX_INIT_REF (DECL_CONTEXT (fn)))
4290	mark_used (fn);
4291
4292      /* If we're creating a temp and we already have one, don't create a
4293         new one.  If we're not creating a temp but we get one, use
4294         INIT_EXPR to collapse the temp into our target.  Otherwise, if the
4295         ctor is trivial, do a bitwise copy with a simple TARGET_EXPR for a
4296         temp or an INIT_EXPR otherwise.  */
4297      if (integer_zerop (TREE_VALUE (args)))
4298	{
4299	  if (! real_lvalue_p (arg))
4300	    return arg;
4301	  else if (TYPE_HAS_TRIVIAL_INIT_REF (DECL_CONTEXT (fn)))
4302	    return build_target_expr_with_type (arg, DECL_CONTEXT (fn));
4303	}
4304      else if ((!real_lvalue_p (arg)
4305		|| TYPE_HAS_TRIVIAL_INIT_REF (DECL_CONTEXT (fn)))
4306	       /* Empty classes have padding which can be hidden
4307	          inside an (empty) base of the class. This must not
4308	          be touched as it might overlay things. When the
4309	          gcc core learns about empty classes, we can treat it
4310	          like other classes. */
4311	       && !(is_empty_class (DECL_CONTEXT (fn))
4312		    && TYPE_HAS_TRIVIAL_INIT_REF (DECL_CONTEXT (fn))))
4313	{
4314	  tree address;
4315	  tree to = stabilize_reference
4316	    (build_indirect_ref (TREE_VALUE (args), 0));
4317
4318	  val = build (INIT_EXPR, DECL_CONTEXT (fn), to, arg);
4319	  address = build_unary_op (ADDR_EXPR, val, 0);
4320	  /* Avoid a warning about this expression, if the address is
4321	     never used.  */
4322	  TREE_USED (address) = 1;
4323	  return address;
4324	}
4325    }
4326  else if (DECL_OVERLOADED_OPERATOR_P (fn) == NOP_EXPR
4327	   && copy_fn_p (fn)
4328	   && TYPE_HAS_TRIVIAL_ASSIGN_REF (DECL_CONTEXT (fn)))
4329    {
4330      tree to = stabilize_reference
4331	(build_indirect_ref (TREE_VALUE (converted_args), 0));
4332
4333      arg = build_indirect_ref (TREE_VALUE (TREE_CHAIN (converted_args)), 0);
4334      if (is_empty_class (TREE_TYPE (to)))
4335	{
4336	  TREE_USED (arg) = 1;
4337
4338	  val = build (COMPOUND_EXPR, DECL_CONTEXT (fn), arg, to);
4339	  /* Even though the assignment may not actually result in any
4340	     code being generated, we do not want to warn about the
4341	     assignment having no effect.  That would be confusing to
4342	     users who may be performing the assignment as part of a
4343	     generic algorithm, for example.
4344
4345	     Ideally, the notions of having side-effects and of being
4346	     useless would be orthogonal.  */
4347	  TREE_SIDE_EFFECTS (val) = 1;
4348	  TREE_NO_UNUSED_WARNING (val) = 1;
4349	}
4350      else
4351	val = build (MODIFY_EXPR, TREE_TYPE (to), to, arg);
4352      return val;
4353    }
4354
4355  mark_used (fn);
4356
4357  if (DECL_VINDEX (fn) && (flags & LOOKUP_NONVIRTUAL) == 0)
4358    {
4359      tree t, *p = &TREE_VALUE (converted_args);
4360      tree binfo = lookup_base (TREE_TYPE (TREE_TYPE (*p)),
4361				DECL_VIRTUAL_CONTEXT (fn),
4362				ba_any, NULL);
4363      my_friendly_assert (binfo && binfo != error_mark_node, 20010730);
4364
4365      *p = build_base_path (PLUS_EXPR, *p, binfo, 1);
4366      if (TREE_SIDE_EFFECTS (*p))
4367	*p = save_expr (*p);
4368      t = build_pointer_type (TREE_TYPE (fn));
4369      if (DECL_CONTEXT (fn) && TYPE_JAVA_INTERFACE (DECL_CONTEXT (fn)))
4370	fn = build_java_interface_fn_ref (fn, *p);
4371      else
4372	fn = build_vfn_ref (build_indirect_ref (*p, 0), DECL_VINDEX (fn));
4373      TREE_TYPE (fn) = t;
4374    }
4375  else if (DECL_INLINE (fn))
4376    fn = inline_conversion (fn);
4377  else
4378    fn = build_addr_func (fn);
4379
4380  /* Recognize certain built-in functions so we can make tree-codes
4381     other than CALL_EXPR.  We do this when it enables fold-const.c
4382     to do something useful.  */
4383
4384  if (TREE_CODE (fn) == ADDR_EXPR
4385      && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL
4386      && DECL_BUILT_IN (TREE_OPERAND (fn, 0)))
4387    {
4388      tree exp;
4389      exp = expand_tree_builtin (TREE_OPERAND (fn, 0), args, converted_args);
4390      if (exp)
4391	return exp;
4392    }
4393
4394  /* Some built-in function calls will be evaluated at
4395     compile-time in fold ().  */
4396  fn = fold (build_call (fn, converted_args));
4397  if (VOID_TYPE_P (TREE_TYPE (fn)))
4398    return fn;
4399  fn = require_complete_type (fn);
4400  if (fn == error_mark_node)
4401    return error_mark_node;
4402  if (IS_AGGR_TYPE (TREE_TYPE (fn)))
4403    fn = build_cplus_new (TREE_TYPE (fn), fn);
4404  return convert_from_reference (fn);
4405}
4406
4407static tree java_iface_lookup_fn;
4408
4409/* Make an expression which yields the address of the Java interface
4410   method FN.  This is achieved by generating a call to libjava's
4411   _Jv_LookupInterfaceMethodIdx().  */
4412
4413static tree
4414build_java_interface_fn_ref (fn, instance)
4415    tree fn, instance;
4416{
4417  tree lookup_args, lookup_fn, method, idx;
4418  tree klass_ref, iface, iface_ref;
4419  int i;
4420
4421  if (!java_iface_lookup_fn)
4422    {
4423      tree endlink = build_void_list_node ();
4424      tree t = tree_cons (NULL_TREE, ptr_type_node,
4425			  tree_cons (NULL_TREE, ptr_type_node,
4426				     tree_cons (NULL_TREE, java_int_type_node,
4427						endlink)));
4428      java_iface_lookup_fn
4429	= builtin_function ("_Jv_LookupInterfaceMethodIdx",
4430			    build_function_type (ptr_type_node, t),
4431			    0, NOT_BUILT_IN, NULL);
4432      ggc_add_tree_root (&java_iface_lookup_fn, 1);
4433    }
4434
4435  /* Look up the pointer to the runtime java.lang.Class object for `instance'.
4436     This is the first entry in the vtable. */
4437  klass_ref = build_vtbl_ref (build_indirect_ref (instance, 0),
4438			      integer_zero_node);
4439
4440  /* Get the java.lang.Class pointer for the interface being called. */
4441  iface = DECL_CONTEXT (fn);
4442  iface_ref = lookup_field (iface, get_identifier ("class$"), 0, 0);
4443  if (!iface_ref || TREE_CODE (iface_ref) != VAR_DECL
4444      || DECL_CONTEXT (iface_ref) != iface)
4445    {
4446      error ("could not find class$ field in java interface type `%T'",
4447		iface);
4448      return error_mark_node;
4449    }
4450  iface_ref = build1 (ADDR_EXPR, build_pointer_type (iface), iface_ref);
4451
4452  /* Determine the itable index of FN. */
4453  i = 1;
4454  for (method = TYPE_METHODS (iface); method; method = TREE_CHAIN (method))
4455    {
4456      if (!DECL_VIRTUAL_P (method))
4457        continue;
4458      if (fn == method)
4459        break;
4460      i++;
4461    }
4462  idx = build_int_2 (i, 0);
4463
4464  lookup_args = tree_cons (NULL_TREE, klass_ref,
4465			   tree_cons (NULL_TREE, iface_ref,
4466				      build_tree_list (NULL_TREE, idx)));
4467  lookup_fn = build1 (ADDR_EXPR,
4468		      build_pointer_type (TREE_TYPE (java_iface_lookup_fn)),
4469		      java_iface_lookup_fn);
4470  return build (CALL_EXPR, ptr_type_node, lookup_fn, lookup_args, NULL_TREE);
4471}
4472
4473/* Returns the value to use for the in-charge parameter when making a
4474   call to a function with the indicated NAME.  */
4475
4476tree
4477in_charge_arg_for_name (name)
4478     tree name;
4479{
4480  if (name == base_ctor_identifier
4481      || name == base_dtor_identifier)
4482    return integer_zero_node;
4483  else if (name == complete_ctor_identifier)
4484    return integer_one_node;
4485  else if (name == complete_dtor_identifier)
4486    return integer_two_node;
4487  else if (name == deleting_dtor_identifier)
4488    return integer_three_node;
4489
4490  /* This function should only be called with one of the names listed
4491     above.  */
4492  abort ();
4493  return NULL_TREE;
4494}
4495
4496static tree
4497build_new_method_call (instance, name, args, basetype_path, flags)
4498     tree instance, name, args, basetype_path;
4499     int flags;
4500{
4501  struct z_candidate *candidates = 0, *cand;
4502  tree explicit_targs = NULL_TREE;
4503  tree basetype, mem_args = NULL_TREE, fns, instance_ptr;
4504  tree pretty_name;
4505  tree user_args;
4506  tree templates = NULL_TREE;
4507  tree call;
4508  int template_only = 0;
4509
4510  if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
4511    {
4512      explicit_targs = TREE_OPERAND (name, 1);
4513      name = TREE_OPERAND (name, 0);
4514      if (DECL_P (name))
4515	name = DECL_NAME (name);
4516      else
4517	{
4518	  if (TREE_CODE (name) == COMPONENT_REF)
4519	    name = TREE_OPERAND (name, 1);
4520	  if (TREE_CODE (name) == OVERLOAD)
4521	    name = DECL_NAME (OVL_CURRENT (name));
4522	}
4523
4524      template_only = 1;
4525    }
4526
4527  user_args = args;
4528  args = resolve_args (args);
4529
4530  if (args == error_mark_node)
4531    return error_mark_node;
4532
4533  if (instance == NULL_TREE)
4534    basetype = BINFO_TYPE (basetype_path);
4535  else
4536    {
4537      if (TREE_CODE (instance) == OFFSET_REF)
4538	instance = resolve_offset_ref (instance);
4539      if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
4540	instance = convert_from_reference (instance);
4541      basetype = TYPE_MAIN_VARIANT (TREE_TYPE (instance));
4542
4543      /* XXX this should be handled before we get here.  */
4544      if (! IS_AGGR_TYPE (basetype))
4545	{
4546	  if ((flags & LOOKUP_COMPLAIN) && basetype != error_mark_node)
4547	    error ("request for member `%D' in `%E', which is of non-aggregate type `%T'",
4548		      name, instance, basetype);
4549
4550	  return error_mark_node;
4551	}
4552    }
4553
4554  if (basetype_path == NULL_TREE)
4555    basetype_path = TYPE_BINFO (basetype);
4556
4557  if (instance)
4558    {
4559      instance_ptr = build_this (instance);
4560
4561      if (! template_only)
4562	{
4563	  /* XXX this should be handled before we get here.  */
4564	  fns = build_field_call (basetype_path, instance_ptr, name, args);
4565	  if (fns)
4566	    return fns;
4567	}
4568    }
4569  else
4570    {
4571      instance_ptr = build_int_2 (0, 0);
4572      TREE_TYPE (instance_ptr) = build_pointer_type (basetype);
4573    }
4574
4575  /* Callers should explicitly indicate whether they want to construct
4576     the complete object or just the part without virtual bases.  */
4577  my_friendly_assert (name != ctor_identifier, 20000408);
4578  /* Similarly for destructors.  */
4579  my_friendly_assert (name != dtor_identifier, 20000408);
4580
4581  if (IDENTIFIER_CTOR_OR_DTOR_P (name))
4582    {
4583      int constructor_p;
4584
4585      constructor_p = (name == complete_ctor_identifier
4586		       || name == base_ctor_identifier);
4587      pretty_name = (constructor_p
4588		     ? constructor_name (basetype) : dtor_identifier);
4589
4590      /* If we're a call to a constructor or destructor for a
4591	 subobject that uses virtual base classes, then we need to
4592	 pass down a pointer to a VTT for the subobject.  */
4593      if ((name == base_ctor_identifier
4594	   || name == base_dtor_identifier)
4595	  && TYPE_USES_VIRTUAL_BASECLASSES (basetype))
4596	{
4597	  tree vtt;
4598	  tree sub_vtt;
4599	  tree basebinfo = basetype_path;
4600
4601	  /* If the current function is a complete object constructor
4602	     or destructor, then we fetch the VTT directly.
4603	     Otherwise, we look it up using the VTT we were given.  */
4604	  vtt = IDENTIFIER_GLOBAL_VALUE (get_vtt_name (current_class_type));
4605	  vtt = decay_conversion (vtt);
4606	  vtt = build (COND_EXPR, TREE_TYPE (vtt),
4607		       build (EQ_EXPR, boolean_type_node,
4608			      current_in_charge_parm, integer_zero_node),
4609		       current_vtt_parm,
4610		       vtt);
4611	  if (TREE_VIA_VIRTUAL (basebinfo))
4612	    basebinfo = binfo_for_vbase (basetype, current_class_type);
4613	  my_friendly_assert (BINFO_SUBVTT_INDEX (basebinfo), 20010110);
4614	  sub_vtt = build (PLUS_EXPR, TREE_TYPE (vtt), vtt,
4615			   BINFO_SUBVTT_INDEX (basebinfo));
4616
4617	  args = tree_cons (NULL_TREE, sub_vtt, args);
4618	}
4619    }
4620  else
4621    pretty_name = name;
4622
4623  fns = lookup_fnfields (basetype_path, name, 1);
4624
4625  if (fns == error_mark_node)
4626    return error_mark_node;
4627  if (fns)
4628    {
4629      tree base = BINFO_TYPE (TREE_PURPOSE (fns));
4630      tree fn = TREE_VALUE (fns);
4631      mem_args = tree_cons (NULL_TREE, instance_ptr, args);
4632      for (; fn; fn = OVL_NEXT (fn))
4633	{
4634	  tree t = OVL_CURRENT (fn);
4635	  tree this_arglist;
4636
4637	  /* We can end up here for copy-init of same or base class.  */
4638	  if ((flags & LOOKUP_ONLYCONVERTING)
4639	      && DECL_NONCONVERTING_P (t))
4640	    continue;
4641
4642	  if (DECL_NONSTATIC_MEMBER_FUNCTION_P (t))
4643	    this_arglist = mem_args;
4644	  else
4645	    this_arglist = args;
4646
4647	  if (TREE_CODE (t) == TEMPLATE_DECL)
4648	    {
4649	      /* A member template. */
4650	      templates = tree_cons (NULL_TREE, t, templates);
4651	      candidates =
4652		add_template_candidate (candidates, t, base, explicit_targs,
4653					this_arglist,
4654					TREE_TYPE (name), flags, DEDUCE_CALL);
4655	    }
4656	  else if (! template_only)
4657	    candidates = add_function_candidate (candidates, t, base,
4658						 this_arglist, flags);
4659
4660	  if (candidates)
4661	    candidates->basetype_path = basetype_path;
4662	}
4663    }
4664
4665  if (! any_viable (candidates))
4666    {
4667      /* XXX will LOOKUP_SPECULATIVELY be needed when this is done?  */
4668      if (flags & LOOKUP_SPECULATIVELY)
4669	return NULL_TREE;
4670      if (!COMPLETE_TYPE_P (basetype))
4671	incomplete_type_error (instance_ptr, basetype);
4672      else
4673	error ("no matching function for call to `%T::%D(%A)%#V'",
4674	       basetype, pretty_name, user_args,
4675	       TREE_TYPE (TREE_TYPE (instance_ptr)));
4676      print_z_candidates (candidates);
4677      return error_mark_node;
4678    }
4679  candidates = splice_viable (candidates);
4680  cand = tourney (candidates);
4681
4682  if (cand == 0)
4683    {
4684      error ("call of overloaded `%D(%A)' is ambiguous", pretty_name,
4685		user_args);
4686      print_z_candidates (candidates);
4687      return error_mark_node;
4688    }
4689
4690  if (DECL_PURE_VIRTUAL_P (cand->fn)
4691      && instance == current_class_ref
4692      && (DECL_CONSTRUCTOR_P (current_function_decl)
4693	  || DECL_DESTRUCTOR_P (current_function_decl))
4694      && ! (flags & LOOKUP_NONVIRTUAL)
4695      && value_member (cand->fn, CLASSTYPE_PURE_VIRTUALS (basetype)))
4696    error ((DECL_CONSTRUCTOR_P (current_function_decl) ?
4697	       "abstract virtual `%#D' called from constructor"
4698	       : "abstract virtual `%#D' called from destructor"),
4699	      cand->fn);
4700  if (TREE_CODE (TREE_TYPE (cand->fn)) == METHOD_TYPE
4701      && is_dummy_object (instance_ptr))
4702    {
4703      error ("cannot call member function `%D' without object", cand->fn);
4704      return error_mark_node;
4705    }
4706
4707  if (DECL_VINDEX (cand->fn) && ! (flags & LOOKUP_NONVIRTUAL)
4708      && resolves_to_fixed_type_p (instance, 0))
4709    flags |= LOOKUP_NONVIRTUAL;
4710
4711  if (TREE_CODE (TREE_TYPE (cand->fn)) == METHOD_TYPE)
4712    call = build_over_call (cand, mem_args, flags);
4713  else
4714    {
4715      call = build_over_call (cand, args, flags);
4716      /* Do evaluate the object parameter in a call to a static member
4717	 function.  */
4718      if (TREE_SIDE_EFFECTS (instance))
4719	call = build (COMPOUND_EXPR, TREE_TYPE (call), instance, call);
4720    }
4721
4722  return call;
4723}
4724
4725/* Returns non-zero iff standard conversion sequence ICS1 is a proper
4726   subsequence of ICS2.  */
4727
4728static int
4729is_subseq (ics1, ics2)
4730     tree ics1, ics2;
4731{
4732  /* We can assume that a conversion of the same code
4733     between the same types indicates a subsequence since we only get
4734     here if the types we are converting from are the same.  */
4735
4736  while (TREE_CODE (ics1) == RVALUE_CONV
4737	 || TREE_CODE (ics1) == LVALUE_CONV)
4738    ics1 = TREE_OPERAND (ics1, 0);
4739
4740  while (1)
4741    {
4742      while (TREE_CODE (ics2) == RVALUE_CONV
4743	  || TREE_CODE (ics2) == LVALUE_CONV)
4744	ics2 = TREE_OPERAND (ics2, 0);
4745
4746      if (TREE_CODE (ics2) == USER_CONV
4747	  || TREE_CODE (ics2) == AMBIG_CONV
4748	  || TREE_CODE (ics2) == IDENTITY_CONV)
4749	/* At this point, ICS1 cannot be a proper subsequence of
4750	   ICS2.  We can get a USER_CONV when we are comparing the
4751	   second standard conversion sequence of two user conversion
4752	   sequences.  */
4753	return 0;
4754
4755      ics2 = TREE_OPERAND (ics2, 0);
4756
4757      if (TREE_CODE (ics2) == TREE_CODE (ics1)
4758	  && same_type_p (TREE_TYPE (ics2), TREE_TYPE (ics1))
4759	  && same_type_p (TREE_TYPE (TREE_OPERAND (ics2, 0)),
4760			     TREE_TYPE (TREE_OPERAND (ics1, 0))))
4761	return 1;
4762    }
4763}
4764
4765/* Returns non-zero iff DERIVED is derived from BASE.  The inputs may
4766   be any _TYPE nodes.  */
4767
4768int
4769is_properly_derived_from (derived, base)
4770     tree derived;
4771     tree base;
4772{
4773  if (!IS_AGGR_TYPE_CODE (TREE_CODE (derived))
4774      || !IS_AGGR_TYPE_CODE (TREE_CODE (base)))
4775    return 0;
4776
4777  /* We only allow proper derivation here.  The DERIVED_FROM_P macro
4778     considers every class derived from itself.  */
4779  return (!same_type_ignoring_top_level_qualifiers_p (derived, base)
4780	  && DERIVED_FROM_P (base, derived));
4781}
4782
4783/* We build the ICS for an implicit object parameter as a pointer
4784   conversion sequence.  However, such a sequence should be compared
4785   as if it were a reference conversion sequence.  If ICS is the
4786   implicit conversion sequence for an implicit object parameter,
4787   modify it accordingly.  */
4788
4789static void
4790maybe_handle_implicit_object (ics)
4791     tree* ics;
4792{
4793  if (ICS_THIS_FLAG (*ics))
4794    {
4795      /* [over.match.funcs]
4796
4797	 For non-static member functions, the type of the
4798	 implicit object parameter is "reference to cv X"
4799	 where X is the class of which the function is a
4800	 member and cv is the cv-qualification on the member
4801	 function declaration.  */
4802      tree t = *ics;
4803      tree reference_type;
4804
4805      /* The `this' parameter is a pointer to a class type.  Make the
4806	 implict conversion talk about a reference to that same class
4807	 type.  */
4808      reference_type = TREE_TYPE (TREE_TYPE (*ics));
4809      reference_type = build_reference_type (reference_type);
4810
4811      if (TREE_CODE (t) == QUAL_CONV)
4812	t = TREE_OPERAND (t, 0);
4813      if (TREE_CODE (t) == PTR_CONV)
4814	t = TREE_OPERAND (t, 0);
4815      t = build1 (IDENTITY_CONV, TREE_TYPE (TREE_TYPE (t)), NULL_TREE);
4816      t = direct_reference_binding (reference_type, t);
4817      *ics = t;
4818    }
4819}
4820
4821/* If *ICS is a REF_BIND set *ICS to the remainder of the conversion,
4822   and return the type to which the reference refers.  Otherwise,
4823   leave *ICS unchanged and return NULL_TREE.  */
4824
4825static tree
4826maybe_handle_ref_bind (ics)
4827     tree* ics;
4828{
4829  if (TREE_CODE (*ics) == REF_BIND)
4830    {
4831      tree old_ics = *ics;
4832      tree type = TREE_TYPE (TREE_TYPE (old_ics));
4833      *ics = TREE_OPERAND (old_ics, 0);
4834      ICS_USER_FLAG (*ics) = ICS_USER_FLAG (old_ics);
4835      ICS_BAD_FLAG (*ics) = ICS_BAD_FLAG (old_ics);
4836      return type;
4837    }
4838
4839  return NULL_TREE;
4840}
4841
4842/* Compare two implicit conversion sequences according to the rules set out in
4843   [over.ics.rank].  Return values:
4844
4845      1: ics1 is better than ics2
4846     -1: ics2 is better than ics1
4847      0: ics1 and ics2 are indistinguishable */
4848
4849static int
4850compare_ics (ics1, ics2)
4851     tree ics1, ics2;
4852{
4853  tree from_type1;
4854  tree from_type2;
4855  tree to_type1;
4856  tree to_type2;
4857  tree deref_from_type1 = NULL_TREE;
4858  tree deref_from_type2 = NULL_TREE;
4859  tree deref_to_type1 = NULL_TREE;
4860  tree deref_to_type2 = NULL_TREE;
4861  int rank1, rank2;
4862
4863  /* REF_BINDING is non-zero if the result of the conversion sequence
4864     is a reference type.   In that case TARGET_TYPE is the
4865     type referred to by the reference.  */
4866  tree target_type1;
4867  tree target_type2;
4868
4869  /* Handle implicit object parameters.  */
4870  maybe_handle_implicit_object (&ics1);
4871  maybe_handle_implicit_object (&ics2);
4872
4873  /* Handle reference parameters.  */
4874  target_type1 = maybe_handle_ref_bind (&ics1);
4875  target_type2 = maybe_handle_ref_bind (&ics2);
4876
4877  /* [over.ics.rank]
4878
4879     When  comparing  the  basic forms of implicit conversion sequences (as
4880     defined in _over.best.ics_)
4881
4882     --a standard conversion sequence (_over.ics.scs_) is a better
4883       conversion sequence than a user-defined conversion sequence
4884       or an ellipsis conversion sequence, and
4885
4886     --a user-defined conversion sequence (_over.ics.user_) is a
4887       better conversion sequence than an ellipsis conversion sequence
4888       (_over.ics.ellipsis_).  */
4889  rank1 = ICS_RANK (ics1);
4890  rank2 = ICS_RANK (ics2);
4891
4892  if (rank1 > rank2)
4893    return -1;
4894  else if (rank1 < rank2)
4895    return 1;
4896
4897  if (rank1 == BAD_RANK)
4898    {
4899      /* XXX Isn't this an extension? */
4900      /* Both ICS are bad.  We try to make a decision based on what
4901	 would have happenned if they'd been good.  */
4902      if (ICS_USER_FLAG (ics1) > ICS_USER_FLAG (ics2)
4903	  || ICS_STD_RANK (ics1) > ICS_STD_RANK (ics2))
4904	return -1;
4905      else if (ICS_USER_FLAG (ics1) < ICS_USER_FLAG (ics2)
4906	       || ICS_STD_RANK (ics1) < ICS_STD_RANK (ics2))
4907	return 1;
4908
4909      /* We couldn't make up our minds; try to figure it out below.  */
4910    }
4911
4912  if (ICS_ELLIPSIS_FLAG (ics1))
4913    /* Both conversions are ellipsis conversions.  */
4914    return 0;
4915
4916  /* User-defined  conversion sequence U1 is a better conversion sequence
4917     than another user-defined conversion sequence U2 if they contain the
4918     same user-defined conversion operator or constructor and if the sec-
4919     ond standard conversion sequence of U1 is  better  than  the  second
4920     standard conversion sequence of U2.  */
4921
4922  if (ICS_USER_FLAG (ics1))
4923    {
4924      tree t1, t2;
4925
4926      for (t1 = ics1; TREE_CODE (t1) != USER_CONV; t1 = TREE_OPERAND (t1, 0))
4927	if (TREE_CODE (t1) == AMBIG_CONV)
4928	  return 0;
4929      for (t2 = ics2; TREE_CODE (t2) != USER_CONV; t2 = TREE_OPERAND (t2, 0))
4930	if (TREE_CODE (t2) == AMBIG_CONV)
4931	  return 0;
4932
4933      if (USER_CONV_FN (t1) != USER_CONV_FN (t2))
4934	return 0;
4935
4936      /* We can just fall through here, after setting up
4937	 FROM_TYPE1 and FROM_TYPE2.  */
4938      from_type1 = TREE_TYPE (t1);
4939      from_type2 = TREE_TYPE (t2);
4940    }
4941  else
4942    {
4943      /* We're dealing with two standard conversion sequences.
4944
4945	 [over.ics.rank]
4946
4947	 Standard conversion sequence S1 is a better conversion
4948	 sequence than standard conversion sequence S2 if
4949
4950	 --S1 is a proper subsequence of S2 (comparing the conversion
4951	   sequences in the canonical form defined by _over.ics.scs_,
4952	   excluding any Lvalue Transformation; the identity
4953	   conversion sequence is considered to be a subsequence of
4954	   any non-identity conversion sequence */
4955
4956      from_type1 = ics1;
4957      while (TREE_CODE (from_type1) != IDENTITY_CONV)
4958	from_type1 = TREE_OPERAND (from_type1, 0);
4959      from_type1 = TREE_TYPE (from_type1);
4960
4961      from_type2 = ics2;
4962      while (TREE_CODE (from_type2) != IDENTITY_CONV)
4963	from_type2 = TREE_OPERAND (from_type2, 0);
4964      from_type2 = TREE_TYPE (from_type2);
4965    }
4966
4967  if (same_type_p (from_type1, from_type2))
4968    {
4969      if (is_subseq (ics1, ics2))
4970	return 1;
4971      if (is_subseq (ics2, ics1))
4972	return -1;
4973    }
4974  /* Otherwise, one sequence cannot be a subsequence of the other; they
4975     don't start with the same type.  This can happen when comparing the
4976     second standard conversion sequence in two user-defined conversion
4977     sequences.  */
4978
4979  /* [over.ics.rank]
4980
4981     Or, if not that,
4982
4983     --the rank of S1 is better than the rank of S2 (by the rules
4984       defined below):
4985
4986    Standard conversion sequences are ordered by their ranks: an Exact
4987    Match is a better conversion than a Promotion, which is a better
4988    conversion than a Conversion.
4989
4990    Two conversion sequences with the same rank are indistinguishable
4991    unless one of the following rules applies:
4992
4993    --A conversion that is not a conversion of a pointer, or pointer
4994      to member, to bool is better than another conversion that is such
4995      a conversion.
4996
4997    The ICS_STD_RANK automatically handles the pointer-to-bool rule,
4998    so that we do not have to check it explicitly.  */
4999  if (ICS_STD_RANK (ics1) < ICS_STD_RANK (ics2))
5000    return 1;
5001  else if (ICS_STD_RANK (ics2) < ICS_STD_RANK (ics1))
5002    return -1;
5003
5004  to_type1 = TREE_TYPE (ics1);
5005  to_type2 = TREE_TYPE (ics2);
5006
5007  if (TYPE_PTR_P (from_type1)
5008      && TYPE_PTR_P (from_type2)
5009      && TYPE_PTR_P (to_type1)
5010      && TYPE_PTR_P (to_type2))
5011    {
5012      deref_from_type1 = TREE_TYPE (from_type1);
5013      deref_from_type2 = TREE_TYPE (from_type2);
5014      deref_to_type1 = TREE_TYPE (to_type1);
5015      deref_to_type2 = TREE_TYPE (to_type2);
5016    }
5017  /* The rules for pointers to members A::* are just like the rules
5018     for pointers A*, except opposite: if B is derived from A then
5019     A::* converts to B::*, not vice versa.  For that reason, we
5020     switch the from_ and to_ variables here.  */
5021  else if (TYPE_PTRMEM_P (from_type1)
5022	   && TYPE_PTRMEM_P (from_type2)
5023	   && TYPE_PTRMEM_P (to_type1)
5024	   && TYPE_PTRMEM_P (to_type2))
5025    {
5026      deref_to_type1 = TYPE_OFFSET_BASETYPE (TREE_TYPE (from_type1));
5027      deref_to_type2 = TYPE_OFFSET_BASETYPE (TREE_TYPE (from_type2));
5028      deref_from_type1 = TYPE_OFFSET_BASETYPE (TREE_TYPE (to_type1));
5029      deref_from_type2 = TYPE_OFFSET_BASETYPE (TREE_TYPE (to_type2));
5030    }
5031  else if (TYPE_PTRMEMFUNC_P (from_type1)
5032	   && TYPE_PTRMEMFUNC_P (from_type2)
5033	   && TYPE_PTRMEMFUNC_P (to_type1)
5034	   && TYPE_PTRMEMFUNC_P (to_type2))
5035    {
5036      deref_to_type1 = TYPE_PTRMEMFUNC_OBJECT_TYPE (from_type1);
5037      deref_to_type2 = TYPE_PTRMEMFUNC_OBJECT_TYPE (from_type2);
5038      deref_from_type1 = TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type1);
5039      deref_from_type2 = TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type2);
5040    }
5041
5042  if (deref_from_type1 != NULL_TREE
5043      && IS_AGGR_TYPE_CODE (TREE_CODE (deref_from_type1))
5044      && IS_AGGR_TYPE_CODE (TREE_CODE (deref_from_type2)))
5045    {
5046      /* This was one of the pointer or pointer-like conversions.
5047
5048	 [over.ics.rank]
5049
5050	 --If class B is derived directly or indirectly from class A,
5051	   conversion of B* to A* is better than conversion of B* to
5052	   void*, and conversion of A* to void* is better than
5053	   conversion of B* to void*.  */
5054      if (TREE_CODE (deref_to_type1) == VOID_TYPE
5055	  && TREE_CODE (deref_to_type2) == VOID_TYPE)
5056	{
5057	  if (is_properly_derived_from (deref_from_type1,
5058					deref_from_type2))
5059	    return -1;
5060	  else if (is_properly_derived_from (deref_from_type2,
5061					     deref_from_type1))
5062	    return 1;
5063	}
5064      else if (TREE_CODE (deref_to_type1) == VOID_TYPE
5065	       || TREE_CODE (deref_to_type2) == VOID_TYPE)
5066	{
5067	  if (same_type_p (deref_from_type1, deref_from_type2))
5068	    {
5069	      if (TREE_CODE (deref_to_type2) == VOID_TYPE)
5070		{
5071		  if (is_properly_derived_from (deref_from_type1,
5072						deref_to_type1))
5073		    return 1;
5074		}
5075	      /* We know that DEREF_TO_TYPE1 is `void' here.  */
5076	      else if (is_properly_derived_from (deref_from_type1,
5077						 deref_to_type2))
5078		return -1;
5079	    }
5080	}
5081      else if (IS_AGGR_TYPE_CODE (TREE_CODE (deref_to_type1))
5082	       && IS_AGGR_TYPE_CODE (TREE_CODE (deref_to_type2)))
5083	{
5084	  /* [over.ics.rank]
5085
5086	     --If class B is derived directly or indirectly from class A
5087	       and class C is derived directly or indirectly from B,
5088
5089	     --conversion of C* to B* is better than conversion of C* to
5090	       A*,
5091
5092	     --conversion of B* to A* is better than conversion of C* to
5093	       A*  */
5094	  if (same_type_p (deref_from_type1, deref_from_type2))
5095	    {
5096	      if (is_properly_derived_from (deref_to_type1,
5097					    deref_to_type2))
5098		return 1;
5099	      else if (is_properly_derived_from (deref_to_type2,
5100						 deref_to_type1))
5101		return -1;
5102	    }
5103	  else if (same_type_p (deref_to_type1, deref_to_type2))
5104	    {
5105	      if (is_properly_derived_from (deref_from_type2,
5106					    deref_from_type1))
5107		return 1;
5108	      else if (is_properly_derived_from (deref_from_type1,
5109						 deref_from_type2))
5110		return -1;
5111	    }
5112	}
5113    }
5114  else if (CLASS_TYPE_P (non_reference (from_type1))
5115	   && same_type_p (from_type1, from_type2))
5116    {
5117      tree from = non_reference (from_type1);
5118
5119      /* [over.ics.rank]
5120
5121	 --binding of an expression of type C to a reference of type
5122	   B& is better than binding an expression of type C to a
5123	   reference of type A&
5124
5125	 --conversion of C to B is better than conversion of C to A,  */
5126      if (is_properly_derived_from (from, to_type1)
5127	  && is_properly_derived_from (from, to_type2))
5128	{
5129	  if (is_properly_derived_from (to_type1, to_type2))
5130	    return 1;
5131	  else if (is_properly_derived_from (to_type2, to_type1))
5132	    return -1;
5133	}
5134    }
5135  else if (CLASS_TYPE_P (non_reference (to_type1))
5136	   && same_type_p (to_type1, to_type2))
5137    {
5138      tree to = non_reference (to_type1);
5139
5140      /* [over.ics.rank]
5141
5142	 --binding of an expression of type B to a reference of type
5143	   A& is better than binding an expression of type C to a
5144	   reference of type A&,
5145
5146	 --onversion of B to A is better than conversion of C to A  */
5147      if (is_properly_derived_from (from_type1, to)
5148	  && is_properly_derived_from (from_type2, to))
5149	{
5150	  if (is_properly_derived_from (from_type2, from_type1))
5151	    return 1;
5152	  else if (is_properly_derived_from (from_type1, from_type2))
5153	    return -1;
5154	}
5155    }
5156
5157  /* [over.ics.rank]
5158
5159     --S1 and S2 differ only in their qualification conversion and  yield
5160       similar  types  T1 and T2 (_conv.qual_), respectively, and the cv-
5161       qualification signature of type T1 is a proper subset of  the  cv-
5162       qualification signature of type T2  */
5163  if (TREE_CODE (ics1) == QUAL_CONV
5164      && TREE_CODE (ics2) == QUAL_CONV
5165      && same_type_p (from_type1, from_type2))
5166    return comp_cv_qual_signature (to_type1, to_type2);
5167
5168  /* [over.ics.rank]
5169
5170     --S1 and S2 are reference bindings (_dcl.init.ref_), and the
5171     types to which the references refer are the same type except for
5172     top-level cv-qualifiers, and the type to which the reference
5173     initialized by S2 refers is more cv-qualified than the type to
5174     which the reference initialized by S1 refers */
5175
5176  if (target_type1 && target_type2
5177      && same_type_ignoring_top_level_qualifiers_p (to_type1, to_type2))
5178    return comp_cv_qualification (target_type2, target_type1);
5179
5180  /* Neither conversion sequence is better than the other.  */
5181  return 0;
5182}
5183
5184/* The source type for this standard conversion sequence.  */
5185
5186static tree
5187source_type (t)
5188     tree t;
5189{
5190  for (;; t = TREE_OPERAND (t, 0))
5191    {
5192      if (TREE_CODE (t) == USER_CONV
5193	  || TREE_CODE (t) == AMBIG_CONV
5194	  || TREE_CODE (t) == IDENTITY_CONV)
5195	return TREE_TYPE (t);
5196    }
5197  abort ();
5198}
5199
5200/* Note a warning about preferring WINNER to LOSER.  We do this by storing
5201   a pointer to LOSER and re-running joust to produce the warning if WINNER
5202   is actually used.  */
5203
5204static void
5205add_warning (winner, loser)
5206     struct z_candidate *winner, *loser;
5207{
5208  winner->warnings = tree_cons (NULL_TREE,
5209				build_ptr_wrapper (loser),
5210				winner->warnings);
5211}
5212
5213/* Returns true iff functions are equivalent. Equivalent functions are
5214   not '==' only if one is a function-local extern function or if
5215   both are extern "C".  */
5216
5217static inline int
5218equal_functions (fn1, fn2)
5219     tree fn1;
5220     tree fn2;
5221{
5222  if (DECL_LOCAL_FUNCTION_P (fn1) || DECL_LOCAL_FUNCTION_P (fn2)
5223      || DECL_EXTERN_C_FUNCTION_P (fn1))
5224    return decls_match (fn1, fn2);
5225  return fn1 == fn2;
5226}
5227
5228/* Compare two candidates for overloading as described in
5229   [over.match.best].  Return values:
5230
5231      1: cand1 is better than cand2
5232     -1: cand2 is better than cand1
5233      0: cand1 and cand2 are indistinguishable */
5234
5235static int
5236joust (cand1, cand2, warn)
5237     struct z_candidate *cand1, *cand2;
5238     int warn;
5239{
5240  int winner = 0;
5241  int i, off1 = 0, off2 = 0, len;
5242
5243  /* Candidates that involve bad conversions are always worse than those
5244     that don't.  */
5245  if (cand1->viable > cand2->viable)
5246    return 1;
5247  if (cand1->viable < cand2->viable)
5248    return -1;
5249
5250  /* If we have two pseudo-candidates for conversions to the same type,
5251     or two candidates for the same function, arbitrarily pick one.  */
5252  if (cand1->fn == cand2->fn
5253      && (TYPE_P (cand1->fn) || DECL_P (cand1->fn)))
5254    return 1;
5255
5256  /* a viable function F1
5257     is defined to be a better function than another viable function F2  if
5258     for  all arguments i, ICSi(F1) is not a worse conversion sequence than
5259     ICSi(F2), and then */
5260
5261  /* for some argument j, ICSj(F1) is a better conversion  sequence  than
5262     ICSj(F2) */
5263
5264  /* For comparing static and non-static member functions, we ignore
5265     the implicit object parameter of the non-static function.  The
5266     standard says to pretend that the static function has an object
5267     parm, but that won't work with operator overloading.  */
5268  len = TREE_VEC_LENGTH (cand1->convs);
5269  if (len != TREE_VEC_LENGTH (cand2->convs))
5270    {
5271      if (DECL_STATIC_FUNCTION_P (cand1->fn)
5272	  && ! DECL_STATIC_FUNCTION_P (cand2->fn))
5273	off2 = 1;
5274      else if (! DECL_STATIC_FUNCTION_P (cand1->fn)
5275	       && DECL_STATIC_FUNCTION_P (cand2->fn))
5276	{
5277	  off1 = 1;
5278	  --len;
5279	}
5280      else
5281	abort ();
5282    }
5283
5284  for (i = 0; i < len; ++i)
5285    {
5286      tree t1 = TREE_VEC_ELT (cand1->convs, i+off1);
5287      tree t2 = TREE_VEC_ELT (cand2->convs, i+off2);
5288      int comp = compare_ics (t1, t2);
5289
5290      if (comp != 0)
5291	{
5292	  if (warn_sign_promo
5293	      && ICS_RANK (t1) + ICS_RANK (t2) == STD_RANK + PROMO_RANK
5294	      && TREE_CODE (t1) == STD_CONV
5295	      && TREE_CODE (t2) == STD_CONV
5296	      && TREE_CODE (TREE_TYPE (t1)) == INTEGER_TYPE
5297	      && TREE_CODE (TREE_TYPE (t2)) == INTEGER_TYPE
5298	      && (TYPE_PRECISION (TREE_TYPE (t1))
5299		  == TYPE_PRECISION (TREE_TYPE (t2)))
5300	      && (TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (t1, 0)))
5301		  || (TREE_CODE (TREE_TYPE (TREE_OPERAND (t1, 0)))
5302		      == ENUMERAL_TYPE)))
5303	    {
5304	      tree type = TREE_TYPE (TREE_OPERAND (t1, 0));
5305	      tree type1, type2;
5306	      struct z_candidate *w, *l;
5307	      if (comp > 0)
5308		type1 = TREE_TYPE (t1), type2 = TREE_TYPE (t2),
5309		  w = cand1, l = cand2;
5310	      else
5311		type1 = TREE_TYPE (t2), type2 = TREE_TYPE (t1),
5312		  w = cand2, l = cand1;
5313
5314	      if (warn)
5315		{
5316		  warning ("passing `%T' chooses `%T' over `%T'",
5317			      type, type1, type2);
5318		  warning ("  in call to `%D'", w->fn);
5319		}
5320	      else
5321		add_warning (w, l);
5322	    }
5323
5324	  if (winner && comp != winner)
5325	    {
5326	      winner = 0;
5327	      goto tweak;
5328	    }
5329	  winner = comp;
5330	}
5331    }
5332
5333  /* warn about confusing overload resolution for user-defined conversions,
5334     either between a constructor and a conversion op, or between two
5335     conversion ops.  */
5336  if (winner && cand1->second_conv
5337      && ((DECL_CONSTRUCTOR_P (cand1->fn)
5338	   != DECL_CONSTRUCTOR_P (cand2->fn))
5339	  /* Don't warn if the two conv ops convert to the same type...  */
5340	  || (! DECL_CONSTRUCTOR_P (cand1->fn)
5341	      && ! same_type_p (TREE_TYPE (TREE_TYPE (cand1->fn)),
5342				TREE_TYPE (TREE_TYPE (cand2->fn))))))
5343    {
5344      int comp = compare_ics (cand1->second_conv, cand2->second_conv);
5345      if (comp != winner)
5346	{
5347	  struct z_candidate *w, *l;
5348	  tree convn;
5349	  if (winner == 1)
5350	    w = cand1, l = cand2;
5351	  else
5352	    w = cand2, l = cand1;
5353	  if (DECL_CONTEXT (cand1->fn) == DECL_CONTEXT (cand2->fn)
5354	      && ! DECL_CONSTRUCTOR_P (cand1->fn)
5355	      && ! DECL_CONSTRUCTOR_P (cand2->fn)
5356	      && (convn = standard_conversion
5357		  (TREE_TYPE (TREE_TYPE (l->fn)),
5358		   TREE_TYPE (TREE_TYPE (w->fn)), NULL_TREE))
5359	      && TREE_CODE (convn) == QUAL_CONV)
5360	    /* Don't complain about `operator char *()' beating
5361	       `operator const char *() const'.  */;
5362	  else if (warn)
5363	    {
5364	      tree source = source_type (TREE_VEC_ELT (w->convs, 0));
5365	      if (! DECL_CONSTRUCTOR_P (w->fn))
5366		source = TREE_TYPE (source);
5367	      warning ("choosing `%D' over `%D'", w->fn, l->fn);
5368	      warning ("  for conversion from `%T' to `%T'",
5369			  source, TREE_TYPE (w->second_conv));
5370	      warning ("  because conversion sequence for the argument is better");
5371	    }
5372	  else
5373	    add_warning (w, l);
5374	}
5375    }
5376
5377  if (winner)
5378    return winner;
5379
5380  /* or, if not that,
5381     F1 is a non-template function and F2 is a template function
5382     specialization.  */
5383
5384  if (! cand1->template && cand2->template)
5385    return 1;
5386  else if (cand1->template && ! cand2->template)
5387    return -1;
5388
5389  /* or, if not that,
5390     F1 and F2 are template functions and the function template for F1 is
5391     more specialized than the template for F2 according to the partial
5392     ordering rules.  */
5393
5394  if (cand1->template && cand2->template)
5395    {
5396      winner = more_specialized
5397        (TI_TEMPLATE (cand1->template), TI_TEMPLATE (cand2->template),
5398         DEDUCE_ORDER,
5399         /* Tell the deduction code how many real function arguments
5400	    we saw, not counting the implicit 'this' argument.  But,
5401	    add_function_candidate() suppresses the "this" argument
5402	    for constructors.
5403
5404	    [temp.func.order]: The presence of unused ellipsis and default
5405	    arguments has no effect on the partial ordering of function
5406	    templates.  */
5407         TREE_VEC_LENGTH (cand1->convs)
5408	 - (DECL_NONSTATIC_MEMBER_FUNCTION_P (cand1->fn)
5409	    - DECL_CONSTRUCTOR_P (cand1->fn)));
5410      /* HERE */
5411      if (winner)
5412        return winner;
5413    }
5414
5415  /* or, if not that,
5416     the  context  is  an  initialization by user-defined conversion (see
5417     _dcl.init_  and  _over.match.user_)  and  the  standard   conversion
5418     sequence  from  the return type of F1 to the destination type (i.e.,
5419     the type of the entity being initialized)  is  a  better  conversion
5420     sequence  than the standard conversion sequence from the return type
5421     of F2 to the destination type.  */
5422
5423  if (cand1->second_conv)
5424    {
5425      winner = compare_ics (cand1->second_conv, cand2->second_conv);
5426      if (winner)
5427        return winner;
5428    }
5429
5430  /* Check whether we can discard a builtin candidate, either because we
5431     have two identical ones or matching builtin and non-builtin candidates.
5432
5433     (Pedantically in the latter case the builtin which matched the user
5434     function should not be added to the overload set, but we spot it here.
5435
5436     [over.match.oper]
5437     ... the builtin candidates include ...
5438     - do not have the same parameter type list as any non-template
5439       non-member candidate.  */
5440
5441  if (TREE_CODE (cand1->fn) == IDENTIFIER_NODE
5442      || TREE_CODE (cand2->fn) == IDENTIFIER_NODE)
5443    {
5444      for (i = 0; i < len; ++i)
5445	if (!same_type_p (TREE_TYPE (TREE_VEC_ELT (cand1->convs, i)),
5446			  TREE_TYPE (TREE_VEC_ELT (cand2->convs, i))))
5447	  break;
5448      if (i == TREE_VEC_LENGTH (cand1->convs))
5449	{
5450	  if (cand1->fn == cand2->fn)
5451	    /* Two built-in candidates; arbitrarily pick one.  */
5452	    return 1;
5453	  else if (TREE_CODE (cand1->fn) == IDENTIFIER_NODE)
5454	    /* cand1 is built-in; prefer cand2.  */
5455	    return -1;
5456	  else
5457	    /* cand2 is built-in; prefer cand1.  */
5458	    return 1;
5459	}
5460    }
5461
5462  /* If the two functions are the same (this can happen with declarations
5463     in multiple scopes and arg-dependent lookup), arbitrarily choose one.  */
5464  if (DECL_P (cand1->fn) && DECL_P (cand2->fn)
5465      && equal_functions (cand1->fn, cand2->fn))
5466    return 1;
5467
5468tweak:
5469
5470  /* Extension: If the worst conversion for one candidate is worse than the
5471     worst conversion for the other, take the first.  */
5472  if (!pedantic)
5473    {
5474      int rank1 = IDENTITY_RANK, rank2 = IDENTITY_RANK;
5475      struct z_candidate *w = 0, *l = 0;
5476
5477      for (i = 0; i < len; ++i)
5478	{
5479	  if (ICS_RANK (TREE_VEC_ELT (cand1->convs, i+off1)) > rank1)
5480	    rank1 = ICS_RANK (TREE_VEC_ELT (cand1->convs, i+off1));
5481	  if (ICS_RANK (TREE_VEC_ELT (cand2->convs, i+off2)) > rank2)
5482	    rank2 = ICS_RANK (TREE_VEC_ELT (cand2->convs, i+off2));
5483	}
5484      if (rank1 < rank2)
5485	winner = 1, w = cand1, l = cand2;
5486      if (rank1 > rank2)
5487	winner = -1, w = cand2, l = cand1;
5488      if (winner)
5489        {
5490	  if (warn)
5491	    {
5492	      pedwarn ("choosing `%D' over `%D'", w->fn, l->fn);
5493	      pedwarn (
5494"  because worst conversion for the former is better than worst conversion for the latter");
5495	    }
5496	  else
5497	    add_warning (w, l);
5498          return winner;
5499        }
5500    }
5501
5502  my_friendly_assert (!winner, 20010121);
5503  return 0;
5504}
5505
5506/* Given a list of candidates for overloading, find the best one, if any.
5507   This algorithm has a worst case of O(2n) (winner is last), and a best
5508   case of O(n/2) (totally ambiguous); much better than a sorting
5509   algorithm.  */
5510
5511static struct z_candidate *
5512tourney (candidates)
5513     struct z_candidate *candidates;
5514{
5515  struct z_candidate *champ = candidates, *challenger;
5516  int fate;
5517  int champ_compared_to_predecessor = 0;
5518
5519  /* Walk through the list once, comparing each current champ to the next
5520     candidate, knocking out a candidate or two with each comparison.  */
5521
5522  for (challenger = champ->next; challenger; )
5523    {
5524      fate = joust (champ, challenger, 0);
5525      if (fate == 1)
5526	challenger = challenger->next;
5527      else
5528	{
5529	  if (fate == 0)
5530	    {
5531	      champ = challenger->next;
5532	      if (champ == 0)
5533		return 0;
5534	      champ_compared_to_predecessor = 0;
5535	    }
5536	  else
5537	    {
5538	      champ = challenger;
5539	      champ_compared_to_predecessor = 1;
5540	    }
5541
5542	  challenger = champ->next;
5543	}
5544    }
5545
5546  /* Make sure the champ is better than all the candidates it hasn't yet
5547     been compared to.  */
5548
5549  for (challenger = candidates;
5550       challenger != champ
5551	 && !(champ_compared_to_predecessor && challenger->next == champ);
5552       challenger = challenger->next)
5553    {
5554      fate = joust (champ, challenger, 0);
5555      if (fate != 1)
5556	return 0;
5557    }
5558
5559  return champ;
5560}
5561
5562/* Returns non-zero if things of type FROM can be converted to TO.  */
5563
5564int
5565can_convert (to, from)
5566     tree to, from;
5567{
5568  return can_convert_arg (to, from, NULL_TREE);
5569}
5570
5571/* Returns non-zero if ARG (of type FROM) can be converted to TO.  */
5572
5573int
5574can_convert_arg (to, from, arg)
5575     tree to, from, arg;
5576{
5577  tree t = implicit_conversion (to, from, arg, LOOKUP_NORMAL);
5578  return (t && ! ICS_BAD_FLAG (t));
5579}
5580
5581/* Like can_convert_arg, but allows dubious conversions as well.  */
5582
5583int
5584can_convert_arg_bad (to, from, arg)
5585     tree to, from, arg;
5586{
5587  tree t = implicit_conversion (to, from, arg, LOOKUP_NORMAL);
5588  return !!t;
5589}
5590
5591/* Convert EXPR to TYPE.  Return the converted expression.
5592
5593   Note that we allow bad conversions here because by the time we get to
5594   this point we are committed to doing the conversion.  If we end up
5595   doing a bad conversion, convert_like will complain.  */
5596
5597tree
5598perform_implicit_conversion (type, expr)
5599     tree type;
5600     tree expr;
5601{
5602  tree conv;
5603
5604  if (expr == error_mark_node)
5605    return error_mark_node;
5606  conv = implicit_conversion (type, TREE_TYPE (expr), expr,
5607			      LOOKUP_NORMAL);
5608  if (!conv)
5609    {
5610      error ("could not convert `%E' to `%T'", expr, type);
5611      return error_mark_node;
5612    }
5613
5614  return convert_like (conv, expr);
5615}
5616
5617/* Convert EXPR to the indicated reference TYPE, in a way suitable for
5618   initializing a variable of that TYPE.  Return the converted
5619   expression.  */
5620
5621tree
5622initialize_reference (type, expr)
5623     tree type;
5624     tree expr;
5625{
5626  tree conv;
5627
5628  conv = reference_binding (type, TREE_TYPE (expr), expr, LOOKUP_NORMAL);
5629  if (!conv || ICS_BAD_FLAG (conv))
5630    {
5631      error ("could not convert `%E' to `%T'", expr, type);
5632      return error_mark_node;
5633    }
5634
5635  return convert_like (conv, expr);
5636}
5637