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