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