00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #include "config.h"
00028 #include "system.h"
00029 #include "coretypes.h"
00030 #include "tm.h"
00031 #include "tree.h"
00032 #include "cp-tree.h"
00033 #include "output.h"
00034 #include "flags.h"
00035 #include "rtl.h"
00036 #include "toplev.h"
00037 #include "expr.h"
00038 #include "diagnostic.h"
00039 #include "intl.h"
00040 #include "target.h"
00041 #include "convert.h"
00042
00043
00044
00045 typedef enum conversion_kind {
00046 ck_identity,
00047 ck_lvalue,
00048 ck_qual,
00049 ck_std,
00050 ck_ptr,
00051 ck_pmem,
00052 ck_base,
00053 ck_ref_bind,
00054 ck_user,
00055 ck_ambig,
00056 ck_rvalue
00057 } conversion_kind;
00058
00059
00060
00061
00062 typedef enum conversion_rank {
00063 cr_identity,
00064 cr_exact,
00065 cr_promotion,
00066 cr_std,
00067 cr_pbool,
00068 cr_user,
00069 cr_ellipsis,
00070 cr_bad
00071 } conversion_rank;
00072
00073
00074
00075
00076
00077 typedef struct conversion conversion;
00078 struct conversion {
00079
00080 conversion_kind kind;
00081
00082 conversion_rank rank;
00083 BOOL_BITFIELD user_conv_p : 1;
00084 BOOL_BITFIELD ellipsis_p : 1;
00085 BOOL_BITFIELD this_p : 1;
00086 BOOL_BITFIELD bad_p : 1;
00087
00088
00089
00090 BOOL_BITFIELD need_temporary_p : 1;
00091
00092
00093
00094 BOOL_BITFIELD check_copy_constructor_p : 1;
00095
00096
00097 BOOL_BITFIELD base_p : 1;
00098
00099 tree type;
00100 union {
00101
00102
00103
00104
00105 conversion *next;
00106
00107
00108 tree expr;
00109 } u;
00110
00111
00112 struct z_candidate *cand;
00113 };
00114
00115 #define CONVERSION_RANK(NODE) \
00116 ((NODE)->bad_p ? cr_bad \
00117 : (NODE)->ellipsis_p ? cr_ellipsis \
00118 : (NODE)->user_conv_p ? cr_user \
00119 : (NODE)->rank)
00120
00121 static struct obstack conversion_obstack;
00122 static bool conversion_obstack_initialized;
00123
00124 static struct z_candidate * tourney (struct z_candidate *);
00125 static int equal_functions (tree, tree);
00126 static int joust (struct z_candidate *, struct z_candidate *, bool);
00127 static int compare_ics (conversion *, conversion *);
00128 static tree build_over_call (struct z_candidate *, int);
00129 static tree build_java_interface_fn_ref (tree, tree);
00130 #define convert_like(CONV, EXPR) \
00131 convert_like_real ((CONV), (EXPR), NULL_TREE, 0, 0, \
00132 true, \
00133 false)
00134 #define convert_like_with_context(CONV, EXPR, FN, ARGNO) \
00135 convert_like_real ((CONV), (EXPR), (FN), (ARGNO), 0, \
00136 true, \
00137 false)
00138 static tree convert_like_real (conversion *, tree, tree, int, int, bool,
00139 bool);
00140 static void op_error (enum tree_code, enum tree_code, tree, tree,
00141 tree, const char *);
00142 static tree build_object_call (tree, tree);
00143 static tree resolve_args (tree);
00144 static struct z_candidate *build_user_type_conversion_1 (tree, tree, int);
00145 static void print_z_candidate (const char *, struct z_candidate *);
00146 static void print_z_candidates (struct z_candidate *);
00147 static tree build_this (tree);
00148 static struct z_candidate *splice_viable (struct z_candidate *, bool, bool *);
00149 static bool any_strictly_viable (struct z_candidate *);
00150 static struct z_candidate *add_template_candidate
00151 (struct z_candidate **, tree, tree, tree, tree, tree,
00152 tree, tree, int, unification_kind_t);
00153 static struct z_candidate *add_template_candidate_real
00154 (struct z_candidate **, tree, tree, tree, tree, tree,
00155 tree, tree, int, tree, unification_kind_t);
00156 static struct z_candidate *add_template_conv_candidate
00157 (struct z_candidate **, tree, tree, tree, tree, tree, tree);
00158 static void add_builtin_candidates
00159 (struct z_candidate **, enum tree_code, enum tree_code,
00160 tree, tree *, int);
00161 static void add_builtin_candidate
00162 (struct z_candidate **, enum tree_code, enum tree_code,
00163 tree, tree, tree, tree *, tree *, int);
00164 static bool is_complete (tree);
00165 static void build_builtin_candidate
00166 (struct z_candidate **, tree, tree, tree, tree *, tree *,
00167 int);
00168 static struct z_candidate *add_conv_candidate
00169 (struct z_candidate **, tree, tree, tree, tree, tree);
00170 static struct z_candidate *add_function_candidate
00171 (struct z_candidate **, tree, tree, tree, tree, tree, int);
00172 static conversion *implicit_conversion (tree, tree, tree, bool, int);
00173 static conversion *standard_conversion (tree, tree, tree, bool, int);
00174 static conversion *reference_binding (tree, tree, tree, int);
00175 static conversion *build_conv (conversion_kind, tree, conversion *);
00176 static bool is_subseq (conversion *, conversion *);
00177 static tree maybe_handle_ref_bind (conversion **);
00178 static void maybe_handle_implicit_object (conversion **);
00179 static struct z_candidate *add_candidate
00180 (struct z_candidate **, tree, tree, size_t,
00181 conversion **, tree, tree, int);
00182 static tree source_type (conversion *);
00183 static void add_warning (struct z_candidate *, struct z_candidate *);
00184 static bool reference_related_p (tree, tree);
00185 static bool reference_compatible_p (tree, tree);
00186 static conversion *convert_class_to_reference (tree, tree, tree);
00187 static conversion *direct_reference_binding (tree, conversion *);
00188 static bool promoted_arithmetic_type_p (tree);
00189 static conversion *conditional_conversion (tree, tree);
00190 static char *name_as_c_string (tree, tree, bool *);
00191 static tree call_builtin_trap (void);
00192 static tree prep_operand (tree);
00193 static void add_candidates (tree, tree, tree, bool, tree, tree,
00194 int, struct z_candidate **);
00195 static conversion *merge_conversion_sequences (conversion *, conversion *);
00196 static bool magic_varargs_p (tree);
00197 static tree build_temp (tree, tree, int, void (**)(const char *, ...));
00198 static void check_constructor_callable (tree, tree);
00199
00200
00201
00202
00203
00204 bool
00205 check_dtor_name (tree basetype, tree name)
00206 {
00207 name = TREE_OPERAND (name, 0);
00208
00209
00210 if (name == error_mark_node)
00211 return true;
00212
00213 if (TREE_CODE (name) == TYPE_DECL)
00214 name = TREE_TYPE (name);
00215 else if (TYPE_P (name))
00216 ;
00217 else if (TREE_CODE (name) == IDENTIFIER_NODE)
00218 {
00219 if ((IS_AGGR_TYPE (basetype) && name == constructor_name (basetype))
00220 || (TREE_CODE (basetype) == ENUMERAL_TYPE
00221 && name == TYPE_IDENTIFIER (basetype)))
00222 name = basetype;
00223 else
00224 name = get_type_value (name);
00225 }
00226 else
00227 {
00228
00229
00230
00231
00232
00233
00234
00235 gcc_assert (DECL_CLASS_TEMPLATE_P (name));
00236 return false;
00237 }
00238
00239 if (name && TYPE_MAIN_VARIANT (basetype) == TYPE_MAIN_VARIANT (name))
00240 return true;
00241 return false;
00242 }
00243
00244
00245
00246
00247 tree
00248 build_addr_func (tree function)
00249 {
00250 tree type = TREE_TYPE (function);
00251
00252
00253
00254 if (TREE_CODE (type) == METHOD_TYPE)
00255 {
00256 if (TREE_CODE (function) == OFFSET_REF)
00257 {
00258 tree object = build_address (TREE_OPERAND (function, 0));
00259 return get_member_function_from_ptrfunc (&object,
00260 TREE_OPERAND (function, 1));
00261 }
00262 function = build_address (function);
00263 }
00264 else
00265 function = decay_conversion (function);
00266
00267 return function;
00268 }
00269
00270
00271
00272
00273
00274 tree
00275 build_call (tree function, tree parms)
00276 {
00277 int is_constructor = 0;
00278 int nothrow;
00279 tree tmp;
00280 tree decl;
00281 tree result_type;
00282 tree fntype;
00283
00284 function = build_addr_func (function);
00285
00286 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function)))
00287 {
00288 sorry ("unable to call pointer to member function here");
00289 return error_mark_node;
00290 }
00291
00292 fntype = TREE_TYPE (TREE_TYPE (function));
00293 result_type = TREE_TYPE (fntype);
00294
00295 if (TREE_CODE (function) == ADDR_EXPR
00296 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
00297 decl = TREE_OPERAND (function, 0);
00298 else
00299 decl = NULL_TREE;
00300
00301
00302
00303 nothrow = ((decl && TREE_NOTHROW (decl))
00304 || TYPE_NOTHROW_P (TREE_TYPE (TREE_TYPE (function))));
00305
00306 if (decl && TREE_THIS_VOLATILE (decl) && cfun)
00307 current_function_returns_abnormally = 1;
00308
00309 if (decl && TREE_DEPRECATED (decl))
00310 warn_deprecated_use (decl);
00311 require_complete_eh_spec_types (fntype, decl);
00312
00313 if (decl && DECL_CONSTRUCTOR_P (decl))
00314 is_constructor = 1;
00315
00316 if (decl && ! TREE_USED (decl))
00317 {
00318
00319
00320
00321 gcc_assert (DECL_ARTIFICIAL (decl)
00322 || !strncmp (IDENTIFIER_POINTER (DECL_NAME (decl)),
00323 "__", 2));
00324 mark_used (decl);
00325 }
00326
00327
00328
00329
00330 if (! decl || ! DECL_BUILT_IN (decl))
00331 for (tmp = parms; tmp; tmp = TREE_CHAIN (tmp))
00332 if (is_empty_class (TREE_TYPE (TREE_VALUE (tmp)))
00333 && ! TREE_ADDRESSABLE (TREE_TYPE (TREE_VALUE (tmp))))
00334 {
00335 tree t = build0 (EMPTY_CLASS_EXPR, TREE_TYPE (TREE_VALUE (tmp)));
00336 TREE_VALUE (tmp) = build2 (COMPOUND_EXPR, TREE_TYPE (t),
00337 TREE_VALUE (tmp), t);
00338 }
00339
00340 function = build3 (CALL_EXPR, result_type, function, parms, NULL_TREE);
00341 TREE_HAS_CONSTRUCTOR (function) = is_constructor;
00342 TREE_NOTHROW (function) = nothrow;
00343
00344 return function;
00345 }
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380 typedef struct z_candidate z_candidate;
00381
00382 typedef struct candidate_warning candidate_warning;
00383 struct candidate_warning {
00384 z_candidate *loser;
00385 candidate_warning *next;
00386 };
00387
00388 struct z_candidate {
00389
00390
00391 tree fn;
00392
00393 tree args;
00394
00395
00396 conversion **convs;
00397
00398 size_t num_convs;
00399
00400
00401
00402 conversion *second_conv;
00403 int viable;
00404
00405
00406
00407
00408
00409 tree access_path;
00410
00411
00412
00413
00414
00415 tree conversion_path;
00416 tree template_decl;
00417 candidate_warning *warnings;
00418 z_candidate *next;
00419 };
00420
00421
00422
00423
00424 bool
00425 null_ptr_cst_p (tree t)
00426 {
00427
00428
00429
00430
00431 t = integral_constant_value (t);
00432 if (t == null_node
00433 || (CP_INTEGRAL_TYPE_P (TREE_TYPE (t)) && integer_zerop (t)))
00434 return true;
00435 return false;
00436 }
00437
00438
00439
00440
00441 bool
00442 sufficient_parms_p (tree parmlist)
00443 {
00444 for (; parmlist && parmlist != void_list_node;
00445 parmlist = TREE_CHAIN (parmlist))
00446 if (!TREE_PURPOSE (parmlist))
00447 return false;
00448 return true;
00449 }
00450
00451
00452
00453
00454 static void *
00455 conversion_obstack_alloc (size_t n)
00456 {
00457 void *p;
00458 if (!conversion_obstack_initialized)
00459 {
00460 gcc_obstack_init (&conversion_obstack);
00461 conversion_obstack_initialized = true;
00462 }
00463 p = obstack_alloc (&conversion_obstack, n);
00464 memset (p, 0, n);
00465 return p;
00466 }
00467
00468
00469
00470 static conversion *
00471 alloc_conversion (conversion_kind kind)
00472 {
00473 conversion *c;
00474 c = conversion_obstack_alloc (sizeof (conversion));
00475 c->kind = kind;
00476 return c;
00477 }
00478
00479 #ifdef ENABLE_CHECKING
00480
00481
00482
00483
00484 void
00485 validate_conversion_obstack (void)
00486 {
00487 if (conversion_obstack_initialized)
00488 gcc_assert ((obstack_next_free (&conversion_obstack)
00489 == obstack_base (&conversion_obstack)));
00490 }
00491
00492 #endif
00493
00494
00495
00496 static conversion **
00497 alloc_conversions (size_t n)
00498 {
00499 return conversion_obstack_alloc (n * sizeof (conversion *));
00500 }
00501
00502 static conversion *
00503 build_conv (conversion_kind code, tree type, conversion *from)
00504 {
00505 conversion *t;
00506 conversion_rank rank = CONVERSION_RANK (from);
00507
00508
00509
00510
00511 t = alloc_conversion (code);
00512 t->type = type;
00513 t->u.next = from;
00514
00515 switch (code)
00516 {
00517 case ck_ptr:
00518 case ck_pmem:
00519 case ck_base:
00520 case ck_std:
00521 if (rank < cr_std)
00522 rank = cr_std;
00523 break;
00524
00525 case ck_qual:
00526 if (rank < cr_exact)
00527 rank = cr_exact;
00528 break;
00529
00530 default:
00531 break;
00532 }
00533 t->rank = rank;
00534 t->user_conv_p = (code == ck_user || from->user_conv_p);
00535 t->bad_p = from->bad_p;
00536 t->base_p = false;
00537 return t;
00538 }
00539
00540
00541
00542
00543 static conversion *
00544 build_identity_conv (tree type, tree expr)
00545 {
00546 conversion *c;
00547
00548 c = alloc_conversion (ck_identity);
00549 c->type = type;
00550 c->u.expr = expr;
00551
00552 return c;
00553 }
00554
00555
00556
00557
00558
00559 static conversion *
00560 build_ambiguous_conv (tree type, tree expr)
00561 {
00562 conversion *c;
00563
00564 c = alloc_conversion (ck_ambig);
00565 c->type = type;
00566 c->u.expr = expr;
00567
00568 return c;
00569 }
00570
00571 tree
00572 strip_top_quals (tree t)
00573 {
00574 if (TREE_CODE (t) == ARRAY_TYPE)
00575 return t;
00576 return cp_build_qualified_type (t, 0);
00577 }
00578
00579
00580
00581
00582
00583
00584 static conversion *
00585 standard_conversion (tree to, tree from, tree expr, bool c_cast_p,
00586 int flags)
00587 {
00588 enum tree_code fcode, tcode;
00589 conversion *conv;
00590 bool fromref = false;
00591
00592 to = non_reference (to);
00593 if (TREE_CODE (from) == REFERENCE_TYPE)
00594 {
00595 fromref = true;
00596 from = TREE_TYPE (from);
00597 }
00598 to = strip_top_quals (to);
00599 from = strip_top_quals (from);
00600
00601 if ((TYPE_PTRFN_P (to) || TYPE_PTRMEMFUNC_P (to))
00602 && expr && type_unknown_p (expr))
00603 {
00604 expr = instantiate_type (to, expr, tf_conv);
00605 if (expr == error_mark_node)
00606 return NULL;
00607 from = TREE_TYPE (expr);
00608 }
00609
00610 fcode = TREE_CODE (from);
00611 tcode = TREE_CODE (to);
00612
00613 conv = build_identity_conv (from, expr);
00614 if (fcode == FUNCTION_TYPE)
00615 {
00616 from = build_pointer_type (from);
00617 fcode = TREE_CODE (from);
00618 conv = build_conv (ck_lvalue, from, conv);
00619 }
00620 else if (fcode == ARRAY_TYPE)
00621 {
00622 from = build_pointer_type (TREE_TYPE (from));
00623 fcode = TREE_CODE (from);
00624 conv = build_conv (ck_lvalue, from, conv);
00625 }
00626 else if (fromref || (expr && lvalue_p (expr)))
00627 conv = build_conv (ck_rvalue, from, conv);
00628
00629
00630 if (tcode == COMPLEX_TYPE && fcode == COMPLEX_TYPE)
00631 {
00632
00633
00634
00635 conversion *part_conv = standard_conversion
00636 (TREE_TYPE (to), TREE_TYPE (from), NULL_TREE, c_cast_p, flags);
00637
00638 if (part_conv)
00639 {
00640 conv = build_conv (part_conv->kind, to, conv);
00641 conv->rank = part_conv->rank;
00642 }
00643 else
00644 conv = NULL;
00645
00646 return conv;
00647 }
00648
00649 if (same_type_p (from, to))
00650 return conv;
00651
00652 if ((tcode == POINTER_TYPE || TYPE_PTR_TO_MEMBER_P (to))
00653 && expr && null_ptr_cst_p (expr))
00654 conv = build_conv (ck_std, to, conv);
00655 else if ((tcode == INTEGER_TYPE && fcode == POINTER_TYPE)
00656 || (tcode == POINTER_TYPE && fcode == INTEGER_TYPE))
00657 {
00658
00659
00660 conv = build_conv (ck_std, to, conv);
00661 conv->bad_p = true;
00662 }
00663 else if (tcode == ENUMERAL_TYPE && fcode == INTEGER_TYPE)
00664 {
00665
00666
00667 conv = build_conv (ck_std, to, conv);
00668 conv->bad_p = true;
00669 }
00670 else if ((tcode == POINTER_TYPE && fcode == POINTER_TYPE)
00671 || (TYPE_PTRMEM_P (to) && TYPE_PTRMEM_P (from)))
00672 {
00673 tree to_pointee;
00674 tree from_pointee;
00675
00676 if (tcode == POINTER_TYPE
00677 && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (from),
00678 TREE_TYPE (to)))
00679 ;
00680 else if (VOID_TYPE_P (TREE_TYPE (to))
00681 && !TYPE_PTRMEM_P (from)
00682 && TREE_CODE (TREE_TYPE (from)) != FUNCTION_TYPE)
00683 {
00684 from = build_pointer_type
00685 (cp_build_qualified_type (void_type_node,
00686 cp_type_quals (TREE_TYPE (from))));
00687 conv = build_conv (ck_ptr, from, conv);
00688 }
00689 else if (TYPE_PTRMEM_P (from))
00690 {
00691 tree fbase = TYPE_PTRMEM_CLASS_TYPE (from);
00692 tree tbase = TYPE_PTRMEM_CLASS_TYPE (to);
00693
00694 if (DERIVED_FROM_P (fbase, tbase)
00695 && (same_type_ignoring_top_level_qualifiers_p
00696 (TYPE_PTRMEM_POINTED_TO_TYPE (from),
00697 TYPE_PTRMEM_POINTED_TO_TYPE (to))))
00698 {
00699 from = build_ptrmem_type (tbase,
00700 TYPE_PTRMEM_POINTED_TO_TYPE (from));
00701 conv = build_conv (ck_pmem, from, conv);
00702 }
00703 else if (!same_type_p (fbase, tbase))
00704 return NULL;
00705 }
00706 else if (IS_AGGR_TYPE (TREE_TYPE (from))
00707 && IS_AGGR_TYPE (TREE_TYPE (to))
00708
00709
00710
00711
00712
00713
00714
00715
00716
00717
00718
00719 && DERIVED_FROM_P (TREE_TYPE (to), TREE_TYPE (from)))
00720 {
00721 from =
00722 cp_build_qualified_type (TREE_TYPE (to),
00723 cp_type_quals (TREE_TYPE (from)));
00724 from = build_pointer_type (from);
00725 conv = build_conv (ck_ptr, from, conv);
00726 conv->base_p = true;
00727 }
00728
00729 if (tcode == POINTER_TYPE)
00730 {
00731 to_pointee = TREE_TYPE (to);
00732 from_pointee = TREE_TYPE (from);
00733 }
00734 else
00735 {
00736 to_pointee = TYPE_PTRMEM_POINTED_TO_TYPE (to);
00737 from_pointee = TYPE_PTRMEM_POINTED_TO_TYPE (from);
00738 }
00739
00740 if (same_type_p (from, to))
00741 ;
00742 else if (c_cast_p && comp_ptr_ttypes_const (to, from))
00743
00744
00745
00746 conv = build_conv (ck_qual, to, conv);
00747 else if (!c_cast_p && comp_ptr_ttypes (to_pointee, from_pointee))
00748 conv = build_conv (ck_qual, to, conv);
00749 else if (expr && string_conv_p (to, expr, 0))
00750
00751 conv = build_conv (ck_qual, to, conv);
00752 else if (ptr_reasonably_similar (to_pointee, from_pointee))
00753 {
00754 conv = build_conv (ck_ptr, to, conv);
00755 conv->bad_p = true;
00756 }
00757 else
00758 return NULL;
00759
00760 from = to;
00761 }
00762 else if (TYPE_PTRMEMFUNC_P (to) && TYPE_PTRMEMFUNC_P (from))
00763 {
00764 tree fromfn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (from));
00765 tree tofn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (to));
00766 tree fbase = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (fromfn)));
00767 tree tbase = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (tofn)));
00768
00769 if (!DERIVED_FROM_P (fbase, tbase)
00770 || !same_type_p (TREE_TYPE (fromfn), TREE_TYPE (tofn))
00771 || !compparms (TREE_CHAIN (TYPE_ARG_TYPES (fromfn)),
00772 TREE_CHAIN (TYPE_ARG_TYPES (tofn)))
00773 || cp_type_quals (fbase) != cp_type_quals (tbase))
00774 return 0;
00775
00776 from = cp_build_qualified_type (tbase, cp_type_quals (fbase));
00777 from = build_method_type_directly (from,
00778 TREE_TYPE (fromfn),
00779 TREE_CHAIN (TYPE_ARG_TYPES (fromfn)));
00780 from = build_ptrmemfunc_type (build_pointer_type (from));
00781 conv = build_conv (ck_pmem, from, conv);
00782 conv->base_p = true;
00783 }
00784 else if (tcode == BOOLEAN_TYPE)
00785 {
00786
00787
00788
00789
00790 if (ARITHMETIC_TYPE_P (from)
00791 || fcode == ENUMERAL_TYPE
00792 || fcode == POINTER_TYPE
00793 || TYPE_PTR_TO_MEMBER_P (from))
00794 {
00795 conv = build_conv (ck_std, to, conv);
00796 if (fcode == POINTER_TYPE
00797 || TYPE_PTRMEM_P (from)
00798 || (TYPE_PTRMEMFUNC_P (from)
00799 && conv->rank < cr_pbool))
00800 conv->rank = cr_pbool;
00801 return conv;
00802 }
00803
00804 return NULL;
00805 }
00806
00807
00808 else if (tcode == INTEGER_TYPE || tcode == BOOLEAN_TYPE
00809 || tcode == REAL_TYPE)
00810 {
00811 if (! (INTEGRAL_CODE_P (fcode) || fcode == REAL_TYPE))
00812 return 0;
00813 conv = build_conv (ck_std, to, conv);
00814
00815
00816 if (same_type_p (to, type_promotes_to (from))
00817 && conv->u.next->rank <= cr_promotion)
00818 conv->rank = cr_promotion;
00819 }
00820 else if (fcode == VECTOR_TYPE && tcode == VECTOR_TYPE
00821 && vector_types_convertible_p (from, to))
00822 return build_conv (ck_std, to, conv);
00823 else if (!(flags & LOOKUP_CONSTRUCTOR_CALLABLE)
00824 && IS_AGGR_TYPE (to) && IS_AGGR_TYPE (from)
00825 && is_properly_derived_from (from, to))
00826 {
00827 if (conv->kind == ck_rvalue)
00828 conv = conv->u.next;
00829 conv = build_conv (ck_base, to, conv);
00830
00831
00832
00833
00834 conv->need_temporary_p = true;
00835 }
00836 else
00837 return NULL;
00838
00839 return conv;
00840 }
00841
00842
00843
00844 static bool
00845 reference_related_p (tree t1, tree t2)
00846 {
00847 t1 = TYPE_MAIN_VARIANT (t1);
00848 t2 = TYPE_MAIN_VARIANT (t2);
00849
00850
00851
00852
00853
00854
00855 return (same_type_p (t1, t2)
00856 || (CLASS_TYPE_P (t1) && CLASS_TYPE_P (t2)
00857 && DERIVED_FROM_P (t1, t2)));
00858 }
00859
00860
00861
00862 static bool
00863 reference_compatible_p (tree t1, tree t2)
00864 {
00865
00866
00867
00868
00869
00870 return (reference_related_p (t1, t2)
00871 && at_least_as_qualified_p (t1, t2));
00872 }
00873
00874
00875
00876
00877 static conversion *
00878 convert_class_to_reference (tree t, tree s, tree expr)
00879 {
00880 tree conversions;
00881 tree arglist;
00882 conversion *conv;
00883 tree reference_type;
00884 struct z_candidate *candidates;
00885 struct z_candidate *cand;
00886 bool any_viable_p;
00887
00888 conversions = lookup_conversions (s);
00889 if (!conversions)
00890 return NULL;
00891
00892
00893
00894
00895
00896
00897
00898
00899
00900
00901
00902
00903
00904
00905
00906
00907 candidates = 0;
00908
00909
00910
00911
00912
00913
00914 arglist = build_int_cst (build_pointer_type (s), 0);
00915 arglist = build_tree_list (NULL_TREE, arglist);
00916
00917 reference_type = build_reference_type (t);
00918
00919 while (conversions)
00920 {
00921 tree fns = TREE_VALUE (conversions);
00922
00923 for (; fns; fns = OVL_NEXT (fns))
00924 {
00925 tree f = OVL_CURRENT (fns);
00926 tree t2 = TREE_TYPE (TREE_TYPE (f));
00927
00928 cand = NULL;
00929
00930
00931
00932 if (TREE_CODE (f) == TEMPLATE_DECL)
00933 {
00934 cand = add_template_candidate (&candidates,
00935 f, s,
00936 NULL_TREE,
00937 arglist,
00938 reference_type,
00939 TYPE_BINFO (s),
00940 TREE_PURPOSE (conversions),
00941 LOOKUP_NORMAL,
00942 DEDUCE_CONV);
00943
00944 if (cand)
00945 {
00946
00947
00948
00949
00950 f = cand->fn;
00951 t2 = TREE_TYPE (TREE_TYPE (f));
00952 if (TREE_CODE (t2) != REFERENCE_TYPE
00953 || !reference_compatible_p (t, TREE_TYPE (t2)))
00954 {
00955 candidates = candidates->next;
00956 cand = NULL;
00957 }
00958 }
00959 }
00960 else if (TREE_CODE (t2) == REFERENCE_TYPE
00961 && reference_compatible_p (t, TREE_TYPE (t2)))
00962 cand = add_function_candidate (&candidates, f, s, arglist,
00963 TYPE_BINFO (s),
00964 TREE_PURPOSE (conversions),
00965 LOOKUP_NORMAL);
00966
00967 if (cand)
00968 {
00969 conversion *identity_conv;
00970
00971
00972
00973 identity_conv
00974 = build_identity_conv (TREE_TYPE (TREE_TYPE
00975 (TREE_TYPE (cand->fn))),
00976 NULL_TREE);
00977 cand->second_conv
00978 = (direct_reference_binding
00979 (reference_type, identity_conv));
00980 cand->second_conv->bad_p |= cand->convs[0]->bad_p;
00981 }
00982 }
00983 conversions = TREE_CHAIN (conversions);
00984 }
00985
00986 candidates = splice_viable (candidates, pedantic, &any_viable_p);
00987
00988
00989 if (!any_viable_p)
00990 return NULL;
00991
00992 cand = tourney (candidates);
00993 if (!cand)
00994 return NULL;
00995
00996
00997
00998 cand->args = tree_cons (NULL_TREE,
00999 build_this (expr),
01000 TREE_CHAIN (cand->args));
01001
01002
01003
01004 conv = build_conv (ck_user,
01005 TREE_TYPE (TREE_TYPE (cand->fn)),
01006 build_identity_conv (TREE_TYPE (expr), expr));
01007 conv->cand = cand;
01008
01009
01010
01011 cand->second_conv = merge_conversion_sequences (conv, cand->second_conv);
01012
01013 if (cand->viable == -1)
01014 conv->bad_p = true;
01015
01016 return cand->second_conv;
01017 }
01018
01019
01020
01021
01022
01023 static conversion *
01024 direct_reference_binding (tree type, conversion *conv)
01025 {
01026 tree t;
01027
01028 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
01029 gcc_assert (TREE_CODE (conv->type) != REFERENCE_TYPE);
01030
01031 t = TREE_TYPE (type);
01032
01033
01034
01035
01036
01037
01038
01039
01040
01041
01042
01043
01044
01045
01046
01047
01048
01049 if (!same_type_ignoring_top_level_qualifiers_p (t, conv->type))
01050 {
01051
01052 conv = build_conv (ck_base, t, conv);
01053
01054
01055
01056 conv->need_temporary_p = false;
01057 }
01058 return build_conv (ck_ref_bind, type, conv);
01059 }
01060
01061
01062
01063
01064
01065
01066
01067 static conversion *
01068 reference_binding (tree rto, tree rfrom, tree expr, int flags)
01069 {
01070 conversion *conv = NULL;
01071 tree to = TREE_TYPE (rto);
01072 tree from = rfrom;
01073 bool related_p;
01074 bool compatible_p;
01075 cp_lvalue_kind lvalue_p = clk_none;
01076
01077 if (TREE_CODE (to) == FUNCTION_TYPE && expr && type_unknown_p (expr))
01078 {
01079 expr = instantiate_type (to, expr, tf_none);
01080 if (expr == error_mark_node)
01081 return NULL;
01082 from = TREE_TYPE (expr);
01083 }
01084
01085 if (TREE_CODE (from) == REFERENCE_TYPE)
01086 {
01087
01088 lvalue_p = clk_ordinary;
01089 from = TREE_TYPE (from);
01090 }
01091 else if (expr)
01092 lvalue_p = real_lvalue_p (expr);
01093
01094
01095
01096
01097 related_p = reference_related_p (to, from);
01098 compatible_p = reference_compatible_p (to, from);
01099
01100 if (lvalue_p && compatible_p)
01101 {
01102
01103
01104
01105
01106
01107
01108
01109
01110
01111 conv = build_identity_conv (from, expr);
01112 conv = direct_reference_binding (rto, conv);
01113 if ((lvalue_p & clk_bitfield) != 0
01114 || ((lvalue_p & clk_packed) != 0 && !TYPE_PACKED (to)))
01115
01116
01117
01118
01119
01120
01121
01122
01123
01124
01125
01126 conv->need_temporary_p = true;
01127
01128 return conv;
01129 }
01130 else if (CLASS_TYPE_P (from) && !(flags & LOOKUP_NO_CONVERSION))
01131 {
01132
01133
01134
01135
01136
01137
01138
01139
01140
01141
01142
01143
01144
01145 conv = convert_class_to_reference (to, from, expr);
01146 if (conv)
01147 return conv;
01148 }
01149
01150
01151
01152 if (flags & LOOKUP_NO_TEMP_BIND)
01153 return NULL;
01154
01155
01156
01157
01158
01159
01160
01161
01162
01163
01164
01165
01166
01167
01168
01169 if (!CP_TYPE_CONST_NON_VOLATILE_P (to))
01170 return NULL;
01171
01172
01173
01174
01175
01176
01177
01178
01179
01180
01181
01182
01183
01184
01185
01186
01187
01188 if (CLASS_TYPE_P (from) && compatible_p)
01189 {
01190 conv = build_identity_conv (from, expr);
01191 conv = direct_reference_binding (rto, conv);
01192 if (!(flags & LOOKUP_CONSTRUCTOR_CALLABLE))
01193 conv->u.next->check_copy_constructor_p = true;
01194 return conv;
01195 }
01196
01197
01198
01199
01200
01201
01202
01203
01204 if (related_p && !at_least_as_qualified_p (to, from))
01205 return NULL;
01206
01207 conv = implicit_conversion (to, from, expr, false,
01208 flags);
01209 if (!conv)
01210 return NULL;
01211
01212 conv = build_conv (ck_ref_bind, rto, conv);
01213
01214
01215 conv->need_temporary_p = true;
01216
01217 return conv;
01218 }
01219
01220
01221
01222
01223
01224
01225
01226 static conversion *
01227 implicit_conversion (tree to, tree from, tree expr, bool c_cast_p,
01228 int flags)
01229 {
01230 conversion *conv;
01231
01232 if (from == error_mark_node || to == error_mark_node
01233 || expr == error_mark_node)
01234 return NULL;
01235
01236 if (TREE_CODE (to) == REFERENCE_TYPE)
01237 conv = reference_binding (to, from, expr, flags);
01238 else
01239 conv = standard_conversion (to, from, expr, c_cast_p, flags);
01240
01241 if (conv)
01242 return conv;
01243
01244 if (expr != NULL_TREE
01245 && (IS_AGGR_TYPE (from)
01246 || IS_AGGR_TYPE (to))
01247 && (flags & LOOKUP_NO_CONVERSION) == 0)
01248 {
01249 struct z_candidate *cand;
01250
01251 cand = build_user_type_conversion_1
01252 (to, expr, LOOKUP_ONLYCONVERTING);
01253 if (cand)
01254 conv = cand->second_conv;
01255
01256
01257
01258
01259 return conv;
01260 }
01261
01262 return NULL;
01263 }
01264
01265
01266
01267
01268 static struct z_candidate *
01269 add_candidate (struct z_candidate **candidates,
01270 tree fn, tree args,
01271 size_t num_convs, conversion **convs,
01272 tree access_path, tree conversion_path,
01273 int viable)
01274 {
01275 struct z_candidate *cand
01276 = conversion_obstack_alloc (sizeof (struct z_candidate));
01277
01278 cand->fn = fn;
01279 cand->args = args;
01280 cand->convs = convs;
01281 cand->num_convs = num_convs;
01282 cand->access_path = access_path;
01283 cand->conversion_path = conversion_path;
01284 cand->viable = viable;
01285 cand->next = *candidates;
01286 *candidates = cand;
01287
01288 return cand;
01289 }
01290
01291
01292
01293
01294
01295
01296
01297
01298 static struct z_candidate *
01299 add_function_candidate (struct z_candidate **candidates,
01300 tree fn, tree ctype, tree arglist,
01301 tree access_path, tree conversion_path,
01302 int flags)
01303 {
01304 tree parmlist = TYPE_ARG_TYPES (TREE_TYPE (fn));
01305 int i, len;
01306 conversion **convs;
01307 tree parmnode, argnode;
01308 tree orig_arglist;
01309 int viable = 1;
01310
01311
01312
01313 if (DECL_ANTICIPATED (fn))
01314 return NULL;
01315
01316
01317
01318 if (DECL_CONSTRUCTOR_P (fn))
01319 {
01320 parmlist = skip_artificial_parms_for (fn, parmlist);
01321 orig_arglist = arglist;
01322 arglist = skip_artificial_parms_for (fn, arglist);
01323 }
01324 else
01325 orig_arglist = arglist;
01326
01327 len = list_length (arglist);
01328 convs = alloc_conversions (len);
01329
01330
01331
01332
01333
01334
01335
01336
01337 parmnode = parmlist;
01338 for (i = 0; i < len; ++i)
01339 {
01340 if (parmnode == NULL_TREE || parmnode == void_list_node)
01341 break;
01342 parmnode = TREE_CHAIN (parmnode);
01343 }
01344
01345 if (i < len && parmnode)
01346 viable = 0;
01347
01348
01349 else if (!sufficient_parms_p (parmnode))
01350 viable = 0;
01351
01352 if (! viable)
01353 goto out;
01354
01355
01356
01357
01358
01359 parmnode = parmlist;
01360 argnode = arglist;
01361
01362 for (i = 0; i < len; ++i)
01363 {
01364 tree arg = TREE_VALUE (argnode);
01365 tree argtype = lvalue_type (arg);
01366 conversion *t;
01367 int is_this;
01368
01369 if (parmnode == void_list_node)
01370 break;
01371
01372 is_this = (i == 0 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
01373 && ! DECL_CONSTRUCTOR_P (fn));
01374
01375 if (parmnode)
01376 {
01377 tree parmtype = TREE_VALUE (parmnode);
01378
01379
01380
01381
01382
01383
01384
01385
01386
01387
01388 if (ctype && is_this)
01389 {
01390 parmtype
01391 = build_qualified_type (ctype,
01392 TYPE_QUALS (TREE_TYPE (parmtype)));
01393 parmtype = build_pointer_type (parmtype);
01394 }
01395
01396 t = implicit_conversion (parmtype, argtype, arg,
01397 false, flags);
01398 }
01399 else
01400 {
01401 t = build_identity_conv (argtype, arg);
01402 t->ellipsis_p = true;
01403 }
01404
01405 if (t && is_this)
01406 t->this_p = true;
01407
01408 convs[i] = t;
01409 if (! t)
01410 {
01411 viable = 0;
01412 break;
01413 }
01414
01415 if (t->bad_p)
01416 viable = -1;
01417
01418 if (parmnode)
01419 parmnode = TREE_CHAIN (parmnode);
01420 argnode = TREE_CHAIN (argnode);
01421 }
01422
01423 out:
01424 return add_candidate (candidates, fn, orig_arglist, len, convs,
01425 access_path, conversion_path, viable);
01426 }
01427
01428
01429
01430
01431
01432
01433
01434
01435
01436
01437
01438
01439 static struct z_candidate *
01440 add_conv_candidate (struct z_candidate **candidates, tree fn, tree obj,
01441 tree arglist, tree access_path, tree conversion_path)
01442 {
01443 tree totype = TREE_TYPE (TREE_TYPE (fn));
01444 int i, len, viable, flags;
01445 tree parmlist, parmnode, argnode;
01446 conversion **convs;
01447
01448 for (parmlist = totype; TREE_CODE (parmlist) != FUNCTION_TYPE; )
01449 parmlist = TREE_TYPE (parmlist);
01450 parmlist = TYPE_ARG_TYPES (parmlist);
01451
01452 len = list_length (arglist) + 1;
01453 convs = alloc_conversions (len);
01454 parmnode = parmlist;
01455 argnode = arglist;
01456 viable = 1;
01457 flags = LOOKUP_NORMAL;
01458
01459
01460 if (*candidates && (*candidates)->fn == totype)
01461 return NULL;
01462
01463 for (i = 0; i < len; ++i)
01464 {
01465 tree arg = i == 0 ? obj : TREE_VALUE (argnode);
01466 tree argtype = lvalue_type (arg);
01467 conversion *t;
01468
01469 if (i == 0)
01470 t = implicit_conversion (totype, argtype, arg, false,
01471 flags);
01472 else if (parmnode == void_list_node)
01473 break;
01474 else if (parmnode)
01475 t = implicit_conversion (TREE_VALUE (parmnode), argtype, arg,
01476 false, flags);
01477 else
01478 {
01479 t = build_identity_conv (argtype, arg);
01480 t->ellipsis_p = true;
01481 }
01482
01483 convs[i] = t;
01484 if (! t)
01485 break;
01486
01487 if (t->bad_p)
01488 viable = -1;
01489
01490 if (i == 0)
01491 continue;
01492
01493 if (parmnode)
01494 parmnode = TREE_CHAIN (parmnode);
01495 argnode = TREE_CHAIN (argnode);
01496 }
01497
01498 if (i < len)
01499 viable = 0;
01500
01501 if (!sufficient_parms_p (parmnode))
01502 viable = 0;
01503
01504 return add_candidate (candidates, totype, arglist, len, convs,
01505 access_path, conversion_path, viable);
01506 }
01507
01508 static void
01509 build_builtin_candidate (struct z_candidate **candidates, tree fnname,
01510 tree type1, tree type2, tree *args, tree *argtypes,
01511 int flags)
01512 {
01513 conversion *t;
01514 conversion **convs;
01515 size_t num_convs;
01516 int viable = 1, i;
01517 tree types[2];
01518
01519 types[0] = type1;
01520 types[1] = type2;
01521
01522 num_convs = args[2] ? 3 : (args[1] ? 2 : 1);
01523 convs = alloc_conversions (num_convs);
01524
01525 for (i = 0; i < 2; ++i)
01526 {
01527 if (! args[i])
01528 break;
01529
01530 t = implicit_conversion (types[i], argtypes[i], args[i],
01531 false, flags);
01532 if (! t)
01533 {
01534 viable = 0;
01535
01536 t = build_identity_conv (types[i], NULL_TREE);
01537 }
01538 else if (t->bad_p)
01539 viable = 0;
01540 convs[i] = t;
01541 }
01542
01543
01544 if (args[2])
01545 {
01546 convs[2] = convs[1];
01547 convs[1] = convs[0];
01548 t = implicit_conversion (boolean_type_node, argtypes[2], args[2],
01549 false, flags);
01550 if (t)
01551 convs[0] = t;
01552 else
01553 viable = 0;
01554 }
01555
01556 add_candidate (candidates, fnname, NULL_TREE,
01557 num_convs, convs,
01558 NULL_TREE,
01559 NULL_TREE,
01560 viable);
01561 }
01562
01563 static bool
01564 is_complete (tree t)
01565 {
01566 return COMPLETE_TYPE_P (complete_type (t));
01567 }
01568
01569
01570
01571 static bool
01572 promoted_arithmetic_type_p (tree type)
01573 {
01574
01575
01576
01577
01578
01579
01580
01581 return ((INTEGRAL_TYPE_P (type)
01582 && same_type_p (type_promotes_to (type), type))
01583 || TREE_CODE (type) == REAL_TYPE);
01584 }
01585
01586
01587
01588
01589
01590
01591
01592
01593
01594
01595
01596 static void
01597 add_builtin_candidate (struct z_candidate **candidates, enum tree_code code,
01598 enum tree_code code2, tree fnname, tree type1,
01599 tree type2, tree *args, tree *argtypes, int flags)
01600 {
01601 switch (code)
01602 {
01603 case POSTINCREMENT_EXPR:
01604 case POSTDECREMENT_EXPR:
01605 args[1] = integer_zero_node;
01606 type2 = integer_type_node;
01607 break;
01608 default:
01609 break;
01610 }
01611
01612 switch (code)
01613 {
01614
01615
01616
01617
01618
01619
01620
01621
01622
01623
01624
01625
01626
01627
01628
01629
01630
01631
01632
01633 case POSTDECREMENT_EXPR:
01634 case PREDECREMENT_EXPR:
01635 if (TREE_CODE (type1) == BOOLEAN_TYPE)
01636 return;
01637 case POSTINCREMENT_EXPR:
01638 case PREINCREMENT_EXPR:
01639 if (ARITHMETIC_TYPE_P (type1) || TYPE_PTROB_P (type1))
01640 {
01641 type1 = build_reference_type (type1);
01642 break;
01643 }
01644 return;
01645
01646
01647
01648
01649
01650
01651
01652
01653
01654
01655 case INDIRECT_REF:
01656 if (TREE_CODE (type1) == POINTER_TYPE
01657 && (TYPE_PTROB_P (type1)
01658 || TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE))
01659 break;
01660 return;
01661
01662
01663
01664
01665
01666
01667
01668
01669
01670 case CONVERT_EXPR:
01671 if (TREE_CODE (type1) == POINTER_TYPE)
01672 break;
01673 case NEGATE_EXPR:
01674 if (ARITHMETIC_TYPE_P (type1))
01675 break;
01676 return;
01677
01678
01679
01680
01681
01682 case BIT_NOT_EXPR:
01683 if (INTEGRAL_TYPE_P (type1))
01684 break;
01685 return;
01686
01687
01688
01689
01690
01691
01692
01693
01694 case MEMBER_REF:
01695 if (TREE_CODE (type1) == POINTER_TYPE
01696 && TYPE_PTR_TO_MEMBER_P (type2))
01697 {
01698 tree c1 = TREE_TYPE (type1);
01699 tree c2 = TYPE_PTRMEM_CLASS_TYPE (type2);
01700
01701 if (IS_AGGR_TYPE (c1) && DERIVED_FROM_P (c2, c1)
01702 && (TYPE_PTRMEMFUNC_P (type2)
01703 || is_complete (TYPE_PTRMEM_POINTED_TO_TYPE (type2))))
01704 break;
01705 }
01706 return;
01707
01708
01709
01710
01711
01712
01713
01714
01715
01716
01717
01718
01719
01720
01721
01722
01723
01724
01725
01726
01727
01728
01729
01730
01731
01732
01733
01734
01735
01736
01737
01738
01739
01740
01741
01742
01743
01744
01745
01746
01747
01748
01749
01750 case MINUS_EXPR:
01751 if (TYPE_PTROB_P (type1) && TYPE_PTROB_P (type2))
01752 break;
01753 if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2))
01754 {
01755 type2 = ptrdiff_type_node;
01756 break;
01757 }
01758 case MULT_EXPR:
01759 case TRUNC_DIV_EXPR:
01760 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
01761 break;
01762 return;
01763
01764 case EQ_EXPR:
01765 case NE_EXPR:
01766 if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2))
01767 || (TYPE_PTRMEM_P (type1) && TYPE_PTRMEM_P (type2)))
01768 break;
01769 if (TYPE_PTR_TO_MEMBER_P (type1) && null_ptr_cst_p (args[1]))
01770 {
01771 type2 = type1;
01772 break;
01773 }
01774 if (TYPE_PTR_TO_MEMBER_P (type2) && null_ptr_cst_p (args[0]))
01775 {
01776 type1 = type2;
01777 break;
01778 }
01779
01780 case LT_EXPR:
01781 case GT_EXPR:
01782 case LE_EXPR:
01783 case GE_EXPR:
01784 case MAX_EXPR:
01785 case MIN_EXPR:
01786 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
01787 break;
01788 if (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
01789 break;
01790 if (TREE_CODE (type1) == ENUMERAL_TYPE && TREE_CODE (type2) == ENUMERAL_TYPE)
01791 break;
01792 if (TYPE_PTR_P (type1) && null_ptr_cst_p (args[1]))
01793 {
01794 type2 = type1;
01795 break;
01796 }
01797 if (null_ptr_cst_p (args[0]) && TYPE_PTR_P (type2))
01798 {
01799 type1 = type2;
01800 break;
01801 }
01802 return;
01803
01804 case PLUS_EXPR:
01805 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
01806 break;
01807 case ARRAY_REF:
01808 if (INTEGRAL_TYPE_P (type1) && TYPE_PTROB_P (type2))
01809 {
01810 type1 = ptrdiff_type_node;
01811 break;
01812 }
01813 if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2))
01814 {
01815 type2 = ptrdiff_type_node;
01816 break;
01817 }
01818 return;
01819
01820
01821
01822
01823
01824
01825
01826
01827
01828
01829
01830
01831 case TRUNC_MOD_EXPR:
01832 case BIT_AND_EXPR:
01833 case BIT_IOR_EXPR:
01834 case BIT_XOR_EXPR:
01835 case LSHIFT_EXPR:
01836 case RSHIFT_EXPR:
01837 if (INTEGRAL_TYPE_P (type1) && INTEGRAL_TYPE_P (type2))
01838 break;
01839 return;
01840
01841
01842
01843
01844
01845
01846
01847
01848
01849
01850
01851
01852
01853
01854
01855
01856
01857
01858
01859
01860
01861
01862
01863
01864
01865
01866
01867
01868
01869
01870
01871
01872
01873
01874
01875
01876
01877 case MODIFY_EXPR:
01878 switch (code2)
01879 {
01880 case PLUS_EXPR:
01881 case MINUS_EXPR:
01882 if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2))
01883 {
01884 type2 = ptrdiff_type_node;
01885 break;
01886 }
01887 case MULT_EXPR:
01888 case TRUNC_DIV_EXPR:
01889 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
01890 break;
01891 return;
01892
01893 case TRUNC_MOD_EXPR:
01894 case BIT_AND_EXPR:
01895 case BIT_IOR_EXPR:
01896 case BIT_XOR_EXPR:
01897 case LSHIFT_EXPR:
01898 case RSHIFT_EXPR:
01899 if (INTEGRAL_TYPE_P (type1) && INTEGRAL_TYPE_P (type2))
01900 break;
01901 return;
01902
01903 case NOP_EXPR:
01904 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
01905 break;
01906 if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2))
01907 || (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
01908 || (TYPE_PTRMEM_P (type1) && TYPE_PTRMEM_P (type2))
01909 || ((TYPE_PTRMEMFUNC_P (type1)
01910 || TREE_CODE (type1) == POINTER_TYPE)
01911 && null_ptr_cst_p (args[1])))
01912 {
01913 type2 = type1;
01914 break;
01915 }
01916 return;
01917
01918 default:
01919 gcc_unreachable ();
01920 }
01921 type1 = build_reference_type (type1);
01922 break;
01923
01924 case COND_EXPR:
01925
01926
01927
01928
01929
01930
01931
01932
01933
01934
01935
01936
01937
01938
01939 if (promoted_arithmetic_type_p (type1)
01940 && promoted_arithmetic_type_p (type2))
01941
01942 break;
01943
01944
01945 if (!(TYPE_PTR_P (type1) || TYPE_PTR_TO_MEMBER_P (type1))
01946 || !(TYPE_PTR_P (type2) || TYPE_PTR_TO_MEMBER_P (type2)))
01947 return;
01948
01949
01950
01951
01952
01953 break;
01954
01955 default:
01956 gcc_unreachable ();
01957 }
01958
01959
01960
01961 if (type2 && !same_type_p (type1, type2)
01962 && TREE_CODE (type1) == TREE_CODE (type2)
01963 && (TREE_CODE (type1) == REFERENCE_TYPE
01964 || (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
01965 || (TYPE_PTRMEM_P (type1) && TYPE_PTRMEM_P (type2))
01966 || TYPE_PTRMEMFUNC_P (type1)
01967 || IS_AGGR_TYPE (type1)
01968 || TREE_CODE (type1) == ENUMERAL_TYPE))
01969 {
01970 build_builtin_candidate
01971 (candidates, fnname, type1, type1, args, argtypes, flags);
01972 build_builtin_candidate
01973 (candidates, fnname, type2, type2, args, argtypes, flags);
01974 return;
01975 }
01976
01977 build_builtin_candidate
01978 (candidates, fnname, type1, type2, args, argtypes, flags);
01979 }
01980
01981 tree
01982 type_decays_to (tree type)
01983 {
01984 if (TREE_CODE (type) == ARRAY_TYPE)
01985 return build_pointer_type (TREE_TYPE (type));
01986 if (TREE_CODE (type) == FUNCTION_TYPE)
01987 return build_pointer_type (type);
01988 return type;
01989 }
01990
01991
01992
01993
01994
01995
01996
01997
01998
01999
02000
02001
02002
02003
02004 static void
02005 add_builtin_candidates (struct z_candidate **candidates, enum tree_code code,
02006 enum tree_code code2, tree fnname, tree *args,
02007 int flags)
02008 {
02009 int ref1, i;
02010 int enum_p = 0;
02011 tree type, argtypes[3];
02012
02013
02014
02015
02016 tree types[2];
02017
02018 for (i = 0; i < 3; ++i)
02019 {
02020 if (args[i])
02021 argtypes[i] = lvalue_type (args[i]);
02022 else
02023 argtypes[i] = NULL_TREE;
02024 }
02025
02026 switch (code)
02027 {
02028
02029
02030
02031
02032
02033 case POSTINCREMENT_EXPR:
02034 case PREINCREMENT_EXPR:
02035 case POSTDECREMENT_EXPR:
02036 case PREDECREMENT_EXPR:
02037 case MODIFY_EXPR:
02038 ref1 = 1;
02039 break;
02040
02041
02042
02043
02044
02045
02046 case TRUTH_NOT_EXPR:
02047 build_builtin_candidate
02048 (candidates, fnname, boolean_type_node,
02049 NULL_TREE, args, argtypes, flags);
02050 return;
02051
02052 case TRUTH_ORIF_EXPR:
02053 case TRUTH_ANDIF_EXPR:
02054 build_builtin_candidate
02055 (candidates, fnname, boolean_type_node,
02056 boolean_type_node, args, argtypes, flags);
02057 return;
02058
02059 case ADDR_EXPR:
02060 case COMPOUND_EXPR:
02061 case COMPONENT_REF:
02062 return;
02063
02064 case COND_EXPR:
02065 case EQ_EXPR:
02066 case NE_EXPR:
02067 case LT_EXPR:
02068 case LE_EXPR:
02069 case GT_EXPR:
02070 case GE_EXPR:
02071 enum_p = 1;
02072
02073
02074 default:
02075 ref1 = 0;
02076 }
02077
02078 types[0] = types[1] = NULL_TREE;
02079
02080 for (i = 0; i < 2; ++i)
02081 {
02082 if (! args[i])
02083 ;
02084 else if (IS_AGGR_TYPE (argtypes[i]))
02085 {
02086 tree convs;
02087
02088 if (i == 0 && code == MODIFY_EXPR && code2 == NOP_EXPR)
02089 return;
02090
02091 convs = lookup_conversions (argtypes[i]);
02092
02093 if (code == COND_EXPR)
02094 {
02095 if (real_lvalue_p (args[i]))
02096 types[i] = tree_cons
02097 (NULL_TREE, build_reference_type (argtypes[i]), types[i]);
02098
02099 types[i] = tree_cons
02100 (NULL_TREE, TYPE_MAIN_VARIANT (argtypes[i]), types[i]);
02101 }
02102
02103 else if (! convs)
02104 return;
02105
02106 for (; convs; convs = TREE_CHAIN (convs))
02107 {
02108 type = TREE_TYPE (TREE_TYPE (OVL_CURRENT (TREE_VALUE (convs))));
02109
02110 if (i == 0 && ref1
02111 && (TREE_CODE (type) != REFERENCE_TYPE
02112 || CP_TYPE_CONST_P (TREE_TYPE (type))))
02113 continue;
02114
02115 if (code == COND_EXPR && TREE_CODE (type) == REFERENCE_TYPE)
02116 types[i] = tree_cons (NULL_TREE, type, types[i]);
02117
02118 type = non_reference (type);
02119 if (i != 0 || ! ref1)
02120 {
02121 type = TYPE_MAIN_VARIANT (type_decays_to (type));
02122 if (enum_p && TREE_CODE (type) == ENUMERAL_TYPE)
02123 types[i] = tree_cons (NULL_TREE, type, types[i]);
02124 if (INTEGRAL_TYPE_P (type))
02125 type = type_promotes_to (type);
02126 }
02127
02128 if (! value_member (type, types[i]))
02129 types[i] = tree_cons (NULL_TREE, type, types[i]);
02130 }
02131 }
02132 else
02133 {
02134 if (code == COND_EXPR && real_lvalue_p (args[i]))
02135 types[i] = tree_cons
02136 (NULL_TREE, build_reference_type (argtypes[i]), types[i]);
02137 type = non_reference (argtypes[i]);
02138 if (i != 0 || ! ref1)
02139 {
02140 type = TYPE_MAIN_VARIANT (type_decays_to (type));
02141 if (enum_p && TREE_CODE (type) == ENUMERAL_TYPE)
02142 types[i] = tree_cons (NULL_TREE, type, types[i]);
02143 if (INTEGRAL_TYPE_P (type))
02144 type = type_promotes_to (type);
02145 }
02146 types[i] = tree_cons (NULL_TREE, type, types[i]);
02147 }
02148 }
02149
02150
02151
02152 for (; types[0]; types[0] = TREE_CHAIN (types[0]))
02153 {
02154 if (types[1])
02155 for (type = types[1]; type; type = TREE_CHAIN (type))
02156 add_builtin_candidate
02157 (candidates, code, code2, fnname, TREE_VALUE (types[0]),
02158 TREE_VALUE (type), args, argtypes, flags);
02159 else
02160 add_builtin_candidate
02161 (candidates, code, code2, fnname, TREE_VALUE (types[0]),
02162 NULL_TREE, args, argtypes, flags);
02163 }
02164
02165 return;
02166 }
02167
02168
02169
02170
02171
02172
02173
02174
02175
02176
02177
02178
02179 static struct z_candidate*
02180 add_template_candidate_real (struct z_candidate **candidates, tree tmpl,
02181 tree ctype, tree explicit_targs, tree arglist,
02182 tree return_type, tree access_path,
02183 tree conversion_path, int flags, tree obj,
02184 unification_kind_t strict)
02185 {
02186 int ntparms = DECL_NTPARMS (tmpl);
02187 tree targs = make_tree_vec (ntparms);
02188 tree args_without_in_chrg = arglist;
02189 struct z_candidate *cand;
02190 int i;
02191 tree fn;
02192
02193
02194
02195 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (tmpl))
02196 args_without_in_chrg = TREE_CHAIN (args_without_in_chrg);
02197
02198 if ((DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (tmpl)
02199 || DECL_BASE_CONSTRUCTOR_P (tmpl))
02200 && CLASSTYPE_VBASECLASSES (DECL_CONTEXT (tmpl)))
02201 args_without_in_chrg = TREE_CHAIN (args_without_in_chrg);
02202
02203 i = fn_type_unification (tmpl, explicit_targs, targs,
02204 args_without_in_chrg,
02205 return_type, strict, -1);
02206
02207 if (i != 0)
02208 return NULL;
02209
02210 fn = instantiate_template (tmpl, targs, tf_none);
02211 if (fn == error_mark_node)
02212 return NULL;
02213
02214
02215
02216
02217
02218
02219
02220
02221
02222
02223
02224
02225
02226
02227
02228
02229
02230
02231
02232
02233
02234
02235
02236 if (DECL_CONSTRUCTOR_P (fn) && list_length (arglist) == 2)
02237 {
02238 tree arg_types = FUNCTION_FIRST_USER_PARMTYPE (fn);
02239 if (arg_types && same_type_p (TYPE_MAIN_VARIANT (TREE_VALUE (arg_types)),
02240 ctype))
02241 return NULL;
02242 }
02243
02244 if (obj != NULL_TREE)
02245
02246 cand = add_conv_candidate (candidates, fn, obj, access_path,
02247 conversion_path, arglist);
02248 else
02249 cand = add_function_candidate (candidates, fn, ctype,
02250 arglist, access_path,
02251 conversion_path, flags);
02252 if (DECL_TI_TEMPLATE (fn) != tmpl)
02253
02254
02255
02256
02257
02258
02259
02260
02261
02262
02263
02264
02265
02266
02267
02268
02269
02270 cand->template_decl = tree_cons (tmpl, targs, NULL_TREE);
02271 else
02272 cand->template_decl = DECL_TEMPLATE_INFO (fn);
02273
02274 return cand;
02275 }
02276
02277
02278 static struct z_candidate *
02279 add_template_candidate (struct z_candidate **candidates, tree tmpl, tree ctype,
02280 tree explicit_targs, tree arglist, tree return_type,
02281 tree access_path, tree conversion_path, int flags,
02282 unification_kind_t strict)
02283 {
02284 return
02285 add_template_candidate_real (candidates, tmpl, ctype,
02286 explicit_targs, arglist, return_type,
02287 access_path, conversion_path,
02288 flags, NULL_TREE, strict);
02289 }
02290
02291
02292 static struct z_candidate *
02293 add_template_conv_candidate (struct z_candidate **candidates, tree tmpl,
02294 tree obj, tree arglist, tree return_type,
02295 tree access_path, tree conversion_path)
02296 {
02297 return
02298 add_template_candidate_real (candidates, tmpl, NULL_TREE, NULL_TREE,
02299 arglist, return_type, access_path,
02300 conversion_path, 0, obj, DEDUCE_CONV);
02301 }
02302
02303
02304
02305
02306
02307
02308
02309 static struct z_candidate*
02310 splice_viable (struct z_candidate *cands,
02311 bool strict_p,
02312 bool *any_viable_p)
02313 {
02314 struct z_candidate *viable;
02315 struct z_candidate **last_viable;
02316 struct z_candidate **cand;
02317
02318 viable = NULL;
02319 last_viable = &viable;
02320 *any_viable_p = false;
02321
02322 cand = &cands;
02323 while (*cand)
02324 {
02325 struct z_candidate *c = *cand;
02326 if (strict_p ? c->viable == 1 : c->viable)
02327 {
02328 *last_viable = c;
02329 *cand = c->next;
02330 c->next = NULL;
02331 last_viable = &c->next;
02332 *any_viable_p = true;
02333 }
02334 else
02335 cand = &c->next;
02336 }
02337
02338 return viable ? viable : cands;
02339 }
02340
02341 static bool
02342 any_strictly_viable (struct z_candidate *cands)
02343 {
02344 for (; cands; cands = cands->next)
02345 if (cands->viable == 1)
02346 return true;
02347 return false;
02348 }
02349
02350
02351
02352
02353
02354 static tree
02355 build_this (tree obj)
02356 {
02357
02358
02359 if (processing_template_decl)
02360 return build_address (obj);
02361
02362 return build_unary_op (ADDR_EXPR, obj, 0);
02363 }
02364
02365
02366
02367
02368
02369 static inline int
02370 equal_functions (tree fn1, tree fn2)
02371 {
02372 if (DECL_LOCAL_FUNCTION_P (fn1) || DECL_LOCAL_FUNCTION_P (fn2)
02373 || DECL_EXTERN_C_FUNCTION_P (fn1))
02374 return decls_match (fn1, fn2);
02375 return fn1 == fn2;
02376 }
02377
02378
02379
02380
02381
02382
02383
02384
02385 static void
02386 print_z_candidate (const char *msgstr, struct z_candidate *candidate)
02387 {
02388 if (TREE_CODE (candidate->fn) == IDENTIFIER_NODE)
02389 {
02390 if (candidate->num_convs == 3)
02391 inform ("%s %D(%T, %T, %T) <built-in>", msgstr, candidate->fn,
02392 candidate->convs[0]->type,
02393 candidate->convs[1]->type,
02394 candidate->convs[2]->type);
02395 else if (candidate->num_convs == 2)
02396 inform ("%s %D(%T, %T) <built-in>", msgstr, candidate->fn,
02397 candidate->convs[0]->type,
02398 candidate->convs[1]->type);
02399 else
02400 inform ("%s %D(%T) <built-in>", msgstr, candidate->fn,
02401 candidate->convs[0]->type);
02402 }
02403 else if (TYPE_P (candidate->fn))
02404 inform ("%s %T <conversion>", msgstr, candidate->fn);
02405 else if (candidate->viable == -1)
02406 inform ("%J%s %+#D <near match>", candidate->fn, msgstr, candidate->fn);
02407 else
02408 inform ("%J%s %+#D", candidate->fn, msgstr, candidate->fn);
02409 }
02410
02411 static void
02412 print_z_candidates (struct z_candidate *candidates)
02413 {
02414 const char *str;
02415 struct z_candidate *cand1;
02416 struct z_candidate **cand2;
02417
02418
02419
02420
02421
02422
02423 for (cand1 = candidates; cand1; cand1 = cand1->next)
02424 {
02425 tree fn = cand1->fn;
02426
02427 if (TREE_CODE (fn) != FUNCTION_DECL)
02428 continue;
02429 cand2 = &cand1->next;
02430 while (*cand2)
02431 {
02432 if (TREE_CODE ((*cand2)->fn) == FUNCTION_DECL
02433 && equal_functions (fn, (*cand2)->fn))
02434 *cand2 = (*cand2)->next;
02435 else
02436 cand2 = &(*cand2)->next;
02437 }
02438 }
02439
02440 if (!candidates)
02441 return;
02442
02443 str = _("candidates are:");
02444 print_z_candidate (str, candidates);
02445 if (candidates->next)
02446 {
02447
02448
02449 size_t len = gcc_gettext_width (str) + 1;
02450 char *spaces = alloca (len);
02451 memset (spaces, ' ', len-1);
02452 spaces[len - 1] = '\0';
02453
02454 candidates = candidates->next;
02455 do
02456 {
02457 print_z_candidate (spaces, candidates);
02458 candidates = candidates->next;
02459 }
02460 while (candidates);
02461 }
02462 }
02463
02464
02465
02466
02467
02468
02469
02470 static conversion *
02471 merge_conversion_sequences (conversion *user_seq, conversion *std_seq)
02472 {
02473 conversion **t;
02474
02475 gcc_assert (user_seq->kind == ck_user);
02476
02477
02478 t = &(std_seq);
02479 while ((*t)->kind != ck_identity)
02480 t = &((*t)->u.next);
02481
02482
02483
02484 *t = user_seq;
02485
02486
02487 std_seq->user_conv_p = true;
02488
02489 return std_seq;
02490 }
02491
02492
02493
02494
02495
02496
02497
02498 static struct z_candidate *
02499 build_user_type_conversion_1 (tree totype, tree expr, int flags)
02500 {
02501 struct z_candidate *candidates, *cand;
02502 tree fromtype = TREE_TYPE (expr);
02503 tree ctors = NULL_TREE;
02504 tree conv_fns = NULL_TREE;
02505 conversion *conv = NULL;
02506 tree args = NULL_TREE;
02507 bool any_viable_p;
02508
02509
02510
02511
02512 gcc_assert (!IS_AGGR_TYPE (fromtype) || !IS_AGGR_TYPE (totype)
02513 || !DERIVED_FROM_P (totype, fromtype));
02514
02515 if (IS_AGGR_TYPE (totype))
02516 ctors = lookup_fnfields (totype, complete_ctor_identifier, 0);
02517
02518 if (IS_AGGR_TYPE (fromtype))
02519 conv_fns = lookup_conversions (fromtype);
02520
02521 candidates = 0;
02522 flags |= LOOKUP_NO_CONVERSION;
02523
02524 if (ctors)
02525 {
02526 tree t;
02527
02528 ctors = BASELINK_FUNCTIONS (ctors);
02529
02530 t = build_int_cst (build_pointer_type (totype), 0);
02531 args = build_tree_list (NULL_TREE, expr);
02532
02533
02534 gcc_assert (!DECL_HAS_IN_CHARGE_PARM_P (OVL_CURRENT (ctors))
02535 && !DECL_HAS_VTT_PARM_P (OVL_CURRENT (ctors)));
02536 args = tree_cons (NULL_TREE, t, args);
02537 }
02538 for (; ctors; ctors = OVL_NEXT (ctors))
02539 {
02540 tree ctor = OVL_CURRENT (ctors);
02541 if (DECL_NONCONVERTING_P (ctor))
02542 continue;
02543
02544 if (TREE_CODE (ctor) == TEMPLATE_DECL)
02545 cand = add_template_candidate (&candidates, ctor, totype,
02546 NULL_TREE, args, NULL_TREE,
02547 TYPE_BINFO (totype),
02548 TYPE_BINFO (totype),
02549 flags,
02550 DEDUCE_CALL);
02551 else
02552 cand = add_function_candidate (&candidates, ctor, totype,
02553 args, TYPE_BINFO (totype),
02554 TYPE_BINFO (totype),
02555 flags);
02556
02557 if (cand)
02558 cand->second_conv = build_identity_conv (totype, NULL_TREE);
02559 }
02560
02561 if (conv_fns)
02562 args = build_tree_list (NULL_TREE, build_this (expr));
02563
02564 for (; conv_fns; conv_fns = TREE_CHAIN (conv_fns))
02565 {
02566 tree fns;
02567 tree conversion_path = TREE_PURPOSE (conv_fns);
02568 int convflags = LOOKUP_NO_CONVERSION;
02569
02570
02571
02572
02573
02574 if (TREE_CODE (totype) == REFERENCE_TYPE)
02575 convflags |= LOOKUP_NO_TEMP_BIND;
02576
02577 for (fns = TREE_VALUE (conv_fns); fns; fns = OVL_NEXT (fns))
02578 {
02579 tree fn = OVL_CURRENT (fns);
02580
02581
02582
02583
02584
02585
02586
02587
02588 if (TREE_CODE (fn) == TEMPLATE_DECL)
02589 cand = add_template_candidate (&candidates, fn, fromtype,
02590 NULL_TREE,
02591 args, totype,
02592 TYPE_BINFO (fromtype),
02593 conversion_path,
02594 flags,
02595 DEDUCE_CONV);
02596 else
02597 cand = add_function_candidate (&candidates, fn, fromtype,
02598 args,
02599 TYPE_BINFO (fromtype),
02600 conversion_path,
02601 flags);
02602
02603 if (cand)
02604 {
02605 conversion *ics
02606 = implicit_conversion (totype,
02607 TREE_TYPE (TREE_TYPE (cand->fn)),
02608 0,
02609 false, convflags);
02610
02611 cand->second_conv = ics;
02612
02613 if (!ics)
02614 cand->viable = 0;
02615 else if (candidates->viable == 1 && ics->bad_p)
02616 cand->viable = -1;
02617 }
02618 }
02619 }
02620
02621 candidates = splice_viable (candidates, pedantic, &any_viable_p);
02622 if (!any_viable_p)
02623 return 0;
02624
02625 cand = tourney (candidates);
02626 if (cand == 0)
02627 {
02628 if (flags & LOOKUP_COMPLAIN)
02629 {
02630 error ("conversion from %qT to %qT is ambiguous",
02631 fromtype, totype);
02632 print_z_candidates (candidates);
02633 }
02634
02635 cand = candidates;
02636 cand->second_conv = build_ambiguous_conv (totype, expr);
02637 cand->second_conv->user_conv_p = true;
02638 if (!any_strictly_viable (candidates))
02639 cand->second_conv->bad_p = true;
02640
02641
02642
02643
02644 return cand;
02645 }
02646
02647
02648 conv = build_conv
02649 (ck_user,
02650 (DECL_CONSTRUCTOR_P (cand->fn)
02651 ? totype : non_reference (TREE_TYPE (TREE_TYPE (cand->fn)))),
02652 build_identity_conv (TREE_TYPE (expr), expr));
02653 conv->cand = cand;
02654
02655
02656 cand->second_conv = merge_conversion_sequences (conv,
02657 cand->second_conv);
02658
02659 if (cand->viable == -1)
02660 cand->second_conv->bad_p = true;
02661
02662 return cand;
02663 }
02664
02665 tree
02666 build_user_type_conversion (tree totype, tree expr, int flags)
02667 {
02668 struct z_candidate *cand
02669 = build_user_type_conversion_1 (totype, expr, flags);
02670
02671 if (cand)
02672 {
02673 if (cand->second_conv->kind == ck_ambig)
02674 return error_mark_node;
02675 expr = convert_like (cand->second_conv, expr);
02676 return convert_from_reference (expr);
02677 }
02678 return NULL_TREE;
02679 }
02680
02681
02682
02683 static tree
02684 resolve_args (tree args)
02685 {
02686 tree t;
02687 for (t = args; t; t = TREE_CHAIN (t))
02688 {
02689 tree arg = TREE_VALUE (t);
02690
02691 if (arg == error_mark_node)
02692 return error_mark_node;
02693 else if (VOID_TYPE_P (TREE_TYPE (arg)))
02694 {
02695 error ("invalid use of void expression");
02696 return error_mark_node;
02697 }
02698 }
02699 return args;
02700 }
02701
02702
02703
02704
02705
02706
02707
02708
02709
02710
02711
02712
02713
02714 static struct z_candidate *
02715 perform_overload_resolution (tree fn,
02716 tree args,
02717 struct z_candidate **candidates,
02718 bool *any_viable_p)
02719 {
02720 struct z_candidate *cand;
02721 tree explicit_targs = NULL_TREE;
02722 int template_only = 0;
02723
02724 *candidates = NULL;
02725 *any_viable_p = true;
02726
02727
02728 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
02729 || TREE_CODE (fn) == TEMPLATE_DECL
02730 || TREE_CODE (fn) == OVERLOAD
02731 || TREE_CODE (fn) == TEMPLATE_ID_EXPR);
02732 gcc_assert (!args || TREE_CODE (args) == TREE_LIST);
02733
02734 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
02735 {
02736 explicit_targs = TREE_OPERAND (fn, 1);
02737 fn = TREE_OPERAND (fn, 0);
02738 template_only = 1;
02739 }
02740
02741
02742 add_candidates (fn, args, explicit_targs, template_only,
02743 NULL_TREE,
02744 NULL_TREE,
02745 LOOKUP_NORMAL,
02746 candidates);
02747
02748 *candidates = splice_viable (*candidates, pedantic, any_viable_p);
02749 if (!*any_viable_p)
02750 return NULL;
02751
02752 cand = tourney (*candidates);
02753 return cand;
02754 }
02755
02756
02757
02758
02759 tree
02760 build_new_function_call (tree fn, tree args)
02761 {
02762 struct z_candidate *candidates, *cand;
02763 bool any_viable_p;
02764 void *p;
02765 tree result;
02766
02767 args = resolve_args (args);
02768 if (args == error_mark_node)
02769 return error_mark_node;
02770
02771
02772 p = conversion_obstack_alloc (0);
02773
02774 cand = perform_overload_resolution (fn, args, &candidates, &any_viable_p);
02775
02776 if (!cand)
02777 {
02778 if (!any_viable_p && candidates && ! candidates->next)
02779 return build_function_call (candidates->fn, args);
02780 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
02781 fn = TREE_OPERAND (fn, 0);
02782 if (!any_viable_p)
02783 error ("no matching function for call to %<%D(%A)%>",
02784 DECL_NAME (OVL_CURRENT (fn)), args);
02785 else
02786 error ("call of overloaded %<%D(%A)%> is ambiguous",
02787 DECL_NAME (OVL_CURRENT (fn)), args);
02788 if (candidates)
02789 print_z_candidates (candidates);
02790 result = error_mark_node;
02791 }
02792 else
02793 result = build_over_call (cand, LOOKUP_NORMAL);
02794
02795
02796 obstack_free (&conversion_obstack, p);
02797
02798 return result;
02799 }
02800
02801
02802
02803
02804
02805
02806
02807
02808
02809 tree
02810 build_operator_new_call (tree fnname, tree args, tree *size, tree *cookie_size)
02811 {
02812 tree fns;
02813 struct z_candidate *candidates;
02814 struct z_candidate *cand;
02815 bool any_viable_p;
02816
02817 args = tree_cons (NULL_TREE, *size, args);
02818 args = resolve_args (args);
02819 if (args == error_mark_node)
02820 return args;
02821
02822
02823
02824
02825
02826
02827
02828
02829
02830
02831 fns = lookup_function_nonclass (fnname, args, false);
02832
02833
02834 cand = perform_overload_resolution (fns, args, &candidates, &any_viable_p);
02835
02836
02837
02838 if (!cand)
02839 {
02840 if (!any_viable_p)
02841 error ("no matching function for call to %<%D(%A)%>",
02842 DECL_NAME (OVL_CURRENT (fns)), args);
02843 else
02844 error ("call of overloaded %<%D(%A)%> is ambiguous",
02845 DECL_NAME (OVL_CURRENT (fns)), args);
02846 if (candidates)
02847 print_z_candidates (candidates);
02848 return error_mark_node;
02849 }
02850
02851
02852
02853
02854 if (*cookie_size)
02855 {
02856 bool use_cookie = true;
02857 if (!abi_version_at_least (2))
02858 {
02859 tree placement = TREE_CHAIN (args);
02860
02861
02862
02863 if (placement && !TREE_CHAIN (placement)
02864 && same_type_p (TREE_TYPE (TREE_VALUE (placement)),
02865 ptr_type_node))
02866 use_cookie = false;
02867 }
02868 else
02869 {
02870 tree arg_types;
02871
02872 arg_types = TYPE_ARG_TYPES (TREE_TYPE (cand->fn));
02873
02874 arg_types = TREE_CHAIN (arg_types);
02875
02876 if (arg_types
02877 && TREE_CHAIN (arg_types) == void_list_node
02878 && same_type_p (TREE_VALUE (arg_types),
02879 ptr_type_node))
02880 use_cookie = false;
02881 }
02882
02883 if (use_cookie)
02884 {
02885
02886 *size = size_binop (PLUS_EXPR, *size, *cookie_size);
02887
02888 TREE_VALUE (args) = *size;
02889 }
02890 else
02891 *cookie_size = NULL_TREE;
02892 }
02893
02894
02895 return build_over_call (cand, LOOKUP_NORMAL);
02896 }
02897
02898 static tree
02899 build_object_call (tree obj, tree args)
02900 {
02901 struct z_candidate *candidates = 0, *cand;
02902 tree fns, convs, mem_args = NULL_TREE;
02903 tree type = TREE_TYPE (obj);
02904 bool any_viable_p;
02905 tree result = NULL_TREE;
02906 void *p;
02907
02908 if (TYPE_PTRMEMFUNC_P (type))
02909 {
02910
02911
02912 error ("pointer-to-member function %E cannot be called without an object; consider using .* or ->*", obj);
02913 return error_mark_node;
02914 }
02915
02916 fns = lookup_fnfields (TYPE_BINFO (type), ansi_opname (CALL_EXPR), 1);
02917 if (fns == error_mark_node)
02918 return error_mark_node;
02919
02920 args = resolve_args (args);
02921
02922 if (args == error_mark_node)
02923 return error_mark_node;
02924
02925
02926 p = conversion_obstack_alloc (0);
02927
02928 if (fns)
02929 {
02930 tree base = BINFO_TYPE (BASELINK_BINFO (fns));
02931 mem_args = tree_cons (NULL_TREE, build_this (obj), args);
02932
02933 for (fns = BASELINK_FUNCTIONS (fns); fns; fns = OVL_NEXT (fns))
02934 {
02935 tree fn = OVL_CURRENT (fns);
02936 if (TREE_CODE (fn) == TEMPLATE_DECL)
02937 add_template_candidate (&candidates, fn, base, NULL_TREE,
02938 mem_args, NULL_TREE,
02939 TYPE_BINFO (type),
02940 TYPE_BINFO (type),
02941 LOOKUP_NORMAL, DEDUCE_CALL);
02942 else
02943 add_function_candidate
02944 (&candidates, fn, base, mem_args, TYPE_BINFO (type),
02945 TYPE_BINFO (type), LOOKUP_NORMAL);
02946 }
02947 }
02948
02949 convs = lookup_conversions (type);
02950
02951 for (; convs; convs = TREE_CHAIN (convs))
02952 {
02953 tree fns = TREE_VALUE (convs);
02954 tree totype = TREE_TYPE (TREE_TYPE (OVL_CURRENT (fns)));
02955
02956 if ((TREE_CODE (totype) == POINTER_TYPE
02957 && TREE_CODE (TREE_TYPE (totype)) == FUNCTION_TYPE)
02958 || (TREE_CODE (totype) == REFERENCE_TYPE
02959 && TREE_CODE (TREE_TYPE (totype)) == FUNCTION_TYPE)
02960 || (TREE_CODE (totype) == REFERENCE_TYPE
02961 && TREE_CODE (TREE_TYPE (totype)) == POINTER_TYPE
02962 && TREE_CODE (TREE_TYPE (TREE_TYPE (totype))) == FUNCTION_TYPE))
02963 for (; fns; fns = OVL_NEXT (fns))
02964 {
02965 tree fn = OVL_CURRENT (fns);
02966 if (TREE_CODE (fn) == TEMPLATE_DECL)
02967 add_template_conv_candidate
02968 (&candidates, fn, obj, args, totype,
02969 NULL_TREE,
02970 NULL_TREE);
02971 else
02972 add_conv_candidate (&candidates, fn, obj, args,
02973 NULL_TREE,
02974 NULL_TREE);
02975 }
02976 }
02977
02978 candidates = splice_viable (candidates, pedantic, &any_viable_p);
02979 if (!any_viable_p)
02980 {
02981 error ("no match for call to %<(%T) (%A)%>", TREE_TYPE (obj), args);
02982 print_z_candidates (candidates);
02983 result = error_mark_node;
02984 }
02985 else
02986 {
02987 cand = tourney (candidates);
02988 if (cand == 0)
02989 {
02990 error ("call of %<(%T) (%A)%> is ambiguous", TREE_TYPE (obj), args);
02991 print_z_candidates (candidates);
02992 result = error_mark_node;
02993 }
02994
02995
02996
02997 else if (TREE_CODE (cand->fn) == FUNCTION_DECL
02998 && DECL_OVERLOADED_OPERATOR_P (cand->fn) == CALL_EXPR)
02999 result = build_over_call (cand, LOOKUP_NORMAL);
03000 else
03001 {
03002 obj = convert_like_with_context (cand->convs[0], obj, cand->fn, -1);
03003 obj = convert_from_reference (obj);
03004 result = build_function_call (obj, args);
03005 }
03006 }
03007
03008
03009 obstack_free (&conversion_obstack, p);
03010
03011 return result;
03012 }
03013
03014 static void
03015 op_error (enum tree_code code, enum tree_code code2,
03016 tree arg1, tree arg2, tree arg3, const char *problem)
03017 {
03018 const char *opname;
03019
03020 if (code == MODIFY_EXPR)
03021 opname = assignment_operator_name_info[code2].name;
03022 else
03023 opname = operator_name_info[code].name;
03024
03025 switch (code)
03026 {
03027 case COND_EXPR:
03028 error ("%s for ternary %<operator?:%> in %<%E ? %E : %E%>",
03029 problem, arg1, arg2, arg3);
03030 break;
03031
03032 case POSTINCREMENT_EXPR:
03033 case POSTDECREMENT_EXPR:
03034 error ("%s for %<operator%s%> in %<%E%s%>", problem, opname, arg1, opname);
03035 break;
03036
03037 case ARRAY_REF:
03038 error ("%s for %<operator[]%> in %<%E[%E]%>", problem, arg1, arg2);
03039 break;
03040
03041 case REALPART_EXPR:
03042 case IMAGPART_EXPR:
03043 error ("%s for %qs in %<%s %E%>", problem, opname, opname, arg1);
03044 break;
03045
03046 default:
03047 if (arg2)
03048 error ("%s for %<operator%s%> in %<%E %s %E%>",
03049 problem, opname, arg1, opname, arg2);
03050 else
03051 error ("%s for %<operator%s%> in %<%s%E%>",
03052 problem, opname, opname, arg1);
03053 break;
03054 }
03055 }
03056
03057
03058
03059
03060 static conversion *
03061 conditional_conversion (tree e1, tree e2)
03062 {
03063 tree t1 = non_reference (TREE_TYPE (e1));
03064 tree t2 = non_reference (TREE_TYPE (e2));
03065 conversion *conv;
03066 bool good_base;
03067
03068
03069
03070
03071
03072
03073
03074 if (real_lvalue_p (e2))
03075 {
03076 conv = implicit_conversion (build_reference_type (t2),
03077 t1,
03078 e1,
03079 false,
03080 LOOKUP_NO_TEMP_BIND);
03081 if (conv)
03082 return conv;
03083 }
03084
03085
03086
03087
03088
03089
03090
03091
03092
03093
03094
03095 if (CLASS_TYPE_P (t1) && CLASS_TYPE_P (t2)
03096 && ((good_base = DERIVED_FROM_P (t2, t1)) || DERIVED_FROM_P (t1, t2)))
03097 {
03098 if (good_base && at_least_as_qualified_p (t2, t1))
03099 {
03100 conv = build_identity_conv (t1, e1);
03101 if (!same_type_p (TYPE_MAIN_VARIANT (t1),
03102 TYPE_MAIN_VARIANT (t2)))
03103 conv = build_conv (ck_base, t2, conv);
03104 else
03105 conv = build_conv (ck_rvalue, t2, conv);
03106 return conv;
03107 }
03108 else
03109 return NULL;
03110 }
03111 else
03112
03113
03114
03115
03116
03117 return implicit_conversion (t2, t1, e1, false,
03118 LOOKUP_NORMAL);
03119 }
03120
03121
03122
03123
03124 tree
03125 build_conditional_expr (tree arg1, tree arg2, tree arg3)
03126 {
03127 tree arg2_type;
03128 tree arg3_type;
03129 tree result = NULL_TREE;
03130 tree result_type = NULL_TREE;
03131 bool lvalue_p = true;
03132 struct z_candidate *candidates = 0;
03133 struct z_candidate *cand;
03134 void *p;
03135
03136
03137
03138
03139
03140 if (!arg2)
03141 {
03142 if (pedantic)
03143 pedwarn ("ISO C++ forbids omitting the middle term of a ?: expression");
03144
03145
03146 if (real_lvalue_p (arg1))
03147 arg2 = arg1 = stabilize_reference (arg1);
03148 else
03149 arg2 = arg1 = save_expr (arg1);
03150 }
03151
03152
03153
03154
03155
03156 arg1 = perform_implicit_conversion (boolean_type_node, arg1);
03157
03158
03159
03160 if (error_operand_p (arg1)
03161 || error_operand_p (arg2)
03162 || error_operand_p (arg3))
03163 return error_mark_node;
03164
03165
03166
03167
03168
03169
03170
03171
03172 arg2_type = TREE_TYPE (arg2);
03173 arg3_type = TREE_TYPE (arg3);
03174 if (VOID_TYPE_P (arg2_type) || VOID_TYPE_P (arg3_type))
03175 {
03176
03177
03178
03179 if (!VOID_TYPE_P (arg2_type))
03180 arg2 = decay_conversion (arg2);
03181 if (!VOID_TYPE_P (arg3_type))
03182 arg3 = decay_conversion (arg3);
03183 arg2_type = TREE_TYPE (arg2);
03184 arg3_type = TREE_TYPE (arg3);
03185
03186
03187
03188
03189
03190
03191
03192
03193
03194
03195
03196
03197
03198
03199
03200 if (TREE_CODE (arg2) == THROW_EXPR
03201 && TREE_CODE (arg3) != THROW_EXPR)
03202 {
03203 if (!VOID_TYPE_P (arg3_type))
03204 arg3 = force_rvalue (arg3);
03205 arg3_type = TREE_TYPE (arg3);
03206 result_type = arg3_type;
03207 }
03208 else if (TREE_CODE (arg2) != THROW_EXPR
03209 && TREE_CODE (arg3) == THROW_EXPR)
03210 {
03211 if (!VOID_TYPE_P (arg2_type))
03212 arg2 = force_rvalue (arg2);
03213 arg2_type = TREE_TYPE (arg2);
03214 result_type = arg2_type;
03215 }
03216 else if (VOID_TYPE_P (arg2_type) && VOID_TYPE_P (arg3_type))
03217 result_type = void_type_node;
03218 else
03219 {
03220 error ("%qE has type %<void%> and is not a throw-expression",
03221 VOID_TYPE_P (arg2_type) ? arg2 : arg3);
03222 return error_mark_node;
03223 }
03224
03225 lvalue_p = false;
03226 goto valid_operands;
03227 }
03228
03229
03230
03231
03232
03233 else if (!same_type_p (arg2_type, arg3_type)
03234 && (CLASS_TYPE_P (arg2_type) || CLASS_TYPE_P (arg3_type)))
03235 {
03236 conversion *conv2;
03237 conversion *conv3;
03238
03239
03240 p = conversion_obstack_alloc (0);
03241
03242 conv2 = conditional_conversion (arg2, arg3);
03243 conv3 = conditional_conversion (arg3, arg2);
03244
03245
03246
03247
03248
03249
03250
03251
03252
03253
03254 if ((conv2 && !conv2->bad_p
03255 && conv3 && !conv3->bad_p)
03256 || (conv2 && conv2->kind == ck_ambig)
03257 || (conv3 && conv3->kind == ck_ambig))
03258 {
03259 error ("operands to ?: have different types");
03260 result = error_mark_node;
03261 }
03262 else if (conv2 && !conv2->bad_p)
03263 {
03264 arg2 = convert_like (conv2, arg2);
03265 arg2 = convert_from_reference (arg2);
03266 arg2_type = TREE_TYPE (arg2);
03267 }
03268 else if (conv3 && !conv3->bad_p)
03269 {
03270 arg3 = convert_like (conv3, arg3);
03271 arg3 = convert_from_reference (arg3);
03272 arg3_type = TREE_TYPE (arg3);
03273 }
03274
03275
03276 obstack_free (&conversion_obstack, p);
03277
03278 if (result)
03279 return result;
03280
03281
03282
03283
03284
03285
03286
03287
03288
03289
03290
03291
03292
03293
03294
03295
03296 if ((conv2 || conv3)
03297 && CLASS_TYPE_P (arg2_type)
03298 && TYPE_QUALS (arg2_type) != TYPE_QUALS (arg3_type))
03299 arg2_type = arg3_type =
03300 cp_build_qualified_type (arg2_type,
03301 TYPE_QUALS (arg2_type)
03302 | TYPE_QUALS (arg3_type));
03303 }
03304
03305
03306
03307
03308
03309 if (real_lvalue_p (arg2)
03310 && real_lvalue_p (arg3)
03311 && same_type_p (arg2_type, arg3_type))
03312 {
03313 result_type = arg2_type;
03314 goto valid_operands;
03315 }
03316
03317
03318
03319
03320
03321
03322
03323
03324 lvalue_p = false;
03325 if (!same_type_p (arg2_type, arg3_type)
03326 && (CLASS_TYPE_P (arg2_type) || CLASS_TYPE_P (arg3_type)))
03327 {
03328 tree args[3];
03329 conversion *conv;
03330 bool any_viable_p;
03331
03332
03333
03334
03335 args[0] = arg2;
03336 args[1] = arg3;
03337 args[2] = arg1;
03338 add_builtin_candidates (&candidates,
03339 COND_EXPR,
03340 NOP_EXPR,
03341 ansi_opname (COND_EXPR),
03342 args,
03343 LOOKUP_NORMAL);
03344
03345
03346
03347
03348
03349 candidates = splice_viable (candidates, pedantic, &any_viable_p);
03350 if (!any_viable_p)
03351 {
03352 op_error (COND_EXPR, NOP_EXPR, arg1, arg2, arg3, "no match");
03353 print_z_candidates (candidates);
03354 return error_mark_node;
03355 }
03356 cand = tourney (candidates);
03357 if (!cand)
03358 {
03359 op_error (COND_EXPR, NOP_EXPR, arg1, arg2, arg3, "no match");
03360 print_z_candidates (candidates);
03361 return error_mark_node;
03362 }
03363
03364
03365
03366
03367
03368
03369 conv = cand->convs[0];
03370 arg1 = convert_like (conv, arg1);
03371 conv = cand->convs[1];
03372 arg2 = convert_like (conv, arg2);
03373 conv = cand->convs[2];
03374 arg3 = convert_like (conv, arg3);
03375 }
03376
03377
03378
03379
03380
03381
03382
03383
03384
03385
03386
03387
03388 arg2 = force_rvalue (arg2);
03389 if (!CLASS_TYPE_P (arg2_type))
03390 arg2_type = TREE_TYPE (arg2);
03391
03392 arg3 = force_rvalue (arg3);
03393 if (!CLASS_TYPE_P (arg2_type))
03394 arg3_type = TREE_TYPE (arg3);
03395
03396 if (arg2 == error_mark_node || arg3 == error_mark_node)
03397 return error_mark_node;
03398
03399
03400
03401
03402
03403
03404
03405 if (same_type_p (arg2_type, arg3_type))
03406 result_type = arg2_type;
03407
03408
03409
03410
03411
03412 else if ((ARITHMETIC_TYPE_P (arg2_type)
03413 || TREE_CODE (arg2_type) == ENUMERAL_TYPE)
03414 && (ARITHMETIC_TYPE_P (arg3_type)
03415 || TREE_CODE (arg3_type) == ENUMERAL_TYPE))
03416 {
03417
03418 result_type = type_after_usual_arithmetic_conversions (arg2_type,
03419 arg3_type);
03420
03421 if (TREE_CODE (arg2_type) == ENUMERAL_TYPE
03422 && TREE_CODE (arg3_type) == ENUMERAL_TYPE)
03423 warning ("enumeral mismatch in conditional expression: %qT vs %qT",
03424 arg2_type, arg3_type);
03425 else if (extra_warnings
03426 && ((TREE_CODE (arg2_type) == ENUMERAL_TYPE
03427 && !same_type_p (arg3_type, type_promotes_to (arg2_type)))
03428 || (TREE_CODE (arg3_type) == ENUMERAL_TYPE
03429 && !same_type_p (arg2_type, type_promotes_to (arg3_type)))))
03430 warning ("enumeral and non-enumeral type in conditional expression");
03431
03432 arg2 = perform_implicit_conversion (result_type, arg2);
03433 arg3 = perform_implicit_conversion (result_type, arg3);
03434 }
03435
03436
03437
03438
03439
03440
03441
03442
03443
03444
03445
03446
03447
03448
03449
03450
03451 else if ((null_ptr_cst_p (arg2)
03452 && (TYPE_PTR_P (arg3_type) || TYPE_PTR_TO_MEMBER_P (arg3_type)))
03453 || (null_ptr_cst_p (arg3)
03454 && (TYPE_PTR_P (arg2_type) || TYPE_PTR_TO_MEMBER_P (arg2_type)))
03455 || (TYPE_PTR_P (arg2_type) && TYPE_PTR_P (arg3_type))
03456 || (TYPE_PTRMEM_P (arg2_type) && TYPE_PTRMEM_P (arg3_type))
03457 || (TYPE_PTRMEMFUNC_P (arg2_type) && TYPE_PTRMEMFUNC_P (arg3_type)))
03458 {
03459 result_type = composite_pointer_type (arg2_type, arg3_type, arg2,
03460 arg3, "conditional expression");
03461 if (result_type == error_mark_node)
03462 return error_mark_node;
03463 arg2 = perform_implicit_conversion (result_type, arg2);
03464 arg3 = perform_implicit_conversion (result_type, arg3);
03465 }
03466
03467 if (!result_type)
03468 {
03469 error ("operands to ?: have different types");
03470 return error_mark_node;
03471 }
03472
03473 valid_operands:
03474 result = fold_if_not_in_template (build3 (COND_EXPR, result_type, arg1,
03475 arg2, arg3));
03476
03477
03478
03479
03480
03481
03482 if (!lvalue_p && CLASS_TYPE_P (TREE_TYPE (result)))
03483 result = get_target_expr (result);
03484
03485
03486
03487 if (!lvalue_p && real_lvalue_p (result))
03488 result = rvalue (result);
03489
03490 return result;
03491 }
03492
03493
03494
03495
03496
03497 static tree
03498 prep_operand (tree operand)
03499 {
03500 if (operand)
03501 {
03502 if (CLASS_TYPE_P (TREE_TYPE (operand))
03503 && CLASSTYPE_TEMPLATE_INSTANTIATION (TREE_TYPE (operand)))
03504
03505 instantiate_class_template (TYPE_MAIN_VARIANT (TREE_TYPE (operand)));
03506 }
03507
03508 return operand;
03509 }
03510
03511
03512
03513
03514
03515
03516
03517
03518
03519 static void
03520 add_candidates (tree fns, tree args,
03521 tree explicit_targs, bool template_only,
03522 tree conversion_path, tree access_path,
03523 int flags,
03524 struct z_candidate **candidates)
03525 {
03526 tree ctype;
03527 tree non_static_args;
03528
03529 ctype = conversion_path ? BINFO_TYPE (conversion_path) : NULL_TREE;
03530
03531 non_static_args = NULL_TREE;
03532
03533 while (fns)
03534 {
03535 tree fn;
03536 tree fn_args;
03537
03538 fn = OVL_CURRENT (fns);
03539
03540 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
03541 {
03542
03543
03544 if (!non_static_args)
03545 non_static_args = tree_cons (NULL_TREE,
03546 build_this (TREE_VALUE (args)),
03547 TREE_CHAIN (args));
03548 fn_args = non_static_args;
03549 }
03550 else
03551
03552 fn_args = args;
03553
03554 if (TREE_CODE (fn) == TEMPLATE_DECL)
03555 add_template_candidate (candidates,
03556 fn,
03557 ctype,
03558 explicit_targs,
03559 fn_args,
03560 NULL_TREE,
03561 access_path,
03562 conversion_path,
03563 flags,
03564 DEDUCE_CALL);
03565 else if (!template_only)
03566 add_function_candidate (candidates,
03567 fn,
03568 ctype,
03569 fn_args,
03570 access_path,
03571 conversion_path,
03572 flags);
03573 fns = OVL_NEXT (fns);
03574 }
03575 }
03576
03577 tree
03578 build_new_op (enum tree_code code, int flags, tree arg1, tree arg2, tree arg3,
03579 bool *overloaded_p)
03580 {
03581 struct z_candidate *candidates = 0, *cand;
03582 tree arglist, fnname;
03583 tree args[3];
03584 tree result = NULL_TREE;
03585 bool result_valid_p = false;
03586 enum tree_code code2 = NOP_EXPR;
03587 conversion *conv;
03588 void *p;
03589 bool strict_p;
03590 bool any_viable_p;
03591
03592 if (error_operand_p (arg1)
03593 || error_operand_p (arg2)
03594 || error_operand_p (arg3))
03595 return error_mark_node;
03596
03597 if (code == MODIFY_EXPR)
03598 {
03599 code2 = TREE_CODE (arg3);
03600 arg3 = NULL_TREE;
03601 fnname = ansi_assopname (code2);
03602 }
03603 else
03604 fnname = ansi_opname (code);
03605
03606 arg1 = prep_operand (arg1);
03607
03608 switch (code)
03609 {
03610 case NEW_EXPR:
03611 case VEC_NEW_EXPR:
03612 case VEC_DELETE_EXPR:
03613 case DELETE_EXPR:
03614
03615 gcc_unreachable ();
03616
03617 case CALL_EXPR:
03618 return build_object_call (arg1, arg2);
03619
03620 default:
03621 break;
03622 }
03623
03624 arg2 = prep_operand (arg2);
03625 arg3 = prep_operand (arg3);
03626
03627 if (code == COND_EXPR)
03628 {
03629 if (arg2 == NULL_TREE
03630 || TREE_CODE (TREE_TYPE (arg2)) == VOID_TYPE
03631 || TREE_CODE (TREE_TYPE (arg3)) == VOID_TYPE
03632 || (! IS_OVERLOAD_TYPE (TREE_TYPE (arg2))
03633 && ! IS_OVERLOAD_TYPE (TREE_TYPE (arg3))))
03634 goto builtin;
03635 }
03636 else if (! IS_OVERLOAD_TYPE (TREE_TYPE (arg1))
03637 && (! arg2 || ! IS_OVERLOAD_TYPE (TREE_TYPE (arg2))))
03638 goto builtin;
03639
03640 if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
03641 arg2 = integer_zero_node;
03642
03643 arglist = NULL_TREE;
03644 if (arg3)
03645 arglist = tree_cons (NULL_TREE, arg3, arglist);
03646 if (arg2)
03647 arglist = tree_cons (NULL_TREE, arg2, arglist);
03648 arglist = tree_cons (NULL_TREE, arg1, arglist);
03649
03650
03651 p = conversion_obstack_alloc (0);
03652
03653
03654
03655 add_candidates (lookup_function_nonclass (fnname, arglist, true),
03656 arglist, NULL_TREE, false, NULL_TREE, NULL_TREE,
03657 flags, &candidates);
03658
03659 if (CLASS_TYPE_P (TREE_TYPE (arg1)))
03660 {
03661 tree fns;
03662
03663 fns = lookup_fnfields (TREE_TYPE (arg1), fnname, 1);
03664 if (fns == error_mark_node)
03665 {
03666 result = error_mark_node;
03667 goto user_defined_result_ready;
03668 }
03669 if (fns)
03670 add_candidates (BASELINK_FUNCTIONS (fns), arglist,
03671 NULL_TREE, false,
03672 BASELINK_BINFO (fns),
03673 TYPE_BINFO (TREE_TYPE (arg1)),
03674 flags, &candidates);
03675 }
03676
03677
03678
03679
03680
03681 if (code == COND_EXPR)
03682 {
03683 args[0] = arg2;
03684 args[1] = arg3;
03685 args[2] = arg1;
03686 }
03687 else
03688 {
03689 args[0] = arg1;
03690 args[1] = arg2;
03691 args[2] = NULL_TREE;
03692 }
03693
03694 add_builtin_candidates (&candidates, code, code2, fnname, args, flags);
03695
03696 switch (code)
03697 {
03698 case COMPOUND_EXPR:
03699 case ADDR_EXPR:
03700
03701
03702
03703
03704
03705
03706 strict_p = true;
03707 break;
03708
03709 default:
03710 strict_p = pedantic;
03711 break;
03712 }
03713
03714 candidates = splice_viable (candidates, strict_p, &any_viable_p);
03715 if (!any_viable_p)
03716 {
03717 switch (code)
03718 {
03719 case POSTINCREMENT_EXPR:
03720 case POSTDECREMENT_EXPR:
03721
03722
03723 if (flags & LOOKUP_COMPLAIN)
03724 pedwarn ("no %<%D(int)%> declared for postfix %qs, "
03725 "trying prefix operator instead",
03726 fnname,
03727 operator_name_info[code].name);
03728 if (code == POSTINCREMENT_EXPR)
03729 code = PREINCREMENT_EXPR;
03730 else
03731 code = PREDECREMENT_EXPR;
03732 result = build_new_op (code, flags, arg1, NULL_TREE, NULL_TREE,
03733 overloaded_p);
03734 break;
03735
03736
03737 case ADDR_EXPR:
03738 case COMPOUND_EXPR:
03739 case COMPONENT_REF:
03740 result = NULL_TREE;
03741 result_valid_p = true;
03742 break;
03743
03744 default:
03745 if (flags & LOOKUP_COMPLAIN)
03746 {
03747 op_error (code, code2, arg1, arg2, arg3, "no match");
03748 print_z_candidates (candidates);
03749 }
03750 result = error_mark_node;
03751 break;
03752 }
03753 }
03754 else
03755 {
03756 cand = tourney (candidates);
03757 if (cand == 0)
03758 {
03759 if (flags & LOOKUP_COMPLAIN)
03760 {
03761 op_error (code, code2, arg1, arg2, arg3, "ambiguous overload");
03762 print_z_candidates (candidates);
03763 }
03764 result = error_mark_node;
03765 }
03766 else if (TREE_CODE (cand->fn) == FUNCTION_DECL)
03767 {
03768 if (overloaded_p)
03769 *overloaded_p = true;
03770
03771 result = build_over_call (cand, LOOKUP_NORMAL);
03772 }
03773 else
03774 {
03775
03776 if (cand->warnings)
03777 {
03778 struct candidate_warning *w;
03779 for (w = cand->warnings; w; w = w->next)
03780 joust (cand, w->loser, 1);
03781 }
03782
03783
03784 switch (code)
03785 {
03786 case GT_EXPR:
03787 case LT_EXPR:
03788 case GE_EXPR:
03789 case LE_EXPR:
03790 case EQ_EXPR:
03791 case NE_EXPR:
03792 if (TREE_CODE (TREE_TYPE (arg1)) == ENUMERAL_TYPE
03793 && TREE_CODE (TREE_TYPE (arg2)) == ENUMERAL_TYPE
03794 && (TYPE_MAIN_VARIANT (TREE_TYPE (arg1))
03795 != TYPE_MAIN_VARIANT (TREE_TYPE (arg2))))
03796 {
03797 warning ("comparison between %q#T and %q#T",
03798 TREE_TYPE (arg1), TREE_TYPE (arg2));
03799 }
03800 break;
03801 default:
03802 break;
03803 }
03804
03805
03806
03807
03808
03809 conv = cand->convs[0];
03810 if (conv->kind == ck_ref_bind)
03811 conv = conv->u.next;
03812 arg1 = convert_like (conv, arg1);
03813 if (arg2)
03814 {
03815 conv = cand->convs[1];
03816 if (conv->kind == ck_ref_bind)
03817 conv = conv->u.next;
03818 arg2 = convert_like (conv, arg2);
03819 }
03820 if (arg3)
03821 {
03822 conv = cand->convs[2];
03823 if (conv->kind == ck_ref_bind)
03824 conv = conv->u.next;
03825 arg3 = convert_like (conv, arg3);
03826 }
03827 }
03828 }
03829
03830 user_defined_result_ready:
03831
03832
03833 obstack_free (&conversion_obstack, p);
03834
03835 if (result || result_valid_p)
03836 return result;
03837
03838 builtin:
03839 switch (code)
03840 {
03841 case MODIFY_EXPR:
03842 return build_modify_expr (arg1, code2, arg2);
03843
03844 case INDIRECT_REF:
03845 return build_indirect_ref (arg1, "unary *");
03846
03847 case PLUS_EXPR:
03848 case MINUS_EXPR:
03849 case MULT_EXPR:
03850 case TRUNC_DIV_EXPR:
03851 case GT_EXPR:
03852 case LT_EXPR:
03853 case GE_EXPR:
03854 case LE_EXPR:
03855 case EQ_EXPR:
03856 case NE_EXPR:
03857 case MAX_EXPR:
03858 case MIN_EXPR:
03859 case LSHIFT_EXPR:
03860 case RSHIFT_EXPR:
03861 case TRUNC_MOD_EXPR:
03862 case BIT_AND_EXPR:
03863 case BIT_IOR_EXPR:
03864 case BIT_XOR_EXPR:
03865 case TRUTH_ANDIF_EXPR:
03866 case TRUTH_ORIF_EXPR:
03867 return cp_build_binary_op (code, arg1, arg2);
03868
03869 case CONVERT_EXPR:
03870 case NEGATE_EXPR:
03871 case BIT_NOT_EXPR:
03872 case TRUTH_NOT_EXPR:
03873 case PREINCREMENT_EXPR:
03874 case POSTINCREMENT_EXPR:
03875 case PREDECREMENT_EXPR:
03876 case POSTDECREMENT_EXPR:
03877 case REALPART_EXPR:
03878 case IMAGPART_EXPR:
03879 return build_unary_op (code, arg1, candidates != 0);
03880
03881 case ARRAY_REF:
03882 return build_array_ref (arg1, arg2);
03883
03884 case COND_EXPR:
03885 return build_conditional_expr (arg1, arg2, arg3);
03886
03887 case MEMBER_REF:
03888 return build_m_component_ref (build_indirect_ref (arg1, NULL), arg2);
03889
03890
03891 case ADDR_EXPR:
03892 case COMPONENT_REF:
03893 case COMPOUND_EXPR:
03894 return NULL_TREE;
03895
03896 default:
03897 gcc_unreachable ();
03898 }
03899 return NULL_TREE;
03900 }
03901
03902
03903
03904
03905
03906
03907
03908
03909
03910
03911
03912
03913
03914
03915 tree
03916 build_op_delete_call (enum tree_code code, tree addr, tree size,
03917 bool global_p, tree placement)
03918 {
03919 tree fn = NULL_TREE;
03920 tree fns, fnname, argtypes, args, type;
03921 int pass;
03922
03923 if (addr == error_mark_node)
03924 return error_mark_node;
03925
03926 type = strip_array_types (TREE_TYPE (TREE_TYPE (addr)));
03927
03928 fnname = ansi_opname (code);
03929
03930 if (CLASS_TYPE_P (type)
03931 && COMPLETE_TYPE_P (complete_type (type))
03932 && !global_p)
03933
03934
03935
03936
03937
03938
03939
03940 {
03941 fns = lookup_fnfields (TYPE_BINFO (type), fnname, 1);
03942 if (fns == error_mark_node)
03943 return error_mark_node;
03944 }
03945 else
03946 fns = NULL_TREE;
03947
03948 if (fns == NULL_TREE)
03949 fns = lookup_name_nonclass (fnname);
03950
03951 if (placement)
03952 {
03953 tree alloc_fn;
03954 tree call_expr;
03955
03956
03957 call_expr = placement;
03958
03959 alloc_fn = get_callee_fndecl (call_expr);
03960 gcc_assert (alloc_fn != NULL_TREE);
03961
03962 argtypes = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (alloc_fn)));
03963
03964 args = TREE_CHAIN (TREE_OPERAND (call_expr, 1));
03965 }
03966 else
03967 {
03968
03969 argtypes = void_list_node;
03970 args = NULL_TREE;
03971 }
03972
03973
03974 addr = cp_convert (ptr_type_node, addr);
03975
03976
03977
03978
03979
03980 for (pass = 0; pass < (placement ? 1 : 2); ++pass)
03981 {
03982
03983
03984 for (fn = BASELINK_P (fns) ? BASELINK_FUNCTIONS (fns) : fns;
03985 fn;
03986 fn = OVL_NEXT (fn))
03987 {
03988 tree t;
03989
03990
03991 t = TYPE_ARG_TYPES (TREE_TYPE (OVL_CURRENT (fn)));
03992 if (!same_type_p (TREE_VALUE (t), ptr_type_node))
03993 continue;
03994 t = TREE_CHAIN (t);
03995
03996 if (pass == 0)
03997 {
03998 tree a = argtypes;
03999 while (a && t)
04000 {
04001 if (!same_type_p (TREE_VALUE (a), TREE_VALUE (t)))
04002 break;
04003 a = TREE_CHAIN (a);
04004 t = TREE_CHAIN (t);
04005 }
04006 if (!a && !t)
04007 break;
04008 }
04009
04010
04011 else if (pass == 1
04012 && same_type_p (TREE_VALUE (t), sizetype)
04013 && TREE_CHAIN (t) == void_list_node)
04014 break;
04015 }
04016
04017
04018 if (fn)
04019 break;
04020 }
04021
04022
04023 if (fn)
04024 {
04025
04026
04027 fn = OVL_CURRENT (fn);
04028
04029
04030
04031 if (DECL_CLASS_SCOPE_P (fn))
04032 perform_or_defer_access_check (TYPE_BINFO (type), fn);
04033
04034 if (pass == 0)
04035 args = tree_cons (NULL_TREE, addr, args);
04036 else
04037 args = tree_cons (NULL_TREE, addr,
04038 build_tree_list (NULL_TREE, size));
04039
04040 if (placement)
04041 {
04042
04043
04044 mark_used (fn);
04045 return build_cxx_call (fn, args);
04046 }
04047 else
04048 return build_function_call (fn, args);
04049 }
04050
04051
04052
04053 if (placement)
04054 return NULL_TREE;
04055
04056 error ("no suitable %<operator %s%> for %qT",
04057 operator_name_info[(int)code].name, type);
04058 return error_mark_node;
04059 }
04060
04061
04062
04063
04064
04065 bool
04066 enforce_access (tree basetype_path, tree decl)
04067 {
04068 gcc_assert (TREE_CODE (basetype_path) == TREE_BINFO);
04069
04070 if (!accessible_p (basetype_path, decl, true))
04071 {
04072 if (TREE_PRIVATE (decl))
04073 cp_error_at ("%q+#D is private", decl);
04074 else if (TREE_PROTECTED (decl))
04075 cp_error_at ("%q+#D is protected", decl);
04076 else
04077 cp_error_at ("%q+#D is inaccessible", decl);
04078 error ("within this context");
04079 return false;
04080 }
04081
04082 return true;
04083 }
04084
04085
04086
04087
04088 static void
04089 check_constructor_callable (tree type, tree expr)
04090 {
04091 build_special_member_call (NULL_TREE,
04092 complete_ctor_identifier,
04093 build_tree_list (NULL_TREE, expr),
04094 type,
04095 LOOKUP_NORMAL | LOOKUP_ONLYCONVERTING
04096 | LOOKUP_NO_CONVERSION
04097 | LOOKUP_CONSTRUCTOR_CALLABLE);
04098 }
04099
04100
04101
04102
04103
04104
04105
04106 static tree
04107 build_temp (tree expr, tree type, int flags,
04108 void (**diagnostic_fn)(const char *, ...))
04109 {
04110 int savew, savee;
04111
04112 savew = warningcount, savee = errorcount;
04113 expr = build_special_member_call (NULL_TREE,
04114 complete_ctor_identifier,
04115 build_tree_list (NULL_TREE, expr),
04116 type, flags);
04117 if (warningcount > savew)
04118 *diagnostic_fn = warning;
04119 else if (errorcount > savee)
04120 *diagnostic_fn = error;
04121 else
04122 *diagnostic_fn = NULL;
04123 return expr;
04124 }
04125
04126
04127
04128
04129
04130
04131
04132
04133
04134
04135
04136
04137 static tree
04138 convert_like_real (conversion *convs, tree expr, tree fn, int argnum,
04139 int inner, bool issue_conversion_warnings,
04140 bool c_cast_p)
04141 {
04142 tree totype = convs->type;
04143 void (*diagnostic_fn)(const char *, ...);
04144
04145 if (convs->bad_p
04146 && convs->kind != ck_user
04147 && convs->kind != ck_ambig
04148 && convs->kind != ck_ref_bind)
04149 {
04150 conversion *t = convs;
04151 for (; t; t = convs->u.next)
04152 {
04153 if (t->kind == ck_user || !t->bad_p)
04154 {
04155 expr = convert_like_real (t, expr, fn, argnum, 1,
04156 false,
04157 false);
04158 break;
04159 }
04160 else if (t->kind == ck_ambig)
04161 return convert_like_real (t, expr, fn, argnum, 1,
04162 false,
04163 false);
04164 else if (t->kind == ck_identity)
04165 break;
04166 }
04167 pedwarn ("invalid conversion from %qT to %qT", TREE_TYPE (expr), totype);
04168 if (fn)
04169 pedwarn (" initializing argument %P of %qD", argnum, fn);
04170 return cp_convert (totype, expr);
04171 }
04172
04173 if (issue_conversion_warnings)
04174 {
04175 tree t = non_reference (totype);
04176
04177
04178 if (ARITHMETIC_TYPE_P (t) && expr == null_node)
04179 {
04180 if (fn)
04181 warning ("passing NULL to non-pointer argument %P of %qD",
04182 argnum, fn);
04183 else
04184 warning ("converting to non-pointer type %qT from NULL", t);
04185 }
04186
04187
04188 if (TREE_CODE (TREE_TYPE (expr)) == REAL_TYPE
04189 && TREE_CODE (t) == INTEGER_TYPE)
04190 {
04191 if (fn)
04192 warning ("passing %qT for argument %P to %qD",
04193 TREE_TYPE (expr), argnum, fn);
04194 else
04195 warning ("converting to %qT from %qT", t, TREE_TYPE (expr));
04196 }
04197
04198
04199 else if (TYPE_UNSIGNED (t) && TREE_CODE (t) != BOOLEAN_TYPE)
04200 {
04201 if (TREE_CODE (expr) == INTEGER_CST && TREE_NEGATED_INT (expr))
04202 {
04203 if (fn)
04204 warning ("passing negative value %qE for argument %P to %qD",
04205 expr, argnum, fn);
04206 else
04207 warning ("converting negative value %qE to %qT", expr, t);
04208 }
04209
04210 overflow_warning (expr);
04211 }
04212 }
04213
04214 switch (convs->kind)
04215 {
04216 case ck_user:
04217 {
04218 struct z_candidate *cand = convs->cand;
04219 tree convfn = cand->fn;
04220 tree args;
04221
04222 if (DECL_CONSTRUCTOR_P (convfn))
04223 {
04224 tree t = build_int_cst (build_pointer_type (DECL_CONTEXT (convfn)),
04225 0);
04226
04227 args = build_tree_list (NULL_TREE, expr);
04228
04229
04230 gcc_assert (!DECL_HAS_IN_CHARGE_PARM_P (convfn)
04231 && !DECL_HAS_VTT_PARM_P (convfn));
04232 args = tree_cons (NULL_TREE, t, args);
04233 }
04234 else
04235 args = build_this (expr);
04236 expr = build_over_call (cand, LOOKUP_NORMAL);
04237
04238
04239
04240 if (DECL_CONSTRUCTOR_P (convfn))
04241 expr = build_cplus_new (totype, expr);
04242
04243
04244
04245
04246
04247
04248
04249
04250
04251 if (IS_AGGR_TYPE (totype)
04252 && (inner >= 0 || !lvalue_p (expr)))
04253 {
04254 expr = (build_temp
04255 (expr, totype,
04256
04257
04258
04259
04260 LOOKUP_NORMAL|LOOKUP_ONLYCONVERTING|LOOKUP_NO_CONVERSION,
04261 &diagnostic_fn));
04262
04263 if (diagnostic_fn)
04264 {
04265 if (fn)
04266 diagnostic_fn
04267 (" initializing argument %P of %qD from result of %qD",
04268 argnum, fn, convfn);
04269 else
04270 diagnostic_fn
04271 (" initializing temporary from result of %qD", convfn);
04272 }
04273 expr = build_cplus_new (totype, expr);
04274 }
04275 return expr;
04276 }
04277 case ck_identity:
04278 if (type_unknown_p (expr))
04279 expr = instantiate_type (totype, expr, tf_error | tf_warning);
04280
04281
04282
04283 if (inner >= 0)
04284 expr = integral_constant_value (expr);
04285 if (convs->check_copy_constructor_p)
04286 check_constructor_callable (totype, expr);
04287 return expr;
04288 case ck_ambig:
04289
04290 return build_user_type_conversion
04291 (totype, convs->u.expr, LOOKUP_NORMAL);
04292
04293 default:
04294 break;
04295 };
04296
04297 expr = convert_like_real (convs->u.next, expr, fn, argnum,
04298 convs->kind == ck_ref_bind ? -1 : 1,
04299 false,
04300 c_cast_p);
04301 if (expr == error_mark_node)
04302 return error_mark_node;
04303
04304 switch (convs->kind)
04305 {
04306 case ck_rvalue:
04307 if (! IS_AGGR_TYPE (totype))
04308 return expr;
04309
04310 case ck_base:
04311 if (convs->kind == ck_base && !convs->need_temporary_p)
04312 {
04313
04314
04315 if (convs->check_copy_constructor_p)
04316 check_constructor_callable (TREE_TYPE (expr), expr);
04317
04318 expr = build_unary_op (ADDR_EXPR, expr, 0);
04319 expr = convert_to_base (expr, build_pointer_type (totype),
04320 !c_cast_p, true);
04321 expr = build_indirect_ref (expr, "implicit conversion");
04322 return expr;
04323 }
04324
04325
04326
04327
04328 expr = build_temp (expr, totype, LOOKUP_NORMAL|LOOKUP_ONLYCONVERTING,
04329 &diagnostic_fn);
04330 if (diagnostic_fn && fn)
04331 diagnostic_fn (" initializing argument %P of %qD", argnum, fn);
04332 return build_cplus_new (totype, expr);
04333
04334 case ck_ref_bind:
04335 {
04336 tree ref_type = totype;
04337
04338
04339 if (convs->need_temporary_p || !lvalue_p (expr))
04340 {
04341 tree type = convs->u.next->type;
04342 cp_lvalue_kind lvalue = real_lvalue_p (expr);
04343
04344 if (!CP_TYPE_CONST_NON_VOLATILE_P (TREE_TYPE (ref_type)))
04345 {
04346
04347
04348 if (lvalue & clk_bitfield)
04349 error ("cannot bind bitfield %qE to %qT",
04350 expr, ref_type);
04351 else if (lvalue & clk_packed)
04352 error ("cannot bind packed field %qE to %qT",
04353 expr, ref_type);
04354 else
04355 error ("cannot bind rvalue %qE to %qT", expr, ref_type);
04356 return error_mark_node;
04357 }
04358
04359
04360
04361
04362
04363
04364 if ((lvalue & clk_packed)
04365 && CLASS_TYPE_P (type)
04366 && !TYPE_HAS_TRIVIAL_INIT_REF (type))
04367 {
04368 error ("cannot bind packed field %qE to %qT",
04369 expr, ref_type);
04370 return error_mark_node;
04371 }
04372 expr = build_target_expr_with_type (expr, type);
04373 }
04374
04375
04376
04377 expr = build_unary_op (ADDR_EXPR, expr, 1);
04378 if (expr == error_mark_node)
04379 return error_mark_node;
04380
04381
04382
04383
04384 expr = cp_convert (build_pointer_type (TREE_TYPE (ref_type)),
04385 expr);
04386
04387 return build_nop (ref_type, expr);
04388 }
04389
04390 case ck_lvalue:
04391 return decay_conversion (expr);
04392
04393 case ck_qual:
04394
04395 string_conv_p (totype, expr, 1);
04396 break;
04397
04398 case ck_ptr:
04399 if (convs->base_p)
04400 expr = convert_to_base (expr, totype, !c_cast_p,
04401 false);
04402 return build_nop (totype, expr);
04403
04404 case ck_pmem:
04405 return convert_ptrmem (totype, expr, false,
04406 c_cast_p);
04407
04408 default:
04409 break;
04410 }
04411 return ocp_convert (totype, expr, CONV_IMPLICIT,
04412 LOOKUP_NORMAL|LOOKUP_NO_CONVERSION);
04413 }
04414
04415
04416
04417 static tree
04418 call_builtin_trap (void)
04419 {
04420 tree fn = implicit_built_in_decls[BUILT_IN_TRAP];
04421
04422 gcc_assert (fn != NULL);
04423 fn = build_call (fn, NULL_TREE);
04424 return fn;
04425 }
04426
04427
04428
04429
04430 tree
04431 convert_arg_to_ellipsis (tree arg)
04432 {
04433
04434
04435
04436
04437 arg = decay_conversion (arg);
04438
04439
04440
04441
04442
04443
04444
04445 if (TREE_CODE (TREE_TYPE (arg)) == REAL_TYPE
04446 && (TYPE_PRECISION (TREE_TYPE (arg))
04447 < TYPE_PRECISION (double_type_node)))
04448 arg = convert_to_real (double_type_node, arg);
04449 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (arg)))
04450 arg = perform_integral_promotions (arg);
04451
04452 arg = require_complete_type (arg);
04453
04454 if (arg != error_mark_node
04455 && !pod_type_p (TREE_TYPE (arg)))
04456 {
04457
04458
04459
04460
04461
04462
04463 if (!skip_evaluation)
04464 warning ("cannot pass objects of non-POD type %q#T through %<...%>; "
04465 "call will abort at runtime", TREE_TYPE (arg));
04466 arg = call_builtin_trap ();
04467 arg = build2 (COMPOUND_EXPR, integer_type_node, arg,
04468 integer_zero_node);
04469 }
04470
04471 return arg;
04472 }
04473
04474
04475
04476 tree
04477 build_x_va_arg (tree expr, tree type)
04478 {
04479 if (processing_template_decl)
04480 return build_min (VA_ARG_EXPR, type, expr);
04481
04482 type = complete_type_or_else (type, NULL_TREE);
04483
04484 if (expr == error_mark_node || !type)
04485 return error_mark_node;
04486
04487 if (! pod_type_p (type))
04488 {
04489
04490 warning ("cannot receive objects of non-POD type %q#T through %<...%>; "
04491 "call will abort at runtime", type);
04492 expr = convert (build_pointer_type (type), null_node);
04493 expr = build2 (COMPOUND_EXPR, TREE_TYPE (expr),
04494 call_builtin_trap (), expr);
04495 expr = build_indirect_ref (expr, NULL);
04496 return expr;
04497 }
04498
04499 return build_va_arg (expr, type);
04500 }
04501
04502
04503
04504
04505
04506 tree
04507 cxx_type_promotes_to (tree type)
04508 {
04509 tree promote;
04510
04511
04512
04513 type = type_decays_to (type);
04514
04515 promote = type_promotes_to (type);
04516 if (same_type_p (type, promote))
04517 promote = type;
04518
04519 return promote;
04520 }
04521
04522
04523
04524
04525
04526 tree
04527 convert_default_arg (tree type, tree arg, tree fn, int parmnum)
04528 {
04529
04530
04531 if (TREE_CODE (arg) == DEFAULT_ARG)
04532 {
04533 error ("the default argument for parameter %d of %qD has "
04534 "not yet been parsed",
04535 parmnum, fn);
04536 return error_mark_node;
04537 }
04538
04539 if (fn && DECL_TEMPLATE_INFO (fn))
04540 arg = tsubst_default_argument (fn, type, arg);
04541
04542 arg = break_out_target_exprs (arg);
04543
04544 if (TREE_CODE (arg) == CONSTRUCTOR)
04545 {
04546 arg = digest_init (type, arg, 0);
04547 arg = convert_for_initialization (0, type, arg, LOOKUP_NORMAL,
04548 "default argument", fn, parmnum);
04549 }
04550 else
04551 {
04552
04553 if (TREE_HAS_CONSTRUCTOR (arg))
04554 arg = copy_node (arg);
04555
04556 arg = convert_for_initialization (0, type, arg, LOOKUP_NORMAL,
04557 "default argument", fn, parmnum);
04558 arg = convert_for_arg_passing (type, arg);
04559 }
04560
04561 return arg;
04562 }
04563
04564
04565
04566
04567 tree
04568 type_passed_as (tree type)
04569 {
04570
04571 if (TREE_ADDRESSABLE (type))
04572 {
04573 type = build_reference_type (type);
04574
04575 type = build_qualified_type (type, TYPE_QUAL_RESTRICT);
04576 }
04577 else if (targetm.calls.promote_prototypes (type)
04578 && INTEGRAL_TYPE_P (type)
04579 && COMPLETE_TYPE_P (type)
04580 && INT_CST_LT_UNSIGNED (TYPE_SIZE (type),
04581 TYPE_SIZE (integer_type_node)))
04582 type = integer_type_node;
04583
04584 return type;
04585 }
04586
04587
04588
04589 tree
04590 convert_for_arg_passing (tree type, tree val)
04591 {
04592 if (val == error_mark_node)
04593 ;
04594
04595 else if (TREE_ADDRESSABLE (type))
04596 val = build1 (ADDR_EXPR, build_reference_type (type), val);
04597 else if (targetm.calls.promote_prototypes (type)
04598 && INTEGRAL_TYPE_P (type)
04599 && COMPLETE_TYPE_P (type)
04600 && INT_CST_LT_UNSIGNED (TYPE_SIZE (type),
04601 TYPE_SIZE (integer_type_node)))
04602 val = perform_integral_promotions (val);
04603 return val;
04604 }
04605
04606
04607
04608
04609
04610 static bool
04611 magic_varargs_p (tree fn)
04612 {
04613 if (DECL_BUILT_IN (fn))
04614 switch (DECL_FUNCTION_CODE (fn))
04615 {
04616 case BUILT_IN_CLASSIFY_TYPE:
04617 case BUILT_IN_CONSTANT_P:
04618 case BUILT_IN_NEXT_ARG:
04619 case BUILT_IN_STDARG_START:
04620 case BUILT_IN_VA_START:
04621 return true;
04622
04623 default:;
04624 }
04625
04626 return false;
04627 }
04628
04629
04630
04631
04632
04633
04634 static tree
04635 build_over_call (struct z_candidate *cand, int flags)
04636 {
04637 tree fn = cand->fn;
04638 tree args = cand->args;
04639 conversion **convs = cand->convs;
04640 conversion *conv;
04641 tree converted_args = NULL_TREE;
04642 tree parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
04643 tree arg, val;
04644 int i = 0;
04645 int is_method = 0;
04646
04647
04648
04649
04650
04651 if (processing_template_decl)
04652 {
04653 tree expr;
04654 tree return_type;
04655 return_type = TREE_TYPE (TREE_TYPE (fn));
04656 expr = build3 (CALL_EXPR, return_type, fn, args, NULL_TREE);
04657 if (TREE_THIS_VOLATILE (fn) && cfun)
04658 current_function_returns_abnormally = 1;
04659 if (!VOID_TYPE_P (return_type))
04660 require_complete_type (return_type);
04661 return convert_from_reference (expr);
04662 }
04663
04664
04665 if (cand->warnings)
04666 {
04667 struct candidate_warning *w;
04668 for (w = cand->warnings; w; w = w->next)
04669 joust (cand, w->loser, 1);
04670 }
04671
04672 if (DECL_FUNCTION_MEMBER_P (fn))
04673 {
04674
04675
04676
04677
04678
04679
04680
04681
04682
04683
04684
04685
04686
04687
04688
04689
04690
04691
04692
04693
04694
04695
04696
04697
04698
04699 if (DECL_TEMPLATE_INFO (fn)
04700 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (fn)))
04701 perform_or_defer_access_check (cand->access_path,
04702 DECL_TI_TEMPLATE (fn));
04703 else
04704 perform_or_defer_access_check (cand->access_path, fn);
04705 }
04706
04707 if (args && TREE_CODE (args) != TREE_LIST)
04708 args = build_tree_list (NULL_TREE, args);
04709 arg = args;
04710
04711
04712
04713 if (DECL_CONSTRUCTOR_P (fn))
04714 {
04715 converted_args = tree_cons (NULL_TREE, TREE_VALUE (arg), converted_args);
04716 arg = TREE_CHAIN (arg);
04717 parm = TREE_CHAIN (parm);
04718
04719 gcc_assert (!DECL_HAS_IN_CHARGE_PARM_P (fn));
04720
04721 if (DECL_HAS_VTT_PARM_P (fn))
04722 {
04723 converted_args = tree_cons
04724 (NULL_TREE, TREE_VALUE (arg), converted_args);
04725 arg = TREE_CHAIN (arg);
04726 parm = TREE_CHAIN (parm);
04727 }
04728 }
04729
04730 else if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
04731 {
04732 tree parmtype = TREE_VALUE (parm);
04733 tree argtype = TREE_TYPE (TREE_VALUE (arg));
04734 tree converted_arg;
04735 tree base_binfo;
04736
04737 if (convs[i]->bad_p)
04738 pedwarn ("passing %qT as %<this%> argument of %q#D discards qualifiers",
04739 TREE_TYPE (argtype), fn);
04740
04741
04742
04743
04744
04745
04746
04747 gcc_assert (TREE_CODE (parmtype) == POINTER_TYPE);
04748
04749 gcc_assert (cand->conversion_path != NULL_TREE);
04750 converted_arg = build_base_path (PLUS_EXPR,
04751 TREE_VALUE (arg),
04752 cand->conversion_path,
04753 1);
04754
04755 if (!accessible_base_p (TREE_TYPE (argtype),
04756 BINFO_TYPE (cand->conversion_path), true))
04757 error ("%qT is not an accessible base of %qT",
04758 BINFO_TYPE (cand->conversion_path),
04759 TREE_TYPE (argtype));
04760
04761
04762
04763 base_binfo = lookup_base (TREE_TYPE (TREE_TYPE (converted_arg)),
04764 TREE_TYPE (parmtype), ba_unique, NULL);
04765 converted_arg = build_base_path (PLUS_EXPR, converted_arg,
04766 base_binfo, 1);
04767
04768 converted_args = tree_cons (NULL_TREE, converted_arg, converted_args);
04769 parm = TREE_CHAIN (parm);
04770 arg = TREE_CHAIN (arg);
04771 ++i;
04772 is_method = 1;
04773 }
04774
04775 for (; arg && parm;
04776 parm = TREE_CHAIN (parm), arg = TREE_CHAIN (arg), ++i)
04777 {
04778 tree type = TREE_VALUE (parm);
04779
04780 conv = convs[i];
04781 val = convert_like_with_context
04782 (conv, TREE_VALUE (arg), fn, i - is_method);
04783
04784 val = convert_for_arg_passing (type, val);
04785 converted_args = tree_cons (NULL_TREE, val, converted_args);
04786 }
04787
04788
04789 for (; parm && parm != void_list_node; parm = TREE_CHAIN (parm), i++)
04790 converted_args
04791 = tree_cons (NULL_TREE,
04792 convert_default_arg (TREE_VALUE (parm),
04793 TREE_PURPOSE (parm),
04794 fn, i - is_method),
04795 converted_args);
04796
04797
04798 for (; arg; arg = TREE_CHAIN (arg))
04799 {
04800 tree a = TREE_VALUE (arg);
04801 if (magic_varargs_p (fn))
04802 ;
04803 else
04804 a = convert_arg_to_ellipsis (a);
04805 converted_args = tree_cons (NULL_TREE, a, converted_args);
04806 }
04807
04808 converted_args = nreverse (converted_args);
04809
04810 check_function_arguments (TYPE_ATTRIBUTES (TREE_TYPE (fn)),
04811 converted_args, TYPE_ARG_TYPES (TREE_TYPE (fn)));
04812
04813
04814
04815
04816 if (! flag_elide_constructors)
04817 ;
04818 else if (cand->num_convs == 1 && DECL_COPY_CONSTRUCTOR_P (fn))
04819 {
04820 tree targ;
04821 arg = skip_artificial_parms_for (fn, converted_args);
04822 arg = TREE_VALUE (arg);
04823
04824
04825 targ = arg;
04826 while (TREE_CODE (targ) == NOP_EXPR
04827 || TREE_CODE (targ) == NON_LVALUE_EXPR
04828 || TREE_CODE (targ) == CONVERT_EXPR)
04829 targ = TREE_OPERAND (targ, 0);
04830 if (TREE_CODE (targ) == ADDR_EXPR)
04831 {
04832 targ = TREE_OPERAND (targ, 0);
04833 if (!same_type_ignoring_top_level_qualifiers_p
04834 (TREE_TYPE (TREE_TYPE (arg)), TREE_TYPE (targ)))
04835 targ = NULL_TREE;
04836 }
04837 else
04838 targ = NULL_TREE;
04839
04840 if (targ)
04841 arg = targ;
04842 else
04843 arg = build_indirect_ref (arg, 0);
04844
04845
04846
04847 if (TYPE_HAS_COMPLEX_INIT_REF (DECL_CONTEXT (fn)))
04848 mark_used (fn);
04849
04850
04851
04852
04853
04854
04855 if (integer_zerop (TREE_VALUE (args)))
04856 {
04857 if (TREE_CODE (arg) == TARGET_EXPR)
04858 return arg;
04859 else if (TYPE_HAS_TRIVIAL_INIT_REF (DECL_CONTEXT (fn)))
04860 return build_target_expr_with_type (arg, DECL_CONTEXT (fn));
04861 }
04862 else if (TREE_CODE (arg) == TARGET_EXPR
04863 || TYPE_HAS_TRIVIAL_INIT_REF (DECL_CONTEXT (fn)))
04864 {
04865 tree to = stabilize_reference
04866 (build_indirect_ref (TREE_VALUE (args), 0));
04867
04868 val = build2 (INIT_EXPR, DECL_CONTEXT (fn), to, arg);
04869 return val;
04870 }
04871 }
04872 else if (DECL_OVERLOADED_OPERATOR_P (fn) == NOP_EXPR
04873 && copy_fn_p (fn)
04874 && TYPE_HAS_TRIVIAL_ASSIGN_REF (DECL_CONTEXT (fn)))
04875 {
04876 tree to = stabilize_reference
04877 (build_indirect_ref (TREE_VALUE (converted_args), 0));
04878 tree type = TREE_TYPE (to);
04879 tree as_base = CLASSTYPE_AS_BASE (type);
04880
04881 arg = TREE_VALUE (TREE_CHAIN (converted_args));
04882 if (tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (as_base)))
04883 {
04884 arg = build_indirect_ref (arg, 0);
04885 val = build2 (MODIFY_EXPR, TREE_TYPE (to), to, arg);
04886 }
04887 else
04888 {
04889
04890
04891
04892 tree args, t;
04893
04894 args = tree_cons (NULL, TYPE_SIZE_UNIT (as_base), NULL);
04895 args = tree_cons (NULL, arg, args);
04896 t = build_unary_op (ADDR_EXPR, to, 0);
04897 args = tree_cons (NULL, t, args);
04898 t = implicit_built_in_decls[BUILT_IN_MEMCPY];
04899 t = build_call (t, args);
04900
04901 t = convert (TREE_TYPE (TREE_VALUE (args)), t);
04902 val = build_indirect_ref (t, 0);
04903 }
04904
04905 return val;
04906 }
04907
04908 mark_used (fn);
04909
04910 if (DECL_VINDEX (fn) && (flags & LOOKUP_NONVIRTUAL) == 0)
04911 {
04912 tree t, *p = &TREE_VALUE (converted_args);
04913 tree binfo = lookup_base (TREE_TYPE (TREE_TYPE (*p)),
04914 DECL_CONTEXT (fn),
04915 ba_any, NULL);
04916 gcc_assert (binfo && binfo != error_mark_node);
04917
04918 *p = build_base_path (PLUS_EXPR, *p, binfo, 1);
04919 if (TREE_SIDE_EFFECTS (*p))
04920 *p = save_expr (*p);
04921 t = build_pointer_type (TREE_TYPE (fn));
04922 if (DECL_CONTEXT (fn) && TYPE_JAVA_INTERFACE (DECL_CONTEXT (fn)))
04923 fn = build_java_interface_fn_ref (fn, *p);
04924 else
04925 fn = build_vfn_ref (*p, DECL_VINDEX (fn));
04926 TREE_TYPE (fn) = t;
04927 }
04928 else if (DECL_INLINE (fn))
04929 fn = inline_conversion (fn);
04930 else
04931 fn = build_addr_func (fn);
04932
04933 return build_cxx_call (fn, converted_args);
04934 }
04935
04936
04937
04938
04939
04940 tree
04941 build_cxx_call (tree fn, tree args)
04942 {
04943 tree fndecl;
04944
04945 fn = build_call (fn, args);
04946
04947
04948 fndecl = get_callee_fndecl (fn);
04949 if ((!fndecl || !TREE_NOTHROW (fndecl))
04950 && at_function_scope_p ()
04951 && cfun)
04952 cp_function_chain->can_throw = 1;
04953
04954
04955
04956 fn = fold_if_not_in_template (fn);
04957
04958 if (VOID_TYPE_P (TREE_TYPE (fn)))
04959 return fn;
04960
04961 fn = require_complete_type (fn);
04962 if (fn == error_mark_node)
04963 return error_mark_node;
04964
04965 if (IS_AGGR_TYPE (TREE_TYPE (fn)))
04966 fn = build_cplus_new (TREE_TYPE (fn), fn);
04967 return convert_from_reference (fn);
04968 }
04969
04970 static GTY(()) tree java_iface_lookup_fn;
04971
04972
04973
04974
04975
04976 static tree
04977 build_java_interface_fn_ref (tree fn, tree instance)
04978 {
04979 tree lookup_args, lookup_fn, method, idx;
04980 tree klass_ref, iface, iface_ref;
04981 int i;
04982
04983 if (!java_iface_lookup_fn)
04984 {
04985 tree endlink = build_void_list_node ();
04986 tree t = tree_cons (NULL_TREE, ptr_type_node,
04987 tree_cons (NULL_TREE, ptr_type_node,
04988 tree_cons (NULL_TREE, java_int_type_node,
04989 endlink)));
04990 java_iface_lookup_fn
04991 = builtin_function ("_Jv_LookupInterfaceMethodIdx",
04992 build_function_type (ptr_type_node, t),
04993 0, NOT_BUILT_IN, NULL, NULL_TREE);
04994 }
04995
04996
04997
04998 klass_ref = build_vtbl_ref (build_indirect_ref (instance, 0),
04999 integer_zero_node);
05000
05001
05002 iface = DECL_CONTEXT (fn);
05003 iface_ref = lookup_field (iface, get_identifier ("class$"), 0, false);
05004 if (!iface_ref || TREE_CODE (iface_ref) != VAR_DECL
05005 || DECL_CONTEXT (iface_ref) != iface)
05006 {
05007 error ("could not find class$ field in java interface type %qT",
05008 iface);
05009 return error_mark_node;
05010 }
05011 iface_ref = build_address (iface_ref);
05012 iface_ref = convert (build_pointer_type (iface), iface_ref);
05013
05014
05015 i = 1;
05016 for (method = TYPE_METHODS (iface); method; method = TREE_CHAIN (method))
05017 {
05018 if (!DECL_VIRTUAL_P (method))
05019 continue;
05020 if (fn == method)
05021 break;
05022 i++;
05023 }
05024 idx = build_int_cst (NULL_TREE, i);
05025
05026 lookup_args = tree_cons (NULL_TREE, klass_ref,
05027 tree_cons (NULL_TREE, iface_ref,
05028 build_tree_list (NULL_TREE, idx)));
05029 lookup_fn = build1 (ADDR_EXPR,
05030 build_pointer_type (TREE_TYPE (java_iface_lookup_fn)),
05031 java_iface_lookup_fn);
05032 return build3 (CALL_EXPR, ptr_type_node, lookup_fn, lookup_args, NULL_TREE);
05033 }
05034
05035
05036
05037
05038
05039
05040 tree
05041 in_charge_arg_for_name (tree name)
05042 {
05043 if (name == base_ctor_identifier
05044 || name == base_dtor_identifier)
05045 return integer_zero_node;
05046 else if (name == complete_ctor_identifier)
05047 return integer_one_node;
05048 else if (name == complete_dtor_identifier)
05049 return integer_two_node;
05050 else if (name == deleting_dtor_identifier)
05051 return integer_three_node;
05052
05053
05054
05055 gcc_unreachable ();
05056 return NULL_TREE;
05057 }
05058
05059
05060
05061
05062
05063
05064
05065
05066
05067
05068
05069
05070
05071 tree
05072 build_special_member_call (tree instance, tree name, tree args,
05073 tree binfo, int flags)
05074 {
05075 tree fns;
05076
05077 tree class_type;
05078
05079 gcc_assert (name == complete_ctor_identifier
05080 || name == base_ctor_identifier
05081 || name == complete_dtor_identifier
05082 || name == base_dtor_identifier
05083 || name == deleting_dtor_identifier
05084 || name == ansi_assopname (NOP_EXPR));
05085 if (TYPE_P (binfo))
05086 {
05087
05088 if (!complete_type_or_else (binfo, NULL_TREE))
05089 return error_mark_node;
05090
05091 binfo = TYPE_BINFO (binfo);
05092 }
05093
05094 gcc_assert (binfo != NULL_TREE);
05095
05096 class_type = BINFO_TYPE (binfo);
05097
05098
05099 if (name == complete_ctor_identifier && !instance)
05100 {
05101 instance = build_int_cst (build_pointer_type (class_type), 0);
05102 instance = build1 (INDIRECT_REF, class_type, instance);
05103 }
05104 else
05105 {
05106 if (name == complete_dtor_identifier
05107 || name == base_dtor_identifier
05108 || name == deleting_dtor_identifier)
05109 gcc_assert (args == NULL_TREE);
05110
05111
05112 if (!same_type_ignoring_top_level_qualifiers_p
05113 (TREE_TYPE (instance), BINFO_TYPE (binfo)))
05114 {
05115 if (name != ansi_assopname (NOP_EXPR))
05116
05117
05118
05119
05120
05121 instance = convert_to_base_statically (instance, binfo);
05122 else
05123
05124
05125 instance = build_base_path (PLUS_EXPR, instance,
05126 binfo, 1);
05127 }
05128 }
05129
05130 gcc_assert (instance != NULL_TREE);
05131
05132 fns = lookup_fnfields (binfo, name, 1);
05133
05134
05135
05136
05137 if ((name == base_ctor_identifier
05138 || name == base_dtor_identifier)
05139 && CLASSTYPE_VBASECLASSES (class_type))
05140 {
05141 tree vtt;
05142 tree sub_vtt;
05143
05144
05145
05146
05147 vtt = TREE_CHAIN (CLASSTYPE_VTABLES (current_class_type));
05148 vtt = decay_conversion (vtt);
05149 vtt = build3 (COND_EXPR, TREE_TYPE (vtt),
05150 build2 (EQ_EXPR, boolean_type_node,
05151 current_in_charge_parm, integer_zero_node),
05152 current_vtt_parm,
05153 vtt);
05154 gcc_assert (BINFO_SUBVTT_INDEX (binfo));
05155 sub_vtt = build2 (PLUS_EXPR, TREE_TYPE (vtt), vtt,
05156 BINFO_SUBVTT_INDEX (binfo));
05157
05158 args = tree_cons (NULL_TREE, sub_vtt, args);
05159 }
05160
05161 return build_new_method_call (instance, fns, args,
05162 TYPE_BINFO (BINFO_TYPE (binfo)),
05163 flags);
05164 }
05165
05166
05167
05168
05169
05170
05171
05172
05173
05174 static char *
05175 name_as_c_string (tree name, tree type, bool *free_p)
05176 {
05177 char *pretty_name;
05178
05179
05180 *free_p = false;
05181
05182 if (IDENTIFIER_CTOR_OR_DTOR_P (name))
05183 {
05184 pretty_name
05185 = (char *) IDENTIFIER_POINTER (constructor_name (type));
05186
05187 if (name == complete_dtor_identifier
05188 || name == base_dtor_identifier
05189 || name == deleting_dtor_identifier)
05190 {
05191 pretty_name = concat ("~", pretty_name, NULL);
05192
05193 *free_p = true;
05194 }
05195 }
05196 else if (IDENTIFIER_TYPENAME_P (name))
05197 {
05198 pretty_name = concat ("operator ",
05199 type_as_string (TREE_TYPE (name),
05200 TFF_PLAIN_IDENTIFIER),
05201 NULL);
05202
05203 *free_p = true;
05204 }
05205 else
05206 pretty_name = (char *) IDENTIFIER_POINTER (name);
05207
05208 return pretty_name;
05209 }
05210
05211
05212
05213 tree
05214 build_new_method_call (tree instance, tree fns, tree args,
05215 tree conversion_path, int flags)
05216 {
05217 struct z_candidate *candidates = 0, *cand;
05218 tree explicit_targs = NULL_TREE;
05219 tree basetype = NULL_TREE;
05220 tree access_binfo;
05221 tree optype;
05222 tree mem_args = NULL_TREE, instance_ptr;
05223 tree name;
05224 tree user_args;
05225 tree call;
05226 tree fn;
05227 tree class_type;
05228 int template_only = 0;
05229 bool any_viable_p;
05230 tree orig_instance;
05231 tree orig_fns;
05232 tree orig_args;
05233 void *p;
05234
05235 gcc_assert (instance != NULL_TREE);
05236
05237 if (error_operand_p (instance)
05238 || error_operand_p (fns)
05239 || args == error_mark_node)
05240 return error_mark_node;
05241
05242 orig_instance = instance;
05243 orig_fns = fns;
05244 orig_args = args;
05245
05246 if (processing_template_decl)
05247 {
05248 instance = build_non_dependent_expr (instance);
05249 if (!BASELINK_P (fns)
05250 && TREE_CODE (fns) != PSEUDO_DTOR_EXPR
05251 && TREE_TYPE (fns) != unknown_type_node)
05252 fns = build_non_dependent_expr (fns);
05253 args = build_non_dependent_args (orig_args);
05254 }
05255
05256
05257 user_args = args;
05258 args = resolve_args (args);
05259 if (args == error_mark_node)
05260 return error_mark_node;
05261
05262 basetype = TYPE_MAIN_VARIANT (TREE_TYPE (instance));
05263 instance_ptr = build_this (instance);
05264
05265 if (!BASELINK_P (fns))
05266 {
05267 error ("call to non-function %qD", fns);
05268 return error_mark_node;
05269 }
05270
05271 if (!conversion_path)
05272 conversion_path = BASELINK_BINFO (fns);
05273 access_binfo = BASELINK_ACCESS_BINFO (fns);
05274 optype = BASELINK_OPTYPE (fns);
05275 fns = BASELINK_FUNCTIONS (fns);
05276
05277 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
05278 {
05279 explicit_targs = TREE_OPERAND (fns, 1);
05280 fns = TREE_OPERAND (fns, 0);
05281 template_only = 1;
05282 }
05283
05284 gcc_assert (TREE_CODE (fns) == FUNCTION_DECL
05285 || TREE_CODE (fns) == TEMPLATE_DECL
05286 || TREE_CODE (fns) == OVERLOAD);
05287
05288
05289 if (! IS_AGGR_TYPE (basetype))
05290 {
05291 if ((flags & LOOKUP_COMPLAIN) && basetype != error_mark_node)
05292 error ("request for member %qD in %qE, which is of non-aggregate "
05293 "type %qT",
05294 fns, instance, basetype);
05295
05296 return error_mark_node;
05297 }
05298
05299 fn = get_first_fn (fns);
05300 name = DECL_NAME (fn);
05301
05302 if (IDENTIFIER_CTOR_OR_DTOR_P (name))
05303 {
05304
05305
05306 gcc_assert (name != ctor_identifier);
05307
05308 gcc_assert (name != dtor_identifier);
05309 }
05310
05311
05312
05313 if (DECL_DESTRUCTOR_P (fn))
05314 {
05315 tree type = build_pointer_type (basetype);
05316 if (!same_type_p (type, TREE_TYPE (instance_ptr)))
05317 instance_ptr = build_nop (type, instance_ptr);
05318 }
05319
05320 class_type = (conversion_path ? BINFO_TYPE (conversion_path) : NULL_TREE);
05321 mem_args = tree_cons (NULL_TREE, instance_ptr, args);
05322
05323
05324 p = conversion_obstack_alloc (0);
05325
05326 for (fn = fns; fn; fn = OVL_NEXT (fn))
05327 {
05328 tree t = OVL_CURRENT (fn);
05329 tree this_arglist;
05330
05331
05332 if ((flags & LOOKUP_ONLYCONVERTING)
05333 && DECL_NONCONVERTING_P (t))
05334 continue;
05335
05336 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (t))
05337 this_arglist = mem_args;
05338 else
05339 this_arglist = args;
05340
05341 if (TREE_CODE (t) == TEMPLATE_DECL)
05342
05343 add_template_candidate (&candidates, t,
05344 class_type,
05345 explicit_targs,
05346 this_arglist, optype,
05347 access_binfo,
05348 conversion_path,
05349 flags,
05350 DEDUCE_CALL);
05351 else if (! template_only)
05352 add_function_candidate (&candidates, t,
05353 class_type,
05354 this_arglist,
05355 access_binfo,
05356 conversion_path,
05357 flags);
05358 }
05359
05360 candidates = splice_viable (candidates, pedantic, &any_viable_p);
05361 if (!any_viable_p)
05362 {
05363 if (!COMPLETE_TYPE_P (basetype))
05364 cxx_incomplete_type_error (instance_ptr, basetype);
05365 else
05366 {
05367 char *pretty_name;
05368 bool free_p;
05369
05370 pretty_name = name_as_c_string (name, basetype, &free_p);
05371 error ("no matching function for call to %<%T::%s(%A)%#V%>",
05372 basetype, pretty_name, user_args,
05373 TREE_TYPE (TREE_TYPE (instance_ptr)));
05374 if (free_p)
05375 free (pretty_name);
05376 }
05377 print_z_candidates (candidates);
05378 call = error_mark_node;
05379 }
05380 else
05381 {
05382 cand = tourney (candidates);
05383 if (cand == 0)
05384 {
05385 char *pretty_name;
05386 bool free_p;
05387
05388 pretty_name = name_as_c_string (name, basetype, &free_p);
05389 error ("call of overloaded %<%s(%A)%> is ambiguous", pretty_name,
05390 user_args);
05391 print_z_candidates (candidates);
05392 if (free_p)
05393 free (pretty_name);
05394 call = error_mark_node;
05395 }
05396 else
05397 {
05398 if (!(flags & LOOKUP_NONVIRTUAL)
05399 && DECL_PURE_VIRTUAL_P (cand->fn)
05400 && instance == current_class_ref
05401 && (DECL_CONSTRUCTOR_P (current_function_decl)
05402 || DECL_DESTRUCTOR_P (current_function_decl)))
05403
05404
05405 warning ((DECL_CONSTRUCTOR_P (current_function_decl) ?
05406 "abstract virtual %q#D called from constructor"
05407 : "abstract virtual %q#D called from destructor"),
05408 cand->fn);
05409
05410 if (TREE_CODE (TREE_TYPE (cand->fn)) == METHOD_TYPE
05411 && is_dummy_object (instance_ptr))
05412 {
05413 error ("cannot call member function %qD without object",
05414 cand->fn);
05415 call = error_mark_node;
05416 }
05417 else
05418 {
05419 if (DECL_VINDEX (cand->fn) && ! (flags & LOOKUP_NONVIRTUAL)
05420 && resolves_to_fixed_type_p (instance, 0))
05421 flags |= LOOKUP_NONVIRTUAL;
05422
05423 call = build_over_call (cand, flags);
05424
05425
05426
05427
05428 if (TREE_CODE (TREE_TYPE (cand->fn)) != METHOD_TYPE
05429 && !is_dummy_object (instance_ptr)
05430 && TREE_SIDE_EFFECTS (instance))
05431 call = build2 (COMPOUND_EXPR, TREE_TYPE (call),
05432 instance, call);
05433 }
05434 }
05435 }
05436
05437 if (processing_template_decl && call != error_mark_node)
05438 call = (build_min_non_dep
05439 (CALL_EXPR, call,
05440 build_min_nt (COMPONENT_REF, orig_instance, orig_fns, NULL_TREE),
05441 orig_args, NULL_TREE));
05442
05443
05444 obstack_free (&conversion_obstack, p);
05445
05446 return call;
05447 }
05448
05449
05450
05451
05452 static bool
05453 is_subseq (conversion *ics1, conversion *ics2)
05454 {
05455
05456
05457
05458
05459 while (ics1->kind == ck_rvalue
05460 || ics1->kind == ck_lvalue)
05461 ics1 = ics1->u.next;
05462
05463 while (1)
05464 {
05465 while (ics2->kind == ck_rvalue
05466 || ics2->kind == ck_lvalue)
05467 ics2 = ics2->u.next;
05468
05469 if (ics2->kind == ck_user
05470 || ics2->kind == ck_ambig
05471 || ics2->kind == ck_identity)
05472
05473
05474
05475
05476 return false;
05477
05478 ics2 = ics2->u.next;
05479
05480 if (ics2->kind == ics1->kind
05481 && same_type_p (ics2->type, ics1->type)
05482 && same_type_p (ics2->u.next->type,
05483 ics1->u.next->type))
05484 return true;
05485 }
05486 }
05487
05488
05489
05490
05491 bool
05492 is_properly_derived_from (tree derived, tree base)
05493 {
05494 if (!IS_AGGR_TYPE_CODE (TREE_CODE (derived))
05495 || !IS_AGGR_TYPE_CODE (TREE_CODE (base)))
05496 return false;
05497
05498
05499
05500 return (!same_type_ignoring_top_level_qualifiers_p (derived, base)
05501 && DERIVED_FROM_P (base, derived));
05502 }
05503
05504
05505
05506
05507
05508
05509
05510 static void
05511 maybe_handle_implicit_object (conversion **ics)
05512 {
05513 if ((*ics)->this_p)
05514 {
05515
05516
05517
05518
05519
05520
05521
05522 conversion *t = *ics;
05523 tree reference_type;
05524
05525
05526
05527
05528 reference_type = TREE_TYPE (t->type);
05529 reference_type = build_reference_type (reference_type);
05530
05531 if (t->kind == ck_qual)
05532 t = t->u.next;
05533 if (t->kind == ck_ptr)
05534 t = t->u.next;
05535 t = build_identity_conv (TREE_TYPE (t->type), NULL_TREE);
05536 t = direct_reference_binding (reference_type, t);
05537 *ics = t;
05538 }
05539 }
05540
05541
05542
05543
05544
05545 static tree
05546 maybe_handle_ref_bind (conversion **ics)
05547 {
05548 if ((*ics)->kind == ck_ref_bind)
05549 {
05550 conversion *old_ics = *ics;
05551 tree type = TREE_TYPE (old_ics->type);
05552 *ics = old_ics->u.next;
05553 (*ics)->user_conv_p = old_ics->user_conv_p;
05554 (*ics)->bad_p = old_ics->bad_p;
05555 return type;
05556 }
05557
05558 return NULL_TREE;
05559 }
05560
05561
05562
05563
05564
05565
05566
05567
05568 static int
05569 compare_ics (conversion *ics1, conversion *ics2)
05570 {
05571 tree from_type1;
05572 tree from_type2;
05573 tree to_type1;
05574 tree to_type2;
05575 tree deref_from_type1 = NULL_TREE;
05576 tree deref_from_type2 = NULL_TREE;
05577 tree deref_to_type1 = NULL_TREE;
05578 tree deref_to_type2 = NULL_TREE;
05579 conversion_rank rank1, rank2;
05580
05581
05582
05583
05584 tree target_type1;
05585 tree target_type2;
05586
05587
05588 maybe_handle_implicit_object (&ics1);
05589 maybe_handle_implicit_object (&ics2);
05590
05591
05592 target_type1 = maybe_handle_ref_bind (&ics1);
05593 target_type2 = maybe_handle_ref_bind (&ics2);
05594
05595
05596
05597
05598
05599
05600
05601
05602
05603
05604
05605
05606
05607 rank1 = CONVERSION_RANK (ics1);
05608 rank2 = CONVERSION_RANK (ics2);
05609
05610 if (rank1 > rank2)
05611 return -1;
05612 else if (rank1 < rank2)
05613 return 1;
05614
05615 if (rank1 == cr_bad)
05616 {
05617
05618
05619
05620 if (ics1->user_conv_p > ics2->user_conv_p
05621 || ics1->rank > ics2->rank)
05622 return -1;
05623 else if (ics1->user_conv_p < ics2->user_conv_p
05624 || ics1->rank < ics2->rank)
05625 return 1;
05626
05627
05628 }
05629
05630 if (ics1->ellipsis_p)
05631
05632 return 0;
05633
05634
05635
05636
05637
05638
05639
05640 if (ics1->user_conv_p)
05641 {
05642 conversion *t1;
05643 conversion *t2;
05644
05645 for (t1 = ics1; t1->kind != ck_user; t1 = t1->u.next)
05646 if (t1->kind == ck_ambig)
05647 return 0;
05648 for (t2 = ics2; t2->kind != ck_user; t2 = t2->u.next)
05649 if (t2->kind == ck_ambig)
05650 return 0;
05651
05652 if (t1->cand->fn != t2->cand->fn)
05653 return 0;
05654
05655
05656
05657 from_type1 = t1->type;
05658 from_type2 = t2->type;
05659 }
05660 else
05661 {
05662 conversion *t1;
05663 conversion *t2;
05664
05665
05666
05667
05668
05669
05670
05671
05672
05673
05674
05675
05676
05677
05678 t1 = ics1;
05679 while (t1->kind != ck_identity)
05680 t1 = t1->u.next;
05681 from_type1 = t1->type;
05682
05683 t2 = ics2;
05684 while (t2->kind != ck_identity)
05685 t2 = t2->u.next;
05686 from_type2 = t2->type;
05687 }
05688
05689 if (same_type_p (from_type1, from_type2))
05690 {
05691 if (is_subseq (ics1, ics2))
05692 return 1;
05693 if (is_subseq (ics2, ics1))
05694 return -1;
05695 }
05696
05697
05698
05699
05700
05701
05702
05703
05704
05705
05706
05707
05708
05709
05710
05711
05712
05713
05714
05715
05716
05717
05718
05719
05720
05721 if (ics1->rank < ics2->rank)
05722 return 1;
05723 else if (ics2->rank < ics1->rank)
05724 return -1;
05725
05726 to_type1 = ics1->type;
05727 to_type2 = ics2->type;
05728
05729 if (TYPE_PTR_P (from_type1)
05730 && TYPE_PTR_P (from_type2)
05731 && TYPE_PTR_P (to_type1)
05732 && TYPE_PTR_P (to_type2))
05733 {
05734 deref_from_type1 = TREE_TYPE (from_type1);
05735 deref_from_type2 = TREE_TYPE (from_type2);
05736 deref_to_type1 = TREE_TYPE (to_type1);
05737 deref_to_type2 = TREE_TYPE (to_type2);
05738 }
05739
05740
05741
05742
05743 else if ((TYPE_PTRMEM_P (from_type1) && TYPE_PTRMEM_P (from_type2)
05744 && TYPE_PTRMEM_P (to_type1) && TYPE_PTRMEM_P (to_type2))
05745 || (TYPE_PTRMEMFUNC_P (from_type1)
05746 && TYPE_PTRMEMFUNC_P (from_type2)
05747 && TYPE_PTRMEMFUNC_P (to_type1)
05748 && TYPE_PTRMEMFUNC_P (to_type2)))
05749 {
05750 deref_to_type1 = TYPE_PTRMEM_CLASS_TYPE (from_type1);
05751 deref_to_type2 = TYPE_PTRMEM_CLASS_TYPE (from_type2);
05752 deref_from_type1 = TYPE_PTRMEM_CLASS_TYPE (to_type1);
05753 deref_from_type2 = TYPE_PTRMEM_CLASS_TYPE (to_type2);
05754 }
05755
05756 if (deref_from_type1 != NULL_TREE
05757 && IS_AGGR_TYPE_CODE (TREE_CODE (deref_from_type1))
05758 && IS_AGGR_TYPE_CODE (TREE_CODE (deref_from_type2)))
05759 {
05760
05761
05762
05763
05764
05765
05766
05767
05768 if (TREE_CODE (deref_to_type1) == VOID_TYPE
05769 && TREE_CODE (deref_to_type2) == VOID_TYPE)
05770 {
05771 if (is_properly_derived_from (deref_from_type1,
05772 deref_from_type2))
05773 return -1;
05774 else if (is_properly_derived_from (deref_from_type2,
05775 deref_from_type1))
05776 return 1;
05777 }
05778 else if (TREE_CODE (deref_to_type1) == VOID_TYPE
05779 || TREE_CODE (deref_to_type2) == VOID_TYPE)
05780 {
05781 if (same_type_p (deref_from_type1, deref_from_type2))
05782 {
05783 if (TREE_CODE (deref_to_type2) == VOID_TYPE)
05784 {
05785 if (is_properly_derived_from (deref_from_type1,
05786 deref_to_type1))
05787 return 1;
05788 }
05789
05790 else if (is_properly_derived_from (deref_from_type1,
05791 deref_to_type2))
05792 return -1;
05793 }
05794 }
05795 else if (IS_AGGR_TYPE_CODE (TREE_CODE (deref_to_type1))
05796 && IS_AGGR_TYPE_CODE (TREE_CODE (deref_to_type2)))
05797 {
05798
05799
05800
05801
05802
05803
05804
05805
05806
05807
05808 if (same_type_p (deref_from_type1, deref_from_type2))
05809 {
05810 if (is_properly_derived_from (deref_to_type1,
05811 deref_to_type2))
05812 return 1;
05813 else if (is_properly_derived_from (deref_to_type2,
05814 deref_to_type1))
05815 return -1;
05816 }
05817 else if (same_type_p (deref_to_type1, deref_to_type2))
05818 {
05819 if (is_properly_derived_from (deref_from_type2,
05820 deref_from_type1))
05821 return 1;
05822 else if (is_properly_derived_from (deref_from_type1,
05823 deref_from_type2))
05824 return -1;
05825 }
05826 }
05827 }
05828 else if (CLASS_TYPE_P (non_reference (from_type1))
05829 && same_type_p (from_type1, from_type2))
05830 {
05831 tree from = non_reference (from_type1);
05832
05833
05834
05835
05836
05837
05838
05839
05840 if (is_properly_derived_from (from, to_type1)
05841 && is_properly_derived_from (from, to_type2))
05842 {
05843 if (is_properly_derived_from (to_type1, to_type2))
05844 return 1;
05845 else if (is_properly_derived_from (to_type2, to_type1))
05846 return -1;
05847 }
05848 }
05849 else if (CLASS_TYPE_P (non_reference (to_type1))
05850 && same_type_p (to_type1, to_type2))
05851 {
05852 tree to = non_reference (to_type1);
05853
05854
05855
05856
05857
05858
05859
05860
05861 if (is_properly_derived_from (from_type1, to)
05862 && is_properly_derived_from (from_type2, to))
05863 {
05864 if (is_properly_derived_from (from_type2, from_type1))
05865 return 1;
05866 else if (is_properly_derived_from (from_type1, from_type2))
05867 return -1;
05868 }
05869 }
05870
05871
05872
05873
05874
05875
05876
05877 if (ics1->kind == ck_qual
05878 && ics2->kind == ck_qual
05879 && same_type_p (from_type1, from_type2))
05880 return comp_cv_qual_signature (to_type1, to_type2);
05881
05882
05883
05884
05885
05886
05887
05888
05889
05890 if (target_type1 && target_type2
05891 && same_type_ignoring_top_level_qualifiers_p (to_type1, to_type2))
05892 return comp_cv_qualification (target_type2, target_type1);
05893
05894
05895 return 0;
05896 }
05897
05898
05899
05900 static tree
05901 source_type (conversion *t)
05902 {
05903 for (;; t = t->u.next)
05904 {
05905 if (t->kind == ck_user
05906 || t->kind == ck_ambig
05907 || t->kind == ck_identity)
05908 return t->type;
05909 }
05910 gcc_unreachable ();
05911 }
05912
05913
05914
05915
05916
05917 static void
05918 add_warning (struct z_candidate *winner, struct z_candidate *loser)
05919 {
05920 candidate_warning *cw;
05921
05922 cw = conversion_obstack_alloc (sizeof (candidate_warning));
05923 cw->loser = loser;
05924 cw->next = winner->warnings;
05925 winner->warnings = cw;
05926 }
05927
05928
05929
05930
05931
05932
05933
05934
05935 static int
05936 joust (struct z_candidate *cand1, struct z_candidate *cand2, bool warn)
05937 {
05938 int winner = 0;
05939 int off1 = 0, off2 = 0;
05940 size_t i;
05941 size_t len;
05942
05943
05944
05945 if (cand1->viable > cand2->viable)
05946 return 1;
05947 if (cand1->viable < cand2->viable)
05948 return -1;
05949
05950
05951
05952 if (cand1->fn == cand2->fn
05953 && (IS_TYPE_OR_DECL_P (cand1->fn)))
05954 return 1;
05955
05956
05957
05958
05959
05960
05961
05962
05963
05964
05965
05966
05967
05968 len = cand1->num_convs;
05969 if (len != cand2->num_convs)
05970 {
05971 int static_1 = DECL_STATIC_FUNCTION_P (cand1->fn);
05972 int static_2 = DECL_STATIC_FUNCTION_P (cand2->fn);
05973
05974 gcc_assert (static_1 != static_2);
05975
05976 if (static_1)
05977 off2 = 1;
05978 else
05979 {
05980 off1 = 1;
05981 --len;
05982 }
05983 }
05984
05985 for (i = 0; i < len; ++i)
05986 {
05987 conversion *t1 = cand1->convs[i + off1];
05988 conversion *t2 = cand2->convs[i + off2];
05989 int comp = compare_ics (t1, t2);
05990
05991 if (comp != 0)
05992 {
05993 if (warn_sign_promo
05994 && (CONVERSION_RANK (t1) + CONVERSION_RANK (t2)
05995 == cr_std + cr_promotion)
05996 && t1->kind == ck_std
05997 && t2->kind == ck_std
05998 && TREE_CODE (t1->type) == INTEGER_TYPE
05999 && TREE_CODE (t2->type) == INTEGER_TYPE
06000 && (TYPE_PRECISION (t1->type)
06001 == TYPE_PRECISION (t2->type))
06002 && (TYPE_UNSIGNED (t1->u.next->type)
06003 || (TREE_CODE (t1->u.next->type)
06004 == ENUMERAL_TYPE)))
06005 {
06006 tree type = t1->u.next->type;
06007 tree type1, type2;
06008 struct z_candidate *w, *l;
06009 if (comp > 0)
06010 type1 = t1->type, type2 = t2->type,
06011 w = cand1, l = cand2;
06012 else
06013 type1 = t2->type, type2 = t1->type,
06014 w = cand2, l = cand1;
06015
06016 if (warn)
06017 {
06018 warning ("passing %qT chooses %qT over %qT",
06019 type, type1, type2);
06020 warning (" in call to %qD", w->fn);
06021 }
06022 else
06023 add_warning (w, l);
06024 }
06025
06026 if (winner && comp != winner)
06027 {
06028 winner = 0;
06029 goto tweak;
06030 }
06031 winner = comp;
06032 }
06033 }
06034
06035
06036
06037
06038 if (winner && warn_conversion && cand1->second_conv
06039 && (!DECL_CONSTRUCTOR_P (cand1->fn) || !DECL_CONSTRUCTOR_P (cand2->fn))
06040 && winner != compare_ics (cand1->second_conv, cand2->second_conv))
06041 {
06042 struct z_candidate *w, *l;
06043 bool give_warning = false;
06044
06045 if (winner == 1)
06046 w = cand1, l = cand2;
06047 else
06048 w = cand2, l = cand1;
06049
06050
06051
06052
06053 if (DECL_CONTEXT (w->fn) == DECL_CONTEXT (l->fn)
06054 && !DECL_CONSTRUCTOR_P (w->fn) && !DECL_CONSTRUCTOR_P (l->fn))
06055 {
06056 tree t = TREE_TYPE (TREE_TYPE (l->fn));
06057 tree f = TREE_TYPE (TREE_TYPE (w->fn));
06058
06059 if (TREE_CODE (t) == TREE_CODE (f) && POINTER_TYPE_P (t))
06060 {
06061 t = TREE_TYPE (t);
06062 f = TREE_TYPE (f);
06063 }
06064 if (!comp_ptr_ttypes (t, f))
06065 give_warning = true;
06066 }
06067 else
06068 give_warning = true;
06069
06070 if (!give_warning)
06071 ;
06072 else if (warn)
06073 {
06074 tree source = source_type (w->convs[0]);
06075 if (! DECL_CONSTRUCTOR_P (w->fn))
06076 source = TREE_TYPE (source);
06077 warning ("choosing %qD over %qD", w->fn, l->fn);
06078 warning (" for conversion from %qT to %qT",
06079 source, w->second_conv->type);
06080 warning (" because conversion sequence for the argument is better");
06081 }
06082 else
06083 add_warning (w, l);
06084 }
06085
06086 if (winner)
06087 return winner;
06088
06089
06090
06091
06092
06093 if (!cand1->template_decl && cand2->template_decl)
06094 return 1;
06095 else if (cand1->template_decl && !cand2->template_decl)
06096 return -1;
06097
06098
06099
06100
06101
06102
06103 if (cand1->template_decl && cand2->template_decl)
06104 {
06105 winner = more_specialized_fn
06106 (TI_TEMPLATE (cand1->template_decl),
06107 TI_TEMPLATE (cand2->template_decl),
06108
06109
06110
06111
06112
06113
06114
06115
06116 cand1->num_convs
06117 - (DECL_NONSTATIC_MEMBER_FUNCTION_P (cand1->fn)
06118 - DECL_CONSTRUCTOR_P (cand1->fn)));
06119 if (winner)
06120 return winner;
06121 }
06122
06123
06124
06125
06126
06127
06128
06129
06130
06131 if (cand1->second_conv)
06132 {
06133 winner = compare_ics (cand1->second_conv, cand2->second_conv);
06134 if (winner)
06135 return winner;
06136 }
06137
06138
06139
06140
06141
06142
06143
06144
06145
06146
06147
06148
06149 if (TREE_CODE (cand1->fn) == IDENTIFIER_NODE
06150 || TREE_CODE (cand2->fn) == IDENTIFIER_NODE)
06151 {
06152 for (i = 0; i < len; ++i)
06153 if (!same_type_p (cand1->convs[i]->type,
06154 cand2->convs[i]->type))
06155 break;
06156 if (i == cand1->num_convs)
06157 {
06158 if (cand1->fn == cand2->fn)
06159
06160 return 1;
06161 else if (TREE_CODE (cand1->fn) == IDENTIFIER_NODE)
06162
06163 return -1;
06164 else
06165
06166 return 1;
06167 }
06168 }
06169
06170
06171
06172 if (DECL_P (cand1->fn) && DECL_P (cand2->fn)
06173 && equal_functions (cand1->fn, cand2->fn))
06174 return 1;
06175
06176 tweak:
06177
06178
06179
06180 if (!pedantic)
06181 {
06182 conversion_rank rank1 = cr_identity, rank2 = cr_identity;
06183 struct z_candidate *w = 0, *l = 0;
06184
06185 for (i = 0; i < len; ++i)
06186 {
06187 if (CONVERSION_RANK (cand1->convs[i+off1]) > rank1)
06188 rank1 = CONVERSION_RANK (cand1->convs[i+off1]);
06189 if (CONVERSION_RANK (cand2->convs[i + off2]) > rank2)
06190 rank2 = CONVERSION_RANK (cand2->convs[i + off2]);
06191 }
06192 if (rank1 < rank2)
06193 winner = 1, w = cand1, l = cand2;
06194 if (rank1 > rank2)
06195 winner = -1, w = cand2, l = cand1;
06196 if (winner)
06197 {
06198 if (warn)
06199 {
06200 pedwarn ("\
06201 ISO C++ says that these are ambiguous, even \
06202 though the worst conversion for the first is better than \
06203 the worst conversion for the second:");
06204 print_z_candidate (_("candidate 1:"), w);
06205 print_z_candidate (_("candidate 2:"), l);
06206 }
06207 else
06208 add_warning (w, l);
06209 return winner;
06210 }
06211 }
06212
06213 gcc_assert (!winner);
06214 return 0;
06215 }
06216
06217
06218
06219
06220
06221
06222 static struct z_candidate *
06223 tourney (struct z_candidate *candidates)
06224 {
06225 struct z_candidate *champ = candidates, *challenger;
06226 int fate;
06227 int champ_compared_to_predecessor = 0;
06228
06229
06230
06231
06232 for (challenger = champ->next; challenger; )
06233 {
06234 fate = joust (champ, challenger, 0);
06235 if (fate == 1)
06236 challenger = challenger->next;
06237 else
06238 {
06239 if (fate == 0)
06240 {
06241 champ = challenger->next;
06242 if (champ == 0)
06243 return 0;
06244 champ_compared_to_predecessor = 0;
06245 }
06246 else
06247 {
06248 champ = challenger;
06249 champ_compared_to_predecessor = 1;
06250 }
06251
06252 challenger = champ->next;
06253 }
06254 }
06255
06256
06257
06258
06259 for (challenger = candidates;
06260 challenger != champ
06261 && !(champ_compared_to_predecessor && challenger->next == champ);
06262 challenger = challenger->next)
06263 {
06264 fate = joust (champ, challenger, 0);
06265 if (fate != 1)
06266 return 0;
06267 }
06268
06269 return champ;
06270 }
06271
06272
06273
06274 bool
06275 can_convert (tree to, tree from)
06276 {
06277 return can_convert_arg (to, from, NULL_TREE);
06278 }
06279
06280
06281
06282 bool
06283 can_convert_arg (tree to, tree from, tree arg)
06284 {
06285 conversion *t;
06286 void *p;
06287 bool ok_p;
06288
06289
06290 p = conversion_obstack_alloc (0);
06291
06292 t = implicit_conversion (to, from, arg, false,
06293 LOOKUP_NORMAL);
06294 ok_p = (t && !t->bad_p);
06295
06296
06297 obstack_free (&conversion_obstack, p);
06298
06299 return ok_p;
06300 }
06301
06302
06303
06304 bool
06305 can_convert_arg_bad (tree to, tree from, tree arg)
06306 {
06307 conversion *t;
06308 void *p;
06309
06310
06311 p = conversion_obstack_alloc (0);
06312
06313 t = implicit_conversion (to, from, arg, false,
06314 LOOKUP_NORMAL);
06315
06316 obstack_free (&conversion_obstack, p);
06317
06318 return t != NULL;
06319 }
06320
06321
06322
06323
06324
06325
06326
06327 tree
06328 perform_implicit_conversion (tree type, tree expr)
06329 {
06330 conversion *conv;
06331 void *p;
06332
06333 if (error_operand_p (expr))
06334 return error_mark_node;
06335
06336
06337 p = conversion_obstack_alloc (0);
06338
06339 conv = implicit_conversion (type, TREE_TYPE (expr), expr,
06340 false,
06341 LOOKUP_NORMAL);
06342 if (!conv)
06343 {
06344 error ("could not convert %qE to %qT", expr, type);
06345 expr = error_mark_node;
06346 }
06347 else
06348 expr = convert_like (conv, expr);
06349
06350
06351 obstack_free (&conversion_obstack, p);
06352
06353 return expr;
06354 }
06355
06356
06357
06358
06359
06360
06361
06362
06363
06364 tree
06365 perform_direct_initialization_if_possible (tree type,
06366 tree expr,
06367 bool c_cast_p)
06368 {
06369 conversion *conv;
06370 void *p;
06371
06372 if (type == error_mark_node || error_operand_p (expr))
06373 return error_mark_node;
06374
06375
06376
06377
06378
06379
06380
06381
06382 if (CLASS_TYPE_P (type))
06383 {
06384 expr = build_special_member_call (NULL_TREE, complete_ctor_identifier,
06385 build_tree_list (NULL_TREE, expr),
06386 type, LOOKUP_NORMAL);
06387 return build_cplus_new (type, expr);
06388 }
06389
06390
06391 p = conversion_obstack_alloc (0);
06392
06393 conv = implicit_conversion (type, TREE_TYPE (expr), expr,
06394 c_cast_p,
06395 LOOKUP_NORMAL);
06396 if (!conv || conv->bad_p)
06397 expr = NULL_TREE;
06398 else
06399 expr = convert_like_real (conv, expr, NULL_TREE, 0, 0,
06400 false,
06401 c_cast_p);
06402
06403
06404 obstack_free (&conversion_obstack, p);
06405
06406 return expr;
06407 }
06408
06409
06410
06411
06412
06413
06414 tree
06415 make_temporary_var_for_ref_to_temp (tree decl, tree type)
06416 {
06417 tree var;
06418
06419
06420 var = build_decl (VAR_DECL, NULL_TREE, type);
06421 DECL_ARTIFICIAL (var) = 1;
06422 DECL_IGNORED_P (var) = 1;
06423 TREE_USED (var) = 1;
06424
06425
06426 if (TREE_STATIC (decl))
06427 {
06428
06429 tree name;
06430
06431 TREE_STATIC (var) = 1;
06432 name = mangle_ref_init_variable (decl);
06433 DECL_NAME (var) = name;
06434 SET_DECL_ASSEMBLER_NAME (var, name);
06435 var = pushdecl_top_level (var);
06436 }
06437 else
06438 {
06439
06440 maybe_push_cleanup_level (type);
06441
06442 DECL_CONTEXT (var) = current_function_decl;
06443 }
06444
06445 return var;
06446 }
06447
06448
06449
06450
06451
06452
06453
06454
06455
06456
06457
06458
06459 tree
06460 initialize_reference (tree type, tree expr, tree decl, tree *cleanup)
06461 {
06462 conversion *conv;
06463 void *p;
06464
06465 if (type == error_mark_node || error_operand_p (expr))
06466 return error_mark_node;
06467
06468
06469 p = conversion_obstack_alloc (0);
06470
06471 conv = reference_binding (type, TREE_TYPE (expr), expr, LOOKUP_NORMAL);
06472 if (!conv || conv->bad_p)
06473 {
06474 if (!(TYPE_QUALS (TREE_TYPE (type)) & TYPE_QUAL_CONST)
06475 && !real_lvalue_p (expr))
06476 error ("invalid initialization of non-const reference of "
06477 "type %qT from a temporary of type %qT",
06478 type, TREE_TYPE (expr));
06479 else
06480 error ("invalid initialization of reference of type "
06481 "%qT from expression of type %qT", type,
06482 TREE_TYPE (expr));
06483 return error_mark_node;
06484 }
06485
06486
06487
06488
06489
06490
06491
06492
06493
06494
06495
06496
06497
06498
06499
06500
06501
06502
06503
06504
06505
06506
06507
06508
06509
06510
06511
06512
06513
06514
06515
06516
06517
06518
06519
06520 gcc_assert (conv->kind == ck_ref_bind);
06521 if (decl)
06522 {
06523 tree var;
06524 tree base_conv_type;
06525
06526
06527 conv = conv->u.next;
06528
06529
06530 if (conv->kind == ck_base)
06531 {
06532 if (conv->check_copy_constructor_p)
06533 check_constructor_callable (TREE_TYPE (expr), expr);
06534 base_conv_type = conv->type;
06535 conv = conv->u.next;
06536 }
06537 else
06538 base_conv_type = NULL_TREE;
06539
06540 expr = convert_like_real (conv, expr,
06541 NULL_TREE, 0,
06542 -1,
06543 true,
06544 false);
06545 if (error_operand_p (expr))
06546 expr = error_mark_node;
06547 else
06548 {
06549 if (!real_lvalue_p (expr))
06550 {
06551 tree init;
06552 tree type;
06553
06554
06555 type = TREE_TYPE (expr);
06556 var = make_temporary_var_for_ref_to_temp (decl, type);
06557 layout_decl (var, 0);
06558
06559
06560
06561
06562
06563
06564
06565
06566 if (TREE_CODE (expr) != TARGET_EXPR)
06567 expr = get_target_expr (expr);
06568
06569
06570 init = build2 (INIT_EXPR, type, var, expr);
06571 if (at_function_scope_p ())
06572 {
06573 add_decl_expr (var);
06574 *cleanup = cxx_maybe_build_cleanup (var);
06575
06576
06577
06578
06579
06580
06581
06582
06583
06584
06585
06586
06587
06588
06589
06590
06591
06592
06593
06594 }
06595 else
06596 {
06597 rest_of_decl_compilation (var, 1, at_eof);
06598 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
06599 static_aggregates = tree_cons (NULL_TREE, var,
06600 static_aggregates);
06601 }
06602
06603 expr = build_address (var);
06604 if (base_conv_type)
06605 expr = convert_to_base (expr,
06606 build_pointer_type (base_conv_type),
06607 true,
06608 true);
06609 expr = build2 (COMPOUND_EXPR, TREE_TYPE (expr), init, expr);
06610 }
06611 else
06612
06613 expr = build_unary_op (ADDR_EXPR, expr, 0);
06614
06615 if (base_conv_type)
06616 expr = (perform_implicit_conversion
06617 (build_pointer_type (base_conv_type), expr));
06618 expr = build_nop (type, expr);
06619 }
06620 }
06621 else
06622
06623 expr = convert_like (conv, expr);
06624
06625
06626 obstack_free (&conversion_obstack, p);
06627
06628 return expr;
06629 }
06630
06631 #include "gt-cp-call.h"