|
30 | 30 | //! binding that cannot be placed is dropped, and the resulting gap is visible to a consumer — which is better |
31 | 31 | //! than a binding in the wrong scope, and much better than a panic in an editor. |
32 | 32 |
|
33 | | -use cpp_parser::{CppAstNode, CppSyntaxKind, CppSyntaxNode, CppTokenKind, MacroBodies}; |
| 33 | +use cpp_parser::{ |
| 34 | + CppAstNode, CppAstToken, CppSyntaxKind, CppSyntaxNode, CppTokenKind, MacroBodies, |
| 35 | +}; |
34 | 36 |
|
35 | 37 | use crate::summary::MacroScopeReading; |
36 | 38 |
|
@@ -1462,6 +1464,37 @@ impl ScopeWalker<'_> { |
1462 | 1464 | } |
1463 | 1465 | } |
1464 | 1466 |
|
| 1467 | + // **A function whose name the specifier sequence holds** — `explicit A(int x) : x_(x) {}` is the shape: |
| 1468 | + // the declarator is `(int x)` and names nothing, so the loop above bound the parameters and never the |
| 1469 | + // function. `CppDeclaration::get_name` reads the name (`305a927` added the arm), and this is the binding that |
| 1470 | + // makes it a **fact**: measured on `struct A { explicit A(int x) : x_(x) {} A() {} int f(); }`, `inspect facts` |
| 1471 | + // listed `A` at 4:5 and nothing at 2:5, so `A::` offered one of the class's two constructors and a hover on the |
| 1472 | + // decorated one answered nothing — the decorator was invisible in the member list while `A()` was there. |
| 1473 | + // |
| 1474 | + // Guarded by "no declarator names anything", which is what keeps every ordinary function away from this: its |
| 1475 | + // declarator holds the name, so the loop already bound it. |
| 1476 | + if declarators(node) |
| 1477 | + .iter() |
| 1478 | + .all(|declarator| declared_name(declarator).is_none()) |
| 1479 | + && let Some(declaration) = cpp_parser::CppDeclaration::cast(node.clone()) |
| 1480 | + && declaration.get_param_list().is_some() |
| 1481 | + && let Some(token) = declaration.get_name() |
| 1482 | + { |
| 1483 | + let text = token.get_name_text().to_string(); |
| 1484 | + if let Some(name) = name_from_text(&text, node) { |
| 1485 | + // **The kind is the one the declaration is**, not `Function`: a constructor's `DeclKind` and its |
| 1486 | + // `parameter_list` read are both decided by this, and `Function` made the fact say `A` with no |
| 1487 | + // parameters — the fact existed and disagreed with its own declaration about what it was. |
| 1488 | + self.bind( |
| 1489 | + inner, |
| 1490 | + name, |
| 1491 | + BindingKind::Constructor, |
| 1492 | + node, |
| 1493 | + cpp_parser::source_range(token.get_range()), |
| 1494 | + ); |
| 1495 | + } |
| 1496 | + } |
| 1497 | + |
1465 | 1498 | // The body of a function, which the grammar puts beside the declarator rather than inside it. Walking |
1466 | 1499 | // it into the scope the declarator opened is what makes a local variable local. |
1467 | 1500 | // **A lambda written in a declarator's own subtree.** `declarator_as` reads a declarator for its name |
@@ -2815,6 +2848,28 @@ fn declared_name(node: &CppSyntaxNode) -> Option<(Name, cpp_parser::SourceRange) |
2815 | 2848 | } |
2816 | 2849 | } |
2817 | 2850 |
|
| 2851 | + // **A name the declarator does not hold** — a constructor written with a specifier (`explicit A(int x)`) or a |
| 2852 | + // destructor (`virtual ~A()`), where the parser keeps the name in the specifier sequence and the declarator is |
| 2853 | + // only `(int x)`. |
| 2854 | + // |
| 2855 | + // [`cpp_parser::CppDeclaration::get_name`] is the reader that knows the arm; asking it here is what makes the |
| 2856 | + // **fact** exist, not just the `missing-return` rule that already asks it. Measured before this arm on |
| 2857 | + // `struct A { explicit A(int x) : x_(x) {} virtual ~A() {} A() {} }`: the parameter `x` was a fact and the |
| 2858 | + // decorated constructor was not, so `A::` offered one of its two constructors and a hover on the declaration |
| 2859 | + // answered nothing. The decorator was invisible in the member list while `A()` was there — a difference no reader |
| 2860 | + // of the class can explain. |
| 2861 | + // |
| 2862 | + // Asked **last**, so every declaration whose declarator names it is unchanged, and only for a declaration: a |
| 2863 | + // declarator and a parameter reach this function too. |
| 2864 | + if let Some(declaration) = cpp_parser::CppDeclaration::cast(node.clone()) |
| 2865 | + && let Some(token) = declaration.get_name() |
| 2866 | + { |
| 2867 | + let text = token.get_name_text().to_string(); |
| 2868 | + if let Some(name) = name_from_text(&text, node) { |
| 2869 | + return Some((name, cpp_parser::source_range(token.get_range()))); |
| 2870 | + } |
| 2871 | + } |
| 2872 | + |
2818 | 2873 | // A node that names something with a direct token has no `NameExpr` to walk: an enumerator is |
2819 | 2874 | // `EnumeratorDecl["Red"]` and a label is `LabelStat["done" ":"]`. Neither can hold an initializer, so the |
2820 | 2875 | // token is unambiguous here in a way it would not be further down the tree. |
|
0 commit comments