call.c revision 96263
1/* Functions related to invoking methods and overloaded functions.
2   Copyright (C) 1987, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3   1999, 2000, 2001, 2002 Free Software Foundation, Inc.
4   Contributed by Michael Tiemann (tiemann@cygnus.com) and
5   modified by Brendan Kehoe (brendan@cygnus.com).
6
7This file is part of GNU CC.
8
9GNU CC is free software; you can redistribute it and/or modify
10it under the terms of the GNU General Public License as published by
11the Free Software Foundation; either version 2, or (at your option)
12any later version.
13
14GNU CC is distributed in the hope that it will be useful,
15but WITHOUT ANY WARRANTY; without even the implied warranty of
16MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17GNU General Public License for more details.
18
19You should have received a copy of the GNU General Public License
20along with GNU CC; see the file COPYING.  If not, write to
21the Free Software Foundation, 59 Temple Place - Suite 330,
22Boston, MA 02111-1307, USA.  */
23
24
25/* High-level class interface.  */
26
27#include "config.h"
28#include "system.h"
29#include "tree.h"
30#include "cp-tree.h"
31#include "output.h"
32#include "flags.h"
33#include "rtl.h"
34#include "toplev.h"
35#include "expr.h"
36#include "ggc.h"
37#include "diagnostic.h"
38
39extern int inhibit_warnings;
40
41static tree build_new_method_call PARAMS ((tree, tree, tree, tree, int));
42
43static tree build_field_call PARAMS ((tree, tree, tree, tree));
44static struct z_candidate * tourney PARAMS ((struct z_candidate *));
45static int equal_functions PARAMS ((tree, tree));
46static int joust PARAMS ((struct z_candidate *, struct z_candidate *, int));
47static int compare_ics PARAMS ((tree, tree));
48static tree build_over_call PARAMS ((struct z_candidate *, tree, int));
49static tree build_java_interface_fn_ref PARAMS ((tree, tree));
50#define convert_like(CONV, EXPR) \
51  convert_like_real ((CONV), (EXPR), NULL_TREE, 0, 0)
52#define convert_like_with_context(CONV, EXPR, FN, ARGNO) \
53  convert_like_real ((CONV), (EXPR), (FN), (ARGNO), 0)
54static tree convert_like_real PARAMS ((tree, tree, tree, int, int));
55static void op_error PARAMS ((enum tree_code, enum tree_code, tree, tree,
56			    tree, const char *));
57static tree build_object_call PARAMS ((tree, tree));
58static tree resolve_args PARAMS ((tree));
59static struct z_candidate * build_user_type_conversion_1
60	PARAMS ((tree, tree, int));
61static void print_z_candidates PARAMS ((struct z_candidate *));
62static tree build_this PARAMS ((tree));
63static struct z_candidate * splice_viable PARAMS ((struct z_candidate *));
64static int any_viable PARAMS ((struct z_candidate *));
65static struct z_candidate * add_template_candidate
66	PARAMS ((struct z_candidate *, tree, tree, tree, tree, tree, int,
67	       unification_kind_t));
68static struct z_candidate * add_template_candidate_real
69	PARAMS ((struct z_candidate *, tree, tree, tree, tree, tree, int,
70	       tree, unification_kind_t));
71static struct z_candidate * add_template_conv_candidate
72        PARAMS ((struct z_candidate *, tree, tree, tree, tree));
73static struct z_candidate * add_builtin_candidates
74	PARAMS ((struct z_candidate *, enum tree_code, enum tree_code,
75	       tree, tree *, int));
76static struct z_candidate * add_builtin_candidate
77	PARAMS ((struct z_candidate *, enum tree_code, enum tree_code,
78	       tree, tree, tree, tree *, tree *, int));
79static int is_complete PARAMS ((tree));
80static struct z_candidate * build_builtin_candidate
81	PARAMS ((struct z_candidate *, tree, tree, tree, tree *, tree *,
82	       int));
83static struct z_candidate * add_conv_candidate
84	PARAMS ((struct z_candidate *, tree, tree, tree));
85static struct z_candidate * add_function_candidate
86	PARAMS ((struct z_candidate *, tree, tree, tree, int));
87static tree implicit_conversion PARAMS ((tree, tree, tree, int));
88static tree standard_conversion PARAMS ((tree, tree, tree));
89static tree reference_binding PARAMS ((tree, tree, tree, int));
90static tree non_reference PARAMS ((tree));
91static tree build_conv PARAMS ((enum tree_code, tree, tree));
92static int is_subseq PARAMS ((tree, tree));
93static tree maybe_handle_ref_bind PARAMS ((tree*));
94static void maybe_handle_implicit_object PARAMS ((tree*));
95static struct z_candidate * add_candidate PARAMS ((struct z_candidate *,
96						   tree, tree, int));
97static tree source_type PARAMS ((tree));
98static void add_warning PARAMS ((struct z_candidate *, struct z_candidate *));
99static int reference_related_p PARAMS ((tree, tree));
100static int reference_compatible_p PARAMS ((tree, tree));
101static tree convert_class_to_reference PARAMS ((tree, tree, tree));
102static tree direct_reference_binding PARAMS ((tree, tree));
103static int promoted_arithmetic_type_p PARAMS ((tree));
104static tree conditional_conversion PARAMS ((tree, tree));
105
106tree
107build_vfield_ref (datum, type)
108     tree datum, type;
109{
110  tree rval;
111
112  if (datum == error_mark_node)
113    return error_mark_node;
114
115  if (TREE_CODE (TREE_TYPE (datum)) == REFERENCE_TYPE)
116    datum = convert_from_reference (datum);
117
118  if (! TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (type))
119    rval = build (COMPONENT_REF, TREE_TYPE (TYPE_VFIELD (type)),
120		  datum, TYPE_VFIELD (type));
121  else
122    rval = build_component_ref (datum, DECL_NAME (TYPE_VFIELD (type)), NULL_TREE, 0);
123
124  return rval;
125}
126
127/* Build a call to a member of an object.  I.e., one that overloads
128   operator ()(), or is a pointer-to-function or pointer-to-method.  */
129
130static tree
131build_field_call (basetype_path, instance_ptr, name, parms)
132     tree basetype_path, instance_ptr, name, parms;
133{
134  tree field, instance;
135
136  if (IDENTIFIER_CTOR_OR_DTOR_P (name))
137    return NULL_TREE;
138
139  /* Speed up the common case.  */
140  if (instance_ptr == current_class_ptr
141      && IDENTIFIER_CLASS_VALUE (name) == NULL_TREE)
142    return NULL_TREE;
143
144  field = lookup_field (basetype_path, name, 1, 0);
145
146  if (field == error_mark_node || field == NULL_TREE)
147    return field;
148
149  if (TREE_CODE (field) == FIELD_DECL || TREE_CODE (field) == VAR_DECL)
150    {
151      /* If it's a field, try overloading operator (),
152	 or calling if the field is a pointer-to-function.  */
153      instance = build_indirect_ref (instance_ptr, NULL);
154      instance = build_component_ref_1 (instance, field, 0);
155
156      if (instance == error_mark_node)
157	return error_mark_node;
158
159      if (IS_AGGR_TYPE (TREE_TYPE (instance)))
160	return build_opfncall (CALL_EXPR, LOOKUP_NORMAL,
161			       instance, parms, NULL_TREE);
162      else if (TREE_CODE (TREE_TYPE (instance)) == FUNCTION_TYPE
163	       || (TREE_CODE (TREE_TYPE (instance)) == POINTER_TYPE
164		   && (TREE_CODE (TREE_TYPE (TREE_TYPE (instance)))
165		       == FUNCTION_TYPE)))
166	return build_function_call (instance, parms);
167    }
168
169  return NULL_TREE;
170}
171
172/* Returns nonzero iff the destructor name specified in NAME
173   (a BIT_NOT_EXPR) matches BASETYPE.  The operand of NAME can take many
174   forms...  */
175
176int
177check_dtor_name (basetype, name)
178     tree basetype, name;
179{
180  name = TREE_OPERAND (name, 0);
181
182  /* Just accept something we've already complained about.  */
183  if (name == error_mark_node)
184    return 1;
185
186  if (TREE_CODE (name) == TYPE_DECL)
187    name = TREE_TYPE (name);
188  else if (TYPE_P (name))
189    /* OK */;
190  else if (TREE_CODE (name) == IDENTIFIER_NODE)
191    {
192      if ((IS_AGGR_TYPE (basetype) && name == constructor_name (basetype))
193	  || (TREE_CODE (basetype) == ENUMERAL_TYPE
194	      && name == TYPE_IDENTIFIER (basetype)))
195	name = basetype;
196      else
197	name = get_type_value (name);
198    }
199  /* In the case of:
200
201       template <class T> struct S { ~S(); };
202       int i;
203       i.~S();
204
205     NAME will be a class template.  */
206  else if (DECL_CLASS_TEMPLATE_P (name))
207    return 0;
208  else
209    abort ();
210
211  if (name && TYPE_MAIN_VARIANT (basetype) == TYPE_MAIN_VARIANT (name))
212    return 1;
213  return 0;
214}
215
216/* Build a method call of the form `EXP->SCOPES::NAME (PARMS)'.
217   This is how virtual function calls are avoided.  */
218
219tree
220build_scoped_method_call (exp, basetype, name, parms)
221     tree exp, basetype, name, parms;
222{
223  /* Because this syntactic form does not allow
224     a pointer to a base class to be `stolen',
225     we need not protect the derived->base conversion
226     that happens here.
227
228     @@ But we do have to check access privileges later.  */
229  tree binfo, decl;
230  tree type = TREE_TYPE (exp);
231
232  if (type == error_mark_node
233      || basetype == error_mark_node)
234    return error_mark_node;
235
236  if (processing_template_decl)
237    {
238      if (TREE_CODE (name) == BIT_NOT_EXPR
239	  && TREE_CODE (TREE_OPERAND (name, 0)) == IDENTIFIER_NODE)
240	{
241	  tree type = get_aggr_from_typedef (TREE_OPERAND (name, 0), 0);
242	  if (type)
243	    name = build_min_nt (BIT_NOT_EXPR, type);
244	}
245      name = build_min_nt (SCOPE_REF, basetype, name);
246      return build_min_nt (METHOD_CALL_EXPR, name, exp, parms, NULL_TREE);
247    }
248
249  if (TREE_CODE (type) == REFERENCE_TYPE)
250    type = TREE_TYPE (type);
251
252  if (TREE_CODE (basetype) == TREE_VEC)
253    {
254      binfo = basetype;
255      basetype = BINFO_TYPE (binfo);
256    }
257  else
258    binfo = NULL_TREE;
259
260  /* Check the destructor call syntax.  */
261  if (TREE_CODE (name) == BIT_NOT_EXPR)
262    {
263      /* We can get here if someone writes their destructor call like
264	 `obj.NS::~T()'; this isn't really a scoped method call, so hand
265	 it off.  */
266      if (TREE_CODE (basetype) == NAMESPACE_DECL)
267	return build_method_call (exp, name, parms, NULL_TREE, LOOKUP_NORMAL);
268
269      if (! check_dtor_name (basetype, name))
270	error ("qualified type `%T' does not match destructor name `~%T'",
271		  basetype, TREE_OPERAND (name, 0));
272
273      /* Destructors can be "called" for simple types; see 5.2.4 and 12.4 Note
274	 that explicit ~int is caught in the parser; this deals with typedefs
275	 and template parms.  */
276      if (! IS_AGGR_TYPE (basetype))
277	{
278	  if (TYPE_MAIN_VARIANT (type) != TYPE_MAIN_VARIANT (basetype))
279	    error ("type of `%E' does not match destructor type `%T' (type was `%T')",
280		      exp, basetype, type);
281
282	  return cp_convert (void_type_node, exp);
283	}
284    }
285
286  if (TREE_CODE (basetype) == NAMESPACE_DECL)
287    {
288      error ("`%D' is a namespace", basetype);
289      return error_mark_node;
290    }
291  if (! is_aggr_type (basetype, 1))
292    return error_mark_node;
293
294  if (! IS_AGGR_TYPE (type))
295    {
296      error ("base object `%E' of scoped method call is of non-aggregate type `%T'",
297		exp, type);
298      return error_mark_node;
299    }
300
301  if (! binfo)
302    {
303      binfo = lookup_base (type, basetype, ba_check, NULL);
304      if (binfo == error_mark_node)
305	return error_mark_node;
306      if (! binfo)
307	error_not_base_type (basetype, type);
308    }
309
310  if (binfo)
311    {
312      if (TREE_CODE (exp) == INDIRECT_REF)
313	{
314	  decl = build_base_path (PLUS_EXPR,
315				  build_unary_op (ADDR_EXPR, exp, 0),
316				  binfo, 1);
317	  decl = build_indirect_ref (decl, NULL);
318	}
319      else
320	decl = build_scoped_ref (exp, basetype);
321
322      /* Call to a destructor.  */
323      if (TREE_CODE (name) == BIT_NOT_EXPR)
324	{
325	  if (! TYPE_HAS_DESTRUCTOR (TREE_TYPE (decl)))
326	    return cp_convert (void_type_node, exp);
327
328	  return build_delete (TREE_TYPE (decl), decl,
329			       sfk_complete_destructor,
330			       LOOKUP_NORMAL|LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR,
331			       0);
332	}
333
334      /* Call to a method.  */
335      return build_method_call (decl, name, parms, binfo,
336				LOOKUP_NORMAL|LOOKUP_NONVIRTUAL);
337    }
338  return error_mark_node;
339}
340
341/* We want the address of a function or method.  We avoid creating a
342   pointer-to-member function.  */
343
344tree
345build_addr_func (function)
346     tree function;
347{
348  tree type = TREE_TYPE (function);
349
350  /* We have to do these by hand to avoid real pointer to member
351     functions.  */
352  if (TREE_CODE (type) == METHOD_TYPE)
353    {
354      tree addr;
355
356      type = build_pointer_type (type);
357
358      if (mark_addressable (function) == 0)
359	return error_mark_node;
360
361      addr = build1 (ADDR_EXPR, type, function);
362
363      /* Address of a static or external variable or function counts
364	 as a constant */
365      if (staticp (function))
366	TREE_CONSTANT (addr) = 1;
367
368      function = addr;
369    }
370  else
371    function = default_conversion (function);
372
373  return function;
374}
375
376/* Build a CALL_EXPR, we can handle FUNCTION_TYPEs, METHOD_TYPEs, or
377   POINTER_TYPE to those.  Note, pointer to member function types
378   (TYPE_PTRMEMFUNC_P) must be handled by our callers.  */
379
380tree
381build_call (function, parms)
382     tree function, parms;
383{
384  int is_constructor = 0;
385  int nothrow;
386  tree tmp;
387  tree decl;
388  tree result_type;
389
390  function = build_addr_func (function);
391
392  if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function)))
393    {
394      sorry ("unable to call pointer to member function here");
395      return error_mark_node;
396    }
397
398  result_type = TREE_TYPE (TREE_TYPE (TREE_TYPE (function)));
399
400  if (TREE_CODE (function) == ADDR_EXPR
401      && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
402    decl = TREE_OPERAND (function, 0);
403  else
404    decl = NULL_TREE;
405
406  /* We check both the decl and the type; a function may be known not to
407     throw without being declared throw().  */
408  nothrow = ((decl && TREE_NOTHROW (decl))
409	     || TYPE_NOTHROW_P (TREE_TYPE (TREE_TYPE (function))));
410
411  if (decl && TREE_THIS_VOLATILE (decl))
412    current_function_returns_abnormally = 1;
413
414  if (decl && TREE_DEPRECATED (decl))
415    warn_deprecated_use (decl);
416
417  if (decl && DECL_CONSTRUCTOR_P (decl))
418    is_constructor = 1;
419
420  if (decl && ! TREE_USED (decl))
421    {
422      /* We invoke build_call directly for several library functions.
423	 These may have been declared normally if we're building libgcc,
424	 so we can't just check DECL_ARTIFICIAL.  */
425      if (DECL_ARTIFICIAL (decl)
426	  || !strncmp (IDENTIFIER_POINTER (DECL_NAME (decl)), "__", 2))
427	mark_used (decl);
428      else
429	abort ();
430    }
431
432  /* Don't pass empty class objects by value.  This is useful
433     for tags in STL, which are used to control overload resolution.
434     We don't need to handle other cases of copying empty classes.  */
435  if (! decl || ! DECL_BUILT_IN (decl))
436    for (tmp = parms; tmp; tmp = TREE_CHAIN (tmp))
437      if (is_empty_class (TREE_TYPE (TREE_VALUE (tmp)))
438	  && ! TREE_ADDRESSABLE (TREE_TYPE (TREE_VALUE (tmp))))
439	{
440	  tree t = build (EMPTY_CLASS_EXPR, TREE_TYPE (TREE_VALUE (tmp)));
441	  TREE_VALUE (tmp) = build (COMPOUND_EXPR, TREE_TYPE (t),
442				    TREE_VALUE (tmp), t);
443	}
444
445  function = build_nt (CALL_EXPR, function, parms, NULL_TREE);
446  TREE_HAS_CONSTRUCTOR (function) = is_constructor;
447  TREE_TYPE (function) = result_type;
448  TREE_SIDE_EFFECTS (function) = 1;
449  TREE_NOTHROW (function) = nothrow;
450
451  return function;
452}
453
454/* Build something of the form ptr->method (args)
455   or object.method (args).  This can also build
456   calls to constructors, and find friends.
457
458   Member functions always take their class variable
459   as a pointer.
460
461   INSTANCE is a class instance.
462
463   NAME is the name of the method desired, usually an IDENTIFIER_NODE.
464
465   PARMS help to figure out what that NAME really refers to.
466
467   BASETYPE_PATH, if non-NULL, contains a chain from the type of INSTANCE
468   down to the real instance type to use for access checking.  We need this
469   information to get protected accesses correct.  This parameter is used
470   by build_member_call.
471
472   FLAGS is the logical disjunction of zero or more LOOKUP_
473   flags.  See cp-tree.h for more info.
474
475   If this is all OK, calls build_function_call with the resolved
476   member function.
477
478   This function must also handle being called to perform
479   initialization, promotion/coercion of arguments, and
480   instantiation of default parameters.
481
482   Note that NAME may refer to an instance variable name.  If
483   `operator()()' is defined for the type of that field, then we return
484   that result.  */
485
486#ifdef GATHER_STATISTICS
487extern int n_build_method_call;
488#endif
489
490tree
491build_method_call (instance, name, parms, basetype_path, flags)
492     tree instance, name, parms, basetype_path;
493     int flags;
494{
495  tree basetype, instance_ptr;
496
497#ifdef GATHER_STATISTICS
498  n_build_method_call++;
499#endif
500
501  if (instance == error_mark_node
502      || name == error_mark_node
503      || parms == error_mark_node
504      || (instance != NULL_TREE && TREE_TYPE (instance) == error_mark_node))
505    return error_mark_node;
506
507  if (processing_template_decl)
508    {
509      /* We need to process template parm names here so that tsubst catches
510	 them properly.  Other type names can wait.  */
511      if (TREE_CODE (name) == BIT_NOT_EXPR)
512	{
513	  tree type = NULL_TREE;
514
515	  if (TREE_CODE (TREE_OPERAND (name, 0)) == IDENTIFIER_NODE)
516	    type = get_aggr_from_typedef (TREE_OPERAND (name, 0), 0);
517	  else if (TREE_CODE (TREE_OPERAND (name, 0)) == TYPE_DECL)
518	    type = TREE_TYPE (TREE_OPERAND (name, 0));
519
520	  if (type && TREE_CODE (type) == TEMPLATE_TYPE_PARM)
521	    name = build_min_nt (BIT_NOT_EXPR, type);
522	}
523
524      return build_min_nt (METHOD_CALL_EXPR, name, instance, parms, NULL_TREE);
525    }
526
527  if (TREE_CODE (name) == BIT_NOT_EXPR)
528    {
529      if (parms)
530	error ("destructors take no parameters");
531      basetype = TREE_TYPE (instance);
532      if (TREE_CODE (basetype) == REFERENCE_TYPE)
533	basetype = TREE_TYPE (basetype);
534
535      if (! check_dtor_name (basetype, name))
536	error
537	  ("destructor name `~%T' does not match type `%T' of expression",
538	   TREE_OPERAND (name, 0), basetype);
539
540      if (! TYPE_HAS_DESTRUCTOR (complete_type (basetype)))
541	return cp_convert (void_type_node, instance);
542      instance = default_conversion (instance);
543      instance_ptr = build_unary_op (ADDR_EXPR, instance, 0);
544      return build_delete (build_pointer_type (basetype),
545			   instance_ptr, sfk_complete_destructor,
546			   LOOKUP_NORMAL|LOOKUP_DESTRUCTOR, 0);
547    }
548
549  return build_new_method_call (instance, name, parms, basetype_path, flags);
550}
551
552/* New overloading code.  */
553
554struct z_candidate {
555  tree fn;
556  tree convs;
557  tree second_conv;
558  int viable;
559  tree basetype_path;
560  tree template;
561  tree warnings;
562  struct z_candidate *next;
563};
564
565#define IDENTITY_RANK 0
566#define EXACT_RANK 1
567#define PROMO_RANK 2
568#define STD_RANK 3
569#define PBOOL_RANK 4
570#define USER_RANK 5
571#define ELLIPSIS_RANK 6
572#define BAD_RANK 7
573
574#define ICS_RANK(NODE)				\
575  (ICS_BAD_FLAG (NODE) ? BAD_RANK   		\
576   : ICS_ELLIPSIS_FLAG (NODE) ? ELLIPSIS_RANK	\
577   : ICS_USER_FLAG (NODE) ? USER_RANK		\
578   : ICS_STD_RANK (NODE))
579
580#define ICS_STD_RANK(NODE) TREE_COMPLEXITY (NODE)
581
582#define ICS_USER_FLAG(NODE) TREE_LANG_FLAG_0 (NODE)
583#define ICS_ELLIPSIS_FLAG(NODE) TREE_LANG_FLAG_1 (NODE)
584#define ICS_THIS_FLAG(NODE) TREE_LANG_FLAG_2 (NODE)
585#define ICS_BAD_FLAG(NODE) TREE_LANG_FLAG_3 (NODE)
586
587/* In a REF_BIND or a BASE_CONV, this indicates that a temporary
588   should be created to hold the result of the conversion.  */
589#define NEED_TEMPORARY_P(NODE) TREE_LANG_FLAG_4 (NODE)
590
591#define USER_CONV_CAND(NODE) \
592  ((struct z_candidate *)WRAPPER_PTR (TREE_OPERAND (NODE, 1)))
593#define USER_CONV_FN(NODE) (USER_CONV_CAND (NODE)->fn)
594
595int
596null_ptr_cst_p (t)
597     tree t;
598{
599  /* [conv.ptr]
600
601     A null pointer constant is an integral constant expression
602     (_expr.const_) rvalue of integer type that evaluates to zero.  */
603  if (t == null_node
604      || (CP_INTEGRAL_TYPE_P (TREE_TYPE (t)) && integer_zerop (t)))
605    return 1;
606  return 0;
607}
608
609
610/* Returns non-zero if PARMLIST consists of only default parms and/or
611   ellipsis. */
612
613int
614sufficient_parms_p (parmlist)
615     tree parmlist;
616{
617  for (; parmlist && parmlist != void_list_node;
618       parmlist = TREE_CHAIN (parmlist))
619    if (!TREE_PURPOSE (parmlist))
620      return 0;
621  return 1;
622}
623
624static tree
625build_conv (code, type, from)
626     enum tree_code code;
627     tree type, from;
628{
629  tree t;
630  int rank = ICS_STD_RANK (from);
631
632  /* We can't use buildl1 here because CODE could be USER_CONV, which
633     takes two arguments.  In that case, the caller is responsible for
634     filling in the second argument.  */
635  t = make_node (code);
636  TREE_TYPE (t) = type;
637  TREE_OPERAND (t, 0) = from;
638
639  switch (code)
640    {
641    case PTR_CONV:
642    case PMEM_CONV:
643    case BASE_CONV:
644    case STD_CONV:
645      if (rank < STD_RANK)
646	rank = STD_RANK;
647      break;
648
649    case QUAL_CONV:
650      if (rank < EXACT_RANK)
651	rank = EXACT_RANK;
652
653    default:
654      break;
655    }
656  ICS_STD_RANK (t) = rank;
657  ICS_USER_FLAG (t) = ICS_USER_FLAG (from);
658  ICS_BAD_FLAG (t) = ICS_BAD_FLAG (from);
659  return t;
660}
661
662/* If T is a REFERENCE_TYPE return the type to which T refers.
663   Otherwise, return T itself.  */
664
665static tree
666non_reference (t)
667     tree t;
668{
669  if (TREE_CODE (t) == REFERENCE_TYPE)
670    t = TREE_TYPE (t);
671  return t;
672}
673
674tree
675strip_top_quals (t)
676     tree t;
677{
678  if (TREE_CODE (t) == ARRAY_TYPE)
679    return t;
680  return TYPE_MAIN_VARIANT (t);
681}
682
683/* Returns the standard conversion path (see [conv]) from type FROM to type
684   TO, if any.  For proper handling of null pointer constants, you must
685   also pass the expression EXPR to convert from.  */
686
687static tree
688standard_conversion (to, from, expr)
689     tree to, from, expr;
690{
691  enum tree_code fcode, tcode;
692  tree conv;
693  int fromref = 0;
694
695  if (TREE_CODE (to) == REFERENCE_TYPE)
696    to = TREE_TYPE (to);
697  if (TREE_CODE (from) == REFERENCE_TYPE)
698    {
699      fromref = 1;
700      from = TREE_TYPE (from);
701    }
702  to = strip_top_quals (to);
703  from = strip_top_quals (from);
704
705  if ((TYPE_PTRFN_P (to) || TYPE_PTRMEMFUNC_P (to))
706      && expr && type_unknown_p (expr))
707    {
708      expr = instantiate_type (to, expr, tf_none);
709      if (expr == error_mark_node)
710	return NULL_TREE;
711      from = TREE_TYPE (expr);
712    }
713
714  fcode = TREE_CODE (from);
715  tcode = TREE_CODE (to);
716
717  conv = build1 (IDENTITY_CONV, from, expr);
718
719  if (fcode == FUNCTION_TYPE)
720    {
721      from = build_pointer_type (from);
722      fcode = TREE_CODE (from);
723      conv = build_conv (LVALUE_CONV, from, conv);
724    }
725  else if (fcode == ARRAY_TYPE)
726    {
727      from = build_pointer_type (TREE_TYPE (from));
728      fcode = TREE_CODE (from);
729      conv = build_conv (LVALUE_CONV, from, conv);
730    }
731  else if (fromref || (expr && lvalue_p (expr)))
732    conv = build_conv (RVALUE_CONV, from, conv);
733
734   /* Allow conversion between `__complex__' data types  */
735  if (tcode == COMPLEX_TYPE && fcode == COMPLEX_TYPE)
736    {
737      /* The standard conversion sequence to convert FROM to TO is
738         the standard conversion sequence to perform componentwise
739         conversion.  */
740      tree part_conv = standard_conversion
741        (TREE_TYPE (to), TREE_TYPE (from), NULL_TREE);
742
743      if (part_conv)
744        {
745          conv = build_conv (TREE_CODE (part_conv), to, conv);
746          ICS_STD_RANK (conv) = ICS_STD_RANK (part_conv);
747        }
748      else
749        conv = NULL_TREE;
750
751      return conv;
752    }
753
754  if (same_type_p (from, to))
755    return conv;
756
757  if ((tcode == POINTER_TYPE || TYPE_PTRMEMFUNC_P (to))
758      && expr && null_ptr_cst_p (expr))
759    {
760      conv = build_conv (STD_CONV, to, conv);
761    }
762  else if ((tcode == INTEGER_TYPE && fcode == POINTER_TYPE)
763	   || (tcode == POINTER_TYPE && fcode == INTEGER_TYPE))
764    {
765      /* For backwards brain damage compatibility, allow interconversion of
766	 pointers and integers with a pedwarn.  */
767      conv = build_conv (STD_CONV, to, conv);
768      ICS_BAD_FLAG (conv) = 1;
769    }
770  else if (tcode == ENUMERAL_TYPE && fcode == INTEGER_TYPE
771	   && TYPE_PRECISION (to) == TYPE_PRECISION (from))
772    {
773      /* For backwards brain damage compatibility, allow interconversion of
774	 enums and integers with a pedwarn.  */
775      conv = build_conv (STD_CONV, to, conv);
776      ICS_BAD_FLAG (conv) = 1;
777    }
778  else if (tcode == POINTER_TYPE && fcode == POINTER_TYPE)
779    {
780      enum tree_code ufcode = TREE_CODE (TREE_TYPE (from));
781      enum tree_code utcode = TREE_CODE (TREE_TYPE (to));
782
783      if (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (from),
784						     TREE_TYPE (to)))
785	;
786      else if (utcode == VOID_TYPE && ufcode != OFFSET_TYPE
787	       && ufcode != FUNCTION_TYPE)
788	{
789	  from = build_pointer_type
790	    (cp_build_qualified_type (void_type_node,
791				      cp_type_quals (TREE_TYPE (from))));
792	  conv = build_conv (PTR_CONV, from, conv);
793	}
794      else if (ufcode == OFFSET_TYPE && utcode == OFFSET_TYPE)
795	{
796	  tree fbase = TYPE_OFFSET_BASETYPE (TREE_TYPE (from));
797	  tree tbase = TYPE_OFFSET_BASETYPE (TREE_TYPE (to));
798
799	  if (DERIVED_FROM_P (fbase, tbase)
800	      && (same_type_ignoring_top_level_qualifiers_p
801		  (TREE_TYPE (TREE_TYPE (from)),
802		   TREE_TYPE (TREE_TYPE (to)))))
803	    {
804	      from = build_offset_type (tbase, TREE_TYPE (TREE_TYPE (from)));
805	      from = build_pointer_type (from);
806	      conv = build_conv (PMEM_CONV, from, conv);
807	    }
808	}
809      else if (IS_AGGR_TYPE (TREE_TYPE (from))
810	       && IS_AGGR_TYPE (TREE_TYPE (to)))
811	{
812	  if (DERIVED_FROM_P (TREE_TYPE (to), TREE_TYPE (from)))
813	    {
814	      from =
815		cp_build_qualified_type (TREE_TYPE (to),
816					 cp_type_quals (TREE_TYPE (from)));
817	      from = build_pointer_type (from);
818	      conv = build_conv (PTR_CONV, from, conv);
819	    }
820	}
821
822      if (same_type_p (from, to))
823	/* OK */;
824      else if (comp_ptr_ttypes (TREE_TYPE (to), TREE_TYPE (from)))
825	conv = build_conv (QUAL_CONV, to, conv);
826      else if (expr && string_conv_p (to, expr, 0))
827	/* converting from string constant to char *.  */
828	conv = build_conv (QUAL_CONV, to, conv);
829      else if (ptr_reasonably_similar (TREE_TYPE (to), TREE_TYPE (from)))
830	{
831	  conv = build_conv (PTR_CONV, to, conv);
832	  ICS_BAD_FLAG (conv) = 1;
833	}
834      else
835	return 0;
836
837      from = to;
838    }
839  else if (TYPE_PTRMEMFUNC_P (to) && TYPE_PTRMEMFUNC_P (from))
840    {
841      tree fromfn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (from));
842      tree tofn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (to));
843      tree fbase = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (fromfn)));
844      tree tbase = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (tofn)));
845
846      if (!DERIVED_FROM_P (fbase, tbase)
847	  || !same_type_p (TREE_TYPE (fromfn), TREE_TYPE (tofn))
848	  || !compparms (TREE_CHAIN (TYPE_ARG_TYPES (fromfn)),
849			 TREE_CHAIN (TYPE_ARG_TYPES (tofn)))
850	  || cp_type_quals (fbase) != cp_type_quals (tbase))
851	return 0;
852
853      from = cp_build_qualified_type (tbase, cp_type_quals (fbase));
854      from = build_cplus_method_type (from, TREE_TYPE (fromfn),
855				      TREE_CHAIN (TYPE_ARG_TYPES (fromfn)));
856      from = build_ptrmemfunc_type (build_pointer_type (from));
857      conv = build_conv (PMEM_CONV, from, conv);
858    }
859  else if (tcode == BOOLEAN_TYPE)
860    {
861      if (! (INTEGRAL_CODE_P (fcode) || fcode == REAL_TYPE
862	     || fcode == POINTER_TYPE || TYPE_PTRMEMFUNC_P (from)))
863	return 0;
864
865      conv = build_conv (STD_CONV, to, conv);
866      if (fcode == POINTER_TYPE
867	  || (TYPE_PTRMEMFUNC_P (from) && ICS_STD_RANK (conv) < PBOOL_RANK))
868	ICS_STD_RANK (conv) = PBOOL_RANK;
869    }
870  /* We don't check for ENUMERAL_TYPE here because there are no standard
871     conversions to enum type.  */
872  else if (tcode == INTEGER_TYPE || tcode == BOOLEAN_TYPE
873	   || tcode == REAL_TYPE)
874    {
875      if (! (INTEGRAL_CODE_P (fcode) || fcode == REAL_TYPE))
876	return 0;
877      conv = build_conv (STD_CONV, to, conv);
878
879      /* Give this a better rank if it's a promotion.  */
880      if (to == type_promotes_to (from)
881	  && ICS_STD_RANK (TREE_OPERAND (conv, 0)) <= PROMO_RANK)
882	ICS_STD_RANK (conv) = PROMO_RANK;
883    }
884  else if (IS_AGGR_TYPE (to) && IS_AGGR_TYPE (from)
885	   && is_properly_derived_from (from, to))
886    {
887      if (TREE_CODE (conv) == RVALUE_CONV)
888	conv = TREE_OPERAND (conv, 0);
889      conv = build_conv (BASE_CONV, to, conv);
890      /* The derived-to-base conversion indicates the initialization
891	 of a parameter with base type from an object of a derived
892	 type.  A temporary object is created to hold the result of
893	 the conversion.  */
894      NEED_TEMPORARY_P (conv) = 1;
895    }
896  else
897    return 0;
898
899  return conv;
900}
901
902/* Returns non-zero if T1 is reference-related to T2.  */
903
904static int
905reference_related_p (t1, t2)
906     tree t1;
907     tree t2;
908{
909  t1 = TYPE_MAIN_VARIANT (t1);
910  t2 = TYPE_MAIN_VARIANT (t2);
911
912  /* [dcl.init.ref]
913
914     Given types "cv1 T1" and "cv2 T2," "cv1 T1" is reference-related
915     to "cv2 T2" if T1 is the same type as T2, or T1 is a base class
916     of T2.  */
917  return (same_type_p (t1, t2)
918	  || (CLASS_TYPE_P (t1) && CLASS_TYPE_P (t2)
919	      && DERIVED_FROM_P (t1, t2)));
920}
921
922/* Returns non-zero if T1 is reference-compatible with T2.  */
923
924static int
925reference_compatible_p (t1, t2)
926     tree t1;
927     tree t2;
928{
929  /* [dcl.init.ref]
930
931     "cv1 T1" is reference compatible with "cv2 T2" if T1 is
932     reference-related to T2 and cv1 is the same cv-qualification as,
933     or greater cv-qualification than, cv2.  */
934  return (reference_related_p (t1, t2)
935	  && at_least_as_qualified_p (t1, t2));
936}
937
938/* Determine whether or not the EXPR (of class type S) can be
939   converted to T as in [over.match.ref].  */
940
941static tree
942convert_class_to_reference (t, s, expr)
943     tree t;
944     tree s;
945     tree expr;
946{
947  tree conversions;
948  tree arglist;
949  tree conv;
950  struct z_candidate *candidates;
951  struct z_candidate *cand;
952
953  /* [over.match.ref]
954
955     Assuming that "cv1 T" is the underlying type of the reference
956     being initialized, and "cv S" is the type of the initializer
957     expression, with S a class type, the candidate functions are
958     selected as follows:
959
960     --The conversion functions of S and its base classes are
961       considered.  Those that are not hidden within S and yield type
962       "reference to cv2 T2", where "cv1 T" is reference-compatible
963       (_dcl.init.ref_) with "cv2 T2", are candidate functions.
964
965     The argument list has one argument, which is the initializer
966     expression.  */
967
968  candidates = 0;
969
970  /* Conceptually, we should take the address of EXPR and put it in
971     the argument list.  Unfortunately, however, that can result in
972     error messages, which we should not issue now because we are just
973     trying to find a conversion operator.  Therefore, we use NULL,
974     cast to the appropriate type.  */
975  arglist = build_int_2 (0, 0);
976  TREE_TYPE (arglist) = build_pointer_type (s);
977  arglist = build_tree_list (NULL_TREE, arglist);
978
979  for (conversions = lookup_conversions (s);
980       conversions;
981       conversions = TREE_CHAIN (conversions))
982    {
983      tree fns = TREE_VALUE (conversions);
984
985      for (; fns; fns = OVL_NEXT (fns))
986	{
987	  tree f = OVL_CURRENT (fns);
988	  tree t2 = TREE_TYPE (TREE_TYPE (f));
989	  struct z_candidate *old_candidates = candidates;
990
991	  /* If this is a template function, try to get an exact
992             match.  */
993	  if (TREE_CODE (f) == TEMPLATE_DECL)
994	    {
995	      candidates
996		= add_template_candidate (candidates,
997					  f, s,
998					  NULL_TREE,
999					  arglist,
1000					  build_reference_type (t),
1001					  LOOKUP_NORMAL,
1002					  DEDUCE_CONV);
1003
1004	      if (candidates != old_candidates)
1005		{
1006		  /* Now, see if the conversion function really returns
1007		     an lvalue of the appropriate type.  From the
1008		     point of view of unification, simply returning an
1009		     rvalue of the right type is good enough.  */
1010		  f = candidates->fn;
1011		  t2 = TREE_TYPE (TREE_TYPE (f));
1012		  if (TREE_CODE (t2) != REFERENCE_TYPE
1013		      || !reference_compatible_p (t, TREE_TYPE (t2)))
1014		    candidates = candidates->next;
1015		}
1016	    }
1017	  else if (TREE_CODE (t2) == REFERENCE_TYPE
1018		   && reference_compatible_p (t, TREE_TYPE (t2)))
1019	    candidates
1020	      = add_function_candidate (candidates, f, s, arglist,
1021					LOOKUP_NORMAL);
1022
1023	  if (candidates != old_candidates)
1024	    candidates->basetype_path = TYPE_BINFO (s);
1025	}
1026    }
1027
1028  /* If none of the conversion functions worked out, let our caller
1029     know.  */
1030  if (!any_viable (candidates))
1031    return NULL_TREE;
1032
1033  candidates = splice_viable (candidates);
1034  cand = tourney (candidates);
1035  if (!cand)
1036    return NULL_TREE;
1037
1038  conv = build1 (IDENTITY_CONV, s, expr);
1039  conv = build_conv (USER_CONV, TREE_TYPE (TREE_TYPE (cand->fn)),
1040		     conv);
1041  TREE_OPERAND (conv, 1) = build_ptr_wrapper (cand);
1042  ICS_USER_FLAG (conv) = 1;
1043  if (cand->viable == -1)
1044    ICS_BAD_FLAG (conv) = 1;
1045  cand->second_conv = conv;
1046
1047  return conv;
1048}
1049
1050/* A reference of the indicated TYPE is being bound directly to the
1051   expression represented by the implicit conversion sequence CONV.
1052   Return a conversion sequence for this binding.  */
1053
1054static tree
1055direct_reference_binding (type, conv)
1056     tree type;
1057     tree conv;
1058{
1059  tree t = TREE_TYPE (type);
1060
1061  /* [over.ics.rank]
1062
1063     When a parameter of reference type binds directly
1064     (_dcl.init.ref_) to an argument expression, the implicit
1065     conversion sequence is the identity conversion, unless the
1066     argument expression has a type that is a derived class of the
1067     parameter type, in which case the implicit conversion sequence is
1068     a derived-to-base Conversion.
1069
1070     If the parameter binds directly to the result of applying a
1071     conversion function to the argument expression, the implicit
1072     conversion sequence is a user-defined conversion sequence
1073     (_over.ics.user_), with the second standard conversion sequence
1074     either an identity conversion or, if the conversion function
1075     returns an entity of a type that is a derived class of the
1076     parameter type, a derived-to-base conversion.  */
1077  if (!same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (conv)))
1078    {
1079      /* Represent the derived-to-base conversion.  */
1080      conv = build_conv (BASE_CONV, t, conv);
1081      /* We will actually be binding to the base-class subobject in
1082	 the derived class, so we mark this conversion appropriately.
1083	 That way, convert_like knows not to generate a temporary.  */
1084      NEED_TEMPORARY_P (conv) = 0;
1085    }
1086  return build_conv (REF_BIND, type, conv);
1087}
1088
1089/* Returns the conversion path from type FROM to reference type TO for
1090   purposes of reference binding.  For lvalue binding, either pass a
1091   reference type to FROM or an lvalue expression to EXPR.  If the
1092   reference will be bound to a temporary, NEED_TEMPORARY_P is set for
1093   the conversion returned.  */
1094
1095static tree
1096reference_binding (rto, rfrom, expr, flags)
1097     tree rto, rfrom, expr;
1098     int flags;
1099{
1100  tree conv = NULL_TREE;
1101  tree to = TREE_TYPE (rto);
1102  tree from = rfrom;
1103  int related_p;
1104  int compatible_p;
1105  cp_lvalue_kind lvalue_p = clk_none;
1106
1107  if (TREE_CODE (to) == FUNCTION_TYPE && expr && type_unknown_p (expr))
1108    {
1109      expr = instantiate_type (to, expr, tf_none);
1110      if (expr == error_mark_node)
1111	return NULL_TREE;
1112      from = TREE_TYPE (expr);
1113    }
1114
1115  if (TREE_CODE (from) == REFERENCE_TYPE)
1116    {
1117      /* Anything with reference type is an lvalue.  */
1118      lvalue_p = clk_ordinary;
1119      from = TREE_TYPE (from);
1120    }
1121  else if (expr)
1122    lvalue_p = real_lvalue_p (expr);
1123
1124  /* Figure out whether or not the types are reference-related and
1125     reference compatible.  We have do do this after stripping
1126     references from FROM.  */
1127  related_p = reference_related_p (to, from);
1128  compatible_p = reference_compatible_p (to, from);
1129
1130  if (lvalue_p && compatible_p)
1131    {
1132      /* [dcl.init.ref]
1133
1134	 If the initializer expression
1135
1136	 -- is an lvalue (but not an lvalue for a bit-field), and "cv1 T1"
1137	    is reference-compatible with "cv2 T2,"
1138
1139	 the reference is bound directly to the initializer exprssion
1140	 lvalue.  */
1141      conv = build1 (IDENTITY_CONV, from, expr);
1142      conv = direct_reference_binding (rto, conv);
1143      if ((lvalue_p & clk_bitfield) != 0
1144	  && CP_TYPE_CONST_NON_VOLATILE_P (to))
1145	/* For the purposes of overload resolution, we ignore the fact
1146	   this expression is a bitfield. (In particular,
1147	   [over.ics.ref] says specifically that a function with a
1148	   non-const reference parameter is viable even if the
1149	   argument is a bitfield.)
1150
1151	   However, when we actually call the function we must create
1152	   a temporary to which to bind the reference.  If the
1153	   reference is volatile, or isn't const, then we cannot make
1154	   a temporary, so we just issue an error when the conversion
1155	   actually occurs.  */
1156	NEED_TEMPORARY_P (conv) = 1;
1157      return conv;
1158    }
1159  else if (CLASS_TYPE_P (from) && !(flags & LOOKUP_NO_CONVERSION))
1160    {
1161      /* [dcl.init.ref]
1162
1163	 If the initializer exprsesion
1164
1165	 -- has a class type (i.e., T2 is a class type) can be
1166	    implicitly converted to an lvalue of type "cv3 T3," where
1167	    "cv1 T1" is reference-compatible with "cv3 T3".  (this
1168	    conversion is selected by enumerating the applicable
1169	    conversion functions (_over.match.ref_) and choosing the
1170	    best one through overload resolution.  (_over.match_).
1171
1172        the reference is bound to the lvalue result of the conversion
1173	in the second case.  */
1174      conv = convert_class_to_reference (to, from, expr);
1175      if (conv)
1176	return direct_reference_binding (rto, conv);
1177    }
1178
1179  /* From this point on, we conceptually need temporaries, even if we
1180     elide them.  Only the cases above are "direct bindings".  */
1181  if (flags & LOOKUP_NO_TEMP_BIND)
1182    return NULL_TREE;
1183
1184  /* [over.ics.rank]
1185
1186     When a parameter of reference type is not bound directly to an
1187     argument expression, the conversion sequence is the one required
1188     to convert the argument expression to the underlying type of the
1189     reference according to _over.best.ics_.  Conceptually, this
1190     conversion sequence corresponds to copy-initializing a temporary
1191     of the underlying type with the argument expression.  Any
1192     difference in top-level cv-qualification is subsumed by the
1193     initialization itself and does not constitute a conversion.  */
1194
1195  /* [dcl.init.ref]
1196
1197     Otherwise, the reference shall be to a non-volatile const type.  */
1198  if (!CP_TYPE_CONST_NON_VOLATILE_P (to))
1199    return NULL_TREE;
1200
1201  /* [dcl.init.ref]
1202
1203     If the initializer expression is an rvalue, with T2 a class type,
1204     and "cv1 T1" is reference-compatible with "cv2 T2", the reference
1205     is bound in one of the following ways:
1206
1207     -- The reference is bound to the object represented by the rvalue
1208        or to a sub-object within that object.
1209
1210     In this case, the implicit conversion sequence is supposed to be
1211     same as we would obtain by generating a temporary.  Fortunately,
1212     if the types are reference compatible, then this is either an
1213     identity conversion or the derived-to-base conversion, just as
1214     for direct binding.  */
1215  if (CLASS_TYPE_P (from) && compatible_p)
1216    {
1217      conv = build1 (IDENTITY_CONV, from, expr);
1218      return direct_reference_binding (rto, conv);
1219    }
1220
1221  /* [dcl.init.ref]
1222
1223     Otherwise, a temporary of type "cv1 T1" is created and
1224     initialized from the initializer expression using the rules for a
1225     non-reference copy initialization.  If T1 is reference-related to
1226     T2, cv1 must be the same cv-qualification as, or greater
1227     cv-qualification than, cv2; otherwise, the program is ill-formed.  */
1228  if (related_p && !at_least_as_qualified_p (to, from))
1229    return NULL_TREE;
1230
1231  conv = implicit_conversion (to, from, expr, flags);
1232  if (!conv)
1233    return NULL_TREE;
1234
1235  conv = build_conv (REF_BIND, rto, conv);
1236  /* This reference binding, unlike those above, requires the
1237     creation of a temporary.  */
1238  NEED_TEMPORARY_P (conv) = 1;
1239
1240  return conv;
1241}
1242
1243/* Returns the implicit conversion sequence (see [over.ics]) from type FROM
1244   to type TO.  The optional expression EXPR may affect the conversion.
1245   FLAGS are the usual overloading flags.  Only LOOKUP_NO_CONVERSION is
1246   significant.  */
1247
1248static tree
1249implicit_conversion (to, from, expr, flags)
1250     tree to, from, expr;
1251     int flags;
1252{
1253  tree conv;
1254  struct z_candidate *cand;
1255
1256  /* Resolve expressions like `A::p' that we thought might become
1257     pointers-to-members.  */
1258  if (expr && TREE_CODE (expr) == OFFSET_REF)
1259    {
1260      expr = resolve_offset_ref (expr);
1261      from = TREE_TYPE (expr);
1262    }
1263
1264  if (from == error_mark_node || to == error_mark_node
1265      || expr == error_mark_node)
1266    return NULL_TREE;
1267
1268  /* Make sure both the FROM and TO types are complete so that
1269     user-defined conversions are available.  */
1270  complete_type (from);
1271  complete_type (to);
1272
1273  if (TREE_CODE (to) == REFERENCE_TYPE)
1274    conv = reference_binding (to, from, expr, flags);
1275  else
1276    conv = standard_conversion (to, from, expr);
1277
1278  if (conv)
1279    ;
1280  else if (expr != NULL_TREE
1281	   && (IS_AGGR_TYPE (from)
1282	       || IS_AGGR_TYPE (to))
1283	   && (flags & LOOKUP_NO_CONVERSION) == 0)
1284    {
1285      cand = build_user_type_conversion_1
1286	(to, expr, LOOKUP_ONLYCONVERTING);
1287      if (cand)
1288	conv = cand->second_conv;
1289
1290      /* We used to try to bind a reference to a temporary here, but that
1291	 is now handled by the recursive call to this function at the end
1292	 of reference_binding.  */
1293    }
1294
1295  return conv;
1296}
1297
1298/* Add a new entry to the list of candidates.  Used by the add_*_candidate
1299   functions.  */
1300
1301static struct z_candidate *
1302add_candidate (candidates, fn, convs, viable)
1303     struct z_candidate *candidates;
1304     tree fn, convs;
1305     int viable;
1306{
1307  struct z_candidate *cand
1308    = (struct z_candidate *) ggc_alloc_cleared (sizeof (struct z_candidate));
1309
1310  cand->fn = fn;
1311  cand->convs = convs;
1312  cand->viable = viable;
1313  cand->next = candidates;
1314
1315  return cand;
1316}
1317
1318/* Create an overload candidate for the function or method FN called with
1319   the argument list ARGLIST and add it to CANDIDATES.  FLAGS is passed on
1320   to implicit_conversion.
1321
1322   CTYPE, if non-NULL, is the type we want to pretend this function
1323   comes from for purposes of overload resolution.  */
1324
1325static struct z_candidate *
1326add_function_candidate (candidates, fn, ctype, arglist, flags)
1327     struct z_candidate *candidates;
1328     tree fn, ctype, arglist;
1329     int flags;
1330{
1331  tree parmlist = TYPE_ARG_TYPES (TREE_TYPE (fn));
1332  int i, len;
1333  tree convs;
1334  tree parmnode, argnode;
1335  int viable = 1;
1336
1337  /* The `this', `in_chrg' and VTT arguments to constructors are not
1338     considered in overload resolution.  */
1339  if (DECL_CONSTRUCTOR_P (fn))
1340    {
1341      parmlist = skip_artificial_parms_for (fn, parmlist);
1342      arglist = skip_artificial_parms_for (fn, arglist);
1343    }
1344
1345  len = list_length (arglist);
1346  convs = make_tree_vec (len);
1347
1348  /* 13.3.2 - Viable functions [over.match.viable]
1349     First, to be a viable function, a candidate function shall have enough
1350     parameters to agree in number with the arguments in the list.
1351
1352     We need to check this first; otherwise, checking the ICSes might cause
1353     us to produce an ill-formed template instantiation.  */
1354
1355  parmnode = parmlist;
1356  for (i = 0; i < len; ++i)
1357    {
1358      if (parmnode == NULL_TREE || parmnode == void_list_node)
1359	break;
1360      parmnode = TREE_CHAIN (parmnode);
1361    }
1362
1363  if (i < len && parmnode)
1364    viable = 0;
1365
1366  /* Make sure there are default args for the rest of the parms.  */
1367  else if (!sufficient_parms_p (parmnode))
1368    viable = 0;
1369
1370  if (! viable)
1371    goto out;
1372
1373  /* Second, for F to be a viable function, there shall exist for each
1374     argument an implicit conversion sequence that converts that argument
1375     to the corresponding parameter of F.  */
1376
1377  parmnode = parmlist;
1378  argnode = arglist;
1379
1380  for (i = 0; i < len; ++i)
1381    {
1382      tree arg = TREE_VALUE (argnode);
1383      tree argtype = lvalue_type (arg);
1384      tree t;
1385      int is_this;
1386
1387      if (parmnode == void_list_node)
1388	break;
1389
1390      is_this = (i == 0 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
1391		 && ! DECL_CONSTRUCTOR_P (fn));
1392
1393      if (parmnode)
1394	{
1395	  tree parmtype = TREE_VALUE (parmnode);
1396
1397	  /* The type of the implicit object parameter ('this') for
1398	     overload resolution is not always the same as for the
1399	     function itself; conversion functions are considered to
1400	     be members of the class being converted, and functions
1401	     introduced by a using-declaration are considered to be
1402	     members of the class that uses them.
1403
1404	     Since build_over_call ignores the ICS for the `this'
1405	     parameter, we can just change the parm type.  */
1406	  if (ctype && is_this)
1407	    {
1408	      parmtype
1409		= build_qualified_type (ctype,
1410					TYPE_QUALS (TREE_TYPE (parmtype)));
1411	      parmtype = build_pointer_type (parmtype);
1412	    }
1413
1414	  t = implicit_conversion (parmtype, argtype, arg, flags);
1415	}
1416      else
1417	{
1418	  t = build1 (IDENTITY_CONV, argtype, arg);
1419	  ICS_ELLIPSIS_FLAG (t) = 1;
1420	}
1421
1422      if (t && is_this)
1423	ICS_THIS_FLAG (t) = 1;
1424
1425      TREE_VEC_ELT (convs, i) = t;
1426      if (! t)
1427	{
1428	  viable = 0;
1429	  break;
1430	}
1431
1432      if (ICS_BAD_FLAG (t))
1433	viable = -1;
1434
1435      if (parmnode)
1436	parmnode = TREE_CHAIN (parmnode);
1437      argnode = TREE_CHAIN (argnode);
1438    }
1439
1440 out:
1441  return add_candidate (candidates, fn, convs, viable);
1442}
1443
1444/* Create an overload candidate for the conversion function FN which will
1445   be invoked for expression OBJ, producing a pointer-to-function which
1446   will in turn be called with the argument list ARGLIST, and add it to
1447   CANDIDATES.  FLAGS is passed on to implicit_conversion.
1448
1449   Actually, we don't really care about FN; we care about the type it
1450   converts to.  There may be multiple conversion functions that will
1451   convert to that type, and we rely on build_user_type_conversion_1 to
1452   choose the best one; so when we create our candidate, we record the type
1453   instead of the function.  */
1454
1455static struct z_candidate *
1456add_conv_candidate (candidates, fn, obj, arglist)
1457     struct z_candidate *candidates;
1458     tree fn, obj, arglist;
1459{
1460  tree totype = TREE_TYPE (TREE_TYPE (fn));
1461  int i, len, viable, flags;
1462  tree parmlist, convs, parmnode, argnode;
1463
1464  for (parmlist = totype; TREE_CODE (parmlist) != FUNCTION_TYPE; )
1465    parmlist = TREE_TYPE (parmlist);
1466  parmlist = TYPE_ARG_TYPES (parmlist);
1467
1468  len = list_length (arglist) + 1;
1469  convs = make_tree_vec (len);
1470  parmnode = parmlist;
1471  argnode = arglist;
1472  viable = 1;
1473  flags = LOOKUP_NORMAL;
1474
1475  /* Don't bother looking up the same type twice.  */
1476  if (candidates && candidates->fn == totype)
1477    return candidates;
1478
1479  for (i = 0; i < len; ++i)
1480    {
1481      tree arg = i == 0 ? obj : TREE_VALUE (argnode);
1482      tree argtype = lvalue_type (arg);
1483      tree t;
1484
1485      if (i == 0)
1486	t = implicit_conversion (totype, argtype, arg, flags);
1487      else if (parmnode == void_list_node)
1488	break;
1489      else if (parmnode)
1490	t = implicit_conversion (TREE_VALUE (parmnode), argtype, arg, flags);
1491      else
1492	{
1493	  t = build1 (IDENTITY_CONV, argtype, arg);
1494	  ICS_ELLIPSIS_FLAG (t) = 1;
1495	}
1496
1497      TREE_VEC_ELT (convs, i) = t;
1498      if (! t)
1499	break;
1500
1501      if (ICS_BAD_FLAG (t))
1502	viable = -1;
1503
1504      if (i == 0)
1505	continue;
1506
1507      if (parmnode)
1508	parmnode = TREE_CHAIN (parmnode);
1509      argnode = TREE_CHAIN (argnode);
1510    }
1511
1512  if (i < len)
1513    viable = 0;
1514
1515  if (!sufficient_parms_p (parmnode))
1516    viable = 0;
1517
1518  return add_candidate (candidates, totype, convs, viable);
1519}
1520
1521static struct z_candidate *
1522build_builtin_candidate (candidates, fnname, type1, type2,
1523			 args, argtypes, flags)
1524     struct z_candidate *candidates;
1525     tree fnname, type1, type2, *args, *argtypes;
1526     int flags;
1527
1528{
1529  tree t, convs;
1530  int viable = 1, i;
1531  tree types[2];
1532
1533  types[0] = type1;
1534  types[1] = type2;
1535
1536  convs = make_tree_vec (args[2] ? 3 : (args[1] ? 2 : 1));
1537
1538  for (i = 0; i < 2; ++i)
1539    {
1540      if (! args[i])
1541	break;
1542
1543      t = implicit_conversion (types[i], argtypes[i], args[i], flags);
1544      if (! t)
1545	{
1546	  viable = 0;
1547	  /* We need something for printing the candidate.  */
1548	  t = build1 (IDENTITY_CONV, types[i], NULL_TREE);
1549	}
1550      else if (ICS_BAD_FLAG (t))
1551	viable = 0;
1552      TREE_VEC_ELT (convs, i) = t;
1553    }
1554
1555  /* For COND_EXPR we rearranged the arguments; undo that now.  */
1556  if (args[2])
1557    {
1558      TREE_VEC_ELT (convs, 2) = TREE_VEC_ELT (convs, 1);
1559      TREE_VEC_ELT (convs, 1) = TREE_VEC_ELT (convs, 0);
1560      t = implicit_conversion (boolean_type_node, argtypes[2], args[2], flags);
1561      if (t)
1562	TREE_VEC_ELT (convs, 0) = t;
1563      else
1564	viable = 0;
1565    }
1566
1567  return add_candidate (candidates, fnname, convs, viable);
1568}
1569
1570static int
1571is_complete (t)
1572     tree t;
1573{
1574  return COMPLETE_TYPE_P (complete_type (t));
1575}
1576
1577/* Returns non-zero if TYPE is a promoted arithmetic type.  */
1578
1579static int
1580promoted_arithmetic_type_p (type)
1581     tree type;
1582{
1583  /* [over.built]
1584
1585     In this section, the term promoted integral type is used to refer
1586     to those integral types which are preserved by integral promotion
1587     (including e.g.  int and long but excluding e.g.  char).
1588     Similarly, the term promoted arithmetic type refers to promoted
1589     integral types plus floating types.  */
1590  return ((INTEGRAL_TYPE_P (type)
1591	   && same_type_p (type_promotes_to (type), type))
1592	  || TREE_CODE (type) == REAL_TYPE);
1593}
1594
1595/* Create any builtin operator overload candidates for the operator in
1596   question given the converted operand types TYPE1 and TYPE2.  The other
1597   args are passed through from add_builtin_candidates to
1598   build_builtin_candidate.
1599
1600   TYPE1 and TYPE2 may not be permissible, and we must filter them.
1601   If CODE is requires candidates operands of the same type of the kind
1602   of which TYPE1 and TYPE2 are, we add both candidates
1603   CODE (TYPE1, TYPE1) and CODE (TYPE2, TYPE2).  */
1604
1605static struct z_candidate *
1606add_builtin_candidate (candidates, code, code2, fnname, type1, type2,
1607		       args, argtypes, flags)
1608     struct z_candidate *candidates;
1609     enum tree_code code, code2;
1610     tree fnname, type1, type2, *args, *argtypes;
1611     int flags;
1612{
1613  switch (code)
1614    {
1615    case POSTINCREMENT_EXPR:
1616    case POSTDECREMENT_EXPR:
1617      args[1] = integer_zero_node;
1618      type2 = integer_type_node;
1619      break;
1620    default:
1621      break;
1622    }
1623
1624  switch (code)
1625    {
1626
1627/* 4 For every pair T, VQ), where T is an arithmetic or  enumeration  type,
1628     and  VQ  is  either  volatile or empty, there exist candidate operator
1629     functions of the form
1630	     VQ T&   operator++(VQ T&);
1631	     T       operator++(VQ T&, int);
1632   5 For every pair T, VQ), where T is an enumeration type or an arithmetic
1633     type  other than bool, and VQ is either volatile or empty, there exist
1634     candidate operator functions of the form
1635	     VQ T&   operator--(VQ T&);
1636	     T       operator--(VQ T&, int);
1637   6 For every pair T, VQ), where T is  a  cv-qualified  or  cv-unqualified
1638     complete  object type, and VQ is either volatile or empty, there exist
1639     candidate operator functions of the form
1640	     T*VQ&   operator++(T*VQ&);
1641	     T*VQ&   operator--(T*VQ&);
1642	     T*      operator++(T*VQ&, int);
1643	     T*      operator--(T*VQ&, int);  */
1644
1645    case POSTDECREMENT_EXPR:
1646    case PREDECREMENT_EXPR:
1647      if (TREE_CODE (type1) == BOOLEAN_TYPE)
1648	return candidates;
1649    case POSTINCREMENT_EXPR:
1650    case PREINCREMENT_EXPR:
1651      if (ARITHMETIC_TYPE_P (type1) || TYPE_PTROB_P (type1))
1652	{
1653	  type1 = build_reference_type (type1);
1654	  break;
1655	}
1656      return candidates;
1657
1658/* 7 For every cv-qualified or cv-unqualified complete object type T, there
1659     exist candidate operator functions of the form
1660
1661	     T&      operator*(T*);
1662
1663   8 For every function type T, there exist candidate operator functions of
1664     the form
1665	     T&      operator*(T*);  */
1666
1667    case INDIRECT_REF:
1668      if (TREE_CODE (type1) == POINTER_TYPE
1669	  && (TYPE_PTROB_P (type1)
1670	      || TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE))
1671	break;
1672      return candidates;
1673
1674/* 9 For every type T, there exist candidate operator functions of the form
1675	     T*      operator+(T*);
1676
1677   10For  every  promoted arithmetic type T, there exist candidate operator
1678     functions of the form
1679	     T       operator+(T);
1680	     T       operator-(T);  */
1681
1682    case CONVERT_EXPR: /* unary + */
1683      if (TREE_CODE (type1) == POINTER_TYPE
1684	  && TREE_CODE (TREE_TYPE (type1)) != OFFSET_TYPE)
1685	break;
1686    case NEGATE_EXPR:
1687      if (ARITHMETIC_TYPE_P (type1))
1688	break;
1689      return candidates;
1690
1691/* 11For every promoted integral type T,  there  exist  candidate  operator
1692     functions of the form
1693	     T       operator~(T);  */
1694
1695    case BIT_NOT_EXPR:
1696      if (INTEGRAL_TYPE_P (type1))
1697	break;
1698      return candidates;
1699
1700/* 12For every quintuple C1, C2, T, CV1, CV2), where C2 is a class type, C1
1701     is the same type as C2 or is a derived class of C2, T  is  a  complete
1702     object type or a function type, and CV1 and CV2 are cv-qualifier-seqs,
1703     there exist candidate operator functions of the form
1704	     CV12 T& operator->*(CV1 C1*, CV2 T C2::*);
1705     where CV12 is the union of CV1 and CV2.  */
1706
1707    case MEMBER_REF:
1708      if (TREE_CODE (type1) == POINTER_TYPE
1709	  && (TYPE_PTRMEMFUNC_P (type2) || TYPE_PTRMEM_P (type2)))
1710	{
1711	  tree c1 = TREE_TYPE (type1);
1712	  tree c2 = (TYPE_PTRMEMFUNC_P (type2)
1713		     ? TYPE_METHOD_BASETYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (type2)))
1714		     : TYPE_OFFSET_BASETYPE (TREE_TYPE (type2)));
1715
1716	  if (IS_AGGR_TYPE (c1) && DERIVED_FROM_P (c2, c1)
1717	      && (TYPE_PTRMEMFUNC_P (type2)
1718		  || is_complete (TREE_TYPE (TREE_TYPE (type2)))))
1719	    break;
1720	}
1721      return candidates;
1722
1723/* 13For every pair of promoted arithmetic types L and R, there exist  can-
1724     didate operator functions of the form
1725	     LR      operator*(L, R);
1726	     LR      operator/(L, R);
1727	     LR      operator+(L, R);
1728	     LR      operator-(L, R);
1729	     bool    operator<(L, R);
1730	     bool    operator>(L, R);
1731	     bool    operator<=(L, R);
1732	     bool    operator>=(L, R);
1733	     bool    operator==(L, R);
1734	     bool    operator!=(L, R);
1735     where  LR  is  the  result of the usual arithmetic conversions between
1736     types L and R.
1737
1738   14For every pair of types T and I, where T  is  a  cv-qualified  or  cv-
1739     unqualified  complete  object  type and I is a promoted integral type,
1740     there exist candidate operator functions of the form
1741	     T*      operator+(T*, I);
1742	     T&      operator[](T*, I);
1743	     T*      operator-(T*, I);
1744	     T*      operator+(I, T*);
1745	     T&      operator[](I, T*);
1746
1747   15For every T, where T is a pointer to complete object type, there exist
1748     candidate operator functions of the form112)
1749	     ptrdiff_t operator-(T, T);
1750
1751   16For every pointer or enumeration type T, there exist candidate operator
1752     functions of the form
1753	     bool    operator<(T, T);
1754	     bool    operator>(T, T);
1755	     bool    operator<=(T, T);
1756	     bool    operator>=(T, T);
1757	     bool    operator==(T, T);
1758	     bool    operator!=(T, T);
1759
1760   17For every pointer to member type T,  there  exist  candidate  operator
1761     functions of the form
1762	     bool    operator==(T, T);
1763	     bool    operator!=(T, T);  */
1764
1765    case MINUS_EXPR:
1766      if (TYPE_PTROB_P (type1) && TYPE_PTROB_P (type2))
1767	break;
1768      if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2))
1769	{
1770	  type2 = ptrdiff_type_node;
1771	  break;
1772	}
1773    case MULT_EXPR:
1774    case TRUNC_DIV_EXPR:
1775      if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1776	break;
1777      return candidates;
1778
1779    case EQ_EXPR:
1780    case NE_EXPR:
1781      if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2))
1782	  || (TYPE_PTRMEM_P (type1) && TYPE_PTRMEM_P (type2)))
1783	break;
1784      if ((TYPE_PTRMEMFUNC_P (type1) || TYPE_PTRMEM_P (type1))
1785	  && null_ptr_cst_p (args[1]))
1786	{
1787	  type2 = type1;
1788	  break;
1789	}
1790      if ((TYPE_PTRMEMFUNC_P (type2) || TYPE_PTRMEM_P (type2))
1791	  && null_ptr_cst_p (args[0]))
1792	{
1793	  type1 = type2;
1794	  break;
1795	}
1796      /* FALLTHROUGH */
1797    case LT_EXPR:
1798    case GT_EXPR:
1799    case LE_EXPR:
1800    case GE_EXPR:
1801    case MAX_EXPR:
1802    case MIN_EXPR:
1803      if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1804        break;
1805      if (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
1806	break;
1807      if (TREE_CODE (type1) == ENUMERAL_TYPE && TREE_CODE (type2) == ENUMERAL_TYPE)
1808        break;
1809      if (TYPE_PTR_P (type1) && null_ptr_cst_p (args[1]))
1810	{
1811	  type2 = type1;
1812	  break;
1813	}
1814      if (null_ptr_cst_p (args[0]) && TYPE_PTR_P (type2))
1815	{
1816	  type1 = type2;
1817	  break;
1818	}
1819      return candidates;
1820
1821    case PLUS_EXPR:
1822      if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1823	break;
1824    case ARRAY_REF:
1825      if (INTEGRAL_TYPE_P (type1) && TYPE_PTROB_P (type2))
1826	{
1827	  type1 = ptrdiff_type_node;
1828	  break;
1829	}
1830      if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2))
1831	{
1832	  type2 = ptrdiff_type_node;
1833	  break;
1834	}
1835      return candidates;
1836
1837/* 18For  every pair of promoted integral types L and R, there exist candi-
1838     date operator functions of the form
1839	     LR      operator%(L, R);
1840	     LR      operator&(L, R);
1841	     LR      operator^(L, R);
1842	     LR      operator|(L, R);
1843	     L       operator<<(L, R);
1844	     L       operator>>(L, R);
1845     where LR is the result of the  usual  arithmetic  conversions  between
1846     types L and R.  */
1847
1848    case TRUNC_MOD_EXPR:
1849    case BIT_AND_EXPR:
1850    case BIT_IOR_EXPR:
1851    case BIT_XOR_EXPR:
1852    case LSHIFT_EXPR:
1853    case RSHIFT_EXPR:
1854      if (INTEGRAL_TYPE_P (type1) && INTEGRAL_TYPE_P (type2))
1855	break;
1856      return candidates;
1857
1858/* 19For  every  triple  L, VQ, R), where L is an arithmetic or enumeration
1859     type, VQ is either volatile or empty, and R is a  promoted  arithmetic
1860     type, there exist candidate operator functions of the form
1861	     VQ L&   operator=(VQ L&, R);
1862	     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.  */
4026      warning ("cannot pass objects of non-POD type `%#T' through `...'",
4027		  TREE_TYPE (arg));
4028    }
4029
4030  return arg;
4031}
4032
4033/* va_arg (EXPR, TYPE) is a builtin. Make sure it is not abused.  */
4034
4035tree
4036build_x_va_arg (expr, type)
4037     tree expr;
4038     tree type;
4039{
4040  if (processing_template_decl)
4041    return build_min (VA_ARG_EXPR, type, expr);
4042
4043  type = complete_type_or_else (type, NULL_TREE);
4044
4045  if (expr == error_mark_node || !type)
4046    return error_mark_node;
4047
4048  if (! pod_type_p (type))
4049    {
4050      /* Undefined behaviour [expr.call] 5.2.2/7.  */
4051      warning ("cannot receive objects of non-POD type `%#T' through `...'",
4052		  type);
4053    }
4054
4055  return build_va_arg (expr, type);
4056}
4057
4058/* TYPE has been given to va_arg. Apply the default conversions which would
4059   have happened when passed via ellipsis. Return the promoted type, or
4060   NULL_TREE, if there is no change.  */
4061
4062tree
4063convert_type_from_ellipsis (type)
4064     tree type;
4065{
4066  tree promote;
4067
4068  if (TREE_CODE (type) == ARRAY_TYPE)
4069    promote = build_pointer_type (TREE_TYPE (type));
4070  else if (TREE_CODE (type) == FUNCTION_TYPE)
4071    promote = build_pointer_type (type);
4072  else
4073    promote = type_promotes_to (type);
4074
4075  return same_type_p (type, promote) ? NULL_TREE : promote;
4076}
4077
4078/* ARG is a default argument expression being passed to a parameter of
4079   the indicated TYPE, which is a parameter to FN.  Do any required
4080   conversions.  Return the converted value.  */
4081
4082tree
4083convert_default_arg (type, arg, fn, parmnum)
4084     tree type;
4085     tree arg;
4086     tree fn;
4087     int parmnum;
4088{
4089  if (TREE_CODE (arg) == DEFAULT_ARG)
4090    {
4091      /* When processing the default args for a class, we can find that
4092         there is an ordering constraint, and we call a function who's
4093         default args have not yet been converted. For instance,
4094          class A {
4095              A (int = 0);
4096              void Foo (A const & = A ());
4097          };
4098         We must process A::A before A::Foo's default arg can be converted.
4099         Remember the dependent function, so do_pending_defargs can retry,
4100         and check loops.  */
4101      unprocessed_defarg_fn (fn);
4102
4103      /* Don't return error_mark node, as we won't be able to distinguish
4104         genuine errors from this case, and that would lead to repeated
4105         diagnostics.  Just make something of the right type.  */
4106      return build1 (NOP_EXPR, type, integer_zero_node);
4107    }
4108
4109  if (fn && DECL_TEMPLATE_INFO (fn))
4110    arg = tsubst_default_argument (fn, type, arg);
4111
4112  arg = break_out_target_exprs (arg);
4113
4114  if (TREE_CODE (arg) == CONSTRUCTOR)
4115    {
4116      arg = digest_init (type, arg, 0);
4117      arg = convert_for_initialization (0, type, arg, LOOKUP_NORMAL,
4118					"default argument", fn, parmnum);
4119    }
4120  else
4121    {
4122      /* This could get clobbered by the following call.  */
4123      if (TREE_HAS_CONSTRUCTOR (arg))
4124	arg = copy_node (arg);
4125
4126      arg = convert_for_initialization (0, type, arg, LOOKUP_NORMAL,
4127					"default argument", fn, parmnum);
4128      if (PROMOTE_PROTOTYPES
4129	  && INTEGRAL_TYPE_P (type)
4130	  && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
4131	arg = default_conversion (arg);
4132    }
4133
4134  return arg;
4135}
4136
4137/* Subroutine of the various build_*_call functions.  Overload resolution
4138   has chosen a winning candidate CAND; build up a CALL_EXPR accordingly.
4139   ARGS is a TREE_LIST of the unconverted arguments to the call.  FLAGS is a
4140   bitmask of various LOOKUP_* flags which apply to the call itself.  */
4141
4142static tree
4143build_over_call (cand, args, flags)
4144     struct z_candidate *cand;
4145     tree args;
4146     int flags;
4147{
4148  tree fn = cand->fn;
4149  tree convs = cand->convs;
4150  tree converted_args = NULL_TREE;
4151  tree parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
4152  tree conv, arg, val;
4153  int i = 0;
4154  int is_method = 0;
4155
4156  /* Give any warnings we noticed during overload resolution.  */
4157  if (cand->warnings)
4158    for (val = cand->warnings; val; val = TREE_CHAIN (val))
4159      joust (cand, WRAPPER_PTR (TREE_VALUE (val)), 1);
4160
4161  if (DECL_FUNCTION_MEMBER_P (fn))
4162    enforce_access (cand->basetype_path, fn);
4163
4164  if (args && TREE_CODE (args) != TREE_LIST)
4165    args = build_tree_list (NULL_TREE, args);
4166  arg = args;
4167
4168  /* The implicit parameters to a constructor are not considered by overload
4169     resolution, and must be of the proper type.  */
4170  if (DECL_CONSTRUCTOR_P (fn))
4171    {
4172      converted_args = tree_cons (NULL_TREE, TREE_VALUE (arg), converted_args);
4173      arg = TREE_CHAIN (arg);
4174      parm = TREE_CHAIN (parm);
4175      if (DECL_HAS_IN_CHARGE_PARM_P (fn))
4176	/* We should never try to call the abstract constructor.  */
4177	abort ();
4178      if (DECL_HAS_VTT_PARM_P (fn))
4179	{
4180	  converted_args = tree_cons
4181	    (NULL_TREE, TREE_VALUE (arg), converted_args);
4182	  arg = TREE_CHAIN (arg);
4183	  parm = TREE_CHAIN (parm);
4184	}
4185    }
4186  /* Bypass access control for 'this' parameter.  */
4187  else if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
4188    {
4189      tree parmtype = TREE_VALUE (parm);
4190      tree argtype = TREE_TYPE (TREE_VALUE (arg));
4191      tree t;
4192      if (ICS_BAD_FLAG (TREE_VEC_ELT (convs, i)))
4193	pedwarn ("passing `%T' as `this' argument of `%#D' discards qualifiers",
4194		    TREE_TYPE (argtype), fn);
4195
4196      /* [class.mfct.nonstatic]: If a nonstatic member function of a class
4197	 X is called for an object that is not of type X, or of a type
4198	 derived from X, the behavior is undefined.
4199
4200         So we can assume that anything passed as 'this' is non-null, and
4201	 optimize accordingly.  */
4202      my_friendly_assert (TREE_CODE (parmtype) == POINTER_TYPE, 19990811);
4203      t = lookup_base (TREE_TYPE (TREE_TYPE (TREE_VALUE (arg))),
4204		       TREE_TYPE (parmtype), ba_ignore, NULL);
4205      t = build_base_path (PLUS_EXPR, TREE_VALUE (arg), t, 1);
4206      converted_args = tree_cons (NULL_TREE, t, converted_args);
4207      parm = TREE_CHAIN (parm);
4208      arg = TREE_CHAIN (arg);
4209      ++i;
4210      is_method = 1;
4211    }
4212
4213  for (; arg && parm;
4214       parm = TREE_CHAIN (parm), arg = TREE_CHAIN (arg), ++i)
4215    {
4216      tree type = TREE_VALUE (parm);
4217
4218      conv = TREE_VEC_ELT (convs, i);
4219      val = convert_like_with_context
4220	(conv, TREE_VALUE (arg), fn, i - is_method);
4221
4222      if (PROMOTE_PROTOTYPES
4223	  && INTEGRAL_TYPE_P (type)
4224	  && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
4225	val = default_conversion (val);
4226      converted_args = tree_cons (NULL_TREE, val, converted_args);
4227    }
4228
4229  /* Default arguments */
4230  for (; parm && parm != void_list_node; parm = TREE_CHAIN (parm), i++)
4231    converted_args
4232      = tree_cons (NULL_TREE,
4233		   convert_default_arg (TREE_VALUE (parm),
4234					TREE_PURPOSE (parm),
4235					fn, i - is_method),
4236		   converted_args);
4237
4238  /* Ellipsis */
4239  for (; arg; arg = TREE_CHAIN (arg))
4240    converted_args
4241      = tree_cons (NULL_TREE,
4242		   convert_arg_to_ellipsis (TREE_VALUE (arg)),
4243		   converted_args);
4244
4245  converted_args = nreverse (converted_args);
4246
4247  if (warn_format)
4248    check_function_format (NULL, TYPE_ATTRIBUTES (TREE_TYPE (fn)),
4249			   converted_args);
4250
4251  /* Avoid actually calling copy constructors and copy assignment operators,
4252     if possible.  */
4253
4254  if (! flag_elide_constructors)
4255    /* Do things the hard way.  */;
4256  else if (TREE_VEC_LENGTH (convs) == 1
4257	   && DECL_COPY_CONSTRUCTOR_P (fn))
4258    {
4259      tree targ;
4260      arg = skip_artificial_parms_for (fn, converted_args);
4261      arg = TREE_VALUE (arg);
4262
4263      /* Pull out the real argument, disregarding const-correctness.  */
4264      targ = arg;
4265      while (TREE_CODE (targ) == NOP_EXPR
4266	     || TREE_CODE (targ) == NON_LVALUE_EXPR
4267	     || TREE_CODE (targ) == CONVERT_EXPR)
4268	targ = TREE_OPERAND (targ, 0);
4269      if (TREE_CODE (targ) == ADDR_EXPR)
4270	{
4271	  targ = TREE_OPERAND (targ, 0);
4272	  if (!same_type_ignoring_top_level_qualifiers_p
4273	      (TREE_TYPE (TREE_TYPE (arg)), TREE_TYPE (targ)))
4274	    targ = NULL_TREE;
4275	}
4276      else
4277	targ = NULL_TREE;
4278
4279      if (targ)
4280	arg = targ;
4281      else
4282	arg = build_indirect_ref (arg, 0);
4283
4284      /* [class.copy]: the copy constructor is implicitly defined even if
4285	 the implementation elided its use.  */
4286      if (TYPE_HAS_COMPLEX_INIT_REF (DECL_CONTEXT (fn)))
4287	mark_used (fn);
4288
4289      /* If we're creating a temp and we already have one, don't create a
4290         new one.  If we're not creating a temp but we get one, use
4291         INIT_EXPR to collapse the temp into our target.  Otherwise, if the
4292         ctor is trivial, do a bitwise copy with a simple TARGET_EXPR for a
4293         temp or an INIT_EXPR otherwise.  */
4294      if (integer_zerop (TREE_VALUE (args)))
4295	{
4296	  if (! real_lvalue_p (arg))
4297	    return arg;
4298	  else if (TYPE_HAS_TRIVIAL_INIT_REF (DECL_CONTEXT (fn)))
4299	    return build_target_expr_with_type (arg, DECL_CONTEXT (fn));
4300	}
4301      else if ((!real_lvalue_p (arg)
4302		|| TYPE_HAS_TRIVIAL_INIT_REF (DECL_CONTEXT (fn)))
4303	       /* Empty classes have padding which can be hidden
4304	          inside an (empty) base of the class. This must not
4305	          be touched as it might overlay things. When the
4306	          gcc core learns about empty classes, we can treat it
4307	          like other classes. */
4308	       && !(is_empty_class (DECL_CONTEXT (fn))
4309		    && TYPE_HAS_TRIVIAL_INIT_REF (DECL_CONTEXT (fn))))
4310	{
4311	  tree address;
4312	  tree to = stabilize_reference
4313	    (build_indirect_ref (TREE_VALUE (args), 0));
4314
4315	  val = build (INIT_EXPR, DECL_CONTEXT (fn), to, arg);
4316	  address = build_unary_op (ADDR_EXPR, val, 0);
4317	  /* Avoid a warning about this expression, if the address is
4318	     never used.  */
4319	  TREE_USED (address) = 1;
4320	  return address;
4321	}
4322    }
4323  else if (DECL_OVERLOADED_OPERATOR_P (fn) == NOP_EXPR
4324	   && copy_fn_p (fn)
4325	   && TYPE_HAS_TRIVIAL_ASSIGN_REF (DECL_CONTEXT (fn)))
4326    {
4327      tree to = stabilize_reference
4328	(build_indirect_ref (TREE_VALUE (converted_args), 0));
4329
4330      arg = build_indirect_ref (TREE_VALUE (TREE_CHAIN (converted_args)), 0);
4331      if (is_empty_class (TREE_TYPE (to)))
4332	{
4333	  TREE_USED (arg) = 1;
4334
4335	  val = build (COMPOUND_EXPR, DECL_CONTEXT (fn), arg, to);
4336	  /* Even though the assignment may not actually result in any
4337	     code being generated, we do not want to warn about the
4338	     assignment having no effect.  That would be confusing to
4339	     users who may be performing the assignment as part of a
4340	     generic algorithm, for example.
4341
4342	     Ideally, the notions of having side-effects and of being
4343	     useless would be orthogonal.  */
4344	  TREE_SIDE_EFFECTS (val) = 1;
4345	  TREE_NO_UNUSED_WARNING (val) = 1;
4346	}
4347      else
4348	val = build (MODIFY_EXPR, TREE_TYPE (to), to, arg);
4349      return val;
4350    }
4351
4352  mark_used (fn);
4353
4354  if (DECL_VINDEX (fn) && (flags & LOOKUP_NONVIRTUAL) == 0)
4355    {
4356      tree t, *p = &TREE_VALUE (converted_args);
4357      tree binfo = lookup_base (TREE_TYPE (TREE_TYPE (*p)),
4358				DECL_VIRTUAL_CONTEXT (fn),
4359				ba_any, NULL);
4360      my_friendly_assert (binfo && binfo != error_mark_node, 20010730);
4361
4362      *p = build_base_path (PLUS_EXPR, *p, binfo, 1);
4363      if (TREE_SIDE_EFFECTS (*p))
4364	*p = save_expr (*p);
4365      t = build_pointer_type (TREE_TYPE (fn));
4366      if (DECL_CONTEXT (fn) && TYPE_JAVA_INTERFACE (DECL_CONTEXT (fn)))
4367	fn = build_java_interface_fn_ref (fn, *p);
4368      else
4369	fn = build_vfn_ref (build_indirect_ref (*p, 0), DECL_VINDEX (fn));
4370      TREE_TYPE (fn) = t;
4371    }
4372  else if (DECL_INLINE (fn))
4373    fn = inline_conversion (fn);
4374  else
4375    fn = build_addr_func (fn);
4376
4377  /* Recognize certain built-in functions so we can make tree-codes
4378     other than CALL_EXPR.  We do this when it enables fold-const.c
4379     to do something useful.  */
4380
4381  if (TREE_CODE (fn) == ADDR_EXPR
4382      && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL
4383      && DECL_BUILT_IN (TREE_OPERAND (fn, 0)))
4384    {
4385      tree exp;
4386      exp = expand_tree_builtin (TREE_OPERAND (fn, 0), args, converted_args);
4387      if (exp)
4388	return exp;
4389    }
4390
4391  /* Some built-in function calls will be evaluated at
4392     compile-time in fold ().  */
4393  fn = fold (build_call (fn, converted_args));
4394  if (VOID_TYPE_P (TREE_TYPE (fn)))
4395    return fn;
4396  fn = require_complete_type (fn);
4397  if (fn == error_mark_node)
4398    return error_mark_node;
4399  if (IS_AGGR_TYPE (TREE_TYPE (fn)))
4400    fn = build_cplus_new (TREE_TYPE (fn), fn);
4401  return convert_from_reference (fn);
4402}
4403
4404static tree java_iface_lookup_fn;
4405
4406/* Make an expression which yields the address of the Java interface
4407   method FN.  This is achieved by generating a call to libjava's
4408   _Jv_LookupInterfaceMethodIdx().  */
4409
4410static tree
4411build_java_interface_fn_ref (fn, instance)
4412    tree fn, instance;
4413{
4414  tree lookup_args, lookup_fn, method, idx;
4415  tree klass_ref, iface, iface_ref;
4416  int i;
4417
4418  if (!java_iface_lookup_fn)
4419    {
4420      tree endlink = build_void_list_node ();
4421      tree t = tree_cons (NULL_TREE, ptr_type_node,
4422			  tree_cons (NULL_TREE, ptr_type_node,
4423				     tree_cons (NULL_TREE, java_int_type_node,
4424						endlink)));
4425      java_iface_lookup_fn
4426	= builtin_function ("_Jv_LookupInterfaceMethodIdx",
4427			    build_function_type (ptr_type_node, t),
4428			    0, NOT_BUILT_IN, NULL);
4429      ggc_add_tree_root (&java_iface_lookup_fn, 1);
4430    }
4431
4432  /* Look up the pointer to the runtime java.lang.Class object for `instance'.
4433     This is the first entry in the vtable. */
4434  klass_ref = build_vtbl_ref (build_indirect_ref (instance, 0),
4435			      integer_zero_node);
4436
4437  /* Get the java.lang.Class pointer for the interface being called. */
4438  iface = DECL_CONTEXT (fn);
4439  iface_ref = lookup_field (iface, get_identifier ("class$"), 0, 0);
4440  if (!iface_ref || TREE_CODE (iface_ref) != VAR_DECL
4441      || DECL_CONTEXT (iface_ref) != iface)
4442    {
4443      error ("could not find class$ field in java interface type `%T'",
4444		iface);
4445      return error_mark_node;
4446    }
4447  iface_ref = build1 (ADDR_EXPR, build_pointer_type (iface), iface_ref);
4448
4449  /* Determine the itable index of FN. */
4450  i = 1;
4451  for (method = TYPE_METHODS (iface); method; method = TREE_CHAIN (method))
4452    {
4453      if (!DECL_VIRTUAL_P (method))
4454        continue;
4455      if (fn == method)
4456        break;
4457      i++;
4458    }
4459  idx = build_int_2 (i, 0);
4460
4461  lookup_args = tree_cons (NULL_TREE, klass_ref,
4462			   tree_cons (NULL_TREE, iface_ref,
4463				      build_tree_list (NULL_TREE, idx)));
4464  lookup_fn = build1 (ADDR_EXPR,
4465		      build_pointer_type (TREE_TYPE (java_iface_lookup_fn)),
4466		      java_iface_lookup_fn);
4467  return build (CALL_EXPR, ptr_type_node, lookup_fn, lookup_args, NULL_TREE);
4468}
4469
4470/* Returns the value to use for the in-charge parameter when making a
4471   call to a function with the indicated NAME.  */
4472
4473tree
4474in_charge_arg_for_name (name)
4475     tree name;
4476{
4477  if (name == base_ctor_identifier
4478      || name == base_dtor_identifier)
4479    return integer_zero_node;
4480  else if (name == complete_ctor_identifier)
4481    return integer_one_node;
4482  else if (name == complete_dtor_identifier)
4483    return integer_two_node;
4484  else if (name == deleting_dtor_identifier)
4485    return integer_three_node;
4486
4487  /* This function should only be called with one of the names listed
4488     above.  */
4489  abort ();
4490  return NULL_TREE;
4491}
4492
4493static tree
4494build_new_method_call (instance, name, args, basetype_path, flags)
4495     tree instance, name, args, basetype_path;
4496     int flags;
4497{
4498  struct z_candidate *candidates = 0, *cand;
4499  tree explicit_targs = NULL_TREE;
4500  tree basetype, mem_args = NULL_TREE, fns, instance_ptr;
4501  tree pretty_name;
4502  tree user_args;
4503  tree templates = NULL_TREE;
4504  tree call;
4505  int template_only = 0;
4506
4507  if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
4508    {
4509      explicit_targs = TREE_OPERAND (name, 1);
4510      name = TREE_OPERAND (name, 0);
4511      if (DECL_P (name))
4512	name = DECL_NAME (name);
4513      else
4514	{
4515	  if (TREE_CODE (name) == COMPONENT_REF)
4516	    name = TREE_OPERAND (name, 1);
4517	  if (TREE_CODE (name) == OVERLOAD)
4518	    name = DECL_NAME (OVL_CURRENT (name));
4519	}
4520
4521      template_only = 1;
4522    }
4523
4524  user_args = args;
4525  args = resolve_args (args);
4526
4527  if (args == error_mark_node)
4528    return error_mark_node;
4529
4530  if (instance == NULL_TREE)
4531    basetype = BINFO_TYPE (basetype_path);
4532  else
4533    {
4534      if (TREE_CODE (instance) == OFFSET_REF)
4535	instance = resolve_offset_ref (instance);
4536      if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
4537	instance = convert_from_reference (instance);
4538      basetype = TYPE_MAIN_VARIANT (TREE_TYPE (instance));
4539
4540      /* XXX this should be handled before we get here.  */
4541      if (! IS_AGGR_TYPE (basetype))
4542	{
4543	  if ((flags & LOOKUP_COMPLAIN) && basetype != error_mark_node)
4544	    error ("request for member `%D' in `%E', which is of non-aggregate type `%T'",
4545		      name, instance, basetype);
4546
4547	  return error_mark_node;
4548	}
4549    }
4550
4551  if (basetype_path == NULL_TREE)
4552    basetype_path = TYPE_BINFO (basetype);
4553
4554  if (instance)
4555    {
4556      instance_ptr = build_this (instance);
4557
4558      if (! template_only)
4559	{
4560	  /* XXX this should be handled before we get here.  */
4561	  fns = build_field_call (basetype_path, instance_ptr, name, args);
4562	  if (fns)
4563	    return fns;
4564	}
4565    }
4566  else
4567    {
4568      instance_ptr = build_int_2 (0, 0);
4569      TREE_TYPE (instance_ptr) = build_pointer_type (basetype);
4570    }
4571
4572  /* Callers should explicitly indicate whether they want to construct
4573     the complete object or just the part without virtual bases.  */
4574  my_friendly_assert (name != ctor_identifier, 20000408);
4575  /* Similarly for destructors.  */
4576  my_friendly_assert (name != dtor_identifier, 20000408);
4577
4578  if (IDENTIFIER_CTOR_OR_DTOR_P (name))
4579    {
4580      int constructor_p;
4581
4582      constructor_p = (name == complete_ctor_identifier
4583		       || name == base_ctor_identifier);
4584      pretty_name = (constructor_p
4585		     ? constructor_name (basetype) : dtor_identifier);
4586
4587      /* If we're a call to a constructor or destructor for a
4588	 subobject that uses virtual base classes, then we need to
4589	 pass down a pointer to a VTT for the subobject.  */
4590      if ((name == base_ctor_identifier
4591	   || name == base_dtor_identifier)
4592	  && TYPE_USES_VIRTUAL_BASECLASSES (basetype))
4593	{
4594	  tree vtt;
4595	  tree sub_vtt;
4596	  tree basebinfo = basetype_path;
4597
4598	  /* If the current function is a complete object constructor
4599	     or destructor, then we fetch the VTT directly.
4600	     Otherwise, we look it up using the VTT we were given.  */
4601	  vtt = IDENTIFIER_GLOBAL_VALUE (get_vtt_name (current_class_type));
4602	  vtt = decay_conversion (vtt);
4603	  vtt = build (COND_EXPR, TREE_TYPE (vtt),
4604		       build (EQ_EXPR, boolean_type_node,
4605			      current_in_charge_parm, integer_zero_node),
4606		       current_vtt_parm,
4607		       vtt);
4608	  if (TREE_VIA_VIRTUAL (basebinfo))
4609	    basebinfo = binfo_for_vbase (basetype, current_class_type);
4610	  my_friendly_assert (BINFO_SUBVTT_INDEX (basebinfo), 20010110);
4611	  sub_vtt = build (PLUS_EXPR, TREE_TYPE (vtt), vtt,
4612			   BINFO_SUBVTT_INDEX (basebinfo));
4613
4614	  args = tree_cons (NULL_TREE, sub_vtt, args);
4615	}
4616    }
4617  else
4618    pretty_name = name;
4619
4620  fns = lookup_fnfields (basetype_path, name, 1);
4621
4622  if (fns == error_mark_node)
4623    return error_mark_node;
4624  if (fns)
4625    {
4626      tree base = BINFO_TYPE (TREE_PURPOSE (fns));
4627      tree fn = TREE_VALUE (fns);
4628      mem_args = tree_cons (NULL_TREE, instance_ptr, args);
4629      for (; fn; fn = OVL_NEXT (fn))
4630	{
4631	  tree t = OVL_CURRENT (fn);
4632	  tree this_arglist;
4633
4634	  /* We can end up here for copy-init of same or base class.  */
4635	  if ((flags & LOOKUP_ONLYCONVERTING)
4636	      && DECL_NONCONVERTING_P (t))
4637	    continue;
4638
4639	  if (DECL_NONSTATIC_MEMBER_FUNCTION_P (t))
4640	    this_arglist = mem_args;
4641	  else
4642	    this_arglist = args;
4643
4644	  if (TREE_CODE (t) == TEMPLATE_DECL)
4645	    {
4646	      /* A member template. */
4647	      templates = tree_cons (NULL_TREE, t, templates);
4648	      candidates =
4649		add_template_candidate (candidates, t, base, explicit_targs,
4650					this_arglist,
4651					TREE_TYPE (name), flags, DEDUCE_CALL);
4652	    }
4653	  else if (! template_only)
4654	    candidates = add_function_candidate (candidates, t, base,
4655						 this_arglist, flags);
4656
4657	  if (candidates)
4658	    candidates->basetype_path = basetype_path;
4659	}
4660    }
4661
4662  if (! any_viable (candidates))
4663    {
4664      /* XXX will LOOKUP_SPECULATIVELY be needed when this is done?  */
4665      if (flags & LOOKUP_SPECULATIVELY)
4666	return NULL_TREE;
4667      if (!COMPLETE_TYPE_P (basetype))
4668	incomplete_type_error (instance_ptr, basetype);
4669      else
4670	error ("no matching function for call to `%T::%D(%A)%#V'",
4671	       basetype, pretty_name, user_args,
4672	       TREE_TYPE (TREE_TYPE (instance_ptr)));
4673      print_z_candidates (candidates);
4674      return error_mark_node;
4675    }
4676  candidates = splice_viable (candidates);
4677  cand = tourney (candidates);
4678
4679  if (cand == 0)
4680    {
4681      error ("call of overloaded `%D(%A)' is ambiguous", pretty_name,
4682		user_args);
4683      print_z_candidates (candidates);
4684      return error_mark_node;
4685    }
4686
4687  if (DECL_PURE_VIRTUAL_P (cand->fn)
4688      && instance == current_class_ref
4689      && (DECL_CONSTRUCTOR_P (current_function_decl)
4690	  || DECL_DESTRUCTOR_P (current_function_decl))
4691      && ! (flags & LOOKUP_NONVIRTUAL)
4692      && value_member (cand->fn, CLASSTYPE_PURE_VIRTUALS (basetype)))
4693    error ((DECL_CONSTRUCTOR_P (current_function_decl) ?
4694	       "abstract virtual `%#D' called from constructor"
4695	       : "abstract virtual `%#D' called from destructor"),
4696	      cand->fn);
4697  if (TREE_CODE (TREE_TYPE (cand->fn)) == METHOD_TYPE
4698      && is_dummy_object (instance_ptr))
4699    {
4700      error ("cannot call member function `%D' without object", cand->fn);
4701      return error_mark_node;
4702    }
4703
4704  if (DECL_VINDEX (cand->fn) && ! (flags & LOOKUP_NONVIRTUAL)
4705      && resolves_to_fixed_type_p (instance, 0))
4706    flags |= LOOKUP_NONVIRTUAL;
4707
4708  if (TREE_CODE (TREE_TYPE (cand->fn)) == METHOD_TYPE)
4709    call = build_over_call (cand, mem_args, flags);
4710  else
4711    {
4712      call = build_over_call (cand, args, flags);
4713      /* Do evaluate the object parameter in a call to a static member
4714	 function.  */
4715      if (TREE_SIDE_EFFECTS (instance))
4716	call = build (COMPOUND_EXPR, TREE_TYPE (call), instance, call);
4717    }
4718
4719  return call;
4720}
4721
4722/* Returns non-zero iff standard conversion sequence ICS1 is a proper
4723   subsequence of ICS2.  */
4724
4725static int
4726is_subseq (ics1, ics2)
4727     tree ics1, ics2;
4728{
4729  /* We can assume that a conversion of the same code
4730     between the same types indicates a subsequence since we only get
4731     here if the types we are converting from are the same.  */
4732
4733  while (TREE_CODE (ics1) == RVALUE_CONV
4734	 || TREE_CODE (ics1) == LVALUE_CONV)
4735    ics1 = TREE_OPERAND (ics1, 0);
4736
4737  while (1)
4738    {
4739      while (TREE_CODE (ics2) == RVALUE_CONV
4740	  || TREE_CODE (ics2) == LVALUE_CONV)
4741	ics2 = TREE_OPERAND (ics2, 0);
4742
4743      if (TREE_CODE (ics2) == USER_CONV
4744	  || TREE_CODE (ics2) == AMBIG_CONV
4745	  || TREE_CODE (ics2) == IDENTITY_CONV)
4746	/* At this point, ICS1 cannot be a proper subsequence of
4747	   ICS2.  We can get a USER_CONV when we are comparing the
4748	   second standard conversion sequence of two user conversion
4749	   sequences.  */
4750	return 0;
4751
4752      ics2 = TREE_OPERAND (ics2, 0);
4753
4754      if (TREE_CODE (ics2) == TREE_CODE (ics1)
4755	  && same_type_p (TREE_TYPE (ics2), TREE_TYPE (ics1))
4756	  && same_type_p (TREE_TYPE (TREE_OPERAND (ics2, 0)),
4757			     TREE_TYPE (TREE_OPERAND (ics1, 0))))
4758	return 1;
4759    }
4760}
4761
4762/* Returns non-zero iff DERIVED is derived from BASE.  The inputs may
4763   be any _TYPE nodes.  */
4764
4765int
4766is_properly_derived_from (derived, base)
4767     tree derived;
4768     tree base;
4769{
4770  if (!IS_AGGR_TYPE_CODE (TREE_CODE (derived))
4771      || !IS_AGGR_TYPE_CODE (TREE_CODE (base)))
4772    return 0;
4773
4774  /* We only allow proper derivation here.  The DERIVED_FROM_P macro
4775     considers every class derived from itself.  */
4776  return (!same_type_ignoring_top_level_qualifiers_p (derived, base)
4777	  && DERIVED_FROM_P (base, derived));
4778}
4779
4780/* We build the ICS for an implicit object parameter as a pointer
4781   conversion sequence.  However, such a sequence should be compared
4782   as if it were a reference conversion sequence.  If ICS is the
4783   implicit conversion sequence for an implicit object parameter,
4784   modify it accordingly.  */
4785
4786static void
4787maybe_handle_implicit_object (ics)
4788     tree* ics;
4789{
4790  if (ICS_THIS_FLAG (*ics))
4791    {
4792      /* [over.match.funcs]
4793
4794	 For non-static member functions, the type of the
4795	 implicit object parameter is "reference to cv X"
4796	 where X is the class of which the function is a
4797	 member and cv is the cv-qualification on the member
4798	 function declaration.  */
4799      tree t = *ics;
4800      tree reference_type;
4801
4802      /* The `this' parameter is a pointer to a class type.  Make the
4803	 implict conversion talk about a reference to that same class
4804	 type.  */
4805      reference_type = TREE_TYPE (TREE_TYPE (*ics));
4806      reference_type = build_reference_type (reference_type);
4807
4808      if (TREE_CODE (t) == QUAL_CONV)
4809	t = TREE_OPERAND (t, 0);
4810      if (TREE_CODE (t) == PTR_CONV)
4811	t = TREE_OPERAND (t, 0);
4812      t = build1 (IDENTITY_CONV, TREE_TYPE (TREE_TYPE (t)), NULL_TREE);
4813      t = direct_reference_binding (reference_type, t);
4814      *ics = t;
4815    }
4816}
4817
4818/* If *ICS is a REF_BIND set *ICS to the remainder of the conversion,
4819   and return the type to which the reference refers.  Otherwise,
4820   leave *ICS unchanged and return NULL_TREE.  */
4821
4822static tree
4823maybe_handle_ref_bind (ics)
4824     tree* ics;
4825{
4826  if (TREE_CODE (*ics) == REF_BIND)
4827    {
4828      tree old_ics = *ics;
4829      tree type = TREE_TYPE (TREE_TYPE (old_ics));
4830      *ics = TREE_OPERAND (old_ics, 0);
4831      ICS_USER_FLAG (*ics) = ICS_USER_FLAG (old_ics);
4832      ICS_BAD_FLAG (*ics) = ICS_BAD_FLAG (old_ics);
4833      return type;
4834    }
4835
4836  return NULL_TREE;
4837}
4838
4839/* Compare two implicit conversion sequences according to the rules set out in
4840   [over.ics.rank].  Return values:
4841
4842      1: ics1 is better than ics2
4843     -1: ics2 is better than ics1
4844      0: ics1 and ics2 are indistinguishable */
4845
4846static int
4847compare_ics (ics1, ics2)
4848     tree ics1, ics2;
4849{
4850  tree from_type1;
4851  tree from_type2;
4852  tree to_type1;
4853  tree to_type2;
4854  tree deref_from_type1 = NULL_TREE;
4855  tree deref_from_type2 = NULL_TREE;
4856  tree deref_to_type1 = NULL_TREE;
4857  tree deref_to_type2 = NULL_TREE;
4858  int rank1, rank2;
4859
4860  /* REF_BINDING is non-zero if the result of the conversion sequence
4861     is a reference type.   In that case TARGET_TYPE is the
4862     type referred to by the reference.  */
4863  tree target_type1;
4864  tree target_type2;
4865
4866  /* Handle implicit object parameters.  */
4867  maybe_handle_implicit_object (&ics1);
4868  maybe_handle_implicit_object (&ics2);
4869
4870  /* Handle reference parameters.  */
4871  target_type1 = maybe_handle_ref_bind (&ics1);
4872  target_type2 = maybe_handle_ref_bind (&ics2);
4873
4874  /* [over.ics.rank]
4875
4876     When  comparing  the  basic forms of implicit conversion sequences (as
4877     defined in _over.best.ics_)
4878
4879     --a standard conversion sequence (_over.ics.scs_) is a better
4880       conversion sequence than a user-defined conversion sequence
4881       or an ellipsis conversion sequence, and
4882
4883     --a user-defined conversion sequence (_over.ics.user_) is a
4884       better conversion sequence than an ellipsis conversion sequence
4885       (_over.ics.ellipsis_).  */
4886  rank1 = ICS_RANK (ics1);
4887  rank2 = ICS_RANK (ics2);
4888
4889  if (rank1 > rank2)
4890    return -1;
4891  else if (rank1 < rank2)
4892    return 1;
4893
4894  if (rank1 == BAD_RANK)
4895    {
4896      /* XXX Isn't this an extension? */
4897      /* Both ICS are bad.  We try to make a decision based on what
4898	 would have happenned if they'd been good.  */
4899      if (ICS_USER_FLAG (ics1) > ICS_USER_FLAG (ics2)
4900	  || ICS_STD_RANK (ics1) > ICS_STD_RANK (ics2))
4901	return -1;
4902      else if (ICS_USER_FLAG (ics1) < ICS_USER_FLAG (ics2)
4903	       || ICS_STD_RANK (ics1) < ICS_STD_RANK (ics2))
4904	return 1;
4905
4906      /* We couldn't make up our minds; try to figure it out below.  */
4907    }
4908
4909  if (ICS_ELLIPSIS_FLAG (ics1))
4910    /* Both conversions are ellipsis conversions.  */
4911    return 0;
4912
4913  /* User-defined  conversion sequence U1 is a better conversion sequence
4914     than another user-defined conversion sequence U2 if they contain the
4915     same user-defined conversion operator or constructor and if the sec-
4916     ond standard conversion sequence of U1 is  better  than  the  second
4917     standard conversion sequence of U2.  */
4918
4919  if (ICS_USER_FLAG (ics1))
4920    {
4921      tree t1, t2;
4922
4923      for (t1 = ics1; TREE_CODE (t1) != USER_CONV; t1 = TREE_OPERAND (t1, 0))
4924	if (TREE_CODE (t1) == AMBIG_CONV)
4925	  return 0;
4926      for (t2 = ics2; TREE_CODE (t2) != USER_CONV; t2 = TREE_OPERAND (t2, 0))
4927	if (TREE_CODE (t2) == AMBIG_CONV)
4928	  return 0;
4929
4930      if (USER_CONV_FN (t1) != USER_CONV_FN (t2))
4931	return 0;
4932
4933      /* We can just fall through here, after setting up
4934	 FROM_TYPE1 and FROM_TYPE2.  */
4935      from_type1 = TREE_TYPE (t1);
4936      from_type2 = TREE_TYPE (t2);
4937    }
4938  else
4939    {
4940      /* We're dealing with two standard conversion sequences.
4941
4942	 [over.ics.rank]
4943
4944	 Standard conversion sequence S1 is a better conversion
4945	 sequence than standard conversion sequence S2 if
4946
4947	 --S1 is a proper subsequence of S2 (comparing the conversion
4948	   sequences in the canonical form defined by _over.ics.scs_,
4949	   excluding any Lvalue Transformation; the identity
4950	   conversion sequence is considered to be a subsequence of
4951	   any non-identity conversion sequence */
4952
4953      from_type1 = ics1;
4954      while (TREE_CODE (from_type1) != IDENTITY_CONV)
4955	from_type1 = TREE_OPERAND (from_type1, 0);
4956      from_type1 = TREE_TYPE (from_type1);
4957
4958      from_type2 = ics2;
4959      while (TREE_CODE (from_type2) != IDENTITY_CONV)
4960	from_type2 = TREE_OPERAND (from_type2, 0);
4961      from_type2 = TREE_TYPE (from_type2);
4962    }
4963
4964  if (same_type_p (from_type1, from_type2))
4965    {
4966      if (is_subseq (ics1, ics2))
4967	return 1;
4968      if (is_subseq (ics2, ics1))
4969	return -1;
4970    }
4971  /* Otherwise, one sequence cannot be a subsequence of the other; they
4972     don't start with the same type.  This can happen when comparing the
4973     second standard conversion sequence in two user-defined conversion
4974     sequences.  */
4975
4976  /* [over.ics.rank]
4977
4978     Or, if not that,
4979
4980     --the rank of S1 is better than the rank of S2 (by the rules
4981       defined below):
4982
4983    Standard conversion sequences are ordered by their ranks: an Exact
4984    Match is a better conversion than a Promotion, which is a better
4985    conversion than a Conversion.
4986
4987    Two conversion sequences with the same rank are indistinguishable
4988    unless one of the following rules applies:
4989
4990    --A conversion that is not a conversion of a pointer, or pointer
4991      to member, to bool is better than another conversion that is such
4992      a conversion.
4993
4994    The ICS_STD_RANK automatically handles the pointer-to-bool rule,
4995    so that we do not have to check it explicitly.  */
4996  if (ICS_STD_RANK (ics1) < ICS_STD_RANK (ics2))
4997    return 1;
4998  else if (ICS_STD_RANK (ics2) < ICS_STD_RANK (ics1))
4999    return -1;
5000
5001  to_type1 = TREE_TYPE (ics1);
5002  to_type2 = TREE_TYPE (ics2);
5003
5004  if (TYPE_PTR_P (from_type1)
5005      && TYPE_PTR_P (from_type2)
5006      && TYPE_PTR_P (to_type1)
5007      && TYPE_PTR_P (to_type2))
5008    {
5009      deref_from_type1 = TREE_TYPE (from_type1);
5010      deref_from_type2 = TREE_TYPE (from_type2);
5011      deref_to_type1 = TREE_TYPE (to_type1);
5012      deref_to_type2 = TREE_TYPE (to_type2);
5013    }
5014  /* The rules for pointers to members A::* are just like the rules
5015     for pointers A*, except opposite: if B is derived from A then
5016     A::* converts to B::*, not vice versa.  For that reason, we
5017     switch the from_ and to_ variables here.  */
5018  else if (TYPE_PTRMEM_P (from_type1)
5019	   && TYPE_PTRMEM_P (from_type2)
5020	   && TYPE_PTRMEM_P (to_type1)
5021	   && TYPE_PTRMEM_P (to_type2))
5022    {
5023      deref_to_type1 = TYPE_OFFSET_BASETYPE (TREE_TYPE (from_type1));
5024      deref_to_type2 = TYPE_OFFSET_BASETYPE (TREE_TYPE (from_type2));
5025      deref_from_type1 = TYPE_OFFSET_BASETYPE (TREE_TYPE (to_type1));
5026      deref_from_type2 = TYPE_OFFSET_BASETYPE (TREE_TYPE (to_type2));
5027    }
5028  else if (TYPE_PTRMEMFUNC_P (from_type1)
5029	   && TYPE_PTRMEMFUNC_P (from_type2)
5030	   && TYPE_PTRMEMFUNC_P (to_type1)
5031	   && TYPE_PTRMEMFUNC_P (to_type2))
5032    {
5033      deref_to_type1 = TYPE_PTRMEMFUNC_OBJECT_TYPE (from_type1);
5034      deref_to_type2 = TYPE_PTRMEMFUNC_OBJECT_TYPE (from_type2);
5035      deref_from_type1 = TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type1);
5036      deref_from_type2 = TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type2);
5037    }
5038
5039  if (deref_from_type1 != NULL_TREE
5040      && IS_AGGR_TYPE_CODE (TREE_CODE (deref_from_type1))
5041      && IS_AGGR_TYPE_CODE (TREE_CODE (deref_from_type2)))
5042    {
5043      /* This was one of the pointer or pointer-like conversions.
5044
5045	 [over.ics.rank]
5046
5047	 --If class B is derived directly or indirectly from class A,
5048	   conversion of B* to A* is better than conversion of B* to
5049	   void*, and conversion of A* to void* is better than
5050	   conversion of B* to void*.  */
5051      if (TREE_CODE (deref_to_type1) == VOID_TYPE
5052	  && TREE_CODE (deref_to_type2) == VOID_TYPE)
5053	{
5054	  if (is_properly_derived_from (deref_from_type1,
5055					deref_from_type2))
5056	    return -1;
5057	  else if (is_properly_derived_from (deref_from_type2,
5058					     deref_from_type1))
5059	    return 1;
5060	}
5061      else if (TREE_CODE (deref_to_type1) == VOID_TYPE
5062	       || TREE_CODE (deref_to_type2) == VOID_TYPE)
5063	{
5064	  if (same_type_p (deref_from_type1, deref_from_type2))
5065	    {
5066	      if (TREE_CODE (deref_to_type2) == VOID_TYPE)
5067		{
5068		  if (is_properly_derived_from (deref_from_type1,
5069						deref_to_type1))
5070		    return 1;
5071		}
5072	      /* We know that DEREF_TO_TYPE1 is `void' here.  */
5073	      else if (is_properly_derived_from (deref_from_type1,
5074						 deref_to_type2))
5075		return -1;
5076	    }
5077	}
5078      else if (IS_AGGR_TYPE_CODE (TREE_CODE (deref_to_type1))
5079	       && IS_AGGR_TYPE_CODE (TREE_CODE (deref_to_type2)))
5080	{
5081	  /* [over.ics.rank]
5082
5083	     --If class B is derived directly or indirectly from class A
5084	       and class C is derived directly or indirectly from B,
5085
5086	     --conversion of C* to B* is better than conversion of C* to
5087	       A*,
5088
5089	     --conversion of B* to A* is better than conversion of C* to
5090	       A*  */
5091	  if (same_type_p (deref_from_type1, deref_from_type2))
5092	    {
5093	      if (is_properly_derived_from (deref_to_type1,
5094					    deref_to_type2))
5095		return 1;
5096	      else if (is_properly_derived_from (deref_to_type2,
5097						 deref_to_type1))
5098		return -1;
5099	    }
5100	  else if (same_type_p (deref_to_type1, deref_to_type2))
5101	    {
5102	      if (is_properly_derived_from (deref_from_type2,
5103					    deref_from_type1))
5104		return 1;
5105	      else if (is_properly_derived_from (deref_from_type1,
5106						 deref_from_type2))
5107		return -1;
5108	    }
5109	}
5110    }
5111  else if (CLASS_TYPE_P (non_reference (from_type1))
5112	   && same_type_p (from_type1, from_type2))
5113    {
5114      tree from = non_reference (from_type1);
5115
5116      /* [over.ics.rank]
5117
5118	 --binding of an expression of type C to a reference of type
5119	   B& is better than binding an expression of type C to a
5120	   reference of type A&
5121
5122	 --conversion of C to B is better than conversion of C to A,  */
5123      if (is_properly_derived_from (from, to_type1)
5124	  && is_properly_derived_from (from, to_type2))
5125	{
5126	  if (is_properly_derived_from (to_type1, to_type2))
5127	    return 1;
5128	  else if (is_properly_derived_from (to_type2, to_type1))
5129	    return -1;
5130	}
5131    }
5132  else if (CLASS_TYPE_P (non_reference (to_type1))
5133	   && same_type_p (to_type1, to_type2))
5134    {
5135      tree to = non_reference (to_type1);
5136
5137      /* [over.ics.rank]
5138
5139	 --binding of an expression of type B to a reference of type
5140	   A& is better than binding an expression of type C to a
5141	   reference of type A&,
5142
5143	 --onversion of B to A is better than conversion of C to A  */
5144      if (is_properly_derived_from (from_type1, to)
5145	  && is_properly_derived_from (from_type2, to))
5146	{
5147	  if (is_properly_derived_from (from_type2, from_type1))
5148	    return 1;
5149	  else if (is_properly_derived_from (from_type1, from_type2))
5150	    return -1;
5151	}
5152    }
5153
5154  /* [over.ics.rank]
5155
5156     --S1 and S2 differ only in their qualification conversion and  yield
5157       similar  types  T1 and T2 (_conv.qual_), respectively, and the cv-
5158       qualification signature of type T1 is a proper subset of  the  cv-
5159       qualification signature of type T2  */
5160  if (TREE_CODE (ics1) == QUAL_CONV
5161      && TREE_CODE (ics2) == QUAL_CONV
5162      && same_type_p (from_type1, from_type2))
5163    return comp_cv_qual_signature (to_type1, to_type2);
5164
5165  /* [over.ics.rank]
5166
5167     --S1 and S2 are reference bindings (_dcl.init.ref_), and the
5168     types to which the references refer are the same type except for
5169     top-level cv-qualifiers, and the type to which the reference
5170     initialized by S2 refers is more cv-qualified than the type to
5171     which the reference initialized by S1 refers */
5172
5173  if (target_type1 && target_type2
5174      && same_type_ignoring_top_level_qualifiers_p (to_type1, to_type2))
5175    return comp_cv_qualification (target_type2, target_type1);
5176
5177  /* Neither conversion sequence is better than the other.  */
5178  return 0;
5179}
5180
5181/* The source type for this standard conversion sequence.  */
5182
5183static tree
5184source_type (t)
5185     tree t;
5186{
5187  for (;; t = TREE_OPERAND (t, 0))
5188    {
5189      if (TREE_CODE (t) == USER_CONV
5190	  || TREE_CODE (t) == AMBIG_CONV
5191	  || TREE_CODE (t) == IDENTITY_CONV)
5192	return TREE_TYPE (t);
5193    }
5194  abort ();
5195}
5196
5197/* Note a warning about preferring WINNER to LOSER.  We do this by storing
5198   a pointer to LOSER and re-running joust to produce the warning if WINNER
5199   is actually used.  */
5200
5201static void
5202add_warning (winner, loser)
5203     struct z_candidate *winner, *loser;
5204{
5205  winner->warnings = tree_cons (NULL_TREE,
5206				build_ptr_wrapper (loser),
5207				winner->warnings);
5208}
5209
5210/* Returns true iff functions are equivalent. Equivalent functions are
5211   not '==' only if one is a function-local extern function or if
5212   both are extern "C".  */
5213
5214static inline int
5215equal_functions (fn1, fn2)
5216     tree fn1;
5217     tree fn2;
5218{
5219  if (DECL_LOCAL_FUNCTION_P (fn1) || DECL_LOCAL_FUNCTION_P (fn2)
5220      || DECL_EXTERN_C_FUNCTION_P (fn1))
5221    return decls_match (fn1, fn2);
5222  return fn1 == fn2;
5223}
5224
5225/* Compare two candidates for overloading as described in
5226   [over.match.best].  Return values:
5227
5228      1: cand1 is better than cand2
5229     -1: cand2 is better than cand1
5230      0: cand1 and cand2 are indistinguishable */
5231
5232static int
5233joust (cand1, cand2, warn)
5234     struct z_candidate *cand1, *cand2;
5235     int warn;
5236{
5237  int winner = 0;
5238  int i, off1 = 0, off2 = 0, len;
5239
5240  /* Candidates that involve bad conversions are always worse than those
5241     that don't.  */
5242  if (cand1->viable > cand2->viable)
5243    return 1;
5244  if (cand1->viable < cand2->viable)
5245    return -1;
5246
5247  /* If we have two pseudo-candidates for conversions to the same type,
5248     or two candidates for the same function, arbitrarily pick one.  */
5249  if (cand1->fn == cand2->fn
5250      && (TYPE_P (cand1->fn) || DECL_P (cand1->fn)))
5251    return 1;
5252
5253  /* a viable function F1
5254     is defined to be a better function than another viable function F2  if
5255     for  all arguments i, ICSi(F1) is not a worse conversion sequence than
5256     ICSi(F2), and then */
5257
5258  /* for some argument j, ICSj(F1) is a better conversion  sequence  than
5259     ICSj(F2) */
5260
5261  /* For comparing static and non-static member functions, we ignore
5262     the implicit object parameter of the non-static function.  The
5263     standard says to pretend that the static function has an object
5264     parm, but that won't work with operator overloading.  */
5265  len = TREE_VEC_LENGTH (cand1->convs);
5266  if (len != TREE_VEC_LENGTH (cand2->convs))
5267    {
5268      if (DECL_STATIC_FUNCTION_P (cand1->fn)
5269	  && ! DECL_STATIC_FUNCTION_P (cand2->fn))
5270	off2 = 1;
5271      else if (! DECL_STATIC_FUNCTION_P (cand1->fn)
5272	       && DECL_STATIC_FUNCTION_P (cand2->fn))
5273	{
5274	  off1 = 1;
5275	  --len;
5276	}
5277      else
5278	abort ();
5279    }
5280
5281  for (i = 0; i < len; ++i)
5282    {
5283      tree t1 = TREE_VEC_ELT (cand1->convs, i+off1);
5284      tree t2 = TREE_VEC_ELT (cand2->convs, i+off2);
5285      int comp = compare_ics (t1, t2);
5286
5287      if (comp != 0)
5288	{
5289	  if (warn_sign_promo
5290	      && ICS_RANK (t1) + ICS_RANK (t2) == STD_RANK + PROMO_RANK
5291	      && TREE_CODE (t1) == STD_CONV
5292	      && TREE_CODE (t2) == STD_CONV
5293	      && TREE_CODE (TREE_TYPE (t1)) == INTEGER_TYPE
5294	      && TREE_CODE (TREE_TYPE (t2)) == INTEGER_TYPE
5295	      && (TYPE_PRECISION (TREE_TYPE (t1))
5296		  == TYPE_PRECISION (TREE_TYPE (t2)))
5297	      && (TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (t1, 0)))
5298		  || (TREE_CODE (TREE_TYPE (TREE_OPERAND (t1, 0)))
5299		      == ENUMERAL_TYPE)))
5300	    {
5301	      tree type = TREE_TYPE (TREE_OPERAND (t1, 0));
5302	      tree type1, type2;
5303	      struct z_candidate *w, *l;
5304	      if (comp > 0)
5305		type1 = TREE_TYPE (t1), type2 = TREE_TYPE (t2),
5306		  w = cand1, l = cand2;
5307	      else
5308		type1 = TREE_TYPE (t2), type2 = TREE_TYPE (t1),
5309		  w = cand2, l = cand1;
5310
5311	      if (warn)
5312		{
5313		  warning ("passing `%T' chooses `%T' over `%T'",
5314			      type, type1, type2);
5315		  warning ("  in call to `%D'", w->fn);
5316		}
5317	      else
5318		add_warning (w, l);
5319	    }
5320
5321	  if (winner && comp != winner)
5322	    {
5323	      winner = 0;
5324	      goto tweak;
5325	    }
5326	  winner = comp;
5327	}
5328    }
5329
5330  /* warn about confusing overload resolution for user-defined conversions,
5331     either between a constructor and a conversion op, or between two
5332     conversion ops.  */
5333  if (winner && cand1->second_conv
5334      && ((DECL_CONSTRUCTOR_P (cand1->fn)
5335	   != DECL_CONSTRUCTOR_P (cand2->fn))
5336	  /* Don't warn if the two conv ops convert to the same type...  */
5337	  || (! DECL_CONSTRUCTOR_P (cand1->fn)
5338	      && ! same_type_p (TREE_TYPE (TREE_TYPE (cand1->fn)),
5339				TREE_TYPE (TREE_TYPE (cand2->fn))))))
5340    {
5341      int comp = compare_ics (cand1->second_conv, cand2->second_conv);
5342      if (comp != winner)
5343	{
5344	  struct z_candidate *w, *l;
5345	  tree convn;
5346	  if (winner == 1)
5347	    w = cand1, l = cand2;
5348	  else
5349	    w = cand2, l = cand1;
5350	  if (DECL_CONTEXT (cand1->fn) == DECL_CONTEXT (cand2->fn)
5351	      && ! DECL_CONSTRUCTOR_P (cand1->fn)
5352	      && ! DECL_CONSTRUCTOR_P (cand2->fn)
5353	      && (convn = standard_conversion
5354		  (TREE_TYPE (TREE_TYPE (l->fn)),
5355		   TREE_TYPE (TREE_TYPE (w->fn)), NULL_TREE))
5356	      && TREE_CODE (convn) == QUAL_CONV)
5357	    /* Don't complain about `operator char *()' beating
5358	       `operator const char *() const'.  */;
5359	  else if (warn)
5360	    {
5361	      tree source = source_type (TREE_VEC_ELT (w->convs, 0));
5362	      if (! DECL_CONSTRUCTOR_P (w->fn))
5363		source = TREE_TYPE (source);
5364	      warning ("choosing `%D' over `%D'", w->fn, l->fn);
5365	      warning ("  for conversion from `%T' to `%T'",
5366			  source, TREE_TYPE (w->second_conv));
5367	      warning ("  because conversion sequence for the argument is better");
5368	    }
5369	  else
5370	    add_warning (w, l);
5371	}
5372    }
5373
5374  if (winner)
5375    return winner;
5376
5377  /* or, if not that,
5378     F1 is a non-template function and F2 is a template function
5379     specialization.  */
5380
5381  if (! cand1->template && cand2->template)
5382    return 1;
5383  else if (cand1->template && ! cand2->template)
5384    return -1;
5385
5386  /* or, if not that,
5387     F1 and F2 are template functions and the function template for F1 is
5388     more specialized than the template for F2 according to the partial
5389     ordering rules.  */
5390
5391  if (cand1->template && cand2->template)
5392    {
5393      winner = more_specialized
5394        (TI_TEMPLATE (cand1->template), TI_TEMPLATE (cand2->template),
5395         DEDUCE_ORDER,
5396         /* Tell the deduction code how many real function arguments
5397	    we saw, not counting the implicit 'this' argument.  But,
5398	    add_function_candidate() suppresses the "this" argument
5399	    for constructors.
5400
5401	    [temp.func.order]: The presence of unused ellipsis and default
5402	    arguments has no effect on the partial ordering of function
5403	    templates.  */
5404         TREE_VEC_LENGTH (cand1->convs)
5405	 - (DECL_NONSTATIC_MEMBER_FUNCTION_P (cand1->fn)
5406	    - DECL_CONSTRUCTOR_P (cand1->fn)));
5407      /* HERE */
5408      if (winner)
5409        return winner;
5410    }
5411
5412  /* or, if not that,
5413     the  context  is  an  initialization by user-defined conversion (see
5414     _dcl.init_  and  _over.match.user_)  and  the  standard   conversion
5415     sequence  from  the return type of F1 to the destination type (i.e.,
5416     the type of the entity being initialized)  is  a  better  conversion
5417     sequence  than the standard conversion sequence from the return type
5418     of F2 to the destination type.  */
5419
5420  if (cand1->second_conv)
5421    {
5422      winner = compare_ics (cand1->second_conv, cand2->second_conv);
5423      if (winner)
5424        return winner;
5425    }
5426
5427  /* Check whether we can discard a builtin candidate, either because we
5428     have two identical ones or matching builtin and non-builtin candidates.
5429
5430     (Pedantically in the latter case the builtin which matched the user
5431     function should not be added to the overload set, but we spot it here.
5432
5433     [over.match.oper]
5434     ... the builtin candidates include ...
5435     - do not have the same parameter type list as any non-template
5436       non-member candidate.  */
5437
5438  if (TREE_CODE (cand1->fn) == IDENTIFIER_NODE
5439      || TREE_CODE (cand2->fn) == IDENTIFIER_NODE)
5440    {
5441      for (i = 0; i < len; ++i)
5442	if (!same_type_p (TREE_TYPE (TREE_VEC_ELT (cand1->convs, i)),
5443			  TREE_TYPE (TREE_VEC_ELT (cand2->convs, i))))
5444	  break;
5445      if (i == TREE_VEC_LENGTH (cand1->convs))
5446	{
5447	  if (cand1->fn == cand2->fn)
5448	    /* Two built-in candidates; arbitrarily pick one.  */
5449	    return 1;
5450	  else if (TREE_CODE (cand1->fn) == IDENTIFIER_NODE)
5451	    /* cand1 is built-in; prefer cand2.  */
5452	    return -1;
5453	  else
5454	    /* cand2 is built-in; prefer cand1.  */
5455	    return 1;
5456	}
5457    }
5458
5459  /* If the two functions are the same (this can happen with declarations
5460     in multiple scopes and arg-dependent lookup), arbitrarily choose one.  */
5461  if (DECL_P (cand1->fn) && DECL_P (cand2->fn)
5462      && equal_functions (cand1->fn, cand2->fn))
5463    return 1;
5464
5465tweak:
5466
5467  /* Extension: If the worst conversion for one candidate is worse than the
5468     worst conversion for the other, take the first.  */
5469  if (!pedantic)
5470    {
5471      int rank1 = IDENTITY_RANK, rank2 = IDENTITY_RANK;
5472      struct z_candidate *w = 0, *l = 0;
5473
5474      for (i = 0; i < len; ++i)
5475	{
5476	  if (ICS_RANK (TREE_VEC_ELT (cand1->convs, i+off1)) > rank1)
5477	    rank1 = ICS_RANK (TREE_VEC_ELT (cand1->convs, i+off1));
5478	  if (ICS_RANK (TREE_VEC_ELT (cand2->convs, i+off2)) > rank2)
5479	    rank2 = ICS_RANK (TREE_VEC_ELT (cand2->convs, i+off2));
5480	}
5481      if (rank1 < rank2)
5482	winner = 1, w = cand1, l = cand2;
5483      if (rank1 > rank2)
5484	winner = -1, w = cand2, l = cand1;
5485      if (winner)
5486        {
5487	  if (warn)
5488	    {
5489	      pedwarn ("choosing `%D' over `%D'", w->fn, l->fn);
5490	      pedwarn (
5491"  because worst conversion for the former is better than worst conversion for the latter");
5492	    }
5493	  else
5494	    add_warning (w, l);
5495          return winner;
5496        }
5497    }
5498
5499  my_friendly_assert (!winner, 20010121);
5500  return 0;
5501}
5502
5503/* Given a list of candidates for overloading, find the best one, if any.
5504   This algorithm has a worst case of O(2n) (winner is last), and a best
5505   case of O(n/2) (totally ambiguous); much better than a sorting
5506   algorithm.  */
5507
5508static struct z_candidate *
5509tourney (candidates)
5510     struct z_candidate *candidates;
5511{
5512  struct z_candidate *champ = candidates, *challenger;
5513  int fate;
5514  int champ_compared_to_predecessor = 0;
5515
5516  /* Walk through the list once, comparing each current champ to the next
5517     candidate, knocking out a candidate or two with each comparison.  */
5518
5519  for (challenger = champ->next; challenger; )
5520    {
5521      fate = joust (champ, challenger, 0);
5522      if (fate == 1)
5523	challenger = challenger->next;
5524      else
5525	{
5526	  if (fate == 0)
5527	    {
5528	      champ = challenger->next;
5529	      if (champ == 0)
5530		return 0;
5531	      champ_compared_to_predecessor = 0;
5532	    }
5533	  else
5534	    {
5535	      champ = challenger;
5536	      champ_compared_to_predecessor = 1;
5537	    }
5538
5539	  challenger = champ->next;
5540	}
5541    }
5542
5543  /* Make sure the champ is better than all the candidates it hasn't yet
5544     been compared to.  */
5545
5546  for (challenger = candidates;
5547       challenger != champ
5548	 && !(champ_compared_to_predecessor && challenger->next == champ);
5549       challenger = challenger->next)
5550    {
5551      fate = joust (champ, challenger, 0);
5552      if (fate != 1)
5553	return 0;
5554    }
5555
5556  return champ;
5557}
5558
5559/* Returns non-zero if things of type FROM can be converted to TO.  */
5560
5561int
5562can_convert (to, from)
5563     tree to, from;
5564{
5565  return can_convert_arg (to, from, NULL_TREE);
5566}
5567
5568/* Returns non-zero if ARG (of type FROM) can be converted to TO.  */
5569
5570int
5571can_convert_arg (to, from, arg)
5572     tree to, from, arg;
5573{
5574  tree t = implicit_conversion (to, from, arg, LOOKUP_NORMAL);
5575  return (t && ! ICS_BAD_FLAG (t));
5576}
5577
5578/* Like can_convert_arg, but allows dubious conversions as well.  */
5579
5580int
5581can_convert_arg_bad (to, from, arg)
5582     tree to, from, arg;
5583{
5584  tree t = implicit_conversion (to, from, arg, LOOKUP_NORMAL);
5585  return !!t;
5586}
5587
5588/* Convert EXPR to TYPE.  Return the converted expression.
5589
5590   Note that we allow bad conversions here because by the time we get to
5591   this point we are committed to doing the conversion.  If we end up
5592   doing a bad conversion, convert_like will complain.  */
5593
5594tree
5595perform_implicit_conversion (type, expr)
5596     tree type;
5597     tree expr;
5598{
5599  tree conv;
5600
5601  if (expr == error_mark_node)
5602    return error_mark_node;
5603  conv = implicit_conversion (type, TREE_TYPE (expr), expr,
5604			      LOOKUP_NORMAL);
5605  if (!conv)
5606    {
5607      error ("could not convert `%E' to `%T'", expr, type);
5608      return error_mark_node;
5609    }
5610
5611  return convert_like (conv, expr);
5612}
5613
5614/* Convert EXPR to the indicated reference TYPE, in a way suitable for
5615   initializing a variable of that TYPE.  Return the converted
5616   expression.  */
5617
5618tree
5619initialize_reference (type, expr)
5620     tree type;
5621     tree expr;
5622{
5623  tree conv;
5624
5625  conv = reference_binding (type, TREE_TYPE (expr), expr, LOOKUP_NORMAL);
5626  if (!conv || ICS_BAD_FLAG (conv))
5627    {
5628      error ("could not convert `%E' to `%T'", expr, type);
5629      return error_mark_node;
5630    }
5631
5632  return convert_like (conv, expr);
5633}
5634