* arm.c (arm_split_constant): Don't try to force a constant to
[official-gcc.git] / gcc / cp / call.c
blob4e2e2538d40b01c0896319cc78cd66f3ca77f99a
1 /* Functions related to invoking methods and overloaded functions.
2 Copyright (C) 1987, 92-97, 1998, 1999 Free Software Foundation, Inc.
3 Contributed by Michael Tiemann (tiemann@cygnus.com) and
4 modified by Brendan Kehoe (brendan@cygnus.com).
6 This file is part of GNU CC.
8 GNU CC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
11 any later version.
13 GNU CC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU CC; see the file COPYING. If not, write to
20 the Free Software Foundation, 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
24 /* High-level class interface. */
26 #include "config.h"
27 #include "system.h"
28 #include "tree.h"
29 #include "cp-tree.h"
30 #include "output.h"
31 #include "flags.h"
32 #include "rtl.h"
33 #include "toplev.h"
35 #include "obstack.h"
36 #define obstack_chunk_alloc xmalloc
37 #define obstack_chunk_free free
39 extern int inhibit_warnings;
40 extern tree ctor_label, dtor_label;
42 static tree build_new_method_call PROTO((tree, tree, tree, tree, int));
44 static tree build_field_call PROTO((tree, tree, tree, tree));
45 static tree find_scoped_type PROTO((tree, tree, tree));
46 static struct z_candidate * tourney PROTO((struct z_candidate *));
47 static int joust PROTO((struct z_candidate *, struct z_candidate *, int));
48 static int compare_ics PROTO((tree, tree));
49 static tree build_over_call PROTO((struct z_candidate *, tree, int));
50 static tree convert_like PROTO((tree, tree));
51 static void op_error PROTO((enum tree_code, enum tree_code, tree, tree,
52 tree, char *));
53 static tree build_object_call PROTO((tree, tree));
54 static tree resolve_args PROTO((tree));
55 static struct z_candidate * build_user_type_conversion_1
56 PROTO ((tree, tree, int));
57 static void print_z_candidates PROTO((struct z_candidate *));
58 static tree build_this PROTO((tree));
59 static struct z_candidate * splice_viable PROTO((struct z_candidate *));
60 static int any_viable PROTO((struct z_candidate *));
61 static struct z_candidate * add_template_candidate
62 PROTO((struct z_candidate *, tree, tree, tree, tree, int,
63 unification_kind_t));
64 static struct z_candidate * add_template_candidate_real
65 PROTO((struct z_candidate *, tree, tree, tree, tree, int,
66 tree, unification_kind_t));
67 static struct z_candidate * add_template_conv_candidate
68 PROTO((struct z_candidate *, tree, tree, tree, tree));
69 static struct z_candidate * add_builtin_candidates
70 PROTO((struct z_candidate *, enum tree_code, enum tree_code,
71 tree, tree *, int));
72 static struct z_candidate * add_builtin_candidate
73 PROTO((struct z_candidate *, enum tree_code, enum tree_code,
74 tree, tree, tree, tree *, tree *, int));
75 static int is_complete PROTO((tree));
76 static struct z_candidate * build_builtin_candidate
77 PROTO((struct z_candidate *, tree, tree, tree, tree *, tree *,
78 int));
79 static struct z_candidate * add_conv_candidate
80 PROTO((struct z_candidate *, tree, tree, tree));
81 static struct z_candidate * add_function_candidate
82 PROTO((struct z_candidate *, tree, tree, int));
83 static tree implicit_conversion PROTO((tree, tree, tree, int));
84 static tree standard_conversion PROTO((tree, tree, tree));
85 static tree reference_binding PROTO((tree, tree, tree, int));
86 static tree strip_top_quals PROTO((tree));
87 static tree non_reference PROTO((tree));
88 static tree build_conv PROTO((enum tree_code, tree, tree));
89 static int is_subseq PROTO((tree, tree));
90 static int is_properly_derived_from PROTO((tree, tree));
91 static int maybe_handle_ref_bind PROTO((tree*, tree*));
92 static void maybe_handle_implicit_object PROTO((tree*));
94 tree
95 build_vfield_ref (datum, type)
96 tree datum, type;
98 tree rval;
100 if (datum == error_mark_node)
101 return error_mark_node;
103 if (TREE_CODE (TREE_TYPE (datum)) == REFERENCE_TYPE)
104 datum = convert_from_reference (datum);
106 if (! TYPE_USES_COMPLEX_INHERITANCE (type))
107 rval = build (COMPONENT_REF, TREE_TYPE (CLASSTYPE_VFIELD (type)),
108 datum, CLASSTYPE_VFIELD (type));
109 else
110 rval = build_component_ref (datum, DECL_NAME (CLASSTYPE_VFIELD (type)), NULL_TREE, 0);
112 return rval;
115 /* Build a call to a member of an object. I.e., one that overloads
116 operator ()(), or is a pointer-to-function or pointer-to-method. */
118 static tree
119 build_field_call (basetype_path, instance_ptr, name, parms)
120 tree basetype_path, instance_ptr, name, parms;
122 tree field, instance;
124 if (name == ctor_identifier || name == dtor_identifier)
125 return NULL_TREE;
127 if (instance_ptr == current_class_ptr)
129 /* Check to see if we really have a reference to an instance variable
130 with `operator()()' overloaded. */
131 field = IDENTIFIER_CLASS_VALUE (name);
133 if (field == NULL_TREE)
135 cp_error ("`this' has no member named `%D'", name);
136 return error_mark_node;
139 if (TREE_CODE (field) == FIELD_DECL || TREE_CODE (field) == VAR_DECL)
141 /* If it's a field, try overloading operator (),
142 or calling if the field is a pointer-to-function. */
143 instance = build_component_ref_1 (current_class_ref, field, 0);
144 if (instance == error_mark_node)
145 return error_mark_node;
147 if (TYPE_LANG_SPECIFIC (TREE_TYPE (instance)))
148 return build_opfncall (CALL_EXPR, LOOKUP_NORMAL, instance, parms, NULL_TREE);
150 if (TREE_CODE (TREE_TYPE (instance)) == POINTER_TYPE)
152 if (TREE_CODE (TREE_TYPE (TREE_TYPE (instance))) == FUNCTION_TYPE)
153 return build_function_call (instance, parms);
154 else if (TREE_CODE (TREE_TYPE (TREE_TYPE (instance))) == METHOD_TYPE)
155 return build_function_call (instance, expr_tree_cons (NULL_TREE, current_class_ptr, parms));
158 return NULL_TREE;
161 /* Check to see if this is not really a reference to an instance variable
162 with `operator()()' overloaded. */
163 field = lookup_field (basetype_path, name, 1, 0);
165 /* This can happen if the reference was ambiguous or for access
166 violations. */
167 if (field == error_mark_node)
168 return error_mark_node;
170 if (field && (TREE_CODE (field) == FIELD_DECL ||
171 TREE_CODE (field) == VAR_DECL))
173 tree basetype;
174 tree ftype = TREE_TYPE (field);
176 if (TREE_CODE (ftype) == REFERENCE_TYPE)
177 ftype = TREE_TYPE (ftype);
179 if (TYPE_LANG_SPECIFIC (ftype))
181 /* Make the next search for this field very short. */
182 basetype = DECL_FIELD_CONTEXT (field);
183 instance_ptr = convert_pointer_to (basetype, instance_ptr);
185 instance = build_indirect_ref (instance_ptr, NULL_PTR);
186 return build_opfncall (CALL_EXPR, LOOKUP_NORMAL,
187 build_component_ref_1 (instance, field, 0),
188 parms, NULL_TREE);
190 if (TREE_CODE (ftype) == POINTER_TYPE)
192 if (TREE_CODE (TREE_TYPE (ftype)) == FUNCTION_TYPE
193 || TREE_CODE (TREE_TYPE (ftype)) == METHOD_TYPE)
195 /* This is a member which is a pointer to function. */
196 tree ref
197 = build_component_ref_1 (build_indirect_ref (instance_ptr,
198 NULL_PTR),
199 field, LOOKUP_COMPLAIN);
200 if (ref == error_mark_node)
201 return error_mark_node;
202 return build_function_call (ref, parms);
205 else if (TREE_CODE (ftype) == METHOD_TYPE)
207 error ("invalid call via pointer-to-member function");
208 return error_mark_node;
210 else
211 return NULL_TREE;
213 return NULL_TREE;
216 static tree
217 find_scoped_type (type, inner_name, inner_types)
218 tree type, inner_name, inner_types;
220 tree tags = CLASSTYPE_TAGS (type);
222 while (tags)
224 /* The TREE_PURPOSE of an enum tag (which becomes a member of the
225 enclosing class) is set to the name for the enum type. So, if
226 inner_name is `bar', and we strike `baz' for `enum bar { baz }',
227 then this test will be true. */
228 if (TREE_PURPOSE (tags) == inner_name)
230 if (inner_types == NULL_TREE)
231 return TYPE_MAIN_DECL (TREE_VALUE (tags));
232 return resolve_scope_to_name (TREE_VALUE (tags), inner_types);
234 tags = TREE_CHAIN (tags);
237 /* Look for a TYPE_DECL. */
238 for (tags = TYPE_FIELDS (type); tags; tags = TREE_CHAIN (tags))
239 if (TREE_CODE (tags) == TYPE_DECL && DECL_NAME (tags) == inner_name)
241 /* Code by raeburn. */
242 if (inner_types == NULL_TREE)
243 return tags;
244 return resolve_scope_to_name (TREE_TYPE (tags), inner_types);
247 return NULL_TREE;
250 /* Resolve an expression NAME1::NAME2::...::NAMEn to
251 the name that names the above nested type. INNER_TYPES
252 is a chain of nested type names (held together by SCOPE_REFs);
253 OUTER_TYPE is the type we know to enclose INNER_TYPES.
254 Returns NULL_TREE if there is an error. */
256 tree
257 resolve_scope_to_name (outer_type, inner_stuff)
258 tree outer_type, inner_stuff;
260 register tree tmp;
261 tree inner_name, inner_type;
263 if (outer_type == NULL_TREE && current_class_type != NULL_TREE)
265 /* We first try to look for a nesting in our current class context,
266 then try any enclosing classes. */
267 tree type = current_class_type;
269 while (type && (TREE_CODE (type) == RECORD_TYPE
270 || TREE_CODE (type) == UNION_TYPE))
272 tree rval = resolve_scope_to_name (type, inner_stuff);
274 if (rval != NULL_TREE)
275 return rval;
276 type = DECL_CONTEXT (TYPE_MAIN_DECL (type));
280 if (TREE_CODE (inner_stuff) == SCOPE_REF)
282 inner_name = TREE_OPERAND (inner_stuff, 0);
283 inner_type = TREE_OPERAND (inner_stuff, 1);
285 else
287 inner_name = inner_stuff;
288 inner_type = NULL_TREE;
291 if (outer_type == NULL_TREE)
293 tree x;
294 /* If we have something that's already a type by itself,
295 use that. */
296 if (IDENTIFIER_HAS_TYPE_VALUE (inner_name))
298 if (inner_type)
299 return resolve_scope_to_name (IDENTIFIER_TYPE_VALUE (inner_name),
300 inner_type);
301 return inner_name;
304 x = lookup_name (inner_name, 0);
306 if (x && TREE_CODE (x) == NAMESPACE_DECL)
308 x = lookup_namespace_name (x, inner_type);
309 return x;
311 return NULL_TREE;
314 if (! IS_AGGR_TYPE (outer_type))
315 return NULL_TREE;
317 /* Look for member classes or enums. */
318 tmp = find_scoped_type (outer_type, inner_name, inner_type);
320 /* If it's not a type in this class, then go down into the
321 base classes and search there. */
322 if (! tmp && TYPE_BINFO (outer_type))
324 tree binfos = TYPE_BINFO_BASETYPES (outer_type);
325 int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
327 for (i = 0; i < n_baselinks; i++)
329 tree base_binfo = TREE_VEC_ELT (binfos, i);
330 tmp = resolve_scope_to_name (BINFO_TYPE (base_binfo), inner_stuff);
331 if (tmp)
332 return tmp;
334 tmp = NULL_TREE;
337 return tmp;
340 /* Returns nonzero iff the destructor name specified in NAME
341 (a BIT_NOT_EXPR) matches BASETYPE. The operand of NAME can take many
342 forms... */
345 check_dtor_name (basetype, name)
346 tree basetype, name;
348 name = TREE_OPERAND (name, 0);
350 /* Just accept something we've already complained about. */
351 if (name == error_mark_node)
352 return 1;
354 if (TREE_CODE (name) == TYPE_DECL)
355 name = TREE_TYPE (name);
356 else if (TREE_CODE_CLASS (TREE_CODE (name)) == 't')
357 /* OK */;
358 else if (TREE_CODE (name) == IDENTIFIER_NODE)
360 if ((IS_AGGR_TYPE (basetype) && name == constructor_name (basetype))
361 || (TREE_CODE (basetype) == ENUMERAL_TYPE
362 && name == TYPE_IDENTIFIER (basetype)))
363 name = basetype;
364 else
365 name = get_type_value (name);
367 else
368 my_friendly_abort (980605);
370 if (name && TYPE_MAIN_VARIANT (basetype) == TYPE_MAIN_VARIANT (name))
371 return 1;
372 return 0;
375 /* Build a method call of the form `EXP->SCOPES::NAME (PARMS)'.
376 This is how virtual function calls are avoided. */
378 tree
379 build_scoped_method_call (exp, basetype, name, parms)
380 tree exp, basetype, name, parms;
382 /* Because this syntactic form does not allow
383 a pointer to a base class to be `stolen',
384 we need not protect the derived->base conversion
385 that happens here.
387 @@ But we do have to check access privileges later. */
388 tree binfo, decl;
389 tree type = TREE_TYPE (exp);
391 if (type == error_mark_node
392 || basetype == error_mark_node)
393 return error_mark_node;
395 if (processing_template_decl)
397 if (TREE_CODE (name) == BIT_NOT_EXPR
398 && TREE_CODE (TREE_OPERAND (name, 0)) == IDENTIFIER_NODE)
400 tree type = get_aggr_from_typedef (TREE_OPERAND (name, 0), 0);
401 if (type)
402 name = build_min_nt (BIT_NOT_EXPR, type);
404 name = build_min_nt (SCOPE_REF, basetype, name);
405 return build_min_nt (METHOD_CALL_EXPR, name, exp, parms, NULL_TREE);
408 if (TREE_CODE (type) == REFERENCE_TYPE)
409 type = TREE_TYPE (type);
411 if (TREE_CODE (basetype) == TREE_VEC)
413 binfo = basetype;
414 basetype = BINFO_TYPE (binfo);
416 else
417 binfo = NULL_TREE;
419 /* Check the destructor call syntax. */
420 if (TREE_CODE (name) == BIT_NOT_EXPR)
422 /* We can get here if someone writes their destructor call like
423 `obj.NS::~T()'; this isn't really a scoped method call, so hand
424 it off. */
425 if (TREE_CODE (basetype) == NAMESPACE_DECL)
426 return build_method_call (exp, name, parms, NULL_TREE, LOOKUP_NORMAL);
428 if (! check_dtor_name (basetype, name))
429 cp_error ("qualified type `%T' does not match destructor name `~%T'",
430 basetype, TREE_OPERAND (name, 0));
432 /* Destructors can be "called" for simple types; see 5.2.4 and 12.4 Note
433 that explicit ~int is caught in the parser; this deals with typedefs
434 and template parms. */
435 if (! IS_AGGR_TYPE (basetype))
437 if (TYPE_MAIN_VARIANT (type) != TYPE_MAIN_VARIANT (basetype))
438 cp_error ("type of `%E' does not match destructor type `%T' (type was `%T')",
439 exp, basetype, type);
441 return cp_convert (void_type_node, exp);
445 if (! is_aggr_type (basetype, 1))
446 return error_mark_node;
448 if (! IS_AGGR_TYPE (type))
450 cp_error ("base object `%E' of scoped method call is of non-aggregate type `%T'",
451 exp, type);
452 return error_mark_node;
455 if (! binfo)
457 binfo = get_binfo (basetype, type, 1);
458 if (binfo == error_mark_node)
459 return error_mark_node;
460 if (! binfo)
461 error_not_base_type (basetype, type);
464 if (binfo)
466 if (TREE_CODE (exp) == INDIRECT_REF)
467 decl = build_indirect_ref
468 (convert_pointer_to_real
469 (binfo, build_unary_op (ADDR_EXPR, exp, 0)), NULL_PTR);
470 else
471 decl = build_scoped_ref (exp, basetype);
473 /* Call to a destructor. */
474 if (TREE_CODE (name) == BIT_NOT_EXPR)
476 if (! TYPE_HAS_DESTRUCTOR (TREE_TYPE (decl)))
477 return cp_convert (void_type_node, exp);
479 return build_delete (TREE_TYPE (decl), decl, integer_two_node,
480 LOOKUP_NORMAL|LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR,
484 /* Call to a method. */
485 return build_method_call (decl, name, parms, binfo,
486 LOOKUP_NORMAL|LOOKUP_NONVIRTUAL);
488 return error_mark_node;
491 /* We want the address of a function or method. We avoid creating a
492 pointer-to-member function. */
494 tree
495 build_addr_func (function)
496 tree function;
498 tree type = TREE_TYPE (function);
500 /* We have to do these by hand to avoid real pointer to member
501 functions. */
502 if (TREE_CODE (type) == METHOD_TYPE)
504 tree addr;
506 type = build_pointer_type (type);
508 if (mark_addressable (function) == 0)
509 return error_mark_node;
511 addr = build1 (ADDR_EXPR, type, function);
513 /* Address of a static or external variable or function counts
514 as a constant */
515 if (staticp (function))
516 TREE_CONSTANT (addr) = 1;
518 function = addr;
520 else
521 function = default_conversion (function);
523 return function;
526 /* Build a CALL_EXPR, we can handle FUNCTION_TYPEs, METHOD_TYPEs, or
527 POINTER_TYPE to those. Note, pointer to member function types
528 (TYPE_PTRMEMFUNC_P) must be handled by our callers. */
530 tree
531 build_call (function, result_type, parms)
532 tree function, result_type, parms;
534 int is_constructor = 0;
535 tree tmp;
536 tree decl;
538 function = build_addr_func (function);
540 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function)))
542 sorry ("unable to call pointer to member function here");
543 return error_mark_node;
546 if (TREE_CODE (function) == ADDR_EXPR
547 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
548 decl = TREE_OPERAND (function, 0);
549 else
550 decl = NULL_TREE;
552 if (decl && DECL_CONSTRUCTOR_P (decl))
553 is_constructor = 1;
555 if (decl)
556 my_friendly_assert (TREE_USED (decl), 990125);
558 /* Don't pass empty class objects by value. This is useful
559 for tags in STL, which are used to control overload resolution.
560 We don't need to handle other cases of copying empty classes. */
561 if (! decl || ! DECL_BUILT_IN (decl))
562 for (tmp = parms; tmp; tmp = TREE_CHAIN (tmp))
563 if (is_empty_class (TREE_TYPE (TREE_VALUE (tmp)))
564 && ! TREE_ADDRESSABLE (TREE_TYPE (TREE_VALUE (tmp))))
566 tree t = make_node (RTL_EXPR);
567 TREE_TYPE (t) = TREE_TYPE (TREE_VALUE (tmp));
568 RTL_EXPR_RTL (t) = const0_rtx;
569 RTL_EXPR_SEQUENCE (t) = NULL_RTX;
570 TREE_VALUE (tmp) = build (COMPOUND_EXPR, TREE_TYPE (t),
571 TREE_VALUE (tmp), t);
574 function = build_nt (CALL_EXPR, function, parms, NULL_TREE);
575 TREE_HAS_CONSTRUCTOR (function) = is_constructor;
576 TREE_TYPE (function) = result_type;
577 TREE_SIDE_EFFECTS (function) = 1;
579 return function;
582 /* Build something of the form ptr->method (args)
583 or object.method (args). This can also build
584 calls to constructors, and find friends.
586 Member functions always take their class variable
587 as a pointer.
589 INSTANCE is a class instance.
591 NAME is the name of the method desired, usually an IDENTIFIER_NODE.
593 PARMS help to figure out what that NAME really refers to.
595 BASETYPE_PATH, if non-NULL, contains a chain from the type of INSTANCE
596 down to the real instance type to use for access checking. We need this
597 information to get protected accesses correct. This parameter is used
598 by build_member_call.
600 FLAGS is the logical disjunction of zero or more LOOKUP_
601 flags. See cp-tree.h for more info.
603 If this is all OK, calls build_function_call with the resolved
604 member function.
606 This function must also handle being called to perform
607 initialization, promotion/coercion of arguments, and
608 instantiation of default parameters.
610 Note that NAME may refer to an instance variable name. If
611 `operator()()' is defined for the type of that field, then we return
612 that result. */
614 tree
615 build_method_call (instance, name, parms, basetype_path, flags)
616 tree instance, name, parms, basetype_path;
617 int flags;
619 tree basetype, instance_ptr;
621 #ifdef GATHER_STATISTICS
622 n_build_method_call++;
623 #endif
625 if (instance == error_mark_node
626 || name == error_mark_node
627 || parms == error_mark_node
628 || (instance != NULL_TREE && TREE_TYPE (instance) == error_mark_node))
629 return error_mark_node;
631 if (processing_template_decl)
633 /* We need to process template parm names here so that tsubst catches
634 them properly. Other type names can wait. */
635 if (TREE_CODE (name) == BIT_NOT_EXPR)
637 tree type = NULL_TREE;
639 if (TREE_CODE (TREE_OPERAND (name, 0)) == IDENTIFIER_NODE)
640 type = get_aggr_from_typedef (TREE_OPERAND (name, 0), 0);
641 else if (TREE_CODE (TREE_OPERAND (name, 0)) == TYPE_DECL)
642 type = TREE_TYPE (TREE_OPERAND (name, 0));
644 if (type && TREE_CODE (type) == TEMPLATE_TYPE_PARM)
645 name = build_min_nt (BIT_NOT_EXPR, type);
648 return build_min_nt (METHOD_CALL_EXPR, name, instance, parms, NULL_TREE);
651 /* This is the logic that magically deletes the second argument to
652 operator delete, if it is not needed. */
653 if (name == ansi_opname[(int) DELETE_EXPR] && list_length (parms)==2)
655 tree save_last = TREE_CHAIN (parms);
657 /* get rid of unneeded argument */
658 TREE_CHAIN (parms) = NULL_TREE;
659 if (build_method_call (instance, name, parms, basetype_path,
660 (LOOKUP_SPECULATIVELY|flags) & ~LOOKUP_COMPLAIN))
662 /* If it finds a match, return it. */
663 return build_method_call (instance, name, parms, basetype_path, flags);
665 /* If it doesn't work, two argument delete must work */
666 TREE_CHAIN (parms) = save_last;
668 /* We already know whether it's needed or not for vec delete. */
669 else if (name == ansi_opname[(int) VEC_DELETE_EXPR]
670 && TYPE_LANG_SPECIFIC (TREE_TYPE (instance))
671 && ! TYPE_VEC_DELETE_TAKES_SIZE (TREE_TYPE (instance)))
672 TREE_CHAIN (parms) = NULL_TREE;
674 if (TREE_CODE (name) == BIT_NOT_EXPR)
676 if (parms)
677 error ("destructors take no parameters");
678 basetype = TREE_TYPE (instance);
679 if (TREE_CODE (basetype) == REFERENCE_TYPE)
680 basetype = TREE_TYPE (basetype);
682 if (! check_dtor_name (basetype, name))
683 cp_error
684 ("destructor name `~%T' does not match type `%T' of expression",
685 TREE_OPERAND (name, 0), basetype);
687 if (! TYPE_HAS_DESTRUCTOR (complete_type (basetype)))
688 return cp_convert (void_type_node, instance);
689 instance = default_conversion (instance);
690 instance_ptr = build_unary_op (ADDR_EXPR, instance, 0);
691 return build_delete (build_pointer_type (basetype),
692 instance_ptr, integer_two_node,
693 LOOKUP_NORMAL|LOOKUP_DESTRUCTOR, 0);
696 return build_new_method_call (instance, name, parms, basetype_path, flags);
699 /* New overloading code. */
701 struct z_candidate {
702 tree fn;
703 tree convs;
704 tree second_conv;
705 int viable;
706 tree basetype_path;
707 tree template;
708 tree warnings;
709 struct z_candidate *next;
712 #define IDENTITY_RANK 0
713 #define EXACT_RANK 1
714 #define PROMO_RANK 2
715 #define STD_RANK 3
716 #define PBOOL_RANK 4
717 #define USER_RANK 5
718 #define ELLIPSIS_RANK 6
719 #define BAD_RANK 7
721 #define ICS_RANK(NODE) \
722 (ICS_BAD_FLAG (NODE) ? BAD_RANK \
723 : ICS_ELLIPSIS_FLAG (NODE) ? ELLIPSIS_RANK \
724 : ICS_USER_FLAG (NODE) ? USER_RANK \
725 : ICS_STD_RANK (NODE))
727 #define ICS_STD_RANK(NODE) TREE_COMPLEXITY (NODE)
729 #define ICS_USER_FLAG(NODE) TREE_LANG_FLAG_0 (NODE)
730 #define ICS_ELLIPSIS_FLAG(NODE) TREE_LANG_FLAG_1 (NODE)
731 #define ICS_THIS_FLAG(NODE) TREE_LANG_FLAG_2 (NODE)
732 #define ICS_BAD_FLAG(NODE) TREE_LANG_FLAG_3 (NODE)
734 #define USER_CONV_CAND(NODE) \
735 ((struct z_candidate *)WRAPPER_PTR (TREE_OPERAND (NODE, 1)))
736 #define USER_CONV_FN(NODE) (USER_CONV_CAND (NODE)->fn)
739 null_ptr_cst_p (t)
740 tree t;
742 if (t == null_node
743 || (integer_zerop (t) && TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE))
744 return 1;
745 return 0;
748 static tree
749 build_conv (code, type, from)
750 enum tree_code code;
751 tree type, from;
753 tree t = build1 (code, type, from);
754 int rank = ICS_STD_RANK (from);
755 switch (code)
757 case PTR_CONV:
758 case PMEM_CONV:
759 case BASE_CONV:
760 case STD_CONV:
761 if (rank < STD_RANK)
762 rank = STD_RANK;
763 break;
765 case QUAL_CONV:
766 if (rank < EXACT_RANK)
767 rank = EXACT_RANK;
769 default:
770 break;
772 ICS_STD_RANK (t) = rank;
773 ICS_USER_FLAG (t) = ICS_USER_FLAG (from);
774 ICS_BAD_FLAG (t) = ICS_BAD_FLAG (from);
775 return t;
778 static tree
779 non_reference (t)
780 tree t;
782 if (TREE_CODE (t) == REFERENCE_TYPE)
783 t = TREE_TYPE (t);
784 return t;
787 static tree
788 strip_top_quals (t)
789 tree t;
791 if (TREE_CODE (t) == ARRAY_TYPE)
792 return t;
793 return TYPE_MAIN_VARIANT (t);
796 /* Returns the standard conversion path (see [conv]) from type FROM to type
797 TO, if any. For proper handling of null pointer constants, you must
798 also pass the expression EXPR to convert from. */
800 static tree
801 standard_conversion (to, from, expr)
802 tree to, from, expr;
804 enum tree_code fcode, tcode;
805 tree conv;
806 int fromref = 0;
808 if (TREE_CODE (to) == REFERENCE_TYPE)
809 to = TREE_TYPE (to);
810 if (TREE_CODE (from) == REFERENCE_TYPE)
812 fromref = 1;
813 from = TREE_TYPE (from);
815 to = strip_top_quals (to);
816 from = strip_top_quals (from);
818 if ((TYPE_PTRFN_P (to) || TYPE_PTRMEMFUNC_P (to))
819 && expr && type_unknown_p (expr))
821 expr = instantiate_type (to, expr, 0);
822 if (expr == error_mark_node)
823 return NULL_TREE;
824 from = TREE_TYPE (expr);
827 fcode = TREE_CODE (from);
828 tcode = TREE_CODE (to);
830 conv = build1 (IDENTITY_CONV, from, expr);
832 if (fcode == FUNCTION_TYPE)
834 from = build_pointer_type (from);
835 fcode = TREE_CODE (from);
836 conv = build_conv (LVALUE_CONV, from, conv);
838 else if (fcode == ARRAY_TYPE)
840 from = build_pointer_type (TREE_TYPE (from));
841 fcode = TREE_CODE (from);
842 conv = build_conv (LVALUE_CONV, from, conv);
844 else if (fromref || (expr && real_lvalue_p (expr)))
845 conv = build_conv (RVALUE_CONV, from, conv);
847 if (from == to)
848 return conv;
850 if ((tcode == POINTER_TYPE || TYPE_PTRMEMFUNC_P (to))
851 && expr && null_ptr_cst_p (expr))
853 conv = build_conv (STD_CONV, to, conv);
855 else if (tcode == POINTER_TYPE && fcode == POINTER_TYPE)
857 enum tree_code ufcode = TREE_CODE (TREE_TYPE (from));
858 enum tree_code utcode = TREE_CODE (TREE_TYPE (to));
860 if (same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (from)),
861 TYPE_MAIN_VARIANT (TREE_TYPE (to))))
863 else if (utcode == VOID_TYPE && ufcode != OFFSET_TYPE
864 && ufcode != FUNCTION_TYPE)
866 from = build_pointer_type
867 (cp_build_qualified_type (void_type_node,
868 CP_TYPE_QUALS (TREE_TYPE (from))));
869 conv = build_conv (PTR_CONV, from, conv);
871 else if (ufcode == OFFSET_TYPE && utcode == OFFSET_TYPE)
873 tree fbase = TYPE_OFFSET_BASETYPE (TREE_TYPE (from));
874 tree tbase = TYPE_OFFSET_BASETYPE (TREE_TYPE (to));
876 if (DERIVED_FROM_P (fbase, tbase)
877 && (same_type_p
878 (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (from))),
879 TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (to))))))
881 from = build_offset_type (tbase, TREE_TYPE (TREE_TYPE (from)));
882 from = build_pointer_type (from);
883 conv = build_conv (PMEM_CONV, from, conv);
886 else if (IS_AGGR_TYPE (TREE_TYPE (from))
887 && IS_AGGR_TYPE (TREE_TYPE (to)))
889 if (DERIVED_FROM_P (TREE_TYPE (to), TREE_TYPE (from)))
891 from =
892 cp_build_qualified_type (TREE_TYPE (to),
893 CP_TYPE_QUALS (TREE_TYPE (from)));
894 from = build_pointer_type (from);
895 conv = build_conv (PTR_CONV, from, conv);
899 if (same_type_p (from, to))
900 /* OK */;
901 else if (comp_ptr_ttypes (TREE_TYPE (to), TREE_TYPE (from)))
902 conv = build_conv (QUAL_CONV, to, conv);
903 else if (expr && string_conv_p (to, expr, 0))
904 /* converting from string constant to char *. */
905 conv = build_conv (QUAL_CONV, to, conv);
906 else if (ptr_reasonably_similar (TREE_TYPE (to), TREE_TYPE (from)))
908 conv = build_conv (PTR_CONV, to, conv);
909 ICS_BAD_FLAG (conv) = 1;
911 else
912 return 0;
914 from = to;
916 else if (TYPE_PTRMEMFUNC_P (to) && TYPE_PTRMEMFUNC_P (from))
918 tree fromfn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (from));
919 tree tofn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (to));
920 tree fbase = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (fromfn)));
921 tree tbase = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (tofn)));
923 if (! DERIVED_FROM_P (fbase, tbase)
924 || ! same_type_p (TREE_TYPE (fromfn), TREE_TYPE (tofn))
925 || ! compparms (TREE_CHAIN (TYPE_ARG_TYPES (fromfn)),
926 TREE_CHAIN (TYPE_ARG_TYPES (tofn)))
927 || CP_TYPE_QUALS (fbase) != CP_TYPE_QUALS (tbase))
928 return 0;
930 from = cp_build_qualified_type (tbase, CP_TYPE_QUALS (fbase));
931 from = build_cplus_method_type (from, TREE_TYPE (fromfn),
932 TREE_CHAIN (TYPE_ARG_TYPES (fromfn)));
933 from = build_ptrmemfunc_type (build_pointer_type (from));
934 conv = build_conv (PMEM_CONV, from, conv);
936 else if (tcode == BOOLEAN_TYPE)
938 if (! (INTEGRAL_CODE_P (fcode) || fcode == REAL_TYPE
939 || fcode == POINTER_TYPE || TYPE_PTRMEMFUNC_P (from)))
940 return 0;
942 conv = build_conv (STD_CONV, to, conv);
943 if (fcode == POINTER_TYPE
944 || (TYPE_PTRMEMFUNC_P (from) && ICS_STD_RANK (conv) < PBOOL_RANK))
945 ICS_STD_RANK (conv) = PBOOL_RANK;
947 /* We don't check for ENUMERAL_TYPE here because there are no standard
948 conversions to enum type. */
949 else if (tcode == INTEGER_TYPE || tcode == BOOLEAN_TYPE
950 || tcode == REAL_TYPE)
952 if (! (INTEGRAL_CODE_P (fcode) || fcode == REAL_TYPE))
953 return 0;
954 conv = build_conv (STD_CONV, to, conv);
956 /* Give this a better rank if it's a promotion. */
957 if (to == type_promotes_to (from)
958 && ICS_STD_RANK (TREE_OPERAND (conv, 0)) <= PROMO_RANK)
959 ICS_STD_RANK (conv) = PROMO_RANK;
961 else if (IS_AGGR_TYPE (to) && IS_AGGR_TYPE (from)
962 && DERIVED_FROM_P (to, from))
964 if (TREE_CODE (conv) == RVALUE_CONV)
965 conv = TREE_OPERAND (conv, 0);
966 conv = build_conv (BASE_CONV, to, conv);
968 else
969 return 0;
971 return conv;
974 /* Returns the conversion path from type FROM to reference type TO for
975 purposes of reference binding. For lvalue binding, either pass a
976 reference type to FROM or an lvalue expression to EXPR.
978 Currently does not distinguish in the generated trees between binding to
979 an lvalue and a temporary. Should it? */
981 static tree
982 reference_binding (rto, rfrom, expr, flags)
983 tree rto, rfrom, expr;
984 int flags;
986 tree conv;
987 int lvalue = 1;
988 tree to = TREE_TYPE (rto);
989 tree from = rfrom;
990 int related;
992 if (TREE_CODE (to) == FUNCTION_TYPE && expr && type_unknown_p (expr))
994 expr = instantiate_type (to, expr, 0);
995 if (expr == error_mark_node)
996 return NULL_TREE;
997 from = TREE_TYPE (expr);
1000 if (TREE_CODE (from) == REFERENCE_TYPE)
1001 from = TREE_TYPE (from);
1002 else if (! expr || ! real_lvalue_p (expr))
1003 lvalue = 0;
1005 related = (same_type_p (TYPE_MAIN_VARIANT (to),
1006 TYPE_MAIN_VARIANT (from))
1007 || (IS_AGGR_TYPE (to) && IS_AGGR_TYPE (from)
1008 && DERIVED_FROM_P (to, from)));
1010 if (lvalue && related && at_least_as_qualified_p (to, from))
1012 conv = build1 (IDENTITY_CONV, from, expr);
1014 if (same_type_p (TYPE_MAIN_VARIANT (to),
1015 TYPE_MAIN_VARIANT (from)))
1016 conv = build_conv (REF_BIND, rto, conv);
1017 else
1019 conv = build_conv (REF_BIND, rto, conv);
1020 ICS_STD_RANK (conv) = STD_RANK;
1023 else
1024 conv = NULL_TREE;
1026 if (! conv)
1028 conv = standard_conversion (to, rfrom, expr);
1029 if (conv)
1031 conv = build_conv (REF_BIND, rto, conv);
1033 /* Bind directly to a base subobject of a class rvalue. Do it
1034 after building the conversion for proper handling of ICS_RANK. */
1035 if (TREE_CODE (TREE_OPERAND (conv, 0)) == BASE_CONV)
1036 TREE_OPERAND (conv, 0) = TREE_OPERAND (TREE_OPERAND (conv, 0), 0);
1038 if (conv
1039 && ((! (CP_TYPE_CONST_NON_VOLATILE_P (to)
1040 && (flags & LOOKUP_NO_TEMP_BIND) == 0))
1041 /* If T1 is reference-related to T2, cv1 must be the same
1042 cv-qualification as, or greater cv-qualification than,
1043 cv2; otherwise, the program is ill-formed. */
1044 || (related && !at_least_as_qualified_p (to, from))))
1045 ICS_BAD_FLAG (conv) = 1;
1048 return conv;
1051 /* Returns the implicit conversion sequence (see [over.ics]) from type FROM
1052 to type TO. The optional expression EXPR may affect the conversion.
1053 FLAGS are the usual overloading flags. Only LOOKUP_NO_CONVERSION is
1054 significant. */
1056 static tree
1057 implicit_conversion (to, from, expr, flags)
1058 tree to, from, expr;
1059 int flags;
1061 tree conv;
1062 struct z_candidate *cand;
1064 if (TREE_CODE (to) == REFERENCE_TYPE)
1065 conv = reference_binding (to, from, expr, flags);
1066 else
1067 conv = standard_conversion (to, from, expr);
1069 if (conv)
1071 else if (expr != NULL_TREE
1072 && (IS_AGGR_TYPE (non_reference (from))
1073 || IS_AGGR_TYPE (non_reference (to)))
1074 && (flags & LOOKUP_NO_CONVERSION) == 0)
1076 cand = build_user_type_conversion_1
1077 (to, expr, LOOKUP_ONLYCONVERTING);
1078 if (cand)
1079 conv = cand->second_conv;
1080 if ((! conv || ICS_BAD_FLAG (conv))
1081 && TREE_CODE (to) == REFERENCE_TYPE
1082 && (flags & LOOKUP_NO_TEMP_BIND) == 0)
1084 cand = build_user_type_conversion_1
1085 (TYPE_MAIN_VARIANT (TREE_TYPE (to)), expr, LOOKUP_ONLYCONVERTING);
1086 if (cand)
1088 if (!CP_TYPE_CONST_NON_VOLATILE_P (TREE_TYPE (to)))
1089 ICS_BAD_FLAG (cand->second_conv) = 1;
1090 if (!conv || (ICS_BAD_FLAG (conv)
1091 > ICS_BAD_FLAG (cand->second_conv)))
1092 conv = build_conv (REF_BIND, to, cand->second_conv);
1097 return conv;
1100 /* Add a new entry to the list of candidates. Used by the add_*_candidate
1101 functions. */
1103 static struct z_candidate *
1104 add_candidate (candidates, fn, convs, viable)
1105 struct z_candidate *candidates;
1106 tree fn, convs;
1107 int viable;
1109 struct z_candidate *cand
1110 = (struct z_candidate *) scratchalloc (sizeof (struct z_candidate));
1112 cand->fn = fn;
1113 cand->convs = convs;
1114 cand->second_conv = NULL_TREE;
1115 cand->viable = viable;
1116 cand->basetype_path = NULL_TREE;
1117 cand->template = NULL_TREE;
1118 cand->warnings = NULL_TREE;
1119 cand->next = candidates;
1121 return cand;
1124 /* Create an overload candidate for the function or method FN called with
1125 the argument list ARGLIST and add it to CANDIDATES. FLAGS is passed on
1126 to implicit_conversion. */
1128 static struct z_candidate *
1129 add_function_candidate (candidates, fn, arglist, flags)
1130 struct z_candidate *candidates;
1131 tree fn, arglist;
1132 int flags;
1134 tree parmlist = TYPE_ARG_TYPES (TREE_TYPE (fn));
1135 int i, len;
1136 tree convs;
1137 tree parmnode, argnode;
1138 int viable = 1;
1140 /* The `this' and `in_chrg' arguments to constructors are not considered
1141 in overload resolution. */
1142 if (DECL_CONSTRUCTOR_P (fn))
1144 parmlist = TREE_CHAIN (parmlist);
1145 arglist = TREE_CHAIN (arglist);
1146 if (TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (fn)))
1148 parmlist = TREE_CHAIN (parmlist);
1149 arglist = TREE_CHAIN (arglist);
1153 len = list_length (arglist);
1155 /* 13.3.2 - Viable functions [over.match.viable]
1156 First, to be a viable function, a candidate function shall have enough
1157 parameters to agree in number with the arguments in the list.
1159 We need to check this first; otherwise, checking the ICSes might cause
1160 us to produce an ill-formed template instantiation. */
1162 parmnode = parmlist;
1163 for (i = 0; i < len; ++i)
1165 if (parmnode == NULL_TREE || parmnode == void_list_node)
1166 break;
1167 parmnode = TREE_CHAIN (parmnode);
1170 if (i < len && parmnode)
1171 viable = 0;
1173 /* Make sure there are default args for the rest of the parms. */
1174 else for (; parmnode && parmnode != void_list_node;
1175 parmnode = TREE_CHAIN (parmnode))
1176 if (! TREE_PURPOSE (parmnode))
1178 viable = 0;
1179 break;
1182 if (! viable)
1183 goto out;
1185 /* Second, for F to be a viable function, there shall exist for each
1186 argument an implicit conversion sequence that converts that argument
1187 to the corresponding parameter of F. */
1189 parmnode = parmlist;
1190 argnode = arglist;
1191 convs = make_scratch_vec (len);
1193 for (i = 0; i < len; ++i)
1195 tree arg = TREE_VALUE (argnode);
1196 tree argtype = lvalue_type (arg);
1197 tree t;
1199 if (parmnode == void_list_node)
1200 break;
1202 if (parmnode)
1204 tree parmtype = TREE_VALUE (parmnode);
1206 /* [over.match.funcs] For conversion functions, the function is
1207 considered to be a member of the class of the implicit object
1208 argument for the purpose of defining the type of the implicit
1209 object parameter.
1211 Since build_over_call ignores the ICS for the `this' parameter,
1212 we can just change the parm type. */
1213 if (DECL_CONV_FN_P (fn) && i == 0)
1215 parmtype
1216 = build_qualified_type (TREE_TYPE (argtype),
1217 TYPE_QUALS (TREE_TYPE (parmtype)));
1218 parmtype = build_pointer_type (parmtype);
1221 t = implicit_conversion (parmtype, argtype, arg, flags);
1223 else
1225 t = build1 (IDENTITY_CONV, argtype, arg);
1226 ICS_ELLIPSIS_FLAG (t) = 1;
1229 if (i == 0 && t && TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE
1230 && ! DECL_CONSTRUCTOR_P (fn))
1231 ICS_THIS_FLAG (t) = 1;
1233 TREE_VEC_ELT (convs, i) = t;
1234 if (! t)
1236 viable = 0;
1237 break;
1240 if (ICS_BAD_FLAG (t))
1241 viable = -1;
1243 if (parmnode)
1244 parmnode = TREE_CHAIN (parmnode);
1245 argnode = TREE_CHAIN (argnode);
1248 out:
1249 return add_candidate (candidates, fn, convs, viable);
1252 /* Create an overload candidate for the conversion function FN which will
1253 be invoked for expression OBJ, producing a pointer-to-function which
1254 will in turn be called with the argument list ARGLIST, and add it to
1255 CANDIDATES. FLAGS is passed on to implicit_conversion.
1257 Actually, we don't really care about FN; we care about the type it
1258 converts to. There may be multiple conversion functions that will
1259 convert to that type, and we rely on build_user_type_conversion_1 to
1260 choose the best one; so when we create our candidate, we record the type
1261 instead of the function. */
1263 static struct z_candidate *
1264 add_conv_candidate (candidates, fn, obj, arglist)
1265 struct z_candidate *candidates;
1266 tree fn, obj, arglist;
1268 tree totype = TREE_TYPE (TREE_TYPE (fn));
1269 tree parmlist = TYPE_ARG_TYPES (TREE_TYPE (totype));
1270 int i, len = list_length (arglist) + 1;
1271 tree convs = make_scratch_vec (len);
1272 tree parmnode = parmlist;
1273 tree argnode = arglist;
1274 int viable = 1;
1275 int flags = LOOKUP_NORMAL;
1277 /* Don't bother looking up the same type twice. */
1278 if (candidates && candidates->fn == totype)
1279 return candidates;
1281 for (i = 0; i < len; ++i)
1283 tree arg = i == 0 ? obj : TREE_VALUE (argnode);
1284 tree argtype = lvalue_type (arg);
1285 tree t;
1287 if (i == 0)
1288 t = implicit_conversion (totype, argtype, arg, flags);
1289 else if (parmnode == void_list_node)
1290 break;
1291 else if (parmnode)
1292 t = implicit_conversion (TREE_VALUE (parmnode), argtype, arg, flags);
1293 else
1295 t = build1 (IDENTITY_CONV, argtype, arg);
1296 ICS_ELLIPSIS_FLAG (t) = 1;
1299 TREE_VEC_ELT (convs, i) = t;
1300 if (! t)
1301 break;
1303 if (ICS_BAD_FLAG (t))
1304 viable = -1;
1306 if (i == 0)
1307 continue;
1309 if (parmnode)
1310 parmnode = TREE_CHAIN (parmnode);
1311 argnode = TREE_CHAIN (argnode);
1314 if (i < len)
1315 viable = 0;
1317 for (; parmnode && parmnode != void_list_node;
1318 parmnode = TREE_CHAIN (parmnode))
1319 if (! TREE_PURPOSE (parmnode))
1321 viable = 0;
1322 break;
1325 return add_candidate (candidates, totype, convs, viable);
1328 static struct z_candidate *
1329 build_builtin_candidate (candidates, fnname, type1, type2,
1330 args, argtypes, flags)
1331 struct z_candidate *candidates;
1332 tree fnname, type1, type2, *args, *argtypes;
1333 int flags;
1336 tree t, convs;
1337 int viable = 1, i;
1338 tree types[2];
1340 types[0] = type1;
1341 types[1] = type2;
1343 convs = make_scratch_vec (args[2] ? 3 : (args[1] ? 2 : 1));
1345 for (i = 0; i < 2; ++i)
1347 if (! args[i])
1348 break;
1350 t = implicit_conversion (types[i], argtypes[i], args[i], flags);
1351 if (! t)
1353 viable = 0;
1354 /* We need something for printing the candidate. */
1355 t = build1 (IDENTITY_CONV, types[i], NULL_TREE);
1357 else if (ICS_BAD_FLAG (t))
1358 viable = 0;
1359 TREE_VEC_ELT (convs, i) = t;
1362 /* For COND_EXPR we rearranged the arguments; undo that now. */
1363 if (args[2])
1365 TREE_VEC_ELT (convs, 2) = TREE_VEC_ELT (convs, 1);
1366 TREE_VEC_ELT (convs, 1) = TREE_VEC_ELT (convs, 0);
1367 t = implicit_conversion (boolean_type_node, argtypes[2], args[2], flags);
1368 if (t)
1369 TREE_VEC_ELT (convs, 0) = t;
1370 else
1371 viable = 0;
1374 return add_candidate (candidates, fnname, convs, viable);
1377 static int
1378 is_complete (t)
1379 tree t;
1381 return TYPE_SIZE (complete_type (t)) != NULL_TREE;
1384 /* Create any builtin operator overload candidates for the operator in
1385 question given the converted operand types TYPE1 and TYPE2. The other
1386 args are passed through from add_builtin_candidates to
1387 build_builtin_candidate. */
1389 static struct z_candidate *
1390 add_builtin_candidate (candidates, code, code2, fnname, type1, type2,
1391 args, argtypes, flags)
1392 struct z_candidate *candidates;
1393 enum tree_code code, code2;
1394 tree fnname, type1, type2, *args, *argtypes;
1395 int flags;
1397 switch (code)
1399 case POSTINCREMENT_EXPR:
1400 case POSTDECREMENT_EXPR:
1401 args[1] = integer_zero_node;
1402 type2 = integer_type_node;
1403 break;
1404 default:
1405 break;
1408 switch (code)
1411 /* 4 For every pair T, VQ), where T is an arithmetic or enumeration type,
1412 and VQ is either volatile or empty, there exist candidate operator
1413 functions of the form
1414 VQ T& operator++(VQ T&);
1415 T operator++(VQ T&, int);
1416 5 For every pair T, VQ), where T is an enumeration type or an arithmetic
1417 type other than bool, and VQ is either volatile or empty, there exist
1418 candidate operator functions of the form
1419 VQ T& operator--(VQ T&);
1420 T operator--(VQ T&, int);
1421 6 For every pair T, VQ), where T is a cv-qualified or cv-unqualified
1422 complete object type, and VQ is either volatile or empty, there exist
1423 candidate operator functions of the form
1424 T*VQ& operator++(T*VQ&);
1425 T*VQ& operator--(T*VQ&);
1426 T* operator++(T*VQ&, int);
1427 T* operator--(T*VQ&, int); */
1429 case POSTDECREMENT_EXPR:
1430 case PREDECREMENT_EXPR:
1431 if (TREE_CODE (type1) == BOOLEAN_TYPE)
1432 return candidates;
1433 case POSTINCREMENT_EXPR:
1434 case PREINCREMENT_EXPR:
1435 if ((ARITHMETIC_TYPE_P (type1) && TREE_CODE (type1) != ENUMERAL_TYPE)
1436 || TYPE_PTROB_P (type1))
1438 type1 = build_reference_type (type1);
1439 break;
1441 return candidates;
1443 /* 7 For every cv-qualified or cv-unqualified complete object type T, there
1444 exist candidate operator functions of the form
1446 T& operator*(T*);
1448 8 For every function type T, there exist candidate operator functions of
1449 the form
1450 T& operator*(T*); */
1452 case INDIRECT_REF:
1453 if (TREE_CODE (type1) == POINTER_TYPE
1454 && (TYPE_PTROB_P (type1)
1455 || TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE))
1456 break;
1457 return candidates;
1459 /* 9 For every type T, there exist candidate operator functions of the form
1460 T* operator+(T*);
1462 10For every promoted arithmetic type T, there exist candidate operator
1463 functions of the form
1464 T operator+(T);
1465 T operator-(T); */
1467 case CONVERT_EXPR: /* unary + */
1468 if (TREE_CODE (type1) == POINTER_TYPE
1469 && TREE_CODE (TREE_TYPE (type1)) != OFFSET_TYPE)
1470 break;
1471 case NEGATE_EXPR:
1472 if (ARITHMETIC_TYPE_P (type1))
1473 break;
1474 return candidates;
1476 /* 11For every promoted integral type T, there exist candidate operator
1477 functions of the form
1478 T operator~(T); */
1480 case BIT_NOT_EXPR:
1481 if (INTEGRAL_TYPE_P (type1))
1482 break;
1483 return candidates;
1485 /* 12For every quintuple C1, C2, T, CV1, CV2), where C2 is a class type, C1
1486 is the same type as C2 or is a derived class of C2, T is a complete
1487 object type or a function type, and CV1 and CV2 are cv-qualifier-seqs,
1488 there exist candidate operator functions of the form
1489 CV12 T& operator->*(CV1 C1*, CV2 T C2::*);
1490 where CV12 is the union of CV1 and CV2. */
1492 case MEMBER_REF:
1493 if (TREE_CODE (type1) == POINTER_TYPE
1494 && (TYPE_PTRMEMFUNC_P (type2) || TYPE_PTRMEM_P (type2)))
1496 tree c1 = TREE_TYPE (type1);
1497 tree c2 = (TYPE_PTRMEMFUNC_P (type2)
1498 ? TYPE_METHOD_BASETYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (type2)))
1499 : TYPE_OFFSET_BASETYPE (TREE_TYPE (type2)));
1501 if (IS_AGGR_TYPE (c1) && DERIVED_FROM_P (c2, c1)
1502 && (TYPE_PTRMEMFUNC_P (type2)
1503 || is_complete (TREE_TYPE (TREE_TYPE (type2)))))
1504 break;
1506 return candidates;
1508 /* 13For every pair of promoted arithmetic types L and R, there exist can-
1509 didate operator functions of the form
1510 LR operator*(L, R);
1511 LR operator/(L, R);
1512 LR operator+(L, R);
1513 LR operator-(L, R);
1514 bool operator<(L, R);
1515 bool operator>(L, R);
1516 bool operator<=(L, R);
1517 bool operator>=(L, R);
1518 bool operator==(L, R);
1519 bool operator!=(L, R);
1520 where LR is the result of the usual arithmetic conversions between
1521 types L and R.
1523 14For every pair of types T and I, where T is a cv-qualified or cv-
1524 unqualified complete object type and I is a promoted integral type,
1525 there exist candidate operator functions of the form
1526 T* operator+(T*, I);
1527 T& operator[](T*, I);
1528 T* operator-(T*, I);
1529 T* operator+(I, T*);
1530 T& operator[](I, T*);
1532 15For every T, where T is a pointer to complete object type, there exist
1533 candidate operator functions of the form112)
1534 ptrdiff_t operator-(T, T);
1536 16For every pointer type T, there exist candidate operator functions of
1537 the form
1538 bool operator<(T, T);
1539 bool operator>(T, T);
1540 bool operator<=(T, T);
1541 bool operator>=(T, T);
1542 bool operator==(T, T);
1543 bool operator!=(T, T);
1545 17For every pointer to member type T, there exist candidate operator
1546 functions of the form
1547 bool operator==(T, T);
1548 bool operator!=(T, T); */
1550 case MINUS_EXPR:
1551 if (TYPE_PTROB_P (type1) && TYPE_PTROB_P (type2))
1552 break;
1553 if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2))
1555 type2 = ptrdiff_type_node;
1556 break;
1558 case MULT_EXPR:
1559 case TRUNC_DIV_EXPR:
1560 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1561 break;
1562 return candidates;
1564 case EQ_EXPR:
1565 case NE_EXPR:
1566 if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2))
1567 || (TYPE_PTRMEM_P (type1) && TYPE_PTRMEM_P (type2)))
1568 break;
1569 if ((TYPE_PTRMEMFUNC_P (type1) || TYPE_PTRMEM_P (type1))
1570 && null_ptr_cst_p (args[1]))
1572 type2 = type1;
1573 break;
1575 if ((TYPE_PTRMEMFUNC_P (type2) || TYPE_PTRMEM_P (type2))
1576 && null_ptr_cst_p (args[0]))
1578 type1 = type2;
1579 break;
1581 case LT_EXPR:
1582 case GT_EXPR:
1583 case LE_EXPR:
1584 case GE_EXPR:
1585 case MAX_EXPR:
1586 case MIN_EXPR:
1587 if ((ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1588 || (TYPE_PTR_P (type1) && TYPE_PTR_P (type2)))
1589 break;
1590 if (TYPE_PTR_P (type1) && null_ptr_cst_p (args[1]))
1592 type2 = type1;
1593 break;
1595 if (null_ptr_cst_p (args[0]) && TYPE_PTR_P (type2))
1597 type1 = type2;
1598 break;
1600 return candidates;
1602 case PLUS_EXPR:
1603 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1604 break;
1605 case ARRAY_REF:
1606 if (INTEGRAL_TYPE_P (type1) && TYPE_PTROB_P (type2))
1608 type1 = ptrdiff_type_node;
1609 break;
1611 if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2))
1613 type2 = ptrdiff_type_node;
1614 break;
1616 return candidates;
1618 /* 18For every pair of promoted integral types L and R, there exist candi-
1619 date operator functions of the form
1620 LR operator%(L, R);
1621 LR operator&(L, R);
1622 LR operator^(L, R);
1623 LR operator|(L, R);
1624 L operator<<(L, R);
1625 L operator>>(L, R);
1626 where LR is the result of the usual arithmetic conversions between
1627 types L and R. */
1629 case TRUNC_MOD_EXPR:
1630 case BIT_AND_EXPR:
1631 case BIT_IOR_EXPR:
1632 case BIT_XOR_EXPR:
1633 case LSHIFT_EXPR:
1634 case RSHIFT_EXPR:
1635 if (INTEGRAL_TYPE_P (type1) && INTEGRAL_TYPE_P (type2))
1636 break;
1637 return candidates;
1639 /* 19For every triple L, VQ, R), where L is an arithmetic or enumeration
1640 type, VQ is either volatile or empty, and R is a promoted arithmetic
1641 type, there exist candidate operator functions of the form
1642 VQ L& operator=(VQ L&, R);
1643 VQ L& operator*=(VQ L&, R);
1644 VQ L& operator/=(VQ L&, R);
1645 VQ L& operator+=(VQ L&, R);
1646 VQ L& operator-=(VQ L&, R);
1648 20For every pair T, VQ), where T is any type and VQ is either volatile
1649 or empty, there exist candidate operator functions of the form
1650 T*VQ& operator=(T*VQ&, T*);
1652 21For every pair T, VQ), where T is a pointer to member type and VQ is
1653 either volatile or empty, there exist candidate operator functions of
1654 the form
1655 VQ T& operator=(VQ T&, T);
1657 22For every triple T, VQ, I), where T is a cv-qualified or cv-
1658 unqualified complete object type, VQ is either volatile or empty, and
1659 I is a promoted integral type, there exist candidate operator func-
1660 tions of the form
1661 T*VQ& operator+=(T*VQ&, I);
1662 T*VQ& operator-=(T*VQ&, I);
1664 23For every triple L, VQ, R), where L is an integral or enumeration
1665 type, VQ is either volatile or empty, and R is a promoted integral
1666 type, there exist candidate operator functions of the form
1668 VQ L& operator%=(VQ L&, R);
1669 VQ L& operator<<=(VQ L&, R);
1670 VQ L& operator>>=(VQ L&, R);
1671 VQ L& operator&=(VQ L&, R);
1672 VQ L& operator^=(VQ L&, R);
1673 VQ L& operator|=(VQ L&, R); */
1675 case MODIFY_EXPR:
1676 switch (code2)
1678 case PLUS_EXPR:
1679 case MINUS_EXPR:
1680 if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2))
1682 type2 = ptrdiff_type_node;
1683 break;
1685 case MULT_EXPR:
1686 case TRUNC_DIV_EXPR:
1687 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1688 break;
1689 return candidates;
1691 case TRUNC_MOD_EXPR:
1692 case BIT_AND_EXPR:
1693 case BIT_IOR_EXPR:
1694 case BIT_XOR_EXPR:
1695 case LSHIFT_EXPR:
1696 case RSHIFT_EXPR:
1697 if (INTEGRAL_TYPE_P (type1) && INTEGRAL_TYPE_P (type2))
1698 break;
1699 return candidates;
1701 case NOP_EXPR:
1702 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1703 break;
1704 if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2))
1705 || (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
1706 || (TYPE_PTRMEM_P (type1) && TYPE_PTRMEM_P (type2))
1707 || ((TYPE_PTRMEMFUNC_P (type1)
1708 || TREE_CODE (type1) == POINTER_TYPE)
1709 && null_ptr_cst_p (args[1])))
1711 type2 = type1;
1712 break;
1714 return candidates;
1716 default:
1717 my_friendly_abort (367);
1719 type1 = build_reference_type (type1);
1720 break;
1722 case COND_EXPR:
1723 /* Kludge around broken overloading rules whereby
1724 bool ? const char& : enum is ambiguous
1725 (between int and const char&). */
1726 flags |= LOOKUP_NO_TEMP_BIND;
1728 /* Extension: Support ?: of enumeral type. Hopefully this will not
1729 be an extension for long. */
1730 if (TREE_CODE (type1) == ENUMERAL_TYPE && type1 == type2)
1731 break;
1732 else if (TREE_CODE (type1) == ENUMERAL_TYPE
1733 || TREE_CODE (type2) == ENUMERAL_TYPE)
1734 return candidates;
1735 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1736 break;
1737 if (TREE_CODE (type1) == TREE_CODE (type2)
1738 && (TREE_CODE (type1) == REFERENCE_TYPE
1739 || TREE_CODE (type1) == POINTER_TYPE
1740 || TYPE_PTRMEMFUNC_P (type1)
1741 || IS_AGGR_TYPE (type1)))
1742 break;
1743 if (TREE_CODE (type1) == REFERENCE_TYPE
1744 || TREE_CODE (type2) == REFERENCE_TYPE)
1745 return candidates;
1746 if (((TYPE_PTRMEMFUNC_P (type1) || TREE_CODE (type1) == POINTER_TYPE)
1747 && null_ptr_cst_p (args[1]))
1748 || IS_AGGR_TYPE (type1))
1750 type2 = type1;
1751 break;
1753 if (((TYPE_PTRMEMFUNC_P (type2) || TREE_CODE (type2) == POINTER_TYPE)
1754 && null_ptr_cst_p (args[0]))
1755 || IS_AGGR_TYPE (type2))
1757 type1 = type2;
1758 break;
1760 return candidates;
1762 default:
1763 my_friendly_abort (367);
1766 /* If we're dealing with two pointer types, we need candidates
1767 for both of them. */
1768 if (type2 && type1 != type2
1769 && TREE_CODE (type1) == TREE_CODE (type2)
1770 && (TREE_CODE (type1) == REFERENCE_TYPE
1771 || (TREE_CODE (type1) == POINTER_TYPE
1772 && TYPE_PTRMEM_P (type1) == TYPE_PTRMEM_P (type2))
1773 || TYPE_PTRMEMFUNC_P (type1)
1774 || IS_AGGR_TYPE (type1)))
1776 candidates = build_builtin_candidate
1777 (candidates, fnname, type1, type1, args, argtypes, flags);
1778 return build_builtin_candidate
1779 (candidates, fnname, type2, type2, args, argtypes, flags);
1782 return build_builtin_candidate
1783 (candidates, fnname, type1, type2, args, argtypes, flags);
1786 tree
1787 type_decays_to (type)
1788 tree type;
1790 if (TREE_CODE (type) == ARRAY_TYPE)
1791 return build_pointer_type (TREE_TYPE (type));
1792 if (TREE_CODE (type) == FUNCTION_TYPE)
1793 return build_pointer_type (type);
1794 return type;
1797 /* There are three conditions of builtin candidates:
1799 1) bool-taking candidates. These are the same regardless of the input.
1800 2) pointer-pair taking candidates. These are generated for each type
1801 one of the input types converts to.
1802 3) arithmetic candidates. According to the WP, we should generate
1803 all of these, but I'm trying not to... */
1805 static struct z_candidate *
1806 add_builtin_candidates (candidates, code, code2, fnname, args, flags)
1807 struct z_candidate *candidates;
1808 enum tree_code code, code2;
1809 tree fnname, *args;
1810 int flags;
1812 int ref1, i;
1813 tree type, argtypes[3], types[2];
1815 for (i = 0; i < 3; ++i)
1817 if (args[i])
1818 argtypes[i] = lvalue_type (args[i]);
1819 else
1820 argtypes[i] = NULL_TREE;
1823 switch (code)
1825 /* 4 For every pair T, VQ), where T is an arithmetic or enumeration type,
1826 and VQ is either volatile or empty, there exist candidate operator
1827 functions of the form
1828 VQ T& operator++(VQ T&); */
1830 case POSTINCREMENT_EXPR:
1831 case PREINCREMENT_EXPR:
1832 case POSTDECREMENT_EXPR:
1833 case PREDECREMENT_EXPR:
1834 case MODIFY_EXPR:
1835 ref1 = 1;
1836 break;
1838 /* 24There also exist candidate operator functions of the form
1839 bool operator!(bool);
1840 bool operator&&(bool, bool);
1841 bool operator||(bool, bool); */
1843 case TRUTH_NOT_EXPR:
1844 return build_builtin_candidate
1845 (candidates, fnname, boolean_type_node,
1846 NULL_TREE, args, argtypes, flags);
1848 case TRUTH_ORIF_EXPR:
1849 case TRUTH_ANDIF_EXPR:
1850 return build_builtin_candidate
1851 (candidates, fnname, boolean_type_node,
1852 boolean_type_node, args, argtypes, flags);
1854 case ADDR_EXPR:
1855 case COMPOUND_EXPR:
1856 case COMPONENT_REF:
1857 return candidates;
1859 default:
1860 ref1 = 0;
1863 types[0] = types[1] = NULL_TREE;
1865 for (i = 0; i < 2; ++i)
1867 if (! args[i])
1869 else if (IS_AGGR_TYPE (argtypes[i]))
1871 tree convs;
1873 if (i == 0 && code == MODIFY_EXPR && code2 == NOP_EXPR)
1874 return candidates;
1876 convs = lookup_conversions (argtypes[i]);
1878 if (code == COND_EXPR)
1880 if (real_lvalue_p (args[i]))
1881 types[i] = scratch_tree_cons
1882 (NULL_TREE, build_reference_type (argtypes[i]), types[i]);
1884 types[i] = scratch_tree_cons
1885 (NULL_TREE, TYPE_MAIN_VARIANT (argtypes[i]), types[i]);
1888 else if (! convs)
1889 return candidates;
1891 for (; convs; convs = TREE_CHAIN (convs))
1893 type = TREE_TYPE (TREE_TYPE (OVL_CURRENT (TREE_VALUE (convs))));
1895 if (i == 0 && ref1
1896 && (TREE_CODE (type) != REFERENCE_TYPE
1897 || CP_TYPE_CONST_P (TREE_TYPE (type))))
1898 continue;
1900 if (code == COND_EXPR && TREE_CODE (type) == REFERENCE_TYPE)
1901 types[i] = scratch_tree_cons (NULL_TREE, type, types[i]);
1903 type = non_reference (type);
1904 if (i != 0 || ! ref1)
1906 type = TYPE_MAIN_VARIANT (type_decays_to (type));
1907 if (code == COND_EXPR && TREE_CODE (type) == ENUMERAL_TYPE)
1908 types[i] = scratch_tree_cons (NULL_TREE, type, types[i]);
1909 if (INTEGRAL_TYPE_P (type))
1910 type = type_promotes_to (type);
1913 if (! value_member (type, types[i]))
1914 types[i] = scratch_tree_cons (NULL_TREE, type, types[i]);
1917 else
1919 if (code == COND_EXPR && real_lvalue_p (args[i]))
1920 types[i] = scratch_tree_cons
1921 (NULL_TREE, build_reference_type (argtypes[i]), types[i]);
1922 type = non_reference (argtypes[i]);
1923 if (i != 0 || ! ref1)
1925 type = TYPE_MAIN_VARIANT (type_decays_to (type));
1926 if (code == COND_EXPR && TREE_CODE (type) == ENUMERAL_TYPE)
1927 types[i] = scratch_tree_cons (NULL_TREE, type, types[i]);
1928 if (INTEGRAL_TYPE_P (type))
1929 type = type_promotes_to (type);
1931 types[i] = scratch_tree_cons (NULL_TREE, type, types[i]);
1935 for (; types[0]; types[0] = TREE_CHAIN (types[0]))
1937 if (types[1])
1938 for (type = types[1]; type; type = TREE_CHAIN (type))
1939 candidates = add_builtin_candidate
1940 (candidates, code, code2, fnname, TREE_VALUE (types[0]),
1941 TREE_VALUE (type), args, argtypes, flags);
1942 else
1943 candidates = add_builtin_candidate
1944 (candidates, code, code2, fnname, TREE_VALUE (types[0]),
1945 NULL_TREE, args, argtypes, flags);
1948 return candidates;
1952 /* If TMPL can be successfully instantiated as indicated by
1953 EXPLICIT_TARGS and ARGLIST, adds the instantiation to CANDIDATES.
1955 TMPL is the template. EXPLICIT_TARGS are any explicit template
1956 arguments. ARGLIST is the arguments provided at the call-site.
1957 The RETURN_TYPE is the desired type for conversion operators. If
1958 OBJ is NULL_TREE, FLAGS are as for add_function_candidate. If an
1959 OBJ is supplied, FLAGS are ignored, and OBJ is as for
1960 add_conv_candidate. */
1962 static struct z_candidate*
1963 add_template_candidate_real (candidates, tmpl, explicit_targs,
1964 arglist, return_type, flags,
1965 obj, strict)
1966 struct z_candidate *candidates;
1967 tree tmpl, explicit_targs, arglist, return_type;
1968 int flags;
1969 tree obj;
1970 unification_kind_t strict;
1972 int ntparms = DECL_NTPARMS (tmpl);
1973 tree targs = make_scratch_vec (ntparms);
1974 struct z_candidate *cand;
1975 int i;
1976 tree fn;
1978 i = fn_type_unification (tmpl, explicit_targs, targs, arglist,
1979 return_type, strict);
1981 if (i != 0)
1982 return candidates;
1984 fn = instantiate_template (tmpl, targs);
1985 if (fn == error_mark_node)
1986 return candidates;
1988 if (obj != NULL_TREE)
1989 /* Aha, this is a conversion function. */
1990 cand = add_conv_candidate (candidates, fn, obj, arglist);
1991 else
1992 cand = add_function_candidate (candidates, fn, arglist, flags);
1993 if (DECL_TI_TEMPLATE (fn) != tmpl)
1994 /* This situation can occur if a member template of a template
1995 class is specialized. Then, instantiate_template might return
1996 an instantiation of the specialization, in which case the
1997 DECL_TI_TEMPLATE field will point at the original
1998 specialization. For example:
2000 template <class T> struct S { template <class U> void f(U);
2001 template <> void f(int) {}; };
2002 S<double> sd;
2003 sd.f(3);
2005 Here, TMPL will be template <class U> S<double>::f(U).
2006 And, instantiate template will give us the specialization
2007 template <> S<double>::f(int). But, the DECL_TI_TEMPLATE field
2008 for this will point at template <class T> template <> S<T>::f(int),
2009 so that we can find the definition. For the purposes of
2010 overload resolution, however, we want the original TMPL. */
2011 cand->template = tree_cons (tmpl, targs, NULL_TREE);
2012 else
2013 cand->template = DECL_TEMPLATE_INFO (fn);
2015 return cand;
2019 static struct z_candidate *
2020 add_template_candidate (candidates, tmpl, explicit_targs,
2021 arglist, return_type, flags, strict)
2022 struct z_candidate *candidates;
2023 tree tmpl, explicit_targs, arglist, return_type;
2024 int flags;
2025 unification_kind_t strict;
2027 return
2028 add_template_candidate_real (candidates, tmpl, explicit_targs,
2029 arglist, return_type, flags,
2030 NULL_TREE, strict);
2034 static struct z_candidate *
2035 add_template_conv_candidate (candidates, tmpl, obj, arglist, return_type)
2036 struct z_candidate *candidates;
2037 tree tmpl, obj, arglist, return_type;
2039 return
2040 add_template_candidate_real (candidates, tmpl, NULL_TREE, arglist,
2041 return_type, 0, obj, DEDUCE_CONV);
2045 static int
2046 any_viable (cands)
2047 struct z_candidate *cands;
2049 for (; cands; cands = cands->next)
2050 if (pedantic ? cands->viable == 1 : cands->viable)
2051 return 1;
2052 return 0;
2055 static struct z_candidate *
2056 splice_viable (cands)
2057 struct z_candidate *cands;
2059 struct z_candidate **p = &cands;
2061 for (; *p; )
2063 if (pedantic ? (*p)->viable == 1 : (*p)->viable)
2064 p = &((*p)->next);
2065 else
2066 *p = (*p)->next;
2069 return cands;
2072 static tree
2073 build_this (obj)
2074 tree obj;
2076 /* Fix this to work on non-lvalues. */
2077 if (IS_SIGNATURE_POINTER (TREE_TYPE (obj))
2078 || IS_SIGNATURE_REFERENCE (TREE_TYPE (obj)))
2079 return obj;
2080 else
2081 return build_unary_op (ADDR_EXPR, obj, 0);
2084 static void
2085 print_z_candidates (candidates)
2086 struct z_candidate *candidates;
2088 char *str = "candidates are:";
2089 for (; candidates; candidates = candidates->next)
2091 if (TREE_CODE (candidates->fn) == IDENTIFIER_NODE)
2093 if (candidates->fn == ansi_opname [COND_EXPR])
2094 cp_error ("%s %D(%T, %T, %T) <builtin>", str, candidates->fn,
2095 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 0)),
2096 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 1)),
2097 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 2)));
2098 else if (TREE_VEC_LENGTH (candidates->convs) == 2)
2099 cp_error ("%s %D(%T, %T) <builtin>", str, candidates->fn,
2100 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 0)),
2101 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 1)));
2102 else
2103 cp_error ("%s %D(%T) <builtin>", str, candidates->fn,
2104 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 0)));
2106 else if (TYPE_P (candidates->fn))
2107 cp_error ("%s %T <conversion>", str, candidates->fn);
2108 else
2109 cp_error_at ("%s %+#D%s", str, candidates->fn,
2110 candidates->viable == -1 ? " <near match>" : "");
2111 str = " ";
2115 /* Returns the best overload candidate to perform the requested
2116 conversion. This function is used for three the overloading situations
2117 described in [over.match.copy], [over.match.conv], and [over.match.ref].
2118 If TOTYPE is a REFERENCE_TYPE, we're trying to find an lvalue binding as
2119 per [dcl.init.ref], so we ignore temporary bindings. */
2121 static struct z_candidate *
2122 build_user_type_conversion_1 (totype, expr, flags)
2123 tree totype, expr;
2124 int flags;
2126 struct z_candidate *candidates, *cand;
2127 tree fromtype = TREE_TYPE (expr);
2128 tree ctors = NULL_TREE, convs = NULL_TREE, *p;
2129 tree args = NULL_TREE;
2130 tree templates = NULL_TREE;
2132 if (IS_AGGR_TYPE (totype))
2133 ctors = lookup_fnfields (TYPE_BINFO (totype), ctor_identifier, 0);
2134 if (IS_AGGR_TYPE (fromtype)
2135 && (! IS_AGGR_TYPE (totype) || ! DERIVED_FROM_P (totype, fromtype)))
2136 convs = lookup_conversions (fromtype);
2138 candidates = 0;
2139 flags |= LOOKUP_NO_CONVERSION;
2141 if (ctors)
2143 tree t = build_int_2 (0, 0);
2144 TREE_TYPE (t) = build_pointer_type (totype);
2145 args = build_scratch_list (NULL_TREE, expr);
2146 if (TYPE_USES_VIRTUAL_BASECLASSES (totype))
2147 args = scratch_tree_cons (NULL_TREE, integer_one_node, args);
2148 args = scratch_tree_cons (NULL_TREE, t, args);
2150 ctors = TREE_VALUE (ctors);
2152 for (; ctors; ctors = OVL_NEXT (ctors))
2154 tree ctor = OVL_CURRENT (ctors);
2155 if (DECL_NONCONVERTING_P (ctor))
2156 continue;
2158 if (TREE_CODE (ctor) == TEMPLATE_DECL)
2160 templates = scratch_tree_cons (NULL_TREE, ctor, templates);
2161 candidates =
2162 add_template_candidate (candidates, ctor,
2163 NULL_TREE, args, NULL_TREE, flags,
2164 DEDUCE_CALL);
2166 else
2167 candidates = add_function_candidate (candidates, ctor,
2168 args, flags);
2170 if (candidates)
2172 candidates->second_conv = build1 (IDENTITY_CONV, totype, NULL_TREE);
2173 candidates->basetype_path = TYPE_BINFO (totype);
2177 if (convs)
2178 args = build_scratch_list (NULL_TREE, build_this (expr));
2180 for (; convs; convs = TREE_CHAIN (convs))
2182 tree fns = TREE_VALUE (convs);
2183 int convflags = LOOKUP_NO_CONVERSION;
2184 tree ics;
2186 /* If we are called to convert to a reference type, we are trying to
2187 find an lvalue binding, so don't even consider temporaries. If
2188 we don't find an lvalue binding, the caller will try again to
2189 look for a temporary binding. */
2190 if (TREE_CODE (totype) == REFERENCE_TYPE)
2191 convflags |= LOOKUP_NO_TEMP_BIND;
2193 if (TREE_CODE (OVL_CURRENT (fns)) != TEMPLATE_DECL)
2194 ics = implicit_conversion
2195 (totype, TREE_TYPE (TREE_TYPE (OVL_CURRENT (fns))), 0, convflags);
2196 else
2197 /* We can't compute this yet. */
2198 ics = error_mark_node;
2200 if (TREE_CODE (totype) == REFERENCE_TYPE && ics && ICS_BAD_FLAG (ics))
2201 /* ignore the near match. */;
2202 else if (ics)
2203 for (; fns; fns = OVL_NEXT (fns))
2205 tree fn = OVL_CURRENT (fns);
2206 struct z_candidate *old_candidates = candidates;
2208 if (TREE_CODE (fn) == TEMPLATE_DECL)
2210 templates = scratch_tree_cons (NULL_TREE, fn, templates);
2211 candidates =
2212 add_template_candidate (candidates, fn, NULL_TREE,
2213 args, totype, flags,
2214 DEDUCE_CONV);
2216 else
2217 candidates = add_function_candidate (candidates, fn,
2218 args, flags);
2220 if (candidates != old_candidates)
2222 if (TREE_CODE (fn) == TEMPLATE_DECL)
2223 ics = implicit_conversion
2224 (totype, TREE_TYPE (TREE_TYPE (candidates->fn)),
2225 0, convflags);
2227 candidates->second_conv = ics;
2228 candidates->basetype_path = TREE_PURPOSE (convs);
2230 if (ics == NULL_TREE)
2231 candidates->viable = 0;
2232 else if (candidates->viable == 1 && ICS_BAD_FLAG (ics))
2233 candidates->viable = -1;
2238 if (! any_viable (candidates))
2240 #if 0
2241 if (flags & LOOKUP_COMPLAIN)
2243 if (candidates && ! candidates->next)
2244 /* say why this one won't work or try to be loose */;
2245 else
2246 cp_error ("no viable candidates");
2248 #endif
2250 return 0;
2253 candidates = splice_viable (candidates);
2254 cand = tourney (candidates);
2256 if (cand == 0)
2258 if (flags & LOOKUP_COMPLAIN)
2260 cp_error ("conversion from `%T' to `%T' is ambiguous",
2261 fromtype, totype);
2262 print_z_candidates (candidates);
2265 cand = candidates; /* any one will do */
2266 cand->second_conv = build1 (AMBIG_CONV, totype, expr);
2267 ICS_USER_FLAG (cand->second_conv) = 1;
2268 ICS_BAD_FLAG (cand->second_conv) = 1;
2270 return cand;
2273 for (p = &(cand->second_conv); TREE_CODE (*p) != IDENTITY_CONV; )
2274 p = &(TREE_OPERAND (*p, 0));
2276 /* Pedantically, normal function declarations are never considered
2277 to refer to template instantiations, so we only do this with
2278 -fguiding-decls. */
2279 if (flag_guiding_decls && templates && ! cand->template
2280 && !DECL_INITIAL (cand->fn)
2281 && TREE_CODE (TREE_TYPE (cand->fn)) != METHOD_TYPE)
2282 add_maybe_template (cand->fn, templates);
2284 *p = build
2285 (USER_CONV,
2286 (DECL_CONSTRUCTOR_P (cand->fn)
2287 ? totype : non_reference (TREE_TYPE (TREE_TYPE (cand->fn)))),
2288 expr, build_expr_ptr_wrapper (cand));
2289 ICS_USER_FLAG (cand->second_conv) = 1;
2290 if (cand->viable == -1)
2291 ICS_BAD_FLAG (cand->second_conv) = 1;
2293 return cand;
2296 tree
2297 build_user_type_conversion (totype, expr, flags)
2298 tree totype, expr;
2299 int flags;
2301 struct z_candidate *cand
2302 = build_user_type_conversion_1 (totype, expr, flags);
2304 if (cand)
2306 if (TREE_CODE (cand->second_conv) == AMBIG_CONV)
2307 return error_mark_node;
2308 return convert_from_reference (convert_like (cand->second_conv, expr));
2310 return NULL_TREE;
2313 /* Do any initial processing on the arguments to a function call. */
2315 static tree
2316 resolve_args (args)
2317 tree args;
2319 tree t;
2320 for (t = args; t; t = TREE_CHAIN (t))
2322 if (TREE_VALUE (t) == error_mark_node)
2323 return error_mark_node;
2324 else if (TREE_CODE (TREE_TYPE (TREE_VALUE (t))) == VOID_TYPE)
2326 error ("invalid use of void expression");
2327 return error_mark_node;
2329 else if (TREE_CODE (TREE_VALUE (t)) == OFFSET_REF)
2330 TREE_VALUE (t) = resolve_offset_ref (TREE_VALUE (t));
2332 return args;
2335 tree
2336 build_new_function_call (fn, args)
2337 tree fn, args;
2339 struct z_candidate *candidates = 0, *cand;
2340 tree explicit_targs = NULL_TREE;
2341 int template_only = 0;
2343 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
2345 explicit_targs = TREE_OPERAND (fn, 1);
2346 fn = TREE_OPERAND (fn, 0);
2347 template_only = 1;
2350 if (really_overloaded_fn (fn))
2352 tree t1;
2353 tree templates = NULL_TREE;
2355 args = resolve_args (args);
2357 if (args == error_mark_node)
2358 return error_mark_node;
2360 for (t1 = fn; t1; t1 = OVL_CHAIN (t1))
2362 tree t = OVL_FUNCTION (t1);
2363 if (TREE_CODE (t) == TEMPLATE_DECL)
2365 templates = scratch_tree_cons (NULL_TREE, t, templates);
2366 candidates = add_template_candidate
2367 (candidates, t, explicit_targs, args, NULL_TREE,
2368 LOOKUP_NORMAL, DEDUCE_CALL);
2370 else if (! template_only)
2371 candidates = add_function_candidate
2372 (candidates, t, args, LOOKUP_NORMAL);
2375 if (! any_viable (candidates))
2377 if (candidates && ! candidates->next)
2378 return build_function_call (candidates->fn, args);
2379 cp_error ("no matching function for call to `%D (%A)'",
2380 DECL_NAME (OVL_FUNCTION (fn)), args);
2381 if (candidates)
2382 print_z_candidates (candidates);
2383 return error_mark_node;
2385 candidates = splice_viable (candidates);
2386 cand = tourney (candidates);
2388 if (cand == 0)
2390 cp_error ("call of overloaded `%D (%A)' is ambiguous",
2391 DECL_NAME (OVL_FUNCTION (fn)), args);
2392 print_z_candidates (candidates);
2393 return error_mark_node;
2396 /* Pedantically, normal function declarations are never considered
2397 to refer to template instantiations, so we only do this with
2398 -fguiding-decls. */
2399 if (flag_guiding_decls && templates && ! cand->template
2400 && ! DECL_INITIAL (cand->fn))
2401 add_maybe_template (cand->fn, templates);
2403 return build_over_call (cand, args, LOOKUP_NORMAL);
2406 /* This is not really overloaded. */
2407 fn = OVL_CURRENT (fn);
2409 return build_function_call (fn, args);
2412 static tree
2413 build_object_call (obj, args)
2414 tree obj, args;
2416 struct z_candidate *candidates = 0, *cand;
2417 tree fns, convs, mem_args = NULL_TREE;
2418 tree type = TREE_TYPE (obj);
2420 if (TYPE_PTRMEMFUNC_P (type))
2422 /* It's no good looking for an overloaded operator() on a
2423 pointer-to-member-function. */
2424 cp_error ("pointer-to-member function %E cannot be called", obj);
2425 cp_error ("without an object; consider using .* or ->*");
2426 return error_mark_node;
2429 fns = lookup_fnfields (TYPE_BINFO (type), ansi_opname [CALL_EXPR], 1);
2430 if (fns == error_mark_node)
2431 return error_mark_node;
2433 args = resolve_args (args);
2435 if (args == error_mark_node)
2436 return error_mark_node;
2438 if (fns)
2440 tree base = TREE_PURPOSE (fns);
2441 mem_args = scratch_tree_cons (NULL_TREE, build_this (obj), args);
2443 for (fns = TREE_VALUE (fns); fns; fns = OVL_NEXT (fns))
2445 tree fn = OVL_CURRENT (fns);
2446 if (TREE_CODE (fn) == TEMPLATE_DECL)
2448 candidates
2449 = add_template_candidate (candidates, fn, NULL_TREE,
2450 mem_args, NULL_TREE,
2451 LOOKUP_NORMAL, DEDUCE_CALL);
2453 else
2454 candidates = add_function_candidate
2455 (candidates, fn, mem_args, LOOKUP_NORMAL);
2457 if (candidates)
2458 candidates->basetype_path = base;
2462 convs = lookup_conversions (type);
2464 for (; convs; convs = TREE_CHAIN (convs))
2466 tree fns = TREE_VALUE (convs);
2467 tree totype = TREE_TYPE (TREE_TYPE (OVL_CURRENT (fns)));
2469 if ((TREE_CODE (totype) == POINTER_TYPE
2470 || TREE_CODE (totype) == REFERENCE_TYPE)
2471 && TREE_CODE (TREE_TYPE (totype)) == FUNCTION_TYPE)
2472 for (; fns; fns = OVL_NEXT (fns))
2474 tree fn = OVL_CURRENT (fns);
2475 if (TREE_CODE (fn) == TEMPLATE_DECL)
2477 candidates = add_template_conv_candidate (candidates,
2479 obj,
2480 args,
2481 totype);
2483 else
2484 candidates = add_conv_candidate (candidates, fn, obj, args);
2486 if (candidates)
2487 candidates->basetype_path = TREE_PURPOSE (convs);
2491 if (! any_viable (candidates))
2493 cp_error ("no match for call to `(%T) (%A)'", TREE_TYPE (obj), args);
2494 print_z_candidates (candidates);
2495 return error_mark_node;
2498 candidates = splice_viable (candidates);
2499 cand = tourney (candidates);
2501 if (cand == 0)
2503 cp_error ("call of `(%T) (%A)' is ambiguous", TREE_TYPE (obj), args);
2504 print_z_candidates (candidates);
2505 return error_mark_node;
2508 if (DECL_NAME (cand->fn) == ansi_opname [CALL_EXPR])
2509 return build_over_call (cand, mem_args, LOOKUP_NORMAL);
2511 obj = convert_like (TREE_VEC_ELT (cand->convs, 0), obj);
2513 /* FIXME */
2514 return build_function_call (obj, args);
2517 static void
2518 op_error (code, code2, arg1, arg2, arg3, problem)
2519 enum tree_code code, code2;
2520 tree arg1, arg2, arg3;
2521 char *problem;
2523 char * opname
2524 = (code == MODIFY_EXPR ? assignop_tab [code2] : opname_tab [code]);
2526 switch (code)
2528 case COND_EXPR:
2529 cp_error ("%s for `%T ? %T : %T'", problem,
2530 error_type (arg1), error_type (arg2), error_type (arg3));
2531 break;
2532 case POSTINCREMENT_EXPR:
2533 case POSTDECREMENT_EXPR:
2534 cp_error ("%s for `%T%s'", problem, error_type (arg1), opname);
2535 break;
2536 case ARRAY_REF:
2537 cp_error ("%s for `%T[%T]'", problem,
2538 error_type (arg1), error_type (arg2));
2539 break;
2540 default:
2541 if (arg2)
2542 cp_error ("%s for `%T %s %T'", problem,
2543 error_type (arg1), opname, error_type (arg2));
2544 else
2545 cp_error ("%s for `%s%T'", problem, opname, error_type (arg1));
2549 tree
2550 build_new_op (code, flags, arg1, arg2, arg3)
2551 enum tree_code code;
2552 int flags;
2553 tree arg1, arg2, arg3;
2555 struct z_candidate *candidates = 0, *cand;
2556 tree fns, mem_arglist = NULL_TREE, arglist, fnname;
2557 enum tree_code code2 = NOP_EXPR;
2558 tree templates = NULL_TREE;
2559 tree conv;
2561 if (arg1 == error_mark_node
2562 || arg2 == error_mark_node
2563 || arg3 == error_mark_node)
2564 return error_mark_node;
2566 /* This can happen if a template takes all non-type parameters, e.g.
2567 undeclared_template<1, 5, 72>a; */
2568 if (code == LT_EXPR && TREE_CODE (arg1) == TEMPLATE_DECL)
2570 cp_error ("`%D' must be declared before use", arg1);
2571 return error_mark_node;
2574 if (code == MODIFY_EXPR)
2576 code2 = TREE_CODE (arg3);
2577 arg3 = NULL_TREE;
2578 fnname = ansi_assopname[code2];
2580 else
2581 fnname = ansi_opname[code];
2583 switch (code)
2585 case NEW_EXPR:
2586 case VEC_NEW_EXPR:
2587 case VEC_DELETE_EXPR:
2588 case DELETE_EXPR:
2589 /* Use build_op_new_call and build_op_delete_call instead. */
2590 my_friendly_abort (981018);
2592 case CALL_EXPR:
2593 return build_object_call (arg1, arg2);
2595 default:
2596 break;
2599 /* The comma operator can have void args. */
2600 if (TREE_CODE (arg1) == OFFSET_REF)
2601 arg1 = resolve_offset_ref (arg1);
2602 if (arg2 && TREE_CODE (arg2) == OFFSET_REF)
2603 arg2 = resolve_offset_ref (arg2);
2604 if (arg3 && TREE_CODE (arg3) == OFFSET_REF)
2605 arg3 = resolve_offset_ref (arg3);
2607 if (code == COND_EXPR)
2609 if (arg2 == NULL_TREE
2610 || TREE_CODE (TREE_TYPE (arg2)) == VOID_TYPE
2611 || TREE_CODE (TREE_TYPE (arg3)) == VOID_TYPE
2612 || (! IS_OVERLOAD_TYPE (TREE_TYPE (arg2))
2613 && ! IS_OVERLOAD_TYPE (TREE_TYPE (arg3))))
2614 goto builtin;
2616 else if (! IS_OVERLOAD_TYPE (TREE_TYPE (arg1))
2617 && (! arg2 || ! IS_OVERLOAD_TYPE (TREE_TYPE (arg2))))
2618 goto builtin;
2620 if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
2621 arg2 = integer_zero_node;
2623 if (arg2 && arg3)
2624 arglist = scratch_tree_cons (NULL_TREE, arg1, scratch_tree_cons
2625 (NULL_TREE, arg2, build_scratch_list (NULL_TREE, arg3)));
2626 else if (arg2)
2627 arglist = scratch_tree_cons (NULL_TREE, arg1, build_scratch_list (NULL_TREE, arg2));
2628 else
2629 arglist = build_scratch_list (NULL_TREE, arg1);
2631 fns = lookup_function_nonclass (fnname, arglist);
2633 if (fns && TREE_CODE (fns) == TREE_LIST)
2634 fns = TREE_VALUE (fns);
2635 for (; fns; fns = OVL_NEXT (fns))
2637 tree fn = OVL_CURRENT (fns);
2638 if (TREE_CODE (fn) == TEMPLATE_DECL)
2640 templates = scratch_tree_cons (NULL_TREE, fn, templates);
2641 candidates
2642 = add_template_candidate (candidates, fn, NULL_TREE,
2643 arglist, TREE_TYPE (fnname),
2644 flags, DEDUCE_CALL);
2646 else
2647 candidates = add_function_candidate (candidates, fn, arglist, flags);
2650 if (IS_AGGR_TYPE (TREE_TYPE (arg1)))
2652 fns = lookup_fnfields (TYPE_BINFO (TREE_TYPE (arg1)), fnname, 1);
2653 if (fns == error_mark_node)
2654 return fns;
2656 else
2657 fns = NULL_TREE;
2659 if (fns)
2661 tree basetype = TREE_PURPOSE (fns);
2662 mem_arglist = scratch_tree_cons (NULL_TREE, build_this (arg1), TREE_CHAIN (arglist));
2663 for (fns = TREE_VALUE (fns); fns; fns = OVL_NEXT (fns))
2665 tree fn = OVL_CURRENT (fns);
2666 tree this_arglist;
2668 if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
2669 this_arglist = mem_arglist;
2670 else
2671 this_arglist = arglist;
2673 if (TREE_CODE (fn) == TEMPLATE_DECL)
2675 /* A member template. */
2676 templates = scratch_tree_cons (NULL_TREE, fn, templates);
2677 candidates
2678 = add_template_candidate (candidates, fn, NULL_TREE,
2679 this_arglist, TREE_TYPE (fnname),
2680 flags, DEDUCE_CALL);
2682 else
2683 candidates = add_function_candidate
2684 (candidates, fn, this_arglist, flags);
2686 if (candidates)
2687 candidates->basetype_path = basetype;
2692 tree args[3];
2694 /* Rearrange the arguments for ?: so that add_builtin_candidate only has
2695 to know about two args; a builtin candidate will always have a first
2696 parameter of type bool. We'll handle that in
2697 build_builtin_candidate. */
2698 if (code == COND_EXPR)
2700 args[0] = arg2;
2701 args[1] = arg3;
2702 args[2] = arg1;
2704 else
2706 args[0] = arg1;
2707 args[1] = arg2;
2708 args[2] = NULL_TREE;
2711 candidates = add_builtin_candidates
2712 (candidates, code, code2, fnname, args, flags);
2715 if (! any_viable (candidates))
2717 switch (code)
2719 case POSTINCREMENT_EXPR:
2720 case POSTDECREMENT_EXPR:
2721 /* Look for an `operator++ (int)'. If they didn't have
2722 one, then we fall back to the old way of doing things. */
2723 if (flags & LOOKUP_COMPLAIN)
2724 cp_pedwarn ("no `%D (int)' declared for postfix `%s', trying prefix operator instead",
2725 fnname, opname_tab [code]);
2726 if (code == POSTINCREMENT_EXPR)
2727 code = PREINCREMENT_EXPR;
2728 else
2729 code = PREDECREMENT_EXPR;
2730 return build_new_op (code, flags, arg1, NULL_TREE, NULL_TREE);
2732 /* The caller will deal with these. */
2733 case ADDR_EXPR:
2734 case COMPOUND_EXPR:
2735 case COMPONENT_REF:
2736 return NULL_TREE;
2738 default:
2739 break;
2741 if (flags & LOOKUP_COMPLAIN)
2743 op_error (code, code2, arg1, arg2, arg3, "no match");
2744 print_z_candidates (candidates);
2746 return error_mark_node;
2748 candidates = splice_viable (candidates);
2749 cand = tourney (candidates);
2751 if (cand == 0)
2753 if (flags & LOOKUP_COMPLAIN)
2755 op_error (code, code2, arg1, arg2, arg3, "ambiguous overload");
2756 print_z_candidates (candidates);
2758 return error_mark_node;
2761 if (TREE_CODE (cand->fn) == FUNCTION_DECL)
2763 extern int warn_synth;
2764 if (warn_synth
2765 && fnname == ansi_opname[MODIFY_EXPR]
2766 && DECL_ARTIFICIAL (cand->fn)
2767 && candidates->next
2768 && ! candidates->next->next)
2770 cp_warning ("using synthesized `%#D' for copy assignment",
2771 cand->fn);
2772 cp_warning_at (" where cfront would use `%#D'",
2773 cand == candidates
2774 ? candidates->next->fn
2775 : candidates->fn);
2778 /* Pedantically, normal function declarations are never considered
2779 to refer to template instantiations, so we only do this with
2780 -fguiding-decls. */
2781 if (flag_guiding_decls && templates && ! cand->template
2782 && ! DECL_INITIAL (cand->fn)
2783 && TREE_CODE (TREE_TYPE (cand->fn)) != METHOD_TYPE)
2784 add_maybe_template (cand->fn, templates);
2786 return build_over_call
2787 (cand,
2788 TREE_CODE (TREE_TYPE (cand->fn)) == METHOD_TYPE
2789 ? mem_arglist : arglist,
2790 LOOKUP_NORMAL);
2793 /* Check for comparison of different enum types. */
2794 switch (code)
2796 case GT_EXPR:
2797 case LT_EXPR:
2798 case GE_EXPR:
2799 case LE_EXPR:
2800 case EQ_EXPR:
2801 case NE_EXPR:
2802 if (TREE_CODE (TREE_TYPE (arg1)) == ENUMERAL_TYPE
2803 && TREE_CODE (TREE_TYPE (arg2)) == ENUMERAL_TYPE
2804 && (TYPE_MAIN_VARIANT (TREE_TYPE (arg1))
2805 != TYPE_MAIN_VARIANT (TREE_TYPE (arg2))))
2807 cp_warning ("comparison between `%#T' and `%#T'",
2808 TREE_TYPE (arg1), TREE_TYPE (arg2));
2810 break;
2811 default:
2812 break;
2815 /* We need to strip any leading REF_BIND so that bitfields don't cause
2816 errors. This should not remove any important conversions, because
2817 builtins don't apply to class objects directly. */
2818 conv = TREE_VEC_ELT (cand->convs, 0);
2819 if (TREE_CODE (conv) == REF_BIND)
2820 conv = TREE_OPERAND (conv, 0);
2821 arg1 = convert_like (conv, arg1);
2822 if (arg2)
2823 arg2 = convert_like (TREE_VEC_ELT (cand->convs, 1), arg2);
2824 if (arg3)
2825 arg3 = convert_like (TREE_VEC_ELT (cand->convs, 2), arg3);
2827 builtin:
2828 switch (code)
2830 case MODIFY_EXPR:
2831 return build_modify_expr (arg1, code2, arg2);
2833 case INDIRECT_REF:
2834 return build_indirect_ref (arg1, "unary *");
2836 case PLUS_EXPR:
2837 case MINUS_EXPR:
2838 case MULT_EXPR:
2839 case TRUNC_DIV_EXPR:
2840 case GT_EXPR:
2841 case LT_EXPR:
2842 case GE_EXPR:
2843 case LE_EXPR:
2844 case EQ_EXPR:
2845 case NE_EXPR:
2846 case MAX_EXPR:
2847 case MIN_EXPR:
2848 case LSHIFT_EXPR:
2849 case RSHIFT_EXPR:
2850 case TRUNC_MOD_EXPR:
2851 case BIT_AND_EXPR:
2852 case BIT_IOR_EXPR:
2853 case BIT_XOR_EXPR:
2854 case TRUTH_ANDIF_EXPR:
2855 case TRUTH_ORIF_EXPR:
2856 return build_binary_op_nodefault (code, arg1, arg2, code);
2858 case CONVERT_EXPR:
2859 case NEGATE_EXPR:
2860 case BIT_NOT_EXPR:
2861 case TRUTH_NOT_EXPR:
2862 case PREINCREMENT_EXPR:
2863 case POSTINCREMENT_EXPR:
2864 case PREDECREMENT_EXPR:
2865 case POSTDECREMENT_EXPR:
2866 case REALPART_EXPR:
2867 case IMAGPART_EXPR:
2868 return build_unary_op (code, arg1, candidates != 0);
2870 case ARRAY_REF:
2871 return build_array_ref (arg1, arg2);
2873 case COND_EXPR:
2874 return build_conditional_expr (arg1, arg2, arg3);
2876 case MEMBER_REF:
2877 return build_m_component_ref
2878 (build_indirect_ref (arg1, NULL_PTR), arg2);
2880 /* The caller will deal with these. */
2881 case ADDR_EXPR:
2882 case COMPONENT_REF:
2883 case COMPOUND_EXPR:
2884 return NULL_TREE;
2886 default:
2887 my_friendly_abort (367);
2888 return NULL_TREE;
2892 /* Build up a call to operator new. This has to be handled differently
2893 from other operators in the way lookup is handled; first members are
2894 considered, then globals. CODE is either NEW_EXPR or VEC_NEW_EXPR.
2895 TYPE is the type to be created. ARGS are any new-placement args.
2896 FLAGS are the usual overloading flags. */
2898 tree
2899 build_op_new_call (code, type, args, flags)
2900 enum tree_code code;
2901 tree type, args;
2902 int flags;
2904 tree fnname = ansi_opname[code];
2906 if (IS_AGGR_TYPE (type) && ! (flags & LOOKUP_GLOBAL)
2907 && (TYPE_GETS_NEW (type) & (1 << (code == VEC_NEW_EXPR))))
2909 return build_method_call (build_dummy_object (type),
2910 fnname, args, NULL_TREE, flags);
2912 else
2913 return build_new_function_call
2914 (lookup_function_nonclass (fnname, args), args);
2917 /* Build a call to operator delete. This has to be handled very specially,
2918 because the restrictions on what signatures match are different from all
2919 other call instances. For a normal delete, only a delete taking (void *)
2920 or (void *, size_t) is accepted. For a placement delete, only an exact
2921 match with the placement new is accepted.
2923 CODE is either DELETE_EXPR or VEC_DELETE_EXPR.
2924 ADDR is the pointer to be deleted. For placement delete, it is also
2925 used to determine what the corresponding new looked like.
2926 SIZE is the size of the memory block to be deleted.
2927 FLAGS are the usual overloading flags.
2928 PLACEMENT is the corresponding placement new call, or 0. */
2930 tree
2931 build_op_delete_call (code, addr, size, flags, placement)
2932 enum tree_code code;
2933 tree addr, size, placement;
2934 int flags;
2936 tree fn, fns, fnname, fntype, argtypes, args, type;
2938 if (addr == error_mark_node)
2939 return error_mark_node;
2941 type = TREE_TYPE (TREE_TYPE (addr));
2942 fnname = ansi_opname[code];
2944 if (IS_AGGR_TYPE (type) && ! (flags & LOOKUP_GLOBAL))
2945 /* In [class.free]
2947 If the result of the lookup is ambiguous or inaccessible, or if
2948 the lookup selects a placement deallocation function, the
2949 program is ill-formed.
2951 Therefore, we ask lookup_fnfields to complain ambout ambiguity. */
2953 fns = lookup_fnfields (TYPE_BINFO (type), fnname, 1);
2954 if (fns == error_mark_node)
2955 return error_mark_node;
2957 else
2958 fns = NULL_TREE;
2960 if (fns == NULL_TREE)
2961 fns = lookup_name_nonclass (fnname);
2963 if (placement)
2965 /* placement is a CALL_EXPR around an ADDR_EXPR around a function. */
2967 /* Extract the function. */
2968 argtypes = TREE_OPERAND (TREE_OPERAND (placement, 0), 0);
2969 /* Then the second parm type. */
2970 argtypes = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (argtypes)));
2972 /* Also the second argument. */
2973 args = TREE_CHAIN (TREE_OPERAND (placement, 1));
2975 else
2977 /* First try it without the size argument. */
2978 argtypes = void_list_node;
2979 args = NULL_TREE;
2982 argtypes = tree_cons (NULL_TREE, ptr_type_node, argtypes);
2983 fntype = build_function_type (void_type_node, argtypes);
2985 /* Strip const and volatile from addr. */
2986 if (type != TYPE_MAIN_VARIANT (type))
2987 addr = cp_convert (build_pointer_type (TYPE_MAIN_VARIANT (type)), addr);
2989 fn = instantiate_type (fntype, fns, 0);
2991 if (fn != error_mark_node)
2993 if (TREE_CODE (fns) == TREE_LIST)
2994 /* Member functions. */
2995 enforce_access (TREE_PURPOSE (fns), fn);
2996 return build_function_call (fn, expr_tree_cons (NULL_TREE, addr, args));
2999 /* If we are doing placement delete we do nothing if we don't find a
3000 matching op delete. */
3001 if (placement)
3002 return NULL_TREE;
3004 /* Normal delete; now try to find a match including the size argument. */
3005 argtypes = tree_cons (NULL_TREE, ptr_type_node,
3006 tree_cons (NULL_TREE, sizetype, void_list_node));
3007 fntype = build_function_type (void_type_node, argtypes);
3009 fn = instantiate_type (fntype, fns, 0);
3011 if (fn != error_mark_node)
3013 if (TREE_CODE (fns) == TREE_LIST)
3014 /* Member functions. */
3015 enforce_access (TREE_PURPOSE (fns), fn);
3016 return build_function_call
3017 (fn, expr_tree_cons (NULL_TREE, addr,
3018 build_expr_list (NULL_TREE, size)));
3021 /* finish_function passes LOOKUP_SPECULATIVELY if we're in a
3022 destructor, in which case the error should be deferred
3023 until someone actually tries to delete one of these. */
3024 if (flags & LOOKUP_SPECULATIVELY)
3025 return NULL_TREE;
3027 cp_error ("no suitable operator delete for `%T'", type);
3028 return error_mark_node;
3031 /* If the current scope isn't allowed to access DECL along
3032 BASETYPE_PATH, give an error. The most derived class in
3033 BASETYPE_PATH is the one used to qualify DECL. */
3036 enforce_access (basetype_path, decl)
3037 tree basetype_path;
3038 tree decl;
3040 int accessible;
3042 accessible = accessible_p (basetype_path, decl);
3043 if (!accessible)
3045 if (TREE_PRIVATE (decl))
3046 cp_error_at ("`%+#D' is private", decl);
3047 else if (TREE_PROTECTED (decl))
3048 cp_error_at ("`%+#D' is protected", decl);
3049 else
3050 cp_error_at ("`%+#D' is inaccessible", decl);
3051 cp_error ("within this context");
3052 return 0;
3055 return 1;
3058 /* Perform the conversions in CONVS on the expression EXPR. */
3060 static tree
3061 convert_like (convs, expr)
3062 tree convs, expr;
3064 if (ICS_BAD_FLAG (convs)
3065 && TREE_CODE (convs) != USER_CONV
3066 && TREE_CODE (convs) != AMBIG_CONV)
3068 tree t = convs;
3069 for (; t; t = TREE_OPERAND (t, 0))
3071 if (TREE_CODE (t) == USER_CONV)
3073 expr = convert_like (t, expr);
3074 break;
3076 else if (TREE_CODE (t) == AMBIG_CONV)
3077 return convert_like (t, expr);
3078 else if (TREE_CODE (t) == IDENTITY_CONV)
3079 break;
3081 return convert_for_initialization
3082 (NULL_TREE, TREE_TYPE (convs), expr, LOOKUP_NORMAL,
3083 "conversion", NULL_TREE, 0);
3086 switch (TREE_CODE (convs))
3088 case USER_CONV:
3090 struct z_candidate *cand
3091 = WRAPPER_PTR (TREE_OPERAND (convs, 1));
3092 tree fn = cand->fn;
3093 tree args;
3095 if (DECL_CONSTRUCTOR_P (fn))
3097 tree t = build_int_2 (0, 0);
3098 TREE_TYPE (t) = build_pointer_type (DECL_CONTEXT (fn));
3100 args = build_scratch_list (NULL_TREE, expr);
3101 if (TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (fn)))
3102 args = scratch_tree_cons (NULL_TREE, integer_one_node, args);
3103 args = scratch_tree_cons (NULL_TREE, t, args);
3105 else
3106 args = build_this (expr);
3107 expr = build_over_call (cand, args, LOOKUP_NORMAL);
3109 /* If this is a constructor or a function returning an aggr type,
3110 we need to build up a TARGET_EXPR. */
3111 if (DECL_CONSTRUCTOR_P (fn))
3112 expr = build_cplus_new (TREE_TYPE (convs), expr);
3114 return expr;
3116 case IDENTITY_CONV:
3117 if (type_unknown_p (expr))
3118 expr = instantiate_type (TREE_TYPE (convs), expr, 1);
3119 if (TREE_READONLY_DECL_P (expr))
3120 expr = decl_constant_value (expr);
3121 return expr;
3122 case AMBIG_CONV:
3123 /* Call build_user_type_conversion again for the error. */
3124 return build_user_type_conversion
3125 (TREE_TYPE (convs), TREE_OPERAND (convs, 0), LOOKUP_NORMAL);
3127 default:
3128 break;
3131 expr = convert_like (TREE_OPERAND (convs, 0), expr);
3132 if (expr == error_mark_node)
3133 return error_mark_node;
3135 switch (TREE_CODE (convs))
3137 case RVALUE_CONV:
3138 if (! IS_AGGR_TYPE (TREE_TYPE (convs)))
3139 return expr;
3140 /* else fall through */
3141 case BASE_CONV:
3143 tree cvt_expr = build_user_type_conversion
3144 (TREE_TYPE (convs), expr, LOOKUP_NORMAL);
3145 if (!cvt_expr)
3147 /* This can occur if, for example, the EXPR has incomplete
3148 type. We can't check for that before attempting the
3149 conversion because the type might be an incomplete
3150 array type, which is OK if some constructor for the
3151 destination type takes a pointer argument. */
3152 if (TYPE_SIZE (TREE_TYPE (expr)) == 0)
3154 if (same_type_p (TREE_TYPE (expr), TREE_TYPE (convs)))
3155 incomplete_type_error (expr, TREE_TYPE (expr));
3156 else
3157 cp_error ("could not convert `%E' (with incomplete type `%T') to `%T'",
3158 expr, TREE_TYPE (expr), TREE_TYPE (convs));
3160 else
3161 cp_error ("could not convert `%E' to `%T'",
3162 expr, TREE_TYPE (convs));
3163 return error_mark_node;
3165 return cvt_expr;
3168 case REF_BIND:
3169 return convert_to_reference
3170 (TREE_TYPE (convs), expr,
3171 CONV_IMPLICIT, LOOKUP_NORMAL|LOOKUP_NO_CONVERSION,
3172 error_mark_node);
3173 case LVALUE_CONV:
3174 return decay_conversion (expr);
3176 case QUAL_CONV:
3177 /* Warn about deprecated conversion if appropriate. */
3178 string_conv_p (TREE_TYPE (convs), expr, 1);
3179 break;
3181 default:
3182 break;
3184 return ocp_convert (TREE_TYPE (convs), expr, CONV_IMPLICIT,
3185 LOOKUP_NORMAL|LOOKUP_NO_CONVERSION);
3188 /* ARG is being passed to a varargs function. Perform any conversions
3189 required. Return the converted value. */
3191 tree
3192 convert_arg_to_ellipsis (arg)
3193 tree arg;
3195 if (TREE_CODE (TREE_TYPE (arg)) == REAL_TYPE
3196 && (TYPE_PRECISION (TREE_TYPE (arg))
3197 < TYPE_PRECISION (double_type_node)))
3198 /* Convert `float' to `double'. */
3199 arg = cp_convert (double_type_node, arg);
3200 else if (IS_AGGR_TYPE (TREE_TYPE (arg))
3201 && ! TYPE_HAS_TRIVIAL_INIT_REF (TREE_TYPE (arg)))
3202 cp_warning ("cannot pass objects of type `%T' through `...'",
3203 TREE_TYPE (arg));
3204 else
3205 /* Convert `short' and `char' to full-size `int'. */
3206 arg = default_conversion (arg);
3208 arg = require_complete_type (arg);
3210 return arg;
3213 /* ARG is a default argument expression being passed to a parameter of
3214 the indicated TYPE, which is a parameter to FN. Do any required
3215 conversions. Return the converted value. */
3217 tree
3218 convert_default_arg (type, arg, fn)
3219 tree type;
3220 tree arg;
3221 tree fn;
3223 if (fn && DECL_TEMPLATE_INFO (fn))
3225 /* This default argument came from a template. Instantiate the
3226 default argument here, not in tsubst. In the case of
3227 something like:
3229 template <class T>
3230 struct S {
3231 static T t();
3232 void f(T = t());
3235 we must be careful to do name lookup in the scope of S<T>,
3236 rather than in the current class. */
3237 if (DECL_CLASS_SCOPE_P (fn))
3238 pushclass (DECL_REAL_CONTEXT (fn), 2);
3240 arg = tsubst_expr (arg, DECL_TI_ARGS (fn), /*complain=*/1, NULL_TREE);
3242 if (DECL_CLASS_SCOPE_P (fn))
3243 popclass (1);
3245 /* Make sure the default argument is reasonable. */
3246 arg = check_default_argument (type, arg);
3249 arg = break_out_target_exprs (arg);
3251 if (TREE_CODE (arg) == CONSTRUCTOR)
3253 arg = digest_init (type, arg, 0);
3254 arg = convert_for_initialization (0, type, arg, LOOKUP_NORMAL,
3255 "default argument", 0, 0);
3257 else
3259 /* This could get clobbered by the following call. */
3260 if (TREE_HAS_CONSTRUCTOR (arg))
3261 arg = copy_node (arg);
3263 arg = convert_for_initialization (0, type, arg, LOOKUP_NORMAL,
3264 "default argument", 0, 0);
3265 #ifdef PROMOTE_PROTOTYPES
3266 if ((TREE_CODE (type) == INTEGER_TYPE
3267 || TREE_CODE (type) == ENUMERAL_TYPE)
3268 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3269 arg = default_conversion (arg);
3270 #endif
3273 return arg;
3276 static tree
3277 build_over_call (cand, args, flags)
3278 struct z_candidate *cand;
3279 tree args;
3280 int flags;
3282 tree fn = cand->fn;
3283 tree convs = cand->convs;
3284 tree converted_args = NULL_TREE;
3285 tree parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
3286 tree conv, arg, val;
3287 int i = 0;
3288 int is_method = 0;
3290 /* Give any warnings we noticed during overload resolution. */
3291 if (cand->warnings)
3292 for (val = cand->warnings; val; val = TREE_CHAIN (val))
3293 joust (cand, WRAPPER_PTR (TREE_VALUE (val)), 1);
3295 if (DECL_FUNCTION_MEMBER_P (fn))
3296 enforce_access (cand->basetype_path, fn);
3298 if (args && TREE_CODE (args) != TREE_LIST)
3299 args = build_scratch_list (NULL_TREE, args);
3300 arg = args;
3302 /* The implicit parameters to a constructor are not considered by overload
3303 resolution, and must be of the proper type. */
3304 if (DECL_CONSTRUCTOR_P (fn))
3306 converted_args = expr_tree_cons (NULL_TREE, TREE_VALUE (arg), converted_args);
3307 arg = TREE_CHAIN (arg);
3308 parm = TREE_CHAIN (parm);
3309 if (TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (fn)))
3311 converted_args = expr_tree_cons
3312 (NULL_TREE, TREE_VALUE (arg), converted_args);
3313 arg = TREE_CHAIN (arg);
3314 parm = TREE_CHAIN (parm);
3317 /* Bypass access control for 'this' parameter. */
3318 else if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
3320 tree parmtype = TREE_VALUE (parm);
3321 tree argtype = TREE_TYPE (TREE_VALUE (arg));
3322 tree t;
3323 if (ICS_BAD_FLAG (TREE_VEC_ELT (convs, i)))
3324 cp_pedwarn ("passing `%T' as `this' argument of `%#D' discards qualifiers",
3325 TREE_TYPE (argtype), fn);
3327 /* [class.mfct.nonstatic]: If a nonstatic member function of a class
3328 X is called for an object that is not of type X, or of a type
3329 derived from X, the behavior is undefined.
3331 So we can assume that anything passed as 'this' is non-null, and
3332 optimize accordingly. */
3333 if (TREE_CODE (parmtype) == POINTER_TYPE)
3334 t = convert_pointer_to_real (TREE_TYPE (parmtype), TREE_VALUE (arg));
3335 else
3336 /* This happens with signatures. */
3337 t = convert_force (parmtype, TREE_VALUE (arg), CONV_C_CAST);
3338 converted_args = expr_tree_cons (NULL_TREE, t, converted_args);
3339 parm = TREE_CHAIN (parm);
3340 arg = TREE_CHAIN (arg);
3341 ++i;
3342 is_method = 1;
3345 for (; arg && parm;
3346 parm = TREE_CHAIN (parm), arg = TREE_CHAIN (arg), ++i)
3348 tree type = TREE_VALUE (parm);
3350 conv = TREE_VEC_ELT (convs, i);
3351 if (ICS_BAD_FLAG (conv))
3353 tree t = conv;
3354 val = TREE_VALUE (arg);
3356 for (; t; t = TREE_OPERAND (t, 0))
3358 if (TREE_CODE (t) == USER_CONV
3359 || TREE_CODE (t) == AMBIG_CONV)
3361 val = convert_like (t, val);
3362 break;
3364 else if (TREE_CODE (t) == IDENTITY_CONV)
3365 break;
3367 val = convert_for_initialization
3368 (NULL_TREE, type, val, LOOKUP_NORMAL,
3369 "argument passing", fn, i - is_method);
3371 else
3373 /* Issue warnings about peculiar, but legal, uses of NULL. */
3374 if (ARITHMETIC_TYPE_P (TREE_VALUE (parm))
3375 && TREE_VALUE (arg) == null_node)
3376 cp_warning ("converting NULL to non-pointer type");
3378 val = convert_like (conv, TREE_VALUE (arg));
3381 #ifdef PROMOTE_PROTOTYPES
3382 if ((TREE_CODE (type) == INTEGER_TYPE
3383 || TREE_CODE (type) == ENUMERAL_TYPE)
3384 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3385 val = default_conversion (val);
3386 #endif
3387 converted_args = expr_tree_cons (NULL_TREE, val, converted_args);
3390 /* Default arguments */
3391 for (; parm && parm != void_list_node; parm = TREE_CHAIN (parm))
3392 converted_args
3393 = expr_tree_cons (NULL_TREE,
3394 convert_default_arg (TREE_VALUE (parm),
3395 TREE_PURPOSE (parm),
3396 fn),
3397 converted_args);
3399 /* Ellipsis */
3400 for (; arg; arg = TREE_CHAIN (arg))
3401 converted_args
3402 = expr_tree_cons (NULL_TREE,
3403 convert_arg_to_ellipsis (TREE_VALUE (arg)),
3404 converted_args);
3406 converted_args = nreverse (converted_args);
3408 if (warn_format && (DECL_NAME (fn) || DECL_ASSEMBLER_NAME (fn)))
3409 check_function_format (DECL_NAME (fn), DECL_ASSEMBLER_NAME (fn),
3410 converted_args);
3412 /* Avoid actually calling copy constructors and copy assignment operators,
3413 if possible. */
3415 if (! flag_elide_constructors)
3416 /* Do things the hard way. */;
3417 else if (DECL_CONSTRUCTOR_P (fn)
3418 && TREE_VEC_LENGTH (convs) == 1
3419 && copy_args_p (fn))
3421 tree targ;
3422 arg = TREE_CHAIN (converted_args);
3423 if (TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (fn)))
3424 arg = TREE_CHAIN (arg);
3425 arg = TREE_VALUE (arg);
3427 /* Pull out the real argument, disregarding const-correctness. */
3428 targ = arg;
3429 while (TREE_CODE (targ) == NOP_EXPR
3430 || TREE_CODE (targ) == NON_LVALUE_EXPR
3431 || TREE_CODE (targ) == CONVERT_EXPR)
3432 targ = TREE_OPERAND (targ, 0);
3433 if (TREE_CODE (targ) == ADDR_EXPR)
3435 targ = TREE_OPERAND (targ, 0);
3436 if (!same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (arg))),
3437 TYPE_MAIN_VARIANT (TREE_TYPE (targ))))
3438 targ = NULL_TREE;
3440 else
3441 targ = NULL_TREE;
3443 if (targ)
3444 arg = targ;
3445 else
3446 arg = build_indirect_ref (arg, 0);
3448 /* [class.copy]: the copy constructor is implicitly defined even if
3449 the implementation elided its use. */
3450 if (TYPE_HAS_COMPLEX_INIT_REF (DECL_CONTEXT (fn)))
3451 mark_used (fn);
3453 /* If we're creating a temp and we already have one, don't create a
3454 new one. If we're not creating a temp but we get one, use
3455 INIT_EXPR to collapse the temp into our target. Otherwise, if the
3456 ctor is trivial, do a bitwise copy with a simple TARGET_EXPR for a
3457 temp or an INIT_EXPR otherwise. */
3458 if (integer_zerop (TREE_VALUE (args)))
3460 if (! real_lvalue_p (arg))
3461 return arg;
3462 else if (TYPE_HAS_TRIVIAL_INIT_REF (DECL_CONTEXT (fn)))
3464 val = build_decl (VAR_DECL, NULL_TREE, DECL_CONTEXT (fn));
3465 val = build (TARGET_EXPR, DECL_CONTEXT (fn), val, arg, 0, 0);
3466 TREE_SIDE_EFFECTS (val) = 1;
3467 return val;
3470 else if (! real_lvalue_p (arg)
3471 || TYPE_HAS_TRIVIAL_INIT_REF (DECL_CONTEXT (fn)))
3473 tree to = stabilize_reference
3474 (build_indirect_ref (TREE_VALUE (args), 0));
3476 /* Don't copy the padding byte; it might not have been allocated
3477 if to is a base subobject. */
3478 if (is_empty_class (DECL_CLASS_CONTEXT (fn)))
3479 return build_unary_op
3480 (ADDR_EXPR, build (COMPOUND_EXPR, TREE_TYPE (to),
3481 cp_convert (void_type_node, arg), to),
3484 val = build (INIT_EXPR, DECL_CONTEXT (fn), to, arg);
3485 TREE_SIDE_EFFECTS (val) = 1;
3486 return build_unary_op (ADDR_EXPR, val, 0);
3489 else if (DECL_NAME (fn) == ansi_opname[MODIFY_EXPR]
3490 && copy_args_p (fn)
3491 && TYPE_HAS_TRIVIAL_ASSIGN_REF (DECL_CLASS_CONTEXT (fn)))
3493 tree to = stabilize_reference
3494 (build_indirect_ref (TREE_VALUE (converted_args), 0));
3496 arg = build_indirect_ref (TREE_VALUE (TREE_CHAIN (converted_args)), 0);
3498 /* Don't copy the padding byte; it might not have been allocated
3499 if to is a base subobject. */
3500 if (is_empty_class (DECL_CLASS_CONTEXT (fn)))
3501 return build (COMPOUND_EXPR, TREE_TYPE (to),
3502 cp_convert (void_type_node, arg), to);
3504 val = build (MODIFY_EXPR, TREE_TYPE (to), to, arg);
3505 TREE_SIDE_EFFECTS (val) = 1;
3506 return val;
3509 mark_used (fn);
3511 if (DECL_CLASS_SCOPE_P (fn) && IS_SIGNATURE (DECL_CONTEXT (fn)))
3512 return build_signature_method_call (fn, converted_args);
3513 else if (DECL_VINDEX (fn) && (flags & LOOKUP_NONVIRTUAL) == 0)
3515 tree t, *p = &TREE_VALUE (converted_args);
3516 tree binfo = get_binfo
3517 (DECL_CONTEXT (fn), TREE_TYPE (TREE_TYPE (*p)), 0);
3518 *p = convert_pointer_to_real (binfo, *p);
3519 if (TREE_SIDE_EFFECTS (*p))
3520 *p = save_expr (*p);
3521 t = build_pointer_type (TREE_TYPE (fn));
3522 fn = build_vfn_ref (p, build_indirect_ref (*p, 0), DECL_VINDEX (fn));
3523 TREE_TYPE (fn) = t;
3525 else if (DECL_INLINE (fn))
3526 fn = inline_conversion (fn);
3527 else
3528 fn = build_addr_func (fn);
3530 /* Recognize certain built-in functions so we can make tree-codes
3531 other than CALL_EXPR. We do this when it enables fold-const.c
3532 to do something useful. */
3534 if (TREE_CODE (fn) == ADDR_EXPR
3535 && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL
3536 && DECL_BUILT_IN (TREE_OPERAND (fn, 0)))
3537 switch (DECL_FUNCTION_CODE (TREE_OPERAND (fn, 0)))
3539 case BUILT_IN_ABS:
3540 case BUILT_IN_LABS:
3541 case BUILT_IN_FABS:
3542 if (converted_args == 0)
3543 return integer_zero_node;
3544 return build_unary_op (ABS_EXPR, TREE_VALUE (converted_args), 0);
3545 default:
3546 break;
3549 fn = build_call (fn, TREE_TYPE (TREE_TYPE (TREE_TYPE (fn))), converted_args);
3550 if (TREE_CODE (TREE_TYPE (fn)) == VOID_TYPE)
3551 return fn;
3552 fn = require_complete_type (fn);
3553 if (IS_AGGR_TYPE (TREE_TYPE (fn)))
3554 fn = build_cplus_new (TREE_TYPE (fn), fn);
3555 return convert_from_reference (fn);
3558 static tree
3559 build_new_method_call (instance, name, args, basetype_path, flags)
3560 tree instance, name, args, basetype_path;
3561 int flags;
3563 struct z_candidate *candidates = 0, *cand;
3564 tree explicit_targs = NULL_TREE;
3565 tree basetype, mem_args = NULL_TREE, fns, instance_ptr;
3566 tree pretty_name;
3567 tree user_args = args;
3568 tree templates = NULL_TREE;
3569 int template_only = 0;
3571 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
3573 explicit_targs = TREE_OPERAND (name, 1);
3574 name = TREE_OPERAND (name, 0);
3575 if (TREE_CODE (name) == TEMPLATE_DECL)
3576 name = DECL_NAME (name);
3577 template_only = 1;
3580 /* If there is an extra argument for controlling virtual bases,
3581 remove it for error reporting. */
3582 if (flags & LOOKUP_HAS_IN_CHARGE)
3583 user_args = TREE_CHAIN (args);
3585 args = resolve_args (args);
3587 if (args == error_mark_node)
3588 return error_mark_node;
3590 if (instance == NULL_TREE)
3591 basetype = BINFO_TYPE (basetype_path);
3592 else
3594 if (TREE_CODE (instance) == OFFSET_REF)
3595 instance = resolve_offset_ref (instance);
3596 if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
3597 instance = convert_from_reference (instance);
3598 basetype = TREE_TYPE (instance);
3600 /* XXX this should be handled before we get here. */
3601 if (! IS_AGGR_TYPE (basetype)
3602 && ! (TYPE_LANG_SPECIFIC (basetype)
3603 && (IS_SIGNATURE_POINTER (basetype)
3604 || IS_SIGNATURE_REFERENCE (basetype))))
3606 if ((flags & LOOKUP_COMPLAIN) && basetype != error_mark_node)
3607 cp_error ("request for member `%D' in `%E', which is of non-aggregate type `%T'",
3608 name, instance, basetype);
3610 return error_mark_node;
3613 /* If `instance' is a signature pointer/reference and `name' is
3614 not a constructor, we are calling a signature member function.
3615 In that case set the `basetype' to the signature type. */
3616 if ((IS_SIGNATURE_POINTER (basetype)
3617 || IS_SIGNATURE_REFERENCE (basetype))
3618 && TYPE_IDENTIFIER (basetype) != name)
3619 basetype = SIGNATURE_TYPE (basetype);
3622 if (basetype_path == NULL_TREE)
3623 basetype_path = TYPE_BINFO (basetype);
3625 if (instance)
3627 instance_ptr = build_this (instance);
3629 if (! template_only)
3631 /* XXX this should be handled before we get here. */
3632 fns = build_field_call (basetype_path, instance_ptr, name, args);
3633 if (fns)
3634 return fns;
3637 else
3639 instance_ptr = build_int_2 (0, 0);
3640 TREE_TYPE (instance_ptr) = build_pointer_type (basetype);
3643 pretty_name
3644 = (name == ctor_identifier ? constructor_name (basetype) : name);
3646 fns = lookup_fnfields (basetype_path, name, 1);
3648 if (fns == error_mark_node)
3649 return error_mark_node;
3650 if (fns)
3652 tree fn = TREE_VALUE (fns);
3653 if (name == ctor_identifier && TYPE_USES_VIRTUAL_BASECLASSES (basetype)
3654 && ! (flags & LOOKUP_HAS_IN_CHARGE))
3656 flags |= LOOKUP_HAS_IN_CHARGE;
3657 args = scratch_tree_cons (NULL_TREE, integer_one_node, args);
3659 mem_args = scratch_tree_cons (NULL_TREE, instance_ptr, args);
3660 for (; fn; fn = OVL_NEXT (fn))
3662 tree t = OVL_CURRENT (fn);
3663 tree this_arglist;
3665 /* We can end up here for copy-init of same or base class. */
3666 if (name == ctor_identifier
3667 && (flags & LOOKUP_ONLYCONVERTING)
3668 && DECL_NONCONVERTING_P (t))
3669 continue;
3670 if (TREE_CODE (TREE_TYPE (t)) == METHOD_TYPE)
3671 this_arglist = mem_args;
3672 else
3673 this_arglist = args;
3675 if (TREE_CODE (t) == TEMPLATE_DECL)
3677 /* A member template. */
3678 templates = scratch_tree_cons (NULL_TREE, t, templates);
3679 candidates =
3680 add_template_candidate (candidates, t, explicit_targs,
3681 this_arglist,
3682 TREE_TYPE (name), flags, DEDUCE_CALL);
3684 else if (! template_only)
3685 candidates = add_function_candidate (candidates, t,
3686 this_arglist, flags);
3688 if (candidates)
3689 candidates->basetype_path = TREE_PURPOSE (fns);
3693 if (! any_viable (candidates))
3695 /* XXX will LOOKUP_SPECULATIVELY be needed when this is done? */
3696 if (flags & LOOKUP_SPECULATIVELY)
3697 return NULL_TREE;
3698 if (TYPE_SIZE (basetype) == 0)
3699 incomplete_type_error (instance_ptr, basetype);
3700 else
3701 cp_error ("no matching function for call to `%T::%D (%A)%V'",
3702 basetype, pretty_name, user_args,
3703 TREE_TYPE (TREE_TYPE (instance_ptr)));
3704 print_z_candidates (candidates);
3705 return error_mark_node;
3707 candidates = splice_viable (candidates);
3708 cand = tourney (candidates);
3710 if (cand == 0)
3712 cp_error ("call of overloaded `%D(%A)' is ambiguous", pretty_name,
3713 user_args);
3714 print_z_candidates (candidates);
3715 return error_mark_node;
3718 if (DECL_ABSTRACT_VIRTUAL_P (cand->fn)
3719 && instance == current_class_ref
3720 && DECL_CONSTRUCTOR_P (current_function_decl)
3721 && ! (flags & LOOKUP_NONVIRTUAL)
3722 && value_member (cand->fn, CLASSTYPE_ABSTRACT_VIRTUALS (basetype)))
3723 cp_error ("abstract virtual `%#D' called from constructor", cand->fn);
3724 if (TREE_CODE (TREE_TYPE (cand->fn)) == METHOD_TYPE
3725 && is_dummy_object (instance_ptr))
3726 cp_error ("cannot call member function `%D' without object", cand->fn);
3728 if (DECL_VINDEX (cand->fn) && ! (flags & LOOKUP_NONVIRTUAL)
3729 && ((instance == current_class_ref && (dtor_label || ctor_label))
3730 || resolves_to_fixed_type_p (instance, 0)))
3731 flags |= LOOKUP_NONVIRTUAL;
3733 /* Pedantically, normal function declarations are never considered
3734 to refer to template instantiations, so we only do this with
3735 -fguiding-decls. */
3736 if (flag_guiding_decls && templates && ! cand->template
3737 && ! DECL_INITIAL (cand->fn))
3738 add_maybe_template (cand->fn, templates);
3740 return build_over_call
3741 (cand,
3742 TREE_CODE (TREE_TYPE (cand->fn)) == METHOD_TYPE ? mem_args : args,
3743 flags);
3746 /* Returns non-zero iff standard conversion sequence ICS1 is a proper
3747 subsequence of ICS2. */
3749 static int
3750 is_subseq (ics1, ics2)
3751 tree ics1, ics2;
3753 /* We can assume that a conversion of the same code
3754 between the same types indicates a subsequence since we only get
3755 here if the types we are converting from are the same. */
3757 while (TREE_CODE (ics1) == RVALUE_CONV
3758 || TREE_CODE (ics1) == LVALUE_CONV)
3759 ics1 = TREE_OPERAND (ics1, 0);
3761 while (1)
3763 while (TREE_CODE (ics2) == RVALUE_CONV
3764 || TREE_CODE (ics2) == LVALUE_CONV)
3765 ics2 = TREE_OPERAND (ics2, 0);
3767 if (TREE_CODE (ics2) == USER_CONV
3768 || TREE_CODE (ics2) == AMBIG_CONV
3769 || TREE_CODE (ics2) == IDENTITY_CONV)
3770 /* At this point, ICS1 cannot be a proper subsequence of
3771 ICS2. We can get a USER_CONV when we are comparing the
3772 second standard conversion sequence of two user conversion
3773 sequences. */
3774 return 0;
3776 ics2 = TREE_OPERAND (ics2, 0);
3778 if (TREE_CODE (ics2) == TREE_CODE (ics1)
3779 && same_type_p (TREE_TYPE (ics2), TREE_TYPE (ics1))
3780 && same_type_p (TREE_TYPE (TREE_OPERAND (ics2, 0)),
3781 TREE_TYPE (TREE_OPERAND (ics1, 0))))
3782 return 1;
3786 /* Returns non-zero iff DERIVED is derived from BASE. The inputs may
3787 be any _TYPE nodes. */
3789 static int
3790 is_properly_derived_from (derived, base)
3791 tree derived;
3792 tree base;
3794 if (!IS_AGGR_TYPE_CODE (TREE_CODE (derived))
3795 || !IS_AGGR_TYPE_CODE (TREE_CODE (base)))
3796 return 0;
3798 /* We only allow proper derivation here. The DERIVED_FROM_P macro
3799 considers every class derived from itself. */
3800 return (!same_type_p (TYPE_MAIN_VARIANT (derived),
3801 TYPE_MAIN_VARIANT (base))
3802 && DERIVED_FROM_P (base, derived));
3805 /* We build the ICS for an implicit object parameter as a pointer
3806 conversion sequence. However, such a sequence should be compared
3807 as if it were a reference conversion sequence. If ICS is the
3808 implicit conversion sequence for an implicit object parameter,
3809 modify it accordingly. */
3811 static void
3812 maybe_handle_implicit_object (ics)
3813 tree* ics;
3815 if (ICS_THIS_FLAG (*ics))
3817 /* [over.match.funcs]
3819 For non-static member functions, the type of the
3820 implicit object parameter is "reference to cv X"
3821 where X is the class of which the function is a
3822 member and cv is the cv-qualification on the member
3823 function declaration. */
3824 tree t = *ics;
3825 if (TREE_CODE (t) == QUAL_CONV)
3826 t = TREE_OPERAND (t, 0);
3827 if (TREE_CODE (t) == PTR_CONV)
3828 t = TREE_OPERAND (t, 0);
3829 t = build1 (IDENTITY_CONV, TREE_TYPE (TREE_TYPE (t)), NULL_TREE);
3830 t = build_conv (REF_BIND,
3831 build_reference_type (TREE_TYPE (TREE_TYPE (*ics))),
3833 ICS_STD_RANK (t) = ICS_STD_RANK (*ics);
3834 *ics = t;
3838 /* If ICS is a REF_BIND, modify it appropriately, set TARGET_TYPE
3839 to the type the reference originally referred to, and return 1.
3840 Otherwise, return 0. */
3842 static int
3843 maybe_handle_ref_bind (ics, target_type)
3844 tree* ics;
3845 tree* target_type;
3847 if (TREE_CODE (*ics) == REF_BIND)
3849 /* [over.ics.rank]
3851 When a parameter of reference type binds directly
3852 (_dcl.init.ref_) to an argument expression, the implicit
3853 conversion sequence is the identity conversion, unless the
3854 argument expression has a type that is a derived class of the
3855 parameter type, in which case the implicit conversion
3856 sequence is a derived-to-base Conversion.
3858 If the parameter binds directly to the result of applying a
3859 conversion function to the argument expression, the implicit
3860 conversion sequence is a user-defined conversion sequence
3861 (_over.ics.user_), with the second standard conversion
3862 sequence either an identity conversion or, if the conversion
3863 function returns an entity of a type that is a derived class
3864 of the parameter type, a derived-to-base Conversion.
3866 When a parameter of reference type is not bound directly to
3867 an argument expression, the conversion sequence is the one
3868 required to convert the argument expression to the underlying
3869 type of the reference according to _over.best.ics_.
3870 Conceptually, this conversion sequence corresponds to
3871 copy-initializing a temporary of the underlying type with the
3872 argument expression. Any difference in top-level
3873 cv-qualification is subsumed by the initialization itself and
3874 does not constitute a conversion. */
3876 tree old_ics = *ics;
3878 *target_type = TREE_TYPE (TREE_TYPE (*ics));
3879 *ics = TREE_OPERAND (*ics, 0);
3880 if (TREE_CODE (*ics) == IDENTITY_CONV
3881 && is_properly_derived_from (TREE_TYPE (*ics), *target_type))
3882 *ics = build_conv (BASE_CONV, *target_type, *ics);
3883 ICS_USER_FLAG (*ics) = ICS_USER_FLAG (old_ics);
3884 ICS_BAD_FLAG (*ics) = ICS_BAD_FLAG (old_ics);
3886 return 1;
3889 return 0;
3892 /* Compare two implicit conversion sequences according to the rules set out in
3893 [over.ics.rank]. Return values:
3895 1: ics1 is better than ics2
3896 -1: ics2 is better than ics1
3897 0: ics1 and ics2 are indistinguishable */
3899 static int
3900 compare_ics (ics1, ics2)
3901 tree ics1, ics2;
3903 tree from_type1;
3904 tree from_type2;
3905 tree to_type1;
3906 tree to_type2;
3907 tree deref_from_type1 = NULL_TREE;
3908 tree deref_from_type2 = NULL_TREE;
3909 tree deref_to_type1 = NULL_TREE;
3910 tree deref_to_type2 = NULL_TREE;
3912 /* REF_BINDING is non-zero if the result of the conversion sequence
3913 is a reference type. In that case TARGET_TYPE is the
3914 type referred to by the reference. */
3915 int ref_binding1;
3916 int ref_binding2;
3917 tree target_type1;
3918 tree target_type2;
3920 /* Handle implicit object parameters. */
3921 maybe_handle_implicit_object (&ics1);
3922 maybe_handle_implicit_object (&ics2);
3924 /* Handle reference parameters. */
3925 ref_binding1 = maybe_handle_ref_bind (&ics1, &target_type1);
3926 ref_binding2 = maybe_handle_ref_bind (&ics2, &target_type2);
3928 /* [over.ics.rank]
3930 When comparing the basic forms of implicit conversion sequences (as
3931 defined in _over.best.ics_)
3933 --a standard conversion sequence (_over.ics.scs_) is a better
3934 conversion sequence than a user-defined conversion sequence
3935 or an ellipsis conversion sequence, and
3937 --a user-defined conversion sequence (_over.ics.user_) is a
3938 better conversion sequence than an ellipsis conversion sequence
3939 (_over.ics.ellipsis_). */
3940 if (ICS_RANK (ics1) > ICS_RANK (ics2))
3941 return -1;
3942 else if (ICS_RANK (ics1) < ICS_RANK (ics2))
3943 return 1;
3945 if (ICS_RANK (ics1) == BAD_RANK)
3947 /* Both ICS are bad. We try to make a decision based on what
3948 would have happenned if they'd been good. */
3949 if (ICS_USER_FLAG (ics1) > ICS_USER_FLAG (ics2)
3950 || ICS_STD_RANK (ics1) > ICS_STD_RANK (ics2))
3951 return -1;
3952 else if (ICS_USER_FLAG (ics1) < ICS_USER_FLAG (ics2)
3953 || ICS_STD_RANK (ics1) < ICS_STD_RANK (ics2))
3954 return 1;
3956 /* We couldn't make up our minds; try to figure it out below. */
3959 if (ICS_ELLIPSIS_FLAG (ics1))
3960 /* Both conversions are ellipsis conversions. */
3961 return 0;
3963 /* User-defined conversion sequence U1 is a better conversion sequence
3964 than another user-defined conversion sequence U2 if they contain the
3965 same user-defined conversion operator or constructor and if the sec-
3966 ond standard conversion sequence of U1 is better than the second
3967 standard conversion sequence of U2. */
3969 if (ICS_USER_FLAG (ics1))
3971 tree t1, t2;
3973 for (t1 = ics1; TREE_CODE (t1) != USER_CONV; t1 = TREE_OPERAND (t1, 0))
3974 if (TREE_CODE (t1) == AMBIG_CONV)
3975 return 0;
3976 for (t2 = ics2; TREE_CODE (t2) != USER_CONV; t2 = TREE_OPERAND (t2, 0))
3977 if (TREE_CODE (t2) == AMBIG_CONV)
3978 return 0;
3980 if (USER_CONV_FN (t1) != USER_CONV_FN (t2))
3981 return 0;
3983 /* We can just fall through here, after setting up
3984 FROM_TYPE1 and FROM_TYPE2. */
3985 from_type1 = TREE_TYPE (t1);
3986 from_type2 = TREE_TYPE (t2);
3988 else
3990 /* We're dealing with two standard conversion sequences.
3992 [over.ics.rank]
3994 Standard conversion sequence S1 is a better conversion
3995 sequence than standard conversion sequence S2 if
3997 --S1 is a proper subsequence of S2 (comparing the conversion
3998 sequences in the canonical form defined by _over.ics.scs_,
3999 excluding any Lvalue Transformation; the identity
4000 conversion sequence is considered to be a subsequence of
4001 any non-identity conversion sequence */
4003 from_type1 = ics1;
4004 while (TREE_CODE (from_type1) != IDENTITY_CONV)
4005 from_type1 = TREE_OPERAND (from_type1, 0);
4006 from_type1 = TREE_TYPE (from_type1);
4008 from_type2 = ics2;
4009 while (TREE_CODE (from_type2) != IDENTITY_CONV)
4010 from_type2 = TREE_OPERAND (from_type2, 0);
4011 from_type2 = TREE_TYPE (from_type2);
4014 if (same_type_p (from_type1, from_type2))
4016 if (is_subseq (ics1, ics2))
4017 return 1;
4018 if (is_subseq (ics2, ics1))
4019 return -1;
4021 /* Otherwise, one sequence cannot be a subsequence of the other; they
4022 don't start with the same type. This can happen when comparing the
4023 second standard conversion sequence in two user-defined conversion
4024 sequences. */
4026 /* [over.ics.rank]
4028 Or, if not that,
4030 --the rank of S1 is better than the rank of S2 (by the rules
4031 defined below):
4033 Standard conversion sequences are ordered by their ranks: an Exact
4034 Match is a better conversion than a Promotion, which is a better
4035 conversion than a Conversion.
4037 Two conversion sequences with the same rank are indistinguishable
4038 unless one of the following rules applies:
4040 --A conversion that is not a conversion of a pointer, or pointer
4041 to member, to bool is better than another conversion that is such
4042 a conversion.
4044 The ICS_STD_RANK automatically handles the pointer-to-bool rule,
4045 so that we do not have to check it explicitly. */
4046 if (ICS_STD_RANK (ics1) < ICS_STD_RANK (ics2))
4047 return 1;
4048 else if (ICS_STD_RANK (ics2) < ICS_STD_RANK (ics1))
4049 return -1;
4051 to_type1 = TREE_TYPE (ics1);
4052 to_type2 = TREE_TYPE (ics2);
4054 if (TYPE_PTR_P (from_type1)
4055 && TYPE_PTR_P (from_type2)
4056 && TYPE_PTR_P (to_type1)
4057 && TYPE_PTR_P (to_type2))
4059 deref_from_type1 = TREE_TYPE (from_type1);
4060 deref_from_type2 = TREE_TYPE (from_type2);
4061 deref_to_type1 = TREE_TYPE (to_type1);
4062 deref_to_type2 = TREE_TYPE (to_type2);
4064 /* The rules for pointers to members A::* are just like the rules
4065 for pointers A*, except opposite: if B is derived from A then
4066 A::* converts to B::*, not vice versa. For that reason, we
4067 switch the from_ and to_ variables here. */
4068 else if (TYPE_PTRMEM_P (from_type1)
4069 && TYPE_PTRMEM_P (from_type2)
4070 && TYPE_PTRMEM_P (to_type1)
4071 && TYPE_PTRMEM_P (to_type2))
4073 deref_to_type1 = TYPE_OFFSET_BASETYPE (TREE_TYPE (from_type1));
4074 deref_to_type2 = TYPE_OFFSET_BASETYPE (TREE_TYPE (from_type2));
4075 deref_from_type1 = TYPE_OFFSET_BASETYPE (TREE_TYPE (to_type1));
4076 deref_from_type2 = TYPE_OFFSET_BASETYPE (TREE_TYPE (to_type2));
4078 else if (TYPE_PTRMEMFUNC_P (from_type1)
4079 && TYPE_PTRMEMFUNC_P (from_type2)
4080 && TYPE_PTRMEMFUNC_P (to_type1)
4081 && TYPE_PTRMEMFUNC_P (to_type2))
4083 deref_to_type1 = TYPE_PTRMEMFUNC_OBJECT_TYPE (from_type1);
4084 deref_to_type2 = TYPE_PTRMEMFUNC_OBJECT_TYPE (from_type2);
4085 deref_from_type1 = TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type1);
4086 deref_from_type2 = TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type2);
4089 if (deref_from_type1 != NULL_TREE
4090 && IS_AGGR_TYPE_CODE (TREE_CODE (deref_from_type1))
4091 && IS_AGGR_TYPE_CODE (TREE_CODE (deref_from_type2)))
4093 /* This was one of the pointer or pointer-like conversions.
4095 [over.ics.rank]
4097 --If class B is derived directly or indirectly from class A,
4098 conversion of B* to A* is better than conversion of B* to
4099 void*, and conversion of A* to void* is better than
4100 conversion of B* to void*. */
4101 if (TREE_CODE (deref_to_type1) == VOID_TYPE
4102 && TREE_CODE (deref_to_type2) == VOID_TYPE)
4104 if (is_properly_derived_from (deref_from_type1,
4105 deref_from_type2))
4106 return -1;
4107 else if (is_properly_derived_from (deref_from_type2,
4108 deref_from_type1))
4109 return 1;
4111 else if (TREE_CODE (deref_to_type1) == VOID_TYPE
4112 || TREE_CODE (deref_to_type2) == VOID_TYPE)
4114 if (same_type_p (deref_from_type1, deref_from_type2))
4116 if (TREE_CODE (deref_to_type2) == VOID_TYPE)
4118 if (is_properly_derived_from (deref_from_type1,
4119 deref_to_type1))
4120 return 1;
4122 /* We know that DEREF_TO_TYPE1 is `void' here. */
4123 else if (is_properly_derived_from (deref_from_type1,
4124 deref_to_type2))
4125 return -1;
4128 else if (IS_AGGR_TYPE_CODE (TREE_CODE (deref_to_type1))
4129 && IS_AGGR_TYPE_CODE (TREE_CODE (deref_to_type2)))
4131 /* [over.ics.rank]
4133 --If class B is derived directly or indirectly from class A
4134 and class C is derived directly or indirectly from B,
4136 --conversion of C* to B* is better than conversion of C* to
4137 A*,
4139 --conversion of B* to A* is better than conversion of C* to
4140 A* */
4141 if (same_type_p (deref_from_type1, deref_from_type2))
4143 if (is_properly_derived_from (deref_to_type1,
4144 deref_to_type2))
4145 return 1;
4146 else if (is_properly_derived_from (deref_to_type2,
4147 deref_to_type1))
4148 return -1;
4150 else if (same_type_p (deref_to_type1, deref_to_type2))
4152 if (is_properly_derived_from (deref_from_type2,
4153 deref_from_type1))
4154 return 1;
4155 else if (is_properly_derived_from (deref_from_type1,
4156 deref_from_type2))
4157 return -1;
4161 else if (IS_AGGR_TYPE_CODE (TREE_CODE (from_type1))
4162 && same_type_p (from_type1, from_type2))
4164 /* [over.ics.rank]
4166 --binding of an expression of type C to a reference of type
4167 B& is better than binding an expression of type C to a
4168 reference of type A&
4170 --conversion of C to B is better than conversion of C to A, */
4171 if (is_properly_derived_from (from_type1, to_type1)
4172 && is_properly_derived_from (from_type1, to_type2))
4174 if (is_properly_derived_from (to_type1, to_type2))
4175 return 1;
4176 else if (is_properly_derived_from (to_type2, to_type1))
4177 return -1;
4180 else if (IS_AGGR_TYPE_CODE (TREE_CODE (to_type1))
4181 && same_type_p (to_type1, to_type2))
4183 /* [over.ics.rank]
4185 --binding of an expression of type B to a reference of type
4186 A& is better than binding an expression of type C to a
4187 reference of type A&,
4189 --onversion of B to A is better than conversion of C to A */
4190 if (is_properly_derived_from (from_type1, to_type1)
4191 && is_properly_derived_from (from_type2, to_type1))
4193 if (is_properly_derived_from (from_type2, from_type1))
4194 return 1;
4195 else if (is_properly_derived_from (from_type1, from_type2))
4196 return -1;
4200 /* [over.ics.rank]
4202 --S1 and S2 differ only in their qualification conversion and yield
4203 similar types T1 and T2 (_conv.qual_), respectively, and the cv-
4204 qualification signature of type T1 is a proper subset of the cv-
4205 qualification signature of type T2 */
4206 if (TREE_CODE (ics1) == QUAL_CONV
4207 && TREE_CODE (ics2) == QUAL_CONV
4208 && same_type_p (from_type1, from_type2))
4209 return comp_cv_qual_signature (to_type1, to_type2);
4211 /* [over.ics.rank]
4213 --S1 and S2 are reference bindings (_dcl.init.ref_), and the
4214 types to which the references refer are the same type except for
4215 top-level cv-qualifiers, and the type to which the reference
4216 initialized by S2 refers is more cv-qualified than the type to
4217 which the reference initialized by S1 refers */
4219 if (ref_binding1 && ref_binding2
4220 && same_type_p (TYPE_MAIN_VARIANT (to_type1),
4221 TYPE_MAIN_VARIANT (to_type2)))
4222 return comp_cv_qualification (target_type2, target_type1);
4224 /* Neither conversion sequence is better than the other. */
4225 return 0;
4228 /* The source type for this standard conversion sequence. */
4230 static tree
4231 source_type (t)
4232 tree t;
4234 for (;; t = TREE_OPERAND (t, 0))
4236 if (TREE_CODE (t) == USER_CONV
4237 || TREE_CODE (t) == AMBIG_CONV
4238 || TREE_CODE (t) == IDENTITY_CONV)
4239 return TREE_TYPE (t);
4241 my_friendly_abort (1823);
4244 /* Note a warning about preferring WINNER to LOSER. We do this by storing
4245 a pointer to LOSER and re-running joust to produce the warning if WINNER
4246 is actually used. */
4248 static void
4249 add_warning (winner, loser)
4250 struct z_candidate *winner, *loser;
4252 winner->warnings = expr_tree_cons (NULL_PTR,
4253 build_expr_ptr_wrapper (loser),
4254 winner->warnings);
4257 /* Compare two candidates for overloading as described in
4258 [over.match.best]. Return values:
4260 1: cand1 is better than cand2
4261 -1: cand2 is better than cand1
4262 0: cand1 and cand2 are indistinguishable */
4264 static int
4265 joust (cand1, cand2, warn)
4266 struct z_candidate *cand1, *cand2;
4267 int warn;
4269 int winner = 0;
4270 int i, off1 = 0, off2 = 0, len;
4272 /* Candidates that involve bad conversions are always worse than those
4273 that don't. */
4274 if (cand1->viable > cand2->viable)
4275 return 1;
4276 if (cand1->viable < cand2->viable)
4277 return -1;
4279 /* If we have two pseudo-candidates for conversions to the same type,
4280 arbitrarily pick one. */
4281 if (TYPE_P (cand1->fn) && cand1->fn == cand2->fn)
4282 return 1;
4284 /* a viable function F1
4285 is defined to be a better function than another viable function F2 if
4286 for all arguments i, ICSi(F1) is not a worse conversion sequence than
4287 ICSi(F2), and then */
4289 /* for some argument j, ICSj(F1) is a better conversion sequence than
4290 ICSj(F2) */
4292 /* For comparing static and non-static member functions, we ignore the
4293 implicit object parameter of the non-static function. The WP says to
4294 pretend that the static function has an object parm, but that won't
4295 work with operator overloading. */
4296 len = TREE_VEC_LENGTH (cand1->convs);
4297 if (len != TREE_VEC_LENGTH (cand2->convs))
4299 if (DECL_STATIC_FUNCTION_P (cand1->fn)
4300 && ! DECL_STATIC_FUNCTION_P (cand2->fn))
4301 off2 = 1;
4302 else if (! DECL_STATIC_FUNCTION_P (cand1->fn)
4303 && DECL_STATIC_FUNCTION_P (cand2->fn))
4305 off1 = 1;
4306 --len;
4308 else
4309 my_friendly_abort (42);
4312 for (i = 0; i < len; ++i)
4314 tree t1 = TREE_VEC_ELT (cand1->convs, i+off1);
4315 tree t2 = TREE_VEC_ELT (cand2->convs, i+off2);
4316 int comp = compare_ics (t1, t2);
4318 if (comp != 0)
4320 if (warn_sign_promo
4321 && ICS_RANK (t1) + ICS_RANK (t2) == STD_RANK + PROMO_RANK
4322 && TREE_CODE (t1) == STD_CONV
4323 && TREE_CODE (t2) == STD_CONV
4324 && TREE_CODE (TREE_TYPE (t1)) == INTEGER_TYPE
4325 && TREE_CODE (TREE_TYPE (t2)) == INTEGER_TYPE
4326 && (TYPE_PRECISION (TREE_TYPE (t1))
4327 == TYPE_PRECISION (TREE_TYPE (t2)))
4328 && (TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (t1, 0)))
4329 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (t1, 0)))
4330 == ENUMERAL_TYPE)))
4332 tree type = TREE_TYPE (TREE_OPERAND (t1, 0));
4333 tree type1, type2;
4334 struct z_candidate *w, *l;
4335 if (comp > 0)
4336 type1 = TREE_TYPE (t1), type2 = TREE_TYPE (t2),
4337 w = cand1, l = cand2;
4338 else
4339 type1 = TREE_TYPE (t2), type2 = TREE_TYPE (t1),
4340 w = cand2, l = cand1;
4342 if (warn)
4344 cp_warning ("passing `%T' chooses `%T' over `%T'",
4345 type, type1, type2);
4346 cp_warning (" in call to `%D'", w->fn);
4348 else
4349 add_warning (w, l);
4352 if (winner && comp != winner)
4354 winner = 0;
4355 goto tweak;
4357 winner = comp;
4361 /* warn about confusing overload resolution for user-defined conversions,
4362 either between a constructor and a conversion op, or between two
4363 conversion ops. */
4364 if (winner && cand1->second_conv
4365 && ((DECL_CONSTRUCTOR_P (cand1->fn)
4366 != DECL_CONSTRUCTOR_P (cand2->fn))
4367 /* Don't warn if the two conv ops convert to the same type... */
4368 || (! DECL_CONSTRUCTOR_P (cand1->fn)
4369 && ! same_type_p (TREE_TYPE (cand1->second_conv),
4370 TREE_TYPE (cand2->second_conv)))))
4372 int comp = compare_ics (cand1->second_conv, cand2->second_conv);
4373 if (comp != winner)
4375 struct z_candidate *w, *l;
4376 if (winner == 1)
4377 w = cand1, l = cand2;
4378 else
4379 w = cand2, l = cand1;
4380 if (warn)
4382 tree source = source_type (TREE_VEC_ELT (w->convs, 0));
4383 if (! DECL_CONSTRUCTOR_P (w->fn))
4384 source = TREE_TYPE (source);
4385 cp_warning ("choosing `%D' over `%D'", w->fn, l->fn);
4386 cp_warning (" for conversion from `%T' to `%T'",
4387 source, TREE_TYPE (w->second_conv));
4388 cp_warning (" because conversion sequence for the argument is better");
4390 else
4391 add_warning (w, l);
4395 if (winner)
4396 return winner;
4398 /* or, if not that,
4399 F1 is a non-template function and F2 is a template function */
4401 if (! cand1->template && cand2->template)
4402 return 1;
4403 else if (cand1->template && ! cand2->template)
4404 return -1;
4405 else if (cand1->template && cand2->template)
4406 winner = more_specialized
4407 (TI_TEMPLATE (cand1->template), TI_TEMPLATE (cand2->template),
4408 NULL_TREE);
4410 /* or, if not that,
4411 the context is an initialization by user-defined conversion (see
4412 _dcl.init_ and _over.match.user_) and the standard conversion
4413 sequence from the return type of F1 to the destination type (i.e.,
4414 the type of the entity being initialized) is a better conversion
4415 sequence than the standard conversion sequence from the return type
4416 of F2 to the destination type. */
4418 if (! winner && cand1->second_conv)
4419 winner = compare_ics (cand1->second_conv, cand2->second_conv);
4421 /* If the built-in candidates are the same, arbitrarily pick one. */
4422 if (! winner && cand1->fn == cand2->fn
4423 && TREE_CODE (cand1->fn) == IDENTIFIER_NODE)
4425 for (i = 0; i < len; ++i)
4426 if (!same_type_p (TREE_TYPE (TREE_VEC_ELT (cand1->convs, i)),
4427 TREE_TYPE (TREE_VEC_ELT (cand2->convs, i))))
4428 break;
4429 if (i == TREE_VEC_LENGTH (cand1->convs))
4430 return 1;
4432 /* Kludge around broken overloading rules whereby
4433 Integer a, b; test ? a : b; is ambiguous, since there's a builtin
4434 that takes references and another that takes values. */
4435 if (cand1->fn == ansi_opname[COND_EXPR])
4437 tree c1 = TREE_VEC_ELT (cand1->convs, 1);
4438 tree c2 = TREE_VEC_ELT (cand2->convs, 1);
4439 tree t1 = strip_top_quals (non_reference (TREE_TYPE (c1)));
4440 tree t2 = strip_top_quals (non_reference (TREE_TYPE (c2)));
4442 if (same_type_p (t1, t2))
4444 if (TREE_CODE (c1) == REF_BIND && TREE_CODE (c2) != REF_BIND)
4445 return 1;
4446 if (TREE_CODE (c1) != REF_BIND && TREE_CODE (c2) == REF_BIND)
4447 return -1;
4452 tweak:
4454 /* Extension: If the worst conversion for one candidate is worse than the
4455 worst conversion for the other, take the first. */
4456 if (! winner && ! pedantic)
4458 int rank1 = IDENTITY_RANK, rank2 = IDENTITY_RANK;
4460 for (i = 0; i < len; ++i)
4462 if (ICS_RANK (TREE_VEC_ELT (cand1->convs, i+off1)) > rank1)
4463 rank1 = ICS_RANK (TREE_VEC_ELT (cand1->convs, i+off1));
4464 if (ICS_RANK (TREE_VEC_ELT (cand2->convs, i+off2)) > rank2)
4465 rank2 = ICS_RANK (TREE_VEC_ELT (cand2->convs, i+off2));
4468 if (rank1 < rank2)
4469 return 1;
4470 if (rank1 > rank2)
4471 return -1;
4474 return winner;
4477 /* Given a list of candidates for overloading, find the best one, if any.
4478 This algorithm has a worst case of O(2n) (winner is last), and a best
4479 case of O(n/2) (totally ambiguous); much better than a sorting
4480 algorithm. */
4482 static struct z_candidate *
4483 tourney (candidates)
4484 struct z_candidate *candidates;
4486 struct z_candidate *champ = candidates, *challenger;
4487 int fate;
4488 int champ_compared_to_predecessor = 0;
4490 /* Walk through the list once, comparing each current champ to the next
4491 candidate, knocking out a candidate or two with each comparison. */
4493 for (challenger = champ->next; challenger; )
4495 fate = joust (champ, challenger, 0);
4496 if (fate == 1)
4497 challenger = challenger->next;
4498 else
4500 if (fate == 0)
4502 champ = challenger->next;
4503 if (champ == 0)
4504 return 0;
4505 champ_compared_to_predecessor = 0;
4507 else
4509 champ = challenger;
4510 champ_compared_to_predecessor = 1;
4513 challenger = champ->next;
4517 /* Make sure the champ is better than all the candidates it hasn't yet
4518 been compared to. */
4520 for (challenger = candidates;
4521 challenger != champ
4522 && !(champ_compared_to_predecessor && challenger->next == champ);
4523 challenger = challenger->next)
4525 fate = joust (champ, challenger, 0);
4526 if (fate != 1)
4527 return 0;
4530 return champ;
4534 can_convert (to, from)
4535 tree to, from;
4537 tree t = implicit_conversion (to, from, NULL_TREE, LOOKUP_NORMAL);
4538 return (t && ! ICS_BAD_FLAG (t));
4542 can_convert_arg (to, from, arg)
4543 tree to, from, arg;
4545 tree t = implicit_conversion (to, from, arg, LOOKUP_NORMAL);
4546 return (t && ! ICS_BAD_FLAG (t));