comparison gcc/cp/init.c @ 131:84e7813d76e9

gcc-8.2
author mir3636
date Thu, 25 Oct 2018 07:37:49 +0900
parents 04ced10e8804
children 1830386684a0
comparison
equal deleted inserted replaced
111:04ced10e8804 131:84e7813d76e9
1 /* Handle initialization things in C++. 1 /* Handle initialization things in C++.
2 Copyright (C) 1987-2017 Free Software Foundation, Inc. 2 Copyright (C) 1987-2018 Free Software Foundation, Inc.
3 Contributed by Michael Tiemann (tiemann@cygnus.com) 3 Contributed by Michael Tiemann (tiemann@cygnus.com)
4 4
5 This file is part of GCC. 5 This file is part of GCC.
6 6
7 GCC is free software; you can redistribute it and/or modify 7 GCC is free software; you can redistribute it and/or modify
180 items with static storage duration that are not otherwise 180 items with static storage duration that are not otherwise
181 initialized are initialized to zero. */ 181 initialized are initialized to zero. */
182 ; 182 ;
183 else if (TYPE_PTR_OR_PTRMEM_P (type)) 183 else if (TYPE_PTR_OR_PTRMEM_P (type))
184 init = fold (convert (type, nullptr_node)); 184 init = fold (convert (type, nullptr_node));
185 else if (NULLPTR_TYPE_P (type))
186 init = build_int_cst (type, 0);
185 else if (SCALAR_TYPE_P (type)) 187 else if (SCALAR_TYPE_P (type))
186 init = fold (convert (type, integer_zero_node)); 188 init = fold (convert (type, integer_zero_node));
187 else if (RECORD_OR_UNION_CODE_P (TREE_CODE (type))) 189 else if (RECORD_OR_UNION_CODE_P (TREE_CODE (type)))
188 { 190 {
189 tree field; 191 tree field;
282 init = build_constructor (type, v); 284 init = build_constructor (type, v);
283 } 285 }
284 else if (VECTOR_TYPE_P (type)) 286 else if (VECTOR_TYPE_P (type))
285 init = build_zero_cst (type); 287 init = build_zero_cst (type);
286 else 288 else
287 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE); 289 {
290 gcc_assert (TYPE_REF_P (type));
291 init = build_zero_cst (type);
292 }
288 293
289 /* In all cases, the initializer is a constant. */ 294 /* In all cases, the initializer is a constant. */
290 if (init) 295 if (init)
291 TREE_CONSTANT (init) = 1; 296 TREE_CONSTANT (init) = 1;
292 297
495 { 500 {
496 if (complain & tf_error) 501 if (complain & tf_error)
497 error ("value-initialization of function type %qT", type); 502 error ("value-initialization of function type %qT", type);
498 return error_mark_node; 503 return error_mark_node;
499 } 504 }
500 else if (TREE_CODE (type) == REFERENCE_TYPE) 505 else if (TYPE_REF_P (type))
501 { 506 {
502 if (complain & tf_error) 507 if (complain & tf_error)
503 error ("value-initialization of reference type %qT", type); 508 error ("value-initialization of reference type %qT", type);
504 return error_mark_node; 509 return error_mark_node;
505 } 510 }
533 } 538 }
534 } 539 }
535 540
536 /* Return the non-static data initializer for FIELD_DECL MEMBER. */ 541 /* Return the non-static data initializer for FIELD_DECL MEMBER. */
537 542
538 static GTY(()) hash_map<tree, tree> *nsdmi_inst; 543 static GTY((cache)) tree_cache_map *nsdmi_inst;
539 544
540 tree 545 tree
541 get_nsdmi (tree member, bool in_ctor, tsubst_flags_t complain) 546 get_nsdmi (tree member, bool in_ctor, tsubst_flags_t complain)
542 { 547 {
543 tree init; 548 tree init;
546 551
547 if (DECL_LANG_SPECIFIC (member) && DECL_TEMPLATE_INFO (member)) 552 if (DECL_LANG_SPECIFIC (member) && DECL_TEMPLATE_INFO (member))
548 { 553 {
549 init = DECL_INITIAL (DECL_TI_TEMPLATE (member)); 554 init = DECL_INITIAL (DECL_TI_TEMPLATE (member));
550 location_t expr_loc 555 location_t expr_loc
551 = EXPR_LOC_OR_LOC (init, DECL_SOURCE_LOCATION (member)); 556 = cp_expr_loc_or_loc (init, DECL_SOURCE_LOCATION (member));
552 tree *slot; 557 tree *slot;
553 if (TREE_CODE (init) == DEFAULT_ARG) 558 if (TREE_CODE (init) == DEFAULT_ARG)
554 /* Unparsed. */; 559 /* Unparsed. */;
555 else if (nsdmi_inst && (slot = nsdmi_inst->get (member))) 560 else if (nsdmi_inst && (slot = nsdmi_inst->get (member)))
556 init = *slot; 561 init = *slot;
570 location_t sloc = input_location; 575 location_t sloc = input_location;
571 input_location = expr_loc; 576 input_location = expr_loc;
572 577
573 DECL_INSTANTIATING_NSDMI_P (member) = 1; 578 DECL_INSTANTIATING_NSDMI_P (member) = 1;
574 579
580 bool pushed = false;
581 if (!currently_open_class (DECL_CONTEXT (member)))
582 {
583 push_to_top_level ();
584 push_nested_class (DECL_CONTEXT (member));
585 pushed = true;
586 }
587
588 gcc_checking_assert (!processing_template_decl);
589
575 inject_this_parameter (DECL_CONTEXT (member), TYPE_UNQUALIFIED); 590 inject_this_parameter (DECL_CONTEXT (member), TYPE_UNQUALIFIED);
576 591
577 start_lambda_scope (member); 592 start_lambda_scope (member);
578 593
579 /* Do deferred instantiation of the NSDMI. */ 594 /* Do deferred instantiation of the NSDMI. */
588 DECL_INSTANTIATING_NSDMI_P (member) = 0; 603 DECL_INSTANTIATING_NSDMI_P (member) = 0;
589 604
590 if (init != error_mark_node) 605 if (init != error_mark_node)
591 { 606 {
592 if (!nsdmi_inst) 607 if (!nsdmi_inst)
593 nsdmi_inst = hash_map<tree,tree>::create_ggc (37); 608 nsdmi_inst = tree_cache_map::create_ggc (37);
594 nsdmi_inst->put (member, init); 609 nsdmi_inst->put (member, init);
610 }
611
612 if (pushed)
613 {
614 pop_nested_class ();
615 pop_from_top_level ();
595 } 616 }
596 617
597 input_location = sloc; 618 input_location = sloc;
598 cp_unevaluated_operand = un; 619 cp_unevaluated_operand = un;
599 } 620 }
629 /* Strip redundant TARGET_EXPR so we don't need to remap it, and 650 /* Strip redundant TARGET_EXPR so we don't need to remap it, and
630 so the aggregate init code below will see a CONSTRUCTOR. */ 651 so the aggregate init code below will see a CONSTRUCTOR. */
631 bool simple_target = (init && SIMPLE_TARGET_EXPR_P (init)); 652 bool simple_target = (init && SIMPLE_TARGET_EXPR_P (init));
632 if (simple_target) 653 if (simple_target)
633 init = TARGET_EXPR_INITIAL (init); 654 init = TARGET_EXPR_INITIAL (init);
634 init = break_out_target_exprs (init); 655 init = break_out_target_exprs (init, /*loc*/true);
635 if (simple_target && TREE_CODE (init) != CONSTRUCTOR) 656 if (simple_target && TREE_CODE (init) != CONSTRUCTOR)
636 /* Now put it back so C++17 copy elision works. */ 657 /* Now put it back so C++17 copy elision works. */
637 init = get_target_expr (init); 658 init = get_target_expr (init);
638 659
639 current_class_ptr = save_ccp; 660 current_class_ptr = save_ccp;
667 688
668 error_at (loc, "initializer for flexible array member %q#D", member); 689 error_at (loc, "initializer for flexible array member %q#D", member);
669 return true; 690 return true;
670 } 691 }
671 692
693 /* If INIT's value can come from a call to std::initializer_list<T>::begin,
694 return that function. Otherwise, NULL_TREE. */
695
696 static tree
697 find_list_begin (tree init)
698 {
699 STRIP_NOPS (init);
700 while (TREE_CODE (init) == COMPOUND_EXPR)
701 init = TREE_OPERAND (init, 1);
702 STRIP_NOPS (init);
703 if (TREE_CODE (init) == COND_EXPR)
704 {
705 tree left = TREE_OPERAND (init, 1);
706 if (!left)
707 left = TREE_OPERAND (init, 0);
708 left = find_list_begin (left);
709 if (left)
710 return left;
711 return find_list_begin (TREE_OPERAND (init, 2));
712 }
713 if (TREE_CODE (init) == CALL_EXPR)
714 if (tree fn = get_callee_fndecl (init))
715 if (id_equal (DECL_NAME (fn), "begin")
716 && is_std_init_list (DECL_CONTEXT (fn)))
717 return fn;
718 return NULL_TREE;
719 }
720
721 /* If INIT initializing MEMBER is copying the address of the underlying array
722 of an initializer_list, warn. */
723
724 static void
725 maybe_warn_list_ctor (tree member, tree init)
726 {
727 tree memtype = TREE_TYPE (member);
728 if (!init || !TYPE_PTR_P (memtype)
729 || !is_list_ctor (current_function_decl))
730 return;
731
732 tree parms = FUNCTION_FIRST_USER_PARMTYPE (current_function_decl);
733 tree initlist = non_reference (TREE_VALUE (parms));
734 tree targs = CLASSTYPE_TI_ARGS (initlist);
735 tree elttype = TREE_VEC_ELT (targs, 0);
736
737 if (!same_type_ignoring_top_level_qualifiers_p
738 (TREE_TYPE (memtype), elttype))
739 return;
740
741 tree begin = find_list_begin (init);
742 if (!begin)
743 return;
744
745 location_t loc = cp_expr_loc_or_loc (init, input_location);
746 warning_at (loc, OPT_Winit_list_lifetime,
747 "initializing %qD from %qE does not extend the lifetime "
748 "of the underlying array", member, begin);
749 }
750
672 /* Initialize MEMBER, a FIELD_DECL, with INIT, a TREE_LIST of 751 /* Initialize MEMBER, a FIELD_DECL, with INIT, a TREE_LIST of
673 arguments. If TREE_LIST is void_type_node, an empty initializer 752 arguments. If TREE_LIST is void_type_node, an empty initializer
674 list was given; if NULL_TREE no initializer was given. */ 753 list was given; if NULL_TREE no initializer was given. */
675 754
676 static void 755 static void
744 init = build2 (INIT_EXPR, type, decl, TREE_VALUE (init)); 823 init = build2 (INIT_EXPR, type, decl, TREE_VALUE (init));
745 finish_expr_stmt (init); 824 finish_expr_stmt (init);
746 } 825 }
747 } 826 }
748 else if (init 827 else if (init
749 && (TREE_CODE (type) == REFERENCE_TYPE 828 && (TYPE_REF_P (type)
750 /* Pre-digested NSDMI. */ 829 /* Pre-digested NSDMI. */
751 || (((TREE_CODE (init) == CONSTRUCTOR 830 || (((TREE_CODE (init) == CONSTRUCTOR
752 && TREE_TYPE (init) == type) 831 && TREE_TYPE (init) == type)
753 /* { } mem-initializer. */ 832 /* { } mem-initializer. */
754 || (TREE_CODE (init) == TREE_LIST 833 || (TREE_CODE (init) == TREE_LIST
834 && init == NULL_TREE 913 && init == NULL_TREE
835 && default_init_uninitialized_part (type)) 914 && default_init_uninitialized_part (type))
836 { 915 {
837 /* TYPE_NEEDS_CONSTRUCTING can be set just because we have a 916 /* TYPE_NEEDS_CONSTRUCTING can be set just because we have a
838 vtable; still give this diagnostic. */ 917 vtable; still give this diagnostic. */
918 auto_diagnostic_group d;
839 if (permerror (DECL_SOURCE_LOCATION (current_function_decl), 919 if (permerror (DECL_SOURCE_LOCATION (current_function_decl),
840 "uninitialized const member in %q#T", type)) 920 "uninitialized const member in %q#T", type))
841 inform (DECL_SOURCE_LOCATION (member), 921 inform (DECL_SOURCE_LOCATION (member),
842 "%q#D should be initialized", member ); 922 "%q#D should be initialized", member );
843 } 923 }
849 { 929 {
850 if (init == NULL_TREE) 930 if (init == NULL_TREE)
851 { 931 {
852 tree core_type; 932 tree core_type;
853 /* member traversal: note it leaves init NULL */ 933 /* member traversal: note it leaves init NULL */
854 if (TREE_CODE (type) == REFERENCE_TYPE) 934 if (TYPE_REF_P (type))
855 { 935 {
936 auto_diagnostic_group d;
856 if (permerror (DECL_SOURCE_LOCATION (current_function_decl), 937 if (permerror (DECL_SOURCE_LOCATION (current_function_decl),
857 "uninitialized reference member in %q#T", type)) 938 "uninitialized reference member in %q#T", type))
858 inform (DECL_SOURCE_LOCATION (member), 939 inform (DECL_SOURCE_LOCATION (member),
859 "%q#D should be initialized", member); 940 "%q#D should be initialized", member);
860 } 941 }
861 else if (CP_TYPE_CONST_P (type)) 942 else if (CP_TYPE_CONST_P (type))
862 { 943 {
944 auto_diagnostic_group d;
863 if (permerror (DECL_SOURCE_LOCATION (current_function_decl), 945 if (permerror (DECL_SOURCE_LOCATION (current_function_decl),
864 "uninitialized const member in %q#T", type)) 946 "uninitialized const member in %q#T", type))
865 inform (DECL_SOURCE_LOCATION (member), 947 inform (DECL_SOURCE_LOCATION (member),
866 "%q#D should be initialized", member ); 948 "%q#D should be initialized", member );
867 } 949 }
878 else if (TREE_CODE (init) == TREE_LIST) 960 else if (TREE_CODE (init) == TREE_LIST)
879 /* There was an explicit member initialization. Do some work 961 /* There was an explicit member initialization. Do some work
880 in that case. */ 962 in that case. */
881 init = build_x_compound_expr_from_list (init, ELK_MEM_INIT, 963 init = build_x_compound_expr_from_list (init, ELK_MEM_INIT,
882 tf_warning_or_error); 964 tf_warning_or_error);
965
966 maybe_warn_list_ctor (member, init);
883 967
884 /* Reject a member initializer for a flexible array member. */ 968 /* Reject a member initializer for a flexible array member. */
885 if (init && !maybe_reject_flexarray_init (member, init)) 969 if (init && !maybe_reject_flexarray_init (member, init))
886 finish_expr_stmt (cp_build_modify_expr (input_location, decl, 970 finish_expr_stmt (cp_build_modify_expr (input_location, decl,
887 INIT_EXPR, init, 971 INIT_EXPR, init,
1258 tree base_addr; 1342 tree base_addr;
1259 1343
1260 base_addr = build_base_path (PLUS_EXPR, current_class_ptr, 1344 base_addr = build_base_path (PLUS_EXPR, current_class_ptr,
1261 subobject, 1, tf_warning_or_error); 1345 subobject, 1, tf_warning_or_error);
1262 expand_aggr_init_1 (subobject, NULL_TREE, 1346 expand_aggr_init_1 (subobject, NULL_TREE,
1263 cp_build_indirect_ref (base_addr, RO_NULL, 1347 cp_build_fold_indirect_ref (base_addr),
1264 tf_warning_or_error),
1265 arguments, 1348 arguments,
1266 flags, 1349 flags,
1267 tf_warning_or_error); 1350 tf_warning_or_error);
1268 expand_cleanup_for_base (subobject, NULL_TREE); 1351 expand_cleanup_for_base (subobject, NULL_TREE);
1269 } 1352 }
1349 tree vtt_parm; 1432 tree vtt_parm;
1350 1433
1351 /* Compute the value to use, when there's a VTT. */ 1434 /* Compute the value to use, when there's a VTT. */
1352 vtt_parm = current_vtt_parm; 1435 vtt_parm = current_vtt_parm;
1353 vtbl2 = fold_build_pointer_plus (vtt_parm, vtt_index); 1436 vtbl2 = fold_build_pointer_plus (vtt_parm, vtt_index);
1354 vtbl2 = cp_build_indirect_ref (vtbl2, RO_NULL, tf_warning_or_error); 1437 vtbl2 = cp_build_fold_indirect_ref (vtbl2);
1355 vtbl2 = convert (TREE_TYPE (vtbl), vtbl2); 1438 vtbl2 = convert (TREE_TYPE (vtbl), vtbl2);
1356 1439
1357 /* The actual initializer is the VTT value only in the subobject 1440 /* The actual initializer is the VTT value only in the subobject
1358 constructor. In maybe_clone_body we'll substitute NULL for 1441 constructor. In maybe_clone_body we'll substitute NULL for
1359 the vtt_parm in the case of the non-subobject constructor. */ 1442 the vtt_parm in the case of the non-subobject constructor. */
1360 vtbl = build_if_in_charge (vtbl, vtbl2); 1443 vtbl = build_if_in_charge (vtbl, vtbl2);
1361 } 1444 }
1362 1445
1363 /* Compute the location of the vtpr. */ 1446 /* Compute the location of the vtpr. */
1364 vtbl_ptr = build_vfield_ref (cp_build_indirect_ref (decl, RO_NULL, 1447 vtbl_ptr = build_vfield_ref (cp_build_fold_indirect_ref (decl),
1365 tf_warning_or_error),
1366 TREE_TYPE (binfo)); 1448 TREE_TYPE (binfo));
1367 gcc_assert (vtbl_ptr != error_mark_node); 1449 gcc_assert (vtbl_ptr != error_mark_node);
1368 1450
1369 /* Assign the vtable to the vptr. */ 1451 /* Assign the vtable to the vptr. */
1370 vtbl = convert_force (TREE_TYPE (vtbl_ptr), vtbl, 0, tf_warning_or_error); 1452 vtbl = convert_force (TREE_TYPE (vtbl_ptr), vtbl, 0, tf_warning_or_error);
1659 1741
1660 if (init == error_mark_node) 1742 if (init == error_mark_node)
1661 return error_mark_node; 1743 return error_mark_node;
1662 1744
1663 location_t init_loc = (init 1745 location_t init_loc = (init
1664 ? EXPR_LOC_OR_LOC (init, input_location) 1746 ? cp_expr_loc_or_loc (init, input_location)
1665 : location_of (exp)); 1747 : location_of (exp));
1666 1748
1667 TREE_READONLY (exp) = 0; 1749 TREE_READONLY (exp) = 0;
1668 TREE_THIS_VOLATILE (exp) = 0; 1750 TREE_THIS_VOLATILE (exp) = 0;
1669 1751
1673 int from_array = 0; 1755 int from_array = 0;
1674 1756
1675 if (VAR_P (exp) && DECL_DECOMPOSITION_P (exp)) 1757 if (VAR_P (exp) && DECL_DECOMPOSITION_P (exp))
1676 { 1758 {
1677 from_array = 1; 1759 from_array = 1;
1760 init = mark_rvalue_use (init);
1678 if (init && DECL_P (init) 1761 if (init && DECL_P (init)
1679 && !(flags & LOOKUP_ONLYCONVERTING)) 1762 && !(flags & LOOKUP_ONLYCONVERTING))
1680 { 1763 {
1681 /* Wrap the initializer in a CONSTRUCTOR so that build_vec_init 1764 /* Wrap the initializer in a CONSTRUCTOR so that build_vec_init
1682 recognizes it as direct-initialization. */ 1765 recognizes it as direct-initialization. */
1685 CONSTRUCTOR_IS_DIRECT_INIT (init) = true; 1768 CONSTRUCTOR_IS_DIRECT_INIT (init) = true;
1686 } 1769 }
1687 } 1770 }
1688 else 1771 else
1689 { 1772 {
1690 /* An array may not be initialized use the parenthesized
1691 initialization form -- unless the initializer is "()". */
1692 if (init && TREE_CODE (init) == TREE_LIST)
1693 {
1694 if (complain & tf_error)
1695 error ("bad array initializer");
1696 return error_mark_node;
1697 }
1698 /* Must arrange to initialize each element of EXP 1773 /* Must arrange to initialize each element of EXP
1699 from elements of INIT. */ 1774 from elements of INIT. */
1700 if (cv_qualified_p (type)) 1775 if (cv_qualified_p (type))
1701 TREE_TYPE (exp) = cv_unqualified (type); 1776 TREE_TYPE (exp) = cv_unqualified (type);
1702 if (itype && cv_qualified_p (itype)) 1777 if (itype && cv_qualified_p (itype))
1703 TREE_TYPE (init) = cv_unqualified (itype); 1778 TREE_TYPE (init) = cv_unqualified (itype);
1704 from_array = (itype && same_type_p (TREE_TYPE (init), 1779 from_array = (itype && same_type_p (TREE_TYPE (init),
1705 TREE_TYPE (exp))); 1780 TREE_TYPE (exp)));
1706 1781
1707 if (init && !from_array 1782 if (init && !BRACE_ENCLOSED_INITIALIZER_P (init)
1708 && !BRACE_ENCLOSED_INITIALIZER_P (init)) 1783 && (!from_array
1784 || (TREE_CODE (init) != CONSTRUCTOR
1785 /* Can happen, eg, handling the compound-literals
1786 extension (ext/complit12.C). */
1787 && TREE_CODE (init) != TARGET_EXPR)))
1709 { 1788 {
1710 if (complain & tf_error) 1789 if (complain & tf_error)
1711 permerror (init_loc, "array must be initialized " 1790 error_at (init_loc, "array must be initialized "
1712 "with a brace-enclosed initializer"); 1791 "with a brace-enclosed initializer");
1713 else 1792 return error_mark_node;
1714 return error_mark_node;
1715 } 1793 }
1716 } 1794 }
1717 1795
1718 stmt_expr = build_vec_init (exp, NULL_TREE, init, 1796 stmt_expr = build_vec_init (exp, NULL_TREE, init,
1719 /*explicit_value_init_p=*/false, 1797 /*explicit_value_init_p=*/false,
1733 && !(TREE_CODE (init) == TARGET_EXPR 1811 && !(TREE_CODE (init) == TARGET_EXPR
1734 && TARGET_EXPR_DIRECT_INIT_P (init)) 1812 && TARGET_EXPR_DIRECT_INIT_P (init))
1735 && !DIRECT_LIST_INIT_P (init)) 1813 && !DIRECT_LIST_INIT_P (init))
1736 flags |= LOOKUP_ONLYCONVERTING; 1814 flags |= LOOKUP_ONLYCONVERTING;
1737 1815
1738 if ((VAR_P (exp) || TREE_CODE (exp) == PARM_DECL)
1739 && !lookup_attribute ("warn_unused", TYPE_ATTRIBUTES (type)))
1740 /* Just know that we've seen something for this node. */
1741 TREE_USED (exp) = 1;
1742
1743 is_global = begin_init_stmts (&stmt_expr, &compound_stmt); 1816 is_global = begin_init_stmts (&stmt_expr, &compound_stmt);
1744 destroy_temps = stmts_are_full_exprs_p (); 1817 destroy_temps = stmts_are_full_exprs_p ();
1745 current_stmt_tree ()->stmts_are_full_exprs_p = 0; 1818 current_stmt_tree ()->stmts_are_full_exprs_p = 0;
1746 expand_aggr_init_1 (TYPE_BINFO (type), exp, exp, 1819 expand_aggr_init_1 (TYPE_BINFO (type), exp, exp,
1747 init, LOOKUP_NORMAL|flags, complain); 1820 init, LOOKUP_NORMAL|flags, complain);
1748 stmt_expr = finish_init_stmts (is_global, stmt_expr, compound_stmt); 1821 stmt_expr = finish_init_stmts (is_global, stmt_expr, compound_stmt);
1749 current_stmt_tree ()->stmts_are_full_exprs_p = destroy_temps; 1822 current_stmt_tree ()->stmts_are_full_exprs_p = destroy_temps;
1750 TREE_READONLY (exp) = was_const; 1823 TREE_READONLY (exp) = was_const;
1751 TREE_THIS_VOLATILE (exp) = was_volatile; 1824 TREE_THIS_VOLATILE (exp) = was_volatile;
1825
1826 if ((VAR_P (exp) || TREE_CODE (exp) == PARM_DECL)
1827 && TREE_SIDE_EFFECTS (stmt_expr)
1828 && !lookup_attribute ("warn_unused", TYPE_ATTRIBUTES (type)))
1829 /* Just know that we've seen something for this node. */
1830 TREE_USED (exp) = 1;
1752 1831
1753 return stmt_expr; 1832 return stmt_expr;
1754 } 1833 }
1755 1834
1756 static void 1835 static void
2363 field_type = strip_array_types (TREE_TYPE (field)); 2442 field_type = strip_array_types (TREE_TYPE (field));
2364 2443
2365 if (type_has_user_provided_constructor (field_type)) 2444 if (type_has_user_provided_constructor (field_type))
2366 continue; 2445 continue;
2367 2446
2368 if (TREE_CODE (field_type) == REFERENCE_TYPE) 2447 if (TYPE_REF_P (field_type))
2369 { 2448 {
2370 ++ error_count; 2449 ++ error_count;
2371 if (complain) 2450 if (complain)
2372 { 2451 {
2373 if (DECL_CONTEXT (field) == origin) 2452 if (DECL_CONTEXT (field) == origin)
2453 } 2532 }
2454 2533
2455 return build_cxx_call (fn, 0, NULL, tf_warning_or_error); 2534 return build_cxx_call (fn, 0, NULL, tf_warning_or_error);
2456 } 2535 }
2457 2536
2458 /* Attempt to find the initializer for field T in the initializer INIT, 2537 /* Attempt to find the initializer for flexible array field T in the
2459 when non-null. Returns the initializer when successful and NULL 2538 initializer INIT, when non-null. Returns the initializer when
2460 otherwise. */ 2539 successful and NULL otherwise. */
2461 static tree 2540 static tree
2462 find_field_init (tree t, tree init) 2541 find_flexarray_init (tree t, tree init)
2463 { 2542 {
2464 if (!init) 2543 if (!init || init == error_mark_node)
2465 return NULL_TREE; 2544 return NULL_TREE;
2466 2545
2467 unsigned HOST_WIDE_INT idx; 2546 unsigned HOST_WIDE_INT idx;
2468 tree field, elt; 2547 tree field, elt;
2469 2548
2470 /* Iterate over all top-level initializer elements. */ 2549 /* Iterate over all top-level initializer elements. */
2471 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (init), idx, field, elt) 2550 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (init), idx, field, elt)
2472 { 2551 /* If the member T is found, return it. */
2473 /* If the member T is found, return it. */ 2552 if (field == t)
2474 if (field == t) 2553 return elt;
2475 return elt; 2554
2476
2477 /* Otherwise continue and/or recurse into nested initializers. */
2478 if (TREE_CODE (elt) == CONSTRUCTOR
2479 && (init = find_field_init (t, elt)))
2480 return init;
2481 }
2482 return NULL_TREE; 2555 return NULL_TREE;
2483 } 2556 }
2484 2557
2485 /* Attempt to verify that the argument, OPER, of a placement new expression 2558 /* Attempt to verify that the argument, OPER, of a placement new expression
2486 refers to an object sufficiently large for an object of TYPE or an array 2559 refers to an object sufficiently large for an object of TYPE or an array
2494 not diagnosed since we don't know how they use the buffer (this could 2567 not diagnosed since we don't know how they use the buffer (this could
2495 be a future extension). */ 2568 be a future extension). */
2496 static void 2569 static void
2497 warn_placement_new_too_small (tree type, tree nelts, tree size, tree oper) 2570 warn_placement_new_too_small (tree type, tree nelts, tree size, tree oper)
2498 { 2571 {
2499 location_t loc = EXPR_LOC_OR_LOC (oper, input_location); 2572 location_t loc = cp_expr_loc_or_loc (oper, input_location);
2500 2573
2501 /* The number of bytes to add to or subtract from the size of the provided 2574 /* The number of bytes to add to or subtract from the size of the provided
2502 buffer based on an offset into an array or an array element reference. 2575 buffer based on an offset into an array or an array element reference.
2503 Although intermediate results may be negative (as in a[3] - 2) the final 2576 Although intermediate results may be negative (as in a[3] - 2) a valid
2504 result cannot be. */ 2577 final result cannot be. */
2505 HOST_WIDE_INT adjust = 0; 2578 offset_int adjust = 0;
2506 /* True when the size of the entire destination object should be used 2579 /* True when the size of the entire destination object should be used
2507 to compute the possibly optimistic estimate of the available space. */ 2580 to compute the possibly optimistic estimate of the available space. */
2508 bool use_obj_size = false; 2581 bool use_obj_size = false;
2509 /* True when the reference to the destination buffer is an ADDR_EXPR. */ 2582 /* True when the reference to the destination buffer is an ADDR_EXPR. */
2510 bool addr_expr = false; 2583 bool addr_expr = false;
2524 2597
2525 /* Handle the common case of array + offset expression when the offset 2598 /* Handle the common case of array + offset expression when the offset
2526 is a constant. */ 2599 is a constant. */
2527 if (TREE_CODE (oper) == POINTER_PLUS_EXPR) 2600 if (TREE_CODE (oper) == POINTER_PLUS_EXPR)
2528 { 2601 {
2529 /* If the offset is comple-time constant, use it to compute a more 2602 /* If the offset is compile-time constant, use it to compute a more
2530 accurate estimate of the size of the buffer. Since the operand 2603 accurate estimate of the size of the buffer. Since the operand
2531 of POINTER_PLUS_EXPR is represented as an unsigned type, convert 2604 of POINTER_PLUS_EXPR is represented as an unsigned type, convert
2532 it to signed first. 2605 it to signed first.
2533 Otherwise, use the size of the entire array as an optimistic 2606 Otherwise, use the size of the entire array as an optimistic
2534 estimate (this may lead to false negatives). */ 2607 estimate (this may lead to false negatives). */
2535 tree adj = TREE_OPERAND (oper, 1); 2608 tree adj = TREE_OPERAND (oper, 1);
2536 if (CONSTANT_CLASS_P (adj)) 2609 if (CONSTANT_CLASS_P (adj))
2537 adjust += tree_to_shwi (convert (ssizetype, adj)); 2610 adjust += wi::to_offset (convert (ssizetype, adj));
2538 else 2611 else
2539 use_obj_size = true; 2612 use_obj_size = true;
2540 2613
2541 oper = TREE_OPERAND (oper, 0); 2614 oper = TREE_OPERAND (oper, 0);
2542 2615
2559 /* Similar to the offset computed above, see if the array index 2632 /* Similar to the offset computed above, see if the array index
2560 is a compile-time constant. If so, and unless the offset was 2633 is a compile-time constant. If so, and unless the offset was
2561 not a compile-time constant, use the index to determine the 2634 not a compile-time constant, use the index to determine the
2562 size of the buffer. Otherwise, use the entire array as 2635 size of the buffer. Otherwise, use the entire array as
2563 an optimistic estimate of the size. */ 2636 an optimistic estimate of the size. */
2564 const_tree adj = TREE_OPERAND (oper, 1); 2637 const_tree adj = fold_non_dependent_expr (TREE_OPERAND (oper, 1));
2565 if (!use_obj_size && CONSTANT_CLASS_P (adj)) 2638 if (!use_obj_size && CONSTANT_CLASS_P (adj))
2566 adjust += tree_to_shwi (adj); 2639 adjust += wi::to_offset (adj);
2567 else 2640 else
2568 { 2641 {
2569 use_obj_size = true; 2642 use_obj_size = true;
2570 adjust = 0; 2643 adjust = 0;
2571 } 2644 }
2580 tree var_decl = NULL_TREE; 2653 tree var_decl = NULL_TREE;
2581 /* True when operand is a COMPONENT_REF, to distinguish flexible array 2654 /* True when operand is a COMPONENT_REF, to distinguish flexible array
2582 members from arrays of unspecified size. */ 2655 members from arrays of unspecified size. */
2583 bool compref = TREE_CODE (oper) == COMPONENT_REF; 2656 bool compref = TREE_CODE (oper) == COMPONENT_REF;
2584 2657
2658 /* For COMPONENT_REF (i.e., a struct member) the size of the entire
2659 enclosing struct. Used to validate the adjustment (offset) into
2660 an array at the end of a struct. */
2661 offset_int compsize = 0;
2662
2585 /* Descend into a struct or union to find the member whose address 2663 /* Descend into a struct or union to find the member whose address
2586 is being used as the argument. */ 2664 is being used as the argument. */
2587 if (TREE_CODE (oper) == COMPONENT_REF) 2665 if (TREE_CODE (oper) == COMPONENT_REF)
2588 { 2666 {
2667 tree comptype = TREE_TYPE (TREE_OPERAND (oper, 0));
2668 compsize = wi::to_offset (TYPE_SIZE_UNIT (comptype));
2669
2589 tree op0 = oper; 2670 tree op0 = oper;
2590 while (TREE_CODE (op0 = TREE_OPERAND (op0, 0)) == COMPONENT_REF); 2671 while (TREE_CODE (op0 = TREE_OPERAND (op0, 0)) == COMPONENT_REF);
2591 if (VAR_P (op0)) 2672 if (VAR_P (op0))
2592 var_decl = op0; 2673 var_decl = op0;
2593 oper = TREE_OPERAND (oper, 1); 2674 oper = TREE_OPERAND (oper, 1);
2594 } 2675 }
2595 2676
2596 if ((addr_expr || !POINTER_TYPE_P (TREE_TYPE (oper))) 2677 tree opertype = TREE_TYPE (oper);
2678 if ((addr_expr || !INDIRECT_TYPE_P (opertype))
2597 && (VAR_P (oper) 2679 && (VAR_P (oper)
2598 || TREE_CODE (oper) == FIELD_DECL 2680 || TREE_CODE (oper) == FIELD_DECL
2599 || TREE_CODE (oper) == PARM_DECL)) 2681 || TREE_CODE (oper) == PARM_DECL))
2600 { 2682 {
2601 /* A possibly optimistic estimate of the number of bytes available 2683 /* A possibly optimistic estimate of the number of bytes available
2602 in the destination buffer. */ 2684 in the destination buffer. */
2603 unsigned HOST_WIDE_INT bytes_avail = 0; 2685 offset_int bytes_avail = 0;
2604 /* True when the estimate above is in fact the exact size 2686 /* True when the estimate above is in fact the exact size
2605 of the destination buffer rather than an estimate. */ 2687 of the destination buffer rather than an estimate. */
2606 bool exact_size = true; 2688 bool exact_size = true;
2607 2689
2608 /* Treat members of unions and members of structs uniformly, even 2690 /* Treat members of unions and members of structs uniformly, even
2613 && tree_fits_uhwi_p (DECL_SIZE_UNIT (oper))) 2695 && tree_fits_uhwi_p (DECL_SIZE_UNIT (oper)))
2614 { 2696 {
2615 /* Use the size of the entire array object when the expression 2697 /* Use the size of the entire array object when the expression
2616 refers to a variable or its size depends on an expression 2698 refers to a variable or its size depends on an expression
2617 that's not a compile-time constant. */ 2699 that's not a compile-time constant. */
2618 bytes_avail = tree_to_uhwi (DECL_SIZE_UNIT (oper)); 2700 bytes_avail = wi::to_offset (DECL_SIZE_UNIT (oper));
2619 exact_size = !use_obj_size; 2701 exact_size = !use_obj_size;
2620 } 2702 }
2621 else if (TYPE_SIZE_UNIT (TREE_TYPE (oper)) 2703 else if (tree opersize = TYPE_SIZE_UNIT (opertype))
2622 && tree_fits_uhwi_p (TYPE_SIZE_UNIT (TREE_TYPE (oper))))
2623 { 2704 {
2624 /* Use the size of the type of the destination buffer object 2705 /* Use the size of the type of the destination buffer object
2625 as the optimistic estimate of the available space in it. */ 2706 as the optimistic estimate of the available space in it.
2626 bytes_avail = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (oper))); 2707 Use the maximum possible size for zero-size arrays and
2627 } 2708 flexible array members (except of initialized objects
2628 else if (var_decl) 2709 thereof). */
2629 { 2710 if (TREE_CODE (opersize) == INTEGER_CST)
2630 /* Constructing into a buffer provided by the flexible array 2711 bytes_avail = wi::to_offset (opersize);
2631 member of a declared object (which is permitted as a G++ 2712 }
2632 extension). If the array member has been initialized, 2713
2633 determine its size from the initializer. Otherwise, 2714 if (bytes_avail == 0)
2634 the array size is zero. */ 2715 {
2635 bytes_avail = 0; 2716 if (var_decl)
2636 2717 {
2637 if (tree init = find_field_init (oper, DECL_INITIAL (var_decl))) 2718 /* Constructing into a buffer provided by the flexible array
2638 bytes_avail = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (init))); 2719 member of a declared object (which is permitted as a G++
2639 } 2720 extension). If the array member has been initialized,
2640 else 2721 determine its size from the initializer. Otherwise,
2641 { 2722 the array size is zero. */
2642 /* Bail if neither the size of the object nor its type is known. */ 2723 if (tree init = find_flexarray_init (oper,
2643 return; 2724 DECL_INITIAL (var_decl)))
2644 } 2725 bytes_avail = wi::to_offset (TYPE_SIZE_UNIT (TREE_TYPE (init)));
2645 2726 }
2646 tree_code oper_code = TREE_CODE (TREE_TYPE (oper)); 2727 else
2728 bytes_avail = (wi::to_offset (TYPE_MAX_VALUE (ptrdiff_type_node))
2729 - compsize);
2730 }
2731
2732 tree_code oper_code = TREE_CODE (opertype);
2647 2733
2648 if (compref && oper_code == ARRAY_TYPE) 2734 if (compref && oper_code == ARRAY_TYPE)
2649 { 2735 {
2650 /* Avoid diagnosing flexible array members (which are accepted 2736 tree nelts = array_type_nelts_top (opertype);
2651 as an extension and diagnosed with -Wpedantic) and zero-length
2652 arrays (also an extension).
2653 Overflowing construction in one-element arrays is diagnosed
2654 only at level 2. */
2655 if (bytes_avail == 0 && !var_decl)
2656 return;
2657
2658 tree nelts = array_type_nelts_top (TREE_TYPE (oper));
2659 tree nelts_cst = maybe_constant_value (nelts); 2737 tree nelts_cst = maybe_constant_value (nelts);
2660 if (TREE_CODE (nelts_cst) == INTEGER_CST 2738 if (TREE_CODE (nelts_cst) == INTEGER_CST
2661 && integer_onep (nelts_cst) 2739 && integer_onep (nelts_cst)
2662 && !var_decl 2740 && !var_decl
2663 && warn_placement_new < 2) 2741 && warn_placement_new < 2)
2664 return; 2742 return;
2665 } 2743 }
2666 2744
2667 /* The size of the buffer can only be adjusted down but not up. */
2668 gcc_checking_assert (0 <= adjust);
2669
2670 /* Reduce the size of the buffer by the adjustment computed above 2745 /* Reduce the size of the buffer by the adjustment computed above
2671 from the offset and/or the index into the array. */ 2746 from the offset and/or the index into the array. */
2672 if (bytes_avail < static_cast<unsigned HOST_WIDE_INT>(adjust)) 2747 if (bytes_avail < adjust || adjust < 0)
2673 bytes_avail = 0; 2748 bytes_avail = 0;
2674 else 2749 else
2675 bytes_avail -= adjust; 2750 {
2751 tree elttype = (TREE_CODE (opertype) == ARRAY_TYPE
2752 ? TREE_TYPE (opertype) : opertype);
2753 if (tree eltsize = TYPE_SIZE_UNIT (elttype))
2754 {
2755 bytes_avail -= adjust * wi::to_offset (eltsize);
2756 if (bytes_avail < 0)
2757 bytes_avail = 0;
2758 }
2759 }
2676 2760
2677 /* The minimum amount of space needed for the allocation. This 2761 /* The minimum amount of space needed for the allocation. This
2678 is an optimistic estimate that makes it possible to detect 2762 is an optimistic estimate that makes it possible to detect
2679 placement new invocation for some undersize buffers but not 2763 placement new invocation for some undersize buffers but not
2680 others. */ 2764 others. */
2681 unsigned HOST_WIDE_INT bytes_need; 2765 offset_int bytes_need;
2682 2766
2683 if (CONSTANT_CLASS_P (size)) 2767 if (CONSTANT_CLASS_P (size))
2684 bytes_need = tree_to_uhwi (size); 2768 bytes_need = wi::to_offset (size);
2685 else if (nelts && CONSTANT_CLASS_P (nelts)) 2769 else if (nelts && CONSTANT_CLASS_P (nelts))
2686 bytes_need = tree_to_uhwi (nelts) 2770 bytes_need = (wi::to_offset (nelts)
2687 * tree_to_uhwi (TYPE_SIZE_UNIT (type)); 2771 * wi::to_offset (TYPE_SIZE_UNIT (type)));
2688 else if (tree_fits_uhwi_p (TYPE_SIZE_UNIT (type))) 2772 else if (tree_fits_uhwi_p (TYPE_SIZE_UNIT (type)))
2689 bytes_need = tree_to_uhwi (TYPE_SIZE_UNIT (type)); 2773 bytes_need = wi::to_offset (TYPE_SIZE_UNIT (type));
2690 else 2774 else
2691 { 2775 {
2692 /* The type is a VLA. */ 2776 /* The type is a VLA. */
2693 return; 2777 return;
2694 } 2778 }
2703 "%<%T [%wu]%> and size %qwu in a region of type %qT " 2787 "%<%T [%wu]%> and size %qwu in a region of type %qT "
2704 "and size %qwi" 2788 "and size %qwi"
2705 : "placement new constructing an object of type " 2789 : "placement new constructing an object of type "
2706 "%<%T [%wu]%> and size %qwu in a region of type %qT " 2790 "%<%T [%wu]%> and size %qwu in a region of type %qT "
2707 "and size at most %qwu", 2791 "and size at most %qwu",
2708 type, tree_to_uhwi (nelts), bytes_need, 2792 type, tree_to_uhwi (nelts), bytes_need.to_uhwi (),
2709 TREE_TYPE (oper), 2793 opertype, bytes_avail.to_uhwi ());
2710 bytes_avail);
2711 else 2794 else
2712 warning_at (loc, OPT_Wplacement_new_, 2795 warning_at (loc, OPT_Wplacement_new_,
2713 exact_size ? 2796 exact_size ?
2714 "placement new constructing an array of objects " 2797 "placement new constructing an array of objects "
2715 "of type %qT and size %qwu in a region of type %qT " 2798 "of type %qT and size %qwu in a region of type %qT "
2716 "and size %qwi" 2799 "and size %qwi"
2717 : "placement new constructing an array of objects " 2800 : "placement new constructing an array of objects "
2718 "of type %qT and size %qwu in a region of type %qT " 2801 "of type %qT and size %qwu in a region of type %qT "
2719 "and size at most %qwu", 2802 "and size at most %qwu",
2720 type, bytes_need, TREE_TYPE (oper), 2803 type, bytes_need.to_uhwi (), opertype,
2721 bytes_avail); 2804 bytes_avail.to_uhwi ());
2722 else 2805 else
2723 warning_at (loc, OPT_Wplacement_new_, 2806 warning_at (loc, OPT_Wplacement_new_,
2724 exact_size ? 2807 exact_size ?
2725 "placement new constructing an object of type %qT " 2808 "placement new constructing an object of type %qT "
2726 "and size %qwu in a region of type %qT and size %qwi" 2809 "and size %qwu in a region of type %qT and size %qwi"
2727 : "placement new constructing an object of type %qT " 2810 : "placement new constructing an object of type %qT "
2728 "and size %qwu in a region of type %qT and size " 2811 "and size %qwu in a region of type %qT and size "
2729 "at most %qwu", 2812 "at most %qwu",
2730 type, bytes_need, TREE_TYPE (oper), 2813 type, bytes_need.to_uhwi (), opertype,
2731 bytes_avail); 2814 bytes_avail.to_uhwi ());
2732 } 2815 }
2733 } 2816 }
2734 } 2817 }
2735 2818
2736 /* True if alignof(T) > __STDCPP_DEFAULT_NEW_ALIGNMENT__. */ 2819 /* True if alignof(T) > __STDCPP_DEFAULT_NEW_ALIGNMENT__. */
2756 non-replaceable placement new function. See DR 1748. 2839 non-replaceable placement new function. See DR 1748.
2757 TODO: Enable in all standard modes. */ 2840 TODO: Enable in all standard modes. */
2758 static bool 2841 static bool
2759 std_placement_new_fn_p (tree alloc_fn) 2842 std_placement_new_fn_p (tree alloc_fn)
2760 { 2843 {
2761 if ((cxx_dialect > cxx14) && DECL_NAMESPACE_SCOPE_P (alloc_fn)) 2844 if (DECL_NAMESPACE_SCOPE_P (alloc_fn))
2762 { 2845 {
2763 tree first_arg = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (alloc_fn))); 2846 tree first_arg = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (alloc_fn)));
2764 if ((TREE_VALUE (first_arg) == ptr_type_node) 2847 if ((TREE_VALUE (first_arg) == ptr_type_node)
2765 && TREE_CHAIN (first_arg) == void_list_node) 2848 && TREE_CHAIN (first_arg) == void_list_node)
2766 return true; 2849 return true;
2847 outer_nelts = nelts; 2930 outer_nelts = nelts;
2848 type = TREE_TYPE (type); 2931 type = TREE_TYPE (type);
2849 outer_nelts_from_type = true; 2932 outer_nelts_from_type = true;
2850 } 2933 }
2851 2934
2852 /* Lots of logic below. depends on whether we have a constant number of 2935 /* Lots of logic below depends on whether we have a constant number of
2853 elements, so go ahead and fold it now. */ 2936 elements, so go ahead and fold it now. */
2854 if (outer_nelts) 2937 const_tree cst_outer_nelts = fold_non_dependent_expr (outer_nelts, complain);
2855 outer_nelts = maybe_constant_value (outer_nelts);
2856 2938
2857 /* If our base type is an array, then make sure we know how many elements 2939 /* If our base type is an array, then make sure we know how many elements
2858 it has. */ 2940 it has. */
2859 for (elt_type = type; 2941 for (elt_type = type;
2860 TREE_CODE (elt_type) == ARRAY_TYPE; 2942 TREE_CODE (elt_type) == ARRAY_TYPE;
2862 { 2944 {
2863 tree inner_nelts = array_type_nelts_top (elt_type); 2945 tree inner_nelts = array_type_nelts_top (elt_type);
2864 tree inner_nelts_cst = maybe_constant_value (inner_nelts); 2946 tree inner_nelts_cst = maybe_constant_value (inner_nelts);
2865 if (TREE_CODE (inner_nelts_cst) == INTEGER_CST) 2947 if (TREE_CODE (inner_nelts_cst) == INTEGER_CST)
2866 { 2948 {
2867 bool overflow; 2949 wi::overflow_type overflow;
2868 offset_int result = wi::mul (wi::to_offset (inner_nelts_cst), 2950 offset_int result = wi::mul (wi::to_offset (inner_nelts_cst),
2869 inner_nelts_count, SIGNED, &overflow); 2951 inner_nelts_count, SIGNED, &overflow);
2870 if (overflow) 2952 if (overflow)
2871 { 2953 {
2872 if (complain & tf_error) 2954 if (complain & tf_error)
2877 } 2959 }
2878 else 2960 else
2879 { 2961 {
2880 if (complain & tf_error) 2962 if (complain & tf_error)
2881 { 2963 {
2882 error_at (EXPR_LOC_OR_LOC (inner_nelts, input_location), 2964 error_at (cp_expr_loc_or_loc (inner_nelts, input_location),
2883 "array size in new-expression must be constant"); 2965 "array size in new-expression must be constant");
2884 cxx_constant_value(inner_nelts); 2966 cxx_constant_value(inner_nelts);
2885 } 2967 }
2886 nelts = error_mark_node; 2968 nelts = error_mark_node;
2887 } 2969 }
2902 return error_mark_node; 2984 return error_mark_node;
2903 2985
2904 /* Warn if we performed the (T[N]) to T[N] transformation and N is 2986 /* Warn if we performed the (T[N]) to T[N] transformation and N is
2905 variable. */ 2987 variable. */
2906 if (outer_nelts_from_type 2988 if (outer_nelts_from_type
2907 && !TREE_CONSTANT (outer_nelts)) 2989 && !TREE_CONSTANT (cst_outer_nelts))
2908 { 2990 {
2909 if (complain & tf_warning_or_error) 2991 if (complain & tf_warning_or_error)
2910 { 2992 {
2911 pedwarn (EXPR_LOC_OR_LOC (outer_nelts, input_location), OPT_Wvla, 2993 pedwarn (cp_expr_loc_or_loc (outer_nelts, input_location), OPT_Wvla,
2912 typedef_variant_p (orig_type) 2994 typedef_variant_p (orig_type)
2913 ? G_("non-constant array new length must be specified " 2995 ? G_("non-constant array new length must be specified "
2914 "directly, not by typedef") 2996 "directly, not by typedef")
2915 : G_("non-constant array new length must be specified " 2997 : G_("non-constant array new length must be specified "
2916 "without parentheses around the type-id")); 2998 "without parentheses around the type-id"));
2923 { 3005 {
2924 if (complain & tf_error) 3006 if (complain & tf_error)
2925 error ("invalid type %<void%> for new"); 3007 error ("invalid type %<void%> for new");
2926 return error_mark_node; 3008 return error_mark_node;
2927 } 3009 }
3010
3011 if (is_std_init_list (elt_type))
3012 warning (OPT_Winit_list_lifetime,
3013 "%<new%> of initializer_list does not "
3014 "extend the lifetime of the underlying array");
2928 3015
2929 if (abstract_virtuals_error_sfinae (ACU_NEW, elt_type, complain)) 3016 if (abstract_virtuals_error_sfinae (ACU_NEW, elt_type, complain))
2930 return error_mark_node; 3017 return error_mark_node;
2931 3018
2932 is_initialized = (type_build_ctor_call (elt_type) || *init != NULL); 3019 is_initialized = (type_build_ctor_call (elt_type) || *init != NULL);
2986 gcc_checking_assert (wi::ltu_p (wi::to_offset (cookie_size), max_size)); 3073 gcc_checking_assert (wi::ltu_p (wi::to_offset (cookie_size), max_size));
2987 /* Unconditionally subtract the cookie size. This decreases the 3074 /* Unconditionally subtract the cookie size. This decreases the
2988 maximum object size and is safe even if we choose not to use 3075 maximum object size and is safe even if we choose not to use
2989 a cookie after all. */ 3076 a cookie after all. */
2990 max_size -= wi::to_offset (cookie_size); 3077 max_size -= wi::to_offset (cookie_size);
2991 bool overflow; 3078 wi::overflow_type overflow;
2992 inner_size = wi::mul (wi::to_offset (size), inner_nelts_count, SIGNED, 3079 inner_size = wi::mul (wi::to_offset (size), inner_nelts_count, SIGNED,
2993 &overflow); 3080 &overflow);
2994 if (overflow || wi::gtu_p (inner_size, max_size)) 3081 if (overflow || wi::gtu_p (inner_size, max_size))
2995 { 3082 {
2996 if (complain & tf_error) 3083 if (complain & tf_error)
3001 max_outer_nelts = wi::udiv_trunc (max_size, inner_size); 3088 max_outer_nelts = wi::udiv_trunc (max_size, inner_size);
3002 max_outer_nelts_tree = wide_int_to_tree (sizetype, max_outer_nelts); 3089 max_outer_nelts_tree = wide_int_to_tree (sizetype, max_outer_nelts);
3003 3090
3004 size = size_binop (MULT_EXPR, size, fold_convert (sizetype, nelts)); 3091 size = size_binop (MULT_EXPR, size, fold_convert (sizetype, nelts));
3005 3092
3006 if (INTEGER_CST == TREE_CODE (outer_nelts)) 3093 if (TREE_CODE (cst_outer_nelts) == INTEGER_CST)
3007 { 3094 {
3008 if (tree_int_cst_lt (max_outer_nelts_tree, outer_nelts)) 3095 if (tree_int_cst_lt (max_outer_nelts_tree, cst_outer_nelts))
3009 { 3096 {
3010 /* When the array size is constant, check it at compile time 3097 /* When the array size is constant, check it at compile time
3011 to make sure it doesn't exceed the implementation-defined 3098 to make sure it doesn't exceed the implementation-defined
3012 maximum, as required by C++ 14 (in C++ 11 this requirement 3099 maximum, as required by C++ 14 (in C++ 11 this requirement
3013 isn't explicitly stated but it's enforced anyway -- see 3100 isn't explicitly stated but it's enforced anyway -- see
3053 3140
3054 /* Allocate the object. */ 3141 /* Allocate the object. */
3055 tree fnname; 3142 tree fnname;
3056 tree fns; 3143 tree fns;
3057 3144
3058 fnname = cp_operator_id (array_p ? VEC_NEW_EXPR : NEW_EXPR); 3145 fnname = ovl_op_identifier (false, array_p ? VEC_NEW_EXPR : NEW_EXPR);
3059 3146
3060 member_new_p = !globally_qualified_p 3147 member_new_p = !globally_qualified_p
3061 && CLASS_TYPE_P (elt_type) 3148 && CLASS_TYPE_P (elt_type)
3062 && (array_p 3149 && (array_p
3063 ? TYPE_HAS_ARRAY_NEW_OPERATOR (elt_type) 3150 ? TYPE_HAS_ARRAY_NEW_OPERATOR (elt_type)
3169 && TYPE_ALIGN (elt_type) > malloc_alignment () 3256 && TYPE_ALIGN (elt_type) > malloc_alignment ()
3170 && (warn_aligned_new > 1 3257 && (warn_aligned_new > 1
3171 || CP_DECL_CONTEXT (alloc_fn) == global_namespace) 3258 || CP_DECL_CONTEXT (alloc_fn) == global_namespace)
3172 && !aligned_allocation_fn_p (alloc_fn)) 3259 && !aligned_allocation_fn_p (alloc_fn))
3173 { 3260 {
3261 auto_diagnostic_group d;
3174 if (warning (OPT_Waligned_new_, "%<new%> of type %qT with extended " 3262 if (warning (OPT_Waligned_new_, "%<new%> of type %qT with extended "
3175 "alignment %d", elt_type, TYPE_ALIGN_UNIT (elt_type))) 3263 "alignment %d", elt_type, TYPE_ALIGN_UNIT (elt_type)))
3176 { 3264 {
3177 inform (input_location, "uses %qD, which does not have an alignment " 3265 inform (input_location, "uses %qD, which does not have an alignment "
3178 "parameter", alloc_fn); 3266 "parameter", alloc_fn);
3266 cookie_ptr = size_binop (MINUS_EXPR, cookie_size, size_in_bytes (sizetype)); 3354 cookie_ptr = size_binop (MINUS_EXPR, cookie_size, size_in_bytes (sizetype));
3267 cookie_ptr = fold_build_pointer_plus_loc (input_location, 3355 cookie_ptr = fold_build_pointer_plus_loc (input_location,
3268 alloc_node, cookie_ptr); 3356 alloc_node, cookie_ptr);
3269 size_ptr_type = build_pointer_type (sizetype); 3357 size_ptr_type = build_pointer_type (sizetype);
3270 cookie_ptr = fold_convert (size_ptr_type, cookie_ptr); 3358 cookie_ptr = fold_convert (size_ptr_type, cookie_ptr);
3271 cookie = cp_build_indirect_ref (cookie_ptr, RO_NULL, complain); 3359 cookie = cp_build_fold_indirect_ref (cookie_ptr);
3272 3360
3273 cookie_expr = build2 (MODIFY_EXPR, sizetype, cookie, nelts); 3361 cookie_expr = build2 (MODIFY_EXPR, sizetype, cookie, nelts);
3274 3362
3275 if (targetm.cxx.cookie_has_size ()) 3363 if (targetm.cxx.cookie_has_size ())
3276 { 3364 {
3278 cookie_ptr = fold_build_pointer_plus (cookie_ptr, 3366 cookie_ptr = fold_build_pointer_plus (cookie_ptr,
3279 fold_build1_loc (input_location, 3367 fold_build1_loc (input_location,
3280 NEGATE_EXPR, sizetype, 3368 NEGATE_EXPR, sizetype,
3281 size_in_bytes (sizetype))); 3369 size_in_bytes (sizetype)));
3282 3370
3283 cookie = cp_build_indirect_ref (cookie_ptr, RO_NULL, complain); 3371 cookie = cp_build_fold_indirect_ref (cookie_ptr);
3284 cookie = build2 (MODIFY_EXPR, sizetype, cookie, 3372 cookie = build2 (MODIFY_EXPR, sizetype, cookie,
3285 size_in_bytes (elt_type)); 3373 size_in_bytes (elt_type));
3286 cookie_expr = build2 (COMPOUND_EXPR, TREE_TYPE (cookie_expr), 3374 cookie_expr = build2 (COMPOUND_EXPR, TREE_TYPE (cookie_expr),
3287 cookie, cookie_expr); 3375 cookie, cookie_expr);
3288 } 3376 }
3324 { 3412 {
3325 /* build_value_init doesn't work in templates, and we don't need 3413 /* build_value_init doesn't work in templates, and we don't need
3326 the initializer anyway since we're going to throw it away and 3414 the initializer anyway since we're going to throw it away and
3327 rebuild it at instantiation time, so just build up a single 3415 rebuild it at instantiation time, so just build up a single
3328 constructor call to get any appropriate diagnostics. */ 3416 constructor call to get any appropriate diagnostics. */
3329 init_expr = cp_build_indirect_ref (data_addr, RO_NULL, complain); 3417 init_expr = cp_build_fold_indirect_ref (data_addr);
3330 if (type_build_ctor_call (elt_type)) 3418 if (type_build_ctor_call (elt_type))
3331 init_expr = build_special_member_call (init_expr, 3419 init_expr = build_special_member_call (init_expr,
3332 complete_ctor_identifier, 3420 complete_ctor_identifier,
3333 init, elt_type, 3421 init, elt_type,
3334 LOOKUP_NORMAL, 3422 LOOKUP_NORMAL,
3358 } 3446 }
3359 } 3447 }
3360 else if (*init) 3448 else if (*init)
3361 { 3449 {
3362 if (complain & tf_error) 3450 if (complain & tf_error)
3363 permerror (input_location, 3451 error ("parenthesized initializer in array new");
3364 "parenthesized initializer in array new"); 3452 return error_mark_node;
3365 else
3366 return error_mark_node;
3367 vecinit = build_tree_list_vec (*init);
3368 } 3453 }
3369 init_expr 3454 init_expr
3370 = build_vec_init (data_addr, 3455 = build_vec_init (data_addr,
3371 cp_build_binary_op (input_location, 3456 cp_build_binary_op (input_location,
3372 MINUS_EXPR, outer_nelts, 3457 MINUS_EXPR, outer_nelts,
3382 leak out. */ 3467 leak out. */
3383 stable = true; 3468 stable = true;
3384 } 3469 }
3385 else 3470 else
3386 { 3471 {
3387 init_expr = cp_build_indirect_ref (data_addr, RO_NULL, complain); 3472 init_expr = cp_build_fold_indirect_ref (data_addr);
3388 3473
3389 if (type_build_ctor_call (type) && !explicit_value_init_p) 3474 if (type_build_ctor_call (type) && !explicit_value_init_p)
3390 { 3475 {
3391 init_expr = build_special_member_call (init_expr, 3476 init_expr = build_special_member_call (init_expr,
3392 complete_ctor_identifier, 3477 complete_ctor_identifier,
3579 { 3664 {
3580 tree auto_node = type_uses_auto (type); 3665 tree auto_node = type_uses_auto (type);
3581 if (auto_node) 3666 if (auto_node)
3582 { 3667 {
3583 tree d_init = NULL_TREE; 3668 tree d_init = NULL_TREE;
3584 if (vec_safe_length (*init) == 1) 3669 const size_t len = vec_safe_length (*init);
3670 /* E.g. new auto(x) must have exactly one element, or
3671 a {} initializer will have one element. */
3672 if (len == 1)
3585 { 3673 {
3586 d_init = (**init)[0]; 3674 d_init = (**init)[0];
3587 d_init = resolve_nondeduced_context (d_init, complain); 3675 d_init = resolve_nondeduced_context (d_init, complain);
3588 } 3676 }
3589 type = do_auto_deduction (type, d_init, auto_node); 3677 /* For the rest, e.g. new A(1, 2, 3), create a list. */
3678 else if (len > 1)
3679 {
3680 unsigned int n;
3681 tree t;
3682 tree *pp = &d_init;
3683 FOR_EACH_VEC_ELT (**init, n, t)
3684 {
3685 t = resolve_nondeduced_context (t, complain);
3686 *pp = build_tree_list (NULL_TREE, t);
3687 pp = &TREE_CHAIN (*pp);
3688 }
3689 }
3690 type = do_auto_deduction (type, d_init, auto_node, complain);
3590 } 3691 }
3591 } 3692 }
3592 3693
3593 if (processing_template_decl) 3694 if (processing_template_decl)
3594 { 3695 {
3632 } 3733 }
3633 3734
3634 /* Try to determine the constant value only for the purposes 3735 /* Try to determine the constant value only for the purposes
3635 of the diagnostic below but continue to use the original 3736 of the diagnostic below but continue to use the original
3636 value and handle const folding later. */ 3737 value and handle const folding later. */
3637 const_tree cst_nelts = maybe_constant_value (nelts); 3738 const_tree cst_nelts = fold_non_dependent_expr (nelts, complain);
3638 3739
3639 /* The expression in a noptr-new-declarator is erroneous if it's of 3740 /* The expression in a noptr-new-declarator is erroneous if it's of
3640 non-class type and its value before converting to std::size_t is 3741 non-class type and its value before converting to std::size_t is
3641 less than zero. ... If the expression is a constant expression, 3742 less than zero. ... If the expression is a constant expression,
3642 the program is ill-fomed. */ 3743 the program is ill-fomed. */
3643 if (INTEGER_CST == TREE_CODE (cst_nelts) 3744 if (TREE_CODE (cst_nelts) == INTEGER_CST
3644 && tree_int_cst_sgn (cst_nelts) == -1) 3745 && tree_int_cst_sgn (cst_nelts) == -1)
3645 { 3746 {
3646 if (complain & tf_error) 3747 if (complain & tf_error)
3647 error ("size of array is negative"); 3748 error ("size of array is negative");
3648 return error_mark_node; 3749 return error_mark_node;
3653 } 3754 }
3654 3755
3655 /* ``A reference cannot be created by the new operator. A reference 3756 /* ``A reference cannot be created by the new operator. A reference
3656 is not an object (8.2.2, 8.4.3), so a pointer to it could not be 3757 is not an object (8.2.2, 8.4.3), so a pointer to it could not be
3657 returned by new.'' ARM 5.3.3 */ 3758 returned by new.'' ARM 5.3.3 */
3658 if (TREE_CODE (type) == REFERENCE_TYPE) 3759 if (TYPE_REF_P (type))
3659 { 3760 {
3660 if (complain & tf_error) 3761 if (complain & tf_error)
3661 error ("new cannot be applied to a reference type"); 3762 error ("new cannot be applied to a reference type");
3662 else 3763 else
3663 return error_mark_node; 3764 return error_mark_node;
3732 if (base == error_mark_node || maxindex == error_mark_node) 3833 if (base == error_mark_node || maxindex == error_mark_node)
3733 return error_mark_node; 3834 return error_mark_node;
3734 3835
3735 if (!COMPLETE_TYPE_P (type)) 3836 if (!COMPLETE_TYPE_P (type))
3736 { 3837 {
3737 if ((complain & tf_warning) 3838 if (complain & tf_warning)
3738 && warning (OPT_Wdelete_incomplete, 3839 {
3739 "possible problem detected in invocation of " 3840 auto_diagnostic_group d;
3740 "delete [] operator:")) 3841 if (warning (OPT_Wdelete_incomplete,
3741 { 3842 "possible problem detected in invocation of "
3742 cxx_incomplete_type_diagnostic (base, type, DK_WARNING); 3843 "delete [] operator:"))
3743 inform (input_location, "neither the destructor nor the " 3844 {
3744 "class-specific operator delete [] will be called, " 3845 cxx_incomplete_type_diagnostic (base, type, DK_WARNING);
3745 "even if they are declared when the class is defined"); 3846 inform (input_location, "neither the destructor nor the "
3746 } 3847 "class-specific operator delete [] will be called, "
3848 "even if they are declared when the class is defined");
3849 }
3850 }
3747 /* This size won't actually be used. */ 3851 /* This size won't actually be used. */
3748 size_exp = size_one_node; 3852 size_exp = size_one_node;
3749 goto no_destructor; 3853 goto no_destructor;
3750 } 3854 }
3751 3855
4005 int num_initialized_elts = 0; 4109 int num_initialized_elts = 0;
4006 bool is_global; 4110 bool is_global;
4007 tree obase = base; 4111 tree obase = base;
4008 bool xvalue = false; 4112 bool xvalue = false;
4009 bool errors = false; 4113 bool errors = false;
4010 location_t loc = (init ? EXPR_LOC_OR_LOC (init, input_location) 4114 location_t loc = (init ? cp_expr_loc_or_loc (init, input_location)
4011 : location_of (base)); 4115 : location_of (base));
4012 4116
4013 if (TREE_CODE (atype) == ARRAY_TYPE && TYPE_DOMAIN (atype)) 4117 if (TREE_CODE (atype) == ARRAY_TYPE && TYPE_DOMAIN (atype))
4014 maxindex = array_type_nelts (atype); 4118 maxindex = array_type_nelts (atype);
4015 4119
4319 4423
4320 for_stmt = begin_for_stmt (NULL_TREE, NULL_TREE); 4424 for_stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
4321 finish_init_stmt (for_stmt); 4425 finish_init_stmt (for_stmt);
4322 finish_for_cond (build2 (GT_EXPR, boolean_type_node, iterator, 4426 finish_for_cond (build2 (GT_EXPR, boolean_type_node, iterator,
4323 build_int_cst (TREE_TYPE (iterator), -1)), 4427 build_int_cst (TREE_TYPE (iterator), -1)),
4324 for_stmt, false); 4428 for_stmt, false, 0);
4325 elt_init = cp_build_unary_op (PREDECREMENT_EXPR, iterator, false, 4429 elt_init = cp_build_unary_op (PREDECREMENT_EXPR, iterator, false,
4326 complain); 4430 complain);
4327 if (elt_init == error_mark_node) 4431 if (elt_init == error_mark_node)
4328 errors = true; 4432 errors = true;
4329 finish_for_expr (elt_init, for_stmt); 4433 finish_for_expr (elt_init, for_stmt);
4358 from = build_tree_list (NULL_TREE, from); 4462 from = build_tree_list (NULL_TREE, from);
4359 } 4463 }
4360 else 4464 else
4361 from = NULL_TREE; 4465 from = NULL_TREE;
4362 4466
4363 if (from_array == 2) 4467 if (TREE_CODE (type) == ARRAY_TYPE)
4468 elt_init = build_vec_init (to, NULL_TREE, from, /*val_init*/false,
4469 from_array, complain);
4470 else if (from_array == 2)
4364 elt_init = cp_build_modify_expr (input_location, to, NOP_EXPR, 4471 elt_init = cp_build_modify_expr (input_location, to, NOP_EXPR,
4365 from, complain); 4472 from, complain);
4366 else if (type_build_ctor_call (type)) 4473 else if (type_build_ctor_call (type))
4367 elt_init = build_aggr_init (to, from, 0, complain); 4474 elt_init = build_aggr_init (to, from, 0, complain);
4368 else if (from) 4475 else if (from)
4372 gcc_unreachable (); 4479 gcc_unreachable ();
4373 } 4480 }
4374 else if (TREE_CODE (type) == ARRAY_TYPE) 4481 else if (TREE_CODE (type) == ARRAY_TYPE)
4375 { 4482 {
4376 if (init && !BRACE_ENCLOSED_INITIALIZER_P (init)) 4483 if (init && !BRACE_ENCLOSED_INITIALIZER_P (init))
4377 sorry 4484 {
4378 ("cannot initialize multi-dimensional array with initializer"); 4485 if ((complain & tf_error))
4379 elt_init = build_vec_init (build1 (INDIRECT_REF, type, base), 4486 error_at (loc, "array must be initialized "
4380 0, init, 4487 "with a brace-enclosed initializer");
4381 explicit_value_init_p, 4488 elt_init = error_mark_node;
4382 0, complain); 4489 }
4490 else
4491 elt_init = build_vec_init (build1 (INDIRECT_REF, type, base),
4492 0, init,
4493 explicit_value_init_p,
4494 0, complain);
4383 } 4495 }
4384 else if (explicit_value_init_p) 4496 else if (explicit_value_init_p)
4385 { 4497 {
4386 elt_init = build_value_init (type, complain); 4498 elt_init = build_value_init (type, complain);
4387 if (elt_init != error_mark_node) 4499 if (elt_init != error_mark_node)
4395 else 4507 else
4396 { 4508 {
4397 if (TREE_CODE (init) == TREE_LIST) 4509 if (TREE_CODE (init) == TREE_LIST)
4398 init = build_x_compound_expr_from_list (init, ELK_INIT, 4510 init = build_x_compound_expr_from_list (init, ELK_INIT,
4399 complain); 4511 complain);
4400 elt_init = build2 (INIT_EXPR, type, to, init); 4512 elt_init = (init == error_mark_node
4513 ? error_mark_node
4514 : build2 (INIT_EXPR, type, to, init));
4401 } 4515 }
4402 } 4516 }
4403 4517
4404 if (elt_init == error_mark_node) 4518 if (elt_init == error_mark_node)
4405 errors = true; 4519 errors = true;
4435 } 4549 }
4436 } 4550 }
4437 } 4551 }
4438 4552
4439 current_stmt_tree ()->stmts_are_full_exprs_p = 1; 4553 current_stmt_tree ()->stmts_are_full_exprs_p = 1;
4440 if (elt_init) 4554 if (elt_init && !errors)
4441 finish_expr_stmt (elt_init); 4555 finish_expr_stmt (elt_init);
4442 current_stmt_tree ()->stmts_are_full_exprs_p = 0; 4556 current_stmt_tree ()->stmts_are_full_exprs_p = 0;
4443 4557
4444 finish_expr_stmt (cp_build_unary_op (PREINCREMENT_EXPR, base, false, 4558 finish_expr_stmt (cp_build_unary_op (PREINCREMENT_EXPR, base, false,
4445 complain)); 4559 complain));
4505 /* Now make the result have the correct type. */ 4619 /* Now make the result have the correct type. */
4506 if (TREE_CODE (atype) == ARRAY_TYPE) 4620 if (TREE_CODE (atype) == ARRAY_TYPE)
4507 { 4621 {
4508 atype = build_pointer_type (atype); 4622 atype = build_pointer_type (atype);
4509 stmt_expr = build1 (NOP_EXPR, atype, stmt_expr); 4623 stmt_expr = build1 (NOP_EXPR, atype, stmt_expr);
4510 stmt_expr = cp_build_indirect_ref (stmt_expr, RO_NULL, complain); 4624 stmt_expr = cp_build_fold_indirect_ref (stmt_expr);
4511 TREE_NO_WARNING (stmt_expr) = 1; 4625 TREE_NO_WARNING (stmt_expr) = 1;
4512 } 4626 }
4513 4627
4514 return stmt_expr; 4628 return stmt_expr;
4515 } 4629 }
4520 static tree 4634 static tree
4521 build_dtor_call (tree exp, special_function_kind dtor_kind, int flags, 4635 build_dtor_call (tree exp, special_function_kind dtor_kind, int flags,
4522 tsubst_flags_t complain) 4636 tsubst_flags_t complain)
4523 { 4637 {
4524 tree name; 4638 tree name;
4525 tree fn;
4526 switch (dtor_kind) 4639 switch (dtor_kind)
4527 { 4640 {
4528 case sfk_complete_destructor: 4641 case sfk_complete_destructor:
4529 name = complete_dtor_identifier; 4642 name = complete_dtor_identifier;
4530 break; 4643 break;
4538 break; 4651 break;
4539 4652
4540 default: 4653 default:
4541 gcc_unreachable (); 4654 gcc_unreachable ();
4542 } 4655 }
4543 fn = lookup_fnfields (TREE_TYPE (exp), name, /*protect=*/2); 4656
4544 return build_new_method_call (exp, fn, 4657 return build_special_member_call (exp, name,
4545 /*args=*/NULL, 4658 /*args=*/NULL,
4546 /*conversion_path=*/NULL_TREE, 4659 /*binfo=*/TREE_TYPE (exp),
4547 flags, 4660 flags,
4548 /*fn_p=*/NULL, 4661 complain);
4549 complain);
4550 } 4662 }
4551 4663
4552 /* Generate a call to a destructor. TYPE is the type to cast ADDR to. 4664 /* Generate a call to a destructor. TYPE is the type to cast ADDR to.
4553 ADDR is an expression which yields the store to be destroyed. 4665 ADDR is an expression which yields the store to be destroyed.
4554 AUTO_DELETE is the name of the destructor to call, i.e., either 4666 AUTO_DELETE is the name of the destructor to call, i.e., either
4572 /* Can happen when CURRENT_EXCEPTION_OBJECT gets its type 4684 /* Can happen when CURRENT_EXCEPTION_OBJECT gets its type
4573 set to `error_mark_node' before it gets properly cleaned up. */ 4685 set to `error_mark_node' before it gets properly cleaned up. */
4574 if (type == error_mark_node) 4686 if (type == error_mark_node)
4575 return error_mark_node; 4687 return error_mark_node;
4576 4688
4577 if (TREE_CODE (type) == POINTER_TYPE) 4689 if (TYPE_PTR_P (type))
4578 type = TYPE_MAIN_VARIANT (TREE_TYPE (type)); 4690 type = TYPE_MAIN_VARIANT (TREE_TYPE (type));
4579 4691
4580 if (TREE_CODE (type) == ARRAY_TYPE) 4692 if (TREE_CODE (type) == ARRAY_TYPE)
4581 { 4693 {
4582 if (TYPE_DOMAIN (type) == NULL_TREE) 4694 if (TYPE_DOMAIN (type) == NULL_TREE)
4586 return error_mark_node; 4698 return error_mark_node;
4587 } 4699 }
4588 return build_vec_delete (addr, array_type_nelts (type), 4700 return build_vec_delete (addr, array_type_nelts (type),
4589 auto_delete, use_global_delete, complain); 4701 auto_delete, use_global_delete, complain);
4590 } 4702 }
4703
4704 bool deleting = (auto_delete == sfk_deleting_destructor);
4705 gcc_assert (deleting == !(flags & LOOKUP_DESTRUCTOR));
4591 4706
4592 if (TYPE_PTR_P (otype)) 4707 if (TYPE_PTR_P (otype))
4593 { 4708 {
4594 addr = mark_rvalue_use (addr); 4709 addr = mark_rvalue_use (addr);
4595 4710
4602 if (!VOID_TYPE_P (type)) 4717 if (!VOID_TYPE_P (type))
4603 { 4718 {
4604 complete_type (type); 4719 complete_type (type);
4605 if (!COMPLETE_TYPE_P (type)) 4720 if (!COMPLETE_TYPE_P (type))
4606 { 4721 {
4607 if ((complain & tf_warning) 4722 if (complain & tf_warning)
4608 && warning (OPT_Wdelete_incomplete,
4609 "possible problem detected in invocation of "
4610 "delete operator:"))
4611 { 4723 {
4612 cxx_incomplete_type_diagnostic (addr, type, DK_WARNING); 4724 auto_diagnostic_group d;
4613 inform (input_location, 4725 if (warning (OPT_Wdelete_incomplete,
4614 "neither the destructor nor the class-specific " 4726 "possible problem detected in invocation of "
4615 "operator delete will be called, even if they are " 4727 "delete operator:"))
4616 "declared when the class is defined"); 4728 {
4729 cxx_incomplete_type_diagnostic (addr, type, DK_WARNING);
4730 inform (input_location,
4731 "neither the destructor nor the class-specific "
4732 "operator delete will be called, even if they "
4733 "are declared when the class is defined");
4734 }
4617 } 4735 }
4618 } 4736 }
4619 else if (auto_delete == sfk_deleting_destructor && warn_delnonvdtor 4737 else if (deleting && warn_delnonvdtor
4620 && MAYBE_CLASS_TYPE_P (type) && !CLASSTYPE_FINAL (type) 4738 && MAYBE_CLASS_TYPE_P (type) && !CLASSTYPE_FINAL (type)
4621 && TYPE_POLYMORPHIC_P (type)) 4739 && TYPE_POLYMORPHIC_P (type))
4622 { 4740 {
4623 tree dtor = CLASSTYPE_DESTRUCTOR (type); 4741 tree dtor = CLASSTYPE_DESTRUCTOR (type);
4624 if (!dtor || !DECL_VINDEX (dtor)) 4742 if (!dtor || !DECL_VINDEX (dtor))
4634 " which has non-virtual destructor" 4752 " which has non-virtual destructor"
4635 " might cause undefined behavior", type); 4753 " might cause undefined behavior", type);
4636 } 4754 }
4637 } 4755 }
4638 } 4756 }
4639 if (TREE_SIDE_EFFECTS (addr))
4640 addr = save_expr (addr);
4641 4757
4642 /* Throw away const and volatile on target type of addr. */ 4758 /* Throw away const and volatile on target type of addr. */
4643 addr = convert_force (build_pointer_type (type), addr, 0, complain); 4759 addr = convert_force (build_pointer_type (type), addr, 0, complain);
4644 } 4760 }
4645 else 4761 else
4648 destructor. If the destructor is accessible, call it, 4764 destructor. If the destructor is accessible, call it,
4649 else report error. */ 4765 else report error. */
4650 addr = cp_build_addr_expr (addr, complain); 4766 addr = cp_build_addr_expr (addr, complain);
4651 if (addr == error_mark_node) 4767 if (addr == error_mark_node)
4652 return error_mark_node; 4768 return error_mark_node;
4653 if (TREE_SIDE_EFFECTS (addr))
4654 addr = save_expr (addr);
4655 4769
4656 addr = convert_force (build_pointer_type (type), addr, 0, complain); 4770 addr = convert_force (build_pointer_type (type), addr, 0, complain);
4657 } 4771 }
4658 4772
4659 if (TYPE_HAS_TRIVIAL_DESTRUCTOR (type)) 4773 if (deleting)
4660 { 4774 /* We will use ADDR multiple times so we must save it. */
4661 /* Make sure the destructor is callable. */ 4775 addr = save_expr (addr);
4662 if (type_build_dtor_call (type)) 4776
4663 { 4777 bool virtual_p = false;
4664 expr = build_dtor_call (cp_build_indirect_ref (addr, RO_NULL, 4778 if (type_build_dtor_call (type))
4665 complain), 4779 {
4666 sfk_complete_destructor, flags, complain);
4667 if (expr == error_mark_node)
4668 return error_mark_node;
4669 }
4670
4671 if (auto_delete != sfk_deleting_destructor)
4672 return void_node;
4673
4674 return build_op_delete_call (DELETE_EXPR, addr,
4675 cxx_sizeof_nowarn (type),
4676 use_global_delete,
4677 /*placement=*/NULL_TREE,
4678 /*alloc_fn=*/NULL_TREE,
4679 complain);
4680 }
4681 else
4682 {
4683 tree head = NULL_TREE;
4684 tree do_delete = NULL_TREE;
4685 tree ifexp;
4686
4687 if (CLASSTYPE_LAZY_DESTRUCTOR (type)) 4780 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
4688 lazily_declare_fn (sfk_destructor, type); 4781 lazily_declare_fn (sfk_destructor, type);
4689 4782 virtual_p = DECL_VIRTUAL_P (CLASSTYPE_DESTRUCTOR (type));
4690 /* For `::delete x', we must not use the deleting destructor 4783 }
4691 since then we would not be sure to get the global `operator 4784
4692 delete'. */ 4785 tree head = NULL_TREE;
4693 if (use_global_delete && auto_delete == sfk_deleting_destructor) 4786 tree do_delete = NULL_TREE;
4694 { 4787
4695 /* We will use ADDR multiple times so we must save it. */ 4788 if (!deleting)
4696 addr = save_expr (addr); 4789 {
4697 head = get_target_expr (build_headof (addr)); 4790 /* Leave do_delete null. */
4698 /* Delete the object. */ 4791 }
4699 do_delete = build_op_delete_call (DELETE_EXPR, 4792 /* For `::delete x', we must not use the deleting destructor
4700 head, 4793 since then we would not be sure to get the global `operator
4701 cxx_sizeof_nowarn (type), 4794 delete'. */
4702 /*global_p=*/true, 4795 else if (use_global_delete)
4703 /*placement=*/NULL_TREE, 4796 {
4704 /*alloc_fn=*/NULL_TREE, 4797 head = get_target_expr (build_headof (addr));
4705 complain); 4798 /* Delete the object. */
4706 /* Otherwise, treat this like a complete object destructor 4799 do_delete = build_op_delete_call (DELETE_EXPR,
4707 call. */ 4800 head,
4708 auto_delete = sfk_complete_destructor; 4801 cxx_sizeof_nowarn (type),
4709 } 4802 /*global_p=*/true,
4710 /* If the destructor is non-virtual, there is no deleting 4803 /*placement=*/NULL_TREE,
4711 variant. Instead, we must explicitly call the appropriate 4804 /*alloc_fn=*/NULL_TREE,
4712 `operator delete' here. */ 4805 complain);
4713 else if (!DECL_VIRTUAL_P (CLASSTYPE_DESTRUCTOR (type)) 4806 /* Otherwise, treat this like a complete object destructor
4714 && auto_delete == sfk_deleting_destructor) 4807 call. */
4715 { 4808 auto_delete = sfk_complete_destructor;
4716 /* We will use ADDR multiple times so we must save it. */ 4809 }
4717 addr = save_expr (addr); 4810 /* If the destructor is non-virtual, there is no deleting
4718 /* Build the call. */ 4811 variant. Instead, we must explicitly call the appropriate
4719 do_delete = build_op_delete_call (DELETE_EXPR, 4812 `operator delete' here. */
4720 addr, 4813 else if (!virtual_p)
4721 cxx_sizeof_nowarn (type), 4814 {
4722 /*global_p=*/false, 4815 /* Build the call. */
4723 /*placement=*/NULL_TREE, 4816 do_delete = build_op_delete_call (DELETE_EXPR,
4724 /*alloc_fn=*/NULL_TREE, 4817 addr,
4725 complain); 4818 cxx_sizeof_nowarn (type),
4726 /* Call the complete object destructor. */ 4819 /*global_p=*/false,
4727 auto_delete = sfk_complete_destructor; 4820 /*placement=*/NULL_TREE,
4728 } 4821 /*alloc_fn=*/NULL_TREE,
4729 else if (auto_delete == sfk_deleting_destructor 4822 complain);
4730 && TYPE_GETS_REG_DELETE (type)) 4823 /* Call the complete object destructor. */
4731 { 4824 auto_delete = sfk_complete_destructor;
4732 /* Make sure we have access to the member op delete, even though 4825 }
4733 we'll actually be calling it from the destructor. */ 4826 else if (TYPE_GETS_REG_DELETE (type))
4734 build_op_delete_call (DELETE_EXPR, addr, cxx_sizeof_nowarn (type), 4827 {
4735 /*global_p=*/false, 4828 /* Make sure we have access to the member op delete, even though
4736 /*placement=*/NULL_TREE, 4829 we'll actually be calling it from the destructor. */
4737 /*alloc_fn=*/NULL_TREE, 4830 build_op_delete_call (DELETE_EXPR, addr, cxx_sizeof_nowarn (type),
4738 complain); 4831 /*global_p=*/false,
4739 } 4832 /*placement=*/NULL_TREE,
4740 4833 /*alloc_fn=*/NULL_TREE,
4741 expr = build_dtor_call (cp_build_indirect_ref (addr, RO_NULL, complain), 4834 complain);
4742 auto_delete, flags, complain); 4835 }
4743 if (expr == error_mark_node) 4836
4744 return error_mark_node; 4837 if (type_build_dtor_call (type))
4745 if (do_delete) 4838 expr = build_dtor_call (cp_build_fold_indirect_ref (addr),
4746 /* The delete operator must be called, regardless of whether 4839 auto_delete, flags, complain);
4747 the destructor throws. 4840 else
4748 4841 expr = build_trivial_dtor_call (addr);
4749 [expr.delete]/7 The deallocation function is called 4842 if (expr == error_mark_node)
4750 regardless of whether the destructor for the object or some 4843 return error_mark_node;
4751 element of the array throws an exception. */ 4844
4752 expr = build2 (TRY_FINALLY_EXPR, void_type_node, expr, do_delete); 4845 if (!deleting)
4753 4846 return expr;
4754 /* We need to calculate this before the dtor changes the vptr. */ 4847
4755 if (head) 4848 if (do_delete && !TREE_SIDE_EFFECTS (expr))
4756 expr = build2 (COMPOUND_EXPR, void_type_node, head, expr); 4849 expr = do_delete;
4757 4850 else if (do_delete)
4758 if (flags & LOOKUP_DESTRUCTOR) 4851 /* The delete operator must be called, regardless of whether
4759 /* Explicit destructor call; don't check for null pointer. */ 4852 the destructor throws.
4760 ifexp = integer_one_node; 4853
4761 else 4854 [expr.delete]/7 The deallocation function is called
4762 { 4855 regardless of whether the destructor for the object or some
4763 /* Handle deleting a null pointer. */ 4856 element of the array throws an exception. */
4764 warning_sentinel s (warn_address); 4857 expr = build2 (TRY_FINALLY_EXPR, void_type_node, expr, do_delete);
4765 ifexp = cp_build_binary_op (input_location, NE_EXPR, addr, 4858
4766 nullptr_node, complain); 4859 /* We need to calculate this before the dtor changes the vptr. */
4767 if (ifexp == error_mark_node) 4860 if (head)
4768 return error_mark_node; 4861 expr = build2 (COMPOUND_EXPR, void_type_node, head, expr);
4769 /* This is a compiler generated comparison, don't emit 4862
4770 e.g. -Wnonnull-compare warning for it. */ 4863 /* Handle deleting a null pointer. */
4771 else if (TREE_CODE (ifexp) == NE_EXPR) 4864 warning_sentinel s (warn_address);
4772 TREE_NO_WARNING (ifexp) = 1; 4865 tree ifexp = cp_build_binary_op (input_location, NE_EXPR, addr,
4773 } 4866 nullptr_node, complain);
4774 4867 ifexp = cp_fully_fold (ifexp);
4775 if (ifexp != integer_one_node) 4868
4776 expr = build3 (COND_EXPR, void_type_node, ifexp, expr, void_node); 4869 if (ifexp == error_mark_node)
4777 4870 return error_mark_node;
4778 return expr; 4871 /* This is a compiler generated comparison, don't emit
4779 } 4872 e.g. -Wnonnull-compare warning for it. */
4873 else if (TREE_CODE (ifexp) == NE_EXPR)
4874 TREE_NO_WARNING (ifexp) = 1;
4875
4876 if (!integer_nonzerop (ifexp))
4877 expr = build3 (COND_EXPR, void_type_node, ifexp, expr, void_node);
4878
4879 return expr;
4780 } 4880 }
4781 4881
4782 /* At the beginning of a destructor, push cleanups that will call the 4882 /* At the beginning of a destructor, push cleanups that will call the
4783 destructors for our base classes and members. 4883 destructors for our base classes and members.
4784 4884
4916 type = strip_array_types (TREE_TYPE (type)); 5016 type = strip_array_types (TREE_TYPE (type));
4917 cookie_addr = fold_build1_loc (input_location, NEGATE_EXPR, 5017 cookie_addr = fold_build1_loc (input_location, NEGATE_EXPR,
4918 sizetype, TYPE_SIZE_UNIT (sizetype)); 5018 sizetype, TYPE_SIZE_UNIT (sizetype));
4919 cookie_addr = fold_build_pointer_plus (fold_convert (size_ptr_type, base), 5019 cookie_addr = fold_build_pointer_plus (fold_convert (size_ptr_type, base),
4920 cookie_addr); 5020 cookie_addr);
4921 maxindex = cp_build_indirect_ref (cookie_addr, RO_NULL, complain); 5021 maxindex = cp_build_fold_indirect_ref (cookie_addr);
4922 } 5022 }
4923 else if (TREE_CODE (type) == ARRAY_TYPE) 5023 else if (TREE_CODE (type) == ARRAY_TYPE)
4924 { 5024 {
4925 /* Get the total number of things in the array, maxindex is a 5025 /* Get the total number of things in the array, maxindex is a
4926 bad name. */ 5026 bad name. */