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
00028
00029 #include "config.h"
00030 #include "system.h"
00031 #include "tree.h"
00032 #include "flags.h"
00033 #include "cp-tree.h"
00034 #include "convert.h"
00035 #include "toplev.h"
00036 #include "decl.h"
00037
00038 static tree cp_convert_to_pointer PARAMS ((tree, tree, int));
00039 static tree convert_to_pointer_force PARAMS ((tree, tree));
00040 static tree build_up_reference PARAMS ((tree, tree, int, tree));
00041 static void warn_ref_binding PARAMS ((tree, tree, tree));
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074 static tree
00075 cp_convert_to_pointer (type, expr, force)
00076 tree type, expr;
00077 int force;
00078 {
00079 register tree intype = TREE_TYPE (expr);
00080 register enum tree_code form;
00081 tree rval;
00082
00083 if (IS_AGGR_TYPE (intype))
00084 {
00085 intype = complete_type (intype);
00086 if (!COMPLETE_TYPE_P (intype))
00087 {
00088 error ("can't convert from incomplete type `%T' to `%T'",
00089 intype, type);
00090 return error_mark_node;
00091 }
00092
00093 rval = build_type_conversion (type, expr);
00094 if (rval)
00095 {
00096 if (rval == error_mark_node)
00097 error ("conversion of `%E' from `%T' to `%T' is ambiguous",
00098 expr, intype, type);
00099 return rval;
00100 }
00101 }
00102
00103
00104 if (TREE_CODE (type) == POINTER_TYPE
00105 && (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
00106 || VOID_TYPE_P (TREE_TYPE (type))))
00107 {
00108
00109
00110 if (TYPE_PTRMEMFUNC_P (intype))
00111 {
00112 tree fntype = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (intype));
00113 tree decl = maybe_dummy_object (TYPE_METHOD_BASETYPE (fntype), 0);
00114 expr = build (OFFSET_REF, fntype, decl, expr);
00115 }
00116
00117 if (TREE_CODE (expr) == OFFSET_REF
00118 && TREE_CODE (TREE_TYPE (expr)) == METHOD_TYPE)
00119 expr = resolve_offset_ref (expr);
00120 if (TREE_CODE (TREE_TYPE (expr)) == METHOD_TYPE)
00121 expr = build_addr_func (expr);
00122 if (TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE)
00123 {
00124 if (TREE_CODE (TREE_TYPE (TREE_TYPE (expr))) == METHOD_TYPE)
00125 if (pedantic || warn_pmf2ptr)
00126 pedwarn ("converting from `%T' to `%T'", TREE_TYPE (expr),
00127 type);
00128 return build1 (NOP_EXPR, type, expr);
00129 }
00130 intype = TREE_TYPE (expr);
00131 }
00132
00133 if (expr == error_mark_node)
00134 return error_mark_node;
00135
00136 form = TREE_CODE (intype);
00137
00138 if (POINTER_TYPE_P (intype))
00139 {
00140 intype = TYPE_MAIN_VARIANT (intype);
00141
00142 if (TYPE_MAIN_VARIANT (type) != intype
00143 && TREE_CODE (type) == POINTER_TYPE
00144 && TREE_CODE (TREE_TYPE (type)) == RECORD_TYPE
00145 && IS_AGGR_TYPE (TREE_TYPE (type))
00146 && IS_AGGR_TYPE (TREE_TYPE (intype))
00147 && TREE_CODE (TREE_TYPE (intype)) == RECORD_TYPE)
00148 {
00149 enum tree_code code = PLUS_EXPR;
00150 tree binfo;
00151 tree intype_class;
00152 tree type_class;
00153 bool same_p;
00154
00155 intype_class = TREE_TYPE (intype);
00156 type_class = TREE_TYPE (type);
00157
00158 same_p = same_type_p (TYPE_MAIN_VARIANT (intype_class),
00159 TYPE_MAIN_VARIANT (type_class));
00160 binfo = NULL_TREE;
00161
00162 if (!same_p)
00163 binfo = lookup_base (intype_class, type_class, ba_check, NULL);
00164 if (!same_p && !binfo)
00165 {
00166
00167 binfo = lookup_base (type_class, intype_class, ba_check, NULL);
00168 code = MINUS_EXPR;
00169 }
00170 if (binfo == error_mark_node)
00171 return error_mark_node;
00172 if (binfo || same_p)
00173 {
00174 if (binfo)
00175 expr = build_base_path (code, expr, binfo, 0);
00176
00177 return build_nop (type, expr);
00178 }
00179 }
00180
00181 if (TYPE_PTRMEM_P (type) && TYPE_PTRMEM_P (intype))
00182 {
00183 tree b1;
00184 tree b2;
00185 tree binfo;
00186 enum tree_code code = PLUS_EXPR;
00187 base_kind bk;
00188
00189 b1 = TYPE_OFFSET_BASETYPE (TREE_TYPE (type));
00190 b2 = TYPE_OFFSET_BASETYPE (TREE_TYPE (intype));
00191 binfo = lookup_base (b1, b2, ba_check, &bk);
00192 if (!binfo)
00193 {
00194 binfo = lookup_base (b2, b1, ba_check, &bk);
00195 code = MINUS_EXPR;
00196 }
00197 if (binfo == error_mark_node)
00198 return error_mark_node;
00199
00200 if (bk == bk_via_virtual)
00201 {
00202 if (force)
00203 warning ("pointer to member cast from `%T' to `%T' is via virtual base",
00204 TREE_TYPE (intype), TREE_TYPE (type));
00205 else
00206 {
00207 error ("pointer to member cast from `%T' to `%T' is via virtual base",
00208 TREE_TYPE (intype), TREE_TYPE (type));
00209 return error_mark_node;
00210 }
00211
00212
00213 return build1 (NOP_EXPR, type, expr);
00214 }
00215
00216 if (TREE_CODE (expr) == PTRMEM_CST)
00217 expr = cplus_expand_constant (expr);
00218
00219 if (binfo)
00220 expr = size_binop (code, convert (sizetype, expr),
00221 BINFO_OFFSET (binfo));
00222 }
00223 else if (TYPE_PTRMEMFUNC_P (type))
00224 {
00225 error ("cannot convert `%E' from type `%T' to type `%T'",
00226 expr, intype, type);
00227 return error_mark_node;
00228 }
00229
00230 return build_nop (type, expr);
00231 }
00232 else if (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype))
00233 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), expr, 0);
00234 else if (TYPE_PTRMEMFUNC_P (intype))
00235 {
00236 error ("cannot convert `%E' from type `%T' to type `%T'",
00237 expr, intype, type);
00238 return error_mark_node;
00239 }
00240
00241 my_friendly_assert (form != OFFSET_TYPE, 186);
00242
00243 if (integer_zerop (expr))
00244 {
00245 if (TYPE_PTRMEMFUNC_P (type))
00246 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), expr, 0);
00247
00248 if (TYPE_PTRMEM_P (type))
00249
00250
00251 expr = build_int_2 (-1, -1);
00252 else
00253 expr = build_int_2 (0, 0);
00254 TREE_TYPE (expr) = type;
00255
00256 force_fit_type (expr, 0);
00257 return expr;
00258 }
00259 else if ((TYPE_PTRMEM_P (type) || TYPE_PTRMEMFUNC_P (type))
00260 && INTEGRAL_CODE_P (form))
00261 {
00262 error ("invalid conversion from '%T' to '%T'", intype, type);
00263 return error_mark_node;
00264 }
00265
00266 if (INTEGRAL_CODE_P (form))
00267 {
00268 if (TYPE_PRECISION (intype) == POINTER_SIZE)
00269 return build1 (CONVERT_EXPR, type, expr);
00270 expr = cp_convert (c_common_type_for_size (POINTER_SIZE, 0), expr);
00271
00272 if (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (expr)))
00273 != GET_MODE_SIZE (TYPE_MODE (type)))
00274
00275
00276 abort ();
00277 return convert_to_pointer (type, expr);
00278 }
00279
00280 if (type_unknown_p (expr))
00281 return instantiate_type (type, expr, tf_error | tf_warning);
00282
00283 error ("cannot convert `%E' from type `%T' to type `%T'",
00284 expr, intype, type);
00285 return error_mark_node;
00286 }
00287
00288
00289
00290
00291
00292 static tree
00293 convert_to_pointer_force (type, expr)
00294 tree type, expr;
00295 {
00296 register tree intype = TREE_TYPE (expr);
00297 register enum tree_code form = TREE_CODE (intype);
00298
00299 if (form == POINTER_TYPE)
00300 {
00301 intype = TYPE_MAIN_VARIANT (intype);
00302
00303 if (TYPE_MAIN_VARIANT (type) != intype
00304 && TREE_CODE (TREE_TYPE (type)) == RECORD_TYPE
00305 && IS_AGGR_TYPE (TREE_TYPE (type))
00306 && IS_AGGR_TYPE (TREE_TYPE (intype))
00307 && TREE_CODE (TREE_TYPE (intype)) == RECORD_TYPE)
00308 {
00309 enum tree_code code = PLUS_EXPR;
00310 tree binfo;
00311
00312 binfo = lookup_base (TREE_TYPE (intype), TREE_TYPE (type),
00313 ba_ignore, NULL);
00314 if (!binfo)
00315 {
00316 binfo = lookup_base (TREE_TYPE (type), TREE_TYPE (intype),
00317 ba_ignore, NULL);
00318 code = MINUS_EXPR;
00319 }
00320 if (binfo == error_mark_node)
00321 return error_mark_node;
00322 if (binfo)
00323 {
00324 expr = build_base_path (code, expr, binfo, 0);
00325 if (expr == error_mark_node)
00326 return error_mark_node;
00327
00328 if (!same_type_p (TREE_TYPE (TREE_TYPE (expr)),
00329 TREE_TYPE (type)))
00330 expr = build_nop (type, expr);
00331 return expr;
00332 }
00333 }
00334 }
00335
00336 return cp_convert_to_pointer (type, expr, 1);
00337 }
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347 static tree
00348 build_up_reference (type, arg, flags, decl)
00349 tree type, arg, decl;
00350 int flags;
00351 {
00352 tree rval;
00353 tree argtype = TREE_TYPE (arg);
00354 tree target_type = TREE_TYPE (type);
00355
00356 my_friendly_assert (TREE_CODE (type) == REFERENCE_TYPE, 187);
00357
00358 if ((flags & DIRECT_BIND) && ! real_lvalue_p (arg))
00359 {
00360
00361
00362 tree targ = arg;
00363
00364 arg = make_temporary_var_for_ref_to_temp (decl, TREE_TYPE (arg));
00365
00366
00367 DECL_INITIAL (arg) = targ;
00368 cp_finish_decl (arg, targ, NULL_TREE,
00369 LOOKUP_ONLYCONVERTING|DIRECT_BIND);
00370 }
00371 else if (!(flags & DIRECT_BIND) && ! lvalue_p (arg))
00372 return get_target_expr (arg);
00373
00374
00375
00376
00377 rval = build_unary_op (ADDR_EXPR, arg, 1);
00378 if (rval == error_mark_node)
00379 return error_mark_node;
00380
00381 if ((flags & LOOKUP_PROTECT)
00382 && TYPE_MAIN_VARIANT (argtype) != TYPE_MAIN_VARIANT (target_type)
00383 && IS_AGGR_TYPE (argtype)
00384 && IS_AGGR_TYPE (target_type))
00385 {
00386
00387 tree binfo = lookup_base (argtype, target_type, ba_check, NULL);
00388 if (binfo == error_mark_node)
00389 return error_mark_node;
00390 if (binfo == NULL_TREE)
00391 return error_not_base_type (target_type, argtype);
00392 rval = build_base_path (PLUS_EXPR, rval, binfo, 1);
00393 }
00394 else
00395 rval
00396 = convert_to_pointer_force (build_pointer_type (target_type), rval);
00397 return build_nop (type, rval);
00398 }
00399
00400
00401
00402
00403
00404
00405
00406
00407
00408 static void
00409 warn_ref_binding (reftype, intype, decl)
00410 tree reftype, intype, decl;
00411 {
00412 tree ttl = TREE_TYPE (reftype);
00413
00414 if (!CP_TYPE_CONST_NON_VOLATILE_P (ttl))
00415 {
00416 const char *msg;
00417
00418 if (CP_TYPE_VOLATILE_P (ttl) && decl)
00419 msg = "initialization of volatile reference type `%#T' from rvalue of type `%T'";
00420 else if (CP_TYPE_VOLATILE_P (ttl))
00421 msg = "conversion to volatile reference type `%#T' from rvalue of type `%T'";
00422 else if (decl)
00423 msg = "initialization of non-const reference type `%#T' from rvalue of type `%T'";
00424 else
00425 msg = "conversion to non-const reference type `%#T' from rvalue of type `%T'";
00426
00427 pedwarn (msg, reftype, intype);
00428 }
00429 }
00430
00431
00432
00433
00434
00435
00436
00437
00438 tree
00439 convert_to_reference (reftype, expr, convtype, flags, decl)
00440 tree reftype, expr;
00441 int convtype, flags;
00442 tree decl;
00443 {
00444 register tree type = TYPE_MAIN_VARIANT (TREE_TYPE (reftype));
00445 register tree intype;
00446 tree rval = NULL_TREE;
00447 tree rval_as_conversion = NULL_TREE;
00448 int i;
00449
00450 if (TREE_CODE (type) == FUNCTION_TYPE
00451 && TREE_TYPE (expr) == unknown_type_node)
00452 {
00453 expr = instantiate_type (type, expr,
00454 (flags & LOOKUP_COMPLAIN)
00455 ? tf_error | tf_warning : tf_none);
00456 if (expr == error_mark_node)
00457 return error_mark_node;
00458
00459 intype = TREE_TYPE (expr);
00460 }
00461 else
00462 {
00463 expr = convert_from_reference (expr);
00464 intype = TREE_TYPE (expr);
00465 }
00466
00467 my_friendly_assert (TREE_CODE (intype) != REFERENCE_TYPE, 364);
00468
00469 intype = TYPE_MAIN_VARIANT (intype);
00470
00471 i = comp_target_types (type, intype, 0);
00472
00473 if (i <= 0 && (convtype & CONV_IMPLICIT) && IS_AGGR_TYPE (intype)
00474 && ! (flags & LOOKUP_NO_CONVERSION))
00475 {
00476
00477
00478 rval_as_conversion
00479 = build_type_conversion (reftype, expr);
00480
00481 if (rval_as_conversion && rval_as_conversion != error_mark_node
00482 && real_lvalue_p (rval_as_conversion))
00483 {
00484 expr = rval_as_conversion;
00485 rval_as_conversion = NULL_TREE;
00486 intype = type;
00487 i = 1;
00488 }
00489 }
00490
00491 if (((convtype & CONV_STATIC) && i == -1)
00492 || ((convtype & CONV_IMPLICIT) && i == 1))
00493 {
00494 if (flags & LOOKUP_COMPLAIN)
00495 {
00496 tree ttl = TREE_TYPE (reftype);
00497 tree ttr = lvalue_type (expr);
00498
00499 if (! real_lvalue_p (expr))
00500 warn_ref_binding (reftype, intype, decl);
00501
00502 if (! (convtype & CONV_CONST)
00503 && !at_least_as_qualified_p (ttl, ttr))
00504 pedwarn ("conversion from `%T' to `%T' discards qualifiers",
00505 ttr, reftype);
00506 }
00507
00508 return build_up_reference (reftype, expr, flags, decl);
00509 }
00510 else if ((convtype & CONV_REINTERPRET) && lvalue_p (expr))
00511 {
00512
00513
00514
00515
00516
00517
00518
00519 if (TREE_CODE (intype) == POINTER_TYPE
00520 && (comptypes (TREE_TYPE (intype), type,
00521 COMPARE_BASE | COMPARE_RELAXED )))
00522 warning ("casting `%T' to `%T' does not dereference pointer",
00523 intype, reftype);
00524
00525 rval = build_unary_op (ADDR_EXPR, expr, 0);
00526 if (rval != error_mark_node)
00527 rval = convert_force (build_pointer_type (TREE_TYPE (reftype)),
00528 rval, 0);
00529 if (rval != error_mark_node)
00530 rval = build1 (NOP_EXPR, reftype, rval);
00531 }
00532 else
00533 {
00534 rval = convert_for_initialization (NULL_TREE, type, expr, flags,
00535 "converting", 0, 0);
00536 if (rval == NULL_TREE || rval == error_mark_node)
00537 return rval;
00538 warn_ref_binding (reftype, intype, decl);
00539 rval = build_up_reference (reftype, rval, flags, decl);
00540 }
00541
00542 if (rval)
00543 {
00544
00545 return rval;
00546 }
00547
00548 my_friendly_assert (TREE_CODE (intype) != OFFSET_TYPE, 189);
00549
00550 if (flags & LOOKUP_COMPLAIN)
00551 error ("cannot convert type `%T' to type `%T'", intype, reftype);
00552
00553 if (flags & LOOKUP_SPECULATIVELY)
00554 return NULL_TREE;
00555
00556 return error_mark_node;
00557 }
00558
00559
00560
00561
00562 tree
00563 convert_from_reference (val)
00564 tree val;
00565 {
00566 tree type = TREE_TYPE (val);
00567
00568 if (TREE_CODE (type) == OFFSET_TYPE)
00569 type = TREE_TYPE (type);
00570 if (TREE_CODE (type) == REFERENCE_TYPE)
00571 return build_indirect_ref (val, NULL);
00572 return val;
00573 }
00574
00575
00576
00577
00578 tree
00579 convert_lvalue (totype, expr)
00580 tree totype, expr;
00581 {
00582 totype = cp_build_qualified_type (totype, TYPE_QUALS (TREE_TYPE (expr)));
00583 totype = build_reference_type (totype);
00584 expr = convert_to_reference (totype, expr, CONV_IMPLICIT, LOOKUP_NORMAL,
00585 NULL_TREE);
00586 return convert_from_reference (expr);
00587 }
00588
00589
00590
00591
00592 tree
00593 force_rvalue (tree expr)
00594 {
00595 if (IS_AGGR_TYPE (TREE_TYPE (expr)) && TREE_CODE (expr) != TARGET_EXPR)
00596 expr = ocp_convert (TREE_TYPE (expr), expr,
00597 CONV_IMPLICIT|CONV_FORCE_TEMP, LOOKUP_NORMAL);
00598 else
00599 expr = decay_conversion (expr);
00600
00601 return expr;
00602 }
00603
00604
00605
00606 tree
00607 cp_convert (type, expr)
00608 tree type, expr;
00609 {
00610 return ocp_convert (type, expr, CONV_OLD_CONVERT, LOOKUP_NORMAL);
00611 }
00612
00613
00614
00615
00616
00617 tree
00618 ocp_convert (type, expr, convtype, flags)
00619 tree type, expr;
00620 int convtype, flags;
00621 {
00622 register tree e = expr;
00623 register enum tree_code code = TREE_CODE (type);
00624
00625 if (e == error_mark_node
00626 || TREE_TYPE (e) == error_mark_node)
00627 return error_mark_node;
00628
00629 complete_type (type);
00630 complete_type (TREE_TYPE (expr));
00631
00632 e = decl_constant_value (e);
00633
00634 if (IS_AGGR_TYPE (type) && (convtype & CONV_FORCE_TEMP)
00635
00636
00637
00638 && TYPE_HAS_CONSTRUCTOR (type))
00639 ;
00640 else if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (TREE_TYPE (e)))
00641 {
00642 if (same_type_p (type, TREE_TYPE (e)))
00643
00644
00645
00646
00647
00648
00649 return e;
00650
00651
00652 else if (TREE_CODE (type) == COMPLEX_TYPE)
00653 return fold (convert_to_complex (type, e));
00654 else
00655 return fold (build1 (NOP_EXPR, type, e));
00656 }
00657
00658 if (code == VOID_TYPE && (convtype & CONV_STATIC))
00659 {
00660 e = convert_to_void (e, NULL);
00661 return e;
00662 }
00663
00664
00665 if (code == OFFSET_TYPE)
00666 {
00667 type = TREE_TYPE (type);
00668 code = TREE_CODE (type);
00669 }
00670
00671 if (TREE_CODE (e) == OFFSET_REF)
00672 e = resolve_offset_ref (e);
00673
00674 if (INTEGRAL_CODE_P (code))
00675 {
00676 tree intype = TREE_TYPE (e);
00677
00678
00679 if (TREE_CODE (type) == ENUMERAL_TYPE
00680 && ((ARITHMETIC_TYPE_P (intype) && ! (convtype & CONV_STATIC))
00681 || (TREE_CODE (intype) == POINTER_TYPE)))
00682 {
00683 pedwarn ("conversion from `%#T' to `%#T'", intype, type);
00684
00685 if (flag_pedantic_errors)
00686 return error_mark_node;
00687 }
00688 if (IS_AGGR_TYPE (intype))
00689 {
00690 tree rval;
00691 rval = build_type_conversion (type, e);
00692 if (rval)
00693 return rval;
00694 if (flags & LOOKUP_COMPLAIN)
00695 error ("`%#T' used where a `%T' was expected", intype, type);
00696 if (flags & LOOKUP_SPECULATIVELY)
00697 return NULL_TREE;
00698 return error_mark_node;
00699 }
00700 if (code == BOOLEAN_TYPE)
00701 {
00702 tree fn = NULL_TREE;
00703
00704
00705
00706 if (TREE_CODE (expr) == FUNCTION_DECL)
00707 fn = expr;
00708 else if (TREE_CODE (expr) == ADDR_EXPR
00709 && TREE_CODE (TREE_OPERAND (expr, 0)) == FUNCTION_DECL)
00710 fn = TREE_OPERAND (expr, 0);
00711 if (fn && !DECL_WEAK (fn))
00712 warning ("the address of `%D', will always be `true'", fn);
00713 return cp_truthvalue_conversion (e);
00714 }
00715 return fold (convert_to_integer (type, e));
00716 }
00717 if (code == POINTER_TYPE || code == REFERENCE_TYPE
00718 || TYPE_PTRMEMFUNC_P (type))
00719 return fold (cp_convert_to_pointer (type, e, 0));
00720 if (code == VECTOR_TYPE)
00721 return fold (convert_to_vector (type, e));
00722 if (code == REAL_TYPE || code == COMPLEX_TYPE)
00723 {
00724 if (IS_AGGR_TYPE (TREE_TYPE (e)))
00725 {
00726 tree rval;
00727 rval = build_type_conversion (type, e);
00728 if (rval)
00729 return rval;
00730 else
00731 if (flags & LOOKUP_COMPLAIN)
00732 error ("`%#T' used where a floating point value was expected",
00733 TREE_TYPE (e));
00734 }
00735 if (code == REAL_TYPE)
00736 return fold (convert_to_real (type, e));
00737 else if (code == COMPLEX_TYPE)
00738 return fold (convert_to_complex (type, e));
00739 }
00740
00741
00742
00743
00744 if (IS_AGGR_TYPE_CODE (code))
00745 {
00746 tree dtype = TREE_TYPE (e);
00747 tree ctor = NULL_TREE;
00748
00749 dtype = TYPE_MAIN_VARIANT (dtype);
00750
00751
00752
00753
00754
00755
00756
00757
00758 ctor = e;
00759
00760 if (abstract_virtuals_error (NULL_TREE, type))
00761 return error_mark_node;
00762
00763 if ((flags & LOOKUP_ONLYCONVERTING)
00764 && ! (IS_AGGR_TYPE (dtype) && DERIVED_FROM_P (type, dtype)))
00765
00766
00767
00768 ctor = build_user_type_conversion (type, ctor, flags);
00769 else
00770 ctor = build_special_member_call (NULL_TREE,
00771 complete_ctor_identifier,
00772 build_tree_list (NULL_TREE, ctor),
00773 TYPE_BINFO (type), flags);
00774 if (ctor)
00775 return build_cplus_new (type, ctor);
00776 }
00777
00778 if (flags & LOOKUP_COMPLAIN)
00779 error ("conversion from `%T' to non-scalar type `%T' requested",
00780 TREE_TYPE (expr), type);
00781 if (flags & LOOKUP_SPECULATIVELY)
00782 return NULL_TREE;
00783 return error_mark_node;
00784 }
00785
00786
00787
00788
00789
00790
00791
00792
00793
00794
00795
00796
00797
00798
00799
00800
00801
00802
00803 tree
00804 convert_to_void (expr, implicit)
00805 tree expr;
00806 const char *implicit;
00807 {
00808 if (expr == error_mark_node
00809 || TREE_TYPE (expr) == error_mark_node)
00810 return error_mark_node;
00811 if (!TREE_TYPE (expr))
00812 return expr;
00813 if (VOID_TYPE_P (TREE_TYPE (expr)))
00814 return expr;
00815 switch (TREE_CODE (expr))
00816 {
00817 case COND_EXPR:
00818 {
00819
00820 tree op1 = TREE_OPERAND (expr,1);
00821 tree op2 = TREE_OPERAND (expr,2);
00822 tree new_op1 = convert_to_void (op1, implicit);
00823 tree new_op2 = convert_to_void (op2, implicit);
00824
00825 expr = build (COND_EXPR, TREE_TYPE (new_op1),
00826 TREE_OPERAND (expr, 0), new_op1, new_op2);
00827 break;
00828 }
00829
00830 case COMPOUND_EXPR:
00831 {
00832
00833 tree op1 = TREE_OPERAND (expr,1);
00834 tree new_op1 = convert_to_void (op1, implicit);
00835
00836 if (new_op1 != op1)
00837 {
00838 tree t = build (COMPOUND_EXPR, TREE_TYPE (new_op1),
00839 TREE_OPERAND (expr, 0), new_op1);
00840 TREE_SIDE_EFFECTS (t) = TREE_SIDE_EFFECTS (expr);
00841 TREE_NO_UNUSED_WARNING (t) = TREE_NO_UNUSED_WARNING (expr);
00842 expr = t;
00843 }
00844
00845 break;
00846 }
00847
00848 case NON_LVALUE_EXPR:
00849 case NOP_EXPR:
00850
00851 break;
00852
00853 case CALL_EXPR:
00854 break;
00855
00856 case INDIRECT_REF:
00857 {
00858 tree type = TREE_TYPE (expr);
00859 int is_reference = TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0)))
00860 == REFERENCE_TYPE;
00861 int is_volatile = TYPE_VOLATILE (type);
00862 int is_complete = COMPLETE_TYPE_P (complete_type (type));
00863
00864 if (is_volatile && !is_complete)
00865 warning ("object of incomplete type `%T' will not be accessed in %s",
00866 type, implicit ? implicit : "void context");
00867 else if (is_reference && is_volatile)
00868 warning ("object of type `%T' will not be accessed in %s",
00869 TREE_TYPE (TREE_OPERAND (expr, 0)),
00870 implicit ? implicit : "void context");
00871 if (is_reference || !is_volatile || !is_complete)
00872 expr = TREE_OPERAND (expr, 0);
00873
00874 break;
00875 }
00876
00877 case VAR_DECL:
00878 {
00879
00880 tree type = TREE_TYPE (expr);
00881 int is_complete = COMPLETE_TYPE_P (complete_type (type));
00882
00883 if (TYPE_VOLATILE (type) && !is_complete)
00884 warning ("object `%E' of incomplete type `%T' will not be accessed in %s",
00885 expr, type, implicit ? implicit : "void context");
00886 break;
00887 }
00888
00889 case OFFSET_REF:
00890 expr = resolve_offset_ref (expr);
00891 break;
00892
00893 default:;
00894 }
00895 {
00896 tree probe = expr;
00897
00898 if (TREE_CODE (probe) == ADDR_EXPR)
00899 probe = TREE_OPERAND (expr, 0);
00900 if (type_unknown_p (probe))
00901 {
00902
00903
00904 pedwarn ("%s cannot resolve address of overloaded function",
00905 implicit ? implicit : "void cast");
00906 }
00907 else if (implicit && probe == expr && is_overloaded_fn (probe))
00908
00909 warning ("%s is a reference, not call, to function `%E'",
00910 implicit, expr);
00911 }
00912
00913 if (expr != error_mark_node && !VOID_TYPE_P (TREE_TYPE (expr)))
00914 {
00915
00916
00917
00918
00919
00920 if (!implicit)
00921 expr = build1 (CONVERT_EXPR, void_type_node, expr);
00922 }
00923 return expr;
00924 }
00925
00926
00927
00928
00929
00930
00931
00932
00933
00934
00935
00936
00937
00938
00939
00940
00941
00942
00943 tree
00944 convert (type, expr)
00945 tree type, expr;
00946 {
00947 tree intype;
00948
00949 if (type == error_mark_node || expr == error_mark_node)
00950 return error_mark_node;
00951
00952 intype = TREE_TYPE (expr);
00953
00954 if (POINTER_TYPE_P (type) && POINTER_TYPE_P (intype))
00955 {
00956 expr = decl_constant_value (expr);
00957 return fold (build1 (NOP_EXPR, type, expr));
00958 }
00959
00960 return ocp_convert (type, expr, CONV_OLD_CONVERT,
00961 LOOKUP_NORMAL|LOOKUP_NO_CONVERSION);
00962 }
00963
00964
00965
00966
00967
00968 tree
00969 convert_force (type, expr, convtype)
00970 tree type;
00971 tree expr;
00972 int convtype;
00973 {
00974 register tree e = expr;
00975 register enum tree_code code = TREE_CODE (type);
00976
00977 if (code == REFERENCE_TYPE)
00978 return fold (convert_to_reference (type, e, CONV_C_CAST, LOOKUP_COMPLAIN,
00979 NULL_TREE));
00980 else if (TREE_CODE (TREE_TYPE (e)) == REFERENCE_TYPE)
00981 e = convert_from_reference (e);
00982
00983 if (code == POINTER_TYPE)
00984 return fold (convert_to_pointer_force (type, e));
00985
00986
00987 if (((TREE_CODE (TREE_TYPE (e)) == POINTER_TYPE && TREE_CODE (e) == ADDR_EXPR
00988 && TREE_CODE (TREE_TYPE (e)) == POINTER_TYPE
00989 && TREE_CODE (TREE_TYPE (TREE_TYPE (e))) == METHOD_TYPE)
00990 || integer_zerop (e)
00991 || TYPE_PTRMEMFUNC_P (TREE_TYPE (e)))
00992 && TYPE_PTRMEMFUNC_P (type))
00993 {
00994
00995 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), e, 1);
00996 }
00997
00998 return ocp_convert (type, e, CONV_C_CAST|convtype, LOOKUP_NORMAL);
00999 }
01000
01001
01002
01003
01004
01005
01006
01007
01008
01009
01010
01011
01012 tree
01013 build_type_conversion (xtype, expr)
01014 tree xtype, expr;
01015 {
01016
01017
01018 return build_user_type_conversion (xtype, expr, LOOKUP_NORMAL);
01019 }
01020
01021
01022
01023
01024
01025
01026 tree
01027 build_expr_type_conversion (desires, expr, complain)
01028 int desires;
01029 tree expr;
01030 int complain;
01031 {
01032 tree basetype = TREE_TYPE (expr);
01033 tree conv = NULL_TREE;
01034 tree winner = NULL_TREE;
01035
01036 if (expr == null_node
01037 && (desires & WANT_INT)
01038 && !(desires & WANT_NULL))
01039 warning ("converting NULL to non-pointer type");
01040
01041 if (TREE_CODE (expr) == OFFSET_REF)
01042 expr = resolve_offset_ref (expr);
01043 expr = convert_from_reference (expr);
01044 basetype = TREE_TYPE (expr);
01045
01046 if (basetype == error_mark_node)
01047 return error_mark_node;
01048
01049 if (! IS_AGGR_TYPE (basetype))
01050 switch (TREE_CODE (basetype))
01051 {
01052 case INTEGER_TYPE:
01053 if ((desires & WANT_NULL) && null_ptr_cst_p (expr))
01054 return expr;
01055
01056
01057 case BOOLEAN_TYPE:
01058 return (desires & WANT_INT) ? expr : NULL_TREE;
01059 case ENUMERAL_TYPE:
01060 return (desires & WANT_ENUM) ? expr : NULL_TREE;
01061 case REAL_TYPE:
01062 return (desires & WANT_FLOAT) ? expr : NULL_TREE;
01063 case POINTER_TYPE:
01064 return (desires & WANT_POINTER) ? expr : NULL_TREE;
01065
01066 case FUNCTION_TYPE:
01067 case ARRAY_TYPE:
01068 return (desires & WANT_POINTER) ? default_conversion (expr)
01069 : NULL_TREE;
01070 default:
01071 return NULL_TREE;
01072 }
01073
01074
01075
01076
01077 if (! TYPE_HAS_CONVERSION (basetype))
01078 return NULL_TREE;
01079
01080 for (conv = lookup_conversions (basetype); conv; conv = TREE_CHAIN (conv))
01081 {
01082 int win = 0;
01083 tree candidate;
01084 tree cand = TREE_VALUE (conv);
01085
01086 if (winner && winner == cand)
01087 continue;
01088
01089 candidate = TREE_TYPE (TREE_TYPE (cand));
01090 if (TREE_CODE (candidate) == REFERENCE_TYPE)
01091 candidate = TREE_TYPE (candidate);
01092
01093 switch (TREE_CODE (candidate))
01094 {
01095 case BOOLEAN_TYPE:
01096 case INTEGER_TYPE:
01097 win = (desires & WANT_INT); break;
01098 case ENUMERAL_TYPE:
01099 win = (desires & WANT_ENUM); break;
01100 case REAL_TYPE:
01101 win = (desires & WANT_FLOAT); break;
01102 case POINTER_TYPE:
01103 win = (desires & WANT_POINTER); break;
01104
01105 default:
01106 break;
01107 }
01108
01109 if (win)
01110 {
01111 if (winner)
01112 {
01113 if (complain)
01114 {
01115 error ("ambiguous default type conversion from `%T'",
01116 basetype);
01117 error (" candidate conversions include `%D' and `%D'",
01118 winner, cand);
01119 }
01120 return error_mark_node;
01121 }
01122 else
01123 winner = cand;
01124 }
01125 }
01126
01127 if (winner)
01128 {
01129 tree type = TREE_TYPE (TREE_TYPE (winner));
01130 if (TREE_CODE (type) == REFERENCE_TYPE)
01131 type = TREE_TYPE (type);
01132 return build_user_type_conversion (type, expr, LOOKUP_NORMAL);
01133 }
01134
01135 return NULL_TREE;
01136 }
01137
01138
01139
01140 tree
01141 type_promotes_to (type)
01142 tree type;
01143 {
01144 int type_quals;
01145
01146 if (type == error_mark_node)
01147 return error_mark_node;
01148
01149 type_quals = cp_type_quals (type);
01150 type = TYPE_MAIN_VARIANT (type);
01151
01152
01153
01154 if (type == boolean_type_node)
01155 type = integer_type_node;
01156
01157
01158
01159 else if (TREE_CODE (type) == ENUMERAL_TYPE
01160 || type == wchar_type_node)
01161 {
01162 int precision = MAX (TYPE_PRECISION (type),
01163 TYPE_PRECISION (integer_type_node));
01164 tree totype = c_common_type_for_size (precision, 0);
01165 if (TREE_UNSIGNED (type)
01166 && ! int_fits_type_p (TYPE_MAX_VALUE (type), totype))
01167 type = c_common_type_for_size (precision, 1);
01168 else
01169 type = totype;
01170 }
01171 else if (c_promoting_integer_type_p (type))
01172 {
01173
01174 if (TREE_UNSIGNED (type)
01175 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
01176 type = unsigned_type_node;
01177 else
01178 type = integer_type_node;
01179 }
01180 else if (type == float_type_node)
01181 type = double_type_node;
01182
01183 return cp_build_qualified_type (type, type_quals);
01184 }
01185
01186
01187
01188
01189
01190
01191
01192
01193
01194
01195 tree
01196 perform_qualification_conversions (type, expr)
01197 tree type;
01198 tree expr;
01199 {
01200 if (TREE_CODE (type) == POINTER_TYPE
01201 && TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE
01202 && comp_ptr_ttypes (TREE_TYPE (type), TREE_TYPE (TREE_TYPE (expr))))
01203 return build1 (NOP_EXPR, type, expr);
01204 else
01205 return error_mark_node;
01206 }