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

gcc-8.2
author mir3636
date Thu, 25 Oct 2018 07:37:49 +0900
parents 04ced10e8804
children 1830386684a0
comparison
equal deleted inserted replaced
111:04ced10e8804 131:84e7813d76e9
1 /* Process declarations and variables for C++ compiler. 1 /* Process declarations and variables for C++ compiler.
2 Copyright (C) 1988-2017 Free Software Foundation, Inc. 2 Copyright (C) 1988-2018 Free Software Foundation, Inc.
3 Contributed by Michael Tiemann (tiemann@cygnus.com) 3 Contributed by Michael Tiemann (tiemann@cygnus.com)
4 4
5 This file is part of GCC. 5 This file is part of GCC.
6 6
7 GCC is free software; you can redistribute it and/or modify 7 GCC is free software; you can redistribute it and/or modify
46 #include "c-family/c-objc.h" 46 #include "c-family/c-objc.h"
47 #include "c-family/c-pragma.h" 47 #include "c-family/c-pragma.h"
48 #include "c-family/c-ubsan.h" 48 #include "c-family/c-ubsan.h"
49 #include "debug.h" 49 #include "debug.h"
50 #include "plugin.h" 50 #include "plugin.h"
51 #include "cilk.h"
52 #include "builtins.h" 51 #include "builtins.h"
53 #include "gimplify.h" 52 #include "gimplify.h"
54 #include "asan.h" 53 #include "asan.h"
54 #include "gcc-rich-location.h"
55 #include "langhooks.h"
55 56
56 /* Possible cases of bad specifiers type used by bad_specifiers. */ 57 /* Possible cases of bad specifiers type used by bad_specifiers. */
57 enum bad_spec_place { 58 enum bad_spec_place {
58 BSP_VAR, /* variable */ 59 BSP_VAR, /* variable */
59 BSP_PARM, /* parameter */ 60 BSP_PARM, /* parameter */
63 64
64 static const char *redeclaration_error_message (tree, tree); 65 static const char *redeclaration_error_message (tree, tree);
65 66
66 static int decl_jump_unsafe (tree); 67 static int decl_jump_unsafe (tree);
67 static void require_complete_types_for_parms (tree); 68 static void require_complete_types_for_parms (tree);
68 static bool ambi_op_p (enum tree_code);
69 static bool unary_op_p (enum tree_code);
70 static void push_local_name (tree); 69 static void push_local_name (tree);
71 static tree grok_reference_init (tree, tree, tree, int); 70 static tree grok_reference_init (tree, tree, tree, int);
72 static tree grokvardecl (tree, tree, tree, const cp_decl_specifier_seq *, 71 static tree grokvardecl (tree, tree, tree, const cp_decl_specifier_seq *,
73 int, int, int, bool, int, tree); 72 int, int, int, bool, int, tree);
74 static int check_static_variable_definition (tree, tree); 73 static void check_static_variable_definition (tree, tree);
75 static void record_unknown_type (tree, const char *); 74 static void record_unknown_type (tree, const char *);
76 static tree builtin_function_1 (tree, tree, bool); 75 static tree builtin_function_1 (tree, tree, bool);
77 static int member_function_or_else (tree, tree, enum overload_flags); 76 static int member_function_or_else (tree, tree, enum overload_flags);
78 static void check_for_uninitialized_const_var (tree);
79 static tree local_variable_p_walkfn (tree *, int *, void *); 77 static tree local_variable_p_walkfn (tree *, int *, void *);
80 static const char *tag_name (enum tag_types); 78 static const char *tag_name (enum tag_types);
81 static tree lookup_and_check_tag (enum tag_types, tree, tag_scope, bool); 79 static tree lookup_and_check_tag (enum tag_types, tree, tag_scope, bool);
82 static void maybe_deduce_size_from_array_init (tree, tree); 80 static void maybe_deduce_size_from_array_init (tree, tree);
83 static void layout_var_decl (tree); 81 static void layout_var_decl (tree);
578 Put it into forward order, just for cleanliness. */ 576 Put it into forward order, just for cleanliness. */
579 tree decls; 577 tree decls;
580 tree subblocks; 578 tree subblocks;
581 tree block; 579 tree block;
582 tree decl; 580 tree decl;
583 int leaving_for_scope;
584 scope_kind kind; 581 scope_kind kind;
585 unsigned ix;
586 582
587 bool subtime = timevar_cond_start (TV_NAME_LOOKUP); 583 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
588 restart: 584 restart:
589 585
590 block = NULL_TREE; 586 block = NULL_TREE;
640 636
641 /* In each subblock, record that this is its superior. */ 637 /* In each subblock, record that this is its superior. */
642 if (keep >= 0) 638 if (keep >= 0)
643 for (link = subblocks; link; link = BLOCK_CHAIN (link)) 639 for (link = subblocks; link; link = BLOCK_CHAIN (link))
644 BLOCK_SUPERCONTEXT (link) = block; 640 BLOCK_SUPERCONTEXT (link) = block;
645
646 /* We still support the old for-scope rules, whereby the variables
647 in a init statement were in scope after the for-statement ended.
648 We only use the new rules if flag_new_for_scope is nonzero. */
649 leaving_for_scope
650 = current_binding_level->kind == sk_for && flag_new_for_scope == 1;
651 641
652 /* Before we remove the declarations first check for unused variables. */ 642 /* Before we remove the declarations first check for unused variables. */
653 if ((warn_unused_variable || warn_unused_but_set_variable) 643 if ((warn_unused_variable || warn_unused_but_set_variable)
654 && current_binding_level->kind != sk_template_parms 644 && current_binding_level->kind != sk_template_parms
655 && !processing_template_decl) 645 && !processing_template_decl)
684 warning_at (DECL_SOURCE_LOCATION (decl), 674 warning_at (DECL_SOURCE_LOCATION (decl),
685 OPT_Wunused_variable, "unused variable %qD", decl); 675 OPT_Wunused_variable, "unused variable %qD", decl);
686 } 676 }
687 else if (DECL_CONTEXT (decl) == current_function_decl 677 else if (DECL_CONTEXT (decl) == current_function_decl
688 // For -Wunused-but-set-variable leave references alone. 678 // For -Wunused-but-set-variable leave references alone.
689 && TREE_CODE (TREE_TYPE (decl)) != REFERENCE_TYPE 679 && !TYPE_REF_P (TREE_TYPE (decl))
690 && errorcount == unused_but_set_errorcount) 680 && errorcount == unused_but_set_errorcount)
691 { 681 {
692 if (!DECL_NAME (decl) && DECL_DECOMPOSITION_P (decl)) 682 if (!DECL_NAME (decl) && DECL_DECOMPOSITION_P (decl))
693 warning_at (DECL_SOURCE_LOCATION (decl), 683 warning_at (DECL_SOURCE_LOCATION (decl),
694 OPT_Wunused_but_set_variable, "structured " 684 OPT_Wunused_but_set_variable, "structured "
706 for (link = decls; link; link = TREE_CHAIN (link)) 696 for (link = decls; link; link = TREE_CHAIN (link))
707 { 697 {
708 decl = TREE_CODE (link) == TREE_LIST ? TREE_VALUE (link) : link; 698 decl = TREE_CODE (link) == TREE_LIST ? TREE_VALUE (link) : link;
709 tree name = OVL_NAME (decl); 699 tree name = OVL_NAME (decl);
710 700
711 if (leaving_for_scope && VAR_P (decl)
712 /* It's hard to make this ARM compatibility hack play nicely with
713 lambdas, and it really isn't necessary in C++11 mode. */
714 && cxx_dialect < cxx11
715 && name)
716 {
717 cxx_binding *ob = outer_binding (name,
718 IDENTIFIER_BINDING (name),
719 /*class_p=*/true);
720 tree ns_binding = NULL_TREE;
721 if (!ob)
722 ns_binding = get_namespace_binding (current_namespace, name);
723
724 if (ob && ob->scope == current_binding_level->level_chain)
725 /* We have something like:
726
727 int i;
728 for (int i; ;);
729
730 and we are leaving the `for' scope. There's no reason to
731 keep the binding of the inner `i' in this case. */
732 ;
733 else if ((ob && (TREE_CODE (ob->value) == TYPE_DECL))
734 || (ns_binding && TREE_CODE (ns_binding) == TYPE_DECL))
735 /* Here, we have something like:
736
737 typedef int I;
738
739 void f () {
740 for (int I; ;);
741 }
742
743 We must pop the for-scope binding so we know what's a
744 type and what isn't. */
745 ;
746 else
747 {
748 /* Mark this VAR_DECL as dead so that we can tell we left it
749 there only for backward compatibility. */
750 DECL_DEAD_FOR_LOCAL (link) = 1;
751
752 /* Keep track of what should have happened when we
753 popped the binding. */
754 if (ob && ob->value)
755 {
756 SET_DECL_SHADOWED_FOR_VAR (link, ob->value);
757 DECL_HAS_SHADOWED_FOR_VAR_P (link) = 1;
758 }
759
760 /* Add it to the list of dead variables in the next
761 outermost binding to that we can remove these when we
762 leave that binding. */
763 vec_safe_push (
764 current_binding_level->level_chain->dead_vars_from_for,
765 link);
766
767 /* Although we don't pop the cxx_binding, we do clear
768 its SCOPE since the scope is going away now. */
769 IDENTIFIER_BINDING (name)->scope
770 = current_binding_level->level_chain;
771
772 /* Don't remove the binding. */
773 name = NULL_TREE;
774 }
775 }
776 /* Remove the binding. */ 701 /* Remove the binding. */
777 if (TREE_CODE (decl) == LABEL_DECL) 702 if (TREE_CODE (decl) == LABEL_DECL)
778 pop_local_label (name, decl); 703 pop_local_label (name, decl);
779 else 704 else
780 pop_local_binding (name, decl); 705 pop_local_binding (name, decl);
781 } 706 }
782
783 /* Remove declarations for any `for' variables from inner scopes
784 that we kept around. */
785 FOR_EACH_VEC_SAFE_ELT_REVERSE (current_binding_level->dead_vars_from_for,
786 ix, decl)
787 pop_local_binding (DECL_NAME (decl), decl);
788 707
789 /* Restore the IDENTIFIER_TYPE_VALUEs. */ 708 /* Restore the IDENTIFIER_TYPE_VALUEs. */
790 for (link = current_binding_level->type_shadowed; 709 for (link = current_binding_level->type_shadowed;
791 link; link = TREE_CHAIN (link)) 710 link; link = TREE_CHAIN (link))
792 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (link), TREE_VALUE (link)); 711 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (link), TREE_VALUE (link));
993 For C++, we must compare the parameter list so that `int' can match 912 For C++, we must compare the parameter list so that `int' can match
994 `int&' in a parameter position, but `int&' is not confused with 913 `int&' in a parameter position, but `int&' is not confused with
995 `const int&'. */ 914 `const int&'. */
996 915
997 int 916 int
998 decls_match (tree newdecl, tree olddecl) 917 decls_match (tree newdecl, tree olddecl, bool record_versions /* = true */)
999 { 918 {
1000 int types_match; 919 int types_match;
1001 920
1002 if (newdecl == olddecl) 921 if (newdecl == olddecl)
1003 return 1; 922 return 1;
1047 r2 = fndecl_declared_return_type (olddecl); 966 r2 = fndecl_declared_return_type (olddecl);
1048 967
1049 if (same_type_p (TREE_TYPE (f1), r2)) 968 if (same_type_p (TREE_TYPE (f1), r2))
1050 { 969 {
1051 if (!prototype_p (f2) && DECL_EXTERN_C_P (olddecl) 970 if (!prototype_p (f2) && DECL_EXTERN_C_P (olddecl)
1052 && (DECL_BUILT_IN (olddecl) 971 && fndecl_built_in_p (olddecl))
1053 #ifndef NO_IMPLICIT_EXTERN_C
1054 || (DECL_IN_SYSTEM_HEADER (newdecl) && !DECL_CLASS_SCOPE_P (newdecl))
1055 || (DECL_IN_SYSTEM_HEADER (olddecl) && !DECL_CLASS_SCOPE_P (olddecl))
1056 #endif
1057 ))
1058 { 972 {
1059 types_match = self_promoting_args_p (p1); 973 types_match = self_promoting_args_p (p1);
1060 if (p1 == void_list_node) 974 if (p1 == void_list_node)
1061 TREE_TYPE (newdecl) = TREE_TYPE (olddecl); 975 TREE_TYPE (newdecl) = TREE_TYPE (olddecl);
1062 } 976 }
1063 #ifndef NO_IMPLICIT_EXTERN_C
1064 else if (!prototype_p (f1)
1065 && (DECL_EXTERN_C_P (olddecl)
1066 && DECL_IN_SYSTEM_HEADER (olddecl)
1067 && !DECL_CLASS_SCOPE_P (olddecl))
1068 && (DECL_EXTERN_C_P (newdecl)
1069 && DECL_IN_SYSTEM_HEADER (newdecl)
1070 && !DECL_CLASS_SCOPE_P (newdecl)))
1071 {
1072 types_match = self_promoting_args_p (p2);
1073 TREE_TYPE (newdecl) = TREE_TYPE (olddecl);
1074 }
1075 #endif
1076 else 977 else
1077 types_match = 978 types_match =
1078 compparms (p1, p2) 979 compparms (p1, p2)
1079 && type_memfn_rqual (f1) == type_memfn_rqual (f2) 980 && type_memfn_rqual (f1) == type_memfn_rqual (f2)
1080 && (TYPE_ATTRIBUTES (TREE_TYPE (newdecl)) == NULL_TREE 981 && (TYPE_ATTRIBUTES (TREE_TYPE (newdecl)) == NULL_TREE
1088 of the same function. Disallow extern "C" functions to be 989 of the same function. Disallow extern "C" functions to be
1089 versions for now. */ 990 versions for now. */
1090 if (types_match 991 if (types_match
1091 && !DECL_EXTERN_C_P (newdecl) 992 && !DECL_EXTERN_C_P (newdecl)
1092 && !DECL_EXTERN_C_P (olddecl) 993 && !DECL_EXTERN_C_P (olddecl)
1093 && maybe_version_functions (newdecl, olddecl)) 994 && record_versions
995 && maybe_version_functions (newdecl, olddecl,
996 (!DECL_FUNCTION_VERSIONED (newdecl)
997 || !DECL_FUNCTION_VERSIONED (olddecl))))
1094 return 0; 998 return 0;
1095 } 999 }
1096 else if (TREE_CODE (newdecl) == TEMPLATE_DECL) 1000 else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
1097 { 1001 {
1098 tree oldres = DECL_TEMPLATE_RESULT (olddecl); 1002 tree oldres = DECL_TEMPLATE_RESULT (olddecl);
1146 1050
1147 return types_match; 1051 return types_match;
1148 } 1052 }
1149 1053
1150 /* NEWDECL and OLDDECL have identical signatures. If they are 1054 /* NEWDECL and OLDDECL have identical signatures. If they are
1151 different versions adjust them and return true. */ 1055 different versions adjust them and return true.
1056 If RECORD is set to true, record function versions. */
1152 1057
1153 bool 1058 bool
1154 maybe_version_functions (tree newdecl, tree olddecl) 1059 maybe_version_functions (tree newdecl, tree olddecl, bool record)
1155 { 1060 {
1156 if (!targetm.target_option.function_versions (newdecl, olddecl)) 1061 if (!targetm.target_option.function_versions (newdecl, olddecl))
1157 return false; 1062 return false;
1158 1063
1159 bool record = false;
1160
1161 if (!DECL_FUNCTION_VERSIONED (olddecl)) 1064 if (!DECL_FUNCTION_VERSIONED (olddecl))
1162 { 1065 {
1163 record = true;
1164 DECL_FUNCTION_VERSIONED (olddecl) = 1; 1066 DECL_FUNCTION_VERSIONED (olddecl) = 1;
1165 if (DECL_ASSEMBLER_NAME_SET_P (olddecl)) 1067 if (DECL_ASSEMBLER_NAME_SET_P (olddecl))
1166 mangle_decl (olddecl); 1068 mangle_decl (olddecl);
1167 } 1069 }
1168 1070
1169 if (!DECL_FUNCTION_VERSIONED (newdecl)) 1071 if (!DECL_FUNCTION_VERSIONED (newdecl))
1170 { 1072 {
1171 record = true;
1172 DECL_FUNCTION_VERSIONED (newdecl) = 1; 1073 DECL_FUNCTION_VERSIONED (newdecl) = 1;
1173 if (DECL_ASSEMBLER_NAME_SET_P (newdecl)) 1074 if (DECL_ASSEMBLER_NAME_SET_P (newdecl))
1174 mangle_decl (newdecl); 1075 mangle_decl (newdecl);
1175 } 1076 }
1176 1077
1177 /* Only record if at least one was not already versions. */
1178 if (record) 1078 if (record)
1179 cgraph_node::record_function_versions (olddecl, newdecl); 1079 cgraph_node::record_function_versions (olddecl, newdecl);
1180 1080
1181 return true; 1081 return true;
1182 } 1082 }
1213 /* It's OK to declare a builtin function as `static'. */ 1113 /* It's OK to declare a builtin function as `static'. */
1214 if (TREE_CODE (olddecl) == FUNCTION_DECL 1114 if (TREE_CODE (olddecl) == FUNCTION_DECL
1215 && DECL_ARTIFICIAL (olddecl)) 1115 && DECL_ARTIFICIAL (olddecl))
1216 return; 1116 return;
1217 1117
1118 auto_diagnostic_group d;
1218 if (permerror (DECL_SOURCE_LOCATION (newdecl), 1119 if (permerror (DECL_SOURCE_LOCATION (newdecl),
1219 "%qD was declared %<extern%> and later %<static%>", newdecl)) 1120 "%qD was declared %<extern%> and later %<static%>", newdecl))
1220 inform (DECL_SOURCE_LOCATION (olddecl), 1121 inform (DECL_SOURCE_LOCATION (olddecl),
1221 "previous declaration of %qD", olddecl); 1122 "previous declaration of %qD", olddecl);
1222 } 1123 }
1235 /* Two default specs are equivalent, don't force evaluation. */ 1136 /* Two default specs are equivalent, don't force evaluation. */
1236 if (UNEVALUATED_NOEXCEPT_SPEC_P (new_exceptions) 1137 if (UNEVALUATED_NOEXCEPT_SPEC_P (new_exceptions)
1237 && UNEVALUATED_NOEXCEPT_SPEC_P (old_exceptions)) 1138 && UNEVALUATED_NOEXCEPT_SPEC_P (old_exceptions))
1238 return; 1139 return;
1239 1140
1240 maybe_instantiate_noexcept (new_decl); 1141 if (!type_dependent_expression_p (old_decl))
1241 maybe_instantiate_noexcept (old_decl); 1142 {
1143 maybe_instantiate_noexcept (new_decl);
1144 maybe_instantiate_noexcept (old_decl);
1145 }
1242 new_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (new_decl)); 1146 new_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (new_decl));
1243 old_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (old_decl)); 1147 old_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (old_decl));
1244 1148
1245 /* [except.spec] 1149 /* [except.spec]
1246 1150
1253 { 1157 {
1254 const char *const msg 1158 const char *const msg
1255 = G_("declaration of %qF has a different exception specifier"); 1159 = G_("declaration of %qF has a different exception specifier");
1256 bool complained = true; 1160 bool complained = true;
1257 location_t new_loc = DECL_SOURCE_LOCATION (new_decl); 1161 location_t new_loc = DECL_SOURCE_LOCATION (new_decl);
1162 auto_diagnostic_group d;
1258 if (DECL_IN_SYSTEM_HEADER (old_decl)) 1163 if (DECL_IN_SYSTEM_HEADER (old_decl))
1259 complained = pedwarn (new_loc, OPT_Wsystem_headers, msg, new_decl); 1164 complained = pedwarn (new_loc, OPT_Wsystem_headers, msg, new_decl);
1260 else if (!flag_exceptions) 1165 else if (!flag_exceptions)
1261 /* We used to silently permit mismatched eh specs with 1166 /* We used to silently permit mismatched eh specs with
1262 -fno-exceptions, so make them a pedwarn now. */ 1167 -fno-exceptions, so make them a pedwarn now. */
1283 if (DECL_DECLARED_CONSTEXPR_P (old_decl) 1188 if (DECL_DECLARED_CONSTEXPR_P (old_decl)
1284 == DECL_DECLARED_CONSTEXPR_P (new_decl)) 1189 == DECL_DECLARED_CONSTEXPR_P (new_decl))
1285 return true; 1190 return true;
1286 if (TREE_CODE (old_decl) == FUNCTION_DECL) 1191 if (TREE_CODE (old_decl) == FUNCTION_DECL)
1287 { 1192 {
1288 if (DECL_BUILT_IN (old_decl)) 1193 if (fndecl_built_in_p (old_decl))
1289 { 1194 {
1290 /* Hide a built-in declaration. */ 1195 /* Hide a built-in declaration. */
1291 DECL_DECLARED_CONSTEXPR_P (old_decl) 1196 DECL_DECLARED_CONSTEXPR_P (old_decl)
1292 = DECL_DECLARED_CONSTEXPR_P (new_decl); 1197 = DECL_DECLARED_CONSTEXPR_P (new_decl);
1293 return true; 1198 return true;
1358 "arguments", decl); 1263 "arguments", decl);
1359 return; 1264 return;
1360 } 1265 }
1361 } 1266 }
1362 1267
1268 /* NEWDECL is a redeclaration of a function or function template OLDDECL,
1269 in any case represented as FUNCTION_DECLs (the DECL_TEMPLATE_RESULTs of
1270 the TEMPLATE_DECLs in case of function templates). This function is used
1271 to enforce the final part of C++17 11.3.6/4, about a single declaration:
1272 "If a friend declaration specifies a default argument expression, that
1273 declaration shall be a definition and shall be the only declaration of
1274 the function or function template in the translation unit." */
1275
1276 static void
1277 check_no_redeclaration_friend_default_args (tree olddecl, tree newdecl,
1278 bool olddecl_hidden_friend_p)
1279 {
1280 if (!olddecl_hidden_friend_p && !DECL_FRIEND_P (newdecl))
1281 return;
1282
1283 tree t1 = FUNCTION_FIRST_USER_PARMTYPE (olddecl);
1284 tree t2 = FUNCTION_FIRST_USER_PARMTYPE (newdecl);
1285
1286 for (; t1 && t1 != void_list_node;
1287 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1288 if ((olddecl_hidden_friend_p && TREE_PURPOSE (t1))
1289 || (DECL_FRIEND_P (newdecl) && TREE_PURPOSE (t2)))
1290 {
1291 auto_diagnostic_group d;
1292 if (permerror (DECL_SOURCE_LOCATION (newdecl),
1293 "friend declaration of %q#D specifies default "
1294 "arguments and isn't the only declaration", newdecl))
1295 inform (DECL_SOURCE_LOCATION (olddecl),
1296 "previous declaration of %q#D", olddecl);
1297 return;
1298 }
1299 }
1300
1301 /* Merge tree bits that correspond to attributes noreturn, nothrow,
1302 const, malloc, and pure from NEWDECL with those of OLDDECL. */
1303
1304 static void
1305 merge_attribute_bits (tree newdecl, tree olddecl)
1306 {
1307 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1308 TREE_THIS_VOLATILE (olddecl) |= TREE_THIS_VOLATILE (newdecl);
1309 TREE_NOTHROW (newdecl) |= TREE_NOTHROW (olddecl);
1310 TREE_NOTHROW (olddecl) |= TREE_NOTHROW (newdecl);
1311 TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
1312 TREE_READONLY (olddecl) |= TREE_READONLY (newdecl);
1313 DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
1314 DECL_IS_MALLOC (olddecl) |= DECL_IS_MALLOC (newdecl);
1315 DECL_PURE_P (newdecl) |= DECL_PURE_P (olddecl);
1316 DECL_PURE_P (olddecl) |= DECL_PURE_P (newdecl);
1317 DECL_UNINLINABLE (newdecl) |= DECL_UNINLINABLE (olddecl);
1318 DECL_UNINLINABLE (olddecl) |= DECL_UNINLINABLE (newdecl);
1319 }
1320
1363 #define GNU_INLINE_P(fn) (DECL_DECLARED_INLINE_P (fn) \ 1321 #define GNU_INLINE_P(fn) (DECL_DECLARED_INLINE_P (fn) \
1364 && lookup_attribute ("gnu_inline", \ 1322 && lookup_attribute ("gnu_inline", \
1365 DECL_ATTRIBUTES (fn))) 1323 DECL_ATTRIBUTES (fn)))
1366 1324
1367 /* If NEWDECL is a redeclaration of OLDDECL, merge the declarations. 1325 /* If NEWDECL is a redeclaration of OLDDECL, merge the declarations.
1376 tree 1334 tree
1377 duplicate_decls (tree newdecl, tree olddecl, bool newdecl_is_friend) 1335 duplicate_decls (tree newdecl, tree olddecl, bool newdecl_is_friend)
1378 { 1336 {
1379 unsigned olddecl_uid = DECL_UID (olddecl); 1337 unsigned olddecl_uid = DECL_UID (olddecl);
1380 int olddecl_friend = 0, types_match = 0, hidden_friend = 0; 1338 int olddecl_friend = 0, types_match = 0, hidden_friend = 0;
1339 int olddecl_hidden_friend = 0;
1381 int new_defines_function = 0; 1340 int new_defines_function = 0;
1382 tree new_template_info; 1341 tree new_template_info;
1342 location_t olddecl_loc = DECL_SOURCE_LOCATION (olddecl);
1343 location_t newdecl_loc = DECL_SOURCE_LOCATION (newdecl);
1383 1344
1384 if (newdecl == olddecl) 1345 if (newdecl == olddecl)
1385 return olddecl; 1346 return olddecl;
1386 1347
1387 types_match = decls_match (newdecl, olddecl); 1348 types_match = decls_match (newdecl, olddecl);
1393 about the same declaration, so just pretend the types match here. */ 1354 about the same declaration, so just pretend the types match here. */
1394 if (TREE_TYPE (newdecl) == error_mark_node 1355 if (TREE_TYPE (newdecl) == error_mark_node
1395 || TREE_TYPE (olddecl) == error_mark_node) 1356 || TREE_TYPE (olddecl) == error_mark_node)
1396 return error_mark_node; 1357 return error_mark_node;
1397 1358
1398 if (UDLIT_OPER_P (DECL_NAME (newdecl)) 1359 if (DECL_NAME (newdecl)
1360 && DECL_NAME (olddecl)
1361 && UDLIT_OPER_P (DECL_NAME (newdecl))
1399 && UDLIT_OPER_P (DECL_NAME (olddecl))) 1362 && UDLIT_OPER_P (DECL_NAME (olddecl)))
1400 { 1363 {
1401 if (TREE_CODE (newdecl) == TEMPLATE_DECL 1364 if (TREE_CODE (newdecl) == TEMPLATE_DECL
1402 && TREE_CODE (olddecl) != TEMPLATE_DECL 1365 && TREE_CODE (olddecl) != TEMPLATE_DECL
1403 && check_raw_literal_operator (olddecl)) 1366 && check_raw_literal_operator (olddecl))
1404 error ("literal operator template %q+D conflicts with" 1367 error_at (newdecl_loc,
1405 " raw literal operator %qD", newdecl, olddecl); 1368 "literal operator template %qD conflicts with"
1369 " raw literal operator %qD", newdecl, olddecl);
1406 else if (TREE_CODE (newdecl) != TEMPLATE_DECL 1370 else if (TREE_CODE (newdecl) != TEMPLATE_DECL
1407 && TREE_CODE (olddecl) == TEMPLATE_DECL 1371 && TREE_CODE (olddecl) == TEMPLATE_DECL
1408 && check_raw_literal_operator (newdecl)) 1372 && check_raw_literal_operator (newdecl))
1409 error ("raw literal operator %q+D conflicts with" 1373 error_at (newdecl_loc,
1410 " literal operator template %qD", newdecl, olddecl); 1374 "raw literal operator %qD conflicts with"
1411 } 1375 " literal operator template %qD", newdecl, olddecl);
1376 }
1377
1378 /* True to merge attributes between the declarations, false to
1379 set OLDDECL's attributes to those of NEWDECL (for template
1380 explicit specializations that specify their own attributes
1381 independent of those specified for the primary template). */
1382 const bool merge_attr = (TREE_CODE (newdecl) != FUNCTION_DECL
1383 || !DECL_TEMPLATE_SPECIALIZATION (newdecl)
1384 || DECL_TEMPLATE_SPECIALIZATION (olddecl));
1412 1385
1413 if (DECL_P (olddecl) 1386 if (DECL_P (olddecl)
1414 && TREE_CODE (newdecl) == FUNCTION_DECL 1387 && TREE_CODE (newdecl) == FUNCTION_DECL
1415 && TREE_CODE (olddecl) == FUNCTION_DECL 1388 && TREE_CODE (olddecl) == FUNCTION_DECL
1389 && merge_attr
1416 && diagnose_mismatched_attributes (olddecl, newdecl)) 1390 && diagnose_mismatched_attributes (olddecl, newdecl))
1417 { 1391 {
1418 if (DECL_INITIAL (olddecl)) 1392 if (DECL_INITIAL (olddecl))
1419 inform (DECL_SOURCE_LOCATION (olddecl), 1393 inform (olddecl_loc,
1420 "previous definition of %qD was here", olddecl); 1394 "previous definition of %qD was here", olddecl);
1421 else 1395 else
1422 inform (DECL_SOURCE_LOCATION (olddecl), 1396 inform (olddecl_loc,
1423 "previous declaration of %qD was here", olddecl); 1397 "previous declaration of %qD was here", olddecl);
1424 } 1398 }
1425 1399
1426 /* Check for redeclaration and other discrepancies. */ 1400 /* Check for redeclaration and other discrepancies. */
1427 if (TREE_CODE (olddecl) == FUNCTION_DECL 1401 if (TREE_CODE (olddecl) == FUNCTION_DECL
1434 explicitly declared. */ 1408 explicitly declared. */
1435 if (DECL_ANTICIPATED (olddecl)) 1409 if (DECL_ANTICIPATED (olddecl))
1436 { 1410 {
1437 if (TREE_PUBLIC (newdecl) 1411 if (TREE_PUBLIC (newdecl)
1438 && CP_DECL_CONTEXT (newdecl) == global_namespace) 1412 && CP_DECL_CONTEXT (newdecl) == global_namespace)
1439 warning_at (DECL_SOURCE_LOCATION (newdecl), 1413 warning_at (newdecl_loc,
1440 OPT_Wbuiltin_declaration_mismatch, 1414 OPT_Wbuiltin_declaration_mismatch,
1441 "built-in function %qD declared as non-function", 1415 "built-in function %qD declared as non-function",
1442 newdecl); 1416 newdecl);
1443 return NULL_TREE; 1417 return NULL_TREE;
1444 } 1418 }
1446 /* If you declare a built-in or predefined function name as static, 1420 /* If you declare a built-in or predefined function name as static,
1447 the old definition is overridden, but optionally warn this was a 1421 the old definition is overridden, but optionally warn this was a
1448 bad choice of name. */ 1422 bad choice of name. */
1449 if (! TREE_PUBLIC (newdecl)) 1423 if (! TREE_PUBLIC (newdecl))
1450 { 1424 {
1451 warning (OPT_Wshadow, 1425 warning_at (newdecl_loc,
1452 DECL_BUILT_IN (olddecl) 1426 OPT_Wshadow,
1453 ? G_("shadowing built-in function %q#D") 1427 fndecl_built_in_p (olddecl)
1454 : G_("shadowing library function %q#D"), olddecl); 1428 ? G_("shadowing built-in function %q#D")
1429 : G_("shadowing library function %q#D"), olddecl);
1455 /* Discard the old built-in function. */ 1430 /* Discard the old built-in function. */
1456 return NULL_TREE; 1431 return NULL_TREE;
1457 } 1432 }
1458 /* If the built-in is not ansi, then programs can override 1433 /* If the built-in is not ansi, then programs can override
1459 it even globally without an error. */ 1434 it even globally without an error. */
1460 else if (! DECL_BUILT_IN (olddecl)) 1435 else if (! fndecl_built_in_p (olddecl))
1461 warning_at (DECL_SOURCE_LOCATION (newdecl), 0, 1436 warning_at (newdecl_loc, 0,
1462 "library function %q#D redeclared as non-function %q#D", 1437 "library function %q#D redeclared as non-function %q#D",
1463 olddecl, newdecl); 1438 olddecl, newdecl);
1464 else 1439 else
1465 error ("declaration of %q+#D conflicts with built-in " 1440 error_at (newdecl_loc,
1466 "declaration %q#D", newdecl, olddecl); 1441 "declaration of %q#D conflicts with built-in "
1442 "declaration %q#D", newdecl, olddecl);
1467 return NULL_TREE; 1443 return NULL_TREE;
1468 } 1444 }
1469 else if (DECL_OMP_DECLARE_REDUCTION_P (olddecl)) 1445 else if (DECL_OMP_DECLARE_REDUCTION_P (olddecl))
1470 { 1446 {
1471 gcc_assert (DECL_OMP_DECLARE_REDUCTION_P (newdecl)); 1447 gcc_assert (DECL_OMP_DECLARE_REDUCTION_P (newdecl));
1472 error_at (DECL_SOURCE_LOCATION (newdecl), 1448 error_at (newdecl_loc,
1473 "redeclaration of %<pragma omp declare reduction%>"); 1449 "redeclaration of %<pragma omp declare reduction%>");
1474 inform (DECL_SOURCE_LOCATION (olddecl), 1450 inform (olddecl_loc,
1475 "previous %<pragma omp declare reduction%> declaration"); 1451 "previous %<pragma omp declare reduction%> declaration");
1476 return error_mark_node; 1452 return error_mark_node;
1477 } 1453 }
1478 else if (!types_match) 1454 else if (!types_match)
1479 { 1455 {
1528 if (! same_type_p (TREE_VALUE (t1), TREE_VALUE (t2))) 1504 if (! same_type_p (TREE_VALUE (t1), TREE_VALUE (t2)))
1529 break; 1505 break;
1530 next_arg:; 1506 next_arg:;
1531 } 1507 }
1532 1508
1533 warning_at (DECL_SOURCE_LOCATION (newdecl), 1509 warning_at (newdecl_loc,
1534 OPT_Wbuiltin_declaration_mismatch, 1510 OPT_Wbuiltin_declaration_mismatch,
1535 "declaration of %q#D conflicts with built-in " 1511 "declaration of %q#D conflicts with built-in "
1536 "declaration %q#D", newdecl, olddecl); 1512 "declaration %q#D", newdecl, olddecl);
1537 } 1513 }
1538 else if ((DECL_EXTERN_C_P (newdecl) 1514 else if ((DECL_EXTERN_C_P (newdecl)
1539 && DECL_EXTERN_C_P (olddecl)) 1515 && DECL_EXTERN_C_P (olddecl))
1540 || compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)), 1516 || compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
1541 TYPE_ARG_TYPES (TREE_TYPE (olddecl)))) 1517 TYPE_ARG_TYPES (TREE_TYPE (olddecl))))
1542 { 1518 {
1519 /* Don't really override olddecl for __* prefixed builtins
1520 except for __[^b]*_chk, the compiler might be using those
1521 explicitly. */
1522 if (fndecl_built_in_p (olddecl))
1523 {
1524 tree id = DECL_NAME (olddecl);
1525 const char *name = IDENTIFIER_POINTER (id);
1526 size_t len;
1527
1528 if (name[0] == '_'
1529 && name[1] == '_'
1530 && (strncmp (name + 2, "builtin_",
1531 strlen ("builtin_")) == 0
1532 || (len = strlen (name)) <= strlen ("___chk")
1533 || memcmp (name + len - strlen ("_chk"),
1534 "_chk", strlen ("_chk") + 1) != 0))
1535 {
1536 if (DECL_INITIAL (newdecl))
1537 {
1538 error_at (newdecl_loc,
1539 "definition of %q#D ambiguates built-in "
1540 "declaration %q#D", newdecl, olddecl);
1541 return error_mark_node;
1542 }
1543 auto_diagnostic_group d;
1544 if (permerror (newdecl_loc,
1545 "new declaration %q#D ambiguates built-in"
1546 " declaration %q#D", newdecl, olddecl)
1547 && flag_permissive)
1548 inform (newdecl_loc,
1549 "ignoring the %q#D declaration", newdecl);
1550 return flag_permissive ? olddecl : error_mark_node;
1551 }
1552 }
1553
1543 /* A near match; override the builtin. */ 1554 /* A near match; override the builtin. */
1544 1555
1545 if (TREE_PUBLIC (newdecl)) 1556 if (TREE_PUBLIC (newdecl))
1546 warning_at (DECL_SOURCE_LOCATION (newdecl), 1557 warning_at (newdecl_loc,
1547 OPT_Wbuiltin_declaration_mismatch, 1558 OPT_Wbuiltin_declaration_mismatch,
1548 "new declaration %q#D ambiguates built-in " 1559 "new declaration %q#D ambiguates built-in "
1549 "declaration %q#D", newdecl, olddecl); 1560 "declaration %q#D", newdecl, olddecl);
1550 else 1561 else
1551 warning (OPT_Wshadow, 1562 warning (OPT_Wshadow,
1552 DECL_BUILT_IN (olddecl) 1563 fndecl_built_in_p (olddecl)
1553 ? G_("shadowing built-in function %q#D") 1564 ? G_("shadowing built-in function %q#D")
1554 : G_("shadowing library function %q#D"), olddecl); 1565 : G_("shadowing library function %q#D"), olddecl);
1555 } 1566 }
1556 else 1567 else
1557 /* Discard the old built-in function. */ 1568 /* Discard the old built-in function. */
1558 return NULL_TREE; 1569 return NULL_TREE;
1559 1570
1637 } 1648 }
1638 1649
1639 error ("%q#D redeclared as different kind of symbol", newdecl); 1650 error ("%q#D redeclared as different kind of symbol", newdecl);
1640 if (TREE_CODE (olddecl) == TREE_LIST) 1651 if (TREE_CODE (olddecl) == TREE_LIST)
1641 olddecl = TREE_VALUE (olddecl); 1652 olddecl = TREE_VALUE (olddecl);
1642 inform (DECL_SOURCE_LOCATION (olddecl), 1653 inform (olddecl_loc,
1643 "previous declaration %q#D", olddecl); 1654 "previous declaration %q#D", olddecl);
1644 1655
1645 return error_mark_node; 1656 return error_mark_node;
1646 } 1657 }
1647 else if (!types_match) 1658 else if (!types_match)
1657 any other template, class, function, object, namespace, value, 1668 any other template, class, function, object, namespace, value,
1658 or type in the same scope. */ 1669 or type in the same scope. */
1659 if (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == TYPE_DECL 1670 if (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == TYPE_DECL
1660 || TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL) 1671 || TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)
1661 { 1672 {
1662 error ("conflicting declaration of template %q+#D", newdecl); 1673 error_at (newdecl_loc,
1663 inform (DECL_SOURCE_LOCATION (olddecl), 1674 "conflicting declaration of template %q#D", newdecl);
1675 inform (olddecl_loc,
1664 "previous declaration %q#D", olddecl); 1676 "previous declaration %q#D", olddecl);
1665 return error_mark_node; 1677 return error_mark_node;
1666 } 1678 }
1667 else if (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == FUNCTION_DECL 1679 else if (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == FUNCTION_DECL
1668 && TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == FUNCTION_DECL 1680 && TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == FUNCTION_DECL
1676 TREE_TYPE (TREE_TYPE (olddecl))) 1688 TREE_TYPE (TREE_TYPE (olddecl)))
1677 // Template functions can also be disambiguated by 1689 // Template functions can also be disambiguated by
1678 // constraints. 1690 // constraints.
1679 && equivalently_constrained (olddecl, newdecl)) 1691 && equivalently_constrained (olddecl, newdecl))
1680 { 1692 {
1681 error ("ambiguating new declaration %q+#D", newdecl); 1693 error_at (newdecl_loc, "ambiguating new declaration %q#D",
1682 inform (DECL_SOURCE_LOCATION (olddecl), 1694 newdecl);
1695 inform (olddecl_loc,
1683 "old declaration %q#D", olddecl); 1696 "old declaration %q#D", olddecl);
1684 } 1697 }
1685 else if (check_concept_refinement (olddecl, newdecl)) 1698 else if (check_concept_refinement (olddecl, newdecl))
1686 return error_mark_node; 1699 return error_mark_node;
1687 return NULL_TREE; 1700 return NULL_TREE;
1688 } 1701 }
1689 if (TREE_CODE (newdecl) == FUNCTION_DECL) 1702 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1690 { 1703 {
1691 if (DECL_EXTERN_C_P (newdecl) && DECL_EXTERN_C_P (olddecl)) 1704 if (DECL_EXTERN_C_P (newdecl) && DECL_EXTERN_C_P (olddecl))
1692 { 1705 {
1693 error ("conflicting declaration of C function %q+#D", 1706 error_at (newdecl_loc,
1694 newdecl); 1707 "conflicting declaration of C function %q#D",
1695 inform (DECL_SOURCE_LOCATION (olddecl), 1708 newdecl);
1709 inform (olddecl_loc,
1696 "previous declaration %q#D", olddecl); 1710 "previous declaration %q#D", olddecl);
1697 return NULL_TREE; 1711 return NULL_TREE;
1698 } 1712 }
1699 /* For function versions, params and types match, but they 1713 /* For function versions, params and types match, but they
1700 are not ambiguous. */ 1714 are not ambiguous. */
1704 && compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)), 1718 && compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
1705 TYPE_ARG_TYPES (TREE_TYPE (olddecl))) 1719 TYPE_ARG_TYPES (TREE_TYPE (olddecl)))
1706 // And the same constraints. 1720 // And the same constraints.
1707 && equivalently_constrained (newdecl, olddecl)) 1721 && equivalently_constrained (newdecl, olddecl))
1708 { 1722 {
1709 error ("ambiguating new declaration of %q+#D", newdecl); 1723 error_at (newdecl_loc,
1710 inform (DECL_SOURCE_LOCATION (olddecl), 1724 "ambiguating new declaration of %q#D", newdecl);
1725 inform (olddecl_loc,
1711 "old declaration %q#D", olddecl); 1726 "old declaration %q#D", olddecl);
1712 return error_mark_node; 1727 return error_mark_node;
1713 } 1728 }
1714 else 1729 else
1715 return NULL_TREE; 1730 return NULL_TREE;
1716 } 1731 }
1717 else 1732 else
1718 { 1733 {
1719 error ("conflicting declaration %q+#D", newdecl); 1734 error_at (newdecl_loc, "conflicting declaration %q#D", newdecl);
1720 inform (DECL_SOURCE_LOCATION (olddecl), 1735 inform (olddecl_loc,
1721 "previous declaration as %q#D", olddecl); 1736 "previous declaration as %q#D", olddecl);
1722 return error_mark_node; 1737 return error_mark_node;
1723 } 1738 }
1724 } 1739 }
1725 else if (TREE_CODE (newdecl) == FUNCTION_DECL 1740 else if (TREE_CODE (newdecl) == FUNCTION_DECL
1770 else 1785 else
1771 { 1786 {
1772 const char *errmsg = redeclaration_error_message (newdecl, olddecl); 1787 const char *errmsg = redeclaration_error_message (newdecl, olddecl);
1773 if (errmsg) 1788 if (errmsg)
1774 { 1789 {
1775 error_at (DECL_SOURCE_LOCATION (newdecl), errmsg, newdecl); 1790 auto_diagnostic_group d;
1791 error_at (newdecl_loc, errmsg, newdecl);
1776 if (DECL_NAME (olddecl) != NULL_TREE) 1792 if (DECL_NAME (olddecl) != NULL_TREE)
1777 inform (DECL_SOURCE_LOCATION (olddecl), 1793 inform (olddecl_loc,
1778 (DECL_INITIAL (olddecl) && namespace_bindings_p ()) 1794 (DECL_INITIAL (olddecl) && namespace_bindings_p ())
1779 ? G_("%q#D previously defined here") 1795 ? G_("%q#D previously defined here")
1780 : G_("%q#D previously declared here"), olddecl); 1796 : G_("%q#D previously declared here"), olddecl);
1781 return error_mark_node; 1797 return error_mark_node;
1782 } 1798 }
1784 && DECL_INITIAL (olddecl) != NULL_TREE 1800 && DECL_INITIAL (olddecl) != NULL_TREE
1785 && !prototype_p (TREE_TYPE (olddecl)) 1801 && !prototype_p (TREE_TYPE (olddecl))
1786 && prototype_p (TREE_TYPE (newdecl))) 1802 && prototype_p (TREE_TYPE (newdecl)))
1787 { 1803 {
1788 /* Prototype decl follows defn w/o prototype. */ 1804 /* Prototype decl follows defn w/o prototype. */
1789 if (warning_at (DECL_SOURCE_LOCATION (newdecl), 0, 1805 auto_diagnostic_group d;
1806 if (warning_at (newdecl_loc, 0,
1790 "prototype specified for %q#D", newdecl)) 1807 "prototype specified for %q#D", newdecl))
1791 inform (DECL_SOURCE_LOCATION (olddecl), 1808 inform (olddecl_loc,
1792 "previous non-prototype definition here"); 1809 "previous non-prototype definition here");
1793 } 1810 }
1794 else if (VAR_OR_FUNCTION_DECL_P (olddecl) 1811 else if (VAR_OR_FUNCTION_DECL_P (olddecl)
1795 && DECL_LANGUAGE (newdecl) != DECL_LANGUAGE (olddecl)) 1812 && DECL_LANGUAGE (newdecl) != DECL_LANGUAGE (olddecl))
1796 { 1813 {
1825 retrofit_lang_decl (newdecl); 1842 retrofit_lang_decl (newdecl);
1826 SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl)); 1843 SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl));
1827 } 1844 }
1828 else 1845 else
1829 { 1846 {
1830 error ("conflicting declaration of %q+#D with %qL linkage", 1847 auto_diagnostic_group d;
1831 newdecl, DECL_LANGUAGE (newdecl)); 1848 error_at (newdecl_loc,
1832 inform (DECL_SOURCE_LOCATION (olddecl), 1849 "conflicting declaration of %q#D with %qL linkage",
1850 newdecl, DECL_LANGUAGE (newdecl));
1851 inform (olddecl_loc,
1833 "previous declaration with %qL linkage", 1852 "previous declaration with %qL linkage",
1834 DECL_LANGUAGE (olddecl)); 1853 DECL_LANGUAGE (olddecl));
1835 } 1854 }
1836 } 1855 }
1837 1856
1858 1877
1859 for (; t1 && t1 != void_list_node; 1878 for (; t1 && t1 != void_list_node;
1860 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2), i++) 1879 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2), i++)
1861 if (TREE_PURPOSE (t1) && TREE_PURPOSE (t2)) 1880 if (TREE_PURPOSE (t1) && TREE_PURPOSE (t2))
1862 { 1881 {
1863 if (1 == simple_cst_equal (TREE_PURPOSE (t1), 1882 if (simple_cst_equal (TREE_PURPOSE (t1),
1864 TREE_PURPOSE (t2))) 1883 TREE_PURPOSE (t2)) == 1)
1865 { 1884 {
1866 if (permerror (input_location, 1885 auto_diagnostic_group d;
1886 if (permerror (newdecl_loc,
1867 "default argument given for parameter " 1887 "default argument given for parameter "
1868 "%d of %q#D", i, newdecl)) 1888 "%d of %q#D", i, newdecl))
1869 inform (DECL_SOURCE_LOCATION (olddecl), 1889 inform (olddecl_loc,
1870 "previous specification in %q#D here", 1890 "previous specification in %q#D here",
1871 olddecl); 1891 olddecl);
1872 } 1892 }
1873 else 1893 else
1874 { 1894 {
1875 error ("default argument given for parameter %d " 1895 auto_diagnostic_group d;
1876 "of %q#D", i, newdecl); 1896 error_at (newdecl_loc,
1877 inform (DECL_SOURCE_LOCATION (olddecl), 1897 "default argument given for parameter %d "
1898 "of %q#D", i, newdecl);
1899 inform (olddecl_loc,
1878 "previous specification in %q#D here", 1900 "previous specification in %q#D here",
1879 olddecl); 1901 olddecl);
1880 } 1902 }
1881 } 1903 }
1904
1905 /* C++17 11.3.6/4: "If a friend declaration specifies a default
1906 argument expression, that declaration... shall be the only
1907 declaration of the function or function template in the
1908 translation unit." */
1909 check_no_redeclaration_friend_default_args
1910 (olddecl, newdecl, DECL_HIDDEN_FRIEND_P (olddecl));
1882 } 1911 }
1883 } 1912 }
1884 } 1913 }
1885 1914
1886 /* Do not merge an implicit typedef with an explicit one. In: 1915 /* Do not merge an implicit typedef with an explicit one. In:
1919 DECL_VIRTUAL_P (newdecl) |= DECL_VIRTUAL_P (olddecl); 1948 DECL_VIRTUAL_P (newdecl) |= DECL_VIRTUAL_P (olddecl);
1920 DECL_INVALID_OVERRIDER_P (newdecl) |= DECL_INVALID_OVERRIDER_P (olddecl); 1949 DECL_INVALID_OVERRIDER_P (newdecl) |= DECL_INVALID_OVERRIDER_P (olddecl);
1921 DECL_FINAL_P (newdecl) |= DECL_FINAL_P (olddecl); 1950 DECL_FINAL_P (newdecl) |= DECL_FINAL_P (olddecl);
1922 DECL_OVERRIDE_P (newdecl) |= DECL_OVERRIDE_P (olddecl); 1951 DECL_OVERRIDE_P (newdecl) |= DECL_OVERRIDE_P (olddecl);
1923 DECL_THIS_STATIC (newdecl) |= DECL_THIS_STATIC (olddecl); 1952 DECL_THIS_STATIC (newdecl) |= DECL_THIS_STATIC (olddecl);
1924 if (DECL_OVERLOADED_OPERATOR_P (olddecl) != ERROR_MARK) 1953 if (DECL_OVERLOADED_OPERATOR_P (olddecl))
1925 SET_OVERLOADED_OPERATOR_CODE 1954 DECL_OVERLOADED_OPERATOR_CODE_RAW (newdecl)
1926 (newdecl, DECL_OVERLOADED_OPERATOR_P (olddecl)); 1955 = DECL_OVERLOADED_OPERATOR_CODE_RAW (olddecl);
1927 new_defines_function = DECL_INITIAL (newdecl) != NULL_TREE; 1956 new_defines_function = DECL_INITIAL (newdecl) != NULL_TREE;
1928 1957
1929 /* Optionally warn about more than one declaration for the same 1958 /* Optionally warn about more than one declaration for the same
1930 name, but don't warn about a function declaration followed by a 1959 name, but don't warn about a function declaration followed by a
1931 definition. */ 1960 definition. */
1937 && ! (newdecl_is_friend || DECL_FRIEND_P (olddecl)) 1966 && ! (newdecl_is_friend || DECL_FRIEND_P (olddecl))
1938 /* Don't warn about declaration followed by specialization. */ 1967 /* Don't warn about declaration followed by specialization. */
1939 && (! DECL_TEMPLATE_SPECIALIZATION (newdecl) 1968 && (! DECL_TEMPLATE_SPECIALIZATION (newdecl)
1940 || DECL_TEMPLATE_SPECIALIZATION (olddecl))) 1969 || DECL_TEMPLATE_SPECIALIZATION (olddecl)))
1941 { 1970 {
1942 if (warning_at (DECL_SOURCE_LOCATION (newdecl), 1971 auto_diagnostic_group d;
1972 if (warning_at (newdecl_loc,
1943 OPT_Wredundant_decls, 1973 OPT_Wredundant_decls,
1944 "redundant redeclaration of %qD in same scope", 1974 "redundant redeclaration of %qD in same scope",
1945 newdecl)) 1975 newdecl))
1946 inform (DECL_SOURCE_LOCATION (olddecl), 1976 inform (olddecl_loc,
1947 "previous declaration of %qD", olddecl); 1977 "previous declaration of %qD", olddecl);
1948 } 1978 }
1949 1979
1950 if (!(DECL_TEMPLATE_INSTANTIATION (olddecl) 1980 if (!(DECL_TEMPLATE_INSTANTIATION (olddecl)
1951 && DECL_TEMPLATE_SPECIALIZATION (newdecl))) 1981 && DECL_TEMPLATE_SPECIALIZATION (newdecl)))
1952 { 1982 {
1953 if (DECL_DELETED_FN (newdecl)) 1983 if (DECL_DELETED_FN (newdecl))
1954 { 1984 {
1955 error ("deleted definition of %q+D", newdecl); 1985 auto_diagnostic_group d;
1956 inform (DECL_SOURCE_LOCATION (olddecl), 1986 error_at (newdecl_loc, "deleted definition of %qD", newdecl);
1987 inform (olddecl_loc,
1957 "previous declaration of %qD", olddecl); 1988 "previous declaration of %qD", olddecl);
1958 } 1989 }
1959 DECL_DELETED_FN (newdecl) |= DECL_DELETED_FN (olddecl); 1990 DECL_DELETED_FN (newdecl) |= DECL_DELETED_FN (olddecl);
1960 } 1991 }
1961 } 1992 }
1972 = CLASSTYPE_FRIEND_CLASSES (oldtype); 2003 = CLASSTYPE_FRIEND_CLASSES (oldtype);
1973 2004
1974 DECL_ORIGINAL_TYPE (newdecl) = DECL_ORIGINAL_TYPE (olddecl); 2005 DECL_ORIGINAL_TYPE (newdecl) = DECL_ORIGINAL_TYPE (olddecl);
1975 } 2006 }
1976 2007
1977 /* Copy all the DECL_... slots specified in the new decl 2008 /* Copy all the DECL_... slots specified in the new decl except for
1978 except for any that we copy here from the old type. */ 2009 any that we copy here from the old type. */
1979 DECL_ATTRIBUTES (newdecl) 2010 if (merge_attr)
1980 = (*targetm.merge_decl_attributes) (olddecl, newdecl); 2011 DECL_ATTRIBUTES (newdecl)
2012 = (*targetm.merge_decl_attributes) (olddecl, newdecl);
2013 else
2014 DECL_ATTRIBUTES (olddecl) = DECL_ATTRIBUTES (newdecl);
1981 2015
1982 if (DECL_DECLARES_FUNCTION_P (olddecl) && DECL_DECLARES_FUNCTION_P (newdecl)) 2016 if (DECL_DECLARES_FUNCTION_P (olddecl) && DECL_DECLARES_FUNCTION_P (newdecl))
1983 { 2017 {
1984 olddecl_friend = DECL_FRIEND_P (olddecl); 2018 olddecl_friend = DECL_FRIEND_P (olddecl);
2019 olddecl_hidden_friend = DECL_HIDDEN_FRIEND_P (olddecl);
1985 hidden_friend = (DECL_ANTICIPATED (olddecl) 2020 hidden_friend = (DECL_ANTICIPATED (olddecl)
1986 && DECL_HIDDEN_FRIEND_P (olddecl) 2021 && DECL_HIDDEN_FRIEND_P (olddecl)
1987 && newdecl_is_friend); 2022 && newdecl_is_friend);
1988 if (!hidden_friend) 2023 if (!hidden_friend)
1989 { 2024 {
1992 } 2027 }
1993 } 2028 }
1994 2029
1995 if (TREE_CODE (newdecl) == TEMPLATE_DECL) 2030 if (TREE_CODE (newdecl) == TEMPLATE_DECL)
1996 { 2031 {
1997 tree old_result; 2032 tree old_result = DECL_TEMPLATE_RESULT (olddecl);
1998 tree new_result; 2033 tree new_result = DECL_TEMPLATE_RESULT (newdecl);
1999 old_result = DECL_TEMPLATE_RESULT (olddecl);
2000 new_result = DECL_TEMPLATE_RESULT (newdecl);
2001 TREE_TYPE (olddecl) = TREE_TYPE (old_result); 2034 TREE_TYPE (olddecl) = TREE_TYPE (old_result);
2002 DECL_TEMPLATE_SPECIALIZATIONS (olddecl) 2035 DECL_TEMPLATE_SPECIALIZATIONS (olddecl)
2003 = chainon (DECL_TEMPLATE_SPECIALIZATIONS (olddecl), 2036 = chainon (DECL_TEMPLATE_SPECIALIZATIONS (olddecl),
2004 DECL_TEMPLATE_SPECIALIZATIONS (newdecl)); 2037 DECL_TEMPLATE_SPECIALIZATIONS (newdecl));
2005 2038
2006 DECL_ATTRIBUTES (old_result) 2039 DECL_ATTRIBUTES (old_result)
2007 = (*targetm.merge_decl_attributes) (old_result, new_result); 2040 = (*targetm.merge_decl_attributes) (old_result, new_result);
2008 2041
2009 if (DECL_FUNCTION_TEMPLATE_P (newdecl)) 2042 if (DECL_FUNCTION_TEMPLATE_P (newdecl))
2010 { 2043 {
2011 /* Per C++11 8.3.6/4, default arguments cannot be added in later
2012 declarations of a function template. */
2013 if (DECL_SOURCE_LOCATION (newdecl) 2044 if (DECL_SOURCE_LOCATION (newdecl)
2014 != DECL_SOURCE_LOCATION (olddecl)) 2045 != DECL_SOURCE_LOCATION (olddecl))
2015 check_redeclaration_no_default_args (newdecl); 2046 {
2047 /* Per C++11 8.3.6/4, default arguments cannot be added in
2048 later declarations of a function template. */
2049 check_redeclaration_no_default_args (newdecl);
2050 /* C++17 11.3.6/4: "If a friend declaration specifies a default
2051 argument expression, that declaration... shall be the only
2052 declaration of the function or function template in the
2053 translation unit." */
2054 check_no_redeclaration_friend_default_args
2055 (old_result, new_result, olddecl_hidden_friend);
2056 }
2016 2057
2017 check_default_args (newdecl); 2058 check_default_args (newdecl);
2018 2059
2019 if (GNU_INLINE_P (old_result) != GNU_INLINE_P (new_result) 2060 if (GNU_INLINE_P (old_result) != GNU_INLINE_P (new_result)
2020 && DECL_INITIAL (new_result)) 2061 && DECL_INITIAL (new_result))
2039 DECL_DECLARED_INLINE_P (old_result) 2080 DECL_DECLARED_INLINE_P (old_result)
2040 |= DECL_DECLARED_INLINE_P (new_result); 2081 |= DECL_DECLARED_INLINE_P (new_result);
2041 DECL_DISREGARD_INLINE_LIMITS (old_result) 2082 DECL_DISREGARD_INLINE_LIMITS (old_result)
2042 |= DECL_DISREGARD_INLINE_LIMITS (new_result); 2083 |= DECL_DISREGARD_INLINE_LIMITS (new_result);
2043 check_redeclaration_exception_specification (newdecl, olddecl); 2084 check_redeclaration_exception_specification (newdecl, olddecl);
2085
2086 merge_attribute_bits (new_result, old_result);
2044 } 2087 }
2045 } 2088 }
2046 2089
2047 /* If the new declaration is a definition, update the file and 2090 /* If the new declaration is a definition, update the file and
2048 line information on the declaration, and also make 2091 line information on the declaration, and also make
2102 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (remove); 2145 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (remove);
2103 break; 2146 break;
2104 } 2147 }
2105 } 2148 }
2106 } 2149 }
2150 else if (merge_attr)
2151 newtype = merge_types (TREE_TYPE (newdecl), TREE_TYPE (olddecl));
2107 else 2152 else
2108 /* Merge the data types specified in the two decls. */ 2153 newtype = TREE_TYPE (newdecl);
2109 newtype = merge_types (TREE_TYPE (newdecl), TREE_TYPE (olddecl));
2110 2154
2111 if (VAR_P (newdecl)) 2155 if (VAR_P (newdecl))
2112 { 2156 {
2113 DECL_THIS_EXTERN (newdecl) |= DECL_THIS_EXTERN (olddecl); 2157 DECL_THIS_EXTERN (newdecl) |= DECL_THIS_EXTERN (olddecl);
2114 /* For already initialized vars, TREE_READONLY could have been 2158 /* For already initialized vars, TREE_READONLY could have been
2120 && !TREE_READONLY (olddecl)) 2164 && !TREE_READONLY (olddecl))
2121 TREE_READONLY (newdecl) = 0; 2165 TREE_READONLY (newdecl) = 0;
2122 DECL_INITIALIZED_P (newdecl) |= DECL_INITIALIZED_P (olddecl); 2166 DECL_INITIALIZED_P (newdecl) |= DECL_INITIALIZED_P (olddecl);
2123 DECL_NONTRIVIALLY_INITIALIZED_P (newdecl) 2167 DECL_NONTRIVIALLY_INITIALIZED_P (newdecl)
2124 |= DECL_NONTRIVIALLY_INITIALIZED_P (olddecl); 2168 |= DECL_NONTRIVIALLY_INITIALIZED_P (olddecl);
2169 if (DECL_DEPENDENT_INIT_P (olddecl))
2170 SET_DECL_DEPENDENT_INIT_P (newdecl, true);
2125 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (newdecl) 2171 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (newdecl)
2126 |= DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (olddecl); 2172 |= DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (olddecl);
2127 if (DECL_CLASS_SCOPE_P (olddecl)) 2173 if (DECL_CLASS_SCOPE_P (olddecl))
2128 DECL_DECLARED_CONSTEXPR_P (newdecl) 2174 DECL_DECLARED_CONSTEXPR_P (newdecl)
2129 |= DECL_DECLARED_CONSTEXPR_P (olddecl); 2175 |= DECL_DECLARED_CONSTEXPR_P (olddecl);
2166 || TREE_CODE (newdecl) == FIELD_DECL 2212 || TREE_CODE (newdecl) == FIELD_DECL
2167 || TREE_CODE (newdecl) == TYPE_DECL) 2213 || TREE_CODE (newdecl) == TYPE_DECL)
2168 && !(processing_template_decl && uses_template_parms (newdecl))) 2214 && !(processing_template_decl && uses_template_parms (newdecl)))
2169 layout_decl (newdecl, 0); 2215 layout_decl (newdecl, 0);
2170 2216
2171 /* Merge the type qualifiers. */
2172 if (TREE_READONLY (newdecl))
2173 TREE_READONLY (olddecl) = 1;
2174 if (TREE_THIS_VOLATILE (newdecl))
2175 TREE_THIS_VOLATILE (olddecl) = 1;
2176 if (TREE_NOTHROW (newdecl))
2177 TREE_NOTHROW (olddecl) = 1;
2178
2179 /* Merge deprecatedness. */ 2217 /* Merge deprecatedness. */
2180 if (TREE_DEPRECATED (newdecl)) 2218 if (TREE_DEPRECATED (newdecl))
2181 TREE_DEPRECATED (olddecl) = 1; 2219 TREE_DEPRECATED (olddecl) = 1;
2182 2220
2183 /* Preserve function specific target and optimization options */ 2221 /* Preserve function specific target and optimization options */
2190 2228
2191 if (DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl) 2229 if (DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl)
2192 && !DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl)) 2230 && !DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl))
2193 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl) 2231 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl)
2194 = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl); 2232 = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl);
2233 }
2234 else
2235 {
2236 /* Merge the const type qualifier. */
2237 if (TREE_READONLY (newdecl))
2238 TREE_READONLY (olddecl) = 1;
2239 /* Merge the volatile type qualifier. */
2240 if (TREE_THIS_VOLATILE (newdecl))
2241 TREE_THIS_VOLATILE (olddecl) = 1;
2195 } 2242 }
2196 2243
2197 /* Merge the initialization information. */ 2244 /* Merge the initialization information. */
2198 if (DECL_INITIAL (newdecl) == NULL_TREE 2245 if (DECL_INITIAL (newdecl) == NULL_TREE
2199 && DECL_INITIAL (olddecl) != NULL_TREE) 2246 && DECL_INITIAL (olddecl) != NULL_TREE)
2210 if (TREE_CODE (newdecl) == FUNCTION_DECL) 2257 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2211 { 2258 {
2212 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl) 2259 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
2213 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl); 2260 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
2214 DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl); 2261 DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
2215 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
2216 TREE_NOTHROW (newdecl) |= TREE_NOTHROW (olddecl);
2217 DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
2218 DECL_IS_OPERATOR_NEW (newdecl) |= DECL_IS_OPERATOR_NEW (olddecl); 2262 DECL_IS_OPERATOR_NEW (newdecl) |= DECL_IS_OPERATOR_NEW (olddecl);
2219 DECL_PURE_P (newdecl) |= DECL_PURE_P (olddecl); 2263 DECL_LOOPING_CONST_OR_PURE_P (newdecl)
2220 TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
2221 DECL_LOOPING_CONST_OR_PURE_P (newdecl)
2222 |= DECL_LOOPING_CONST_OR_PURE_P (olddecl); 2264 |= DECL_LOOPING_CONST_OR_PURE_P (olddecl);
2265
2266 if (merge_attr)
2267 merge_attribute_bits (newdecl, olddecl);
2268 else
2269 {
2270 /* Merge the noreturn bit. */
2271 TREE_THIS_VOLATILE (olddecl) = TREE_THIS_VOLATILE (newdecl);
2272 TREE_READONLY (olddecl) = TREE_READONLY (newdecl);
2273 TREE_NOTHROW (olddecl) = TREE_NOTHROW (newdecl);
2274 DECL_IS_MALLOC (olddecl) = DECL_IS_MALLOC (newdecl);
2275 DECL_PURE_P (olddecl) = DECL_PURE_P (newdecl);
2276 }
2223 /* Keep the old RTL. */ 2277 /* Keep the old RTL. */
2224 COPY_DECL_RTL (olddecl, newdecl); 2278 COPY_DECL_RTL (olddecl, newdecl);
2225 } 2279 }
2226 else if (VAR_P (newdecl) 2280 else if (VAR_P (newdecl)
2227 && (DECL_SIZE (olddecl) || !DECL_SIZE (newdecl))) 2281 && (DECL_SIZE (olddecl) || !DECL_SIZE (newdecl)))
2381 appropriate. */ 2435 appropriate. */
2382 DECL_VISIBILITY_SPECIFIED (olddecl) = 0; 2436 DECL_VISIBILITY_SPECIFIED (olddecl) = 0;
2383 2437
2384 /* [temp.expl.spec/14] We don't inline explicit specialization 2438 /* [temp.expl.spec/14] We don't inline explicit specialization
2385 just because the primary template says so. */ 2439 just because the primary template says so. */
2386 2440 gcc_assert (!merge_attr);
2387 /* But still keep DECL_DISREGARD_INLINE_LIMITS in sync with 2441
2388 the always_inline attribute. */ 2442 DECL_DECLARED_INLINE_P (olddecl)
2389 if (DECL_DISREGARD_INLINE_LIMITS (olddecl) 2443 = DECL_DECLARED_INLINE_P (newdecl);
2390 && !DECL_DISREGARD_INLINE_LIMITS (newdecl)) 2444
2391 { 2445 DECL_DISREGARD_INLINE_LIMITS (olddecl)
2392 if (DECL_DECLARED_INLINE_P (newdecl)) 2446 = DECL_DISREGARD_INLINE_LIMITS (newdecl);
2393 DECL_DISREGARD_INLINE_LIMITS (newdecl) = true; 2447
2394 else 2448 DECL_UNINLINABLE (olddecl) = DECL_UNINLINABLE (newdecl);
2395 DECL_ATTRIBUTES (newdecl)
2396 = remove_attribute ("always_inline",
2397 DECL_ATTRIBUTES (newdecl));
2398 }
2399 } 2449 }
2400 else if (new_defines_function && DECL_INITIAL (olddecl)) 2450 else if (new_defines_function && DECL_INITIAL (olddecl))
2401 { 2451 {
2402 /* Never inline re-defined extern inline functions. 2452 /* Never inline re-defined extern inline functions.
2403 FIXME: this could be better handled by keeping both 2453 FIXME: this could be better handled by keeping both
2452 DECL_RESULT (olddecl) = DECL_RESULT (newdecl); 2502 DECL_RESULT (olddecl) = DECL_RESULT (newdecl);
2453 } 2503 }
2454 /* If redeclaring a builtin function, it stays built in 2504 /* If redeclaring a builtin function, it stays built in
2455 if newdecl is a gnu_inline definition, or if newdecl is just 2505 if newdecl is a gnu_inline definition, or if newdecl is just
2456 a declaration. */ 2506 a declaration. */
2457 if (DECL_BUILT_IN (olddecl) 2507 if (fndecl_built_in_p (olddecl)
2458 && (new_defines_function ? GNU_INLINE_P (newdecl) : types_match)) 2508 && (new_defines_function ? GNU_INLINE_P (newdecl) : types_match))
2459 { 2509 {
2460 DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl); 2510 DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
2461 DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl); 2511 DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
2462 /* If we're keeping the built-in definition, keep the rtl, 2512 /* If we're keeping the built-in definition, keep the rtl,
2508 /* Warn about conflicting visibility specifications. */ 2558 /* Warn about conflicting visibility specifications. */
2509 if (DECL_VISIBILITY_SPECIFIED (olddecl) 2559 if (DECL_VISIBILITY_SPECIFIED (olddecl)
2510 && DECL_VISIBILITY_SPECIFIED (newdecl) 2560 && DECL_VISIBILITY_SPECIFIED (newdecl)
2511 && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl)) 2561 && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl))
2512 { 2562 {
2513 if (warning_at (DECL_SOURCE_LOCATION (newdecl), OPT_Wattributes, 2563 auto_diagnostic_group d;
2564 if (warning_at (newdecl_loc, OPT_Wattributes,
2514 "%qD: visibility attribute ignored because it " 2565 "%qD: visibility attribute ignored because it "
2515 "conflicts with previous declaration", newdecl)) 2566 "conflicts with previous declaration", newdecl))
2516 inform (DECL_SOURCE_LOCATION (olddecl), 2567 inform (olddecl_loc,
2517 "previous declaration of %qD", olddecl); 2568 "previous declaration of %qD", olddecl);
2518 } 2569 }
2519 /* Choose the declaration which specified visibility. */ 2570 /* Choose the declaration which specified visibility. */
2520 if (DECL_VISIBILITY_SPECIFIED (olddecl)) 2571 if (DECL_VISIBILITY_SPECIFIED (olddecl))
2521 { 2572 {
2920 /* Static data member declared outside a class definition 2971 /* Static data member declared outside a class definition
2921 if the variable is defined within the class with constexpr 2972 if the variable is defined within the class with constexpr
2922 specifier is declaration rather than definition (and 2973 specifier is declaration rather than definition (and
2923 deprecated). */ 2974 deprecated). */
2924 if (cxx_dialect >= cxx17 2975 if (cxx_dialect >= cxx17
2976 && VAR_P (olddecl)
2925 && DECL_CLASS_SCOPE_P (olddecl) 2977 && DECL_CLASS_SCOPE_P (olddecl)
2926 && DECL_DECLARED_CONSTEXPR_P (olddecl) 2978 && DECL_DECLARED_CONSTEXPR_P (olddecl)
2927 && !DECL_INITIAL (newdecl)) 2979 && !DECL_INITIAL (newdecl))
2928 { 2980 {
2929 DECL_EXTERNAL (newdecl) = 1; 2981 DECL_EXTERNAL (newdecl) = 1;
2930 /* For now, only warn with explicit -Wdeprecated. */ 2982 /* For now, only warn with explicit -Wdeprecated. */
2931 if (global_options_set.x_warn_deprecated 2983 if (global_options_set.x_warn_deprecated)
2932 && warning_at (DECL_SOURCE_LOCATION (newdecl), OPT_Wdeprecated, 2984 {
2933 "redundant redeclaration of %<constexpr%> static " 2985 auto_diagnostic_group d;
2934 "data member %qD", newdecl)) 2986 if (warning_at (DECL_SOURCE_LOCATION (newdecl), OPT_Wdeprecated,
2935 inform (DECL_SOURCE_LOCATION (olddecl), 2987 "redundant redeclaration of %<constexpr%> "
2936 "previous declaration of %qD", olddecl); 2988 "static data member %qD", newdecl))
2989 inform (DECL_SOURCE_LOCATION (olddecl),
2990 "previous declaration of %qD", olddecl);
2991 }
2937 return NULL; 2992 return NULL;
2938 } 2993 }
2939 2994
2940 /* Reject two definitions. */ 2995 /* Reject two definitions. */
2941 return G_("redefinition of %q#D"); 2996 return G_("redefinition of %q#D");
3131 continue; 3186 continue;
3132 3187
3133 if (!identified) 3188 if (!identified)
3134 { 3189 {
3135 complained = identify_goto (decl, input_location, locus, 3190 complained = identify_goto (decl, input_location, locus,
3136 DK_PERMERROR); 3191 problem > 1
3192 ? DK_ERROR : DK_PERMERROR);
3137 identified = 1; 3193 identified = 1;
3138 } 3194 }
3139 if (complained) 3195 if (complained)
3140 { 3196 {
3141 if (problem > 1) 3197 if (problem > 1)
3315 else if (ent->in_catch_scope && !saw_catch) 3371 else if (ent->in_catch_scope && !saw_catch)
3316 inform (input_location, " enters catch block"); 3372 inform (input_location, " enters catch block");
3317 else if (ent->in_transaction_scope) 3373 else if (ent->in_transaction_scope)
3318 inform (input_location, " enters synchronized or atomic statement"); 3374 inform (input_location, " enters synchronized or atomic statement");
3319 else if (ent->in_constexpr_if) 3375 else if (ent->in_constexpr_if)
3320 inform (input_location, " enters constexpr if statement"); 3376 inform (input_location, " enters %<constexpr%> if statement");
3321 } 3377 }
3322 3378
3323 if (ent->in_omp_scope) 3379 if (ent->in_omp_scope)
3324 { 3380 {
3325 if (complained) 3381 if (complained)
3427 of the GNU case range extension. */ 3483 of the GNU case range extension. */
3428 splay_tree cases; 3484 splay_tree cases;
3429 /* Remember whether there was a case value that is outside the 3485 /* Remember whether there was a case value that is outside the
3430 range of the original type of the controlling expression. */ 3486 range of the original type of the controlling expression. */
3431 bool outside_range_p; 3487 bool outside_range_p;
3488 /* Remember whether a default: case label has been seen. */
3489 bool has_default_p;
3490 /* Remember whether a BREAK_STMT has been seen in this SWITCH_STMT. */
3491 bool break_stmt_seen_p;
3492 /* Set if inside of {FOR,DO,WHILE}_BODY nested inside of a switch,
3493 where BREAK_STMT doesn't belong to the SWITCH_STMT. */
3494 bool in_loop_body_p;
3432 }; 3495 };
3433 3496
3434 /* A stack of the currently active switch statements. The innermost 3497 /* A stack of the currently active switch statements. The innermost
3435 switch statement is on the top of the stack. There is no need to 3498 switch statement is on the top of the stack. There is no need to
3436 mark the stack for garbage collection because it is only active 3499 mark the stack for garbage collection because it is only active
3449 p->level = current_binding_level; 3512 p->level = current_binding_level;
3450 p->next = switch_stack; 3513 p->next = switch_stack;
3451 p->switch_stmt = switch_stmt; 3514 p->switch_stmt = switch_stmt;
3452 p->cases = splay_tree_new (case_compare, NULL, NULL); 3515 p->cases = splay_tree_new (case_compare, NULL, NULL);
3453 p->outside_range_p = false; 3516 p->outside_range_p = false;
3517 p->has_default_p = false;
3518 p->break_stmt_seen_p = false;
3519 p->in_loop_body_p = false;
3454 switch_stack = p; 3520 switch_stack = p;
3455 } 3521 }
3456 3522
3457 void 3523 void
3458 pop_switch (void) 3524 pop_switch (void)
3459 { 3525 {
3460 struct cp_switch *cs = switch_stack; 3526 struct cp_switch *cs = switch_stack;
3461 location_t switch_location; 3527 location_t switch_location;
3462 3528
3463 /* Emit warnings as needed. */ 3529 /* Emit warnings as needed. */
3464 switch_location = EXPR_LOC_OR_LOC (cs->switch_stmt, input_location); 3530 switch_location = cp_expr_loc_or_loc (cs->switch_stmt, input_location);
3465 const bool bool_cond_p 3531 const bool bool_cond_p
3466 = (SWITCH_STMT_TYPE (cs->switch_stmt) 3532 = (SWITCH_STMT_TYPE (cs->switch_stmt)
3467 && TREE_CODE (SWITCH_STMT_TYPE (cs->switch_stmt)) == BOOLEAN_TYPE); 3533 && TREE_CODE (SWITCH_STMT_TYPE (cs->switch_stmt)) == BOOLEAN_TYPE);
3468 if (!processing_template_decl) 3534 if (!processing_template_decl)
3469 c_do_switch_warnings (cs->cases, switch_location, 3535 c_do_switch_warnings (cs->cases, switch_location,
3470 SWITCH_STMT_TYPE (cs->switch_stmt), 3536 SWITCH_STMT_TYPE (cs->switch_stmt),
3471 SWITCH_STMT_COND (cs->switch_stmt), 3537 SWITCH_STMT_COND (cs->switch_stmt),
3472 bool_cond_p, cs->outside_range_p); 3538 bool_cond_p, cs->outside_range_p);
3473 3539
3540 /* For the benefit of block_may_fallthru remember if the switch body
3541 case labels cover all possible values and if there are break; stmts. */
3542 if (cs->has_default_p
3543 || (!processing_template_decl
3544 && c_switch_covers_all_cases_p (cs->cases,
3545 SWITCH_STMT_TYPE (cs->switch_stmt))))
3546 SWITCH_STMT_ALL_CASES_P (cs->switch_stmt) = 1;
3547 if (!cs->break_stmt_seen_p)
3548 SWITCH_STMT_NO_BREAK_P (cs->switch_stmt) = 1;
3549 gcc_assert (!cs->in_loop_body_p);
3474 splay_tree_delete (cs->cases); 3550 splay_tree_delete (cs->cases);
3475 switch_stack = switch_stack->next; 3551 switch_stack = switch_stack->next;
3476 free (cs); 3552 free (cs);
3553 }
3554
3555 /* Note that a BREAK_STMT is about to be added. If it is inside of
3556 a SWITCH_STMT and not inside of a loop body inside of it, note
3557 in switch_stack we've seen a BREAK_STMT. */
3558
3559 void
3560 note_break_stmt (void)
3561 {
3562 if (switch_stack && !switch_stack->in_loop_body_p)
3563 switch_stack->break_stmt_seen_p = true;
3564 }
3565
3566 /* Note the start of processing of an iteration statement's body.
3567 The note_break_stmt function will do nothing while processing it.
3568 Return a flag that should be passed to note_iteration_stmt_body_end. */
3569
3570 bool
3571 note_iteration_stmt_body_start (void)
3572 {
3573 if (!switch_stack)
3574 return false;
3575 bool ret = switch_stack->in_loop_body_p;
3576 switch_stack->in_loop_body_p = true;
3577 return ret;
3578 }
3579
3580 /* Note the end of processing of an iteration statement's body. */
3581
3582 void
3583 note_iteration_stmt_body_end (bool prev)
3584 {
3585 if (switch_stack)
3586 switch_stack->in_loop_body_p = prev;
3477 } 3587 }
3478 3588
3479 /* Convert a case constant VALUE in a switch to the type TYPE of the switch 3589 /* Convert a case constant VALUE in a switch to the type TYPE of the switch
3480 condition. Note that if TYPE and VALUE are already integral we don't 3590 condition. Note that if TYPE and VALUE are already integral we don't
3481 really do the conversion because the language-independent 3591 really do the conversion because the language-independent
3484 static tree 3594 static tree
3485 case_conversion (tree type, tree value) 3595 case_conversion (tree type, tree value)
3486 { 3596 {
3487 if (value == NULL_TREE) 3597 if (value == NULL_TREE)
3488 return value; 3598 return value;
3599
3600 value = mark_rvalue_use (value);
3489 3601
3490 if (cxx_dialect >= cxx11 3602 if (cxx_dialect >= cxx11
3491 && (SCOPED_ENUM_P (type) 3603 && (SCOPED_ENUM_P (type)
3492 || !INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (value)))) 3604 || !INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (value))))
3493 { 3605 {
3508 { 3620 {
3509 tree cond, r; 3621 tree cond, r;
3510 cp_binding_level *p; 3622 cp_binding_level *p;
3511 tree type; 3623 tree type;
3512 3624
3625 if (low_value == NULL_TREE && high_value == NULL_TREE)
3626 switch_stack->has_default_p = true;
3627
3513 if (processing_template_decl) 3628 if (processing_template_decl)
3514 { 3629 {
3515 tree label; 3630 tree label;
3516 3631
3517 /* For templates, just add the case label; we'll do semantic 3632 /* For templates, just add the case label; we'll do semantic
3527 3642
3528 if (!check_switch_goto (switch_stack->level)) 3643 if (!check_switch_goto (switch_stack->level))
3529 return error_mark_node; 3644 return error_mark_node;
3530 3645
3531 type = SWITCH_STMT_TYPE (switch_stack->switch_stmt); 3646 type = SWITCH_STMT_TYPE (switch_stack->switch_stmt);
3647 if (type == error_mark_node)
3648 return error_mark_node;
3532 3649
3533 low_value = case_conversion (type, low_value); 3650 low_value = case_conversion (type, low_value);
3534 high_value = case_conversion (type, high_value); 3651 high_value = case_conversion (type, high_value);
3535 3652
3536 r = c_add_case_label (loc, switch_stack->cases, cond, type, 3653 r = c_add_case_label (loc, switch_stack->cases, cond, type,
3698 } 3815 }
3699 } 3816 }
3700 if (TREE_CODE (name) == TEMPLATE_DECL) 3817 if (TREE_CODE (name) == TEMPLATE_DECL)
3701 { 3818 {
3702 if (complain & tf_error) 3819 if (complain & tf_error)
3703 error ("%qD used without template parameters", name); 3820 error ("%qD used without template arguments", name);
3704 return error_mark_node; 3821 return error_mark_node;
3705 } 3822 }
3706 gcc_assert (identifier_p (name)); 3823 gcc_assert (identifier_p (name));
3707 gcc_assert (TYPE_P (context)); 3824 gcc_assert (TYPE_P (context));
3708 3825
3973 to be unique to the TU. */ 4090 to be unique to the TU. */
3974 {"_GLOBAL__N_1", &anon_identifier, cik_normal}, 4091 {"_GLOBAL__N_1", &anon_identifier, cik_normal},
3975 {"auto", &auto_identifier, cik_normal}, 4092 {"auto", &auto_identifier, cik_normal},
3976 {"decltype(auto)", &decltype_auto_identifier, cik_normal}, 4093 {"decltype(auto)", &decltype_auto_identifier, cik_normal},
3977 {"initializer_list", &init_list_identifier, cik_normal}, 4094 {"initializer_list", &init_list_identifier, cik_normal},
4095 {"__for_range ", &for_range__identifier, cik_normal},
4096 {"__for_begin ", &for_begin__identifier, cik_normal},
4097 {"__for_end ", &for_end__identifier, cik_normal},
4098 {"__for_range", &for_range_identifier, cik_normal},
4099 {"__for_begin", &for_begin_identifier, cik_normal},
4100 {"__for_end", &for_end_identifier, cik_normal},
4101 {"abi_tag", &abi_tag_identifier, cik_normal},
4102 {"aligned", &aligned_identifier, cik_normal},
4103 {"begin", &begin_identifier, cik_normal},
4104 {"end", &end_identifier, cik_normal},
4105 {"get", &get__identifier, cik_normal},
4106 {"gnu", &gnu_identifier, cik_normal},
4107 {"tuple_element", &tuple_element_identifier, cik_normal},
4108 {"tuple_size", &tuple_size_identifier, cik_normal},
4109 {"type", &type_identifier, cik_normal},
4110 {"value", &value_identifier, cik_normal},
4111 {"_FUN", &fun_identifier, cik_normal},
4112 {"__closure", &closure_identifier, cik_normal},
3978 {NULL, NULL, cik_normal} 4113 {NULL, NULL, cik_normal}
3979 }; 4114 };
3980 4115
3981 for (const predefined_identifier *pid = predefined_identifiers; 4116 for (const predefined_identifier *pid = predefined_identifiers;
3982 pid->name; ++pid) 4117 pid->name; ++pid)
4012 global_namespace = build_lang_decl (NAMESPACE_DECL, global_identifier, 4147 global_namespace = build_lang_decl (NAMESPACE_DECL, global_identifier,
4013 void_type_node); 4148 void_type_node);
4014 TREE_PUBLIC (global_namespace) = 1; 4149 TREE_PUBLIC (global_namespace) = 1;
4015 DECL_CONTEXT (global_namespace) 4150 DECL_CONTEXT (global_namespace)
4016 = build_translation_unit_decl (get_identifier (main_input_filename)); 4151 = build_translation_unit_decl (get_identifier (main_input_filename));
4152 /* Remember whether we want the empty class passing ABI change warning
4153 in this TU. */
4154 TRANSLATION_UNIT_WARN_EMPTY_P (DECL_CONTEXT (global_namespace))
4155 = warn_abi && abi_version_crosses (12);
4017 debug_hooks->register_main_translation_unit 4156 debug_hooks->register_main_translation_unit
4018 (DECL_CONTEXT (global_namespace)); 4157 (DECL_CONTEXT (global_namespace));
4019 begin_scope (sk_namespace, global_namespace); 4158 begin_scope (sk_namespace, global_namespace);
4020 current_namespace = global_namespace; 4159 current_namespace = global_namespace;
4021 4160
4031 pop_namespace (); 4170 pop_namespace ();
4032 4171
4033 flag_noexcept_type = (cxx_dialect >= cxx17); 4172 flag_noexcept_type = (cxx_dialect >= cxx17);
4034 4173
4035 c_common_nodes_and_builtins (); 4174 c_common_nodes_and_builtins ();
4175
4176 tree bool_ftype = build_function_type_list (boolean_type_node, NULL_TREE);
4177 tree decl
4178 = add_builtin_function ("__builtin_is_constant_evaluated",
4179 bool_ftype, CP_BUILT_IN_IS_CONSTANT_EVALUATED,
4180 BUILT_IN_FRONTEND, NULL, NULL_TREE);
4181 set_call_expr_flags (decl, ECF_CONST | ECF_NOTHROW | ECF_LEAF);
4036 4182
4037 integer_two_node = build_int_cst (NULL_TREE, 2); 4183 integer_two_node = build_int_cst (NULL_TREE, 2);
4038 4184
4039 /* Guess at the initial static decls size. */ 4185 /* Guess at the initial static decls size. */
4040 vec_alloc (static_decls, 500); 4186 vec_alloc (static_decls, 500);
4357 const char *name = IDENTIFIER_POINTER (id); 4503 const char *name = IDENTIFIER_POINTER (id);
4358 4504
4359 retrofit_lang_decl (decl); 4505 retrofit_lang_decl (decl);
4360 4506
4361 DECL_ARTIFICIAL (decl) = 1; 4507 DECL_ARTIFICIAL (decl) = 1;
4362 SET_OVERLOADED_OPERATOR_CODE (decl, ERROR_MARK);
4363 SET_DECL_LANGUAGE (decl, lang_c); 4508 SET_DECL_LANGUAGE (decl, lang_c);
4364 /* Runtime library routines are, by definition, available in an 4509 /* Runtime library routines are, by definition, available in an
4365 external shared object. */ 4510 external shared object. */
4366 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT; 4511 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
4367 DECL_VISIBILITY_SPECIFIED (decl) = 1; 4512 DECL_VISIBILITY_SPECIFIED (decl) = 1;
4445 { 4590 {
4446 tree fn = build_lang_decl (FUNCTION_DECL, name, type); 4591 tree fn = build_lang_decl (FUNCTION_DECL, name, type);
4447 DECL_EXTERNAL (fn) = 1; 4592 DECL_EXTERNAL (fn) = 1;
4448 TREE_PUBLIC (fn) = 1; 4593 TREE_PUBLIC (fn) = 1;
4449 DECL_ARTIFICIAL (fn) = 1; 4594 DECL_ARTIFICIAL (fn) = 1;
4450 SET_OVERLOADED_OPERATOR_CODE (fn, operator_code); 4595 DECL_OVERLOADED_OPERATOR_CODE_RAW (fn)
4596 = OVL_OP_INFO (false, operator_code)->ovl_op_code;
4451 SET_DECL_LANGUAGE (fn, lang_c); 4597 SET_DECL_LANGUAGE (fn, lang_c);
4452 /* Runtime library routines are, by definition, available in an 4598 /* Runtime library routines are, by definition, available in an
4453 external shared object. */ 4599 external shared object. */
4454 DECL_VISIBILITY (fn) = VISIBILITY_DEFAULT; 4600 DECL_VISIBILITY (fn) = VISIBILITY_DEFAULT;
4455 DECL_VISIBILITY_SPECIFIED (fn) = 1; 4601 DECL_VISIBILITY_SPECIFIED (fn) = 1;
4510 4656
4511 static tree 4657 static tree
4512 push_cp_library_fn (enum tree_code operator_code, tree type, 4658 push_cp_library_fn (enum tree_code operator_code, tree type,
4513 int ecf_flags) 4659 int ecf_flags)
4514 { 4660 {
4515 tree fn = build_cp_library_fn (cp_operator_id (operator_code), 4661 tree fn = build_cp_library_fn (ovl_op_identifier (false, operator_code),
4516 operator_code, 4662 operator_code, type, ecf_flags);
4517 type, ecf_flags);
4518 pushdecl (fn); 4663 pushdecl (fn);
4519 if (flag_tm) 4664 if (flag_tm)
4520 apply_tm_attr (fn, get_identifier ("transaction_safe")); 4665 apply_tm_attr (fn, get_identifier ("transaction_safe"));
4521 return fn; 4666 return fn;
4522 } 4667 }
4605 warn_misplaced_attr_for_class_type (source_location location, 4750 warn_misplaced_attr_for_class_type (source_location location,
4606 tree class_type) 4751 tree class_type)
4607 { 4752 {
4608 gcc_assert (OVERLOAD_TYPE_P (class_type)); 4753 gcc_assert (OVERLOAD_TYPE_P (class_type));
4609 4754
4755 auto_diagnostic_group d;
4610 if (warning_at (location, OPT_Wattributes, 4756 if (warning_at (location, OPT_Wattributes,
4611 "attribute ignored in declaration " 4757 "attribute ignored in declaration "
4612 "of %q#T", class_type)) 4758 "of %q#T", class_type))
4613 inform (location, 4759 inform (location,
4614 "attribute for %q#T must follow the %qs keyword", 4760 "attribute for %q#T must follow the %qs keyword",
4945 error ("definition of %q#D is marked %<dllimport%>", decl); 5091 error ("definition of %q#D is marked %<dllimport%>", decl);
4946 DECL_DLLIMPORT_P (decl) = 0; 5092 DECL_DLLIMPORT_P (decl) = 0;
4947 } 5093 }
4948 5094
4949 /* If #pragma weak was used, mark the decl weak now. */ 5095 /* If #pragma weak was used, mark the decl weak now. */
4950 if (!processing_template_decl) 5096 if (!processing_template_decl && !DECL_DECOMPOSITION_P (decl))
4951 maybe_apply_pragma_weak (decl); 5097 maybe_apply_pragma_weak (decl);
4952 5098
4953 if (TREE_CODE (decl) == FUNCTION_DECL 5099 if (TREE_CODE (decl) == FUNCTION_DECL
4954 && DECL_DECLARED_INLINE_P (decl) 5100 && DECL_DECLARED_INLINE_P (decl)
4955 && DECL_UNINLINABLE (decl) 5101 && DECL_UNINLINABLE (decl)
4965 { 5111 {
4966 tree field = lookup_field (context, DECL_NAME (decl), 0, false); 5112 tree field = lookup_field (context, DECL_NAME (decl), 0, false);
4967 if (field == NULL_TREE 5113 if (field == NULL_TREE
4968 || !(VAR_P (field) || variable_template_p (field))) 5114 || !(VAR_P (field) || variable_template_p (field)))
4969 error ("%q+#D is not a static data member of %q#T", decl, context); 5115 error ("%q+#D is not a static data member of %q#T", decl, context);
5116 else if (variable_template_p (field)
5117 && (DECL_LANG_SPECIFIC (decl)
5118 && DECL_TEMPLATE_SPECIALIZATION (decl)))
5119 /* OK, specialization was already checked. */;
4970 else if (variable_template_p (field) && !this_tmpl) 5120 else if (variable_template_p (field) && !this_tmpl)
4971 { 5121 {
4972 if (DECL_LANG_SPECIFIC (decl) 5122 error_at (DECL_SOURCE_LOCATION (decl),
4973 && DECL_TEMPLATE_SPECIALIZATION (decl)) 5123 "non-member-template declaration of %qD", decl);
4974 /* OK, specialization was already checked. */; 5124 inform (DECL_SOURCE_LOCATION (field), "does not match "
4975 else 5125 "member template declaration here");
4976 { 5126 return error_mark_node;
4977 error_at (DECL_SOURCE_LOCATION (decl),
4978 "non-member-template declaration of %qD", decl);
4979 inform (DECL_SOURCE_LOCATION (field), "does not match "
4980 "member template declaration here");
4981 return error_mark_node;
4982 }
4983 } 5127 }
4984 else 5128 else
4985 { 5129 {
4986 if (variable_template_p (field)) 5130 if (variable_template_p (field))
4987 field = DECL_TEMPLATE_RESULT (field); 5131 field = DECL_TEMPLATE_RESULT (field);
5246 a given brace-enclosed initializer is for a class type or for an 5390 a given brace-enclosed initializer is for a class type or for an
5247 array. This function checks that CE does not use a designated 5391 array. This function checks that CE does not use a designated
5248 initializer. If it does, an error is issued. Returns true if CE 5392 initializer. If it does, an error is issued. Returns true if CE
5249 is valid, i.e., does not have a designated initializer. */ 5393 is valid, i.e., does not have a designated initializer. */
5250 5394
5251 static bool 5395 bool
5252 check_array_designated_initializer (constructor_elt *ce, 5396 check_array_designated_initializer (constructor_elt *ce,
5253 unsigned HOST_WIDE_INT index) 5397 unsigned HOST_WIDE_INT index)
5254 { 5398 {
5255 /* Designated initializers for array elements are not supported. */ 5399 /* Designated initializers for array elements are not supported. */
5256 if (ce->index) 5400 if (ce->index)
5272 5416
5273 tree ce_index = build_expr_type_conversion (WANT_INT | WANT_ENUM, 5417 tree ce_index = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5274 ce->index, true); 5418 ce->index, true);
5275 if (ce_index 5419 if (ce_index
5276 && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (ce_index)) 5420 && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (ce_index))
5277 && (TREE_CODE (ce_index = maybe_constant_value (ce_index)) 5421 && (TREE_CODE (ce_index = fold_non_dependent_expr (ce_index))
5278 == INTEGER_CST)) 5422 == INTEGER_CST))
5279 { 5423 {
5280 /* A C99 designator is OK if it matches the current index. */ 5424 /* A C99 designator is OK if it matches the current index. */
5281 if (wi::to_wide (ce_index) == index) 5425 if (wi::to_wide (ce_index) == index)
5282 return true; 5426 {
5427 ce->index = ce_index;
5428 return true;
5429 }
5283 else 5430 else
5284 sorry ("non-trivial designated initializers not supported"); 5431 sorry ("non-trivial designated initializers not supported");
5285 } 5432 }
5286 else 5433 else
5287 error ("C99 designator %qE is not an integral constant-expression", 5434 error ("C99 designator %qE is not an integral constant-expression",
5320 { 5467 {
5321 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (initializer); 5468 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (initializer);
5322 constructor_elt *ce; 5469 constructor_elt *ce;
5323 HOST_WIDE_INT i; 5470 HOST_WIDE_INT i;
5324 FOR_EACH_VEC_SAFE_ELT (v, i, ce) 5471 FOR_EACH_VEC_SAFE_ELT (v, i, ce)
5325 if (!check_array_designated_initializer (ce, i)) 5472 {
5326 failure = 1; 5473 if (instantiation_dependent_expression_p (ce->index))
5327 } 5474 return;
5328 5475 if (!check_array_designated_initializer (ce, i))
5329 if (!failure) 5476 failure = 1;
5477 }
5478 }
5479
5480 if (failure)
5481 TREE_TYPE (decl) = error_mark_node;
5482 else
5330 { 5483 {
5331 failure = cp_complete_array_type (&TREE_TYPE (decl), initializer, 5484 failure = cp_complete_array_type (&TREE_TYPE (decl), initializer,
5332 do_default); 5485 do_default);
5333 if (failure == 1) 5486 if (failure == 1)
5334 { 5487 {
5335 error_at (EXPR_LOC_OR_LOC (initializer, 5488 error_at (cp_expr_loc_or_loc (initializer,
5336 DECL_SOURCE_LOCATION (decl)), 5489 DECL_SOURCE_LOCATION (decl)),
5337 "initializer fails to determine size of %qD", decl); 5490 "initializer fails to determine size of %qD", decl);
5338 } 5491 }
5339 else if (failure == 2) 5492 else if (failure == 2)
5340 { 5493 {
5428 instance of the variable at link-time. */ 5581 instance of the variable at link-time. */
5429 5582
5430 void 5583 void
5431 maybe_commonize_var (tree decl) 5584 maybe_commonize_var (tree decl)
5432 { 5585 {
5586 /* Don't mess with __FUNCTION__ and similar. */
5587 if (DECL_ARTIFICIAL (decl))
5588 return;
5589
5433 /* Static data in a function with comdat linkage also has comdat 5590 /* Static data in a function with comdat linkage also has comdat
5434 linkage. */ 5591 linkage. */
5435 if ((TREE_STATIC (decl) 5592 if ((TREE_STATIC (decl)
5436 /* Don't mess with __FUNCTION__. */
5437 && ! DECL_ARTIFICIAL (decl)
5438 && DECL_FUNCTION_SCOPE_P (decl) 5593 && DECL_FUNCTION_SCOPE_P (decl)
5439 && vague_linkage_p (DECL_CONTEXT (decl))) 5594 && vague_linkage_p (DECL_CONTEXT (decl)))
5440 || (TREE_PUBLIC (decl) && DECL_INLINE_VAR_P (decl))) 5595 || (TREE_PUBLIC (decl) && DECL_INLINE_VAR_P (decl)))
5441 { 5596 {
5442 if (flag_weak) 5597 if (flag_weak)
5479 } 5634 }
5480 } 5635 }
5481 } 5636 }
5482 } 5637 }
5483 5638
5484 /* Issue an error message if DECL is an uninitialized const variable. */ 5639 /* Issue an error message if DECL is an uninitialized const variable.
5485 5640 CONSTEXPR_CONTEXT_P is true when the function is called in a constexpr
5486 static void 5641 context from potential_constant_expression. Returns true if all is well,
5487 check_for_uninitialized_const_var (tree decl) 5642 false otherwise. */
5643
5644 bool
5645 check_for_uninitialized_const_var (tree decl, bool constexpr_context_p,
5646 tsubst_flags_t complain)
5488 { 5647 {
5489 tree type = strip_array_types (TREE_TYPE (decl)); 5648 tree type = strip_array_types (TREE_TYPE (decl));
5490 5649
5491 /* ``Unless explicitly declared extern, a const object does not have 5650 /* ``Unless explicitly declared extern, a const object does not have
5492 external linkage and must be initialized. ($8.4; $12.1)'' ARM 5651 external linkage and must be initialized. ($8.4; $12.1)'' ARM
5493 7.1.6 */ 5652 7.1.6 */
5494 if (VAR_P (decl) 5653 if (VAR_P (decl)
5495 && TREE_CODE (type) != REFERENCE_TYPE 5654 && !TYPE_REF_P (type)
5496 && (CP_TYPE_CONST_P (type) || var_in_constexpr_fn (decl)) 5655 && (constexpr_context_p
5497 && !DECL_INITIAL (decl)) 5656 || CP_TYPE_CONST_P (type) || var_in_constexpr_fn (decl))
5657 && !DECL_NONTRIVIALLY_INITIALIZED_P (decl))
5498 { 5658 {
5499 tree field = default_init_uninitialized_part (type); 5659 tree field = default_init_uninitialized_part (type);
5500 if (!field) 5660 if (!field)
5501 return; 5661 return true;
5502 5662
5503 if (CP_TYPE_CONST_P (type)) 5663 if (!constexpr_context_p)
5504 permerror (DECL_SOURCE_LOCATION (decl), 5664 {
5505 "uninitialized const %qD", decl); 5665 if (CP_TYPE_CONST_P (type))
5506 else 5666 {
5507 { 5667 if (complain & tf_error)
5508 if (!is_instantiation_of_constexpr (current_function_decl)) 5668 permerror (DECL_SOURCE_LOCATION (decl),
5509 error_at (DECL_SOURCE_LOCATION (decl), 5669 "uninitialized const %qD", decl);
5510 "uninitialized variable %qD in %<constexpr%> function", 5670 }
5511 decl); 5671 else
5512 cp_function_chain->invalid_constexpr = true; 5672 {
5513 } 5673 if (!is_instantiation_of_constexpr (current_function_decl)
5514 5674 && (complain & tf_error))
5515 if (CLASS_TYPE_P (type)) 5675 error_at (DECL_SOURCE_LOCATION (decl),
5676 "uninitialized variable %qD in %<constexpr%> "
5677 "function", decl);
5678 cp_function_chain->invalid_constexpr = true;
5679 }
5680 }
5681 else if (complain & tf_error)
5682 error_at (DECL_SOURCE_LOCATION (decl),
5683 "uninitialized variable %qD in %<constexpr%> context",
5684 decl);
5685
5686 if (CLASS_TYPE_P (type) && (complain & tf_error))
5516 { 5687 {
5517 tree defaulted_ctor; 5688 tree defaulted_ctor;
5518 5689
5519 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)), 5690 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
5520 "%q#T has no user-provided default constructor", type); 5691 "%q#T has no user-provided default constructor", type);
5525 "explicitly defaulted in the class body"); 5696 "explicitly defaulted in the class body");
5526 inform (DECL_SOURCE_LOCATION (field), 5697 inform (DECL_SOURCE_LOCATION (field),
5527 "and the implicitly-defined constructor does not " 5698 "and the implicitly-defined constructor does not "
5528 "initialize %q#D", field); 5699 "initialize %q#D", field);
5529 } 5700 }
5530 } 5701
5702 return false;
5703 }
5704
5705 return true;
5531 } 5706 }
5532 5707
5533 /* Structure holding the current initializer being processed by reshape_init. 5708 /* Structure holding the current initializer being processed by reshape_init.
5534 CUR is a pointer to the current element being processed, END is a pointer 5709 CUR is a pointer to the current element being processed, END is a pointer
5535 after the last element present in the initializer. */ 5710 after the last element present in the initializer. */
5549 tree 5724 tree
5550 next_initializable_field (tree field) 5725 next_initializable_field (tree field)
5551 { 5726 {
5552 while (field 5727 while (field
5553 && (TREE_CODE (field) != FIELD_DECL 5728 && (TREE_CODE (field) != FIELD_DECL
5554 || (DECL_C_BIT_FIELD (field) && !DECL_NAME (field)) 5729 || DECL_UNNAMED_BIT_FIELD (field)
5555 || (DECL_ARTIFICIAL (field) 5730 || (DECL_ARTIFICIAL (field)
5556 && !(cxx_dialect >= cxx17 && DECL_FIELD_IS_BASE (field))))) 5731 && !(cxx_dialect >= cxx17 && DECL_FIELD_IS_BASE (field)))))
5557 field = DECL_CHAIN (field); 5732 field = DECL_CHAIN (field);
5558 5733
5559 return field; 5734 return field;
5722 { 5897 {
5723 if (d->cur->index == error_mark_node) 5898 if (d->cur->index == error_mark_node)
5724 return error_mark_node; 5899 return error_mark_node;
5725 5900
5726 if (TREE_CODE (d->cur->index) == FIELD_DECL) 5901 if (TREE_CODE (d->cur->index) == FIELD_DECL)
5727 /* We already reshaped this. */ 5902 {
5728 gcc_assert (d->cur->index == field); 5903 /* We already reshaped this. */
5904 if (field != d->cur->index)
5905 {
5906 tree id = DECL_NAME (d->cur->index);
5907 gcc_assert (id);
5908 gcc_checking_assert (d->cur->index
5909 == get_class_binding (type, id, false));
5910 field = d->cur->index;
5911 }
5912 }
5729 else if (TREE_CODE (d->cur->index) == IDENTIFIER_NODE) 5913 else if (TREE_CODE (d->cur->index) == IDENTIFIER_NODE)
5730 field = get_class_binding (type, d->cur->index, false); 5914 field = get_class_binding (type, d->cur->index, false);
5731 else 5915 else
5732 { 5916 {
5733 if (complain & tf_error) 5917 if (complain & tf_error)
6027 tree elt = CONSTRUCTOR_ELT (init, 0)->value; 6211 tree elt = CONSTRUCTOR_ELT (init, 0)->value;
6028 type = cv_unqualified (type); 6212 type = cv_unqualified (type);
6029 if (check_narrowing (ENUM_UNDERLYING_TYPE (type), elt, complain)) 6213 if (check_narrowing (ENUM_UNDERLYING_TYPE (type), elt, complain))
6030 { 6214 {
6031 warning_sentinel w (warn_useless_cast); 6215 warning_sentinel w (warn_useless_cast);
6216 warning_sentinel w2 (warn_ignored_qualifiers);
6032 return cp_build_c_cast (type, elt, tf_warning_or_error); 6217 return cp_build_c_cast (type, elt, tf_warning_or_error);
6033 } 6218 }
6034 else 6219 else
6035 return error_mark_node; 6220 return error_mark_node;
6036 } 6221 }
6156 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS); 6341 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6157 init = build_zero_init (type, NULL_TREE, false); 6342 init = build_zero_init (type, NULL_TREE, false);
6158 } 6343 }
6159 else if (init_len != 1 && TREE_CODE (type) != COMPLEX_TYPE) 6344 else if (init_len != 1 && TREE_CODE (type) != COMPLEX_TYPE)
6160 { 6345 {
6161 error_at (EXPR_LOC_OR_LOC (init, DECL_SOURCE_LOCATION (decl)), 6346 error_at (cp_expr_loc_or_loc (init, DECL_SOURCE_LOCATION (decl)),
6162 "scalar object %qD requires one element in " 6347 "scalar object %qD requires one element in "
6163 "initializer", decl); 6348 "initializer", decl);
6164 TREE_TYPE (decl) = error_mark_node; 6349 TREE_TYPE (decl) = error_mark_node;
6165 return NULL_TREE; 6350 return NULL_TREE;
6166 } 6351 }
6167 } 6352 }
6168 } 6353 }
6169 6354
6170 if (TREE_CODE (decl) == CONST_DECL) 6355 if (TREE_CODE (decl) == CONST_DECL)
6171 { 6356 {
6172 gcc_assert (TREE_CODE (type) != REFERENCE_TYPE); 6357 gcc_assert (!TYPE_REF_P (type));
6173 6358
6174 DECL_INITIAL (decl) = init; 6359 DECL_INITIAL (decl) = init;
6175 6360
6176 gcc_assert (init != NULL_TREE); 6361 gcc_assert (init != NULL_TREE);
6177 init = NULL_TREE; 6362 init = NULL_TREE;
6178 } 6363 }
6179 else if (!init && DECL_REALLY_EXTERN (decl)) 6364 else if (!init && DECL_REALLY_EXTERN (decl))
6180 ; 6365 ;
6181 else if (init || type_build_ctor_call (type) 6366 else if (init || type_build_ctor_call (type)
6182 || TREE_CODE (type) == REFERENCE_TYPE) 6367 || TYPE_REF_P (type))
6183 { 6368 {
6184 if (TREE_CODE (type) == REFERENCE_TYPE) 6369 if (TYPE_REF_P (type))
6185 { 6370 {
6186 init = grok_reference_init (decl, type, init, flags); 6371 init = grok_reference_init (decl, type, init, flags);
6187 flags |= LOOKUP_ALREADY_DIGESTED; 6372 flags |= LOOKUP_ALREADY_DIGESTED;
6188 } 6373 }
6189 else if (!init) 6374 else if (!init)
6190 check_for_uninitialized_const_var (decl); 6375 check_for_uninitialized_const_var (decl, /*constexpr_context_p=*/false,
6376 tf_warning_or_error);
6191 /* Do not reshape constructors of vectors (they don't need to be 6377 /* Do not reshape constructors of vectors (they don't need to be
6192 reshaped. */ 6378 reshaped. */
6193 else if (BRACE_ENCLOSED_INITIALIZER_P (init)) 6379 else if (BRACE_ENCLOSED_INITIALIZER_P (init))
6194 { 6380 {
6195 if (is_std_init_list (type)) 6381 if (is_std_init_list (type))
6200 } 6386 }
6201 else if (TYPE_NON_AGGREGATE_CLASS (type)) 6387 else if (TYPE_NON_AGGREGATE_CLASS (type))
6202 { 6388 {
6203 /* Don't reshape if the class has constructors. */ 6389 /* Don't reshape if the class has constructors. */
6204 if (cxx_dialect == cxx98) 6390 if (cxx_dialect == cxx98)
6205 error_at (EXPR_LOC_OR_LOC (init, DECL_SOURCE_LOCATION (decl)), 6391 error_at (cp_expr_loc_or_loc (init, DECL_SOURCE_LOCATION (decl)),
6206 "in C++98 %qD must be initialized by " 6392 "in C++98 %qD must be initialized by "
6207 "constructor, not by %<{...}%>", 6393 "constructor, not by %<{...}%>",
6208 decl); 6394 decl);
6209 } 6395 }
6210 else if (VECTOR_TYPE_P (type) && TYPE_VECTOR_OPAQUE (type)) 6396 else if (VECTOR_TYPE_P (type) && TYPE_VECTOR_OPAQUE (type))
6292 enclosing expression. */ 6478 enclosing expression. */
6293 gcc_assert (stmts_are_full_exprs_p ()); 6479 gcc_assert (stmts_are_full_exprs_p ());
6294 6480
6295 init_code = store_init_value (decl, init, cleanups, flags); 6481 init_code = store_init_value (decl, init, cleanups, flags);
6296 6482
6483 if (DECL_INITIAL (decl)
6484 && TREE_CODE (DECL_INITIAL (decl)) == CONSTRUCTOR
6485 && !vec_safe_is_empty (CONSTRUCTOR_ELTS (DECL_INITIAL (decl))))
6486 {
6487 tree elt = CONSTRUCTOR_ELTS (DECL_INITIAL (decl))->last ().value;
6488 if (TREE_CODE (TREE_TYPE (elt)) == ARRAY_TYPE
6489 && TYPE_SIZE (TREE_TYPE (elt)) == NULL_TREE)
6490 cp_complete_array_type (&TREE_TYPE (elt), elt, false);
6491 }
6492
6297 if (pedantic && TREE_CODE (type) == ARRAY_TYPE 6493 if (pedantic && TREE_CODE (type) == ARRAY_TYPE
6298 && DECL_INITIAL (decl) 6494 && DECL_INITIAL (decl)
6299 && TREE_CODE (DECL_INITIAL (decl)) == STRING_CST 6495 && TREE_CODE (DECL_INITIAL (decl)) == STRING_CST
6300 && PAREN_STRING_LITERAL_P (DECL_INITIAL (decl))) 6496 && PAREN_STRING_LITERAL_P (DECL_INITIAL (decl)))
6301 warning_at (EXPR_LOC_OR_LOC (DECL_INITIAL (decl), 6497 warning_at (cp_expr_loc_or_loc (DECL_INITIAL (decl),
6302 DECL_SOURCE_LOCATION (decl)), 6498 DECL_SOURCE_LOCATION (decl)),
6303 0, "array %qD initialized by parenthesized " 6499 0, "array %qD initialized by parenthesized "
6304 "string literal %qE", 6500 "string literal %qE",
6305 decl, DECL_INITIAL (decl)); 6501 decl, DECL_INITIAL (decl));
6306 init = NULL; 6502 init = NULL;
6312 && (CLASSTYPE_READONLY_FIELDS_NEED_INIT (core_type) 6508 && (CLASSTYPE_READONLY_FIELDS_NEED_INIT (core_type)
6313 || CLASSTYPE_REF_FIELDS_NEED_INIT (core_type))) 6509 || CLASSTYPE_REF_FIELDS_NEED_INIT (core_type)))
6314 diagnose_uninitialized_cst_or_ref_member (core_type, /*using_new=*/false, 6510 diagnose_uninitialized_cst_or_ref_member (core_type, /*using_new=*/false,
6315 /*complain=*/true); 6511 /*complain=*/true);
6316 6512
6317 check_for_uninitialized_const_var (decl); 6513 check_for_uninitialized_const_var (decl, /*constexpr_context_p=*/false,
6514 tf_warning_or_error);
6318 } 6515 }
6319 6516
6320 if (init && init != error_mark_node) 6517 if (init && init != error_mark_node)
6321 init_code = build2 (INIT_EXPR, type, decl, init); 6518 init_code = build2 (INIT_EXPR, type, decl, init);
6322 6519
6326 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = false; 6523 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = false;
6327 TREE_CONSTANT (decl) = false; 6524 TREE_CONSTANT (decl) = false;
6328 } 6525 }
6329 6526
6330 if (init_code 6527 if (init_code
6331 && (DECL_IN_AGGR_P (decl) && !DECL_VAR_DECLARED_INLINE_P (decl))) 6528 && (DECL_IN_AGGR_P (decl)
6529 && DECL_INITIALIZED_IN_CLASS_P (decl)
6530 && !DECL_VAR_DECLARED_INLINE_P (decl)))
6332 { 6531 {
6333 static int explained = 0; 6532 static int explained = 0;
6334 6533
6335 if (cxx_dialect < cxx11) 6534 if (cxx_dialect < cxx11)
6336 error ("initializer invalid for static member with constructor"); 6535 error ("initializer invalid for static member with constructor");
6372 DECL_HARD_REGISTER (decl) = 1; 6571 DECL_HARD_REGISTER (decl) = 1;
6373 } 6572 }
6374 else 6573 else
6375 { 6574 {
6376 if (TREE_CODE (decl) == FUNCTION_DECL 6575 if (TREE_CODE (decl) == FUNCTION_DECL
6377 && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL) 6576 && fndecl_built_in_p (decl, BUILT_IN_NORMAL))
6378 set_builtin_user_assembler_name (decl, asmspec); 6577 set_builtin_user_assembler_name (decl, asmspec);
6379 set_user_assembler_name (decl, asmspec); 6578 set_user_assembler_name (decl, asmspec);
6380 } 6579 }
6381 } 6580 }
6382 6581
6514 /* Stick simple initializers in DECL_INITIAL so that 6713 /* Stick simple initializers in DECL_INITIAL so that
6515 -Wno-init-self works (c++/34772). */ 6714 -Wno-init-self works (c++/34772). */
6516 gcc_assert (TREE_OPERAND (init, 0) == decl); 6715 gcc_assert (TREE_OPERAND (init, 0) == decl);
6517 DECL_INITIAL (decl) = rinit; 6716 DECL_INITIAL (decl) = rinit;
6518 6717
6519 if (warn_init_self && TREE_CODE (type) == REFERENCE_TYPE) 6718 if (warn_init_self && TYPE_REF_P (type))
6520 { 6719 {
6521 STRIP_NOPS (rinit); 6720 STRIP_NOPS (rinit);
6522 if (rinit == decl) 6721 if (rinit == decl)
6523 warning_at (DECL_SOURCE_LOCATION (decl), 6722 warning_at (DECL_SOURCE_LOCATION (decl),
6524 OPT_Winit_self, 6723 OPT_Winit_self,
6575 gcc_assert (DECL_ARTIFICIAL (decl)); 6774 gcc_assert (DECL_ARTIFICIAL (decl));
6576 init = build_constructor (TREE_TYPE (decl), v); 6775 init = build_constructor (TREE_TYPE (decl), v);
6577 gcc_assert (TREE_CODE (init) == CONSTRUCTOR); 6776 gcc_assert (TREE_CODE (init) == CONSTRUCTOR);
6578 DECL_INITIAL (decl) = init; 6777 DECL_INITIAL (decl) = init;
6579 DECL_INITIALIZED_P (decl) = 1; 6778 DECL_INITIALIZED_P (decl) = 1;
6779 /* Mark the decl as constexpr so that we can access its content
6780 at compile time. */
6781 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
6782 DECL_DECLARED_CONSTEXPR_P (decl) = true;
6580 determine_visibility (decl); 6783 determine_visibility (decl);
6581 layout_var_decl (decl); 6784 layout_var_decl (decl);
6582 maybe_commonize_var (decl); 6785 maybe_commonize_var (decl);
6583 make_rtl_for_nonlocal_decl (decl, init, /*asmspec=*/NULL); 6786 make_rtl_for_nonlocal_decl (decl, init, /*asmspec=*/NULL);
6584 }
6585
6586 /* INIT is the initializer for a variable, as represented by the
6587 parser. Returns true iff INIT is type-dependent. */
6588
6589 static bool
6590 type_dependent_init_p (tree init)
6591 {
6592 if (TREE_CODE (init) == TREE_LIST)
6593 /* A parenthesized initializer, e.g.: int i (3, 2); ? */
6594 return any_type_dependent_elements_p (init);
6595 else if (TREE_CODE (init) == CONSTRUCTOR)
6596 /* A brace-enclosed initializer, e.g.: int i = { 3 }; ? */
6597 {
6598 if (dependent_type_p (TREE_TYPE (init)))
6599 return true;
6600
6601 vec<constructor_elt, va_gc> *elts;
6602 size_t nelts;
6603 size_t i;
6604
6605 elts = CONSTRUCTOR_ELTS (init);
6606 nelts = vec_safe_length (elts);
6607 for (i = 0; i < nelts; ++i)
6608 if (type_dependent_init_p ((*elts)[i].value))
6609 return true;
6610 }
6611 else
6612 /* It must be a simple expression, e.g., int i = 3; */
6613 return type_dependent_expression_p (init);
6614
6615 return false;
6616 } 6787 }
6617 6788
6618 /* INIT is the initializer for a variable, as represented by the 6789 /* INIT is the initializer for a variable, as represented by the
6619 parser. Returns true iff INIT is value-dependent. */ 6790 parser. Returns true iff INIT is value-dependent. */
6620 6791
6625 /* A parenthesized initializer, e.g.: int i (3, 2); ? */ 6796 /* A parenthesized initializer, e.g.: int i (3, 2); ? */
6626 return any_value_dependent_elements_p (init); 6797 return any_value_dependent_elements_p (init);
6627 else if (TREE_CODE (init) == CONSTRUCTOR) 6798 else if (TREE_CODE (init) == CONSTRUCTOR)
6628 /* A brace-enclosed initializer, e.g.: int i = { 3 }; ? */ 6799 /* A brace-enclosed initializer, e.g.: int i = { 3 }; ? */
6629 { 6800 {
6801 if (dependent_type_p (TREE_TYPE (init)))
6802 return true;
6803
6630 vec<constructor_elt, va_gc> *elts; 6804 vec<constructor_elt, va_gc> *elts;
6631 size_t nelts; 6805 size_t nelts;
6632 size_t i; 6806 size_t i;
6633 6807
6634 elts = CONSTRUCTOR_ELTS (init); 6808 elts = CONSTRUCTOR_ELTS (init);
6730 && TYPE_BEING_DEFINED (current_class_type) 6904 && TYPE_BEING_DEFINED (current_class_type)
6731 && !CLASSTYPE_TEMPLATE_INSTANTIATION (current_class_type) 6905 && !CLASSTYPE_TEMPLATE_INSTANTIATION (current_class_type)
6732 && (DECL_INITIAL (decl) || init)) 6906 && (DECL_INITIAL (decl) || init))
6733 DECL_INITIALIZED_IN_CLASS_P (decl) = 1; 6907 DECL_INITIALIZED_IN_CLASS_P (decl) = 1;
6734 6908
6909 /* Do auto deduction unless decl is a function or an uninstantiated
6910 template specialization. */
6735 if (TREE_CODE (decl) != FUNCTION_DECL 6911 if (TREE_CODE (decl) != FUNCTION_DECL
6912 && !(init == NULL_TREE
6913 && DECL_LANG_SPECIFIC (decl)
6914 && DECL_TEMPLATE_INSTANTIATION (decl)
6915 && !DECL_TEMPLATE_INSTANTIATED (decl))
6736 && (auto_node = type_uses_auto (type))) 6916 && (auto_node = type_uses_auto (type)))
6737 { 6917 {
6738 tree d_init; 6918 tree d_init;
6739 if (init == NULL_TREE) 6919 if (init == NULL_TREE)
6740 { 6920 gcc_assert (CLASS_PLACEHOLDER_TEMPLATE (auto_node));
6741 if (DECL_LANG_SPECIFIC (decl)
6742 && DECL_TEMPLATE_INSTANTIATION (decl)
6743 && !DECL_TEMPLATE_INSTANTIATED (decl))
6744 {
6745 /* init is null because we're deferring instantiating the
6746 initializer until we need it. Well, we need it now. */
6747 instantiate_decl (decl, /*defer_ok*/true, /*expl*/false);
6748 return;
6749 }
6750
6751 gcc_assert (CLASS_PLACEHOLDER_TEMPLATE (auto_node));
6752 }
6753 d_init = init; 6921 d_init = init;
6754 if (d_init) 6922 if (d_init)
6755 { 6923 {
6756 if (TREE_CODE (d_init) == TREE_LIST 6924 if (TREE_CODE (d_init) == TREE_LIST
6757 && !CLASS_PLACEHOLDER_TEMPLATE (auto_node)) 6925 && !CLASS_PLACEHOLDER_TEMPLATE (auto_node))
6775 return; 6943 return;
6776 } 6944 }
6777 cp_apply_type_quals_to_decl (cp_type_quals (type), decl); 6945 cp_apply_type_quals_to_decl (cp_type_quals (type), decl);
6778 } 6946 }
6779 6947
6780 if (!ensure_literal_type_for_constexpr_object (decl)) 6948 if (ensure_literal_type_for_constexpr_object (decl) == error_mark_node)
6781 DECL_DECLARED_CONSTEXPR_P (decl) = 0; 6949 {
6950 DECL_DECLARED_CONSTEXPR_P (decl) = 0;
6951 if (VAR_P (decl) && DECL_CLASS_SCOPE_P (decl))
6952 {
6953 init = NULL_TREE;
6954 DECL_EXTERNAL (decl) = 1;
6955 }
6956 }
6782 6957
6783 if (VAR_P (decl) 6958 if (VAR_P (decl)
6784 && DECL_CLASS_SCOPE_P (decl) 6959 && DECL_CLASS_SCOPE_P (decl)
6785 && DECL_INITIALIZED_IN_CLASS_P (decl)) 6960 && DECL_INITIALIZED_IN_CLASS_P (decl))
6786 check_static_variable_definition (decl, type); 6961 check_static_variable_definition (decl, type);
6815 { 6990 {
6816 DECL_NONTRIVIALLY_INITIALIZED_P (decl) = 1; 6991 DECL_NONTRIVIALLY_INITIALIZED_P (decl) = 1;
6817 /* If DECL is a reference, then we want to know whether init is a 6992 /* If DECL is a reference, then we want to know whether init is a
6818 reference constant; init_const_expr_p as passed tells us whether 6993 reference constant; init_const_expr_p as passed tells us whether
6819 it's an rvalue constant. */ 6994 it's an rvalue constant. */
6820 if (TREE_CODE (type) == REFERENCE_TYPE) 6995 if (TYPE_REF_P (type))
6821 init_const_expr_p = potential_constant_expression (init); 6996 init_const_expr_p = potential_constant_expression (init);
6822 if (init_const_expr_p) 6997 if (init_const_expr_p)
6823 { 6998 {
6824 /* Set these flags now for templates. We'll update the flags in 6999 /* Set these flags now for templates. We'll update the flags in
6825 store_init_value for instantiations. */ 7000 store_init_value for instantiations. */
6826 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1; 7001 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
6827 if (decl_maybe_constant_var_p (decl) 7002 if (decl_maybe_constant_var_p (decl)
6828 /* FIXME setting TREE_CONSTANT on refs breaks the back end. */ 7003 /* FIXME setting TREE_CONSTANT on refs breaks the back end. */
6829 && TREE_CODE (type) != REFERENCE_TYPE) 7004 && !TYPE_REF_P (type))
6830 TREE_CONSTANT (decl) = 1; 7005 TREE_CONSTANT (decl) = 1;
6831 } 7006 }
6832 } 7007 }
6833 7008
6834 if (processing_template_decl) 7009 if (processing_template_decl)
6844 if (check_for_bare_parameter_packs (init)) 7019 if (check_for_bare_parameter_packs (init))
6845 { 7020 {
6846 init = NULL_TREE; 7021 init = NULL_TREE;
6847 DECL_INITIAL (decl) = NULL_TREE; 7022 DECL_INITIAL (decl) = NULL_TREE;
6848 } 7023 }
6849
6850 init = do_dependent_capture (init);
6851 7024
6852 /* Generally, initializers in templates are expanded when the 7025 /* Generally, initializers in templates are expanded when the
6853 template is instantiated. But, if DECL is a variable constant 7026 template is instantiated. But, if DECL is a variable constant
6854 then it can be used in future constant expressions, so its value 7027 then it can be used in future constant expressions, so its value
6855 must be available. */ 7028 must be available. */
6856 7029
7030 bool dep_init = false;
7031
6857 if (!VAR_P (decl) || type_dependent_p) 7032 if (!VAR_P (decl) || type_dependent_p)
6858 /* We can't do anything if the decl has dependent type. */; 7033 /* We can't do anything if the decl has dependent type. */;
7034 else if (!init && is_concept_var (decl))
7035 {
7036 error ("variable concept has no initializer");
7037 init = boolean_true_node;
7038 }
6859 else if (init 7039 else if (init
6860 && init_const_expr_p 7040 && init_const_expr_p
6861 && TREE_CODE (type) != REFERENCE_TYPE 7041 && !TYPE_REF_P (type)
6862 && decl_maybe_constant_var_p (decl) 7042 && decl_maybe_constant_var_p (decl)
6863 && !type_dependent_init_p (init) 7043 && !(dep_init = value_dependent_init_p (init)))
6864 && !value_dependent_init_p (init))
6865 { 7044 {
6866 /* This variable seems to be a non-dependent constant, so process 7045 /* This variable seems to be a non-dependent constant, so process
6867 its initializer. If check_initializer returns non-null the 7046 its initializer. If check_initializer returns non-null the
6868 initialization wasn't constant after all. */ 7047 initialization wasn't constant after all. */
6869 tree init_code; 7048 tree init_code;
6871 init_code = check_initializer (decl, init, flags, &cleanups); 7050 init_code = check_initializer (decl, init, flags, &cleanups);
6872 if (init_code == NULL_TREE) 7051 if (init_code == NULL_TREE)
6873 init = NULL_TREE; 7052 init = NULL_TREE;
6874 release_tree_vector (cleanups); 7053 release_tree_vector (cleanups);
6875 } 7054 }
6876 else if (!init && is_concept_var (decl))
6877 error ("variable concept has no initializer");
6878 else if (!DECL_PRETTY_FUNCTION_P (decl)) 7055 else if (!DECL_PRETTY_FUNCTION_P (decl))
6879 { 7056 {
6880 /* Deduce array size even if the initializer is dependent. */ 7057 /* Deduce array size even if the initializer is dependent. */
6881 maybe_deduce_size_from_array_init (decl, init); 7058 maybe_deduce_size_from_array_init (decl, init);
6882 /* And complain about multiple initializers. */ 7059 /* And complain about multiple initializers. */
6886 tf_warning_or_error); 7063 tf_warning_or_error);
6887 } 7064 }
6888 7065
6889 if (init) 7066 if (init)
6890 DECL_INITIAL (decl) = init; 7067 DECL_INITIAL (decl) = init;
7068
7069 if (dep_init)
7070 {
7071 retrofit_lang_decl (decl);
7072 SET_DECL_DEPENDENT_INIT_P (decl, true);
7073 }
6891 return; 7074 return;
6892 } 7075 }
6893 7076
6894 /* Just store non-static data member initializers for later. */ 7077 /* Just store non-static data member initializers for later. */
6895 if (init && TREE_CODE (decl) == FIELD_DECL) 7078 if (init && TREE_CODE (decl) == FIELD_DECL)
6919 } 7102 }
6920 7103
6921 /* A reference will be modified here, as it is initialized. */ 7104 /* A reference will be modified here, as it is initialized. */
6922 if (! DECL_EXTERNAL (decl) 7105 if (! DECL_EXTERNAL (decl)
6923 && TREE_READONLY (decl) 7106 && TREE_READONLY (decl)
6924 && TREE_CODE (type) == REFERENCE_TYPE) 7107 && TYPE_REF_P (type))
6925 { 7108 {
6926 was_readonly = 1; 7109 was_readonly = 1;
6927 TREE_READONLY (decl) = 0; 7110 TREE_READONLY (decl) = 0;
6928 } 7111 }
6929 7112
7127 } 7310 }
7128 7311
7129 if (was_readonly) 7312 if (was_readonly)
7130 TREE_READONLY (decl) = 1; 7313 TREE_READONLY (decl) = 1;
7131 7314
7315 if (flag_openmp
7316 && VAR_P (decl)
7317 && lookup_attribute ("omp declare target implicit",
7318 DECL_ATTRIBUTES (decl)))
7319 {
7320 DECL_ATTRIBUTES (decl)
7321 = remove_attribute ("omp declare target implicit",
7322 DECL_ATTRIBUTES (decl));
7323 complete_type (TREE_TYPE (decl));
7324 if (!cp_omp_mappable_type (TREE_TYPE (decl)))
7325 error ("%q+D in declare target directive does not have mappable type",
7326 decl);
7327 else if (!lookup_attribute ("omp declare target",
7328 DECL_ATTRIBUTES (decl))
7329 && !lookup_attribute ("omp declare target link",
7330 DECL_ATTRIBUTES (decl)))
7331 DECL_ATTRIBUTES (decl)
7332 = tree_cons (get_identifier ("omp declare target"),
7333 NULL_TREE, DECL_ATTRIBUTES (decl));
7334 }
7335
7132 invoke_plugin_callbacks (PLUGIN_FINISH_DECL, decl); 7336 invoke_plugin_callbacks (PLUGIN_FINISH_DECL, decl);
7133 } 7337 }
7134 7338
7135 /* For class TYPE return itself or some its bases that contain 7339 /* For class TYPE return itself or some its bases that contain
7136 any direct non-static data members. Return error_mark_node if an 7340 any direct non-static data members. Return error_mark_node if an
7139 static tree 7343 static tree
7140 find_decomp_class_base (location_t loc, tree type, tree ret) 7344 find_decomp_class_base (location_t loc, tree type, tree ret)
7141 { 7345 {
7142 bool member_seen = false; 7346 bool member_seen = false;
7143 for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field)) 7347 for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
7144 if (TREE_CODE (field) != FIELD_DECL || DECL_ARTIFICIAL (field)) 7348 if (TREE_CODE (field) != FIELD_DECL
7349 || DECL_ARTIFICIAL (field)
7350 || DECL_UNNAMED_BIT_FIELD (field))
7145 continue; 7351 continue;
7146 else if (ret) 7352 else if (ret)
7147 return type; 7353 return type;
7148 else if (ANON_AGGR_TYPE_P (TREE_TYPE (field))) 7354 else if (ANON_AGGR_TYPE_P (TREE_TYPE (field)))
7149 { 7355 {
7154 error_at (loc, "cannot decompose class type %qT because it has an " 7360 error_at (loc, "cannot decompose class type %qT because it has an "
7155 "anonymous union member", type); 7361 "anonymous union member", type);
7156 inform (DECL_SOURCE_LOCATION (field), "declared here"); 7362 inform (DECL_SOURCE_LOCATION (field), "declared here");
7157 return error_mark_node; 7363 return error_mark_node;
7158 } 7364 }
7159 else if (TREE_PRIVATE (field) || TREE_PROTECTED (field)) 7365 else if (!accessible_p (type, field, true))
7160 { 7366 {
7161 error_at (loc, "cannot decompose non-public member %qD of %qT", 7367 error_at (loc, "cannot decompose inaccessible member %qD of %qT",
7162 field, type); 7368 field, type);
7163 inform (DECL_SOURCE_LOCATION (field), 7369 inform (DECL_SOURCE_LOCATION (field),
7164 TREE_PRIVATE (field) 7370 TREE_PRIVATE (field)
7165 ? G_("declared private here") 7371 ? G_("declared private here")
7166 : G_("declared protected here")); 7372 : G_("declared protected here"));
7178 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++) 7384 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
7179 { 7385 {
7180 tree t = find_decomp_class_base (loc, TREE_TYPE (base_binfo), ret); 7386 tree t = find_decomp_class_base (loc, TREE_TYPE (base_binfo), ret);
7181 if (t == error_mark_node) 7387 if (t == error_mark_node)
7182 return error_mark_node; 7388 return error_mark_node;
7183 if (t != NULL_TREE) 7389 if (t != NULL_TREE && t != ret)
7184 { 7390 {
7185 if (ret == type) 7391 if (ret == type)
7186 { 7392 {
7187 error_at (loc, "cannot decompose class type %qT: both it and " 7393 error_at (loc, "cannot decompose class type %qT: both it and "
7188 "its base class %qT have non-static data members", 7394 "its base class %qT have non-static data members",
7189 type, t); 7395 type, t);
7190 return error_mark_node; 7396 return error_mark_node;
7191 } 7397 }
7192 else if (orig_ret != NULL_TREE) 7398 else if (orig_ret != NULL_TREE)
7193 return t; 7399 return t;
7194 else if (ret == t)
7195 /* OK, found the same base along another path. We'll complain
7196 in convert_to_base if it's ambiguous. */;
7197 else if (ret != NULL_TREE) 7400 else if (ret != NULL_TREE)
7198 { 7401 {
7199 error_at (loc, "cannot decompose class type %qT: its base " 7402 error_at (loc, "cannot decompose class type %qT: its base "
7200 "classes %qT and %qT have non-static data " 7403 "classes %qT and %qT have non-static data "
7201 "members", type, ret, t); 7404 "members", type, ret, t);
7213 static tree 7416 static tree
7214 get_tuple_size (tree type) 7417 get_tuple_size (tree type)
7215 { 7418 {
7216 tree args = make_tree_vec (1); 7419 tree args = make_tree_vec (1);
7217 TREE_VEC_ELT (args, 0) = type; 7420 TREE_VEC_ELT (args, 0) = type;
7218 tree inst = lookup_template_class (get_identifier ("tuple_size"), args, 7421 tree inst = lookup_template_class (tuple_size_identifier, args,
7219 /*in_decl*/NULL_TREE, 7422 /*in_decl*/NULL_TREE,
7220 /*context*/std_node, 7423 /*context*/std_node,
7221 /*entering_scope*/false, tf_none); 7424 /*entering_scope*/false, tf_none);
7222 inst = complete_type (inst); 7425 inst = complete_type (inst);
7223 if (inst == error_mark_node || !COMPLETE_TYPE_P (inst)) 7426 if (inst == error_mark_node || !COMPLETE_TYPE_P (inst))
7224 return NULL_TREE; 7427 return NULL_TREE;
7225 tree val = lookup_qualified_name (inst, get_identifier ("value"), 7428 tree val = lookup_qualified_name (inst, value_identifier,
7226 /*type*/false, /*complain*/false); 7429 /*type*/false, /*complain*/false);
7227 if (TREE_CODE (val) == VAR_DECL || TREE_CODE (val) == CONST_DECL) 7430 if (TREE_CODE (val) == VAR_DECL || TREE_CODE (val) == CONST_DECL)
7228 val = maybe_constant_value (val); 7431 val = maybe_constant_value (val);
7229 if (TREE_CODE (val) == INTEGER_CST) 7432 if (TREE_CODE (val) == INTEGER_CST)
7230 return val; 7433 return val;
7238 get_tuple_element_type (tree type, unsigned i) 7441 get_tuple_element_type (tree type, unsigned i)
7239 { 7442 {
7240 tree args = make_tree_vec (2); 7443 tree args = make_tree_vec (2);
7241 TREE_VEC_ELT (args, 0) = build_int_cst (integer_type_node, i); 7444 TREE_VEC_ELT (args, 0) = build_int_cst (integer_type_node, i);
7242 TREE_VEC_ELT (args, 1) = type; 7445 TREE_VEC_ELT (args, 1) = type;
7243 tree inst = lookup_template_class (get_identifier ("tuple_element"), args, 7446 tree inst = lookup_template_class (tuple_element_identifier, args,
7244 /*in_decl*/NULL_TREE, 7447 /*in_decl*/NULL_TREE,
7245 /*context*/std_node, 7448 /*context*/std_node,
7246 /*entering_scope*/false, 7449 /*entering_scope*/false,
7247 tf_warning_or_error); 7450 tf_warning_or_error);
7248 return make_typename_type (inst, get_identifier ("type"), 7451 return make_typename_type (inst, type_identifier,
7249 none_type, tf_warning_or_error); 7452 none_type, tf_warning_or_error);
7250 } 7453 }
7251 7454
7252 /* Return e.get<i>() or get<i>(e). */ 7455 /* Return e.get<i>() or get<i>(e). */
7253 7456
7254 static tree 7457 static tree
7255 get_tuple_decomp_init (tree decl, unsigned i) 7458 get_tuple_decomp_init (tree decl, unsigned i)
7256 { 7459 {
7257 tree get_id = get_identifier ("get");
7258 tree targs = make_tree_vec (1); 7460 tree targs = make_tree_vec (1);
7259 TREE_VEC_ELT (targs, 0) = build_int_cst (integer_type_node, i); 7461 TREE_VEC_ELT (targs, 0) = build_int_cst (integer_type_node, i);
7260 7462
7261 tree etype = TREE_TYPE (decl); 7463 tree etype = TREE_TYPE (decl);
7262 tree e = convert_from_reference (decl); 7464 tree e = convert_from_reference (decl);
7263 7465
7264 /* [The id-expression] e is an lvalue if the type of the entity e is an 7466 /* [The id-expression] e is an lvalue if the type of the entity e is an
7265 lvalue reference and an xvalue otherwise. */ 7467 lvalue reference and an xvalue otherwise. */
7266 if (TREE_CODE (etype) != REFERENCE_TYPE 7468 if (!TYPE_REF_P (etype)
7267 || TYPE_REF_IS_RVALUE (etype)) 7469 || TYPE_REF_IS_RVALUE (etype))
7268 e = move (e); 7470 e = move (e);
7269 7471
7270 tree fns = lookup_qualified_name (TREE_TYPE (e), get_id, 7472 tree fns = lookup_qualified_name (TREE_TYPE (e), get__identifier,
7271 /*type*/false, /*complain*/false); 7473 /*type*/false, /*complain*/false);
7272 if (fns != error_mark_node) 7474 bool use_member_get = false;
7475
7476 /* To use a member get, member lookup must find at least one
7477 declaration that is a function template
7478 whose first template parameter is a non-type parameter. */
7479 for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (fns)); iter; ++iter)
7480 {
7481 tree fn = *iter;
7482 if (TREE_CODE (fn) == TEMPLATE_DECL)
7483 {
7484 tree tparms = DECL_TEMPLATE_PARMS (fn);
7485 tree parm = TREE_VEC_ELT (INNERMOST_TEMPLATE_PARMS (tparms), 0);
7486 if (TREE_CODE (TREE_VALUE (parm)) == PARM_DECL)
7487 {
7488 use_member_get = true;
7489 break;
7490 }
7491 }
7492 }
7493
7494 if (use_member_get)
7273 { 7495 {
7274 fns = lookup_template_function (fns, targs); 7496 fns = lookup_template_function (fns, targs);
7275 return build_new_method_call (e, fns, /*args*/NULL, 7497 return build_new_method_call (e, fns, /*args*/NULL,
7276 /*path*/NULL_TREE, LOOKUP_NORMAL, 7498 /*path*/NULL_TREE, LOOKUP_NORMAL,
7277 /*fn_p*/NULL, tf_warning_or_error); 7499 /*fn_p*/NULL, tf_warning_or_error);
7278 } 7500 }
7279 else 7501 else
7280 { 7502 {
7281 vec<tree,va_gc> *args = make_tree_vector_single (e); 7503 vec<tree,va_gc> *args = make_tree_vector_single (e);
7282 fns = lookup_template_function (get_id, targs); 7504 fns = lookup_template_function (get__identifier, targs);
7283 fns = perform_koenig_lookup (fns, args, tf_warning_or_error); 7505 fns = perform_koenig_lookup (fns, args, tf_warning_or_error);
7284 return finish_call_expr (fns, &args, /*novirt*/false, 7506 return finish_call_expr (fns, &args, /*novirt*/false,
7285 /*koenig*/true, tf_warning_or_error); 7507 /*koenig*/true, tf_warning_or_error);
7286 } 7508 }
7287 } 7509 }
7288 7510
7289 /* It's impossible to recover the decltype of a tuple decomposition variable 7511 /* It's impossible to recover the decltype of a tuple decomposition variable
7290 based on the actual type of the variable, so store it in a hash table. */ 7512 based on the actual type of the variable, so store it in a hash table. */
7291 static GTY(()) hash_map<tree,tree> *decomp_type_table; 7513
7514 static GTY((cache)) tree_cache_map *decomp_type_table;
7292 static void 7515 static void
7293 store_decomp_type (tree v, tree t) 7516 store_decomp_type (tree v, tree t)
7294 { 7517 {
7295 if (!decomp_type_table) 7518 if (!decomp_type_table)
7296 decomp_type_table = hash_map<tree,tree>::create_ggc (13); 7519 decomp_type_table = tree_cache_map::create_ggc (13);
7297 decomp_type_table->put (v, t); 7520 decomp_type_table->put (v, t);
7298 } 7521 }
7299 7522
7300 tree 7523 tree
7301 lookup_decomp_type (tree v) 7524 lookup_decomp_type (tree v)
7302 { 7525 {
7303 return *decomp_type_table->get (v); 7526 return *decomp_type_table->get (v);
7527 }
7528
7529 /* Mangle a decomposition declaration if needed. Arguments like
7530 in cp_finish_decomp. */
7531
7532 void
7533 cp_maybe_mangle_decomp (tree decl, tree first, unsigned int count)
7534 {
7535 if (!processing_template_decl
7536 && !error_operand_p (decl)
7537 && DECL_NAMESPACE_SCOPE_P (decl))
7538 {
7539 auto_vec<tree, 16> v;
7540 v.safe_grow (count);
7541 tree d = first;
7542 for (unsigned int i = 0; i < count; i++, d = DECL_CHAIN (d))
7543 v[count - i - 1] = d;
7544 SET_DECL_ASSEMBLER_NAME (decl, mangle_decomp (decl, v));
7545 maybe_apply_pragma_weak (decl);
7546 }
7304 } 7547 }
7305 7548
7306 /* Finish a decomposition declaration. DECL is the underlying declaration 7549 /* Finish a decomposition declaration. DECL is the underlying declaration
7307 "e", FIRST is the head of a chain of decls for the individual identifiers 7550 "e", FIRST is the head of a chain of decls for the individual identifiers
7308 chained through DECL_CHAIN in reverse order and COUNT is the number of 7551 chained through DECL_CHAIN in reverse order and COUNT is the number of
7363 } 7606 }
7364 7607
7365 tree type = TREE_TYPE (decl); 7608 tree type = TREE_TYPE (decl);
7366 tree dexp = decl; 7609 tree dexp = decl;
7367 7610
7368 if (TREE_CODE (type) == REFERENCE_TYPE) 7611 if (TYPE_REF_P (type))
7369 { 7612 {
7370 dexp = convert_from_reference (dexp); 7613 dexp = convert_from_reference (dexp);
7371 type = TREE_TYPE (type); 7614 type = complete_type (TREE_TYPE (type));
7615 if (type == error_mark_node)
7616 goto error_out;
7617 if (!COMPLETE_TYPE_P (type))
7618 {
7619 error_at (loc, "structured binding refers to incomplete type %qT",
7620 type);
7621 goto error_out;
7622 }
7372 } 7623 }
7373 7624
7374 tree eltype = NULL_TREE; 7625 tree eltype = NULL_TREE;
7375 unsigned HOST_WIDE_INT eltscnt = 0; 7626 unsigned HOST_WIDE_INT eltscnt = 0;
7376 if (TREE_CODE (type) == ARRAY_TYPE) 7627 if (TREE_CODE (type) == ARRAY_TYPE)
7387 eltscnt = tree_to_uhwi (nelts); 7638 eltscnt = tree_to_uhwi (nelts);
7388 if (count != eltscnt) 7639 if (count != eltscnt)
7389 { 7640 {
7390 cnt_mismatch: 7641 cnt_mismatch:
7391 if (count > eltscnt) 7642 if (count > eltscnt)
7392 error_at (loc, "%u names provided while %qT decomposes into " 7643 error_n (loc, count,
7393 "%wu elements", count, type, eltscnt); 7644 "%u name provided for structured binding",
7645 "%u names provided for structured binding", count);
7394 else 7646 else
7395 error_at (loc, "only %u names provided while %qT decomposes into " 7647 error_n (loc, count,
7396 "%wu elements", count, type, eltscnt); 7648 "only %u name provided for structured binding",
7649 "only %u names provided for structured binding", count);
7650 inform_n (loc, eltscnt,
7651 "while %qT decomposes into %wu element",
7652 "while %qT decomposes into %wu elements",
7653 type, eltscnt);
7397 goto error_out; 7654 goto error_out;
7398 } 7655 }
7399 eltype = TREE_TYPE (type); 7656 eltype = TREE_TYPE (type);
7400 for (unsigned int i = 0; i < count; i++) 7657 for (unsigned int i = 0; i < count; i++)
7401 { 7658 {
7432 DECL_HAS_VALUE_EXPR_P (v[i]) = 1; 7689 DECL_HAS_VALUE_EXPR_P (v[i]) = 1;
7433 } 7690 }
7434 } 7691 }
7435 else if (TREE_CODE (type) == VECTOR_TYPE) 7692 else if (TREE_CODE (type) == VECTOR_TYPE)
7436 { 7693 {
7437 eltscnt = TYPE_VECTOR_SUBPARTS (type); 7694 if (!TYPE_VECTOR_SUBPARTS (type).is_constant (&eltscnt))
7695 {
7696 error_at (loc, "cannot decompose variable length vector %qT", type);
7697 goto error_out;
7698 }
7438 if (count != eltscnt) 7699 if (count != eltscnt)
7439 goto cnt_mismatch; 7700 goto cnt_mismatch;
7440 eltype = cp_build_qualified_type (TREE_TYPE (type), TYPE_QUALS (type)); 7701 eltype = cp_build_qualified_type (TREE_TYPE (type), TYPE_QUALS (type));
7441 for (unsigned int i = 0; i < count; i++) 7702 for (unsigned int i = 0; i < count; i++)
7442 { 7703 {
7458 { 7719 {
7459 if (tsize == error_mark_node) 7720 if (tsize == error_mark_node)
7460 { 7721 {
7461 error_at (loc, "%<std::tuple_size<%T>::value%> is not an integral " 7722 error_at (loc, "%<std::tuple_size<%T>::value%> is not an integral "
7462 "constant expression", type); 7723 "constant expression", type);
7724 goto error_out;
7725 }
7726 if (!tree_fits_uhwi_p (tsize))
7727 {
7728 error_n (loc, count,
7729 "%u name provided for structured binding",
7730 "%u names provided for structured binding", count);
7731 inform (loc, "while %qT decomposes into %E elements",
7732 type, tsize);
7463 goto error_out; 7733 goto error_out;
7464 } 7734 }
7465 eltscnt = tree_to_uhwi (tsize); 7735 eltscnt = tree_to_uhwi (tsize);
7466 if (count != eltscnt) 7736 if (count != eltscnt)
7467 goto cnt_mismatch; 7737 goto cnt_mismatch;
7516 else if (LAMBDA_TYPE_P (type)) 7786 else if (LAMBDA_TYPE_P (type))
7517 { 7787 {
7518 error_at (loc, "cannot decompose lambda closure type %qT", type); 7788 error_at (loc, "cannot decompose lambda closure type %qT", type);
7519 goto error_out; 7789 goto error_out;
7520 } 7790 }
7791 else if (processing_template_decl && !COMPLETE_TYPE_P (type))
7792 pedwarn (loc, 0, "structured binding refers to incomplete class type %qT",
7793 type);
7521 else 7794 else
7522 { 7795 {
7523 tree btype = find_decomp_class_base (loc, type, NULL_TREE); 7796 tree btype = find_decomp_class_base (loc, type, NULL_TREE);
7524 if (btype == error_mark_node) 7797 if (btype == error_mark_node)
7525 goto error_out; 7798 goto error_out;
7528 error_at (loc, "cannot decompose class type %qT without non-static " 7801 error_at (loc, "cannot decompose class type %qT without non-static "
7529 "data members", type); 7802 "data members", type);
7530 goto error_out; 7803 goto error_out;
7531 } 7804 }
7532 for (tree field = TYPE_FIELDS (btype); field; field = TREE_CHAIN (field)) 7805 for (tree field = TYPE_FIELDS (btype); field; field = TREE_CHAIN (field))
7533 if (TREE_CODE (field) != FIELD_DECL || DECL_ARTIFICIAL (field)) 7806 if (TREE_CODE (field) != FIELD_DECL
7807 || DECL_ARTIFICIAL (field)
7808 || DECL_UNNAMED_BIT_FIELD (field))
7534 continue; 7809 continue;
7535 else 7810 else
7536 eltscnt++; 7811 eltscnt++;
7537 if (count != eltscnt) 7812 if (count != eltscnt)
7538 goto cnt_mismatch; 7813 goto cnt_mismatch;
7543 /*nonnull*/false, tf_warning_or_error); 7818 /*nonnull*/false, tf_warning_or_error);
7544 type = btype; 7819 type = btype;
7545 } 7820 }
7546 unsigned int i = 0; 7821 unsigned int i = 0;
7547 for (tree field = TYPE_FIELDS (btype); field; field = TREE_CHAIN (field)) 7822 for (tree field = TYPE_FIELDS (btype); field; field = TREE_CHAIN (field))
7548 if (TREE_CODE (field) != FIELD_DECL || DECL_ARTIFICIAL (field)) 7823 if (TREE_CODE (field) != FIELD_DECL
7824 || DECL_ARTIFICIAL (field)
7825 || DECL_UNNAMED_BIT_FIELD (field))
7549 continue; 7826 continue;
7550 else 7827 else
7551 { 7828 {
7552 tree tt = finish_non_static_data_member (field, unshare_expr (t), 7829 tree tt = finish_non_static_data_member (field, unshare_expr (t),
7553 NULL_TREE); 7830 NULL_TREE);
7572 NULL_TREE, NULL_TREE); 7849 NULL_TREE, NULL_TREE);
7573 SET_DECL_VALUE_EXPR (v[i], a); 7850 SET_DECL_VALUE_EXPR (v[i], a);
7574 DECL_HAS_VALUE_EXPR_P (v[i]) = 1; 7851 DECL_HAS_VALUE_EXPR_P (v[i]) = 1;
7575 } 7852 }
7576 } 7853 }
7577 else if (DECL_NAMESPACE_SCOPE_P (decl))
7578 SET_DECL_ASSEMBLER_NAME (decl, mangle_decomp (decl, v));
7579 } 7854 }
7580 7855
7581 /* Returns a declaration for a VAR_DECL as if: 7856 /* Returns a declaration for a VAR_DECL as if:
7582 7857
7583 extern "C" TYPE NAME; 7858 extern "C" TYPE NAME;
8166 means that we didn't have these two bits set in the original type 8441 means that we didn't have these two bits set in the original type
8167 either. In completing the type, we are expected to propagate these 8442 either. In completing the type, we are expected to propagate these
8168 bits. See also complete_type which does the same thing for arrays 8443 bits. See also complete_type which does the same thing for arrays
8169 of fixed size. */ 8444 of fixed size. */
8170 type = *ptype; 8445 type = *ptype;
8171 if (TYPE_DOMAIN (type)) 8446 if (type != error_mark_node && TYPE_DOMAIN (type))
8172 { 8447 {
8173 elt_type = TREE_TYPE (type); 8448 elt_type = TREE_TYPE (type);
8174 TYPE_NEEDS_CONSTRUCTING (type) = TYPE_NEEDS_CONSTRUCTING (elt_type); 8449 TYPE_NEEDS_CONSTRUCTING (type) = TYPE_NEEDS_CONSTRUCTING (elt_type);
8175 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type) 8450 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
8176 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (elt_type); 8451 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (elt_type);
8240 enum bad_spec_place type, 8515 enum bad_spec_place type,
8241 int virtualp, 8516 int virtualp,
8242 int quals, 8517 int quals,
8243 int inlinep, 8518 int inlinep,
8244 int friendp, 8519 int friendp,
8245 int raises) 8520 int raises,
8521 const location_t* locations)
8246 { 8522 {
8247 switch (type) 8523 switch (type)
8248 { 8524 {
8249 case BSP_VAR: 8525 case BSP_VAR:
8250 if (virtualp) 8526 if (virtualp)
8251 error ("%qD declared as a %<virtual%> variable", object); 8527 error_at (locations[ds_virtual],
8528 "%qD declared as a %<virtual%> variable", object);
8252 if (quals) 8529 if (quals)
8253 error ("%<const%> and %<volatile%> function specifiers on " 8530 error ("%<const%> and %<volatile%> function specifiers on "
8254 "%qD invalid in variable declaration", object); 8531 "%qD invalid in variable declaration", object);
8255 break; 8532 break;
8256 case BSP_PARM: 8533 case BSP_PARM:
8257 if (virtualp) 8534 if (virtualp)
8258 error ("%qD declared as a %<virtual%> parameter", object); 8535 error_at (locations[ds_virtual],
8536 "%qD declared as a %<virtual%> parameter", object);
8259 if (inlinep) 8537 if (inlinep)
8260 error ("%qD declared as an %<inline%> parameter", object); 8538 error_at (locations[ds_inline],
8539 "%qD declared as an %<inline%> parameter", object);
8261 if (quals) 8540 if (quals)
8262 error ("%<const%> and %<volatile%> function specifiers on " 8541 error ("%<const%> and %<volatile%> function specifiers on "
8263 "%qD invalid in parameter declaration", object); 8542 "%qD invalid in parameter declaration", object);
8264 break; 8543 break;
8265 case BSP_TYPE: 8544 case BSP_TYPE:
8266 if (virtualp) 8545 if (virtualp)
8267 error ("%qD declared as a %<virtual%> type", object); 8546 error_at (locations[ds_virtual],
8547 "%qD declared as a %<virtual%> type", object);
8268 if (inlinep) 8548 if (inlinep)
8269 error ("%qD declared as an %<inline%> type", object); 8549 error_at (locations[ds_inline],
8550 "%qD declared as an %<inline%> type", object);
8270 if (quals) 8551 if (quals)
8271 error ("%<const%> and %<volatile%> function specifiers on " 8552 error ("%<const%> and %<volatile%> function specifiers on "
8272 "%qD invalid in type declaration", object); 8553 "%qD invalid in type declaration", object);
8273 break; 8554 break;
8274 case BSP_FIELD: 8555 case BSP_FIELD:
8275 if (virtualp) 8556 if (virtualp)
8276 error ("%qD declared as a %<virtual%> field", object); 8557 error_at (locations[ds_virtual],
8558 "%qD declared as a %<virtual%> field", object);
8277 if (inlinep) 8559 if (inlinep)
8278 error ("%qD declared as an %<inline%> field", object); 8560 error_at (locations[ds_inline],
8561 "%qD declared as an %<inline%> field", object);
8279 if (quals) 8562 if (quals)
8280 error ("%<const%> and %<volatile%> function specifiers on " 8563 error ("%<const%> and %<volatile%> function specifiers on "
8281 "%qD invalid in field declaration", object); 8564 "%qD invalid in field declaration", object);
8282 break; 8565 break;
8283 default: 8566 default:
8371 static void 8654 static void
8372 check_concept_fn (tree fn) 8655 check_concept_fn (tree fn)
8373 { 8656 {
8374 // A constraint is nullary. 8657 // A constraint is nullary.
8375 if (DECL_ARGUMENTS (fn)) 8658 if (DECL_ARGUMENTS (fn))
8376 error ("concept %q#D declared with function parameters", fn); 8659 error_at (DECL_SOURCE_LOCATION (fn),
8660 "concept %q#D declared with function parameters", fn);
8377 8661
8378 // The declared return type of the concept shall be bool, and 8662 // The declared return type of the concept shall be bool, and
8379 // it shall not be deduced from it definition. 8663 // it shall not be deduced from it definition.
8380 tree type = TREE_TYPE (TREE_TYPE (fn)); 8664 tree type = TREE_TYPE (TREE_TYPE (fn));
8381 if (is_auto (type)) 8665 if (is_auto (type))
8382 error ("concept %q#D declared with a deduced return type", fn); 8666 error_at (DECL_SOURCE_LOCATION (fn),
8667 "concept %q#D declared with a deduced return type", fn);
8383 else if (type != boolean_type_node) 8668 else if (type != boolean_type_node)
8384 error ("concept %q#D with non-%<bool%> return type %qT", fn, type); 8669 error_at (DECL_SOURCE_LOCATION (fn),
8670 "concept %q#D with non-%<bool%> return type %qT", fn, type);
8385 } 8671 }
8386 8672
8387 /* Helper function. Replace the temporary this parameter injected 8673 /* Helper function. Replace the temporary this parameter injected
8388 during cp_finish_omp_declare_simd with the real this parameter. */ 8674 during cp_finish_omp_declare_simd with the real this parameter. */
8389 8675
8422 grokfndecl (tree ctype, 8708 grokfndecl (tree ctype,
8423 tree type, 8709 tree type,
8424 tree declarator, 8710 tree declarator,
8425 tree parms, 8711 tree parms,
8426 tree orig_declarator, 8712 tree orig_declarator,
8713 const cp_decl_specifier_seq *declspecs,
8427 tree decl_reqs, 8714 tree decl_reqs,
8428 int virtualp, 8715 int virtualp,
8429 enum overload_flags flags, 8716 enum overload_flags flags,
8430 cp_cv_quals quals, 8717 cp_cv_quals quals,
8431 cp_ref_qualifier rqual, 8718 cp_ref_qualifier rqual,
8435 int publicp, 8722 int publicp,
8436 int inlinep, 8723 int inlinep,
8437 bool deletedp, 8724 bool deletedp,
8438 special_function_kind sfk, 8725 special_function_kind sfk,
8439 bool funcdef_flag, 8726 bool funcdef_flag,
8727 bool late_return_type_p,
8440 int template_count, 8728 int template_count,
8441 tree in_namespace, 8729 tree in_namespace,
8442 tree* attrlist, 8730 tree* attrlist,
8443 location_t location) 8731 location_t location)
8444 { 8732 {
8445 tree decl; 8733 tree decl;
8446 int staticp = ctype && TREE_CODE (type) == FUNCTION_TYPE; 8734 int staticp = ctype && TREE_CODE (type) == FUNCTION_TYPE;
8447 tree t; 8735 tree t;
8448 8736
8737 if (location == UNKNOWN_LOCATION)
8738 location = input_location;
8739
8449 // Was the concept specifier present? 8740 // Was the concept specifier present?
8450 bool concept_p = inlinep & 4; 8741 bool concept_p = inlinep & 4;
8451 8742
8452 // Concept declarations must have a corresponding definition. 8743 // Concept declarations must have a corresponding definition.
8453 if (concept_p && !funcdef_flag) 8744 if (concept_p && !funcdef_flag)
8454 { 8745 {
8455 error ("concept %qD has no definition", declarator); 8746 error_at (location, "concept %qD has no definition", declarator);
8456 return NULL_TREE; 8747 return NULL_TREE;
8457 } 8748 }
8458 8749
8459 if (rqual) 8750 type = build_cp_fntype_variant (type, rqual, raises, late_return_type_p);
8460 type = build_ref_qualified_type (type, rqual); 8751
8461 if (raises) 8752 decl = build_lang_decl_loc (location, FUNCTION_DECL, declarator, type);
8462 type = build_exception_variant (type, raises);
8463
8464 decl = build_lang_decl (FUNCTION_DECL, declarator, type);
8465 8753
8466 /* Set the constraints on the declaration. */ 8754 /* Set the constraints on the declaration. */
8467 if (flag_concepts) 8755 if (flag_concepts)
8468 { 8756 {
8469 tree tmpl_reqs = NULL_TREE; 8757 tree tmpl_reqs = NULL_TREE;
8475 decl_reqs = normalize_expression (decl_reqs); 8763 decl_reqs = normalize_expression (decl_reqs);
8476 8764
8477 tree ci = build_constraints (tmpl_reqs, decl_reqs); 8765 tree ci = build_constraints (tmpl_reqs, decl_reqs);
8478 set_constraints (decl, ci); 8766 set_constraints (decl, ci);
8479 } 8767 }
8480
8481 /* If we have an explicit location, use it, otherwise use whatever
8482 build_lang_decl used (probably input_location). */
8483 if (location != UNKNOWN_LOCATION)
8484 DECL_SOURCE_LOCATION (decl) = location;
8485 8768
8486 if (TREE_CODE (type) == METHOD_TYPE) 8769 if (TREE_CODE (type) == METHOD_TYPE)
8487 { 8770 {
8488 tree parm = build_this_parm (decl, type, quals); 8771 tree parm = build_this_parm (decl, type, quals);
8489 DECL_CHAIN (parm) = parms; 8772 DECL_CHAIN (parm) = parms;
8519 } 8802 }
8520 8803
8521 if (friendp && TREE_CODE (orig_declarator) == TEMPLATE_ID_EXPR) 8804 if (friendp && TREE_CODE (orig_declarator) == TEMPLATE_ID_EXPR)
8522 { 8805 {
8523 if (funcdef_flag) 8806 if (funcdef_flag)
8524 error ("defining explicit specialization %qD in friend declaration", 8807 error_at (location,
8525 orig_declarator); 8808 "defining explicit specialization %qD in friend declaration",
8809 orig_declarator);
8526 else 8810 else
8527 { 8811 {
8528 tree fns = TREE_OPERAND (orig_declarator, 0); 8812 tree fns = TREE_OPERAND (orig_declarator, 0);
8529 tree args = TREE_OPERAND (orig_declarator, 1); 8813 tree args = TREE_OPERAND (orig_declarator, 1);
8530 8814
8531 if (PROCESSING_REAL_TEMPLATE_DECL_P ()) 8815 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
8532 { 8816 {
8533 /* Something like `template <class T> friend void f<T>()'. */ 8817 /* Something like `template <class T> friend void f<T>()'. */
8534 error ("invalid use of template-id %qD in declaration " 8818 error_at (location,
8535 "of primary template", 8819 "invalid use of template-id %qD in declaration "
8536 orig_declarator); 8820 "of primary template",
8821 orig_declarator);
8537 return NULL_TREE; 8822 return NULL_TREE;
8538 } 8823 }
8539 8824
8540 8825
8541 /* A friend declaration of the form friend void f<>(). Record 8826 /* A friend declaration of the form friend void f<>(). Record
8547 8832
8548 for (t = TYPE_ARG_TYPES (TREE_TYPE (decl)); t; t = TREE_CHAIN (t)) 8833 for (t = TYPE_ARG_TYPES (TREE_TYPE (decl)); t; t = TREE_CHAIN (t))
8549 if (TREE_PURPOSE (t) 8834 if (TREE_PURPOSE (t)
8550 && TREE_CODE (TREE_PURPOSE (t)) == DEFAULT_ARG) 8835 && TREE_CODE (TREE_PURPOSE (t)) == DEFAULT_ARG)
8551 { 8836 {
8552 error ("default arguments are not allowed in declaration " 8837 error_at (defarg_location (TREE_PURPOSE (t)),
8553 "of friend template specialization %qD", 8838 "default arguments are not allowed in declaration "
8554 decl); 8839 "of friend template specialization %qD",
8840 decl);
8555 return NULL_TREE; 8841 return NULL_TREE;
8556 } 8842 }
8557 8843
8558 if (inlinep & 1) 8844 if (inlinep & 1)
8559 { 8845 {
8560 error ("%<inline%> is not allowed in declaration of friend " 8846 error_at (declspecs->locations[ds_inline],
8561 "template specialization %qD", 8847 "%<inline%> is not allowed in declaration of friend "
8562 decl); 8848 "template specialization %qD",
8849 decl);
8563 return NULL_TREE; 8850 return NULL_TREE;
8564 } 8851 }
8565 } 8852 }
8853 }
8854
8855 /* C++17 11.3.6/4: "If a friend declaration specifies a default argument
8856 expression, that declaration shall be a definition..." */
8857 if (friendp && !funcdef_flag)
8858 {
8859 for (tree t = FUNCTION_FIRST_USER_PARMTYPE (decl);
8860 t && t != void_list_node; t = TREE_CHAIN (t))
8861 if (TREE_PURPOSE (t))
8862 {
8863 permerror (DECL_SOURCE_LOCATION (decl),
8864 "friend declaration of %qD specifies default "
8865 "arguments and isn't a definition", decl);
8866 break;
8867 }
8566 } 8868 }
8567 8869
8568 /* If this decl has namespace scope, set that up. */ 8870 /* If this decl has namespace scope, set that up. */
8569 if (in_namespace) 8871 if (in_namespace)
8570 set_decl_namespace (decl, in_namespace, friendp); 8872 set_decl_namespace (decl, in_namespace, friendp);
8604 } 8906 }
8605 8907
8606 if (ctype == NULL_TREE && DECL_MAIN_P (decl)) 8908 if (ctype == NULL_TREE && DECL_MAIN_P (decl))
8607 { 8909 {
8608 if (PROCESSING_REAL_TEMPLATE_DECL_P()) 8910 if (PROCESSING_REAL_TEMPLATE_DECL_P())
8609 error ("cannot declare %<::main%> to be a template"); 8911 error_at (location, "cannot declare %<::main%> to be a template");
8610 if (inlinep & 1) 8912 if (inlinep & 1)
8611 error ("cannot declare %<::main%> to be inline"); 8913 error_at (declspecs->locations[ds_inline],
8914 "cannot declare %<::main%> to be inline");
8612 if (inlinep & 2) 8915 if (inlinep & 2)
8613 error ("cannot declare %<::main%> to be constexpr"); 8916 error_at (declspecs->locations[ds_constexpr],
8917 "cannot declare %<::main%> to be %<constexpr%>");
8614 if (!publicp) 8918 if (!publicp)
8615 error ("cannot declare %<::main%> to be static"); 8919 error_at (location, "cannot declare %<::main%> to be static");
8616 inlinep = 0; 8920 inlinep = 0;
8617 publicp = 1; 8921 publicp = 1;
8618 } 8922 }
8619 8923
8620 /* Members of anonymous types and local classes have no linkage; make 8924 /* Members of anonymous types and local classes have no linkage; make
8696 if (funcdef_flag) 9000 if (funcdef_flag)
8697 error_at (location, 9001 error_at (location,
8698 "deduction guide %qD must not have a function body", decl); 9002 "deduction guide %qD must not have a function body", decl);
8699 } 9003 }
8700 else if (IDENTIFIER_ANY_OP_P (DECL_NAME (decl)) 9004 else if (IDENTIFIER_ANY_OP_P (DECL_NAME (decl))
8701 && !grok_op_properties (decl, /*complain=*/true)) 9005 && !grok_op_properties (decl, /*complain=*/true))
8702 return NULL_TREE; 9006 return NULL_TREE;
8703 else if (UDLIT_OPER_P (DECL_NAME (decl))) 9007 else if (UDLIT_OPER_P (DECL_NAME (decl)))
8704 { 9008 {
8705 bool long_long_unsigned_p; 9009 bool long_long_unsigned_p;
8706 bool long_double_p; 9010 bool long_double_p;
8707 const char *suffix = NULL; 9011 const char *suffix = NULL;
8708 /* [over.literal]/6: Literal operators shall not have C linkage. */ 9012 /* [over.literal]/6: Literal operators shall not have C linkage. */
8709 if (DECL_LANGUAGE (decl) == lang_c) 9013 if (DECL_LANGUAGE (decl) == lang_c)
8710 { 9014 {
8711 error ("literal operator with C linkage"); 9015 error_at (location, "literal operator with C linkage");
8712 maybe_show_extern_c_location (); 9016 maybe_show_extern_c_location ();
8713 return NULL_TREE; 9017 return NULL_TREE;
8714 } 9018 }
8715 9019
8716 if (DECL_NAMESPACE_SCOPE_P (decl)) 9020 if (DECL_NAMESPACE_SCOPE_P (decl))
8717 { 9021 {
8718 if (!check_literal_operator_args (decl, &long_long_unsigned_p, 9022 if (!check_literal_operator_args (decl, &long_long_unsigned_p,
8719 &long_double_p)) 9023 &long_double_p))
8720 { 9024 {
8721 error ("%qD has invalid argument list", decl); 9025 error_at (location, "%qD has invalid argument list", decl);
8722 return NULL_TREE; 9026 return NULL_TREE;
8723 } 9027 }
8724 9028
8725 suffix = UDLIT_OP_SUFFIX (DECL_NAME (decl)); 9029 suffix = UDLIT_OP_SUFFIX (DECL_NAME (decl));
8726 if (long_long_unsigned_p) 9030 if (long_long_unsigned_p)
8727 { 9031 {
8728 if (cpp_interpret_int_suffix (parse_in, suffix, strlen (suffix))) 9032 if (cpp_interpret_int_suffix (parse_in, suffix, strlen (suffix)))
8729 warning (0, "integer suffix %qs" 9033 warning_at (location, 0, "integer suffix %qs"
8730 " shadowed by implementation", suffix); 9034 " shadowed by implementation", suffix);
8731 } 9035 }
8732 else if (long_double_p) 9036 else if (long_double_p)
8733 { 9037 {
8734 if (cpp_interpret_float_suffix (parse_in, suffix, strlen (suffix))) 9038 if (cpp_interpret_float_suffix (parse_in, suffix, strlen (suffix)))
8735 warning (0, "floating point suffix %qs" 9039 warning_at (location, 0, "floating point suffix %qs"
8736 " shadowed by implementation", suffix); 9040 " shadowed by implementation", suffix);
8737 } 9041 }
9042 /* 17.6.3.3.5 */
9043 if (suffix[0] != '_'
9044 && !in_system_header_at (location)
9045 && !current_function_decl && !(friendp && !funcdef_flag))
9046 warning_at (location, OPT_Wliteral_suffix,
9047 "literal operator suffixes not preceded by %<_%>"
9048 " are reserved for future standardization");
8738 } 9049 }
8739 else 9050 else
8740 { 9051 {
8741 error ("%qD must be a non-member function", decl); 9052 error_at (location, "%qD must be a non-member function", decl);
8742 return NULL_TREE; 9053 return NULL_TREE;
8743 } 9054 }
8744 } 9055 }
8745 9056
8746 if (funcdef_flag) 9057 if (funcdef_flag)
8749 DECL_INITIAL (decl) = error_mark_node; 9060 DECL_INITIAL (decl) = error_mark_node;
8750 9061
8751 if (TYPE_NOTHROW_P (type) || nothrow_libfn_p (decl)) 9062 if (TYPE_NOTHROW_P (type) || nothrow_libfn_p (decl))
8752 TREE_NOTHROW (decl) = 1; 9063 TREE_NOTHROW (decl) = 1;
8753 9064
8754 if (flag_openmp || flag_openmp_simd || flag_cilkplus) 9065 if (flag_openmp || flag_openmp_simd)
8755 { 9066 {
8756 /* Adjust "omp declare simd" attributes. */ 9067 /* Adjust "omp declare simd" attributes. */
8757 tree ods = lookup_attribute ("omp declare simd", *attrlist); 9068 tree ods = lookup_attribute ("omp declare simd", *attrlist);
8758 if (ods) 9069 if (ods)
8759 { 9070 {
8794 9105
8795 decl = check_explicit_specialization (orig_declarator, decl, 9106 decl = check_explicit_specialization (orig_declarator, decl,
8796 template_count, 9107 template_count,
8797 2 * funcdef_flag + 9108 2 * funcdef_flag +
8798 4 * (friendp != 0) + 9109 4 * (friendp != 0) +
8799 8 * concept_p); 9110 8 * concept_p,
9111 *attrlist);
8800 if (decl == error_mark_node) 9112 if (decl == error_mark_node)
8801 return NULL_TREE; 9113 return NULL_TREE;
8802 9114
8803 if (DECL_STATIC_FUNCTION_P (decl)) 9115 if (DECL_STATIC_FUNCTION_P (decl))
8804 check_static_quals (decl, quals); 9116 check_static_quals (decl, quals);
8815 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)), 9127 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
8816 integer_type_node)) 9128 integer_type_node))
8817 { 9129 {
8818 tree oldtypeargs = TYPE_ARG_TYPES (TREE_TYPE (decl)); 9130 tree oldtypeargs = TYPE_ARG_TYPES (TREE_TYPE (decl));
8819 tree newtype; 9131 tree newtype;
8820 error ("%<::main%> must return %<int%>"); 9132 error_at (declspecs->locations[ds_type_spec],
9133 "%<::main%> must return %<int%>");
8821 newtype = build_function_type (integer_type_node, oldtypeargs); 9134 newtype = build_function_type (integer_type_node, oldtypeargs);
8822 TREE_TYPE (decl) = newtype; 9135 TREE_TYPE (decl) = newtype;
8823 } 9136 }
8824 if (warn_main) 9137 if (warn_main)
8825 check_main_parameter_types (decl); 9138 check_main_parameter_types (decl);
9076 Note that this also forbids explicit specializations. */ 9389 Note that this also forbids explicit specializations. */
9077 if (conceptp) 9390 if (conceptp)
9078 { 9391 {
9079 if (!processing_template_decl) 9392 if (!processing_template_decl)
9080 { 9393 {
9081 error ("a non-template variable cannot be %<concept%>"); 9394 error_at (declspecs->locations[ds_concept],
9395 "a non-template variable cannot be %<concept%>");
9082 return NULL_TREE; 9396 return NULL_TREE;
9083 } 9397 }
9084 else 9398 else
9085 DECL_DECLARED_CONCEPT_P (decl) = true; 9399 DECL_DECLARED_CONCEPT_P (decl) = true;
9086 if (!same_type_ignoring_top_level_qualifiers_p (type, boolean_type_node)) 9400 if (!same_type_ignoring_top_level_qualifiers_p (type, boolean_type_node))
9181 } 9495 }
9182 } 9496 }
9183 9497
9184 /* DECL is a VAR_DECL defined in-class, whose TYPE is also given. 9498 /* DECL is a VAR_DECL defined in-class, whose TYPE is also given.
9185 Check to see that the definition is valid. Issue appropriate error 9499 Check to see that the definition is valid. Issue appropriate error
9186 messages. Return 1 if the definition is particularly bad, or 0 9500 messages. */
9187 otherwise. */ 9501
9188 9502 static void
9189 static int
9190 check_static_variable_definition (tree decl, tree type) 9503 check_static_variable_definition (tree decl, tree type)
9191 { 9504 {
9192 /* Avoid redundant diagnostics on out-of-class definitions. */ 9505 /* Avoid redundant diagnostics on out-of-class definitions. */
9193 if (!current_class_type || !TYPE_BEING_DEFINED (current_class_type)) 9506 if (!current_class_type || !TYPE_BEING_DEFINED (current_class_type))
9194 return 0; 9507 ;
9195 /* Can't check yet if we don't know the type. */ 9508 /* Can't check yet if we don't know the type. */
9196 if (dependent_type_p (type)) 9509 else if (dependent_type_p (type))
9197 return 0; 9510 ;
9198 /* If DECL is declared constexpr, we'll do the appropriate checks 9511 /* If DECL is declared constexpr, we'll do the appropriate checks
9199 in check_initializer. Similarly for inline static data members. */ 9512 in check_initializer. Similarly for inline static data members. */
9200 if (DECL_P (decl) 9513 else if (DECL_P (decl)
9201 && (DECL_DECLARED_CONSTEXPR_P (decl) 9514 && (DECL_DECLARED_CONSTEXPR_P (decl)
9515 || undeduced_auto_decl (decl)
9202 || DECL_VAR_DECLARED_INLINE_P (decl))) 9516 || DECL_VAR_DECLARED_INLINE_P (decl)))
9203 return 0; 9517 ;
9204 else if (cxx_dialect >= cxx11 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)) 9518 else if (cxx_dialect >= cxx11 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type))
9205 { 9519 {
9206 if (!COMPLETE_TYPE_P (type)) 9520 if (!COMPLETE_TYPE_P (type))
9207 error_at (DECL_SOURCE_LOCATION (decl), 9521 error_at (DECL_SOURCE_LOCATION (decl),
9208 "in-class initialization of static data member %q#D of " 9522 "in-class initialization of static data member %q#D of "
9213 "static data member %q#D of non-integral type", decl); 9527 "static data member %q#D of non-integral type", decl);
9214 else 9528 else
9215 error_at (DECL_SOURCE_LOCATION (decl), 9529 error_at (DECL_SOURCE_LOCATION (decl),
9216 "in-class initialization of static data member %q#D of " 9530 "in-class initialization of static data member %q#D of "
9217 "non-literal type", decl); 9531 "non-literal type", decl);
9218 return 1; 9532 }
9219 }
9220
9221 /* Motion 10 at San Diego: If a static const integral data member is 9533 /* Motion 10 at San Diego: If a static const integral data member is
9222 initialized with an integral constant expression, the initializer 9534 initialized with an integral constant expression, the initializer
9223 may appear either in the declaration (within the class), or in 9535 may appear either in the declaration (within the class), or in
9224 the definition, but not both. If it appears in the class, the 9536 the definition, but not both. If it appears in the class, the
9225 member is a member constant. The file-scope definition is always 9537 member is a member constant. The file-scope definition is always
9226 required. */ 9538 required. */
9227 if (!ARITHMETIC_TYPE_P (type) && TREE_CODE (type) != ENUMERAL_TYPE) 9539 else if (!ARITHMETIC_TYPE_P (type) && TREE_CODE (type) != ENUMERAL_TYPE)
9228 { 9540 error_at (DECL_SOURCE_LOCATION (decl),
9229 error_at (DECL_SOURCE_LOCATION (decl), 9541 "invalid in-class initialization of static data member "
9230 "invalid in-class initialization of static data member " 9542 "of non-integral type %qT",
9231 "of non-integral type %qT", 9543 type);
9232 type);
9233 return 1;
9234 }
9235 else if (!CP_TYPE_CONST_P (type)) 9544 else if (!CP_TYPE_CONST_P (type))
9236 error_at (DECL_SOURCE_LOCATION (decl), 9545 error_at (DECL_SOURCE_LOCATION (decl),
9237 "ISO C++ forbids in-class initialization of non-const " 9546 "ISO C++ forbids in-class initialization of non-const "
9238 "static member %qD", 9547 "static member %qD",
9239 decl); 9548 decl);
9240 else if (!INTEGRAL_OR_ENUMERATION_TYPE_P (type)) 9549 else if (!INTEGRAL_OR_ENUMERATION_TYPE_P (type))
9241 pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wpedantic, 9550 pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wpedantic,
9242 "ISO C++ forbids initialization of member constant " 9551 "ISO C++ forbids initialization of member constant "
9243 "%qD of non-integral type %qT", decl, type); 9552 "%qD of non-integral type %qT", decl, type);
9244
9245 return 0;
9246 } 9553 }
9247 9554
9248 /* *expr_p is part of the TYPE_SIZE of a variably-sized array. If any 9555 /* *expr_p is part of the TYPE_SIZE of a variably-sized array. If any
9249 SAVE_EXPRs in *expr_p wrap expressions with side-effects, break those 9556 SAVE_EXPRs in *expr_p wrap expressions with side-effects, break those
9250 expressions out into temporary variables so that walk_tree doesn't 9557 expressions out into temporary variables so that walk_tree doesn't
9284 fold_sizeof_expr (tree t) 9591 fold_sizeof_expr (tree t)
9285 { 9592 {
9286 tree r; 9593 tree r;
9287 if (SIZEOF_EXPR_TYPE_P (t)) 9594 if (SIZEOF_EXPR_TYPE_P (t))
9288 r = cxx_sizeof_or_alignof_type (TREE_TYPE (TREE_OPERAND (t, 0)), 9595 r = cxx_sizeof_or_alignof_type (TREE_TYPE (TREE_OPERAND (t, 0)),
9289 SIZEOF_EXPR, false); 9596 SIZEOF_EXPR, false, false);
9290 else if (TYPE_P (TREE_OPERAND (t, 0))) 9597 else if (TYPE_P (TREE_OPERAND (t, 0)))
9291 r = cxx_sizeof_or_alignof_type (TREE_OPERAND (t, 0), SIZEOF_EXPR, 9598 r = cxx_sizeof_or_alignof_type (TREE_OPERAND (t, 0), SIZEOF_EXPR,
9292 false); 9599 false, false);
9293 else 9600 else
9294 r = cxx_sizeof_or_alignof_expr (TREE_OPERAND (t, 0), SIZEOF_EXPR, 9601 r = cxx_sizeof_or_alignof_expr (TREE_OPERAND (t, 0), SIZEOF_EXPR,
9295 false); 9602 false);
9296 if (r == error_mark_node) 9603 if (r == error_mark_node)
9297 r = size_one_node; 9604 r = size_one_node;
9311 if (error_operand_p (size)) 9618 if (error_operand_p (size))
9312 return error_mark_node; 9619 return error_mark_node;
9313 9620
9314 if (!type_dependent_expression_p (size)) 9621 if (!type_dependent_expression_p (size))
9315 { 9622 {
9316 tree type = TREE_TYPE (size); 9623 osize = size = mark_rvalue_use (size);
9317
9318 size = mark_rvalue_use (size);
9319 9624
9320 if (cxx_dialect < cxx11 && TREE_CODE (size) == NOP_EXPR 9625 if (cxx_dialect < cxx11 && TREE_CODE (size) == NOP_EXPR
9321 && TREE_SIDE_EFFECTS (size)) 9626 && TREE_SIDE_EFFECTS (size))
9322 /* In C++98, we mark a non-constant array bound with a magic 9627 /* In C++98, we mark a non-constant array bound with a magic
9323 NOP_EXPR with TREE_SIDE_EFFECTS; don't fold in that case. */; 9628 NOP_EXPR with TREE_SIDE_EFFECTS; don't fold in that case. */;
9324 else 9629 else
9325 { 9630 {
9326 size = instantiate_non_dependent_expr_sfinae (size, complain); 9631 size = instantiate_non_dependent_expr_sfinae (size, complain);
9327 9632 size = build_converted_constant_expr (size_type_node, size, complain);
9328 if (CLASS_TYPE_P (type) 9633 size = maybe_constant_value (size);
9329 && CLASSTYPE_LITERAL_P (type))
9330 {
9331 size = build_expr_type_conversion (WANT_INT, size, true);
9332 if (!size)
9333 {
9334 if (!(complain & tf_error))
9335 return error_mark_node;
9336 if (name)
9337 error ("size of array %qD has non-integral type %qT",
9338 name, type);
9339 else
9340 error ("size of array has non-integral type %qT", type);
9341 size = integer_one_node;
9342 }
9343 if (size == error_mark_node)
9344 return error_mark_node;
9345 type = TREE_TYPE (size);
9346 }
9347
9348 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
9349 size = maybe_constant_value (size);
9350 9634
9351 if (!TREE_CONSTANT (size)) 9635 if (!TREE_CONSTANT (size))
9352 size = osize; 9636 size = osize;
9353 } 9637 }
9354 9638
9355 if (error_operand_p (size)) 9639 if (error_operand_p (size))
9356 return error_mark_node; 9640 return error_mark_node;
9357 9641
9358 /* The array bound must be an integer type. */ 9642 /* The array bound must be an integer type. */
9643 tree type = TREE_TYPE (size);
9359 if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type)) 9644 if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
9360 { 9645 {
9361 if (!(complain & tf_error)) 9646 if (!(complain & tf_error))
9362 return error_mark_node; 9647 return error_mark_node;
9363 if (name) 9648 if (name)
9364 error ("size of array %qD has non-integral type %qT", name, type); 9649 error ("size of array %qD has non-integral type %qT", name, type);
9365 else 9650 else
9366 error ("size of array has non-integral type %qT", type); 9651 error ("size of array has non-integral type %qT", type);
9367 size = integer_one_node; 9652 size = integer_one_node;
9368 type = TREE_TYPE (size);
9369 } 9653 }
9370 } 9654 }
9371 9655
9372 /* A type is dependent if it is...an array type constructed from any 9656 /* A type is dependent if it is...an array type constructed from any
9373 dependent type or whose size is specified by a constant expression 9657 dependent type or whose size is specified by a constant expression
9391 9675
9392 if (TREE_CODE (size) != INTEGER_CST) 9676 if (TREE_CODE (size) != INTEGER_CST)
9393 { 9677 {
9394 tree folded = cp_fully_fold (size); 9678 tree folded = cp_fully_fold (size);
9395 if (TREE_CODE (folded) == INTEGER_CST) 9679 if (TREE_CODE (folded) == INTEGER_CST)
9396 pedwarn (location_of (size), OPT_Wpedantic, 9680 pedwarn (input_location, OPT_Wpedantic,
9397 "size of array is not an integral constant-expression"); 9681 "size of array is not an integral constant-expression");
9398 /* Use the folded result for VLAs, too; it will have resolved 9682 /* Use the folded result for VLAs, too; it will have resolved
9399 SIZEOF_EXPR. */ 9683 SIZEOF_EXPR. */
9400 size = folded; 9684 size = folded;
9401 } 9685 }
9402 9686
9403 /* Normally, the array-bound will be a constant. */ 9687 /* Normally, the array-bound will be a constant. */
9404 if (TREE_CODE (size) == INTEGER_CST) 9688 if (TREE_CODE (size) == INTEGER_CST)
9405 { 9689 {
9406 /* Check to see if the array bound overflowed. Make that an
9407 error, no matter how generous we're being. */
9408 constant_expression_error (size);
9409
9410 /* An array must have a positive number of elements. */ 9690 /* An array must have a positive number of elements. */
9411 if (tree_int_cst_lt (size, integer_zero_node)) 9691 if (!valid_constant_size_p (size))
9412 { 9692 {
9413 if (!(complain & tf_error)) 9693 if (!(complain & tf_error))
9414 return error_mark_node; 9694 return error_mark_node;
9695
9415 if (name) 9696 if (name)
9416 error ("size of array %qD is negative", name); 9697 error ("size of array %qD is negative", name);
9417 else 9698 else
9418 error ("size of array is negative"); 9699 error ("size of array is negative");
9419 size = integer_one_node; 9700 size = integer_one_node;
9470 if (processing_template_decl && !TREE_CONSTANT (size)) 9751 if (processing_template_decl && !TREE_CONSTANT (size))
9471 /* A variable sized array. */ 9752 /* A variable sized array. */
9472 itype = build_min (MINUS_EXPR, sizetype, size, integer_one_node); 9753 itype = build_min (MINUS_EXPR, sizetype, size, integer_one_node);
9473 else 9754 else
9474 { 9755 {
9475 HOST_WIDE_INT saved_processing_template_decl;
9476
9477 /* Compute the index of the largest element in the array. It is 9756 /* Compute the index of the largest element in the array. It is
9478 one less than the number of elements in the array. We save 9757 one less than the number of elements in the array. We save
9479 and restore PROCESSING_TEMPLATE_DECL so that computations in 9758 and restore PROCESSING_TEMPLATE_DECL so that computations in
9480 cp_build_binary_op will be appropriately folded. */ 9759 cp_build_binary_op will be appropriately folded. */
9481 saved_processing_template_decl = processing_template_decl; 9760 {
9482 processing_template_decl = 0; 9761 processing_template_decl_sentinel s;
9483 itype = cp_build_binary_op (input_location, 9762 itype = cp_build_binary_op (input_location,
9484 MINUS_EXPR, 9763 MINUS_EXPR,
9485 cp_convert (ssizetype, size, complain), 9764 cp_convert (ssizetype, size, complain),
9486 cp_convert (ssizetype, integer_one_node, 9765 cp_convert (ssizetype, integer_one_node,
9487 complain), 9766 complain),
9488 complain); 9767 complain);
9489 itype = maybe_constant_value (itype); 9768 itype = maybe_constant_value (itype);
9490 processing_template_decl = saved_processing_template_decl; 9769 }
9491 9770
9492 if (!TREE_CONSTANT (itype)) 9771 if (!TREE_CONSTANT (itype))
9493 { 9772 {
9494 /* A variable sized array. */ 9773 /* A variable sized array. */
9495 itype = variable_size (itype); 9774 itype = variable_size (itype);
9566 9845
9567 /* 8.3.4/1: If the type of the identifier of D contains the auto 9846 /* 8.3.4/1: If the type of the identifier of D contains the auto
9568 type-specifier, the program is ill-formed. */ 9847 type-specifier, the program is ill-formed. */
9569 if (type_uses_auto (type)) 9848 if (type_uses_auto (type))
9570 { 9849 {
9571 error ("%qD declared as array of %qT", name, type); 9850 if (name)
9851 error ("%qD declared as array of %qT", name, type);
9852 else
9853 error ("creating array of %qT", type);
9572 return error_mark_node; 9854 return error_mark_node;
9573 } 9855 }
9574 9856
9575 /* If there are some types which cannot be array elements, 9857 /* If there are some types which cannot be array elements,
9576 issue an error-message and return. */ 9858 issue an error-message and return. */
9635 abstract_virtuals_error (name, type); 9917 abstract_virtuals_error (name, type);
9636 9918
9637 return build_cplus_array_type (type, itype); 9919 return build_cplus_array_type (type, itype);
9638 } 9920 }
9639 9921
9922 /* Returns the smallest location that is not UNKNOWN_LOCATION. */
9923
9924 static location_t
9925 min_location (location_t loca, location_t locb)
9926 {
9927 if (loca == UNKNOWN_LOCATION
9928 || (locb != UNKNOWN_LOCATION
9929 && linemap_location_before_p (line_table, locb, loca)))
9930 return locb;
9931 return loca;
9932 }
9933
9640 /* Returns the smallest location != UNKNOWN_LOCATION among the 9934 /* Returns the smallest location != UNKNOWN_LOCATION among the
9641 three stored in LOCATIONS[ds_const], LOCATIONS[ds_volatile], 9935 three stored in LOCATIONS[ds_const], LOCATIONS[ds_volatile],
9642 and LOCATIONS[ds_restrict]. */ 9936 and LOCATIONS[ds_restrict]. */
9643 9937
9644 static location_t 9938 static location_t
9647 location_t loc = UNKNOWN_LOCATION; 9941 location_t loc = UNKNOWN_LOCATION;
9648 9942
9649 if (type_quals & TYPE_QUAL_CONST) 9943 if (type_quals & TYPE_QUAL_CONST)
9650 loc = locations[ds_const]; 9944 loc = locations[ds_const];
9651 9945
9652 if ((type_quals & TYPE_QUAL_VOLATILE) 9946 if (type_quals & TYPE_QUAL_VOLATILE)
9653 && (loc == UNKNOWN_LOCATION || locations[ds_volatile] < loc)) 9947 loc = min_location (loc, locations[ds_volatile]);
9654 loc = locations[ds_volatile]; 9948
9655 9949 if (type_quals & TYPE_QUAL_RESTRICT)
9656 if ((type_quals & TYPE_QUAL_RESTRICT) 9950 loc = min_location (loc, locations[ds_restrict]);
9657 && (loc == UNKNOWN_LOCATION || locations[ds_restrict] < loc))
9658 loc = locations[ds_restrict];
9659 9951
9660 return loc; 9952 return loc;
9661 } 9953 }
9662 9954
9663 /* Check that it's OK to declare a function with the indicated TYPE 9955 /* Check that it's OK to declare a function with the indicated TYPE
9720 error ("return type specified for deduction guide"); 10012 error ("return type specified for deduction guide");
9721 else if (type_quals != TYPE_UNQUALIFIED) 10013 else if (type_quals != TYPE_UNQUALIFIED)
9722 error_at (smallest_type_quals_location (type_quals, locations), 10014 error_at (smallest_type_quals_location (type_quals, locations),
9723 "qualifiers are not allowed on declaration of " 10015 "qualifiers are not allowed on declaration of "
9724 "deduction guide"); 10016 "deduction guide");
9725 type = make_template_placeholder (CLASSTYPE_TI_TEMPLATE (optype)); 10017 if (TREE_CODE (optype) == TEMPLATE_TEMPLATE_PARM)
10018 {
10019 error ("template template parameter %qT in declaration of "
10020 "deduction guide", optype);
10021 type = error_mark_node;
10022 }
10023 else
10024 type = make_template_placeholder (CLASSTYPE_TI_TEMPLATE (optype));
9726 for (int i = 0; i < ds_last; ++i) 10025 for (int i = 0; i < ds_last; ++i)
9727 if (i != ds_explicit && locations[i]) 10026 if (i != ds_explicit && locations[i])
9728 error_at (locations[i], 10027 error_at (locations[i],
9729 "decl-specifier in declaration of deduction guide"); 10028 "decl-specifier in declaration of deduction guide");
9730 break; 10029 break;
9762 } 10061 }
9763 10062
9764 /* Handle declaring DECL as an inline variable. */ 10063 /* Handle declaring DECL as an inline variable. */
9765 10064
9766 static void 10065 static void
9767 mark_inline_variable (tree decl) 10066 mark_inline_variable (tree decl, location_t loc)
9768 { 10067 {
9769 bool inlinep = true; 10068 bool inlinep = true;
9770 if (! toplevel_bindings_p ()) 10069 if (! toplevel_bindings_p ())
9771 { 10070 {
9772 error ("%<inline%> specifier invalid for variable " 10071 error_at (loc, "%<inline%> specifier invalid for variable "
9773 "%qD declared at block scope", decl); 10072 "%qD declared at block scope", decl);
9774 inlinep = false; 10073 inlinep = false;
9775 } 10074 }
9776 else if (cxx_dialect < cxx17) 10075 else if (cxx_dialect < cxx17)
9777 pedwarn (DECL_SOURCE_LOCATION (decl), 0, 10076 pedwarn (loc, 0, "inline variables are only available "
9778 "inline variables are only available "
9779 "with -std=c++17 or -std=gnu++17"); 10077 "with -std=c++17 or -std=gnu++17");
9780 if (inlinep) 10078 if (inlinep)
9781 { 10079 {
9782 retrofit_lang_decl (decl); 10080 retrofit_lang_decl (decl);
9783 SET_DECL_VAR_DECLARED_INLINE_P (decl); 10081 SET_DECL_VAR_DECLARED_INLINE_P (decl);
10018 tree decl = id_declarator->u.id.unqualified_name; 10316 tree decl = id_declarator->u.id.unqualified_name;
10019 if (!decl) 10317 if (!decl)
10020 break; 10318 break;
10021 if (qualifying_scope) 10319 if (qualifying_scope)
10022 { 10320 {
10321 if (check_for_bare_parameter_packs (qualifying_scope,
10322 id_declarator->id_loc))
10323 return error_mark_node;
10023 if (at_function_scope_p ()) 10324 if (at_function_scope_p ())
10024 { 10325 {
10025 /* [dcl.meaning] 10326 /* [dcl.meaning]
10026 10327
10027 A declarator-id shall not be qualified except 10328 A declarator-id shall not be qualified except
10214 if (name == NULL) 10515 if (name == NULL)
10215 name = decl_context == PARM ? "parameter" : "type name"; 10516 name = decl_context == PARM ? "parameter" : "type name";
10216 10517
10217 if (concept_p && typedef_p) 10518 if (concept_p && typedef_p)
10218 { 10519 {
10219 error ("%<concept%> cannot appear in a typedef declaration"); 10520 error_at (declspecs->locations[ds_concept],
10521 "%<concept%> cannot appear in a typedef declaration");
10220 return error_mark_node; 10522 return error_mark_node;
10221 } 10523 }
10222 10524
10223 if (constexpr_p && typedef_p) 10525 if (constexpr_p && typedef_p)
10224 { 10526 {
10225 error ("%<constexpr%> cannot appear in a typedef declaration"); 10527 error_at (declspecs->locations[ds_constexpr],
10528 "%<constexpr%> cannot appear in a typedef declaration");
10226 return error_mark_node; 10529 return error_mark_node;
10227 } 10530 }
10228 10531
10229 /* If there were multiple types specified in the decl-specifier-seq, 10532 /* If there were multiple types specified in the decl-specifier-seq,
10230 issue an error message. */ 10533 issue an error message. */
10245 if (type == error_mark_node) 10548 if (type == error_mark_node)
10246 { 10549 {
10247 type = NULL_TREE; 10550 type = NULL_TREE;
10248 type_was_error_mark_node = true; 10551 type_was_error_mark_node = true;
10249 } 10552 }
10250 /* If the entire declaration is itself tagged as deprecated then 10553 cp_warn_deprecated_use (type);
10251 suppress reports of deprecated items. */
10252 if (type && TREE_DEPRECATED (type)
10253 && deprecated_state != DEPRECATED_SUPPRESS)
10254 warn_deprecated_use (type, NULL_TREE);
10255 if (type && TREE_CODE (type) == TYPE_DECL) 10554 if (type && TREE_CODE (type) == TYPE_DECL)
10256 { 10555 {
10257 typedef_decl = type; 10556 typedef_decl = type;
10258 type = TREE_TYPE (typedef_decl); 10557 type = TREE_TYPE (typedef_decl);
10259 if (TREE_DEPRECATED (type) 10558 if (DECL_ARTIFICIAL (typedef_decl))
10260 && DECL_ARTIFICIAL (typedef_decl) 10559 cp_warn_deprecated_use (type);
10261 && deprecated_state != DEPRECATED_SUPPRESS)
10262 warn_deprecated_use (type, NULL_TREE);
10263 } 10560 }
10264 /* No type at all: default to `int', and set DEFAULTED_INT 10561 /* No type at all: default to `int', and set DEFAULTED_INT
10265 because it was not a user-defined typedef. */ 10562 because it was not a user-defined typedef. */
10266 if (type == NULL_TREE) 10563 if (type == NULL_TREE)
10267 { 10564 {
10330 else if (pedantic) 10627 else if (pedantic)
10331 pedwarn (input_location, OPT_Wpedantic, 10628 pedwarn (input_location, OPT_Wpedantic,
10332 "ISO C++ forbids declaration of %qs with no type", name); 10629 "ISO C++ forbids declaration of %qs with no type", name);
10333 else 10630 else
10334 warning (OPT_Wreturn_type, 10631 warning (OPT_Wreturn_type,
10335 "ISO C++ forbids declaration of %qs with no type", name); 10632 "ISO C++ forbids declaration of %qs with no type", name);
10336 10633
10337 if (type_was_error_mark_node && template_parm_flag) 10634 if (type_was_error_mark_node && template_parm_flag)
10338 /* FIXME we should be able to propagate the error_mark_node as is 10635 /* FIXME we should be able to propagate the error_mark_node as is
10339 for other contexts too. */ 10636 for other contexts too. */
10340 type = error_mark_node; 10637 type = error_mark_node;
10371 10668
10372 /* Check all other uses of type modifiers. */ 10669 /* Check all other uses of type modifiers. */
10373 10670
10374 if (unsigned_p || signed_p || long_p || short_p) 10671 if (unsigned_p || signed_p || long_p || short_p)
10375 { 10672 {
10673 location_t loc;
10674 const char *key;
10675 if (unsigned_p)
10676 {
10677 key = "unsigned";
10678 loc = declspecs->locations[ds_unsigned];
10679 }
10680 else if (signed_p)
10681 {
10682 key = "signed";
10683 loc = declspecs->locations[ds_signed];
10684 }
10685 else if (longlong)
10686 {
10687 key = "long long";
10688 loc = declspecs->locations[ds_long_long];
10689 }
10690 else if (long_p)
10691 {
10692 key = "long";
10693 loc = declspecs->locations[ds_long];
10694 }
10695 else /* if (short_p) */
10696 {
10697 key = "short";
10698 loc = declspecs->locations[ds_short];
10699 }
10700
10376 int ok = 0; 10701 int ok = 0;
10377 10702
10378 if ((signed_p || unsigned_p) && TREE_CODE (type) != INTEGER_TYPE) 10703 if (signed_p && unsigned_p)
10379 error ("%<signed%> or %<unsigned%> invalid for %qs", name); 10704 {
10380 else if (signed_p && unsigned_p) 10705 gcc_rich_location richloc (declspecs->locations[ds_signed]);
10381 error ("%<signed%> and %<unsigned%> specified together for %qs", name); 10706 richloc.add_range (declspecs->locations[ds_unsigned]);
10382 else if (longlong && TREE_CODE (type) != INTEGER_TYPE) 10707 error_at (&richloc,
10383 error ("%<long long%> invalid for %qs", name); 10708 "%<signed%> and %<unsigned%> specified together");
10384 else if (long_p && TREE_CODE (type) == REAL_TYPE) 10709 }
10385 error ("%<long%> invalid for %qs", name);
10386 else if (short_p && TREE_CODE (type) == REAL_TYPE)
10387 error ("%<short%> invalid for %qs", name);
10388 else if ((long_p || short_p) && TREE_CODE (type) != INTEGER_TYPE)
10389 error ("%<long%> or %<short%> invalid for %qs", name);
10390 else if ((long_p || short_p || explicit_char || explicit_int) && explicit_intN)
10391 error ("%<long%>, %<int%>, %<short%>, or %<char%> invalid for %qs", name);
10392 else if ((long_p || short_p) && explicit_char)
10393 error ("%<long%> or %<short%> specified with char for %qs", name);
10394 else if (long_p && short_p) 10710 else if (long_p && short_p)
10395 error ("%<long%> and %<short%> specified together for %qs", name); 10711 {
10396 else if (type == char16_type_node || type == char32_type_node) 10712 gcc_rich_location richloc (declspecs->locations[ds_long]);
10397 { 10713 richloc.add_range (declspecs->locations[ds_short]);
10398 if (signed_p || unsigned_p) 10714 error_at (&richloc, "%<long%> and %<short%> specified together");
10399 error ("%<signed%> or %<unsigned%> invalid for %qs", name); 10715 }
10400 else if (short_p || long_p) 10716 else if (TREE_CODE (type) != INTEGER_TYPE
10401 error ("%<short%> or %<long%> invalid for %qs", name); 10717 || type == char16_type_node || type == char32_type_node
10718 || ((long_p || short_p)
10719 && (explicit_char || explicit_intN)))
10720 error_at (loc, "%qs specified with %qT", key, type);
10721 else if (!explicit_int && !defaulted_int
10722 && !explicit_char && !explicit_intN)
10723 {
10724 if (typedef_decl)
10725 {
10726 pedwarn (loc, OPT_Wpedantic, "%qs specified with %qT",
10727 key, type);
10728 ok = !flag_pedantic_errors;
10729 }
10730 else if (declspecs->decltype_p)
10731 error_at (loc, "%qs specified with %<decltype%>", key);
10732 else
10733 error_at (loc, "%qs specified with %<typeof%>", key);
10402 } 10734 }
10403 else 10735 else
10404 { 10736 ok = 1;
10405 ok = 1;
10406 if (!explicit_int && !defaulted_int && !explicit_char && !explicit_intN && pedantic)
10407 {
10408 pedwarn (input_location, OPT_Wpedantic,
10409 "long, short, signed or unsigned used invalidly for %qs",
10410 name);
10411 if (flag_pedantic_errors)
10412 ok = 0;
10413 }
10414 }
10415 10737
10416 /* Discard the type modifiers if they are invalid. */ 10738 /* Discard the type modifiers if they are invalid. */
10417 if (! ok) 10739 if (! ok)
10418 { 10740 {
10419 unsigned_p = false; 10741 unsigned_p = false;
10529 10851
10530 if (virtualp) 10852 if (virtualp)
10531 { 10853 {
10532 if (staticp == 2) 10854 if (staticp == 2)
10533 { 10855 {
10534 error ("member %qD cannot be declared both %<virtual%> " 10856 gcc_rich_location richloc (declspecs->locations[ds_virtual]);
10535 "and %<static%>", dname); 10857 richloc.add_range (declspecs->locations[ds_storage_class]);
10858 error_at (&richloc, "member %qD cannot be declared both %<virtual%> "
10859 "and %<static%>", dname);
10536 storage_class = sc_none; 10860 storage_class = sc_none;
10537 staticp = 0; 10861 staticp = 0;
10538 } 10862 }
10539 if (constexpr_p) 10863 if (constexpr_p && cxx_dialect < cxx2a)
10540 error ("member %qD cannot be declared both %<virtual%> " 10864 {
10541 "and %<constexpr%>", dname); 10865 gcc_rich_location richloc (declspecs->locations[ds_virtual]);
10866 richloc.add_range (declspecs->locations[ds_constexpr]);
10867 pedwarn (&richloc, OPT_Wpedantic, "member %qD can be declared both "
10868 "%<virtual%> and %<constexpr%> only in -std=c++2a or "
10869 "-std=gnu++2a", dname);
10870 }
10542 } 10871 }
10543 friendp = decl_spec_seq_has_spec_p (declspecs, ds_friend); 10872 friendp = decl_spec_seq_has_spec_p (declspecs, ds_friend);
10544 10873
10545 /* Issue errors about use of storage classes for parameters. */ 10874 /* Issue errors about use of storage classes for parameters. */
10546 if (decl_context == PARM) 10875 if (decl_context == PARM)
10547 { 10876 {
10548 if (typedef_p) 10877 if (typedef_p)
10549 { 10878 {
10550 error ("typedef declaration invalid in parameter declaration"); 10879 error_at (declspecs->locations[ds_typedef],
10880 "typedef declaration invalid in parameter declaration");
10551 return error_mark_node; 10881 return error_mark_node;
10552 } 10882 }
10553 else if (template_parm_flag && storage_class != sc_none) 10883 else if (template_parm_flag && storage_class != sc_none)
10554 { 10884 {
10555 error ("storage class specified for template parameter %qs", name); 10885 error_at (min_location (declspecs->locations[ds_thread],
10886 declspecs->locations[ds_storage_class]),
10887 "storage class specified for template parameter %qs",
10888 name);
10556 return error_mark_node; 10889 return error_mark_node;
10557 } 10890 }
10558 else if (storage_class == sc_static 10891 else if (storage_class == sc_static
10559 || storage_class == sc_extern 10892 || storage_class == sc_extern
10560 || thread_p) 10893 || thread_p)
10561 error ("storage class specifiers invalid in parameter declarations"); 10894 {
10895 error_at (min_location (declspecs->locations[ds_thread],
10896 declspecs->locations[ds_storage_class]),
10897 "storage class specified for parameter %qs", name);
10898 return error_mark_node;
10899 }
10562 10900
10563 /* Function parameters cannot be concept. */ 10901 /* Function parameters cannot be concept. */
10564 if (concept_p) 10902 if (concept_p)
10565 error ("a parameter cannot be declared %<concept%>"); 10903 error_at (declspecs->locations[ds_concept],
10904 "a parameter cannot be declared %<concept%>");
10566 /* Function parameters cannot be constexpr. If we saw one, moan 10905 /* Function parameters cannot be constexpr. If we saw one, moan
10567 and pretend it wasn't there. */ 10906 and pretend it wasn't there. */
10568 else if (constexpr_p) 10907 else if (constexpr_p)
10569 { 10908 {
10570 error ("a parameter cannot be declared %<constexpr%>"); 10909 error_at (declspecs->locations[ds_constexpr],
10910 "a parameter cannot be declared %<constexpr%>");
10571 constexpr_p = 0; 10911 constexpr_p = 0;
10572 } 10912 }
10573 } 10913 }
10574 10914
10575 /* Give error if `virtual' is used outside of class declaration. */ 10915 /* Give error if `virtual' is used outside of class declaration. */
10688 /* C++ also allows inlines and signed and unsigned elements, 11028 /* C++ also allows inlines and signed and unsigned elements,
10689 but in those cases we don't come in here. */ 11029 but in those cases we don't come in here. */
10690 ; 11030 ;
10691 else 11031 else
10692 { 11032 {
11033 location_t loc
11034 = min_location (declspecs->locations[ds_thread],
11035 declspecs->locations[ds_storage_class]);
10693 if (decl_context == FIELD) 11036 if (decl_context == FIELD)
10694 error ("storage class specified for %qs", name); 11037 error_at (loc, "storage class specified for %qs", name);
11038 else if (decl_context == PARM || decl_context == CATCHPARM)
11039 error_at (loc, "storage class specified for parameter %qs", name);
10695 else 11040 else
10696 { 11041 error_at (loc, "storage class specified for typename");
10697 if (decl_context == PARM || decl_context == CATCHPARM)
10698 error ("storage class specified for parameter %qs", name);
10699 else
10700 error ("storage class specified for typename");
10701 }
10702 if (storage_class == sc_register 11042 if (storage_class == sc_register
10703 || storage_class == sc_auto 11043 || storage_class == sc_auto
10704 || storage_class == sc_extern 11044 || storage_class == sc_extern
10705 || thread_p) 11045 || thread_p)
10706 storage_class = sc_none; 11046 storage_class = sc_none;
10717 else if (thread_p 11057 else if (thread_p
10718 && storage_class != sc_extern 11058 && storage_class != sc_extern
10719 && storage_class != sc_static) 11059 && storage_class != sc_static)
10720 { 11060 {
10721 if (declspecs->gnu_thread_keyword_p) 11061 if (declspecs->gnu_thread_keyword_p)
10722 pedwarn (input_location, 0, "function-scope %qs implicitly auto and " 11062 pedwarn (declspecs->locations[ds_thread],
11063 0, "function-scope %qs implicitly auto and "
10723 "declared %<__thread%>", name); 11064 "declared %<__thread%>", name);
10724 11065
10725 /* When thread_local is applied to a variable of block scope the 11066 /* When thread_local is applied to a variable of block scope the
10726 storage-class-specifier static is implied if it does not appear 11067 storage-class-specifier static is implied if it does not appear
10727 explicitly. */ 11068 explicitly. */
10729 staticp = 1; 11070 staticp = 1;
10730 } 11071 }
10731 11072
10732 if (storage_class && friendp) 11073 if (storage_class && friendp)
10733 { 11074 {
10734 error ("storage class specifiers invalid in friend function declarations"); 11075 error_at (min_location (declspecs->locations[ds_thread],
11076 declspecs->locations[ds_storage_class]),
11077 "storage class specifiers invalid in friend function "
11078 "declarations");
10735 storage_class = sc_none; 11079 storage_class = sc_none;
10736 staticp = 0; 11080 staticp = 0;
10737 } 11081 }
10738 11082
10739 if (!id_declarator) 11083 if (!id_declarator)
10758 } 11102 }
10759 } 11103 }
10760 11104
10761 if (declspecs->std_attributes) 11105 if (declspecs->std_attributes)
10762 { 11106 {
10763 /* Apply the c++11 attributes to the type preceding them. */ 11107 location_t attr_loc = declspecs->locations[ds_std_attribute];
10764 input_location = declspecs->locations[ds_std_attribute]; 11108 if (warning_at (attr_loc, OPT_Wattributes, "attribute ignored"))
10765 decl_attributes (&type, declspecs->std_attributes, 0); 11109 inform (attr_loc, "an attribute that appertains to a type-specifier "
10766 input_location = saved_loc; 11110 "is ignored");
10767 } 11111 }
10768 11112
10769 /* Determine the type of the entity declared by recurring on the 11113 /* Determine the type of the entity declared by recurring on the
10770 declarator. */ 11114 declarator. */
10771 for (; declarator; declarator = declarator->declarator) 11115 for (; declarator; declarator = declarator->declarator)
10791 returned_attrs = decl_attributes (&type, 11135 returned_attrs = decl_attributes (&type,
10792 chainon (returned_attrs, attrs), 11136 chainon (returned_attrs, attrs),
10793 attr_flags); 11137 attr_flags);
10794 } 11138 }
10795 11139
10796 /* We don't want to warn in parmeter context because we don't 11140 inner_declarator = declarator->declarator;
11141
11142 /* We don't want to warn in parameter context because we don't
10797 yet know if the parse will succeed, and this might turn out 11143 yet know if the parse will succeed, and this might turn out
10798 to be a constructor call. */ 11144 to be a constructor call. */
10799 if (decl_context != PARM 11145 if (decl_context != PARM
10800 && declarator->parenthesized != UNKNOWN_LOCATION) 11146 && decl_context != TYPENAME
11147 && !typedef_p
11148 && declarator->parenthesized != UNKNOWN_LOCATION
11149 /* If the type is class-like and the inner name used a
11150 global namespace qualifier, we need the parens.
11151 Unfortunately all we can tell is whether a qualified name
11152 was used or not. */
11153 && !(inner_declarator
11154 && inner_declarator->kind == cdk_id
11155 && inner_declarator->u.id.qualifying_scope
11156 && (MAYBE_CLASS_TYPE_P (type)
11157 || TREE_CODE (type) == ENUMERAL_TYPE)))
10801 warning_at (declarator->parenthesized, OPT_Wparentheses, 11158 warning_at (declarator->parenthesized, OPT_Wparentheses,
10802 "unnecessary parentheses in declaration of %qs", name); 11159 "unnecessary parentheses in declaration of %qs", name);
10803 if (declarator->kind == cdk_id || declarator->kind == cdk_decomp) 11160 if (declarator->kind == cdk_id || declarator->kind == cdk_decomp)
10804 break; 11161 break;
10805
10806 inner_declarator = declarator->declarator;
10807 11162
10808 switch (declarator->kind) 11163 switch (declarator->kind)
10809 { 11164 {
10810 case cdk_array: 11165 case cdk_array:
10811 type = create_array_type_for_decl (dname, type, 11166 type = create_array_type_for_decl (dname, type,
10856 closest to the identifier. */ 11211 closest to the identifier. */
10857 funcdecl_p = inner_declarator && inner_declarator->kind == cdk_id; 11212 funcdecl_p = inner_declarator && inner_declarator->kind == cdk_id;
10858 11213
10859 /* Handle a late-specified return type. */ 11214 /* Handle a late-specified return type. */
10860 tree late_return_type = declarator->u.function.late_return_type; 11215 tree late_return_type = declarator->u.function.late_return_type;
10861 if (funcdecl_p) 11216 if (funcdecl_p
11217 /* This is the case e.g. for
11218 using T = auto () -> int. */
11219 || inner_declarator == NULL)
10862 { 11220 {
10863 if (tree auto_node = type_uses_auto (type)) 11221 if (tree auto_node = type_uses_auto (type))
10864 { 11222 {
10865 if (!late_return_type) 11223 if (!late_return_type)
10866 { 11224 {
10888 error ("%qs function with trailing return type has" 11246 error ("%qs function with trailing return type has"
10889 " %qT as its type rather than plain %<auto%>", 11247 " %qT as its type rather than plain %<auto%>",
10890 name, type); 11248 name, type);
10891 return error_mark_node; 11249 return error_mark_node;
10892 } 11250 }
10893 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node)) 11251 else if (is_auto (type) && AUTO_IS_DECLTYPE (type))
10894 { 11252 {
10895 if (!late_return_type) 11253 if (funcdecl_p)
11254 error ("%qs function with trailing return type has "
11255 "%<decltype(auto)%> as its type rather than "
11256 "plain %<auto%>", name);
11257 else
11258 error ("invalid use of %<decltype(auto)%>");
11259 return error_mark_node;
11260 }
11261 tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node);
11262 if (!tmpl)
11263 if (tree late_auto = type_uses_auto (late_return_type))
11264 tmpl = CLASS_PLACEHOLDER_TEMPLATE (late_auto);
11265 if (tmpl)
11266 {
11267 if (!dguide_name_p (unqualified_id))
10896 { 11268 {
10897 if (dguide_name_p (unqualified_id)) 11269 error_at (declarator->id_loc, "deduced class "
10898 error_at (declarator->id_loc, "deduction guide " 11270 "type %qD in function return type",
10899 "for %qT must have trailing return " 11271 DECL_NAME (tmpl));
10900 "type", TREE_TYPE (tmpl));
10901 else
10902 error_at (declarator->id_loc, "deduced class "
10903 "type %qT in function return type",
10904 type);
10905 inform (DECL_SOURCE_LOCATION (tmpl), 11272 inform (DECL_SOURCE_LOCATION (tmpl),
10906 "%qD declared here", tmpl); 11273 "%qD declared here", tmpl);
11274 return error_mark_node;
11275 }
11276 else if (!late_return_type)
11277 {
11278 error_at (declarator->id_loc, "deduction guide "
11279 "for %qT must have trailing return "
11280 "type", TREE_TYPE (tmpl));
11281 inform (DECL_SOURCE_LOCATION (tmpl),
11282 "%qD declared here", tmpl);
11283 return error_mark_node;
10907 } 11284 }
10908 else if (CLASS_TYPE_P (late_return_type) 11285 else if (CLASS_TYPE_P (late_return_type)
10909 && CLASSTYPE_TEMPLATE_INFO (late_return_type) 11286 && CLASSTYPE_TEMPLATE_INFO (late_return_type)
10910 && (CLASSTYPE_TI_TEMPLATE (late_return_type) 11287 && (CLASSTYPE_TI_TEMPLATE (late_return_type)
10911 == tmpl)) 11288 == tmpl))
11019 /* It's a constructor. */ 11396 /* It's a constructor. */
11020 if (explicitp == 1) 11397 if (explicitp == 1)
11021 explicitp = 2; 11398 explicitp = 2;
11022 if (virtualp) 11399 if (virtualp)
11023 { 11400 {
11024 permerror (input_location, 11401 permerror (declspecs->locations[ds_virtual],
11025 "constructors cannot be declared %<virtual%>"); 11402 "constructors cannot be declared %<virtual%>");
11026 virtualp = 0; 11403 virtualp = 0;
11027 } 11404 }
11028 if (decl_context == FIELD 11405 if (decl_context == FIELD
11029 && sfk != sfk_constructor) 11406 && sfk != sfk_constructor)
11035 else if (friendp) 11412 else if (friendp)
11036 { 11413 {
11037 if (virtualp) 11414 if (virtualp)
11038 { 11415 {
11039 /* Cannot be both friend and virtual. */ 11416 /* Cannot be both friend and virtual. */
11040 error ("virtual functions cannot be friends"); 11417 gcc_rich_location richloc (declspecs->locations[ds_virtual]);
11418 richloc.add_range (declspecs->locations[ds_friend]);
11419 error_at (&richloc, "virtual functions cannot be friends");
11041 friendp = 0; 11420 friendp = 0;
11042 } 11421 }
11043 if (decl_context == NORMAL) 11422 if (decl_context == NORMAL)
11044 error ("friend declaration not in class definition"); 11423 error ("friend declaration not in class definition");
11045 if (current_function_decl && funcdef_flag) 11424 if (current_function_decl && funcdef_flag)
11046 error ("can%'t define friend function %qs in a local " 11425 {
11047 "class definition", 11426 error ("can%'t define friend function %qs in a local "
11048 name); 11427 "class definition", name);
11428 friendp = 0;
11429 }
11049 } 11430 }
11050 else if (ctype && sfk == sfk_conversion) 11431 else if (ctype && sfk == sfk_conversion)
11051 { 11432 {
11052 if (explicitp == 1) 11433 if (explicitp == 1)
11053 { 11434 {
11061 { 11442 {
11062 if (explicitp == 1) 11443 if (explicitp == 1)
11063 explicitp = 2; 11444 explicitp = 2;
11064 } 11445 }
11065 11446
11066 arg_types = grokparms (declarator->u.function.parameters, 11447 tree pushed_scope = NULL_TREE;
11067 &parms); 11448 if (funcdecl_p
11449 && decl_context != FIELD
11450 && inner_declarator->u.id.qualifying_scope
11451 && CLASS_TYPE_P (inner_declarator->u.id.qualifying_scope))
11452 pushed_scope
11453 = push_scope (inner_declarator->u.id.qualifying_scope);
11454
11455 arg_types = grokparms (declarator->u.function.parameters, &parms);
11456
11457 if (pushed_scope)
11458 pop_scope (pushed_scope);
11068 11459
11069 if (inner_declarator 11460 if (inner_declarator
11070 && inner_declarator->kind == cdk_id 11461 && inner_declarator->kind == cdk_id
11071 && inner_declarator->u.id.sfk == sfk_destructor 11462 && inner_declarator->u.id.sfk == sfk_destructor
11072 && arg_types != void_list_node) 11463 && arg_types != void_list_node)
11105 case cdk_reference: 11496 case cdk_reference:
11106 case cdk_ptrmem: 11497 case cdk_ptrmem:
11107 /* Filter out pointers-to-references and references-to-references. 11498 /* Filter out pointers-to-references and references-to-references.
11108 We can get these if a TYPE_DECL is used. */ 11499 We can get these if a TYPE_DECL is used. */
11109 11500
11110 if (TREE_CODE (type) == REFERENCE_TYPE) 11501 if (TYPE_REF_P (type))
11111 { 11502 {
11112 if (declarator->kind != cdk_reference) 11503 if (declarator->kind != cdk_reference)
11113 { 11504 {
11114 error ("cannot declare pointer to %q#T", type); 11505 error ("cannot declare pointer to %q#T", type);
11115 type = TREE_TYPE (type); 11506 type = TREE_TYPE (type);
11211 to create the type "rvalue reference to cv TD' creates the 11602 to create the type "rvalue reference to cv TD' creates the
11212 type TD." 11603 type TD."
11213 */ 11604 */
11214 if (VOID_TYPE_P (type)) 11605 if (VOID_TYPE_P (type))
11215 /* We already gave an error. */; 11606 /* We already gave an error. */;
11216 else if (TREE_CODE (type) == REFERENCE_TYPE) 11607 else if (TYPE_REF_P (type))
11217 { 11608 {
11218 if (declarator->u.reference.rvalue_ref) 11609 if (declarator->u.reference.rvalue_ref)
11219 /* Leave type alone. */; 11610 /* Leave type alone. */;
11220 else 11611 else
11221 type = cp_build_reference_type (TREE_TYPE (type), false); 11612 type = cp_build_reference_type (TREE_TYPE (type), false);
11292 if (constexpr_p && innermost_code != cdk_function) 11683 if (constexpr_p && innermost_code != cdk_function)
11293 { 11684 {
11294 /* DR1688 says that a `constexpr' specifier in combination with 11685 /* DR1688 says that a `constexpr' specifier in combination with
11295 `volatile' is valid. */ 11686 `volatile' is valid. */
11296 11687
11297 if (TREE_CODE (type) != REFERENCE_TYPE) 11688 if (!TYPE_REF_P (type))
11298 { 11689 {
11299 type_quals |= TYPE_QUAL_CONST; 11690 type_quals |= TYPE_QUAL_CONST;
11300 type = cp_build_qualified_type (type, type_quals); 11691 type = cp_build_qualified_type (type, type_quals);
11301 } 11692 }
11302 } 11693 }
11398 11789
11399 if (declarator 11790 if (declarator
11400 && declarator->kind == cdk_id 11791 && declarator->kind == cdk_id
11401 && declarator->std_attributes 11792 && declarator->std_attributes
11402 && attrlist != NULL) 11793 && attrlist != NULL)
11403 /* [dcl.meaning]/1: The optional attribute-specifier-seq following 11794 {
11404 a declarator-id appertains to the entity that is declared. */ 11795 /* [dcl.meaning]/1: The optional attribute-specifier-seq following
11405 *attrlist = chainon (*attrlist, declarator->std_attributes); 11796 a declarator-id appertains to the entity that is declared. */
11797 if (declarator->std_attributes != error_mark_node)
11798 *attrlist = chainon (*attrlist, declarator->std_attributes);
11799 else
11800 /* We should have already diagnosed the issue (c++/78344). */
11801 gcc_assert (seen_error ());
11802 }
11406 11803
11407 /* Handle parameter packs. */ 11804 /* Handle parameter packs. */
11408 if (parameter_pack_p) 11805 if (parameter_pack_p)
11409 { 11806 {
11410 if (decl_context == PARM) 11807 if (decl_context == PARM)
11469 else if (type_quals & TYPE_QUAL_CONST) 11866 else if (type_quals & TYPE_QUAL_CONST)
11470 { 11867 {
11471 error ("const %qs cannot be declared %<mutable%>", name); 11868 error ("const %qs cannot be declared %<mutable%>", name);
11472 storage_class = sc_none; 11869 storage_class = sc_none;
11473 } 11870 }
11474 else if (TREE_CODE (type) == REFERENCE_TYPE) 11871 else if (TYPE_REF_P (type))
11475 { 11872 {
11476 permerror (input_location, "reference %qs cannot be declared " 11873 permerror (input_location, "reference %qs cannot be declared "
11477 "%<mutable%>", name); 11874 "%<mutable%>", name);
11478 storage_class = sc_none; 11875 storage_class = sc_none;
11479 } 11876 }
11480 } 11877 }
11481 11878
11482 /* If this is declaring a typedef name, return a TYPE_DECL. */ 11879 /* If this is declaring a typedef name, return a TYPE_DECL. */
11483 if (typedef_p && decl_context != TYPENAME) 11880 if (typedef_p && decl_context != TYPENAME)
11484 { 11881 {
11882 bool alias_p = decl_spec_seq_has_spec_p (declspecs, ds_alias);
11485 tree decl; 11883 tree decl;
11486 11884
11487 /* This declaration: 11885 /* This declaration:
11488 11886
11489 typedef void f(int) const; 11887 typedef void f(int) const;
11502 rqual = REF_QUAL_NONE; 11900 rqual = REF_QUAL_NONE;
11503 } 11901 }
11504 11902
11505 if (type_uses_auto (type)) 11903 if (type_uses_auto (type))
11506 { 11904 {
11507 error ("typedef declared %<auto%>"); 11905 if (alias_p)
11906 error_at (declspecs->locations[ds_type_spec],
11907 "%<auto%> not allowed in alias declaration");
11908 else
11909 error_at (declspecs->locations[ds_type_spec],
11910 "typedef declared %<auto%>");
11508 type = error_mark_node; 11911 type = error_mark_node;
11509 } 11912 }
11510 11913
11511 if (reqs) 11914 if (reqs)
11512 error_at (location_of (reqs), "requires-clause on typedef"); 11915 error_at (location_of (reqs), "requires-clause on typedef");
11916
11917 if (id_declarator && declarator->u.id.qualifying_scope)
11918 {
11919 error ("typedef name may not be a nested-name-specifier");
11920 type = error_mark_node;
11921 }
11513 11922
11514 if (decl_context == FIELD) 11923 if (decl_context == FIELD)
11515 decl = build_lang_decl (TYPE_DECL, unqualified_id, type); 11924 decl = build_lang_decl (TYPE_DECL, unqualified_id, type);
11516 else 11925 else
11517 decl = build_decl (input_location, TYPE_DECL, unqualified_id, type); 11926 decl = build_decl (input_location, TYPE_DECL, unqualified_id, type);
11518 if (id_declarator && declarator->u.id.qualifying_scope) {
11519 error_at (DECL_SOURCE_LOCATION (decl),
11520 "typedef name may not be a nested-name-specifier");
11521 TREE_TYPE (decl) = error_mark_node;
11522 }
11523 11927
11524 if (decl_context != FIELD) 11928 if (decl_context != FIELD)
11525 { 11929 {
11526 if (!current_function_decl) 11930 if (!current_function_decl)
11527 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace); 11931 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
11528 else if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (current_function_decl) 11932 else if (DECL_MAYBE_IN_CHARGE_CDTOR_P (current_function_decl))
11529 || (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P
11530 (current_function_decl)))
11531 /* The TYPE_DECL is "abstract" because there will be 11933 /* The TYPE_DECL is "abstract" because there will be
11532 clones of this constructor/destructor, and there will 11934 clones of this constructor/destructor, and there will
11533 be copies of this TYPE_DECL generated in those 11935 be copies of this TYPE_DECL generated in those
11534 clones. The decloning optimization (for space) may 11936 clones. The decloning optimization (for space) may
11535 revert this subsequently if it determines that 11937 revert this subsequently if it determines that
11560 || (typedef_decl && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl))) 11962 || (typedef_decl && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
11561 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1; 11963 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
11562 11964
11563 bad_specifiers (decl, BSP_TYPE, virtualp, 11965 bad_specifiers (decl, BSP_TYPE, virtualp,
11564 memfn_quals != TYPE_UNQUALIFIED, 11966 memfn_quals != TYPE_UNQUALIFIED,
11565 inlinep, friendp, raises != NULL_TREE); 11967 inlinep, friendp, raises != NULL_TREE,
11566 11968 declspecs->locations);
11567 if (decl_spec_seq_has_spec_p (declspecs, ds_alias)) 11969
11970 if (alias_p)
11568 /* Acknowledge that this was written: 11971 /* Acknowledge that this was written:
11569 `using analias = atype;'. */ 11972 `using analias = atype;'. */
11570 TYPE_DECL_ALIAS_P (decl) = 1; 11973 TYPE_DECL_ALIAS_P (decl) = 1;
11571 11974
11572 return decl; 11975 return decl;
11766 decl = cp_build_parm_decl (NULL_TREE, unqualified_id, type); 12169 decl = cp_build_parm_decl (NULL_TREE, unqualified_id, type);
11767 DECL_ARRAY_PARAMETER_P (decl) = array_parameter_p; 12170 DECL_ARRAY_PARAMETER_P (decl) = array_parameter_p;
11768 12171
11769 bad_specifiers (decl, BSP_PARM, virtualp, 12172 bad_specifiers (decl, BSP_PARM, virtualp,
11770 memfn_quals != TYPE_UNQUALIFIED, 12173 memfn_quals != TYPE_UNQUALIFIED,
11771 inlinep, friendp, raises != NULL_TREE); 12174 inlinep, friendp, raises != NULL_TREE,
12175 declspecs->locations);
11772 } 12176 }
11773 else if (decl_context == FIELD) 12177 else if (decl_context == FIELD)
11774 { 12178 {
11775 if (!staticp && !friendp && TREE_CODE (type) != METHOD_TYPE) 12179 if (!staticp && !friendp && TREE_CODE (type) != METHOD_TYPE)
11776 if (tree auto_node = type_uses_auto (type)) 12180 if (tree auto_node = type_uses_auto (type))
11853 12257
11854 /* ``A union may [ ... ] not [ have ] virtual functions.'' 12258 /* ``A union may [ ... ] not [ have ] virtual functions.''
11855 ARM 9.5 */ 12259 ARM 9.5 */
11856 if (virtualp && TREE_CODE (ctype) == UNION_TYPE) 12260 if (virtualp && TREE_CODE (ctype) == UNION_TYPE)
11857 { 12261 {
11858 error ("function %qD declared %<virtual%> inside a union", 12262 error_at (declspecs->locations[ds_virtual],
11859 unqualified_id); 12263 "function %qD declared %<virtual%> inside a union",
12264 unqualified_id);
11860 return error_mark_node; 12265 return error_mark_node;
11861 } 12266 }
11862 12267
11863 if (virtualp 12268 if (virtualp
11864 && identifier_p (unqualified_id) 12269 && identifier_p (unqualified_id)
11865 && IDENTIFIER_NEWDEL_OP_P (unqualified_id)) 12270 && IDENTIFIER_NEWDEL_OP_P (unqualified_id))
11866 { 12271 {
11867 error ("%qD cannot be declared %<virtual%>, since it " 12272 error_at (declspecs->locations[ds_virtual],
11868 "is always static", unqualified_id); 12273 "%qD cannot be declared %<virtual%>, since it "
12274 "is always static", unqualified_id);
11869 virtualp = 0; 12275 virtualp = 0;
11870 } 12276 }
11871 } 12277 }
11872 12278
11873 /* Check that the name used for a destructor makes sense. */ 12279 /* Check that the name used for a destructor makes sense. */
11889 uqname, ctype); 12295 uqname, ctype);
11890 return error_mark_node; 12296 return error_mark_node;
11891 } 12297 }
11892 if (concept_p) 12298 if (concept_p)
11893 { 12299 {
11894 error ("a destructor cannot be %<concept%>"); 12300 error_at (declspecs->locations[ds_concept],
12301 "a destructor cannot be %<concept%>");
11895 return error_mark_node; 12302 return error_mark_node;
11896 } 12303 }
11897 if (constexpr_p) 12304 if (constexpr_p)
11898 { 12305 {
11899 error ("a destructor cannot be %<constexpr%>"); 12306 error_at (declspecs->locations[ds_constexpr],
12307 "a destructor cannot be %<constexpr%>");
11900 return error_mark_node; 12308 return error_mark_node;
11901 } 12309 }
11902 } 12310 }
11903 else if (sfk == sfk_constructor && friendp && !ctype) 12311 else if (sfk == sfk_constructor && friendp && !ctype)
11904 { 12312 {
11908 return error_mark_node; 12316 return error_mark_node;
11909 } 12317 }
11910 if (sfk == sfk_constructor) 12318 if (sfk == sfk_constructor)
11911 if (concept_p) 12319 if (concept_p)
11912 { 12320 {
11913 error ("a constructor cannot be %<concept%>"); 12321 error_at (declspecs->locations[ds_concept],
12322 "a constructor cannot be %<concept%>");
11914 return error_mark_node; 12323 return error_mark_node;
11915 } 12324 }
11916 if (concept_p) 12325 if (concept_p)
11917 { 12326 {
11918 error ("a concept cannot be a member function"); 12327 error_at (declspecs->locations[ds_concept],
12328 "a concept cannot be a member function");
11919 concept_p = false; 12329 concept_p = false;
11920 } 12330 }
11921 12331
11922 if (TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR) 12332 if (TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR)
11923 { 12333 {
11936 function_context = (ctype != NULL_TREE) ? 12346 function_context = (ctype != NULL_TREE) ?
11937 decl_function_context (TYPE_MAIN_DECL (ctype)) : NULL_TREE; 12347 decl_function_context (TYPE_MAIN_DECL (ctype)) : NULL_TREE;
11938 publicp = (! friendp || ! staticp) 12348 publicp = (! friendp || ! staticp)
11939 && function_context == NULL_TREE; 12349 && function_context == NULL_TREE;
11940 12350
11941 if (late_return_type_p)
11942 TYPE_HAS_LATE_RETURN_TYPE (type) = 1;
11943
11944 decl = grokfndecl (ctype, type, 12351 decl = grokfndecl (ctype, type,
11945 TREE_CODE (unqualified_id) != TEMPLATE_ID_EXPR 12352 TREE_CODE (unqualified_id) != TEMPLATE_ID_EXPR
11946 ? unqualified_id : dname, 12353 ? unqualified_id : dname,
11947 parms, 12354 parms,
11948 unqualified_id, 12355 unqualified_id,
12356 declspecs,
11949 reqs, 12357 reqs,
11950 virtualp, flags, memfn_quals, rqual, raises, 12358 virtualp, flags, memfn_quals, rqual, raises,
11951 friendp ? -1 : 0, friendp, publicp, 12359 friendp ? -1 : 0, friendp, publicp,
11952 inlinep | (2 * constexpr_p) | (4 * concept_p), 12360 inlinep | (2 * constexpr_p) | (4 * concept_p),
11953 initialized == SD_DELETED, sfk, 12361 initialized == SD_DELETED, sfk,
11954 funcdef_flag, template_count, in_namespace, 12362 funcdef_flag, late_return_type_p,
12363 template_count, in_namespace,
11955 attrlist, declarator->id_loc); 12364 attrlist, declarator->id_loc);
11956 decl = set_virt_specifiers (decl, virt_specifiers); 12365 decl = set_virt_specifiers (decl, virt_specifiers);
11957 if (decl == NULL_TREE) 12366 if (decl == NULL_TREE)
11958 return error_mark_node; 12367 return error_mark_node;
11959 #if 0 12368 #if 0
11997 { 12406 {
11998 if (friendp) 12407 if (friendp)
11999 { 12408 {
12000 error ("%qE is neither function nor member function; " 12409 error ("%qE is neither function nor member function; "
12001 "cannot be declared friend", unqualified_id); 12410 "cannot be declared friend", unqualified_id);
12002 friendp = 0; 12411 return error_mark_node;
12003 } 12412 }
12004 decl = NULL_TREE; 12413 decl = NULL_TREE;
12005 } 12414 }
12006 12415
12007 if (friendp) 12416 if (friendp)
12041 ? declarator->id_loc 12450 ? declarator->id_loc
12042 : input_location, 12451 : input_location,
12043 VAR_DECL, unqualified_id, type); 12452 VAR_DECL, unqualified_id, type);
12044 set_linkage_for_static_data_member (decl); 12453 set_linkage_for_static_data_member (decl);
12045 if (concept_p) 12454 if (concept_p)
12046 error ("static data member %qE declared %<concept%>", 12455 error_at (declspecs->locations[ds_concept],
12047 unqualified_id); 12456 "static data member %qE declared %<concept%>",
12457 unqualified_id);
12048 else if (constexpr_p && !initialized) 12458 else if (constexpr_p && !initialized)
12049 { 12459 {
12050 error ("constexpr static data member %qD must have an " 12460 error ("%<constexpr%> static data member %qD must have an "
12051 "initializer", decl); 12461 "initializer", decl);
12052 constexpr_p = false; 12462 constexpr_p = false;
12053 } 12463 }
12054 12464
12055 if (inlinep) 12465 if (inlinep)
12056 mark_inline_variable (decl); 12466 mark_inline_variable (decl, declspecs->locations[ds_inline]);
12057 12467
12058 if (!DECL_VAR_DECLARED_INLINE_P (decl) 12468 if (!DECL_VAR_DECLARED_INLINE_P (decl)
12059 && !(cxx_dialect >= cxx17 && constexpr_p)) 12469 && !(cxx_dialect >= cxx17 && constexpr_p))
12060 /* Even if there is an in-class initialization, DECL 12470 /* Even if there is an in-class initialization, DECL
12061 is considered undefined until an out-of-class 12471 is considered undefined until an out-of-class
12073 } 12483 }
12074 } 12484 }
12075 else 12485 else
12076 { 12486 {
12077 if (concept_p) 12487 if (concept_p)
12078 error ("non-static data member %qE declared %<concept%>", 12488 error_at (declspecs->locations[ds_concept],
12079 unqualified_id); 12489 "non-static data member %qE declared %<concept%>",
12490 unqualified_id);
12080 else if (constexpr_p) 12491 else if (constexpr_p)
12081 { 12492 {
12082 error ("non-static data member %qE declared %<constexpr%>", 12493 error_at (declspecs->locations[ds_constexpr],
12083 unqualified_id); 12494 "non-static data member %qE declared %<constexpr%>",
12495 unqualified_id);
12084 constexpr_p = false; 12496 constexpr_p = false;
12085 } 12497 }
12086 decl = build_decl (input_location, 12498 decl = build_decl (input_location,
12087 FIELD_DECL, unqualified_id, type); 12499 FIELD_DECL, unqualified_id, type);
12088 DECL_NONADDRESSABLE_P (decl) = bitfield; 12500 DECL_NONADDRESSABLE_P (decl) = bitfield;
12089 if (bitfield && !unqualified_id) 12501 if (bitfield && !unqualified_id)
12090 TREE_NO_WARNING (decl) = 1; 12502 {
12503 TREE_NO_WARNING (decl) = 1;
12504 DECL_PADDING_P (decl) = 1;
12505 }
12091 12506
12092 if (storage_class == sc_mutable) 12507 if (storage_class == sc_mutable)
12093 { 12508 {
12094 DECL_MUTABLE_P (decl) = 1; 12509 DECL_MUTABLE_P (decl) = 1;
12095 storage_class = sc_none; 12510 storage_class = sc_none;
12110 } 12525 }
12111 12526
12112 bad_specifiers (decl, BSP_FIELD, virtualp, 12527 bad_specifiers (decl, BSP_FIELD, virtualp,
12113 memfn_quals != TYPE_UNQUALIFIED, 12528 memfn_quals != TYPE_UNQUALIFIED,
12114 staticp ? false : inlinep, friendp, 12529 staticp ? false : inlinep, friendp,
12115 raises != NULL_TREE); 12530 raises != NULL_TREE,
12531 declspecs->locations);
12116 } 12532 }
12117 } 12533 }
12118 else if (TREE_CODE (type) == FUNCTION_TYPE 12534 else if (TREE_CODE (type) == FUNCTION_TYPE
12119 || TREE_CODE (type) == METHOD_TYPE) 12535 || TREE_CODE (type) == METHOD_TYPE)
12120 { 12536 {
12135 else if (storage_class == sc_register) 12551 else if (storage_class == sc_register)
12136 error ("storage class %<register%> invalid for function %qs", name); 12552 error ("storage class %<register%> invalid for function %qs", name);
12137 else if (thread_p) 12553 else if (thread_p)
12138 { 12554 {
12139 if (declspecs->gnu_thread_keyword_p) 12555 if (declspecs->gnu_thread_keyword_p)
12140 error ("storage class %<__thread%> invalid for function %qs", 12556 error_at (declspecs->locations[ds_thread],
12141 name); 12557 "storage class %<__thread%> invalid for function %qs",
12558 name);
12142 else 12559 else
12143 error ("storage class %<thread_local%> invalid for function %qs", 12560 error_at (declspecs->locations[ds_thread],
12144 name); 12561 "storage class %<thread_local%> invalid for "
12562 "function %qs", name);
12145 } 12563 }
12146 12564
12147 if (virt_specifiers) 12565 if (virt_specifiers)
12148 error ("virt-specifiers in %qs not allowed outside a class definition", name); 12566 error ("virt-specifiers in %qs not allowed outside a class "
12567 "definition", name);
12149 /* Function declaration not at top level. 12568 /* Function declaration not at top level.
12150 Storage classes other than `extern' are not allowed 12569 Storage classes other than `extern' are not allowed
12151 and `extern' makes no difference. */ 12570 and `extern' makes no difference. */
12152 if (! toplevel_bindings_p () 12571 if (! toplevel_bindings_p ()
12153 && (storage_class == sc_static 12572 && (storage_class == sc_static
12154 || decl_spec_seq_has_spec_p (declspecs, ds_inline)) 12573 || decl_spec_seq_has_spec_p (declspecs, ds_inline))
12155 && pedantic) 12574 && pedantic)
12156 { 12575 {
12157 if (storage_class == sc_static) 12576 if (storage_class == sc_static)
12158 pedwarn (input_location, OPT_Wpedantic, 12577 pedwarn (declspecs->locations[ds_storage_class], OPT_Wpedantic,
12159 "%<static%> specifier invalid for function %qs " 12578 "%<static%> specifier invalid for function %qs "
12160 "declared out of global scope", name); 12579 "declared out of global scope", name);
12161 else 12580 else
12162 pedwarn (input_location, OPT_Wpedantic, 12581 pedwarn (declspecs->locations[ds_inline], OPT_Wpedantic,
12163 "%<inline%> specifier invalid for function %qs " 12582 "%<inline%> specifier invalid for function %qs "
12164 "declared out of global scope", name); 12583 "declared out of global scope", name);
12165 } 12584 }
12166 12585
12167 if (ctype == NULL_TREE) 12586 if (ctype == NULL_TREE)
12183 12602
12184 /* Record whether the function is public. */ 12603 /* Record whether the function is public. */
12185 publicp = (ctype != NULL_TREE 12604 publicp = (ctype != NULL_TREE
12186 || storage_class != sc_static); 12605 || storage_class != sc_static);
12187 12606
12188 if (late_return_type_p)
12189 TYPE_HAS_LATE_RETURN_TYPE (type) = 1;
12190
12191 decl = grokfndecl (ctype, type, original_name, parms, unqualified_id, 12607 decl = grokfndecl (ctype, type, original_name, parms, unqualified_id,
12608 declspecs,
12192 reqs, virtualp, flags, memfn_quals, rqual, raises, 12609 reqs, virtualp, flags, memfn_quals, rqual, raises,
12193 1, friendp, 12610 1, friendp,
12194 publicp, 12611 publicp,
12195 inlinep | (2 * constexpr_p) | (4 * concept_p), 12612 inlinep | (2 * constexpr_p) | (4 * concept_p),
12196 initialized == SD_DELETED, 12613 initialized == SD_DELETED,
12197 sfk, 12614 sfk,
12198 funcdef_flag, 12615 funcdef_flag,
12616 late_return_type_p,
12199 template_count, in_namespace, attrlist, 12617 template_count, in_namespace, attrlist,
12200 declarator->id_loc); 12618 declarator->id_loc);
12201 if (decl == NULL_TREE) 12619 if (decl == NULL_TREE)
12202 return error_mark_node; 12620 return error_mark_node;
12203 12621
12217 } 12635 }
12218 else if (current_function_decl) 12636 else if (current_function_decl)
12219 { 12637 {
12220 /* 7.1.1: There can be no static function declarations within a 12638 /* 7.1.1: There can be no static function declarations within a
12221 block. */ 12639 block. */
12222 error ("cannot declare static function inside another function"); 12640 error_at (declspecs->locations[ds_storage_class],
12641 "cannot declare static function inside another function");
12223 invalid_static = 1; 12642 invalid_static = 1;
12224 } 12643 }
12225 12644
12226 if (invalid_static) 12645 if (invalid_static)
12227 { 12646 {
12246 if (decl == NULL_TREE) 12665 if (decl == NULL_TREE)
12247 return error_mark_node; 12666 return error_mark_node;
12248 12667
12249 bad_specifiers (decl, BSP_VAR, virtualp, 12668 bad_specifiers (decl, BSP_VAR, virtualp,
12250 memfn_quals != TYPE_UNQUALIFIED, 12669 memfn_quals != TYPE_UNQUALIFIED,
12251 inlinep, friendp, raises != NULL_TREE); 12670 inlinep, friendp, raises != NULL_TREE,
12671 declspecs->locations);
12252 12672
12253 if (ctype) 12673 if (ctype)
12254 { 12674 {
12255 DECL_CONTEXT (decl) = ctype; 12675 DECL_CONTEXT (decl) = ctype;
12256 if (staticp == 1) 12676 if (staticp == 1)
12273 storage_class = sc_none; 12693 storage_class = sc_none;
12274 } 12694 }
12275 } 12695 }
12276 else if (constexpr_p && DECL_EXTERNAL (decl)) 12696 else if (constexpr_p && DECL_EXTERNAL (decl))
12277 { 12697 {
12278 error ("declaration of constexpr variable %qD is not a definition", 12698 error ("declaration of %<constexpr%> variable %qD "
12279 decl); 12699 "is not a definition", decl);
12280 constexpr_p = false; 12700 constexpr_p = false;
12281 } 12701 }
12282 12702
12283 if (inlinep) 12703 if (inlinep)
12284 mark_inline_variable (decl); 12704 mark_inline_variable (decl, declspecs->locations[ds_inline]);
12285 if (innermost_code == cdk_decomp) 12705 if (innermost_code == cdk_decomp)
12286 { 12706 {
12287 gcc_assert (declarator && declarator->kind == cdk_decomp); 12707 gcc_assert (declarator && declarator->kind == cdk_decomp);
12288 DECL_SOURCE_LOCATION (decl) = declarator->id_loc; 12708 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
12289 DECL_ARTIFICIAL (decl) = 1; 12709 DECL_ARTIFICIAL (decl) = 1;
12449 /* [dcl.fct.default] 12869 /* [dcl.fct.default]
12450 12870
12451 A default argument expression is implicitly converted to the 12871 A default argument expression is implicitly converted to the
12452 parameter type. */ 12872 parameter type. */
12453 ++cp_unevaluated_operand; 12873 ++cp_unevaluated_operand;
12454 perform_implicit_conversion_flags (decl_type, arg, complain, 12874 /* Avoid digest_init clobbering the initializer. */
12875 tree carg = BRACE_ENCLOSED_INITIALIZER_P (arg) ? unshare_expr (arg): arg;
12876 perform_implicit_conversion_flags (decl_type, carg, complain,
12455 LOOKUP_IMPLICIT); 12877 LOOKUP_IMPLICIT);
12456 --cp_unevaluated_operand; 12878 --cp_unevaluated_operand;
12457 12879
12458 /* Avoid redundant -Wzero-as-null-pointer-constant warnings at 12880 /* Avoid redundant -Wzero-as-null-pointer-constant warnings at
12459 the call sites. */ 12881 the call sites. */
12578 { 13000 {
12579 if (deprecated_state != DEPRECATED_SUPPRESS) 13001 if (deprecated_state != DEPRECATED_SUPPRESS)
12580 { 13002 {
12581 tree deptype = type_is_deprecated (type); 13003 tree deptype = type_is_deprecated (type);
12582 if (deptype) 13004 if (deptype)
12583 warn_deprecated_use (deptype, NULL_TREE); 13005 cp_warn_deprecated_use (deptype);
12584 } 13006 }
12585 13007
12586 /* Top-level qualifiers on the parameters are 13008 /* Top-level qualifiers on the parameters are
12587 ignored for function types. */ 13009 ignored for function types. */
12588 type = cp_build_qualified_type (type, 0); 13010 type = cp_build_qualified_type (type, 0);
12592 type = build_pointer_type (type); 13014 type = build_pointer_type (type);
12593 TREE_TYPE (decl) = type; 13015 TREE_TYPE (decl) = type;
12594 } 13016 }
12595 else if (abstract_virtuals_error (decl, type)) 13017 else if (abstract_virtuals_error (decl, type))
12596 any_error = 1; /* Seems like a good idea. */ 13018 any_error = 1; /* Seems like a good idea. */
12597 else if (cxx_dialect < cxx17 && POINTER_TYPE_P (type)) 13019 else if (cxx_dialect < cxx17 && INDIRECT_TYPE_P (type))
12598 { 13020 {
12599 /* Before C++17 DR 393: 13021 /* Before C++17 DR 393:
12600 [dcl.fct]/6, parameter types cannot contain pointers 13022 [dcl.fct]/6, parameter types cannot contain pointers
12601 (references) to arrays of unknown bound. */ 13023 (references) to arrays of unknown bound. */
12602 tree t = TREE_TYPE (type); 13024 tree t = TREE_TYPE (type);
12689 if (TYPE_MAIN_VARIANT (arg_type) == DECL_CONTEXT (d)) 13111 if (TYPE_MAIN_VARIANT (arg_type) == DECL_CONTEXT (d))
12690 { 13112 {
12691 /* Pass by value copy assignment operator. */ 13113 /* Pass by value copy assignment operator. */
12692 result = -1; 13114 result = -1;
12693 } 13115 }
12694 else if (TREE_CODE (arg_type) == REFERENCE_TYPE 13116 else if (TYPE_REF_P (arg_type)
12695 && !TYPE_REF_IS_RVALUE (arg_type) 13117 && !TYPE_REF_IS_RVALUE (arg_type)
12696 && TYPE_MAIN_VARIANT (TREE_TYPE (arg_type)) == DECL_CONTEXT (d)) 13118 && TYPE_MAIN_VARIANT (TREE_TYPE (arg_type)) == DECL_CONTEXT (d))
12697 { 13119 {
12698 if (CP_TYPE_CONST_P (TREE_TYPE (arg_type))) 13120 if (CP_TYPE_CONST_P (TREE_TYPE (arg_type)))
12699 result = 2; 13121 result = 2;
12757 13179
12758 arg_type = TREE_VALUE (args); 13180 arg_type = TREE_VALUE (args);
12759 if (arg_type == error_mark_node) 13181 if (arg_type == error_mark_node)
12760 return 0; 13182 return 0;
12761 13183
12762 if (TREE_CODE (arg_type) == REFERENCE_TYPE 13184 if (TYPE_REF_P (arg_type)
12763 && TYPE_REF_IS_RVALUE (arg_type) 13185 && TYPE_REF_IS_RVALUE (arg_type)
12764 && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (arg_type)), 13186 && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (arg_type)),
12765 DECL_CONTEXT (d))) 13187 DECL_CONTEXT (d)))
12766 result = true; 13188 result = true;
12767 13189
12816 13238
12817 if (DECL_DECLARED_CONSTEXPR_P (decl) 13239 if (DECL_DECLARED_CONSTEXPR_P (decl)
12818 && !ctor && !move_fn_p (decl)) 13240 && !ctor && !move_fn_p (decl))
12819 TYPE_HAS_CONSTEXPR_CTOR (class_type) = 1; 13241 TYPE_HAS_CONSTEXPR_CTOR (class_type) = 1;
12820 } 13242 }
12821 else if (DECL_NAME (decl) == cp_assignment_operator_id (NOP_EXPR)) 13243 else if (DECL_NAME (decl) == assign_op_identifier)
12822 { 13244 {
12823 /* [class.copy] 13245 /* [class.copy]
12824 13246
12825 A non-template assignment operator for class X is a copy 13247 A non-template assignment operator for class X is a copy
12826 assignment operator if its parameter is of type X, X&, const 13248 assignment operator if its parameter is of type X, X&, const
12876 } 13298 }
12877 13299
12878 return true; 13300 return true;
12879 } 13301 }
12880 13302
12881 /* An operator with this code is unary, but can also be binary. */ 13303 /* DECL is a declaration for an overloaded or conversion operator. If
12882 13304 COMPLAIN is true, errors are issued for invalid declarations. */
12883 static bool
12884 ambi_op_p (enum tree_code code)
12885 {
12886 return (code == INDIRECT_REF
12887 || code == ADDR_EXPR
12888 || code == UNARY_PLUS_EXPR
12889 || code == NEGATE_EXPR
12890 || code == PREINCREMENT_EXPR
12891 || code == PREDECREMENT_EXPR);
12892 }
12893
12894 /* An operator with this name can only be unary. */
12895
12896 static bool
12897 unary_op_p (enum tree_code code)
12898 {
12899 return (code == TRUTH_NOT_EXPR
12900 || code == BIT_NOT_EXPR
12901 || code == COMPONENT_REF
12902 || code == TYPE_EXPR);
12903 }
12904
12905 /* DECL is a declaration for an overloaded operator. If COMPLAIN is true,
12906 errors are issued for invalid declarations. */
12907 13305
12908 bool 13306 bool
12909 grok_op_properties (tree decl, bool complain) 13307 grok_op_properties (tree decl, bool complain)
12910 { 13308 {
12911 tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl)); 13309 tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
12912 tree argtype; 13310 bool methodp = TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE;
12913 int methodp = (TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE);
12914 tree name = DECL_NAME (decl); 13311 tree name = DECL_NAME (decl);
12915 enum tree_code operator_code; 13312 location_t loc = DECL_SOURCE_LOCATION (decl);
12916 int arity; 13313
12917 bool ellipsis_p; 13314 tree class_type = DECL_CONTEXT (decl);
12918 tree class_type;
12919
12920 /* Count the number of arguments and check for ellipsis. */
12921 for (argtype = argtypes, arity = 0;
12922 argtype && argtype != void_list_node;
12923 argtype = TREE_CHAIN (argtype))
12924 ++arity;
12925 ellipsis_p = !argtype;
12926
12927 class_type = DECL_CONTEXT (decl);
12928 if (class_type && !CLASS_TYPE_P (class_type)) 13315 if (class_type && !CLASS_TYPE_P (class_type))
12929 class_type = NULL_TREE; 13316 class_type = NULL_TREE;
12930 13317
13318 tree_code operator_code;
13319 unsigned op_flags;
12931 if (IDENTIFIER_CONV_OP_P (name)) 13320 if (IDENTIFIER_CONV_OP_P (name))
12932 operator_code = TYPE_EXPR; 13321 {
13322 /* Conversion operators are TYPE_EXPR for the purposes of this
13323 function. */
13324 operator_code = TYPE_EXPR;
13325 op_flags = OVL_OP_FLAG_UNARY;
13326 }
12933 else 13327 else
12934 { 13328 {
12935 /* It'd be nice to hang something else of the identifier to 13329 const ovl_op_info_t *ovl_op = IDENTIFIER_OVL_OP_INFO (name);
12936 find CODE more directly. */ 13330
12937 bool assign_op = IDENTIFIER_ASSIGN_OP_P (name); 13331 operator_code = ovl_op->tree_code;
12938 const operator_name_info_t *oni 13332 op_flags = ovl_op->flags;
12939 = (assign_op ? assignment_operator_name_info : operator_name_info); 13333 gcc_checking_assert (operator_code != ERROR_MARK);
12940 13334 DECL_OVERLOADED_OPERATOR_CODE_RAW (decl) = ovl_op->ovl_op_code;
12941 if (false) 13335 }
12942 ; 13336
12943 #define DEF_OPERATOR(NAME, CODE, MANGLING, ARITY, KIND) \ 13337 if (op_flags & OVL_OP_FLAG_ALLOC)
12944 else if (assign_op == (KIND == cik_assign_op) \ 13338 {
12945 && oni[int (CODE)].identifier == name) \ 13339 /* operator new and operator delete are quite special. */
12946 operator_code = (CODE); 13340 if (class_type)
12947 #include "operators.def" 13341 switch (op_flags)
12948 #undef DEF_OPERATOR 13342 {
12949 else 13343 case OVL_OP_FLAG_ALLOC:
12950 gcc_unreachable (); 13344 TYPE_HAS_NEW_OPERATOR (class_type) = 1;
12951 } 13345 break;
12952 while (0); 13346
12953 gcc_assert (operator_code != MAX_TREE_CODES); 13347 case OVL_OP_FLAG_ALLOC | OVL_OP_FLAG_DELETE:
12954 SET_OVERLOADED_OPERATOR_CODE (decl, operator_code); 13348 TYPE_GETS_DELETE (class_type) |= 1;
12955 13349 break;
12956 if (class_type) 13350
12957 switch (operator_code) 13351 case OVL_OP_FLAG_ALLOC | OVL_OP_FLAG_VEC:
12958 { 13352 TYPE_HAS_ARRAY_NEW_OPERATOR (class_type) = 1;
12959 case NEW_EXPR: 13353 break;
12960 TYPE_HAS_NEW_OPERATOR (class_type) = 1; 13354
12961 break; 13355 case OVL_OP_FLAG_ALLOC | OVL_OP_FLAG_DELETE | OVL_OP_FLAG_VEC:
12962 13356 TYPE_GETS_DELETE (class_type) |= 2;
12963 case DELETE_EXPR: 13357 break;
12964 TYPE_GETS_DELETE (class_type) |= 1; 13358
12965 break; 13359 default:
12966 13360 gcc_unreachable ();
12967 case VEC_NEW_EXPR: 13361 }
12968 TYPE_HAS_ARRAY_NEW_OPERATOR (class_type) = 1; 13362
12969 break; 13363 /* [basic.std.dynamic.allocation]/1:
12970 13364
12971 case VEC_DELETE_EXPR: 13365 A program is ill-formed if an allocation function is declared
12972 TYPE_GETS_DELETE (class_type) |= 2; 13366 in a namespace scope other than global scope or declared
12973 break; 13367 static in global scope.
12974 13368
12975 default: 13369 The same also holds true for deallocation functions. */
12976 break;
12977 }
12978
12979 /* [basic.std.dynamic.allocation]/1:
12980
12981 A program is ill-formed if an allocation function is declared
12982 in a namespace scope other than global scope or declared static
12983 in global scope.
12984
12985 The same also holds true for deallocation functions. */
12986 if (operator_code == NEW_EXPR || operator_code == VEC_NEW_EXPR
12987 || operator_code == DELETE_EXPR || operator_code == VEC_DELETE_EXPR)
12988 {
12989 if (DECL_NAMESPACE_SCOPE_P (decl)) 13370 if (DECL_NAMESPACE_SCOPE_P (decl))
12990 { 13371 {
12991 if (CP_DECL_CONTEXT (decl) != global_namespace) 13372 if (CP_DECL_CONTEXT (decl) != global_namespace)
12992 { 13373 {
12993 error ("%qD may not be declared within a namespace", decl); 13374 error_at (loc, "%qD may not be declared within a namespace",
13375 decl);
12994 return false; 13376 return false;
12995 } 13377 }
12996 else if (!TREE_PUBLIC (decl)) 13378
13379 if (!TREE_PUBLIC (decl))
12997 { 13380 {
12998 error ("%qD may not be declared as static", decl); 13381 error_at (loc, "%qD may not be declared as static", decl);
12999 return false; 13382 return false;
13000 } 13383 }
13001 } 13384 }
13002 } 13385
13003 13386 if (op_flags & OVL_OP_FLAG_DELETE)
13004 if (operator_code == NEW_EXPR || operator_code == VEC_NEW_EXPR) 13387 TREE_TYPE (decl) = coerce_delete_type (TREE_TYPE (decl), loc);
13005 { 13388 else
13006 TREE_TYPE (decl) = coerce_new_type (TREE_TYPE (decl)); 13389 {
13007 DECL_IS_OPERATOR_NEW (decl) = 1; 13390 DECL_IS_OPERATOR_NEW (decl) = 1;
13008 } 13391 TREE_TYPE (decl) = coerce_new_type (TREE_TYPE (decl), loc);
13009 else if (operator_code == DELETE_EXPR || operator_code == VEC_DELETE_EXPR) 13392 }
13010 TREE_TYPE (decl) = coerce_delete_type (TREE_TYPE (decl)); 13393
13011 else 13394 return true;
13012 { 13395 }
13013 /* An operator function must either be a non-static member function 13396
13014 or have at least one parameter of a class, a reference to a class, 13397 /* An operator function must either be a non-static member function
13015 an enumeration, or a reference to an enumeration. 13.4.0.6 */ 13398 or have at least one parameter of a class, a reference to a class,
13016 if (! methodp || DECL_STATIC_FUNCTION_P (decl)) 13399 an enumeration, or a reference to an enumeration. 13.4.0.6 */
13017 { 13400 if (! methodp || DECL_STATIC_FUNCTION_P (decl))
13018 if (operator_code == TYPE_EXPR 13401 {
13019 || operator_code == CALL_EXPR 13402 if (operator_code == TYPE_EXPR
13020 || operator_code == COMPONENT_REF 13403 || operator_code == CALL_EXPR
13021 || operator_code == ARRAY_REF 13404 || operator_code == COMPONENT_REF
13022 || operator_code == NOP_EXPR) 13405 || operator_code == ARRAY_REF
13406 || operator_code == NOP_EXPR)
13407 {
13408 error_at (loc, "%qD must be a nonstatic member function", decl);
13409 return false;
13410 }
13411
13412 if (DECL_STATIC_FUNCTION_P (decl))
13413 {
13414 error_at (loc, "%qD must be either a non-static member "
13415 "function or a non-member function", decl);
13416 return false;
13417 }
13418
13419 for (tree arg = argtypes; ; arg = TREE_CHAIN (arg))
13420 {
13421 if (!arg || arg == void_list_node)
13023 { 13422 {
13024 error ("%qD must be a nonstatic member function", decl); 13423 if (complain)
13424 error_at(loc, "%qD must have an argument of class or "
13425 "enumerated type", decl);
13025 return false; 13426 return false;
13026 } 13427 }
13027 else 13428
13028 { 13429 tree type = non_reference (TREE_VALUE (arg));
13029 tree p; 13430 if (type == error_mark_node)
13030 13431 return false;
13031 if (DECL_STATIC_FUNCTION_P (decl)) 13432
13032 { 13433 /* MAYBE_CLASS_TYPE_P, rather than CLASS_TYPE_P, is used
13033 error ("%qD must be either a non-static member " 13434 because these checks are performed even on template
13034 "function or a non-member function", decl); 13435 functions. */
13035 return false; 13436 if (MAYBE_CLASS_TYPE_P (type)
13036 } 13437 || TREE_CODE (type) == ENUMERAL_TYPE)
13037 13438 break;
13038 for (p = argtypes; p && p != void_list_node; p = TREE_CHAIN (p)) 13439 }
13039 { 13440 }
13040 tree arg = non_reference (TREE_VALUE (p)); 13441
13041 if (arg == error_mark_node) 13442 if (operator_code == CALL_EXPR)
13042 return false; 13443 /* There are no further restrictions on the arguments to an overloaded
13043 13444 "operator ()". */
13044 /* MAYBE_CLASS_TYPE_P, rather than CLASS_TYPE_P, is used 13445 return true;
13045 because these checks are performed even on 13446
13046 template functions. */ 13447 if (operator_code == COND_EXPR)
13047 if (MAYBE_CLASS_TYPE_P (arg) 13448 {
13048 || TREE_CODE (arg) == ENUMERAL_TYPE) 13449 /* 13.4.0.3 */
13049 break; 13450 error_at (loc, "ISO C++ prohibits overloading operator ?:");
13050 } 13451 return false;
13051 13452 }
13052 if (!p || p == void_list_node) 13453
13053 { 13454 /* Count the number of arguments and check for ellipsis. */
13054 if (complain) 13455 int arity = 0;
13055 error ("%qD must have an argument of class or " 13456 for (tree arg = argtypes; arg != void_list_node; arg = TREE_CHAIN (arg))
13056 "enumerated type", decl); 13457 {
13057 return false; 13458 if (!arg)
13058 } 13459 {
13059 } 13460 /* Variadic. */
13060 } 13461 error_at (loc, "%qD must not have variable number of arguments",
13061 13462 decl);
13062 /* There are no restrictions on the arguments to an overloaded
13063 "operator ()". */
13064 if (operator_code == CALL_EXPR)
13065 return true;
13066
13067 /* Warn about conversion operators that will never be used. */
13068 if (IDENTIFIER_CONV_OP_P (name)
13069 && ! DECL_TEMPLATE_INFO (decl)
13070 && warn_conversion
13071 /* Warn only declaring the function; there is no need to
13072 warn again about out-of-class definitions. */
13073 && class_type == current_class_type)
13074 {
13075 tree t = TREE_TYPE (name);
13076 int ref = (TREE_CODE (t) == REFERENCE_TYPE);
13077
13078 if (ref)
13079 t = TYPE_MAIN_VARIANT (TREE_TYPE (t));
13080
13081 if (VOID_TYPE_P (t))
13082 warning (OPT_Wconversion,
13083 ref
13084 ? G_("conversion to a reference to void "
13085 "will never use a type conversion operator")
13086 : G_("conversion to void "
13087 "will never use a type conversion operator"));
13088 else if (class_type)
13089 {
13090 if (t == class_type)
13091 warning (OPT_Wconversion,
13092 ref
13093 ? G_("conversion to a reference to the same type "
13094 "will never use a type conversion operator")
13095 : G_("conversion to the same type "
13096 "will never use a type conversion operator"));
13097 /* Don't force t to be complete here. */
13098 else if (MAYBE_CLASS_TYPE_P (t)
13099 && COMPLETE_TYPE_P (t)
13100 && DERIVED_FROM_P (t, class_type))
13101 warning (OPT_Wconversion,
13102 ref
13103 ? G_("conversion to a reference to a base class "
13104 "will never use a type conversion operator")
13105 : G_("conversion to a base class "
13106 "will never use a type conversion operator"));
13107 }
13108
13109 }
13110
13111 if (operator_code == COND_EXPR)
13112 {
13113 /* 13.4.0.3 */
13114 error ("ISO C++ prohibits overloading operator ?:");
13115 return false; 13463 return false;
13116 } 13464 }
13117 else if (ellipsis_p) 13465 ++arity;
13118 { 13466 }
13119 error ("%qD must not have variable number of arguments", decl); 13467
13468 /* Verify correct number of arguments. */
13469 switch (op_flags)
13470 {
13471 case OVL_OP_FLAG_AMBIARY:
13472 if (arity == 1)
13473 {
13474 /* We have a unary instance of an ambi-ary op. Remap to the
13475 unary one. */
13476 unsigned alt = ovl_op_alternate[ovl_op_mapping [operator_code]];
13477 const ovl_op_info_t *ovl_op = &ovl_op_info[false][alt];
13478 gcc_checking_assert (ovl_op->flags == OVL_OP_FLAG_UNARY);
13479 operator_code = ovl_op->tree_code;
13480 DECL_OVERLOADED_OPERATOR_CODE_RAW (decl) = ovl_op->ovl_op_code;
13481 }
13482 else if (arity != 2)
13483 {
13484 /* This was an ambiguous operator but is invalid. */
13485 error_at (loc,
13486 methodp
13487 ? G_("%qD must have either zero or one argument")
13488 : G_("%qD must have either one or two arguments"), decl);
13120 return false; 13489 return false;
13121 } 13490 }
13122 else if (ambi_op_p (operator_code)) 13491 else if ((operator_code == POSTINCREMENT_EXPR
13123 {
13124 if (arity == 1)
13125 /* We pick the one-argument operator codes by default, so
13126 we don't have to change anything. */
13127 ;
13128 else if (arity == 2)
13129 {
13130 /* If we thought this was a unary operator, we now know
13131 it to be a binary operator. */
13132 switch (operator_code)
13133 {
13134 case INDIRECT_REF:
13135 operator_code = MULT_EXPR;
13136 break;
13137
13138 case ADDR_EXPR:
13139 operator_code = BIT_AND_EXPR;
13140 break;
13141
13142 case UNARY_PLUS_EXPR:
13143 operator_code = PLUS_EXPR;
13144 break;
13145
13146 case NEGATE_EXPR:
13147 operator_code = MINUS_EXPR;
13148 break;
13149
13150 case PREINCREMENT_EXPR:
13151 operator_code = POSTINCREMENT_EXPR;
13152 break;
13153
13154 case PREDECREMENT_EXPR:
13155 operator_code = POSTDECREMENT_EXPR;
13156 break;
13157
13158 default:
13159 gcc_unreachable ();
13160 }
13161
13162 SET_OVERLOADED_OPERATOR_CODE (decl, operator_code);
13163
13164 if ((operator_code == POSTINCREMENT_EXPR
13165 || operator_code == POSTDECREMENT_EXPR)
13166 && ! processing_template_decl
13167 && ! same_type_p (TREE_VALUE (TREE_CHAIN (argtypes)), integer_type_node))
13168 {
13169 if (methodp)
13170 error ("postfix %qD must take %<int%> as its argument",
13171 decl);
13172 else
13173 error ("postfix %qD must take %<int%> as its second "
13174 "argument", decl);
13175 return false;
13176 }
13177 }
13178 else
13179 {
13180 if (methodp)
13181 error ("%qD must take either zero or one argument", decl);
13182 else
13183 error ("%qD must take either one or two arguments", decl);
13184 return false;
13185 }
13186
13187 /* More Effective C++ rule 6. */
13188 if (warn_ecpp
13189 && (operator_code == POSTINCREMENT_EXPR
13190 || operator_code == POSTDECREMENT_EXPR
13191 || operator_code == PREINCREMENT_EXPR
13192 || operator_code == PREDECREMENT_EXPR))
13193 {
13194 tree arg = TREE_VALUE (argtypes);
13195 tree ret = TREE_TYPE (TREE_TYPE (decl));
13196 if (methodp || TREE_CODE (arg) == REFERENCE_TYPE)
13197 arg = TREE_TYPE (arg);
13198 arg = TYPE_MAIN_VARIANT (arg);
13199 if (operator_code == PREINCREMENT_EXPR
13200 || operator_code == PREDECREMENT_EXPR)
13201 {
13202 if (TREE_CODE (ret) != REFERENCE_TYPE
13203 || !same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (ret)),
13204 arg))
13205 warning (OPT_Weffc__, "prefix %qD should return %qT", decl,
13206 build_reference_type (arg));
13207 }
13208 else
13209 {
13210 if (!same_type_p (TYPE_MAIN_VARIANT (ret), arg))
13211 warning (OPT_Weffc__, "postfix %qD should return %qT", decl, arg);
13212 }
13213 }
13214 }
13215 else if (unary_op_p (operator_code))
13216 {
13217 if (arity != 1)
13218 {
13219 if (methodp)
13220 error ("%qD must take %<void%>", decl);
13221 else
13222 error ("%qD must take exactly one argument", decl);
13223 return false;
13224 }
13225 }
13226 else /* if (binary_op_p (operator_code)) */
13227 {
13228 if (arity != 2)
13229 {
13230 if (methodp)
13231 error ("%qD must take exactly one argument", decl);
13232 else
13233 error ("%qD must take exactly two arguments", decl);
13234 return false;
13235 }
13236
13237 /* More Effective C++ rule 7. */
13238 if (warn_ecpp
13239 && (operator_code == TRUTH_ANDIF_EXPR
13240 || operator_code == TRUTH_ORIF_EXPR
13241 || operator_code == COMPOUND_EXPR))
13242 warning (OPT_Weffc__, "user-defined %qD always evaluates both arguments",
13243 decl);
13244 }
13245
13246 /* Effective C++ rule 23. */
13247 if (warn_ecpp
13248 && arity == 2
13249 && !DECL_ASSIGNMENT_OPERATOR_P (decl)
13250 && (operator_code == PLUS_EXPR
13251 || operator_code == MINUS_EXPR
13252 || operator_code == TRUNC_DIV_EXPR
13253 || operator_code == MULT_EXPR
13254 || operator_code == TRUNC_MOD_EXPR)
13255 && TREE_CODE (TREE_TYPE (TREE_TYPE (decl))) == REFERENCE_TYPE)
13256 warning (OPT_Weffc__, "%qD should return by value", decl);
13257
13258 /* [over.oper]/8 */
13259 for (; argtypes && argtypes != void_list_node;
13260 argtypes = TREE_CHAIN (argtypes))
13261 if (TREE_PURPOSE (argtypes))
13262 {
13263 TREE_PURPOSE (argtypes) = NULL_TREE;
13264 if (operator_code == POSTINCREMENT_EXPR
13265 || operator_code == POSTDECREMENT_EXPR) 13492 || operator_code == POSTDECREMENT_EXPR)
13266 { 13493 && ! processing_template_decl
13267 pedwarn (input_location, OPT_Wpedantic, "%qD cannot have default arguments", 13494 /* x++ and x--'s second argument must be an int. */
13268 decl); 13495 && ! same_type_p (TREE_VALUE (TREE_CHAIN (argtypes)),
13269 } 13496 integer_type_node))
13270 else 13497 {
13271 { 13498 error_at (loc,
13272 error ("%qD cannot have default arguments", decl); 13499 methodp
13273 return false; 13500 ? G_("postfix %qD must have %<int%> as its argument")
13274 } 13501 : G_("postfix %qD must have %<int%> as its second argument"),
13275 } 13502 decl);
13276 } 13503 return false;
13504 }
13505 break;
13506
13507 case OVL_OP_FLAG_UNARY:
13508 if (arity != 1)
13509 {
13510 error_at (loc,
13511 methodp
13512 ? G_("%qD must have no arguments")
13513 : G_("%qD must have exactly one argument"), decl);
13514 return false;
13515 }
13516 break;
13517
13518 case OVL_OP_FLAG_BINARY:
13519 if (arity != 2)
13520 {
13521 error_at (loc,
13522 methodp
13523 ? G_("%qD must have exactly one argument")
13524 : G_("%qD must have exactly two arguments"), decl);
13525 return false;
13526 }
13527 break;
13528
13529 default:
13530 gcc_unreachable ();
13531 }
13532
13533 /* There can be no default arguments. */
13534 for (tree arg = argtypes; arg != void_list_node; arg = TREE_CHAIN (arg))
13535 if (TREE_PURPOSE (arg))
13536 {
13537 TREE_PURPOSE (arg) = NULL_TREE;
13538 error_at (loc, "%qD cannot have default arguments", decl);
13539 return false;
13540 }
13541
13542 /* At this point the declaration is well-formed. It may not be
13543 sensible though. */
13544
13545 /* Check member function warnings only on the in-class declaration.
13546 There's no point warning on an out-of-class definition. */
13547 if (class_type && class_type != current_class_type)
13548 return true;
13549
13550 /* Warn about conversion operators that will never be used. */
13551 if (IDENTIFIER_CONV_OP_P (name)
13552 && ! DECL_TEMPLATE_INFO (decl)
13553 && warn_class_conversion)
13554 {
13555 tree t = TREE_TYPE (name);
13556 int ref = TYPE_REF_P (t);
13557
13558 if (ref)
13559 t = TYPE_MAIN_VARIANT (TREE_TYPE (t));
13560
13561 if (VOID_TYPE_P (t))
13562 warning_at (loc, OPT_Wclass_conversion, "converting %qT to %<void%> "
13563 "will never use a type conversion operator", class_type);
13564 else if (class_type)
13565 {
13566 if (same_type_ignoring_top_level_qualifiers_p (t, class_type))
13567 warning_at (loc, OPT_Wclass_conversion,
13568 ref
13569 ? G_("converting %qT to a reference to the same type "
13570 "will never use a type conversion operator")
13571 : G_("converting %qT to the same type "
13572 "will never use a type conversion operator"),
13573 class_type);
13574 /* Don't force t to be complete here. */
13575 else if (MAYBE_CLASS_TYPE_P (t)
13576 && COMPLETE_TYPE_P (t)
13577 && DERIVED_FROM_P (t, class_type))
13578 warning_at (loc, OPT_Wclass_conversion,
13579 ref
13580 ? G_("converting %qT to a reference to a base class "
13581 "%qT will never use a type conversion operator")
13582 : G_("converting %qT to a base class %qT "
13583 "will never use a type conversion operator"),
13584 class_type, t);
13585 }
13586 }
13587
13588 if (!warn_ecpp)
13589 return true;
13590
13591 /* Effective C++ rules below. */
13592
13593 /* More Effective C++ rule 7. */
13594 if (operator_code == TRUTH_ANDIF_EXPR
13595 || operator_code == TRUTH_ORIF_EXPR
13596 || operator_code == COMPOUND_EXPR)
13597 warning_at (loc, OPT_Weffc__,
13598 "user-defined %qD always evaluates both arguments", decl);
13599
13600 /* More Effective C++ rule 6. */
13601 if (operator_code == POSTINCREMENT_EXPR
13602 || operator_code == POSTDECREMENT_EXPR
13603 || operator_code == PREINCREMENT_EXPR
13604 || operator_code == PREDECREMENT_EXPR)
13605 {
13606 tree arg = TREE_VALUE (argtypes);
13607 tree ret = TREE_TYPE (TREE_TYPE (decl));
13608 if (methodp || TYPE_REF_P (arg))
13609 arg = TREE_TYPE (arg);
13610 arg = TYPE_MAIN_VARIANT (arg);
13611
13612 if (operator_code == PREINCREMENT_EXPR
13613 || operator_code == PREDECREMENT_EXPR)
13614 {
13615 if (!TYPE_REF_P (ret)
13616 || !same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (ret)), arg))
13617 warning_at (loc, OPT_Weffc__, "prefix %qD should return %qT", decl,
13618 build_reference_type (arg));
13619 }
13620 else
13621 {
13622 if (!same_type_p (TYPE_MAIN_VARIANT (ret), arg))
13623 warning_at (loc, OPT_Weffc__,
13624 "postfix %qD should return %qT", decl, arg);
13625 }
13626 }
13627
13628 /* Effective C++ rule 23. */
13629 if (!DECL_ASSIGNMENT_OPERATOR_P (decl)
13630 && (operator_code == PLUS_EXPR
13631 || operator_code == MINUS_EXPR
13632 || operator_code == TRUNC_DIV_EXPR
13633 || operator_code == MULT_EXPR
13634 || operator_code == TRUNC_MOD_EXPR)
13635 && TYPE_REF_P (TREE_TYPE (TREE_TYPE (decl))))
13636 warning_at (loc, OPT_Weffc__, "%qD should return by value", decl);
13637
13277 return true; 13638 return true;
13278 } 13639 }
13279 13640
13280 /* Return a string giving the keyword associate with CODE. */ 13641 /* Return a string giving the keyword associate with CODE. */
13281 13642
13550 /* Since SCOPE is not TS_CURRENT, we are not looking at a 13911 /* Since SCOPE is not TS_CURRENT, we are not looking at a
13551 definition of this tag. Since, in addition, we are currently 13912 definition of this tag. Since, in addition, we are currently
13552 processing a (member) template declaration of a template 13913 processing a (member) template declaration of a template
13553 class, we must be very careful; consider: 13914 class, we must be very careful; consider:
13554 13915
13555 template <class X> 13916 template <class X> struct S1
13556 struct S1 13917
13557 13918 template <class U> struct S2
13558 template <class U> 13919 {
13559 struct S2 13920 template <class V> friend struct S1;
13560 { template <class V> 13921 };
13561 friend struct S1; };
13562 13922
13563 Here, the S2::S1 declaration should not be confused with the 13923 Here, the S2::S1 declaration should not be confused with the
13564 outer declaration. In particular, the inner version should 13924 outer declaration. In particular, the inner version should
13565 have a template parameter of level 2, not level 1. This 13925 have a template parameter of level 2, not level 1.
13566 would be particularly important if the member declaration 13926
13567 were instead: 13927 On the other hand, when presented with:
13568 13928
13569 template <class V = U> friend struct S1; 13929 template <class T> struct S1
13570 13930 {
13571 say, when we should tsubst into `U' when instantiating 13931 template <class U> struct S2 {};
13572 S2. On the other hand, when presented with: 13932 template <class U> friend struct S2;
13573
13574 template <class T>
13575 struct S1 {
13576 template <class U>
13577 struct S2 {};
13578 template <class U>
13579 friend struct S2;
13580 }; 13933 };
13581 13934
13582 we must find the inner binding eventually. We 13935 the friend must find S1::S2 eventually. We accomplish this
13583 accomplish this by making sure that the new type we 13936 by making sure that the new type we create to represent this
13584 create to represent this declaration has the right 13937 declaration has the right TYPE_CONTEXT. */
13585 TYPE_CONTEXT. */
13586 context = TYPE_CONTEXT (t); 13938 context = TYPE_CONTEXT (t);
13587 t = NULL_TREE; 13939 t = NULL_TREE;
13588 } 13940 }
13589 13941
13590 if (! t) 13942 if (! t)
13601 else 13953 else
13602 { 13954 {
13603 t = make_class_type (code); 13955 t = make_class_type (code);
13604 TYPE_CONTEXT (t) = context; 13956 TYPE_CONTEXT (t) = context;
13605 if (scope == ts_lambda) 13957 if (scope == ts_lambda)
13606 /* Mark it as a lambda type. */ 13958 {
13607 CLASSTYPE_LAMBDA_EXPR (t) = error_mark_node; 13959 /* Mark it as a lambda type. */
13960 CLASSTYPE_LAMBDA_EXPR (t) = error_mark_node;
13961 /* And push it into current scope. */
13962 scope = ts_current;
13963 }
13608 t = pushtag (name, t, scope); 13964 t = pushtag (name, t, scope);
13609 } 13965 }
13610 } 13966 }
13611 else 13967 else
13612 { 13968 {
13630 error ("redeclaration of %qT as a non-template", t); 13986 error ("redeclaration of %qT as a non-template", t);
13631 inform (location_of (t), "previous declaration %qD", t); 13987 inform (location_of (t), "previous declaration %qD", t);
13632 return error_mark_node; 13988 return error_mark_node;
13633 } 13989 }
13634 13990
13635 /* Make injected friend class visible. */
13636 if (scope != ts_within_enclosing_non_class && TYPE_HIDDEN_P (t)) 13991 if (scope != ts_within_enclosing_non_class && TYPE_HIDDEN_P (t))
13637 { 13992 {
13993 /* This is no longer an invisible friend. Make it
13994 visible. */
13638 tree decl = TYPE_NAME (t); 13995 tree decl = TYPE_NAME (t);
13639 13996
13640 DECL_ANTICIPATED (decl) = false; 13997 DECL_ANTICIPATED (decl) = false;
13641 DECL_FRIEND_P (decl) = false; 13998 DECL_FRIEND_P (decl) = false;
13642 13999
13763 vec_alloc (BINFO_BASE_ACCESSES (binfo), max_bases); 14120 vec_alloc (BINFO_BASE_ACCESSES (binfo), max_bases);
13764 /* A C++98 POD cannot have base classes. */ 14121 /* A C++98 POD cannot have base classes. */
13765 CLASSTYPE_NON_LAYOUT_POD_P (ref) = true; 14122 CLASSTYPE_NON_LAYOUT_POD_P (ref) = true;
13766 14123
13767 if (TREE_CODE (ref) == UNION_TYPE) 14124 if (TREE_CODE (ref) == UNION_TYPE)
13768 error ("derived union %qT invalid", ref); 14125 {
14126 error ("derived union %qT invalid", ref);
14127 return;
14128 }
13769 } 14129 }
13770 14130
13771 if (max_bases > 1) 14131 if (max_bases > 1)
13772 warning (OPT_Wmultiple_inheritance, 14132 warning (OPT_Wmultiple_inheritance,
13773 "%qT defined with multiple direct bases", ref); 14133 "%qT defined with multiple direct bases", ref);
13996 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)), 14356 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)),
13997 "previous definition here"); 14357 "previous definition here");
13998 enumtype = error_mark_node; 14358 enumtype = error_mark_node;
13999 } 14359 }
14000 else if (underlying_type && ENUM_UNDERLYING_TYPE (enumtype) 14360 else if (underlying_type && ENUM_UNDERLYING_TYPE (enumtype)
14001 && !dependent_type_p (underlying_type)
14002 && !dependent_type_p (ENUM_UNDERLYING_TYPE (enumtype))
14003 && !same_type_p (underlying_type, 14361 && !same_type_p (underlying_type,
14004 ENUM_UNDERLYING_TYPE (enumtype))) 14362 ENUM_UNDERLYING_TYPE (enumtype)))
14005 { 14363 {
14006 error_at (input_location, "different underlying type " 14364 error_at (input_location, "different underlying type "
14007 "in enum %q#T", enumtype); 14365 "in enum %q#T", enumtype);
14022 enumtype = NULL_TREE; 14380 enumtype = NULL_TREE;
14023 } 14381 }
14024 14382
14025 /* enumtype may be an ENUMERAL_TYPE if this is a redefinition 14383 /* enumtype may be an ENUMERAL_TYPE if this is a redefinition
14026 of an opaque enum, or an opaque enum of an already defined 14384 of an opaque enum, or an opaque enum of an already defined
14027 enumeration (C++0x only). 14385 enumeration (C++11).
14028 In any other case, it'll be NULL_TREE. */ 14386 In any other case, it'll be NULL_TREE. */
14029 if (!enumtype) 14387 if (!enumtype)
14030 { 14388 {
14031 if (is_new) 14389 if (is_new)
14032 *is_new = true; 14390 *is_new = true;
14033 } 14391 }
14034 prevtype = enumtype; 14392 prevtype = enumtype;
14035 14393
14036 /* Do not push the decl more than once, unless we need to 14394 /* Do not push the decl more than once. */
14037 compare underlying types at instantiation time */
14038 if (!enumtype 14395 if (!enumtype
14039 || TREE_CODE (enumtype) != ENUMERAL_TYPE 14396 || TREE_CODE (enumtype) != ENUMERAL_TYPE)
14040 || (underlying_type
14041 && dependent_type_p (underlying_type))
14042 || (ENUM_UNDERLYING_TYPE (enumtype)
14043 && dependent_type_p (ENUM_UNDERLYING_TYPE (enumtype))))
14044 { 14397 {
14045 enumtype = cxx_make_type (ENUMERAL_TYPE); 14398 enumtype = cxx_make_type (ENUMERAL_TYPE);
14046 enumtype = pushtag (name, enumtype, /*tag_scope=*/ts_current); 14399 enumtype = pushtag (name, enumtype, /*tag_scope=*/ts_current);
14047 14400
14048 /* std::byte aliases anything. */ 14401 /* std::byte aliases anything. */
14422 if (value == NULL_TREE) 14775 if (value == NULL_TREE)
14423 { 14776 {
14424 if (TYPE_VALUES (enumtype)) 14777 if (TYPE_VALUES (enumtype))
14425 { 14778 {
14426 tree prev_value; 14779 tree prev_value;
14427 bool overflowed;
14428 14780
14429 /* C++03 7.2/4: If no initializer is specified for the first 14781 /* C++03 7.2/4: If no initializer is specified for the first
14430 enumerator, the type is an unspecified integral 14782 enumerator, the type is an unspecified integral
14431 type. Otherwise the type is the same as the type of the 14783 type. Otherwise the type is the same as the type of the
14432 initializing value of the preceding enumerator unless the 14784 initializing value of the preceding enumerator unless the
14436 prev_value = DECL_INITIAL (TREE_VALUE (TYPE_VALUES (enumtype))); 14788 prev_value = DECL_INITIAL (TREE_VALUE (TYPE_VALUES (enumtype)));
14437 if (error_operand_p (prev_value)) 14789 if (error_operand_p (prev_value))
14438 value = error_mark_node; 14790 value = error_mark_node;
14439 else 14791 else
14440 { 14792 {
14793 wi::overflow_type overflowed;
14441 tree type = TREE_TYPE (prev_value); 14794 tree type = TREE_TYPE (prev_value);
14442 signop sgn = TYPE_SIGN (type); 14795 signop sgn = TYPE_SIGN (type);
14443 widest_int wi = wi::add (wi::to_widest (prev_value), 1, sgn, 14796 widest_int wi = wi::add (wi::to_widest (prev_value), 1, sgn,
14444 &overflowed); 14797 &overflowed);
14445 if (!overflowed) 14798 if (!overflowed)
14462 pos ? G_("\ 14815 pos ? G_("\
14463 incremented enumerator value is too large for %<unsigned long%>") : G_("\ 14816 incremented enumerator value is too large for %<unsigned long%>") : G_("\
14464 incremented enumerator value is too large for %<long%>")); 14817 incremented enumerator value is too large for %<long%>"));
14465 } 14818 }
14466 if (type == NULL_TREE) 14819 if (type == NULL_TREE)
14467 overflowed = true; 14820 overflowed = wi::OVF_UNKNOWN;
14468 else 14821 else
14469 value = wide_int_to_tree (type, wi); 14822 value = wide_int_to_tree (type, wi);
14470 } 14823 }
14471 14824
14472 if (overflowed) 14825 if (overflowed)
14609 fntype = build_method_type_directly (TREE_TYPE (TREE_VALUE (args)), 14962 fntype = build_method_type_directly (TREE_TYPE (TREE_VALUE (args)),
14610 void_type_node, 14963 void_type_node,
14611 TREE_CHAIN (args)); 14964 TREE_CHAIN (args));
14612 else 14965 else
14613 fntype = build_function_type (void_type_node, args); 14966 fntype = build_function_type (void_type_node, args);
14614 fntype
14615 = build_exception_variant (fntype,
14616 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl)));
14617 fntype = (cp_build_type_attribute_variant 14967 fntype = (cp_build_type_attribute_variant
14618 (fntype, TYPE_ATTRIBUTES (TREE_TYPE (decl)))); 14968 (fntype, TYPE_ATTRIBUTES (TREE_TYPE (decl))));
14969 fntype = cxx_copy_lang_qualifiers (fntype, TREE_TYPE (decl));
14619 TREE_TYPE (decl) = fntype; 14970 TREE_TYPE (decl) = fntype;
14620 } 14971 }
14621 else 14972 else
14622 { 14973 {
14623 abstract_virtuals_error (decl, TREE_TYPE (fntype)); 14974 abstract_virtuals_error (decl, TREE_TYPE (fntype));
14654 15005
14655 tree ctype = current_class_type; 15006 tree ctype = current_class_type;
14656 if (!vbases) 15007 if (!vbases)
14657 ctype = CLASSTYPE_AS_BASE (ctype); 15008 ctype = CLASSTYPE_AS_BASE (ctype);
14658 15009
14659 tree clobber = build_constructor (ctype, NULL); 15010 tree clobber = build_clobber (ctype);
14660 TREE_THIS_VOLATILE (clobber) = true;
14661 15011
14662 tree thisref = current_class_ref; 15012 tree thisref = current_class_ref;
14663 if (ctype != current_class_type) 15013 if (ctype != current_class_type)
14664 { 15014 {
14665 thisref = build_nop (build_reference_type (ctype), current_class_ptr); 15015 thisref = build_nop (build_reference_type (ctype), current_class_ptr);
14774 if (flags & SF_INCLASS_INLINE) 15124 if (flags & SF_INCLASS_INLINE)
14775 maybe_begin_member_template_processing (decl1); 15125 maybe_begin_member_template_processing (decl1);
14776 15126
14777 /* Effective C++ rule 15. */ 15127 /* Effective C++ rule 15. */
14778 if (warn_ecpp 15128 if (warn_ecpp
14779 && DECL_OVERLOADED_OPERATOR_P (decl1) == NOP_EXPR 15129 && DECL_ASSIGNMENT_OPERATOR_P (decl1)
15130 && DECL_OVERLOADED_OPERATOR_IS (decl1, NOP_EXPR)
14780 && VOID_TYPE_P (TREE_TYPE (fntype))) 15131 && VOID_TYPE_P (TREE_TYPE (fntype)))
14781 warning (OPT_Weffc__, "%<operator=%> should return a reference to %<*this%>"); 15132 warning (OPT_Weffc__,
15133 "%<operator=%> should return a reference to %<*this%>");
14782 15134
14783 /* Make the init_value nonzero so pushdecl knows this is not tentative. 15135 /* Make the init_value nonzero so pushdecl knows this is not tentative.
14784 error_mark_node is replaced below (in poplevel) with the BLOCK. */ 15136 error_mark_node is replaced below (in poplevel) with the BLOCK. */
14785 if (!DECL_INITIAL (decl1)) 15137 if (!DECL_INITIAL (decl1))
14786 DECL_INITIAL (decl1) = error_mark_node; 15138 DECL_INITIAL (decl1) = error_mark_node;
14951 15303
14952 gcc_assert (t != NULL_TREE && TREE_CODE (t) == PARM_DECL); 15304 gcc_assert (t != NULL_TREE && TREE_CODE (t) == PARM_DECL);
14953 gcc_assert (TYPE_PTR_P (TREE_TYPE (t))); 15305 gcc_assert (TYPE_PTR_P (TREE_TYPE (t)));
14954 15306
14955 cp_function_chain->x_current_class_ref 15307 cp_function_chain->x_current_class_ref
14956 = cp_build_indirect_ref (t, RO_NULL, tf_warning_or_error); 15308 = cp_build_fold_indirect_ref (t);
14957 /* Set this second to avoid shortcut in cp_build_indirect_ref. */ 15309 /* Set this second to avoid shortcut in cp_build_indirect_ref. */
14958 cp_function_chain->x_current_class_ptr = t; 15310 cp_function_chain->x_current_class_ptr = t;
14959 15311
14960 /* Constructors and destructors need to know whether they're "in 15312 /* Constructors and destructors need to know whether they're "in
14961 charge" of initializing virtual base classes. */ 15313 charge" of initializing virtual base classes. */
15299 finish_compound_stmt (compound_stmt); 15651 finish_compound_stmt (compound_stmt);
15300 15652
15301 if (flag_lifetime_dse 15653 if (flag_lifetime_dse
15302 /* Clobbering an empty base is harmful if it overlays real data. */ 15654 /* Clobbering an empty base is harmful if it overlays real data. */
15303 && !is_empty_class (current_class_type)) 15655 && !is_empty_class (current_class_type))
15304 finish_decl_cleanup (NULL_TREE, build_clobber_this ()); 15656 {
15657 if (sanitize_flags_p (SANITIZE_VPTR)
15658 && (flag_sanitize_recover & SANITIZE_VPTR) == 0
15659 && TYPE_CONTAINS_VPTR_P (current_class_type))
15660 {
15661 tree binfo = TYPE_BINFO (current_class_type);
15662 tree ref
15663 = cp_build_fold_indirect_ref (current_class_ptr);
15664
15665 tree vtbl_ptr = build_vfield_ref (ref, TREE_TYPE (binfo));
15666 tree vtbl = build_zero_cst (TREE_TYPE (vtbl_ptr));
15667 tree stmt = cp_build_modify_expr (input_location, vtbl_ptr,
15668 NOP_EXPR, vtbl,
15669 tf_warning_or_error);
15670 /* If the vptr is shared with some virtual nearly empty base,
15671 don't clear it if not in charge, the dtor of the virtual
15672 nearly empty base will do that later. */
15673 if (CLASSTYPE_VBASECLASSES (current_class_type)
15674 && CLASSTYPE_PRIMARY_BINFO (current_class_type)
15675 && BINFO_VIRTUAL_P
15676 (CLASSTYPE_PRIMARY_BINFO (current_class_type)))
15677 {
15678 stmt = convert_to_void (stmt, ICV_STATEMENT,
15679 tf_warning_or_error);
15680 stmt = build_if_in_charge (stmt);
15681 }
15682 finish_decl_cleanup (NULL_TREE, stmt);
15683 }
15684 else
15685 finish_decl_cleanup (NULL_TREE, build_clobber_this ());
15686 }
15305 15687
15306 /* And insert cleanups for our bases and members so that they 15688 /* And insert cleanups for our bases and members so that they
15307 will be properly destroyed if we throw. */ 15689 will be properly destroyed if we throw. */
15308 push_base_cleanups (); 15690 push_base_cleanups ();
15309 } 15691 }
15438 && !cp_function_chain->invalid_constexpr 15820 && !cp_function_chain->invalid_constexpr
15439 && !DECL_CLONED_FUNCTION_P (fun)) 15821 && !DECL_CLONED_FUNCTION_P (fun))
15440 register_constexpr_fundef (fun, DECL_SAVED_TREE (fun)); 15822 register_constexpr_fundef (fun, DECL_SAVED_TREE (fun));
15441 } 15823 }
15442 15824
15825 /* Attempt to add a fix-it hint to RICHLOC suggesting the insertion
15826 of "return *this;" immediately before its location, using FNDECL's
15827 first statement (if any) to give the indentation, if appropriate. */
15828
15829 static void
15830 add_return_star_this_fixit (gcc_rich_location *richloc, tree fndecl)
15831 {
15832 location_t indent = UNKNOWN_LOCATION;
15833 tree stmts = expr_first (DECL_SAVED_TREE (fndecl));
15834 if (stmts)
15835 indent = EXPR_LOCATION (stmts);
15836 richloc->add_fixit_insert_formatted ("return *this;",
15837 richloc->get_loc (),
15838 indent);
15839 }
15840
15443 /* Finish up a function declaration and compile that function 15841 /* Finish up a function declaration and compile that function
15444 all the way to assembler language output. The free the storage 15842 all the way to assembler language output. The free the storage
15445 for the function definition. INLINE_P is TRUE if we just 15843 for the function definition. INLINE_P is TRUE if we just
15446 finished processing the body of an in-class inline function 15844 finished processing the body of an in-class inline function
15447 definition. (This processing will have taken place after the 15845 definition. (This processing will have taken place after the
15491 current_eh_spec_block); 15889 current_eh_spec_block);
15492 } 15890 }
15493 15891
15494 /* If we're saving up tree structure, tie off the function now. */ 15892 /* If we're saving up tree structure, tie off the function now. */
15495 DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl)); 15893 DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl));
15496
15497 if (fn_contains_cilk_spawn_p (cfun) && !processing_template_decl)
15498 cfun->cilk_frame_decl = insert_cilk_frame (fndecl);
15499 15894
15500 finish_fname_decls (); 15895 finish_fname_decls ();
15501 15896
15502 /* If this function can't throw any exceptions, remember that. */ 15897 /* If this function can't throw any exceptions, remember that. */
15503 if (!processing_template_decl 15898 if (!processing_template_decl
15634 /* Structor return values (if any) are set by the compiler. */ 16029 /* Structor return values (if any) are set by the compiler. */
15635 && !DECL_CONSTRUCTOR_P (fndecl) 16030 && !DECL_CONSTRUCTOR_P (fndecl)
15636 && !DECL_DESTRUCTOR_P (fndecl) 16031 && !DECL_DESTRUCTOR_P (fndecl)
15637 && targetm.warn_func_return (fndecl)) 16032 && targetm.warn_func_return (fndecl))
15638 { 16033 {
15639 warning (OPT_Wreturn_type, 16034 gcc_rich_location richloc (input_location);
15640 "no return statement in function returning non-void"); 16035 /* Potentially add a "return *this;" fix-it hint for
16036 assignment operators. */
16037 if (IDENTIFIER_ASSIGN_OP_P (DECL_NAME (fndecl)))
16038 {
16039 tree valtype = TREE_TYPE (DECL_RESULT (fndecl));
16040 if (TREE_CODE (valtype) == REFERENCE_TYPE
16041 && same_type_ignoring_top_level_qualifiers_p
16042 (TREE_TYPE (valtype), TREE_TYPE (current_class_ref)))
16043 if (global_dc->option_enabled (OPT_Wreturn_type,
16044 global_dc->option_state))
16045 add_return_star_this_fixit (&richloc, fndecl);
16046 }
16047 warning_at (&richloc, OPT_Wreturn_type,
16048 "no return statement in function returning non-void");
15641 TREE_NO_WARNING (fndecl) = 1; 16049 TREE_NO_WARNING (fndecl) = 1;
15642 } 16050 }
15643 16051
15644 /* Store the end of the function, so that we get good line number 16052 /* Store the end of the function, so that we get good line number
15645 info for the epilogue. */ 16053 info for the epilogue. */
15662 && DECL_NAME (decl) 16070 && DECL_NAME (decl)
15663 && !DECL_ARTIFICIAL (decl) 16071 && !DECL_ARTIFICIAL (decl)
15664 && !TREE_NO_WARNING (decl) 16072 && !TREE_NO_WARNING (decl)
15665 && !DECL_IN_SYSTEM_HEADER (decl) 16073 && !DECL_IN_SYSTEM_HEADER (decl)
15666 && TREE_TYPE (decl) != error_mark_node 16074 && TREE_TYPE (decl) != error_mark_node
15667 && TREE_CODE (TREE_TYPE (decl)) != REFERENCE_TYPE 16075 && !TYPE_REF_P (TREE_TYPE (decl))
15668 && (!CLASS_TYPE_P (TREE_TYPE (decl)) 16076 && (!CLASS_TYPE_P (TREE_TYPE (decl))
15669 || !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))) 16077 || !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (decl))))
15670 warning_at (DECL_SOURCE_LOCATION (decl), 16078 warning_at (DECL_SOURCE_LOCATION (decl),
15671 OPT_Wunused_but_set_parameter, 16079 OPT_Wunused_but_set_parameter,
15672 "parameter %qD set but not used", decl); 16080 "parameter %qD set but not used", decl);
15994 tree fntype; 16402 tree fntype;
15995 tree args; 16403 tree args;
15996 16404
15997 if (TYPE_PTRMEMFUNC_P (memfntype)) 16405 if (TYPE_PTRMEMFUNC_P (memfntype))
15998 memfntype = TYPE_PTRMEMFUNC_FN_TYPE (memfntype); 16406 memfntype = TYPE_PTRMEMFUNC_FN_TYPE (memfntype);
15999 if (POINTER_TYPE_P (memfntype) 16407 if (INDIRECT_TYPE_P (memfntype)
16000 || TREE_CODE (memfntype) == FUNCTION_DECL) 16408 || TREE_CODE (memfntype) == FUNCTION_DECL)
16001 memfntype = TREE_TYPE (memfntype); 16409 memfntype = TREE_TYPE (memfntype);
16002 if (TREE_CODE (memfntype) == FUNCTION_TYPE) 16410 if (TREE_CODE (memfntype) == FUNCTION_TYPE)
16003 return memfntype; 16411 return memfntype;
16004 gcc_assert (TREE_CODE (memfntype) == METHOD_TYPE); 16412 gcc_assert (TREE_CODE (memfntype) == METHOD_TYPE);
16005 args = TYPE_ARG_TYPES (memfntype); 16413 args = TYPE_ARG_TYPES (memfntype);
16006 cp_ref_qualifier rqual = type_memfn_rqual (memfntype);
16007 fntype = build_function_type (TREE_TYPE (memfntype), TREE_CHAIN (args)); 16414 fntype = build_function_type (TREE_TYPE (memfntype), TREE_CHAIN (args));
16008 fntype = apply_memfn_quals (fntype, type_memfn_quals (memfntype), rqual); 16415 fntype = apply_memfn_quals (fntype, type_memfn_quals (memfntype));
16009 fntype = (cp_build_type_attribute_variant 16416 fntype = (cp_build_type_attribute_variant
16010 (fntype, TYPE_ATTRIBUTES (memfntype))); 16417 (fntype, TYPE_ATTRIBUTES (memfntype)));
16011 fntype = (build_exception_variant 16418 fntype = cxx_copy_lang_qualifiers (fntype, memfntype);
16012 (fntype, TYPE_RAISES_EXCEPTIONS (memfntype)));
16013 if (TYPE_HAS_LATE_RETURN_TYPE (memfntype))
16014 TYPE_HAS_LATE_RETURN_TYPE (fntype) = 1;
16015 return fntype; 16419 return fntype;
16016 } 16420 }
16017 16421
16018 /* DECL was originally constructed as a non-static member function, 16422 /* DECL was originally constructed as a non-static member function,
16019 but turned out to be static. Update it accordingly. */ 16423 but turned out to be static. Update it accordingly. */
16127 return f->x_auto_return_pattern; 16531 return f->x_auto_return_pattern;
16128 } 16532 }
16129 return TREE_TYPE (TREE_TYPE (fn)); 16533 return TREE_TYPE (TREE_TYPE (fn));
16130 } 16534 }
16131 16535
16132 /* Returns true iff DECL was declared with an auto type and it has 16536 /* Returns true iff DECL is a variable or function declared with an auto type
16133 not yet been deduced to a real type. */ 16537 that has not yet been deduced to a real type. */
16134 16538
16135 bool 16539 bool
16136 undeduced_auto_decl (tree decl) 16540 undeduced_auto_decl (tree decl)
16137 { 16541 {
16138 if (cxx_dialect < cxx11) 16542 if (cxx_dialect < cxx11)
16139 return false; 16543 return false;
16140 return type_uses_auto (TREE_TYPE (decl)); 16544 return ((VAR_OR_FUNCTION_DECL_P (decl)
16545 || TREE_CODE (decl) == TEMPLATE_DECL)
16546 && type_uses_auto (TREE_TYPE (decl)));
16141 } 16547 }
16142 16548
16143 /* Complain if DECL has an undeduced return type. */ 16549 /* Complain if DECL has an undeduced return type. */
16144 16550
16145 bool 16551 bool