xref: /openbsd/gnu/usr.bin/gcc/gcc/cp/call.c (revision e97b50d0)
1 /* Functions related to invoking methods and overloaded functions.
2    Copyright (C) 1987, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 2003,
3    1999, 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
4    Contributed by Michael Tiemann (tiemann@cygnus.com) and
5    modified by Brendan Kehoe (brendan@cygnus.com).
6 
7 This file is part of GNU CC.
8 
9 GNU CC is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2, or (at your option)
12 any later version.
13 
14 GNU CC is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 GNU General Public License for more details.
18 
19 You should have received a copy of the GNU General Public License
20 along with GNU CC; see the file COPYING.  If not, write to
21 the Free Software Foundation, 59 Temple Place - Suite 330,
22 Boston, 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 
39 extern int inhibit_warnings;
40 
41 static tree build_field_call PARAMS ((tree, tree, tree));
42 static struct z_candidate * tourney PARAMS ((struct z_candidate *));
43 static int equal_functions PARAMS ((tree, tree));
44 static int joust PARAMS ((struct z_candidate *, struct z_candidate *, int));
45 static int compare_ics PARAMS ((tree, tree));
46 static tree build_over_call PARAMS ((struct z_candidate *, tree, int));
47 static tree build_java_interface_fn_ref PARAMS ((tree, tree));
48 #define convert_like(CONV, EXPR)				\
49   convert_like_real ((CONV), (EXPR), NULL_TREE, 0, 0, 		\
50 		     /*issue_conversion_warnings=*/true)
51 #define convert_like_with_context(CONV, EXPR, FN, ARGNO)	\
52   convert_like_real ((CONV), (EXPR), (FN), (ARGNO), 0, 		\
53 		     /*issue_conversion_warnings=*/true)
54 static tree convert_like_real (tree, tree, tree, int, int, bool);
55 static void op_error PARAMS ((enum tree_code, enum tree_code, tree, tree,
56 			    tree, const char *));
57 static tree build_object_call PARAMS ((tree, tree));
58 static tree resolve_args PARAMS ((tree));
59 static struct z_candidate * build_user_type_conversion_1
60 	PARAMS ((tree, tree, int));
61 static void print_z_candidates PARAMS ((struct z_candidate *));
62 static tree build_this PARAMS ((tree));
63 static struct z_candidate * splice_viable PARAMS ((struct z_candidate *));
64 static int any_viable PARAMS ((struct z_candidate *));
65 static int any_strictly_viable PARAMS ((struct z_candidate *));
66 static struct z_candidate * add_template_candidate
67 	PARAMS ((struct z_candidate **, tree, tree, tree, tree, tree,
68 		 tree, tree, int, unification_kind_t));
69 static struct z_candidate * add_template_candidate_real
70 	PARAMS ((struct z_candidate **, tree, tree, tree, tree, tree,
71 		 tree, tree, int, tree, unification_kind_t));
72 static struct z_candidate * add_template_conv_candidate
73         PARAMS ((struct z_candidate **, tree, tree, tree, tree, tree, tree));
74 static void add_builtin_candidates
75 	PARAMS ((struct z_candidate **, enum tree_code, enum tree_code,
76 	       tree, tree *, int));
77 static void add_builtin_candidate
78 	PARAMS ((struct z_candidate **, enum tree_code, enum tree_code,
79 	       tree, tree, tree, tree *, tree *, int));
80 static int is_complete PARAMS ((tree));
81 static void build_builtin_candidate
82 	PARAMS ((struct z_candidate **, tree, tree, tree, tree *, tree *,
83 	       int));
84 static struct z_candidate * add_conv_candidate
85 	PARAMS ((struct z_candidate **, tree, tree, tree, tree, tree));
86 static struct z_candidate * add_function_candidate
87 	(struct z_candidate **, tree, tree, tree, tree, tree, int);
88 static tree implicit_conversion PARAMS ((tree, tree, tree, int));
89 static tree standard_conversion PARAMS ((tree, tree, tree));
90 static tree reference_binding (tree, tree, tree, int);
91 static tree non_reference PARAMS ((tree));
92 static tree build_conv PARAMS ((enum tree_code, tree, tree));
93 static int is_subseq PARAMS ((tree, tree));
94 static tree maybe_handle_ref_bind PARAMS ((tree*));
95 static void maybe_handle_implicit_object PARAMS ((tree*));
96 static struct z_candidate *add_candidate
97         (struct z_candidate **, tree, tree, tree, tree, int);
98 static tree source_type PARAMS ((tree));
99 static void add_warning PARAMS ((struct z_candidate *, struct z_candidate *));
100 static int reference_related_p PARAMS ((tree, tree));
101 static int reference_compatible_p PARAMS ((tree, tree));
102 static tree convert_class_to_reference PARAMS ((tree, tree, tree));
103 static tree direct_reference_binding PARAMS ((tree, tree));
104 static int promoted_arithmetic_type_p PARAMS ((tree));
105 static tree conditional_conversion PARAMS ((tree, tree));
106 static tree call_builtin_trap PARAMS ((void));
107 static tree merge_conversion_sequences (tree, tree);
108 
109 tree
build_vfield_ref(datum,type)110 build_vfield_ref (datum, type)
111      tree datum, type;
112 {
113   if (datum == error_mark_node)
114     return error_mark_node;
115 
116   if (TREE_CODE (TREE_TYPE (datum)) == REFERENCE_TYPE)
117     datum = convert_from_reference (datum);
118 
119   if (TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (type)
120       && !same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (datum), type))
121     datum = convert_to_base (datum, type, /*check_access=*/false);
122 
123   return build (COMPONENT_REF, TREE_TYPE (TYPE_VFIELD (type)),
124 		datum, TYPE_VFIELD (type));
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 
130 static tree
build_field_call(tree instance_ptr,tree decl,tree parms)131 build_field_call (tree instance_ptr, tree decl, tree parms)
132 {
133   tree instance;
134 
135   if (decl == error_mark_node || decl == NULL_TREE)
136     return decl;
137 
138   if (TREE_CODE (decl) == FIELD_DECL || TREE_CODE (decl) == VAR_DECL)
139     {
140       /* If it's a field, try overloading operator (),
141 	 or calling if the field is a pointer-to-function.  */
142       instance = build_indirect_ref (instance_ptr, NULL);
143       instance = build_class_member_access_expr (instance, decl,
144 						 /*access_path=*/NULL_TREE,
145 						 /*preserve_reference=*/false);
146 
147       if (instance == error_mark_node)
148 	return error_mark_node;
149 
150       if (IS_AGGR_TYPE (TREE_TYPE (instance)))
151 	return build_opfncall (CALL_EXPR, LOOKUP_NORMAL,
152 			       instance, parms, NULL_TREE);
153       else if (TREE_CODE (TREE_TYPE (instance)) == FUNCTION_TYPE
154 	       || (TREE_CODE (TREE_TYPE (instance)) == POINTER_TYPE
155 		   && (TREE_CODE (TREE_TYPE (TREE_TYPE (instance)))
156 		       == FUNCTION_TYPE)))
157 	return build_function_call (instance, parms);
158     }
159 
160   return NULL_TREE;
161 }
162 
163 /* Returns nonzero iff the destructor name specified in NAME
164    (a BIT_NOT_EXPR) matches BASETYPE.  The operand of NAME can take many
165    forms...  */
166 
167 int
check_dtor_name(basetype,name)168 check_dtor_name (basetype, name)
169      tree basetype, name;
170 {
171   name = TREE_OPERAND (name, 0);
172 
173   /* Just accept something we've already complained about.  */
174   if (name == error_mark_node)
175     return 1;
176 
177   if (TREE_CODE (name) == TYPE_DECL)
178     name = TREE_TYPE (name);
179   else if (TYPE_P (name))
180     /* OK */;
181   else if (TREE_CODE (name) == IDENTIFIER_NODE)
182     {
183       if ((IS_AGGR_TYPE (basetype) && name == constructor_name (basetype))
184 	  || (TREE_CODE (basetype) == ENUMERAL_TYPE
185 	      && name == TYPE_IDENTIFIER (basetype)))
186 	name = basetype;
187       else
188 	name = get_type_value (name);
189     }
190   /* In the case of:
191 
192        template <class T> struct S { ~S(); };
193        int i;
194        i.~S();
195 
196      NAME will be a class template.  */
197   else if (DECL_CLASS_TEMPLATE_P (name))
198     return 0;
199   else
200     abort ();
201 
202   if (name && TYPE_MAIN_VARIANT (basetype) == TYPE_MAIN_VARIANT (name))
203     return 1;
204   return 0;
205 }
206 
207 /* Build a method call of the form `EXP->SCOPES::NAME (PARMS)'.
208    This is how virtual function calls are avoided.  */
209 
210 tree
build_scoped_method_call(exp,basetype,name,parms)211 build_scoped_method_call (exp, basetype, name, parms)
212      tree exp, basetype, name, parms;
213 {
214   /* Because this syntactic form does not allow
215      a pointer to a base class to be `stolen',
216      we need not protect the derived->base conversion
217      that happens here.
218 
219      @@ But we do have to check access privileges later.  */
220   tree binfo, decl;
221   tree type = TREE_TYPE (exp);
222 
223   if (type == error_mark_node
224       || basetype == error_mark_node)
225     return error_mark_node;
226 
227   if (processing_template_decl)
228     {
229       if (TREE_CODE (name) == BIT_NOT_EXPR
230 	  && TREE_CODE (TREE_OPERAND (name, 0)) == IDENTIFIER_NODE)
231 	{
232 	  tree type = get_aggr_from_typedef (TREE_OPERAND (name, 0), 0);
233 	  if (type)
234 	    name = build_min_nt (BIT_NOT_EXPR, type);
235 	}
236       name = build_min_nt (SCOPE_REF, basetype, name);
237       return build_min_nt (METHOD_CALL_EXPR, name, exp, parms, NULL_TREE);
238     }
239 
240   if (TREE_CODE (type) == REFERENCE_TYPE)
241     type = TREE_TYPE (type);
242 
243   if (TREE_CODE (basetype) == TREE_VEC)
244     {
245       binfo = basetype;
246       basetype = BINFO_TYPE (binfo);
247     }
248   else
249     binfo = NULL_TREE;
250 
251   /* Check the destructor call syntax.  */
252   if (TREE_CODE (name) == BIT_NOT_EXPR)
253     {
254       /* We can get here if someone writes their destructor call like
255 	 `obj.NS::~T()'; this isn't really a scoped method call, so hand
256 	 it off.  */
257       if (TREE_CODE (basetype) == NAMESPACE_DECL)
258 	return build_method_call (exp, name, parms, NULL_TREE, LOOKUP_NORMAL);
259 
260       if (! check_dtor_name (basetype, name))
261 	error ("qualified type `%T' does not match destructor name `~%T'",
262 		  basetype, TREE_OPERAND (name, 0));
263 
264       /* Destructors can be "called" for simple types; see 5.2.4 and 12.4 Note
265 	 that explicit ~int is caught in the parser; this deals with typedefs
266 	 and template parms.  */
267       if (! IS_AGGR_TYPE (basetype))
268 	{
269 	  if (TYPE_MAIN_VARIANT (type) != TYPE_MAIN_VARIANT (basetype))
270 	    error ("type of `%E' does not match destructor type `%T' (type was `%T')",
271 		      exp, basetype, type);
272 
273 	  return cp_convert (void_type_node, exp);
274 	}
275     }
276 
277   if (TREE_CODE (basetype) == NAMESPACE_DECL)
278     {
279       error ("`%D' is a namespace", basetype);
280       return error_mark_node;
281     }
282   if (! is_aggr_type (basetype, 1))
283     return error_mark_node;
284 
285   if (! IS_AGGR_TYPE (type))
286     {
287       error ("base object `%E' of scoped method call is of non-aggregate type `%T'",
288 		exp, type);
289       return error_mark_node;
290     }
291 
292   decl = build_scoped_ref (exp, basetype, &binfo);
293 
294   if (binfo)
295     {
296       /* Call to a destructor.  */
297       if (TREE_CODE (name) == BIT_NOT_EXPR)
298 	{
299 	  if (! TYPE_HAS_DESTRUCTOR (TREE_TYPE (decl)))
300 	    return cp_convert (void_type_node, exp);
301 
302 	  return build_delete (TREE_TYPE (decl), decl,
303 			       sfk_complete_destructor,
304 			       LOOKUP_NORMAL|LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR,
305 			       0);
306 	}
307 
308       /* Call to a method.  */
309       return build_method_call (decl, name, parms, binfo,
310 				LOOKUP_NORMAL|LOOKUP_NONVIRTUAL);
311     }
312   return error_mark_node;
313 }
314 
315 /* We want the address of a function or method.  We avoid creating a
316    pointer-to-member function.  */
317 
318 tree
build_addr_func(function)319 build_addr_func (function)
320      tree function;
321 {
322   tree type = TREE_TYPE (function);
323 
324   /* We have to do these by hand to avoid real pointer to member
325      functions.  */
326   if (TREE_CODE (type) == METHOD_TYPE)
327     {
328       tree addr;
329 
330       type = build_pointer_type (type);
331 
332       if (!cxx_mark_addressable (function))
333 	return error_mark_node;
334 
335       addr = build1 (ADDR_EXPR, type, function);
336 
337       /* Address of a static or external variable or function counts
338 	 as a constant */
339       if (staticp (function))
340 	TREE_CONSTANT (addr) = 1;
341 
342       function = addr;
343     }
344   else
345     function = default_conversion (function);
346 
347   return function;
348 }
349 
350 /* Build a CALL_EXPR, we can handle FUNCTION_TYPEs, METHOD_TYPEs, or
351    POINTER_TYPE to those.  Note, pointer to member function types
352    (TYPE_PTRMEMFUNC_P) must be handled by our callers.  */
353 
354 tree
build_call(function,parms)355 build_call (function, parms)
356      tree function, parms;
357 {
358   int is_constructor = 0;
359   int nothrow;
360   tree tmp;
361   tree decl;
362   tree result_type;
363   tree fntype;
364 
365   function = build_addr_func (function);
366 
367   if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function)))
368     {
369       sorry ("unable to call pointer to member function here");
370       return error_mark_node;
371     }
372 
373   fntype = TREE_TYPE (TREE_TYPE (function));
374   result_type = TREE_TYPE (fntype);
375 
376   if (TREE_CODE (function) == ADDR_EXPR
377       && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
378     decl = TREE_OPERAND (function, 0);
379   else
380     decl = NULL_TREE;
381 
382   /* We check both the decl and the type; a function may be known not to
383      throw without being declared throw().  */
384   nothrow = ((decl && TREE_NOTHROW (decl))
385 	     || TYPE_NOTHROW_P (TREE_TYPE (TREE_TYPE (function))));
386 
387   if (decl && TREE_THIS_VOLATILE (decl) && cfun)
388     current_function_returns_abnormally = 1;
389 
390   if (decl && TREE_DEPRECATED (decl))
391     warn_deprecated_use (decl);
392   require_complete_eh_spec_types (fntype, decl);
393 
394   if (decl && DECL_CONSTRUCTOR_P (decl))
395     is_constructor = 1;
396 
397   if (decl && ! TREE_USED (decl))
398     {
399       /* We invoke build_call directly for several library functions.
400 	 These may have been declared normally if we're building libgcc,
401 	 so we can't just check DECL_ARTIFICIAL.  */
402       if (DECL_ARTIFICIAL (decl)
403 	  || !strncmp (IDENTIFIER_POINTER (DECL_NAME (decl)), "__", 2))
404 	mark_used (decl);
405       else
406 	abort ();
407     }
408 
409   /* Don't pass empty class objects by value.  This is useful
410      for tags in STL, which are used to control overload resolution.
411      We don't need to handle other cases of copying empty classes.  */
412   if (! decl || ! DECL_BUILT_IN (decl))
413     for (tmp = parms; tmp; tmp = TREE_CHAIN (tmp))
414       if (is_empty_class (TREE_TYPE (TREE_VALUE (tmp)))
415 	  && ! TREE_ADDRESSABLE (TREE_TYPE (TREE_VALUE (tmp))))
416 	{
417 	  tree t = build (EMPTY_CLASS_EXPR, TREE_TYPE (TREE_VALUE (tmp)));
418 	  TREE_VALUE (tmp) = build (COMPOUND_EXPR, TREE_TYPE (t),
419 				    TREE_VALUE (tmp), t);
420 	}
421 
422   function = build_nt (CALL_EXPR, function, parms, NULL_TREE);
423   TREE_HAS_CONSTRUCTOR (function) = is_constructor;
424   TREE_TYPE (function) = result_type;
425   TREE_SIDE_EFFECTS (function) = 1;
426   TREE_NOTHROW (function) = nothrow;
427 
428   return function;
429 }
430 
431 /* Build something of the form ptr->method (args)
432    or object.method (args).  This can also build
433    calls to constructors, and find friends.
434 
435    Member functions always take their class variable
436    as a pointer.
437 
438    INSTANCE is a class instance.
439 
440    NAME is the name of the method desired, usually an IDENTIFIER_NODE.
441 
442    PARMS help to figure out what that NAME really refers to.
443 
444    BASETYPE_PATH, if non-NULL, contains a chain from the type of INSTANCE
445    down to the real instance type to use for access checking.  We need this
446    information to get protected accesses correct.  This parameter is used
447    by build_member_call.
448 
449    FLAGS is the logical disjunction of zero or more LOOKUP_
450    flags.  See cp-tree.h for more info.
451 
452    If this is all OK, calls build_function_call with the resolved
453    member function.
454 
455    This function must also handle being called to perform
456    initialization, promotion/coercion of arguments, and
457    instantiation of default parameters.
458 
459    Note that NAME may refer to an instance variable name.  If
460    `operator()()' is defined for the type of that field, then we return
461    that result.  */
462 
463 #ifdef GATHER_STATISTICS
464 extern int n_build_method_call;
465 #endif
466 
467 tree
build_method_call(instance,name,parms,basetype_path,flags)468 build_method_call (instance, name, parms, basetype_path, flags)
469      tree instance, name, parms, basetype_path;
470      int flags;
471 {
472   tree fn;
473   tree object_type;
474   tree template_args = NULL_TREE;
475   bool has_template_args = false;
476 
477 #ifdef GATHER_STATISTICS
478   n_build_method_call++;
479 #endif
480 
481   if (instance == error_mark_node
482       || name == error_mark_node
483       || parms == error_mark_node
484       || (instance && TREE_TYPE (instance) == error_mark_node))
485     return error_mark_node;
486 
487   if (processing_template_decl)
488     {
489       /* We need to process template parm names here so that tsubst catches
490 	 them properly.  Other type names can wait.  */
491       if (TREE_CODE (name) == BIT_NOT_EXPR)
492 	{
493 	  tree type = NULL_TREE;
494 
495 	  if (TREE_CODE (TREE_OPERAND (name, 0)) == IDENTIFIER_NODE)
496 	    type = get_aggr_from_typedef (TREE_OPERAND (name, 0), 0);
497 	  else if (TREE_CODE (TREE_OPERAND (name, 0)) == TYPE_DECL)
498 	    type = TREE_TYPE (TREE_OPERAND (name, 0));
499 
500 	  if (type && TREE_CODE (type) == TEMPLATE_TYPE_PARM)
501 	    name = build_min_nt (BIT_NOT_EXPR, type);
502 	}
503 
504       return build_min_nt (METHOD_CALL_EXPR, name, instance, parms, NULL_TREE);
505     }
506 
507   if (TREE_CODE (instance) == OFFSET_REF)
508     instance = resolve_offset_ref (instance);
509   if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
510     instance = convert_from_reference (instance);
511   object_type = TREE_TYPE (instance);
512 
513   if (TREE_CODE (name) == BIT_NOT_EXPR)
514     {
515       tree instance_ptr;
516 
517       if (parms)
518 	error ("destructors take no parameters");
519 
520       if (! check_dtor_name (object_type, name))
521 	error
522 	  ("destructor name `~%T' does not match type `%T' of expression",
523 	   TREE_OPERAND (name, 0), object_type);
524 
525       /* The destructor type must be complete.  */
526       object_type = complete_type_or_else (object_type, NULL_TREE);
527       if (!object_type || object_type == error_mark_node)
528 	return error_mark_node;
529 
530       if (! TYPE_HAS_DESTRUCTOR (object_type))
531 	return cp_convert (void_type_node, instance);
532       instance = default_conversion (instance);
533       instance_ptr = build_unary_op (ADDR_EXPR, instance, 0);
534       return build_delete (build_pointer_type (object_type),
535 			   instance_ptr, sfk_complete_destructor,
536 			   LOOKUP_NORMAL|LOOKUP_DESTRUCTOR, 0);
537     }
538 
539   if (!CLASS_TYPE_P (object_type))
540     {
541       if ((flags & LOOKUP_COMPLAIN)
542 	  && TREE_TYPE (instance) != error_mark_node)
543 	error ("request for member `%D' in `%E', which is of non-aggregate type `%T'",
544 	       name, instance, object_type);
545       return error_mark_node;
546     }
547 
548   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
549     {
550       template_args = TREE_OPERAND (name, 1);
551       has_template_args = true;
552       name = TREE_OPERAND (name, 0);
553     }
554   if (TREE_CODE (name) == OVERLOAD)
555     name = DECL_NAME (get_first_fn (name));
556   else if (TREE_CODE (name) == LOOKUP_EXPR)
557     name = TREE_OPERAND (name, 0);
558   else if (DECL_P (name))
559     name = DECL_NAME (name);
560   if (has_template_args)
561     fn = lookup_fnfields (object_type, name, /*protect=*/2);
562   else
563     fn = lookup_member (object_type, name, /*protect=*/2, /*want_type=*/0);
564 
565   if (fn && TREE_CODE (fn) == TREE_LIST && !BASELINK_P (fn))
566     {
567       error ("request for member `%D' is ambiguous", name);
568       print_candidates (fn);
569       return error_mark_node;
570     }
571 
572   /* If the name could not be found, issue an error.  */
573   if (!fn)
574     {
575       unqualified_name_lookup_error (name);
576       return error_mark_node;
577     }
578 
579   if (BASELINK_P (fn) && has_template_args)
580     BASELINK_FUNCTIONS (fn)
581       = build_nt (TEMPLATE_ID_EXPR,
582 		  BASELINK_FUNCTIONS (fn),
583 		  template_args);
584   if (BASELINK_P (fn) && basetype_path)
585     BASELINK_ACCESS_BINFO (fn) = basetype_path;
586 
587   return build_new_method_call (instance, fn, parms,
588 				/*conversion_path=*/NULL_TREE, flags);
589 }
590 
591 /* New overloading code.  */
592 
593 struct z_candidate GTY(()) {
594   /* The FUNCTION_DECL that will be called if this candidate is
595      selected by overload resolution.  */
596   tree fn;
597   tree convs;
598   tree second_conv;
599   int viable;
600   /* If FN is a member function, the binfo indicating the path used to
601      qualify the name of FN at the call site.  This path is used to
602      determine whether or not FN is accessible if it is selected by
603      overload resolution.  The DECL_CONTEXT of FN will always be a
604      (possibly improper) base of this binfo.  */
605   tree access_path;
606   /* If FN is a non-static member function, the binfo indicating the
607      subobject to which the `this' pointer should be converted if FN
608      is selected by overload resolution.  The type pointed to the by
609      the `this' pointer must correspond to the most derived class
610      indicated by the CONVERSION_PATH.  */
611   tree conversion_path;
612   tree template;
613   tree warnings;
614   struct z_candidate *next;
615 };
616 
617 #define IDENTITY_RANK 0
618 #define EXACT_RANK 1
619 #define PROMO_RANK 2
620 #define STD_RANK 3
621 #define PBOOL_RANK 4
622 #define USER_RANK 5
623 #define ELLIPSIS_RANK 6
624 #define BAD_RANK 7
625 
626 #define ICS_RANK(NODE)				\
627   (ICS_BAD_FLAG (NODE) ? BAD_RANK   		\
628    : ICS_ELLIPSIS_FLAG (NODE) ? ELLIPSIS_RANK	\
629    : ICS_USER_FLAG (NODE) ? USER_RANK		\
630    : ICS_STD_RANK (NODE))
631 
632 #define ICS_STD_RANK(NODE) TREE_COMPLEXITY (NODE)
633 
634 #define ICS_USER_FLAG(NODE) TREE_LANG_FLAG_0 (NODE)
635 #define ICS_ELLIPSIS_FLAG(NODE) TREE_LANG_FLAG_1 (NODE)
636 #define ICS_THIS_FLAG(NODE) TREE_LANG_FLAG_2 (NODE)
637 #define ICS_BAD_FLAG(NODE) TREE_LANG_FLAG_3 (NODE)
638 
639 /* In a REF_BIND or a BASE_CONV, this indicates that a temporary
640    should be created to hold the result of the conversion.  */
641 #define NEED_TEMPORARY_P(NODE) TREE_LANG_FLAG_4 (NODE)
642 
643 #define USER_CONV_CAND(NODE) WRAPPER_ZC (TREE_OPERAND (NODE, 1))
644 #define USER_CONV_FN(NODE) (USER_CONV_CAND (NODE)->fn)
645 
646 int
null_ptr_cst_p(t)647 null_ptr_cst_p (t)
648      tree t;
649 {
650   /* [conv.ptr]
651 
652      A null pointer constant is an integral constant expression
653      (_expr.const_) rvalue of integer type that evaluates to zero.  */
654   if (t == null_node
655       || (CP_INTEGRAL_TYPE_P (TREE_TYPE (t)) && integer_zerop (t)))
656     return 1;
657   return 0;
658 }
659 
660 
661 /* Returns nonzero if PARMLIST consists of only default parms and/or
662    ellipsis.  */
663 
664 int
sufficient_parms_p(parmlist)665 sufficient_parms_p (parmlist)
666      tree parmlist;
667 {
668   for (; parmlist && parmlist != void_list_node;
669        parmlist = TREE_CHAIN (parmlist))
670     if (!TREE_PURPOSE (parmlist))
671       return 0;
672   return 1;
673 }
674 
675 static tree
build_conv(code,type,from)676 build_conv (code, type, from)
677      enum tree_code code;
678      tree type, from;
679 {
680   tree t;
681   int rank = ICS_STD_RANK (from);
682 
683   /* We can't use buildl1 here because CODE could be USER_CONV, which
684      takes two arguments.  In that case, the caller is responsible for
685      filling in the second argument.  */
686   t = make_node (code);
687   TREE_TYPE (t) = type;
688   TREE_OPERAND (t, 0) = from;
689 
690   switch (code)
691     {
692     case PTR_CONV:
693     case PMEM_CONV:
694     case BASE_CONV:
695     case STD_CONV:
696       if (rank < STD_RANK)
697 	rank = STD_RANK;
698       break;
699 
700     case QUAL_CONV:
701       if (rank < EXACT_RANK)
702 	rank = EXACT_RANK;
703 
704     default:
705       break;
706     }
707   ICS_STD_RANK (t) = rank;
708   ICS_USER_FLAG (t) = (code == USER_CONV || ICS_USER_FLAG (from));
709   ICS_BAD_FLAG (t) = ICS_BAD_FLAG (from);
710   return t;
711 }
712 
713 /* If T is a REFERENCE_TYPE return the type to which T refers.
714    Otherwise, return T itself.  */
715 
716 static tree
non_reference(t)717 non_reference (t)
718      tree t;
719 {
720   if (TREE_CODE (t) == REFERENCE_TYPE)
721     t = TREE_TYPE (t);
722   return t;
723 }
724 
725 tree
strip_top_quals(t)726 strip_top_quals (t)
727      tree t;
728 {
729   if (TREE_CODE (t) == ARRAY_TYPE)
730     return t;
731   return TYPE_MAIN_VARIANT (t);
732 }
733 
734 /* Returns the standard conversion path (see [conv]) from type FROM to type
735    TO, if any.  For proper handling of null pointer constants, you must
736    also pass the expression EXPR to convert from.  */
737 
738 static tree
standard_conversion(to,from,expr)739 standard_conversion (to, from, expr)
740      tree to, from, expr;
741 {
742   enum tree_code fcode, tcode;
743   tree conv;
744   int fromref = 0;
745 
746   if (TREE_CODE (to) == REFERENCE_TYPE)
747     to = TREE_TYPE (to);
748   if (TREE_CODE (from) == REFERENCE_TYPE)
749     {
750       fromref = 1;
751       from = TREE_TYPE (from);
752     }
753   to = strip_top_quals (to);
754   from = strip_top_quals (from);
755 
756   if ((TYPE_PTRFN_P (to) || TYPE_PTRMEMFUNC_P (to))
757       && expr && type_unknown_p (expr))
758     {
759       expr = instantiate_type (to, expr, tf_conv);
760       if (expr == error_mark_node)
761 	return NULL_TREE;
762       from = TREE_TYPE (expr);
763     }
764 
765   fcode = TREE_CODE (from);
766   tcode = TREE_CODE (to);
767 
768   conv = build1 (IDENTITY_CONV, from, expr);
769 
770   if (fcode == FUNCTION_TYPE)
771     {
772       from = build_pointer_type (from);
773       fcode = TREE_CODE (from);
774       conv = build_conv (LVALUE_CONV, from, conv);
775     }
776   else if (fcode == ARRAY_TYPE)
777     {
778       from = build_pointer_type (TREE_TYPE (from));
779       fcode = TREE_CODE (from);
780       conv = build_conv (LVALUE_CONV, from, conv);
781     }
782   else if (fromref || (expr && lvalue_p (expr)))
783     conv = build_conv (RVALUE_CONV, from, conv);
784 
785    /* Allow conversion between `__complex__' data types  */
786   if (tcode == COMPLEX_TYPE && fcode == COMPLEX_TYPE)
787     {
788       /* The standard conversion sequence to convert FROM to TO is
789          the standard conversion sequence to perform componentwise
790          conversion.  */
791       tree part_conv = standard_conversion
792         (TREE_TYPE (to), TREE_TYPE (from), NULL_TREE);
793 
794       if (part_conv)
795         {
796           conv = build_conv (TREE_CODE (part_conv), to, conv);
797           ICS_STD_RANK (conv) = ICS_STD_RANK (part_conv);
798         }
799       else
800         conv = NULL_TREE;
801 
802       return conv;
803     }
804 
805   if (same_type_p (from, to))
806     return conv;
807 
808   if ((tcode == POINTER_TYPE || TYPE_PTRMEMFUNC_P (to))
809       && expr && null_ptr_cst_p (expr))
810     {
811       conv = build_conv (STD_CONV, to, conv);
812     }
813   else if ((tcode == INTEGER_TYPE && fcode == POINTER_TYPE)
814 	   || (tcode == POINTER_TYPE && fcode == INTEGER_TYPE))
815     {
816       /* For backwards brain damage compatibility, allow interconversion of
817 	 pointers and integers with a pedwarn.  */
818       conv = build_conv (STD_CONV, to, conv);
819       ICS_BAD_FLAG (conv) = 1;
820     }
821   else if (tcode == ENUMERAL_TYPE && fcode == INTEGER_TYPE
822 	   && TYPE_PRECISION (to) == TYPE_PRECISION (from))
823     {
824       /* For backwards brain damage compatibility, allow interconversion of
825 	 enums and integers with a pedwarn.  */
826       conv = build_conv (STD_CONV, to, conv);
827       ICS_BAD_FLAG (conv) = 1;
828     }
829   else if (tcode == POINTER_TYPE && fcode == POINTER_TYPE)
830     {
831       enum tree_code ufcode = TREE_CODE (TREE_TYPE (from));
832       enum tree_code utcode = TREE_CODE (TREE_TYPE (to));
833 
834       if (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (from),
835 						     TREE_TYPE (to)))
836 	;
837       else if (utcode == VOID_TYPE && ufcode != OFFSET_TYPE
838 	       && ufcode != FUNCTION_TYPE)
839 	{
840 	  from = build_pointer_type
841 	    (cp_build_qualified_type (void_type_node,
842 				      cp_type_quals (TREE_TYPE (from))));
843 	  conv = build_conv (PTR_CONV, from, conv);
844 	}
845       else if (ufcode == OFFSET_TYPE && utcode == OFFSET_TYPE)
846 	{
847 	  tree fbase = TYPE_OFFSET_BASETYPE (TREE_TYPE (from));
848 	  tree tbase = TYPE_OFFSET_BASETYPE (TREE_TYPE (to));
849 
850 	  if (DERIVED_FROM_P (fbase, tbase)
851 	      && (same_type_ignoring_top_level_qualifiers_p
852 		  (TREE_TYPE (TREE_TYPE (from)),
853 		   TREE_TYPE (TREE_TYPE (to)))))
854 	    {
855 	      from = build_ptrmem_type (tbase, TREE_TYPE (TREE_TYPE (from)));
856 	      conv = build_conv (PMEM_CONV, from, conv);
857 	    }
858 	}
859       else if (IS_AGGR_TYPE (TREE_TYPE (from))
860 	       && IS_AGGR_TYPE (TREE_TYPE (to))
861 	       /* [conv.ptr]
862 
863 	          An rvalue of type "pointer to cv D," where D is a
864 		  class type, can be converted to an rvalue of type
865 		  "pointer to cv B," where B is a base class (clause
866 		  _class.derived_) of D.  If B is an inaccessible
867 		  (clause _class.access_) or ambiguous
868 		  (_class.member.lookup_) base class of D, a program
869 		  that necessitates this conversion is ill-formed.  */
870 	       /* Therefore, we use DERIVED_FROM_P, and not
871 		  ACESSIBLY_UNIQUELY_DERIVED_FROM_P, in this test.  */
872 	       && DERIVED_FROM_P (TREE_TYPE (to), TREE_TYPE (from)))
873 	{
874 	  from =
875 	    cp_build_qualified_type (TREE_TYPE (to),
876 				     cp_type_quals (TREE_TYPE (from)));
877 	  from = build_pointer_type (from);
878 	  conv = build_conv (PTR_CONV, from, conv);
879 	}
880 
881       if (same_type_p (from, to))
882 	/* OK */;
883       else if (comp_ptr_ttypes (TREE_TYPE (to), TREE_TYPE (from)))
884 	conv = build_conv (QUAL_CONV, to, conv);
885       else if (expr && string_conv_p (to, expr, 0))
886 	/* converting from string constant to char *.  */
887 	conv = build_conv (QUAL_CONV, to, conv);
888       else if (ptr_reasonably_similar (TREE_TYPE (to), TREE_TYPE (from)))
889 	{
890 	  conv = build_conv (PTR_CONV, to, conv);
891 	  ICS_BAD_FLAG (conv) = 1;
892 	}
893       else
894 	return 0;
895 
896       from = to;
897     }
898   else if (TYPE_PTRMEMFUNC_P (to) && TYPE_PTRMEMFUNC_P (from))
899     {
900       tree fromfn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (from));
901       tree tofn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (to));
902       tree fbase = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (fromfn)));
903       tree tbase = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (tofn)));
904 
905       if (!DERIVED_FROM_P (fbase, tbase)
906 	  || !same_type_p (TREE_TYPE (fromfn), TREE_TYPE (tofn))
907 	  || !compparms (TREE_CHAIN (TYPE_ARG_TYPES (fromfn)),
908 			 TREE_CHAIN (TYPE_ARG_TYPES (tofn)))
909 	  || cp_type_quals (fbase) != cp_type_quals (tbase))
910 	return 0;
911 
912       from = cp_build_qualified_type (tbase, cp_type_quals (fbase));
913       from = build_cplus_method_type (from, TREE_TYPE (fromfn),
914 				      TREE_CHAIN (TYPE_ARG_TYPES (fromfn)));
915       from = build_ptrmemfunc_type (build_pointer_type (from));
916       conv = build_conv (PMEM_CONV, from, conv);
917     }
918   else if (tcode == BOOLEAN_TYPE)
919     {
920       if (! (INTEGRAL_CODE_P (fcode) || fcode == REAL_TYPE
921 	     || fcode == POINTER_TYPE || TYPE_PTRMEMFUNC_P (from)))
922 	return 0;
923 
924       conv = build_conv (STD_CONV, to, conv);
925       if (fcode == POINTER_TYPE
926 	  || (TYPE_PTRMEMFUNC_P (from) && ICS_STD_RANK (conv) < PBOOL_RANK))
927 	ICS_STD_RANK (conv) = PBOOL_RANK;
928     }
929   /* We don't check for ENUMERAL_TYPE here because there are no standard
930      conversions to enum type.  */
931   else if (tcode == INTEGER_TYPE || tcode == BOOLEAN_TYPE
932 	   || tcode == REAL_TYPE)
933     {
934       if (! (INTEGRAL_CODE_P (fcode) || fcode == REAL_TYPE))
935 	return 0;
936       conv = build_conv (STD_CONV, to, conv);
937 
938       /* Give this a better rank if it's a promotion.  */
939       if (to == type_promotes_to (from)
940 	  && ICS_STD_RANK (TREE_OPERAND (conv, 0)) <= PROMO_RANK)
941 	ICS_STD_RANK (conv) = PROMO_RANK;
942     }
943   else if (IS_AGGR_TYPE (to) && IS_AGGR_TYPE (from)
944 	   && is_properly_derived_from (from, to))
945     {
946       if (TREE_CODE (conv) == RVALUE_CONV)
947 	conv = TREE_OPERAND (conv, 0);
948       conv = build_conv (BASE_CONV, to, conv);
949       /* The derived-to-base conversion indicates the initialization
950 	 of a parameter with base type from an object of a derived
951 	 type.  A temporary object is created to hold the result of
952 	 the conversion.  */
953       NEED_TEMPORARY_P (conv) = 1;
954     }
955   else
956     return 0;
957 
958   return conv;
959 }
960 
961 /* Returns nonzero if T1 is reference-related to T2.  */
962 
963 static int
reference_related_p(t1,t2)964 reference_related_p (t1, t2)
965      tree t1;
966      tree t2;
967 {
968   t1 = TYPE_MAIN_VARIANT (t1);
969   t2 = TYPE_MAIN_VARIANT (t2);
970 
971   /* [dcl.init.ref]
972 
973      Given types "cv1 T1" and "cv2 T2," "cv1 T1" is reference-related
974      to "cv2 T2" if T1 is the same type as T2, or T1 is a base class
975      of T2.  */
976   return (same_type_p (t1, t2)
977 	  || (CLASS_TYPE_P (t1) && CLASS_TYPE_P (t2)
978 	      && DERIVED_FROM_P (t1, t2)));
979 }
980 
981 /* Returns nonzero if T1 is reference-compatible with T2.  */
982 
983 static int
reference_compatible_p(t1,t2)984 reference_compatible_p (t1, t2)
985      tree t1;
986      tree t2;
987 {
988   /* [dcl.init.ref]
989 
990      "cv1 T1" is reference compatible with "cv2 T2" if T1 is
991      reference-related to T2 and cv1 is the same cv-qualification as,
992      or greater cv-qualification than, cv2.  */
993   return (reference_related_p (t1, t2)
994 	  && at_least_as_qualified_p (t1, t2));
995 }
996 
997 /* Determine whether or not the EXPR (of class type S) can be
998    converted to T as in [over.match.ref].  */
999 
1000 static tree
convert_class_to_reference(t,s,expr)1001 convert_class_to_reference (t, s, expr)
1002      tree t;
1003      tree s;
1004      tree expr;
1005 {
1006   tree conversions;
1007   tree arglist;
1008   tree conv;
1009   tree reference_type;
1010   struct z_candidate *candidates;
1011   struct z_candidate *cand;
1012 
1013   conversions = lookup_conversions (s);
1014   if (!conversions)
1015     return NULL_TREE;
1016 
1017   /* [over.match.ref]
1018 
1019      Assuming that "cv1 T" is the underlying type of the reference
1020      being initialized, and "cv S" is the type of the initializer
1021      expression, with S a class type, the candidate functions are
1022      selected as follows:
1023 
1024      --The conversion functions of S and its base classes are
1025        considered.  Those that are not hidden within S and yield type
1026        "reference to cv2 T2", where "cv1 T" is reference-compatible
1027        (_dcl.init.ref_) with "cv2 T2", are candidate functions.
1028 
1029      The argument list has one argument, which is the initializer
1030      expression.  */
1031 
1032   candidates = 0;
1033 
1034   /* Conceptually, we should take the address of EXPR and put it in
1035      the argument list.  Unfortunately, however, that can result in
1036      error messages, which we should not issue now because we are just
1037      trying to find a conversion operator.  Therefore, we use NULL,
1038      cast to the appropriate type.  */
1039   arglist = build_int_2 (0, 0);
1040   TREE_TYPE (arglist) = build_pointer_type (s);
1041   arglist = build_tree_list (NULL_TREE, arglist);
1042 
1043   reference_type = build_reference_type (t);
1044 
1045   while (conversions)
1046     {
1047       tree fns = TREE_VALUE (conversions);
1048 
1049       for (; fns; fns = OVL_NEXT (fns))
1050 	{
1051 	  tree f = OVL_CURRENT (fns);
1052 	  tree t2 = TREE_TYPE (TREE_TYPE (f));
1053 
1054 	  cand = NULL;
1055 
1056 	  /* If this is a template function, try to get an exact
1057              match.  */
1058 	  if (TREE_CODE (f) == TEMPLATE_DECL)
1059 	    {
1060 	      cand = add_template_candidate (&candidates,
1061 					     f, s,
1062 					     NULL_TREE,
1063 					     arglist,
1064 					     reference_type,
1065 					     TYPE_BINFO (s),
1066 					     TREE_PURPOSE (conversions),
1067 					     LOOKUP_NORMAL,
1068 					     DEDUCE_CONV);
1069 
1070 	      if (cand)
1071 		{
1072 		  /* Now, see if the conversion function really returns
1073 		     an lvalue of the appropriate type.  From the
1074 		     point of view of unification, simply returning an
1075 		     rvalue of the right type is good enough.  */
1076 		  f = cand->fn;
1077 		  t2 = TREE_TYPE (TREE_TYPE (f));
1078 		  if (TREE_CODE (t2) != REFERENCE_TYPE
1079 		      || !reference_compatible_p (t, TREE_TYPE (t2)))
1080 		    {
1081 		      candidates = candidates->next;
1082 		      cand = NULL;
1083 		    }
1084 		}
1085 	    }
1086 	  else if (TREE_CODE (t2) == REFERENCE_TYPE
1087 		   && reference_compatible_p (t, TREE_TYPE (t2)))
1088 	    cand = add_function_candidate (&candidates, f, s, arglist,
1089 					   TYPE_BINFO (s),
1090 					   TREE_PURPOSE (conversions),
1091 					   LOOKUP_NORMAL);
1092 
1093 	  if (cand)
1094             {
1095               /* Build a standard conversion sequence indicating the
1096                  binding from the reference type returned by the
1097                  function to the desired REFERENCE_TYPE.  */
1098               cand->second_conv
1099                 = (direct_reference_binding
1100                    (reference_type,
1101                     build1 (IDENTITY_CONV,
1102                             TREE_TYPE (TREE_TYPE (TREE_TYPE (cand->fn))),
1103                             NULL_TREE)));
1104               ICS_BAD_FLAG (cand->second_conv)
1105                 |= ICS_BAD_FLAG (TREE_VEC_ELT (cand->convs, 0));
1106             }
1107 	}
1108       conversions = TREE_CHAIN (conversions);
1109     }
1110 
1111   /* If none of the conversion functions worked out, let our caller
1112      know.  */
1113   if (!any_viable (candidates))
1114     return NULL_TREE;
1115 
1116   candidates = splice_viable (candidates);
1117   cand = tourney (candidates);
1118   if (!cand)
1119     return NULL_TREE;
1120 
1121   /* Build a user-defined conversion sequence representing the
1122      conversion.  */
1123   conv = build_conv (USER_CONV,
1124 		     TREE_TYPE (TREE_TYPE (cand->fn)),
1125 		     build1 (IDENTITY_CONV, TREE_TYPE (expr), expr));
1126   TREE_OPERAND (conv, 1) = build_zc_wrapper (cand);
1127 
1128   /* Merge it with the standard conversion sequence from the
1129      conversion function's return type to the desired type.  */
1130   cand->second_conv = merge_conversion_sequences (conv, cand->second_conv);
1131 
1132   if (cand->viable == -1)
1133     ICS_BAD_FLAG (conv) = 1;
1134 
1135   return cand->second_conv;
1136 }
1137 
1138 /* A reference of the indicated TYPE is being bound directly to the
1139    expression represented by the implicit conversion sequence CONV.
1140    Return a conversion sequence for this binding.  */
1141 
1142 static tree
direct_reference_binding(type,conv)1143 direct_reference_binding (type, conv)
1144      tree type;
1145      tree conv;
1146 {
1147   tree t;
1148 
1149   my_friendly_assert (TREE_CODE (type) == REFERENCE_TYPE, 20030306);
1150   my_friendly_assert (TREE_CODE (TREE_TYPE (conv)) != REFERENCE_TYPE,
1151 		      20030306);
1152 
1153   t = TREE_TYPE (type);
1154 
1155   /* [over.ics.rank]
1156 
1157      When a parameter of reference type binds directly
1158      (_dcl.init.ref_) to an argument expression, the implicit
1159      conversion sequence is the identity conversion, unless the
1160      argument expression has a type that is a derived class of the
1161      parameter type, in which case the implicit conversion sequence is
1162      a derived-to-base Conversion.
1163 
1164      If the parameter binds directly to the result of applying a
1165      conversion function to the argument expression, the implicit
1166      conversion sequence is a user-defined conversion sequence
1167      (_over.ics.user_), with the second standard conversion sequence
1168      either an identity conversion or, if the conversion function
1169      returns an entity of a type that is a derived class of the
1170      parameter type, a derived-to-base conversion.  */
1171   if (!same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (conv)))
1172     {
1173       /* Represent the derived-to-base conversion.  */
1174       conv = build_conv (BASE_CONV, t, conv);
1175       /* We will actually be binding to the base-class subobject in
1176 	 the derived class, so we mark this conversion appropriately.
1177 	 That way, convert_like knows not to generate a temporary.  */
1178       NEED_TEMPORARY_P (conv) = 0;
1179     }
1180   return build_conv (REF_BIND, type, conv);
1181 }
1182 
1183 /* Returns the conversion path from type FROM to reference type TO for
1184    purposes of reference binding.  For lvalue binding, either pass a
1185    reference type to FROM or an lvalue expression to EXPR.  If the
1186    reference will be bound to a temporary, NEED_TEMPORARY_P is set for
1187    the conversion returned.  */
1188 
1189 static tree
reference_binding(tree rto,tree rfrom,tree expr,int flags)1190 reference_binding (tree rto, tree rfrom, tree expr, int flags)
1191 {
1192   tree conv = NULL_TREE;
1193   tree to = TREE_TYPE (rto);
1194   tree from = rfrom;
1195   int related_p;
1196   int compatible_p;
1197   cp_lvalue_kind lvalue_p = clk_none;
1198 
1199   if (TREE_CODE (to) == FUNCTION_TYPE && expr && type_unknown_p (expr))
1200     {
1201       expr = instantiate_type (to, expr, tf_none);
1202       if (expr == error_mark_node)
1203 	return NULL_TREE;
1204       from = TREE_TYPE (expr);
1205     }
1206 
1207   if (TREE_CODE (from) == REFERENCE_TYPE)
1208     {
1209       /* Anything with reference type is an lvalue.  */
1210       lvalue_p = clk_ordinary;
1211       from = TREE_TYPE (from);
1212     }
1213   else if (expr)
1214     lvalue_p = real_lvalue_p (expr);
1215 
1216   /* Figure out whether or not the types are reference-related and
1217      reference compatible.  We have do do this after stripping
1218      references from FROM.  */
1219   related_p = reference_related_p (to, from);
1220   compatible_p = reference_compatible_p (to, from);
1221 
1222   if (lvalue_p && compatible_p)
1223     {
1224       /* [dcl.init.ref]
1225 
1226 	 If the initializer expression
1227 
1228 	 -- is an lvalue (but not an lvalue for a bit-field), and "cv1 T1"
1229 	    is reference-compatible with "cv2 T2,"
1230 
1231 	 the reference is bound directly to the initializer exprssion
1232 	 lvalue.  */
1233       conv = build1 (IDENTITY_CONV, from, expr);
1234       conv = direct_reference_binding (rto, conv);
1235       if ((lvalue_p & clk_bitfield) != 0
1236 	  && CP_TYPE_CONST_NON_VOLATILE_P (to))
1237 	/* For the purposes of overload resolution, we ignore the fact
1238 	   this expression is a bitfield. (In particular,
1239 	   [over.ics.ref] says specifically that a function with a
1240 	   non-const reference parameter is viable even if the
1241 	   argument is a bitfield.)
1242 
1243 	   However, when we actually call the function we must create
1244 	   a temporary to which to bind the reference.  If the
1245 	   reference is volatile, or isn't const, then we cannot make
1246 	   a temporary, so we just issue an error when the conversion
1247 	   actually occurs.  */
1248 	NEED_TEMPORARY_P (conv) = 1;
1249       return conv;
1250     }
1251   else if (CLASS_TYPE_P (from) && !(flags & LOOKUP_NO_CONVERSION))
1252     {
1253       /* [dcl.init.ref]
1254 
1255 	 If the initializer exprsesion
1256 
1257 	 -- has a class type (i.e., T2 is a class type) can be
1258 	    implicitly converted to an lvalue of type "cv3 T3," where
1259 	    "cv1 T1" is reference-compatible with "cv3 T3".  (this
1260 	    conversion is selected by enumerating the applicable
1261 	    conversion functions (_over.match.ref_) and choosing the
1262 	    best one through overload resolution.  (_over.match_).
1263 
1264         the reference is bound to the lvalue result of the conversion
1265 	in the second case.  */
1266       conv = convert_class_to_reference (to, from, expr);
1267       if (conv)
1268 	return conv;
1269     }
1270 
1271   /* From this point on, we conceptually need temporaries, even if we
1272      elide them.  Only the cases above are "direct bindings".  */
1273   if (flags & LOOKUP_NO_TEMP_BIND)
1274     return NULL_TREE;
1275 
1276   /* [over.ics.rank]
1277 
1278      When a parameter of reference type is not bound directly to an
1279      argument expression, the conversion sequence is the one required
1280      to convert the argument expression to the underlying type of the
1281      reference according to _over.best.ics_.  Conceptually, this
1282      conversion sequence corresponds to copy-initializing a temporary
1283      of the underlying type with the argument expression.  Any
1284      difference in top-level cv-qualification is subsumed by the
1285      initialization itself and does not constitute a conversion.  */
1286 
1287   /* [dcl.init.ref]
1288 
1289      Otherwise, the reference shall be to a non-volatile const type.  */
1290   if (!CP_TYPE_CONST_NON_VOLATILE_P (to))
1291     return NULL_TREE;
1292 
1293   /* [dcl.init.ref]
1294 
1295      If the initializer expression is an rvalue, with T2 a class type,
1296      and "cv1 T1" is reference-compatible with "cv2 T2", the reference
1297      is bound in one of the following ways:
1298 
1299      -- The reference is bound to the object represented by the rvalue
1300         or to a sub-object within that object.
1301 
1302      -- ...
1303 
1304      We use the first alternative.  The implicit conversion sequence
1305      is supposed to be same as we would obtain by generating a
1306      temporary.  Fortunately, if the types are reference compatible,
1307      then this is either an identity conversion or the derived-to-base
1308      conversion, just as for direct binding.  */
1309   if (CLASS_TYPE_P (from) && compatible_p)
1310     {
1311       conv = build1 (IDENTITY_CONV, from, expr);
1312       return direct_reference_binding (rto, conv);
1313     }
1314 
1315   /* [dcl.init.ref]
1316 
1317      Otherwise, a temporary of type "cv1 T1" is created and
1318      initialized from the initializer expression using the rules for a
1319      non-reference copy initialization.  If T1 is reference-related to
1320      T2, cv1 must be the same cv-qualification as, or greater
1321      cv-qualification than, cv2; otherwise, the program is ill-formed.  */
1322   if (related_p && !at_least_as_qualified_p (to, from))
1323     return NULL_TREE;
1324 
1325   conv = implicit_conversion (to, from, expr, flags);
1326   if (!conv)
1327     return NULL_TREE;
1328 
1329   conv = build_conv (REF_BIND, rto, conv);
1330   /* This reference binding, unlike those above, requires the
1331      creation of a temporary.  */
1332   NEED_TEMPORARY_P (conv) = 1;
1333 
1334   return conv;
1335 }
1336 
1337 /* Returns the implicit conversion sequence (see [over.ics]) from type FROM
1338    to type TO.  The optional expression EXPR may affect the conversion.
1339    FLAGS are the usual overloading flags.  Only LOOKUP_NO_CONVERSION is
1340    significant.  */
1341 
1342 static tree
implicit_conversion(to,from,expr,flags)1343 implicit_conversion (to, from, expr, flags)
1344      tree to, from, expr;
1345      int flags;
1346 {
1347   tree conv;
1348 
1349   /* Resolve expressions like `A::p' that we thought might become
1350      pointers-to-members.  */
1351   if (expr && TREE_CODE (expr) == OFFSET_REF)
1352     {
1353       expr = resolve_offset_ref (expr);
1354       from = TREE_TYPE (expr);
1355     }
1356 
1357   if (from == error_mark_node || to == error_mark_node
1358       || expr == error_mark_node)
1359     return NULL_TREE;
1360 
1361   if (TREE_CODE (to) == REFERENCE_TYPE)
1362     conv = reference_binding (to, from, expr, flags);
1363   else
1364     conv = standard_conversion (to, from, expr);
1365 
1366   if (conv)
1367     ;
1368   else if (expr != NULL_TREE
1369 	   && (IS_AGGR_TYPE (from)
1370 	       || IS_AGGR_TYPE (to))
1371 	   && (flags & LOOKUP_NO_CONVERSION) == 0)
1372     {
1373       struct z_candidate *cand;
1374 
1375       cand = build_user_type_conversion_1
1376 	(to, expr, LOOKUP_ONLYCONVERTING);
1377       if (cand)
1378 	conv = cand->second_conv;
1379 
1380       /* We used to try to bind a reference to a temporary here, but that
1381 	 is now handled by the recursive call to this function at the end
1382 	 of reference_binding.  */
1383     }
1384 
1385   return conv;
1386 }
1387 
1388 /* Add a new entry to the list of candidates.  Used by the add_*_candidate
1389    functions.  */
1390 
1391 static struct z_candidate *
add_candidate(struct z_candidate ** candidates,tree fn,tree convs,tree access_path,tree conversion_path,int viable)1392 add_candidate (struct z_candidate **candidates,
1393 	       tree fn, tree convs, tree access_path, tree
1394 	       conversion_path, int viable)
1395 {
1396   struct z_candidate *cand
1397     = (struct z_candidate *) ggc_alloc_cleared (sizeof (struct z_candidate));
1398 
1399   cand->fn = fn;
1400   cand->convs = convs;
1401   cand->access_path = access_path;
1402   cand->conversion_path = conversion_path;
1403   cand->viable = viable;
1404   cand->next = *candidates;
1405   *candidates = cand;
1406 
1407   return cand;
1408 }
1409 
1410 /* Create an overload candidate for the function or method FN called with
1411    the argument list ARGLIST and add it to CANDIDATES.  FLAGS is passed on
1412    to implicit_conversion.
1413 
1414    CTYPE, if non-NULL, is the type we want to pretend this function
1415    comes from for purposes of overload resolution.  */
1416 
1417 static struct z_candidate *
add_function_candidate(struct z_candidate ** candidates,tree fn,tree ctype,tree arglist,tree access_path,tree conversion_path,int flags)1418 add_function_candidate (struct z_candidate **candidates,
1419 			tree fn, tree ctype, tree arglist,
1420 			tree access_path, tree conversion_path,
1421 			int flags)
1422 {
1423   tree parmlist = TYPE_ARG_TYPES (TREE_TYPE (fn));
1424   int i, len;
1425   tree convs;
1426   tree parmnode, argnode;
1427   int viable = 1;
1428 
1429   /* Built-in functions that haven't been declared don't really
1430      exist.  */
1431   if (DECL_ANTICIPATED (fn))
1432     return NULL;
1433 
1434   /* The `this', `in_chrg' and VTT arguments to constructors are not
1435      considered in overload resolution.  */
1436   if (DECL_CONSTRUCTOR_P (fn))
1437     {
1438       parmlist = skip_artificial_parms_for (fn, parmlist);
1439       arglist = skip_artificial_parms_for (fn, arglist);
1440     }
1441 
1442   len = list_length (arglist);
1443   convs = make_tree_vec (len);
1444 
1445   /* 13.3.2 - Viable functions [over.match.viable]
1446      First, to be a viable function, a candidate function shall have enough
1447      parameters to agree in number with the arguments in the list.
1448 
1449      We need to check this first; otherwise, checking the ICSes might cause
1450      us to produce an ill-formed template instantiation.  */
1451 
1452   parmnode = parmlist;
1453   for (i = 0; i < len; ++i)
1454     {
1455       if (parmnode == NULL_TREE || parmnode == void_list_node)
1456 	break;
1457       parmnode = TREE_CHAIN (parmnode);
1458     }
1459 
1460   if (i < len && parmnode)
1461     viable = 0;
1462 
1463   /* Make sure there are default args for the rest of the parms.  */
1464   else if (!sufficient_parms_p (parmnode))
1465     viable = 0;
1466 
1467   if (! viable)
1468     goto out;
1469 
1470   /* Second, for F to be a viable function, there shall exist for each
1471      argument an implicit conversion sequence that converts that argument
1472      to the corresponding parameter of F.  */
1473 
1474   parmnode = parmlist;
1475   argnode = arglist;
1476 
1477   for (i = 0; i < len; ++i)
1478     {
1479       tree arg = TREE_VALUE (argnode);
1480       tree argtype = lvalue_type (arg);
1481       tree t;
1482       int is_this;
1483 
1484       if (parmnode == void_list_node)
1485 	break;
1486 
1487       is_this = (i == 0 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
1488 		 && ! DECL_CONSTRUCTOR_P (fn));
1489 
1490       if (parmnode)
1491 	{
1492 	  tree parmtype = TREE_VALUE (parmnode);
1493 
1494 	  /* The type of the implicit object parameter ('this') for
1495 	     overload resolution is not always the same as for the
1496 	     function itself; conversion functions are considered to
1497 	     be members of the class being converted, and functions
1498 	     introduced by a using-declaration are considered to be
1499 	     members of the class that uses them.
1500 
1501 	     Since build_over_call ignores the ICS for the `this'
1502 	     parameter, we can just change the parm type.  */
1503 	  if (ctype && is_this)
1504 	    {
1505 	      parmtype
1506 		= build_qualified_type (ctype,
1507 					TYPE_QUALS (TREE_TYPE (parmtype)));
1508 	      parmtype = build_pointer_type (parmtype);
1509 	    }
1510 
1511 	  t = implicit_conversion (parmtype, argtype, arg, flags);
1512 	}
1513       else
1514 	{
1515 	  t = build1 (IDENTITY_CONV, argtype, arg);
1516 	  ICS_ELLIPSIS_FLAG (t) = 1;
1517 	}
1518 
1519       if (t && is_this)
1520 	ICS_THIS_FLAG (t) = 1;
1521 
1522       TREE_VEC_ELT (convs, i) = t;
1523       if (! t)
1524 	{
1525 	  viable = 0;
1526 	  break;
1527 	}
1528 
1529       if (ICS_BAD_FLAG (t))
1530 	viable = -1;
1531 
1532       if (parmnode)
1533 	parmnode = TREE_CHAIN (parmnode);
1534       argnode = TREE_CHAIN (argnode);
1535     }
1536 
1537  out:
1538   return add_candidate (candidates, fn, convs, access_path,
1539 			conversion_path, viable);
1540 }
1541 
1542 /* Create an overload candidate for the conversion function FN which will
1543    be invoked for expression OBJ, producing a pointer-to-function which
1544    will in turn be called with the argument list ARGLIST, and add it to
1545    CANDIDATES.  FLAGS is passed on to implicit_conversion.
1546 
1547    Actually, we don't really care about FN; we care about the type it
1548    converts to.  There may be multiple conversion functions that will
1549    convert to that type, and we rely on build_user_type_conversion_1 to
1550    choose the best one; so when we create our candidate, we record the type
1551    instead of the function.  */
1552 
1553 static struct z_candidate *
add_conv_candidate(candidates,fn,obj,arglist,access_path,conversion_path)1554 add_conv_candidate (candidates, fn, obj, arglist, access_path,
1555 		    conversion_path)
1556      struct z_candidate **candidates;
1557      tree fn, obj, arglist;
1558      tree access_path;
1559      tree conversion_path;
1560 {
1561   tree totype = TREE_TYPE (TREE_TYPE (fn));
1562   int i, len, viable, flags;
1563   tree parmlist, convs, parmnode, argnode;
1564 
1565   for (parmlist = totype; TREE_CODE (parmlist) != FUNCTION_TYPE; )
1566     parmlist = TREE_TYPE (parmlist);
1567   parmlist = TYPE_ARG_TYPES (parmlist);
1568 
1569   len = list_length (arglist) + 1;
1570   convs = make_tree_vec (len);
1571   parmnode = parmlist;
1572   argnode = arglist;
1573   viable = 1;
1574   flags = LOOKUP_NORMAL;
1575 
1576   /* Don't bother looking up the same type twice.  */
1577   if (*candidates && (*candidates)->fn == totype)
1578     return NULL;
1579 
1580   for (i = 0; i < len; ++i)
1581     {
1582       tree arg = i == 0 ? obj : TREE_VALUE (argnode);
1583       tree argtype = lvalue_type (arg);
1584       tree t;
1585 
1586       if (i == 0)
1587 	t = implicit_conversion (totype, argtype, arg, flags);
1588       else if (parmnode == void_list_node)
1589 	break;
1590       else if (parmnode)
1591 	t = implicit_conversion (TREE_VALUE (parmnode), argtype, arg, flags);
1592       else
1593 	{
1594 	  t = build1 (IDENTITY_CONV, argtype, arg);
1595 	  ICS_ELLIPSIS_FLAG (t) = 1;
1596 	}
1597 
1598       TREE_VEC_ELT (convs, i) = t;
1599       if (! t)
1600 	break;
1601 
1602       if (ICS_BAD_FLAG (t))
1603 	viable = -1;
1604 
1605       if (i == 0)
1606 	continue;
1607 
1608       if (parmnode)
1609 	parmnode = TREE_CHAIN (parmnode);
1610       argnode = TREE_CHAIN (argnode);
1611     }
1612 
1613   if (i < len)
1614     viable = 0;
1615 
1616   if (!sufficient_parms_p (parmnode))
1617     viable = 0;
1618 
1619   return add_candidate (candidates, totype, convs, access_path,
1620 			conversion_path, viable);
1621 }
1622 
1623 static void
build_builtin_candidate(candidates,fnname,type1,type2,args,argtypes,flags)1624 build_builtin_candidate (candidates, fnname, type1, type2,
1625 			 args, argtypes, flags)
1626      struct z_candidate **candidates;
1627      tree fnname, type1, type2, *args, *argtypes;
1628      int flags;
1629 
1630 {
1631   tree t, convs;
1632   int viable = 1, i;
1633   tree types[2];
1634 
1635   types[0] = type1;
1636   types[1] = type2;
1637 
1638   convs = make_tree_vec (args[2] ? 3 : (args[1] ? 2 : 1));
1639 
1640   for (i = 0; i < 2; ++i)
1641     {
1642       if (! args[i])
1643 	break;
1644 
1645       t = implicit_conversion (types[i], argtypes[i], args[i], flags);
1646       if (! t)
1647 	{
1648 	  viable = 0;
1649 	  /* We need something for printing the candidate.  */
1650 	  t = build1 (IDENTITY_CONV, types[i], NULL_TREE);
1651 	}
1652       else if (ICS_BAD_FLAG (t))
1653 	viable = 0;
1654       TREE_VEC_ELT (convs, i) = t;
1655     }
1656 
1657   /* For COND_EXPR we rearranged the arguments; undo that now.  */
1658   if (args[2])
1659     {
1660       TREE_VEC_ELT (convs, 2) = TREE_VEC_ELT (convs, 1);
1661       TREE_VEC_ELT (convs, 1) = TREE_VEC_ELT (convs, 0);
1662       t = implicit_conversion (boolean_type_node, argtypes[2], args[2], flags);
1663       if (t)
1664 	TREE_VEC_ELT (convs, 0) = t;
1665       else
1666 	viable = 0;
1667     }
1668 
1669   add_candidate (candidates, fnname, convs,
1670 		 /*access_path=*/NULL_TREE,
1671 		 /*conversion_path=*/NULL_TREE,
1672 		 viable);
1673 }
1674 
1675 static int
is_complete(t)1676 is_complete (t)
1677      tree t;
1678 {
1679   return COMPLETE_TYPE_P (complete_type (t));
1680 }
1681 
1682 /* Returns nonzero if TYPE is a promoted arithmetic type.  */
1683 
1684 static int
promoted_arithmetic_type_p(type)1685 promoted_arithmetic_type_p (type)
1686      tree type;
1687 {
1688   /* [over.built]
1689 
1690      In this section, the term promoted integral type is used to refer
1691      to those integral types which are preserved by integral promotion
1692      (including e.g.  int and long but excluding e.g.  char).
1693      Similarly, the term promoted arithmetic type refers to promoted
1694      integral types plus floating types.  */
1695   return ((INTEGRAL_TYPE_P (type)
1696 	   && same_type_p (type_promotes_to (type), type))
1697 	  || TREE_CODE (type) == REAL_TYPE);
1698 }
1699 
1700 /* Create any builtin operator overload candidates for the operator in
1701    question given the converted operand types TYPE1 and TYPE2.  The other
1702    args are passed through from add_builtin_candidates to
1703    build_builtin_candidate.
1704 
1705    TYPE1 and TYPE2 may not be permissible, and we must filter them.
1706    If CODE is requires candidates operands of the same type of the kind
1707    of which TYPE1 and TYPE2 are, we add both candidates
1708    CODE (TYPE1, TYPE1) and CODE (TYPE2, TYPE2).  */
1709 
1710 static void
add_builtin_candidate(candidates,code,code2,fnname,type1,type2,args,argtypes,flags)1711 add_builtin_candidate (candidates, code, code2, fnname, type1, type2,
1712 		       args, argtypes, flags)
1713      struct z_candidate **candidates;
1714      enum tree_code code, code2;
1715      tree fnname, type1, type2, *args, *argtypes;
1716      int flags;
1717 {
1718   switch (code)
1719     {
1720     case POSTINCREMENT_EXPR:
1721     case POSTDECREMENT_EXPR:
1722       args[1] = integer_zero_node;
1723       type2 = integer_type_node;
1724       break;
1725     default:
1726       break;
1727     }
1728 
1729   switch (code)
1730     {
1731 
1732 /* 4 For every pair T, VQ), where T is an arithmetic or  enumeration  type,
1733      and  VQ  is  either  volatile or empty, there exist candidate operator
1734      functions of the form
1735 	     VQ T&   operator++(VQ T&);
1736 	     T       operator++(VQ T&, int);
1737    5 For every pair T, VQ), where T is an enumeration type or an arithmetic
1738      type  other than bool, and VQ is either volatile or empty, there exist
1739      candidate operator functions of the form
1740 	     VQ T&   operator--(VQ T&);
1741 	     T       operator--(VQ T&, int);
1742    6 For every pair T, VQ), where T is  a  cv-qualified  or  cv-unqualified
1743      complete  object type, and VQ is either volatile or empty, there exist
1744      candidate operator functions of the form
1745 	     T*VQ&   operator++(T*VQ&);
1746 	     T*VQ&   operator--(T*VQ&);
1747 	     T*      operator++(T*VQ&, int);
1748 	     T*      operator--(T*VQ&, int);  */
1749 
1750     case POSTDECREMENT_EXPR:
1751     case PREDECREMENT_EXPR:
1752       if (TREE_CODE (type1) == BOOLEAN_TYPE)
1753 	return;
1754     case POSTINCREMENT_EXPR:
1755     case PREINCREMENT_EXPR:
1756       if (ARITHMETIC_TYPE_P (type1) || TYPE_PTROB_P (type1))
1757 	{
1758 	  type1 = build_reference_type (type1);
1759 	  break;
1760 	}
1761       return;
1762 
1763 /* 7 For every cv-qualified or cv-unqualified complete object type T, there
1764      exist candidate operator functions of the form
1765 
1766 	     T&      operator*(T*);
1767 
1768    8 For every function type T, there exist candidate operator functions of
1769      the form
1770 	     T&      operator*(T*);  */
1771 
1772     case INDIRECT_REF:
1773       if (TREE_CODE (type1) == POINTER_TYPE
1774 	  && (TYPE_PTROB_P (type1)
1775 	      || TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE))
1776 	break;
1777       return;
1778 
1779 /* 9 For every type T, there exist candidate operator functions of the form
1780 	     T*      operator+(T*);
1781 
1782    10For  every  promoted arithmetic type T, there exist candidate operator
1783      functions of the form
1784 	     T       operator+(T);
1785 	     T       operator-(T);  */
1786 
1787     case CONVERT_EXPR: /* unary + */
1788       if (TREE_CODE (type1) == POINTER_TYPE
1789 	  && TREE_CODE (TREE_TYPE (type1)) != OFFSET_TYPE)
1790 	break;
1791     case NEGATE_EXPR:
1792       if (ARITHMETIC_TYPE_P (type1))
1793 	break;
1794       return;
1795 
1796 /* 11For every promoted integral type T,  there  exist  candidate  operator
1797      functions of the form
1798 	     T       operator~(T);  */
1799 
1800     case BIT_NOT_EXPR:
1801       if (INTEGRAL_TYPE_P (type1))
1802 	break;
1803       return;
1804 
1805 /* 12For every quintuple C1, C2, T, CV1, CV2), where C2 is a class type, C1
1806      is the same type as C2 or is a derived class of C2, T  is  a  complete
1807      object type or a function type, and CV1 and CV2 are cv-qualifier-seqs,
1808      there exist candidate operator functions of the form
1809 	     CV12 T& operator->*(CV1 C1*, CV2 T C2::*);
1810      where CV12 is the union of CV1 and CV2.  */
1811 
1812     case MEMBER_REF:
1813       if (TREE_CODE (type1) == POINTER_TYPE
1814 	  && (TYPE_PTRMEMFUNC_P (type2) || TYPE_PTRMEM_P (type2)))
1815 	{
1816 	  tree c1 = TREE_TYPE (type1);
1817 	  tree c2 = (TYPE_PTRMEMFUNC_P (type2)
1818 		     ? TYPE_METHOD_BASETYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (type2)))
1819 		     : TYPE_OFFSET_BASETYPE (TREE_TYPE (type2)));
1820 
1821 	  if (IS_AGGR_TYPE (c1) && DERIVED_FROM_P (c2, c1)
1822 	      && (TYPE_PTRMEMFUNC_P (type2)
1823 		  || is_complete (TREE_TYPE (TREE_TYPE (type2)))))
1824 	    break;
1825 	}
1826       return;
1827 
1828 /* 13For every pair of promoted arithmetic types L and R, there exist  can-
1829      didate operator functions of the form
1830 	     LR      operator*(L, R);
1831 	     LR      operator/(L, R);
1832 	     LR      operator+(L, R);
1833 	     LR      operator-(L, R);
1834 	     bool    operator<(L, R);
1835 	     bool    operator>(L, R);
1836 	     bool    operator<=(L, R);
1837 	     bool    operator>=(L, R);
1838 	     bool    operator==(L, R);
1839 	     bool    operator!=(L, R);
1840      where  LR  is  the  result of the usual arithmetic conversions between
1841      types L and R.
1842 
1843    14For every pair of types T and I, where T  is  a  cv-qualified  or  cv-
1844      unqualified  complete  object  type and I is a promoted integral type,
1845      there exist candidate operator functions of the form
1846 	     T*      operator+(T*, I);
1847 	     T&      operator[](T*, I);
1848 	     T*      operator-(T*, I);
1849 	     T*      operator+(I, T*);
1850 	     T&      operator[](I, T*);
1851 
1852    15For every T, where T is a pointer to complete object type, there exist
1853      candidate operator functions of the form112)
1854 	     ptrdiff_t operator-(T, T);
1855 
1856    16For every pointer or enumeration type T, there exist candidate operator
1857      functions of the form
1858 	     bool    operator<(T, T);
1859 	     bool    operator>(T, T);
1860 	     bool    operator<=(T, T);
1861 	     bool    operator>=(T, T);
1862 	     bool    operator==(T, T);
1863 	     bool    operator!=(T, T);
1864 
1865    17For every pointer to member type T,  there  exist  candidate  operator
1866      functions of the form
1867 	     bool    operator==(T, T);
1868 	     bool    operator!=(T, T);  */
1869 
1870     case MINUS_EXPR:
1871       if (TYPE_PTROB_P (type1) && TYPE_PTROB_P (type2))
1872 	break;
1873       if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2))
1874 	{
1875 	  type2 = ptrdiff_type_node;
1876 	  break;
1877 	}
1878     case MULT_EXPR:
1879     case TRUNC_DIV_EXPR:
1880       if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1881 	break;
1882       return;
1883 
1884     case EQ_EXPR:
1885     case NE_EXPR:
1886       if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2))
1887 	  || (TYPE_PTRMEM_P (type1) && TYPE_PTRMEM_P (type2)))
1888 	break;
1889       if ((TYPE_PTRMEMFUNC_P (type1) || TYPE_PTRMEM_P (type1))
1890 	  && null_ptr_cst_p (args[1]))
1891 	{
1892 	  type2 = type1;
1893 	  break;
1894 	}
1895       if ((TYPE_PTRMEMFUNC_P (type2) || TYPE_PTRMEM_P (type2))
1896 	  && null_ptr_cst_p (args[0]))
1897 	{
1898 	  type1 = type2;
1899 	  break;
1900 	}
1901       /* FALLTHROUGH */
1902     case LT_EXPR:
1903     case GT_EXPR:
1904     case LE_EXPR:
1905     case GE_EXPR:
1906     case MAX_EXPR:
1907     case MIN_EXPR:
1908       if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1909         break;
1910       if (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
1911 	break;
1912       if (TREE_CODE (type1) == ENUMERAL_TYPE && TREE_CODE (type2) == ENUMERAL_TYPE)
1913         break;
1914       if (TYPE_PTR_P (type1) && null_ptr_cst_p (args[1]))
1915 	{
1916 	  type2 = type1;
1917 	  break;
1918 	}
1919       if (null_ptr_cst_p (args[0]) && TYPE_PTR_P (type2))
1920 	{
1921 	  type1 = type2;
1922 	  break;
1923 	}
1924       return;
1925 
1926     case PLUS_EXPR:
1927       if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1928 	break;
1929     case ARRAY_REF:
1930       if (INTEGRAL_TYPE_P (type1) && TYPE_PTROB_P (type2))
1931 	{
1932 	  type1 = ptrdiff_type_node;
1933 	  break;
1934 	}
1935       if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2))
1936 	{
1937 	  type2 = ptrdiff_type_node;
1938 	  break;
1939 	}
1940       return;
1941 
1942 /* 18For  every pair of promoted integral types L and R, there exist candi-
1943      date operator functions of the form
1944 	     LR      operator%(L, R);
1945 	     LR      operator&(L, R);
1946 	     LR      operator^(L, R);
1947 	     LR      operator|(L, R);
1948 	     L       operator<<(L, R);
1949 	     L       operator>>(L, R);
1950      where LR is the result of the  usual  arithmetic  conversions  between
1951      types L and R.  */
1952 
1953     case TRUNC_MOD_EXPR:
1954     case BIT_AND_EXPR:
1955     case BIT_IOR_EXPR:
1956     case BIT_XOR_EXPR:
1957     case LSHIFT_EXPR:
1958     case RSHIFT_EXPR:
1959       if (INTEGRAL_TYPE_P (type1) && INTEGRAL_TYPE_P (type2))
1960 	break;
1961       return;
1962 
1963 /* 19For  every  triple  L, VQ, R), where L is an arithmetic or enumeration
1964      type, VQ is either volatile or empty, and R is a  promoted  arithmetic
1965      type, there exist candidate operator functions of the form
1966 	     VQ L&   operator=(VQ L&, R);
1967 	     VQ L&   operator*=(VQ L&, R);
1968 	     VQ L&   operator/=(VQ L&, R);
1969 	     VQ L&   operator+=(VQ L&, R);
1970 	     VQ L&   operator-=(VQ L&, R);
1971 
1972    20For  every  pair T, VQ), where T is any type and VQ is either volatile
1973      or empty, there exist candidate operator functions of the form
1974 	     T*VQ&   operator=(T*VQ&, T*);
1975 
1976    21For every pair T, VQ), where T is a pointer to member type and  VQ  is
1977      either  volatile or empty, there exist candidate operator functions of
1978      the form
1979 	     VQ T&   operator=(VQ T&, T);
1980 
1981    22For every triple  T,  VQ,  I),  where  T  is  a  cv-qualified  or  cv-
1982      unqualified  complete object type, VQ is either volatile or empty, and
1983      I is a promoted integral type, there exist  candidate  operator  func-
1984      tions of the form
1985 	     T*VQ&   operator+=(T*VQ&, I);
1986 	     T*VQ&   operator-=(T*VQ&, I);
1987 
1988    23For  every  triple  L,  VQ,  R), where L is an integral or enumeration
1989      type, VQ is either volatile or empty, and R  is  a  promoted  integral
1990      type, there exist candidate operator functions of the form
1991 
1992 	     VQ L&   operator%=(VQ L&, R);
1993 	     VQ L&   operator<<=(VQ L&, R);
1994 	     VQ L&   operator>>=(VQ L&, R);
1995 	     VQ L&   operator&=(VQ L&, R);
1996 	     VQ L&   operator^=(VQ L&, R);
1997 	     VQ L&   operator|=(VQ L&, R);  */
1998 
1999     case MODIFY_EXPR:
2000       switch (code2)
2001 	{
2002 	case PLUS_EXPR:
2003 	case MINUS_EXPR:
2004 	  if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2))
2005 	    {
2006 	      type2 = ptrdiff_type_node;
2007 	      break;
2008 	    }
2009 	case MULT_EXPR:
2010 	case TRUNC_DIV_EXPR:
2011 	  if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
2012 	    break;
2013 	  return;
2014 
2015 	case TRUNC_MOD_EXPR:
2016 	case BIT_AND_EXPR:
2017 	case BIT_IOR_EXPR:
2018 	case BIT_XOR_EXPR:
2019 	case LSHIFT_EXPR:
2020 	case RSHIFT_EXPR:
2021 	  if (INTEGRAL_TYPE_P (type1) && INTEGRAL_TYPE_P (type2))
2022 	    break;
2023 	  return;
2024 
2025 	case NOP_EXPR:
2026 	  if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
2027 	    break;
2028 	  if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2))
2029 	      || (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
2030 	      || (TYPE_PTRMEM_P (type1) && TYPE_PTRMEM_P (type2))
2031 	      || ((TYPE_PTRMEMFUNC_P (type1)
2032 		   || TREE_CODE (type1) == POINTER_TYPE)
2033 		  && null_ptr_cst_p (args[1])))
2034 	    {
2035 	      type2 = type1;
2036 	      break;
2037 	    }
2038 	  return;
2039 
2040 	default:
2041 	  abort ();
2042 	}
2043       type1 = build_reference_type (type1);
2044       break;
2045 
2046     case COND_EXPR:
2047       /* [over.built]
2048 
2049 	 For every pair of promoted arithmetic types L and R, there
2050 	 exist candidate operator functions of the form
2051 
2052 	 LR operator?(bool, L, R);
2053 
2054 	 where LR is the result of the usual arithmetic conversions
2055 	 between types L and R.
2056 
2057 	 For every type T, where T is a pointer or pointer-to-member
2058 	 type, there exist candidate operator functions of the form T
2059 	 operator?(bool, T, T);  */
2060 
2061       if (promoted_arithmetic_type_p (type1)
2062 	  && promoted_arithmetic_type_p (type2))
2063 	/* That's OK.  */
2064 	break;
2065 
2066       /* Otherwise, the types should be pointers.  */
2067       if (!(TREE_CODE (type1) == POINTER_TYPE
2068 	    || TYPE_PTRMEM_P (type1)
2069 	    || TYPE_PTRMEMFUNC_P (type1))
2070 	  || !(TREE_CODE (type2) == POINTER_TYPE
2071 	       || TYPE_PTRMEM_P (type2)
2072 	       || TYPE_PTRMEMFUNC_P (type2)))
2073 	return;
2074 
2075       /* We don't check that the two types are the same; the logic
2076 	 below will actually create two candidates; one in which both
2077 	 parameter types are TYPE1, and one in which both parameter
2078 	 types are TYPE2.  */
2079       break;
2080 
2081     default:
2082       abort ();
2083     }
2084 
2085   /* If we're dealing with two pointer types or two enumeral types,
2086      we need candidates for both of them.  */
2087   if (type2 && !same_type_p (type1, type2)
2088       && TREE_CODE (type1) == TREE_CODE (type2)
2089       && (TREE_CODE (type1) == REFERENCE_TYPE
2090 	  || (TREE_CODE (type1) == POINTER_TYPE
2091 	      && TYPE_PTRMEM_P (type1) == TYPE_PTRMEM_P (type2))
2092 	  || TYPE_PTRMEMFUNC_P (type1)
2093 	  || IS_AGGR_TYPE (type1)
2094 	  || TREE_CODE (type1) == ENUMERAL_TYPE))
2095     {
2096       build_builtin_candidate
2097 	(candidates, fnname, type1, type1, args, argtypes, flags);
2098       build_builtin_candidate
2099 	(candidates, fnname, type2, type2, args, argtypes, flags);
2100       return;
2101     }
2102 
2103   build_builtin_candidate
2104     (candidates, fnname, type1, type2, args, argtypes, flags);
2105 }
2106 
2107 tree
type_decays_to(type)2108 type_decays_to (type)
2109      tree type;
2110 {
2111   if (TREE_CODE (type) == ARRAY_TYPE)
2112     return build_pointer_type (TREE_TYPE (type));
2113   if (TREE_CODE (type) == FUNCTION_TYPE)
2114     return build_pointer_type (type);
2115   return type;
2116 }
2117 
2118 /* There are three conditions of builtin candidates:
2119 
2120    1) bool-taking candidates.  These are the same regardless of the input.
2121    2) pointer-pair taking candidates.  These are generated for each type
2122       one of the input types converts to.
2123    3) arithmetic candidates.  According to the standard, we should generate
2124       all of these, but I'm trying not to...
2125 
2126    Here we generate a superset of the possible candidates for this particular
2127    case.  That is a subset of the full set the standard defines, plus some
2128    other cases which the standard disallows. add_builtin_candidate will
2129    filter out the invalid set.  */
2130 
2131 static void
add_builtin_candidates(candidates,code,code2,fnname,args,flags)2132 add_builtin_candidates (candidates, code, code2, fnname, args, flags)
2133      struct z_candidate **candidates;
2134      enum tree_code code, code2;
2135      tree fnname, *args;
2136      int flags;
2137 {
2138   int ref1, i;
2139   int enum_p = 0;
2140   tree type, argtypes[3];
2141   /* TYPES[i] is the set of possible builtin-operator parameter types
2142      we will consider for the Ith argument.  These are represented as
2143      a TREE_LIST; the TREE_VALUE of each node is the potential
2144      parameter type.  */
2145   tree types[2];
2146 
2147   for (i = 0; i < 3; ++i)
2148     {
2149       if (args[i])
2150 	argtypes[i]  = lvalue_type (args[i]);
2151       else
2152 	argtypes[i] = NULL_TREE;
2153     }
2154 
2155   switch (code)
2156     {
2157 /* 4 For every pair T, VQ), where T is an arithmetic or  enumeration  type,
2158      and  VQ  is  either  volatile or empty, there exist candidate operator
2159      functions of the form
2160 		 VQ T&   operator++(VQ T&);  */
2161 
2162     case POSTINCREMENT_EXPR:
2163     case PREINCREMENT_EXPR:
2164     case POSTDECREMENT_EXPR:
2165     case PREDECREMENT_EXPR:
2166     case MODIFY_EXPR:
2167       ref1 = 1;
2168       break;
2169 
2170 /* 24There also exist candidate operator functions of the form
2171 	     bool    operator!(bool);
2172 	     bool    operator&&(bool, bool);
2173 	     bool    operator||(bool, bool);  */
2174 
2175     case TRUTH_NOT_EXPR:
2176       build_builtin_candidate
2177 	(candidates, fnname, boolean_type_node,
2178 	 NULL_TREE, args, argtypes, flags);
2179       return;
2180 
2181     case TRUTH_ORIF_EXPR:
2182     case TRUTH_ANDIF_EXPR:
2183       build_builtin_candidate
2184 	(candidates, fnname, boolean_type_node,
2185 	 boolean_type_node, args, argtypes, flags);
2186       return;
2187 
2188     case ADDR_EXPR:
2189     case COMPOUND_EXPR:
2190     case COMPONENT_REF:
2191       return;
2192 
2193     case COND_EXPR:
2194     case EQ_EXPR:
2195     case NE_EXPR:
2196     case LT_EXPR:
2197     case LE_EXPR:
2198     case GT_EXPR:
2199     case GE_EXPR:
2200       enum_p = 1;
2201       /* FALLTHROUGH */
2202 
2203     default:
2204       ref1 = 0;
2205     }
2206 
2207   types[0] = types[1] = NULL_TREE;
2208 
2209   for (i = 0; i < 2; ++i)
2210     {
2211       if (! args[i])
2212 	;
2213       else if (IS_AGGR_TYPE (argtypes[i]))
2214 	{
2215 	  tree convs;
2216 
2217 	  if (i == 0 && code == MODIFY_EXPR && code2 == NOP_EXPR)
2218 	    return;
2219 
2220 	  convs = lookup_conversions (argtypes[i]);
2221 
2222 	  if (code == COND_EXPR)
2223 	    {
2224 	      if (real_lvalue_p (args[i]))
2225 		types[i] = tree_cons
2226 		  (NULL_TREE, build_reference_type (argtypes[i]), types[i]);
2227 
2228 	      types[i] = tree_cons
2229 		(NULL_TREE, TYPE_MAIN_VARIANT (argtypes[i]), types[i]);
2230 	    }
2231 
2232 	  else if (! convs)
2233 	    return;
2234 
2235 	  for (; convs; convs = TREE_CHAIN (convs))
2236 	    {
2237 	      type = TREE_TYPE (TREE_TYPE (OVL_CURRENT (TREE_VALUE (convs))));
2238 
2239 	      if (i == 0 && ref1
2240 		  && (TREE_CODE (type) != REFERENCE_TYPE
2241 		      || CP_TYPE_CONST_P (TREE_TYPE (type))))
2242 		continue;
2243 
2244 	      if (code == COND_EXPR && TREE_CODE (type) == REFERENCE_TYPE)
2245 		types[i] = tree_cons (NULL_TREE, type, types[i]);
2246 
2247 	      type = non_reference (type);
2248 	      if (i != 0 || ! ref1)
2249 		{
2250 		  type = TYPE_MAIN_VARIANT (type_decays_to (type));
2251 	          if (enum_p && TREE_CODE (type) == ENUMERAL_TYPE)
2252 	            types[i] = tree_cons (NULL_TREE, type, types[i]);
2253 		  if (INTEGRAL_TYPE_P (type))
2254 		    type = type_promotes_to (type);
2255 		}
2256 
2257 	      if (! value_member (type, types[i]))
2258 		types[i] = tree_cons (NULL_TREE, type, types[i]);
2259 	    }
2260 	}
2261       else
2262 	{
2263 	  if (code == COND_EXPR && real_lvalue_p (args[i]))
2264 	    types[i] = tree_cons
2265 	      (NULL_TREE, build_reference_type (argtypes[i]), types[i]);
2266 	  type = non_reference (argtypes[i]);
2267 	  if (i != 0 || ! ref1)
2268 	    {
2269 	      type = TYPE_MAIN_VARIANT (type_decays_to (type));
2270 	      if (enum_p && TREE_CODE (type) == ENUMERAL_TYPE)
2271 	        types[i] = tree_cons (NULL_TREE, type, types[i]);
2272 	      if (INTEGRAL_TYPE_P (type))
2273 		type = type_promotes_to (type);
2274 	    }
2275 	  types[i] = tree_cons (NULL_TREE, type, types[i]);
2276 	}
2277     }
2278 
2279   /* Run through the possible parameter types of both arguments,
2280      creating candidates with those parameter types.  */
2281   for (; types[0]; types[0] = TREE_CHAIN (types[0]))
2282     {
2283       if (types[1])
2284 	for (type = types[1]; type; type = TREE_CHAIN (type))
2285 	  add_builtin_candidate
2286 	    (candidates, code, code2, fnname, TREE_VALUE (types[0]),
2287 	     TREE_VALUE (type), args, argtypes, flags);
2288       else
2289 	add_builtin_candidate
2290 	  (candidates, code, code2, fnname, TREE_VALUE (types[0]),
2291 	   NULL_TREE, args, argtypes, flags);
2292     }
2293 
2294   return;
2295 }
2296 
2297 
2298 /* If TMPL can be successfully instantiated as indicated by
2299    EXPLICIT_TARGS and ARGLIST, adds the instantiation to CANDIDATES.
2300 
2301    TMPL is the template.  EXPLICIT_TARGS are any explicit template
2302    arguments.  ARGLIST is the arguments provided at the call-site.
2303    The RETURN_TYPE is the desired type for conversion operators.  If
2304    OBJ is NULL_TREE, FLAGS and CTYPE are as for add_function_candidate.
2305    If an OBJ is supplied, FLAGS and CTYPE are ignored, and OBJ is as for
2306    add_conv_candidate.  */
2307 
2308 static struct z_candidate*
add_template_candidate_real(candidates,tmpl,ctype,explicit_targs,arglist,return_type,access_path,conversion_path,flags,obj,strict)2309 add_template_candidate_real (candidates, tmpl, ctype, explicit_targs,
2310 			     arglist, return_type, access_path,
2311 			     conversion_path, flags, obj, strict)
2312      struct z_candidate **candidates;
2313      tree tmpl, ctype, explicit_targs, arglist, return_type;
2314      tree access_path;
2315      tree conversion_path;
2316      int flags;
2317      tree obj;
2318      unification_kind_t strict;
2319 {
2320   int ntparms = DECL_NTPARMS (tmpl);
2321   tree targs = make_tree_vec (ntparms);
2322   tree args_without_in_chrg = arglist;
2323   struct z_candidate *cand;
2324   int i;
2325   tree fn;
2326 
2327   /* We don't do deduction on the in-charge parameter, the VTT
2328      parameter or 'this'.  */
2329   if (DECL_NONSTATIC_MEMBER_FUNCTION_P (tmpl))
2330     args_without_in_chrg = TREE_CHAIN (args_without_in_chrg);
2331 
2332   if ((DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (tmpl)
2333        || DECL_BASE_CONSTRUCTOR_P (tmpl))
2334       && TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (tmpl)))
2335     args_without_in_chrg = TREE_CHAIN (args_without_in_chrg);
2336 
2337   i = fn_type_unification (tmpl, explicit_targs, targs,
2338 			   args_without_in_chrg,
2339 			   return_type, strict, -1);
2340 
2341   if (i != 0)
2342     return NULL;
2343 
2344   fn = instantiate_template (tmpl, targs);
2345   if (fn == error_mark_node)
2346     return NULL;
2347 
2348   /* In [class.copy]:
2349 
2350        A member function template is never instantiated to perform the
2351        copy of a class object to an object of its class type.
2352 
2353      It's a little unclear what this means; the standard explicitly
2354      does allow a template to be used to copy a class.  For example,
2355      in:
2356 
2357        struct A {
2358          A(A&);
2359 	 template <class T> A(const T&);
2360        };
2361        const A f ();
2362        void g () { A a (f ()); }
2363 
2364      the member template will be used to make the copy.  The section
2365      quoted above appears in the paragraph that forbids constructors
2366      whose only parameter is (a possibly cv-qualified variant of) the
2367      class type, and a logical interpretation is that the intent was
2368      to forbid the instantiation of member templates which would then
2369      have that form.  */
2370   if (DECL_CONSTRUCTOR_P (fn) && list_length (arglist) == 2)
2371     {
2372       tree arg_types = FUNCTION_FIRST_USER_PARMTYPE (fn);
2373       if (arg_types && same_type_p (TYPE_MAIN_VARIANT (TREE_VALUE (arg_types)),
2374 				    ctype))
2375 	return NULL;
2376     }
2377 
2378   if (obj != NULL_TREE)
2379     /* Aha, this is a conversion function.  */
2380     cand = add_conv_candidate (candidates, fn, obj, access_path,
2381 			       conversion_path, arglist);
2382   else
2383     cand = add_function_candidate (candidates, fn, ctype,
2384 				   arglist, access_path,
2385 				   conversion_path, flags);
2386   if (DECL_TI_TEMPLATE (fn) != tmpl)
2387     /* This situation can occur if a member template of a template
2388        class is specialized.  Then, instantiate_template might return
2389        an instantiation of the specialization, in which case the
2390        DECL_TI_TEMPLATE field will point at the original
2391        specialization.  For example:
2392 
2393 	 template <class T> struct S { template <class U> void f(U);
2394 				       template <> void f(int) {}; };
2395 	 S<double> sd;
2396 	 sd.f(3);
2397 
2398        Here, TMPL will be template <class U> S<double>::f(U).
2399        And, instantiate template will give us the specialization
2400        template <> S<double>::f(int).  But, the DECL_TI_TEMPLATE field
2401        for this will point at template <class T> template <> S<T>::f(int),
2402        so that we can find the definition.  For the purposes of
2403        overload resolution, however, we want the original TMPL.  */
2404     cand->template = tree_cons (tmpl, targs, NULL_TREE);
2405   else
2406     cand->template = DECL_TEMPLATE_INFO (fn);
2407 
2408   return cand;
2409 }
2410 
2411 
2412 static struct z_candidate *
add_template_candidate(candidates,tmpl,ctype,explicit_targs,arglist,return_type,access_path,conversion_path,flags,strict)2413 add_template_candidate (candidates, tmpl, ctype, explicit_targs,
2414 			arglist, return_type, access_path,
2415 			conversion_path, flags, strict)
2416      struct z_candidate **candidates;
2417      tree tmpl, ctype, explicit_targs, arglist, return_type;
2418      tree access_path;
2419      tree conversion_path;
2420      int flags;
2421      unification_kind_t strict;
2422 {
2423   return
2424     add_template_candidate_real (candidates, tmpl, ctype,
2425 				 explicit_targs, arglist, return_type,
2426 				 access_path, conversion_path,
2427 				 flags, NULL_TREE, strict);
2428 }
2429 
2430 
2431 static struct z_candidate *
add_template_conv_candidate(candidates,tmpl,obj,arglist,return_type,access_path,conversion_path)2432 add_template_conv_candidate (candidates, tmpl, obj, arglist, return_type,
2433 			     access_path, conversion_path)
2434      struct z_candidate **candidates;
2435      tree tmpl, obj, arglist, return_type;
2436      tree access_path;
2437      tree conversion_path;
2438 {
2439   return
2440     add_template_candidate_real (candidates, tmpl, NULL_TREE, NULL_TREE,
2441 				 arglist, return_type, access_path,
2442 				 conversion_path, 0, obj, DEDUCE_CONV);
2443 }
2444 
2445 
2446 static int
any_viable(cands)2447 any_viable (cands)
2448      struct z_candidate *cands;
2449 {
2450   for (; cands; cands = cands->next)
2451     if (pedantic ? cands->viable == 1 : cands->viable)
2452       return 1;
2453   return 0;
2454 }
2455 
2456 static int
any_strictly_viable(cands)2457 any_strictly_viable (cands)
2458      struct z_candidate *cands;
2459 {
2460   for (; cands; cands = cands->next)
2461     if (cands->viable == 1)
2462       return 1;
2463   return 0;
2464 }
2465 
2466 static struct z_candidate *
splice_viable(cands)2467 splice_viable (cands)
2468      struct z_candidate *cands;
2469 {
2470   struct z_candidate **p = &cands;
2471 
2472   for (; *p; )
2473     {
2474       if (pedantic ? (*p)->viable == 1 : (*p)->viable)
2475 	p = &((*p)->next);
2476       else
2477 	*p = (*p)->next;
2478     }
2479 
2480   return cands;
2481 }
2482 
2483 static tree
build_this(obj)2484 build_this (obj)
2485      tree obj;
2486 {
2487   /* Fix this to work on non-lvalues.  */
2488   return build_unary_op (ADDR_EXPR, obj, 0);
2489 }
2490 
2491 /* Returns true iff functions are equivalent. Equivalent functions are
2492    not '==' only if one is a function-local extern function or if
2493    both are extern "C".  */
2494 
2495 static inline int
equal_functions(fn1,fn2)2496 equal_functions (fn1, fn2)
2497      tree fn1;
2498      tree fn2;
2499 {
2500   if (DECL_LOCAL_FUNCTION_P (fn1) || DECL_LOCAL_FUNCTION_P (fn2)
2501       || DECL_EXTERN_C_FUNCTION_P (fn1))
2502     return decls_match (fn1, fn2);
2503   return fn1 == fn2;
2504 }
2505 
2506 static void
print_z_candidates(struct z_candidate * candidates)2507 print_z_candidates (struct z_candidate *candidates)
2508 {
2509   const char *str;
2510   struct z_candidate *cand1;
2511   struct z_candidate **cand2;
2512 
2513   /* There may be duplicates in the set of candidates.  We put off
2514      checking this condition as long as possible, since we have no way
2515      to eliminate duplicates from a set of functions in less than n^2
2516      time.  Now we are about to emit an error message, so it is more
2517      permissible to go slowly.  */
2518   for (cand1 = candidates; cand1; cand1 = cand1->next)
2519     {
2520       tree fn = cand1->fn;
2521       /* Skip builtin candidates and conversion functions.  */
2522       if (TREE_CODE (fn) != FUNCTION_DECL)
2523 	continue;
2524       cand2 = &cand1->next;
2525       while (*cand2)
2526 	{
2527 	  if (TREE_CODE ((*cand2)->fn) == FUNCTION_DECL
2528 	      && equal_functions (fn, (*cand2)->fn))
2529 	    *cand2 = (*cand2)->next;
2530 	  else
2531 	    cand2 = &(*cand2)->next;
2532 	}
2533     }
2534 
2535   str = "candidates are:";
2536   for (; candidates; candidates = candidates->next)
2537     {
2538       if (TREE_CODE (candidates->fn) == IDENTIFIER_NODE)
2539 	{
2540 	  if (TREE_VEC_LENGTH (candidates->convs) == 3)
2541 	    error ("%s %D(%T, %T, %T) <built-in>", str, candidates->fn,
2542 		      TREE_TYPE (TREE_VEC_ELT (candidates->convs, 0)),
2543 		      TREE_TYPE (TREE_VEC_ELT (candidates->convs, 1)),
2544 		      TREE_TYPE (TREE_VEC_ELT (candidates->convs, 2)));
2545 	  else if (TREE_VEC_LENGTH (candidates->convs) == 2)
2546 	    error ("%s %D(%T, %T) <built-in>", str, candidates->fn,
2547 		      TREE_TYPE (TREE_VEC_ELT (candidates->convs, 0)),
2548 		      TREE_TYPE (TREE_VEC_ELT (candidates->convs, 1)));
2549 	  else
2550 	    error ("%s %D(%T) <built-in>", str, candidates->fn,
2551 		      TREE_TYPE (TREE_VEC_ELT (candidates->convs, 0)));
2552 	}
2553       else if (TYPE_P (candidates->fn))
2554 	error ("%s %T <conversion>", str, candidates->fn);
2555       else
2556 	cp_error_at ("%s %+#D%s", str, candidates->fn,
2557 		     candidates->viable == -1 ? " <near match>" : "");
2558       str = "               ";
2559     }
2560 }
2561 
2562 /* USER_SEQ is a user-defined conversion sequence, beginning with a
2563    USER_CONV.  STD_SEQ is the standard conversion sequence applied to
2564    the result of the conversion function to convert it to the final
2565    desired type.  Merge the the two sequences into a single sequence,
2566    and return the merged sequence.  */
2567 
2568 static tree
merge_conversion_sequences(tree user_seq,tree std_seq)2569 merge_conversion_sequences (tree user_seq, tree std_seq)
2570 {
2571   tree *t;
2572 
2573   my_friendly_assert (TREE_CODE (user_seq) == USER_CONV,
2574 		      20030306);
2575 
2576   /* Find the end of the second conversion sequence.  */
2577   t = &(std_seq);
2578   while (TREE_CODE (*t) != IDENTITY_CONV)
2579     t = &TREE_OPERAND (*t, 0);
2580 
2581   /* Replace the identity conversion with the user conversion
2582      sequence.  */
2583   *t = user_seq;
2584 
2585   /* The entire sequence is a user-conversion sequence.  */
2586   ICS_USER_FLAG (std_seq) = 1;
2587 
2588   return std_seq;
2589 }
2590 
2591 /* Returns the best overload candidate to perform the requested
2592    conversion.  This function is used for three the overloading situations
2593    described in [over.match.copy], [over.match.conv], and [over.match.ref].
2594    If TOTYPE is a REFERENCE_TYPE, we're trying to find an lvalue binding as
2595    per [dcl.init.ref], so we ignore temporary bindings.  */
2596 
2597 static struct z_candidate *
build_user_type_conversion_1(totype,expr,flags)2598 build_user_type_conversion_1 (totype, expr, flags)
2599      tree totype, expr;
2600      int flags;
2601 {
2602   struct z_candidate *candidates, *cand;
2603   tree fromtype = TREE_TYPE (expr);
2604   tree ctors = NULL_TREE, convs = NULL_TREE;
2605   tree args = NULL_TREE;
2606 
2607   /* We represent conversion within a hierarchy using RVALUE_CONV and
2608      BASE_CONV, as specified by [over.best.ics]; these become plain
2609      constructor calls, as specified in [dcl.init].  */
2610   my_friendly_assert (!IS_AGGR_TYPE (fromtype) || !IS_AGGR_TYPE (totype)
2611 		      || !DERIVED_FROM_P (totype, fromtype), 20011226);
2612 
2613   if (IS_AGGR_TYPE (totype))
2614     ctors = lookup_fnfields (TYPE_BINFO (totype),
2615 			     complete_ctor_identifier,
2616 			     0);
2617 
2618   if (IS_AGGR_TYPE (fromtype))
2619     convs = lookup_conversions (fromtype);
2620 
2621   candidates = 0;
2622   flags |= LOOKUP_NO_CONVERSION;
2623 
2624   if (ctors)
2625     {
2626       tree t;
2627 
2628       ctors = BASELINK_FUNCTIONS (ctors);
2629 
2630       t = build_int_2 (0, 0);
2631       TREE_TYPE (t) = build_pointer_type (totype);
2632       args = build_tree_list (NULL_TREE, expr);
2633       /* We should never try to call the abstract or base constructor
2634 	 from here.  */
2635       my_friendly_assert (!DECL_HAS_IN_CHARGE_PARM_P (OVL_CURRENT (ctors))
2636 			  && !DECL_HAS_VTT_PARM_P (OVL_CURRENT (ctors)),
2637 			  20011226);
2638       args = tree_cons (NULL_TREE, t, args);
2639     }
2640   for (; ctors; ctors = OVL_NEXT (ctors))
2641     {
2642       tree ctor = OVL_CURRENT (ctors);
2643       if (DECL_NONCONVERTING_P (ctor))
2644 	continue;
2645 
2646       if (TREE_CODE (ctor) == TEMPLATE_DECL)
2647 	cand = add_template_candidate (&candidates, ctor, totype,
2648 				       NULL_TREE, args, NULL_TREE,
2649 				       TYPE_BINFO (totype),
2650 				       TYPE_BINFO (totype),
2651 				       flags,
2652 				       DEDUCE_CALL);
2653       else
2654 	cand = add_function_candidate (&candidates, ctor, totype,
2655 				       args, TYPE_BINFO (totype),
2656 				       TYPE_BINFO (totype),
2657 				       flags);
2658 
2659       if (cand)
2660 	cand->second_conv = build1 (IDENTITY_CONV, totype, NULL_TREE);
2661     }
2662 
2663   if (convs)
2664     args = build_tree_list (NULL_TREE, build_this (expr));
2665 
2666   for (; convs; convs = TREE_CHAIN (convs))
2667     {
2668       tree fns;
2669       tree conversion_path = TREE_PURPOSE (convs);
2670       int convflags = LOOKUP_NO_CONVERSION;
2671 
2672       /* If we are called to convert to a reference type, we are trying to
2673 	 find an lvalue binding, so don't even consider temporaries.  If
2674 	 we don't find an lvalue binding, the caller will try again to
2675 	 look for a temporary binding.  */
2676       if (TREE_CODE (totype) == REFERENCE_TYPE)
2677 	convflags |= LOOKUP_NO_TEMP_BIND;
2678 
2679       for (fns = TREE_VALUE (convs); fns; fns = OVL_NEXT (fns))
2680 	{
2681 	  tree fn = OVL_CURRENT (fns);
2682 
2683 	  /* [over.match.funcs] For conversion functions, the function
2684 	     is considered to be a member of the class of the implicit
2685 	     object argument for the purpose of defining the type of
2686 	     the implicit object parameter.
2687 
2688 	     So we pass fromtype as CTYPE to add_*_candidate.  */
2689 
2690 	  if (TREE_CODE (fn) == TEMPLATE_DECL)
2691 	    cand = add_template_candidate (&candidates, fn, fromtype, NULL_TREE,
2692 					   args, totype,
2693 					   TYPE_BINFO (fromtype),
2694 					   conversion_path,
2695 					   flags,
2696 					   DEDUCE_CONV);
2697 	  else
2698 	    cand = add_function_candidate (&candidates, fn, fromtype,
2699 					   args,
2700 					   TYPE_BINFO (fromtype),
2701 					   conversion_path,
2702 					   flags);
2703 
2704 	  if (cand)
2705 	    {
2706 	      tree ics = implicit_conversion
2707 		(totype, TREE_TYPE (TREE_TYPE (cand->fn)),
2708 		 0, convflags);
2709 
2710 	      cand->second_conv = ics;
2711 
2712 	      if (ics == NULL_TREE)
2713 		cand->viable = 0;
2714 	      else if (cand->viable == 1 && ICS_BAD_FLAG (ics))
2715 		cand->viable = -1;
2716 	    }
2717 	}
2718     }
2719 
2720   if (! any_viable (candidates))
2721     return 0;
2722 
2723   candidates = splice_viable (candidates);
2724   cand = tourney (candidates);
2725 
2726   if (cand == 0)
2727     {
2728       if (flags & LOOKUP_COMPLAIN)
2729 	{
2730 	  error ("conversion from `%T' to `%T' is ambiguous",
2731 		    fromtype, totype);
2732 	  print_z_candidates (candidates);
2733 	}
2734 
2735       cand = candidates;	/* any one will do */
2736       cand->second_conv = build1 (AMBIG_CONV, totype, expr);
2737       ICS_USER_FLAG (cand->second_conv) = 1;
2738       if (!any_strictly_viable (candidates))
2739 	ICS_BAD_FLAG (cand->second_conv) = 1;
2740       /* If there are viable candidates, don't set ICS_BAD_FLAG; an
2741 	 ambiguous conversion is no worse than another user-defined
2742 	 conversion.  */
2743 
2744       return cand;
2745     }
2746 
2747   /* Build the user conversion sequence.  */
2748   convs = build_conv
2749     (USER_CONV,
2750      (DECL_CONSTRUCTOR_P (cand->fn)
2751       ? totype : non_reference (TREE_TYPE (TREE_TYPE (cand->fn)))),
2752      build1 (IDENTITY_CONV, TREE_TYPE (expr), expr));
2753   TREE_OPERAND (convs, 1) = build_zc_wrapper (cand);
2754 
2755   /* Combine it with the second conversion sequence.  */
2756   cand->second_conv = merge_conversion_sequences (convs,
2757 						  cand->second_conv);
2758 
2759   if (cand->viable == -1)
2760     ICS_BAD_FLAG (cand->second_conv) = 1;
2761 
2762   return cand;
2763 }
2764 
2765 tree
build_user_type_conversion(totype,expr,flags)2766 build_user_type_conversion (totype, expr, flags)
2767      tree totype, expr;
2768      int flags;
2769 {
2770   struct z_candidate *cand
2771     = build_user_type_conversion_1 (totype, expr, flags);
2772 
2773   if (cand)
2774     {
2775       if (TREE_CODE (cand->second_conv) == AMBIG_CONV)
2776 	return error_mark_node;
2777       return convert_from_reference (convert_like (cand->second_conv, expr));
2778     }
2779   return NULL_TREE;
2780 }
2781 
2782 /* Find the possibly overloaded set of functions corresponding to a
2783    call of the form SCOPE::NAME (...). NAME might be a
2784    TEMPLATE_ID_EXPR, OVERLOAD, _DECL, IDENTIFIER_NODE or LOOKUP_EXPR. */
2785 
2786 tree
resolve_scoped_fn_name(tree scope,tree name)2787 resolve_scoped_fn_name (tree scope, tree name)
2788 {
2789   tree fn;
2790   tree template_args = NULL_TREE;
2791   bool is_template_id = TREE_CODE (name) == TEMPLATE_ID_EXPR;
2792 
2793   if (is_template_id)
2794     {
2795       template_args = TREE_OPERAND (name, 1);
2796       name = TREE_OPERAND (name, 0);
2797     }
2798   if (TREE_CODE (name) == OVERLOAD)
2799     name = DECL_NAME (get_first_fn (name));
2800   else if (TREE_CODE (name) == LOOKUP_EXPR)
2801     name = TREE_OPERAND (name, 0);
2802 
2803   if (TREE_CODE (scope) == NAMESPACE_DECL)
2804     fn = lookup_namespace_name (scope, name);
2805   else if (!CLASS_TYPE_P (scope))
2806     {
2807       error ("`%T' is not a class type", scope);
2808       return error_mark_node;
2809     }
2810   else
2811     {
2812       if (!TYPE_BEING_DEFINED (scope)
2813 	  && !COMPLETE_TYPE_P (complete_type (scope)))
2814 	{
2815 	  error ("incomplete type '%T' cannot be used to name a scope",
2816 		 scope);
2817 	  return error_mark_node;
2818 	}
2819 
2820       if (BASELINK_P (name))
2821 	fn = name;
2822       else
2823 	fn = lookup_member (scope, name, /*protect=*/1, /*prefer_type=*/0);
2824       if (fn && current_class_type)
2825 	fn = (adjust_result_of_qualified_name_lookup
2826 	      (fn, scope, current_class_type));
2827 
2828       /* It might be the name of a function pointer member.  */
2829       if (fn && TREE_CODE (fn) == FIELD_DECL)
2830 	fn = resolve_offset_ref (build_offset_ref (scope, fn));
2831     }
2832 
2833   if (!fn)
2834     {
2835       error ("'%D' has no member named '%E'", scope, name);
2836       return error_mark_node;
2837     }
2838   if (is_template_id)
2839     {
2840       tree fns = fn;
2841 
2842       if (BASELINK_P (fn))
2843 	fns = BASELINK_FUNCTIONS (fns);
2844       fns = build_nt (TEMPLATE_ID_EXPR, fns, template_args);
2845       if (BASELINK_P (fn))
2846 	BASELINK_FUNCTIONS (fn) = fns;
2847       else
2848 	fn = fns;
2849     }
2850 
2851   return fn;
2852 }
2853 
2854 /* Do any initial processing on the arguments to a function call.  */
2855 
2856 static tree
resolve_args(args)2857 resolve_args (args)
2858      tree args;
2859 {
2860   tree t;
2861   for (t = args; t; t = TREE_CHAIN (t))
2862     {
2863       tree arg = TREE_VALUE (t);
2864 
2865       if (arg == error_mark_node)
2866 	return error_mark_node;
2867       else if (VOID_TYPE_P (TREE_TYPE (arg)))
2868 	{
2869 	  error ("invalid use of void expression");
2870 	  return error_mark_node;
2871 	}
2872       else if (TREE_CODE (arg) == OFFSET_REF)
2873 	arg = resolve_offset_ref (arg);
2874       arg = convert_from_reference (arg);
2875       TREE_VALUE (t) = arg;
2876     }
2877   return args;
2878 }
2879 
2880 /* Return an expression for a call to FN (a namespace-scope function)
2881    with the ARGS.  */
2882 
2883 tree
build_new_function_call(fn,args)2884 build_new_function_call (fn, args)
2885      tree fn, args;
2886 {
2887   struct z_candidate *candidates = 0, *cand;
2888   tree explicit_targs = NULL_TREE;
2889   int template_only = 0;
2890 
2891   /* Check FN and ARGS.  */
2892   my_friendly_assert (TREE_CODE (fn) == FUNCTION_DECL
2893 		      || TREE_CODE (fn) == TEMPLATE_DECL
2894 		      || TREE_CODE (fn) == OVERLOAD
2895 		      || TREE_CODE (fn) == TEMPLATE_ID_EXPR,
2896 		      20020712);
2897   my_friendly_assert (!args || TREE_CODE (args) == TREE_LIST,
2898 		      20020712);
2899 
2900   if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
2901     {
2902       explicit_targs = TREE_OPERAND (fn, 1);
2903       fn = TREE_OPERAND (fn, 0);
2904       template_only = 1;
2905     }
2906 
2907   if (really_overloaded_fn (fn)
2908       || TREE_CODE (fn) == TEMPLATE_DECL)
2909     {
2910       tree t1;
2911 
2912       args = resolve_args (args);
2913 
2914       if (args == error_mark_node)
2915 	return error_mark_node;
2916 
2917       for (t1 = fn; t1; t1 = OVL_NEXT (t1))
2918 	{
2919 	  tree t = OVL_CURRENT (t1);
2920 
2921 	  if (TREE_CODE (t) == TEMPLATE_DECL)
2922 	    add_template_candidate
2923 	      (&candidates, t, NULL_TREE, explicit_targs, args,
2924 	       NULL_TREE,
2925 	       /*access_path=*/NULL_TREE, /*conversion_path=*/NULL_TREE,
2926 	       LOOKUP_NORMAL, DEDUCE_CALL);
2927 	  else if (! template_only)
2928 	    add_function_candidate
2929 	      (&candidates, t, NULL_TREE, args, /*access_path=*/NULL_TREE,
2930 	       /*conversion_path=*/NULL_TREE, LOOKUP_NORMAL);
2931 	}
2932 
2933       if (! any_viable (candidates))
2934 	{
2935 	  if (candidates && ! candidates->next)
2936 	    return build_function_call (candidates->fn, args);
2937 	  error ("no matching function for call to `%D(%A)'",
2938 		    DECL_NAME (OVL_CURRENT (fn)), args);
2939 	  if (candidates)
2940 	    print_z_candidates (candidates);
2941 	  return error_mark_node;
2942 	}
2943       candidates = splice_viable (candidates);
2944       cand = tourney (candidates);
2945 
2946       if (cand == 0)
2947 	{
2948 	  error ("call of overloaded `%D(%A)' is ambiguous",
2949 		    DECL_NAME (OVL_FUNCTION (fn)), args);
2950 	  print_z_candidates (candidates);
2951 	  return error_mark_node;
2952 	}
2953 
2954       return build_over_call (cand, args, LOOKUP_NORMAL);
2955     }
2956 
2957   /* This is not really overloaded.  */
2958   fn = OVL_CURRENT (fn);
2959 
2960   return build_function_call (fn, args);
2961 }
2962 
2963 static tree
build_object_call(obj,args)2964 build_object_call (obj, args)
2965      tree obj, args;
2966 {
2967   struct z_candidate *candidates = 0, *cand;
2968   tree fns, convs, mem_args = NULL_TREE;
2969   tree type = TREE_TYPE (obj);
2970 
2971   if (TYPE_PTRMEMFUNC_P (type))
2972     {
2973       /* It's no good looking for an overloaded operator() on a
2974 	 pointer-to-member-function.  */
2975       error ("pointer-to-member function %E cannot be called without an object; consider using .* or ->*", obj);
2976       return error_mark_node;
2977     }
2978 
2979   fns = lookup_fnfields (TYPE_BINFO (type), ansi_opname (CALL_EXPR), 1);
2980   if (fns == error_mark_node)
2981     return error_mark_node;
2982 
2983   args = resolve_args (args);
2984 
2985   if (args == error_mark_node)
2986     return error_mark_node;
2987 
2988   if (fns)
2989     {
2990       tree base = BINFO_TYPE (BASELINK_BINFO (fns));
2991       mem_args = tree_cons (NULL_TREE, build_this (obj), args);
2992 
2993       for (fns = BASELINK_FUNCTIONS (fns); fns; fns = OVL_NEXT (fns))
2994 	{
2995 	  tree fn = OVL_CURRENT (fns);
2996 	  if (TREE_CODE (fn) == TEMPLATE_DECL)
2997 	    add_template_candidate (&candidates, fn, base, NULL_TREE,
2998 				    mem_args, NULL_TREE,
2999 				    TYPE_BINFO (type),
3000 				    TYPE_BINFO (type),
3001 				    LOOKUP_NORMAL, DEDUCE_CALL);
3002 	  else
3003 	    add_function_candidate
3004 	      (&candidates, fn, base, mem_args, TYPE_BINFO (type),
3005 	       TYPE_BINFO (type), LOOKUP_NORMAL);
3006 	}
3007     }
3008 
3009   convs = lookup_conversions (type);
3010 
3011   for (; convs; convs = TREE_CHAIN (convs))
3012     {
3013       tree fns = TREE_VALUE (convs);
3014       tree totype = TREE_TYPE (TREE_TYPE (OVL_CURRENT (fns)));
3015 
3016       if ((TREE_CODE (totype) == POINTER_TYPE
3017 	   && TREE_CODE (TREE_TYPE (totype)) == FUNCTION_TYPE)
3018 	  || (TREE_CODE (totype) == REFERENCE_TYPE
3019 	      && TREE_CODE (TREE_TYPE (totype)) == FUNCTION_TYPE)
3020 	  || (TREE_CODE (totype) == REFERENCE_TYPE
3021 	      && TREE_CODE (TREE_TYPE (totype)) == POINTER_TYPE
3022 	      && TREE_CODE (TREE_TYPE (TREE_TYPE (totype))) == FUNCTION_TYPE))
3023 	for (; fns; fns = OVL_NEXT (fns))
3024 	  {
3025 	    tree fn = OVL_CURRENT (fns);
3026 	    if (TREE_CODE (fn) == TEMPLATE_DECL)
3027 	      add_template_conv_candidate
3028 		(&candidates, fn, obj, args, totype,
3029 		 /*access_path=*/NULL_TREE,
3030 		 /*conversion_path=*/NULL_TREE);
3031 	    else
3032 	      add_conv_candidate (&candidates, fn, obj, args,
3033 				  /*conversion_path=*/NULL_TREE,
3034 				  /*access_path=*/NULL_TREE);
3035 	  }
3036     }
3037 
3038   if (! any_viable (candidates))
3039     {
3040       error ("no match for call to `(%T) (%A)'", TREE_TYPE (obj), args);
3041       print_z_candidates (candidates);
3042       return error_mark_node;
3043     }
3044 
3045   candidates = splice_viable (candidates);
3046   cand = tourney (candidates);
3047 
3048   if (cand == 0)
3049     {
3050       error ("call of `(%T) (%A)' is ambiguous", TREE_TYPE (obj), args);
3051       print_z_candidates (candidates);
3052       return error_mark_node;
3053     }
3054 
3055   /* Since cand->fn will be a type, not a function, for a conversion
3056      function, we must be careful not to unconditionally look at
3057      DECL_NAME here.  */
3058   if (TREE_CODE (cand->fn) == FUNCTION_DECL
3059       && DECL_OVERLOADED_OPERATOR_P (cand->fn) == CALL_EXPR)
3060     return build_over_call (cand, mem_args, LOOKUP_NORMAL);
3061 
3062   obj = convert_like_with_context
3063           (TREE_VEC_ELT (cand->convs, 0), obj, cand->fn, -1);
3064 
3065   /* FIXME */
3066   return build_function_call (obj, args);
3067 }
3068 
3069 static void
op_error(code,code2,arg1,arg2,arg3,problem)3070 op_error (code, code2, arg1, arg2, arg3, problem)
3071      enum tree_code code, code2;
3072      tree arg1, arg2, arg3;
3073      const char *problem;
3074 {
3075   const char *opname;
3076 
3077   if (code == MODIFY_EXPR)
3078     opname = assignment_operator_name_info[code2].name;
3079   else
3080     opname = operator_name_info[code].name;
3081 
3082   switch (code)
3083     {
3084     case COND_EXPR:
3085       error ("%s for ternary 'operator?:' in '%E ? %E : %E'",
3086              problem, arg1, arg2, arg3);
3087       break;
3088 
3089     case POSTINCREMENT_EXPR:
3090     case POSTDECREMENT_EXPR:
3091       error ("%s for 'operator%s' in '%E%s'", problem, opname, arg1, opname);
3092       break;
3093 
3094     case ARRAY_REF:
3095       error ("%s for 'operator[]' in '%E[%E]'", problem, arg1, arg2);
3096       break;
3097 
3098     default:
3099       if (arg2)
3100 	error ("%s for 'operator%s' in '%E %s %E'",
3101                problem, opname, arg1, opname, arg2);
3102       else
3103 	error ("%s for 'operator%s' in '%s%E'",
3104                problem, opname, opname, arg1);
3105       break;
3106     }
3107 }
3108 
3109 /* Return the implicit conversion sequence that could be used to
3110    convert E1 to E2 in [expr.cond].  */
3111 
3112 static tree
conditional_conversion(e1,e2)3113 conditional_conversion (e1, e2)
3114      tree e1;
3115      tree e2;
3116 {
3117   tree t1 = non_reference (TREE_TYPE (e1));
3118   tree t2 = non_reference (TREE_TYPE (e2));
3119   tree conv;
3120   bool good_base;
3121 
3122   /* [expr.cond]
3123 
3124      If E2 is an lvalue: E1 can be converted to match E2 if E1 can be
3125      implicitly converted (clause _conv_) to the type "reference to
3126      T2", subject to the constraint that in the conversion the
3127      reference must bind directly (_dcl.init.ref_) to E1.  */
3128   if (real_lvalue_p (e2))
3129     {
3130       conv = implicit_conversion (build_reference_type (t2),
3131 				  t1,
3132 				  e1,
3133 				  LOOKUP_NO_TEMP_BIND);
3134       if (conv)
3135 	return conv;
3136     }
3137 
3138   /* [expr.cond]
3139 
3140      If E1 and E2 have class type, and the underlying class types are
3141      the same or one is a base class of the other: E1 can be converted
3142      to match E2 if the class of T2 is the same type as, or a base
3143      class of, the class of T1, and the cv-qualification of T2 is the
3144      same cv-qualification as, or a greater cv-qualification than, the
3145      cv-qualification of T1.  If the conversion is applied, E1 is
3146      changed to an rvalue of type T2 that still refers to the original
3147      source class object (or the appropriate subobject thereof).  */
3148   if (CLASS_TYPE_P (t1) && CLASS_TYPE_P (t2)
3149       && ((good_base = DERIVED_FROM_P (t2, t1)) || DERIVED_FROM_P (t1, t2)))
3150     {
3151       if (good_base && at_least_as_qualified_p (t2, t1))
3152 	{
3153 	  conv = build1 (IDENTITY_CONV, t1, e1);
3154 	  if (!same_type_p (TYPE_MAIN_VARIANT (t1),
3155 			    TYPE_MAIN_VARIANT (t2)))
3156 	    conv = build_conv (BASE_CONV, t2, conv);
3157 	  return conv;
3158 	}
3159       else
3160 	return NULL_TREE;
3161     }
3162   else
3163     /* [expr.cond]
3164 
3165        Otherwise: E1 can be converted to match E2 if E1 can be implicitly
3166        converted to the type that expression E2 would have if E2 were
3167        converted to an rvalue (or the type it has, if E2 is an rvalue).  */
3168     return implicit_conversion (t2, t1, e1, LOOKUP_NORMAL);
3169 }
3170 
3171 /* Implement [expr.cond].  ARG1, ARG2, and ARG3 are the three
3172    arguments to the conditional expression.  */
3173 
3174 tree
build_conditional_expr(arg1,arg2,arg3)3175 build_conditional_expr (arg1, arg2, arg3)
3176      tree arg1;
3177      tree arg2;
3178      tree arg3;
3179 {
3180   tree arg2_type;
3181   tree arg3_type;
3182   tree result;
3183   tree result_type = NULL_TREE;
3184   int lvalue_p = 1;
3185   struct z_candidate *candidates = 0;
3186   struct z_candidate *cand;
3187 
3188   /* As a G++ extension, the second argument to the conditional can be
3189      omitted.  (So that `a ? : c' is roughly equivalent to `a ? a :
3190      c'.)  If the second operand is omitted, make sure it is
3191      calculated only once.  */
3192   if (!arg2)
3193     {
3194       if (pedantic)
3195 	pedwarn ("ISO C++ forbids omitting the middle term of a ?: expression");
3196       arg1 = arg2 = save_expr (arg1);
3197     }
3198 
3199   /* [expr.cond]
3200 
3201      The first expr ession is implicitly converted to bool (clause
3202      _conv_).  */
3203   arg1 = cp_convert (boolean_type_node, arg1);
3204 
3205   /* If something has already gone wrong, just pass that fact up the
3206      tree.  */
3207   if (arg1 == error_mark_node
3208       || arg2 == error_mark_node
3209       || arg3 == error_mark_node
3210       || TREE_TYPE (arg1) == error_mark_node
3211       || TREE_TYPE (arg2) == error_mark_node
3212       || TREE_TYPE (arg3) == error_mark_node)
3213     return error_mark_node;
3214 
3215   /* [expr.cond]
3216 
3217      If either the second or the third operand has type (possibly
3218      cv-qualified) void, then the lvalue-to-rvalue (_conv.lval_),
3219      array-to-pointer (_conv.array_), and function-to-pointer
3220      (_conv.func_) standard conversions are performed on the second
3221      and third operands.  */
3222   arg2_type = TREE_TYPE (arg2);
3223   arg3_type = TREE_TYPE (arg3);
3224   if (VOID_TYPE_P (arg2_type) || VOID_TYPE_P (arg3_type))
3225     {
3226       /* Do the conversions.  We don't these for `void' type arguments
3227 	 since it can't have any effect and since decay_conversion
3228 	 does not handle that case gracefully.  */
3229       if (!VOID_TYPE_P (arg2_type))
3230 	arg2 = decay_conversion (arg2);
3231       if (!VOID_TYPE_P (arg3_type))
3232 	arg3 = decay_conversion (arg3);
3233       arg2_type = TREE_TYPE (arg2);
3234       arg3_type = TREE_TYPE (arg3);
3235 
3236       /* [expr.cond]
3237 
3238 	 One of the following shall hold:
3239 
3240 	 --The second or the third operand (but not both) is a
3241 	   throw-expression (_except.throw_); the result is of the
3242 	   type of the other and is an rvalue.
3243 
3244 	 --Both the second and the third operands have type void; the
3245 	   result is of type void and is an rvalue.
3246 
3247          We must avoid calling force_rvalue for expressions of type
3248 	 "void" because it will complain that their value is being
3249 	 used.   */
3250       if (TREE_CODE (arg2) == THROW_EXPR
3251 	  && TREE_CODE (arg3) != THROW_EXPR)
3252 	{
3253 	  if (!VOID_TYPE_P (arg3_type))
3254 	    arg3 = force_rvalue (arg3);
3255 	  arg3_type = TREE_TYPE (arg3);
3256 	  result_type = arg3_type;
3257 	}
3258       else if (TREE_CODE (arg2) != THROW_EXPR
3259 	       && TREE_CODE (arg3) == THROW_EXPR)
3260 	{
3261 	  if (!VOID_TYPE_P (arg2_type))
3262 	    arg2 = force_rvalue (arg2);
3263 	  arg2_type = TREE_TYPE (arg2);
3264 	  result_type = arg2_type;
3265 	}
3266       else if (VOID_TYPE_P (arg2_type) && VOID_TYPE_P (arg3_type))
3267 	result_type = void_type_node;
3268       else
3269 	{
3270 	  error ("`%E' has type `void' and is not a throw-expression",
3271 		    VOID_TYPE_P (arg2_type) ? arg2 : arg3);
3272 	  return error_mark_node;
3273 	}
3274 
3275       lvalue_p = 0;
3276       goto valid_operands;
3277     }
3278   /* [expr.cond]
3279 
3280      Otherwise, if the second and third operand have different types,
3281      and either has (possibly cv-qualified) class type, an attempt is
3282      made to convert each of those operands to the type of the other.  */
3283   else if (!same_type_p (arg2_type, arg3_type)
3284 	   && (CLASS_TYPE_P (arg2_type) || CLASS_TYPE_P (arg3_type)))
3285     {
3286       tree conv2 = conditional_conversion (arg2, arg3);
3287       tree conv3 = conditional_conversion (arg3, arg2);
3288 
3289       /* [expr.cond]
3290 
3291 	 If both can be converted, or one can be converted but the
3292 	 conversion is ambiguous, the program is ill-formed.  If
3293 	 neither can be converted, the operands are left unchanged and
3294 	 further checking is performed as described below.  If exactly
3295 	 one conversion is possible, that conversion is applied to the
3296 	 chosen operand and the converted operand is used in place of
3297 	 the original operand for the remainder of this section.  */
3298       if ((conv2 && !ICS_BAD_FLAG (conv2)
3299 	   && conv3 && !ICS_BAD_FLAG (conv3))
3300 	  || (conv2 && TREE_CODE (conv2) == AMBIG_CONV)
3301 	  || (conv3 && TREE_CODE (conv3) == AMBIG_CONV))
3302 	{
3303 	  error ("operands to ?: have different types");
3304 	  return error_mark_node;
3305 	}
3306       else if (conv2 && !ICS_BAD_FLAG (conv2))
3307 	{
3308 	  arg2 = convert_like (conv2, arg2);
3309 	  arg2 = convert_from_reference (arg2);
3310 	  /* That may not quite have done the trick.  If the two types
3311 	     are cv-qualified variants of one another, we will have
3312 	     just used an IDENTITY_CONV.  (There's no conversion from
3313 	     an lvalue of one class type to an lvalue of another type,
3314 	     even a cv-qualified variant, and we don't want to lose
3315 	     lvalue-ness here.)  So, we manually add a NOP_EXPR here
3316 	     if necessary.  */
3317 	  if (!same_type_p (TREE_TYPE (arg2), arg3_type))
3318 	    arg2 = build1 (NOP_EXPR, arg3_type, arg2);
3319 	  arg2_type = TREE_TYPE (arg2);
3320 	}
3321       else if (conv3 && !ICS_BAD_FLAG (conv3))
3322 	{
3323 	  arg3 = convert_like (conv3, arg3);
3324 	  arg3 = convert_from_reference (arg3);
3325 	  if (!same_type_p (TREE_TYPE (arg3), arg2_type))
3326 	    arg3 = build1 (NOP_EXPR, arg2_type, arg3);
3327 	  arg3_type = TREE_TYPE (arg3);
3328 	}
3329     }
3330 
3331   /* [expr.cond]
3332 
3333      If the second and third operands are lvalues and have the same
3334      type, the result is of that type and is an lvalue.  */
3335   if (real_lvalue_p (arg2) && real_lvalue_p (arg3) &&
3336       same_type_p (arg2_type, arg3_type))
3337     {
3338       result_type = arg2_type;
3339       goto valid_operands;
3340     }
3341 
3342   /* [expr.cond]
3343 
3344      Otherwise, the result is an rvalue.  If the second and third
3345      operand do not have the same type, and either has (possibly
3346      cv-qualified) class type, overload resolution is used to
3347      determine the conversions (if any) to be applied to the operands
3348      (_over.match.oper_, _over.built_).  */
3349   lvalue_p = 0;
3350   if (!same_type_p (arg2_type, arg3_type)
3351       && (CLASS_TYPE_P (arg2_type) || CLASS_TYPE_P (arg3_type)))
3352     {
3353       tree args[3];
3354       tree conv;
3355 
3356       /* Rearrange the arguments so that add_builtin_candidate only has
3357 	 to know about two args.  In build_builtin_candidates, the
3358 	 arguments are unscrambled.  */
3359       args[0] = arg2;
3360       args[1] = arg3;
3361       args[2] = arg1;
3362       add_builtin_candidates (&candidates,
3363 			      COND_EXPR,
3364 			      NOP_EXPR,
3365 			      ansi_opname (COND_EXPR),
3366 			      args,
3367 			      LOOKUP_NORMAL);
3368 
3369       /* [expr.cond]
3370 
3371 	 If the overload resolution fails, the program is
3372 	 ill-formed.  */
3373       if (!any_viable (candidates))
3374 	{
3375 	  op_error (COND_EXPR, NOP_EXPR, arg1, arg2, arg3, "no match");
3376 	  print_z_candidates (candidates);
3377 	  return error_mark_node;
3378 	}
3379       candidates = splice_viable (candidates);
3380       cand = tourney (candidates);
3381       if (!cand)
3382 	{
3383 	  op_error (COND_EXPR, NOP_EXPR, arg1, arg2, arg3, "no match");
3384 	  print_z_candidates (candidates);
3385 	  return error_mark_node;
3386 	}
3387 
3388       /* [expr.cond]
3389 
3390 	 Otherwise, the conversions thus determined are applied, and
3391 	 the converted operands are used in place of the original
3392 	 operands for the remainder of this section.  */
3393       conv = TREE_VEC_ELT (cand->convs, 0);
3394       arg1 = convert_like (conv, arg1);
3395       conv = TREE_VEC_ELT (cand->convs, 1);
3396       arg2 = convert_like (conv, arg2);
3397       conv = TREE_VEC_ELT (cand->convs, 2);
3398       arg3 = convert_like (conv, arg3);
3399     }
3400 
3401   /* [expr.cond]
3402 
3403      Lvalue-to-rvalue (_conv.lval_), array-to-pointer (_conv.array_),
3404      and function-to-pointer (_conv.func_) standard conversions are
3405      performed on the second and third operands.
3406 
3407      We need to force the lvalue-to-rvalue conversion here for class types,
3408      so we get TARGET_EXPRs; trying to deal with a COND_EXPR of class rvalues
3409      that isn't wrapped with a TARGET_EXPR plays havoc with exception
3410      regions.
3411 
3412      We use ocp_convert rather than build_user_type_conversion because the
3413      latter returns NULL_TREE on failure, while the former gives an error.  */
3414 
3415   arg2 = force_rvalue (arg2);
3416   arg2_type = TREE_TYPE (arg2);
3417 
3418   arg3 = force_rvalue (arg3);
3419   arg3_type = TREE_TYPE (arg3);
3420 
3421   if (arg2 == error_mark_node || arg3 == error_mark_node)
3422     return error_mark_node;
3423 
3424   /* [expr.cond]
3425 
3426      After those conversions, one of the following shall hold:
3427 
3428      --The second and third operands have the same type; the result  is  of
3429        that type.  */
3430   if (same_type_p (arg2_type, arg3_type))
3431     result_type = arg2_type;
3432   /* [expr.cond]
3433 
3434      --The second and third operands have arithmetic or enumeration
3435        type; the usual arithmetic conversions are performed to bring
3436        them to a common type, and the result is of that type.  */
3437   else if ((ARITHMETIC_TYPE_P (arg2_type)
3438 	    || TREE_CODE (arg2_type) == ENUMERAL_TYPE)
3439 	   && (ARITHMETIC_TYPE_P (arg3_type)
3440 	       || TREE_CODE (arg3_type) == ENUMERAL_TYPE))
3441     {
3442       /* In this case, there is always a common type.  */
3443       result_type = type_after_usual_arithmetic_conversions (arg2_type,
3444 							     arg3_type);
3445 
3446       if (TREE_CODE (arg2_type) == ENUMERAL_TYPE
3447           && TREE_CODE (arg3_type) == ENUMERAL_TYPE)
3448          warning ("enumeral mismatch in conditional expression: `%T' vs `%T'",
3449                    arg2_type, arg3_type);
3450       else if (extra_warnings
3451                && ((TREE_CODE (arg2_type) == ENUMERAL_TYPE
3452                     && !same_type_p (arg3_type, type_promotes_to (arg2_type)))
3453                    || (TREE_CODE (arg3_type) == ENUMERAL_TYPE
3454                        && !same_type_p (arg2_type, type_promotes_to (arg3_type)))))
3455         warning ("enumeral and non-enumeral type in conditional expression");
3456 
3457       arg2 = perform_implicit_conversion (result_type, arg2);
3458       arg3 = perform_implicit_conversion (result_type, arg3);
3459     }
3460   /* [expr.cond]
3461 
3462      --The second and third operands have pointer type, or one has
3463        pointer type and the other is a null pointer constant; pointer
3464        conversions (_conv.ptr_) and qualification conversions
3465        (_conv.qual_) are performed to bring them to their composite
3466        pointer type (_expr.rel_).  The result is of the composite
3467        pointer type.
3468 
3469      --The second and third operands have pointer to member type, or
3470        one has pointer to member type and the other is a null pointer
3471        constant; pointer to member conversions (_conv.mem_) and
3472        qualification conversions (_conv.qual_) are performed to bring
3473        them to a common type, whose cv-qualification shall match the
3474        cv-qualification of either the second or the third operand.
3475        The result is of the common type.  */
3476   else if ((null_ptr_cst_p (arg2)
3477 	    && (TYPE_PTR_P (arg3_type) || TYPE_PTRMEM_P (arg3_type)
3478 		|| TYPE_PTRMEMFUNC_P (arg3_type)))
3479 	   || (null_ptr_cst_p (arg3)
3480 	       && (TYPE_PTR_P (arg2_type) || TYPE_PTRMEM_P (arg2_type)
3481 		|| TYPE_PTRMEMFUNC_P (arg2_type)))
3482 	   || (TYPE_PTR_P (arg2_type) && TYPE_PTR_P (arg3_type))
3483 	   || (TYPE_PTRMEM_P (arg2_type) && TYPE_PTRMEM_P (arg3_type))
3484 	   || (TYPE_PTRMEMFUNC_P (arg2_type)
3485 	       && TYPE_PTRMEMFUNC_P (arg3_type)))
3486     {
3487       result_type = composite_pointer_type (arg2_type, arg3_type, arg2,
3488 					    arg3, "conditional expression");
3489       arg2 = perform_implicit_conversion (result_type, arg2);
3490       arg3 = perform_implicit_conversion (result_type, arg3);
3491     }
3492 
3493   if (!result_type)
3494     {
3495       error ("operands to ?: have different types");
3496       return error_mark_node;
3497     }
3498 
3499  valid_operands:
3500   result = fold (build (COND_EXPR, result_type, arg1, arg2, arg3));
3501   /* Expand both sides into the same slot, hopefully the target of the
3502      ?: expression.  We used to check for TARGET_EXPRs here, but now we
3503      sometimes wrap them in NOP_EXPRs so the test would fail.  */
3504   if (!lvalue_p && IS_AGGR_TYPE (result_type))
3505     result = build_target_expr_with_type (result, result_type);
3506 
3507   /* If this expression is an rvalue, but might be mistaken for an
3508      lvalue, we must add a NON_LVALUE_EXPR.  */
3509   if (!lvalue_p && real_lvalue_p (result))
3510     result = build1 (NON_LVALUE_EXPR, result_type, result);
3511 
3512   return result;
3513 }
3514 
3515 tree
build_new_op(code,flags,arg1,arg2,arg3)3516 build_new_op (code, flags, arg1, arg2, arg3)
3517      enum tree_code code;
3518      int flags;
3519      tree arg1, arg2, arg3;
3520 {
3521   struct z_candidate *candidates = 0, *cand;
3522   tree fns, mem_arglist = NULL_TREE, arglist, fnname;
3523   enum tree_code code2 = NOP_EXPR;
3524   tree conv;
3525   bool viable_candidates;
3526 
3527   if (arg1 == error_mark_node
3528       || arg2 == error_mark_node
3529       || arg3 == error_mark_node)
3530     return error_mark_node;
3531 
3532   /* This can happen if a template takes all non-type parameters, e.g.
3533      undeclared_template<1, 5, 72>a;  */
3534   if (code == LT_EXPR && TREE_CODE (arg1) == TEMPLATE_DECL)
3535     {
3536       error ("`%D' must be declared before use", arg1);
3537       return error_mark_node;
3538     }
3539 
3540   if (code == MODIFY_EXPR)
3541     {
3542       code2 = TREE_CODE (arg3);
3543       arg3 = NULL_TREE;
3544       fnname = ansi_assopname (code2);
3545     }
3546   else
3547     fnname = ansi_opname (code);
3548 
3549   if (TREE_CODE (arg1) == OFFSET_REF)
3550     arg1 = resolve_offset_ref (arg1);
3551   arg1 = convert_from_reference (arg1);
3552   if (CLASS_TYPE_P (TREE_TYPE (arg1))
3553       && CLASSTYPE_TEMPLATE_INSTANTIATION (TREE_TYPE (arg1)))
3554     /* Make sure the template type is instantiated now.  */
3555     instantiate_class_template (TYPE_MAIN_VARIANT (TREE_TYPE (arg1)));
3556 
3557   switch (code)
3558     {
3559     case NEW_EXPR:
3560     case VEC_NEW_EXPR:
3561     case VEC_DELETE_EXPR:
3562     case DELETE_EXPR:
3563       /* Use build_op_new_call and build_op_delete_call instead.  */
3564       abort ();
3565 
3566     case CALL_EXPR:
3567       return build_object_call (arg1, arg2);
3568 
3569     default:
3570       break;
3571     }
3572 
3573   if (arg2)
3574     {
3575       if (TREE_CODE (arg2) == OFFSET_REF)
3576 	arg2 = resolve_offset_ref (arg2);
3577       arg2 = convert_from_reference (arg2);
3578       if (CLASS_TYPE_P (TREE_TYPE (arg2))
3579 	  && CLASSTYPE_TEMPLATE_INSTANTIATION (TREE_TYPE (arg2)))
3580 	instantiate_class_template (TYPE_MAIN_VARIANT (TREE_TYPE (arg2)));
3581     }
3582   if (arg3)
3583     {
3584       if (TREE_CODE (arg3) == OFFSET_REF)
3585 	arg3 = resolve_offset_ref (arg3);
3586       arg3 = convert_from_reference (arg3);
3587       if (CLASS_TYPE_P (TREE_TYPE (arg3))
3588 	  && CLASSTYPE_TEMPLATE_INSTANTIATION (TREE_TYPE (arg3)))
3589 	instantiate_class_template (TYPE_MAIN_VARIANT (TREE_TYPE (arg3)));
3590     }
3591 
3592   if (code == COND_EXPR)
3593     {
3594       if (arg2 == NULL_TREE
3595 	  || TREE_CODE (TREE_TYPE (arg2)) == VOID_TYPE
3596 	  || TREE_CODE (TREE_TYPE (arg3)) == VOID_TYPE
3597 	  || (! IS_OVERLOAD_TYPE (TREE_TYPE (arg2))
3598 	      && ! IS_OVERLOAD_TYPE (TREE_TYPE (arg3))))
3599 	goto builtin;
3600     }
3601   else if (! IS_OVERLOAD_TYPE (TREE_TYPE (arg1))
3602 	   && (! arg2 || ! IS_OVERLOAD_TYPE (TREE_TYPE (arg2))))
3603     goto builtin;
3604 
3605   if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
3606     arg2 = integer_zero_node;
3607 
3608   arglist = NULL_TREE;
3609   if (arg3)
3610     arglist = tree_cons (NULL_TREE, arg3, arglist);
3611   if (arg2)
3612     arglist = tree_cons (NULL_TREE, arg2, arglist);
3613   arglist = tree_cons (NULL_TREE, arg1, arglist);
3614 
3615   fns = lookup_function_nonclass (fnname, arglist);
3616 
3617   if (fns && TREE_CODE (fns) == TREE_LIST)
3618     fns = TREE_VALUE (fns);
3619   for (; fns; fns = OVL_NEXT (fns))
3620     {
3621       tree fn = OVL_CURRENT (fns);
3622       if (TREE_CODE (fn) == TEMPLATE_DECL)
3623 	add_template_candidate (&candidates, fn, NULL_TREE, NULL_TREE,
3624 				arglist, TREE_TYPE (fnname),
3625 				/*access_path=*/NULL_TREE,
3626 				/*conversion_path=*/NULL_TREE,
3627 				flags, DEDUCE_CALL);
3628       else
3629 	add_function_candidate (&candidates, fn, NULL_TREE,
3630 				arglist,
3631 				/*access_path=*/NULL_TREE,
3632 				/*conversion_path=*/NULL_TREE,
3633 				flags);
3634     }
3635 
3636   if (IS_AGGR_TYPE (TREE_TYPE (arg1)))
3637     {
3638       fns = lookup_fnfields (TYPE_BINFO (TREE_TYPE (arg1)), fnname, 1);
3639       if (fns == error_mark_node)
3640 	return fns;
3641     }
3642   else
3643     fns = NULL_TREE;
3644 
3645   if (fns)
3646     {
3647       tree conversion_path = BASELINK_BINFO (fns);
3648 
3649       mem_arglist = tree_cons (NULL_TREE, build_this (arg1), TREE_CHAIN (arglist));
3650       for (fns = BASELINK_FUNCTIONS (fns); fns; fns = OVL_NEXT (fns))
3651 	{
3652 	  tree fn = OVL_CURRENT (fns);
3653 	  tree this_arglist;
3654 	  tree access_path = TYPE_BINFO (TREE_TYPE (arg1));
3655 
3656 	  if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
3657 	    this_arglist = mem_arglist;
3658 	  else
3659 	    this_arglist = arglist;
3660 
3661 	  if (TREE_CODE (fn) == TEMPLATE_DECL)
3662 	    /* A member template.  */
3663 	    add_template_candidate (&candidates, fn,
3664 				    BINFO_TYPE (conversion_path),
3665 				    NULL_TREE,
3666 				    this_arglist,  TREE_TYPE (fnname),
3667 				    access_path, conversion_path,
3668 				    flags, DEDUCE_CALL);
3669 	  else
3670 	    add_function_candidate
3671 	      (&candidates, fn, BINFO_TYPE (conversion_path), this_arglist,
3672 	       access_path, conversion_path, flags);
3673 	}
3674     }
3675 
3676   {
3677     tree args[3];
3678 
3679     /* Rearrange the arguments for ?: so that add_builtin_candidate only has
3680        to know about two args; a builtin candidate will always have a first
3681        parameter of type bool.  We'll handle that in
3682        build_builtin_candidate.  */
3683     if (code == COND_EXPR)
3684       {
3685 	args[0] = arg2;
3686 	args[1] = arg3;
3687 	args[2] = arg1;
3688       }
3689     else
3690       {
3691 	args[0] = arg1;
3692 	args[1] = arg2;
3693 	args[2] = NULL_TREE;
3694       }
3695 
3696     add_builtin_candidates (&candidates, code, code2, fnname, args, flags);
3697   }
3698 
3699   switch (code)
3700     {
3701     case COMPOUND_EXPR:
3702     case ADDR_EXPR:
3703       /* For these, the built-in candidates set is empty
3704 	 [over.match.oper]/3.  We don't want non-strict matches
3705 	 because exact matches are always possible with built-in
3706 	 operators.  The built-in candidate set for COMPONENT_REF
3707 	 would be empty too, but since there are no such built-in
3708 	 operators, we accept non-strict matches for them.  */
3709       viable_candidates = any_strictly_viable (candidates);
3710       break;
3711 
3712     default:
3713       viable_candidates = any_viable (candidates);
3714       break;
3715     }
3716 
3717   if (! viable_candidates)
3718     {
3719       switch (code)
3720 	{
3721 	case POSTINCREMENT_EXPR:
3722 	case POSTDECREMENT_EXPR:
3723 	  /* Look for an `operator++ (int)'.  If they didn't have
3724 	     one, then we fall back to the old way of doing things.  */
3725 	  if (flags & LOOKUP_COMPLAIN)
3726 	    pedwarn ("no `%D(int)' declared for postfix `%s', trying prefix operator instead",
3727 			fnname,
3728 			operator_name_info[code].name);
3729 	  if (code == POSTINCREMENT_EXPR)
3730 	    code = PREINCREMENT_EXPR;
3731 	  else
3732 	    code = PREDECREMENT_EXPR;
3733 	  return build_new_op (code, flags, arg1, NULL_TREE, NULL_TREE);
3734 
3735 	  /* The caller will deal with these.  */
3736 	case ADDR_EXPR:
3737 	case COMPOUND_EXPR:
3738 	case COMPONENT_REF:
3739 	  return NULL_TREE;
3740 
3741 	default:
3742 	  break;
3743 	}
3744       if (flags & LOOKUP_COMPLAIN)
3745 	{
3746 	  op_error (code, code2, arg1, arg2, arg3, "no match");
3747 	  print_z_candidates (candidates);
3748 	}
3749       return error_mark_node;
3750     }
3751   candidates = splice_viable (candidates);
3752   cand = tourney (candidates);
3753 
3754   if (cand == 0)
3755     {
3756       if (flags & LOOKUP_COMPLAIN)
3757 	{
3758 	  op_error (code, code2, arg1, arg2, arg3, "ambiguous overload");
3759 	  print_z_candidates (candidates);
3760 	}
3761       return error_mark_node;
3762     }
3763 
3764   if (TREE_CODE (cand->fn) == FUNCTION_DECL)
3765     return build_over_call
3766       (cand,
3767        TREE_CODE (TREE_TYPE (cand->fn)) == METHOD_TYPE
3768        ? mem_arglist : arglist,
3769        LOOKUP_NORMAL);
3770 
3771   /* Check for comparison of different enum types.  */
3772   switch (code)
3773     {
3774     case GT_EXPR:
3775     case LT_EXPR:
3776     case GE_EXPR:
3777     case LE_EXPR:
3778     case EQ_EXPR:
3779     case NE_EXPR:
3780       if (TREE_CODE (TREE_TYPE (arg1)) == ENUMERAL_TYPE
3781 	  && TREE_CODE (TREE_TYPE (arg2)) == ENUMERAL_TYPE
3782 	  && (TYPE_MAIN_VARIANT (TREE_TYPE (arg1))
3783 	      != TYPE_MAIN_VARIANT (TREE_TYPE (arg2))))
3784 	{
3785 	  warning ("comparison between `%#T' and `%#T'",
3786 		      TREE_TYPE (arg1), TREE_TYPE (arg2));
3787 	}
3788       break;
3789     default:
3790       break;
3791     }
3792 
3793   /* We need to strip any leading REF_BIND so that bitfields don't cause
3794      errors.  This should not remove any important conversions, because
3795      builtins don't apply to class objects directly.  */
3796   conv = TREE_VEC_ELT (cand->convs, 0);
3797   if (TREE_CODE (conv) == REF_BIND)
3798     conv = TREE_OPERAND (conv, 0);
3799   arg1 = convert_like (conv, arg1);
3800   if (arg2)
3801     {
3802       conv = TREE_VEC_ELT (cand->convs, 1);
3803       if (TREE_CODE (conv) == REF_BIND)
3804         conv = TREE_OPERAND (conv, 0);
3805       arg2 = convert_like (conv, arg2);
3806     }
3807   if (arg3)
3808     {
3809       conv = TREE_VEC_ELT (cand->convs, 2);
3810       if (TREE_CODE (conv) == REF_BIND)
3811         conv = TREE_OPERAND (conv, 0);
3812       arg3 = convert_like (conv, arg3);
3813     }
3814 
3815 builtin:
3816   switch (code)
3817     {
3818     case MODIFY_EXPR:
3819       return build_modify_expr (arg1, code2, arg2);
3820 
3821     case INDIRECT_REF:
3822       return build_indirect_ref (arg1, "unary *");
3823 
3824     case PLUS_EXPR:
3825     case MINUS_EXPR:
3826     case MULT_EXPR:
3827     case TRUNC_DIV_EXPR:
3828     case GT_EXPR:
3829     case LT_EXPR:
3830     case GE_EXPR:
3831     case LE_EXPR:
3832     case EQ_EXPR:
3833     case NE_EXPR:
3834     case MAX_EXPR:
3835     case MIN_EXPR:
3836     case LSHIFT_EXPR:
3837     case RSHIFT_EXPR:
3838     case TRUNC_MOD_EXPR:
3839     case BIT_AND_EXPR:
3840     case BIT_IOR_EXPR:
3841     case BIT_XOR_EXPR:
3842     case TRUTH_ANDIF_EXPR:
3843     case TRUTH_ORIF_EXPR:
3844       return cp_build_binary_op (code, arg1, arg2);
3845 
3846     case CONVERT_EXPR:
3847     case NEGATE_EXPR:
3848     case BIT_NOT_EXPR:
3849     case TRUTH_NOT_EXPR:
3850     case PREINCREMENT_EXPR:
3851     case POSTINCREMENT_EXPR:
3852     case PREDECREMENT_EXPR:
3853     case POSTDECREMENT_EXPR:
3854     case REALPART_EXPR:
3855     case IMAGPART_EXPR:
3856       return build_unary_op (code, arg1, candidates != 0);
3857 
3858     case ARRAY_REF:
3859       return build_array_ref (arg1, arg2);
3860 
3861     case COND_EXPR:
3862       return build_conditional_expr (arg1, arg2, arg3);
3863 
3864     case MEMBER_REF:
3865       return build_m_component_ref
3866 	(build_indirect_ref (arg1, NULL), arg2);
3867 
3868       /* The caller will deal with these.  */
3869     case ADDR_EXPR:
3870     case COMPONENT_REF:
3871     case COMPOUND_EXPR:
3872       return NULL_TREE;
3873 
3874     default:
3875       abort ();
3876       return NULL_TREE;
3877     }
3878 }
3879 
3880 /* Build a call to operator delete.  This has to be handled very specially,
3881    because the restrictions on what signatures match are different from all
3882    other call instances.  For a normal delete, only a delete taking (void *)
3883    or (void *, size_t) is accepted.  For a placement delete, only an exact
3884    match with the placement new is accepted.
3885 
3886    CODE is either DELETE_EXPR or VEC_DELETE_EXPR.
3887    ADDR is the pointer to be deleted.
3888    SIZE is the size of the memory block to be deleted.
3889    FLAGS are the usual overloading flags.
3890    PLACEMENT is the corresponding placement new call, or NULL_TREE.  */
3891 
3892 tree
build_op_delete_call(code,addr,size,flags,placement)3893 build_op_delete_call (code, addr, size, flags, placement)
3894      enum tree_code code;
3895      tree addr, size, placement;
3896      int flags;
3897 {
3898   tree fn = NULL_TREE;
3899   tree fns, fnname, fntype, argtypes, args, type;
3900   int pass;
3901 
3902   if (addr == error_mark_node)
3903     return error_mark_node;
3904 
3905   type = TREE_TYPE (TREE_TYPE (addr));
3906   while (TREE_CODE (type) == ARRAY_TYPE)
3907     type = TREE_TYPE (type);
3908 
3909   fnname = ansi_opname (code);
3910 
3911   if (IS_AGGR_TYPE (type) && ! (flags & LOOKUP_GLOBAL))
3912     /* In [class.free]
3913 
3914        If the result of the lookup is ambiguous or inaccessible, or if
3915        the lookup selects a placement deallocation function, the
3916        program is ill-formed.
3917 
3918        Therefore, we ask lookup_fnfields to complain ambout ambiguity.  */
3919     {
3920       fns = lookup_fnfields (TYPE_BINFO (type), fnname, 1);
3921       if (fns == error_mark_node)
3922 	return error_mark_node;
3923     }
3924   else
3925     fns = NULL_TREE;
3926 
3927   if (fns == NULL_TREE)
3928     fns = lookup_name_nonclass (fnname);
3929 
3930   if (placement)
3931     {
3932       tree alloc_fn;
3933       tree call_expr;
3934 
3935       /* Find the allocation function that is being called.  */
3936       call_expr = placement;
3937       /* Sometimes we have a COMPOUND_EXPR, rather than a simple
3938 	 CALL_EXPR.  */
3939       while (TREE_CODE (call_expr) == COMPOUND_EXPR)
3940 	call_expr = TREE_OPERAND (call_expr, 1);
3941       /* Extract the function.  */
3942       alloc_fn = get_callee_fndecl (call_expr);
3943       my_friendly_assert (alloc_fn != NULL_TREE, 20020327);
3944       /* Then the second parm type.  */
3945       argtypes = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (alloc_fn)));
3946       /* Also the second argument.  */
3947       args = TREE_CHAIN (TREE_OPERAND (call_expr, 1));
3948     }
3949   else
3950     {
3951       /* First try it without the size argument.  */
3952       argtypes = void_list_node;
3953       args = NULL_TREE;
3954     }
3955 
3956   /* Strip const and volatile from addr.  */
3957   addr = cp_convert (ptr_type_node, addr);
3958 
3959   /* We make two tries at finding a matching `operator delete'.  On
3960      the first pass, we look for an one-operator (or placement)
3961      operator delete.  If we're not doing placement delete, then on
3962      the second pass we look for a two-argument delete.  */
3963   for (pass = 0; pass < (placement ? 1 : 2); ++pass)
3964     {
3965       if (pass == 0)
3966 	argtypes = tree_cons (NULL_TREE, ptr_type_node, argtypes);
3967       else
3968 	/* Normal delete; now try to find a match including the size
3969 	   argument.  */
3970 	argtypes = tree_cons (NULL_TREE, ptr_type_node,
3971 			      tree_cons (NULL_TREE, sizetype,
3972 					 void_list_node));
3973       fntype = build_function_type (void_type_node, argtypes);
3974 
3975       /* Go through the `operator delete' functions looking for one
3976 	 with a matching type.  */
3977       for (fn = BASELINK_P (fns) ? BASELINK_FUNCTIONS (fns) : fns;
3978 	   fn;
3979 	   fn = OVL_NEXT (fn))
3980 	{
3981 	  tree t;
3982 
3983 	  /* Exception specifications on the `delete' operator do not
3984 	     matter.  */
3985 	  t = build_exception_variant (TREE_TYPE (OVL_CURRENT (fn)),
3986 				       NULL_TREE);
3987 	  /* We also don't compare attributes.  We're really just
3988 	     trying to check the types of the first two parameters.  */
3989 	  if (comptypes (t, fntype, COMPARE_NO_ATTRIBUTES))
3990 	    break;
3991 	}
3992 
3993       /* If we found a match, we're done.  */
3994       if (fn)
3995 	break;
3996     }
3997 
3998   /* If we have a matching function, call it.  */
3999   if (fn)
4000     {
4001       /* Make sure we have the actual function, and not an
4002 	 OVERLOAD.  */
4003       fn = OVL_CURRENT (fn);
4004 
4005       /* If the FN is a member function, make sure that it is
4006 	 accessible.  */
4007       if (DECL_CLASS_SCOPE_P (fn))
4008 	enforce_access (type, fn);
4009 
4010       if (pass == 0)
4011 	args = tree_cons (NULL_TREE, addr, args);
4012       else
4013 	args = tree_cons (NULL_TREE, addr,
4014 			  build_tree_list (NULL_TREE, size));
4015 
4016       return build_function_call (fn, args);
4017     }
4018 
4019   /* If we are doing placement delete we do nothing if we don't find a
4020      matching op delete.  */
4021   if (placement)
4022     return NULL_TREE;
4023 
4024   error ("no suitable `operator delete' for `%T'", type);
4025   return error_mark_node;
4026 }
4027 
4028 /* If the current scope isn't allowed to access DECL along
4029    BASETYPE_PATH, give an error.  The most derived class in
4030    BASETYPE_PATH is the one used to qualify DECL.  */
4031 
4032 int
enforce_access(basetype_path,decl)4033 enforce_access (basetype_path, decl)
4034      tree basetype_path;
4035      tree decl;
4036 {
4037   int accessible;
4038 
4039   accessible = accessible_p (basetype_path, decl);
4040   if (!accessible)
4041     {
4042       if (TREE_PRIVATE (decl))
4043 	cp_error_at ("`%+#D' is private", decl);
4044       else if (TREE_PROTECTED (decl))
4045 	cp_error_at ("`%+#D' is protected", decl);
4046       else
4047 	cp_error_at ("`%+#D' is inaccessible", decl);
4048       error ("within this context");
4049       return 0;
4050     }
4051 
4052   return 1;
4053 }
4054 
4055 /* Perform the conversions in CONVS on the expression EXPR.  FN and
4056    ARGNUM are used for diagnostics.  ARGNUM is zero based, -1
4057    indicates the `this' argument of a method.  INNER is nonzero when
4058    being called to continue a conversion chain. It is negative when a
4059    reference binding will be applied, positive otherwise.  If
4060    ISSUE_CONVERSION_WARNINGS is true, warnings about suspicious
4061    conversions will be emitted if appropriate.  */
4062 
4063 static tree
convert_like_real(tree convs,tree expr,tree fn,int argnum,int inner,bool issue_conversion_warnings)4064 convert_like_real (tree convs, tree expr, tree fn, int argnum, int inner,
4065 		   bool issue_conversion_warnings)
4066 {
4067   int savew, savee;
4068 
4069   tree totype = TREE_TYPE (convs);
4070 
4071   if (ICS_BAD_FLAG (convs)
4072       && TREE_CODE (convs) != USER_CONV
4073       && TREE_CODE (convs) != AMBIG_CONV
4074       && TREE_CODE (convs) != REF_BIND)
4075     {
4076       tree t = convs;
4077       for (; t; t = TREE_OPERAND (t, 0))
4078 	{
4079 	  if (TREE_CODE (t) == USER_CONV || !ICS_BAD_FLAG (t))
4080 	    {
4081 	      expr = convert_like_real (t, expr, fn, argnum, 1,
4082 					/*issue_conversion_warnings=*/false);
4083 	      break;
4084 	    }
4085 	  else if (TREE_CODE (t) == AMBIG_CONV)
4086 	    return convert_like_real (t, expr, fn, argnum, 1,
4087 				      /*issue_conversion_warnings=*/false);
4088 	  else if (TREE_CODE (t) == IDENTITY_CONV)
4089 	    break;
4090 	}
4091       pedwarn ("invalid conversion from `%T' to `%T'", TREE_TYPE (expr), totype);
4092       if (fn)
4093 	pedwarn ("  initializing argument %P of `%D'", argnum, fn);
4094       return cp_convert (totype, expr);
4095     }
4096 
4097   if (issue_conversion_warnings)
4098     expr = dubious_conversion_warnings
4099              (totype, expr, "converting", fn, argnum);
4100   switch (TREE_CODE (convs))
4101     {
4102     case USER_CONV:
4103       {
4104 	struct z_candidate *cand = USER_CONV_CAND (convs);
4105 	tree convfn = cand->fn;
4106 	tree args;
4107 
4108 	if (DECL_CONSTRUCTOR_P (convfn))
4109 	  {
4110 	    tree t = build_int_2 (0, 0);
4111 	    TREE_TYPE (t) = build_pointer_type (DECL_CONTEXT (convfn));
4112 
4113 	    args = build_tree_list (NULL_TREE, expr);
4114 	    if (DECL_HAS_IN_CHARGE_PARM_P (convfn)
4115 		|| DECL_HAS_VTT_PARM_P (convfn))
4116 	      /* We should never try to call the abstract or base constructor
4117 		 from here.  */
4118 	      abort ();
4119 	    args = tree_cons (NULL_TREE, t, args);
4120 	  }
4121 	else
4122 	  args = build_this (expr);
4123 	expr = build_over_call (cand, args, LOOKUP_NORMAL);
4124 
4125 	/* If this is a constructor or a function returning an aggr type,
4126 	   we need to build up a TARGET_EXPR.  */
4127 	if (DECL_CONSTRUCTOR_P (convfn))
4128 	  expr = build_cplus_new (totype, expr);
4129 
4130 	/* The result of the call is then used to direct-initialize the object
4131 	   that is the destination of the copy-initialization.  [dcl.init]
4132 
4133 	   Note that this step is not reflected in the conversion sequence;
4134 	   it affects the semantics when we actually perform the
4135 	   conversion, but is not considered during overload resolution.
4136 
4137 	   If the target is a class, that means call a ctor.  */
4138 	if (IS_AGGR_TYPE (totype)
4139 	    && (inner >= 0 || !lvalue_p (expr)))
4140 	  {
4141 	    savew = warningcount, savee = errorcount;
4142 	    expr = build_special_member_call
4143 	      (NULL_TREE, complete_ctor_identifier,
4144 	       build_tree_list (NULL_TREE, expr), TYPE_BINFO (totype),
4145 	       /* Core issue 84, now a DR, says that we don't allow UDCs
4146 		  for these args (which deliberately breaks copy-init of an
4147 		  auto_ptr<Base> from an auto_ptr<Derived>).  */
4148 	       LOOKUP_NORMAL|LOOKUP_ONLYCONVERTING|LOOKUP_NO_CONVERSION);
4149 
4150 	    /* Tell the user where this failing constructor call came from.  */
4151 	    if (fn)
4152 	      {
4153 		if (warningcount > savew)
4154 		  warning
4155 		    ("  initializing argument %P of `%D' from result of `%D'",
4156 		     argnum, fn, convfn);
4157 		else if (errorcount > savee)
4158 		  error
4159 		    ("  initializing argument %P of `%D' from result of `%D'",
4160 		     argnum, fn, convfn);
4161 	      }
4162 	    else
4163 	      {
4164 		if (warningcount > savew)
4165 		  warning ("  initializing temporary from result of `%D'",
4166 			      convfn);
4167 		else if (errorcount > savee)
4168 		  error ("  initializing temporary from result of `%D'",
4169 			    convfn);
4170 	      }
4171 	    expr = build_cplus_new (totype, expr);
4172 	  }
4173 	return expr;
4174       }
4175     case IDENTITY_CONV:
4176       if (type_unknown_p (expr))
4177 	expr = instantiate_type (totype, expr, tf_error | tf_warning);
4178       return expr;
4179     case AMBIG_CONV:
4180       /* Call build_user_type_conversion again for the error.  */
4181       return build_user_type_conversion
4182 	(totype, TREE_OPERAND (convs, 0), LOOKUP_NORMAL);
4183 
4184     default:
4185       break;
4186     };
4187 
4188   expr = convert_like_real (TREE_OPERAND (convs, 0), expr, fn, argnum,
4189                             TREE_CODE (convs) == REF_BIND ? -1 : 1,
4190 			    /*issue_conversion_warnings=*/false);
4191   if (expr == error_mark_node)
4192     return error_mark_node;
4193 
4194   /* Convert a non-array constant variable to its underlying value, unless we
4195      are about to bind it to a reference, in which case we need to
4196      leave it as an lvalue.  */
4197   if (TREE_CODE (convs) != REF_BIND
4198       && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
4199     expr = decl_constant_value (expr);
4200 
4201   switch (TREE_CODE (convs))
4202     {
4203     case RVALUE_CONV:
4204       if (! IS_AGGR_TYPE (totype))
4205 	return expr;
4206       /* else fall through */
4207     case BASE_CONV:
4208       if (TREE_CODE (convs) == BASE_CONV && !NEED_TEMPORARY_P (convs))
4209 	{
4210 	  /* We are going to bind a reference directly to a base-class
4211 	     subobject of EXPR.  */
4212 	  tree base_ptr = build_pointer_type (totype);
4213 
4214 	  /* Build an expression for `*((base*) &expr)'.  */
4215 	  expr = build_unary_op (ADDR_EXPR, expr, 0);
4216 	  expr = perform_implicit_conversion (base_ptr, expr);
4217 	  expr = build_indirect_ref (expr, "implicit conversion");
4218 	  return expr;
4219 	}
4220 
4221       /* Copy-initialization where the cv-unqualified version of the source
4222 	 type is the same class as, or a derived class of, the class of the
4223 	 destination [is treated as direct-initialization].  [dcl.init] */
4224       savew = warningcount, savee = errorcount;
4225       expr = build_special_member_call (NULL_TREE, complete_ctor_identifier,
4226 					build_tree_list (NULL_TREE, expr),
4227 					TYPE_BINFO (totype),
4228 					LOOKUP_NORMAL|LOOKUP_ONLYCONVERTING);
4229       if (fn)
4230 	{
4231 	  if (warningcount > savew)
4232 	    warning ("  initializing argument %P of `%D'", argnum, fn);
4233 	  else if (errorcount > savee)
4234 	    error ("  initializing argument %P of `%D'", argnum, fn);
4235 	}
4236       return build_cplus_new (totype, expr);
4237 
4238     case REF_BIND:
4239       {
4240 	tree ref_type = totype;
4241 
4242 	/* If necessary, create a temporary.  */
4243 	if (NEED_TEMPORARY_P (convs) || !non_cast_lvalue_p (expr))
4244 	  {
4245 	    tree type = TREE_TYPE (TREE_OPERAND (convs, 0));
4246 	    expr = build_target_expr_with_type (expr, type);
4247 	  }
4248 
4249 	/* Take the address of the thing to which we will bind the
4250 	   reference.  */
4251 	expr = build_unary_op (ADDR_EXPR, expr, 1);
4252 	if (expr == error_mark_node)
4253 	  return error_mark_node;
4254 
4255 	/* Convert it to a pointer to the type referred to by the
4256 	   reference.  This will adjust the pointer if a derived to
4257 	   base conversion is being performed.  */
4258 	expr = cp_convert (build_pointer_type (TREE_TYPE (ref_type)),
4259 			   expr);
4260 	/* Convert the pointer to the desired reference type.  */
4261 	return build_nop (ref_type, expr);
4262       }
4263 
4264     case LVALUE_CONV:
4265       return decay_conversion (expr);
4266 
4267     case QUAL_CONV:
4268       /* Warn about deprecated conversion if appropriate.  */
4269       string_conv_p (totype, expr, 1);
4270       break;
4271 
4272     default:
4273       break;
4274     }
4275   return ocp_convert (totype, expr, CONV_IMPLICIT,
4276 		      LOOKUP_NORMAL|LOOKUP_NO_CONVERSION);
4277 }
4278 
4279 /* Build a call to __builtin_trap which can be used in an expression.  */
4280 
4281 static tree
call_builtin_trap()4282 call_builtin_trap ()
4283 {
4284   tree fn = get_identifier ("__builtin_trap");
4285   if (IDENTIFIER_GLOBAL_VALUE (fn))
4286     fn = IDENTIFIER_GLOBAL_VALUE (fn);
4287   else
4288     abort ();
4289 
4290   fn = build_call (fn, NULL_TREE);
4291   fn = build (COMPOUND_EXPR, integer_type_node, fn, integer_zero_node);
4292   return fn;
4293 }
4294 
4295 /* ARG is being passed to a varargs function.  Perform any conversions
4296    required.  Array/function to pointer decay must have already happened.
4297    Return the converted value.  */
4298 
4299 tree
convert_arg_to_ellipsis(arg)4300 convert_arg_to_ellipsis (arg)
4301      tree arg;
4302 {
4303   if (TREE_CODE (TREE_TYPE (arg)) == REAL_TYPE
4304       && (TYPE_PRECISION (TREE_TYPE (arg))
4305 	  < TYPE_PRECISION (double_type_node)))
4306     /* Convert `float' to `double'.  */
4307     arg = cp_convert (double_type_node, arg);
4308   else
4309     /* Convert `short' and `char' to full-size `int'.  */
4310     arg = default_conversion (arg);
4311 
4312   arg = require_complete_type (arg);
4313 
4314   if (arg != error_mark_node
4315       && !pod_type_p (TREE_TYPE (arg)))
4316     {
4317       /* Undefined behavior [expr.call] 5.2.2/7.  We used to just warn
4318 	 here and do a bitwise copy, but now cp_expr_size will abort if we
4319 	 try to do that.
4320 	 If the call appears in the context of a sizeof expression,
4321 	 there is no need to emit a warning, since the expression won't be
4322 	 evaluated. We keep the builtin_trap just as a safety check.  */
4323       if (!skip_evaluation)
4324 	warning ("cannot pass objects of non-POD type `%#T' through `...'; "
4325 		 "call will abort at runtime", TREE_TYPE (arg));
4326       arg = call_builtin_trap ();
4327     }
4328 
4329   return arg;
4330 }
4331 
4332 /* va_arg (EXPR, TYPE) is a builtin. Make sure it is not abused.  */
4333 
4334 tree
build_x_va_arg(expr,type)4335 build_x_va_arg (expr, type)
4336      tree expr;
4337      tree type;
4338 {
4339   if (processing_template_decl)
4340     return build_min (VA_ARG_EXPR, type, expr);
4341 
4342   type = complete_type_or_else (type, NULL_TREE);
4343 
4344   if (expr == error_mark_node || !type)
4345     return error_mark_node;
4346 
4347   if (! pod_type_p (type))
4348     {
4349       /* Undefined behavior [expr.call] 5.2.2/7.  */
4350       warning ("cannot receive objects of non-POD type `%#T' through `...'",
4351 		  type);
4352     }
4353 
4354   return build_va_arg (expr, type);
4355 }
4356 
4357 /* TYPE has been given to va_arg.  Apply the default conversions which
4358    would have happened when passed via ellipsis.  Return the promoted
4359    type, or the passed type if there is no change.  */
4360 
4361 tree
cxx_type_promotes_to(type)4362 cxx_type_promotes_to (type)
4363      tree type;
4364 {
4365   tree promote;
4366 
4367   if (TREE_CODE (type) == ARRAY_TYPE)
4368     return build_pointer_type (TREE_TYPE (type));
4369 
4370   if (TREE_CODE (type) == FUNCTION_TYPE)
4371     return build_pointer_type (type);
4372 
4373   promote = type_promotes_to (type);
4374   if (same_type_p (type, promote))
4375     promote = type;
4376 
4377   return promote;
4378 }
4379 
4380 /* ARG is a default argument expression being passed to a parameter of
4381    the indicated TYPE, which is a parameter to FN.  Do any required
4382    conversions.  Return the converted value.  */
4383 
4384 tree
convert_default_arg(type,arg,fn,parmnum)4385 convert_default_arg (type, arg, fn, parmnum)
4386      tree type;
4387      tree arg;
4388      tree fn;
4389      int parmnum;
4390 {
4391   if (TREE_CODE (arg) == DEFAULT_ARG)
4392     {
4393       /* When processing the default args for a class, we can find that
4394          there is an ordering constraint, and we call a function who's
4395          default args have not yet been converted. For instance,
4396           class A {
4397               A (int = 0);
4398               void Foo (A const & = A ());
4399           };
4400          We must process A::A before A::Foo's default arg can be converted.
4401          Remember the dependent function, so do_pending_defargs can retry,
4402          and check loops.  */
4403       unprocessed_defarg_fn (fn);
4404 
4405       /* Don't return error_mark node, as we won't be able to distinguish
4406          genuine errors from this case, and that would lead to repeated
4407          diagnostics.  Just make something of the right type.  */
4408       return build1 (NOP_EXPR, type, integer_zero_node);
4409     }
4410 
4411   if (fn && DECL_TEMPLATE_INFO (fn))
4412     arg = tsubst_default_argument (fn, type, arg);
4413 
4414   arg = break_out_target_exprs (arg);
4415 
4416   if (TREE_CODE (arg) == CONSTRUCTOR)
4417     {
4418       arg = digest_init (type, arg, 0);
4419       arg = convert_for_initialization (0, type, arg, LOOKUP_NORMAL,
4420 					"default argument", fn, parmnum);
4421     }
4422   else
4423     {
4424       /* This could get clobbered by the following call.  */
4425       if (TREE_HAS_CONSTRUCTOR (arg))
4426 	arg = copy_node (arg);
4427 
4428       arg = convert_for_initialization (0, type, arg, LOOKUP_NORMAL,
4429 					"default argument", fn, parmnum);
4430       arg = convert_for_arg_passing (type, arg);
4431     }
4432 
4433   return arg;
4434 }
4435 
4436 /* Returns the type which will really be used for passing an argument of
4437    type TYPE.  */
4438 
4439 tree
type_passed_as(type)4440 type_passed_as (type)
4441      tree type;
4442 {
4443   /* Pass classes with copy ctors by invisible reference.  */
4444   if (TREE_ADDRESSABLE (type))
4445     type = build_reference_type (type);
4446   else if (PROMOTE_PROTOTYPES
4447 	   && INTEGRAL_TYPE_P (type)
4448 	   && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
4449     type = integer_type_node;
4450 
4451   return type;
4452 }
4453 
4454 /* Actually perform the appropriate conversion.  */
4455 
4456 tree
convert_for_arg_passing(type,val)4457 convert_for_arg_passing (type, val)
4458      tree type, val;
4459 {
4460   if (val == error_mark_node)
4461     ;
4462   /* Pass classes with copy ctors by invisible reference.  */
4463   else if (TREE_ADDRESSABLE (type))
4464     val = build1 (ADDR_EXPR, build_reference_type (type), val);
4465   else if (PROMOTE_PROTOTYPES
4466 	   && INTEGRAL_TYPE_P (type)
4467 	   && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
4468     val = default_conversion (val);
4469   return val;
4470 }
4471 
4472 /* Subroutine of the various build_*_call functions.  Overload resolution
4473    has chosen a winning candidate CAND; build up a CALL_EXPR accordingly.
4474    ARGS is a TREE_LIST of the unconverted arguments to the call.  FLAGS is a
4475    bitmask of various LOOKUP_* flags which apply to the call itself.  */
4476 
4477 static tree
build_over_call(cand,args,flags)4478 build_over_call (cand, args, flags)
4479      struct z_candidate *cand;
4480      tree args;
4481      int flags;
4482 {
4483   tree fn = cand->fn;
4484   tree convs = cand->convs;
4485   tree converted_args = NULL_TREE;
4486   tree parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
4487   tree conv, arg, val;
4488   int i = 0;
4489   int is_method = 0;
4490 
4491   /* Give any warnings we noticed during overload resolution.  */
4492   if (cand->warnings)
4493     for (val = cand->warnings; val; val = TREE_CHAIN (val))
4494       joust (cand, WRAPPER_ZC (TREE_VALUE (val)), 1);
4495 
4496   if (DECL_FUNCTION_MEMBER_P (fn))
4497     enforce_access (cand->access_path, fn);
4498 
4499   if (args && TREE_CODE (args) != TREE_LIST)
4500     args = build_tree_list (NULL_TREE, args);
4501   arg = args;
4502 
4503   /* The implicit parameters to a constructor are not considered by overload
4504      resolution, and must be of the proper type.  */
4505   if (DECL_CONSTRUCTOR_P (fn))
4506     {
4507       converted_args = tree_cons (NULL_TREE, TREE_VALUE (arg), converted_args);
4508       arg = TREE_CHAIN (arg);
4509       parm = TREE_CHAIN (parm);
4510       if (DECL_HAS_IN_CHARGE_PARM_P (fn))
4511 	/* We should never try to call the abstract constructor.  */
4512 	abort ();
4513       if (DECL_HAS_VTT_PARM_P (fn))
4514 	{
4515 	  converted_args = tree_cons
4516 	    (NULL_TREE, TREE_VALUE (arg), converted_args);
4517 	  arg = TREE_CHAIN (arg);
4518 	  parm = TREE_CHAIN (parm);
4519 	}
4520     }
4521   /* Bypass access control for 'this' parameter.  */
4522   else if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
4523     {
4524       tree parmtype = TREE_VALUE (parm);
4525       tree argtype = TREE_TYPE (TREE_VALUE (arg));
4526       tree converted_arg;
4527       tree base_binfo;
4528 
4529       if (ICS_BAD_FLAG (TREE_VEC_ELT (convs, i)))
4530 	pedwarn ("passing `%T' as `this' argument of `%#D' discards qualifiers",
4531 		    TREE_TYPE (argtype), fn);
4532 
4533       /* [class.mfct.nonstatic]: If a nonstatic member function of a class
4534 	 X is called for an object that is not of type X, or of a type
4535 	 derived from X, the behavior is undefined.
4536 
4537          So we can assume that anything passed as 'this' is non-null, and
4538 	 optimize accordingly.  */
4539       my_friendly_assert (TREE_CODE (parmtype) == POINTER_TYPE, 19990811);
4540       /* Convert to the base in which the function was declared.  */
4541       my_friendly_assert (cand->conversion_path != NULL_TREE, 20020730);
4542       converted_arg = build_base_path (PLUS_EXPR,
4543 				       TREE_VALUE (arg),
4544 				       cand->conversion_path,
4545 				       1);
4546       /* Check that the base class is accessible.  */
4547       if (!accessible_base_p (TREE_TYPE (argtype),
4548 			      BINFO_TYPE (cand->conversion_path)))
4549 	error ("`%T' is not an accessible base of `%T'",
4550 	       BINFO_TYPE (cand->conversion_path),
4551 	       TREE_TYPE (argtype));
4552       /* If fn was found by a using declaration, the conversion path
4553          will be to the derived class, not the base declaring fn. We
4554          must convert from derived to base.  */
4555       base_binfo = lookup_base (TREE_TYPE (TREE_TYPE (converted_arg)),
4556 				TREE_TYPE (parmtype), ba_ignore, NULL);
4557       converted_arg = build_base_path (PLUS_EXPR, converted_arg,
4558 				       base_binfo, 1);
4559 
4560       converted_args = tree_cons (NULL_TREE, converted_arg, converted_args);
4561       parm = TREE_CHAIN (parm);
4562       arg = TREE_CHAIN (arg);
4563       ++i;
4564       is_method = 1;
4565     }
4566 
4567   for (; arg && parm;
4568        parm = TREE_CHAIN (parm), arg = TREE_CHAIN (arg), ++i)
4569     {
4570       tree type = TREE_VALUE (parm);
4571 
4572       conv = TREE_VEC_ELT (convs, i);
4573       val = convert_like_with_context
4574 	(conv, TREE_VALUE (arg), fn, i - is_method);
4575 
4576       val = convert_for_arg_passing (type, val);
4577       converted_args = tree_cons (NULL_TREE, val, converted_args);
4578     }
4579 
4580   /* Default arguments */
4581   for (; parm && parm != void_list_node; parm = TREE_CHAIN (parm), i++)
4582     converted_args
4583       = tree_cons (NULL_TREE,
4584 		   convert_default_arg (TREE_VALUE (parm),
4585 					TREE_PURPOSE (parm),
4586 					fn, i - is_method),
4587 		   converted_args);
4588 
4589   /* Ellipsis */
4590   for (; arg; arg = TREE_CHAIN (arg))
4591     converted_args
4592       = tree_cons (NULL_TREE,
4593 		   convert_arg_to_ellipsis (TREE_VALUE (arg)),
4594 		   converted_args);
4595 
4596   converted_args = nreverse (converted_args);
4597 
4598   if (warn_format)
4599     check_function_format (NULL, TYPE_ATTRIBUTES (TREE_TYPE (fn)),
4600 			   converted_args);
4601 
4602   if (warn_bounded)
4603     check_function_bounded (NULL, TYPE_ATTRIBUTES (TREE_TYPE (fn)),
4604 			   converted_args);
4605 
4606   /* Avoid actually calling copy constructors and copy assignment operators,
4607      if possible.  */
4608 
4609   if (! flag_elide_constructors)
4610     /* Do things the hard way.  */;
4611   else if (TREE_VEC_LENGTH (convs) == 1
4612 	   && DECL_COPY_CONSTRUCTOR_P (fn))
4613     {
4614       tree targ;
4615       arg = skip_artificial_parms_for (fn, converted_args);
4616       arg = TREE_VALUE (arg);
4617 
4618       /* Pull out the real argument, disregarding const-correctness.  */
4619       targ = arg;
4620       while (TREE_CODE (targ) == NOP_EXPR
4621 	     || TREE_CODE (targ) == NON_LVALUE_EXPR
4622 	     || TREE_CODE (targ) == CONVERT_EXPR)
4623 	targ = TREE_OPERAND (targ, 0);
4624       if (TREE_CODE (targ) == ADDR_EXPR)
4625 	{
4626 	  targ = TREE_OPERAND (targ, 0);
4627 	  if (!same_type_ignoring_top_level_qualifiers_p
4628 	      (TREE_TYPE (TREE_TYPE (arg)), TREE_TYPE (targ)))
4629 	    targ = NULL_TREE;
4630 	}
4631       else
4632 	targ = NULL_TREE;
4633 
4634       if (targ)
4635 	arg = targ;
4636       else
4637 	arg = build_indirect_ref (arg, 0);
4638 
4639       /* [class.copy]: the copy constructor is implicitly defined even if
4640 	 the implementation elided its use.  */
4641       if (TYPE_HAS_COMPLEX_INIT_REF (DECL_CONTEXT (fn)))
4642 	mark_used (fn);
4643 
4644       /* If we're creating a temp and we already have one, don't create a
4645          new one.  If we're not creating a temp but we get one, use
4646          INIT_EXPR to collapse the temp into our target.  Otherwise, if the
4647          ctor is trivial, do a bitwise copy with a simple TARGET_EXPR for a
4648          temp or an INIT_EXPR otherwise.  */
4649       if (integer_zerop (TREE_VALUE (args)))
4650 	{
4651 	  if (TREE_CODE (arg) == TARGET_EXPR)
4652 	    return arg;
4653 	  else if (TYPE_HAS_TRIVIAL_INIT_REF (DECL_CONTEXT (fn)))
4654 	    return build_target_expr_with_type (arg, DECL_CONTEXT (fn));
4655 	}
4656       else if (TREE_CODE (arg) == TARGET_EXPR
4657 	       || TYPE_HAS_TRIVIAL_INIT_REF (DECL_CONTEXT (fn)))
4658 	{
4659 	  tree address;
4660 	  tree to = stabilize_reference
4661 	    (build_indirect_ref (TREE_VALUE (args), 0));
4662 
4663 	  val = build (INIT_EXPR, DECL_CONTEXT (fn), to, arg);
4664 	  address = build_unary_op (ADDR_EXPR, val, 0);
4665 	  /* Avoid a warning about this expression, if the address is
4666 	     never used.  */
4667 	  TREE_USED (address) = 1;
4668 	  return address;
4669 	}
4670     }
4671   else if (DECL_OVERLOADED_OPERATOR_P (fn) == NOP_EXPR
4672 	   && copy_fn_p (fn)
4673 	   && TYPE_HAS_TRIVIAL_ASSIGN_REF (DECL_CONTEXT (fn)))
4674     {
4675       tree to = stabilize_reference
4676 	(build_indirect_ref (TREE_VALUE (converted_args), 0));
4677 
4678       arg = build_indirect_ref (TREE_VALUE (TREE_CHAIN (converted_args)), 0);
4679       val = build (MODIFY_EXPR, TREE_TYPE (to), to, arg);
4680       return val;
4681     }
4682 
4683   mark_used (fn);
4684 
4685   if (DECL_VINDEX (fn) && (flags & LOOKUP_NONVIRTUAL) == 0)
4686     {
4687       tree t, *p = &TREE_VALUE (converted_args);
4688       tree binfo = lookup_base (TREE_TYPE (TREE_TYPE (*p)),
4689 				DECL_CONTEXT (fn),
4690 				ba_any, NULL);
4691       my_friendly_assert (binfo && binfo != error_mark_node, 20010730);
4692 
4693       *p = build_base_path (PLUS_EXPR, *p, binfo, 1);
4694       if (TREE_SIDE_EFFECTS (*p))
4695 	*p = save_expr (*p);
4696       t = build_pointer_type (TREE_TYPE (fn));
4697       if (DECL_CONTEXT (fn) && TYPE_JAVA_INTERFACE (DECL_CONTEXT (fn)))
4698 	fn = build_java_interface_fn_ref (fn, *p);
4699       else
4700 	fn = build_vfn_ref (build_indirect_ref (*p, 0), DECL_VINDEX (fn));
4701       TREE_TYPE (fn) = t;
4702     }
4703   else if (DECL_INLINE (fn))
4704     fn = inline_conversion (fn);
4705   else
4706     fn = build_addr_func (fn);
4707 
4708   return build_cxx_call (fn, args, converted_args);
4709 }
4710 
4711 /* Build and return a call to FN, using the the CONVERTED_ARGS.  ARGS
4712    gives the original form of the arguments.  This function performs
4713    no overload resolution, conversion, or other high-level
4714    operations.  */
4715 
4716 tree
build_cxx_call(tree fn,tree args,tree converted_args)4717 build_cxx_call(tree fn, tree args, tree converted_args)
4718 {
4719   tree fndecl;
4720 
4721   /* Recognize certain built-in functions so we can make tree-codes
4722      other than CALL_EXPR.  We do this when it enables fold-const.c
4723      to do something useful.  */
4724   if (TREE_CODE (fn) == ADDR_EXPR
4725       && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL
4726       && DECL_BUILT_IN (TREE_OPERAND (fn, 0)))
4727     {
4728       tree exp;
4729       exp = expand_tree_builtin (TREE_OPERAND (fn, 0), args, converted_args);
4730       if (exp)
4731 	return exp;
4732     }
4733 
4734   fn = build_call (fn, converted_args);
4735 
4736   /* If this call might throw an exception, note that fact.  */
4737   fndecl = get_callee_fndecl (fn);
4738   if ((!fndecl || !TREE_NOTHROW (fndecl))
4739       && at_function_scope_p ()
4740       && cfun)
4741     cp_function_chain->can_throw = 1;
4742 
4743   /* Some built-in function calls will be evaluated at compile-time in
4744      fold ().  */
4745   fn = fold (fn);
4746 
4747   if (VOID_TYPE_P (TREE_TYPE (fn)))
4748     return fn;
4749 
4750   fn = require_complete_type (fn);
4751   if (fn == error_mark_node)
4752     return error_mark_node;
4753 
4754   if (IS_AGGR_TYPE (TREE_TYPE (fn)))
4755     fn = build_cplus_new (TREE_TYPE (fn), fn);
4756   return convert_from_reference (fn);
4757 }
4758 
4759 static GTY(()) tree java_iface_lookup_fn;
4760 
4761 /* Make an expression which yields the address of the Java interface
4762    method FN.  This is achieved by generating a call to libjava's
4763    _Jv_LookupInterfaceMethodIdx().  */
4764 
4765 static tree
build_java_interface_fn_ref(fn,instance)4766 build_java_interface_fn_ref (fn, instance)
4767     tree fn, instance;
4768 {
4769   tree lookup_args, lookup_fn, method, idx;
4770   tree klass_ref, iface, iface_ref;
4771   int i;
4772 
4773   if (!java_iface_lookup_fn)
4774     {
4775       tree endlink = build_void_list_node ();
4776       tree t = tree_cons (NULL_TREE, ptr_type_node,
4777 			  tree_cons (NULL_TREE, ptr_type_node,
4778 				     tree_cons (NULL_TREE, java_int_type_node,
4779 						endlink)));
4780       java_iface_lookup_fn
4781 	= builtin_function ("_Jv_LookupInterfaceMethodIdx",
4782 			    build_function_type (ptr_type_node, t),
4783 			    0, NOT_BUILT_IN, NULL, NULL_TREE);
4784     }
4785 
4786   /* Look up the pointer to the runtime java.lang.Class object for `instance'.
4787      This is the first entry in the vtable.  */
4788   klass_ref = build_vtbl_ref (build_indirect_ref (instance, 0),
4789 			      integer_zero_node);
4790 
4791   /* Get the java.lang.Class pointer for the interface being called.  */
4792   iface = DECL_CONTEXT (fn);
4793   iface_ref = lookup_field (iface, get_identifier ("class$"), 0, 0);
4794   if (!iface_ref || TREE_CODE (iface_ref) != VAR_DECL
4795       || DECL_CONTEXT (iface_ref) != iface)
4796     {
4797       error ("could not find class$ field in java interface type `%T'",
4798 		iface);
4799       return error_mark_node;
4800     }
4801   iface_ref = build1 (ADDR_EXPR, build_pointer_type (iface), iface_ref);
4802 
4803   /* Determine the itable index of FN.  */
4804   i = 1;
4805   for (method = TYPE_METHODS (iface); method; method = TREE_CHAIN (method))
4806     {
4807       if (!DECL_VIRTUAL_P (method))
4808         continue;
4809       if (fn == method)
4810         break;
4811       i++;
4812     }
4813   idx = build_int_2 (i, 0);
4814 
4815   lookup_args = tree_cons (NULL_TREE, klass_ref,
4816 			   tree_cons (NULL_TREE, iface_ref,
4817 				      build_tree_list (NULL_TREE, idx)));
4818   lookup_fn = build1 (ADDR_EXPR,
4819 		      build_pointer_type (TREE_TYPE (java_iface_lookup_fn)),
4820 		      java_iface_lookup_fn);
4821   return build (CALL_EXPR, ptr_type_node, lookup_fn, lookup_args, NULL_TREE);
4822 }
4823 
4824 /* Returns the value to use for the in-charge parameter when making a
4825    call to a function with the indicated NAME.  */
4826 
4827 tree
in_charge_arg_for_name(name)4828 in_charge_arg_for_name (name)
4829      tree name;
4830 {
4831   if (name == base_ctor_identifier
4832       || name == base_dtor_identifier)
4833     return integer_zero_node;
4834   else if (name == complete_ctor_identifier)
4835     return integer_one_node;
4836   else if (name == complete_dtor_identifier)
4837     return integer_two_node;
4838   else if (name == deleting_dtor_identifier)
4839     return integer_three_node;
4840 
4841   /* This function should only be called with one of the names listed
4842      above.  */
4843   abort ();
4844   return NULL_TREE;
4845 }
4846 
4847 /* Build a call to a constructor, destructor, or an assignment
4848    operator for INSTANCE, an expression with class type.  NAME
4849    indicates the special member function to call; ARGS are the
4850    arguments.  BINFO indicates the base of INSTANCE that is to be
4851    passed as the `this' parameter to the member function called.
4852 
4853    FLAGS are the LOOKUP_* flags to use when processing the call.
4854 
4855    If NAME indicates a complete object constructor, INSTANCE may be
4856    NULL_TREE.  In this case, the caller will call build_cplus_new to
4857    store the newly constructed object into a VAR_DECL.  */
4858 
4859 tree
build_special_member_call(tree instance,tree name,tree args,tree binfo,int flags)4860 build_special_member_call (tree instance, tree name, tree args,
4861 			   tree binfo, int flags)
4862 {
4863   tree fns;
4864   /* The type of the subobject to be constructed or destroyed.  */
4865   tree class_type;
4866 
4867   my_friendly_assert (name == complete_ctor_identifier
4868 		      || name == base_ctor_identifier
4869 		      || name == complete_dtor_identifier
4870 		      || name == base_dtor_identifier
4871 		      || name == deleting_dtor_identifier
4872 		      || name == ansi_assopname (NOP_EXPR),
4873 		      20020712);
4874   my_friendly_assert (binfo != NULL_TREE, 20020712);
4875 
4876   class_type = BINFO_TYPE (binfo);
4877 
4878   /* Handle the special case where INSTANCE is NULL_TREE.  */
4879   if (name == complete_ctor_identifier && !instance)
4880     {
4881       instance = build_int_2 (0, 0);
4882       TREE_TYPE (instance) = build_pointer_type (class_type);
4883       instance = build1 (INDIRECT_REF, class_type, instance);
4884     }
4885   else
4886     {
4887       if (name == complete_dtor_identifier
4888 	  || name == base_dtor_identifier
4889 	  || name == deleting_dtor_identifier)
4890 	my_friendly_assert (args == NULL_TREE, 20020712);
4891 
4892       /* Convert to the base class, if necessary.  */
4893       if (!same_type_ignoring_top_level_qualifiers_p
4894 	  (TREE_TYPE (instance), BINFO_TYPE (binfo)))
4895 	{
4896 	  if (name != ansi_assopname (NOP_EXPR))
4897 	    /* For constructors and destructors, either the base is
4898 	       non-virtual, or it is virtual but we are doing the
4899 	       conversion from a constructor or destructor for the
4900 	       complete object.  In either case, we can convert
4901 	       statically.  */
4902 	    instance = convert_to_base_statically (instance, binfo);
4903 	  else
4904 	    /* However, for assignment operators, we must convert
4905 	       dynamically if the base is virtual.  */
4906 	    instance = build_base_path (PLUS_EXPR, instance,
4907 					binfo, /*nonnull=*/1);
4908 	}
4909     }
4910 
4911   my_friendly_assert (instance != NULL_TREE, 20020712);
4912 
4913   /* Resolve the name.  */
4914   if (!complete_type_or_else (BINFO_TYPE (binfo), NULL_TREE))
4915     return error_mark_node;
4916 
4917   fns = lookup_fnfields (binfo, name, 1);
4918 
4919   /* When making a call to a constructor or destructor for a subobject
4920      that uses virtual base classes, pass down a pointer to a VTT for
4921      the subobject.  */
4922   if ((name == base_ctor_identifier
4923        || name == base_dtor_identifier)
4924       && TYPE_USES_VIRTUAL_BASECLASSES (class_type))
4925     {
4926       tree vtt;
4927       tree sub_vtt;
4928 
4929       /* If the current function is a complete object constructor
4930 	 or destructor, then we fetch the VTT directly.
4931 	 Otherwise, we look it up using the VTT we were given.  */
4932       vtt = TREE_CHAIN (CLASSTYPE_VTABLES (current_class_type));
4933       vtt = decay_conversion (vtt);
4934       vtt = build (COND_EXPR, TREE_TYPE (vtt),
4935 		   build (EQ_EXPR, boolean_type_node,
4936 			  current_in_charge_parm, integer_zero_node),
4937 		   current_vtt_parm,
4938 		   vtt);
4939       if (TREE_VIA_VIRTUAL (binfo))
4940 	binfo = binfo_for_vbase (class_type, current_class_type);
4941       my_friendly_assert (BINFO_SUBVTT_INDEX (binfo), 20010110);
4942       sub_vtt = build (PLUS_EXPR, TREE_TYPE (vtt), vtt,
4943 		       BINFO_SUBVTT_INDEX (binfo));
4944 
4945       args = tree_cons (NULL_TREE, sub_vtt, args);
4946     }
4947 
4948   return build_new_method_call (instance, fns, args,
4949 				TYPE_BINFO (BINFO_TYPE (binfo)),
4950 				flags);
4951 }
4952 
4953 /* Build a call to "INSTANCE.FN (ARGS)".  */
4954 
4955 tree
build_new_method_call(tree instance,tree fns,tree args,tree conversion_path,int flags)4956 build_new_method_call (tree instance, tree fns, tree args,
4957 		       tree conversion_path, int flags)
4958 {
4959   struct z_candidate *candidates = 0, *cand;
4960   tree explicit_targs = NULL_TREE;
4961   tree basetype = NULL_TREE;
4962   tree access_binfo;
4963   tree optype;
4964   tree mem_args = NULL_TREE, instance_ptr;
4965   tree name, pretty_name;
4966   tree user_args;
4967   tree call;
4968   int template_only = 0;
4969 
4970   my_friendly_assert (instance != NULL_TREE, 20020729);
4971 
4972   if (instance == error_mark_node || fns == error_mark_node
4973       || args == error_mark_node)
4974     return error_mark_node;
4975 
4976   /* Process the argument list.  */
4977   user_args = args;
4978   args = resolve_args (args);
4979   if (args == error_mark_node)
4980     return error_mark_node;
4981 
4982   if (TREE_CODE (instance) == OFFSET_REF)
4983     instance = resolve_offset_ref (instance);
4984   if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
4985     instance = convert_from_reference (instance);
4986   basetype = TYPE_MAIN_VARIANT (TREE_TYPE (instance));
4987   instance_ptr = build_this (instance);
4988 
4989   if (!BASELINK_P (fns))
4990     {
4991       call = build_field_call (instance_ptr, fns, args);
4992       if (call)
4993 	return call;
4994       error ("call to non-function `%D'", fns);
4995       return error_mark_node;
4996     }
4997 
4998   if (!conversion_path)
4999     conversion_path = BASELINK_BINFO (fns);
5000   access_binfo = BASELINK_ACCESS_BINFO (fns);
5001   optype = BASELINK_OPTYPE (fns);
5002   fns = BASELINK_FUNCTIONS (fns);
5003 
5004   if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
5005     {
5006       explicit_targs = TREE_OPERAND (fns, 1);
5007       fns = TREE_OPERAND (fns, 0);
5008       template_only = 1;
5009     }
5010 
5011   my_friendly_assert (TREE_CODE (fns) == FUNCTION_DECL
5012 		      || TREE_CODE (fns) == TEMPLATE_DECL
5013 		      || TREE_CODE (fns) == OVERLOAD,
5014 		      20020712);
5015 
5016   /* XXX this should be handled before we get here.  */
5017   if (! IS_AGGR_TYPE (basetype))
5018     {
5019       if ((flags & LOOKUP_COMPLAIN) && basetype != error_mark_node)
5020 	error ("request for member `%D' in `%E', which is of non-aggregate type `%T'",
5021 	       fns, instance, basetype);
5022 
5023       return error_mark_node;
5024     }
5025 
5026   name = DECL_NAME (get_first_fn (fns));
5027 
5028   if (IDENTIFIER_CTOR_OR_DTOR_P (name))
5029     {
5030       /* Callers should explicitly indicate whether they want to construct
5031 	 the complete object or just the part without virtual bases.  */
5032       my_friendly_assert (name != ctor_identifier, 20000408);
5033       /* Similarly for destructors.  */
5034       my_friendly_assert (name != dtor_identifier, 20000408);
5035 
5036       if (name == complete_ctor_identifier
5037 	  || name == base_ctor_identifier)
5038 	pretty_name = constructor_name (basetype);
5039       else
5040 	pretty_name = dtor_identifier;
5041     }
5042   else
5043     pretty_name = name;
5044 
5045   if (fns)
5046     {
5047       tree fn;
5048       tree class_type = (conversion_path
5049 			 ? BINFO_TYPE (conversion_path)
5050 			 : NULL_TREE);
5051 
5052       mem_args = tree_cons (NULL_TREE, instance_ptr, args);
5053       for (fn = fns; fn; fn = OVL_NEXT (fn))
5054 	{
5055 	  tree t = OVL_CURRENT (fn);
5056 	  tree this_arglist;
5057 
5058 	  /* We can end up here for copy-init of same or base class.  */
5059 	  if ((flags & LOOKUP_ONLYCONVERTING)
5060 	      && DECL_NONCONVERTING_P (t))
5061 	    continue;
5062 
5063 	  if (DECL_NONSTATIC_MEMBER_FUNCTION_P (t))
5064 	    this_arglist = mem_args;
5065 	  else
5066 	    this_arglist = args;
5067 
5068 	  if (TREE_CODE (t) == TEMPLATE_DECL)
5069 	    /* A member template.  */
5070 	    add_template_candidate (&candidates, t,
5071 				    class_type,
5072 				    explicit_targs,
5073 				    this_arglist, optype,
5074 				    access_binfo,
5075 				    conversion_path,
5076 				    flags,
5077 				    DEDUCE_CALL);
5078 	  else if (! template_only)
5079 	    add_function_candidate (&candidates, t,
5080 				    class_type,
5081 				    this_arglist,
5082 				    access_binfo,
5083 				    conversion_path,
5084 				    flags);
5085 	}
5086     }
5087 
5088   if (! any_viable (candidates))
5089     {
5090       /* XXX will LOOKUP_SPECULATIVELY be needed when this is done?  */
5091       if (flags & LOOKUP_SPECULATIVELY)
5092 	return NULL_TREE;
5093       if (!COMPLETE_TYPE_P (basetype))
5094 	cxx_incomplete_type_error (instance_ptr, basetype);
5095       else
5096 	error ("no matching function for call to `%T::%D(%A)%#V'",
5097 	       basetype, pretty_name, user_args,
5098 	       TREE_TYPE (TREE_TYPE (instance_ptr)));
5099       print_z_candidates (candidates);
5100       return error_mark_node;
5101     }
5102   candidates = splice_viable (candidates);
5103   cand = tourney (candidates);
5104 
5105   if (cand == 0)
5106     {
5107       error ("call of overloaded `%D(%A)' is ambiguous", pretty_name,
5108 		user_args);
5109       print_z_candidates (candidates);
5110       return error_mark_node;
5111     }
5112 
5113   if (DECL_PURE_VIRTUAL_P (cand->fn)
5114       && instance == current_class_ref
5115       && (DECL_CONSTRUCTOR_P (current_function_decl)
5116 	  || DECL_DESTRUCTOR_P (current_function_decl))
5117       && ! (flags & LOOKUP_NONVIRTUAL)
5118       && value_member (cand->fn, CLASSTYPE_PURE_VIRTUALS (basetype)))
5119     error ((DECL_CONSTRUCTOR_P (current_function_decl) ?
5120 	       "abstract virtual `%#D' called from constructor"
5121 	       : "abstract virtual `%#D' called from destructor"),
5122 	      cand->fn);
5123   if (TREE_CODE (TREE_TYPE (cand->fn)) == METHOD_TYPE
5124       && is_dummy_object (instance_ptr))
5125     {
5126       error ("cannot call member function `%D' without object", cand->fn);
5127       return error_mark_node;
5128     }
5129 
5130   if (DECL_VINDEX (cand->fn) && ! (flags & LOOKUP_NONVIRTUAL)
5131       && resolves_to_fixed_type_p (instance, 0))
5132     flags |= LOOKUP_NONVIRTUAL;
5133 
5134   if (TREE_CODE (TREE_TYPE (cand->fn)) == METHOD_TYPE)
5135     call = build_over_call (cand, mem_args, flags);
5136   else
5137     {
5138       call = build_over_call (cand, args, flags);
5139       /* In an expression of the form `a->f()' where `f' turns out to
5140 	 be a static member function, `a' is none-the-less evaluated.  */
5141       if (instance && TREE_SIDE_EFFECTS (instance))
5142 	call = build (COMPOUND_EXPR, TREE_TYPE (call), instance, call);
5143     }
5144 
5145   return call;
5146 }
5147 
5148 /* Returns nonzero iff standard conversion sequence ICS1 is a proper
5149    subsequence of ICS2.  */
5150 
5151 static int
is_subseq(ics1,ics2)5152 is_subseq (ics1, ics2)
5153      tree ics1, ics2;
5154 {
5155   /* We can assume that a conversion of the same code
5156      between the same types indicates a subsequence since we only get
5157      here if the types we are converting from are the same.  */
5158 
5159   while (TREE_CODE (ics1) == RVALUE_CONV
5160 	 || TREE_CODE (ics1) == LVALUE_CONV)
5161     ics1 = TREE_OPERAND (ics1, 0);
5162 
5163   while (1)
5164     {
5165       while (TREE_CODE (ics2) == RVALUE_CONV
5166 	  || TREE_CODE (ics2) == LVALUE_CONV)
5167 	ics2 = TREE_OPERAND (ics2, 0);
5168 
5169       if (TREE_CODE (ics2) == USER_CONV
5170 	  || TREE_CODE (ics2) == AMBIG_CONV
5171 	  || TREE_CODE (ics2) == IDENTITY_CONV)
5172 	/* At this point, ICS1 cannot be a proper subsequence of
5173 	   ICS2.  We can get a USER_CONV when we are comparing the
5174 	   second standard conversion sequence of two user conversion
5175 	   sequences.  */
5176 	return 0;
5177 
5178       ics2 = TREE_OPERAND (ics2, 0);
5179 
5180       if (TREE_CODE (ics2) == TREE_CODE (ics1)
5181 	  && same_type_p (TREE_TYPE (ics2), TREE_TYPE (ics1))
5182 	  && same_type_p (TREE_TYPE (TREE_OPERAND (ics2, 0)),
5183 			     TREE_TYPE (TREE_OPERAND (ics1, 0))))
5184 	return 1;
5185     }
5186 }
5187 
5188 /* Returns nonzero iff DERIVED is derived from BASE.  The inputs may
5189    be any _TYPE nodes.  */
5190 
5191 int
is_properly_derived_from(derived,base)5192 is_properly_derived_from (derived, base)
5193      tree derived;
5194      tree base;
5195 {
5196   if (!IS_AGGR_TYPE_CODE (TREE_CODE (derived))
5197       || !IS_AGGR_TYPE_CODE (TREE_CODE (base)))
5198     return 0;
5199 
5200   /* We only allow proper derivation here.  The DERIVED_FROM_P macro
5201      considers every class derived from itself.  */
5202   return (!same_type_ignoring_top_level_qualifiers_p (derived, base)
5203 	  && DERIVED_FROM_P (base, derived));
5204 }
5205 
5206 /* We build the ICS for an implicit object parameter as a pointer
5207    conversion sequence.  However, such a sequence should be compared
5208    as if it were a reference conversion sequence.  If ICS is the
5209    implicit conversion sequence for an implicit object parameter,
5210    modify it accordingly.  */
5211 
5212 static void
maybe_handle_implicit_object(ics)5213 maybe_handle_implicit_object (ics)
5214      tree* ics;
5215 {
5216   if (ICS_THIS_FLAG (*ics))
5217     {
5218       /* [over.match.funcs]
5219 
5220 	 For non-static member functions, the type of the
5221 	 implicit object parameter is "reference to cv X"
5222 	 where X is the class of which the function is a
5223 	 member and cv is the cv-qualification on the member
5224 	 function declaration.  */
5225       tree t = *ics;
5226       tree reference_type;
5227 
5228       /* The `this' parameter is a pointer to a class type.  Make the
5229 	 implict conversion talk about a reference to that same class
5230 	 type.  */
5231       reference_type = TREE_TYPE (TREE_TYPE (*ics));
5232       reference_type = build_reference_type (reference_type);
5233 
5234       if (TREE_CODE (t) == QUAL_CONV)
5235 	t = TREE_OPERAND (t, 0);
5236       if (TREE_CODE (t) == PTR_CONV)
5237 	t = TREE_OPERAND (t, 0);
5238       t = build1 (IDENTITY_CONV, TREE_TYPE (TREE_TYPE (t)), NULL_TREE);
5239       t = direct_reference_binding (reference_type, t);
5240       *ics = t;
5241     }
5242 }
5243 
5244 /* If *ICS is a REF_BIND set *ICS to the remainder of the conversion,
5245    and return the type to which the reference refers.  Otherwise,
5246    leave *ICS unchanged and return NULL_TREE.  */
5247 
5248 static tree
maybe_handle_ref_bind(ics)5249 maybe_handle_ref_bind (ics)
5250      tree* ics;
5251 {
5252   if (TREE_CODE (*ics) == REF_BIND)
5253     {
5254       tree old_ics = *ics;
5255       tree type = TREE_TYPE (TREE_TYPE (old_ics));
5256       *ics = TREE_OPERAND (old_ics, 0);
5257       ICS_USER_FLAG (*ics) = ICS_USER_FLAG (old_ics);
5258       ICS_BAD_FLAG (*ics) = ICS_BAD_FLAG (old_ics);
5259       return type;
5260     }
5261 
5262   return NULL_TREE;
5263 }
5264 
5265 /* Compare two implicit conversion sequences according to the rules set out in
5266    [over.ics.rank].  Return values:
5267 
5268       1: ics1 is better than ics2
5269      -1: ics2 is better than ics1
5270       0: ics1 and ics2 are indistinguishable */
5271 
5272 static int
compare_ics(ics1,ics2)5273 compare_ics (ics1, ics2)
5274      tree ics1, ics2;
5275 {
5276   tree from_type1;
5277   tree from_type2;
5278   tree to_type1;
5279   tree to_type2;
5280   tree deref_from_type1 = NULL_TREE;
5281   tree deref_from_type2 = NULL_TREE;
5282   tree deref_to_type1 = NULL_TREE;
5283   tree deref_to_type2 = NULL_TREE;
5284   int rank1, rank2;
5285 
5286   /* REF_BINDING is nonzero if the result of the conversion sequence
5287      is a reference type.   In that case TARGET_TYPE is the
5288      type referred to by the reference.  */
5289   tree target_type1;
5290   tree target_type2;
5291 
5292   /* Handle implicit object parameters.  */
5293   maybe_handle_implicit_object (&ics1);
5294   maybe_handle_implicit_object (&ics2);
5295 
5296   /* Handle reference parameters.  */
5297   target_type1 = maybe_handle_ref_bind (&ics1);
5298   target_type2 = maybe_handle_ref_bind (&ics2);
5299 
5300   /* [over.ics.rank]
5301 
5302      When  comparing  the  basic forms of implicit conversion sequences (as
5303      defined in _over.best.ics_)
5304 
5305      --a standard conversion sequence (_over.ics.scs_) is a better
5306        conversion sequence than a user-defined conversion sequence
5307        or an ellipsis conversion sequence, and
5308 
5309      --a user-defined conversion sequence (_over.ics.user_) is a
5310        better conversion sequence than an ellipsis conversion sequence
5311        (_over.ics.ellipsis_).  */
5312   rank1 = ICS_RANK (ics1);
5313   rank2 = ICS_RANK (ics2);
5314 
5315   if (rank1 > rank2)
5316     return -1;
5317   else if (rank1 < rank2)
5318     return 1;
5319 
5320   if (rank1 == BAD_RANK)
5321     {
5322       /* XXX Isn't this an extension? */
5323       /* Both ICS are bad.  We try to make a decision based on what
5324 	 would have happenned if they'd been good.  */
5325       if (ICS_USER_FLAG (ics1) > ICS_USER_FLAG (ics2)
5326 	  || ICS_STD_RANK (ics1) > ICS_STD_RANK (ics2))
5327 	return -1;
5328       else if (ICS_USER_FLAG (ics1) < ICS_USER_FLAG (ics2)
5329 	       || ICS_STD_RANK (ics1) < ICS_STD_RANK (ics2))
5330 	return 1;
5331 
5332       /* We couldn't make up our minds; try to figure it out below.  */
5333     }
5334 
5335   if (ICS_ELLIPSIS_FLAG (ics1))
5336     /* Both conversions are ellipsis conversions.  */
5337     return 0;
5338 
5339   /* User-defined  conversion sequence U1 is a better conversion sequence
5340      than another user-defined conversion sequence U2 if they contain the
5341      same user-defined conversion operator or constructor and if the sec-
5342      ond standard conversion sequence of U1 is  better  than  the  second
5343      standard conversion sequence of U2.  */
5344 
5345   if (ICS_USER_FLAG (ics1))
5346     {
5347       tree t1, t2;
5348 
5349       for (t1 = ics1; TREE_CODE (t1) != USER_CONV; t1 = TREE_OPERAND (t1, 0))
5350 	if (TREE_CODE (t1) == AMBIG_CONV)
5351 	  return 0;
5352       for (t2 = ics2; TREE_CODE (t2) != USER_CONV; t2 = TREE_OPERAND (t2, 0))
5353 	if (TREE_CODE (t2) == AMBIG_CONV)
5354 	  return 0;
5355 
5356       if (USER_CONV_FN (t1) != USER_CONV_FN (t2))
5357 	return 0;
5358 
5359       /* We can just fall through here, after setting up
5360 	 FROM_TYPE1 and FROM_TYPE2.  */
5361       from_type1 = TREE_TYPE (t1);
5362       from_type2 = TREE_TYPE (t2);
5363     }
5364   else
5365     {
5366       /* We're dealing with two standard conversion sequences.
5367 
5368 	 [over.ics.rank]
5369 
5370 	 Standard conversion sequence S1 is a better conversion
5371 	 sequence than standard conversion sequence S2 if
5372 
5373 	 --S1 is a proper subsequence of S2 (comparing the conversion
5374 	   sequences in the canonical form defined by _over.ics.scs_,
5375 	   excluding any Lvalue Transformation; the identity
5376 	   conversion sequence is considered to be a subsequence of
5377 	   any non-identity conversion sequence */
5378 
5379       from_type1 = ics1;
5380       while (TREE_CODE (from_type1) != IDENTITY_CONV)
5381 	from_type1 = TREE_OPERAND (from_type1, 0);
5382       from_type1 = TREE_TYPE (from_type1);
5383 
5384       from_type2 = ics2;
5385       while (TREE_CODE (from_type2) != IDENTITY_CONV)
5386 	from_type2 = TREE_OPERAND (from_type2, 0);
5387       from_type2 = TREE_TYPE (from_type2);
5388     }
5389 
5390   if (same_type_p (from_type1, from_type2))
5391     {
5392       if (is_subseq (ics1, ics2))
5393 	return 1;
5394       if (is_subseq (ics2, ics1))
5395 	return -1;
5396     }
5397   /* Otherwise, one sequence cannot be a subsequence of the other; they
5398      don't start with the same type.  This can happen when comparing the
5399      second standard conversion sequence in two user-defined conversion
5400      sequences.  */
5401 
5402   /* [over.ics.rank]
5403 
5404      Or, if not that,
5405 
5406      --the rank of S1 is better than the rank of S2 (by the rules
5407        defined below):
5408 
5409     Standard conversion sequences are ordered by their ranks: an Exact
5410     Match is a better conversion than a Promotion, which is a better
5411     conversion than a Conversion.
5412 
5413     Two conversion sequences with the same rank are indistinguishable
5414     unless one of the following rules applies:
5415 
5416     --A conversion that is not a conversion of a pointer, or pointer
5417       to member, to bool is better than another conversion that is such
5418       a conversion.
5419 
5420     The ICS_STD_RANK automatically handles the pointer-to-bool rule,
5421     so that we do not have to check it explicitly.  */
5422   if (ICS_STD_RANK (ics1) < ICS_STD_RANK (ics2))
5423     return 1;
5424   else if (ICS_STD_RANK (ics2) < ICS_STD_RANK (ics1))
5425     return -1;
5426 
5427   to_type1 = TREE_TYPE (ics1);
5428   to_type2 = TREE_TYPE (ics2);
5429 
5430   if (TYPE_PTR_P (from_type1)
5431       && TYPE_PTR_P (from_type2)
5432       && TYPE_PTR_P (to_type1)
5433       && TYPE_PTR_P (to_type2))
5434     {
5435       deref_from_type1 = TREE_TYPE (from_type1);
5436       deref_from_type2 = TREE_TYPE (from_type2);
5437       deref_to_type1 = TREE_TYPE (to_type1);
5438       deref_to_type2 = TREE_TYPE (to_type2);
5439     }
5440   /* The rules for pointers to members A::* are just like the rules
5441      for pointers A*, except opposite: if B is derived from A then
5442      A::* converts to B::*, not vice versa.  For that reason, we
5443      switch the from_ and to_ variables here.  */
5444   else if (TYPE_PTRMEM_P (from_type1)
5445 	   && TYPE_PTRMEM_P (from_type2)
5446 	   && TYPE_PTRMEM_P (to_type1)
5447 	   && TYPE_PTRMEM_P (to_type2))
5448     {
5449       deref_to_type1 = TYPE_OFFSET_BASETYPE (TREE_TYPE (from_type1));
5450       deref_to_type2 = TYPE_OFFSET_BASETYPE (TREE_TYPE (from_type2));
5451       deref_from_type1 = TYPE_OFFSET_BASETYPE (TREE_TYPE (to_type1));
5452       deref_from_type2 = TYPE_OFFSET_BASETYPE (TREE_TYPE (to_type2));
5453     }
5454   else if (TYPE_PTRMEMFUNC_P (from_type1)
5455 	   && TYPE_PTRMEMFUNC_P (from_type2)
5456 	   && TYPE_PTRMEMFUNC_P (to_type1)
5457 	   && TYPE_PTRMEMFUNC_P (to_type2))
5458     {
5459       deref_to_type1 = TYPE_PTRMEMFUNC_OBJECT_TYPE (from_type1);
5460       deref_to_type2 = TYPE_PTRMEMFUNC_OBJECT_TYPE (from_type2);
5461       deref_from_type1 = TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type1);
5462       deref_from_type2 = TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type2);
5463     }
5464 
5465   if (deref_from_type1 != NULL_TREE
5466       && IS_AGGR_TYPE_CODE (TREE_CODE (deref_from_type1))
5467       && IS_AGGR_TYPE_CODE (TREE_CODE (deref_from_type2)))
5468     {
5469       /* This was one of the pointer or pointer-like conversions.
5470 
5471 	 [over.ics.rank]
5472 
5473 	 --If class B is derived directly or indirectly from class A,
5474 	   conversion of B* to A* is better than conversion of B* to
5475 	   void*, and conversion of A* to void* is better than
5476 	   conversion of B* to void*.  */
5477       if (TREE_CODE (deref_to_type1) == VOID_TYPE
5478 	  && TREE_CODE (deref_to_type2) == VOID_TYPE)
5479 	{
5480 	  if (is_properly_derived_from (deref_from_type1,
5481 					deref_from_type2))
5482 	    return -1;
5483 	  else if (is_properly_derived_from (deref_from_type2,
5484 					     deref_from_type1))
5485 	    return 1;
5486 	}
5487       else if (TREE_CODE (deref_to_type1) == VOID_TYPE
5488 	       || TREE_CODE (deref_to_type2) == VOID_TYPE)
5489 	{
5490 	  if (same_type_p (deref_from_type1, deref_from_type2))
5491 	    {
5492 	      if (TREE_CODE (deref_to_type2) == VOID_TYPE)
5493 		{
5494 		  if (is_properly_derived_from (deref_from_type1,
5495 						deref_to_type1))
5496 		    return 1;
5497 		}
5498 	      /* We know that DEREF_TO_TYPE1 is `void' here.  */
5499 	      else if (is_properly_derived_from (deref_from_type1,
5500 						 deref_to_type2))
5501 		return -1;
5502 	    }
5503 	}
5504       else if (IS_AGGR_TYPE_CODE (TREE_CODE (deref_to_type1))
5505 	       && IS_AGGR_TYPE_CODE (TREE_CODE (deref_to_type2)))
5506 	{
5507 	  /* [over.ics.rank]
5508 
5509 	     --If class B is derived directly or indirectly from class A
5510 	       and class C is derived directly or indirectly from B,
5511 
5512 	     --conversion of C* to B* is better than conversion of C* to
5513 	       A*,
5514 
5515 	     --conversion of B* to A* is better than conversion of C* to
5516 	       A*  */
5517 	  if (same_type_p (deref_from_type1, deref_from_type2))
5518 	    {
5519 	      if (is_properly_derived_from (deref_to_type1,
5520 					    deref_to_type2))
5521 		return 1;
5522 	      else if (is_properly_derived_from (deref_to_type2,
5523 						 deref_to_type1))
5524 		return -1;
5525 	    }
5526 	  else if (same_type_p (deref_to_type1, deref_to_type2))
5527 	    {
5528 	      if (is_properly_derived_from (deref_from_type2,
5529 					    deref_from_type1))
5530 		return 1;
5531 	      else if (is_properly_derived_from (deref_from_type1,
5532 						 deref_from_type2))
5533 		return -1;
5534 	    }
5535 	}
5536     }
5537   else if (CLASS_TYPE_P (non_reference (from_type1))
5538 	   && same_type_p (from_type1, from_type2))
5539     {
5540       tree from = non_reference (from_type1);
5541 
5542       /* [over.ics.rank]
5543 
5544 	 --binding of an expression of type C to a reference of type
5545 	   B& is better than binding an expression of type C to a
5546 	   reference of type A&
5547 
5548 	 --conversion of C to B is better than conversion of C to A,  */
5549       if (is_properly_derived_from (from, to_type1)
5550 	  && is_properly_derived_from (from, to_type2))
5551 	{
5552 	  if (is_properly_derived_from (to_type1, to_type2))
5553 	    return 1;
5554 	  else if (is_properly_derived_from (to_type2, to_type1))
5555 	    return -1;
5556 	}
5557     }
5558   else if (CLASS_TYPE_P (non_reference (to_type1))
5559 	   && same_type_p (to_type1, to_type2))
5560     {
5561       tree to = non_reference (to_type1);
5562 
5563       /* [over.ics.rank]
5564 
5565 	 --binding of an expression of type B to a reference of type
5566 	   A& is better than binding an expression of type C to a
5567 	   reference of type A&,
5568 
5569 	 --onversion of B to A is better than conversion of C to A  */
5570       if (is_properly_derived_from (from_type1, to)
5571 	  && is_properly_derived_from (from_type2, to))
5572 	{
5573 	  if (is_properly_derived_from (from_type2, from_type1))
5574 	    return 1;
5575 	  else if (is_properly_derived_from (from_type1, from_type2))
5576 	    return -1;
5577 	}
5578     }
5579 
5580   /* [over.ics.rank]
5581 
5582      --S1 and S2 differ only in their qualification conversion and  yield
5583        similar  types  T1 and T2 (_conv.qual_), respectively, and the cv-
5584        qualification signature of type T1 is a proper subset of  the  cv-
5585        qualification signature of type T2  */
5586   if (TREE_CODE (ics1) == QUAL_CONV
5587       && TREE_CODE (ics2) == QUAL_CONV
5588       && same_type_p (from_type1, from_type2))
5589     return comp_cv_qual_signature (to_type1, to_type2);
5590 
5591   /* [over.ics.rank]
5592 
5593      --S1 and S2 are reference bindings (_dcl.init.ref_), and the
5594      types to which the references refer are the same type except for
5595      top-level cv-qualifiers, and the type to which the reference
5596      initialized by S2 refers is more cv-qualified than the type to
5597      which the reference initialized by S1 refers */
5598 
5599   if (target_type1 && target_type2
5600       && same_type_ignoring_top_level_qualifiers_p (to_type1, to_type2))
5601     return comp_cv_qualification (target_type2, target_type1);
5602 
5603   /* Neither conversion sequence is better than the other.  */
5604   return 0;
5605 }
5606 
5607 /* The source type for this standard conversion sequence.  */
5608 
5609 static tree
source_type(t)5610 source_type (t)
5611      tree t;
5612 {
5613   for (;; t = TREE_OPERAND (t, 0))
5614     {
5615       if (TREE_CODE (t) == USER_CONV
5616 	  || TREE_CODE (t) == AMBIG_CONV
5617 	  || TREE_CODE (t) == IDENTITY_CONV)
5618 	return TREE_TYPE (t);
5619     }
5620   abort ();
5621 }
5622 
5623 /* Note a warning about preferring WINNER to LOSER.  We do this by storing
5624    a pointer to LOSER and re-running joust to produce the warning if WINNER
5625    is actually used.  */
5626 
5627 static void
add_warning(winner,loser)5628 add_warning (winner, loser)
5629      struct z_candidate *winner, *loser;
5630 {
5631   winner->warnings = tree_cons (NULL_TREE,
5632 				build_zc_wrapper (loser),
5633 				winner->warnings);
5634 }
5635 
5636 /* Compare two candidates for overloading as described in
5637    [over.match.best].  Return values:
5638 
5639       1: cand1 is better than cand2
5640      -1: cand2 is better than cand1
5641       0: cand1 and cand2 are indistinguishable */
5642 
5643 static int
joust(cand1,cand2,warn)5644 joust (cand1, cand2, warn)
5645      struct z_candidate *cand1, *cand2;
5646      int warn;
5647 {
5648   int winner = 0;
5649   int i, off1 = 0, off2 = 0, len;
5650 
5651   /* Candidates that involve bad conversions are always worse than those
5652      that don't.  */
5653   if (cand1->viable > cand2->viable)
5654     return 1;
5655   if (cand1->viable < cand2->viable)
5656     return -1;
5657 
5658   /* If we have two pseudo-candidates for conversions to the same type,
5659      or two candidates for the same function, arbitrarily pick one.  */
5660   if (cand1->fn == cand2->fn
5661       && (TYPE_P (cand1->fn) || DECL_P (cand1->fn)))
5662     return 1;
5663 
5664   /* a viable function F1
5665      is defined to be a better function than another viable function F2  if
5666      for  all arguments i, ICSi(F1) is not a worse conversion sequence than
5667      ICSi(F2), and then */
5668 
5669   /* for some argument j, ICSj(F1) is a better conversion  sequence  than
5670      ICSj(F2) */
5671 
5672   /* For comparing static and non-static member functions, we ignore
5673      the implicit object parameter of the non-static function.  The
5674      standard says to pretend that the static function has an object
5675      parm, but that won't work with operator overloading.  */
5676   len = TREE_VEC_LENGTH (cand1->convs);
5677   if (len != TREE_VEC_LENGTH (cand2->convs))
5678     {
5679       if (DECL_STATIC_FUNCTION_P (cand1->fn)
5680 	  && ! DECL_STATIC_FUNCTION_P (cand2->fn))
5681 	off2 = 1;
5682       else if (! DECL_STATIC_FUNCTION_P (cand1->fn)
5683 	       && DECL_STATIC_FUNCTION_P (cand2->fn))
5684 	{
5685 	  off1 = 1;
5686 	  --len;
5687 	}
5688       else
5689 	abort ();
5690     }
5691 
5692   for (i = 0; i < len; ++i)
5693     {
5694       tree t1 = TREE_VEC_ELT (cand1->convs, i+off1);
5695       tree t2 = TREE_VEC_ELT (cand2->convs, i+off2);
5696       int comp = compare_ics (t1, t2);
5697 
5698       if (comp != 0)
5699 	{
5700 	  if (warn_sign_promo
5701 	      && ICS_RANK (t1) + ICS_RANK (t2) == STD_RANK + PROMO_RANK
5702 	      && TREE_CODE (t1) == STD_CONV
5703 	      && TREE_CODE (t2) == STD_CONV
5704 	      && TREE_CODE (TREE_TYPE (t1)) == INTEGER_TYPE
5705 	      && TREE_CODE (TREE_TYPE (t2)) == INTEGER_TYPE
5706 	      && (TYPE_PRECISION (TREE_TYPE (t1))
5707 		  == TYPE_PRECISION (TREE_TYPE (t2)))
5708 	      && (TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (t1, 0)))
5709 		  || (TREE_CODE (TREE_TYPE (TREE_OPERAND (t1, 0)))
5710 		      == ENUMERAL_TYPE)))
5711 	    {
5712 	      tree type = TREE_TYPE (TREE_OPERAND (t1, 0));
5713 	      tree type1, type2;
5714 	      struct z_candidate *w, *l;
5715 	      if (comp > 0)
5716 		type1 = TREE_TYPE (t1), type2 = TREE_TYPE (t2),
5717 		  w = cand1, l = cand2;
5718 	      else
5719 		type1 = TREE_TYPE (t2), type2 = TREE_TYPE (t1),
5720 		  w = cand2, l = cand1;
5721 
5722 	      if (warn)
5723 		{
5724 		  warning ("passing `%T' chooses `%T' over `%T'",
5725 			      type, type1, type2);
5726 		  warning ("  in call to `%D'", w->fn);
5727 		}
5728 	      else
5729 		add_warning (w, l);
5730 	    }
5731 
5732 	  if (winner && comp != winner)
5733 	    {
5734 	      winner = 0;
5735 	      goto tweak;
5736 	    }
5737 	  winner = comp;
5738 	}
5739     }
5740 
5741   /* warn about confusing overload resolution for user-defined conversions,
5742      either between a constructor and a conversion op, or between two
5743      conversion ops.  */
5744   if (winner && cand1->second_conv
5745       && ((DECL_CONSTRUCTOR_P (cand1->fn)
5746 	   != DECL_CONSTRUCTOR_P (cand2->fn))
5747 	  /* Don't warn if the two conv ops convert to the same type...  */
5748 	  || (! DECL_CONSTRUCTOR_P (cand1->fn)
5749 	      && ! same_type_p (TREE_TYPE (TREE_TYPE (cand1->fn)),
5750 				TREE_TYPE (TREE_TYPE (cand2->fn))))))
5751     {
5752       int comp = compare_ics (cand1->second_conv, cand2->second_conv);
5753       if (comp != winner)
5754 	{
5755 	  struct z_candidate *w, *l;
5756 	  tree convn;
5757 	  if (winner == 1)
5758 	    w = cand1, l = cand2;
5759 	  else
5760 	    w = cand2, l = cand1;
5761 	  if (DECL_CONTEXT (cand1->fn) == DECL_CONTEXT (cand2->fn)
5762 	      && ! DECL_CONSTRUCTOR_P (cand1->fn)
5763 	      && ! DECL_CONSTRUCTOR_P (cand2->fn)
5764 	      && (convn = standard_conversion
5765 		  (TREE_TYPE (TREE_TYPE (l->fn)),
5766 		   TREE_TYPE (TREE_TYPE (w->fn)), NULL_TREE))
5767 	      && TREE_CODE (convn) == QUAL_CONV)
5768 	    /* Don't complain about `operator char *()' beating
5769 	       `operator const char *() const'.  */;
5770 	  else if (warn && warn_conversion)
5771 	    {
5772 	      tree source = source_type (TREE_VEC_ELT (w->convs, 0));
5773 	      if (! DECL_CONSTRUCTOR_P (w->fn))
5774 		source = TREE_TYPE (source);
5775 	      warning ("choosing `%D' over `%D'", w->fn, l->fn);
5776 	      warning ("  for conversion from `%T' to `%T'",
5777 			  source, TREE_TYPE (w->second_conv));
5778 	      warning ("  because conversion sequence for the argument is better");
5779 	    }
5780 	  else
5781 	    add_warning (w, l);
5782 	}
5783     }
5784 
5785   if (winner)
5786     return winner;
5787 
5788   /* or, if not that,
5789      F1 is a non-template function and F2 is a template function
5790      specialization.  */
5791 
5792   if (! cand1->template && cand2->template)
5793     return 1;
5794   else if (cand1->template && ! cand2->template)
5795     return -1;
5796 
5797   /* or, if not that,
5798      F1 and F2 are template functions and the function template for F1 is
5799      more specialized than the template for F2 according to the partial
5800      ordering rules.  */
5801 
5802   if (cand1->template && cand2->template)
5803     {
5804       winner = more_specialized
5805         (TI_TEMPLATE (cand1->template), TI_TEMPLATE (cand2->template),
5806          DEDUCE_ORDER,
5807          /* Tell the deduction code how many real function arguments
5808 	    we saw, not counting the implicit 'this' argument.  But,
5809 	    add_function_candidate() suppresses the "this" argument
5810 	    for constructors.
5811 
5812 	    [temp.func.order]: The presence of unused ellipsis and default
5813 	    arguments has no effect on the partial ordering of function
5814 	    templates.  */
5815          TREE_VEC_LENGTH (cand1->convs)
5816 	 - (DECL_NONSTATIC_MEMBER_FUNCTION_P (cand1->fn)
5817 	    - DECL_CONSTRUCTOR_P (cand1->fn)));
5818       /* HERE */
5819       if (winner)
5820         return winner;
5821     }
5822 
5823   /* or, if not that,
5824      the  context  is  an  initialization by user-defined conversion (see
5825      _dcl.init_  and  _over.match.user_)  and  the  standard   conversion
5826      sequence  from  the return type of F1 to the destination type (i.e.,
5827      the type of the entity being initialized)  is  a  better  conversion
5828      sequence  than the standard conversion sequence from the return type
5829      of F2 to the destination type.  */
5830 
5831   if (cand1->second_conv)
5832     {
5833       winner = compare_ics (cand1->second_conv, cand2->second_conv);
5834       if (winner)
5835         return winner;
5836     }
5837 
5838   /* Check whether we can discard a builtin candidate, either because we
5839      have two identical ones or matching builtin and non-builtin candidates.
5840 
5841      (Pedantically in the latter case the builtin which matched the user
5842      function should not be added to the overload set, but we spot it here.
5843 
5844      [over.match.oper]
5845      ... the builtin candidates include ...
5846      - do not have the same parameter type list as any non-template
5847        non-member candidate.  */
5848 
5849   if (TREE_CODE (cand1->fn) == IDENTIFIER_NODE
5850       || TREE_CODE (cand2->fn) == IDENTIFIER_NODE)
5851     {
5852       for (i = 0; i < len; ++i)
5853 	if (!same_type_p (TREE_TYPE (TREE_VEC_ELT (cand1->convs, i)),
5854 			  TREE_TYPE (TREE_VEC_ELT (cand2->convs, i))))
5855 	  break;
5856       if (i == TREE_VEC_LENGTH (cand1->convs))
5857 	{
5858 	  if (cand1->fn == cand2->fn)
5859 	    /* Two built-in candidates; arbitrarily pick one.  */
5860 	    return 1;
5861 	  else if (TREE_CODE (cand1->fn) == IDENTIFIER_NODE)
5862 	    /* cand1 is built-in; prefer cand2.  */
5863 	    return -1;
5864 	  else
5865 	    /* cand2 is built-in; prefer cand1.  */
5866 	    return 1;
5867 	}
5868     }
5869 
5870   /* If the two functions are the same (this can happen with declarations
5871      in multiple scopes and arg-dependent lookup), arbitrarily choose one.  */
5872   if (DECL_P (cand1->fn) && DECL_P (cand2->fn)
5873       && equal_functions (cand1->fn, cand2->fn))
5874     return 1;
5875 
5876 tweak:
5877 
5878   /* Extension: If the worst conversion for one candidate is worse than the
5879      worst conversion for the other, take the first.  */
5880   if (!pedantic)
5881     {
5882       int rank1 = IDENTITY_RANK, rank2 = IDENTITY_RANK;
5883       struct z_candidate *w = 0, *l = 0;
5884 
5885       for (i = 0; i < len; ++i)
5886 	{
5887 	  if (ICS_RANK (TREE_VEC_ELT (cand1->convs, i+off1)) > rank1)
5888 	    rank1 = ICS_RANK (TREE_VEC_ELT (cand1->convs, i+off1));
5889 	  if (ICS_RANK (TREE_VEC_ELT (cand2->convs, i+off2)) > rank2)
5890 	    rank2 = ICS_RANK (TREE_VEC_ELT (cand2->convs, i+off2));
5891 	}
5892       if (rank1 < rank2)
5893 	winner = 1, w = cand1, l = cand2;
5894       if (rank1 > rank2)
5895 	winner = -1, w = cand2, l = cand1;
5896       if (winner)
5897         {
5898 	  if (warn)
5899 	    pedwarn ("ISO C++ says that `%D' and `%D' are ambiguous \
5900 even though the worst conversion for the former is better than the worst \
5901 conversion for the latter", w->fn, l->fn);
5902 	  else
5903 	    add_warning (w, l);
5904           return winner;
5905         }
5906     }
5907 
5908   my_friendly_assert (!winner, 20010121);
5909   return 0;
5910 }
5911 
5912 /* Given a list of candidates for overloading, find the best one, if any.
5913    This algorithm has a worst case of O(2n) (winner is last), and a best
5914    case of O(n/2) (totally ambiguous); much better than a sorting
5915    algorithm.  */
5916 
5917 static struct z_candidate *
tourney(candidates)5918 tourney (candidates)
5919      struct z_candidate *candidates;
5920 {
5921   struct z_candidate *champ = candidates, *challenger;
5922   int fate;
5923   int champ_compared_to_predecessor = 0;
5924 
5925   /* Walk through the list once, comparing each current champ to the next
5926      candidate, knocking out a candidate or two with each comparison.  */
5927 
5928   for (challenger = champ->next; challenger; )
5929     {
5930       fate = joust (champ, challenger, 0);
5931       if (fate == 1)
5932 	challenger = challenger->next;
5933       else
5934 	{
5935 	  if (fate == 0)
5936 	    {
5937 	      champ = challenger->next;
5938 	      if (champ == 0)
5939 		return 0;
5940 	      champ_compared_to_predecessor = 0;
5941 	    }
5942 	  else
5943 	    {
5944 	      champ = challenger;
5945 	      champ_compared_to_predecessor = 1;
5946 	    }
5947 
5948 	  challenger = champ->next;
5949 	}
5950     }
5951 
5952   /* Make sure the champ is better than all the candidates it hasn't yet
5953      been compared to.  */
5954 
5955   for (challenger = candidates;
5956        challenger != champ
5957 	 && !(champ_compared_to_predecessor && challenger->next == champ);
5958        challenger = challenger->next)
5959     {
5960       fate = joust (champ, challenger, 0);
5961       if (fate != 1)
5962 	return 0;
5963     }
5964 
5965   return champ;
5966 }
5967 
5968 /* Returns nonzero if things of type FROM can be converted to TO.  */
5969 
5970 int
can_convert(to,from)5971 can_convert (to, from)
5972      tree to, from;
5973 {
5974   return can_convert_arg (to, from, NULL_TREE);
5975 }
5976 
5977 /* Returns nonzero if ARG (of type FROM) can be converted to TO.  */
5978 
5979 int
can_convert_arg(to,from,arg)5980 can_convert_arg (to, from, arg)
5981      tree to, from, arg;
5982 {
5983   tree t = implicit_conversion (to, from, arg, LOOKUP_NORMAL);
5984   return (t && ! ICS_BAD_FLAG (t));
5985 }
5986 
5987 /* Like can_convert_arg, but allows dubious conversions as well.  */
5988 
5989 int
can_convert_arg_bad(to,from,arg)5990 can_convert_arg_bad (to, from, arg)
5991      tree to, from, arg;
5992 {
5993   tree t = implicit_conversion (to, from, arg, LOOKUP_NORMAL);
5994   return !!t;
5995 }
5996 
5997 /* Convert EXPR to TYPE.  Return the converted expression.
5998 
5999    Note that we allow bad conversions here because by the time we get to
6000    this point we are committed to doing the conversion.  If we end up
6001    doing a bad conversion, convert_like will complain.  */
6002 
6003 tree
perform_implicit_conversion(type,expr)6004 perform_implicit_conversion (type, expr)
6005      tree type;
6006      tree expr;
6007 {
6008   tree conv;
6009 
6010   if (expr == error_mark_node)
6011     return error_mark_node;
6012   conv = implicit_conversion (type, TREE_TYPE (expr), expr,
6013 			      LOOKUP_NORMAL);
6014   if (!conv)
6015     {
6016       error ("could not convert `%E' to `%T'", expr, type);
6017       return error_mark_node;
6018     }
6019 
6020   return convert_like (conv, expr);
6021 }
6022 
6023 /* Convert EXPR to TYPE (as a direct-initialization) if that is
6024    permitted.  If the conversion is valid, the converted expression is
6025    returned.  Otherwise, NULL_TREE is returned, except in the case
6026    that TYPE is a class type; in that case, an error is issued.  */
6027 
6028 tree
perform_direct_initialization_if_possible(tree type,tree expr)6029 perform_direct_initialization_if_possible (tree type, tree expr)
6030 {
6031   tree conv;
6032 
6033   if (type == error_mark_node || error_operand_p (expr))
6034     return error_mark_node;
6035   /* [dcl.init]
6036 
6037      If the destination type is a (possibly cv-qualified) class type:
6038 
6039      -- If the initialization is direct-initialization ...,
6040      constructors are considered. ... If no constructor applies, or
6041      the overload resolution is ambiguous, the initialization is
6042      ill-formed.  */
6043   if (CLASS_TYPE_P (type))
6044     {
6045       expr = build_special_member_call (NULL_TREE, complete_ctor_identifier,
6046 					build_tree_list (NULL_TREE, expr),
6047 					TYPE_BINFO (type),
6048 					LOOKUP_NORMAL);
6049       return build_cplus_new (type, expr);
6050     }
6051   conv = implicit_conversion (type, TREE_TYPE (expr), expr,
6052 			      LOOKUP_NORMAL);
6053   if (!conv || ICS_BAD_FLAG (conv))
6054     return NULL_TREE;
6055   return convert_like_real (conv, expr, NULL_TREE, 0, 0,
6056 			    /*issue_conversion_warnings=*/false);
6057 }
6058 
6059 /* DECL is a VAR_DECL whose type is a REFERENCE_TYPE.  The reference
6060    is being bound to a temporary.  Create and return a new VAR_DECL
6061    with the indicated TYPE; this variable will store the value to
6062    which the reference is bound.  */
6063 
6064 tree
make_temporary_var_for_ref_to_temp(tree decl,tree type)6065 make_temporary_var_for_ref_to_temp (tree decl, tree type)
6066 {
6067   tree var;
6068 
6069   /* Create the variable.  */
6070   var = build_decl (VAR_DECL, NULL_TREE, type);
6071   DECL_ARTIFICIAL (var) = 1;
6072   TREE_USED (var) = 1;
6073 
6074   /* Register the variable.  */
6075   if (TREE_STATIC (decl))
6076     {
6077       /* Namespace-scope or local static; give it a mangled name.  */
6078       tree name;
6079 
6080       TREE_STATIC (var) = 1;
6081       name = mangle_ref_init_variable (decl);
6082       DECL_NAME (var) = name;
6083       SET_DECL_ASSEMBLER_NAME (var, name);
6084       var = pushdecl_top_level (var);
6085     }
6086   else
6087     {
6088       /* Create a new cleanup level if necessary.  */
6089       maybe_push_cleanup_level (type);
6090       /* Don't push unnamed temps.  Do set DECL_CONTEXT, though.  */
6091       DECL_CONTEXT (var) = current_function_decl;
6092     }
6093 
6094   return var;
6095 }
6096 
6097   /* Convert EXPR to the indicated reference TYPE, in a way suitable
6098      for initializing a variable of that TYPE.  If DECL is non-NULL,
6099      it is the VAR_DECL being initialized with the EXPR.  (In that
6100      case, the type of DECL will be TYPE.)  If DECL is non-NULL, then
6101      CLEANUP must also be non-NULL, and with *CLEANUP initialized to
6102      NULL.  Upon return, if *CLEANUP is no longer NULL, it will be a
6103      CLEANUP_STMT that should be inserted after the returned
6104      expression is used to initialize DECL.
6105 
6106      Return the converted expression.  */
6107 
6108 tree
initialize_reference(type,expr,decl,cleanup)6109 initialize_reference (type, expr, decl, cleanup)
6110      tree type;
6111      tree expr;
6112      tree decl;
6113      tree *cleanup;
6114 {
6115   tree conv;
6116 
6117   if (type == error_mark_node || error_operand_p (expr))
6118     return error_mark_node;
6119 
6120   conv = reference_binding (type, TREE_TYPE (expr), expr, LOOKUP_NORMAL);
6121   if (!conv || ICS_BAD_FLAG (conv))
6122     {
6123       if (!(TYPE_QUALS (TREE_TYPE (type)) & TYPE_QUAL_CONST)
6124           && !real_lvalue_p (expr))
6125         error ("invalid initialization of non-const reference of "
6126                "type '%T' from a temporary of type '%T'",
6127                type, TREE_TYPE (expr));
6128       else
6129         error ("invalid initialization of reference of type "
6130 	       "'%T' from expression of type '%T'", type,
6131 	       TREE_TYPE (expr));
6132       return error_mark_node;
6133     }
6134 
6135   /* If DECL is non-NULL, then this special rule applies:
6136 
6137        [class.temporary]
6138 
6139        The temporary to which the reference is bound or the temporary
6140        that is the complete object to which the reference is bound
6141        persists for the lifetime of the reference.
6142 
6143        The temporaries created during the evaluation of the expression
6144        initializing the reference, except the temporary to which the
6145        reference is bound, are destroyed at the end of the
6146        full-expression in which they are created.
6147 
6148      In that case, we store the converted expression into a new
6149      VAR_DECL in a new scope.
6150 
6151      However, we want to be careful not to create temporaries when
6152      they are not required.  For example, given:
6153 
6154        struct B {};
6155        struct D : public B {};
6156        D f();
6157        const B& b = f();
6158 
6159      there is no need to copy the return value from "f"; we can just
6160      extend its lifetime.  Similarly, given:
6161 
6162        struct S {};
6163        struct T { operator S(); };
6164        T t;
6165        const S& s = t;
6166 
6167     we can extend the lifetime of the return value of the conversion
6168     operator.  */
6169   my_friendly_assert (TREE_CODE (conv) == REF_BIND, 20030302);
6170   if (decl)
6171     {
6172       tree var;
6173       tree base_conv_type;
6174 
6175       /* Skip over the REF_BIND.  */
6176       conv = TREE_OPERAND (conv, 0);
6177       /* If the next conversion is a BASE_CONV, skip that too -- but
6178 	 remember that the conversion was required.  */
6179       if (TREE_CODE (conv) == BASE_CONV && !NEED_TEMPORARY_P (conv))
6180 	{
6181 	  base_conv_type = TREE_TYPE (conv);
6182 	  conv = TREE_OPERAND (conv, 0);
6183 	}
6184       else
6185 	base_conv_type = NULL_TREE;
6186       /* Perform the remainder of the conversion.  */
6187       expr = convert_like_real (conv, expr,
6188 				/*fn=*/NULL_TREE, /*argnum=*/0,
6189 				/*inner=*/-1,
6190 				/*issue_conversion_warnings=*/true);
6191       if (!real_non_cast_lvalue_p (expr))
6192 	{
6193 	  tree init;
6194 	  tree type;
6195 
6196 	  /* Create the temporary variable.  */
6197 	  type = TREE_TYPE (expr);
6198 	  var = make_temporary_var_for_ref_to_temp (decl, type);
6199 	  layout_decl (var, 0);
6200 	  /* If the rvalue is the result of a function call it will be
6201 	     a TARGET_EXPR.  If it is some other construct (such as a
6202 	     member access expression where the underlying object is
6203 	     itself the result of a function call), turn it into a
6204 	     TARGET_EXPR here.  It is important that EXPR be a
6205 	     TARGET_EXPR below since otherwise the INIT_EXPR will
6206 	     attempt to make a bitwise copy of EXPR to intialize
6207 	     VAR. */
6208 	  if (TREE_CODE (expr) != TARGET_EXPR)
6209 	    expr = get_target_expr (expr);
6210 	  /* Create the INIT_EXPR that will initialize the temporary
6211 	     variable.  */
6212 	  init = build (INIT_EXPR, type, var, expr);
6213 	  if (at_function_scope_p ())
6214 	    {
6215 	      add_decl_stmt (var);
6216 	      *cleanup = cxx_maybe_build_cleanup (var);
6217 	      if (*cleanup)
6218 		/* We must be careful to destroy the temporary only
6219 		   after its initialization has taken place.  If the
6220 		   initialization throws an exception, then the
6221 		   destructor should not be run.  We cannot simply
6222 		   transform INIT into something like:
6223 
6224 		     (INIT, ({ CLEANUP_STMT; }))
6225 
6226 		   because emit_local_var always treats the
6227 		   initializer as a full-expression.  Thus, the
6228 		   destructor would run too early; it would run at the
6229 		   end of initializing the reference variable, rather
6230 		   than at the end of the block enclosing the
6231 		   reference variable.
6232 
6233 		   The solution is to pass back a CLEANUP_STMT which
6234 		   the caller is responsible for attaching to the
6235 		   statement tree.  */
6236 		*cleanup = build_stmt (CLEANUP_STMT, var, *cleanup);
6237 	    }
6238 	  else
6239 	    {
6240 	      rest_of_decl_compilation (var, NULL, /*toplev=*/1, at_eof);
6241 	      if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
6242 		static_aggregates = tree_cons (NULL_TREE, var,
6243 					       static_aggregates);
6244 	    }
6245 	  /* Use its address to initialize the reference variable.  */
6246 	  expr = build_address (var);
6247 	  expr = build (COMPOUND_EXPR, TREE_TYPE (expr), init, expr);
6248 	}
6249       else
6250 	/* Take the address of EXPR.  */
6251 	expr = build_unary_op (ADDR_EXPR, expr, 0);
6252       /* If a BASE_CONV was required, perform it now.  */
6253       if (base_conv_type)
6254 	expr = (perform_implicit_conversion
6255 		(build_pointer_type (base_conv_type), expr));
6256       return build_nop (type, expr);
6257     }
6258 
6259   /* Perform the conversion.  */
6260   return convert_like (conv, expr);
6261 }
6262 
6263 #include "gt-cp-call.h"
6264