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