comparison gcc/cp/name-lookup.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 /* Definitions for C++ name lookup routines. 1 /* Definitions for C++ name lookup routines.
2 Copyright (C) 2003-2017 Free Software Foundation, Inc. 2 Copyright (C) 2003-2018 Free Software Foundation, Inc.
3 Contributed by Gabriel Dos Reis <gdr@integrable-solutions.net> 3 Contributed by Gabriel Dos Reis <gdr@integrable-solutions.net>
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
17 You should have received a copy of the GNU General Public License 17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see 18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */ 19 <http://www.gnu.org/licenses/>. */
20 20
21 #include "config.h" 21 #include "config.h"
22 #define INCLUDE_UNIQUE_PTR
22 #include "system.h" 23 #include "system.h"
23 #include "coretypes.h" 24 #include "coretypes.h"
24 #include "cp-tree.h" 25 #include "cp-tree.h"
25 #include "timevar.h" 26 #include "timevar.h"
26 #include "stringpool.h" 27 #include "stringpool.h"
30 #include "c-family/c-pragma.h" 31 #include "c-family/c-pragma.h"
31 #include "params.h" 32 #include "params.h"
32 #include "gcc-rich-location.h" 33 #include "gcc-rich-location.h"
33 #include "spellcheck-tree.h" 34 #include "spellcheck-tree.h"
34 #include "parser.h" 35 #include "parser.h"
36 #include "c-family/name-hint.h"
37 #include "c-family/known-headers.h"
38 #include "c-family/c-spellcheck.h"
35 39
36 static cxx_binding *cxx_binding_make (tree value, tree type); 40 static cxx_binding *cxx_binding_make (tree value, tree type);
37 static cp_binding_level *innermost_nonclass_level (void); 41 static cp_binding_level *innermost_nonclass_level (void);
38 static void set_identifier_type_value_with_scope (tree id, tree decl, 42 static void set_identifier_type_value_with_scope (tree id, tree decl,
39 cp_binding_level *b); 43 cp_binding_level *b);
44 static bool maybe_suggest_missing_std_header (location_t location, tree name);
40 45
41 /* Create an overload suitable for recording an artificial TYPE_DECL 46 /* Create an overload suitable for recording an artificial TYPE_DECL
42 and another decl. We use this machanism to implement the struct 47 and another decl. We use this machanism to implement the struct
43 stat hack within a namespace. It'd be nice to use it everywhere. */ 48 stat hack within a namespace. It'd be nice to use it everywhere. */
44 49
137 find_local_binding (cp_binding_level *b, tree name) 142 find_local_binding (cp_binding_level *b, tree name)
138 { 143 {
139 if (cxx_binding *binding = IDENTIFIER_BINDING (name)) 144 if (cxx_binding *binding = IDENTIFIER_BINDING (name))
140 for (;; b = b->level_chain) 145 for (;; b = b->level_chain)
141 { 146 {
142 if (binding->scope == b 147 if (binding->scope == b)
143 && !(VAR_P (binding->value)
144 && DECL_DEAD_FOR_LOCAL (binding->value)))
145 return binding; 148 return binding;
146 149
147 /* Cleanup contours are transparent to the language. */ 150 /* Cleanup contours are transparent to the language. */
148 if (b->kind != sk_cleanup) 151 if (b->kind != sk_cleanup)
149 break; 152 break;
553 /* We've visited this scope before. Return what we found then. */ 556 /* We've visited this scope before. Return what we found then. */
554 return found_p (scope); 557 return found_p (scope);
555 558
556 /* Look in exactly namespace. */ 559 /* Look in exactly namespace. */
557 bool found = search_namespace_only (scope); 560 bool found = search_namespace_only (scope);
558 561
559 /* Recursively look in its inline children. */ 562 /* Don't look into inline children, if we're looking for an
560 if (vec<tree, va_gc> *inlinees = DECL_NAMESPACE_INLINEES (scope)) 563 anonymous name -- it must be in the current scope, if anywhere. */
561 for (unsigned ix = inlinees->length (); ix--;) 564 if (name)
562 found |= search_namespace ((*inlinees)[ix]); 565 /* Recursively look in its inline children. */
566 if (vec<tree, va_gc> *inlinees = DECL_NAMESPACE_INLINEES (scope))
567 for (unsigned ix = inlinees->length (); ix--;)
568 found |= search_namespace ((*inlinees)[ix]);
563 569
564 if (found) 570 if (found)
565 mark_found (scope); 571 mark_found (scope);
566 572
567 return found; 573 return found;
705 } 711 }
706 while (ix < queue->length ()); 712 while (ix < queue->length ());
707 done:; 713 done:;
708 if (scope == global_namespace) 714 if (scope == global_namespace)
709 break; 715 break;
716
717 /* If looking for hidden names, we only look in the innermost
718 namespace scope. [namespace.memdef]/3 If a friend
719 declaration in a non-local class first declares a class,
720 function, class template or function template the friend is a
721 member of the innermost enclosing namespace. See also
722 [basic.lookup.unqual]/7 */
723 if (flags & LOOKUP_HIDDEN)
724 break;
710 } 725 }
711 726
712 vec_safe_truncate (queue, length); 727 vec_safe_truncate (queue, length);
713 728
714 return found; 729 return found;
1131 1146
1132 static tree 1147 static tree
1133 member_vec_linear_search (vec<tree, va_gc> *member_vec, tree name) 1148 member_vec_linear_search (vec<tree, va_gc> *member_vec, tree name)
1134 { 1149 {
1135 for (int ix = member_vec->length (); ix--;) 1150 for (int ix = member_vec->length (); ix--;)
1136 /* We can get a NULL binding during insertion of a new method
1137 name, because the identifier_binding machinery performs a
1138 lookup. If we find such a NULL slot, that's the thing we were
1139 looking for, so we might as well bail out immediately. */
1140 if (tree binding = (*member_vec)[ix]) 1151 if (tree binding = (*member_vec)[ix])
1141 { 1152 if (OVL_NAME (binding) == name)
1142 if (OVL_NAME (binding) == name) 1153 return binding;
1143 return binding;
1144 }
1145 else
1146 break;
1147 1154
1148 return NULL_TREE; 1155 return NULL_TREE;
1149 } 1156 }
1150 1157
1151 /* Linear search of (partially ordered) fields of KLASS for NAME. */ 1158 /* Linear search of (partially ordered) fields of KLASS for NAME. */
1155 { 1162 {
1156 for (tree fields = TYPE_FIELDS (klass); fields; fields = DECL_CHAIN (fields)) 1163 for (tree fields = TYPE_FIELDS (klass); fields; fields = DECL_CHAIN (fields))
1157 { 1164 {
1158 tree decl = fields; 1165 tree decl = fields;
1159 1166
1160 if (!want_type 1167 if (TREE_CODE (decl) == FIELD_DECL
1161 && TREE_CODE (decl) == FIELD_DECL
1162 && ANON_AGGR_TYPE_P (TREE_TYPE (decl))) 1168 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
1163 { 1169 {
1164 tree anon = TREE_TYPE (decl); 1170 if (tree temp = search_anon_aggr (TREE_TYPE (decl), name, want_type))
1165 gcc_assert (COMPLETE_TYPE_P (anon)); 1171 return temp;
1166 tree temp;
1167
1168 if (vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (anon))
1169 temp = member_vec_linear_search (member_vec, name);
1170 else
1171 temp = fields_linear_search (anon, name, want_type);
1172
1173 if (temp)
1174 {
1175 /* Anon members can only contain fields. */
1176 gcc_assert (!STAT_HACK_P (temp) && !DECL_DECLARES_TYPE_P (temp));
1177 return temp;
1178 }
1179 } 1172 }
1180 1173
1181 if (DECL_NAME (decl) != name) 1174 if (DECL_NAME (decl) != name)
1182 continue; 1175 continue;
1183 1176
1195 if (!want_type || DECL_DECLARES_TYPE_P (decl)) 1188 if (!want_type || DECL_DECLARES_TYPE_P (decl))
1196 return decl; 1189 return decl;
1197 } 1190 }
1198 1191
1199 return NULL_TREE; 1192 return NULL_TREE;
1193 }
1194
1195 /* Look for NAME member inside of anonymous aggregate ANON. Although
1196 such things should only contain FIELD_DECLs, we check that too
1197 late, and would give very confusing errors if we weren't
1198 permissive here. */
1199
1200 tree
1201 search_anon_aggr (tree anon, tree name, bool want_type)
1202 {
1203 gcc_assert (COMPLETE_TYPE_P (anon));
1204 tree ret = get_class_binding_direct (anon, name, want_type);
1205 return ret;
1200 } 1206 }
1201 1207
1202 /* Look for NAME as an immediate member of KLASS (including 1208 /* Look for NAME as an immediate member of KLASS (including
1203 anon-members or unscoped enum member). TYPE_OR_FNS is zero for 1209 anon-members or unscoped enum member). TYPE_OR_FNS is zero for
1204 regular search. >0 to get a type binding (if there is one) and <0 1210 regular search. >0 to get a type binding (if there is one) and <0
1297 else if (IDENTIFIER_DTOR_P (name)) 1303 else if (IDENTIFIER_DTOR_P (name))
1298 { 1304 {
1299 if (CLASSTYPE_LAZY_DESTRUCTOR (klass)) 1305 if (CLASSTYPE_LAZY_DESTRUCTOR (klass))
1300 lazily_declare_fn (sfk_destructor, klass); 1306 lazily_declare_fn (sfk_destructor, klass);
1301 } 1307 }
1302 else if (name == cp_assignment_operator_id (NOP_EXPR)) 1308 else if (name == assign_op_identifier)
1303 { 1309 {
1304 if (CLASSTYPE_LAZY_COPY_ASSIGN (klass)) 1310 if (CLASSTYPE_LAZY_COPY_ASSIGN (klass))
1305 lazily_declare_fn (sfk_copy_assignment, klass); 1311 lazily_declare_fn (sfk_copy_assignment, klass);
1306 if (CLASSTYPE_LAZY_MOVE_ASSIGN (klass)) 1312 if (CLASSTYPE_LAZY_MOVE_ASSIGN (klass))
1307 lazily_declare_fn (sfk_move_assignment, klass); 1313 lazily_declare_fn (sfk_move_assignment, klass);
1310 1316
1311 return get_class_binding_direct (klass, name, type_or_fns); 1317 return get_class_binding_direct (klass, name, type_or_fns);
1312 } 1318 }
1313 1319
1314 /* Find the slot containing overloads called 'NAME'. If there is no 1320 /* Find the slot containing overloads called 'NAME'. If there is no
1315 such slot, create an empty one. KLASS might be complete at this 1321 such slot and the class is complete, create an empty one, at the
1316 point, in which case we need to preserve ordering. Deals with 1322 correct point in the sorted member vector. Otherwise return NULL.
1317 conv_op marker handling. */ 1323 Deals with conv_op marker handling. */
1318 1324
1319 tree * 1325 tree *
1320 get_member_slot (tree klass, tree name) 1326 find_member_slot (tree klass, tree name)
1321 { 1327 {
1322 bool complete_p = COMPLETE_TYPE_P (klass); 1328 bool complete_p = COMPLETE_TYPE_P (klass);
1323 1329
1324 vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (klass); 1330 vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (klass);
1325 if (!member_vec) 1331 if (!member_vec)
1326 { 1332 {
1327 vec_alloc (member_vec, 8); 1333 vec_alloc (member_vec, 8);
1328 CLASSTYPE_MEMBER_VEC (klass) = member_vec; 1334 CLASSTYPE_MEMBER_VEC (klass) = member_vec;
1365 1371
1366 if (complete_p && fn_name > name) 1372 if (complete_p && fn_name > name)
1367 break; 1373 break;
1368 } 1374 }
1369 1375
1370 /* No slot found. Create one at IX. We know in this case that our 1376 /* No slot found, add one if the class is complete. */
1371 caller will succeed in adding the function. */
1372 if (complete_p) 1377 if (complete_p)
1373 { 1378 {
1374 /* Do exact allocation when complete, as we don't expect to add 1379 /* Do exact allocation, as we don't expect to add many. */
1375 many. */ 1380 gcc_assert (name != conv_op_identifier);
1376 vec_safe_reserve_exact (member_vec, 1); 1381 vec_safe_reserve_exact (member_vec, 1);
1382 CLASSTYPE_MEMBER_VEC (klass) = member_vec;
1377 member_vec->quick_insert (ix, NULL_TREE); 1383 member_vec->quick_insert (ix, NULL_TREE);
1378 } 1384 return &(*member_vec)[ix];
1379 else 1385 }
1380 { 1386
1381 gcc_checking_assert (ix == length); 1387 return NULL;
1382 vec_safe_push (member_vec, NULL_TREE); 1388 }
1383 } 1389
1390 /* KLASS is an incomplete class to which we're adding a method NAME.
1391 Add a slot and deal with conv_op marker handling. */
1392
1393 tree *
1394 add_member_slot (tree klass, tree name)
1395 {
1396 gcc_assert (!COMPLETE_TYPE_P (klass));
1397
1398 vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (klass);
1399 vec_safe_push (member_vec, NULL_TREE);
1384 CLASSTYPE_MEMBER_VEC (klass) = member_vec; 1400 CLASSTYPE_MEMBER_VEC (klass) = member_vec;
1385 1401
1386 tree *slot = &(*member_vec)[ix]; 1402 tree *slot = &member_vec->last ();
1387 if (name == conv_op_identifier) 1403 if (IDENTIFIER_CONV_OP_P (name))
1388 { 1404 {
1389 /* Install the marker prefix. */ 1405 /* Install the marker prefix. */
1390 *slot = ovl_make (conv_op_marker, NULL_TREE); 1406 *slot = ovl_make (conv_op_marker, NULL_TREE);
1391 slot = &OVL_CHAIN (*slot); 1407 slot = &OVL_CHAIN (*slot);
1392 } 1408 }
1455 1471
1456 We should not be doing #1, but in either case it doesn't matter 1472 We should not be doing #1, but in either case it doesn't matter
1457 how we order these. Use UID as a proxy for source ordering, so 1473 how we order these. Use UID as a proxy for source ordering, so
1458 that identically-located decls still have a well-defined stable 1474 that identically-located decls still have a well-defined stable
1459 ordering. */ 1475 ordering. */
1460 return DECL_UID (a) < DECL_UID (b) ? -1 : +1; 1476 if (DECL_UID (a) != DECL_UID (b))
1477 return DECL_UID (a) < DECL_UID (b) ? -1 : +1;
1478 gcc_assert (a == b);
1479 return 0;
1461 } 1480 }
1462 1481
1463 static struct { 1482 static struct {
1464 gt_pointer_operator new_value; 1483 gt_pointer_operator new_value;
1465 void *cookie; 1484 void *cookie;
1493 { 1512 {
1494 if (vec<tree, va_gc> *member_vec = (vec<tree, va_gc> *) obj) 1513 if (vec<tree, va_gc> *member_vec = (vec<tree, va_gc> *) obj)
1495 { 1514 {
1496 resort_data.new_value = new_value; 1515 resort_data.new_value = new_value;
1497 resort_data.cookie = cookie; 1516 resort_data.cookie = cookie;
1498 qsort (member_vec->address (), member_vec->length (), 1517 member_vec->qsort (resort_member_name_cmp);
1499 sizeof (tree), resort_member_name_cmp);
1500 } 1518 }
1501 } 1519 }
1502 1520
1503 /* Recursively count the number of fields in KLASS, including anonymous 1521 /* Recursively count the number of fields in KLASS, including anonymous
1504 union members. */ 1522 union members. */
1570 member_vec_dedup (vec<tree, va_gc> *member_vec) 1588 member_vec_dedup (vec<tree, va_gc> *member_vec)
1571 { 1589 {
1572 unsigned len = member_vec->length (); 1590 unsigned len = member_vec->length ();
1573 unsigned store = 0; 1591 unsigned store = 0;
1574 1592
1575 tree current = (*member_vec)[0], name = OVL_NAME (current); 1593 if (!len)
1576 tree next = NULL_TREE, next_name = NULL_TREE; 1594 return;
1577 for (unsigned jx, ix = 0; ix < len; 1595
1578 ix = jx, current = next, name = next_name) 1596 tree name = OVL_NAME ((*member_vec)[0]);
1579 { 1597 for (unsigned jx, ix = 0; ix < len; ix = jx)
1598 {
1599 tree current = NULL_TREE;
1580 tree to_type = NULL_TREE; 1600 tree to_type = NULL_TREE;
1581 tree to_using = NULL_TREE; 1601 tree to_using = NULL_TREE;
1582 tree marker = NULL_TREE; 1602 tree marker = NULL_TREE;
1583 if (IDENTIFIER_CONV_OP_P (name)) 1603
1584 { 1604 for (jx = ix; jx < len; jx++)
1585 marker = current; 1605 {
1586 current = OVL_CHAIN (current); 1606 tree next = (*member_vec)[jx];
1587 name = DECL_NAME (OVL_FUNCTION (marker)); 1607 if (jx != ix)
1588 gcc_checking_assert (name == conv_op_identifier);
1589 }
1590
1591 if (TREE_CODE (current) == USING_DECL)
1592 {
1593 current = strip_using_decl (current);
1594 if (is_overloaded_fn (current))
1595 current = NULL_TREE;
1596 else if (TREE_CODE (current) == USING_DECL)
1597 { 1608 {
1598 to_using = current; 1609 tree next_name = OVL_NAME (next);
1599 current = NULL_TREE; 1610 if (next_name != name)
1611 {
1612 name = next_name;
1613 break;
1614 }
1600 } 1615 }
1601 } 1616
1602 1617 if (IDENTIFIER_CONV_OP_P (name))
1603 if (current && DECL_DECLARES_TYPE_P (current))
1604 {
1605 to_type = current;
1606 current = NULL_TREE;
1607 }
1608
1609 for (jx = ix + 1; jx < len; jx++)
1610 {
1611 next = (*member_vec)[jx];
1612 next_name = OVL_NAME (next);
1613 if (next_name != name)
1614 break;
1615
1616 if (marker)
1617 { 1618 {
1618 gcc_checking_assert (OVL_FUNCTION (marker) 1619 marker = next;
1619 == OVL_FUNCTION (next));
1620 next = OVL_CHAIN (next); 1620 next = OVL_CHAIN (next);
1621 } 1621 }
1622 1622
1623 if (TREE_CODE (next) == USING_DECL) 1623 if (TREE_CODE (next) == USING_DECL)
1624 { 1624 {
1625 if (IDENTIFIER_CTOR_P (name))
1626 /* Dependent inherited ctor. */
1627 continue;
1628
1625 next = strip_using_decl (next); 1629 next = strip_using_decl (next);
1626 if (is_overloaded_fn (next)) 1630 if (TREE_CODE (next) == USING_DECL)
1627 next = NULL_TREE;
1628 else if (TREE_CODE (next) == USING_DECL)
1629 { 1631 {
1630 to_using = next; 1632 to_using = next;
1631 next = NULL_TREE; 1633 continue;
1632 } 1634 }
1635
1636 if (is_overloaded_fn (next))
1637 continue;
1633 } 1638 }
1634 1639
1635 if (next && DECL_DECLARES_TYPE_P (next)) 1640 if (DECL_DECLARES_TYPE_P (next))
1636 to_type = next; 1641 {
1642 to_type = next;
1643 continue;
1644 }
1645
1646 if (!current)
1647 current = next;
1637 } 1648 }
1638 1649
1639 if (to_using) 1650 if (to_using)
1640 { 1651 {
1641 if (!current) 1652 if (!current)
1650 current = to_type; 1661 current = to_type;
1651 else 1662 else
1652 current = stat_hack (current, to_type); 1663 current = stat_hack (current, to_type);
1653 } 1664 }
1654 1665
1655 gcc_assert (current); 1666 if (current)
1656 if (marker) 1667 {
1657 { 1668 if (marker)
1658 OVL_CHAIN (marker) = current; 1669 {
1659 current = marker; 1670 OVL_CHAIN (marker) = current;
1660 } 1671 current = marker;
1661 (*member_vec)[store++] = current; 1672 }
1673 (*member_vec)[store++] = current;
1674 }
1662 } 1675 }
1663 1676
1664 while (store++ < len) 1677 while (store++ < len)
1665 member_vec->pop (); 1678 member_vec->pop ();
1666 } 1679 }
1685 } 1698 }
1686 1699
1687 if (member_vec) 1700 if (member_vec)
1688 { 1701 {
1689 CLASSTYPE_MEMBER_VEC (klass) = member_vec; 1702 CLASSTYPE_MEMBER_VEC (klass) = member_vec;
1690 qsort (member_vec->address (), member_vec->length (), 1703 member_vec->qsort (member_name_cmp);
1691 sizeof (tree), member_name_cmp);
1692 member_vec_dedup (member_vec); 1704 member_vec_dedup (member_vec);
1693 } 1705 }
1694 } 1706 }
1695 1707
1696 /* Insert lately defined enum ENUMTYPE into KLASS for the sorted case. */ 1708 /* Insert lately defined enum ENUMTYPE into KLASS for the sorted case. */
1714 if (CLASSTYPE_MEMBER_VEC (klass)) 1726 if (CLASSTYPE_MEMBER_VEC (klass))
1715 member_vec_append_enum_values (member_vec, enumtype); 1727 member_vec_append_enum_values (member_vec, enumtype);
1716 else 1728 else
1717 member_vec_append_class_fields (member_vec, klass); 1729 member_vec_append_class_fields (member_vec, klass);
1718 CLASSTYPE_MEMBER_VEC (klass) = member_vec; 1730 CLASSTYPE_MEMBER_VEC (klass) = member_vec;
1719 qsort (member_vec->address (), member_vec->length (), 1731 member_vec->qsort (member_name_cmp);
1720 sizeof (tree), member_name_cmp);
1721 member_vec_dedup (member_vec); 1732 member_vec_dedup (member_vec);
1722 } 1733 }
1723 } 1734 }
1724 1735
1725 /* Compute the chain index of a binding_entry given the HASH value of its 1736 /* Compute the chain index of a binding_entry given the HASH value of its
2470 } 2481 }
2471 2482
2472 done: 2483 done:
2473 if (to_val) 2484 if (to_val)
2474 { 2485 {
2475 if (level->kind != sk_namespace 2486 if (level->kind == sk_namespace || to_type == decl || to_val == decl)
2476 && !to_type && binding->value && OVL_P (to_val)) 2487 add_decl_to_level (level, decl);
2477 update_local_overload (binding, to_val);
2478 else 2488 else
2479 { 2489 {
2480 tree to_add = to_val; 2490 gcc_checking_assert (binding->value && OVL_P (binding->value));
2481 2491 update_local_overload (binding, to_val);
2482 if (level->kind == sk_namespace)
2483 to_add = decl;
2484 else if (to_type == decl)
2485 to_add = decl;
2486 else if (TREE_CODE (to_add) == OVERLOAD)
2487 to_add = build_tree_list (NULL_TREE, to_add);
2488
2489 add_decl_to_level (level, to_add);
2490 } 2492 }
2491 2493
2492 if (slot) 2494 if (slot)
2493 { 2495 {
2494 if (STAT_HACK_P (*slot)) 2496 if (STAT_HACK_P (*slot))
2522 static void 2524 static void
2523 check_extern_c_conflict (tree decl) 2525 check_extern_c_conflict (tree decl)
2524 { 2526 {
2525 /* Ignore artificial or system header decls. */ 2527 /* Ignore artificial or system header decls. */
2526 if (DECL_ARTIFICIAL (decl) || DECL_IN_SYSTEM_HEADER (decl)) 2528 if (DECL_ARTIFICIAL (decl) || DECL_IN_SYSTEM_HEADER (decl))
2529 return;
2530
2531 /* This only applies to decls at namespace scope. */
2532 if (!DECL_NAMESPACE_SCOPE_P (decl))
2527 return; 2533 return;
2528 2534
2529 if (!extern_c_decls) 2535 if (!extern_c_decls)
2530 extern_c_decls = hash_table<named_decl_hash>::create_ggc (127); 2536 extern_c_decls = hash_table<named_decl_hash>::create_ggc (127);
2531 2537
2551 else if (DECL_ASSEMBLER_NAME_SET_P (old)) 2557 else if (DECL_ASSEMBLER_NAME_SET_P (old))
2552 SET_DECL_ASSEMBLER_NAME (decl, DECL_ASSEMBLER_NAME (old)); 2558 SET_DECL_ASSEMBLER_NAME (decl, DECL_ASSEMBLER_NAME (old));
2553 2559
2554 if (mismatch) 2560 if (mismatch)
2555 { 2561 {
2562 auto_diagnostic_group d;
2556 pedwarn (input_location, 0, 2563 pedwarn (input_location, 0,
2557 "conflicting C language linkage declaration %q#D", decl); 2564 "conflicting C language linkage declaration %q#D", decl);
2558 inform (DECL_SOURCE_LOCATION (old), 2565 inform (DECL_SOURCE_LOCATION (old),
2559 "previous declaration %q#D", old); 2566 "previous declaration %q#D", old);
2560 if (mismatch < 0) 2567 if (mismatch < 0)
2599 } 2606 }
2600 2607
2601 return NULL_TREE; 2608 return NULL_TREE;
2602 } 2609 }
2603 2610
2611 /* Subroutine of check_local_shadow. */
2612
2613 static void
2614 inform_shadowed (tree shadowed)
2615 {
2616 inform (DECL_SOURCE_LOCATION (shadowed),
2617 "shadowed declaration is here");
2618 }
2619
2604 /* DECL is being declared at a local scope. Emit suitable shadow 2620 /* DECL is being declared at a local scope. Emit suitable shadow
2605 warnings. */ 2621 warnings. */
2606 2622
2607 static void 2623 static void
2608 check_local_shadow (tree decl) 2624 check_local_shadow (tree decl)
2610 /* Don't complain about the parms we push and then pop 2626 /* Don't complain about the parms we push and then pop
2611 while tentatively parsing a function declarator. */ 2627 while tentatively parsing a function declarator. */
2612 if (TREE_CODE (decl) == PARM_DECL && !DECL_CONTEXT (decl)) 2628 if (TREE_CODE (decl) == PARM_DECL && !DECL_CONTEXT (decl))
2613 return; 2629 return;
2614 2630
2615 /* Inline decls shadow nothing. */
2616 if (DECL_FROM_INLINE (decl))
2617 return;
2618
2619 /* External decls are something else. */ 2631 /* External decls are something else. */
2620 if (DECL_EXTERNAL (decl)) 2632 if (DECL_EXTERNAL (decl))
2621 return; 2633 return;
2622 2634
2623 tree old = NULL_TREE; 2635 tree old = NULL_TREE;
2625 if (cxx_binding *binding = outer_binding (DECL_NAME (decl), NULL, true)) 2637 if (cxx_binding *binding = outer_binding (DECL_NAME (decl), NULL, true))
2626 { 2638 {
2627 old = binding->value; 2639 old = binding->value;
2628 old_scope = binding->scope; 2640 old_scope = binding->scope;
2629 } 2641 }
2630 while (old && VAR_P (old) && DECL_DEAD_FOR_LOCAL (old)) 2642
2631 old = DECL_SHADOWED_FOR_VAR (old);
2632
2633 tree shadowed = NULL_TREE;
2634 if (old 2643 if (old
2635 && (TREE_CODE (old) == PARM_DECL 2644 && (TREE_CODE (old) == PARM_DECL
2636 || VAR_P (old) 2645 || VAR_P (old)
2637 || (TREE_CODE (old) == TYPE_DECL 2646 || (TREE_CODE (old) == TYPE_DECL
2638 && (!DECL_ARTIFICIAL (old) 2647 && (!DECL_ARTIFICIAL (old)
2639 || TREE_CODE (decl) == TYPE_DECL))) 2648 || TREE_CODE (decl) == TYPE_DECL)))
2649 && DECL_FUNCTION_SCOPE_P (old)
2640 && (!DECL_ARTIFICIAL (decl) 2650 && (!DECL_ARTIFICIAL (decl)
2651 || is_capture_proxy (decl)
2641 || DECL_IMPLICIT_TYPEDEF_P (decl) 2652 || DECL_IMPLICIT_TYPEDEF_P (decl)
2642 || (VAR_P (decl) && DECL_ANON_UNION_VAR_P (decl)))) 2653 || (VAR_P (decl) && DECL_ANON_UNION_VAR_P (decl))))
2643 { 2654 {
2644 /* DECL shadows a local thing possibly of interest. */ 2655 /* DECL shadows a local thing possibly of interest. */
2645 2656
2657 /* DR 2211: check that captures and parameters
2658 do not have the same name. */
2659 if (is_capture_proxy (decl))
2660 {
2661 if (current_lambda_expr ()
2662 && DECL_CONTEXT (old) == lambda_function (current_lambda_expr ())
2663 && TREE_CODE (old) == PARM_DECL
2664 && DECL_NAME (decl) != this_identifier)
2665 {
2666 error_at (DECL_SOURCE_LOCATION (old),
2667 "lambda parameter %qD "
2668 "previously declared as a capture", old);
2669 }
2670 return;
2671 }
2646 /* Don't complain if it's from an enclosing function. */ 2672 /* Don't complain if it's from an enclosing function. */
2647 if (DECL_CONTEXT (old) == current_function_decl 2673 else if (DECL_CONTEXT (old) == current_function_decl
2648 && TREE_CODE (decl) != PARM_DECL 2674 && TREE_CODE (decl) != PARM_DECL
2649 && TREE_CODE (old) == PARM_DECL) 2675 && TREE_CODE (old) == PARM_DECL)
2650 { 2676 {
2651 /* Go to where the parms should be and see if we find 2677 /* Go to where the parms should be and see if we find
2652 them there. */ 2678 them there. */
2682 detected elsewhere. */ 2708 detected elsewhere. */
2683 else if (VAR_P (old) 2709 else if (VAR_P (old)
2684 && old_scope == current_binding_level->level_chain 2710 && old_scope == current_binding_level->level_chain
2685 && (old_scope->kind == sk_cond || old_scope->kind == sk_for)) 2711 && (old_scope->kind == sk_cond || old_scope->kind == sk_for))
2686 { 2712 {
2713 auto_diagnostic_group d;
2687 error ("redeclaration of %q#D", decl); 2714 error ("redeclaration of %q#D", decl);
2688 inform (DECL_SOURCE_LOCATION (old), 2715 inform (DECL_SOURCE_LOCATION (old),
2689 "%q#D previously declared here", old); 2716 "%q#D previously declared here", old);
2690 return; 2717 return;
2691 } 2718 }
2704 || (TREE_CODE (old) == PARM_DECL 2731 || (TREE_CODE (old) == PARM_DECL
2705 && (current_binding_level->kind == sk_catch 2732 && (current_binding_level->kind == sk_catch
2706 || current_binding_level->level_chain->kind == sk_catch) 2733 || current_binding_level->level_chain->kind == sk_catch)
2707 && in_function_try_handler)) 2734 && in_function_try_handler))
2708 { 2735 {
2736 auto_diagnostic_group d;
2709 if (permerror (input_location, "redeclaration of %q#D", decl)) 2737 if (permerror (input_location, "redeclaration of %q#D", decl))
2710 inform (DECL_SOURCE_LOCATION (old), 2738 inform (DECL_SOURCE_LOCATION (old),
2711 "%q#D previously declared here", old); 2739 "%q#D previously declared here", old);
2712 return; 2740 return;
2713 } 2741 }
2730 warning_code = OPT_Wshadow_local; 2758 warning_code = OPT_Wshadow_local;
2731 else if (warn_shadow_compatible_local 2759 else if (warn_shadow_compatible_local
2732 && (same_type_p (TREE_TYPE (old), TREE_TYPE (decl)) 2760 && (same_type_p (TREE_TYPE (old), TREE_TYPE (decl))
2733 || (!dependent_type_p (TREE_TYPE (decl)) 2761 || (!dependent_type_p (TREE_TYPE (decl))
2734 && !dependent_type_p (TREE_TYPE (old)) 2762 && !dependent_type_p (TREE_TYPE (old))
2763 /* If the new decl uses auto, we don't yet know
2764 its type (the old type cannot be using auto
2765 at this point, without also being
2766 dependent). This is an indication we're
2767 (now) doing the shadow checking too
2768 early. */
2769 && !type_uses_auto (TREE_TYPE (decl))
2735 && can_convert (TREE_TYPE (old), TREE_TYPE (decl), 2770 && can_convert (TREE_TYPE (old), TREE_TYPE (decl),
2736 tf_none)))) 2771 tf_none))))
2737 warning_code = OPT_Wshadow_compatible_local; 2772 warning_code = OPT_Wshadow_compatible_local;
2738 else 2773 else
2739 return; 2774 return;
2744 else if (is_capture_proxy (old)) 2779 else if (is_capture_proxy (old))
2745 msg = "declaration of %qD shadows a lambda capture"; 2780 msg = "declaration of %qD shadows a lambda capture";
2746 else 2781 else
2747 msg = "declaration of %qD shadows a previous local"; 2782 msg = "declaration of %qD shadows a previous local";
2748 2783
2784 auto_diagnostic_group d;
2749 if (warning_at (input_location, warning_code, msg, decl)) 2785 if (warning_at (input_location, warning_code, msg, decl))
2750 { 2786 inform_shadowed (old);
2751 shadowed = old;
2752 goto inform_shadowed;
2753 }
2754 return; 2787 return;
2755 } 2788 }
2756 2789
2757 if (!warn_shadow) 2790 if (!warn_shadow)
2758 return; 2791 return;
2773 if (!OVL_P (member) 2806 if (!OVL_P (member)
2774 || TREE_CODE (decl) == FUNCTION_DECL 2807 || TREE_CODE (decl) == FUNCTION_DECL
2775 || TYPE_PTRFN_P (TREE_TYPE (decl)) 2808 || TYPE_PTRFN_P (TREE_TYPE (decl))
2776 || TYPE_PTRMEMFUNC_P (TREE_TYPE (decl))) 2809 || TYPE_PTRMEMFUNC_P (TREE_TYPE (decl)))
2777 { 2810 {
2811 auto_diagnostic_group d;
2778 if (warning_at (input_location, OPT_Wshadow, 2812 if (warning_at (input_location, OPT_Wshadow,
2779 "declaration of %qD shadows a member of %qT", 2813 "declaration of %qD shadows a member of %qT",
2780 decl, current_nonlambda_class_type ()) 2814 decl, current_nonlambda_class_type ())
2781 && DECL_P (member)) 2815 && DECL_P (member))
2782 { 2816 inform_shadowed (member);
2783 shadowed = member;
2784 goto inform_shadowed;
2785 }
2786 } 2817 }
2787 return; 2818 return;
2788 } 2819 }
2789 2820
2790 /* Now look for a namespace shadow. */ 2821 /* Now look for a namespace shadow. */
2795 && (!DECL_ARTIFICIAL (old) 2826 && (!DECL_ARTIFICIAL (old)
2796 || TREE_CODE (decl) == TYPE_DECL))) 2827 || TREE_CODE (decl) == TYPE_DECL)))
2797 && !instantiating_current_function_p ()) 2828 && !instantiating_current_function_p ())
2798 /* XXX shadow warnings in outer-more namespaces */ 2829 /* XXX shadow warnings in outer-more namespaces */
2799 { 2830 {
2831 auto_diagnostic_group d;
2800 if (warning_at (input_location, OPT_Wshadow, 2832 if (warning_at (input_location, OPT_Wshadow,
2801 "declaration of %qD shadows a global declaration", 2833 "declaration of %qD shadows a global declaration",
2802 decl)) 2834 decl))
2803 { 2835 inform_shadowed (old);
2804 shadowed = old;
2805 goto inform_shadowed;
2806 }
2807 return; 2836 return;
2808 } 2837 }
2809 2838
2810 return; 2839 return;
2811
2812 inform_shadowed:
2813 inform (DECL_SOURCE_LOCATION (shadowed), "shadowed declaration is here");
2814 } 2840 }
2815 2841
2816 /* DECL is being pushed inside function CTX. Set its context, if 2842 /* DECL is being pushed inside function CTX. Set its context, if
2817 needed. */ 2843 needed. */
2818 2844
2867 { 2893 {
2868 ns_value 2894 ns_value
2869 = find_namespace_value (current_namespace, DECL_NAME (decl)); 2895 = find_namespace_value (current_namespace, DECL_NAME (decl));
2870 loc_value = ns_value; 2896 loc_value = ns_value;
2871 } 2897 }
2872 if (loc_value == error_mark_node) 2898 if (loc_value == error_mark_node
2899 /* An ambiguous lookup. */
2900 || (loc_value && TREE_CODE (loc_value) == TREE_LIST))
2873 loc_value = NULL_TREE; 2901 loc_value = NULL_TREE;
2874 2902
2875 for (ovl_iterator iter (loc_value); iter; ++iter) 2903 for (ovl_iterator iter (loc_value); iter; ++iter)
2876 if (!iter.hidden_p () 2904 if (!iter.hidden_p ()
2877 && (TREE_STATIC (*iter) || DECL_EXTERNAL (*iter)) 2905 && (TREE_STATIC (*iter) || DECL_EXTERNAL (*iter))
2915 2943
2916 /* Avoid repeating a lookup. */ 2944 /* Avoid repeating a lookup. */
2917 if (ns_value == decl) 2945 if (ns_value == decl)
2918 ns_value = find_namespace_value (current_namespace, DECL_NAME (decl)); 2946 ns_value = find_namespace_value (current_namespace, DECL_NAME (decl));
2919 2947
2920 if (ns_value == error_mark_node) 2948 if (ns_value == error_mark_node
2949 || (ns_value && TREE_CODE (ns_value) == TREE_LIST))
2921 ns_value = NULL_TREE; 2950 ns_value = NULL_TREE;
2922 2951
2923 for (ovl_iterator iter (ns_value); iter; ++iter) 2952 for (ovl_iterator iter (ns_value); iter; ++iter)
2924 { 2953 {
2925 tree other = *iter; 2954 tree other = *iter;
2931 else if (TREE_CODE (other) != TREE_CODE (decl) 2960 else if (TREE_CODE (other) != TREE_CODE (decl)
2932 || ((VAR_P (decl) || matching_fn_p (other, decl)) 2961 || ((VAR_P (decl) || matching_fn_p (other, decl))
2933 && !comptypes (TREE_TYPE (decl), TREE_TYPE (other), 2962 && !comptypes (TREE_TYPE (decl), TREE_TYPE (other),
2934 COMPARE_REDECLARATION))) 2963 COMPARE_REDECLARATION)))
2935 { 2964 {
2965 auto_diagnostic_group d;
2936 if (permerror (DECL_SOURCE_LOCATION (decl), 2966 if (permerror (DECL_SOURCE_LOCATION (decl),
2937 "local external declaration %q#D", decl)) 2967 "local external declaration %q#D", decl))
2938 inform (DECL_SOURCE_LOCATION (other), 2968 inform (DECL_SOURCE_LOCATION (other),
2939 "does not match previous declaration %q#D", other); 2969 "does not match previous declaration %q#D", other);
2940 break; 2970 break;
3052 check_default_args (decl); 3082 check_default_args (decl);
3053 3083
3054 if (is_friend) 3084 if (is_friend)
3055 { 3085 {
3056 if (level->kind != sk_namespace) 3086 if (level->kind != sk_namespace)
3057 /* In a local class, a friend function declaration must 3087 {
3058 find a matching decl in the innermost non-class scope. 3088 /* In a local class, a friend function declaration must
3059 [class.friend/11] */ 3089 find a matching decl in the innermost non-class scope.
3060 error ("friend declaration %qD in local class without " 3090 [class.friend/11] */
3061 "prior local declaration", decl); 3091 error ("friend declaration %qD in local class without "
3062 else if (!flag_friend_injection) 3092 "prior local declaration", decl);
3063 /* Hide it from ordinary lookup. */ 3093 /* Don't attempt to push it. */
3064 DECL_ANTICIPATED (decl) = DECL_HIDDEN_FRIEND_P (decl) = true; 3094 return error_mark_node;
3095 }
3096 /* Hide it from ordinary lookup. */
3097 DECL_ANTICIPATED (decl) = DECL_HIDDEN_FRIEND_P (decl) = true;
3065 } 3098 }
3066 } 3099 }
3067 3100
3068 if (level->kind != sk_namespace) 3101 if (level->kind != sk_namespace)
3069 { 3102 {
3199 /* And put DECL on the list of things declared by the current 3232 /* And put DECL on the list of things declared by the current
3200 binding level. */ 3233 binding level. */
3201 add_decl_to_level (b, decl); 3234 add_decl_to_level (b, decl);
3202 } 3235 }
3203 3236
3204 /* Check to see whether or not DECL is a variable that would have been
3205 in scope under the ARM, but is not in scope under the ANSI/ISO
3206 standard. If so, issue an error message. If name lookup would
3207 work in both cases, but return a different result, this function
3208 returns the result of ANSI/ISO lookup. Otherwise, it returns
3209 DECL. */
3210
3211 tree
3212 check_for_out_of_scope_variable (tree decl)
3213 {
3214 tree shadowed;
3215
3216 /* We only care about out of scope variables. */
3217 if (!(VAR_P (decl) && DECL_DEAD_FOR_LOCAL (decl)))
3218 return decl;
3219
3220 shadowed = DECL_HAS_SHADOWED_FOR_VAR_P (decl)
3221 ? DECL_SHADOWED_FOR_VAR (decl) : NULL_TREE ;
3222 while (shadowed != NULL_TREE && VAR_P (shadowed)
3223 && DECL_DEAD_FOR_LOCAL (shadowed))
3224 shadowed = DECL_HAS_SHADOWED_FOR_VAR_P (shadowed)
3225 ? DECL_SHADOWED_FOR_VAR (shadowed) : NULL_TREE;
3226 if (!shadowed)
3227 shadowed = find_namespace_value (current_namespace, DECL_NAME (decl));
3228 if (shadowed)
3229 {
3230 if (!DECL_ERROR_REPORTED (decl))
3231 {
3232 warning (0, "name lookup of %qD changed", DECL_NAME (decl));
3233 warning_at (DECL_SOURCE_LOCATION (shadowed), 0,
3234 " matches this %qD under ISO standard rules",
3235 shadowed);
3236 warning_at (DECL_SOURCE_LOCATION (decl), 0,
3237 " matches this %qD under old rules", decl);
3238 DECL_ERROR_REPORTED (decl) = 1;
3239 }
3240 return shadowed;
3241 }
3242
3243 /* If we have already complained about this declaration, there's no
3244 need to do it again. */
3245 if (DECL_ERROR_REPORTED (decl))
3246 return decl;
3247
3248 DECL_ERROR_REPORTED (decl) = 1;
3249
3250 if (TREE_TYPE (decl) == error_mark_node)
3251 return decl;
3252
3253 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
3254 {
3255 error ("name lookup of %qD changed for ISO %<for%> scoping",
3256 DECL_NAME (decl));
3257 error (" cannot use obsolete binding at %q+D because "
3258 "it has a destructor", decl);
3259 return error_mark_node;
3260 }
3261 else
3262 {
3263 permerror (input_location, "name lookup of %qD changed for ISO %<for%> scoping",
3264 DECL_NAME (decl));
3265 if (flag_permissive)
3266 permerror (DECL_SOURCE_LOCATION (decl),
3267 " using obsolete binding at %qD", decl);
3268 else
3269 {
3270 static bool hint;
3271 if (!hint)
3272 {
3273 inform (input_location, "(if you use %<-fpermissive%> G++ will accept your code)");
3274 hint = true;
3275 }
3276 }
3277 }
3278
3279 return decl;
3280 }
3281 3237
3282 /* true means unconditionally make a BLOCK for the next level pushed. */ 3238 /* true means unconditionally make a BLOCK for the next level pushed. */
3283 3239
3284 static bool keep_next_level_flag; 3240 static bool keep_next_level_flag;
3285 3241
3327 if (scope->this_entity) 3283 if (scope->this_entity)
3328 verbatim ("%s %<%s(%E)%> %p %d\n", action, desc, 3284 verbatim ("%s %<%s(%E)%> %p %d\n", action, desc,
3329 scope->this_entity, (void *) scope, line); 3285 scope->this_entity, (void *) scope, line);
3330 else 3286 else
3331 verbatim ("%s %s %p %d\n", action, desc, (void *) scope, line); 3287 verbatim ("%s %s %p %d\n", action, desc, (void *) scope, line);
3332 }
3333
3334 /* Return the estimated initial size of the hashtable of a NAMESPACE
3335 scope. */
3336
3337 static inline size_t
3338 namespace_scope_ht_size (tree ns)
3339 {
3340 tree name = DECL_NAME (ns);
3341
3342 return name == std_identifier
3343 ? NAMESPACE_STD_HT_SIZE
3344 : (name == global_identifier
3345 ? GLOBAL_SCOPE_HT_SIZE
3346 : NAMESPACE_ORDINARY_HT_SIZE);
3347 } 3288 }
3348 3289
3349 /* A chain of binding_level structures awaiting reuse. */ 3290 /* A chain of binding_level structures awaiting reuse. */
3350 3291
3351 static GTY((deletable)) cp_binding_level *free_binding_level; 3292 static GTY((deletable)) cp_binding_level *free_binding_level;
3725 else 3666 else
3726 fprintf (stderr, "<nil>\n"); 3667 fprintf (stderr, "<nil>\n");
3727 } 3668 }
3728 3669
3729 3670
3730 void 3671 static void
3731 print_other_binding_stack (cp_binding_level *stack) 3672 print_other_binding_stack (cp_binding_level *stack)
3732 { 3673 {
3733 cp_binding_level *level; 3674 cp_binding_level *level;
3734 for (level = stack; !global_scope_p (level); level = level->level_chain) 3675 for (level = stack; !global_scope_p (level); level = level->level_chain)
3735 { 3676 {
3943 3884
3944 static tree 3885 static tree
3945 do_pushdecl_with_scope (tree x, cp_binding_level *level, bool is_friend) 3886 do_pushdecl_with_scope (tree x, cp_binding_level *level, bool is_friend)
3946 { 3887 {
3947 cp_binding_level *b; 3888 cp_binding_level *b;
3948 tree function_decl = current_function_decl; 3889
3949
3950 current_function_decl = NULL_TREE;
3951 if (level->kind == sk_class) 3890 if (level->kind == sk_class)
3952 { 3891 {
3953 b = class_binding_level; 3892 b = class_binding_level;
3954 class_binding_level = level; 3893 class_binding_level = level;
3955 pushdecl_class_level (x); 3894 pushdecl_class_level (x);
3956 class_binding_level = b; 3895 class_binding_level = b;
3957 } 3896 }
3958 else 3897 else
3959 { 3898 {
3899 tree function_decl = current_function_decl;
3900 if (level->kind == sk_namespace)
3901 current_function_decl = NULL_TREE;
3960 b = current_binding_level; 3902 b = current_binding_level;
3961 current_binding_level = level; 3903 current_binding_level = level;
3962 x = pushdecl (x, is_friend); 3904 x = pushdecl (x, is_friend);
3963 current_binding_level = b; 3905 current_binding_level = b;
3964 } 3906 current_function_decl = function_decl;
3965 current_function_decl = function_decl; 3907 }
3966 return x; 3908 return x;
3967 } 3909 }
3968 3910
3969 /* Inject X into the local scope just before the function parms. */ 3911 /* Inject X into the local scope just before the function parms. */
3970 3912
4464 else if (ANON_AGGR_TYPE_P (TREE_TYPE (x))) 4406 else if (ANON_AGGR_TYPE_P (TREE_TYPE (x)))
4465 { 4407 {
4466 /* If X is an anonymous aggregate, all of its members are 4408 /* If X is an anonymous aggregate, all of its members are
4467 treated as if they were members of the class containing the 4409 treated as if they were members of the class containing the
4468 aggregate, for naming purposes. */ 4410 aggregate, for naming purposes. */
4469 tree f; 4411 location_t save_location = input_location;
4470 4412 tree anon = TREE_TYPE (x);
4471 for (f = TYPE_FIELDS (TREE_TYPE (x)); f; f = DECL_CHAIN (f)) 4413 if (vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (anon))
4472 { 4414 for (unsigned ix = member_vec->length (); ix--;)
4473 location_t save_location = input_location; 4415 {
4474 input_location = DECL_SOURCE_LOCATION (f); 4416 tree binding = (*member_vec)[ix];
4475 if (!pushdecl_class_level (f)) 4417 if (STAT_HACK_P (binding))
4476 is_valid = false; 4418 {
4477 input_location = save_location; 4419 if (!pushdecl_class_level (STAT_TYPE (binding)))
4478 } 4420 is_valid = false;
4421 binding = STAT_DECL (binding);
4422 }
4423 if (!pushdecl_class_level (binding))
4424 is_valid = false;
4425 }
4426 else
4427 for (tree f = TYPE_FIELDS (anon); f; f = DECL_CHAIN (f))
4428 if (TREE_CODE (f) == FIELD_DECL)
4429 {
4430 input_location = DECL_SOURCE_LOCATION (f);
4431 if (!pushdecl_class_level (f))
4432 is_valid = false;
4433 }
4434 input_location = save_location;
4479 } 4435 }
4480 timevar_cond_stop (TV_NAME_LOOKUP, subtime); 4436 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
4481 return is_valid; 4437 return is_valid;
4482 } 4438 }
4483 4439
5012 bool 4968 bool
5013 handle_namespace_attrs (tree ns, tree attributes) 4969 handle_namespace_attrs (tree ns, tree attributes)
5014 { 4970 {
5015 tree d; 4971 tree d;
5016 bool saw_vis = false; 4972 bool saw_vis = false;
4973
4974 if (attributes == error_mark_node)
4975 return false;
5017 4976
5018 for (d = attributes; d; d = TREE_CHAIN (d)) 4977 for (d = attributes; d; d = TREE_CHAIN (d))
5019 { 4978 {
5020 tree name = get_attribute_name (d); 4979 tree name = get_attribute_name (d);
5021 tree args = TREE_VALUE (d); 4980 tree args = TREE_VALUE (d);
5296 if (!(flags & LOOKUP_HIDDEN) && is_lambda_ignored_entity (val)) 5255 if (!(flags & LOOKUP_HIDDEN) && is_lambda_ignored_entity (val))
5297 return false; 5256 return false;
5298 return true; 5257 return true;
5299 } 5258 }
5300 5259
5260 /* Is there a "using namespace std;" directive within USINGS? */
5261
5262 static bool
5263 using_directives_contain_std_p (vec<tree, va_gc> *usings)
5264 {
5265 if (!usings)
5266 return false;
5267
5268 for (unsigned ix = usings->length (); ix--;)
5269 if ((*usings)[ix] == std_node)
5270 return true;
5271
5272 return false;
5273 }
5274
5275 /* Is there a "using namespace std;" directive within the current
5276 namespace (or its ancestors)?
5277 Compare with name_lookup::search_unqualified. */
5278
5279 static bool
5280 has_using_namespace_std_directive_p ()
5281 {
5282 /* Look at local using-directives. */
5283 for (cp_binding_level *level = current_binding_level;
5284 level->kind != sk_namespace;
5285 level = level->level_chain)
5286 if (using_directives_contain_std_p (level->using_directives))
5287 return true;
5288
5289 /* Look at this namespace and its ancestors. */
5290 for (tree scope = current_namespace; scope; scope = CP_DECL_CONTEXT (scope))
5291 {
5292 if (using_directives_contain_std_p (DECL_NAMESPACE_USING (scope)))
5293 return true;
5294
5295 if (scope == global_namespace)
5296 break;
5297 }
5298
5299 return false;
5300 }
5301
5301 /* Suggest alternatives for NAME, an IDENTIFIER_NODE for which name 5302 /* Suggest alternatives for NAME, an IDENTIFIER_NODE for which name
5302 lookup failed. Search through all available namespaces and print out 5303 lookup failed. Search through all available namespaces and print out
5303 possible candidates. If no exact matches are found, and 5304 possible candidates. If no exact matches are found, and
5304 SUGGEST_MISSPELLINGS is true, then also look for near-matches and 5305 SUGGEST_MISSPELLINGS is true, then also look for near-matches and
5305 suggest the best near-match, if there is one. */ 5306 suggest the best near-match, if there is one. */
5366 tree val = candidates[ix]; 5367 tree val = candidates[ix];
5367 5368
5368 inform (location_of (val), " %qE", val); 5369 inform (location_of (val), " %qE", val);
5369 } 5370 }
5370 candidates.release (); 5371 candidates.release ();
5371 } 5372 return;
5372 else if (!suggest_misspellings) 5373 }
5373 ; 5374
5374 else if (const char *fuzzy = lookup_name_fuzzy (name, FUZZY_LOOKUP_NAME)) 5375 /* No candidates were found in the available namespaces. */
5376
5377 /* If there's a "using namespace std;" active, and this
5378 is one of the most common "std::" names, then it's probably a
5379 missing #include. */
5380 if (has_using_namespace_std_directive_p ())
5381 if (maybe_suggest_missing_std_header (location, name))
5382 return;
5383
5384 /* Otherwise, consider misspellings. */
5385 if (!suggest_misspellings)
5386 return;
5387 if (name_hint hint = lookup_name_fuzzy (name, FUZZY_LOOKUP_NAME,
5388 location))
5375 { 5389 {
5376 /* Show a spelling correction. */ 5390 /* Show a spelling correction. */
5377 gcc_rich_location richloc (location); 5391 gcc_rich_location richloc (location);
5378 5392
5379 richloc.add_fixit_replace (fuzzy); 5393 richloc.add_fixit_replace (hint.suggestion ());
5380 inform_at_rich_loc (&richloc, "suggested alternative: %qs", fuzzy); 5394 inform (&richloc, "suggested alternative: %qs", hint.suggestion ());
5381 } 5395 }
5382 } 5396 }
5397
5398 /* A well-known name within the C++ standard library, returned by
5399 get_std_name_hint. */
5400
5401 struct std_name_hint
5402 {
5403 /* A name within "std::". */
5404 const char *name;
5405
5406 /* The header name defining it within the C++ Standard Library
5407 (with '<' and '>'). */
5408 const char *header;
5409
5410 /* The dialect of C++ in which this was added. */
5411 enum cxx_dialect min_dialect;
5412 };
5383 5413
5384 /* Subroutine of maybe_suggest_missing_header for handling unrecognized names 5414 /* Subroutine of maybe_suggest_missing_header for handling unrecognized names
5385 for some of the most common names within "std::". 5415 for some of the most common names within "std::".
5386 Given non-NULL NAME, a name for lookup within "std::", return the header 5416 Given non-NULL NAME, return the std_name_hint for it, or NULL. */
5387 name defining it within the C++ Standard Library (with '<' and '>'), 5417
5388 or NULL. */ 5418 static const std_name_hint *
5389
5390 static const char *
5391 get_std_name_hint (const char *name) 5419 get_std_name_hint (const char *name)
5392 { 5420 {
5393 struct std_name_hint
5394 {
5395 const char *name;
5396 const char *header;
5397 };
5398 static const std_name_hint hints[] = { 5421 static const std_name_hint hints[] = {
5422 /* <any>. */
5423 {"any", "<any>", cxx17},
5424 {"any_cast", "<any>", cxx17},
5425 {"make_any", "<any>", cxx17},
5399 /* <array>. */ 5426 /* <array>. */
5400 {"array", "<array>"}, // C++11 5427 {"array", "<array>", cxx11},
5428 /* <atomic>. */
5429 {"atomic", "<atomic>", cxx11},
5430 {"atomic_flag", "<atomic>", cxx11},
5431 /* <bitset>. */
5432 {"bitset", "<bitset>", cxx11},
5433 /* <complex>. */
5434 {"complex", "<complex>", cxx98},
5435 {"complex_literals", "<complex>", cxx98},
5436 /* <condition_variable>. */
5437 {"condition_variable", "<condition_variable>", cxx11},
5438 {"condition_variable_any", "<condition_variable>", cxx11},
5401 /* <deque>. */ 5439 /* <deque>. */
5402 {"deque", "<deque>"}, 5440 {"deque", "<deque>", cxx98},
5403 /* <forward_list>. */ 5441 /* <forward_list>. */
5404 {"forward_list", "<forward_list>"}, // C++11 5442 {"forward_list", "<forward_list>", cxx11},
5405 /* <fstream>. */ 5443 /* <fstream>. */
5406 {"basic_filebuf", "<fstream>"}, 5444 {"basic_filebuf", "<fstream>", cxx98},
5407 {"basic_ifstream", "<fstream>"}, 5445 {"basic_ifstream", "<fstream>", cxx98},
5408 {"basic_ofstream", "<fstream>"}, 5446 {"basic_ofstream", "<fstream>", cxx98},
5409 {"basic_fstream", "<fstream>"}, 5447 {"basic_fstream", "<fstream>", cxx98},
5448 {"fstream", "<fstream>", cxx98},
5449 {"ifstream", "<fstream>", cxx98},
5450 {"ofstream", "<fstream>", cxx98},
5451 /* <functional>. */
5452 {"bind", "<functional>", cxx11},
5453 {"function", "<functional>", cxx11},
5454 {"hash", "<functional>", cxx11},
5455 {"mem_fn", "<functional>", cxx11},
5456 /* <future>. */
5457 {"async", "<future>", cxx11},
5458 {"future", "<future>", cxx11},
5459 {"packaged_task", "<future>", cxx11},
5460 {"promise", "<future>", cxx11},
5410 /* <iostream>. */ 5461 /* <iostream>. */
5411 {"cin", "<iostream>"}, 5462 {"cin", "<iostream>", cxx98},
5412 {"cout", "<iostream>"}, 5463 {"cout", "<iostream>", cxx98},
5413 {"cerr", "<iostream>"}, 5464 {"cerr", "<iostream>", cxx98},
5414 {"clog", "<iostream>"}, 5465 {"clog", "<iostream>", cxx98},
5415 {"wcin", "<iostream>"}, 5466 {"wcin", "<iostream>", cxx98},
5416 {"wcout", "<iostream>"}, 5467 {"wcout", "<iostream>", cxx98},
5417 {"wclog", "<iostream>"}, 5468 {"wclog", "<iostream>", cxx98},
5469 /* <istream>. */
5470 {"istream", "<istream>", cxx98},
5471 /* <iterator>. */
5472 {"advance", "<iterator>", cxx98},
5473 {"back_inserter", "<iterator>", cxx98},
5474 {"begin", "<iterator>", cxx11},
5475 {"distance", "<iterator>", cxx98},
5476 {"end", "<iterator>", cxx11},
5477 {"front_inserter", "<iterator>", cxx98},
5478 {"inserter", "<iterator>", cxx98},
5479 {"istream_iterator", "<iterator>", cxx98},
5480 {"istreambuf_iterator", "<iterator>", cxx98},
5481 {"iterator_traits", "<iterator>", cxx98},
5482 {"move_iterator", "<iterator>", cxx11},
5483 {"next", "<iterator>", cxx11},
5484 {"ostream_iterator", "<iterator>", cxx98},
5485 {"ostreambuf_iterator", "<iterator>", cxx98},
5486 {"prev", "<iterator>", cxx11},
5487 {"reverse_iterator", "<iterator>", cxx98},
5488 /* <ostream>. */
5489 {"ostream", "<ostream>", cxx98},
5418 /* <list>. */ 5490 /* <list>. */
5419 {"list", "<list>"}, 5491 {"list", "<list>", cxx98},
5420 /* <map>. */ 5492 /* <map>. */
5421 {"map", "<map>"}, 5493 {"map", "<map>", cxx98},
5422 {"multimap", "<map>"}, 5494 {"multimap", "<map>", cxx98},
5495 /* <memory>. */
5496 {"make_shared", "<memory>", cxx11},
5497 {"make_unique", "<memory>", cxx11},
5498 {"shared_ptr", "<memory>", cxx11},
5499 {"unique_ptr", "<memory>", cxx11},
5500 {"weak_ptr", "<memory>", cxx11},
5501 /* <mutex>. */
5502 {"mutex", "<mutex>", cxx11},
5503 {"timed_mutex", "<mutex>", cxx11},
5504 {"recursive_mutex", "<mutex>", cxx11},
5505 {"recursive_timed_mutex", "<mutex>", cxx11},
5506 {"once_flag", "<mutex>", cxx11},
5507 {"call_once,", "<mutex>", cxx11},
5508 {"lock", "<mutex>", cxx11},
5509 {"scoped_lock", "<mutex>", cxx17},
5510 {"try_lock", "<mutex>", cxx11},
5511 {"lock_guard", "<mutex>", cxx11},
5512 {"unique_lock", "<mutex>", cxx11},
5513 /* <optional>. */
5514 {"optional", "<optional>", cxx17},
5515 {"make_optional", "<optional>", cxx17},
5516 /* <ostream>. */
5517 {"ostream", "<ostream>", cxx98},
5518 {"wostream", "<ostream>", cxx98},
5519 {"ends", "<ostream>", cxx98},
5520 {"flush", "<ostream>", cxx98},
5521 {"endl", "<ostream>", cxx98},
5423 /* <queue>. */ 5522 /* <queue>. */
5424 {"queue", "<queue>"}, 5523 {"queue", "<queue>", cxx98},
5425 {"priority_queue", "<queue>"}, 5524 {"priority_queue", "<queue>", cxx98},
5426 /* <ostream>. */
5427 {"ostream", "<ostream>"},
5428 {"wostream", "<ostream>"},
5429 {"ends", "<ostream>"},
5430 {"flush", "<ostream>"},
5431 {"endl", "<ostream>"},
5432 /* <set>. */ 5525 /* <set>. */
5433 {"set", "<set>"}, 5526 {"set", "<set>", cxx98},
5434 {"multiset", "<set>"}, 5527 {"multiset", "<set>", cxx98},
5528 /* <shared_mutex>. */
5529 {"shared_lock", "<shared_mutex>", cxx14},
5530 {"shared_mutex", "<shared_mutex>", cxx17},
5531 {"shared_timed_mutex", "<shared_mutex>", cxx14},
5435 /* <sstream>. */ 5532 /* <sstream>. */
5436 {"basic_stringbuf", "<sstream>"}, 5533 {"basic_stringbuf", "<sstream>", cxx98},
5437 {"basic_istringstream", "<sstream>"}, 5534 {"basic_istringstream", "<sstream>", cxx98},
5438 {"basic_ostringstream", "<sstream>"}, 5535 {"basic_ostringstream", "<sstream>", cxx98},
5439 {"basic_stringstream", "<sstream>"}, 5536 {"basic_stringstream", "<sstream>", cxx98},
5537 {"istringstream", "<sstream>", cxx98},
5538 {"ostringstream", "<sstream>", cxx98},
5539 {"stringstream", "<sstream>", cxx98},
5440 /* <stack>. */ 5540 /* <stack>. */
5441 {"stack", "<stack>"}, 5541 {"stack", "<stack>", cxx98},
5442 /* <string>. */ 5542 /* <string>. */
5443 {"string", "<string>"}, 5543 {"basic_string", "<string>", cxx98},
5444 {"wstring", "<string>"}, 5544 {"string", "<string>", cxx98},
5445 {"u16string", "<string>"}, 5545 {"wstring", "<string>", cxx98},
5446 {"u32string", "<string>"}, 5546 {"u16string", "<string>", cxx11},
5547 {"u32string", "<string>", cxx11},
5548 /* <string_view>. */
5549 {"string_view", "<string_view>", cxx17},
5550 /* <thread>. */
5551 {"thread", "<thread>", cxx11},
5552 /* <tuple>. */
5553 {"make_tuple", "<tuple>", cxx11},
5554 {"tuple", "<tuple>", cxx11},
5555 {"tuple_element", "<tuple>", cxx11},
5556 {"tuple_size", "<tuple>", cxx11},
5447 /* <unordered_map>. */ 5557 /* <unordered_map>. */
5448 {"unordered_map", "<unordered_map>"}, // C++11 5558 {"unordered_map", "<unordered_map>", cxx11},
5449 {"unordered_multimap", "<unordered_map>"}, // C++11 5559 {"unordered_multimap", "<unordered_map>", cxx11},
5450 /* <unordered_set>. */ 5560 /* <unordered_set>. */
5451 {"unordered_set", "<unordered_set>"}, // C++11 5561 {"unordered_set", "<unordered_set>", cxx11},
5452 {"unordered_multiset", "<unordered_set>"}, // C++11 5562 {"unordered_multiset", "<unordered_set>", cxx11},
5563 /* <utility>. */
5564 {"declval", "<utility>", cxx11},
5565 {"forward", "<utility>", cxx11},
5566 {"make_pair", "<utility>", cxx98},
5567 {"move", "<utility>", cxx11},
5568 {"pair", "<utility>", cxx98},
5569 /* <variant>. */
5570 {"variant", "<variant>", cxx17},
5571 {"visit", "<variant>", cxx17},
5453 /* <vector>. */ 5572 /* <vector>. */
5454 {"vector", "<vector>"}, 5573 {"vector", "<vector>", cxx98},
5455 }; 5574 };
5456 const size_t num_hints = sizeof (hints) / sizeof (hints[0]); 5575 const size_t num_hints = sizeof (hints) / sizeof (hints[0]);
5457 for (size_t i = 0; i < num_hints; i++) 5576 for (size_t i = 0; i < num_hints; i++)
5458 { 5577 {
5459 if (0 == strcmp (name, hints[i].name)) 5578 if (strcmp (name, hints[i].name) == 0)
5460 return hints[i].header; 5579 return &hints[i];
5461 } 5580 }
5462 return NULL; 5581 return NULL;
5582 }
5583
5584 /* Describe DIALECT. */
5585
5586 static const char *
5587 get_cxx_dialect_name (enum cxx_dialect dialect)
5588 {
5589 switch (dialect)
5590 {
5591 default:
5592 gcc_unreachable ();
5593 case cxx98:
5594 return "C++98";
5595 case cxx11:
5596 return "C++11";
5597 case cxx14:
5598 return "C++14";
5599 case cxx17:
5600 return "C++17";
5601 case cxx2a:
5602 return "C++2a";
5603 }
5604 }
5605
5606 /* Suggest pertinent header files for NAME at LOCATION, for common
5607 names within the "std" namespace.
5608 Return true iff a suggestion was offered. */
5609
5610 static bool
5611 maybe_suggest_missing_std_header (location_t location, tree name)
5612 {
5613 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
5614
5615 const char *name_str = IDENTIFIER_POINTER (name);
5616 const std_name_hint *header_hint = get_std_name_hint (name_str);
5617 if (!header_hint)
5618 return false;
5619
5620 gcc_rich_location richloc (location);
5621 if (cxx_dialect >= header_hint->min_dialect)
5622 {
5623 const char *header = header_hint->header;
5624 maybe_add_include_fixit (&richloc, header, true);
5625 inform (&richloc,
5626 "%<std::%s%> is defined in header %qs;"
5627 " did you forget to %<#include %s%>?",
5628 name_str, header, header);
5629 }
5630 else
5631 {
5632 inform (&richloc,
5633 "%<std::%s%> is only available from %s onwards",
5634 name_str, get_cxx_dialect_name (header_hint->min_dialect));
5635 }
5636 return true;
5463 } 5637 }
5464 5638
5465 /* If SCOPE is the "std" namespace, then suggest pertinent header 5639 /* If SCOPE is the "std" namespace, then suggest pertinent header
5466 files for NAME at LOCATION. 5640 files for NAME at LOCATION.
5467 Return true iff a suggestion was offered. */ 5641 Return true iff a suggestion was offered. */
5474 if (TREE_CODE (scope) != NAMESPACE_DECL) 5648 if (TREE_CODE (scope) != NAMESPACE_DECL)
5475 return false; 5649 return false;
5476 /* We only offer suggestions for the "std" namespace. */ 5650 /* We only offer suggestions for the "std" namespace. */
5477 if (scope != std_node) 5651 if (scope != std_node)
5478 return false; 5652 return false;
5479 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE); 5653 return maybe_suggest_missing_std_header (location, name);
5480
5481 const char *name_str = IDENTIFIER_POINTER (name);
5482 const char *header_hint = get_std_name_hint (name_str);
5483 if (!header_hint)
5484 return false;
5485
5486 gcc_rich_location richloc (location);
5487 maybe_add_include_fixit (&richloc, header_hint);
5488 inform_at_rich_loc (&richloc,
5489 "%<std::%s%> is defined in header %qs;"
5490 " did you forget to %<#include %s%>?",
5491 name_str, header_hint, header_hint);
5492 return true;
5493 } 5654 }
5494 5655
5495 /* Look for alternatives for NAME, an IDENTIFIER_NODE for which name 5656 /* Look for alternatives for NAME, an IDENTIFIER_NODE for which name
5496 lookup failed within the explicitly provided SCOPE. Suggest the 5657 lookup failed within the explicitly provided SCOPE. Suggest the
5497 the best meaningful candidates (if any) as a fix-it hint. 5658 the best meaningful candidates (if any) as a fix-it hint.
5499 5660
5500 bool 5661 bool
5501 suggest_alternative_in_explicit_scope (location_t location, tree name, 5662 suggest_alternative_in_explicit_scope (location_t location, tree name,
5502 tree scope) 5663 tree scope)
5503 { 5664 {
5665 /* Something went very wrong; don't suggest anything. */
5666 if (name == error_mark_node)
5667 return false;
5668
5504 /* Resolve any namespace aliases. */ 5669 /* Resolve any namespace aliases. */
5505 scope = ORIGINAL_NAMESPACE (scope); 5670 scope = ORIGINAL_NAMESPACE (scope);
5506 5671
5507 if (maybe_suggest_missing_header (location, name, scope)) 5672 if (maybe_suggest_missing_header (location, name, scope))
5508 return true; 5673 return true;
5516 const char *fuzzy_name = bm.get_best_meaningful_candidate (); 5681 const char *fuzzy_name = bm.get_best_meaningful_candidate ();
5517 if (fuzzy_name) 5682 if (fuzzy_name)
5518 { 5683 {
5519 gcc_rich_location richloc (location); 5684 gcc_rich_location richloc (location);
5520 richloc.add_fixit_replace (fuzzy_name); 5685 richloc.add_fixit_replace (fuzzy_name);
5521 inform_at_rich_loc (&richloc, "suggested alternative: %qs", 5686 inform (&richloc, "suggested alternative: %qs",
5522 fuzzy_name); 5687 fuzzy_name);
5523 return true; 5688 return true;
5524 } 5689 }
5525 5690
5526 return false; 5691 return false;
5527 } 5692 }
5595 = lookup_member_fuzzy (type, name, want_type_p); 5760 = lookup_member_fuzzy (type, name, want_type_p);
5596 if (best_matching_field) 5761 if (best_matching_field)
5597 bm.consider (IDENTIFIER_POINTER (best_matching_field)); 5762 bm.consider (IDENTIFIER_POINTER (best_matching_field));
5598 } 5763 }
5599 5764
5765 /* Only suggest names reserved for the implementation if NAME begins
5766 with an underscore. */
5767 bool consider_implementation_names = (IDENTIFIER_POINTER (name)[0] == '_');
5768
5600 for (tree t = lvl->names; t; t = TREE_CHAIN (t)) 5769 for (tree t = lvl->names; t; t = TREE_CHAIN (t))
5601 { 5770 {
5602 tree d = t; 5771 tree d = t;
5603 5772
5604 /* OVERLOADs or decls from using declaration are wrapped into 5773 /* OVERLOADs or decls from using declaration are wrapped into
5611 if (TREE_TYPE (d) == error_mark_node) 5780 if (TREE_TYPE (d) == error_mark_node)
5612 continue; 5781 continue;
5613 5782
5614 /* Skip anticipated decls of builtin functions. */ 5783 /* Skip anticipated decls of builtin functions. */
5615 if (TREE_CODE (d) == FUNCTION_DECL 5784 if (TREE_CODE (d) == FUNCTION_DECL
5616 && DECL_BUILT_IN (d) 5785 && fndecl_built_in_p (d)
5617 && DECL_ANTICIPATED (d)) 5786 && DECL_ANTICIPATED (d))
5618 continue; 5787 continue;
5619 5788
5620 if (tree name = DECL_NAME (d)) 5789 /* Skip compiler-generated variables (e.g. __for_begin/__for_end
5621 /* Ignore internal names with spaces in them. */ 5790 within range for). */
5622 if (!strchr (IDENTIFIER_POINTER (name), ' ')) 5791 if (TREE_CODE (d) == VAR_DECL
5623 bm.consider (IDENTIFIER_POINTER (name)); 5792 && DECL_ARTIFICIAL (d))
5793 continue;
5794
5795 tree suggestion = DECL_NAME (d);
5796 if (!suggestion)
5797 continue;
5798
5799 /* Don't suggest names that are for anonymous aggregate types, as
5800 they are an implementation detail generated by the compiler. */
5801 if (anon_aggrname_p (suggestion))
5802 continue;
5803
5804 const char *suggestion_str = IDENTIFIER_POINTER (suggestion);
5805
5806 /* Ignore internal names with spaces in them. */
5807 if (strchr (suggestion_str, ' '))
5808 continue;
5809
5810 /* Don't suggest names that are reserved for use by the
5811 implementation, unless NAME began with an underscore. */
5812 if (name_reserved_for_implementation_p (suggestion_str)
5813 && !consider_implementation_names)
5814 continue;
5815
5816 bm.consider (suggestion_str);
5817 }
5818 }
5819
5820 /* Subclass of deferred_diagnostic. Notify the user that the
5821 given macro was used before it was defined.
5822 This can be done in the C++ frontend since tokenization happens
5823 upfront. */
5824
5825 class macro_use_before_def : public deferred_diagnostic
5826 {
5827 public:
5828 /* Factory function. Return a new macro_use_before_def instance if
5829 appropriate, or return NULL. */
5830 static macro_use_before_def *
5831 maybe_make (location_t use_loc, cpp_hashnode *macro)
5832 {
5833 source_location def_loc = cpp_macro_definition_location (macro);
5834 if (def_loc == UNKNOWN_LOCATION)
5835 return NULL;
5836
5837 /* We only want to issue a note if the macro was used *before* it was
5838 defined.
5839 We don't want to issue a note for cases where a macro was incorrectly
5840 used, leaving it unexpanded (e.g. by using the wrong argument
5841 count). */
5842 if (!linemap_location_before_p (line_table, use_loc, def_loc))
5843 return NULL;
5844
5845 return new macro_use_before_def (use_loc, macro);
5846 }
5847
5848 private:
5849 /* Ctor. LOC is the location of the usage. MACRO is the
5850 macro that was used. */
5851 macro_use_before_def (location_t loc, cpp_hashnode *macro)
5852 : deferred_diagnostic (loc), m_macro (macro)
5853 {
5854 gcc_assert (macro);
5855 }
5856
5857 ~macro_use_before_def ()
5858 {
5859 if (is_suppressed_p ())
5860 return;
5861
5862 inform (get_location (), "the macro %qs had not yet been defined",
5863 (const char *)m_macro->ident.str);
5864 inform (cpp_macro_definition_location (m_macro),
5865 "it was later defined here");
5866 }
5867
5868 private:
5869 cpp_hashnode *m_macro;
5870 };
5871
5872 /* Determine if it can ever make sense to offer RID as a suggestion for
5873 a misspelling.
5874
5875 Subroutine of lookup_name_fuzzy. */
5876
5877 static bool
5878 suggest_rid_p (enum rid rid)
5879 {
5880 switch (rid)
5881 {
5882 /* Support suggesting function-like keywords. */
5883 case RID_STATIC_ASSERT:
5884 return true;
5885
5886 default:
5887 /* Support suggesting the various decl-specifier words, to handle
5888 e.g. "singed" vs "signed" typos. */
5889 if (cp_keyword_starts_decl_specifier_p (rid))
5890 return true;
5891
5892 /* Otherwise, don't offer it. This avoids suggesting e.g. "if"
5893 and "do" for short misspellings, which are likely to lead to
5894 nonsensical results. */
5895 return false;
5624 } 5896 }
5625 } 5897 }
5626 5898
5627 /* Search for near-matches for NAME within the current bindings, and within 5899 /* Search for near-matches for NAME within the current bindings, and within
5628 macro names, returning the best match as a const char *, or NULL if 5900 macro names, returning the best match as a const char *, or NULL if
5629 no reasonable match is found. */ 5901 no reasonable match is found.
5630 5902
5631 const char * 5903 Use LOC for any deferred diagnostics. */
5632 lookup_name_fuzzy (tree name, enum lookup_name_fuzzy_kind kind) 5904
5905 name_hint
5906 lookup_name_fuzzy (tree name, enum lookup_name_fuzzy_kind kind, location_t loc)
5633 { 5907 {
5634 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE); 5908 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
5909
5910 /* First, try some well-known names in the C++ standard library, in case
5911 the user forgot a #include. */
5912 const char *header_hint
5913 = get_cp_stdlib_header_for_name (IDENTIFIER_POINTER (name));
5914 if (header_hint)
5915 return name_hint (NULL,
5916 new suggest_missing_header (loc,
5917 IDENTIFIER_POINTER (name),
5918 header_hint));
5635 5919
5636 best_match <tree, const char *> bm (name); 5920 best_match <tree, const char *> bm (name);
5637 5921
5638 cp_binding_level *lvl; 5922 cp_binding_level *lvl;
5639 for (lvl = scope_chain->class_bindings; lvl; lvl = lvl->level_chain) 5923 for (lvl = scope_chain->class_bindings; lvl; lvl = lvl->level_chain)
5657 best_macro_match bmm (name, bm.get_best_distance (), parse_in); 5941 best_macro_match bmm (name, bm.get_best_distance (), parse_in);
5658 cpp_hashnode *best_macro = bmm.get_best_meaningful_candidate (); 5942 cpp_hashnode *best_macro = bmm.get_best_meaningful_candidate ();
5659 /* If a macro is the closest so far to NAME, consider it. */ 5943 /* If a macro is the closest so far to NAME, consider it. */
5660 if (best_macro) 5944 if (best_macro)
5661 bm.consider ((const char *)best_macro->ident.str); 5945 bm.consider ((const char *)best_macro->ident.str);
5946 else if (bmm.get_best_distance () == 0)
5947 {
5948 /* If we have an exact match for a macro name, then either the
5949 macro was used with the wrong argument count, or the macro
5950 has been used before it was defined. */
5951 if (cpp_hashnode *macro = bmm.blithely_get_best_candidate ())
5952 if (cpp_user_macro_p (macro))
5953 return name_hint (NULL,
5954 macro_use_before_def::maybe_make (loc, macro));
5955 }
5662 5956
5663 /* Try the "starts_decl_specifier_p" keywords to detect 5957 /* Try the "starts_decl_specifier_p" keywords to detect
5664 "singed" vs "signed" typos. */ 5958 "singed" vs "signed" typos. */
5665 for (unsigned i = 0; i < num_c_common_reswords; i++) 5959 for (unsigned i = 0; i < num_c_common_reswords; i++)
5666 { 5960 {
5667 const c_common_resword *resword = &c_common_reswords[i]; 5961 const c_common_resword *resword = &c_common_reswords[i];
5668 5962
5669 if (kind == FUZZY_LOOKUP_TYPENAME) 5963 if (!suggest_rid_p (resword->rid))
5670 if (!cp_keyword_starts_decl_specifier_p (resword->rid)) 5964 continue;
5671 continue;
5672 5965
5673 tree resword_identifier = ridpointers [resword->rid]; 5966 tree resword_identifier = ridpointers [resword->rid];
5674 if (!resword_identifier) 5967 if (!resword_identifier)
5675 continue; 5968 continue;
5676 gcc_assert (TREE_CODE (resword_identifier) == IDENTIFIER_NODE); 5969 gcc_assert (TREE_CODE (resword_identifier) == IDENTIFIER_NODE);
5681 continue; 5974 continue;
5682 5975
5683 bm.consider (IDENTIFIER_POINTER (resword_identifier)); 5976 bm.consider (IDENTIFIER_POINTER (resword_identifier));
5684 } 5977 }
5685 5978
5686 return bm.get_best_meaningful_candidate (); 5979 return name_hint (bm.get_best_meaningful_candidate (), NULL);
5687 } 5980 }
5688 5981
5689 /* Subroutine of outer_binding. 5982 /* Subroutine of outer_binding.
5690 5983
5691 Returns TRUE if BINDING is a binding to a template parameter of 5984 Returns TRUE if BINDING is a binding to a template parameter of
6229 do_pushtag (tree name, tree type, tag_scope scope) 6522 do_pushtag (tree name, tree type, tag_scope scope)
6230 { 6523 {
6231 tree decl; 6524 tree decl;
6232 6525
6233 cp_binding_level *b = current_binding_level; 6526 cp_binding_level *b = current_binding_level;
6234 while (/* Cleanup scopes are not scopes from the point of view of 6527 while (true)
6235 the language. */ 6528 {
6236 b->kind == sk_cleanup 6529 if (/* Cleanup scopes are not scopes from the point of view of
6237 /* Neither are function parameter scopes. */ 6530 the language. */
6238 || b->kind == sk_function_parms 6531 b->kind == sk_cleanup
6239 /* Neither are the scopes used to hold template parameters 6532 /* Neither are function parameter scopes. */
6240 for an explicit specialization. For an ordinary template 6533 || b->kind == sk_function_parms
6241 declaration, these scopes are not scopes from the point of 6534 /* Neither are the scopes used to hold template parameters
6242 view of the language. */ 6535 for an explicit specialization. For an ordinary template
6243 || (b->kind == sk_template_parms 6536 declaration, these scopes are not scopes from the point of
6244 && (b->explicit_spec_p || scope == ts_global)) 6537 view of the language. */
6245 /* Pushing into a class is ok for lambdas or when we want current */ 6538 || (b->kind == sk_template_parms
6246 || (b->kind == sk_class 6539 && (b->explicit_spec_p || scope == ts_global)))
6247 && scope != ts_lambda 6540 b = b->level_chain;
6248 && (scope != ts_current 6541 else if (b->kind == sk_class
6249 /* We may be defining a new type in the initializer 6542 && scope != ts_current)
6250 of a static member variable. We allow this when 6543 {
6251 not pedantic, and it is particularly useful for 6544 b = b->level_chain;
6252 type punning via an anonymous union. */ 6545 if (b->kind == sk_template_parms)
6253 || COMPLETE_TYPE_P (b->this_entity)))) 6546 b = b->level_chain;
6254 b = b->level_chain; 6547 }
6548 else
6549 break;
6550 }
6255 6551
6256 gcc_assert (identifier_p (name)); 6552 gcc_assert (identifier_p (name));
6257 6553
6258 /* Do C++ gratuitous typedefing. */ 6554 /* Do C++ gratuitous typedefing. */
6259 if (identifier_type_value_1 (name) != type) 6555 if (identifier_type_value_1 (name) != type)
6262 int in_class = 0; 6558 int in_class = 0;
6263 tree context = TYPE_CONTEXT (type); 6559 tree context = TYPE_CONTEXT (type);
6264 6560
6265 if (! context) 6561 if (! context)
6266 { 6562 {
6267 tree cs = current_scope (); 6563 cp_binding_level *cb = b;
6564 while (cb->kind != sk_namespace
6565 && cb->kind != sk_class
6566 && (cb->kind != sk_function_parms
6567 || !cb->this_entity))
6568 cb = cb->level_chain;
6569 tree cs = cb->this_entity;
6570
6571 gcc_checking_assert (TREE_CODE (cs) == FUNCTION_DECL
6572 ? cs == current_function_decl
6573 : TYPE_P (cs) ? cs == current_class_type
6574 : cs == current_namespace);
6268 6575
6269 if (scope == ts_current 6576 if (scope == ts_current
6270 || scope == ts_lambda
6271 || (cs && TREE_CODE (cs) == FUNCTION_DECL)) 6577 || (cs && TREE_CODE (cs) == FUNCTION_DECL))
6272 context = cs; 6578 context = cs;
6273 else if (cs && TYPE_P (cs)) 6579 else if (cs && TYPE_P (cs))
6274 /* When declaring a friend class of a local class, we want 6580 /* When declaring a friend class of a local class, we want
6275 to inject the newly named class into the scope 6581 to inject the newly named class into the scope
6302 if (decl == error_mark_node) 6608 if (decl == error_mark_node)
6303 return decl; 6609 return decl;
6304 6610
6305 if (b->kind == sk_class) 6611 if (b->kind == sk_class)
6306 { 6612 {
6307 if (!TYPE_BEING_DEFINED (current_class_type) 6613 if (!TYPE_BEING_DEFINED (current_class_type))
6308 && scope != ts_lambda) 6614 /* Don't push anywhere if the class is complete; a lambda in an
6309 return error_mark_node; 6615 NSDMI is not a member of the class. */
6310 6616 ;
6311 if (!PROCESSING_REAL_TEMPLATE_DECL_P ()) 6617 else if (!PROCESSING_REAL_TEMPLATE_DECL_P ())
6312 /* Put this TYPE_DECL on the TYPE_FIELDS list for the 6618 /* Put this TYPE_DECL on the TYPE_FIELDS list for the
6313 class. But if it's a member template class, we want 6619 class. But if it's a member template class, we want
6314 the TEMPLATE_DECL, not the TYPE_DECL, so this is done 6620 the TEMPLATE_DECL, not the TYPE_DECL, so this is done
6315 later. */ 6621 later. */
6316 finish_member_declaration (decl); 6622 finish_member_declaration (decl);
6325 6631
6326 if (DECL_CONTEXT (decl) == std_node 6632 if (DECL_CONTEXT (decl) == std_node
6327 && init_list_identifier == DECL_NAME (TYPE_NAME (type)) 6633 && init_list_identifier == DECL_NAME (TYPE_NAME (type))
6328 && !CLASSTYPE_TEMPLATE_INFO (type)) 6634 && !CLASSTYPE_TEMPLATE_INFO (type))
6329 { 6635 {
6330 error ("declaration of std::initializer_list does not match " 6636 error ("declaration of %<std::initializer_list%> does not match "
6331 "#include <initializer_list>, isn't a template"); 6637 "%<#include <initializer_list>%>, isn't a template");
6332 return error_mark_node; 6638 return error_mark_node;
6333 } 6639 }
6334 } 6640 }
6335 6641
6336 if (! in_class) 6642 if (! in_class)
6350 { 6656 {
6351 /* Push a DECL_EXPR so we call pushtag at the right time in 6657 /* Push a DECL_EXPR so we call pushtag at the right time in
6352 template instantiation rather than in some nested context. */ 6658 template instantiation rather than in some nested context. */
6353 add_decl_expr (decl); 6659 add_decl_expr (decl);
6354 } 6660 }
6355 else 6661 /* Lambdas use LAMBDA_EXPR_DISCRIMINATOR instead. */
6662 else if (!LAMBDA_TYPE_P (type))
6356 vec_safe_push (local_classes, type); 6663 vec_safe_push (local_classes, type);
6357 } 6664 }
6358 } 6665 }
6359 6666
6360 if (b->kind == sk_class 6667 if (b->kind == sk_class
6568 s->inhibit_evaluation_warnings = c_inhibit_evaluation_warnings; 6875 s->inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
6569 s->x_stmt_tree.stmts_are_full_exprs_p = true; 6876 s->x_stmt_tree.stmts_are_full_exprs_p = true;
6570 6877
6571 scope_chain = s; 6878 scope_chain = s;
6572 current_function_decl = NULL_TREE; 6879 current_function_decl = NULL_TREE;
6573 vec_alloc (current_lang_base, 10); 6880 current_lang_base = NULL;
6574 current_lang_name = lang_name_cplusplus; 6881 current_lang_name = lang_name_cplusplus;
6575 current_namespace = global_namespace; 6882 current_namespace = global_namespace;
6576 push_class_stack (); 6883 push_class_stack ();
6577 cp_unevaluated_operand = 0; 6884 cp_unevaluated_operand = 0;
6578 c_inhibit_evaluation_warnings = 0; 6885 c_inhibit_evaluation_warnings = 0;
6588 /* Clear out class-level bindings cache. */ 6895 /* Clear out class-level bindings cache. */
6589 if (previous_class_level) 6896 if (previous_class_level)
6590 invalidate_class_lookup_cache (); 6897 invalidate_class_lookup_cache ();
6591 pop_class_stack (); 6898 pop_class_stack ();
6592 6899
6593 current_lang_base = 0; 6900 release_tree_vector (current_lang_base);
6594 6901
6595 scope_chain = s->prev; 6902 scope_chain = s->prev;
6596 FOR_EACH_VEC_SAFE_ELT (s->old_bindings, i, saved) 6903 FOR_EACH_VEC_SAFE_ELT (s->old_bindings, i, saved)
6597 { 6904 {
6598 tree id = saved->identifier; 6905 tree id = saved->identifier;
6956 7263
6957 /* Ignore this FUNCTION_DECL if it refers to a builtin declaration 7264 /* Ignore this FUNCTION_DECL if it refers to a builtin declaration
6958 of a builtin function. */ 7265 of a builtin function. */
6959 if (TREE_CODE (t) == FUNCTION_DECL 7266 if (TREE_CODE (t) == FUNCTION_DECL
6960 && DECL_EXTERNAL (t) 7267 && DECL_EXTERNAL (t)
6961 && DECL_BUILT_IN (t)) 7268 && fndecl_built_in_p (t))
6962 return; 7269 return;
6963 7270
6964 /* Do not supply context to imported_module_or_decl, if 7271 /* Do not supply context to imported_module_or_decl, if
6965 it is a global namespace. */ 7272 it is a global namespace. */
6966 if (context == global_namespace) 7273 if (context == global_namespace)