Mercurial > hg > CbC > CbC_gcc
comparison gcc/cp/method.c @ 145:1830386684a0
gcc-9.2.0
author | anatofuz |
---|---|
date | Thu, 13 Feb 2020 11:34:05 +0900 |
parents | 84e7813d76e9 |
children |
comparison
equal
deleted
inserted
replaced
131:84e7813d76e9 | 145:1830386684a0 |
---|---|
1 /* Handle the hair of processing (but not expanding) inline functions. | 1 /* Handle the hair of processing (but not expanding) inline functions. |
2 Also manage function and variable name overloading. | 2 Also manage function and variable name overloading. |
3 Copyright (C) 1987-2018 Free Software Foundation, Inc. | 3 Copyright (C) 1987-2020 Free Software Foundation, Inc. |
4 Contributed by Michael Tiemann (tiemann@cygnus.com) | 4 Contributed by Michael Tiemann (tiemann@cygnus.com) |
5 | 5 |
6 This file is part of GCC. | 6 This file is part of GCC. |
7 | 7 |
8 GCC is free software; you can redistribute it and/or modify | 8 GCC is free software; you can redistribute it and/or modify |
28 #include "cp-tree.h" | 28 #include "cp-tree.h" |
29 #include "stringpool.h" | 29 #include "stringpool.h" |
30 #include "cgraph.h" | 30 #include "cgraph.h" |
31 #include "varasm.h" | 31 #include "varasm.h" |
32 #include "toplev.h" | 32 #include "toplev.h" |
33 #include "intl.h" | |
33 #include "common/common-target.h" | 34 #include "common/common-target.h" |
34 | 35 |
35 static void do_build_copy_assign (tree); | 36 static void do_build_copy_assign (tree); |
36 static void do_build_copy_constructor (tree); | 37 static void do_build_copy_constructor (tree); |
37 static tree make_alias_for_thunk (tree); | 38 static tree make_alias_for_thunk (tree); |
116 THUNK_ALIAS (thunk) = NULL_TREE; | 117 THUNK_ALIAS (thunk) = NULL_TREE; |
117 | 118 |
118 DECL_INTERFACE_KNOWN (thunk) = 1; | 119 DECL_INTERFACE_KNOWN (thunk) = 1; |
119 DECL_NOT_REALLY_EXTERN (thunk) = 1; | 120 DECL_NOT_REALLY_EXTERN (thunk) = 1; |
120 DECL_COMDAT (thunk) = DECL_COMDAT (function); | 121 DECL_COMDAT (thunk) = DECL_COMDAT (function); |
121 DECL_SAVED_FUNCTION_DATA (thunk) = NULL; | 122 DECL_SAVED_AUTO_RETURN_TYPE (thunk) = NULL; |
122 /* The thunk itself is not a constructor or destructor, even if | 123 /* The thunk itself is not a constructor or destructor, even if |
123 the thing it is thunking to is. */ | 124 the thing it is thunking to is. */ |
124 DECL_CXX_DESTRUCTOR_P (thunk) = 0; | 125 DECL_CXX_DESTRUCTOR_P (thunk) = 0; |
125 DECL_CXX_CONSTRUCTOR_P (thunk) = 0; | 126 DECL_CXX_CONSTRUCTOR_P (thunk) = 0; |
126 DECL_EXTERNAL (thunk) = 1; | 127 DECL_EXTERNAL (thunk) = 1; |
204 DECL_EXTERNAL (alias) = 0; | 205 DECL_EXTERNAL (alias) = 0; |
205 DECL_ARTIFICIAL (alias) = 1; | 206 DECL_ARTIFICIAL (alias) = 1; |
206 DECL_TEMPLATE_INSTANTIATED (alias) = 0; | 207 DECL_TEMPLATE_INSTANTIATED (alias) = 0; |
207 if (TREE_CODE (alias) == FUNCTION_DECL) | 208 if (TREE_CODE (alias) == FUNCTION_DECL) |
208 { | 209 { |
209 DECL_SAVED_FUNCTION_DATA (alias) = NULL; | 210 DECL_SAVED_AUTO_RETURN_TYPE (alias) = NULL; |
210 DECL_CXX_DESTRUCTOR_P (alias) = 0; | 211 DECL_CXX_DESTRUCTOR_P (alias) = 0; |
211 DECL_CXX_CONSTRUCTOR_P (alias) = 0; | 212 DECL_CXX_CONSTRUCTOR_P (alias) = 0; |
212 DECL_PENDING_INLINE_P (alias) = 0; | 213 DECL_PENDING_INLINE_P (alias) = 0; |
213 DECL_DECLARED_INLINE_P (alias) = 0; | 214 DECL_DECLARED_INLINE_P (alias) = 0; |
214 DECL_INITIAL (alias) = error_mark_node; | 215 DECL_INITIAL (alias) = error_mark_node; |
400 case sfk_copy_assignment: | 401 case sfk_copy_assignment: |
401 return !TYPE_HAS_COMPLEX_COPY_ASSIGN (ctype); | 402 return !TYPE_HAS_COMPLEX_COPY_ASSIGN (ctype); |
402 case sfk_move_assignment: | 403 case sfk_move_assignment: |
403 return !TYPE_HAS_COMPLEX_MOVE_ASSIGN (ctype); | 404 return !TYPE_HAS_COMPLEX_MOVE_ASSIGN (ctype); |
404 case sfk_destructor: | 405 case sfk_destructor: |
406 case sfk_virtual_destructor: | |
405 return !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (ctype); | 407 return !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (ctype); |
406 case sfk_inheriting_constructor: | 408 case sfk_inheriting_constructor: |
409 case sfk_comparison: | |
407 return false; | 410 return false; |
408 default: | 411 default: |
409 gcc_unreachable (); | 412 gcc_unreachable (); |
410 } | 413 } |
411 } | 414 } |
469 if (DECL_PACK_P (parm)) | 472 if (DECL_PACK_P (parm)) |
470 type = PACK_EXPANSION_PATTERN (type); | 473 type = PACK_EXPANSION_PATTERN (type); |
471 if (!TYPE_REF_P (type)) | 474 if (!TYPE_REF_P (type)) |
472 type = cp_build_reference_type (type, /*rval=*/true); | 475 type = cp_build_reference_type (type, /*rval=*/true); |
473 warning_sentinel w (warn_useless_cast); | 476 warning_sentinel w (warn_useless_cast); |
474 exp = build_static_cast (type, exp, tf_warning_or_error); | 477 exp = build_static_cast (input_location, type, exp, |
478 tf_warning_or_error); | |
475 if (DECL_PACK_P (parm)) | 479 if (DECL_PACK_P (parm)) |
476 exp = make_pack_expansion (exp); | 480 exp = make_pack_expansion (exp); |
477 return exp; | 481 return exp; |
478 } | 482 } |
479 | 483 |
672 finish_expr_stmt (t); | 676 finish_expr_stmt (t); |
673 } | 677 } |
674 } | 678 } |
675 else | 679 else |
676 { | 680 { |
677 tree fields = TYPE_FIELDS (current_class_type); | |
678 tree member_init_list = NULL_TREE; | 681 tree member_init_list = NULL_TREE; |
679 int cvquals = cp_type_quals (TREE_TYPE (parm)); | |
680 int i; | 682 int i; |
681 tree binfo, base_binfo; | 683 tree binfo, base_binfo; |
682 tree init; | |
683 vec<tree, va_gc> *vbases; | 684 vec<tree, va_gc> *vbases; |
684 | 685 |
685 /* Initialize all the base-classes with the parameter converted | 686 /* Initialize all the base-classes with the parameter converted |
686 to their type so that we get their copy constructor and not | 687 to their type so that we get their copy constructor and not |
687 another constructor that takes current_class_type. We must | 688 another constructor that takes current_class_type. We must |
701 continue; | 702 continue; |
702 member_init_list = add_one_base_init (base_binfo, parm, move_p, | 703 member_init_list = add_one_base_init (base_binfo, parm, move_p, |
703 inh, member_init_list); | 704 inh, member_init_list); |
704 } | 705 } |
705 | 706 |
706 for (; fields; fields = DECL_CHAIN (fields)) | 707 if (!inh) |
707 { | 708 { |
708 tree field = fields; | 709 int cvquals = cp_type_quals (TREE_TYPE (parm)); |
709 tree expr_type; | 710 |
710 | 711 for (tree fields = TYPE_FIELDS (current_class_type); |
711 if (TREE_CODE (field) != FIELD_DECL) | 712 fields; fields = DECL_CHAIN (fields)) |
712 continue; | |
713 if (inh) | |
714 continue; | |
715 | |
716 expr_type = TREE_TYPE (field); | |
717 if (DECL_NAME (field)) | |
718 { | 713 { |
719 if (VFIELD_NAME_P (DECL_NAME (field))) | 714 tree field = fields; |
715 tree expr_type; | |
716 | |
717 if (TREE_CODE (field) != FIELD_DECL) | |
720 continue; | 718 continue; |
719 | |
720 expr_type = TREE_TYPE (field); | |
721 if (DECL_NAME (field)) | |
722 { | |
723 if (VFIELD_NAME_P (DECL_NAME (field))) | |
724 continue; | |
725 } | |
726 else if (ANON_AGGR_TYPE_P (expr_type) && TYPE_FIELDS (expr_type)) | |
727 /* Just use the field; anonymous types can't have | |
728 nontrivial copy ctors or assignment ops or this | |
729 function would be deleted. */; | |
730 else | |
731 continue; | |
732 | |
733 /* Compute the type of "init->field". If the copy-constructor | |
734 parameter is, for example, "const S&", and the type of | |
735 the field is "T", then the type will usually be "const | |
736 T". (There are no cv-qualified variants of reference | |
737 types.) */ | |
738 if (!TYPE_REF_P (expr_type)) | |
739 { | |
740 int quals = cvquals; | |
741 | |
742 if (DECL_MUTABLE_P (field)) | |
743 quals &= ~TYPE_QUAL_CONST; | |
744 quals |= cp_type_quals (expr_type); | |
745 expr_type = cp_build_qualified_type (expr_type, quals); | |
746 } | |
747 | |
748 tree init = build3 (COMPONENT_REF, expr_type, parm, field, NULL_TREE); | |
749 if (move_p && !TYPE_REF_P (expr_type) | |
750 /* 'move' breaks bit-fields, and has no effect for scalars. */ | |
751 && !scalarish_type_p (expr_type)) | |
752 init = move (init); | |
753 init = build_tree_list (NULL_TREE, init); | |
754 | |
755 member_init_list = tree_cons (field, init, member_init_list); | |
721 } | 756 } |
722 else if (ANON_AGGR_TYPE_P (expr_type) && TYPE_FIELDS (expr_type)) | 757 } |
723 /* Just use the field; anonymous types can't have | 758 |
724 nontrivial copy ctors or assignment ops or this | |
725 function would be deleted. */; | |
726 else | |
727 continue; | |
728 | |
729 /* Compute the type of "init->field". If the copy-constructor | |
730 parameter is, for example, "const S&", and the type of | |
731 the field is "T", then the type will usually be "const | |
732 T". (There are no cv-qualified variants of reference | |
733 types.) */ | |
734 if (!TYPE_REF_P (expr_type)) | |
735 { | |
736 int quals = cvquals; | |
737 | |
738 if (DECL_MUTABLE_P (field)) | |
739 quals &= ~TYPE_QUAL_CONST; | |
740 quals |= cp_type_quals (expr_type); | |
741 expr_type = cp_build_qualified_type (expr_type, quals); | |
742 } | |
743 | |
744 init = build3 (COMPONENT_REF, expr_type, parm, field, NULL_TREE); | |
745 if (move_p && !TYPE_REF_P (expr_type) | |
746 /* 'move' breaks bit-fields, and has no effect for scalars. */ | |
747 && !scalarish_type_p (expr_type)) | |
748 init = move (init); | |
749 init = build_tree_list (NULL_TREE, init); | |
750 | |
751 member_init_list = tree_cons (field, init, member_init_list); | |
752 } | |
753 finish_mem_initializers (member_init_list); | 759 finish_mem_initializers (member_init_list); |
754 } | 760 } |
755 } | 761 } |
756 | 762 |
757 static void | 763 static void |
785 /* Assign to each of the direct base classes. */ | 791 /* Assign to each of the direct base classes. */ |
786 for (binfo = TYPE_BINFO (current_class_type), i = 0; | 792 for (binfo = TYPE_BINFO (current_class_type), i = 0; |
787 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++) | 793 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++) |
788 { | 794 { |
789 tree converted_parm; | 795 tree converted_parm; |
790 vec<tree, va_gc> *parmvec; | |
791 | 796 |
792 /* We must convert PARM directly to the base class | 797 /* We must convert PARM directly to the base class |
793 explicitly since the base class may be ambiguous. */ | 798 explicitly since the base class may be ambiguous. */ |
794 converted_parm = build_base_path (PLUS_EXPR, parm, base_binfo, 1, | 799 converted_parm = build_base_path (PLUS_EXPR, parm, base_binfo, 1, |
795 tf_warning_or_error); | 800 tf_warning_or_error); |
796 if (move_p) | 801 if (move_p) |
797 converted_parm = move (converted_parm); | 802 converted_parm = move (converted_parm); |
798 /* Call the base class assignment operator. */ | 803 /* Call the base class assignment operator. */ |
799 parmvec = make_tree_vector_single (converted_parm); | 804 releasing_vec parmvec (make_tree_vector_single (converted_parm)); |
800 finish_expr_stmt | 805 finish_expr_stmt |
801 (build_special_member_call (current_class_ref, | 806 (build_special_member_call (current_class_ref, |
802 assign_op_identifier, | 807 assign_op_identifier, |
803 &parmvec, | 808 &parmvec, |
804 base_binfo, | 809 base_binfo, |
805 flags, | 810 flags, |
806 tf_warning_or_error)); | 811 tf_warning_or_error)); |
807 release_tree_vector (parmvec); | |
808 } | 812 } |
809 | 813 |
810 /* Assign to each of the non-static data members. */ | 814 /* Assign to each of the non-static data members. */ |
811 for (fields = TYPE_FIELDS (current_class_type); | 815 for (fields = TYPE_FIELDS (current_class_type); |
812 fields; | 816 fields; |
823 | 827 |
824 expr_type = TREE_TYPE (field); | 828 expr_type = TREE_TYPE (field); |
825 | 829 |
826 if (CP_TYPE_CONST_P (expr_type)) | 830 if (CP_TYPE_CONST_P (expr_type)) |
827 { | 831 { |
828 error ("non-static const member %q#D, can%'t use default " | 832 error ("non-static const member %q#D, cannot use default " |
829 "assignment operator", field); | 833 "assignment operator", field); |
830 continue; | 834 continue; |
831 } | 835 } |
832 else if (TYPE_REF_P (expr_type)) | 836 else if (TYPE_REF_P (expr_type)) |
833 { | 837 { |
834 error ("non-static reference member %q#D, can%'t use " | 838 error ("non-static reference member %q#D, cannot use " |
835 "default assignment operator", field); | 839 "default assignment operator", field); |
836 continue; | 840 continue; |
837 } | 841 } |
838 | 842 |
839 if (DECL_NAME (field)) | 843 if (DECL_NAME (field)) |
873 } | 877 } |
874 finish_return_stmt (current_class_ref); | 878 finish_return_stmt (current_class_ref); |
875 finish_compound_stmt (compound_stmt); | 879 finish_compound_stmt (compound_stmt); |
876 } | 880 } |
877 | 881 |
882 /* C++20 <compare> comparison category types. */ | |
883 | |
884 enum comp_cat_tag | |
885 { | |
886 cc_partial_ordering, | |
887 cc_weak_ordering, | |
888 cc_strong_ordering, | |
889 cc_last | |
890 }; | |
891 | |
892 /* Names of the comparison categories and their value members, to be indexed by | |
893 comp_cat_tag enumerators. genericize_spaceship below relies on the ordering | |
894 of the members. */ | |
895 | |
896 struct comp_cat_info_t | |
897 { | |
898 const char *name; | |
899 const char *members[4]; | |
900 }; | |
901 static const comp_cat_info_t comp_cat_info[cc_last] | |
902 = { | |
903 { "partial_ordering", "equivalent", "greater", "less", "unordered" }, | |
904 { "weak_ordering", "equivalent", "greater", "less" }, | |
905 { "strong_ordering", "equal", "greater", "less" } | |
906 }; | |
907 | |
908 /* A cache of the category types to speed repeated lookups. */ | |
909 | |
910 static GTY((deletable)) tree comp_cat_cache[cc_last]; | |
911 | |
912 /* Look up one of the result variables in the comparison category type. */ | |
913 | |
914 static tree | |
915 lookup_comparison_result (tree type, const char *name_str, | |
916 tsubst_flags_t complain = tf_warning_or_error) | |
917 { | |
918 tree name = get_identifier (name_str); | |
919 tree decl = lookup_qualified_name (type, name); | |
920 if (TREE_CODE (decl) != VAR_DECL) | |
921 { | |
922 if (complain & tf_error) | |
923 { | |
924 auto_diagnostic_group d; | |
925 if (decl == error_mark_node || TREE_CODE (decl) == TREE_LIST) | |
926 qualified_name_lookup_error (type, name, decl, input_location); | |
927 else | |
928 error ("%qD is not a static data member", decl); | |
929 inform (input_location, "determining value of %qs", "operator<=>"); | |
930 } | |
931 return error_mark_node; | |
932 } | |
933 return decl; | |
934 } | |
935 | |
936 /* Look up a <compare> comparison category type in std. */ | |
937 | |
938 static tree | |
939 lookup_comparison_category (comp_cat_tag tag, | |
940 tsubst_flags_t complain = tf_warning_or_error) | |
941 { | |
942 if (tree cached = comp_cat_cache[tag]) | |
943 return cached; | |
944 | |
945 tree name = get_identifier (comp_cat_info[tag].name); | |
946 tree decl = lookup_qualified_name (std_node, name); | |
947 if (TREE_CODE (decl) != TYPE_DECL) | |
948 { | |
949 if (complain & tf_error) | |
950 { | |
951 auto_diagnostic_group d; | |
952 if (decl == error_mark_node || TREE_CODE (decl) == TREE_LIST) | |
953 qualified_name_lookup_error (std_node, name, decl, input_location); | |
954 else | |
955 error ("%qD is not a type", decl); | |
956 inform (input_location, "forming type of %qs", "operator<=>"); | |
957 } | |
958 return error_mark_node; | |
959 } | |
960 /* Also make sure we can look up the value members now, since we won't | |
961 really use them until genericize time. */ | |
962 tree type = TREE_TYPE (decl); | |
963 for (int i = 0; i < 4; ++i) | |
964 { | |
965 const char *p = comp_cat_info[tag].members[i]; | |
966 if (!p) break; | |
967 if (lookup_comparison_result (type, p, complain) | |
968 == error_mark_node) | |
969 return error_mark_node; | |
970 } | |
971 return comp_cat_cache[tag] = type; | |
972 } | |
973 | |
974 /* Wrapper that takes the tag rather than the type. */ | |
975 | |
976 static tree | |
977 lookup_comparison_result (comp_cat_tag tag, const char *name_str, | |
978 tsubst_flags_t complain = tf_warning_or_error) | |
979 { | |
980 tree type = lookup_comparison_category (tag, complain); | |
981 return lookup_comparison_result (type, name_str, complain); | |
982 } | |
983 | |
984 /* Wrapper that takes the index into the members array instead of the name. */ | |
985 | |
986 static tree | |
987 lookup_comparison_result (comp_cat_tag tag, tree type, int idx) | |
988 { | |
989 const char *name_str = comp_cat_info[tag].members[idx]; | |
990 if (!name_str) | |
991 return NULL_TREE; | |
992 return lookup_comparison_result (type, name_str); | |
993 } | |
994 | |
995 /* Does TYPE correspond to TAG? */ | |
996 | |
997 static bool | |
998 is_cat (tree type, comp_cat_tag tag) | |
999 { | |
1000 tree name = TYPE_LINKAGE_IDENTIFIER (type); | |
1001 return id_equal (name, comp_cat_info[tag].name); | |
1002 } | |
1003 | |
1004 /* Return the comp_cat_tag for TYPE. */ | |
1005 | |
1006 static comp_cat_tag | |
1007 cat_tag_for (tree type) | |
1008 { | |
1009 for (int i = 0; i < cc_last; ++i) | |
1010 { | |
1011 comp_cat_tag tag = (comp_cat_tag)i; | |
1012 if (is_cat (type, tag)) | |
1013 return tag; | |
1014 } | |
1015 return cc_last; | |
1016 } | |
1017 | |
1018 /* Return the comparison category tag of a <=> expression with non-class type | |
1019 OPTYPE. */ | |
1020 | |
1021 static comp_cat_tag | |
1022 spaceship_comp_cat (tree optype) | |
1023 { | |
1024 if (INTEGRAL_OR_ENUMERATION_TYPE_P (optype) || TYPE_PTROBV_P (optype)) | |
1025 return cc_strong_ordering; | |
1026 else if (TREE_CODE (optype) == REAL_TYPE) | |
1027 return cc_partial_ordering; | |
1028 | |
1029 /* ??? should vector <=> produce a vector of one of the above? */ | |
1030 gcc_unreachable (); | |
1031 } | |
1032 | |
1033 /* Return the comparison category type of a <=> expression with non-class type | |
1034 OPTYPE. */ | |
1035 | |
1036 tree | |
1037 spaceship_type (tree optype, tsubst_flags_t complain) | |
1038 { | |
1039 comp_cat_tag tag = spaceship_comp_cat (optype); | |
1040 return lookup_comparison_category (tag, complain); | |
1041 } | |
1042 | |
1043 /* Turn <=> with type TYPE and operands OP0 and OP1 into GENERIC. */ | |
1044 | |
1045 tree | |
1046 genericize_spaceship (tree type, tree op0, tree op1) | |
1047 { | |
1048 /* ??? maybe optimize based on knowledge of representation? */ | |
1049 comp_cat_tag tag = cat_tag_for (type); | |
1050 gcc_checking_assert (tag < cc_last); | |
1051 | |
1052 tree r; | |
1053 op0 = save_expr (op0); | |
1054 op1 = save_expr (op1); | |
1055 | |
1056 tree gt = lookup_comparison_result (tag, type, 1); | |
1057 | |
1058 if (tag == cc_partial_ordering) | |
1059 { | |
1060 /* op0 == op1 ? equivalent : op0 < op1 ? less : | |
1061 op0 > op1 ? greater : unordered */ | |
1062 tree uo = lookup_comparison_result (tag, type, 3); | |
1063 tree comp = fold_build2 (GT_EXPR, boolean_type_node, op0, op1); | |
1064 r = fold_build3 (COND_EXPR, type, comp, gt, uo); | |
1065 } | |
1066 else | |
1067 /* op0 == op1 ? equal : op0 < op1 ? less : greater */ | |
1068 r = gt; | |
1069 | |
1070 tree lt = lookup_comparison_result (tag, type, 2); | |
1071 tree comp = fold_build2 (LT_EXPR, boolean_type_node, op0, op1); | |
1072 r = fold_build3 (COND_EXPR, type, comp, lt, r); | |
1073 | |
1074 tree eq = lookup_comparison_result (tag, type, 0); | |
1075 comp = fold_build2 (EQ_EXPR, boolean_type_node, op0, op1); | |
1076 r = fold_build3 (COND_EXPR, type, comp, eq, r); | |
1077 | |
1078 /* Wrap the whole thing in a TARGET_EXPR like build_conditional_expr_1. */ | |
1079 r = get_target_expr (r); | |
1080 | |
1081 return r; | |
1082 } | |
1083 | |
1084 /* Check that the signature of a defaulted comparison operator is | |
1085 well-formed. */ | |
1086 | |
1087 static bool | |
1088 early_check_defaulted_comparison (tree fn) | |
1089 { | |
1090 location_t loc = DECL_SOURCE_LOCATION (fn); | |
1091 tree ctx; | |
1092 if (DECL_CLASS_SCOPE_P (fn)) | |
1093 ctx = DECL_CONTEXT (fn); | |
1094 else | |
1095 ctx = DECL_FRIEND_CONTEXT (fn); | |
1096 bool ok = true; | |
1097 | |
1098 if (cxx_dialect < cxx2a) | |
1099 { | |
1100 error_at (loc, "defaulted %qD only available with %<-std=c++2a%> or " | |
1101 "%<-std=gnu++2a%>", fn); | |
1102 return false; | |
1103 } | |
1104 | |
1105 if (!DECL_OVERLOADED_OPERATOR_IS (fn, SPACESHIP_EXPR) | |
1106 && !same_type_p (TREE_TYPE (TREE_TYPE (fn)), boolean_type_node)) | |
1107 { | |
1108 diagnostic_t kind = DK_UNSPECIFIED; | |
1109 int opt = 0; | |
1110 if (is_auto (TREE_TYPE (fn))) | |
1111 kind = DK_PEDWARN; | |
1112 else | |
1113 kind = DK_ERROR; | |
1114 emit_diagnostic (kind, loc, opt, | |
1115 "defaulted %qD must return %<bool%>", fn); | |
1116 if (kind == DK_ERROR) | |
1117 ok = false; | |
1118 } | |
1119 | |
1120 bool mem = DECL_NONSTATIC_MEMBER_FUNCTION_P (fn); | |
1121 if (mem && type_memfn_quals (TREE_TYPE (fn)) != TYPE_QUAL_CONST) | |
1122 { | |
1123 error_at (loc, "defaulted %qD must be %<const%>", fn); | |
1124 ok = false; | |
1125 } | |
1126 tree parmnode = FUNCTION_FIRST_USER_PARMTYPE (fn); | |
1127 bool saw_byval = false; | |
1128 bool saw_byref = mem; | |
1129 bool saw_bad = false; | |
1130 for (; parmnode != void_list_node; parmnode = TREE_CHAIN (parmnode)) | |
1131 { | |
1132 tree parmtype = TREE_VALUE (parmnode); | |
1133 if (same_type_p (parmtype, ctx)) | |
1134 saw_byval = true; | |
1135 else if (TREE_CODE (parmtype) != REFERENCE_TYPE | |
1136 || TYPE_QUALS (TREE_TYPE (parmtype)) != TYPE_QUAL_CONST | |
1137 || !(same_type_ignoring_top_level_qualifiers_p | |
1138 (TREE_TYPE (parmtype), ctx))) | |
1139 saw_bad = true; | |
1140 else | |
1141 saw_byref = true; | |
1142 } | |
1143 | |
1144 if (saw_bad || (saw_byval && saw_byref)) | |
1145 { | |
1146 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)) | |
1147 error_at (loc, "defaulted member %qD must have parameter type " | |
1148 "%<const %T&%>", fn, ctx); | |
1149 else if (saw_bad) | |
1150 error_at (loc, "defaulted %qD must have parameters of either type " | |
1151 "%<const %T&%> or %qT", fn, ctx, ctx); | |
1152 else | |
1153 error_at (loc, "defaulted %qD must have parameters of either type " | |
1154 "%<const %T&%> or %qT, not both", fn, ctx, ctx); | |
1155 ok = false; | |
1156 } | |
1157 | |
1158 /* We still need to deduce deleted/constexpr/noexcept and maybe return. */ | |
1159 DECL_MAYBE_DELETED (fn) = ok; | |
1160 | |
1161 return ok; | |
1162 } | |
1163 | |
1164 /* Subroutine of build_comparison_op. Given the vec of memberwise | |
1165 comparisons COMPS, calculate the overall comparison category for | |
1166 operator<=>. */ | |
1167 | |
1168 static tree | |
1169 common_comparison_type (vec<tree> &comps) | |
1170 { | |
1171 tree seen[cc_last] = {}; | |
1172 | |
1173 for (unsigned i = 0; i < comps.length(); ++i) | |
1174 { | |
1175 tree comp = comps[i]; | |
1176 tree ctype = TREE_TYPE (comp); | |
1177 comp_cat_tag tag = cat_tag_for (ctype); | |
1178 if (tag < cc_last) | |
1179 seen[tag] = ctype; | |
1180 else | |
1181 /* If any Ti is not a comparison category type, U is void. */ | |
1182 return void_type_node; | |
1183 } | |
1184 | |
1185 /* Otherwise, if at least one T i is std::partial_ordering, U is | |
1186 std::partial_ordering. */ | |
1187 if (tree t = seen[cc_partial_ordering]) return t; | |
1188 | |
1189 /* Otherwise, if at least one T i is std::weak_ordering, U is | |
1190 std::weak_ordering. */ | |
1191 if (tree t = seen[cc_weak_ordering]) return t; | |
1192 | |
1193 /* Otherwise, U is std::strong_ordering. */ | |
1194 if (tree t = seen[cc_strong_ordering]) return t; | |
1195 return lookup_comparison_category (cc_strong_ordering); | |
1196 } | |
1197 | |
1198 /* Data structure for build_comparison_op. */ | |
1199 | |
1200 struct comp_info | |
1201 { | |
1202 tree fndecl; | |
1203 location_t loc; | |
1204 bool defining; | |
1205 bool first_time; | |
1206 bool constexp; | |
1207 bool was_constexp; | |
1208 bool noex; | |
1209 | |
1210 comp_info (tree fndecl, tsubst_flags_t &complain) | |
1211 : fndecl (fndecl) | |
1212 { | |
1213 loc = DECL_SOURCE_LOCATION (fndecl); | |
1214 | |
1215 /* We only have tf_error set when we're called from | |
1216 explain_invalid_constexpr_fn or maybe_explain_implicit_delete. */ | |
1217 defining = !(complain & tf_error); | |
1218 | |
1219 first_time = DECL_MAYBE_DELETED (fndecl); | |
1220 DECL_MAYBE_DELETED (fndecl) = false; | |
1221 | |
1222 /* Do we want to try to set constexpr? */ | |
1223 was_constexp = DECL_DECLARED_CONSTEXPR_P (fndecl); | |
1224 constexp = first_time; | |
1225 if (constexp) | |
1226 /* Set this for var_in_constexpr_fn. */ | |
1227 DECL_DECLARED_CONSTEXPR_P (fndecl) = true; | |
1228 | |
1229 /* Do we want to try to set noexcept? */ | |
1230 noex = first_time; | |
1231 if (noex) | |
1232 { | |
1233 tree raises = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fndecl)); | |
1234 if (raises && !UNEVALUATED_NOEXCEPT_SPEC_P (raises)) | |
1235 /* There was an explicit exception-specification. */ | |
1236 noex = false; | |
1237 } | |
1238 } | |
1239 | |
1240 /* EXPR is an expression built as part of the function body. | |
1241 Adjust the properties appropriately. */ | |
1242 void check (tree expr) | |
1243 { | |
1244 if (expr == error_mark_node) | |
1245 DECL_DELETED_FN (fndecl) = true; | |
1246 if ((constexp || was_constexp) | |
1247 && !potential_rvalue_constant_expression (expr)) | |
1248 { | |
1249 if (was_constexp) | |
1250 require_potential_rvalue_constant_expression (expr); | |
1251 else | |
1252 constexp = false; | |
1253 } | |
1254 if (noex && !expr_noexcept_p (expr, tf_none)) | |
1255 noex = false; | |
1256 } | |
1257 | |
1258 ~comp_info () | |
1259 { | |
1260 if (first_time) | |
1261 { | |
1262 DECL_DECLARED_CONSTEXPR_P (fndecl) = constexp || was_constexp; | |
1263 tree raises = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fndecl)); | |
1264 if (!raises || UNEVALUATED_NOEXCEPT_SPEC_P (raises)) | |
1265 { | |
1266 raises = noex ? noexcept_true_spec : noexcept_false_spec; | |
1267 TREE_TYPE (fndecl) = build_exception_variant (TREE_TYPE (fndecl), | |
1268 raises); | |
1269 } | |
1270 } | |
1271 } | |
1272 }; | |
1273 | |
1274 /* Build up the definition of a defaulted comparison operator. Unlike other | |
1275 defaulted functions that use synthesized_method_walk to determine whether | |
1276 the function is e.g. deleted, for comparisons we use the same code. We try | |
1277 to use synthesize_method at the earliest opportunity and bail out if the | |
1278 function ends up being deleted. */ | |
1279 | |
1280 static void | |
1281 build_comparison_op (tree fndecl, tsubst_flags_t complain) | |
1282 { | |
1283 comp_info info (fndecl, complain); | |
1284 | |
1285 if (!info.defining && !(complain & tf_error) && !DECL_MAYBE_DELETED (fndecl)) | |
1286 return; | |
1287 | |
1288 int flags = LOOKUP_NORMAL | LOOKUP_NONVIRTUAL | LOOKUP_DEFAULTED; | |
1289 const ovl_op_info_t *op = IDENTIFIER_OVL_OP_INFO (DECL_NAME (fndecl)); | |
1290 tree_code code = op->tree_code; | |
1291 | |
1292 tree lhs = DECL_ARGUMENTS (fndecl); | |
1293 tree rhs = DECL_CHAIN (lhs); | |
1294 if (is_this_parameter (lhs)) | |
1295 lhs = cp_build_fold_indirect_ref (lhs); | |
1296 else | |
1297 lhs = convert_from_reference (lhs); | |
1298 rhs = convert_from_reference (rhs); | |
1299 tree ctype = TYPE_MAIN_VARIANT (TREE_TYPE (lhs)); | |
1300 | |
1301 iloc_sentinel ils (info.loc); | |
1302 | |
1303 /* A defaulted comparison operator function for class C is defined as | |
1304 deleted if ... C is a union-like class. */ | |
1305 if (TREE_CODE (ctype) == UNION_TYPE) | |
1306 { | |
1307 if (complain & tf_error) | |
1308 inform (info.loc, "cannot default compare union %qT", ctype); | |
1309 DECL_DELETED_FN (fndecl) = true; | |
1310 return; | |
1311 } | |
1312 | |
1313 tree compound_stmt = NULL_TREE; | |
1314 if (info.defining) | |
1315 compound_stmt = begin_compound_stmt (0); | |
1316 else | |
1317 ++cp_unevaluated_operand; | |
1318 | |
1319 tree rettype = TREE_TYPE (TREE_TYPE (fndecl)); | |
1320 if (code != SPACESHIP_EXPR && is_auto (rettype)) | |
1321 { | |
1322 rettype = boolean_type_node; | |
1323 apply_deduced_return_type (fndecl, rettype); | |
1324 } | |
1325 | |
1326 if (code == EQ_EXPR || code == SPACESHIP_EXPR) | |
1327 { | |
1328 auto_vec<tree> comps; | |
1329 | |
1330 /* Compare each of the subobjects. Note that we get bases from | |
1331 next_initializable_field because we're past C++17. */ | |
1332 for (tree field = next_initializable_field (TYPE_FIELDS (ctype)); | |
1333 field; | |
1334 field = next_initializable_field (DECL_CHAIN (field))) | |
1335 { | |
1336 tree expr_type = TREE_TYPE (field); | |
1337 | |
1338 /* A defaulted comparison operator function for class C is defined as | |
1339 deleted if any non-static data member of C is of reference type or | |
1340 C is a union-like class. */ | |
1341 if (TREE_CODE (expr_type) == REFERENCE_TYPE) | |
1342 { | |
1343 if (complain & tf_error) | |
1344 inform (DECL_SOURCE_LOCATION (field), "cannot default compare " | |
1345 "reference member %qD", field); | |
1346 DECL_DELETED_FN (fndecl) = true; | |
1347 continue; | |
1348 } | |
1349 else if (ANON_UNION_TYPE_P (expr_type)) | |
1350 { | |
1351 if (complain & tf_error) | |
1352 inform (DECL_SOURCE_LOCATION (field), "cannot default compare " | |
1353 "anonymous union member"); | |
1354 DECL_DELETED_FN (fndecl) = true; | |
1355 continue; | |
1356 } | |
1357 | |
1358 tree lhs_mem = build3 (COMPONENT_REF, expr_type, lhs, field, | |
1359 NULL_TREE); | |
1360 tree rhs_mem = build3 (COMPONENT_REF, expr_type, rhs, field, | |
1361 NULL_TREE); | |
1362 tree comp = build_new_op (info.loc, code, flags, lhs_mem, rhs_mem, | |
1363 NULL_TREE, NULL, complain); | |
1364 if (comp == error_mark_node) | |
1365 { | |
1366 DECL_DELETED_FN (fndecl) = true; | |
1367 continue; | |
1368 } | |
1369 comps.safe_push (comp); | |
1370 } | |
1371 if (code == SPACESHIP_EXPR && is_auto (rettype)) | |
1372 { | |
1373 rettype = common_comparison_type (comps); | |
1374 apply_deduced_return_type (fndecl, rettype); | |
1375 } | |
1376 for (unsigned i = 0; i < comps.length(); ++i) | |
1377 { | |
1378 tree comp = comps[i]; | |
1379 tree eq, retval = NULL_TREE, if_ = NULL_TREE; | |
1380 if (info.defining) | |
1381 if_ = begin_if_stmt (); | |
1382 /* Spaceship is specified to use !=, but for the comparison category | |
1383 types, != is equivalent to !(==), so let's use == directly. */ | |
1384 if (code == EQ_EXPR) | |
1385 { | |
1386 /* if (x==y); else return false; */ | |
1387 eq = comp; | |
1388 retval = boolean_false_node; | |
1389 } | |
1390 else | |
1391 { | |
1392 /* if (auto v = x<=>y, v == 0); else return v; */ | |
1393 if (TREE_CODE (comp) == SPACESHIP_EXPR) | |
1394 TREE_TYPE (comp) = rettype; | |
1395 else | |
1396 comp = build_static_cast (input_location, rettype, comp, | |
1397 complain); | |
1398 info.check (comp); | |
1399 if (info.defining) | |
1400 { | |
1401 tree var = create_temporary_var (rettype); | |
1402 pushdecl (var); | |
1403 cp_finish_decl (var, comp, false, NULL_TREE, flags); | |
1404 comp = retval = var; | |
1405 } | |
1406 eq = build_new_op (info.loc, EQ_EXPR, flags, comp, | |
1407 integer_zero_node, NULL_TREE, NULL, | |
1408 complain); | |
1409 } | |
1410 tree ceq = contextual_conv_bool (eq, complain); | |
1411 info.check (ceq); | |
1412 if (info.defining) | |
1413 { | |
1414 finish_if_stmt_cond (ceq, if_); | |
1415 finish_then_clause (if_); | |
1416 begin_else_clause (if_); | |
1417 finish_return_stmt (retval); | |
1418 finish_else_clause (if_); | |
1419 finish_if_stmt (if_); | |
1420 } | |
1421 } | |
1422 if (info.defining) | |
1423 { | |
1424 tree val; | |
1425 if (code == EQ_EXPR) | |
1426 val = boolean_true_node; | |
1427 else | |
1428 { | |
1429 tree seql = lookup_comparison_result (cc_strong_ordering, | |
1430 "equal", complain); | |
1431 val = build_static_cast (input_location, rettype, seql, | |
1432 complain); | |
1433 } | |
1434 finish_return_stmt (val); | |
1435 } | |
1436 } | |
1437 else if (code == NE_EXPR) | |
1438 { | |
1439 tree comp = build_new_op (info.loc, EQ_EXPR, flags, lhs, rhs, | |
1440 NULL_TREE, NULL, complain); | |
1441 comp = contextual_conv_bool (comp, complain); | |
1442 info.check (comp); | |
1443 if (info.defining) | |
1444 { | |
1445 tree neg = build1 (TRUTH_NOT_EXPR, boolean_type_node, comp); | |
1446 finish_return_stmt (neg); | |
1447 } | |
1448 } | |
1449 else | |
1450 { | |
1451 tree comp = build_new_op (info.loc, SPACESHIP_EXPR, flags, lhs, rhs, | |
1452 NULL_TREE, NULL, complain); | |
1453 tree comp2 = build_new_op (info.loc, code, flags, comp, integer_zero_node, | |
1454 NULL_TREE, NULL, complain); | |
1455 info.check (comp2); | |
1456 if (info.defining) | |
1457 finish_return_stmt (comp2); | |
1458 } | |
1459 | |
1460 if (info.defining) | |
1461 finish_compound_stmt (compound_stmt); | |
1462 else | |
1463 --cp_unevaluated_operand; | |
1464 } | |
1465 | |
1466 /* True iff DECL is an implicitly-declared special member function with no real | |
1467 source location, so we can use its DECL_SOURCE_LOCATION to remember where we | |
1468 triggered its synthesis. */ | |
1469 | |
1470 bool | |
1471 decl_remember_implicit_trigger_p (tree decl) | |
1472 { | |
1473 if (!DECL_ARTIFICIAL (decl)) | |
1474 return false; | |
1475 special_function_kind sfk = special_function_p (decl); | |
1476 /* Inherited constructors have the location of their using-declaration, and | |
1477 operator== has the location of the corresponding operator<=>. */ | |
1478 return (sfk != sfk_inheriting_constructor | |
1479 && sfk != sfk_comparison); | |
1480 } | |
1481 | |
878 /* Synthesize FNDECL, a non-static member function. */ | 1482 /* Synthesize FNDECL, a non-static member function. */ |
879 | 1483 |
880 void | 1484 void |
881 synthesize_method (tree fndecl) | 1485 synthesize_method (tree fndecl) |
882 { | 1486 { |
885 bool need_body = true; | 1489 bool need_body = true; |
886 tree stmt; | 1490 tree stmt; |
887 location_t save_input_location = input_location; | 1491 location_t save_input_location = input_location; |
888 int error_count = errorcount; | 1492 int error_count = errorcount; |
889 int warning_count = warningcount + werrorcount; | 1493 int warning_count = warningcount + werrorcount; |
1494 special_function_kind sfk = special_function_p (fndecl); | |
890 | 1495 |
891 /* Reset the source location, we might have been previously | 1496 /* Reset the source location, we might have been previously |
892 deferred, and thus have saved where we were first needed. */ | 1497 deferred, and thus have saved where we were first needed. */ |
893 DECL_SOURCE_LOCATION (fndecl) | 1498 if (decl_remember_implicit_trigger_p (fndecl)) |
894 = DECL_SOURCE_LOCATION (TYPE_NAME (DECL_CONTEXT (fndecl))); | 1499 DECL_SOURCE_LOCATION (fndecl) |
1500 = DECL_SOURCE_LOCATION (TYPE_NAME (DECL_CONTEXT (fndecl))); | |
895 | 1501 |
896 /* If we've been asked to synthesize a clone, just synthesize the | 1502 /* If we've been asked to synthesize a clone, just synthesize the |
897 cloned function instead. Doing so will automatically fill in the | 1503 cloned function instead. Doing so will automatically fill in the |
898 body for the clone. */ | 1504 body for the clone. */ |
899 if (DECL_CLONED_FUNCTION_P (fndecl)) | 1505 if (DECL_CLONED_FUNCTION_P (fndecl)) |
925 if (arg_chain != void_list_node) | 1531 if (arg_chain != void_list_node) |
926 do_build_copy_constructor (fndecl); | 1532 do_build_copy_constructor (fndecl); |
927 else | 1533 else |
928 finish_mem_initializers (NULL_TREE); | 1534 finish_mem_initializers (NULL_TREE); |
929 } | 1535 } |
1536 else if (sfk == sfk_comparison) | |
1537 { | |
1538 /* Pass tf_none so the function is just deleted if there's a problem. */ | |
1539 build_comparison_op (fndecl, tf_none); | |
1540 need_body = false; | |
1541 } | |
930 | 1542 |
931 /* If we haven't yet generated the body of the function, just | 1543 /* If we haven't yet generated the body of the function, just |
932 generate an empty compound statement. */ | 1544 generate an empty compound statement. */ |
933 if (need_body) | 1545 if (need_body) |
934 { | 1546 { |
936 compound_stmt = begin_compound_stmt (BCS_FN_BODY); | 1548 compound_stmt = begin_compound_stmt (BCS_FN_BODY); |
937 finish_compound_stmt (compound_stmt); | 1549 finish_compound_stmt (compound_stmt); |
938 } | 1550 } |
939 | 1551 |
940 finish_function_body (stmt); | 1552 finish_function_body (stmt); |
941 expand_or_defer_fn (finish_function (/*inline_p=*/false)); | 1553 finish_function (/*inline_p=*/false); |
1554 | |
1555 if (!DECL_DELETED_FN (fndecl)) | |
1556 expand_or_defer_fn (fndecl); | |
942 | 1557 |
943 input_location = save_input_location; | 1558 input_location = save_input_location; |
944 | 1559 |
945 if (! context) | 1560 if (! context) |
946 pop_from_top_level (); | 1561 pop_from_top_level (); |
948 pop_function_context (); | 1563 pop_function_context (); |
949 | 1564 |
950 pop_deferring_access_checks (); | 1565 pop_deferring_access_checks (); |
951 | 1566 |
952 if (error_count != errorcount || warning_count != warningcount + werrorcount) | 1567 if (error_count != errorcount || warning_count != warningcount + werrorcount) |
953 inform (input_location, "synthesized method %qD first required here", | 1568 if (DECL_ARTIFICIAL (fndecl)) |
954 fndecl); | 1569 inform (input_location, "synthesized method %qD first required here", |
1570 fndecl); | |
955 } | 1571 } |
956 | 1572 |
957 /* Build a reference to type TYPE with cv-quals QUALS, which is an | 1573 /* Build a reference to type TYPE with cv-quals QUALS, which is an |
958 rvalue if RVALUE is true. */ | 1574 rvalue if RVALUE is true. */ |
959 | 1575 |
987 static tree | 1603 static tree |
988 locate_fn_flags (tree type, tree name, tree argtype, int flags, | 1604 locate_fn_flags (tree type, tree name, tree argtype, int flags, |
989 tsubst_flags_t complain) | 1605 tsubst_flags_t complain) |
990 { | 1606 { |
991 tree ob, fn, fns, binfo, rval; | 1607 tree ob, fn, fns, binfo, rval; |
992 vec<tree, va_gc> *args; | |
993 | 1608 |
994 if (TYPE_P (type)) | 1609 if (TYPE_P (type)) |
995 binfo = TYPE_BINFO (type); | 1610 binfo = TYPE_BINFO (type); |
996 else | 1611 else |
997 { | 1612 { |
998 binfo = type; | 1613 binfo = type; |
999 type = BINFO_TYPE (binfo); | 1614 type = BINFO_TYPE (binfo); |
1000 } | 1615 } |
1001 | 1616 |
1002 ob = build_stub_object (cp_build_reference_type (type, false)); | 1617 ob = build_stub_object (cp_build_reference_type (type, false)); |
1003 args = make_tree_vector (); | 1618 releasing_vec args; |
1004 if (argtype) | 1619 if (argtype) |
1005 { | 1620 { |
1006 if (TREE_CODE (argtype) == TREE_LIST) | 1621 if (TREE_CODE (argtype) == TREE_LIST) |
1007 { | 1622 { |
1008 for (tree elt = argtype; elt && elt != void_list_node; | 1623 for (tree elt = argtype; elt && elt != void_list_node; |
1021 } | 1636 } |
1022 | 1637 |
1023 fns = lookup_fnfields (binfo, name, 0); | 1638 fns = lookup_fnfields (binfo, name, 0); |
1024 rval = build_new_method_call (ob, fns, &args, binfo, flags, &fn, complain); | 1639 rval = build_new_method_call (ob, fns, &args, binfo, flags, &fn, complain); |
1025 | 1640 |
1026 release_tree_vector (args); | |
1027 if (fn && rval == error_mark_node) | 1641 if (fn && rval == error_mark_node) |
1028 return rval; | 1642 return rval; |
1029 else | 1643 else |
1030 return fn; | 1644 return fn; |
1031 } | 1645 } |
1199 tree expr; | 1813 tree expr; |
1200 if (code == MODIFY_EXPR) | 1814 if (code == MODIFY_EXPR) |
1201 expr = assignable_expr (to, from); | 1815 expr = assignable_expr (to, from); |
1202 else if (trivial && from && TREE_CHAIN (from)) | 1816 else if (trivial && from && TREE_CHAIN (from)) |
1203 return error_mark_node; // only 0- and 1-argument ctors can be trivial | 1817 return error_mark_node; // only 0- and 1-argument ctors can be trivial |
1818 else if (TREE_CODE (to) == ARRAY_TYPE && !TYPE_DOMAIN (to)) | |
1819 return error_mark_node; // can't construct an array of unknown bound | |
1204 else | 1820 else |
1205 expr = constructible_expr (to, from); | 1821 expr = constructible_expr (to, from); |
1206 return expr; | 1822 return expr; |
1207 } | 1823 } |
1208 | 1824 |
1233 if (expr == error_mark_node) | 1849 if (expr == error_mark_node) |
1234 return false; | 1850 return false; |
1235 return !!expr; | 1851 return !!expr; |
1236 } | 1852 } |
1237 | 1853 |
1854 /* Categorize various special_function_kinds. */ | |
1855 #define SFK_CTOR_P(sfk) \ | |
1856 ((sfk) >= sfk_constructor && (sfk) <= sfk_move_constructor) | |
1857 #define SFK_DTOR_P(sfk) \ | |
1858 ((sfk) == sfk_destructor || (sfk) == sfk_virtual_destructor) | |
1859 #define SFK_ASSIGN_P(sfk) \ | |
1860 ((sfk) == sfk_copy_assignment || (sfk) == sfk_move_assignment) | |
1861 #define SFK_COPY_P(sfk) \ | |
1862 ((sfk) == sfk_copy_constructor || (sfk) == sfk_copy_assignment) | |
1863 #define SFK_MOVE_P(sfk) \ | |
1864 ((sfk) == sfk_move_constructor || (sfk) == sfk_move_assignment) | |
1865 | |
1238 /* Subroutine of synthesized_method_walk. Update SPEC_P, TRIVIAL_P and | 1866 /* Subroutine of synthesized_method_walk. Update SPEC_P, TRIVIAL_P and |
1239 DELETED_P or give an error message MSG with argument ARG. */ | 1867 DELETED_P or give an error message MSG with argument ARG. */ |
1240 | 1868 |
1241 static void | 1869 static void |
1242 process_subob_fn (tree fn, tree *spec_p, bool *trivial_p, | 1870 process_subob_fn (tree fn, special_function_kind sfk, tree *spec_p, |
1243 bool *deleted_p, bool *constexpr_p, | 1871 bool *trivial_p, bool *deleted_p, bool *constexpr_p, |
1244 bool diag, tree arg, bool dtor_from_ctor = false) | 1872 bool diag, tree arg, bool dtor_from_ctor = false) |
1245 { | 1873 { |
1246 if (!fn || fn == error_mark_node) | 1874 if (!fn || fn == error_mark_node) |
1247 { | 1875 { |
1248 if (deleted_p) | 1876 if (deleted_p) |
1250 return; | 1878 return; |
1251 } | 1879 } |
1252 | 1880 |
1253 if (spec_p) | 1881 if (spec_p) |
1254 { | 1882 { |
1255 maybe_instantiate_noexcept (fn); | 1883 if (!maybe_instantiate_noexcept (fn)) |
1256 tree raises = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)); | 1884 *spec_p = error_mark_node; |
1257 *spec_p = merge_exception_specifiers (*spec_p, raises); | 1885 else |
1886 { | |
1887 tree raises = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)); | |
1888 *spec_p = merge_exception_specifiers (*spec_p, raises); | |
1889 } | |
1258 } | 1890 } |
1259 | 1891 |
1260 if (!trivial_fn_p (fn) && !dtor_from_ctor) | 1892 if (!trivial_fn_p (fn) && !dtor_from_ctor) |
1261 { | 1893 { |
1262 if (trivial_p) | 1894 if (trivial_p) |
1275 { | 1907 { |
1276 *constexpr_p = false; | 1908 *constexpr_p = false; |
1277 if (diag) | 1909 if (diag) |
1278 { | 1910 { |
1279 inform (DECL_SOURCE_LOCATION (fn), | 1911 inform (DECL_SOURCE_LOCATION (fn), |
1280 "defaulted constructor calls non-%<constexpr%> %qD", fn); | 1912 SFK_DTOR_P (sfk) |
1913 ? G_("defaulted destructor calls non-%<constexpr%> %qD") | |
1914 : G_("defaulted constructor calls non-%<constexpr%> %qD"), | |
1915 fn); | |
1281 explain_invalid_constexpr_fn (fn); | 1916 explain_invalid_constexpr_fn (fn); |
1282 } | 1917 } |
1283 } | 1918 } |
1284 } | 1919 } |
1285 | 1920 |
1287 aggregates. If DTOR_FROM_CTOR is true, we're walking subobject destructors | 1922 aggregates. If DTOR_FROM_CTOR is true, we're walking subobject destructors |
1288 called from a synthesized constructor, in which case we don't consider | 1923 called from a synthesized constructor, in which case we don't consider |
1289 the triviality of the subobject destructor. */ | 1924 the triviality of the subobject destructor. */ |
1290 | 1925 |
1291 static void | 1926 static void |
1292 walk_field_subobs (tree fields, tree fnname, special_function_kind sfk, | 1927 walk_field_subobs (tree fields, special_function_kind sfk, tree fnname, |
1293 int quals, bool copy_arg_p, bool move_p, | 1928 int quals, tree *spec_p, bool *trivial_p, |
1294 bool assign_p, tree *spec_p, bool *trivial_p, | |
1295 bool *deleted_p, bool *constexpr_p, | 1929 bool *deleted_p, bool *constexpr_p, |
1296 bool diag, int flags, tsubst_flags_t complain, | 1930 bool diag, int flags, tsubst_flags_t complain, |
1297 bool dtor_from_ctor) | 1931 bool dtor_from_ctor) |
1298 { | 1932 { |
1299 tree field; | 1933 tree field; |
1300 for (field = fields; field; field = DECL_CHAIN (field)) | 1934 for (field = fields; field; field = DECL_CHAIN (field)) |
1301 { | 1935 { |
1302 tree mem_type, argtype, rval; | 1936 tree mem_type, argtype, rval; |
1303 | 1937 |
1304 if (TREE_CODE (field) != FIELD_DECL | 1938 if (TREE_CODE (field) != FIELD_DECL || DECL_ARTIFICIAL (field)) |
1305 || DECL_ARTIFICIAL (field)) | |
1306 continue; | 1939 continue; |
1307 | 1940 |
1308 /* Variant members only affect deletedness. In particular, they don't | 1941 /* Variant members only affect deletedness. In particular, they don't |
1309 affect the exception-specification of a user-provided destructor, | 1942 affect the exception-specification of a user-provided destructor, |
1310 which we're figuring out via get_defaulted_eh_spec. So if we aren't | 1943 which we're figuring out via get_defaulted_eh_spec. So if we aren't |
1313 if (sfk == sfk_destructor && deleted_p == NULL | 1946 if (sfk == sfk_destructor && deleted_p == NULL |
1314 && TREE_CODE (DECL_CONTEXT (field)) == UNION_TYPE) | 1947 && TREE_CODE (DECL_CONTEXT (field)) == UNION_TYPE) |
1315 break; | 1948 break; |
1316 | 1949 |
1317 mem_type = strip_array_types (TREE_TYPE (field)); | 1950 mem_type = strip_array_types (TREE_TYPE (field)); |
1318 if (assign_p) | 1951 if (SFK_ASSIGN_P (sfk)) |
1319 { | 1952 { |
1320 bool bad = true; | 1953 bool bad = true; |
1321 if (CP_TYPE_CONST_P (mem_type) && !CLASS_TYPE_P (mem_type)) | 1954 if (CP_TYPE_CONST_P (mem_type) && !CLASS_TYPE_P (mem_type)) |
1322 { | 1955 { |
1323 if (diag) | 1956 if (diag) |
1324 error ("non-static const member %q#D, can%'t use default " | 1957 error ("non-static const member %q#D, cannot use default " |
1325 "assignment operator", field); | 1958 "assignment operator", field); |
1326 } | 1959 } |
1327 else if (TYPE_REF_P (mem_type)) | 1960 else if (TYPE_REF_P (mem_type)) |
1328 { | 1961 { |
1329 if (diag) | 1962 if (diag) |
1330 error ("non-static reference member %q#D, can%'t use " | 1963 error ("non-static reference member %q#D, cannot use " |
1331 "default assignment operator", field); | 1964 "default assignment operator", field); |
1332 } | 1965 } |
1333 else | 1966 else |
1334 bad = false; | 1967 bad = false; |
1335 | 1968 |
1350 /* Core 1351: If the field has an NSDMI that could throw, the | 1983 /* Core 1351: If the field has an NSDMI that could throw, the |
1351 default constructor is noexcept(false). */ | 1984 default constructor is noexcept(false). */ |
1352 if (spec_p) | 1985 if (spec_p) |
1353 { | 1986 { |
1354 tree nsdmi = get_nsdmi (field, /*ctor*/false, complain); | 1987 tree nsdmi = get_nsdmi (field, /*ctor*/false, complain); |
1355 if (!expr_noexcept_p (nsdmi, complain)) | 1988 if (nsdmi == error_mark_node) |
1989 *spec_p = error_mark_node; | |
1990 else if (*spec_p != error_mark_node | |
1991 && !expr_noexcept_p (nsdmi, complain)) | |
1356 *spec_p = noexcept_false_spec; | 1992 *spec_p = noexcept_false_spec; |
1357 } | 1993 } |
1358 /* Don't do the normal processing. */ | 1994 /* Don't do the normal processing. */ |
1359 continue; | 1995 continue; |
1360 } | 1996 } |
1385 } | 2021 } |
1386 | 2022 |
1387 if (bad && deleted_p) | 2023 if (bad && deleted_p) |
1388 *deleted_p = true; | 2024 *deleted_p = true; |
1389 | 2025 |
1390 /* For an implicitly-defined default constructor to be constexpr, | 2026 /* Before C++20, for an implicitly-defined default constructor to |
1391 every member must have a user-provided default constructor or | 2027 be constexpr, every member must have a user-provided default |
1392 an explicit initializer. */ | 2028 constructor or an explicit initializer. */ |
1393 if (constexpr_p && !CLASS_TYPE_P (mem_type) | 2029 if (constexpr_p |
2030 && cxx_dialect < cxx2a | |
2031 && !CLASS_TYPE_P (mem_type) | |
1394 && TREE_CODE (DECL_CONTEXT (field)) != UNION_TYPE) | 2032 && TREE_CODE (DECL_CONTEXT (field)) != UNION_TYPE) |
1395 { | 2033 { |
1396 *constexpr_p = false; | 2034 *constexpr_p = false; |
1397 if (diag) | 2035 if (diag) |
1398 inform (DECL_SOURCE_LOCATION (field), | 2036 inform (DECL_SOURCE_LOCATION (field), |
1417 if (!CLASS_TYPE_P (mem_type)) | 2055 if (!CLASS_TYPE_P (mem_type)) |
1418 continue; | 2056 continue; |
1419 | 2057 |
1420 if (ANON_AGGR_TYPE_P (mem_type)) | 2058 if (ANON_AGGR_TYPE_P (mem_type)) |
1421 { | 2059 { |
1422 walk_field_subobs (TYPE_FIELDS (mem_type), fnname, sfk, quals, | 2060 walk_field_subobs (TYPE_FIELDS (mem_type), sfk, fnname, quals, |
1423 copy_arg_p, move_p, assign_p, spec_p, trivial_p, | 2061 spec_p, trivial_p, deleted_p, constexpr_p, |
1424 deleted_p, constexpr_p, | |
1425 diag, flags, complain, dtor_from_ctor); | 2062 diag, flags, complain, dtor_from_ctor); |
1426 continue; | 2063 continue; |
1427 } | 2064 } |
1428 | 2065 |
1429 if (copy_arg_p) | 2066 if (SFK_COPY_P (sfk) || SFK_MOVE_P (sfk)) |
1430 { | 2067 { |
1431 int mem_quals = cp_type_quals (mem_type) | quals; | 2068 int mem_quals = cp_type_quals (mem_type) | quals; |
1432 if (DECL_MUTABLE_P (field)) | 2069 if (DECL_MUTABLE_P (field)) |
1433 mem_quals &= ~TYPE_QUAL_CONST; | 2070 mem_quals &= ~TYPE_QUAL_CONST; |
1434 argtype = build_stub_type (mem_type, mem_quals, move_p); | 2071 argtype = build_stub_type (mem_type, mem_quals, SFK_MOVE_P (sfk)); |
1435 } | 2072 } |
1436 else | 2073 else |
1437 argtype = NULL_TREE; | 2074 argtype = NULL_TREE; |
1438 | 2075 |
1439 rval = locate_fn_flags (mem_type, fnname, argtype, flags, complain); | 2076 rval = locate_fn_flags (mem_type, fnname, argtype, flags, complain); |
1440 | 2077 |
1441 process_subob_fn (rval, spec_p, trivial_p, deleted_p, | 2078 process_subob_fn (rval, sfk, spec_p, trivial_p, deleted_p, |
1442 constexpr_p, diag, field, dtor_from_ctor); | 2079 constexpr_p, diag, field, dtor_from_ctor); |
1443 } | 2080 } |
1444 } | 2081 } |
1445 | 2082 |
1446 /* Base walker helper for synthesized_method_walk. Inspect a direct | 2083 /* Base walker helper for synthesized_method_walk. Inspect a direct |
1447 or virtual base. BINFO is the parent type's binfo. BASE_BINFO is | 2084 or virtual base. BINFO is the parent type's binfo. BASE_BINFO is |
1448 the base binfo of interests. All other parms are as for | 2085 the base binfo of interests. All other parms are as for |
1449 synthesized_method_walk, or its local vars. */ | 2086 synthesized_method_walk, or its local vars. */ |
1450 | 2087 |
1451 static tree | 2088 static tree |
1452 synthesized_method_base_walk (tree binfo, tree base_binfo, | 2089 synthesized_method_base_walk (tree binfo, tree base_binfo, |
1453 int quals, bool copy_arg_p, | 2090 special_function_kind sfk, tree fnname, int quals, |
1454 bool move_p, bool ctor_p, | |
1455 tree *inheriting_ctor, tree inherited_parms, | 2091 tree *inheriting_ctor, tree inherited_parms, |
1456 tree fnname, int flags, bool diag, | 2092 int flags, bool diag, |
1457 tree *spec_p, bool *trivial_p, | 2093 tree *spec_p, bool *trivial_p, |
1458 bool *deleted_p, bool *constexpr_p) | 2094 bool *deleted_p, bool *constexpr_p) |
1459 { | 2095 { |
1460 bool inherited_binfo = false; | 2096 bool inherited_binfo = false; |
1461 tree argtype = NULL_TREE; | 2097 tree argtype = NULL_TREE; |
1462 deferring_kind defer = dk_no_deferred; | 2098 deferring_kind defer = dk_no_deferred; |
1463 | 2099 |
1464 if (copy_arg_p) | 2100 if (SFK_COPY_P (sfk) || SFK_MOVE_P (sfk)) |
1465 argtype = build_stub_type (BINFO_TYPE (base_binfo), quals, move_p); | 2101 argtype = build_stub_type (BINFO_TYPE (base_binfo), quals, SFK_MOVE_P (sfk)); |
1466 else if (inheriting_ctor | 2102 else if (inheriting_ctor |
1467 && (inherited_binfo | 2103 && (inherited_binfo |
1468 = binfo_inherited_from (binfo, base_binfo, *inheriting_ctor))) | 2104 = binfo_inherited_from (binfo, base_binfo, *inheriting_ctor))) |
1469 { | 2105 { |
1470 argtype = inherited_parms; | 2106 argtype = inherited_parms; |
1471 /* Don't check access on the inherited constructor. */ | 2107 /* Don't check access on the inherited constructor. */ |
1472 if (flag_new_inheriting_ctors) | 2108 if (flag_new_inheriting_ctors) |
1473 defer = dk_deferred; | 2109 defer = dk_deferred; |
1474 } | 2110 } |
1475 /* To be conservative, ignore access to the base dtor that | 2111 else if (cxx_dialect >= cxx14 && sfk == sfk_virtual_destructor |
1476 DR1658 instructs us to ignore. See the comment in | |
1477 synthesized_method_walk. */ | |
1478 else if (cxx_dialect >= cxx14 && fnname == complete_dtor_identifier | |
1479 && BINFO_VIRTUAL_P (base_binfo) | 2112 && BINFO_VIRTUAL_P (base_binfo) |
1480 && ABSTRACT_CLASS_TYPE_P (BINFO_TYPE (binfo))) | 2113 && ABSTRACT_CLASS_TYPE_P (BINFO_TYPE (binfo))) |
2114 /* Don't check access when looking at vbases of abstract class's | |
2115 virtual destructor. */ | |
1481 defer = dk_no_check; | 2116 defer = dk_no_check; |
1482 | 2117 |
1483 if (defer != dk_no_deferred) | 2118 if (defer != dk_no_deferred) |
1484 push_deferring_access_checks (defer); | 2119 push_deferring_access_checks (defer); |
1485 tree rval = locate_fn_flags (base_binfo, fnname, argtype, flags, | 2120 tree rval = locate_fn_flags (base_binfo, fnname, argtype, flags, |
1491 if (inherited_binfo && rval | 2126 if (inherited_binfo && rval |
1492 && DECL_P (*inheriting_ctor) && DECL_P (rval) | 2127 && DECL_P (*inheriting_ctor) && DECL_P (rval) |
1493 && DECL_CONTEXT (*inheriting_ctor) == DECL_CONTEXT (rval)) | 2128 && DECL_CONTEXT (*inheriting_ctor) == DECL_CONTEXT (rval)) |
1494 *inheriting_ctor = DECL_CLONED_FUNCTION (rval); | 2129 *inheriting_ctor = DECL_CLONED_FUNCTION (rval); |
1495 | 2130 |
1496 process_subob_fn (rval, spec_p, trivial_p, deleted_p, | 2131 process_subob_fn (rval, sfk, spec_p, trivial_p, deleted_p, |
1497 constexpr_p, diag, BINFO_TYPE (base_binfo)); | 2132 constexpr_p, diag, BINFO_TYPE (base_binfo)); |
1498 if (ctor_p && | 2133 if (SFK_CTOR_P (sfk) |
1499 (!BINFO_VIRTUAL_P (base_binfo) | 2134 && (!BINFO_VIRTUAL_P (base_binfo) |
1500 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (BINFO_TYPE (base_binfo)))) | 2135 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (BINFO_TYPE (base_binfo)))) |
1501 { | 2136 { |
1502 /* In a constructor we also need to check the subobject | 2137 /* In a constructor we also need to check the subobject |
1503 destructors for cleanup of partially constructed objects. */ | 2138 destructors for cleanup of partially constructed objects. */ |
1504 tree dtor = locate_fn_flags (base_binfo, complete_dtor_identifier, | 2139 tree dtor = locate_fn_flags (base_binfo, complete_dtor_identifier, |
1505 NULL_TREE, flags, | 2140 NULL_TREE, flags, |
1506 diag ? tf_warning_or_error : tf_none); | 2141 diag ? tf_warning_or_error : tf_none); |
1507 /* Note that we don't pass down trivial_p; the subobject | 2142 /* Note that we don't pass down trivial_p; the subobject |
1508 destructors don't affect triviality of the constructor. Nor | 2143 destructors don't affect triviality of the constructor. Nor |
1509 do they affect constexpr-ness (a constant expression doesn't | 2144 do they affect constexpr-ness (a constant expression doesn't |
1510 throw) or exception-specification (a throw from one of the | 2145 throw) or exception-specification (a throw from one of the |
1511 dtors would be a double-fault). */ | 2146 dtors would be a double-fault). */ |
1512 process_subob_fn (dtor, NULL, NULL, deleted_p, NULL, false, | 2147 process_subob_fn (dtor, sfk, NULL, NULL, deleted_p, NULL, false, |
1513 BINFO_TYPE (base_binfo), /*dtor_from_ctor*/true); | 2148 BINFO_TYPE (base_binfo), /*dtor_from_ctor*/true); |
1514 } | 2149 } |
1515 | 2150 |
1516 return rval; | 2151 return rval; |
1517 } | 2152 } |
1527 synthesized_method_walk (tree ctype, special_function_kind sfk, bool const_p, | 2162 synthesized_method_walk (tree ctype, special_function_kind sfk, bool const_p, |
1528 tree *spec_p, bool *trivial_p, bool *deleted_p, | 2163 tree *spec_p, bool *trivial_p, bool *deleted_p, |
1529 bool *constexpr_p, bool diag, | 2164 bool *constexpr_p, bool diag, |
1530 tree *inheriting_ctor, tree inherited_parms) | 2165 tree *inheriting_ctor, tree inherited_parms) |
1531 { | 2166 { |
1532 tree binfo, base_binfo, fnname; | 2167 tree binfo, base_binfo; |
1533 int i; | 2168 int i; |
2169 | |
2170 /* SFK must be exactly one category. */ | |
2171 gcc_checking_assert (SFK_DTOR_P(sfk) + SFK_CTOR_P(sfk) | |
2172 + SFK_ASSIGN_P(sfk) == 1); | |
1534 | 2173 |
1535 if (spec_p) | 2174 if (spec_p) |
1536 *spec_p = (cxx_dialect >= cxx11 ? noexcept_true_spec : empty_except_spec); | 2175 *spec_p = (cxx_dialect >= cxx11 ? noexcept_true_spec : empty_except_spec); |
1537 | 2176 |
1538 if (deleted_p) | 2177 if (deleted_p) |
1554 } | 2193 } |
1555 | 2194 |
1556 *deleted_p = false; | 2195 *deleted_p = false; |
1557 } | 2196 } |
1558 | 2197 |
1559 bool ctor_p = false; | |
1560 bool assign_p = false; | |
1561 bool check_vdtor = false; | 2198 bool check_vdtor = false; |
1562 switch (sfk) | 2199 tree fnname; |
1563 { | 2200 |
1564 case sfk_move_assignment: | 2201 if (SFK_DTOR_P (sfk)) |
1565 case sfk_copy_assignment: | 2202 { |
1566 assign_p = true; | |
1567 fnname = assign_op_identifier; | |
1568 break; | |
1569 | |
1570 case sfk_destructor: | |
1571 check_vdtor = true; | 2203 check_vdtor = true; |
1572 /* The synthesized method will call base dtors, but check complete | 2204 /* The synthesized method will call base dtors, but check complete |
1573 here to avoid having to deal with VTT. */ | 2205 here to avoid having to deal with VTT. */ |
1574 fnname = complete_dtor_identifier; | 2206 fnname = complete_dtor_identifier; |
1575 break; | 2207 } |
1576 | 2208 else if (SFK_ASSIGN_P (sfk)) |
1577 case sfk_constructor: | 2209 fnname = assign_op_identifier; |
1578 case sfk_move_constructor: | 2210 else |
1579 case sfk_copy_constructor: | 2211 fnname = complete_ctor_identifier; |
1580 case sfk_inheriting_constructor: | |
1581 ctor_p = true; | |
1582 fnname = complete_ctor_identifier; | |
1583 break; | |
1584 | |
1585 default: | |
1586 gcc_unreachable (); | |
1587 } | |
1588 | 2212 |
1589 gcc_assert ((sfk == sfk_inheriting_constructor) | 2213 gcc_assert ((sfk == sfk_inheriting_constructor) |
1590 == (inheriting_ctor && *inheriting_ctor != NULL_TREE)); | 2214 == (inheriting_ctor && *inheriting_ctor != NULL_TREE)); |
1591 | 2215 |
1592 /* If that user-written default constructor would satisfy the | 2216 /* If that user-written default constructor would satisfy the |
1599 subobject is a constexpr function, and | 2223 subobject is a constexpr function, and |
1600 - for each non-static data member of X that is of class type (or array | 2224 - for each non-static data member of X that is of class type (or array |
1601 thereof), the assignment operator selected to copy/move that | 2225 thereof), the assignment operator selected to copy/move that |
1602 member is a constexpr function. */ | 2226 member is a constexpr function. */ |
1603 if (constexpr_p) | 2227 if (constexpr_p) |
1604 *constexpr_p = ctor_p || (assign_p && cxx_dialect >= cxx14); | 2228 *constexpr_p = (SFK_CTOR_P (sfk) |
1605 | 2229 || (SFK_ASSIGN_P (sfk) && cxx_dialect >= cxx14) |
1606 bool move_p = false; | 2230 || (SFK_DTOR_P (sfk) && cxx_dialect >= cxx2a)); |
1607 bool copy_arg_p = false; | |
1608 switch (sfk) | |
1609 { | |
1610 case sfk_constructor: | |
1611 case sfk_destructor: | |
1612 case sfk_inheriting_constructor: | |
1613 break; | |
1614 | |
1615 case sfk_move_constructor: | |
1616 case sfk_move_assignment: | |
1617 move_p = true; | |
1618 /* FALLTHRU */ | |
1619 case sfk_copy_constructor: | |
1620 case sfk_copy_assignment: | |
1621 copy_arg_p = true; | |
1622 break; | |
1623 | |
1624 default: | |
1625 gcc_unreachable (); | |
1626 } | |
1627 | 2231 |
1628 bool expected_trivial = type_has_trivial_fn (ctype, sfk); | 2232 bool expected_trivial = type_has_trivial_fn (ctype, sfk); |
1629 if (trivial_p) | 2233 if (trivial_p) |
1630 *trivial_p = expected_trivial; | 2234 *trivial_p = expected_trivial; |
1631 | 2235 |
1638 early in C++0x. The same considerations apply in C++98/03, but | 2242 early in C++0x. The same considerations apply in C++98/03, but |
1639 there the definition of triviality does not consider overload | 2243 there the definition of triviality does not consider overload |
1640 resolution, so a constructor can be trivial even if it would otherwise | 2244 resolution, so a constructor can be trivial even if it would otherwise |
1641 call a non-trivial constructor. */ | 2245 call a non-trivial constructor. */ |
1642 if (expected_trivial | 2246 if (expected_trivial |
1643 && (!copy_arg_p || cxx_dialect < cxx11)) | 2247 && (!(SFK_COPY_P (sfk) || SFK_MOVE_P (sfk)) || cxx_dialect < cxx11)) |
1644 { | 2248 { |
1645 if (constexpr_p && sfk == sfk_constructor) | 2249 if (constexpr_p && sfk == sfk_constructor) |
1646 { | 2250 { |
1647 bool cx = trivial_default_constructor_is_constexpr (ctype); | 2251 bool cx = trivial_default_constructor_is_constexpr (ctype); |
1648 *constexpr_p = cx; | 2252 *constexpr_p = cx; |
1673 int quals = const_p ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED; | 2277 int quals = const_p ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED; |
1674 | 2278 |
1675 for (binfo = TYPE_BINFO (ctype), i = 0; | 2279 for (binfo = TYPE_BINFO (ctype), i = 0; |
1676 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i) | 2280 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i) |
1677 { | 2281 { |
1678 if (!assign_p && BINFO_VIRTUAL_P (base_binfo)) | 2282 if (!SFK_ASSIGN_P (sfk) && BINFO_VIRTUAL_P (base_binfo)) |
1679 /* We'll handle virtual bases below. */ | 2283 /* We'll handle virtual bases below. */ |
1680 continue; | 2284 continue; |
1681 | 2285 |
1682 tree fn = synthesized_method_base_walk (binfo, base_binfo, quals, | 2286 tree fn = synthesized_method_base_walk (binfo, base_binfo, |
1683 copy_arg_p, move_p, ctor_p, | 2287 sfk, fnname, quals, |
1684 inheriting_ctor, | 2288 inheriting_ctor, inherited_parms, |
1685 inherited_parms, | 2289 flags, diag, spec_p, trivial_p, |
1686 fnname, flags, diag, | |
1687 spec_p, trivial_p, | |
1688 deleted_p, constexpr_p); | 2290 deleted_p, constexpr_p); |
1689 | 2291 |
1690 if (diag && assign_p && move_p | 2292 if (diag && SFK_ASSIGN_P (sfk) && SFK_MOVE_P (sfk) |
1691 && BINFO_VIRTUAL_P (base_binfo) | 2293 && BINFO_VIRTUAL_P (base_binfo) |
1692 && fn && TREE_CODE (fn) == FUNCTION_DECL | 2294 && fn && TREE_CODE (fn) == FUNCTION_DECL |
1693 && move_fn_p (fn) && !trivial_fn_p (fn) | 2295 && move_fn_p (fn) && !trivial_fn_p (fn) |
1694 && vbase_has_user_provided_move_assign (BINFO_TYPE (base_binfo))) | 2296 && vbase_has_user_provided_move_assign (BINFO_TYPE (base_binfo))) |
1695 warning (OPT_Wvirtual_move_assign, | 2297 warning (OPT_Wvirtual_move_assign, |
1714 check_vdtor = false; | 2316 check_vdtor = false; |
1715 } | 2317 } |
1716 } | 2318 } |
1717 | 2319 |
1718 vec<tree, va_gc> *vbases = CLASSTYPE_VBASECLASSES (ctype); | 2320 vec<tree, va_gc> *vbases = CLASSTYPE_VBASECLASSES (ctype); |
1719 if (assign_p) | 2321 if (SFK_ASSIGN_P (sfk)) |
1720 /* Already examined vbases above. */; | 2322 /* Already examined vbases above. */; |
1721 else if (vec_safe_is_empty (vbases)) | 2323 else if (vec_safe_is_empty (vbases)) |
1722 /* No virtual bases to worry about. */; | 2324 /* No virtual bases to worry about. */; |
1723 else if (ABSTRACT_CLASS_TYPE_P (ctype) && cxx_dialect >= cxx14 | 2325 else if (ABSTRACT_CLASS_TYPE_P (ctype) && cxx_dialect >= cxx14 |
1724 /* DR 1658 specifies that vbases of abstract classes are | 2326 /* DR 1658 specifies that vbases of abstract classes are |
1725 ignored for both ctors and dtors. However, that breaks | 2327 ignored for both ctors and dtors. Except DR 2336 |
1726 virtual dtor overriding when the ignored base has a | 2328 overrides that skipping when determing the eh-spec of a |
1727 throwing destructor. So, ignore that piece of 1658. A | 2329 virtual destructor. */ |
1728 defect has been filed (no number yet). */ | 2330 && sfk != sfk_virtual_destructor) |
1729 && sfk != sfk_destructor) | |
1730 /* Vbase cdtors are not relevant. */; | 2331 /* Vbase cdtors are not relevant. */; |
1731 else | 2332 else |
1732 { | 2333 { |
1733 if (constexpr_p) | 2334 if (constexpr_p) |
1734 *constexpr_p = false; | 2335 *constexpr_p = false; |
1735 | 2336 |
1736 FOR_EACH_VEC_ELT (*vbases, i, base_binfo) | 2337 FOR_EACH_VEC_ELT (*vbases, i, base_binfo) |
1737 synthesized_method_base_walk (binfo, base_binfo, quals, | 2338 synthesized_method_base_walk (binfo, base_binfo, sfk, fnname, quals, |
1738 copy_arg_p, move_p, ctor_p, | |
1739 inheriting_ctor, inherited_parms, | 2339 inheriting_ctor, inherited_parms, |
1740 fnname, flags, diag, | 2340 flags, diag, |
1741 spec_p, trivial_p, | 2341 spec_p, trivial_p, deleted_p, constexpr_p); |
1742 deleted_p, constexpr_p); | |
1743 } | 2342 } |
1744 | 2343 |
1745 /* Now handle the non-static data members. */ | 2344 /* Now handle the non-static data members. */ |
1746 walk_field_subobs (TYPE_FIELDS (ctype), fnname, sfk, quals, | 2345 walk_field_subobs (TYPE_FIELDS (ctype), sfk, fnname, quals, |
1747 copy_arg_p, move_p, assign_p, spec_p, trivial_p, | 2346 spec_p, trivial_p, deleted_p, constexpr_p, |
1748 deleted_p, constexpr_p, | |
1749 diag, flags, complain, /*dtor_from_ctor*/false); | 2347 diag, flags, complain, /*dtor_from_ctor*/false); |
1750 if (ctor_p) | 2348 if (SFK_CTOR_P (sfk)) |
1751 walk_field_subobs (TYPE_FIELDS (ctype), complete_dtor_identifier, | 2349 walk_field_subobs (TYPE_FIELDS (ctype), sfk_destructor, |
1752 sfk_destructor, TYPE_UNQUALIFIED, false, | 2350 complete_dtor_identifier, TYPE_UNQUALIFIED, |
1753 false, false, NULL, NULL, | 2351 NULL, NULL, deleted_p, NULL, |
1754 deleted_p, NULL, | |
1755 false, flags, complain, /*dtor_from_ctor*/true); | 2352 false, flags, complain, /*dtor_from_ctor*/true); |
1756 | 2353 |
1757 pop_scope (scope); | 2354 pop_scope (scope); |
1758 | 2355 |
1759 pop_deferring_access_checks (); | 2356 pop_deferring_access_checks (); |
1768 get_defaulted_eh_spec (tree decl, tsubst_flags_t complain) | 2365 get_defaulted_eh_spec (tree decl, tsubst_flags_t complain) |
1769 { | 2366 { |
1770 if (DECL_CLONED_FUNCTION_P (decl)) | 2367 if (DECL_CLONED_FUNCTION_P (decl)) |
1771 decl = DECL_CLONED_FUNCTION (decl); | 2368 decl = DECL_CLONED_FUNCTION (decl); |
1772 special_function_kind sfk = special_function_p (decl); | 2369 special_function_kind sfk = special_function_p (decl); |
2370 if (sfk == sfk_comparison) | |
2371 { | |
2372 /* We're in synthesize_method. Start with NULL_TREE, build_comparison_op | |
2373 will adjust as needed. */ | |
2374 gcc_assert (decl == current_function_decl); | |
2375 return NULL_TREE; | |
2376 } | |
1773 tree ctype = DECL_CONTEXT (decl); | 2377 tree ctype = DECL_CONTEXT (decl); |
1774 tree parms = FUNCTION_FIRST_USER_PARMTYPE (decl); | 2378 tree parms = FUNCTION_FIRST_USER_PARMTYPE (decl); |
1775 tree parm_type = TREE_VALUE (parms); | 2379 tree parm_type = TREE_VALUE (parms); |
1776 bool const_p = CP_TYPE_CONST_P (non_reference (parm_type)); | 2380 bool const_p = CP_TYPE_CONST_P (non_reference (parm_type)); |
1777 tree spec = empty_except_spec; | 2381 tree spec = empty_except_spec; |
1778 bool diag = !DECL_DELETED_FN (decl) && (complain & tf_error); | 2382 bool diag = !DECL_DELETED_FN (decl) && (complain & tf_error); |
1779 tree inh = DECL_INHERITED_CTOR (decl); | 2383 tree inh = DECL_INHERITED_CTOR (decl); |
2384 if (SFK_DTOR_P (sfk) && DECL_VIRTUAL_P (decl)) | |
2385 /* We have to examine virtual bases even if abstract. */ | |
2386 sfk = sfk_virtual_destructor; | |
2387 bool pushed = false; | |
2388 if (CLASSTYPE_TEMPLATE_INSTANTIATION (ctype)) | |
2389 pushed = push_tinst_level (decl); | |
1780 synthesized_method_walk (ctype, sfk, const_p, &spec, NULL, NULL, | 2390 synthesized_method_walk (ctype, sfk, const_p, &spec, NULL, NULL, |
1781 NULL, diag, &inh, parms); | 2391 NULL, diag, &inh, parms); |
2392 if (pushed) | |
2393 pop_tinst_level (); | |
1782 return spec; | 2394 return spec; |
1783 } | 2395 } |
1784 | 2396 |
1785 /* DECL is a deleted function. If it's implicitly deleted, explain why and | 2397 /* DECL is a deleted function. If it's implicitly deleted, explain why and |
1786 return true; else return false. */ | 2398 return true; else return false. */ |
1843 "%q#D inherits from multiple base subobjects", | 2455 "%q#D inherits from multiple base subobjects", |
1844 decl); | 2456 decl); |
1845 informed = true; | 2457 informed = true; |
1846 } | 2458 } |
1847 } | 2459 } |
1848 if (!informed) | 2460 if (!informed && sfk == sfk_comparison) |
2461 { | |
2462 inform (DECL_SOURCE_LOCATION (decl), | |
2463 "%q#D is implicitly deleted because the default " | |
2464 "definition would be ill-formed:", decl); | |
2465 build_comparison_op (decl, tf_warning_or_error); | |
2466 } | |
2467 else if (!informed) | |
1849 { | 2468 { |
1850 tree parms = FUNCTION_FIRST_USER_PARMTYPE (decl); | 2469 tree parms = FUNCTION_FIRST_USER_PARMTYPE (decl); |
1851 tree parm_type = TREE_VALUE (parms); | 2470 bool const_p = false; |
1852 bool const_p = CP_TYPE_CONST_P (non_reference (parm_type)); | 2471 if (parms) |
2472 { | |
2473 tree parm_type = TREE_VALUE (parms); | |
2474 const_p = CP_TYPE_CONST_P (non_reference (parm_type)); | |
2475 } | |
1853 tree raises = NULL_TREE; | 2476 tree raises = NULL_TREE; |
1854 bool deleted_p = false; | 2477 bool deleted_p = false; |
1855 tree scope = push_scope (ctype); | 2478 tree scope = push_scope (ctype); |
1856 tree inh = DECL_INHERITED_CTOR (decl); | 2479 tree inh = DECL_INHERITED_CTOR (decl); |
1857 | 2480 |
1890 it can't be constexpr. */ | 2513 it can't be constexpr. */ |
1891 | 2514 |
1892 void | 2515 void |
1893 explain_implicit_non_constexpr (tree decl) | 2516 explain_implicit_non_constexpr (tree decl) |
1894 { | 2517 { |
1895 tree parm_type = TREE_VALUE (FUNCTION_FIRST_USER_PARMTYPE (decl)); | 2518 tree parms = FUNCTION_FIRST_USER_PARMTYPE (decl); |
1896 bool const_p = CP_TYPE_CONST_P (non_reference (parm_type)); | 2519 bool const_p = CP_TYPE_CONST_P (non_reference (TREE_VALUE (parms))); |
1897 tree inh = DECL_INHERITED_CTOR (decl); | 2520 tree inh = DECL_INHERITED_CTOR (decl); |
1898 bool dummy; | 2521 bool dummy; |
1899 synthesized_method_walk (DECL_CLASS_CONTEXT (decl), | 2522 special_function_kind sfk = special_function_p (decl); |
1900 special_function_p (decl), const_p, | 2523 if (sfk == sfk_comparison) |
1901 NULL, NULL, NULL, &dummy, true, | 2524 { |
1902 &inh, | 2525 DECL_DECLARED_CONSTEXPR_P (decl) = true; |
1903 FUNCTION_FIRST_USER_PARMTYPE (decl)); | 2526 build_comparison_op (decl, tf_warning_or_error); |
2527 DECL_DECLARED_CONSTEXPR_P (decl) = false; | |
2528 } | |
2529 else | |
2530 synthesized_method_walk (DECL_CLASS_CONTEXT (decl), | |
2531 sfk, const_p, | |
2532 NULL, NULL, NULL, &dummy, true, | |
2533 &inh, parms); | |
1904 } | 2534 } |
1905 | 2535 |
1906 /* DECL is an instantiation of an inheriting constructor template. Deduce | 2536 /* DECL is an instantiation of an inheriting constructor template. Deduce |
1907 the correct exception-specification and deletedness for this particular | 2537 the correct exception-specification and deletedness for this particular |
1908 specialization. */ | 2538 specialization. */ |
1937 } | 2567 } |
1938 | 2568 |
1939 /* Implicitly declare the special function indicated by KIND, as a | 2569 /* Implicitly declare the special function indicated by KIND, as a |
1940 member of TYPE. For copy constructors and assignment operators, | 2570 member of TYPE. For copy constructors and assignment operators, |
1941 CONST_P indicates whether these functions should take a const | 2571 CONST_P indicates whether these functions should take a const |
1942 reference argument or a non-const reference. Returns the | 2572 reference argument or a non-const reference. |
1943 FUNCTION_DECL for the implicitly declared function. */ | 2573 Returns the FUNCTION_DECL for the implicitly declared function. */ |
1944 | 2574 |
1945 tree | 2575 tree |
1946 implicitly_declare_fn (special_function_kind kind, tree type, | 2576 implicitly_declare_fn (special_function_kind kind, tree type, |
1947 bool const_p, tree inherited_ctor, | 2577 bool const_p, tree pattern_fn, |
1948 tree inherited_parms) | 2578 tree inherited_parms) |
1949 { | 2579 { |
1950 tree fn; | 2580 tree fn; |
1951 tree parameter_types = void_list_node; | 2581 tree parameter_types = void_list_node; |
1952 tree return_type; | 2582 tree return_type; |
1954 tree raises = empty_except_spec; | 2584 tree raises = empty_except_spec; |
1955 tree rhs_parm_type = NULL_TREE; | 2585 tree rhs_parm_type = NULL_TREE; |
1956 tree this_parm; | 2586 tree this_parm; |
1957 tree name; | 2587 tree name; |
1958 HOST_WIDE_INT saved_processing_template_decl; | 2588 HOST_WIDE_INT saved_processing_template_decl; |
1959 bool deleted_p; | 2589 bool deleted_p = false; |
1960 bool constexpr_p; | 2590 bool constexpr_p = false; |
2591 bool friend_p = (kind == sfk_comparison && DECL_FRIEND_P (pattern_fn)); | |
2592 tree inherited_ctor = (kind == sfk_inheriting_constructor | |
2593 ? pattern_fn : NULL_TREE); | |
1961 | 2594 |
1962 /* Because we create declarations for implicitly declared functions | 2595 /* Because we create declarations for implicitly declared functions |
1963 lazily, we may be creating the declaration for a member of TYPE | 2596 lazily, we may be creating the declaration for a member of TYPE |
1964 while in some completely different context. However, TYPE will | 2597 while in some completely different context. However, TYPE will |
1965 never be a dependent class (because we never want to do lookups | 2598 never be a dependent class (because we never want to do lookups |
1982 return_type = build_pointer_type (type); | 2615 return_type = build_pointer_type (type); |
1983 } | 2616 } |
1984 else | 2617 else |
1985 return_type = void_type_node; | 2618 return_type = void_type_node; |
1986 | 2619 |
2620 int this_quals = TYPE_UNQUALIFIED; | |
1987 switch (kind) | 2621 switch (kind) |
1988 { | 2622 { |
1989 case sfk_destructor: | 2623 case sfk_destructor: |
1990 /* Destructor. */ | 2624 /* Destructor. */ |
1991 name = dtor_identifier; | 2625 name = dtor_identifier; |
2025 | 2659 |
2026 parameter_types = tree_cons (NULL_TREE, rhs_parm_type, parameter_types); | 2660 parameter_types = tree_cons (NULL_TREE, rhs_parm_type, parameter_types); |
2027 } | 2661 } |
2028 break; | 2662 break; |
2029 } | 2663 } |
2664 | |
2665 case sfk_comparison: | |
2666 /* If the class definition does not explicitly declare an == operator | |
2667 function, but declares a defaulted three-way comparison operator | |
2668 function, an == operator function is declared implicitly with the same | |
2669 access as the three-way comparison operator function. | |
2670 | |
2671 The implicitly-declared == operator for a class X is an inline member | |
2672 and is defined as defaulted in the definition of X. | |
2673 | |
2674 If the three-way comparison operator function is declared as a | |
2675 non-static const member, the implicitly-declared == operator function | |
2676 is a member of the form | |
2677 | |
2678 bool X::operator==(const X&) const; | |
2679 | |
2680 Otherwise, the implicitly-declared == operator function is of the form | |
2681 | |
2682 friend bool operator==(const X&, const X&); */ | |
2683 /* No other comparison operator is implicitly declared. */ | |
2684 name = ovl_op_identifier (false, EQ_EXPR); | |
2685 return_type = boolean_type_node; | |
2686 rhs_parm_type = cp_build_qualified_type (type, TYPE_QUAL_CONST); | |
2687 rhs_parm_type = cp_build_reference_type (rhs_parm_type, false); | |
2688 parameter_types = tree_cons (NULL_TREE, rhs_parm_type, parameter_types); | |
2689 if (friend_p) | |
2690 parameter_types = tree_cons (NULL_TREE, rhs_parm_type, parameter_types); | |
2691 this_quals = TYPE_QUAL_CONST; | |
2692 break; | |
2693 | |
2030 default: | 2694 default: |
2031 gcc_unreachable (); | 2695 gcc_unreachable (); |
2032 } | 2696 } |
2033 | 2697 |
2034 bool trivial_p = false; | 2698 bool trivial_p = false; |
2042 constexpr_p = DECL_DECLARED_CONSTEXPR_P (inherited_ctor); | 2706 constexpr_p = DECL_DECLARED_CONSTEXPR_P (inherited_ctor); |
2043 } | 2707 } |
2044 else if (cxx_dialect >= cxx11) | 2708 else if (cxx_dialect >= cxx11) |
2045 { | 2709 { |
2046 raises = noexcept_deferred_spec; | 2710 raises = noexcept_deferred_spec; |
2047 synthesized_method_walk (type, kind, const_p, NULL, &trivial_p, | 2711 if (kind != sfk_comparison) |
2048 &deleted_p, &constexpr_p, false, | 2712 synthesized_method_walk (type, kind, const_p, NULL, &trivial_p, |
2049 &inherited_ctor, inherited_parms); | 2713 &deleted_p, &constexpr_p, false, |
2714 &inherited_ctor, inherited_parms); | |
2050 } | 2715 } |
2051 else | 2716 else |
2052 synthesized_method_walk (type, kind, const_p, &raises, &trivial_p, | 2717 synthesized_method_walk (type, kind, const_p, &raises, &trivial_p, |
2053 &deleted_p, &constexpr_p, false, | 2718 &deleted_p, &constexpr_p, false, |
2054 &inherited_ctor, inherited_parms); | 2719 &inherited_ctor, inherited_parms); |
2055 /* Don't bother marking a deleted constructor as constexpr. */ | 2720 /* Don't bother marking a deleted constructor as constexpr. */ |
2056 if (deleted_p) | 2721 if (deleted_p) |
2057 constexpr_p = false; | 2722 constexpr_p = false; |
2058 /* A trivial copy/move constructor is also a constexpr constructor, | 2723 /* A trivial copy/move constructor is also a constexpr constructor, |
2059 unless the class has virtual bases (7.1.5p4). */ | 2724 unless the class has virtual bases (7.1.5p4). */ |
2060 else if (trivial_p && cxx_dialect >= cxx11 | 2725 else if (trivial_p |
2726 && cxx_dialect >= cxx11 | |
2061 && (kind == sfk_copy_constructor | 2727 && (kind == sfk_copy_constructor |
2062 || kind == sfk_move_constructor) | 2728 || kind == sfk_move_constructor) |
2063 && !CLASSTYPE_VBASECLASSES (type)) | 2729 && !CLASSTYPE_VBASECLASSES (type)) |
2064 gcc_assert (constexpr_p); | 2730 gcc_assert (constexpr_p); |
2065 | 2731 |
2066 if (!trivial_p && type_has_trivial_fn (type, kind)) | 2732 if (!trivial_p && type_has_trivial_fn (type, kind)) |
2067 type_set_nontrivial_flag (type, kind); | 2733 type_set_nontrivial_flag (type, kind); |
2068 | 2734 |
2069 /* Create the function. */ | 2735 /* Create the function. */ |
2070 fn_type = build_method_type_directly (type, return_type, parameter_types); | 2736 if (friend_p) |
2737 fn_type = build_function_type (return_type, parameter_types); | |
2738 else | |
2739 { | |
2740 tree this_type = cp_build_qualified_type (type, this_quals); | |
2741 fn_type = build_method_type_directly (this_type, return_type, | |
2742 parameter_types); | |
2743 } | |
2744 | |
2071 if (raises) | 2745 if (raises) |
2072 fn_type = build_exception_variant (fn_type, raises); | 2746 { |
2747 if (raises != error_mark_node) | |
2748 fn_type = build_exception_variant (fn_type, raises); | |
2749 else | |
2750 /* Can happen, eg, in C++98 mode for an ill-formed non-static data | |
2751 member initializer (c++/89914). */ | |
2752 gcc_assert (seen_error ()); | |
2753 } | |
2073 fn = build_lang_decl (FUNCTION_DECL, name, fn_type); | 2754 fn = build_lang_decl (FUNCTION_DECL, name, fn_type); |
2074 if (kind != sfk_inheriting_constructor) | 2755 if (kind == sfk_comparison) |
2756 { | |
2757 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (pattern_fn); | |
2758 DECL_MAYBE_DELETED (fn) = true; | |
2759 } | |
2760 else if (kind != sfk_inheriting_constructor) | |
2075 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (TYPE_NAME (type)); | 2761 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (TYPE_NAME (type)); |
2076 | 2762 |
2077 if (!IDENTIFIER_CDTOR_P (name)) | 2763 if (IDENTIFIER_OVL_OP_P (name)) |
2078 /* Assignment operator. */ | 2764 { |
2079 DECL_OVERLOADED_OPERATOR_CODE_RAW (fn) = OVL_OP_NOP_EXPR; | 2765 const ovl_op_info_t *op = IDENTIFIER_OVL_OP_INFO (name); |
2766 DECL_OVERLOADED_OPERATOR_CODE_RAW (fn) = op->ovl_op_code; | |
2767 } | |
2080 else if (IDENTIFIER_CTOR_P (name)) | 2768 else if (IDENTIFIER_CTOR_P (name)) |
2081 DECL_CXX_CONSTRUCTOR_P (fn) = true; | 2769 DECL_CXX_CONSTRUCTOR_P (fn) = true; |
2770 else if (IDENTIFIER_DTOR_P (name)) | |
2771 DECL_CXX_DESTRUCTOR_P (fn) = true; | |
2082 else | 2772 else |
2083 DECL_CXX_DESTRUCTOR_P (fn) = true; | 2773 gcc_unreachable (); |
2084 | 2774 |
2085 SET_DECL_ALIGN (fn, MINIMUM_METHOD_BOUNDARY); | 2775 SET_DECL_ALIGN (fn, MINIMUM_METHOD_BOUNDARY); |
2086 | 2776 |
2087 /* Create the explicit arguments. */ | 2777 /* Create the explicit arguments. */ |
2088 if (rhs_parm_type) | 2778 if (rhs_parm_type) |
2093 tree decl = cp_build_parm_decl (fn, NULL_TREE, rhs_parm_type); | 2783 tree decl = cp_build_parm_decl (fn, NULL_TREE, rhs_parm_type); |
2094 TREE_READONLY (decl) = 1; | 2784 TREE_READONLY (decl) = 1; |
2095 retrofit_lang_decl (decl); | 2785 retrofit_lang_decl (decl); |
2096 DECL_PARM_INDEX (decl) = DECL_PARM_LEVEL (decl) = 1; | 2786 DECL_PARM_INDEX (decl) = DECL_PARM_LEVEL (decl) = 1; |
2097 DECL_ARGUMENTS (fn) = decl; | 2787 DECL_ARGUMENTS (fn) = decl; |
2788 if (friend_p) | |
2789 { | |
2790 /* The second parm of friend op==. */ | |
2791 tree decl2 = copy_decl (decl); | |
2792 DECL_CHAIN (decl) = decl2; | |
2793 DECL_PARM_INDEX (decl2) = 2; | |
2794 } | |
2098 } | 2795 } |
2099 else if (kind == sfk_inheriting_constructor) | 2796 else if (kind == sfk_inheriting_constructor) |
2100 { | 2797 { |
2101 tree *p = &DECL_ARGUMENTS (fn); | 2798 tree *p = &DECL_ARGUMENTS (fn); |
2102 int index = 1; | 2799 int index = 1; |
2117 TREE_PROTECTED (fn) = TREE_PROTECTED (inherited_ctor); | 2814 TREE_PROTECTED (fn) = TREE_PROTECTED (inherited_ctor); |
2118 /* Copy constexpr from the inherited constructor even if the | 2815 /* Copy constexpr from the inherited constructor even if the |
2119 inheriting constructor doesn't satisfy the requirements. */ | 2816 inheriting constructor doesn't satisfy the requirements. */ |
2120 constexpr_p = DECL_DECLARED_CONSTEXPR_P (inherited_ctor); | 2817 constexpr_p = DECL_DECLARED_CONSTEXPR_P (inherited_ctor); |
2121 } | 2818 } |
2122 /* Add the "this" parameter. */ | 2819 |
2123 this_parm = build_this_parm (fn, fn_type, TYPE_UNQUALIFIED); | 2820 if (friend_p) |
2124 DECL_CHAIN (this_parm) = DECL_ARGUMENTS (fn); | 2821 DECL_CONTEXT (fn) = DECL_CONTEXT (pattern_fn); |
2125 DECL_ARGUMENTS (fn) = this_parm; | 2822 else |
2126 | 2823 { |
2127 grokclassfn (type, fn, kind == sfk_destructor ? DTOR_FLAG : NO_SPECIAL); | 2824 /* Add the "this" parameter. */ |
2825 this_parm = build_this_parm (fn, fn_type, this_quals); | |
2826 DECL_CHAIN (this_parm) = DECL_ARGUMENTS (fn); | |
2827 DECL_ARGUMENTS (fn) = this_parm; | |
2828 | |
2829 grokclassfn (type, fn, kind == sfk_destructor ? DTOR_FLAG : NO_SPECIAL); | |
2830 } | |
2831 | |
2128 DECL_IN_AGGR_P (fn) = 1; | 2832 DECL_IN_AGGR_P (fn) = 1; |
2129 DECL_ARTIFICIAL (fn) = 1; | 2833 DECL_ARTIFICIAL (fn) = 1; |
2130 DECL_DEFAULTED_FN (fn) = 1; | 2834 DECL_DEFAULTED_FN (fn) = 1; |
2131 if (cxx_dialect >= cxx11) | 2835 if (cxx_dialect >= cxx11) |
2132 { | 2836 { |
2137 DECL_NOT_REALLY_EXTERN (fn) = 1; | 2841 DECL_NOT_REALLY_EXTERN (fn) = 1; |
2138 DECL_DECLARED_INLINE_P (fn) = 1; | 2842 DECL_DECLARED_INLINE_P (fn) = 1; |
2139 set_linkage_according_to_type (type, fn); | 2843 set_linkage_according_to_type (type, fn); |
2140 if (TREE_PUBLIC (fn)) | 2844 if (TREE_PUBLIC (fn)) |
2141 DECL_COMDAT (fn) = 1; | 2845 DECL_COMDAT (fn) = 1; |
2846 if (kind == sfk_comparison && !friend_p) | |
2847 { | |
2848 /* The implicit op== has the same access as the op<=>. */ | |
2849 TREE_PRIVATE (fn) = TREE_PRIVATE (pattern_fn); | |
2850 TREE_PROTECTED (fn) = TREE_PROTECTED (pattern_fn); | |
2851 } | |
2142 rest_of_decl_compilation (fn, namespace_bindings_p (), at_eof); | 2852 rest_of_decl_compilation (fn, namespace_bindings_p (), at_eof); |
2143 gcc_assert (!TREE_USED (fn)); | 2853 gcc_assert (!TREE_USED (fn)); |
2144 | 2854 |
2145 /* Propagate constraints from the inherited constructor. */ | 2855 /* Propagate constraints from the inherited constructor. */ |
2146 if (flag_concepts && inherited_ctor) | 2856 if (flag_concepts && inherited_ctor) |
2178 defaulted_late_check (tree fn) | 2888 defaulted_late_check (tree fn) |
2179 { | 2889 { |
2180 /* Complain about invalid signature for defaulted fn. */ | 2890 /* Complain about invalid signature for defaulted fn. */ |
2181 tree ctx = DECL_CONTEXT (fn); | 2891 tree ctx = DECL_CONTEXT (fn); |
2182 special_function_kind kind = special_function_p (fn); | 2892 special_function_kind kind = special_function_p (fn); |
2893 | |
2894 if (kind == sfk_comparison) | |
2895 { | |
2896 /* If the function was declared constexpr, check that the definition | |
2897 qualifies. Otherwise we can define the function lazily. */ | |
2898 if (DECL_DECLARED_CONSTEXPR_P (fn)) | |
2899 synthesize_method (fn); | |
2900 return; | |
2901 } | |
2902 | |
2183 bool fn_const_p = (copy_fn_p (fn) == 2); | 2903 bool fn_const_p = (copy_fn_p (fn) == 2); |
2184 tree implicit_fn = implicitly_declare_fn (kind, ctx, fn_const_p, | 2904 tree implicit_fn = implicitly_declare_fn (kind, ctx, fn_const_p, |
2185 NULL, NULL); | 2905 NULL, NULL); |
2186 tree eh_spec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (implicit_fn)); | 2906 tree eh_spec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (implicit_fn)); |
2187 | 2907 |
2200 { | 2920 { |
2201 DECL_DELETED_FN (fn) = 1; | 2921 DECL_DELETED_FN (fn) = 1; |
2202 return; | 2922 return; |
2203 } | 2923 } |
2204 | 2924 |
2205 /* 8.4.2/2: An explicitly-defaulted function (...) may have an explicit | 2925 /* If a function is explicitly defaulted on its first declaration without an |
2206 exception-specification only if it is compatible (15.4) with the | 2926 exception-specification, it is implicitly considered to have the same |
2207 exception-specification on the implicit declaration. If a function | 2927 exception-specification as if it had been implicitly declared. */ |
2208 is explicitly defaulted on its first declaration, (...) it is | 2928 if (!TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)) |
2209 implicitly considered to have the same exception-specification as if | 2929 && DECL_DEFAULTED_IN_CLASS_P (fn)) |
2210 it had been implicitly declared. */ | 2930 TREE_TYPE (fn) = build_exception_variant (TREE_TYPE (fn), eh_spec); |
2211 maybe_instantiate_noexcept (fn); | |
2212 tree fn_spec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)); | |
2213 if (!fn_spec) | |
2214 { | |
2215 if (DECL_DEFAULTED_IN_CLASS_P (fn)) | |
2216 TREE_TYPE (fn) = build_exception_variant (TREE_TYPE (fn), eh_spec); | |
2217 } | |
2218 else if (UNEVALUATED_NOEXCEPT_SPEC_P (fn_spec)) | |
2219 /* Equivalent to the implicit spec. */; | |
2220 else if (DECL_DEFAULTED_IN_CLASS_P (fn) | |
2221 && !CLASSTYPE_TEMPLATE_INSTANTIATION (ctx)) | |
2222 /* We can't compare an explicit exception-specification on a | |
2223 constructor defaulted in the class body to the implicit | |
2224 exception-specification until after we've parsed any NSDMI; see | |
2225 after_nsdmi_defaulted_late_checks. */; | |
2226 else | |
2227 { | |
2228 tree eh_spec = get_defaulted_eh_spec (fn); | |
2229 if (!comp_except_specs (fn_spec, eh_spec, ce_normal)) | |
2230 { | |
2231 if (DECL_DEFAULTED_IN_CLASS_P (fn)) | |
2232 DECL_DELETED_FN (fn) = true; | |
2233 else | |
2234 error ("function %q+D defaulted on its redeclaration " | |
2235 "with an exception-specification that differs from " | |
2236 "the implicit exception-specification %qX", fn, eh_spec); | |
2237 } | |
2238 } | |
2239 | 2931 |
2240 if (DECL_DEFAULTED_IN_CLASS_P (fn) | 2932 if (DECL_DEFAULTED_IN_CLASS_P (fn) |
2241 && DECL_DECLARED_CONSTEXPR_P (implicit_fn)) | 2933 && DECL_DECLARED_CONSTEXPR_P (implicit_fn)) |
2242 { | 2934 { |
2243 /* Hmm...should we do this for out-of-class too? Should it be OK to | 2935 /* Hmm...should we do this for out-of-class too? Should it be OK to |
2252 && DECL_DECLARED_CONSTEXPR_P (fn)) | 2944 && DECL_DECLARED_CONSTEXPR_P (fn)) |
2253 { | 2945 { |
2254 if (!CLASSTYPE_TEMPLATE_INSTANTIATION (ctx)) | 2946 if (!CLASSTYPE_TEMPLATE_INSTANTIATION (ctx)) |
2255 { | 2947 { |
2256 error ("explicitly defaulted function %q+D cannot be declared " | 2948 error ("explicitly defaulted function %q+D cannot be declared " |
2257 "as %<constexpr%> because the implicit declaration is not " | 2949 "%qs because the implicit declaration is not %qs:", fn, |
2258 "%<constexpr%>:", fn); | 2950 DECL_IMMEDIATE_FUNCTION_P (fn) ? "consteval" : "constexpr", |
2951 "constexpr"); | |
2259 explain_implicit_non_constexpr (fn); | 2952 explain_implicit_non_constexpr (fn); |
2260 } | 2953 } |
2261 DECL_DECLARED_CONSTEXPR_P (fn) = false; | 2954 DECL_DECLARED_CONSTEXPR_P (fn) = false; |
2262 } | 2955 } |
2263 } | |
2264 | |
2265 /* OK, we've parsed the NSDMI for class T, now we can check any explicit | |
2266 exception-specifications on functions defaulted in the class body. */ | |
2267 | |
2268 void | |
2269 after_nsdmi_defaulted_late_checks (tree t) | |
2270 { | |
2271 if (uses_template_parms (t)) | |
2272 return; | |
2273 if (t == error_mark_node) | |
2274 return; | |
2275 for (tree fn = TYPE_FIELDS (t); fn; fn = DECL_CHAIN (fn)) | |
2276 if (!DECL_ARTIFICIAL (fn) | |
2277 && DECL_DECLARES_FUNCTION_P (fn) | |
2278 && DECL_DEFAULTED_IN_CLASS_P (fn)) | |
2279 { | |
2280 tree fn_spec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)); | |
2281 if (UNEVALUATED_NOEXCEPT_SPEC_P (fn_spec)) | |
2282 continue; | |
2283 | |
2284 tree eh_spec = get_defaulted_eh_spec (fn); | |
2285 if (!comp_except_specs (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)), | |
2286 eh_spec, ce_normal)) | |
2287 DECL_DELETED_FN (fn) = true; | |
2288 } | |
2289 } | 2956 } |
2290 | 2957 |
2291 /* Returns true iff FN can be explicitly defaulted, and gives any | 2958 /* Returns true iff FN can be explicitly defaulted, and gives any |
2292 errors if defaulting FN is ill-formed. */ | 2959 errors if defaulting FN is ill-formed. */ |
2293 | 2960 |
2321 if (copy_fn_p (fn)) | 2988 if (copy_fn_p (fn)) |
2322 kind = sfk_copy_assignment; | 2989 kind = sfk_copy_assignment; |
2323 else if (move_fn_p (fn)) | 2990 else if (move_fn_p (fn)) |
2324 kind = sfk_move_assignment; | 2991 kind = sfk_move_assignment; |
2325 } | 2992 } |
2993 else if (DECL_OVERLOADED_OPERATOR_CODE_RAW (fn) >= OVL_OP_EQ_EXPR | |
2994 && DECL_OVERLOADED_OPERATOR_CODE_RAW (fn) <= OVL_OP_SPACESHIP_EXPR) | |
2995 { | |
2996 kind = sfk_comparison; | |
2997 if (!early_check_defaulted_comparison (fn)) | |
2998 return false; | |
2999 } | |
2326 | 3000 |
2327 if (kind == sfk_none) | 3001 if (kind == sfk_none) |
2328 { | 3002 { |
2329 error ("%qD cannot be defaulted", fn); | 3003 error ("%qD cannot be defaulted", fn); |
2330 return false; | 3004 return false; |
2342 /* Avoid do_warn_unused_parameter warnings. */ | 3016 /* Avoid do_warn_unused_parameter warnings. */ |
2343 for (tree p = FUNCTION_FIRST_USER_PARM (fn); p; p = DECL_CHAIN (p)) | 3017 for (tree p = FUNCTION_FIRST_USER_PARM (fn); p; p = DECL_CHAIN (p)) |
2344 if (DECL_NAME (p)) | 3018 if (DECL_NAME (p)) |
2345 TREE_NO_WARNING (p) = 1; | 3019 TREE_NO_WARNING (p) = 1; |
2346 | 3020 |
2347 if (TYPE_BEING_DEFINED (DECL_CONTEXT (fn))) | 3021 if (current_class_type && TYPE_BEING_DEFINED (current_class_type)) |
2348 /* Defer checking. */; | 3022 /* Defer checking. */; |
2349 else if (!processing_template_decl) | 3023 else if (!processing_template_decl) |
2350 defaulted_late_check (fn); | 3024 defaulted_late_check (fn); |
2351 | 3025 |
2352 return true; | 3026 return true; |
2401 if ((sfk == sfk_copy_assignment || sfk == sfk_copy_constructor) | 3075 if ((sfk == sfk_copy_assignment || sfk == sfk_copy_constructor) |
2402 && cxx_dialect >= cxx11) | 3076 && cxx_dialect >= cxx11) |
2403 { | 3077 { |
2404 if (classtype_has_move_assign_or_move_ctor_p (type, true)) | 3078 if (classtype_has_move_assign_or_move_ctor_p (type, true)) |
2405 DECL_DELETED_FN (fn) = true; | 3079 DECL_DELETED_FN (fn) = true; |
2406 else if (classtype_has_user_copy_or_dtor (type)) | 3080 else if (classtype_has_depr_implicit_copy (type)) |
2407 /* The implicit definition of a copy constructor as defaulted is | 3081 /* The implicit definition of a copy constructor as defaulted is |
2408 deprecated if the class has a user-declared copy assignment operator | 3082 deprecated if the class has a user-declared copy assignment operator |
2409 or a user-declared destructor. The implicit definition of a copy | 3083 or a user-declared destructor. The implicit definition of a copy |
2410 assignment operator as defaulted is deprecated if the class has a | 3084 assignment operator as defaulted is deprecated if the class has a |
2411 user-declared copy constructor or a user-declared destructor (15.4, | 3085 user-declared copy constructor or a user-declared destructor (15.4, |